blob: ab419dc931188eb1106e31c1a69f9ee8ba4f32bb [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 Pitrou9ed5f272013-08-13 20:18:52 +020040 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
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200365/* This function is called from PyOS_AfterFork to destroy all threads which are
366 * not running in the child process, and clear internal locks which might be
367 * held by those threads. (This could also be done using pthread_atfork
368 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000369
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;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200375 PyThreadState *current_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();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200381 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 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 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200386 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 "threading");
388 if (threading == NULL) {
389 /* threading not imported */
390 PyErr_Clear();
391 return;
392 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200393 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 if (result == NULL)
395 PyErr_WriteUnraisable(threading);
396 else
397 Py_DECREF(result);
398 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200399
400 /* Destroy all threads except the current one */
401 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000402}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000403
404#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000405static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000406static int pending_async_exc = 0;
407#endif /* WITH_THREAD */
408
409/* This function is used to signal that async exceptions are waiting to be
410 raised, therefore it is also useful in non-threaded builds. */
411
412void
413_PyEval_SignalAsyncExc(void)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000416}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417
Guido van Rossumff4949e1992-08-05 19:58:53 +0000418/* Functions save_thread and restore_thread are always defined so
419 dynamically loaded modules needn't be compiled separately for use
420 with and without threads: */
421
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000422PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 PyThreadState *tstate = PyThreadState_Swap(NULL);
426 if (tstate == NULL)
427 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000428#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (gil_created())
430 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433}
434
435void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (tstate == NULL)
439 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000440#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (gil_created()) {
442 int err = errno;
443 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200444 /* _Py_Finalizing is protected by the GIL */
445 if (_Py_Finalizing && tstate != _Py_Finalizing) {
446 drop_gil(tstate);
447 PyThread_exit_thread();
448 assert(0); /* unreachable */
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 errno = err;
451 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454}
455
456
Guido van Rossuma9672091994-09-14 13:31:22 +0000457/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
458 signal handlers or Mac I/O completion routines) can schedule calls
459 to a function to be called synchronously.
460 The synchronous function is called with one void* argument.
461 It should return 0 for success or -1 for failure -- failure should
462 be accompanied by an exception.
463
464 If registry succeeds, the registry function returns 0; if it fails
465 (e.g. due to too many pending calls) it returns -1 (without setting
466 an exception condition).
467
468 Note that because registry may occur from within signal handlers,
469 or other asynchronous events, calling malloc() is unsafe!
470
471#ifdef WITH_THREAD
472 Any thread can schedule pending calls, but only the main thread
473 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474 There is no facility to schedule calls to a particular thread, but
475 that should be easy to change, should that ever be required. In
476 that case, the static variables here should go into the python
477 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000478#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000480
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481#ifdef WITH_THREAD
482
483/* The WITH_THREAD implementation is thread-safe. It allows
484 scheduling to be made from any thread, and even from an executing
485 callback.
486 */
487
488#define NPENDINGCALLS 32
489static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 int (*func)(void *);
491 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492} pendingcalls[NPENDINGCALLS];
493static int pendingfirst = 0;
494static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
496int
497Py_AddPendingCall(int (*func)(void *), void *arg)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we
505 * hold the lock during the Py_MakePendingCalls() function.
506 * This avoids a deadlock in that case.
507 * Note that signals can be delivered on any thread. In particular,
508 * on Windows, a SIGINT is delivered on a system-created worker
509 * thread.
510 * We also check for lock being NULL, in the unlikely case that
511 * this function is called before any bytecode evaluation takes place.
512 */
513 if (lock != NULL) {
514 for (i = 0; i<100; i++) {
515 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
516 break;
517 }
518 if (i == 100)
519 return -1;
520 }
521
522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
524 if (j == pendingfirst) {
525 result = -1; /* Queue full */
526 } else {
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
530 }
531 /* signal main loop */
532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL)
534 PyThread_release_lock(lock);
535 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538int
539Py_MakePendingCalls(void)
540{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200541 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int i;
543 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (!pending_lock) {
546 /* initial allocation of the lock */
547 pending_lock = PyThread_allocate_lock();
548 if (pending_lock == NULL)
549 return -1;
550 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* only service pending calls on main thread */
553 if (main_thread && PyThread_get_thread_ident() != main_thread)
554 return 0;
555 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200556 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200558 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* perform a bounded number of calls, in case of recursion */
560 for (i=0; i<NPENDINGCALLS; i++) {
561 int j;
562 int (*func)(void *);
563 void *arg = NULL;
564
565 /* pop one item off the queue while holding the lock */
566 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
567 j = pendingfirst;
568 if (j == pendinglast) {
569 func = NULL; /* Queue empty */
570 } else {
571 func = pendingcalls[j].func;
572 arg = pendingcalls[j].arg;
573 pendingfirst = (j + 1) % NPENDINGCALLS;
574 }
575 if (pendingfirst != pendinglast)
576 SIGNAL_PENDING_CALLS();
577 else
578 UNSIGNAL_PENDING_CALLS();
579 PyThread_release_lock(pending_lock);
580 /* having released the lock, perform the callback */
581 if (func == NULL)
582 break;
583 r = func(arg);
584 if (r)
585 break;
586 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200587 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000589}
590
591#else /* if ! defined WITH_THREAD */
592
593/*
594 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
595 This code is used for signal handling in python that isn't built
596 with WITH_THREAD.
597 Don't use this implementation when Py_AddPendingCalls() can happen
598 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599
Guido van Rossuma9672091994-09-14 13:31:22 +0000600 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601 (1) nested asynchronous calls to Py_AddPendingCall()
602 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604 (1) is very unlikely because typically signal delivery
605 is blocked during signal handling. So it should be impossible.
606 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000607 The current code is safe against (2), but not against (1).
608 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 thread is present, interrupted by signals, and that the critical
610 section is protected with the "busy" variable. On Windows, which
611 delivers SIGINT on a system thread, this does not hold and therefore
612 Windows really shouldn't use this version.
613 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000614*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000615
Guido van Rossuma9672091994-09-14 13:31:22 +0000616#define NPENDINGCALLS 32
617static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 int (*func)(void *);
619 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000620} pendingcalls[NPENDINGCALLS];
621static volatile int pendingfirst = 0;
622static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000623static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000624
625int
Thomas Wouters334fb892000-07-25 12:56:38 +0000626Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 static volatile int busy = 0;
629 int i, j;
630 /* XXX Begin critical section */
631 if (busy)
632 return -1;
633 busy = 1;
634 i = pendinglast;
635 j = (i + 1) % NPENDINGCALLS;
636 if (j == pendingfirst) {
637 busy = 0;
638 return -1; /* Queue full */
639 }
640 pendingcalls[i].func = func;
641 pendingcalls[i].arg = arg;
642 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 SIGNAL_PENDING_CALLS();
645 busy = 0;
646 /* XXX End critical section */
647 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000648}
649
Guido van Rossum180d7b41994-09-29 09:45:57 +0000650int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 static int busy = 0;
654 if (busy)
655 return 0;
656 busy = 1;
657 UNSIGNAL_PENDING_CALLS();
658 for (;;) {
659 int i;
660 int (*func)(void *);
661 void *arg;
662 i = pendingfirst;
663 if (i == pendinglast)
664 break; /* Queue empty */
665 func = pendingcalls[i].func;
666 arg = pendingcalls[i].arg;
667 pendingfirst = (i + 1) % NPENDINGCALLS;
668 if (func(arg) < 0) {
669 busy = 0;
670 SIGNAL_PENDING_CALLS(); /* We're not done yet */
671 return -1;
672 }
673 }
674 busy = 0;
675 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000676}
677
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000678#endif /* WITH_THREAD */
679
Guido van Rossuma9672091994-09-14 13:31:22 +0000680
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681/* The interpreter's recursion limit */
682
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000683#ifndef Py_DEFAULT_RECURSION_LIMIT
684#define Py_DEFAULT_RECURSION_LIMIT 1000
685#endif
686static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
687int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000689int
690Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693}
694
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000695void
696Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 recursion_limit = new_limit;
699 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700}
701
Armin Rigo2b3eb402003-10-28 12:05:48 +0000702/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
703 if the recursion_depth reaches _Py_CheckRecursionLimit.
704 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
705 to guarantee that _Py_CheckRecursiveCall() is regularly called.
706 Without USE_STACKCHECK, there is no need for this. */
707int
708_Py_CheckRecursiveCall(char *where)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711
712#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (PyOS_CheckStack()) {
714 --tstate->recursion_depth;
715 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
716 return -1;
717 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 _Py_CheckRecursionLimit = recursion_limit;
720 if (tstate->recursion_critical)
721 /* Somebody asked that we don't check for recursion. */
722 return 0;
723 if (tstate->overflowed) {
724 if (tstate->recursion_depth > recursion_limit + 50) {
725 /* Overflowing while handling an overflow. Give up. */
726 Py_FatalError("Cannot recover from stack overflow.");
727 }
728 return 0;
729 }
730 if (tstate->recursion_depth > recursion_limit) {
731 --tstate->recursion_depth;
732 tstate->overflowed = 1;
733 PyErr_Format(PyExc_RuntimeError,
734 "maximum recursion depth exceeded%s",
735 where);
736 return -1;
737 }
738 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000739}
740
Guido van Rossum374a9221991-04-04 10:40:29 +0000741/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000742enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000743 WHY_NOT = 0x0001, /* No error */
744 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000745 WHY_RETURN = 0x0008, /* 'return' statement */
746 WHY_BREAK = 0x0010, /* 'break' statement */
747 WHY_CONTINUE = 0x0020, /* 'continue' statement */
748 WHY_YIELD = 0x0040, /* 'yield' operator */
749 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000750};
Guido van Rossum374a9221991-04-04 10:40:29 +0000751
Benjamin Peterson87880242011-07-03 16:48:31 -0500752static void save_exc_state(PyThreadState *, PyFrameObject *);
753static void swap_exc_state(PyThreadState *, PyFrameObject *);
754static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400755static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000756static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000757
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000758/* Records whether tracing is on for any thread. Counts the number of
759 threads for which tstate->c_tracefunc is non-NULL, so if the value
760 is 0, we know we don't have to check this thread's c_tracefunc.
761 This speeds up the if statement in PyEval_EvalFrameEx() after
762 fast_next_opcode*/
763static int _Py_TracingPossible = 0;
764
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000765
Guido van Rossum374a9221991-04-04 10:40:29 +0000766
Guido van Rossumb209a111997-04-29 18:18:01 +0000767PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000768PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 return PyEval_EvalCodeEx(co,
771 globals, locals,
772 (PyObject **)NULL, 0,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000776}
777
778
779/* Interpreter main loop */
780
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000781PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000782PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /* This is for backward compatibility with extension modules that
784 used this API; core interpreter code should call
785 PyEval_EvalFrameEx() */
786 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000787}
788
789PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000791{
Guido van Rossum950361c1997-01-24 13:49:28 +0000792#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000794#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200795 PyObject **stack_pointer; /* Next free slot in value stack */
796 unsigned char *next_instr;
797 int opcode; /* Current opcode */
798 int oparg; /* Current opcode argument, if any */
799 enum why_code why; /* Reason for block stack unwind */
800 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 PyObject *retval = NULL; /* Return value */
802 PyThreadState *tstate = PyThreadState_GET();
803 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 is true when the line being executed has changed. The
810 initial values are such as to make this false the first
811 time it is tested. */
812 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 unsigned char *first_instr;
815 PyObject *names;
816 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000817
Brett Cannon368b4b72012-04-02 12:17:59 -0400818#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200819 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400820#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200821
Antoine Pitroub52ec782009-01-25 16:34:23 +0000822/* Computed GOTOs, or
823 the-optimization-commonly-but-improperly-known-as-"threaded code"
824 using gcc's labels-as-values extension
825 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
826
827 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829 combined with a lookup table of jump addresses. However, since the
830 indirect jump instruction is shared by all opcodes, the CPU will have a
831 hard time making the right prediction for where to jump next (actually,
832 it will be always wrong except in the uncommon case of a sequence of
833 several identical opcodes).
834
835 "Threaded code" in contrast, uses an explicit jump table and an explicit
836 indirect jump instruction at the end of each opcode. Since the jump
837 instruction is at a different address for each opcode, the CPU will make a
838 separate prediction for each of these instructions, which is equivalent to
839 predicting the second opcode of each opcode pair. These predictions have
840 a much better chance to turn out valid, especially in small bytecode loops.
841
842 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844 and potentially many more instructions (depending on the pipeline width).
845 A correctly predicted branch, however, is nearly free.
846
847 At the time of this writing, the "threaded code" version is up to 15-20%
848 faster than the normal "switch" version, depending on the compiler and the
849 CPU architecture.
850
851 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
852 because it would render the measurements invalid.
853
854
855 NOTE: care must be taken that the compiler doesn't try to "optimize" the
856 indirect jumps by sharing them between all opcodes. Such optimizations
857 can be disabled on gcc by using the -fno-gcse flag (or possibly
858 -fno-crossjumping).
859*/
860
Antoine Pitrou042b1282010-08-13 21:15:58 +0000861#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000863#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000864#endif
865
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#ifdef HAVE_COMPUTED_GOTOS
867 #ifndef USE_COMPUTED_GOTOS
868 #define USE_COMPUTED_GOTOS 1
869 #endif
870#else
871 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
872 #error "Computed gotos are not supported on this compiler."
873 #endif
874 #undef USE_COMPUTED_GOTOS
875 #define USE_COMPUTED_GOTOS 0
876#endif
877
878#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879/* Import the static jump table */
880#include "opcode_targets.h"
881
882/* This macro is used when several opcodes defer to the same implementation
883 (e.g. SETUP_LOOP, SETUP_FINALLY) */
884#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 TARGET_##op: \
886 opcode = op; \
887 if (HAS_ARG(op)) \
888 oparg = NEXTARG(); \
889 case op: \
890 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000891
892#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 TARGET_##op: \
894 opcode = op; \
895 if (HAS_ARG(op)) \
896 oparg = NEXTARG(); \
897 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000898
899
900#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 { \
902 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
903 FAST_DISPATCH(); \
904 } \
905 continue; \
906 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000907
908#ifdef LLTRACE
909#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 { \
911 if (!lltrace && !_Py_TracingPossible) { \
912 f->f_lasti = INSTR_OFFSET(); \
913 goto *opcode_targets[*next_instr++]; \
914 } \
915 goto fast_next_opcode; \
916 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917#else
918#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 { \
920 if (!_Py_TracingPossible) { \
921 f->f_lasti = INSTR_OFFSET(); \
922 goto *opcode_targets[*next_instr++]; \
923 } \
924 goto fast_next_opcode; \
925 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#endif
927
928#else
929#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000931#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* silence compiler warnings about `impl` unused */ \
933 if (0) goto impl; \
934 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000935#define DISPATCH() continue
936#define FAST_DISPATCH() goto fast_next_opcode
937#endif
938
939
Neal Norwitza81d2202002-07-14 00:27:26 +0000940/* Tuple access macros */
941
942#ifndef Py_DEBUG
943#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
944#else
945#define GETITEM(v, i) PyTuple_GetItem((v), (i))
946#endif
947
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000948#ifdef WITH_TSC
949/* Use Pentium timestamp counter to mark certain events:
950 inst0 -- beginning of switch statement for opcode dispatch
951 inst1 -- end of switch statement (may be skipped)
952 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000954 (may be skipped)
955 intr1 -- beginning of long interruption
956 intr2 -- end of long interruption
957
958 Many opcodes call out to helper C functions. In some cases, the
959 time in those functions should be counted towards the time for the
960 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
961 calls another Python function; there's no point in charge all the
962 bytecode executed by the called function to the caller.
963
964 It's hard to make a useful judgement statically. In the presence
965 of operator overloading, it's impossible to tell if a call will
966 execute new Python code or not.
967
968 It's a case-by-case judgement. I'll use intr1 for the following
969 cases:
970
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000971 IMPORT_STAR
972 IMPORT_FROM
973 CALL_FUNCTION (and friends)
974
975 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
977 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 READ_TIMESTAMP(inst0);
980 READ_TIMESTAMP(inst1);
981 READ_TIMESTAMP(loop0);
982 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* shut up the compiler */
985 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000986#endif
987
Guido van Rossum374a9221991-04-04 10:40:29 +0000988/* Code access macros */
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990#define INSTR_OFFSET() ((int)(next_instr - first_instr))
991#define NEXTOP() (*next_instr++)
992#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
993#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
994#define JUMPTO(x) (next_instr = first_instr + (x))
995#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000996
Raymond Hettingerf606f872003-03-16 03:11:04 +0000997/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 Some opcodes tend to come in pairs thus making it possible to
999 predict the second code when the first is run. For example,
1000 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1001 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Verifying the prediction costs a single high-speed test of a register
1004 variable against a constant. If the pairing was good, then the
1005 processor's own internal branch predication has a high likelihood of
1006 success, resulting in a nearly zero-overhead transition to the
1007 next opcode. A successful prediction saves a trip through the eval-loop
1008 including its two unpredictable branches, the HAS_ARG test and the
1009 switch-case. Combined with the processor's internal branch prediction,
1010 a successful PREDICT has the effect of making the two opcodes run as if
1011 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001012
Georg Brandl86b2fb92008-07-16 03:43:04 +00001013 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 predictions turned-on and interpret the results as if some opcodes
1015 had been combined or turn-off predictions so that the opcode frequency
1016 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017
1018 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 the CPU to record separate branch prediction information for each
1020 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001021
Raymond Hettingerf606f872003-03-16 03:11:04 +00001022*/
1023
Antoine Pitrou042b1282010-08-13 21:15:58 +00001024#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025#define PREDICT(op) if (0) goto PRED_##op
1026#define PREDICTED(op) PRED_##op:
1027#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001028#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1030#define PREDICTED(op) PRED_##op: next_instr++
1031#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001032#endif
1033
Raymond Hettingerf606f872003-03-16 03:11:04 +00001034
Guido van Rossum374a9221991-04-04 10:40:29 +00001035/* Stack manipulation macros */
1036
Martin v. Löwis18e16552006-02-15 17:27:45 +00001037/* The stack can grow at most MAXINT deep, as co_nlocals and
1038 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001039#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1040#define EMPTY() (STACK_LEVEL() == 0)
1041#define TOP() (stack_pointer[-1])
1042#define SECOND() (stack_pointer[-2])
1043#define THIRD() (stack_pointer[-3])
1044#define FOURTH() (stack_pointer[-4])
1045#define PEEK(n) (stack_pointer[-(n)])
1046#define SET_TOP(v) (stack_pointer[-1] = (v))
1047#define SET_SECOND(v) (stack_pointer[-2] = (v))
1048#define SET_THIRD(v) (stack_pointer[-3] = (v))
1049#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1050#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1051#define BASIC_STACKADJ(n) (stack_pointer += n)
1052#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1053#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001054
Guido van Rossum96a42c81992-01-12 02:29:51 +00001055#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001057 lltrace && prtrace(TOP(), "push")); \
1058 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001060 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001062 lltrace && prtrace(TOP(), "stackadj")); \
1063 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001064#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1066 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001067#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001068#define PUSH(v) BASIC_PUSH(v)
1069#define POP() BASIC_POP()
1070#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001071#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001072#endif
1073
Guido van Rossum681d79a1995-07-18 14:51:37 +00001074/* Local variable macros */
1075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001077
1078/* The SETLOCAL() macro must not DECREF the local variable in-place and
1079 then store the new value; it must copy the old value to a temporary
1080 value, then store the new value, and then DECREF the temporary value.
1081 This is because it is possible that during the DECREF the frame is
1082 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1083 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001085 GETLOCAL(i) = value; \
1086 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001087
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001088
1089#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 while (STACK_LEVEL() > (b)->b_level) { \
1091 PyObject *v = POP(); \
1092 Py_XDECREF(v); \
1093 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001094
1095#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 { \
1097 PyObject *type, *value, *traceback; \
1098 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1099 while (STACK_LEVEL() > (b)->b_level + 3) { \
1100 value = POP(); \
1101 Py_XDECREF(value); \
1102 } \
1103 type = tstate->exc_type; \
1104 value = tstate->exc_value; \
1105 traceback = tstate->exc_traceback; \
1106 tstate->exc_type = POP(); \
1107 tstate->exc_value = POP(); \
1108 tstate->exc_traceback = POP(); \
1109 Py_XDECREF(type); \
1110 Py_XDECREF(value); \
1111 Py_XDECREF(traceback); \
1112 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001113
Guido van Rossuma027efa1997-05-05 20:56:21 +00001114/* Start of code */
1115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* push frame */
1117 if (Py_EnterRecursiveCall(""))
1118 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (tstate->use_tracing) {
1123 if (tstate->c_tracefunc != NULL) {
1124 /* tstate->c_tracefunc, if defined, is a
1125 function that will be called on *every* entry
1126 to a code block. Its return value, if not
1127 None, is a function that will be called at
1128 the start of each executed line of code.
1129 (Actually, the function must return itself
1130 in order to continue tracing.) The trace
1131 functions are called with three arguments:
1132 a pointer to the current frame, a string
1133 indicating why the function is called, and
1134 an argument which depends on the situation.
1135 The global trace function is also called
1136 whenever an exception is detected. */
1137 if (call_trace_protected(tstate->c_tracefunc,
1138 tstate->c_traceobj,
1139 f, PyTrace_CALL, Py_None)) {
1140 /* Trace function raised an error */
1141 goto exit_eval_frame;
1142 }
1143 }
1144 if (tstate->c_profilefunc != NULL) {
1145 /* Similar for c_profilefunc, except it needn't
1146 return itself and isn't called for "line" events */
1147 if (call_trace_protected(tstate->c_profilefunc,
1148 tstate->c_profileobj,
1149 f, PyTrace_CALL, Py_None)) {
1150 /* Profile function raised an error */
1151 goto exit_eval_frame;
1152 }
1153 }
1154 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 co = f->f_code;
1157 names = co->co_names;
1158 consts = co->co_consts;
1159 fastlocals = f->f_localsplus;
1160 freevars = f->f_localsplus + co->co_nlocals;
1161 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1162 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 f->f_lasti now refers to the index of the last instruction
1165 executed. You might think this was obvious from the name, but
1166 this wasn't always true before 2.3! PyFrame_New now sets
1167 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1168 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1169 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001170 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1171 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 When the PREDICT() macros are enabled, some opcode pairs follow in
1174 direct succession without updating f->f_lasti. A successful
1175 prediction effectively links the two codes together as if they
1176 were a single new opcode; accordingly,f->f_lasti will point to
1177 the first code in the pair (for instance, GET_ITER followed by
1178 FOR_ITER is effectively a single opcode and f->f_lasti will point
1179 at to the beginning of the combined pair.)
1180 */
1181 next_instr = first_instr + f->f_lasti + 1;
1182 stack_pointer = f->f_stacktop;
1183 assert(stack_pointer != NULL);
1184 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001185 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (co->co_flags & CO_GENERATOR && !throwflag) {
1188 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1189 /* We were in an except handler when we left,
1190 restore the exception state which was put aside
1191 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001192 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001194 else
1195 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001197
Tim Peters5ca576e2001-06-18 22:08:13 +00001198#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001199 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001200#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001204 if (throwflag) /* support for generator.throw() */
1205 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001206
Victor Stinnerace47d72013-07-18 01:41:08 +02001207#ifdef Py_DEBUG
1208 /* PyEval_EvalFrameEx() must not be called with an exception set,
1209 because it may clear it (directly or indirectly) and so the
1210 caller looses its exception */
1211 assert(!PyErr_Occurred());
1212#endif
1213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001215#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (inst1 == 0) {
1217 /* Almost surely, the opcode executed a break
1218 or a continue, preventing inst1 from being set
1219 on the way out of the loop.
1220 */
1221 READ_TIMESTAMP(inst1);
1222 loop1 = inst1;
1223 }
1224 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1225 intr0, intr1);
1226 ticked = 0;
1227 inst1 = 0;
1228 intr0 = 0;
1229 intr1 = 0;
1230 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001231#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1233 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001234 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 /* Do periodic things. Doing this every time through
1237 the loop would add too much overhead, so we do it
1238 only every Nth instruction. We also do it if
1239 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1240 event needs attention (e.g. a signal handler or
1241 async I/O handler); see Py_AddPendingCall() and
1242 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1245 if (*next_instr == SETUP_FINALLY) {
1246 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001247 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 goto fast_next_opcode;
1249 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001250#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001254 if (Py_MakePendingCalls() < 0)
1255 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001257#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001258 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 /* Give another thread a chance */
1260 if (PyThreadState_Swap(NULL) != tstate)
1261 Py_FatalError("ceval: tstate mix-up");
1262 drop_gil(tstate);
1263
1264 /* Other threads may run now */
1265
1266 take_gil(tstate);
1267 if (PyThreadState_Swap(tstate) != NULL)
1268 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001270#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 /* Check for asynchronous exceptions. */
1272 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001273 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 tstate->async_exc = NULL;
1275 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001276 PyErr_SetNone(exc);
1277 Py_DECREF(exc);
1278 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 }
1280 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 fast_next_opcode:
1283 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001288 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* see maybe_call_line_trace
1291 for expository comments */
1292 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 err = maybe_call_line_trace(tstate->c_tracefunc,
1295 tstate->c_traceobj,
1296 f, &instr_lb, &instr_ub,
1297 &instr_prev);
1298 /* Reload possibly changed frame fields */
1299 JUMPTO(f->f_lasti);
1300 if (f->f_stacktop != NULL) {
1301 stack_pointer = f->f_stacktop;
1302 f->f_stacktop = NULL;
1303 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001306 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 opcode = NEXTOP();
1312 oparg = 0; /* allows oparg to be stored in a register because
1313 it doesn't have to be remembered across a full loop */
1314 if (HAS_ARG(opcode))
1315 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001316 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001317#ifdef DYNAMIC_EXECUTION_PROFILE
1318#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 dxpairs[lastopcode][opcode]++;
1320 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001321#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001323#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001324
Guido van Rossum96a42c81992-01-12 02:29:51 +00001325#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 if (lltrace) {
1329 if (HAS_ARG(opcode)) {
1330 printf("%d: %d, %d\n",
1331 f->f_lasti, opcode, oparg);
1332 }
1333 else {
1334 printf("%d: %d\n",
1335 f->f_lasti, opcode);
1336 }
1337 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001338#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 /* Main switch on opcode */
1341 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 /* BEWARE!
1346 It is essential that any operation that fails sets either
1347 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1348 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 TARGET(NOP)
1351 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001352
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 TARGET(LOAD_FAST) {
1354 PyObject *value = GETLOCAL(oparg);
1355 if (value == NULL) {
1356 format_exc_check_arg(PyExc_UnboundLocalError,
1357 UNBOUNDLOCAL_ERROR_MSG,
1358 PyTuple_GetItem(co->co_varnames, oparg));
1359 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001361 Py_INCREF(value);
1362 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 }
1365
1366 TARGET(LOAD_CONST) {
1367 PyObject *value = GETITEM(consts, oparg);
1368 Py_INCREF(value);
1369 PUSH(value);
1370 FAST_DISPATCH();
1371 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 TARGET(STORE_FAST) {
1375 PyObject *value = POP();
1376 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001379
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 TARGET(POP_TOP) {
1381 PyObject *value = POP();
1382 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001385
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 TARGET(ROT_TWO) {
1387 PyObject *top = TOP();
1388 PyObject *second = SECOND();
1389 SET_TOP(second);
1390 SET_SECOND(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(ROT_THREE) {
1395 PyObject *top = TOP();
1396 PyObject *second = SECOND();
1397 PyObject *third = THIRD();
1398 SET_TOP(second);
1399 SET_SECOND(third);
1400 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 TARGET(DUP_TOP) {
1405 PyObject *top = TOP();
1406 Py_INCREF(top);
1407 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 TARGET(DUP_TOP_TWO) {
1412 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001413 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001414 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001415 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001416 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001417 SET_TOP(top);
1418 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001419 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001421
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001422 TARGET(UNARY_POSITIVE) {
1423 PyObject *value = TOP();
1424 PyObject *res = PyNumber_Positive(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_NEGATIVE) {
1433 PyObject *value = TOP();
1434 PyObject *res = PyNumber_Negative(value);
1435 Py_DECREF(value);
1436 SET_TOP(res);
1437 if (res == NULL)
1438 goto error;
1439 DISPATCH();
1440 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001441
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 TARGET(UNARY_NOT) {
1443 PyObject *value = TOP();
1444 int err = PyObject_IsTrue(value);
1445 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 if (err == 0) {
1447 Py_INCREF(Py_True);
1448 SET_TOP(Py_True);
1449 DISPATCH();
1450 }
1451 else if (err > 0) {
1452 Py_INCREF(Py_False);
1453 SET_TOP(Py_False);
1454 err = 0;
1455 DISPATCH();
1456 }
1457 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 goto error;
1459 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 TARGET(UNARY_INVERT) {
1462 PyObject *value = TOP();
1463 PyObject *res = PyNumber_Invert(value);
1464 Py_DECREF(value);
1465 SET_TOP(res);
1466 if (res == NULL)
1467 goto error;
1468 DISPATCH();
1469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 TARGET(BINARY_POWER) {
1472 PyObject *exp = POP();
1473 PyObject *base = TOP();
1474 PyObject *res = PyNumber_Power(base, exp, Py_None);
1475 Py_DECREF(base);
1476 Py_DECREF(exp);
1477 SET_TOP(res);
1478 if (res == NULL)
1479 goto error;
1480 DISPATCH();
1481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 TARGET(BINARY_MULTIPLY) {
1484 PyObject *right = POP();
1485 PyObject *left = TOP();
1486 PyObject *res = PyNumber_Multiply(left, right);
1487 Py_DECREF(left);
1488 Py_DECREF(right);
1489 SET_TOP(res);
1490 if (res == NULL)
1491 goto error;
1492 DISPATCH();
1493 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001494
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 TARGET(BINARY_TRUE_DIVIDE) {
1496 PyObject *divisor = POP();
1497 PyObject *dividend = TOP();
1498 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1499 Py_DECREF(dividend);
1500 Py_DECREF(divisor);
1501 SET_TOP(quotient);
1502 if (quotient == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 TARGET(BINARY_FLOOR_DIVIDE) {
1508 PyObject *divisor = POP();
1509 PyObject *dividend = TOP();
1510 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1511 Py_DECREF(dividend);
1512 Py_DECREF(divisor);
1513 SET_TOP(quotient);
1514 if (quotient == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001518
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 TARGET(BINARY_MODULO) {
1520 PyObject *divisor = POP();
1521 PyObject *dividend = TOP();
1522 PyObject *res = PyUnicode_CheckExact(dividend) ?
1523 PyUnicode_Format(dividend, divisor) :
1524 PyNumber_Remainder(dividend, divisor);
1525 Py_DECREF(divisor);
1526 Py_DECREF(dividend);
1527 SET_TOP(res);
1528 if (res == NULL)
1529 goto error;
1530 DISPATCH();
1531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 TARGET(BINARY_ADD) {
1534 PyObject *right = POP();
1535 PyObject *left = TOP();
1536 PyObject *sum;
1537 if (PyUnicode_CheckExact(left) &&
1538 PyUnicode_CheckExact(right)) {
1539 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001540 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001541 }
1542 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 sum = PyNumber_Add(left, right);
1544 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001545 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001546 Py_DECREF(right);
1547 SET_TOP(sum);
1548 if (sum == NULL)
1549 goto error;
1550 DISPATCH();
1551 }
1552
1553 TARGET(BINARY_SUBTRACT) {
1554 PyObject *right = POP();
1555 PyObject *left = TOP();
1556 PyObject *diff = PyNumber_Subtract(left, right);
1557 Py_DECREF(right);
1558 Py_DECREF(left);
1559 SET_TOP(diff);
1560 if (diff == NULL)
1561 goto error;
1562 DISPATCH();
1563 }
1564
1565 TARGET(BINARY_SUBSCR) {
1566 PyObject *sub = POP();
1567 PyObject *container = TOP();
1568 PyObject *res = PyObject_GetItem(container, sub);
1569 Py_DECREF(container);
1570 Py_DECREF(sub);
1571 SET_TOP(res);
1572 if (res == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
1576
1577 TARGET(BINARY_LSHIFT) {
1578 PyObject *right = POP();
1579 PyObject *left = TOP();
1580 PyObject *res = PyNumber_Lshift(left, right);
1581 Py_DECREF(left);
1582 Py_DECREF(right);
1583 SET_TOP(res);
1584 if (res == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
1588
1589 TARGET(BINARY_RSHIFT) {
1590 PyObject *right = POP();
1591 PyObject *left = TOP();
1592 PyObject *res = PyNumber_Rshift(left, right);
1593 Py_DECREF(left);
1594 Py_DECREF(right);
1595 SET_TOP(res);
1596 if (res == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
1600
1601 TARGET(BINARY_AND) {
1602 PyObject *right = POP();
1603 PyObject *left = TOP();
1604 PyObject *res = PyNumber_And(left, right);
1605 Py_DECREF(left);
1606 Py_DECREF(right);
1607 SET_TOP(res);
1608 if (res == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
1612
1613 TARGET(BINARY_XOR) {
1614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_Xor(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(res);
1620 if (res == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
1624
1625 TARGET(BINARY_OR) {
1626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_Or(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
1636
1637 TARGET(LIST_APPEND) {
1638 PyObject *v = POP();
1639 PyObject *list = PEEK(oparg);
1640 int err;
1641 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 if (err != 0)
1644 goto error;
1645 PREDICT(JUMP_ABSOLUTE);
1646 DISPATCH();
1647 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001648
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 TARGET(SET_ADD) {
1650 PyObject *v = POP();
1651 PyObject *set = stack_pointer[-oparg];
1652 int err;
1653 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 if (err != 0)
1656 goto error;
1657 PREDICT(JUMP_ABSOLUTE);
1658 DISPATCH();
1659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 TARGET(INPLACE_POWER) {
1662 PyObject *exp = POP();
1663 PyObject *base = TOP();
1664 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1665 Py_DECREF(base);
1666 Py_DECREF(exp);
1667 SET_TOP(res);
1668 if (res == NULL)
1669 goto error;
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 TARGET(INPLACE_MULTIPLY) {
1674 PyObject *right = POP();
1675 PyObject *left = TOP();
1676 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1677 Py_DECREF(left);
1678 Py_DECREF(right);
1679 SET_TOP(res);
1680 if (res == NULL)
1681 goto error;
1682 DISPATCH();
1683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 TARGET(INPLACE_TRUE_DIVIDE) {
1686 PyObject *divisor = POP();
1687 PyObject *dividend = TOP();
1688 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1689 Py_DECREF(dividend);
1690 Py_DECREF(divisor);
1691 SET_TOP(quotient);
1692 if (quotient == NULL)
1693 goto error;
1694 DISPATCH();
1695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 TARGET(INPLACE_FLOOR_DIVIDE) {
1698 PyObject *divisor = POP();
1699 PyObject *dividend = TOP();
1700 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1701 Py_DECREF(dividend);
1702 Py_DECREF(divisor);
1703 SET_TOP(quotient);
1704 if (quotient == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 TARGET(INPLACE_MODULO) {
1710 PyObject *right = POP();
1711 PyObject *left = TOP();
1712 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1713 Py_DECREF(left);
1714 Py_DECREF(right);
1715 SET_TOP(mod);
1716 if (mod == NULL)
1717 goto error;
1718 DISPATCH();
1719 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001720
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 TARGET(INPLACE_ADD) {
1722 PyObject *right = POP();
1723 PyObject *left = TOP();
1724 PyObject *sum;
1725 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1726 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001727 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001728 }
1729 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001730 sum = PyNumber_InPlaceAdd(left, right);
1731 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001732 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 Py_DECREF(right);
1734 SET_TOP(sum);
1735 if (sum == NULL)
1736 goto error;
1737 DISPATCH();
1738 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 TARGET(INPLACE_SUBTRACT) {
1741 PyObject *right = POP();
1742 PyObject *left = TOP();
1743 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1744 Py_DECREF(left);
1745 Py_DECREF(right);
1746 SET_TOP(diff);
1747 if (diff == NULL)
1748 goto error;
1749 DISPATCH();
1750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 TARGET(INPLACE_LSHIFT) {
1753 PyObject *right = POP();
1754 PyObject *left = TOP();
1755 PyObject *res = PyNumber_InPlaceLshift(left, right);
1756 Py_DECREF(left);
1757 Py_DECREF(right);
1758 SET_TOP(res);
1759 if (res == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 TARGET(INPLACE_RSHIFT) {
1765 PyObject *right = POP();
1766 PyObject *left = TOP();
1767 PyObject *res = PyNumber_InPlaceRshift(left, right);
1768 Py_DECREF(left);
1769 Py_DECREF(right);
1770 SET_TOP(res);
1771 if (res == NULL)
1772 goto error;
1773 DISPATCH();
1774 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 TARGET(INPLACE_AND) {
1777 PyObject *right = POP();
1778 PyObject *left = TOP();
1779 PyObject *res = PyNumber_InPlaceAnd(left, right);
1780 Py_DECREF(left);
1781 Py_DECREF(right);
1782 SET_TOP(res);
1783 if (res == NULL)
1784 goto error;
1785 DISPATCH();
1786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 TARGET(INPLACE_XOR) {
1789 PyObject *right = POP();
1790 PyObject *left = TOP();
1791 PyObject *res = PyNumber_InPlaceXor(left, right);
1792 Py_DECREF(left);
1793 Py_DECREF(right);
1794 SET_TOP(res);
1795 if (res == NULL)
1796 goto error;
1797 DISPATCH();
1798 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 TARGET(INPLACE_OR) {
1801 PyObject *right = POP();
1802 PyObject *left = TOP();
1803 PyObject *res = PyNumber_InPlaceOr(left, right);
1804 Py_DECREF(left);
1805 Py_DECREF(right);
1806 SET_TOP(res);
1807 if (res == NULL)
1808 goto error;
1809 DISPATCH();
1810 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001811
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 TARGET(STORE_SUBSCR) {
1813 PyObject *sub = TOP();
1814 PyObject *container = SECOND();
1815 PyObject *v = THIRD();
1816 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 STACKADJ(-3);
1818 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001819 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001821 Py_DECREF(container);
1822 Py_DECREF(sub);
1823 if (err != 0)
1824 goto error;
1825 DISPATCH();
1826 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 TARGET(DELETE_SUBSCR) {
1829 PyObject *sub = TOP();
1830 PyObject *container = SECOND();
1831 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 STACKADJ(-2);
1833 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 err = PyObject_DelItem(container, sub);
1835 Py_DECREF(container);
1836 Py_DECREF(sub);
1837 if (err != 0)
1838 goto error;
1839 DISPATCH();
1840 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001841
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001842 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001843 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001844 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001845 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001846 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 PyErr_SetString(PyExc_RuntimeError,
1849 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001850 Py_DECREF(value);
1851 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001853 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 Py_DECREF(value);
1855 if (res == NULL)
1856 goto error;
1857 Py_DECREF(res);
1858 DISPATCH();
1859 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001860
Thomas Wouters434d0822000-08-24 20:11:32 +00001861#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001863#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 TARGET(RAISE_VARARGS) {
1865 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 switch (oparg) {
1867 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 if (do_raise(exc, cause)) {
1873 why = WHY_EXCEPTION;
1874 goto fast_block_end;
1875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 break;
1877 default:
1878 PyErr_SetString(PyExc_SystemError,
1879 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 break;
1881 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 goto error;
1883 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001884
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 retval = POP();
1887 why = WHY_RETURN;
1888 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001890
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 TARGET(YIELD_FROM) {
1892 PyObject *v = POP();
1893 PyObject *reciever = TOP();
1894 int err;
1895 if (PyGen_CheckExact(reciever)) {
1896 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001897 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001898 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 if (v == Py_None)
1900 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001901 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 retval = _PyObject_CallMethodId(reciever, &PyId_send, "O", v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 Py_DECREF(v);
1905 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001906 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001907 if (tstate->c_tracefunc != NULL
1908 && PyErr_ExceptionMatches(PyExc_StopIteration))
1909 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001910 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 if (err < 0)
1912 goto error;
1913 Py_DECREF(reciever);
1914 SET_TOP(val);
1915 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001916 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001917 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001918 f->f_stacktop = stack_pointer;
1919 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001920 /* and repeat... */
1921 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001922 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001924
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 retval = POP();
1927 f->f_stacktop = stack_pointer;
1928 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001931
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 TARGET(POP_EXCEPT) {
1933 PyTryBlock *b = PyFrame_BlockPop(f);
1934 if (b->b_type != EXCEPT_HANDLER) {
1935 PyErr_SetString(PyExc_SystemError,
1936 "popped block is not an except handler");
1937 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001939 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001942
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 TARGET(POP_BLOCK) {
1944 PyTryBlock *b = PyFrame_BlockPop(f);
1945 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 TARGET(END_FINALLY) {
1951 PyObject *status = POP();
1952 if (PyLong_Check(status)) {
1953 why = (enum why_code) PyLong_AS_LONG(status);
1954 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 if (why == WHY_RETURN ||
1956 why == WHY_CONTINUE)
1957 retval = POP();
1958 if (why == WHY_SILENCED) {
1959 /* An exception was silenced by 'with', we must
1960 manually unwind the EXCEPT_HANDLER block which was
1961 created when the exception was caught, otherwise
1962 the stack will be in an inconsistent state. */
1963 PyTryBlock *b = PyFrame_BlockPop(f);
1964 assert(b->b_type == EXCEPT_HANDLER);
1965 UNWIND_EXCEPT_HANDLER(b);
1966 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 Py_DECREF(status);
1968 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 Py_DECREF(status);
1971 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 else if (PyExceptionClass_Check(status)) {
1974 PyObject *exc = POP();
1975 PyObject *tb = POP();
1976 PyErr_Restore(status, exc, tb);
1977 why = WHY_EXCEPTION;
1978 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001980 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 PyErr_SetString(PyExc_SystemError,
1982 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 Py_DECREF(status);
1984 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 Py_DECREF(status);
1987 DISPATCH();
1988 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001989
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001991 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001992
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001994 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1996 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001997 PyErr_SetString(PyExc_NameError,
1998 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001999 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002000 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002002 }
2003 else {
2004 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2005 if (build_class_str == NULL)
2006 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2008 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002009 if (PyErr_ExceptionMatches(PyExc_KeyError))
2010 PyErr_SetString(PyExc_NameError,
2011 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002013 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002016 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002017 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002018
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 TARGET(STORE_NAME) {
2020 PyObject *name = GETITEM(names, oparg);
2021 PyObject *v = POP();
2022 PyObject *ns = f->f_locals;
2023 int err;
2024 if (ns == NULL) {
2025 PyErr_Format(PyExc_SystemError,
2026 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 if (PyDict_CheckExact(ns))
2031 err = PyDict_SetItem(ns, name, v);
2032 else
2033 err = PyObject_SetItem(ns, name, v);
2034 Py_DECREF(v);
2035 if (err != 0)
2036 goto error;
2037 DISPATCH();
2038 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002039
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 TARGET(DELETE_NAME) {
2041 PyObject *name = GETITEM(names, oparg);
2042 PyObject *ns = f->f_locals;
2043 int err;
2044 if (ns == NULL) {
2045 PyErr_Format(PyExc_SystemError,
2046 "no locals when deleting %R", name);
2047 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 err = PyObject_DelItem(ns, name);
2050 if (err != 0) {
2051 format_exc_check_arg(PyExc_NameError,
2052 NAME_ERROR_MSG,
2053 name);
2054 goto error;
2055 }
2056 DISPATCH();
2057 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002059 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002060 TARGET(UNPACK_SEQUENCE) {
2061 PyObject *seq = POP(), *item, **items;
2062 if (PyTuple_CheckExact(seq) &&
2063 PyTuple_GET_SIZE(seq) == oparg) {
2064 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 item = items[oparg];
2067 Py_INCREF(item);
2068 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 } else if (PyList_CheckExact(seq) &&
2071 PyList_GET_SIZE(seq) == oparg) {
2072 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 item = items[oparg];
2075 Py_INCREF(item);
2076 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002079 stack_pointer + oparg)) {
2080 STACKADJ(oparg);
2081 } else {
2082 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 Py_DECREF(seq);
2084 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002087 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002089
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 TARGET(UNPACK_EX) {
2091 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2092 PyObject *seq = POP();
2093
2094 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2095 stack_pointer + totalargs)) {
2096 stack_pointer += totalargs;
2097 } else {
2098 Py_DECREF(seq);
2099 goto error;
2100 }
2101 Py_DECREF(seq);
2102 DISPATCH();
2103 }
2104
2105 TARGET(STORE_ATTR) {
2106 PyObject *name = GETITEM(names, oparg);
2107 PyObject *owner = TOP();
2108 PyObject *v = SECOND();
2109 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 Py_DECREF(owner);
2114 if (err != 0)
2115 goto error;
2116 DISPATCH();
2117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002118
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 TARGET(DELETE_ATTR) {
2120 PyObject *name = GETITEM(names, oparg);
2121 PyObject *owner = POP();
2122 int err;
2123 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2124 Py_DECREF(owner);
2125 if (err != 0)
2126 goto error;
2127 DISPATCH();
2128 }
2129
2130 TARGET(STORE_GLOBAL) {
2131 PyObject *name = GETITEM(names, oparg);
2132 PyObject *v = POP();
2133 int err;
2134 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 if (err != 0)
2137 goto error;
2138 DISPATCH();
2139 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002140
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 TARGET(DELETE_GLOBAL) {
2142 PyObject *name = GETITEM(names, oparg);
2143 int err;
2144 err = PyDict_DelItem(f->f_globals, name);
2145 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002147 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002149 }
2150 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 TARGET(LOAD_NAME) {
2154 PyObject *name = GETITEM(names, oparg);
2155 PyObject *locals = f->f_locals;
2156 PyObject *v;
2157 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 "no locals when loading %R", name);
2160 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 if (PyDict_CheckExact(locals)) {
2163 v = PyDict_GetItem(locals, name);
2164 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 }
2166 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002167 v = PyObject_GetItem(locals, name);
Antoine Pitrou1cfa0ba2013-10-07 20:40:59 +02002168 if (v == NULL && _PyErr_OCCURRED()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002169 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2170 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 PyErr_Clear();
2172 }
2173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 if (v == NULL) {
2175 v = PyDict_GetItem(f->f_globals, name);
2176 Py_XINCREF(v);
2177 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002178 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 v = PyDict_GetItem(f->f_builtins, name);
2180 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 format_exc_check_arg(
2182 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 NAME_ERROR_MSG, name);
2184 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002187 }
2188 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 v = PyObject_GetItem(f->f_builtins, name);
2190 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002191 if (PyErr_ExceptionMatches(PyExc_KeyError))
2192 format_exc_check_arg(
2193 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 NAME_ERROR_MSG, name);
2195 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002196 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002197 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002204 TARGET(LOAD_GLOBAL) {
2205 PyObject *name = GETITEM(names, oparg);
2206 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002207 if (PyDict_CheckExact(f->f_globals)
2208 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002210 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 name);
2212 if (v == NULL) {
Antoine Pitrou59c900d2013-10-07 20:38:51 +02002213 if (!_PyErr_OCCURRED())
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002214 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002215 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002220 else {
2221 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 v = PyObject_GetItem(f->f_globals, name);
2223 if (v == NULL) {
2224 v = PyObject_GetItem(f->f_builtins, name);
2225 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002226 if (PyErr_ExceptionMatches(PyExc_KeyError))
2227 format_exc_check_arg(
2228 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002229 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002231 }
2232 }
2233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002237
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 TARGET(DELETE_FAST) {
2239 PyObject *v = GETLOCAL(oparg);
2240 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 SETLOCAL(oparg, NULL);
2242 DISPATCH();
2243 }
2244 format_exc_check_arg(
2245 PyExc_UnboundLocalError,
2246 UNBOUNDLOCAL_ERROR_MSG,
2247 PyTuple_GetItem(co->co_varnames, oparg)
2248 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 goto error;
2250 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002251
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 TARGET(DELETE_DEREF) {
2253 PyObject *cell = freevars[oparg];
2254 if (PyCell_GET(cell) != NULL) {
2255 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002256 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002257 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002258 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 goto error;
2260 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002261
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 TARGET(LOAD_CLOSURE) {
2263 PyObject *cell = freevars[oparg];
2264 Py_INCREF(cell);
2265 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002268
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002269 TARGET(LOAD_CLASSDEREF) {
2270 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002271 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002272 assert(locals);
2273 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2274 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2275 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2276 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2277 if (PyDict_CheckExact(locals)) {
2278 value = PyDict_GetItem(locals, name);
2279 Py_XINCREF(value);
2280 }
2281 else {
2282 value = PyObject_GetItem(locals, name);
2283 if (value == NULL && PyErr_Occurred()) {
2284 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2285 goto error;
2286 PyErr_Clear();
2287 }
2288 }
2289 if (!value) {
2290 PyObject *cell = freevars[oparg];
2291 value = PyCell_GET(cell);
2292 if (value == NULL) {
2293 format_exc_unbound(co, oparg);
2294 goto error;
2295 }
2296 Py_INCREF(value);
2297 }
2298 PUSH(value);
2299 DISPATCH();
2300 }
2301
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 TARGET(LOAD_DEREF) {
2303 PyObject *cell = freevars[oparg];
2304 PyObject *value = PyCell_GET(cell);
2305 if (value == NULL) {
2306 format_exc_unbound(co, oparg);
2307 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002309 Py_INCREF(value);
2310 PUSH(value);
2311 DISPATCH();
2312 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 TARGET(STORE_DEREF) {
2315 PyObject *v = POP();
2316 PyObject *cell = freevars[oparg];
2317 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 DISPATCH();
2320 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 TARGET(BUILD_TUPLE) {
2323 PyObject *tup = PyTuple_New(oparg);
2324 if (tup == NULL)
2325 goto error;
2326 while (--oparg >= 0) {
2327 PyObject *item = POP();
2328 PyTuple_SET_ITEM(tup, oparg, item);
2329 }
2330 PUSH(tup);
2331 DISPATCH();
2332 }
2333
2334 TARGET(BUILD_LIST) {
2335 PyObject *list = PyList_New(oparg);
2336 if (list == NULL)
2337 goto error;
2338 while (--oparg >= 0) {
2339 PyObject *item = POP();
2340 PyList_SET_ITEM(list, oparg, item);
2341 }
2342 PUSH(list);
2343 DISPATCH();
2344 }
2345
2346 TARGET(BUILD_SET) {
2347 PyObject *set = PySet_New(NULL);
2348 int err = 0;
2349 if (set == NULL)
2350 goto error;
2351 while (--oparg >= 0) {
2352 PyObject *item = POP();
2353 if (err == 0)
2354 err = PySet_Add(set, item);
2355 Py_DECREF(item);
2356 }
2357 if (err != 0) {
2358 Py_DECREF(set);
2359 goto error;
2360 }
2361 PUSH(set);
2362 DISPATCH();
2363 }
2364
2365 TARGET(BUILD_MAP) {
2366 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2367 if (map == NULL)
2368 goto error;
2369 PUSH(map);
2370 DISPATCH();
2371 }
2372
2373 TARGET(STORE_MAP) {
2374 PyObject *key = TOP();
2375 PyObject *value = SECOND();
2376 PyObject *map = THIRD();
2377 int err;
2378 STACKADJ(-2);
2379 assert(PyDict_CheckExact(map));
2380 err = PyDict_SetItem(map, key, value);
2381 Py_DECREF(value);
2382 Py_DECREF(key);
2383 if (err != 0)
2384 goto error;
2385 DISPATCH();
2386 }
2387
2388 TARGET(MAP_ADD) {
2389 PyObject *key = TOP();
2390 PyObject *value = SECOND();
2391 PyObject *map;
2392 int err;
2393 STACKADJ(-2);
2394 map = stack_pointer[-oparg]; /* dict */
2395 assert(PyDict_CheckExact(map));
2396 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2397 Py_DECREF(value);
2398 Py_DECREF(key);
2399 if (err != 0)
2400 goto error;
2401 PREDICT(JUMP_ABSOLUTE);
2402 DISPATCH();
2403 }
2404
2405 TARGET(LOAD_ATTR) {
2406 PyObject *name = GETITEM(names, oparg);
2407 PyObject *owner = TOP();
2408 PyObject *res = PyObject_GetAttr(owner, name);
2409 Py_DECREF(owner);
2410 SET_TOP(res);
2411 if (res == NULL)
2412 goto error;
2413 DISPATCH();
2414 }
2415
2416 TARGET(COMPARE_OP) {
2417 PyObject *right = POP();
2418 PyObject *left = TOP();
2419 PyObject *res = cmp_outcome(oparg, left, right);
2420 Py_DECREF(left);
2421 Py_DECREF(right);
2422 SET_TOP(res);
2423 if (res == NULL)
2424 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002425 PREDICT(POP_JUMP_IF_FALSE);
2426 PREDICT(POP_JUMP_IF_TRUE);
2427 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002428 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002429
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 TARGET(IMPORT_NAME) {
2431 _Py_IDENTIFIER(__import__);
2432 PyObject *name = GETITEM(names, oparg);
2433 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002434 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 if (func == NULL) {
2436 PyErr_SetString(PyExc_ImportError,
2437 "__import__ not found");
2438 goto error;
2439 }
2440 Py_INCREF(func);
2441 from = POP();
2442 level = TOP();
2443 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2444 args = PyTuple_Pack(5,
2445 name,
2446 f->f_globals,
2447 f->f_locals == NULL ?
2448 Py_None : f->f_locals,
2449 from,
2450 level);
2451 else
2452 args = PyTuple_Pack(4,
2453 name,
2454 f->f_globals,
2455 f->f_locals == NULL ?
2456 Py_None : f->f_locals,
2457 from);
2458 Py_DECREF(level);
2459 Py_DECREF(from);
2460 if (args == NULL) {
2461 Py_DECREF(func);
2462 STACKADJ(-1);
2463 goto error;
2464 }
2465 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002466 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 READ_TIMESTAMP(intr1);
2468 Py_DECREF(args);
2469 Py_DECREF(func);
2470 SET_TOP(res);
2471 if (res == NULL)
2472 goto error;
2473 DISPATCH();
2474 }
2475
2476 TARGET(IMPORT_STAR) {
2477 PyObject *from = POP(), *locals;
2478 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002479 if (PyFrame_FastToLocalsWithError(f) < 0)
2480 goto error;
2481
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 locals = f->f_locals;
2483 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 PyErr_SetString(PyExc_SystemError,
2485 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 }
2488 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 READ_TIMESTAMP(intr1);
2491 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 Py_DECREF(from);
2493 if (err != 0)
2494 goto error;
2495 DISPATCH();
2496 }
Guido van Rossum25831651993-05-19 14:50:45 +00002497
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 TARGET(IMPORT_FROM) {
2499 PyObject *name = GETITEM(names, oparg);
2500 PyObject *from = TOP();
2501 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 PUSH(res);
2506 if (res == NULL)
2507 goto error;
2508 DISPATCH();
2509 }
Thomas Wouters52152252000-08-17 22:55:00 +00002510
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002512 JUMPBY(oparg);
2513 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 TARGET(POP_JUMP_IF_FALSE) {
2518 PyObject *cond = POP();
2519 int err;
2520 if (cond == Py_True) {
2521 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 FAST_DISPATCH();
2523 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 if (cond == Py_False) {
2525 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 JUMPTO(oparg);
2527 FAST_DISPATCH();
2528 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002529 err = PyObject_IsTrue(cond);
2530 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 if (err > 0)
2532 err = 0;
2533 else if (err == 0)
2534 JUMPTO(oparg);
2535 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002538 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 TARGET(POP_JUMP_IF_TRUE) {
2542 PyObject *cond = POP();
2543 int err;
2544 if (cond == Py_False) {
2545 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 FAST_DISPATCH();
2547 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 if (cond == Py_True) {
2549 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 JUMPTO(oparg);
2551 FAST_DISPATCH();
2552 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 err = PyObject_IsTrue(cond);
2554 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 if (err > 0) {
2556 err = 0;
2557 JUMPTO(oparg);
2558 }
2559 else if (err == 0)
2560 ;
2561 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002562 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002565
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002566 TARGET(JUMP_IF_FALSE_OR_POP) {
2567 PyObject *cond = TOP();
2568 int err;
2569 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 FAST_DISPATCH();
2573 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 JUMPTO(oparg);
2576 FAST_DISPATCH();
2577 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002578 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002579 if (err > 0) {
2580 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 err = 0;
2583 }
2584 else if (err == 0)
2585 JUMPTO(oparg);
2586 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002590
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 TARGET(JUMP_IF_TRUE_OR_POP) {
2592 PyObject *cond = TOP();
2593 int err;
2594 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 FAST_DISPATCH();
2598 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 JUMPTO(oparg);
2601 FAST_DISPATCH();
2602 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 if (err > 0) {
2605 err = 0;
2606 JUMPTO(oparg);
2607 }
2608 else if (err == 0) {
2609 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 }
2612 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002618 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002620#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 /* Enabling this path speeds-up all while and for-loops by bypassing
2622 the per-loop checks for signals. By default, this should be turned-off
2623 because it prevents detection of a control-break in tight loops like
2624 "while 1: pass". Compile with this option turned-on when you need
2625 the speed-up and do not need break checking inside tight loops (ones
2626 that contain only instructions ending with FAST_DISPATCH).
2627 */
2628 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002629#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002631#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002632 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002633
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002634 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002635 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002636 PyObject *iterable = TOP();
2637 PyObject *iter = PyObject_GetIter(iterable);
2638 Py_DECREF(iterable);
2639 SET_TOP(iter);
2640 if (iter == NULL)
2641 goto error;
2642 PREDICT(FOR_ITER);
2643 DISPATCH();
2644 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002647 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 PyObject *iter = TOP();
2650 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2651 if (next != NULL) {
2652 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 PREDICT(STORE_FAST);
2654 PREDICT(UNPACK_SEQUENCE);
2655 DISPATCH();
2656 }
2657 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2659 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002660 else if (tstate->c_tracefunc != NULL)
2661 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 PyErr_Clear();
2663 }
2664 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 STACKADJ(-1);
2666 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 JUMPBY(oparg);
2668 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002669 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002670
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002671 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 why = WHY_BREAK;
2673 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002675
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 if (retval == NULL)
2679 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 why = WHY_CONTINUE;
2681 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2685 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2686 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 /* NOTE: If you add any new block-setup opcodes that
2689 are not try/except/finally handlers, you may need
2690 to update the PyGen_NeedsFinalizing() function.
2691 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2694 STACK_LEVEL());
2695 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002697
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002698 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002699 _Py_IDENTIFIER(__exit__);
2700 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 PyObject *mgr = TOP();
2702 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2703 PyObject *res;
2704 if (exit == NULL)
2705 goto error;
2706 SET_TOP(exit);
2707 enter = special_lookup(mgr, &PyId___enter__);
2708 Py_DECREF(mgr);
2709 if (enter == NULL)
2710 goto error;
2711 res = PyObject_CallFunctionObjArgs(enter, NULL);
2712 Py_DECREF(enter);
2713 if (res == NULL)
2714 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002715 /* Setup the finally block before pushing the result
2716 of __enter__ on the stack. */
2717 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2718 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002719
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 DISPATCH();
2722 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002723
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 TARGET(WITH_CLEANUP) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002725 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 how/why we entered the finally clause:
2727 - TOP = None
2728 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2729 - TOP = WHY_*; no retval below it
2730 - (TOP, SECOND, THIRD) = exc_info()
2731 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2732 Below them is EXIT, the context.__exit__ bound method.
2733 In the last case, we must call
2734 EXIT(TOP, SECOND, THIRD)
2735 otherwise we must call
2736 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002737
Benjamin Peterson8f169482013-10-29 22:25:06 -04002738 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002740 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 In addition, if the stack represents an exception,
2744 *and* the function call returns a 'true' value, we
2745 push WHY_SILENCED onto the stack. END_FINALLY will
2746 then not re-raise the exception. (But non-local
2747 gotos should still be resumed.)
2748 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2752 int err;
2753 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 (void)POP();
2755 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 else if (PyLong_Check(exc)) {
2759 STACKADJ(-1);
2760 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 case WHY_RETURN:
2762 case WHY_CONTINUE:
2763 /* Retval in TOP. */
2764 exit_func = SECOND();
2765 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 break;
2768 default:
2769 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 break;
2772 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 }
2775 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 val = SECOND();
2779 tb = THIRD();
2780 tp2 = FOURTH();
2781 exc2 = PEEK(5);
2782 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002784 SET_VALUE(7, tb2);
2785 SET_VALUE(6, exc2);
2786 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2788 SET_FOURTH(NULL);
2789 /* We just shifted the stack down, so we have
2790 to tell the except handler block that the
2791 values are lower than it expects. */
2792 block = &f->f_blockstack[f->f_iblock - 1];
2793 assert(block->b_type == EXCEPT_HANDLER);
2794 block->b_level--;
2795 }
2796 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 if (res == NULL)
2800 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002801
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 if (exc != Py_None)
2803 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 else
2805 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 else if (err > 0) {
2811 err = 0;
2812 /* There was an exception and a True return */
2813 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2814 }
2815 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002818
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 TARGET(CALL_FUNCTION) {
2820 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 PCALL(PCALL_ALL);
2822 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002823#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002825#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002827#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 PUSH(res);
2830 if (res == NULL)
2831 goto error;
2832 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2836 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2837 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 int na = oparg & 0xff;
2840 int nk = (oparg>>8) & 0xff;
2841 int flags = (opcode - CALL_FUNCTION) & 3;
2842 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 PCALL(PCALL_ALL);
2845 if (flags & CALL_FLAG_VAR)
2846 n++;
2847 if (flags & CALL_FLAG_KW)
2848 n++;
2849 pfunc = stack_pointer - n - 1;
2850 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002853 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 PyObject *self = PyMethod_GET_SELF(func);
2855 Py_INCREF(self);
2856 func = PyMethod_GET_FUNCTION(func);
2857 Py_INCREF(func);
2858 Py_DECREF(*pfunc);
2859 *pfunc = self;
2860 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002861 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 } else
2863 Py_INCREF(func);
2864 sp = stack_pointer;
2865 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 READ_TIMESTAMP(intr1);
2868 stack_pointer = sp;
2869 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 PyObject *o = POP();
2873 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 PUSH(res);
2876 if (res == NULL)
2877 goto error;
2878 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2882 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 int posdefaults = oparg & 0xff;
2885 int kwdefaults = (oparg>>8) & 0xff;
2886 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002887
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 PyObject *qualname = POP(); /* qualname */
2889 PyObject *code = POP(); /* code object */
2890 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2891 Py_DECREF(code);
2892 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002893
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 if (func == NULL)
2895 goto error;
2896
2897 if (opcode == MAKE_CLOSURE) {
2898 PyObject *closure = POP();
2899 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 Py_DECREF(func);
2902 Py_DECREF(closure);
2903 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002907
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 PyObject *names = POP(); /* names of args with annotations */
2911 PyObject *anns = PyDict_New();
2912 if (anns == NULL) {
2913 Py_DECREF(func);
2914 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 assert(num_annotations == name_ix+1);
2918 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 PyObject *name, *value;
2920 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 name = PyTuple_GET_ITEM(names, name_ix);
2923 value = POP();
2924 err = PyDict_SetItem(anns, name, value);
2925 Py_DECREF(value);
2926 if (err != 0) {
2927 Py_DECREF(anns);
2928 Py_DECREF(func);
2929 goto error;
2930 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002932
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 /* Can't happen unless
2935 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 Py_DECREF(anns);
2937 Py_DECREF(func);
2938 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 Py_DECREF(anns);
2941 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 if (kwdefaults > 0) {
2946 PyObject *defs = PyDict_New();
2947 if (defs == NULL) {
2948 Py_DECREF(func);
2949 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 }
2951 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 PyObject *v = POP(); /* default value */
2953 PyObject *key = POP(); /* kw only arg name */
2954 int err = PyDict_SetItem(defs, key, v);
2955 Py_DECREF(v);
2956 Py_DECREF(key);
2957 if (err != 0) {
2958 Py_DECREF(defs);
2959 Py_DECREF(func);
2960 goto error;
2961 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 /* Can't happen unless
2965 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 Py_DECREF(func);
2967 Py_DECREF(defs);
2968 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05002972 if (posdefaults > 0) {
2973 PyObject *defs = PyTuple_New(posdefaults);
2974 if (defs == NULL) {
2975 Py_DECREF(func);
2976 goto error;
2977 }
2978 while (--posdefaults >= 0)
2979 PyTuple_SET_ITEM(defs, posdefaults, POP());
2980 if (PyFunction_SetDefaults(func, defs) != 0) {
2981 /* Can't happen unless
2982 PyFunction_SetDefaults changes. */
2983 Py_DECREF(defs);
2984 Py_DECREF(func);
2985 goto error;
2986 }
2987 Py_DECREF(defs);
2988 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002989 PUSH(func);
2990 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002992
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 TARGET(BUILD_SLICE) {
2994 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 step = NULL;
2999 stop = POP();
3000 start = TOP();
3001 slice = PySlice_New(start, stop, step);
3002 Py_DECREF(start);
3003 Py_DECREF(stop);
3004 Py_XDECREF(step);
3005 SET_TOP(slice);
3006 if (slice == NULL)
3007 goto error;
3008 DISPATCH();
3009 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003010
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003012 opcode = NEXTOP();
3013 oparg = oparg<<16 | NEXTARG();
3014 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003016
Antoine Pitrou042b1282010-08-13 21:15:58 +00003017#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003019#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 default:
3021 fprintf(stderr,
3022 "XXX lineno: %d, opcode: %d\n",
3023 PyFrame_GetLineNumber(f),
3024 opcode);
3025 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003027
3028#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003030#endif
3031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003033
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 /* This should never be reached. Every opcode should end with DISPATCH()
3035 or goto error. */
3036 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003037
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003040
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041 assert(why == WHY_NOT);
3042 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003043
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003045#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 if (!PyErr_Occurred())
3047 PyErr_SetString(PyExc_SystemError,
3048 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003049#else
3050 assert(PyErr_Occurred());
3051#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003052
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 /* Log traceback info. */
3054 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003055
Benjamin Peterson51f46162013-01-23 08:38:47 -05003056 if (tstate->c_tracefunc != NULL)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003058
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003059fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 assert(why != WHY_NOT);
3061
3062 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 while (why != WHY_NOT && f->f_iblock > 0) {
3064 /* Peek at the current block. */
3065 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 assert(why != WHY_YIELD);
3068 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3069 why = WHY_NOT;
3070 JUMPTO(PyLong_AS_LONG(retval));
3071 Py_DECREF(retval);
3072 break;
3073 }
3074 /* Now we have to pop the block. */
3075 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 if (b->b_type == EXCEPT_HANDLER) {
3078 UNWIND_EXCEPT_HANDLER(b);
3079 continue;
3080 }
3081 UNWIND_BLOCK(b);
3082 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3083 why = WHY_NOT;
3084 JUMPTO(b->b_handler);
3085 break;
3086 }
3087 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3088 || b->b_type == SETUP_FINALLY)) {
3089 PyObject *exc, *val, *tb;
3090 int handler = b->b_handler;
3091 /* Beware, this invalidates all b->b_* fields */
3092 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3093 PUSH(tstate->exc_traceback);
3094 PUSH(tstate->exc_value);
3095 if (tstate->exc_type != NULL) {
3096 PUSH(tstate->exc_type);
3097 }
3098 else {
3099 Py_INCREF(Py_None);
3100 PUSH(Py_None);
3101 }
3102 PyErr_Fetch(&exc, &val, &tb);
3103 /* Make the raw exception data
3104 available to the handler,
3105 so a program can emulate the
3106 Python main loop. */
3107 PyErr_NormalizeException(
3108 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003109 if (tb != NULL)
3110 PyException_SetTraceback(val, tb);
3111 else
3112 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 Py_INCREF(exc);
3114 tstate->exc_type = exc;
3115 Py_INCREF(val);
3116 tstate->exc_value = val;
3117 tstate->exc_traceback = tb;
3118 if (tb == NULL)
3119 tb = Py_None;
3120 Py_INCREF(tb);
3121 PUSH(tb);
3122 PUSH(val);
3123 PUSH(exc);
3124 why = WHY_NOT;
3125 JUMPTO(handler);
3126 break;
3127 }
3128 if (b->b_type == SETUP_FINALLY) {
3129 if (why & (WHY_RETURN | WHY_CONTINUE))
3130 PUSH(retval);
3131 PUSH(PyLong_FromLong((long)why));
3132 why = WHY_NOT;
3133 JUMPTO(b->b_handler);
3134 break;
3135 }
3136 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 if (why != WHY_NOT)
3141 break;
3142 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003143
Victor Stinnerace47d72013-07-18 01:41:08 +02003144 assert(!PyErr_Occurred());
3145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003146 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 assert(why != WHY_YIELD);
3149 /* Pop remaining stack entries. */
3150 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003151 PyObject *o = POP();
3152 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003155 if (why != WHY_RETURN)
3156 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003157
Victor Stinnerace47d72013-07-18 01:41:08 +02003158 assert((retval != NULL && !PyErr_Occurred())
3159 || (retval == NULL && PyErr_Occurred()));
3160
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003161fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003162 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3163 /* The purpose of this block is to put aside the generator's exception
3164 state and restore that of the calling frame. If the current
3165 exception state is from the caller, we clear the exception values
3166 on the generator frame, so they are not swapped back in latter. The
3167 origin of the current exception state is determined by checking for
3168 except handler blocks, which we must be in iff a new exception
3169 state came into existence in this frame. (An uncaught exception
3170 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3171 int i;
3172 for (i = 0; i < f->f_iblock; i++)
3173 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3174 break;
3175 if (i == f->f_iblock)
3176 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003177 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003178 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003179 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003180 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003183 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 if (why == WHY_RETURN || why == WHY_YIELD) {
3185 if (call_trace(tstate->c_tracefunc,
3186 tstate->c_traceobj, f,
3187 PyTrace_RETURN, retval)) {
3188 Py_XDECREF(retval);
3189 retval = NULL;
3190 why = WHY_EXCEPTION;
3191 }
3192 }
3193 else if (why == WHY_EXCEPTION) {
3194 call_trace_protected(tstate->c_tracefunc,
3195 tstate->c_traceobj, f,
3196 PyTrace_RETURN, NULL);
3197 }
3198 }
3199 if (tstate->c_profilefunc) {
3200 if (why == WHY_EXCEPTION)
3201 call_trace_protected(tstate->c_profilefunc,
3202 tstate->c_profileobj, f,
3203 PyTrace_RETURN, NULL);
3204 else if (call_trace(tstate->c_profilefunc,
3205 tstate->c_profileobj, f,
3206 PyTrace_RETURN, retval)) {
3207 Py_XDECREF(retval);
3208 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003209 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 }
3211 }
3212 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003215exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003217 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003221}
3222
Benjamin Petersonb204a422011-06-05 22:04:07 -05003223static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003224format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3225{
3226 int err;
3227 Py_ssize_t len = PyList_GET_SIZE(names);
3228 PyObject *name_str, *comma, *tail, *tmp;
3229
3230 assert(PyList_CheckExact(names));
3231 assert(len >= 1);
3232 /* Deal with the joys of natural language. */
3233 switch (len) {
3234 case 1:
3235 name_str = PyList_GET_ITEM(names, 0);
3236 Py_INCREF(name_str);
3237 break;
3238 case 2:
3239 name_str = PyUnicode_FromFormat("%U and %U",
3240 PyList_GET_ITEM(names, len - 2),
3241 PyList_GET_ITEM(names, len - 1));
3242 break;
3243 default:
3244 tail = PyUnicode_FromFormat(", %U, and %U",
3245 PyList_GET_ITEM(names, len - 2),
3246 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003247 if (tail == NULL)
3248 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003249 /* Chop off the last two objects in the list. This shouldn't actually
3250 fail, but we can't be too careful. */
3251 err = PyList_SetSlice(names, len - 2, len, NULL);
3252 if (err == -1) {
3253 Py_DECREF(tail);
3254 return;
3255 }
3256 /* Stitch everything up into a nice comma-separated list. */
3257 comma = PyUnicode_FromString(", ");
3258 if (comma == NULL) {
3259 Py_DECREF(tail);
3260 return;
3261 }
3262 tmp = PyUnicode_Join(comma, names);
3263 Py_DECREF(comma);
3264 if (tmp == NULL) {
3265 Py_DECREF(tail);
3266 return;
3267 }
3268 name_str = PyUnicode_Concat(tmp, tail);
3269 Py_DECREF(tmp);
3270 Py_DECREF(tail);
3271 break;
3272 }
3273 if (name_str == NULL)
3274 return;
3275 PyErr_Format(PyExc_TypeError,
3276 "%U() missing %i required %s argument%s: %U",
3277 co->co_name,
3278 len,
3279 kind,
3280 len == 1 ? "" : "s",
3281 name_str);
3282 Py_DECREF(name_str);
3283}
3284
3285static void
3286missing_arguments(PyCodeObject *co, int missing, int defcount,
3287 PyObject **fastlocals)
3288{
3289 int i, j = 0;
3290 int start, end;
3291 int positional = defcount != -1;
3292 const char *kind = positional ? "positional" : "keyword-only";
3293 PyObject *missing_names;
3294
3295 /* Compute the names of the arguments that are missing. */
3296 missing_names = PyList_New(missing);
3297 if (missing_names == NULL)
3298 return;
3299 if (positional) {
3300 start = 0;
3301 end = co->co_argcount - defcount;
3302 }
3303 else {
3304 start = co->co_argcount;
3305 end = start + co->co_kwonlyargcount;
3306 }
3307 for (i = start; i < end; i++) {
3308 if (GETLOCAL(i) == NULL) {
3309 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3310 PyObject *name = PyObject_Repr(raw);
3311 if (name == NULL) {
3312 Py_DECREF(missing_names);
3313 return;
3314 }
3315 PyList_SET_ITEM(missing_names, j++, name);
3316 }
3317 }
3318 assert(j == missing);
3319 format_missing(kind, co, missing_names);
3320 Py_DECREF(missing_names);
3321}
3322
3323static void
3324too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003325{
3326 int plural;
3327 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003328 int i;
3329 PyObject *sig, *kwonly_sig;
3330
Benjamin Petersone109c702011-06-24 09:37:26 -05003331 assert((co->co_flags & CO_VARARGS) == 0);
3332 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003333 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003334 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003335 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003336 if (defcount) {
3337 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003338 plural = 1;
3339 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3340 }
3341 else {
3342 plural = co->co_argcount != 1;
3343 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3344 }
3345 if (sig == NULL)
3346 return;
3347 if (kwonly_given) {
3348 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3349 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3350 kwonly_given != 1 ? "s" : "");
3351 if (kwonly_sig == NULL) {
3352 Py_DECREF(sig);
3353 return;
3354 }
3355 }
3356 else {
3357 /* This will not fail. */
3358 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003359 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003360 }
3361 PyErr_Format(PyExc_TypeError,
3362 "%U() takes %U positional argument%s but %d%U %s given",
3363 co->co_name,
3364 sig,
3365 plural ? "s" : "",
3366 given,
3367 kwonly_sig,
3368 given == 1 && !kwonly_given ? "was" : "were");
3369 Py_DECREF(sig);
3370 Py_DECREF(kwonly_sig);
3371}
3372
Guido van Rossumc2e20742006-02-27 22:32:47 +00003373/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003374 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003375 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003376
Tim Peters6d6c1a32001-08-02 04:15:00 +00003377PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003378PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 PyObject **args, int argcount, PyObject **kws, int kwcount,
3380 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003381{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003382 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003383 PyFrameObject *f;
3384 PyObject *retval = NULL;
3385 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 PyThreadState *tstate = PyThreadState_GET();
3387 PyObject *x, *u;
3388 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003389 int i;
3390 int n = argcount;
3391 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 if (globals == NULL) {
3394 PyErr_SetString(PyExc_SystemError,
3395 "PyEval_EvalCodeEx: NULL globals");
3396 return NULL;
3397 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 assert(tstate != NULL);
3400 assert(globals != NULL);
3401 f = PyFrame_New(tstate, co, globals, locals);
3402 if (f == NULL)
3403 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 fastlocals = f->f_localsplus;
3406 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003407
Benjamin Petersonb204a422011-06-05 22:04:07 -05003408 /* Parse arguments. */
3409 if (co->co_flags & CO_VARKEYWORDS) {
3410 kwdict = PyDict_New();
3411 if (kwdict == NULL)
3412 goto fail;
3413 i = total_args;
3414 if (co->co_flags & CO_VARARGS)
3415 i++;
3416 SETLOCAL(i, kwdict);
3417 }
3418 if (argcount > co->co_argcount)
3419 n = co->co_argcount;
3420 for (i = 0; i < n; i++) {
3421 x = args[i];
3422 Py_INCREF(x);
3423 SETLOCAL(i, x);
3424 }
3425 if (co->co_flags & CO_VARARGS) {
3426 u = PyTuple_New(argcount - n);
3427 if (u == NULL)
3428 goto fail;
3429 SETLOCAL(total_args, u);
3430 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 x = args[i];
3432 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003433 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003435 }
3436 for (i = 0; i < kwcount; i++) {
3437 PyObject **co_varnames;
3438 PyObject *keyword = kws[2*i];
3439 PyObject *value = kws[2*i + 1];
3440 int j;
3441 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3442 PyErr_Format(PyExc_TypeError,
3443 "%U() keywords must be strings",
3444 co->co_name);
3445 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003447 /* Speed hack: do raw pointer compares. As names are
3448 normally interned this should almost always hit. */
3449 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3450 for (j = 0; j < total_args; j++) {
3451 PyObject *nm = co_varnames[j];
3452 if (nm == keyword)
3453 goto kw_found;
3454 }
3455 /* Slow fallback, just in case */
3456 for (j = 0; j < total_args; j++) {
3457 PyObject *nm = co_varnames[j];
3458 int cmp = PyObject_RichCompareBool(
3459 keyword, nm, Py_EQ);
3460 if (cmp > 0)
3461 goto kw_found;
3462 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003464 }
3465 if (j >= total_args && kwdict == NULL) {
3466 PyErr_Format(PyExc_TypeError,
3467 "%U() got an unexpected "
3468 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 co->co_name,
3470 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003471 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003473 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3474 goto fail;
3475 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003476 continue;
3477 kw_found:
3478 if (GETLOCAL(j) != NULL) {
3479 PyErr_Format(PyExc_TypeError,
3480 "%U() got multiple "
3481 "values for argument '%S'",
3482 co->co_name,
3483 keyword);
3484 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003486 Py_INCREF(value);
3487 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003489 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003490 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 goto fail;
3492 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003493 if (argcount < co->co_argcount) {
3494 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003495 int missing = 0;
3496 for (i = argcount; i < m; i++)
3497 if (GETLOCAL(i) == NULL)
3498 missing++;
3499 if (missing) {
3500 missing_arguments(co, missing, defcount, fastlocals);
3501 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003502 }
3503 if (n > m)
3504 i = n - m;
3505 else
3506 i = 0;
3507 for (; i < defcount; i++) {
3508 if (GETLOCAL(m+i) == NULL) {
3509 PyObject *def = defs[i];
3510 Py_INCREF(def);
3511 SETLOCAL(m+i, def);
3512 }
3513 }
3514 }
3515 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003516 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003517 for (i = co->co_argcount; i < total_args; i++) {
3518 PyObject *name;
3519 if (GETLOCAL(i) != NULL)
3520 continue;
3521 name = PyTuple_GET_ITEM(co->co_varnames, i);
3522 if (kwdefs != NULL) {
3523 PyObject *def = PyDict_GetItem(kwdefs, name);
3524 if (def) {
3525 Py_INCREF(def);
3526 SETLOCAL(i, def);
3527 continue;
3528 }
3529 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003530 missing++;
3531 }
3532 if (missing) {
3533 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003534 goto fail;
3535 }
3536 }
3537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003539 vars into frame. */
3540 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003542 int arg;
3543 /* Possibly account for the cell variable being an argument. */
3544 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003545 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003546 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003547 /* Clear the local copy. */
3548 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003549 }
3550 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003551 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003552 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003553 if (c == NULL)
3554 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003555 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003557 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3558 PyObject *o = PyTuple_GET_ITEM(closure, i);
3559 Py_INCREF(o);
3560 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 if (co->co_flags & CO_GENERATOR) {
3564 /* Don't need to keep the reference to f_back, it will be set
3565 * when the generator is resumed. */
3566 Py_XDECREF(f->f_back);
3567 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003571 /* Create a new generator that owns the ready to run frame
3572 * and return that as the value. */
3573 return PyGen_New(f);
3574 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003577
Thomas Woutersce272b62007-09-19 21:19:28 +00003578fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 /* decref'ing the frame can cause __del__ methods to get invoked,
3581 which can call back into Python. While we're done with the
3582 current Python frame (f), the associated C stack is still in use,
3583 so recursion_depth must be boosted for the duration.
3584 */
3585 assert(tstate != NULL);
3586 ++tstate->recursion_depth;
3587 Py_DECREF(f);
3588 --tstate->recursion_depth;
3589 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003590}
3591
3592
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003593static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003594special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003597 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003599 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 return NULL;
3601 }
3602 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003603}
3604
3605
Benjamin Peterson87880242011-07-03 16:48:31 -05003606/* These 3 functions deal with the exception state of generators. */
3607
3608static void
3609save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3610{
3611 PyObject *type, *value, *traceback;
3612 Py_XINCREF(tstate->exc_type);
3613 Py_XINCREF(tstate->exc_value);
3614 Py_XINCREF(tstate->exc_traceback);
3615 type = f->f_exc_type;
3616 value = f->f_exc_value;
3617 traceback = f->f_exc_traceback;
3618 f->f_exc_type = tstate->exc_type;
3619 f->f_exc_value = tstate->exc_value;
3620 f->f_exc_traceback = tstate->exc_traceback;
3621 Py_XDECREF(type);
3622 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003623 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003624}
3625
3626static void
3627swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3628{
3629 PyObject *tmp;
3630 tmp = tstate->exc_type;
3631 tstate->exc_type = f->f_exc_type;
3632 f->f_exc_type = tmp;
3633 tmp = tstate->exc_value;
3634 tstate->exc_value = f->f_exc_value;
3635 f->f_exc_value = tmp;
3636 tmp = tstate->exc_traceback;
3637 tstate->exc_traceback = f->f_exc_traceback;
3638 f->f_exc_traceback = tmp;
3639}
3640
3641static void
3642restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3643{
3644 PyObject *type, *value, *tb;
3645 type = tstate->exc_type;
3646 value = tstate->exc_value;
3647 tb = tstate->exc_traceback;
3648 tstate->exc_type = f->f_exc_type;
3649 tstate->exc_value = f->f_exc_value;
3650 tstate->exc_traceback = f->f_exc_traceback;
3651 f->f_exc_type = NULL;
3652 f->f_exc_value = NULL;
3653 f->f_exc_traceback = NULL;
3654 Py_XDECREF(type);
3655 Py_XDECREF(value);
3656 Py_XDECREF(tb);
3657}
3658
3659
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003660/* Logic for the raise statement (too complicated for inlining).
3661 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003662static int
Collin Winter828f04a2007-08-31 00:04:24 +00003663do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 if (exc == NULL) {
3668 /* Reraise */
3669 PyThreadState *tstate = PyThreadState_GET();
3670 PyObject *tb;
3671 type = tstate->exc_type;
3672 value = tstate->exc_value;
3673 tb = tstate->exc_traceback;
3674 if (type == Py_None) {
3675 PyErr_SetString(PyExc_RuntimeError,
3676 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003677 return 0;
3678 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 Py_XINCREF(type);
3680 Py_XINCREF(value);
3681 Py_XINCREF(tb);
3682 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003683 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 /* We support the following forms of raise:
3687 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003688 raise <instance>
3689 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 if (PyExceptionClass_Check(exc)) {
3692 type = exc;
3693 value = PyObject_CallObject(exc, NULL);
3694 if (value == NULL)
3695 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003696 if (!PyExceptionInstance_Check(value)) {
3697 PyErr_Format(PyExc_TypeError,
3698 "calling %R should have returned an instance of "
3699 "BaseException, not %R",
3700 type, Py_TYPE(value));
3701 goto raise_error;
3702 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 }
3704 else if (PyExceptionInstance_Check(exc)) {
3705 value = exc;
3706 type = PyExceptionInstance_Class(exc);
3707 Py_INCREF(type);
3708 }
3709 else {
3710 /* Not something you can raise. You get an exception
3711 anyway, just not what you specified :-) */
3712 Py_DECREF(exc);
3713 PyErr_SetString(PyExc_TypeError,
3714 "exceptions must derive from BaseException");
3715 goto raise_error;
3716 }
Collin Winter828f04a2007-08-31 00:04:24 +00003717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 if (cause) {
3719 PyObject *fixed_cause;
3720 if (PyExceptionClass_Check(cause)) {
3721 fixed_cause = PyObject_CallObject(cause, NULL);
3722 if (fixed_cause == NULL)
3723 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003724 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003726 else if (PyExceptionInstance_Check(cause)) {
3727 fixed_cause = cause;
3728 }
3729 else if (cause == Py_None) {
3730 Py_DECREF(cause);
3731 fixed_cause = NULL;
3732 }
3733 else {
3734 PyErr_SetString(PyExc_TypeError,
3735 "exception causes must derive from "
3736 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 goto raise_error;
3738 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003739 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 }
Collin Winter828f04a2007-08-31 00:04:24 +00003741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 PyErr_SetObject(type, value);
3743 /* PyErr_SetObject incref's its arguments */
3744 Py_XDECREF(value);
3745 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003746 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003747
3748raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 Py_XDECREF(value);
3750 Py_XDECREF(type);
3751 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003752 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003753}
3754
Tim Petersd6d010b2001-06-21 02:49:55 +00003755/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003756 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003757
Guido van Rossum0368b722007-05-11 16:50:42 +00003758 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3759 with a variable target.
3760*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003761
Barry Warsawe42b18f1997-08-25 22:13:04 +00003762static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003763unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 int i = 0, j = 0;
3766 Py_ssize_t ll = 0;
3767 PyObject *it; /* iter(v) */
3768 PyObject *w;
3769 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 it = PyObject_GetIter(v);
3774 if (it == NULL)
3775 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 for (; i < argcnt; i++) {
3778 w = PyIter_Next(it);
3779 if (w == NULL) {
3780 /* Iterator done, via error or exhaustion. */
3781 if (!PyErr_Occurred()) {
3782 PyErr_Format(PyExc_ValueError,
3783 "need more than %d value%s to unpack",
3784 i, i == 1 ? "" : "s");
3785 }
3786 goto Error;
3787 }
3788 *--sp = w;
3789 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 if (argcntafter == -1) {
3792 /* We better have exhausted the iterator now. */
3793 w = PyIter_Next(it);
3794 if (w == NULL) {
3795 if (PyErr_Occurred())
3796 goto Error;
3797 Py_DECREF(it);
3798 return 1;
3799 }
3800 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003801 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3802 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 goto Error;
3804 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 l = PySequence_List(it);
3807 if (l == NULL)
3808 goto Error;
3809 *--sp = l;
3810 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 ll = PyList_GET_SIZE(l);
3813 if (ll < argcntafter) {
3814 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3815 argcnt + ll);
3816 goto Error;
3817 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 /* Pop the "after-variable" args off the list. */
3820 for (j = argcntafter; j > 0; j--, i++) {
3821 *--sp = PyList_GET_ITEM(l, ll - j);
3822 }
3823 /* Resize the list. */
3824 Py_SIZE(l) = ll - argcntafter;
3825 Py_DECREF(it);
3826 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003827
Tim Petersd6d010b2001-06-21 02:49:55 +00003828Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 for (; i > 0; i--, sp++)
3830 Py_DECREF(*sp);
3831 Py_XDECREF(it);
3832 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003833}
3834
3835
Guido van Rossum96a42c81992-01-12 02:29:51 +00003836#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003837static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003838prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 printf("%s ", str);
3841 if (PyObject_Print(v, stdout, 0) != 0)
3842 PyErr_Clear(); /* Don't know what else to do */
3843 printf("\n");
3844 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003845}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003846#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003847
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003848static void
Fred Drake5755ce62001-06-27 19:19:46 +00003849call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003850{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003851 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 int err;
3853 PyErr_Fetch(&type, &value, &traceback);
3854 if (value == NULL) {
3855 value = Py_None;
3856 Py_INCREF(value);
3857 }
R David Murray35837612013-04-19 12:56:57 -04003858 PyErr_NormalizeException(&type, &value, &traceback);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003859 orig_traceback = traceback;
3860 if (traceback == NULL) {
3861 Py_INCREF(Py_None);
3862 traceback = Py_None;
3863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 arg = PyTuple_Pack(3, type, value, traceback);
3865 if (arg == NULL) {
3866 PyErr_Restore(type, value, traceback);
3867 return;
3868 }
3869 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3870 Py_DECREF(arg);
3871 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003872 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 else {
3874 Py_XDECREF(type);
3875 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003876 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003878}
3879
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003880static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003881call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 PyObject *type, *value, *traceback;
3885 int err;
3886 PyErr_Fetch(&type, &value, &traceback);
3887 err = call_trace(func, obj, frame, what, arg);
3888 if (err == 0)
3889 {
3890 PyErr_Restore(type, value, traceback);
3891 return 0;
3892 }
3893 else {
3894 Py_XDECREF(type);
3895 Py_XDECREF(value);
3896 Py_XDECREF(traceback);
3897 return -1;
3898 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003899}
3900
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003901static int
Fred Drake5755ce62001-06-27 19:19:46 +00003902call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003904{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003905 PyThreadState *tstate = frame->f_tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 int result;
3907 if (tstate->tracing)
3908 return 0;
3909 tstate->tracing++;
3910 tstate->use_tracing = 0;
3911 result = func(obj, frame, what, arg);
3912 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3913 || (tstate->c_profilefunc != NULL));
3914 tstate->tracing--;
3915 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003916}
3917
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003918PyObject *
3919_PyEval_CallTracing(PyObject *func, PyObject *args)
3920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 PyFrameObject *frame = PyEval_GetFrame();
3922 PyThreadState *tstate = frame->f_tstate;
3923 int save_tracing = tstate->tracing;
3924 int save_use_tracing = tstate->use_tracing;
3925 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 tstate->tracing = 0;
3928 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3929 || (tstate->c_profilefunc != NULL));
3930 result = PyObject_Call(func, args, NULL);
3931 tstate->tracing = save_tracing;
3932 tstate->use_tracing = save_use_tracing;
3933 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003934}
3935
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003936/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003937static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003938maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3940 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 int result = 0;
3943 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 /* If the last instruction executed isn't in the current
3946 instruction window, reset the window.
3947 */
3948 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3949 PyAddrPair bounds;
3950 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3951 &bounds);
3952 *instr_lb = bounds.ap_lower;
3953 *instr_ub = bounds.ap_upper;
3954 }
3955 /* If the last instruction falls at the start of a line or if
3956 it represents a jump backwards, update the frame's line
3957 number and call the trace function. */
3958 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3959 frame->f_lineno = line;
3960 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3961 }
3962 *instr_prev = frame->f_lasti;
3963 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003964}
3965
Fred Drake5755ce62001-06-27 19:19:46 +00003966void
3967PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 PyThreadState *tstate = PyThreadState_GET();
3970 PyObject *temp = tstate->c_profileobj;
3971 Py_XINCREF(arg);
3972 tstate->c_profilefunc = NULL;
3973 tstate->c_profileobj = NULL;
3974 /* Must make sure that tracing is not ignored if 'temp' is freed */
3975 tstate->use_tracing = tstate->c_tracefunc != NULL;
3976 Py_XDECREF(temp);
3977 tstate->c_profilefunc = func;
3978 tstate->c_profileobj = arg;
3979 /* Flag that tracing or profiling is turned on */
3980 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003981}
3982
3983void
3984PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3985{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 PyThreadState *tstate = PyThreadState_GET();
3987 PyObject *temp = tstate->c_traceobj;
3988 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3989 Py_XINCREF(arg);
3990 tstate->c_tracefunc = NULL;
3991 tstate->c_traceobj = NULL;
3992 /* Must make sure that profiling is not ignored if 'temp' is freed */
3993 tstate->use_tracing = tstate->c_profilefunc != NULL;
3994 Py_XDECREF(temp);
3995 tstate->c_tracefunc = func;
3996 tstate->c_traceobj = arg;
3997 /* Flag that tracing or profiling is turned on */
3998 tstate->use_tracing = ((func != NULL)
3999 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004000}
4001
Guido van Rossumb209a111997-04-29 18:18:01 +00004002PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004003PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 PyFrameObject *current_frame = PyEval_GetFrame();
4006 if (current_frame == NULL)
4007 return PyThreadState_GET()->interp->builtins;
4008 else
4009 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004010}
4011
Guido van Rossumb209a111997-04-29 18:18:01 +00004012PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004013PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004016 if (current_frame == NULL) {
4017 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004019 }
4020
4021 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4022 return NULL;
4023
4024 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004026}
4027
Guido van Rossumb209a111997-04-29 18:18:01 +00004028PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004029PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004030{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 PyFrameObject *current_frame = PyEval_GetFrame();
4032 if (current_frame == NULL)
4033 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004034
4035 assert(current_frame->f_globals != NULL);
4036 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004037}
4038
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004039PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004040PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 PyThreadState *tstate = PyThreadState_GET();
4043 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004044}
4045
Guido van Rossum6135a871995-01-09 17:53:26 +00004046int
Tim Peters5ba58662001-07-16 02:29:45 +00004047PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 PyFrameObject *current_frame = PyEval_GetFrame();
4050 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 if (current_frame != NULL) {
4053 const int codeflags = current_frame->f_code->co_flags;
4054 const int compilerflags = codeflags & PyCF_MASK;
4055 if (compilerflags) {
4056 result = 1;
4057 cf->cf_flags |= compilerflags;
4058 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004059#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 if (codeflags & CO_GENERATOR_ALLOWED) {
4061 result = 1;
4062 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4063 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004064#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 }
4066 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004067}
4068
Guido van Rossum3f5da241990-12-20 15:06:42 +00004069
Guido van Rossum681d79a1995-07-18 14:51:37 +00004070/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004071 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004072
Guido van Rossumb209a111997-04-29 18:18:01 +00004073PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004074PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004075{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004077
Victor Stinnerace47d72013-07-18 01:41:08 +02004078#ifdef Py_DEBUG
4079 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4080 set, because it may clear it (directly or indirectly)
4081 and so the caller looses its exception */
4082 assert(!PyErr_Occurred());
4083#endif
4084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 if (arg == NULL) {
4086 arg = PyTuple_New(0);
4087 if (arg == NULL)
4088 return NULL;
4089 }
4090 else if (!PyTuple_Check(arg)) {
4091 PyErr_SetString(PyExc_TypeError,
4092 "argument list must be a tuple");
4093 return NULL;
4094 }
4095 else
4096 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 if (kw != NULL && !PyDict_Check(kw)) {
4099 PyErr_SetString(PyExc_TypeError,
4100 "keyword list must be a dictionary");
4101 Py_DECREF(arg);
4102 return NULL;
4103 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004105 result = PyObject_Call(func, arg, kw);
4106 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004107
4108 assert((result != NULL && !PyErr_Occurred())
4109 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004111}
4112
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004113const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004114PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 if (PyMethod_Check(func))
4117 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4118 else if (PyFunction_Check(func))
4119 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4120 else if (PyCFunction_Check(func))
4121 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4122 else
4123 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004124}
4125
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004126const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004127PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 if (PyMethod_Check(func))
4130 return "()";
4131 else if (PyFunction_Check(func))
4132 return "()";
4133 else if (PyCFunction_Check(func))
4134 return "()";
4135 else
4136 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004137}
4138
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004139static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004140err_args(PyObject *func, int flags, int nargs)
4141{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004142 if (flags & METH_NOARGS)
4143 PyErr_Format(PyExc_TypeError,
4144 "%.200s() takes no arguments (%d given)",
4145 ((PyCFunctionObject *)func)->m_ml->ml_name,
4146 nargs);
4147 else
4148 PyErr_Format(PyExc_TypeError,
4149 "%.200s() takes exactly one argument (%d given)",
4150 ((PyCFunctionObject *)func)->m_ml->ml_name,
4151 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004152}
4153
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004154#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004155if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 if (call_trace(tstate->c_profilefunc, \
4157 tstate->c_profileobj, \
4158 tstate->frame, PyTrace_C_CALL, \
4159 func)) { \
4160 x = NULL; \
4161 } \
4162 else { \
4163 x = call; \
4164 if (tstate->c_profilefunc != NULL) { \
4165 if (x == NULL) { \
4166 call_trace_protected(tstate->c_profilefunc, \
4167 tstate->c_profileobj, \
4168 tstate->frame, PyTrace_C_EXCEPTION, \
4169 func); \
4170 /* XXX should pass (type, value, tb) */ \
4171 } else { \
4172 if (call_trace(tstate->c_profilefunc, \
4173 tstate->c_profileobj, \
4174 tstate->frame, PyTrace_C_RETURN, \
4175 func)) { \
4176 Py_DECREF(x); \
4177 x = NULL; \
4178 } \
4179 } \
4180 } \
4181 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004182} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 x = call; \
4184 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004185
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004186static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004187call_function(PyObject ***pp_stack, int oparg
4188#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004190#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004192{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004193 int na = oparg & 0xff;
4194 int nk = (oparg>>8) & 0xff;
4195 int n = na + 2 * nk;
4196 PyObject **pfunc = (*pp_stack) - n - 1;
4197 PyObject *func = *pfunc;
4198 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 /* Always dispatch PyCFunction first, because these are
4201 presumed to be the most frequent callable object.
4202 */
4203 if (PyCFunction_Check(func) && nk == 0) {
4204 int flags = PyCFunction_GET_FLAGS(func);
4205 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 PCALL(PCALL_CFUNCTION);
4208 if (flags & (METH_NOARGS | METH_O)) {
4209 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4210 PyObject *self = PyCFunction_GET_SELF(func);
4211 if (flags & METH_NOARGS && na == 0) {
4212 C_TRACE(x, (*meth)(self,NULL));
4213 }
4214 else if (flags & METH_O && na == 1) {
4215 PyObject *arg = EXT_POP(*pp_stack);
4216 C_TRACE(x, (*meth)(self,arg));
4217 Py_DECREF(arg);
4218 }
4219 else {
4220 err_args(func, flags, na);
4221 x = NULL;
4222 }
4223 }
4224 else {
4225 PyObject *callargs;
4226 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004227 if (callargs != NULL) {
4228 READ_TIMESTAMP(*pintr0);
4229 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4230 READ_TIMESTAMP(*pintr1);
4231 Py_XDECREF(callargs);
4232 }
4233 else {
4234 x = NULL;
4235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 }
4237 } else {
4238 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4239 /* optimize access to bound methods */
4240 PyObject *self = PyMethod_GET_SELF(func);
4241 PCALL(PCALL_METHOD);
4242 PCALL(PCALL_BOUND_METHOD);
4243 Py_INCREF(self);
4244 func = PyMethod_GET_FUNCTION(func);
4245 Py_INCREF(func);
4246 Py_DECREF(*pfunc);
4247 *pfunc = self;
4248 na++;
4249 n++;
4250 } else
4251 Py_INCREF(func);
4252 READ_TIMESTAMP(*pintr0);
4253 if (PyFunction_Check(func))
4254 x = fast_function(func, pp_stack, n, na, nk);
4255 else
4256 x = do_call(func, pp_stack, na, nk);
4257 READ_TIMESTAMP(*pintr1);
4258 Py_DECREF(func);
4259 }
Victor Stinnerf243ee42013-07-16 01:02:12 +02004260 assert((x != NULL && !PyErr_Occurred())
4261 || (x == NULL && PyErr_Occurred()));
Tim Peters8a5c3c72004-04-05 19:36:21 +00004262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 /* Clear the stack of the function object. Also removes
4264 the arguments in case they weren't consumed already
4265 (fast_function() and err_args() leave them on the stack).
4266 */
4267 while ((*pp_stack) > pfunc) {
4268 w = EXT_POP(*pp_stack);
4269 Py_DECREF(w);
4270 PCALL(PCALL_POP);
4271 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004272
4273 assert((x != NULL && !PyErr_Occurred())
4274 || (x == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004276}
4277
Jeremy Hylton192690e2002-08-16 18:36:11 +00004278/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004279 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004280 For the simplest case -- a function that takes only positional
4281 arguments and is called with only positional arguments -- it
4282 inlines the most primitive frame setup code from
4283 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4284 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004285*/
4286
4287static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004288fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4291 PyObject *globals = PyFunction_GET_GLOBALS(func);
4292 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4293 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4294 PyObject **d = NULL;
4295 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 PCALL(PCALL_FUNCTION);
4298 PCALL(PCALL_FAST_FUNCTION);
4299 if (argdefs == NULL && co->co_argcount == n &&
4300 co->co_kwonlyargcount == 0 && nk==0 &&
4301 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4302 PyFrameObject *f;
4303 PyObject *retval = NULL;
4304 PyThreadState *tstate = PyThreadState_GET();
4305 PyObject **fastlocals, **stack;
4306 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 PCALL(PCALL_FASTER_FUNCTION);
4309 assert(globals != NULL);
4310 /* XXX Perhaps we should create a specialized
4311 PyFrame_New() that doesn't take locals, but does
4312 take builtins without sanity checking them.
4313 */
4314 assert(tstate != NULL);
4315 f = PyFrame_New(tstate, co, globals, NULL);
4316 if (f == NULL)
4317 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 fastlocals = f->f_localsplus;
4320 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 for (i = 0; i < n; i++) {
4323 Py_INCREF(*stack);
4324 fastlocals[i] = *stack++;
4325 }
4326 retval = PyEval_EvalFrameEx(f,0);
4327 ++tstate->recursion_depth;
4328 Py_DECREF(f);
4329 --tstate->recursion_depth;
4330 return retval;
4331 }
4332 if (argdefs != NULL) {
4333 d = &PyTuple_GET_ITEM(argdefs, 0);
4334 nd = Py_SIZE(argdefs);
4335 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004336 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 (PyObject *)NULL, (*pp_stack)-n, na,
4338 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4339 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004340}
4341
4342static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004343update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4344 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 PyObject *kwdict = NULL;
4347 if (orig_kwdict == NULL)
4348 kwdict = PyDict_New();
4349 else {
4350 kwdict = PyDict_Copy(orig_kwdict);
4351 Py_DECREF(orig_kwdict);
4352 }
4353 if (kwdict == NULL)
4354 return NULL;
4355 while (--nk >= 0) {
4356 int err;
4357 PyObject *value = EXT_POP(*pp_stack);
4358 PyObject *key = EXT_POP(*pp_stack);
4359 if (PyDict_GetItem(kwdict, key) != NULL) {
4360 PyErr_Format(PyExc_TypeError,
4361 "%.200s%s got multiple values "
4362 "for keyword argument '%U'",
4363 PyEval_GetFuncName(func),
4364 PyEval_GetFuncDesc(func),
4365 key);
4366 Py_DECREF(key);
4367 Py_DECREF(value);
4368 Py_DECREF(kwdict);
4369 return NULL;
4370 }
4371 err = PyDict_SetItem(kwdict, key, value);
4372 Py_DECREF(key);
4373 Py_DECREF(value);
4374 if (err) {
4375 Py_DECREF(kwdict);
4376 return NULL;
4377 }
4378 }
4379 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004380}
4381
4382static PyObject *
4383update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 callargs = PyTuple_New(nstack + nstar);
4389 if (callargs == NULL) {
4390 return NULL;
4391 }
4392 if (nstar) {
4393 int i;
4394 for (i = 0; i < nstar; i++) {
4395 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4396 Py_INCREF(a);
4397 PyTuple_SET_ITEM(callargs, nstack + i, a);
4398 }
4399 }
4400 while (--nstack >= 0) {
4401 w = EXT_POP(*pp_stack);
4402 PyTuple_SET_ITEM(callargs, nstack, w);
4403 }
4404 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004405}
4406
4407static PyObject *
4408load_args(PyObject ***pp_stack, int na)
4409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 PyObject *args = PyTuple_New(na);
4411 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 if (args == NULL)
4414 return NULL;
4415 while (--na >= 0) {
4416 w = EXT_POP(*pp_stack);
4417 PyTuple_SET_ITEM(args, na, w);
4418 }
4419 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004420}
4421
4422static PyObject *
4423do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 PyObject *callargs = NULL;
4426 PyObject *kwdict = NULL;
4427 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 if (nk > 0) {
4430 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4431 if (kwdict == NULL)
4432 goto call_fail;
4433 }
4434 callargs = load_args(pp_stack, na);
4435 if (callargs == NULL)
4436 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004437#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 /* At this point, we have to look at the type of func to
4439 update the call stats properly. Do it here so as to avoid
4440 exposing the call stats machinery outside ceval.c
4441 */
4442 if (PyFunction_Check(func))
4443 PCALL(PCALL_FUNCTION);
4444 else if (PyMethod_Check(func))
4445 PCALL(PCALL_METHOD);
4446 else if (PyType_Check(func))
4447 PCALL(PCALL_TYPE);
4448 else if (PyCFunction_Check(func))
4449 PCALL(PCALL_CFUNCTION);
4450 else
4451 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 if (PyCFunction_Check(func)) {
4454 PyThreadState *tstate = PyThreadState_GET();
4455 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4456 }
4457 else
4458 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004459call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 Py_XDECREF(callargs);
4461 Py_XDECREF(kwdict);
4462 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004463}
4464
4465static PyObject *
4466ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 int nstar = 0;
4469 PyObject *callargs = NULL;
4470 PyObject *stararg = NULL;
4471 PyObject *kwdict = NULL;
4472 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 if (flags & CALL_FLAG_KW) {
4475 kwdict = EXT_POP(*pp_stack);
4476 if (!PyDict_Check(kwdict)) {
4477 PyObject *d;
4478 d = PyDict_New();
4479 if (d == NULL)
4480 goto ext_call_fail;
4481 if (PyDict_Update(d, kwdict) != 0) {
4482 Py_DECREF(d);
4483 /* PyDict_Update raises attribute
4484 * error (percolated from an attempt
4485 * to get 'keys' attribute) instead of
4486 * a type error if its second argument
4487 * is not a mapping.
4488 */
4489 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4490 PyErr_Format(PyExc_TypeError,
4491 "%.200s%.200s argument after ** "
4492 "must be a mapping, not %.200s",
4493 PyEval_GetFuncName(func),
4494 PyEval_GetFuncDesc(func),
4495 kwdict->ob_type->tp_name);
4496 }
4497 goto ext_call_fail;
4498 }
4499 Py_DECREF(kwdict);
4500 kwdict = d;
4501 }
4502 }
4503 if (flags & CALL_FLAG_VAR) {
4504 stararg = EXT_POP(*pp_stack);
4505 if (!PyTuple_Check(stararg)) {
4506 PyObject *t = NULL;
4507 t = PySequence_Tuple(stararg);
4508 if (t == NULL) {
4509 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4510 PyErr_Format(PyExc_TypeError,
4511 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004512 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 PyEval_GetFuncName(func),
4514 PyEval_GetFuncDesc(func),
4515 stararg->ob_type->tp_name);
4516 }
4517 goto ext_call_fail;
4518 }
4519 Py_DECREF(stararg);
4520 stararg = t;
4521 }
4522 nstar = PyTuple_GET_SIZE(stararg);
4523 }
4524 if (nk > 0) {
4525 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4526 if (kwdict == NULL)
4527 goto ext_call_fail;
4528 }
4529 callargs = update_star_args(na, nstar, stararg, pp_stack);
4530 if (callargs == NULL)
4531 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004532#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 /* At this point, we have to look at the type of func to
4534 update the call stats properly. Do it here so as to avoid
4535 exposing the call stats machinery outside ceval.c
4536 */
4537 if (PyFunction_Check(func))
4538 PCALL(PCALL_FUNCTION);
4539 else if (PyMethod_Check(func))
4540 PCALL(PCALL_METHOD);
4541 else if (PyType_Check(func))
4542 PCALL(PCALL_TYPE);
4543 else if (PyCFunction_Check(func))
4544 PCALL(PCALL_CFUNCTION);
4545 else
4546 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004547#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004548 if (PyCFunction_Check(func)) {
4549 PyThreadState *tstate = PyThreadState_GET();
4550 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4551 }
4552 else
4553 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004554ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 Py_XDECREF(callargs);
4556 Py_XDECREF(kwdict);
4557 Py_XDECREF(stararg);
Victor Stinnerf243ee42013-07-16 01:02:12 +02004558 assert((result != NULL && !PyErr_Occurred())
4559 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004561}
4562
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004563/* Extract a slice index from a PyInt or PyLong or an object with the
4564 nb_index slot defined, and store in *pi.
4565 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4566 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 +00004567 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004568*/
Tim Petersb5196382001-12-16 19:44:20 +00004569/* Note: If v is NULL, return success without storing into *pi. This
4570 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4571 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004572*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004573int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004574_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 if (v != NULL) {
4577 Py_ssize_t x;
4578 if (PyIndex_Check(v)) {
4579 x = PyNumber_AsSsize_t(v, NULL);
4580 if (x == -1 && PyErr_Occurred())
4581 return 0;
4582 }
4583 else {
4584 PyErr_SetString(PyExc_TypeError,
4585 "slice indices must be integers or "
4586 "None or have an __index__ method");
4587 return 0;
4588 }
4589 *pi = x;
4590 }
4591 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004592}
4593
Guido van Rossum486364b2007-06-30 05:01:58 +00004594#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004596
Guido van Rossumb209a111997-04-29 18:18:01 +00004597static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004598cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 int res = 0;
4601 switch (op) {
4602 case PyCmp_IS:
4603 res = (v == w);
4604 break;
4605 case PyCmp_IS_NOT:
4606 res = (v != w);
4607 break;
4608 case PyCmp_IN:
4609 res = PySequence_Contains(w, v);
4610 if (res < 0)
4611 return NULL;
4612 break;
4613 case PyCmp_NOT_IN:
4614 res = PySequence_Contains(w, v);
4615 if (res < 0)
4616 return NULL;
4617 res = !res;
4618 break;
4619 case PyCmp_EXC_MATCH:
4620 if (PyTuple_Check(w)) {
4621 Py_ssize_t i, length;
4622 length = PyTuple_Size(w);
4623 for (i = 0; i < length; i += 1) {
4624 PyObject *exc = PyTuple_GET_ITEM(w, i);
4625 if (!PyExceptionClass_Check(exc)) {
4626 PyErr_SetString(PyExc_TypeError,
4627 CANNOT_CATCH_MSG);
4628 return NULL;
4629 }
4630 }
4631 }
4632 else {
4633 if (!PyExceptionClass_Check(w)) {
4634 PyErr_SetString(PyExc_TypeError,
4635 CANNOT_CATCH_MSG);
4636 return NULL;
4637 }
4638 }
4639 res = PyErr_GivenExceptionMatches(v, w);
4640 break;
4641 default:
4642 return PyObject_RichCompare(v, w, op);
4643 }
4644 v = res ? Py_True : Py_False;
4645 Py_INCREF(v);
4646 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004647}
4648
Thomas Wouters52152252000-08-17 22:55:00 +00004649static PyObject *
4650import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 x = PyObject_GetAttr(v, name);
4655 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Brett Cannona79e4fb2013-07-12 11:22:26 -04004656 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 }
4658 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004659}
Guido van Rossumac7be682001-01-17 15:42:30 +00004660
Thomas Wouters52152252000-08-17 22:55:00 +00004661static int
4662import_all_from(PyObject *locals, PyObject *v)
4663{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004664 _Py_IDENTIFIER(__all__);
4665 _Py_IDENTIFIER(__dict__);
4666 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 PyObject *dict, *name, *value;
4668 int skip_leading_underscores = 0;
4669 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 if (all == NULL) {
4672 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4673 return -1; /* Unexpected error */
4674 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004675 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 if (dict == NULL) {
4677 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4678 return -1;
4679 PyErr_SetString(PyExc_ImportError,
4680 "from-import-* object has no __dict__ and no __all__");
4681 return -1;
4682 }
4683 all = PyMapping_Keys(dict);
4684 Py_DECREF(dict);
4685 if (all == NULL)
4686 return -1;
4687 skip_leading_underscores = 1;
4688 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 for (pos = 0, err = 0; ; pos++) {
4691 name = PySequence_GetItem(all, pos);
4692 if (name == NULL) {
4693 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4694 err = -1;
4695 else
4696 PyErr_Clear();
4697 break;
4698 }
4699 if (skip_leading_underscores &&
4700 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004701 PyUnicode_READY(name) != -1 &&
4702 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 {
4704 Py_DECREF(name);
4705 continue;
4706 }
4707 value = PyObject_GetAttr(v, name);
4708 if (value == NULL)
4709 err = -1;
4710 else if (PyDict_CheckExact(locals))
4711 err = PyDict_SetItem(locals, name, value);
4712 else
4713 err = PyObject_SetItem(locals, name, value);
4714 Py_DECREF(name);
4715 Py_XDECREF(value);
4716 if (err != 0)
4717 break;
4718 }
4719 Py_DECREF(all);
4720 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004721}
4722
Guido van Rossumac7be682001-01-17 15:42:30 +00004723static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004724format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004725{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 if (!obj)
4729 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004731 obj_str = _PyUnicode_AsString(obj);
4732 if (!obj_str)
4733 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004736}
Guido van Rossum950361c1997-01-24 13:49:28 +00004737
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004738static void
4739format_exc_unbound(PyCodeObject *co, int oparg)
4740{
4741 PyObject *name;
4742 /* Don't stomp existing exception */
4743 if (PyErr_Occurred())
4744 return;
4745 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4746 name = PyTuple_GET_ITEM(co->co_cellvars,
4747 oparg);
4748 format_exc_check_arg(
4749 PyExc_UnboundLocalError,
4750 UNBOUNDLOCAL_ERROR_MSG,
4751 name);
4752 } else {
4753 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4754 PyTuple_GET_SIZE(co->co_cellvars));
4755 format_exc_check_arg(PyExc_NameError,
4756 UNBOUNDFREE_ERROR_MSG, name);
4757 }
4758}
4759
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004760static PyObject *
4761unicode_concatenate(PyObject *v, PyObject *w,
4762 PyFrameObject *f, unsigned char *next_instr)
4763{
4764 PyObject *res;
4765 if (Py_REFCNT(v) == 2) {
4766 /* In the common case, there are 2 references to the value
4767 * stored in 'variable' when the += is performed: one on the
4768 * value stack (in 'v') and one still stored in the
4769 * 'variable'. We try to delete the variable now to reduce
4770 * the refcnt to 1.
4771 */
4772 switch (*next_instr) {
4773 case STORE_FAST:
4774 {
4775 int oparg = PEEKARG();
4776 PyObject **fastlocals = f->f_localsplus;
4777 if (GETLOCAL(oparg) == v)
4778 SETLOCAL(oparg, NULL);
4779 break;
4780 }
4781 case STORE_DEREF:
4782 {
4783 PyObject **freevars = (f->f_localsplus +
4784 f->f_code->co_nlocals);
4785 PyObject *c = freevars[PEEKARG()];
4786 if (PyCell_GET(c) == v)
4787 PyCell_Set(c, NULL);
4788 break;
4789 }
4790 case STORE_NAME:
4791 {
4792 PyObject *names = f->f_code->co_names;
4793 PyObject *name = GETITEM(names, PEEKARG());
4794 PyObject *locals = f->f_locals;
4795 if (PyDict_CheckExact(locals) &&
4796 PyDict_GetItem(locals, name) == v) {
4797 if (PyDict_DelItem(locals, name) != 0) {
4798 PyErr_Clear();
4799 }
4800 }
4801 break;
4802 }
4803 }
4804 }
4805 res = v;
4806 PyUnicode_Append(&res, w);
4807 return res;
4808}
4809
Guido van Rossum950361c1997-01-24 13:49:28 +00004810#ifdef DYNAMIC_EXECUTION_PROFILE
4811
Skip Montanarof118cb12001-10-15 20:51:38 +00004812static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004813getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004815 int i;
4816 PyObject *l = PyList_New(256);
4817 if (l == NULL) return NULL;
4818 for (i = 0; i < 256; i++) {
4819 PyObject *x = PyLong_FromLong(a[i]);
4820 if (x == NULL) {
4821 Py_DECREF(l);
4822 return NULL;
4823 }
4824 PyList_SetItem(l, i, x);
4825 }
4826 for (i = 0; i < 256; i++)
4827 a[i] = 0;
4828 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004829}
4830
4831PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004832_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004833{
4834#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004836#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 int i;
4838 PyObject *l = PyList_New(257);
4839 if (l == NULL) return NULL;
4840 for (i = 0; i < 257; i++) {
4841 PyObject *x = getarray(dxpairs[i]);
4842 if (x == NULL) {
4843 Py_DECREF(l);
4844 return NULL;
4845 }
4846 PyList_SetItem(l, i, x);
4847 }
4848 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004849#endif
4850}
4851
4852#endif