blob: cbc0fabd032416e14f7afbf3c64decd3ddac756f [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 Peterson3b0431d2013-04-30 09:41:40 -04002263 TARGET(LOAD_CLASSDEREF) {
2264 PyObject *name, *value, *locals = f->f_locals;
2265 int idx;
2266 assert(locals);
2267 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2268 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2269 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2270 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2271 if (PyDict_CheckExact(locals)) {
2272 value = PyDict_GetItem(locals, name);
2273 Py_XINCREF(value);
2274 }
2275 else {
2276 value = PyObject_GetItem(locals, name);
2277 if (value == NULL && PyErr_Occurred()) {
2278 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2279 goto error;
2280 PyErr_Clear();
2281 }
2282 }
2283 if (!value) {
2284 PyObject *cell = freevars[oparg];
2285 value = PyCell_GET(cell);
2286 if (value == NULL) {
2287 format_exc_unbound(co, oparg);
2288 goto error;
2289 }
2290 Py_INCREF(value);
2291 }
2292 PUSH(value);
2293 DISPATCH();
2294 }
2295
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 TARGET(LOAD_DEREF) {
2297 PyObject *cell = freevars[oparg];
2298 PyObject *value = PyCell_GET(cell);
2299 if (value == NULL) {
2300 format_exc_unbound(co, oparg);
2301 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 Py_INCREF(value);
2304 PUSH(value);
2305 DISPATCH();
2306 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002307
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 TARGET(STORE_DEREF) {
2309 PyObject *v = POP();
2310 PyObject *cell = freevars[oparg];
2311 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 DISPATCH();
2314 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002315
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 TARGET(BUILD_TUPLE) {
2317 PyObject *tup = PyTuple_New(oparg);
2318 if (tup == NULL)
2319 goto error;
2320 while (--oparg >= 0) {
2321 PyObject *item = POP();
2322 PyTuple_SET_ITEM(tup, oparg, item);
2323 }
2324 PUSH(tup);
2325 DISPATCH();
2326 }
2327
2328 TARGET(BUILD_LIST) {
2329 PyObject *list = PyList_New(oparg);
2330 if (list == NULL)
2331 goto error;
2332 while (--oparg >= 0) {
2333 PyObject *item = POP();
2334 PyList_SET_ITEM(list, oparg, item);
2335 }
2336 PUSH(list);
2337 DISPATCH();
2338 }
2339
2340 TARGET(BUILD_SET) {
2341 PyObject *set = PySet_New(NULL);
2342 int err = 0;
2343 if (set == NULL)
2344 goto error;
2345 while (--oparg >= 0) {
2346 PyObject *item = POP();
2347 if (err == 0)
2348 err = PySet_Add(set, item);
2349 Py_DECREF(item);
2350 }
2351 if (err != 0) {
2352 Py_DECREF(set);
2353 goto error;
2354 }
2355 PUSH(set);
2356 DISPATCH();
2357 }
2358
2359 TARGET(BUILD_MAP) {
2360 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2361 if (map == NULL)
2362 goto error;
2363 PUSH(map);
2364 DISPATCH();
2365 }
2366
2367 TARGET(STORE_MAP) {
2368 PyObject *key = TOP();
2369 PyObject *value = SECOND();
2370 PyObject *map = THIRD();
2371 int err;
2372 STACKADJ(-2);
2373 assert(PyDict_CheckExact(map));
2374 err = PyDict_SetItem(map, key, value);
2375 Py_DECREF(value);
2376 Py_DECREF(key);
2377 if (err != 0)
2378 goto error;
2379 DISPATCH();
2380 }
2381
2382 TARGET(MAP_ADD) {
2383 PyObject *key = TOP();
2384 PyObject *value = SECOND();
2385 PyObject *map;
2386 int err;
2387 STACKADJ(-2);
2388 map = stack_pointer[-oparg]; /* dict */
2389 assert(PyDict_CheckExact(map));
2390 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2391 Py_DECREF(value);
2392 Py_DECREF(key);
2393 if (err != 0)
2394 goto error;
2395 PREDICT(JUMP_ABSOLUTE);
2396 DISPATCH();
2397 }
2398
2399 TARGET(LOAD_ATTR) {
2400 PyObject *name = GETITEM(names, oparg);
2401 PyObject *owner = TOP();
2402 PyObject *res = PyObject_GetAttr(owner, name);
2403 Py_DECREF(owner);
2404 SET_TOP(res);
2405 if (res == NULL)
2406 goto error;
2407 DISPATCH();
2408 }
2409
2410 TARGET(COMPARE_OP) {
2411 PyObject *right = POP();
2412 PyObject *left = TOP();
2413 PyObject *res = cmp_outcome(oparg, left, right);
2414 Py_DECREF(left);
2415 Py_DECREF(right);
2416 SET_TOP(res);
2417 if (res == NULL)
2418 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 PREDICT(POP_JUMP_IF_FALSE);
2420 PREDICT(POP_JUMP_IF_TRUE);
2421 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002422 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002423
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 TARGET(IMPORT_NAME) {
2425 _Py_IDENTIFIER(__import__);
2426 PyObject *name = GETITEM(names, oparg);
2427 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002428 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 if (func == NULL) {
2430 PyErr_SetString(PyExc_ImportError,
2431 "__import__ not found");
2432 goto error;
2433 }
2434 Py_INCREF(func);
2435 from = POP();
2436 level = TOP();
2437 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2438 args = PyTuple_Pack(5,
2439 name,
2440 f->f_globals,
2441 f->f_locals == NULL ?
2442 Py_None : f->f_locals,
2443 from,
2444 level);
2445 else
2446 args = PyTuple_Pack(4,
2447 name,
2448 f->f_globals,
2449 f->f_locals == NULL ?
2450 Py_None : f->f_locals,
2451 from);
2452 Py_DECREF(level);
2453 Py_DECREF(from);
2454 if (args == NULL) {
2455 Py_DECREF(func);
2456 STACKADJ(-1);
2457 goto error;
2458 }
2459 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002460 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 READ_TIMESTAMP(intr1);
2462 Py_DECREF(args);
2463 Py_DECREF(func);
2464 SET_TOP(res);
2465 if (res == NULL)
2466 goto error;
2467 DISPATCH();
2468 }
2469
2470 TARGET(IMPORT_STAR) {
2471 PyObject *from = POP(), *locals;
2472 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 PyFrame_FastToLocals(f);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 locals = f->f_locals;
2475 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 PyErr_SetString(PyExc_SystemError,
2477 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 }
2480 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 READ_TIMESTAMP(intr1);
2483 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 Py_DECREF(from);
2485 if (err != 0)
2486 goto error;
2487 DISPATCH();
2488 }
Guido van Rossum25831651993-05-19 14:50:45 +00002489
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 TARGET(IMPORT_FROM) {
2491 PyObject *name = GETITEM(names, oparg);
2492 PyObject *from = TOP();
2493 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 PUSH(res);
2498 if (res == NULL)
2499 goto error;
2500 DISPATCH();
2501 }
Thomas Wouters52152252000-08-17 22:55:00 +00002502
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 JUMPBY(oparg);
2505 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002508 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 TARGET(POP_JUMP_IF_FALSE) {
2510 PyObject *cond = POP();
2511 int err;
2512 if (cond == Py_True) {
2513 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 FAST_DISPATCH();
2515 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 if (cond == Py_False) {
2517 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 JUMPTO(oparg);
2519 FAST_DISPATCH();
2520 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 err = PyObject_IsTrue(cond);
2522 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 if (err > 0)
2524 err = 0;
2525 else if (err == 0)
2526 JUMPTO(oparg);
2527 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 TARGET(POP_JUMP_IF_TRUE) {
2534 PyObject *cond = POP();
2535 int err;
2536 if (cond == Py_False) {
2537 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 FAST_DISPATCH();
2539 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002540 if (cond == Py_True) {
2541 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002542 JUMPTO(oparg);
2543 FAST_DISPATCH();
2544 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 err = PyObject_IsTrue(cond);
2546 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 if (err > 0) {
2548 err = 0;
2549 JUMPTO(oparg);
2550 }
2551 else if (err == 0)
2552 ;
2553 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002557
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 TARGET(JUMP_IF_FALSE_OR_POP) {
2559 PyObject *cond = TOP();
2560 int err;
2561 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 FAST_DISPATCH();
2565 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 JUMPTO(oparg);
2568 FAST_DISPATCH();
2569 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 if (err > 0) {
2572 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002573 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 err = 0;
2575 }
2576 else if (err == 0)
2577 JUMPTO(oparg);
2578 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 TARGET(JUMP_IF_TRUE_OR_POP) {
2584 PyObject *cond = TOP();
2585 int err;
2586 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 FAST_DISPATCH();
2590 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 JUMPTO(oparg);
2593 FAST_DISPATCH();
2594 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 if (err > 0) {
2597 err = 0;
2598 JUMPTO(oparg);
2599 }
2600 else if (err == 0) {
2601 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 }
2604 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002605 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002607 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002612#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 /* Enabling this path speeds-up all while and for-loops by bypassing
2614 the per-loop checks for signals. By default, this should be turned-off
2615 because it prevents detection of a control-break in tight loops like
2616 "while 1: pass". Compile with this option turned-on when you need
2617 the speed-up and do not need break checking inside tight loops (ones
2618 that contain only instructions ending with FAST_DISPATCH).
2619 */
2620 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002621#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002623#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 PyObject *iterable = TOP();
2629 PyObject *iter = PyObject_GetIter(iterable);
2630 Py_DECREF(iterable);
2631 SET_TOP(iter);
2632 if (iter == NULL)
2633 goto error;
2634 PREDICT(FOR_ITER);
2635 DISPATCH();
2636 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002641 PyObject *iter = TOP();
2642 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2643 if (next != NULL) {
2644 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 PREDICT(STORE_FAST);
2646 PREDICT(UNPACK_SEQUENCE);
2647 DISPATCH();
2648 }
2649 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2651 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 PyErr_Clear();
2653 }
2654 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 STACKADJ(-1);
2656 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 JUMPBY(oparg);
2658 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002660
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 why = WHY_BREAK;
2663 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002665
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 if (retval == NULL)
2669 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 why = WHY_CONTINUE;
2671 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2675 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2676 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 /* NOTE: If you add any new block-setup opcodes that
2679 are not try/except/finally handlers, you may need
2680 to update the PyGen_NeedsFinalizing() function.
2681 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2684 STACK_LEVEL());
2685 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002687
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002689 _Py_IDENTIFIER(__exit__);
2690 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002691 PyObject *mgr = TOP();
2692 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2693 PyObject *res;
2694 if (exit == NULL)
2695 goto error;
2696 SET_TOP(exit);
2697 enter = special_lookup(mgr, &PyId___enter__);
2698 Py_DECREF(mgr);
2699 if (enter == NULL)
2700 goto error;
2701 res = PyObject_CallFunctionObjArgs(enter, NULL);
2702 Py_DECREF(enter);
2703 if (res == NULL)
2704 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 /* Setup the finally block before pushing the result
2706 of __enter__ on the stack. */
2707 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2708 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002709
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 DISPATCH();
2712 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002713
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 TARGET(WITH_CLEANUP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 /* At the top of the stack are 1-3 values indicating
2716 how/why we entered the finally clause:
2717 - TOP = None
2718 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2719 - TOP = WHY_*; no retval below it
2720 - (TOP, SECOND, THIRD) = exc_info()
2721 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2722 Below them is EXIT, the context.__exit__ bound method.
2723 In the last case, we must call
2724 EXIT(TOP, SECOND, THIRD)
2725 otherwise we must call
2726 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 In the first two cases, we remove EXIT from the
2729 stack, leaving the rest in the same order. In the
2730 third case, we shift the bottom 3 values of the
2731 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 In addition, if the stack represents an exception,
2734 *and* the function call returns a 'true' value, we
2735 push WHY_SILENCED onto the stack. END_FINALLY will
2736 then not re-raise the exception. (But non-local
2737 gotos should still be resumed.)
2738 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2742 int err;
2743 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 (void)POP();
2745 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002746 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 else if (PyLong_Check(exc)) {
2749 STACKADJ(-1);
2750 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 case WHY_RETURN:
2752 case WHY_CONTINUE:
2753 /* Retval in TOP. */
2754 exit_func = SECOND();
2755 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 break;
2758 default:
2759 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 break;
2762 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 }
2765 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 val = SECOND();
2769 tb = THIRD();
2770 tp2 = FOURTH();
2771 exc2 = PEEK(5);
2772 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 SET_VALUE(7, tb2);
2775 SET_VALUE(6, exc2);
2776 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2778 SET_FOURTH(NULL);
2779 /* We just shifted the stack down, so we have
2780 to tell the except handler block that the
2781 values are lower than it expects. */
2782 block = &f->f_blockstack[f->f_iblock - 1];
2783 assert(block->b_type == EXCEPT_HANDLER);
2784 block->b_level--;
2785 }
2786 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 if (res == NULL)
2790 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002791
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 if (exc != Py_None)
2793 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 else
2795 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 else if (err > 0) {
2801 err = 0;
2802 /* There was an exception and a True return */
2803 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2804 }
2805 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002808
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 TARGET(CALL_FUNCTION) {
2810 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 PCALL(PCALL_ALL);
2812 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002813#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002815#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002817#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 PUSH(res);
2820 if (res == NULL)
2821 goto error;
2822 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002825 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2826 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2827 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 int na = oparg & 0xff;
2830 int nk = (oparg>>8) & 0xff;
2831 int flags = (opcode - CALL_FUNCTION) & 3;
2832 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 PCALL(PCALL_ALL);
2835 if (flags & CALL_FLAG_VAR)
2836 n++;
2837 if (flags & CALL_FLAG_KW)
2838 n++;
2839 pfunc = stack_pointer - n - 1;
2840 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002843 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 PyObject *self = PyMethod_GET_SELF(func);
2845 Py_INCREF(self);
2846 func = PyMethod_GET_FUNCTION(func);
2847 Py_INCREF(func);
2848 Py_DECREF(*pfunc);
2849 *pfunc = self;
2850 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002851 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 } else
2853 Py_INCREF(func);
2854 sp = stack_pointer;
2855 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 READ_TIMESTAMP(intr1);
2858 stack_pointer = sp;
2859 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 PyObject *o = POP();
2863 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 PUSH(res);
2866 if (res == NULL)
2867 goto error;
2868 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2872 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 int posdefaults = oparg & 0xff;
2875 int kwdefaults = (oparg>>8) & 0xff;
2876 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002877
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 PyObject *qualname = POP(); /* qualname */
2879 PyObject *code = POP(); /* code object */
2880 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2881 Py_DECREF(code);
2882 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002883
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 if (func == NULL)
2885 goto error;
2886
2887 if (opcode == MAKE_CLOSURE) {
2888 PyObject *closure = POP();
2889 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 Py_DECREF(func);
2892 Py_DECREF(closure);
2893 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002897
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 PyObject *names = POP(); /* names of args with annotations */
2901 PyObject *anns = PyDict_New();
2902 if (anns == NULL) {
2903 Py_DECREF(func);
2904 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 assert(num_annotations == name_ix+1);
2908 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 PyObject *name, *value;
2910 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 name = PyTuple_GET_ITEM(names, name_ix);
2913 value = POP();
2914 err = PyDict_SetItem(anns, name, value);
2915 Py_DECREF(value);
2916 if (err != 0) {
2917 Py_DECREF(anns);
2918 Py_DECREF(func);
2919 goto error;
2920 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002922
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 /* Can't happen unless
2925 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 Py_DECREF(anns);
2927 Py_DECREF(func);
2928 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 Py_DECREF(anns);
2931 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 if (kwdefaults > 0) {
2936 PyObject *defs = PyDict_New();
2937 if (defs == NULL) {
2938 Py_DECREF(func);
2939 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 }
2941 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 PyObject *v = POP(); /* default value */
2943 PyObject *key = POP(); /* kw only arg name */
2944 int err = PyDict_SetItem(defs, key, v);
2945 Py_DECREF(v);
2946 Py_DECREF(key);
2947 if (err != 0) {
2948 Py_DECREF(defs);
2949 Py_DECREF(func);
2950 goto error;
2951 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002952 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 /* Can't happen unless
2955 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 Py_DECREF(func);
2957 Py_DECREF(defs);
2958 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05002962 if (posdefaults > 0) {
2963 PyObject *defs = PyTuple_New(posdefaults);
2964 if (defs == NULL) {
2965 Py_DECREF(func);
2966 goto error;
2967 }
2968 while (--posdefaults >= 0)
2969 PyTuple_SET_ITEM(defs, posdefaults, POP());
2970 if (PyFunction_SetDefaults(func, defs) != 0) {
2971 /* Can't happen unless
2972 PyFunction_SetDefaults changes. */
2973 Py_DECREF(defs);
2974 Py_DECREF(func);
2975 goto error;
2976 }
2977 Py_DECREF(defs);
2978 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 PUSH(func);
2980 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002982
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 TARGET(BUILD_SLICE) {
2984 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002985 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 step = NULL;
2989 stop = POP();
2990 start = TOP();
2991 slice = PySlice_New(start, stop, step);
2992 Py_DECREF(start);
2993 Py_DECREF(stop);
2994 Py_XDECREF(step);
2995 SET_TOP(slice);
2996 if (slice == NULL)
2997 goto error;
2998 DISPATCH();
2999 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003000
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 opcode = NEXTOP();
3003 oparg = oparg<<16 | NEXTARG();
3004 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003006
Antoine Pitrou042b1282010-08-13 21:15:58 +00003007#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003009#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 default:
3011 fprintf(stderr,
3012 "XXX lineno: %d, opcode: %d\n",
3013 PyFrame_GetLineNumber(f),
3014 opcode);
3015 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003017
3018#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003020#endif
3021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003023
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 /* This should never be reached. Every opcode should end with DISPATCH()
3025 or goto error. */
3026 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003027
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003030
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003031 assert(why == WHY_NOT);
3032 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003033
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 /* Double-check exception status. */
3035 if (!PyErr_Occurred())
3036 PyErr_SetString(PyExc_SystemError,
3037 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00003038
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 /* Log traceback info. */
3040 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003041
Benjamin Peterson51f46162013-01-23 08:38:47 -05003042 if (tstate->c_tracefunc != NULL)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003044
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003045fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 assert(why != WHY_NOT);
3047
3048 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 while (why != WHY_NOT && f->f_iblock > 0) {
3050 /* Peek at the current block. */
3051 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 assert(why != WHY_YIELD);
3054 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3055 why = WHY_NOT;
3056 JUMPTO(PyLong_AS_LONG(retval));
3057 Py_DECREF(retval);
3058 break;
3059 }
3060 /* Now we have to pop the block. */
3061 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 if (b->b_type == EXCEPT_HANDLER) {
3064 UNWIND_EXCEPT_HANDLER(b);
3065 continue;
3066 }
3067 UNWIND_BLOCK(b);
3068 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3069 why = WHY_NOT;
3070 JUMPTO(b->b_handler);
3071 break;
3072 }
3073 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3074 || b->b_type == SETUP_FINALLY)) {
3075 PyObject *exc, *val, *tb;
3076 int handler = b->b_handler;
3077 /* Beware, this invalidates all b->b_* fields */
3078 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3079 PUSH(tstate->exc_traceback);
3080 PUSH(tstate->exc_value);
3081 if (tstate->exc_type != NULL) {
3082 PUSH(tstate->exc_type);
3083 }
3084 else {
3085 Py_INCREF(Py_None);
3086 PUSH(Py_None);
3087 }
3088 PyErr_Fetch(&exc, &val, &tb);
3089 /* Make the raw exception data
3090 available to the handler,
3091 so a program can emulate the
3092 Python main loop. */
3093 PyErr_NormalizeException(
3094 &exc, &val, &tb);
3095 PyException_SetTraceback(val, tb);
3096 Py_INCREF(exc);
3097 tstate->exc_type = exc;
3098 Py_INCREF(val);
3099 tstate->exc_value = val;
3100 tstate->exc_traceback = tb;
3101 if (tb == NULL)
3102 tb = Py_None;
3103 Py_INCREF(tb);
3104 PUSH(tb);
3105 PUSH(val);
3106 PUSH(exc);
3107 why = WHY_NOT;
3108 JUMPTO(handler);
3109 break;
3110 }
3111 if (b->b_type == SETUP_FINALLY) {
3112 if (why & (WHY_RETURN | WHY_CONTINUE))
3113 PUSH(retval);
3114 PUSH(PyLong_FromLong((long)why));
3115 why = WHY_NOT;
3116 JUMPTO(b->b_handler);
3117 break;
3118 }
3119 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 if (why != WHY_NOT)
3124 break;
3125 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 assert(why != WHY_YIELD);
3130 /* Pop remaining stack entries. */
3131 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 PyObject *o = POP();
3133 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 if (why != WHY_RETURN)
3137 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003138
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003139fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003140 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3141 /* The purpose of this block is to put aside the generator's exception
3142 state and restore that of the calling frame. If the current
3143 exception state is from the caller, we clear the exception values
3144 on the generator frame, so they are not swapped back in latter. The
3145 origin of the current exception state is determined by checking for
3146 except handler blocks, which we must be in iff a new exception
3147 state came into existence in this frame. (An uncaught exception
3148 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3149 int i;
3150 for (i = 0; i < f->f_iblock; i++)
3151 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3152 break;
3153 if (i == f->f_iblock)
3154 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003155 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003156 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003157 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003158 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003160 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003161 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003162 if (why == WHY_RETURN || why == WHY_YIELD) {
3163 if (call_trace(tstate->c_tracefunc,
3164 tstate->c_traceobj, f,
3165 PyTrace_RETURN, retval)) {
3166 Py_XDECREF(retval);
3167 retval = NULL;
3168 why = WHY_EXCEPTION;
3169 }
3170 }
3171 else if (why == WHY_EXCEPTION) {
3172 call_trace_protected(tstate->c_tracefunc,
3173 tstate->c_traceobj, f,
3174 PyTrace_RETURN, NULL);
3175 }
3176 }
3177 if (tstate->c_profilefunc) {
3178 if (why == WHY_EXCEPTION)
3179 call_trace_protected(tstate->c_profilefunc,
3180 tstate->c_profileobj, f,
3181 PyTrace_RETURN, NULL);
3182 else if (call_trace(tstate->c_profilefunc,
3183 tstate->c_profileobj, f,
3184 PyTrace_RETURN, retval)) {
3185 Py_XDECREF(retval);
3186 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003187 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 }
3189 }
3190 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003193exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 Py_LeaveRecursiveCall();
3195 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003198}
3199
Benjamin Petersonb204a422011-06-05 22:04:07 -05003200static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003201format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3202{
3203 int err;
3204 Py_ssize_t len = PyList_GET_SIZE(names);
3205 PyObject *name_str, *comma, *tail, *tmp;
3206
3207 assert(PyList_CheckExact(names));
3208 assert(len >= 1);
3209 /* Deal with the joys of natural language. */
3210 switch (len) {
3211 case 1:
3212 name_str = PyList_GET_ITEM(names, 0);
3213 Py_INCREF(name_str);
3214 break;
3215 case 2:
3216 name_str = PyUnicode_FromFormat("%U and %U",
3217 PyList_GET_ITEM(names, len - 2),
3218 PyList_GET_ITEM(names, len - 1));
3219 break;
3220 default:
3221 tail = PyUnicode_FromFormat(", %U, and %U",
3222 PyList_GET_ITEM(names, len - 2),
3223 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003224 if (tail == NULL)
3225 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003226 /* Chop off the last two objects in the list. This shouldn't actually
3227 fail, but we can't be too careful. */
3228 err = PyList_SetSlice(names, len - 2, len, NULL);
3229 if (err == -1) {
3230 Py_DECREF(tail);
3231 return;
3232 }
3233 /* Stitch everything up into a nice comma-separated list. */
3234 comma = PyUnicode_FromString(", ");
3235 if (comma == NULL) {
3236 Py_DECREF(tail);
3237 return;
3238 }
3239 tmp = PyUnicode_Join(comma, names);
3240 Py_DECREF(comma);
3241 if (tmp == NULL) {
3242 Py_DECREF(tail);
3243 return;
3244 }
3245 name_str = PyUnicode_Concat(tmp, tail);
3246 Py_DECREF(tmp);
3247 Py_DECREF(tail);
3248 break;
3249 }
3250 if (name_str == NULL)
3251 return;
3252 PyErr_Format(PyExc_TypeError,
3253 "%U() missing %i required %s argument%s: %U",
3254 co->co_name,
3255 len,
3256 kind,
3257 len == 1 ? "" : "s",
3258 name_str);
3259 Py_DECREF(name_str);
3260}
3261
3262static void
3263missing_arguments(PyCodeObject *co, int missing, int defcount,
3264 PyObject **fastlocals)
3265{
3266 int i, j = 0;
3267 int start, end;
3268 int positional = defcount != -1;
3269 const char *kind = positional ? "positional" : "keyword-only";
3270 PyObject *missing_names;
3271
3272 /* Compute the names of the arguments that are missing. */
3273 missing_names = PyList_New(missing);
3274 if (missing_names == NULL)
3275 return;
3276 if (positional) {
3277 start = 0;
3278 end = co->co_argcount - defcount;
3279 }
3280 else {
3281 start = co->co_argcount;
3282 end = start + co->co_kwonlyargcount;
3283 }
3284 for (i = start; i < end; i++) {
3285 if (GETLOCAL(i) == NULL) {
3286 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3287 PyObject *name = PyObject_Repr(raw);
3288 if (name == NULL) {
3289 Py_DECREF(missing_names);
3290 return;
3291 }
3292 PyList_SET_ITEM(missing_names, j++, name);
3293 }
3294 }
3295 assert(j == missing);
3296 format_missing(kind, co, missing_names);
3297 Py_DECREF(missing_names);
3298}
3299
3300static void
3301too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003302{
3303 int plural;
3304 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003305 int i;
3306 PyObject *sig, *kwonly_sig;
3307
Benjamin Petersone109c702011-06-24 09:37:26 -05003308 assert((co->co_flags & CO_VARARGS) == 0);
3309 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003310 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003311 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003312 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003313 if (defcount) {
3314 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003315 plural = 1;
3316 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3317 }
3318 else {
3319 plural = co->co_argcount != 1;
3320 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3321 }
3322 if (sig == NULL)
3323 return;
3324 if (kwonly_given) {
3325 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3326 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3327 kwonly_given != 1 ? "s" : "");
3328 if (kwonly_sig == NULL) {
3329 Py_DECREF(sig);
3330 return;
3331 }
3332 }
3333 else {
3334 /* This will not fail. */
3335 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003336 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003337 }
3338 PyErr_Format(PyExc_TypeError,
3339 "%U() takes %U positional argument%s but %d%U %s given",
3340 co->co_name,
3341 sig,
3342 plural ? "s" : "",
3343 given,
3344 kwonly_sig,
3345 given == 1 && !kwonly_given ? "was" : "were");
3346 Py_DECREF(sig);
3347 Py_DECREF(kwonly_sig);
3348}
3349
Guido van Rossumc2e20742006-02-27 22:32:47 +00003350/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003351 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003352 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003353
Tim Peters6d6c1a32001-08-02 04:15:00 +00003354PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003355PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 PyObject **args, int argcount, PyObject **kws, int kwcount,
3357 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003358{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003359 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 register PyFrameObject *f;
3361 register PyObject *retval = NULL;
3362 register PyObject **fastlocals, **freevars;
3363 PyThreadState *tstate = PyThreadState_GET();
3364 PyObject *x, *u;
3365 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003366 int i;
3367 int n = argcount;
3368 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 if (globals == NULL) {
3371 PyErr_SetString(PyExc_SystemError,
3372 "PyEval_EvalCodeEx: NULL globals");
3373 return NULL;
3374 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 assert(tstate != NULL);
3377 assert(globals != NULL);
3378 f = PyFrame_New(tstate, co, globals, locals);
3379 if (f == NULL)
3380 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 fastlocals = f->f_localsplus;
3383 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003384
Benjamin Petersonb204a422011-06-05 22:04:07 -05003385 /* Parse arguments. */
3386 if (co->co_flags & CO_VARKEYWORDS) {
3387 kwdict = PyDict_New();
3388 if (kwdict == NULL)
3389 goto fail;
3390 i = total_args;
3391 if (co->co_flags & CO_VARARGS)
3392 i++;
3393 SETLOCAL(i, kwdict);
3394 }
3395 if (argcount > co->co_argcount)
3396 n = co->co_argcount;
3397 for (i = 0; i < n; i++) {
3398 x = args[i];
3399 Py_INCREF(x);
3400 SETLOCAL(i, x);
3401 }
3402 if (co->co_flags & CO_VARARGS) {
3403 u = PyTuple_New(argcount - n);
3404 if (u == NULL)
3405 goto fail;
3406 SETLOCAL(total_args, u);
3407 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 x = args[i];
3409 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003410 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003412 }
3413 for (i = 0; i < kwcount; i++) {
3414 PyObject **co_varnames;
3415 PyObject *keyword = kws[2*i];
3416 PyObject *value = kws[2*i + 1];
3417 int j;
3418 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3419 PyErr_Format(PyExc_TypeError,
3420 "%U() keywords must be strings",
3421 co->co_name);
3422 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003424 /* Speed hack: do raw pointer compares. As names are
3425 normally interned this should almost always hit. */
3426 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3427 for (j = 0; j < total_args; j++) {
3428 PyObject *nm = co_varnames[j];
3429 if (nm == keyword)
3430 goto kw_found;
3431 }
3432 /* Slow fallback, just in case */
3433 for (j = 0; j < total_args; j++) {
3434 PyObject *nm = co_varnames[j];
3435 int cmp = PyObject_RichCompareBool(
3436 keyword, nm, Py_EQ);
3437 if (cmp > 0)
3438 goto kw_found;
3439 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003441 }
3442 if (j >= total_args && kwdict == NULL) {
3443 PyErr_Format(PyExc_TypeError,
3444 "%U() got an unexpected "
3445 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 co->co_name,
3447 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003448 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003450 PyDict_SetItem(kwdict, keyword, value);
3451 continue;
3452 kw_found:
3453 if (GETLOCAL(j) != NULL) {
3454 PyErr_Format(PyExc_TypeError,
3455 "%U() got multiple "
3456 "values for argument '%S'",
3457 co->co_name,
3458 keyword);
3459 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003461 Py_INCREF(value);
3462 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003464 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003465 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 goto fail;
3467 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003468 if (argcount < co->co_argcount) {
3469 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003470 int missing = 0;
3471 for (i = argcount; i < m; i++)
3472 if (GETLOCAL(i) == NULL)
3473 missing++;
3474 if (missing) {
3475 missing_arguments(co, missing, defcount, fastlocals);
3476 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003477 }
3478 if (n > m)
3479 i = n - m;
3480 else
3481 i = 0;
3482 for (; i < defcount; i++) {
3483 if (GETLOCAL(m+i) == NULL) {
3484 PyObject *def = defs[i];
3485 Py_INCREF(def);
3486 SETLOCAL(m+i, def);
3487 }
3488 }
3489 }
3490 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003491 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003492 for (i = co->co_argcount; i < total_args; i++) {
3493 PyObject *name;
3494 if (GETLOCAL(i) != NULL)
3495 continue;
3496 name = PyTuple_GET_ITEM(co->co_varnames, i);
3497 if (kwdefs != NULL) {
3498 PyObject *def = PyDict_GetItem(kwdefs, name);
3499 if (def) {
3500 Py_INCREF(def);
3501 SETLOCAL(i, def);
3502 continue;
3503 }
3504 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003505 missing++;
3506 }
3507 if (missing) {
3508 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003509 goto fail;
3510 }
3511 }
3512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003514 vars into frame. */
3515 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003517 int arg;
3518 /* Possibly account for the cell variable being an argument. */
3519 if (co->co_cell2arg != NULL &&
3520 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3521 c = PyCell_New(GETLOCAL(arg));
3522 else
3523 c = PyCell_New(NULL);
3524 if (c == NULL)
3525 goto fail;
3526 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003528 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3529 PyObject *o = PyTuple_GET_ITEM(closure, i);
3530 Py_INCREF(o);
3531 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 if (co->co_flags & CO_GENERATOR) {
3535 /* Don't need to keep the reference to f_back, it will be set
3536 * when the generator is resumed. */
3537 Py_XDECREF(f->f_back);
3538 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 /* Create a new generator that owns the ready to run frame
3543 * and return that as the value. */
3544 return PyGen_New(f);
3545 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003548
Thomas Woutersce272b62007-09-19 21:19:28 +00003549fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 /* decref'ing the frame can cause __del__ methods to get invoked,
3552 which can call back into Python. While we're done with the
3553 current Python frame (f), the associated C stack is still in use,
3554 so recursion_depth must be boosted for the duration.
3555 */
3556 assert(tstate != NULL);
3557 ++tstate->recursion_depth;
3558 Py_DECREF(f);
3559 --tstate->recursion_depth;
3560 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003561}
3562
3563
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003564static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003565special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003568 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003570 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 return NULL;
3572 }
3573 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003574}
3575
3576
Benjamin Peterson87880242011-07-03 16:48:31 -05003577/* These 3 functions deal with the exception state of generators. */
3578
3579static void
3580save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3581{
3582 PyObject *type, *value, *traceback;
3583 Py_XINCREF(tstate->exc_type);
3584 Py_XINCREF(tstate->exc_value);
3585 Py_XINCREF(tstate->exc_traceback);
3586 type = f->f_exc_type;
3587 value = f->f_exc_value;
3588 traceback = f->f_exc_traceback;
3589 f->f_exc_type = tstate->exc_type;
3590 f->f_exc_value = tstate->exc_value;
3591 f->f_exc_traceback = tstate->exc_traceback;
3592 Py_XDECREF(type);
3593 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003594 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003595}
3596
3597static void
3598swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3599{
3600 PyObject *tmp;
3601 tmp = tstate->exc_type;
3602 tstate->exc_type = f->f_exc_type;
3603 f->f_exc_type = tmp;
3604 tmp = tstate->exc_value;
3605 tstate->exc_value = f->f_exc_value;
3606 f->f_exc_value = tmp;
3607 tmp = tstate->exc_traceback;
3608 tstate->exc_traceback = f->f_exc_traceback;
3609 f->f_exc_traceback = tmp;
3610}
3611
3612static void
3613restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3614{
3615 PyObject *type, *value, *tb;
3616 type = tstate->exc_type;
3617 value = tstate->exc_value;
3618 tb = tstate->exc_traceback;
3619 tstate->exc_type = f->f_exc_type;
3620 tstate->exc_value = f->f_exc_value;
3621 tstate->exc_traceback = f->f_exc_traceback;
3622 f->f_exc_type = NULL;
3623 f->f_exc_value = NULL;
3624 f->f_exc_traceback = NULL;
3625 Py_XDECREF(type);
3626 Py_XDECREF(value);
3627 Py_XDECREF(tb);
3628}
3629
3630
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003631/* Logic for the raise statement (too complicated for inlining).
3632 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003633static int
Collin Winter828f04a2007-08-31 00:04:24 +00003634do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 if (exc == NULL) {
3639 /* Reraise */
3640 PyThreadState *tstate = PyThreadState_GET();
3641 PyObject *tb;
3642 type = tstate->exc_type;
3643 value = tstate->exc_value;
3644 tb = tstate->exc_traceback;
3645 if (type == Py_None) {
3646 PyErr_SetString(PyExc_RuntimeError,
3647 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003648 return 0;
3649 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 Py_XINCREF(type);
3651 Py_XINCREF(value);
3652 Py_XINCREF(tb);
3653 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003654 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 /* We support the following forms of raise:
3658 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003659 raise <instance>
3660 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 if (PyExceptionClass_Check(exc)) {
3663 type = exc;
3664 value = PyObject_CallObject(exc, NULL);
3665 if (value == NULL)
3666 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003667 if (!PyExceptionInstance_Check(value)) {
3668 PyErr_Format(PyExc_TypeError,
3669 "calling %R should have returned an instance of "
3670 "BaseException, not %R",
3671 type, Py_TYPE(value));
3672 goto raise_error;
3673 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 }
3675 else if (PyExceptionInstance_Check(exc)) {
3676 value = exc;
3677 type = PyExceptionInstance_Class(exc);
3678 Py_INCREF(type);
3679 }
3680 else {
3681 /* Not something you can raise. You get an exception
3682 anyway, just not what you specified :-) */
3683 Py_DECREF(exc);
3684 PyErr_SetString(PyExc_TypeError,
3685 "exceptions must derive from BaseException");
3686 goto raise_error;
3687 }
Collin Winter828f04a2007-08-31 00:04:24 +00003688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 if (cause) {
3690 PyObject *fixed_cause;
3691 if (PyExceptionClass_Check(cause)) {
3692 fixed_cause = PyObject_CallObject(cause, NULL);
3693 if (fixed_cause == NULL)
3694 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003695 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003697 else if (PyExceptionInstance_Check(cause)) {
3698 fixed_cause = cause;
3699 }
3700 else if (cause == Py_None) {
3701 Py_DECREF(cause);
3702 fixed_cause = NULL;
3703 }
3704 else {
3705 PyErr_SetString(PyExc_TypeError,
3706 "exception causes must derive from "
3707 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 goto raise_error;
3709 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003710 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 }
Collin Winter828f04a2007-08-31 00:04:24 +00003712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 PyErr_SetObject(type, value);
3714 /* PyErr_SetObject incref's its arguments */
3715 Py_XDECREF(value);
3716 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003717 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003718
3719raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 Py_XDECREF(value);
3721 Py_XDECREF(type);
3722 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003723 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003724}
3725
Tim Petersd6d010b2001-06-21 02:49:55 +00003726/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003727 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003728
Guido van Rossum0368b722007-05-11 16:50:42 +00003729 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3730 with a variable target.
3731*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003732
Barry Warsawe42b18f1997-08-25 22:13:04 +00003733static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003734unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 int i = 0, j = 0;
3737 Py_ssize_t ll = 0;
3738 PyObject *it; /* iter(v) */
3739 PyObject *w;
3740 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 it = PyObject_GetIter(v);
3745 if (it == NULL)
3746 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 for (; i < argcnt; i++) {
3749 w = PyIter_Next(it);
3750 if (w == NULL) {
3751 /* Iterator done, via error or exhaustion. */
3752 if (!PyErr_Occurred()) {
3753 PyErr_Format(PyExc_ValueError,
3754 "need more than %d value%s to unpack",
3755 i, i == 1 ? "" : "s");
3756 }
3757 goto Error;
3758 }
3759 *--sp = w;
3760 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 if (argcntafter == -1) {
3763 /* We better have exhausted the iterator now. */
3764 w = PyIter_Next(it);
3765 if (w == NULL) {
3766 if (PyErr_Occurred())
3767 goto Error;
3768 Py_DECREF(it);
3769 return 1;
3770 }
3771 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003772 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3773 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 goto Error;
3775 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 l = PySequence_List(it);
3778 if (l == NULL)
3779 goto Error;
3780 *--sp = l;
3781 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 ll = PyList_GET_SIZE(l);
3784 if (ll < argcntafter) {
3785 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3786 argcnt + ll);
3787 goto Error;
3788 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 /* Pop the "after-variable" args off the list. */
3791 for (j = argcntafter; j > 0; j--, i++) {
3792 *--sp = PyList_GET_ITEM(l, ll - j);
3793 }
3794 /* Resize the list. */
3795 Py_SIZE(l) = ll - argcntafter;
3796 Py_DECREF(it);
3797 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003798
Tim Petersd6d010b2001-06-21 02:49:55 +00003799Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003800 for (; i > 0; i--, sp++)
3801 Py_DECREF(*sp);
3802 Py_XDECREF(it);
3803 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003804}
3805
3806
Guido van Rossum96a42c81992-01-12 02:29:51 +00003807#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003808static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003809prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 printf("%s ", str);
3812 if (PyObject_Print(v, stdout, 0) != 0)
3813 PyErr_Clear(); /* Don't know what else to do */
3814 printf("\n");
3815 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003816}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003817#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003818
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003819static void
Fred Drake5755ce62001-06-27 19:19:46 +00003820call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 PyObject *type, *value, *traceback, *arg;
3823 int err;
3824 PyErr_Fetch(&type, &value, &traceback);
3825 if (value == NULL) {
3826 value = Py_None;
3827 Py_INCREF(value);
3828 }
R David Murray35837612013-04-19 12:56:57 -04003829 PyErr_NormalizeException(&type, &value, &traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 arg = PyTuple_Pack(3, type, value, traceback);
3831 if (arg == NULL) {
3832 PyErr_Restore(type, value, traceback);
3833 return;
3834 }
3835 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3836 Py_DECREF(arg);
3837 if (err == 0)
3838 PyErr_Restore(type, value, traceback);
3839 else {
3840 Py_XDECREF(type);
3841 Py_XDECREF(value);
3842 Py_XDECREF(traceback);
3843 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003844}
3845
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003846static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003847call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 PyObject *type, *value, *traceback;
3851 int err;
3852 PyErr_Fetch(&type, &value, &traceback);
3853 err = call_trace(func, obj, frame, what, arg);
3854 if (err == 0)
3855 {
3856 PyErr_Restore(type, value, traceback);
3857 return 0;
3858 }
3859 else {
3860 Py_XDECREF(type);
3861 Py_XDECREF(value);
3862 Py_XDECREF(traceback);
3863 return -1;
3864 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003865}
3866
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003867static int
Fred Drake5755ce62001-06-27 19:19:46 +00003868call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 register PyThreadState *tstate = frame->f_tstate;
3872 int result;
3873 if (tstate->tracing)
3874 return 0;
3875 tstate->tracing++;
3876 tstate->use_tracing = 0;
3877 result = func(obj, frame, what, arg);
3878 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3879 || (tstate->c_profilefunc != NULL));
3880 tstate->tracing--;
3881 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003882}
3883
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003884PyObject *
3885_PyEval_CallTracing(PyObject *func, PyObject *args)
3886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 PyFrameObject *frame = PyEval_GetFrame();
3888 PyThreadState *tstate = frame->f_tstate;
3889 int save_tracing = tstate->tracing;
3890 int save_use_tracing = tstate->use_tracing;
3891 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 tstate->tracing = 0;
3894 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3895 || (tstate->c_profilefunc != NULL));
3896 result = PyObject_Call(func, args, NULL);
3897 tstate->tracing = save_tracing;
3898 tstate->use_tracing = save_use_tracing;
3899 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003900}
3901
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003902/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003903static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003904maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3906 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 int result = 0;
3909 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 /* If the last instruction executed isn't in the current
3912 instruction window, reset the window.
3913 */
3914 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3915 PyAddrPair bounds;
3916 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3917 &bounds);
3918 *instr_lb = bounds.ap_lower;
3919 *instr_ub = bounds.ap_upper;
3920 }
3921 /* If the last instruction falls at the start of a line or if
3922 it represents a jump backwards, update the frame's line
3923 number and call the trace function. */
3924 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3925 frame->f_lineno = line;
3926 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3927 }
3928 *instr_prev = frame->f_lasti;
3929 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003930}
3931
Fred Drake5755ce62001-06-27 19:19:46 +00003932void
3933PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 PyThreadState *tstate = PyThreadState_GET();
3936 PyObject *temp = tstate->c_profileobj;
3937 Py_XINCREF(arg);
3938 tstate->c_profilefunc = NULL;
3939 tstate->c_profileobj = NULL;
3940 /* Must make sure that tracing is not ignored if 'temp' is freed */
3941 tstate->use_tracing = tstate->c_tracefunc != NULL;
3942 Py_XDECREF(temp);
3943 tstate->c_profilefunc = func;
3944 tstate->c_profileobj = arg;
3945 /* Flag that tracing or profiling is turned on */
3946 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003947}
3948
3949void
3950PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3951{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 PyThreadState *tstate = PyThreadState_GET();
3953 PyObject *temp = tstate->c_traceobj;
3954 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3955 Py_XINCREF(arg);
3956 tstate->c_tracefunc = NULL;
3957 tstate->c_traceobj = NULL;
3958 /* Must make sure that profiling is not ignored if 'temp' is freed */
3959 tstate->use_tracing = tstate->c_profilefunc != NULL;
3960 Py_XDECREF(temp);
3961 tstate->c_tracefunc = func;
3962 tstate->c_traceobj = arg;
3963 /* Flag that tracing or profiling is turned on */
3964 tstate->use_tracing = ((func != NULL)
3965 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003966}
3967
Guido van Rossumb209a111997-04-29 18:18:01 +00003968PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003969PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 PyFrameObject *current_frame = PyEval_GetFrame();
3972 if (current_frame == NULL)
3973 return PyThreadState_GET()->interp->builtins;
3974 else
3975 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003976}
3977
Guido van Rossumb209a111997-04-29 18:18:01 +00003978PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003979PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 PyFrameObject *current_frame = PyEval_GetFrame();
3982 if (current_frame == NULL)
3983 return NULL;
3984 PyFrame_FastToLocals(current_frame);
3985 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003986}
3987
Guido van Rossumb209a111997-04-29 18:18:01 +00003988PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003989PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 PyFrameObject *current_frame = PyEval_GetFrame();
3992 if (current_frame == NULL)
3993 return NULL;
3994 else
3995 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003996}
3997
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003998PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003999PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004000{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 PyThreadState *tstate = PyThreadState_GET();
4002 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004003}
4004
Guido van Rossum6135a871995-01-09 17:53:26 +00004005int
Tim Peters5ba58662001-07-16 02:29:45 +00004006PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004008 PyFrameObject *current_frame = PyEval_GetFrame();
4009 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 if (current_frame != NULL) {
4012 const int codeflags = current_frame->f_code->co_flags;
4013 const int compilerflags = codeflags & PyCF_MASK;
4014 if (compilerflags) {
4015 result = 1;
4016 cf->cf_flags |= compilerflags;
4017 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004018#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 if (codeflags & CO_GENERATOR_ALLOWED) {
4020 result = 1;
4021 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4022 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004023#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 }
4025 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004026}
4027
Guido van Rossum3f5da241990-12-20 15:06:42 +00004028
Guido van Rossum681d79a1995-07-18 14:51:37 +00004029/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004030 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004031
Guido van Rossumb209a111997-04-29 18:18:01 +00004032PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004033PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004034{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004035 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004037 if (arg == NULL) {
4038 arg = PyTuple_New(0);
4039 if (arg == NULL)
4040 return NULL;
4041 }
4042 else if (!PyTuple_Check(arg)) {
4043 PyErr_SetString(PyExc_TypeError,
4044 "argument list must be a tuple");
4045 return NULL;
4046 }
4047 else
4048 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 if (kw != NULL && !PyDict_Check(kw)) {
4051 PyErr_SetString(PyExc_TypeError,
4052 "keyword list must be a dictionary");
4053 Py_DECREF(arg);
4054 return NULL;
4055 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 result = PyObject_Call(func, arg, kw);
4058 Py_DECREF(arg);
4059 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004060}
4061
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004062const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004063PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004064{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 if (PyMethod_Check(func))
4066 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4067 else if (PyFunction_Check(func))
4068 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4069 else if (PyCFunction_Check(func))
4070 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4071 else
4072 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004073}
4074
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004075const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004076PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 if (PyMethod_Check(func))
4079 return "()";
4080 else if (PyFunction_Check(func))
4081 return "()";
4082 else if (PyCFunction_Check(func))
4083 return "()";
4084 else
4085 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004086}
4087
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004088static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004089err_args(PyObject *func, int flags, int nargs)
4090{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 if (flags & METH_NOARGS)
4092 PyErr_Format(PyExc_TypeError,
4093 "%.200s() takes no arguments (%d given)",
4094 ((PyCFunctionObject *)func)->m_ml->ml_name,
4095 nargs);
4096 else
4097 PyErr_Format(PyExc_TypeError,
4098 "%.200s() takes exactly one argument (%d given)",
4099 ((PyCFunctionObject *)func)->m_ml->ml_name,
4100 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004101}
4102
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004103#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004104if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 if (call_trace(tstate->c_profilefunc, \
4106 tstate->c_profileobj, \
4107 tstate->frame, PyTrace_C_CALL, \
4108 func)) { \
4109 x = NULL; \
4110 } \
4111 else { \
4112 x = call; \
4113 if (tstate->c_profilefunc != NULL) { \
4114 if (x == NULL) { \
4115 call_trace_protected(tstate->c_profilefunc, \
4116 tstate->c_profileobj, \
4117 tstate->frame, PyTrace_C_EXCEPTION, \
4118 func); \
4119 /* XXX should pass (type, value, tb) */ \
4120 } else { \
4121 if (call_trace(tstate->c_profilefunc, \
4122 tstate->c_profileobj, \
4123 tstate->frame, PyTrace_C_RETURN, \
4124 func)) { \
4125 Py_DECREF(x); \
4126 x = NULL; \
4127 } \
4128 } \
4129 } \
4130 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004131} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 x = call; \
4133 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004134
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004135static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004136call_function(PyObject ***pp_stack, int oparg
4137#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004139#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 int na = oparg & 0xff;
4143 int nk = (oparg>>8) & 0xff;
4144 int n = na + 2 * nk;
4145 PyObject **pfunc = (*pp_stack) - n - 1;
4146 PyObject *func = *pfunc;
4147 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 /* Always dispatch PyCFunction first, because these are
4150 presumed to be the most frequent callable object.
4151 */
4152 if (PyCFunction_Check(func) && nk == 0) {
4153 int flags = PyCFunction_GET_FLAGS(func);
4154 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 PCALL(PCALL_CFUNCTION);
4157 if (flags & (METH_NOARGS | METH_O)) {
4158 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4159 PyObject *self = PyCFunction_GET_SELF(func);
4160 if (flags & METH_NOARGS && na == 0) {
4161 C_TRACE(x, (*meth)(self,NULL));
4162 }
4163 else if (flags & METH_O && na == 1) {
4164 PyObject *arg = EXT_POP(*pp_stack);
4165 C_TRACE(x, (*meth)(self,arg));
4166 Py_DECREF(arg);
4167 }
4168 else {
4169 err_args(func, flags, na);
4170 x = NULL;
4171 }
4172 }
4173 else {
4174 PyObject *callargs;
4175 callargs = load_args(pp_stack, na);
4176 READ_TIMESTAMP(*pintr0);
4177 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4178 READ_TIMESTAMP(*pintr1);
4179 Py_XDECREF(callargs);
4180 }
4181 } else {
4182 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4183 /* optimize access to bound methods */
4184 PyObject *self = PyMethod_GET_SELF(func);
4185 PCALL(PCALL_METHOD);
4186 PCALL(PCALL_BOUND_METHOD);
4187 Py_INCREF(self);
4188 func = PyMethod_GET_FUNCTION(func);
4189 Py_INCREF(func);
4190 Py_DECREF(*pfunc);
4191 *pfunc = self;
4192 na++;
4193 n++;
4194 } else
4195 Py_INCREF(func);
4196 READ_TIMESTAMP(*pintr0);
4197 if (PyFunction_Check(func))
4198 x = fast_function(func, pp_stack, n, na, nk);
4199 else
4200 x = do_call(func, pp_stack, na, nk);
4201 READ_TIMESTAMP(*pintr1);
4202 Py_DECREF(func);
4203 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 /* Clear the stack of the function object. Also removes
4206 the arguments in case they weren't consumed already
4207 (fast_function() and err_args() leave them on the stack).
4208 */
4209 while ((*pp_stack) > pfunc) {
4210 w = EXT_POP(*pp_stack);
4211 Py_DECREF(w);
4212 PCALL(PCALL_POP);
4213 }
4214 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004215}
4216
Jeremy Hylton192690e2002-08-16 18:36:11 +00004217/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004218 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004219 For the simplest case -- a function that takes only positional
4220 arguments and is called with only positional arguments -- it
4221 inlines the most primitive frame setup code from
4222 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4223 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004224*/
4225
4226static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004227fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4230 PyObject *globals = PyFunction_GET_GLOBALS(func);
4231 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4232 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4233 PyObject **d = NULL;
4234 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 PCALL(PCALL_FUNCTION);
4237 PCALL(PCALL_FAST_FUNCTION);
4238 if (argdefs == NULL && co->co_argcount == n &&
4239 co->co_kwonlyargcount == 0 && nk==0 &&
4240 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4241 PyFrameObject *f;
4242 PyObject *retval = NULL;
4243 PyThreadState *tstate = PyThreadState_GET();
4244 PyObject **fastlocals, **stack;
4245 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 PCALL(PCALL_FASTER_FUNCTION);
4248 assert(globals != NULL);
4249 /* XXX Perhaps we should create a specialized
4250 PyFrame_New() that doesn't take locals, but does
4251 take builtins without sanity checking them.
4252 */
4253 assert(tstate != NULL);
4254 f = PyFrame_New(tstate, co, globals, NULL);
4255 if (f == NULL)
4256 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004258 fastlocals = f->f_localsplus;
4259 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 for (i = 0; i < n; i++) {
4262 Py_INCREF(*stack);
4263 fastlocals[i] = *stack++;
4264 }
4265 retval = PyEval_EvalFrameEx(f,0);
4266 ++tstate->recursion_depth;
4267 Py_DECREF(f);
4268 --tstate->recursion_depth;
4269 return retval;
4270 }
4271 if (argdefs != NULL) {
4272 d = &PyTuple_GET_ITEM(argdefs, 0);
4273 nd = Py_SIZE(argdefs);
4274 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004275 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 (PyObject *)NULL, (*pp_stack)-n, na,
4277 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4278 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004279}
4280
4281static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004282update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4283 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 PyObject *kwdict = NULL;
4286 if (orig_kwdict == NULL)
4287 kwdict = PyDict_New();
4288 else {
4289 kwdict = PyDict_Copy(orig_kwdict);
4290 Py_DECREF(orig_kwdict);
4291 }
4292 if (kwdict == NULL)
4293 return NULL;
4294 while (--nk >= 0) {
4295 int err;
4296 PyObject *value = EXT_POP(*pp_stack);
4297 PyObject *key = EXT_POP(*pp_stack);
4298 if (PyDict_GetItem(kwdict, key) != NULL) {
4299 PyErr_Format(PyExc_TypeError,
4300 "%.200s%s got multiple values "
4301 "for keyword argument '%U'",
4302 PyEval_GetFuncName(func),
4303 PyEval_GetFuncDesc(func),
4304 key);
4305 Py_DECREF(key);
4306 Py_DECREF(value);
4307 Py_DECREF(kwdict);
4308 return NULL;
4309 }
4310 err = PyDict_SetItem(kwdict, key, value);
4311 Py_DECREF(key);
4312 Py_DECREF(value);
4313 if (err) {
4314 Py_DECREF(kwdict);
4315 return NULL;
4316 }
4317 }
4318 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004319}
4320
4321static PyObject *
4322update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 callargs = PyTuple_New(nstack + nstar);
4328 if (callargs == NULL) {
4329 return NULL;
4330 }
4331 if (nstar) {
4332 int i;
4333 for (i = 0; i < nstar; i++) {
4334 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4335 Py_INCREF(a);
4336 PyTuple_SET_ITEM(callargs, nstack + i, a);
4337 }
4338 }
4339 while (--nstack >= 0) {
4340 w = EXT_POP(*pp_stack);
4341 PyTuple_SET_ITEM(callargs, nstack, w);
4342 }
4343 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004344}
4345
4346static PyObject *
4347load_args(PyObject ***pp_stack, int na)
4348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 PyObject *args = PyTuple_New(na);
4350 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 if (args == NULL)
4353 return NULL;
4354 while (--na >= 0) {
4355 w = EXT_POP(*pp_stack);
4356 PyTuple_SET_ITEM(args, na, w);
4357 }
4358 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004359}
4360
4361static PyObject *
4362do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 PyObject *callargs = NULL;
4365 PyObject *kwdict = NULL;
4366 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 if (nk > 0) {
4369 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4370 if (kwdict == NULL)
4371 goto call_fail;
4372 }
4373 callargs = load_args(pp_stack, na);
4374 if (callargs == NULL)
4375 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004376#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 /* At this point, we have to look at the type of func to
4378 update the call stats properly. Do it here so as to avoid
4379 exposing the call stats machinery outside ceval.c
4380 */
4381 if (PyFunction_Check(func))
4382 PCALL(PCALL_FUNCTION);
4383 else if (PyMethod_Check(func))
4384 PCALL(PCALL_METHOD);
4385 else if (PyType_Check(func))
4386 PCALL(PCALL_TYPE);
4387 else if (PyCFunction_Check(func))
4388 PCALL(PCALL_CFUNCTION);
4389 else
4390 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004391#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 if (PyCFunction_Check(func)) {
4393 PyThreadState *tstate = PyThreadState_GET();
4394 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4395 }
4396 else
4397 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004398call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 Py_XDECREF(callargs);
4400 Py_XDECREF(kwdict);
4401 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004402}
4403
4404static PyObject *
4405ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4406{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 int nstar = 0;
4408 PyObject *callargs = NULL;
4409 PyObject *stararg = NULL;
4410 PyObject *kwdict = NULL;
4411 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 if (flags & CALL_FLAG_KW) {
4414 kwdict = EXT_POP(*pp_stack);
4415 if (!PyDict_Check(kwdict)) {
4416 PyObject *d;
4417 d = PyDict_New();
4418 if (d == NULL)
4419 goto ext_call_fail;
4420 if (PyDict_Update(d, kwdict) != 0) {
4421 Py_DECREF(d);
4422 /* PyDict_Update raises attribute
4423 * error (percolated from an attempt
4424 * to get 'keys' attribute) instead of
4425 * a type error if its second argument
4426 * is not a mapping.
4427 */
4428 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4429 PyErr_Format(PyExc_TypeError,
4430 "%.200s%.200s argument after ** "
4431 "must be a mapping, not %.200s",
4432 PyEval_GetFuncName(func),
4433 PyEval_GetFuncDesc(func),
4434 kwdict->ob_type->tp_name);
4435 }
4436 goto ext_call_fail;
4437 }
4438 Py_DECREF(kwdict);
4439 kwdict = d;
4440 }
4441 }
4442 if (flags & CALL_FLAG_VAR) {
4443 stararg = EXT_POP(*pp_stack);
4444 if (!PyTuple_Check(stararg)) {
4445 PyObject *t = NULL;
4446 t = PySequence_Tuple(stararg);
4447 if (t == NULL) {
4448 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4449 PyErr_Format(PyExc_TypeError,
4450 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004451 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 PyEval_GetFuncName(func),
4453 PyEval_GetFuncDesc(func),
4454 stararg->ob_type->tp_name);
4455 }
4456 goto ext_call_fail;
4457 }
4458 Py_DECREF(stararg);
4459 stararg = t;
4460 }
4461 nstar = PyTuple_GET_SIZE(stararg);
4462 }
4463 if (nk > 0) {
4464 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4465 if (kwdict == NULL)
4466 goto ext_call_fail;
4467 }
4468 callargs = update_star_args(na, nstar, stararg, pp_stack);
4469 if (callargs == NULL)
4470 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004471#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 /* At this point, we have to look at the type of func to
4473 update the call stats properly. Do it here so as to avoid
4474 exposing the call stats machinery outside ceval.c
4475 */
4476 if (PyFunction_Check(func))
4477 PCALL(PCALL_FUNCTION);
4478 else if (PyMethod_Check(func))
4479 PCALL(PCALL_METHOD);
4480 else if (PyType_Check(func))
4481 PCALL(PCALL_TYPE);
4482 else if (PyCFunction_Check(func))
4483 PCALL(PCALL_CFUNCTION);
4484 else
4485 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004486#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 if (PyCFunction_Check(func)) {
4488 PyThreadState *tstate = PyThreadState_GET();
4489 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4490 }
4491 else
4492 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004493ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 Py_XDECREF(callargs);
4495 Py_XDECREF(kwdict);
4496 Py_XDECREF(stararg);
4497 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004498}
4499
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004500/* Extract a slice index from a PyInt or PyLong or an object with the
4501 nb_index slot defined, and store in *pi.
4502 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4503 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 +00004504 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004505*/
Tim Petersb5196382001-12-16 19:44:20 +00004506/* Note: If v is NULL, return success without storing into *pi. This
4507 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4508 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004509*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004510int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004511_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004512{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 if (v != NULL) {
4514 Py_ssize_t x;
4515 if (PyIndex_Check(v)) {
4516 x = PyNumber_AsSsize_t(v, NULL);
4517 if (x == -1 && PyErr_Occurred())
4518 return 0;
4519 }
4520 else {
4521 PyErr_SetString(PyExc_TypeError,
4522 "slice indices must be integers or "
4523 "None or have an __index__ method");
4524 return 0;
4525 }
4526 *pi = x;
4527 }
4528 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004529}
4530
Guido van Rossum486364b2007-06-30 05:01:58 +00004531#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004533
Guido van Rossumb209a111997-04-29 18:18:01 +00004534static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004535cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004536{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 int res = 0;
4538 switch (op) {
4539 case PyCmp_IS:
4540 res = (v == w);
4541 break;
4542 case PyCmp_IS_NOT:
4543 res = (v != w);
4544 break;
4545 case PyCmp_IN:
4546 res = PySequence_Contains(w, v);
4547 if (res < 0)
4548 return NULL;
4549 break;
4550 case PyCmp_NOT_IN:
4551 res = PySequence_Contains(w, v);
4552 if (res < 0)
4553 return NULL;
4554 res = !res;
4555 break;
4556 case PyCmp_EXC_MATCH:
4557 if (PyTuple_Check(w)) {
4558 Py_ssize_t i, length;
4559 length = PyTuple_Size(w);
4560 for (i = 0; i < length; i += 1) {
4561 PyObject *exc = PyTuple_GET_ITEM(w, i);
4562 if (!PyExceptionClass_Check(exc)) {
4563 PyErr_SetString(PyExc_TypeError,
4564 CANNOT_CATCH_MSG);
4565 return NULL;
4566 }
4567 }
4568 }
4569 else {
4570 if (!PyExceptionClass_Check(w)) {
4571 PyErr_SetString(PyExc_TypeError,
4572 CANNOT_CATCH_MSG);
4573 return NULL;
4574 }
4575 }
4576 res = PyErr_GivenExceptionMatches(v, w);
4577 break;
4578 default:
4579 return PyObject_RichCompare(v, w, op);
4580 }
4581 v = res ? Py_True : Py_False;
4582 Py_INCREF(v);
4583 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004584}
4585
Thomas Wouters52152252000-08-17 22:55:00 +00004586static PyObject *
4587import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004588{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 x = PyObject_GetAttr(v, name);
4592 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4593 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4594 }
4595 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004596}
Guido van Rossumac7be682001-01-17 15:42:30 +00004597
Thomas Wouters52152252000-08-17 22:55:00 +00004598static int
4599import_all_from(PyObject *locals, PyObject *v)
4600{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004601 _Py_IDENTIFIER(__all__);
4602 _Py_IDENTIFIER(__dict__);
4603 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 PyObject *dict, *name, *value;
4605 int skip_leading_underscores = 0;
4606 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 if (all == NULL) {
4609 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4610 return -1; /* Unexpected error */
4611 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004612 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 if (dict == NULL) {
4614 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4615 return -1;
4616 PyErr_SetString(PyExc_ImportError,
4617 "from-import-* object has no __dict__ and no __all__");
4618 return -1;
4619 }
4620 all = PyMapping_Keys(dict);
4621 Py_DECREF(dict);
4622 if (all == NULL)
4623 return -1;
4624 skip_leading_underscores = 1;
4625 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 for (pos = 0, err = 0; ; pos++) {
4628 name = PySequence_GetItem(all, pos);
4629 if (name == NULL) {
4630 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4631 err = -1;
4632 else
4633 PyErr_Clear();
4634 break;
4635 }
4636 if (skip_leading_underscores &&
4637 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004638 PyUnicode_READY(name) != -1 &&
4639 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 {
4641 Py_DECREF(name);
4642 continue;
4643 }
4644 value = PyObject_GetAttr(v, name);
4645 if (value == NULL)
4646 err = -1;
4647 else if (PyDict_CheckExact(locals))
4648 err = PyDict_SetItem(locals, name, value);
4649 else
4650 err = PyObject_SetItem(locals, name, value);
4651 Py_DECREF(name);
4652 Py_XDECREF(value);
4653 if (err != 0)
4654 break;
4655 }
4656 Py_DECREF(all);
4657 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004658}
4659
Guido van Rossumac7be682001-01-17 15:42:30 +00004660static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004661format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 if (!obj)
4666 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 obj_str = _PyUnicode_AsString(obj);
4669 if (!obj_str)
4670 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004673}
Guido van Rossum950361c1997-01-24 13:49:28 +00004674
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004675static void
4676format_exc_unbound(PyCodeObject *co, int oparg)
4677{
4678 PyObject *name;
4679 /* Don't stomp existing exception */
4680 if (PyErr_Occurred())
4681 return;
4682 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4683 name = PyTuple_GET_ITEM(co->co_cellvars,
4684 oparg);
4685 format_exc_check_arg(
4686 PyExc_UnboundLocalError,
4687 UNBOUNDLOCAL_ERROR_MSG,
4688 name);
4689 } else {
4690 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4691 PyTuple_GET_SIZE(co->co_cellvars));
4692 format_exc_check_arg(PyExc_NameError,
4693 UNBOUNDFREE_ERROR_MSG, name);
4694 }
4695}
4696
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004697static PyObject *
4698unicode_concatenate(PyObject *v, PyObject *w,
4699 PyFrameObject *f, unsigned char *next_instr)
4700{
4701 PyObject *res;
4702 if (Py_REFCNT(v) == 2) {
4703 /* In the common case, there are 2 references to the value
4704 * stored in 'variable' when the += is performed: one on the
4705 * value stack (in 'v') and one still stored in the
4706 * 'variable'. We try to delete the variable now to reduce
4707 * the refcnt to 1.
4708 */
4709 switch (*next_instr) {
4710 case STORE_FAST:
4711 {
4712 int oparg = PEEKARG();
4713 PyObject **fastlocals = f->f_localsplus;
4714 if (GETLOCAL(oparg) == v)
4715 SETLOCAL(oparg, NULL);
4716 break;
4717 }
4718 case STORE_DEREF:
4719 {
4720 PyObject **freevars = (f->f_localsplus +
4721 f->f_code->co_nlocals);
4722 PyObject *c = freevars[PEEKARG()];
4723 if (PyCell_GET(c) == v)
4724 PyCell_Set(c, NULL);
4725 break;
4726 }
4727 case STORE_NAME:
4728 {
4729 PyObject *names = f->f_code->co_names;
4730 PyObject *name = GETITEM(names, PEEKARG());
4731 PyObject *locals = f->f_locals;
4732 if (PyDict_CheckExact(locals) &&
4733 PyDict_GetItem(locals, name) == v) {
4734 if (PyDict_DelItem(locals, name) != 0) {
4735 PyErr_Clear();
4736 }
4737 }
4738 break;
4739 }
4740 }
4741 }
4742 res = v;
4743 PyUnicode_Append(&res, w);
4744 return res;
4745}
4746
Guido van Rossum950361c1997-01-24 13:49:28 +00004747#ifdef DYNAMIC_EXECUTION_PROFILE
4748
Skip Montanarof118cb12001-10-15 20:51:38 +00004749static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004750getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 int i;
4753 PyObject *l = PyList_New(256);
4754 if (l == NULL) return NULL;
4755 for (i = 0; i < 256; i++) {
4756 PyObject *x = PyLong_FromLong(a[i]);
4757 if (x == NULL) {
4758 Py_DECREF(l);
4759 return NULL;
4760 }
4761 PyList_SetItem(l, i, x);
4762 }
4763 for (i = 0; i < 256; i++)
4764 a[i] = 0;
4765 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004766}
4767
4768PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004769_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004770{
4771#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004772 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004773#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004774 int i;
4775 PyObject *l = PyList_New(257);
4776 if (l == NULL) return NULL;
4777 for (i = 0; i < 257; i++) {
4778 PyObject *x = getarray(dxpairs[i]);
4779 if (x == NULL) {
4780 Py_DECREF(l);
4781 return NULL;
4782 }
4783 PyList_SetItem(l, i, x);
4784 }
4785 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004786#endif
4787}
4788
4789#endif