blob: 6132e16ac8d188b325a3532b2cccf4326d0bffb7 [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);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500141static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "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{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200373 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyObject *threading, *result;
375 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (!gil_created())
378 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 recreate_gil();
380 pending_lock = PyThread_allocate_lock();
381 take_gil(tstate);
382 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 /* Update the threading module with the new state.
385 */
386 tstate = PyThreadState_GET();
387 threading = PyMapping_GetItemString(tstate->interp->modules,
388 "threading");
389 if (threading == NULL) {
390 /* threading not imported */
391 PyErr_Clear();
392 return;
393 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200394 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (result == NULL)
396 PyErr_WriteUnraisable(threading);
397 else
398 Py_DECREF(result);
399 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000400}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000401
402#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000403static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000404static int pending_async_exc = 0;
405#endif /* WITH_THREAD */
406
407/* This function is used to signal that async exceptions are waiting to be
408 raised, therefore it is also useful in non-threaded builds. */
409
410void
411_PyEval_SignalAsyncExc(void)
412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000414}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000415
Guido van Rossumff4949e1992-08-05 19:58:53 +0000416/* Functions save_thread and restore_thread are always defined so
417 dynamically loaded modules needn't be compiled separately for use
418 with and without threads: */
419
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000420PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 PyThreadState *tstate = PyThreadState_Swap(NULL);
424 if (tstate == NULL)
425 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000426#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (gil_created())
428 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000429#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431}
432
433void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000434PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (tstate == NULL)
437 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000438#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (gil_created()) {
440 int err = errno;
441 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200442 /* _Py_Finalizing is protected by the GIL */
443 if (_Py_Finalizing && tstate != _Py_Finalizing) {
444 drop_gil(tstate);
445 PyThread_exit_thread();
446 assert(0); /* unreachable */
447 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 errno = err;
449 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000450#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452}
453
454
Guido van Rossuma9672091994-09-14 13:31:22 +0000455/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
456 signal handlers or Mac I/O completion routines) can schedule calls
457 to a function to be called synchronously.
458 The synchronous function is called with one void* argument.
459 It should return 0 for success or -1 for failure -- failure should
460 be accompanied by an exception.
461
462 If registry succeeds, the registry function returns 0; if it fails
463 (e.g. due to too many pending calls) it returns -1 (without setting
464 an exception condition).
465
466 Note that because registry may occur from within signal handlers,
467 or other asynchronous events, calling malloc() is unsafe!
468
469#ifdef WITH_THREAD
470 Any thread can schedule pending calls, but only the main thread
471 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000472 There is no facility to schedule calls to a particular thread, but
473 that should be easy to change, should that ever be required. In
474 that case, the static variables here should go into the python
475 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000476#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000478
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479#ifdef WITH_THREAD
480
481/* The WITH_THREAD implementation is thread-safe. It allows
482 scheduling to be made from any thread, and even from an executing
483 callback.
484 */
485
486#define NPENDINGCALLS 32
487static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 int (*func)(void *);
489 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000490} pendingcalls[NPENDINGCALLS];
491static int pendingfirst = 0;
492static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000493
494int
495Py_AddPendingCall(int (*func)(void *), void *arg)
496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 int i, j, result=0;
498 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 /* try a few times for the lock. Since this mechanism is used
501 * for signal handling (on the main thread), there is a (slim)
502 * chance that a signal is delivered on the same thread while we
503 * hold the lock during the Py_MakePendingCalls() function.
504 * This avoids a deadlock in that case.
505 * Note that signals can be delivered on any thread. In particular,
506 * on Windows, a SIGINT is delivered on a system-created worker
507 * thread.
508 * We also check for lock being NULL, in the unlikely case that
509 * this function is called before any bytecode evaluation takes place.
510 */
511 if (lock != NULL) {
512 for (i = 0; i<100; i++) {
513 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
514 break;
515 }
516 if (i == 100)
517 return -1;
518 }
519
520 i = pendinglast;
521 j = (i + 1) % NPENDINGCALLS;
522 if (j == pendingfirst) {
523 result = -1; /* Queue full */
524 } else {
525 pendingcalls[i].func = func;
526 pendingcalls[i].arg = arg;
527 pendinglast = j;
528 }
529 /* signal main loop */
530 SIGNAL_PENDING_CALLS();
531 if (lock != NULL)
532 PyThread_release_lock(lock);
533 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000534}
535
536int
537Py_MakePendingCalls(void)
538{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200539 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 int i;
541 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 if (!pending_lock) {
544 /* initial allocation of the lock */
545 pending_lock = PyThread_allocate_lock();
546 if (pending_lock == NULL)
547 return -1;
548 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 /* only service pending calls on main thread */
551 if (main_thread && PyThread_get_thread_ident() != main_thread)
552 return 0;
553 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200554 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200556 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* perform a bounded number of calls, in case of recursion */
558 for (i=0; i<NPENDINGCALLS; i++) {
559 int j;
560 int (*func)(void *);
561 void *arg = NULL;
562
563 /* pop one item off the queue while holding the lock */
564 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
565 j = pendingfirst;
566 if (j == pendinglast) {
567 func = NULL; /* Queue empty */
568 } else {
569 func = pendingcalls[j].func;
570 arg = pendingcalls[j].arg;
571 pendingfirst = (j + 1) % NPENDINGCALLS;
572 }
573 if (pendingfirst != pendinglast)
574 SIGNAL_PENDING_CALLS();
575 else
576 UNSIGNAL_PENDING_CALLS();
577 PyThread_release_lock(pending_lock);
578 /* having released the lock, perform the callback */
579 if (func == NULL)
580 break;
581 r = func(arg);
582 if (r)
583 break;
584 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200585 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000587}
588
589#else /* if ! defined WITH_THREAD */
590
591/*
592 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
593 This code is used for signal handling in python that isn't built
594 with WITH_THREAD.
595 Don't use this implementation when Py_AddPendingCalls() can happen
596 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597
Guido van Rossuma9672091994-09-14 13:31:22 +0000598 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000599 (1) nested asynchronous calls to Py_AddPendingCall()
600 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000602 (1) is very unlikely because typically signal delivery
603 is blocked during signal handling. So it should be impossible.
604 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000605 The current code is safe against (2), but not against (1).
606 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000607 thread is present, interrupted by signals, and that the critical
608 section is protected with the "busy" variable. On Windows, which
609 delivers SIGINT on a system thread, this does not hold and therefore
610 Windows really shouldn't use this version.
611 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000612*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000613
Guido van Rossuma9672091994-09-14 13:31:22 +0000614#define NPENDINGCALLS 32
615static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 int (*func)(void *);
617 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000618} pendingcalls[NPENDINGCALLS];
619static volatile int pendingfirst = 0;
620static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000621static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000622
623int
Thomas Wouters334fb892000-07-25 12:56:38 +0000624Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 static volatile int busy = 0;
627 int i, j;
628 /* XXX Begin critical section */
629 if (busy)
630 return -1;
631 busy = 1;
632 i = pendinglast;
633 j = (i + 1) % NPENDINGCALLS;
634 if (j == pendingfirst) {
635 busy = 0;
636 return -1; /* Queue full */
637 }
638 pendingcalls[i].func = func;
639 pendingcalls[i].arg = arg;
640 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000641
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 SIGNAL_PENDING_CALLS();
643 busy = 0;
644 /* XXX End critical section */
645 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000646}
647
Guido van Rossum180d7b41994-09-29 09:45:57 +0000648int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000649Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 static int busy = 0;
652 if (busy)
653 return 0;
654 busy = 1;
655 UNSIGNAL_PENDING_CALLS();
656 for (;;) {
657 int i;
658 int (*func)(void *);
659 void *arg;
660 i = pendingfirst;
661 if (i == pendinglast)
662 break; /* Queue empty */
663 func = pendingcalls[i].func;
664 arg = pendingcalls[i].arg;
665 pendingfirst = (i + 1) % NPENDINGCALLS;
666 if (func(arg) < 0) {
667 busy = 0;
668 SIGNAL_PENDING_CALLS(); /* We're not done yet */
669 return -1;
670 }
671 }
672 busy = 0;
673 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000674}
675
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000676#endif /* WITH_THREAD */
677
Guido van Rossuma9672091994-09-14 13:31:22 +0000678
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000679/* The interpreter's recursion limit */
680
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000681#ifndef Py_DEFAULT_RECURSION_LIMIT
682#define Py_DEFAULT_RECURSION_LIMIT 1000
683#endif
684static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
685int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000687int
688Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691}
692
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000693void
694Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 recursion_limit = new_limit;
697 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698}
699
Armin Rigo2b3eb402003-10-28 12:05:48 +0000700/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
701 if the recursion_depth reaches _Py_CheckRecursionLimit.
702 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
703 to guarantee that _Py_CheckRecursiveCall() is regularly called.
704 Without USE_STACKCHECK, there is no need for this. */
705int
706_Py_CheckRecursiveCall(char *where)
707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000709
710#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 if (PyOS_CheckStack()) {
712 --tstate->recursion_depth;
713 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
714 return -1;
715 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000716#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 _Py_CheckRecursionLimit = recursion_limit;
718 if (tstate->recursion_critical)
719 /* Somebody asked that we don't check for recursion. */
720 return 0;
721 if (tstate->overflowed) {
722 if (tstate->recursion_depth > recursion_limit + 50) {
723 /* Overflowing while handling an overflow. Give up. */
724 Py_FatalError("Cannot recover from stack overflow.");
725 }
726 return 0;
727 }
728 if (tstate->recursion_depth > recursion_limit) {
729 --tstate->recursion_depth;
730 tstate->overflowed = 1;
731 PyErr_Format(PyExc_RuntimeError,
732 "maximum recursion depth exceeded%s",
733 where);
734 return -1;
735 }
736 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000737}
738
Guido van Rossum374a9221991-04-04 10:40:29 +0000739/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000740enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000741 WHY_NOT = 0x0001, /* No error */
742 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000743 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 *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400753static int 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 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 register PyObject **fastlocals, **freevars;
799 PyObject *retval = NULL; /* Return value */
800 PyThreadState *tstate = PyThreadState_GET();
801 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 is true when the line being executed has changed. The
808 initial values are such as to make this false the first
809 time it is tested. */
810 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 unsigned char *first_instr;
813 PyObject *names;
814 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000815
Brett Cannon368b4b72012-04-02 12:17:59 -0400816#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200817 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400818#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200819
Antoine Pitroub52ec782009-01-25 16:34:23 +0000820/* Computed GOTOs, or
821 the-optimization-commonly-but-improperly-known-as-"threaded code"
822 using gcc's labels-as-values extension
823 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
824
825 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000827 combined with a lookup table of jump addresses. However, since the
828 indirect jump instruction is shared by all opcodes, the CPU will have a
829 hard time making the right prediction for where to jump next (actually,
830 it will be always wrong except in the uncommon case of a sequence of
831 several identical opcodes).
832
833 "Threaded code" in contrast, uses an explicit jump table and an explicit
834 indirect jump instruction at the end of each opcode. Since the jump
835 instruction is at a different address for each opcode, the CPU will make a
836 separate prediction for each of these instructions, which is equivalent to
837 predicting the second opcode of each opcode pair. These predictions have
838 a much better chance to turn out valid, especially in small bytecode loops.
839
840 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000842 and potentially many more instructions (depending on the pipeline width).
843 A correctly predicted branch, however, is nearly free.
844
845 At the time of this writing, the "threaded code" version is up to 15-20%
846 faster than the normal "switch" version, depending on the compiler and the
847 CPU architecture.
848
849 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
850 because it would render the measurements invalid.
851
852
853 NOTE: care must be taken that the compiler doesn't try to "optimize" the
854 indirect jumps by sharing them between all opcodes. Such optimizations
855 can be disabled on gcc by using the -fno-gcse flag (or possibly
856 -fno-crossjumping).
857*/
858
Antoine Pitrou042b1282010-08-13 21:15:58 +0000859#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000860#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000861#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862#endif
863
Antoine Pitrou042b1282010-08-13 21:15:58 +0000864#ifdef HAVE_COMPUTED_GOTOS
865 #ifndef USE_COMPUTED_GOTOS
866 #define USE_COMPUTED_GOTOS 1
867 #endif
868#else
869 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
870 #error "Computed gotos are not supported on this compiler."
871 #endif
872 #undef USE_COMPUTED_GOTOS
873 #define USE_COMPUTED_GOTOS 0
874#endif
875
876#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000877/* Import the static jump table */
878#include "opcode_targets.h"
879
880/* This macro is used when several opcodes defer to the same implementation
881 (e.g. SETUP_LOOP, SETUP_FINALLY) */
882#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 TARGET_##op: \
884 opcode = op; \
885 if (HAS_ARG(op)) \
886 oparg = NEXTARG(); \
887 case op: \
888 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000889
890#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 TARGET_##op: \
892 opcode = op; \
893 if (HAS_ARG(op)) \
894 oparg = NEXTARG(); \
895 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000896
897
898#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 { \
900 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
901 FAST_DISPATCH(); \
902 } \
903 continue; \
904 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000905
906#ifdef LLTRACE
907#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 { \
909 if (!lltrace && !_Py_TracingPossible) { \
910 f->f_lasti = INSTR_OFFSET(); \
911 goto *opcode_targets[*next_instr++]; \
912 } \
913 goto fast_next_opcode; \
914 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000915#else
916#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 { \
918 if (!_Py_TracingPossible) { \
919 f->f_lasti = INSTR_OFFSET(); \
920 goto *opcode_targets[*next_instr++]; \
921 } \
922 goto fast_next_opcode; \
923 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000924#endif
925
926#else
927#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000929#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 /* silence compiler warnings about `impl` unused */ \
931 if (0) goto impl; \
932 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000933#define DISPATCH() continue
934#define FAST_DISPATCH() goto fast_next_opcode
935#endif
936
937
Neal Norwitza81d2202002-07-14 00:27:26 +0000938/* Tuple access macros */
939
940#ifndef Py_DEBUG
941#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
942#else
943#define GETITEM(v, i) PyTuple_GetItem((v), (i))
944#endif
945
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000946#ifdef WITH_TSC
947/* Use Pentium timestamp counter to mark certain events:
948 inst0 -- beginning of switch statement for opcode dispatch
949 inst1 -- end of switch statement (may be skipped)
950 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000952 (may be skipped)
953 intr1 -- beginning of long interruption
954 intr2 -- end of long interruption
955
956 Many opcodes call out to helper C functions. In some cases, the
957 time in those functions should be counted towards the time for the
958 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
959 calls another Python function; there's no point in charge all the
960 bytecode executed by the called function to the caller.
961
962 It's hard to make a useful judgement statically. In the presence
963 of operator overloading, it's impossible to tell if a call will
964 execute new Python code or not.
965
966 It's a case-by-case judgement. I'll use intr1 for the following
967 cases:
968
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000969 IMPORT_STAR
970 IMPORT_FROM
971 CALL_FUNCTION (and friends)
972
973 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
975 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 READ_TIMESTAMP(inst0);
978 READ_TIMESTAMP(inst1);
979 READ_TIMESTAMP(loop0);
980 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 /* shut up the compiler */
983 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000984#endif
985
Guido van Rossum374a9221991-04-04 10:40:29 +0000986/* Code access macros */
987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988#define INSTR_OFFSET() ((int)(next_instr - first_instr))
989#define NEXTOP() (*next_instr++)
990#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
991#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
992#define JUMPTO(x) (next_instr = first_instr + (x))
993#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000994
Raymond Hettingerf606f872003-03-16 03:11:04 +0000995/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 Some opcodes tend to come in pairs thus making it possible to
997 predict the second code when the first is run. For example,
998 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
999 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Verifying the prediction costs a single high-speed test of a register
1002 variable against a constant. If the pairing was good, then the
1003 processor's own internal branch predication has a high likelihood of
1004 success, resulting in a nearly zero-overhead transition to the
1005 next opcode. A successful prediction saves a trip through the eval-loop
1006 including its two unpredictable branches, the HAS_ARG test and the
1007 switch-case. Combined with the processor's internal branch prediction,
1008 a successful PREDICT has the effect of making the two opcodes run as if
1009 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001010
Georg Brandl86b2fb92008-07-16 03:43:04 +00001011 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 predictions turned-on and interpret the results as if some opcodes
1013 had been combined or turn-off predictions so that the opcode frequency
1014 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001015
1016 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 the CPU to record separate branch prediction information for each
1018 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001019
Raymond Hettingerf606f872003-03-16 03:11:04 +00001020*/
1021
Antoine Pitrou042b1282010-08-13 21:15:58 +00001022#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023#define PREDICT(op) if (0) goto PRED_##op
1024#define PREDICTED(op) PRED_##op:
1025#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001026#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1028#define PREDICTED(op) PRED_##op: next_instr++
1029#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001030#endif
1031
Raymond Hettingerf606f872003-03-16 03:11:04 +00001032
Guido van Rossum374a9221991-04-04 10:40:29 +00001033/* Stack manipulation macros */
1034
Martin v. Löwis18e16552006-02-15 17:27:45 +00001035/* The stack can grow at most MAXINT deep, as co_nlocals and
1036 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001037#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1038#define EMPTY() (STACK_LEVEL() == 0)
1039#define TOP() (stack_pointer[-1])
1040#define SECOND() (stack_pointer[-2])
1041#define THIRD() (stack_pointer[-3])
1042#define FOURTH() (stack_pointer[-4])
1043#define PEEK(n) (stack_pointer[-(n)])
1044#define SET_TOP(v) (stack_pointer[-1] = (v))
1045#define SET_SECOND(v) (stack_pointer[-2] = (v))
1046#define SET_THIRD(v) (stack_pointer[-3] = (v))
1047#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1048#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1049#define BASIC_STACKADJ(n) (stack_pointer += n)
1050#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1051#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001052
Guido van Rossum96a42c81992-01-12 02:29:51 +00001053#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001055 lltrace && prtrace(TOP(), "push")); \
1056 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001058 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001060 lltrace && prtrace(TOP(), "stackadj")); \
1061 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001062#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001063 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1064 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001065#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001066#define PUSH(v) BASIC_PUSH(v)
1067#define POP() BASIC_POP()
1068#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001069#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001070#endif
1071
Guido van Rossum681d79a1995-07-18 14:51:37 +00001072/* Local variable macros */
1073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001075
1076/* The SETLOCAL() macro must not DECREF the local variable in-place and
1077 then store the new value; it must copy the old value to a temporary
1078 value, then store the new value, and then DECREF the temporary value.
1079 This is because it is possible that during the DECREF the frame is
1080 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1081 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001083 GETLOCAL(i) = value; \
1084 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001085
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001086
1087#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 while (STACK_LEVEL() > (b)->b_level) { \
1089 PyObject *v = POP(); \
1090 Py_XDECREF(v); \
1091 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001092
1093#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 { \
1095 PyObject *type, *value, *traceback; \
1096 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1097 while (STACK_LEVEL() > (b)->b_level + 3) { \
1098 value = POP(); \
1099 Py_XDECREF(value); \
1100 } \
1101 type = tstate->exc_type; \
1102 value = tstate->exc_value; \
1103 traceback = tstate->exc_traceback; \
1104 tstate->exc_type = POP(); \
1105 tstate->exc_value = POP(); \
1106 tstate->exc_traceback = POP(); \
1107 Py_XDECREF(type); \
1108 Py_XDECREF(value); \
1109 Py_XDECREF(traceback); \
1110 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001111
Guido van Rossuma027efa1997-05-05 20:56:21 +00001112/* Start of code */
1113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 /* push frame */
1115 if (Py_EnterRecursiveCall(""))
1116 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 if (tstate->use_tracing) {
1121 if (tstate->c_tracefunc != NULL) {
1122 /* tstate->c_tracefunc, if defined, is a
1123 function that will be called on *every* entry
1124 to a code block. Its return value, if not
1125 None, is a function that will be called at
1126 the start of each executed line of code.
1127 (Actually, the function must return itself
1128 in order to continue tracing.) The trace
1129 functions are called with three arguments:
1130 a pointer to the current frame, a string
1131 indicating why the function is called, and
1132 an argument which depends on the situation.
1133 The global trace function is also called
1134 whenever an exception is detected. */
1135 if (call_trace_protected(tstate->c_tracefunc,
1136 tstate->c_traceobj,
1137 f, PyTrace_CALL, Py_None)) {
1138 /* Trace function raised an error */
1139 goto exit_eval_frame;
1140 }
1141 }
1142 if (tstate->c_profilefunc != NULL) {
1143 /* Similar for c_profilefunc, except it needn't
1144 return itself and isn't called for "line" events */
1145 if (call_trace_protected(tstate->c_profilefunc,
1146 tstate->c_profileobj,
1147 f, PyTrace_CALL, Py_None)) {
1148 /* Profile function raised an error */
1149 goto exit_eval_frame;
1150 }
1151 }
1152 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001154 co = f->f_code;
1155 names = co->co_names;
1156 consts = co->co_consts;
1157 fastlocals = f->f_localsplus;
1158 freevars = f->f_localsplus + co->co_nlocals;
1159 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1160 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 f->f_lasti now refers to the index of the last instruction
1163 executed. You might think this was obvious from the name, but
1164 this wasn't always true before 2.3! PyFrame_New now sets
1165 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1166 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1167 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001168 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1169 multiple values.
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
Victor Stinner3c1e4812012-03-26 22:10:51 +02001196 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != 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;
Guido van Rossumac7be682001-01-17 15:42:30 +00001200
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001201 if (throwflag) /* support for generator.throw() */
1202 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001205#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 if (inst1 == 0) {
1207 /* Almost surely, the opcode executed a break
1208 or a continue, preventing inst1 from being set
1209 on the way out of the loop.
1210 */
1211 READ_TIMESTAMP(inst1);
1212 loop1 = inst1;
1213 }
1214 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1215 intr0, intr1);
1216 ticked = 0;
1217 inst1 = 0;
1218 intr0 = 0;
1219 intr1 = 0;
1220 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001221#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1223 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 /* Do periodic things. Doing this every time through
1226 the loop would add too much overhead, so we do it
1227 only every Nth instruction. We also do it if
1228 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1229 event needs attention (e.g. a signal handler or
1230 async I/O handler); see Py_AddPendingCall() and
1231 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1234 if (*next_instr == SETUP_FINALLY) {
1235 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001236 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 goto fast_next_opcode;
1238 }
1239 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001240#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001242#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001244 if (Py_MakePendingCalls() < 0)
1245 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001247#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001248 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 /* Give another thread a chance */
1250 if (PyThreadState_Swap(NULL) != tstate)
1251 Py_FatalError("ceval: tstate mix-up");
1252 drop_gil(tstate);
1253
1254 /* Other threads may run now */
1255
1256 take_gil(tstate);
1257 if (PyThreadState_Swap(tstate) != NULL)
1258 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001260#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 /* Check for asynchronous exceptions. */
1262 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 tstate->async_exc = NULL;
1265 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001266 PyErr_SetNone(exc);
1267 Py_DECREF(exc);
1268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
1270 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 fast_next_opcode:
1273 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001278 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001279 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 /* see maybe_call_line_trace
1281 for expository comments */
1282 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 err = maybe_call_line_trace(tstate->c_tracefunc,
1285 tstate->c_traceobj,
1286 f, &instr_lb, &instr_ub,
1287 &instr_prev);
1288 /* Reload possibly changed frame fields */
1289 JUMPTO(f->f_lasti);
1290 if (f->f_stacktop != NULL) {
1291 stack_pointer = f->f_stacktop;
1292 f->f_stacktop = NULL;
1293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 opcode = NEXTOP();
1302 oparg = 0; /* allows oparg to be stored in a register because
1303 it doesn't have to be remembered across a full loop */
1304 if (HAS_ARG(opcode))
1305 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001306 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001307#ifdef DYNAMIC_EXECUTION_PROFILE
1308#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 dxpairs[lastopcode][opcode]++;
1310 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001311#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001313#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001314
Guido van Rossum96a42c81992-01-12 02:29:51 +00001315#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 if (lltrace) {
1319 if (HAS_ARG(opcode)) {
1320 printf("%d: %d, %d\n",
1321 f->f_lasti, opcode, oparg);
1322 }
1323 else {
1324 printf("%d: %d\n",
1325 f->f_lasti, opcode);
1326 }
1327 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001328#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 /* Main switch on opcode */
1331 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 /* BEWARE!
1336 It is essential that any operation that fails sets either
1337 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1338 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 TARGET(NOP)
1341 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001342
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001343 TARGET(LOAD_FAST) {
1344 PyObject *value = GETLOCAL(oparg);
1345 if (value == NULL) {
1346 format_exc_check_arg(PyExc_UnboundLocalError,
1347 UNBOUNDLOCAL_ERROR_MSG,
1348 PyTuple_GetItem(co->co_varnames, oparg));
1349 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001351 Py_INCREF(value);
1352 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001354 }
1355
1356 TARGET(LOAD_CONST) {
1357 PyObject *value = GETITEM(consts, oparg);
1358 Py_INCREF(value);
1359 PUSH(value);
1360 FAST_DISPATCH();
1361 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 TARGET(STORE_FAST) {
1365 PyObject *value = POP();
1366 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001368 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001369
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 TARGET(POP_TOP) {
1371 PyObject *value = POP();
1372 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 TARGET(ROT_TWO) {
1377 PyObject *top = TOP();
1378 PyObject *second = SECOND();
1379 SET_TOP(second);
1380 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 TARGET(ROT_THREE) {
1385 PyObject *top = TOP();
1386 PyObject *second = SECOND();
1387 PyObject *third = THIRD();
1388 SET_TOP(second);
1389 SET_SECOND(third);
1390 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001392 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001393
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 TARGET(DUP_TOP) {
1395 PyObject *top = TOP();
1396 Py_INCREF(top);
1397 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 TARGET(DUP_TOP_TWO) {
1402 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001404 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001406 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 SET_TOP(top);
1408 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001409 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001411
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 TARGET(UNARY_POSITIVE) {
1413 PyObject *value = TOP();
1414 PyObject *res = PyNumber_Positive(value);
1415 Py_DECREF(value);
1416 SET_TOP(res);
1417 if (res == NULL)
1418 goto error;
1419 DISPATCH();
1420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 TARGET(UNARY_NEGATIVE) {
1423 PyObject *value = TOP();
1424 PyObject *res = PyNumber_Negative(value);
1425 Py_DECREF(value);
1426 SET_TOP(res);
1427 if (res == NULL)
1428 goto error;
1429 DISPATCH();
1430 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 TARGET(UNARY_NOT) {
1433 PyObject *value = TOP();
1434 int err = PyObject_IsTrue(value);
1435 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 if (err == 0) {
1437 Py_INCREF(Py_True);
1438 SET_TOP(Py_True);
1439 DISPATCH();
1440 }
1441 else if (err > 0) {
1442 Py_INCREF(Py_False);
1443 SET_TOP(Py_False);
1444 err = 0;
1445 DISPATCH();
1446 }
1447 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 goto error;
1449 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 TARGET(UNARY_INVERT) {
1452 PyObject *value = TOP();
1453 PyObject *res = PyNumber_Invert(value);
1454 Py_DECREF(value);
1455 SET_TOP(res);
1456 if (res == NULL)
1457 goto error;
1458 DISPATCH();
1459 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 TARGET(BINARY_POWER) {
1462 PyObject *exp = POP();
1463 PyObject *base = TOP();
1464 PyObject *res = PyNumber_Power(base, exp, Py_None);
1465 Py_DECREF(base);
1466 Py_DECREF(exp);
1467 SET_TOP(res);
1468 if (res == NULL)
1469 goto error;
1470 DISPATCH();
1471 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 TARGET(BINARY_MULTIPLY) {
1474 PyObject *right = POP();
1475 PyObject *left = TOP();
1476 PyObject *res = PyNumber_Multiply(left, right);
1477 Py_DECREF(left);
1478 Py_DECREF(right);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 TARGET(BINARY_TRUE_DIVIDE) {
1486 PyObject *divisor = POP();
1487 PyObject *dividend = TOP();
1488 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1489 Py_DECREF(dividend);
1490 Py_DECREF(divisor);
1491 SET_TOP(quotient);
1492 if (quotient == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 TARGET(BINARY_FLOOR_DIVIDE) {
1498 PyObject *divisor = POP();
1499 PyObject *dividend = TOP();
1500 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1501 Py_DECREF(dividend);
1502 Py_DECREF(divisor);
1503 SET_TOP(quotient);
1504 if (quotient == NULL)
1505 goto error;
1506 DISPATCH();
1507 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001508
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001509 TARGET(BINARY_MODULO) {
1510 PyObject *divisor = POP();
1511 PyObject *dividend = TOP();
1512 PyObject *res = PyUnicode_CheckExact(dividend) ?
1513 PyUnicode_Format(dividend, divisor) :
1514 PyNumber_Remainder(dividend, divisor);
1515 Py_DECREF(divisor);
1516 Py_DECREF(dividend);
1517 SET_TOP(res);
1518 if (res == NULL)
1519 goto error;
1520 DISPATCH();
1521 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 TARGET(BINARY_ADD) {
1524 PyObject *right = POP();
1525 PyObject *left = TOP();
1526 PyObject *sum;
1527 if (PyUnicode_CheckExact(left) &&
1528 PyUnicode_CheckExact(right)) {
1529 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001530 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001531 }
1532 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 sum = PyNumber_Add(left, right);
1534 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001535 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 Py_DECREF(right);
1537 SET_TOP(sum);
1538 if (sum == NULL)
1539 goto error;
1540 DISPATCH();
1541 }
1542
1543 TARGET(BINARY_SUBTRACT) {
1544 PyObject *right = POP();
1545 PyObject *left = TOP();
1546 PyObject *diff = PyNumber_Subtract(left, right);
1547 Py_DECREF(right);
1548 Py_DECREF(left);
1549 SET_TOP(diff);
1550 if (diff == NULL)
1551 goto error;
1552 DISPATCH();
1553 }
1554
1555 TARGET(BINARY_SUBSCR) {
1556 PyObject *sub = POP();
1557 PyObject *container = TOP();
1558 PyObject *res = PyObject_GetItem(container, sub);
1559 Py_DECREF(container);
1560 Py_DECREF(sub);
1561 SET_TOP(res);
1562 if (res == NULL)
1563 goto error;
1564 DISPATCH();
1565 }
1566
1567 TARGET(BINARY_LSHIFT) {
1568 PyObject *right = POP();
1569 PyObject *left = TOP();
1570 PyObject *res = PyNumber_Lshift(left, right);
1571 Py_DECREF(left);
1572 Py_DECREF(right);
1573 SET_TOP(res);
1574 if (res == NULL)
1575 goto error;
1576 DISPATCH();
1577 }
1578
1579 TARGET(BINARY_RSHIFT) {
1580 PyObject *right = POP();
1581 PyObject *left = TOP();
1582 PyObject *res = PyNumber_Rshift(left, right);
1583 Py_DECREF(left);
1584 Py_DECREF(right);
1585 SET_TOP(res);
1586 if (res == NULL)
1587 goto error;
1588 DISPATCH();
1589 }
1590
1591 TARGET(BINARY_AND) {
1592 PyObject *right = POP();
1593 PyObject *left = TOP();
1594 PyObject *res = PyNumber_And(left, right);
1595 Py_DECREF(left);
1596 Py_DECREF(right);
1597 SET_TOP(res);
1598 if (res == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
1602
1603 TARGET(BINARY_XOR) {
1604 PyObject *right = POP();
1605 PyObject *left = TOP();
1606 PyObject *res = PyNumber_Xor(left, right);
1607 Py_DECREF(left);
1608 Py_DECREF(right);
1609 SET_TOP(res);
1610 if (res == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
1614
1615 TARGET(BINARY_OR) {
1616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *res = PyNumber_Or(left, right);
1619 Py_DECREF(left);
1620 Py_DECREF(right);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
1626
1627 TARGET(LIST_APPEND) {
1628 PyObject *v = POP();
1629 PyObject *list = PEEK(oparg);
1630 int err;
1631 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 if (err != 0)
1634 goto error;
1635 PREDICT(JUMP_ABSOLUTE);
1636 DISPATCH();
1637 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001639 TARGET(SET_ADD) {
1640 PyObject *v = POP();
1641 PyObject *set = stack_pointer[-oparg];
1642 int err;
1643 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001645 if (err != 0)
1646 goto error;
1647 PREDICT(JUMP_ABSOLUTE);
1648 DISPATCH();
1649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 TARGET(INPLACE_POWER) {
1652 PyObject *exp = POP();
1653 PyObject *base = TOP();
1654 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1655 Py_DECREF(base);
1656 Py_DECREF(exp);
1657 SET_TOP(res);
1658 if (res == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001663 TARGET(INPLACE_MULTIPLY) {
1664 PyObject *right = POP();
1665 PyObject *left = TOP();
1666 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1667 Py_DECREF(left);
1668 Py_DECREF(right);
1669 SET_TOP(res);
1670 if (res == NULL)
1671 goto error;
1672 DISPATCH();
1673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 TARGET(INPLACE_TRUE_DIVIDE) {
1676 PyObject *divisor = POP();
1677 PyObject *dividend = TOP();
1678 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1679 Py_DECREF(dividend);
1680 Py_DECREF(divisor);
1681 SET_TOP(quotient);
1682 if (quotient == NULL)
1683 goto error;
1684 DISPATCH();
1685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 TARGET(INPLACE_FLOOR_DIVIDE) {
1688 PyObject *divisor = POP();
1689 PyObject *dividend = TOP();
1690 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1691 Py_DECREF(dividend);
1692 Py_DECREF(divisor);
1693 SET_TOP(quotient);
1694 if (quotient == NULL)
1695 goto error;
1696 DISPATCH();
1697 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 TARGET(INPLACE_MODULO) {
1700 PyObject *right = POP();
1701 PyObject *left = TOP();
1702 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1703 Py_DECREF(left);
1704 Py_DECREF(right);
1705 SET_TOP(mod);
1706 if (mod == NULL)
1707 goto error;
1708 DISPATCH();
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001711 TARGET(INPLACE_ADD) {
1712 PyObject *right = POP();
1713 PyObject *left = TOP();
1714 PyObject *sum;
1715 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1716 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001717 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001718 }
1719 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001720 sum = PyNumber_InPlaceAdd(left, right);
1721 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001722 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 Py_DECREF(right);
1724 SET_TOP(sum);
1725 if (sum == NULL)
1726 goto error;
1727 DISPATCH();
1728 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 TARGET(INPLACE_SUBTRACT) {
1731 PyObject *right = POP();
1732 PyObject *left = TOP();
1733 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1734 Py_DECREF(left);
1735 Py_DECREF(right);
1736 SET_TOP(diff);
1737 if (diff == NULL)
1738 goto error;
1739 DISPATCH();
1740 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 TARGET(INPLACE_LSHIFT) {
1743 PyObject *right = POP();
1744 PyObject *left = TOP();
1745 PyObject *res = PyNumber_InPlaceLshift(left, right);
1746 Py_DECREF(left);
1747 Py_DECREF(right);
1748 SET_TOP(res);
1749 if (res == NULL)
1750 goto error;
1751 DISPATCH();
1752 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001753
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001754 TARGET(INPLACE_RSHIFT) {
1755 PyObject *right = POP();
1756 PyObject *left = TOP();
1757 PyObject *res = PyNumber_InPlaceRshift(left, right);
1758 Py_DECREF(left);
1759 Py_DECREF(right);
1760 SET_TOP(res);
1761 if (res == NULL)
1762 goto error;
1763 DISPATCH();
1764 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001765
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 TARGET(INPLACE_AND) {
1767 PyObject *right = POP();
1768 PyObject *left = TOP();
1769 PyObject *res = PyNumber_InPlaceAnd(left, right);
1770 Py_DECREF(left);
1771 Py_DECREF(right);
1772 SET_TOP(res);
1773 if (res == NULL)
1774 goto error;
1775 DISPATCH();
1776 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 TARGET(INPLACE_XOR) {
1779 PyObject *right = POP();
1780 PyObject *left = TOP();
1781 PyObject *res = PyNumber_InPlaceXor(left, right);
1782 Py_DECREF(left);
1783 Py_DECREF(right);
1784 SET_TOP(res);
1785 if (res == NULL)
1786 goto error;
1787 DISPATCH();
1788 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 TARGET(INPLACE_OR) {
1791 PyObject *right = POP();
1792 PyObject *left = TOP();
1793 PyObject *res = PyNumber_InPlaceOr(left, right);
1794 Py_DECREF(left);
1795 Py_DECREF(right);
1796 SET_TOP(res);
1797 if (res == NULL)
1798 goto error;
1799 DISPATCH();
1800 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001801
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 TARGET(STORE_SUBSCR) {
1803 PyObject *sub = TOP();
1804 PyObject *container = SECOND();
1805 PyObject *v = THIRD();
1806 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 STACKADJ(-3);
1808 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 Py_DECREF(container);
1812 Py_DECREF(sub);
1813 if (err != 0)
1814 goto error;
1815 DISPATCH();
1816 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 TARGET(DELETE_SUBSCR) {
1819 PyObject *sub = TOP();
1820 PyObject *container = SECOND();
1821 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 STACKADJ(-2);
1823 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 err = PyObject_DelItem(container, sub);
1825 Py_DECREF(container);
1826 Py_DECREF(sub);
1827 if (err != 0)
1828 goto error;
1829 DISPATCH();
1830 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001831
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001832 TARGET(PRINT_EXPR) {
1833 PyObject *value = POP();
1834 PyObject *hook = PySys_GetObject("displayhook");
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001835 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 PyErr_SetString(PyExc_RuntimeError,
1838 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001839 Py_DECREF(value);
1840 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001842 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 Py_DECREF(value);
1844 if (res == NULL)
1845 goto error;
1846 Py_DECREF(res);
1847 DISPATCH();
1848 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001849
Thomas Wouters434d0822000-08-24 20:11:32 +00001850#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001852#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 TARGET(RAISE_VARARGS) {
1854 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 switch (oparg) {
1856 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 if (do_raise(exc, cause)) {
1862 why = WHY_EXCEPTION;
1863 goto fast_block_end;
1864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 break;
1866 default:
1867 PyErr_SetString(PyExc_SystemError,
1868 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 break;
1870 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 goto error;
1872 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001873
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 TARGET(STORE_LOCALS) {
1875 PyObject *locals = POP();
1876 PyObject *old = f->f_locals;
1877 Py_XDECREF(old);
1878 f->f_locals = locals;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001881
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 retval = POP();
1884 why = WHY_RETURN;
1885 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001887
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 TARGET(YIELD_FROM) {
1889 PyObject *v = POP();
1890 PyObject *reciever = TOP();
1891 int err;
1892 if (PyGen_CheckExact(reciever)) {
1893 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001894 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001895 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 if (v == Py_None)
1897 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001898 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 retval = _PyObject_CallMethodId(reciever, &PyId_send, "O", v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001900 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 Py_DECREF(v);
1902 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001903 PyObject *val;
Nick Coghlanc40bc092012-06-17 15:15:49 +10001904 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 if (err < 0)
1906 goto error;
1907 Py_DECREF(reciever);
1908 SET_TOP(val);
1909 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001910 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001911 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001912 f->f_stacktop = stack_pointer;
1913 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001914 /* and repeat... */
1915 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001916 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001917 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001918
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 retval = POP();
1921 f->f_stacktop = stack_pointer;
1922 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001924 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001925
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 TARGET(POP_EXCEPT) {
1927 PyTryBlock *b = PyFrame_BlockPop(f);
1928 if (b->b_type != EXCEPT_HANDLER) {
1929 PyErr_SetString(PyExc_SystemError,
1930 "popped block is not an except handler");
1931 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001936
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 TARGET(POP_BLOCK) {
1938 PyTryBlock *b = PyFrame_BlockPop(f);
1939 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 TARGET(END_FINALLY) {
1945 PyObject *status = POP();
1946 if (PyLong_Check(status)) {
1947 why = (enum why_code) PyLong_AS_LONG(status);
1948 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 if (why == WHY_RETURN ||
1950 why == WHY_CONTINUE)
1951 retval = POP();
1952 if (why == WHY_SILENCED) {
1953 /* An exception was silenced by 'with', we must
1954 manually unwind the EXCEPT_HANDLER block which was
1955 created when the exception was caught, otherwise
1956 the stack will be in an inconsistent state. */
1957 PyTryBlock *b = PyFrame_BlockPop(f);
1958 assert(b->b_type == EXCEPT_HANDLER);
1959 UNWIND_EXCEPT_HANDLER(b);
1960 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 Py_DECREF(status);
1962 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 Py_DECREF(status);
1965 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 else if (PyExceptionClass_Check(status)) {
1968 PyObject *exc = POP();
1969 PyObject *tb = POP();
1970 PyErr_Restore(status, exc, tb);
1971 why = WHY_EXCEPTION;
1972 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001974 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 PyErr_SetString(PyExc_SystemError,
1976 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001977 Py_DECREF(status);
1978 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001980 Py_DECREF(status);
1981 DISPATCH();
1982 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001983
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001985 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001986
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001988 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1990 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001991 PyErr_SetString(PyExc_NameError,
1992 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001994 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001996 }
1997 else {
1998 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1999 if (build_class_str == NULL)
2000 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2002 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002003 if (PyErr_ExceptionMatches(PyExc_KeyError))
2004 PyErr_SetString(PyExc_NameError,
2005 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002007 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002010 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002011 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 TARGET(STORE_NAME) {
2014 PyObject *name = GETITEM(names, oparg);
2015 PyObject *v = POP();
2016 PyObject *ns = f->f_locals;
2017 int err;
2018 if (ns == NULL) {
2019 PyErr_Format(PyExc_SystemError,
2020 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 if (PyDict_CheckExact(ns))
2025 err = PyDict_SetItem(ns, name, v);
2026 else
2027 err = PyObject_SetItem(ns, name, v);
2028 Py_DECREF(v);
2029 if (err != 0)
2030 goto error;
2031 DISPATCH();
2032 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002033
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002034 TARGET(DELETE_NAME) {
2035 PyObject *name = GETITEM(names, oparg);
2036 PyObject *ns = f->f_locals;
2037 int err;
2038 if (ns == NULL) {
2039 PyErr_Format(PyExc_SystemError,
2040 "no locals when deleting %R", name);
2041 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 err = PyObject_DelItem(ns, name);
2044 if (err != 0) {
2045 format_exc_check_arg(PyExc_NameError,
2046 NAME_ERROR_MSG,
2047 name);
2048 goto error;
2049 }
2050 DISPATCH();
2051 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002054 TARGET(UNPACK_SEQUENCE) {
2055 PyObject *seq = POP(), *item, **items;
2056 if (PyTuple_CheckExact(seq) &&
2057 PyTuple_GET_SIZE(seq) == oparg) {
2058 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 item = items[oparg];
2061 Py_INCREF(item);
2062 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 } else if (PyList_CheckExact(seq) &&
2065 PyList_GET_SIZE(seq) == oparg) {
2066 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 item = items[oparg];
2069 Py_INCREF(item);
2070 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 stack_pointer + oparg)) {
2074 STACKADJ(oparg);
2075 } else {
2076 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 Py_DECREF(seq);
2078 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002081 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002083
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 TARGET(UNPACK_EX) {
2085 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2086 PyObject *seq = POP();
2087
2088 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2089 stack_pointer + totalargs)) {
2090 stack_pointer += totalargs;
2091 } else {
2092 Py_DECREF(seq);
2093 goto error;
2094 }
2095 Py_DECREF(seq);
2096 DISPATCH();
2097 }
2098
2099 TARGET(STORE_ATTR) {
2100 PyObject *name = GETITEM(names, oparg);
2101 PyObject *owner = TOP();
2102 PyObject *v = SECOND();
2103 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 Py_DECREF(owner);
2108 if (err != 0)
2109 goto error;
2110 DISPATCH();
2111 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002112
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 TARGET(DELETE_ATTR) {
2114 PyObject *name = GETITEM(names, oparg);
2115 PyObject *owner = POP();
2116 int err;
2117 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2118 Py_DECREF(owner);
2119 if (err != 0)
2120 goto error;
2121 DISPATCH();
2122 }
2123
2124 TARGET(STORE_GLOBAL) {
2125 PyObject *name = GETITEM(names, oparg);
2126 PyObject *v = POP();
2127 int err;
2128 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 if (err != 0)
2131 goto error;
2132 DISPATCH();
2133 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 TARGET(DELETE_GLOBAL) {
2136 PyObject *name = GETITEM(names, oparg);
2137 int err;
2138 err = PyDict_DelItem(f->f_globals, name);
2139 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002141 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002143 }
2144 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002146
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 TARGET(LOAD_NAME) {
2148 PyObject *name = GETITEM(names, oparg);
2149 PyObject *locals = f->f_locals;
2150 PyObject *v;
2151 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 "no locals when loading %R", name);
2154 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 if (PyDict_CheckExact(locals)) {
2157 v = PyDict_GetItem(locals, name);
2158 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 }
2160 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 v = PyObject_GetItem(locals, name);
2162 if (v == NULL && PyErr_Occurred()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002163 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2164 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 PyErr_Clear();
2166 }
2167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 if (v == NULL) {
2169 v = PyDict_GetItem(f->f_globals, name);
2170 Py_XINCREF(v);
2171 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002172 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 v = PyDict_GetItem(f->f_builtins, name);
2174 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002175 format_exc_check_arg(
2176 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 NAME_ERROR_MSG, name);
2178 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 }
2182 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 v = PyObject_GetItem(f->f_builtins, name);
2184 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002185 if (PyErr_ExceptionMatches(PyExc_KeyError))
2186 format_exc_check_arg(
2187 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 NAME_ERROR_MSG, name);
2189 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002190 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002191 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002197
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 TARGET(LOAD_GLOBAL) {
2199 PyObject *name = GETITEM(names, oparg);
2200 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002201 if (PyDict_CheckExact(f->f_globals)
2202 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002204 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 name);
2206 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002207 if (!PyErr_Occurred())
2208 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002209 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002214 else {
2215 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 v = PyObject_GetItem(f->f_globals, name);
2217 if (v == NULL) {
2218 v = PyObject_GetItem(f->f_builtins, name);
2219 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002220 if (PyErr_ExceptionMatches(PyExc_KeyError))
2221 format_exc_check_arg(
2222 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002223 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002224 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002225 }
2226 }
2227 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002231
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 TARGET(DELETE_FAST) {
2233 PyObject *v = GETLOCAL(oparg);
2234 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 SETLOCAL(oparg, NULL);
2236 DISPATCH();
2237 }
2238 format_exc_check_arg(
2239 PyExc_UnboundLocalError,
2240 UNBOUNDLOCAL_ERROR_MSG,
2241 PyTuple_GetItem(co->co_varnames, oparg)
2242 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 goto error;
2244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002245
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 TARGET(DELETE_DEREF) {
2247 PyObject *cell = freevars[oparg];
2248 if (PyCell_GET(cell) != NULL) {
2249 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002250 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002251 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002252 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 goto error;
2254 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002255
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 TARGET(LOAD_CLOSURE) {
2257 PyObject *cell = freevars[oparg];
2258 Py_INCREF(cell);
2259 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002262
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 TARGET(LOAD_DEREF) {
2264 PyObject *cell = freevars[oparg];
2265 PyObject *value = PyCell_GET(cell);
2266 if (value == NULL) {
2267 format_exc_unbound(co, oparg);
2268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 Py_INCREF(value);
2271 PUSH(value);
2272 DISPATCH();
2273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 TARGET(STORE_DEREF) {
2276 PyObject *v = POP();
2277 PyObject *cell = freevars[oparg];
2278 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002280 DISPATCH();
2281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002282
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 TARGET(BUILD_TUPLE) {
2284 PyObject *tup = PyTuple_New(oparg);
2285 if (tup == NULL)
2286 goto error;
2287 while (--oparg >= 0) {
2288 PyObject *item = POP();
2289 PyTuple_SET_ITEM(tup, oparg, item);
2290 }
2291 PUSH(tup);
2292 DISPATCH();
2293 }
2294
2295 TARGET(BUILD_LIST) {
2296 PyObject *list = PyList_New(oparg);
2297 if (list == NULL)
2298 goto error;
2299 while (--oparg >= 0) {
2300 PyObject *item = POP();
2301 PyList_SET_ITEM(list, oparg, item);
2302 }
2303 PUSH(list);
2304 DISPATCH();
2305 }
2306
2307 TARGET(BUILD_SET) {
2308 PyObject *set = PySet_New(NULL);
2309 int err = 0;
2310 if (set == NULL)
2311 goto error;
2312 while (--oparg >= 0) {
2313 PyObject *item = POP();
2314 if (err == 0)
2315 err = PySet_Add(set, item);
2316 Py_DECREF(item);
2317 }
2318 if (err != 0) {
2319 Py_DECREF(set);
2320 goto error;
2321 }
2322 PUSH(set);
2323 DISPATCH();
2324 }
2325
2326 TARGET(BUILD_MAP) {
2327 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2328 if (map == NULL)
2329 goto error;
2330 PUSH(map);
2331 DISPATCH();
2332 }
2333
2334 TARGET(STORE_MAP) {
2335 PyObject *key = TOP();
2336 PyObject *value = SECOND();
2337 PyObject *map = THIRD();
2338 int err;
2339 STACKADJ(-2);
2340 assert(PyDict_CheckExact(map));
2341 err = PyDict_SetItem(map, key, value);
2342 Py_DECREF(value);
2343 Py_DECREF(key);
2344 if (err != 0)
2345 goto error;
2346 DISPATCH();
2347 }
2348
2349 TARGET(MAP_ADD) {
2350 PyObject *key = TOP();
2351 PyObject *value = SECOND();
2352 PyObject *map;
2353 int err;
2354 STACKADJ(-2);
2355 map = stack_pointer[-oparg]; /* dict */
2356 assert(PyDict_CheckExact(map));
2357 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2358 Py_DECREF(value);
2359 Py_DECREF(key);
2360 if (err != 0)
2361 goto error;
2362 PREDICT(JUMP_ABSOLUTE);
2363 DISPATCH();
2364 }
2365
2366 TARGET(LOAD_ATTR) {
2367 PyObject *name = GETITEM(names, oparg);
2368 PyObject *owner = TOP();
2369 PyObject *res = PyObject_GetAttr(owner, name);
2370 Py_DECREF(owner);
2371 SET_TOP(res);
2372 if (res == NULL)
2373 goto error;
2374 DISPATCH();
2375 }
2376
2377 TARGET(COMPARE_OP) {
2378 PyObject *right = POP();
2379 PyObject *left = TOP();
2380 PyObject *res = cmp_outcome(oparg, left, right);
2381 Py_DECREF(left);
2382 Py_DECREF(right);
2383 SET_TOP(res);
2384 if (res == NULL)
2385 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 PREDICT(POP_JUMP_IF_FALSE);
2387 PREDICT(POP_JUMP_IF_TRUE);
2388 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002389 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002390
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 TARGET(IMPORT_NAME) {
2392 _Py_IDENTIFIER(__import__);
2393 PyObject *name = GETITEM(names, oparg);
2394 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002395 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 if (func == NULL) {
2397 PyErr_SetString(PyExc_ImportError,
2398 "__import__ not found");
2399 goto error;
2400 }
2401 Py_INCREF(func);
2402 from = POP();
2403 level = TOP();
2404 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2405 args = PyTuple_Pack(5,
2406 name,
2407 f->f_globals,
2408 f->f_locals == NULL ?
2409 Py_None : f->f_locals,
2410 from,
2411 level);
2412 else
2413 args = PyTuple_Pack(4,
2414 name,
2415 f->f_globals,
2416 f->f_locals == NULL ?
2417 Py_None : f->f_locals,
2418 from);
2419 Py_DECREF(level);
2420 Py_DECREF(from);
2421 if (args == NULL) {
2422 Py_DECREF(func);
2423 STACKADJ(-1);
2424 goto error;
2425 }
2426 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002427 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 READ_TIMESTAMP(intr1);
2429 Py_DECREF(args);
2430 Py_DECREF(func);
2431 SET_TOP(res);
2432 if (res == NULL)
2433 goto error;
2434 DISPATCH();
2435 }
2436
2437 TARGET(IMPORT_STAR) {
2438 PyObject *from = POP(), *locals;
2439 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002440 PyFrame_FastToLocals(f);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 locals = f->f_locals;
2442 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 PyErr_SetString(PyExc_SystemError,
2444 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002446 }
2447 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 READ_TIMESTAMP(intr1);
2450 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002451 Py_DECREF(from);
2452 if (err != 0)
2453 goto error;
2454 DISPATCH();
2455 }
Guido van Rossum25831651993-05-19 14:50:45 +00002456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 TARGET(IMPORT_FROM) {
2458 PyObject *name = GETITEM(names, oparg);
2459 PyObject *from = TOP();
2460 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 PUSH(res);
2465 if (res == NULL)
2466 goto error;
2467 DISPATCH();
2468 }
Thomas Wouters52152252000-08-17 22:55:00 +00002469
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 JUMPBY(oparg);
2472 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002473 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 TARGET(POP_JUMP_IF_FALSE) {
2477 PyObject *cond = POP();
2478 int err;
2479 if (cond == Py_True) {
2480 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 FAST_DISPATCH();
2482 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 if (cond == Py_False) {
2484 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 JUMPTO(oparg);
2486 FAST_DISPATCH();
2487 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 err = PyObject_IsTrue(cond);
2489 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 if (err > 0)
2491 err = 0;
2492 else if (err == 0)
2493 JUMPTO(oparg);
2494 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002500 TARGET(POP_JUMP_IF_TRUE) {
2501 PyObject *cond = POP();
2502 int err;
2503 if (cond == Py_False) {
2504 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 FAST_DISPATCH();
2506 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002507 if (cond == Py_True) {
2508 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 JUMPTO(oparg);
2510 FAST_DISPATCH();
2511 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 err = PyObject_IsTrue(cond);
2513 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 if (err > 0) {
2515 err = 0;
2516 JUMPTO(oparg);
2517 }
2518 else if (err == 0)
2519 ;
2520 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002524
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 TARGET(JUMP_IF_FALSE_OR_POP) {
2526 PyObject *cond = TOP();
2527 int err;
2528 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 FAST_DISPATCH();
2532 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 JUMPTO(oparg);
2535 FAST_DISPATCH();
2536 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002537 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 if (err > 0) {
2539 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 err = 0;
2542 }
2543 else if (err == 0)
2544 JUMPTO(oparg);
2545 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002549
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 TARGET(JUMP_IF_TRUE_OR_POP) {
2551 PyObject *cond = TOP();
2552 int err;
2553 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002555 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 FAST_DISPATCH();
2557 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002559 JUMPTO(oparg);
2560 FAST_DISPATCH();
2561 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 if (err > 0) {
2564 err = 0;
2565 JUMPTO(oparg);
2566 }
2567 else if (err == 0) {
2568 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 }
2571 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002572 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002579#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 /* Enabling this path speeds-up all while and for-loops by bypassing
2581 the per-loop checks for signals. By default, this should be turned-off
2582 because it prevents detection of a control-break in tight loops like
2583 "while 1: pass". Compile with this option turned-on when you need
2584 the speed-up and do not need break checking inside tight loops (ones
2585 that contain only instructions ending with FAST_DISPATCH).
2586 */
2587 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002588#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002590#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002592
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 PyObject *iterable = TOP();
2596 PyObject *iter = PyObject_GetIter(iterable);
2597 Py_DECREF(iterable);
2598 SET_TOP(iter);
2599 if (iter == NULL)
2600 goto error;
2601 PREDICT(FOR_ITER);
2602 DISPATCH();
2603 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 PyObject *iter = TOP();
2609 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2610 if (next != NULL) {
2611 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 PREDICT(STORE_FAST);
2613 PREDICT(UNPACK_SEQUENCE);
2614 DISPATCH();
2615 }
2616 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2618 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 PyErr_Clear();
2620 }
2621 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 STACKADJ(-1);
2623 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 JUMPBY(oparg);
2625 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002627
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 why = WHY_BREAK;
2630 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002631 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002632
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002633 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 if (retval == NULL)
2636 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 why = WHY_CONTINUE;
2638 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002641 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2642 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2643 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002644 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 /* NOTE: If you add any new block-setup opcodes that
2646 are not try/except/finally handlers, you may need
2647 to update the PyGen_NeedsFinalizing() function.
2648 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2651 STACK_LEVEL());
2652 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002654
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002656 _Py_IDENTIFIER(__exit__);
2657 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 PyObject *mgr = TOP();
2659 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2660 PyObject *res;
2661 if (exit == NULL)
2662 goto error;
2663 SET_TOP(exit);
2664 enter = special_lookup(mgr, &PyId___enter__);
2665 Py_DECREF(mgr);
2666 if (enter == NULL)
2667 goto error;
2668 res = PyObject_CallFunctionObjArgs(enter, NULL);
2669 Py_DECREF(enter);
2670 if (res == NULL)
2671 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 /* Setup the finally block before pushing the result
2673 of __enter__ on the stack. */
2674 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2675 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002676
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 DISPATCH();
2679 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002680
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 TARGET(WITH_CLEANUP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 /* At the top of the stack are 1-3 values indicating
2683 how/why we entered the finally clause:
2684 - TOP = None
2685 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2686 - TOP = WHY_*; no retval below it
2687 - (TOP, SECOND, THIRD) = exc_info()
2688 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2689 Below them is EXIT, the context.__exit__ bound method.
2690 In the last case, we must call
2691 EXIT(TOP, SECOND, THIRD)
2692 otherwise we must call
2693 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002694
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002695 In the first two cases, we remove EXIT from the
2696 stack, leaving the rest in the same order. In the
2697 third case, we shift the bottom 3 values of the
2698 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 In addition, if the stack represents an exception,
2701 *and* the function call returns a 'true' value, we
2702 push WHY_SILENCED onto the stack. END_FINALLY will
2703 then not re-raise the exception. (But non-local
2704 gotos should still be resumed.)
2705 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2709 int err;
2710 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 (void)POP();
2712 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 else if (PyLong_Check(exc)) {
2716 STACKADJ(-1);
2717 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 case WHY_RETURN:
2719 case WHY_CONTINUE:
2720 /* Retval in TOP. */
2721 exit_func = SECOND();
2722 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 break;
2725 default:
2726 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 break;
2729 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 }
2732 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 val = SECOND();
2736 tb = THIRD();
2737 tp2 = FOURTH();
2738 exc2 = PEEK(5);
2739 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 SET_VALUE(7, tb2);
2742 SET_VALUE(6, exc2);
2743 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2745 SET_FOURTH(NULL);
2746 /* We just shifted the stack down, so we have
2747 to tell the except handler block that the
2748 values are lower than it expects. */
2749 block = &f->f_blockstack[f->f_iblock - 1];
2750 assert(block->b_type == EXCEPT_HANDLER);
2751 block->b_level--;
2752 }
2753 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 if (res == NULL)
2757 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002758
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 if (exc != Py_None)
2760 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 else
2762 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002765 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 else if (err > 0) {
2768 err = 0;
2769 /* There was an exception and a True return */
2770 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2771 }
2772 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002775
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 TARGET(CALL_FUNCTION) {
2777 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 PCALL(PCALL_ALL);
2779 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002780#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002782#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002784#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 PUSH(res);
2787 if (res == NULL)
2788 goto error;
2789 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2793 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2794 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 int na = oparg & 0xff;
2797 int nk = (oparg>>8) & 0xff;
2798 int flags = (opcode - CALL_FUNCTION) & 3;
2799 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 PCALL(PCALL_ALL);
2802 if (flags & CALL_FLAG_VAR)
2803 n++;
2804 if (flags & CALL_FLAG_KW)
2805 n++;
2806 pfunc = stack_pointer - n - 1;
2807 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002810 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 PyObject *self = PyMethod_GET_SELF(func);
2812 Py_INCREF(self);
2813 func = PyMethod_GET_FUNCTION(func);
2814 Py_INCREF(func);
2815 Py_DECREF(*pfunc);
2816 *pfunc = self;
2817 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002818 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 } else
2820 Py_INCREF(func);
2821 sp = stack_pointer;
2822 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 READ_TIMESTAMP(intr1);
2825 stack_pointer = sp;
2826 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 PyObject *o = POP();
2830 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 PUSH(res);
2833 if (res == NULL)
2834 goto error;
2835 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2839 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 int posdefaults = oparg & 0xff;
2842 int kwdefaults = (oparg>>8) & 0xff;
2843 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002844
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 PyObject *qualname = POP(); /* qualname */
2846 PyObject *code = POP(); /* code object */
2847 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2848 Py_DECREF(code);
2849 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002850
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 if (func == NULL)
2852 goto error;
2853
2854 if (opcode == MAKE_CLOSURE) {
2855 PyObject *closure = POP();
2856 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 Py_DECREF(func);
2859 Py_DECREF(closure);
2860 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002864
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 PyObject *names = POP(); /* names of args with annotations */
2868 PyObject *anns = PyDict_New();
2869 if (anns == NULL) {
2870 Py_DECREF(func);
2871 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 assert(num_annotations == name_ix+1);
2875 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002876 PyObject *name, *value;
2877 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 name = PyTuple_GET_ITEM(names, name_ix);
2880 value = POP();
2881 err = PyDict_SetItem(anns, name, value);
2882 Py_DECREF(value);
2883 if (err != 0) {
2884 Py_DECREF(anns);
2885 Py_DECREF(func);
2886 goto error;
2887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002889
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 /* Can't happen unless
2892 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 Py_DECREF(anns);
2894 Py_DECREF(func);
2895 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 Py_DECREF(anns);
2898 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 if (kwdefaults > 0) {
2903 PyObject *defs = PyDict_New();
2904 if (defs == NULL) {
2905 Py_DECREF(func);
2906 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 }
2908 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 PyObject *v = POP(); /* default value */
2910 PyObject *key = POP(); /* kw only arg name */
2911 int err = PyDict_SetItem(defs, key, v);
2912 Py_DECREF(v);
2913 Py_DECREF(key);
2914 if (err != 0) {
2915 Py_DECREF(defs);
2916 Py_DECREF(func);
2917 goto error;
2918 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 /* Can't happen unless
2922 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 Py_DECREF(func);
2924 Py_DECREF(defs);
2925 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002928 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05002929 if (posdefaults > 0) {
2930 PyObject *defs = PyTuple_New(posdefaults);
2931 if (defs == NULL) {
2932 Py_DECREF(func);
2933 goto error;
2934 }
2935 while (--posdefaults >= 0)
2936 PyTuple_SET_ITEM(defs, posdefaults, POP());
2937 if (PyFunction_SetDefaults(func, defs) != 0) {
2938 /* Can't happen unless
2939 PyFunction_SetDefaults changes. */
2940 Py_DECREF(defs);
2941 Py_DECREF(func);
2942 goto error;
2943 }
2944 Py_DECREF(defs);
2945 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 PUSH(func);
2947 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002949
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 TARGET(BUILD_SLICE) {
2951 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 step = NULL;
2956 stop = POP();
2957 start = TOP();
2958 slice = PySlice_New(start, stop, step);
2959 Py_DECREF(start);
2960 Py_DECREF(stop);
2961 Py_XDECREF(step);
2962 SET_TOP(slice);
2963 if (slice == NULL)
2964 goto error;
2965 DISPATCH();
2966 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002967
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 opcode = NEXTOP();
2970 oparg = oparg<<16 | NEXTARG();
2971 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002973
Antoine Pitrou042b1282010-08-13 21:15:58 +00002974#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002976#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 default:
2978 fprintf(stderr,
2979 "XXX lineno: %d, opcode: %d\n",
2980 PyFrame_GetLineNumber(f),
2981 opcode);
2982 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002984
2985#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002987#endif
2988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002990
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 /* This should never be reached. Every opcode should end with DISPATCH()
2992 or goto error. */
2993 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00002994
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002997
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 assert(why == WHY_NOT);
2999 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003000
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 /* Double-check exception status. */
3002 if (!PyErr_Occurred())
3003 PyErr_SetString(PyExc_SystemError,
3004 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00003005
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 /* Log traceback info. */
3007 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003008
Benjamin Peterson51f46162013-01-23 08:38:47 -05003009 if (tstate->c_tracefunc != NULL)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003011
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003012fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003013 assert(why != WHY_NOT);
3014
3015 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 while (why != WHY_NOT && f->f_iblock > 0) {
3017 /* Peek at the current block. */
3018 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 assert(why != WHY_YIELD);
3021 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3022 why = WHY_NOT;
3023 JUMPTO(PyLong_AS_LONG(retval));
3024 Py_DECREF(retval);
3025 break;
3026 }
3027 /* Now we have to pop the block. */
3028 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 if (b->b_type == EXCEPT_HANDLER) {
3031 UNWIND_EXCEPT_HANDLER(b);
3032 continue;
3033 }
3034 UNWIND_BLOCK(b);
3035 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3036 why = WHY_NOT;
3037 JUMPTO(b->b_handler);
3038 break;
3039 }
3040 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3041 || b->b_type == SETUP_FINALLY)) {
3042 PyObject *exc, *val, *tb;
3043 int handler = b->b_handler;
3044 /* Beware, this invalidates all b->b_* fields */
3045 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3046 PUSH(tstate->exc_traceback);
3047 PUSH(tstate->exc_value);
3048 if (tstate->exc_type != NULL) {
3049 PUSH(tstate->exc_type);
3050 }
3051 else {
3052 Py_INCREF(Py_None);
3053 PUSH(Py_None);
3054 }
3055 PyErr_Fetch(&exc, &val, &tb);
3056 /* Make the raw exception data
3057 available to the handler,
3058 so a program can emulate the
3059 Python main loop. */
3060 PyErr_NormalizeException(
3061 &exc, &val, &tb);
3062 PyException_SetTraceback(val, tb);
3063 Py_INCREF(exc);
3064 tstate->exc_type = exc;
3065 Py_INCREF(val);
3066 tstate->exc_value = val;
3067 tstate->exc_traceback = tb;
3068 if (tb == NULL)
3069 tb = Py_None;
3070 Py_INCREF(tb);
3071 PUSH(tb);
3072 PUSH(val);
3073 PUSH(exc);
3074 why = WHY_NOT;
3075 JUMPTO(handler);
3076 break;
3077 }
3078 if (b->b_type == SETUP_FINALLY) {
3079 if (why & (WHY_RETURN | WHY_CONTINUE))
3080 PUSH(retval);
3081 PUSH(PyLong_FromLong((long)why));
3082 why = WHY_NOT;
3083 JUMPTO(b->b_handler);
3084 break;
3085 }
3086 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 if (why != WHY_NOT)
3091 break;
3092 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 assert(why != WHY_YIELD);
3097 /* Pop remaining stack entries. */
3098 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 PyObject *o = POP();
3100 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 if (why != WHY_RETURN)
3104 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003105
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003106fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003107 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3108 /* The purpose of this block is to put aside the generator's exception
3109 state and restore that of the calling frame. If the current
3110 exception state is from the caller, we clear the exception values
3111 on the generator frame, so they are not swapped back in latter. The
3112 origin of the current exception state is determined by checking for
3113 except handler blocks, which we must be in iff a new exception
3114 state came into existence in this frame. (An uncaught exception
3115 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3116 int i;
3117 for (i = 0; i < f->f_iblock; i++)
3118 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3119 break;
3120 if (i == f->f_iblock)
3121 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003122 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003123 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003124 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003125 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003128 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 if (why == WHY_RETURN || why == WHY_YIELD) {
3130 if (call_trace(tstate->c_tracefunc,
3131 tstate->c_traceobj, f,
3132 PyTrace_RETURN, retval)) {
3133 Py_XDECREF(retval);
3134 retval = NULL;
3135 why = WHY_EXCEPTION;
3136 }
3137 }
3138 else if (why == WHY_EXCEPTION) {
3139 call_trace_protected(tstate->c_tracefunc,
3140 tstate->c_traceobj, f,
3141 PyTrace_RETURN, NULL);
3142 }
3143 }
3144 if (tstate->c_profilefunc) {
3145 if (why == WHY_EXCEPTION)
3146 call_trace_protected(tstate->c_profilefunc,
3147 tstate->c_profileobj, f,
3148 PyTrace_RETURN, NULL);
3149 else if (call_trace(tstate->c_profilefunc,
3150 tstate->c_profileobj, f,
3151 PyTrace_RETURN, retval)) {
3152 Py_XDECREF(retval);
3153 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003154 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 }
3156 }
3157 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003160exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 Py_LeaveRecursiveCall();
3162 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003165}
3166
Benjamin Petersonb204a422011-06-05 22:04:07 -05003167static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003168format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3169{
3170 int err;
3171 Py_ssize_t len = PyList_GET_SIZE(names);
3172 PyObject *name_str, *comma, *tail, *tmp;
3173
3174 assert(PyList_CheckExact(names));
3175 assert(len >= 1);
3176 /* Deal with the joys of natural language. */
3177 switch (len) {
3178 case 1:
3179 name_str = PyList_GET_ITEM(names, 0);
3180 Py_INCREF(name_str);
3181 break;
3182 case 2:
3183 name_str = PyUnicode_FromFormat("%U and %U",
3184 PyList_GET_ITEM(names, len - 2),
3185 PyList_GET_ITEM(names, len - 1));
3186 break;
3187 default:
3188 tail = PyUnicode_FromFormat(", %U, and %U",
3189 PyList_GET_ITEM(names, len - 2),
3190 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003191 if (tail == NULL)
3192 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003193 /* Chop off the last two objects in the list. This shouldn't actually
3194 fail, but we can't be too careful. */
3195 err = PyList_SetSlice(names, len - 2, len, NULL);
3196 if (err == -1) {
3197 Py_DECREF(tail);
3198 return;
3199 }
3200 /* Stitch everything up into a nice comma-separated list. */
3201 comma = PyUnicode_FromString(", ");
3202 if (comma == NULL) {
3203 Py_DECREF(tail);
3204 return;
3205 }
3206 tmp = PyUnicode_Join(comma, names);
3207 Py_DECREF(comma);
3208 if (tmp == NULL) {
3209 Py_DECREF(tail);
3210 return;
3211 }
3212 name_str = PyUnicode_Concat(tmp, tail);
3213 Py_DECREF(tmp);
3214 Py_DECREF(tail);
3215 break;
3216 }
3217 if (name_str == NULL)
3218 return;
3219 PyErr_Format(PyExc_TypeError,
3220 "%U() missing %i required %s argument%s: %U",
3221 co->co_name,
3222 len,
3223 kind,
3224 len == 1 ? "" : "s",
3225 name_str);
3226 Py_DECREF(name_str);
3227}
3228
3229static void
3230missing_arguments(PyCodeObject *co, int missing, int defcount,
3231 PyObject **fastlocals)
3232{
3233 int i, j = 0;
3234 int start, end;
3235 int positional = defcount != -1;
3236 const char *kind = positional ? "positional" : "keyword-only";
3237 PyObject *missing_names;
3238
3239 /* Compute the names of the arguments that are missing. */
3240 missing_names = PyList_New(missing);
3241 if (missing_names == NULL)
3242 return;
3243 if (positional) {
3244 start = 0;
3245 end = co->co_argcount - defcount;
3246 }
3247 else {
3248 start = co->co_argcount;
3249 end = start + co->co_kwonlyargcount;
3250 }
3251 for (i = start; i < end; i++) {
3252 if (GETLOCAL(i) == NULL) {
3253 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3254 PyObject *name = PyObject_Repr(raw);
3255 if (name == NULL) {
3256 Py_DECREF(missing_names);
3257 return;
3258 }
3259 PyList_SET_ITEM(missing_names, j++, name);
3260 }
3261 }
3262 assert(j == missing);
3263 format_missing(kind, co, missing_names);
3264 Py_DECREF(missing_names);
3265}
3266
3267static void
3268too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003269{
3270 int plural;
3271 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003272 int i;
3273 PyObject *sig, *kwonly_sig;
3274
Benjamin Petersone109c702011-06-24 09:37:26 -05003275 assert((co->co_flags & CO_VARARGS) == 0);
3276 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003277 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003278 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003279 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003280 if (defcount) {
3281 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003282 plural = 1;
3283 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3284 }
3285 else {
3286 plural = co->co_argcount != 1;
3287 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3288 }
3289 if (sig == NULL)
3290 return;
3291 if (kwonly_given) {
3292 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3293 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3294 kwonly_given != 1 ? "s" : "");
3295 if (kwonly_sig == NULL) {
3296 Py_DECREF(sig);
3297 return;
3298 }
3299 }
3300 else {
3301 /* This will not fail. */
3302 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003303 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003304 }
3305 PyErr_Format(PyExc_TypeError,
3306 "%U() takes %U positional argument%s but %d%U %s given",
3307 co->co_name,
3308 sig,
3309 plural ? "s" : "",
3310 given,
3311 kwonly_sig,
3312 given == 1 && !kwonly_given ? "was" : "were");
3313 Py_DECREF(sig);
3314 Py_DECREF(kwonly_sig);
3315}
3316
Guido van Rossumc2e20742006-02-27 22:32:47 +00003317/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003318 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003319 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003320
Tim Peters6d6c1a32001-08-02 04:15:00 +00003321PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003322PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyObject **args, int argcount, PyObject **kws, int kwcount,
3324 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003325{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003326 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 register PyFrameObject *f;
3328 register PyObject *retval = NULL;
3329 register PyObject **fastlocals, **freevars;
3330 PyThreadState *tstate = PyThreadState_GET();
3331 PyObject *x, *u;
3332 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003333 int i;
3334 int n = argcount;
3335 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 if (globals == NULL) {
3338 PyErr_SetString(PyExc_SystemError,
3339 "PyEval_EvalCodeEx: NULL globals");
3340 return NULL;
3341 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 assert(tstate != NULL);
3344 assert(globals != NULL);
3345 f = PyFrame_New(tstate, co, globals, locals);
3346 if (f == NULL)
3347 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003348
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 fastlocals = f->f_localsplus;
3350 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003351
Benjamin Petersonb204a422011-06-05 22:04:07 -05003352 /* Parse arguments. */
3353 if (co->co_flags & CO_VARKEYWORDS) {
3354 kwdict = PyDict_New();
3355 if (kwdict == NULL)
3356 goto fail;
3357 i = total_args;
3358 if (co->co_flags & CO_VARARGS)
3359 i++;
3360 SETLOCAL(i, kwdict);
3361 }
3362 if (argcount > co->co_argcount)
3363 n = co->co_argcount;
3364 for (i = 0; i < n; i++) {
3365 x = args[i];
3366 Py_INCREF(x);
3367 SETLOCAL(i, x);
3368 }
3369 if (co->co_flags & CO_VARARGS) {
3370 u = PyTuple_New(argcount - n);
3371 if (u == NULL)
3372 goto fail;
3373 SETLOCAL(total_args, u);
3374 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 x = args[i];
3376 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003377 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003379 }
3380 for (i = 0; i < kwcount; i++) {
3381 PyObject **co_varnames;
3382 PyObject *keyword = kws[2*i];
3383 PyObject *value = kws[2*i + 1];
3384 int j;
3385 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3386 PyErr_Format(PyExc_TypeError,
3387 "%U() keywords must be strings",
3388 co->co_name);
3389 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003391 /* Speed hack: do raw pointer compares. As names are
3392 normally interned this should almost always hit. */
3393 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3394 for (j = 0; j < total_args; j++) {
3395 PyObject *nm = co_varnames[j];
3396 if (nm == keyword)
3397 goto kw_found;
3398 }
3399 /* Slow fallback, just in case */
3400 for (j = 0; j < total_args; j++) {
3401 PyObject *nm = co_varnames[j];
3402 int cmp = PyObject_RichCompareBool(
3403 keyword, nm, Py_EQ);
3404 if (cmp > 0)
3405 goto kw_found;
3406 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003408 }
3409 if (j >= total_args && kwdict == NULL) {
3410 PyErr_Format(PyExc_TypeError,
3411 "%U() got an unexpected "
3412 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 co->co_name,
3414 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003415 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003417 PyDict_SetItem(kwdict, keyword, value);
3418 continue;
3419 kw_found:
3420 if (GETLOCAL(j) != NULL) {
3421 PyErr_Format(PyExc_TypeError,
3422 "%U() got multiple "
3423 "values for argument '%S'",
3424 co->co_name,
3425 keyword);
3426 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003428 Py_INCREF(value);
3429 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003431 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003432 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 goto fail;
3434 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003435 if (argcount < co->co_argcount) {
3436 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003437 int missing = 0;
3438 for (i = argcount; i < m; i++)
3439 if (GETLOCAL(i) == NULL)
3440 missing++;
3441 if (missing) {
3442 missing_arguments(co, missing, defcount, fastlocals);
3443 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003444 }
3445 if (n > m)
3446 i = n - m;
3447 else
3448 i = 0;
3449 for (; i < defcount; i++) {
3450 if (GETLOCAL(m+i) == NULL) {
3451 PyObject *def = defs[i];
3452 Py_INCREF(def);
3453 SETLOCAL(m+i, def);
3454 }
3455 }
3456 }
3457 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003458 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003459 for (i = co->co_argcount; i < total_args; i++) {
3460 PyObject *name;
3461 if (GETLOCAL(i) != NULL)
3462 continue;
3463 name = PyTuple_GET_ITEM(co->co_varnames, i);
3464 if (kwdefs != NULL) {
3465 PyObject *def = PyDict_GetItem(kwdefs, name);
3466 if (def) {
3467 Py_INCREF(def);
3468 SETLOCAL(i, def);
3469 continue;
3470 }
3471 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003472 missing++;
3473 }
3474 if (missing) {
3475 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003476 goto fail;
3477 }
3478 }
3479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003481 vars into frame. */
3482 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003484 int arg;
3485 /* Possibly account for the cell variable being an argument. */
3486 if (co->co_cell2arg != NULL &&
3487 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3488 c = PyCell_New(GETLOCAL(arg));
3489 else
3490 c = PyCell_New(NULL);
3491 if (c == NULL)
3492 goto fail;
3493 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003494 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003495 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3496 PyObject *o = PyTuple_GET_ITEM(closure, i);
3497 Py_INCREF(o);
3498 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 if (co->co_flags & CO_GENERATOR) {
3502 /* Don't need to keep the reference to f_back, it will be set
3503 * when the generator is resumed. */
3504 Py_XDECREF(f->f_back);
3505 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 /* Create a new generator that owns the ready to run frame
3510 * and return that as the value. */
3511 return PyGen_New(f);
3512 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003515
Thomas Woutersce272b62007-09-19 21:19:28 +00003516fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 /* decref'ing the frame can cause __del__ methods to get invoked,
3519 which can call back into Python. While we're done with the
3520 current Python frame (f), the associated C stack is still in use,
3521 so recursion_depth must be boosted for the duration.
3522 */
3523 assert(tstate != NULL);
3524 ++tstate->recursion_depth;
3525 Py_DECREF(f);
3526 --tstate->recursion_depth;
3527 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003528}
3529
3530
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003531static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003532special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003535 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003537 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 return NULL;
3539 }
3540 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003541}
3542
3543
Benjamin Peterson87880242011-07-03 16:48:31 -05003544/* These 3 functions deal with the exception state of generators. */
3545
3546static void
3547save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3548{
3549 PyObject *type, *value, *traceback;
3550 Py_XINCREF(tstate->exc_type);
3551 Py_XINCREF(tstate->exc_value);
3552 Py_XINCREF(tstate->exc_traceback);
3553 type = f->f_exc_type;
3554 value = f->f_exc_value;
3555 traceback = f->f_exc_traceback;
3556 f->f_exc_type = tstate->exc_type;
3557 f->f_exc_value = tstate->exc_value;
3558 f->f_exc_traceback = tstate->exc_traceback;
3559 Py_XDECREF(type);
3560 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003561 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003562}
3563
3564static void
3565swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3566{
3567 PyObject *tmp;
3568 tmp = tstate->exc_type;
3569 tstate->exc_type = f->f_exc_type;
3570 f->f_exc_type = tmp;
3571 tmp = tstate->exc_value;
3572 tstate->exc_value = f->f_exc_value;
3573 f->f_exc_value = tmp;
3574 tmp = tstate->exc_traceback;
3575 tstate->exc_traceback = f->f_exc_traceback;
3576 f->f_exc_traceback = tmp;
3577}
3578
3579static void
3580restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3581{
3582 PyObject *type, *value, *tb;
3583 type = tstate->exc_type;
3584 value = tstate->exc_value;
3585 tb = tstate->exc_traceback;
3586 tstate->exc_type = f->f_exc_type;
3587 tstate->exc_value = f->f_exc_value;
3588 tstate->exc_traceback = f->f_exc_traceback;
3589 f->f_exc_type = NULL;
3590 f->f_exc_value = NULL;
3591 f->f_exc_traceback = NULL;
3592 Py_XDECREF(type);
3593 Py_XDECREF(value);
3594 Py_XDECREF(tb);
3595}
3596
3597
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003598/* Logic for the raise statement (too complicated for inlining).
3599 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003600static int
Collin Winter828f04a2007-08-31 00:04:24 +00003601do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003602{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 if (exc == NULL) {
3606 /* Reraise */
3607 PyThreadState *tstate = PyThreadState_GET();
3608 PyObject *tb;
3609 type = tstate->exc_type;
3610 value = tstate->exc_value;
3611 tb = tstate->exc_traceback;
3612 if (type == Py_None) {
3613 PyErr_SetString(PyExc_RuntimeError,
3614 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003615 return 0;
3616 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 Py_XINCREF(type);
3618 Py_XINCREF(value);
3619 Py_XINCREF(tb);
3620 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003621 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 /* We support the following forms of raise:
3625 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003626 raise <instance>
3627 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 if (PyExceptionClass_Check(exc)) {
3630 type = exc;
3631 value = PyObject_CallObject(exc, NULL);
3632 if (value == NULL)
3633 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003634 if (!PyExceptionInstance_Check(value)) {
3635 PyErr_Format(PyExc_TypeError,
3636 "calling %R should have returned an instance of "
3637 "BaseException, not %R",
3638 type, Py_TYPE(value));
3639 goto raise_error;
3640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 }
3642 else if (PyExceptionInstance_Check(exc)) {
3643 value = exc;
3644 type = PyExceptionInstance_Class(exc);
3645 Py_INCREF(type);
3646 }
3647 else {
3648 /* Not something you can raise. You get an exception
3649 anyway, just not what you specified :-) */
3650 Py_DECREF(exc);
3651 PyErr_SetString(PyExc_TypeError,
3652 "exceptions must derive from BaseException");
3653 goto raise_error;
3654 }
Collin Winter828f04a2007-08-31 00:04:24 +00003655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 if (cause) {
3657 PyObject *fixed_cause;
3658 if (PyExceptionClass_Check(cause)) {
3659 fixed_cause = PyObject_CallObject(cause, NULL);
3660 if (fixed_cause == NULL)
3661 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003662 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003664 else if (PyExceptionInstance_Check(cause)) {
3665 fixed_cause = cause;
3666 }
3667 else if (cause == Py_None) {
3668 Py_DECREF(cause);
3669 fixed_cause = NULL;
3670 }
3671 else {
3672 PyErr_SetString(PyExc_TypeError,
3673 "exception causes must derive from "
3674 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 goto raise_error;
3676 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003677 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 }
Collin Winter828f04a2007-08-31 00:04:24 +00003679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 PyErr_SetObject(type, value);
3681 /* PyErr_SetObject incref's its arguments */
3682 Py_XDECREF(value);
3683 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003684 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003685
3686raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 Py_XDECREF(value);
3688 Py_XDECREF(type);
3689 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003690 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003691}
3692
Tim Petersd6d010b2001-06-21 02:49:55 +00003693/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003694 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003695
Guido van Rossum0368b722007-05-11 16:50:42 +00003696 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3697 with a variable target.
3698*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003699
Barry Warsawe42b18f1997-08-25 22:13:04 +00003700static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003701unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 int i = 0, j = 0;
3704 Py_ssize_t ll = 0;
3705 PyObject *it; /* iter(v) */
3706 PyObject *w;
3707 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 it = PyObject_GetIter(v);
3712 if (it == NULL)
3713 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 for (; i < argcnt; i++) {
3716 w = PyIter_Next(it);
3717 if (w == NULL) {
3718 /* Iterator done, via error or exhaustion. */
3719 if (!PyErr_Occurred()) {
3720 PyErr_Format(PyExc_ValueError,
3721 "need more than %d value%s to unpack",
3722 i, i == 1 ? "" : "s");
3723 }
3724 goto Error;
3725 }
3726 *--sp = w;
3727 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 if (argcntafter == -1) {
3730 /* We better have exhausted the iterator now. */
3731 w = PyIter_Next(it);
3732 if (w == NULL) {
3733 if (PyErr_Occurred())
3734 goto Error;
3735 Py_DECREF(it);
3736 return 1;
3737 }
3738 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003739 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3740 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 goto Error;
3742 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 l = PySequence_List(it);
3745 if (l == NULL)
3746 goto Error;
3747 *--sp = l;
3748 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 ll = PyList_GET_SIZE(l);
3751 if (ll < argcntafter) {
3752 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3753 argcnt + ll);
3754 goto Error;
3755 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 /* Pop the "after-variable" args off the list. */
3758 for (j = argcntafter; j > 0; j--, i++) {
3759 *--sp = PyList_GET_ITEM(l, ll - j);
3760 }
3761 /* Resize the list. */
3762 Py_SIZE(l) = ll - argcntafter;
3763 Py_DECREF(it);
3764 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003765
Tim Petersd6d010b2001-06-21 02:49:55 +00003766Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003767 for (; i > 0; i--, sp++)
3768 Py_DECREF(*sp);
3769 Py_XDECREF(it);
3770 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003771}
3772
3773
Guido van Rossum96a42c81992-01-12 02:29:51 +00003774#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003775static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003776prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 printf("%s ", str);
3779 if (PyObject_Print(v, stdout, 0) != 0)
3780 PyErr_Clear(); /* Don't know what else to do */
3781 printf("\n");
3782 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003783}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003784#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003785
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003786static void
Fred Drake5755ce62001-06-27 19:19:46 +00003787call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 PyObject *type, *value, *traceback, *arg;
3790 int err;
3791 PyErr_Fetch(&type, &value, &traceback);
3792 if (value == NULL) {
3793 value = Py_None;
3794 Py_INCREF(value);
3795 }
3796 arg = PyTuple_Pack(3, type, value, traceback);
3797 if (arg == NULL) {
3798 PyErr_Restore(type, value, traceback);
3799 return;
3800 }
3801 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3802 Py_DECREF(arg);
3803 if (err == 0)
3804 PyErr_Restore(type, value, traceback);
3805 else {
3806 Py_XDECREF(type);
3807 Py_XDECREF(value);
3808 Py_XDECREF(traceback);
3809 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003810}
3811
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003812static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003813call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 PyObject *type, *value, *traceback;
3817 int err;
3818 PyErr_Fetch(&type, &value, &traceback);
3819 err = call_trace(func, obj, frame, what, arg);
3820 if (err == 0)
3821 {
3822 PyErr_Restore(type, value, traceback);
3823 return 0;
3824 }
3825 else {
3826 Py_XDECREF(type);
3827 Py_XDECREF(value);
3828 Py_XDECREF(traceback);
3829 return -1;
3830 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003831}
3832
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003833static int
Fred Drake5755ce62001-06-27 19:19:46 +00003834call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003836{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 register PyThreadState *tstate = frame->f_tstate;
3838 int result;
3839 if (tstate->tracing)
3840 return 0;
3841 tstate->tracing++;
3842 tstate->use_tracing = 0;
3843 result = func(obj, frame, what, arg);
3844 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3845 || (tstate->c_profilefunc != NULL));
3846 tstate->tracing--;
3847 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003848}
3849
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003850PyObject *
3851_PyEval_CallTracing(PyObject *func, PyObject *args)
3852{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 PyFrameObject *frame = PyEval_GetFrame();
3854 PyThreadState *tstate = frame->f_tstate;
3855 int save_tracing = tstate->tracing;
3856 int save_use_tracing = tstate->use_tracing;
3857 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003859 tstate->tracing = 0;
3860 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3861 || (tstate->c_profilefunc != NULL));
3862 result = PyObject_Call(func, args, NULL);
3863 tstate->tracing = save_tracing;
3864 tstate->use_tracing = save_use_tracing;
3865 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003866}
3867
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003868/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003869static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003870maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3872 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 int result = 0;
3875 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 /* If the last instruction executed isn't in the current
3878 instruction window, reset the window.
3879 */
3880 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3881 PyAddrPair bounds;
3882 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3883 &bounds);
3884 *instr_lb = bounds.ap_lower;
3885 *instr_ub = bounds.ap_upper;
3886 }
3887 /* If the last instruction falls at the start of a line or if
3888 it represents a jump backwards, update the frame's line
3889 number and call the trace function. */
3890 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3891 frame->f_lineno = line;
3892 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3893 }
3894 *instr_prev = frame->f_lasti;
3895 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003896}
3897
Fred Drake5755ce62001-06-27 19:19:46 +00003898void
3899PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 PyThreadState *tstate = PyThreadState_GET();
3902 PyObject *temp = tstate->c_profileobj;
3903 Py_XINCREF(arg);
3904 tstate->c_profilefunc = NULL;
3905 tstate->c_profileobj = NULL;
3906 /* Must make sure that tracing is not ignored if 'temp' is freed */
3907 tstate->use_tracing = tstate->c_tracefunc != NULL;
3908 Py_XDECREF(temp);
3909 tstate->c_profilefunc = func;
3910 tstate->c_profileobj = arg;
3911 /* Flag that tracing or profiling is turned on */
3912 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003913}
3914
3915void
3916PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 PyThreadState *tstate = PyThreadState_GET();
3919 PyObject *temp = tstate->c_traceobj;
3920 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3921 Py_XINCREF(arg);
3922 tstate->c_tracefunc = NULL;
3923 tstate->c_traceobj = NULL;
3924 /* Must make sure that profiling is not ignored if 'temp' is freed */
3925 tstate->use_tracing = tstate->c_profilefunc != NULL;
3926 Py_XDECREF(temp);
3927 tstate->c_tracefunc = func;
3928 tstate->c_traceobj = arg;
3929 /* Flag that tracing or profiling is turned on */
3930 tstate->use_tracing = ((func != NULL)
3931 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003932}
3933
Guido van Rossumb209a111997-04-29 18:18:01 +00003934PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003935PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003936{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 PyFrameObject *current_frame = PyEval_GetFrame();
3938 if (current_frame == NULL)
3939 return PyThreadState_GET()->interp->builtins;
3940 else
3941 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003942}
3943
Guido van Rossumb209a111997-04-29 18:18:01 +00003944PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003945PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003946{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 PyFrameObject *current_frame = PyEval_GetFrame();
3948 if (current_frame == NULL)
3949 return NULL;
3950 PyFrame_FastToLocals(current_frame);
3951 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003952}
3953
Guido van Rossumb209a111997-04-29 18:18:01 +00003954PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003955PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 PyFrameObject *current_frame = PyEval_GetFrame();
3958 if (current_frame == NULL)
3959 return NULL;
3960 else
3961 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003962}
3963
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003964PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003965PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003966{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 PyThreadState *tstate = PyThreadState_GET();
3968 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003969}
3970
Guido van Rossum6135a871995-01-09 17:53:26 +00003971int
Tim Peters5ba58662001-07-16 02:29:45 +00003972PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003973{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003974 PyFrameObject *current_frame = PyEval_GetFrame();
3975 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 if (current_frame != NULL) {
3978 const int codeflags = current_frame->f_code->co_flags;
3979 const int compilerflags = codeflags & PyCF_MASK;
3980 if (compilerflags) {
3981 result = 1;
3982 cf->cf_flags |= compilerflags;
3983 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003984#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 if (codeflags & CO_GENERATOR_ALLOWED) {
3986 result = 1;
3987 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3988 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003989#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 }
3991 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003992}
3993
Guido van Rossum3f5da241990-12-20 15:06:42 +00003994
Guido van Rossum681d79a1995-07-18 14:51:37 +00003995/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003996 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003997
Guido van Rossumb209a111997-04-29 18:18:01 +00003998PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003999PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 if (arg == NULL) {
4004 arg = PyTuple_New(0);
4005 if (arg == NULL)
4006 return NULL;
4007 }
4008 else if (!PyTuple_Check(arg)) {
4009 PyErr_SetString(PyExc_TypeError,
4010 "argument list must be a tuple");
4011 return NULL;
4012 }
4013 else
4014 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004016 if (kw != NULL && !PyDict_Check(kw)) {
4017 PyErr_SetString(PyExc_TypeError,
4018 "keyword list must be a dictionary");
4019 Py_DECREF(arg);
4020 return NULL;
4021 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 result = PyObject_Call(func, arg, kw);
4024 Py_DECREF(arg);
4025 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004026}
4027
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004028const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004029PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 if (PyMethod_Check(func))
4032 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4033 else if (PyFunction_Check(func))
4034 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4035 else if (PyCFunction_Check(func))
4036 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4037 else
4038 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004039}
4040
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004041const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004042PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 if (PyMethod_Check(func))
4045 return "()";
4046 else if (PyFunction_Check(func))
4047 return "()";
4048 else if (PyCFunction_Check(func))
4049 return "()";
4050 else
4051 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004052}
4053
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004054static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004055err_args(PyObject *func, int flags, int nargs)
4056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 if (flags & METH_NOARGS)
4058 PyErr_Format(PyExc_TypeError,
4059 "%.200s() takes no arguments (%d given)",
4060 ((PyCFunctionObject *)func)->m_ml->ml_name,
4061 nargs);
4062 else
4063 PyErr_Format(PyExc_TypeError,
4064 "%.200s() takes exactly one argument (%d given)",
4065 ((PyCFunctionObject *)func)->m_ml->ml_name,
4066 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004067}
4068
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004069#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004070if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 if (call_trace(tstate->c_profilefunc, \
4072 tstate->c_profileobj, \
4073 tstate->frame, PyTrace_C_CALL, \
4074 func)) { \
4075 x = NULL; \
4076 } \
4077 else { \
4078 x = call; \
4079 if (tstate->c_profilefunc != NULL) { \
4080 if (x == NULL) { \
4081 call_trace_protected(tstate->c_profilefunc, \
4082 tstate->c_profileobj, \
4083 tstate->frame, PyTrace_C_EXCEPTION, \
4084 func); \
4085 /* XXX should pass (type, value, tb) */ \
4086 } else { \
4087 if (call_trace(tstate->c_profilefunc, \
4088 tstate->c_profileobj, \
4089 tstate->frame, PyTrace_C_RETURN, \
4090 func)) { \
4091 Py_DECREF(x); \
4092 x = NULL; \
4093 } \
4094 } \
4095 } \
4096 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004097} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 x = call; \
4099 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004100
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004101static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004102call_function(PyObject ***pp_stack, int oparg
4103#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004105#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 int na = oparg & 0xff;
4109 int nk = (oparg>>8) & 0xff;
4110 int n = na + 2 * nk;
4111 PyObject **pfunc = (*pp_stack) - n - 1;
4112 PyObject *func = *pfunc;
4113 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004115 /* Always dispatch PyCFunction first, because these are
4116 presumed to be the most frequent callable object.
4117 */
4118 if (PyCFunction_Check(func) && nk == 0) {
4119 int flags = PyCFunction_GET_FLAGS(func);
4120 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 PCALL(PCALL_CFUNCTION);
4123 if (flags & (METH_NOARGS | METH_O)) {
4124 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4125 PyObject *self = PyCFunction_GET_SELF(func);
4126 if (flags & METH_NOARGS && na == 0) {
4127 C_TRACE(x, (*meth)(self,NULL));
4128 }
4129 else if (flags & METH_O && na == 1) {
4130 PyObject *arg = EXT_POP(*pp_stack);
4131 C_TRACE(x, (*meth)(self,arg));
4132 Py_DECREF(arg);
4133 }
4134 else {
4135 err_args(func, flags, na);
4136 x = NULL;
4137 }
4138 }
4139 else {
4140 PyObject *callargs;
4141 callargs = load_args(pp_stack, na);
4142 READ_TIMESTAMP(*pintr0);
4143 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4144 READ_TIMESTAMP(*pintr1);
4145 Py_XDECREF(callargs);
4146 }
4147 } else {
4148 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4149 /* optimize access to bound methods */
4150 PyObject *self = PyMethod_GET_SELF(func);
4151 PCALL(PCALL_METHOD);
4152 PCALL(PCALL_BOUND_METHOD);
4153 Py_INCREF(self);
4154 func = PyMethod_GET_FUNCTION(func);
4155 Py_INCREF(func);
4156 Py_DECREF(*pfunc);
4157 *pfunc = self;
4158 na++;
4159 n++;
4160 } else
4161 Py_INCREF(func);
4162 READ_TIMESTAMP(*pintr0);
4163 if (PyFunction_Check(func))
4164 x = fast_function(func, pp_stack, n, na, nk);
4165 else
4166 x = do_call(func, pp_stack, na, nk);
4167 READ_TIMESTAMP(*pintr1);
4168 Py_DECREF(func);
4169 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 /* Clear the stack of the function object. Also removes
4172 the arguments in case they weren't consumed already
4173 (fast_function() and err_args() leave them on the stack).
4174 */
4175 while ((*pp_stack) > pfunc) {
4176 w = EXT_POP(*pp_stack);
4177 Py_DECREF(w);
4178 PCALL(PCALL_POP);
4179 }
4180 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004181}
4182
Jeremy Hylton192690e2002-08-16 18:36:11 +00004183/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004184 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004185 For the simplest case -- a function that takes only positional
4186 arguments and is called with only positional arguments -- it
4187 inlines the most primitive frame setup code from
4188 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4189 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004190*/
4191
4192static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004193fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004194{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4196 PyObject *globals = PyFunction_GET_GLOBALS(func);
4197 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4198 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4199 PyObject **d = NULL;
4200 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 PCALL(PCALL_FUNCTION);
4203 PCALL(PCALL_FAST_FUNCTION);
4204 if (argdefs == NULL && co->co_argcount == n &&
4205 co->co_kwonlyargcount == 0 && nk==0 &&
4206 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4207 PyFrameObject *f;
4208 PyObject *retval = NULL;
4209 PyThreadState *tstate = PyThreadState_GET();
4210 PyObject **fastlocals, **stack;
4211 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 PCALL(PCALL_FASTER_FUNCTION);
4214 assert(globals != NULL);
4215 /* XXX Perhaps we should create a specialized
4216 PyFrame_New() that doesn't take locals, but does
4217 take builtins without sanity checking them.
4218 */
4219 assert(tstate != NULL);
4220 f = PyFrame_New(tstate, co, globals, NULL);
4221 if (f == NULL)
4222 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 fastlocals = f->f_localsplus;
4225 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 for (i = 0; i < n; i++) {
4228 Py_INCREF(*stack);
4229 fastlocals[i] = *stack++;
4230 }
4231 retval = PyEval_EvalFrameEx(f,0);
4232 ++tstate->recursion_depth;
4233 Py_DECREF(f);
4234 --tstate->recursion_depth;
4235 return retval;
4236 }
4237 if (argdefs != NULL) {
4238 d = &PyTuple_GET_ITEM(argdefs, 0);
4239 nd = Py_SIZE(argdefs);
4240 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004241 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 (PyObject *)NULL, (*pp_stack)-n, na,
4243 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4244 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004245}
4246
4247static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004248update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4249 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004250{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 PyObject *kwdict = NULL;
4252 if (orig_kwdict == NULL)
4253 kwdict = PyDict_New();
4254 else {
4255 kwdict = PyDict_Copy(orig_kwdict);
4256 Py_DECREF(orig_kwdict);
4257 }
4258 if (kwdict == NULL)
4259 return NULL;
4260 while (--nk >= 0) {
4261 int err;
4262 PyObject *value = EXT_POP(*pp_stack);
4263 PyObject *key = EXT_POP(*pp_stack);
4264 if (PyDict_GetItem(kwdict, key) != NULL) {
4265 PyErr_Format(PyExc_TypeError,
4266 "%.200s%s got multiple values "
4267 "for keyword argument '%U'",
4268 PyEval_GetFuncName(func),
4269 PyEval_GetFuncDesc(func),
4270 key);
4271 Py_DECREF(key);
4272 Py_DECREF(value);
4273 Py_DECREF(kwdict);
4274 return NULL;
4275 }
4276 err = PyDict_SetItem(kwdict, key, value);
4277 Py_DECREF(key);
4278 Py_DECREF(value);
4279 if (err) {
4280 Py_DECREF(kwdict);
4281 return NULL;
4282 }
4283 }
4284 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004285}
4286
4287static PyObject *
4288update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004289 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 callargs = PyTuple_New(nstack + nstar);
4294 if (callargs == NULL) {
4295 return NULL;
4296 }
4297 if (nstar) {
4298 int i;
4299 for (i = 0; i < nstar; i++) {
4300 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4301 Py_INCREF(a);
4302 PyTuple_SET_ITEM(callargs, nstack + i, a);
4303 }
4304 }
4305 while (--nstack >= 0) {
4306 w = EXT_POP(*pp_stack);
4307 PyTuple_SET_ITEM(callargs, nstack, w);
4308 }
4309 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004310}
4311
4312static PyObject *
4313load_args(PyObject ***pp_stack, int na)
4314{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 PyObject *args = PyTuple_New(na);
4316 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 if (args == NULL)
4319 return NULL;
4320 while (--na >= 0) {
4321 w = EXT_POP(*pp_stack);
4322 PyTuple_SET_ITEM(args, na, w);
4323 }
4324 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004325}
4326
4327static PyObject *
4328do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 PyObject *callargs = NULL;
4331 PyObject *kwdict = NULL;
4332 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 if (nk > 0) {
4335 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4336 if (kwdict == NULL)
4337 goto call_fail;
4338 }
4339 callargs = load_args(pp_stack, na);
4340 if (callargs == NULL)
4341 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004342#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 /* At this point, we have to look at the type of func to
4344 update the call stats properly. Do it here so as to avoid
4345 exposing the call stats machinery outside ceval.c
4346 */
4347 if (PyFunction_Check(func))
4348 PCALL(PCALL_FUNCTION);
4349 else if (PyMethod_Check(func))
4350 PCALL(PCALL_METHOD);
4351 else if (PyType_Check(func))
4352 PCALL(PCALL_TYPE);
4353 else if (PyCFunction_Check(func))
4354 PCALL(PCALL_CFUNCTION);
4355 else
4356 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004357#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 if (PyCFunction_Check(func)) {
4359 PyThreadState *tstate = PyThreadState_GET();
4360 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4361 }
4362 else
4363 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004364call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 Py_XDECREF(callargs);
4366 Py_XDECREF(kwdict);
4367 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004368}
4369
4370static PyObject *
4371ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 int nstar = 0;
4374 PyObject *callargs = NULL;
4375 PyObject *stararg = NULL;
4376 PyObject *kwdict = NULL;
4377 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 if (flags & CALL_FLAG_KW) {
4380 kwdict = EXT_POP(*pp_stack);
4381 if (!PyDict_Check(kwdict)) {
4382 PyObject *d;
4383 d = PyDict_New();
4384 if (d == NULL)
4385 goto ext_call_fail;
4386 if (PyDict_Update(d, kwdict) != 0) {
4387 Py_DECREF(d);
4388 /* PyDict_Update raises attribute
4389 * error (percolated from an attempt
4390 * to get 'keys' attribute) instead of
4391 * a type error if its second argument
4392 * is not a mapping.
4393 */
4394 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4395 PyErr_Format(PyExc_TypeError,
4396 "%.200s%.200s argument after ** "
4397 "must be a mapping, not %.200s",
4398 PyEval_GetFuncName(func),
4399 PyEval_GetFuncDesc(func),
4400 kwdict->ob_type->tp_name);
4401 }
4402 goto ext_call_fail;
4403 }
4404 Py_DECREF(kwdict);
4405 kwdict = d;
4406 }
4407 }
4408 if (flags & CALL_FLAG_VAR) {
4409 stararg = EXT_POP(*pp_stack);
4410 if (!PyTuple_Check(stararg)) {
4411 PyObject *t = NULL;
4412 t = PySequence_Tuple(stararg);
4413 if (t == NULL) {
4414 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4415 PyErr_Format(PyExc_TypeError,
4416 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004417 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 PyEval_GetFuncName(func),
4419 PyEval_GetFuncDesc(func),
4420 stararg->ob_type->tp_name);
4421 }
4422 goto ext_call_fail;
4423 }
4424 Py_DECREF(stararg);
4425 stararg = t;
4426 }
4427 nstar = PyTuple_GET_SIZE(stararg);
4428 }
4429 if (nk > 0) {
4430 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4431 if (kwdict == NULL)
4432 goto ext_call_fail;
4433 }
4434 callargs = update_star_args(na, nstar, stararg, pp_stack);
4435 if (callargs == NULL)
4436 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004437#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 /* At this point, we have to look at the type of func to
4439 update the call stats properly. Do it here so as to avoid
4440 exposing the call stats machinery outside ceval.c
4441 */
4442 if (PyFunction_Check(func))
4443 PCALL(PCALL_FUNCTION);
4444 else if (PyMethod_Check(func))
4445 PCALL(PCALL_METHOD);
4446 else if (PyType_Check(func))
4447 PCALL(PCALL_TYPE);
4448 else if (PyCFunction_Check(func))
4449 PCALL(PCALL_CFUNCTION);
4450 else
4451 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 if (PyCFunction_Check(func)) {
4454 PyThreadState *tstate = PyThreadState_GET();
4455 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4456 }
4457 else
4458 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004459ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 Py_XDECREF(callargs);
4461 Py_XDECREF(kwdict);
4462 Py_XDECREF(stararg);
4463 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004464}
4465
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004466/* Extract a slice index from a PyInt or PyLong or an object with the
4467 nb_index slot defined, and store in *pi.
4468 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4469 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 +00004470 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004471*/
Tim Petersb5196382001-12-16 19:44:20 +00004472/* Note: If v is NULL, return success without storing into *pi. This
4473 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4474 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004475*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004476int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004477_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 if (v != NULL) {
4480 Py_ssize_t x;
4481 if (PyIndex_Check(v)) {
4482 x = PyNumber_AsSsize_t(v, NULL);
4483 if (x == -1 && PyErr_Occurred())
4484 return 0;
4485 }
4486 else {
4487 PyErr_SetString(PyExc_TypeError,
4488 "slice indices must be integers or "
4489 "None or have an __index__ method");
4490 return 0;
4491 }
4492 *pi = x;
4493 }
4494 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004495}
4496
Guido van Rossum486364b2007-06-30 05:01:58 +00004497#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004499
Guido van Rossumb209a111997-04-29 18:18:01 +00004500static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004501cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 int res = 0;
4504 switch (op) {
4505 case PyCmp_IS:
4506 res = (v == w);
4507 break;
4508 case PyCmp_IS_NOT:
4509 res = (v != w);
4510 break;
4511 case PyCmp_IN:
4512 res = PySequence_Contains(w, v);
4513 if (res < 0)
4514 return NULL;
4515 break;
4516 case PyCmp_NOT_IN:
4517 res = PySequence_Contains(w, v);
4518 if (res < 0)
4519 return NULL;
4520 res = !res;
4521 break;
4522 case PyCmp_EXC_MATCH:
4523 if (PyTuple_Check(w)) {
4524 Py_ssize_t i, length;
4525 length = PyTuple_Size(w);
4526 for (i = 0; i < length; i += 1) {
4527 PyObject *exc = PyTuple_GET_ITEM(w, i);
4528 if (!PyExceptionClass_Check(exc)) {
4529 PyErr_SetString(PyExc_TypeError,
4530 CANNOT_CATCH_MSG);
4531 return NULL;
4532 }
4533 }
4534 }
4535 else {
4536 if (!PyExceptionClass_Check(w)) {
4537 PyErr_SetString(PyExc_TypeError,
4538 CANNOT_CATCH_MSG);
4539 return NULL;
4540 }
4541 }
4542 res = PyErr_GivenExceptionMatches(v, w);
4543 break;
4544 default:
4545 return PyObject_RichCompare(v, w, op);
4546 }
4547 v = res ? Py_True : Py_False;
4548 Py_INCREF(v);
4549 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004550}
4551
Thomas Wouters52152252000-08-17 22:55:00 +00004552static PyObject *
4553import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 x = PyObject_GetAttr(v, name);
4558 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4559 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4560 }
4561 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004562}
Guido van Rossumac7be682001-01-17 15:42:30 +00004563
Thomas Wouters52152252000-08-17 22:55:00 +00004564static int
4565import_all_from(PyObject *locals, PyObject *v)
4566{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004567 _Py_IDENTIFIER(__all__);
4568 _Py_IDENTIFIER(__dict__);
4569 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 PyObject *dict, *name, *value;
4571 int skip_leading_underscores = 0;
4572 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004574 if (all == NULL) {
4575 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4576 return -1; /* Unexpected error */
4577 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004578 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 if (dict == NULL) {
4580 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4581 return -1;
4582 PyErr_SetString(PyExc_ImportError,
4583 "from-import-* object has no __dict__ and no __all__");
4584 return -1;
4585 }
4586 all = PyMapping_Keys(dict);
4587 Py_DECREF(dict);
4588 if (all == NULL)
4589 return -1;
4590 skip_leading_underscores = 1;
4591 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 for (pos = 0, err = 0; ; pos++) {
4594 name = PySequence_GetItem(all, pos);
4595 if (name == NULL) {
4596 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4597 err = -1;
4598 else
4599 PyErr_Clear();
4600 break;
4601 }
4602 if (skip_leading_underscores &&
4603 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004604 PyUnicode_READY(name) != -1 &&
4605 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 {
4607 Py_DECREF(name);
4608 continue;
4609 }
4610 value = PyObject_GetAttr(v, name);
4611 if (value == NULL)
4612 err = -1;
4613 else if (PyDict_CheckExact(locals))
4614 err = PyDict_SetItem(locals, name, value);
4615 else
4616 err = PyObject_SetItem(locals, name, value);
4617 Py_DECREF(name);
4618 Py_XDECREF(value);
4619 if (err != 0)
4620 break;
4621 }
4622 Py_DECREF(all);
4623 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004624}
4625
Guido van Rossumac7be682001-01-17 15:42:30 +00004626static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004627format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 if (!obj)
4632 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 obj_str = _PyUnicode_AsString(obj);
4635 if (!obj_str)
4636 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004639}
Guido van Rossum950361c1997-01-24 13:49:28 +00004640
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004641static void
4642format_exc_unbound(PyCodeObject *co, int oparg)
4643{
4644 PyObject *name;
4645 /* Don't stomp existing exception */
4646 if (PyErr_Occurred())
4647 return;
4648 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4649 name = PyTuple_GET_ITEM(co->co_cellvars,
4650 oparg);
4651 format_exc_check_arg(
4652 PyExc_UnboundLocalError,
4653 UNBOUNDLOCAL_ERROR_MSG,
4654 name);
4655 } else {
4656 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4657 PyTuple_GET_SIZE(co->co_cellvars));
4658 format_exc_check_arg(PyExc_NameError,
4659 UNBOUNDFREE_ERROR_MSG, name);
4660 }
4661}
4662
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004663static PyObject *
4664unicode_concatenate(PyObject *v, PyObject *w,
4665 PyFrameObject *f, unsigned char *next_instr)
4666{
4667 PyObject *res;
4668 if (Py_REFCNT(v) == 2) {
4669 /* In the common case, there are 2 references to the value
4670 * stored in 'variable' when the += is performed: one on the
4671 * value stack (in 'v') and one still stored in the
4672 * 'variable'. We try to delete the variable now to reduce
4673 * the refcnt to 1.
4674 */
4675 switch (*next_instr) {
4676 case STORE_FAST:
4677 {
4678 int oparg = PEEKARG();
4679 PyObject **fastlocals = f->f_localsplus;
4680 if (GETLOCAL(oparg) == v)
4681 SETLOCAL(oparg, NULL);
4682 break;
4683 }
4684 case STORE_DEREF:
4685 {
4686 PyObject **freevars = (f->f_localsplus +
4687 f->f_code->co_nlocals);
4688 PyObject *c = freevars[PEEKARG()];
4689 if (PyCell_GET(c) == v)
4690 PyCell_Set(c, NULL);
4691 break;
4692 }
4693 case STORE_NAME:
4694 {
4695 PyObject *names = f->f_code->co_names;
4696 PyObject *name = GETITEM(names, PEEKARG());
4697 PyObject *locals = f->f_locals;
4698 if (PyDict_CheckExact(locals) &&
4699 PyDict_GetItem(locals, name) == v) {
4700 if (PyDict_DelItem(locals, name) != 0) {
4701 PyErr_Clear();
4702 }
4703 }
4704 break;
4705 }
4706 }
4707 }
4708 res = v;
4709 PyUnicode_Append(&res, w);
4710 return res;
4711}
4712
Guido van Rossum950361c1997-01-24 13:49:28 +00004713#ifdef DYNAMIC_EXECUTION_PROFILE
4714
Skip Montanarof118cb12001-10-15 20:51:38 +00004715static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004716getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 int i;
4719 PyObject *l = PyList_New(256);
4720 if (l == NULL) return NULL;
4721 for (i = 0; i < 256; i++) {
4722 PyObject *x = PyLong_FromLong(a[i]);
4723 if (x == NULL) {
4724 Py_DECREF(l);
4725 return NULL;
4726 }
4727 PyList_SetItem(l, i, x);
4728 }
4729 for (i = 0; i < 256; i++)
4730 a[i] = 0;
4731 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004732}
4733
4734PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004735_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004736{
4737#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004739#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 int i;
4741 PyObject *l = PyList_New(257);
4742 if (l == NULL) return NULL;
4743 for (i = 0; i < 257; i++) {
4744 PyObject *x = getarray(dxpairs[i]);
4745 if (x == NULL) {
4746 Py_DECREF(l);
4747 return NULL;
4748 }
4749 PyList_SetItem(l, i, x);
4750 }
4751 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004752#endif
4753}
4754
4755#endif