blob: 25fbc0fc79f424a211dccf41c79c39b3b7654a7a [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
Victor Stinner0b881dd2014-12-12 13:17:41 +010068#define READ_TIMESTAMP(val) do { \
69 unsigned int h, l; \
70 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
71 (val) = ((uint64)l) | (((uint64)h) << 32); \
72 } while(0)
Mark Dickinsona25b1312009-10-31 10:18:44 +000073
74
75#else
76
77#error "Don't know how to implement timestamp counter for this architecture"
78
Michael W. Hudson800ba232004-08-12 18:19:17 +000079#endif
80
Thomas Wouters477c8d52006-05-27 19:21:47 +000081void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 uint64 intr, inst, loop;
85 PyThreadState *tstate = PyThreadState_Get();
86 if (!tstate->interp->tscdump)
87 return;
88 intr = intr1 - intr0;
89 inst = inst1 - inst0 - intr;
90 loop = loop1 - loop0 - intr;
91 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000092 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093}
Michael W. Hudson800ba232004-08-12 18:19:17 +000094
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095#endif
96
Guido van Rossum04691fc1992-08-12 15:35:34 +000097/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000098/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000099
Guido van Rossum408027e1996-12-30 16:17:54 +0000100#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000101/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102#define LLTRACE 1 /* Low-level trace feature */
103#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000104#endif
105
Jeremy Hylton52820442001-01-03 23:52:36 +0000106typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000107
Guido van Rossum374a9221991-04-04 10:40:29 +0000108/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
115static PyObject * do_call(PyObject *, PyObject ***, int, int);
116static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000117static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
120static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000121#define CALL_FLAG_VAR 1
122#define CALL_FLAG_KW 2
123
Guido van Rossum0a066c01992-03-27 17:29:15 +0000124#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000125static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000126static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000127#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100128static int call_trace(Py_tracefunc, PyObject *,
129 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000131static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100132 PyThreadState *, PyFrameObject *,
133 int, PyObject *);
134static void call_exc_trace(Py_tracefunc, PyObject *,
135 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000136static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100137 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000138
Thomas Wouters477c8d52006-05-27 19:21:47 +0000139static PyObject * cmp_outcome(int, PyObject *, PyObject *);
140static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000141static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000142static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000143static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200144static PyObject * unicode_concatenate(PyObject *, PyObject *,
145 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500146static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000147
Paul Prescode68140d2000-08-30 20:25:01 +0000148#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000150#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000152#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 "free variable '%.200s' referenced before assignment" \
154 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000155
Guido van Rossum950361c1997-01-24 13:49:28 +0000156/* Dynamic execution profile */
157#ifdef DYNAMIC_EXECUTION_PROFILE
158#ifdef DXPAIRS
159static long dxpairs[257][256];
160#define dxp dxpairs[256]
161#else
162static long dxp[256];
163#endif
164#endif
165
Jeremy Hylton985eba52003-02-05 23:13:00 +0000166/* Function call profile */
167#ifdef CALL_PROFILE
168#define PCALL_NUM 11
169static int pcall[PCALL_NUM];
170
171#define PCALL_ALL 0
172#define PCALL_FUNCTION 1
173#define PCALL_FAST_FUNCTION 2
174#define PCALL_FASTER_FUNCTION 3
175#define PCALL_METHOD 4
176#define PCALL_BOUND_METHOD 5
177#define PCALL_CFUNCTION 6
178#define PCALL_TYPE 7
179#define PCALL_GENERATOR 8
180#define PCALL_OTHER 9
181#define PCALL_POP 10
182
183/* Notes about the statistics
184
185 PCALL_FAST stats
186
187 FAST_FUNCTION means no argument tuple needs to be created.
188 FASTER_FUNCTION means that the fast-path frame setup code is used.
189
190 If there is a method call where the call can be optimized by changing
191 the argument tuple and calling the function directly, it gets recorded
192 twice.
193
194 As a result, the relationship among the statistics appears to be
195 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
196 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
197 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
198 PCALL_METHOD > PCALL_BOUND_METHOD
199*/
200
201#define PCALL(POS) pcall[POS]++
202
203PyObject *
204PyEval_GetCallStats(PyObject *self)
205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 return Py_BuildValue("iiiiiiiiiii",
207 pcall[0], pcall[1], pcall[2], pcall[3],
208 pcall[4], pcall[5], pcall[6], pcall[7],
209 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000210}
211#else
212#define PCALL(O)
213
214PyObject *
215PyEval_GetCallStats(PyObject *self)
216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 Py_INCREF(Py_None);
218 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000219}
220#endif
221
Tim Peters5ca576e2001-06-18 22:08:13 +0000222
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000223#ifdef WITH_THREAD
224#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
225#else
226#define GIL_REQUEST 0
227#endif
228
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000229/* This can set eval_breaker to 0 even though gil_drop_request became
230 1. We believe this is all right because the eval loop will release
231 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000232#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_store_relaxed( \
234 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000235 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
237 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000239#ifdef WITH_THREAD
240
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 do { \
243 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
244 _Py_atomic_store_relaxed(&eval_breaker, 1); \
245 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000246
247#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 do { \
249 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
250 COMPUTE_EVAL_BREAKER(); \
251 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000253#endif
254
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000255/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 do { \
258 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
259 _Py_atomic_store_relaxed(&eval_breaker, 1); \
260 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000261
262#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 do { \
264 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
265 COMPUTE_EVAL_BREAKER(); \
266 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000267
268#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 do { \
270 pending_async_exc = 1; \
271 _Py_atomic_store_relaxed(&eval_breaker, 1); \
272 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000276
277
Guido van Rossume59214e1994-08-30 08:01:59 +0000278#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000279
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000282#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000283#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000284
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000285static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000286static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000287/* This single variable consolidates all requests to break out of the fast path
288 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000289static _Py_atomic_int eval_breaker = {0};
290/* Request for dropping the GIL */
291static _Py_atomic_int gil_drop_request = {0};
292/* Request for running pending calls. */
293static _Py_atomic_int pendingcalls_to_do = {0};
294/* Request for looking at the `async_exc` field of the current thread state.
295 Guarded by the GIL. */
296static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000297
298#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299
Tim Peters7f468f22004-10-11 02:40:51 +0000300int
301PyEval_ThreadsInitialized(void)
302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000304}
305
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (gil_created())
310 return;
311 create_gil();
312 take_gil(PyThreadState_GET());
313 main_thread = PyThread_get_thread_ident();
314 if (!pending_lock)
315 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000316}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000317
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000318void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000319_PyEval_FiniThreads(void)
320{
321 if (!gil_created())
322 return;
323 destroy_gil();
324 assert(!gil_created());
325}
326
327void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000328PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 PyThreadState *tstate = PyThreadState_GET();
331 if (tstate == NULL)
332 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
333 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000334}
335
336void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000337PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 /* This function must succeed when the current thread state is NULL.
340 We therefore avoid PyThreadState_GET() which dumps a fatal error
341 in debug mode.
342 */
343 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
344 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000345}
346
347void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (tstate == NULL)
351 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
352 /* Check someone has called PyEval_InitThreads() to create the lock */
353 assert(gil_created());
354 take_gil(tstate);
355 if (PyThreadState_Swap(tstate) != NULL)
356 Py_FatalError(
357 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000358}
359
360void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000361PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (tstate == NULL)
364 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
365 if (PyThreadState_Swap(NULL) != tstate)
366 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
367 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000368}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000369
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200370/* This function is called from PyOS_AfterFork to destroy all threads which are
371 * not running in the child process, and clear internal locks which might be
372 * held by those threads. (This could also be done using pthread_atfork
373 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000374
375void
376PyEval_ReInitThreads(void)
377{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200378 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200380 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (!gil_created())
383 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 recreate_gil();
385 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200386 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Update the threading module with the new state.
390 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200391 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 "threading");
393 if (threading == NULL) {
394 /* threading not imported */
395 PyErr_Clear();
396 return;
397 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200398 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (result == NULL)
400 PyErr_WriteUnraisable(threading);
401 else
402 Py_DECREF(result);
403 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200404
405 /* Destroy all threads except the current one */
406 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000407}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000408
409#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000410static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000411static int pending_async_exc = 0;
412#endif /* WITH_THREAD */
413
414/* This function is used to signal that async exceptions are waiting to be
415 raised, therefore it is also useful in non-threaded builds. */
416
417void
418_PyEval_SignalAsyncExc(void)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000421}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422
Guido van Rossumff4949e1992-08-05 19:58:53 +0000423/* Functions save_thread and restore_thread are always defined so
424 dynamically loaded modules needn't be compiled separately for use
425 with and without threads: */
426
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000427PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000428PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyThreadState *tstate = PyThreadState_Swap(NULL);
431 if (tstate == NULL)
432 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000433#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (gil_created())
435 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000438}
439
440void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000441PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (tstate == NULL)
444 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000445#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (gil_created()) {
447 int err = errno;
448 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200449 /* _Py_Finalizing is protected by the GIL */
450 if (_Py_Finalizing && tstate != _Py_Finalizing) {
451 drop_gil(tstate);
452 PyThread_exit_thread();
453 assert(0); /* unreachable */
454 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 errno = err;
456 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000457#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000459}
460
461
Guido van Rossuma9672091994-09-14 13:31:22 +0000462/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
463 signal handlers or Mac I/O completion routines) can schedule calls
464 to a function to be called synchronously.
465 The synchronous function is called with one void* argument.
466 It should return 0 for success or -1 for failure -- failure should
467 be accompanied by an exception.
468
469 If registry succeeds, the registry function returns 0; if it fails
470 (e.g. due to too many pending calls) it returns -1 (without setting
471 an exception condition).
472
473 Note that because registry may occur from within signal handlers,
474 or other asynchronous events, calling malloc() is unsafe!
475
476#ifdef WITH_THREAD
477 Any thread can schedule pending calls, but only the main thread
478 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479 There is no facility to schedule calls to a particular thread, but
480 that should be easy to change, should that ever be required. In
481 that case, the static variables here should go into the python
482 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000483#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000485
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000486#ifdef WITH_THREAD
487
488/* The WITH_THREAD implementation is thread-safe. It allows
489 scheduling to be made from any thread, and even from an executing
490 callback.
491 */
492
493#define NPENDINGCALLS 32
494static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 int (*func)(void *);
496 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497} pendingcalls[NPENDINGCALLS];
498static int pendingfirst = 0;
499static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000500
501int
502Py_AddPendingCall(int (*func)(void *), void *arg)
503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 int i, j, result=0;
505 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 /* try a few times for the lock. Since this mechanism is used
508 * for signal handling (on the main thread), there is a (slim)
509 * chance that a signal is delivered on the same thread while we
510 * hold the lock during the Py_MakePendingCalls() function.
511 * This avoids a deadlock in that case.
512 * Note that signals can be delivered on any thread. In particular,
513 * on Windows, a SIGINT is delivered on a system-created worker
514 * thread.
515 * We also check for lock being NULL, in the unlikely case that
516 * this function is called before any bytecode evaluation takes place.
517 */
518 if (lock != NULL) {
519 for (i = 0; i<100; i++) {
520 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
521 break;
522 }
523 if (i == 100)
524 return -1;
525 }
526
527 i = pendinglast;
528 j = (i + 1) % NPENDINGCALLS;
529 if (j == pendingfirst) {
530 result = -1; /* Queue full */
531 } else {
532 pendingcalls[i].func = func;
533 pendingcalls[i].arg = arg;
534 pendinglast = j;
535 }
536 /* signal main loop */
537 SIGNAL_PENDING_CALLS();
538 if (lock != NULL)
539 PyThread_release_lock(lock);
540 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000541}
542
543int
544Py_MakePendingCalls(void)
545{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200546 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 int i;
548 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 if (!pending_lock) {
551 /* initial allocation of the lock */
552 pending_lock = PyThread_allocate_lock();
553 if (pending_lock == NULL)
554 return -1;
555 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* only service pending calls on main thread */
558 if (main_thread && PyThread_get_thread_ident() != main_thread)
559 return 0;
560 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200561 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200563 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 /* perform a bounded number of calls, in case of recursion */
565 for (i=0; i<NPENDINGCALLS; i++) {
566 int j;
567 int (*func)(void *);
568 void *arg = NULL;
569
570 /* pop one item off the queue while holding the lock */
571 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
572 j = pendingfirst;
573 if (j == pendinglast) {
574 func = NULL; /* Queue empty */
575 } else {
576 func = pendingcalls[j].func;
577 arg = pendingcalls[j].arg;
578 pendingfirst = (j + 1) % NPENDINGCALLS;
579 }
580 if (pendingfirst != pendinglast)
581 SIGNAL_PENDING_CALLS();
582 else
583 UNSIGNAL_PENDING_CALLS();
584 PyThread_release_lock(pending_lock);
585 /* having released the lock, perform the callback */
586 if (func == NULL)
587 break;
588 r = func(arg);
589 if (r)
590 break;
591 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200592 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000594}
595
596#else /* if ! defined WITH_THREAD */
597
598/*
599 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
600 This code is used for signal handling in python that isn't built
601 with WITH_THREAD.
602 Don't use this implementation when Py_AddPendingCalls() can happen
603 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604
Guido van Rossuma9672091994-09-14 13:31:22 +0000605 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000606 (1) nested asynchronous calls to Py_AddPendingCall()
607 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 (1) is very unlikely because typically signal delivery
610 is blocked during signal handling. So it should be impossible.
611 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000612 The current code is safe against (2), but not against (1).
613 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000614 thread is present, interrupted by signals, and that the critical
615 section is protected with the "busy" variable. On Windows, which
616 delivers SIGINT on a system thread, this does not hold and therefore
617 Windows really shouldn't use this version.
618 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000619*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000620
Guido van Rossuma9672091994-09-14 13:31:22 +0000621#define NPENDINGCALLS 32
622static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 int (*func)(void *);
624 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000625} pendingcalls[NPENDINGCALLS];
626static volatile int pendingfirst = 0;
627static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000628static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000629
630int
Thomas Wouters334fb892000-07-25 12:56:38 +0000631Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 static volatile int busy = 0;
634 int i, j;
635 /* XXX Begin critical section */
636 if (busy)
637 return -1;
638 busy = 1;
639 i = pendinglast;
640 j = (i + 1) % NPENDINGCALLS;
641 if (j == pendingfirst) {
642 busy = 0;
643 return -1; /* Queue full */
644 }
645 pendingcalls[i].func = func;
646 pendingcalls[i].arg = arg;
647 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 SIGNAL_PENDING_CALLS();
650 busy = 0;
651 /* XXX End critical section */
652 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000653}
654
Guido van Rossum180d7b41994-09-29 09:45:57 +0000655int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000656Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 static int busy = 0;
659 if (busy)
660 return 0;
661 busy = 1;
662 UNSIGNAL_PENDING_CALLS();
663 for (;;) {
664 int i;
665 int (*func)(void *);
666 void *arg;
667 i = pendingfirst;
668 if (i == pendinglast)
669 break; /* Queue empty */
670 func = pendingcalls[i].func;
671 arg = pendingcalls[i].arg;
672 pendingfirst = (i + 1) % NPENDINGCALLS;
673 if (func(arg) < 0) {
674 busy = 0;
675 SIGNAL_PENDING_CALLS(); /* We're not done yet */
676 return -1;
677 }
678 }
679 busy = 0;
680 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000681}
682
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000683#endif /* WITH_THREAD */
684
Guido van Rossuma9672091994-09-14 13:31:22 +0000685
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686/* The interpreter's recursion limit */
687
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000688#ifndef Py_DEFAULT_RECURSION_LIMIT
689#define Py_DEFAULT_RECURSION_LIMIT 1000
690#endif
691static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
692int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000694int
695Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698}
699
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000700void
701Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 recursion_limit = new_limit;
704 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000705}
706
Armin Rigo2b3eb402003-10-28 12:05:48 +0000707/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
708 if the recursion_depth reaches _Py_CheckRecursionLimit.
709 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
710 to guarantee that _Py_CheckRecursiveCall() is regularly called.
711 Without USE_STACKCHECK, there is no need for this. */
712int
713_Py_CheckRecursiveCall(char *where)
714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000716
717#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 if (PyOS_CheckStack()) {
719 --tstate->recursion_depth;
720 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
721 return -1;
722 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000723#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 _Py_CheckRecursionLimit = recursion_limit;
725 if (tstate->recursion_critical)
726 /* Somebody asked that we don't check for recursion. */
727 return 0;
728 if (tstate->overflowed) {
729 if (tstate->recursion_depth > recursion_limit + 50) {
730 /* Overflowing while handling an overflow. Give up. */
731 Py_FatalError("Cannot recover from stack overflow.");
732 }
733 return 0;
734 }
735 if (tstate->recursion_depth > recursion_limit) {
736 --tstate->recursion_depth;
737 tstate->overflowed = 1;
738 PyErr_Format(PyExc_RuntimeError,
739 "maximum recursion depth exceeded%s",
740 where);
741 return -1;
742 }
743 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000744}
745
Guido van Rossum374a9221991-04-04 10:40:29 +0000746/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000747enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000748 WHY_NOT = 0x0001, /* No error */
749 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000750 WHY_RETURN = 0x0008, /* 'return' statement */
751 WHY_BREAK = 0x0010, /* 'break' statement */
752 WHY_CONTINUE = 0x0020, /* 'continue' statement */
753 WHY_YIELD = 0x0040, /* 'yield' operator */
754 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000755};
Guido van Rossum374a9221991-04-04 10:40:29 +0000756
Benjamin Peterson87880242011-07-03 16:48:31 -0500757static void save_exc_state(PyThreadState *, PyFrameObject *);
758static void swap_exc_state(PyThreadState *, PyFrameObject *);
759static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400760static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000761static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000762
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000763/* Records whether tracing is on for any thread. Counts the number of
764 threads for which tstate->c_tracefunc is non-NULL, so if the value
765 is 0, we know we don't have to check this thread's c_tracefunc.
766 This speeds up the if statement in PyEval_EvalFrameEx() after
767 fast_next_opcode*/
768static int _Py_TracingPossible = 0;
769
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000770
Guido van Rossum374a9221991-04-04 10:40:29 +0000771
Guido van Rossumb209a111997-04-29 18:18:01 +0000772PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000773PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 return PyEval_EvalCodeEx(co,
776 globals, locals,
777 (PyObject **)NULL, 0,
778 (PyObject **)NULL, 0,
779 (PyObject **)NULL, 0,
780 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000781}
782
783
784/* Interpreter main loop */
785
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000786PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000787PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 /* This is for backward compatibility with extension modules that
789 used this API; core interpreter code should call
790 PyEval_EvalFrameEx() */
791 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000792}
793
794PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000795PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000796{
Guido van Rossum950361c1997-01-24 13:49:28 +0000797#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000799#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200800 PyObject **stack_pointer; /* Next free slot in value stack */
801 unsigned char *next_instr;
802 int opcode; /* Current opcode */
803 int oparg; /* Current opcode argument, if any */
804 enum why_code why; /* Reason for block stack unwind */
805 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject *retval = NULL; /* Return value */
807 PyThreadState *tstate = PyThreadState_GET();
808 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 is true when the line being executed has changed. The
815 initial values are such as to make this false the first
816 time it is tested. */
817 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 unsigned char *first_instr;
820 PyObject *names;
821 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000822
Brett Cannon368b4b72012-04-02 12:17:59 -0400823#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200824 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400825#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200826
Antoine Pitroub52ec782009-01-25 16:34:23 +0000827/* Computed GOTOs, or
828 the-optimization-commonly-but-improperly-known-as-"threaded code"
829 using gcc's labels-as-values extension
830 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
831
832 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000834 combined with a lookup table of jump addresses. However, since the
835 indirect jump instruction is shared by all opcodes, the CPU will have a
836 hard time making the right prediction for where to jump next (actually,
837 it will be always wrong except in the uncommon case of a sequence of
838 several identical opcodes).
839
840 "Threaded code" in contrast, uses an explicit jump table and an explicit
841 indirect jump instruction at the end of each opcode. Since the jump
842 instruction is at a different address for each opcode, the CPU will make a
843 separate prediction for each of these instructions, which is equivalent to
844 predicting the second opcode of each opcode pair. These predictions have
845 a much better chance to turn out valid, especially in small bytecode loops.
846
847 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000849 and potentially many more instructions (depending on the pipeline width).
850 A correctly predicted branch, however, is nearly free.
851
852 At the time of this writing, the "threaded code" version is up to 15-20%
853 faster than the normal "switch" version, depending on the compiler and the
854 CPU architecture.
855
856 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
857 because it would render the measurements invalid.
858
859
860 NOTE: care must be taken that the compiler doesn't try to "optimize" the
861 indirect jumps by sharing them between all opcodes. Such optimizations
862 can be disabled on gcc by using the -fno-gcse flag (or possibly
863 -fno-crossjumping).
864*/
865
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000868#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000869#endif
870
Antoine Pitrou042b1282010-08-13 21:15:58 +0000871#ifdef HAVE_COMPUTED_GOTOS
872 #ifndef USE_COMPUTED_GOTOS
873 #define USE_COMPUTED_GOTOS 1
874 #endif
875#else
876 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
877 #error "Computed gotos are not supported on this compiler."
878 #endif
879 #undef USE_COMPUTED_GOTOS
880 #define USE_COMPUTED_GOTOS 0
881#endif
882
883#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000884/* Import the static jump table */
885#include "opcode_targets.h"
886
887/* This macro is used when several opcodes defer to the same implementation
888 (e.g. SETUP_LOOP, SETUP_FINALLY) */
889#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 TARGET_##op: \
891 opcode = op; \
892 if (HAS_ARG(op)) \
893 oparg = NEXTARG(); \
894 case op: \
895 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000896
897#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 TARGET_##op: \
899 opcode = op; \
900 if (HAS_ARG(op)) \
901 oparg = NEXTARG(); \
902 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000903
904
905#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 { \
907 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
908 FAST_DISPATCH(); \
909 } \
910 continue; \
911 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912
913#ifdef LLTRACE
914#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 { \
916 if (!lltrace && !_Py_TracingPossible) { \
917 f->f_lasti = INSTR_OFFSET(); \
918 goto *opcode_targets[*next_instr++]; \
919 } \
920 goto fast_next_opcode; \
921 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922#else
923#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 { \
925 if (!_Py_TracingPossible) { \
926 f->f_lasti = INSTR_OFFSET(); \
927 goto *opcode_targets[*next_instr++]; \
928 } \
929 goto fast_next_opcode; \
930 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000931#endif
932
933#else
934#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000936#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 /* silence compiler warnings about `impl` unused */ \
938 if (0) goto impl; \
939 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000940#define DISPATCH() continue
941#define FAST_DISPATCH() goto fast_next_opcode
942#endif
943
944
Neal Norwitza81d2202002-07-14 00:27:26 +0000945/* Tuple access macros */
946
947#ifndef Py_DEBUG
948#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
949#else
950#define GETITEM(v, i) PyTuple_GetItem((v), (i))
951#endif
952
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000953#ifdef WITH_TSC
954/* Use Pentium timestamp counter to mark certain events:
955 inst0 -- beginning of switch statement for opcode dispatch
956 inst1 -- end of switch statement (may be skipped)
957 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000959 (may be skipped)
960 intr1 -- beginning of long interruption
961 intr2 -- end of long interruption
962
963 Many opcodes call out to helper C functions. In some cases, the
964 time in those functions should be counted towards the time for the
965 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
966 calls another Python function; there's no point in charge all the
967 bytecode executed by the called function to the caller.
968
969 It's hard to make a useful judgement statically. In the presence
970 of operator overloading, it's impossible to tell if a call will
971 execute new Python code or not.
972
973 It's a case-by-case judgement. I'll use intr1 for the following
974 cases:
975
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000976 IMPORT_STAR
977 IMPORT_FROM
978 CALL_FUNCTION (and friends)
979
980 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
982 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 READ_TIMESTAMP(inst0);
985 READ_TIMESTAMP(inst1);
986 READ_TIMESTAMP(loop0);
987 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 /* shut up the compiler */
990 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000991#endif
992
Guido van Rossum374a9221991-04-04 10:40:29 +0000993/* Code access macros */
994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995#define INSTR_OFFSET() ((int)(next_instr - first_instr))
996#define NEXTOP() (*next_instr++)
997#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
998#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
999#define JUMPTO(x) (next_instr = first_instr + (x))
1000#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001001
Raymond Hettingerf606f872003-03-16 03:11:04 +00001002/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Some opcodes tend to come in pairs thus making it possible to
1004 predict the second code when the first is run. For example,
1005 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1006 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 Verifying the prediction costs a single high-speed test of a register
1009 variable against a constant. If the pairing was good, then the
1010 processor's own internal branch predication has a high likelihood of
1011 success, resulting in a nearly zero-overhead transition to the
1012 next opcode. A successful prediction saves a trip through the eval-loop
1013 including its two unpredictable branches, the HAS_ARG test and the
1014 switch-case. Combined with the processor's internal branch prediction,
1015 a successful PREDICT has the effect of making the two opcodes run as if
1016 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001017
Georg Brandl86b2fb92008-07-16 03:43:04 +00001018 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 predictions turned-on and interpret the results as if some opcodes
1020 had been combined or turn-off predictions so that the opcode frequency
1021 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001022
1023 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 the CPU to record separate branch prediction information for each
1025 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001026
Raymond Hettingerf606f872003-03-16 03:11:04 +00001027*/
1028
Antoine Pitrou042b1282010-08-13 21:15:58 +00001029#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030#define PREDICT(op) if (0) goto PRED_##op
1031#define PREDICTED(op) PRED_##op:
1032#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001033#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1035#define PREDICTED(op) PRED_##op: next_instr++
1036#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001037#endif
1038
Raymond Hettingerf606f872003-03-16 03:11:04 +00001039
Guido van Rossum374a9221991-04-04 10:40:29 +00001040/* Stack manipulation macros */
1041
Martin v. Löwis18e16552006-02-15 17:27:45 +00001042/* The stack can grow at most MAXINT deep, as co_nlocals and
1043 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001044#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1045#define EMPTY() (STACK_LEVEL() == 0)
1046#define TOP() (stack_pointer[-1])
1047#define SECOND() (stack_pointer[-2])
1048#define THIRD() (stack_pointer[-3])
1049#define FOURTH() (stack_pointer[-4])
1050#define PEEK(n) (stack_pointer[-(n)])
1051#define SET_TOP(v) (stack_pointer[-1] = (v))
1052#define SET_SECOND(v) (stack_pointer[-2] = (v))
1053#define SET_THIRD(v) (stack_pointer[-3] = (v))
1054#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1055#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1056#define BASIC_STACKADJ(n) (stack_pointer += n)
1057#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1058#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001059
Guido van Rossum96a42c81992-01-12 02:29:51 +00001060#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001062 lltrace && prtrace(TOP(), "push")); \
1063 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001067 lltrace && prtrace(TOP(), "stackadj")); \
1068 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001069#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001070 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1071 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001072#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001073#define PUSH(v) BASIC_PUSH(v)
1074#define POP() BASIC_POP()
1075#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001076#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001077#endif
1078
Guido van Rossum681d79a1995-07-18 14:51:37 +00001079/* Local variable macros */
1080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001082
1083/* The SETLOCAL() macro must not DECREF the local variable in-place and
1084 then store the new value; it must copy the old value to a temporary
1085 value, then store the new value, and then DECREF the temporary value.
1086 This is because it is possible that during the DECREF the frame is
1087 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1088 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001090 GETLOCAL(i) = value; \
1091 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001092
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001093
1094#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 while (STACK_LEVEL() > (b)->b_level) { \
1096 PyObject *v = POP(); \
1097 Py_XDECREF(v); \
1098 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001099
1100#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 { \
1102 PyObject *type, *value, *traceback; \
1103 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1104 while (STACK_LEVEL() > (b)->b_level + 3) { \
1105 value = POP(); \
1106 Py_XDECREF(value); \
1107 } \
1108 type = tstate->exc_type; \
1109 value = tstate->exc_value; \
1110 traceback = tstate->exc_traceback; \
1111 tstate->exc_type = POP(); \
1112 tstate->exc_value = POP(); \
1113 tstate->exc_traceback = POP(); \
1114 Py_XDECREF(type); \
1115 Py_XDECREF(value); \
1116 Py_XDECREF(traceback); \
1117 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001118
Guido van Rossuma027efa1997-05-05 20:56:21 +00001119/* Start of code */
1120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 /* push frame */
1122 if (Py_EnterRecursiveCall(""))
1123 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (tstate->use_tracing) {
1128 if (tstate->c_tracefunc != NULL) {
1129 /* tstate->c_tracefunc, if defined, is a
1130 function that will be called on *every* entry
1131 to a code block. Its return value, if not
1132 None, is a function that will be called at
1133 the start of each executed line of code.
1134 (Actually, the function must return itself
1135 in order to continue tracing.) The trace
1136 functions are called with three arguments:
1137 a pointer to the current frame, a string
1138 indicating why the function is called, and
1139 an argument which depends on the situation.
1140 The global trace function is also called
1141 whenever an exception is detected. */
1142 if (call_trace_protected(tstate->c_tracefunc,
1143 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001144 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 /* Trace function raised an error */
1146 goto exit_eval_frame;
1147 }
1148 }
1149 if (tstate->c_profilefunc != NULL) {
1150 /* Similar for c_profilefunc, except it needn't
1151 return itself and isn't called for "line" events */
1152 if (call_trace_protected(tstate->c_profilefunc,
1153 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001154 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 /* Profile function raised an error */
1156 goto exit_eval_frame;
1157 }
1158 }
1159 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 co = f->f_code;
1162 names = co->co_names;
1163 consts = co->co_consts;
1164 fastlocals = f->f_localsplus;
1165 freevars = f->f_localsplus + co->co_nlocals;
1166 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1167 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 f->f_lasti now refers to the index of the last instruction
1170 executed. You might think this was obvious from the name, but
1171 this wasn't always true before 2.3! PyFrame_New now sets
1172 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1173 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1174 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001175 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1176 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 When the PREDICT() macros are enabled, some opcode pairs follow in
1179 direct succession without updating f->f_lasti. A successful
1180 prediction effectively links the two codes together as if they
1181 were a single new opcode; accordingly,f->f_lasti will point to
1182 the first code in the pair (for instance, GET_ITER followed by
1183 FOR_ITER is effectively a single opcode and f->f_lasti will point
1184 at to the beginning of the combined pair.)
1185 */
1186 next_instr = first_instr + f->f_lasti + 1;
1187 stack_pointer = f->f_stacktop;
1188 assert(stack_pointer != NULL);
1189 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001190 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001191
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001192 if (co->co_flags & CO_GENERATOR) {
1193 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 /* We were in an except handler when we left,
1195 restore the exception state which was put aside
1196 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001197 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001199 else
1200 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001202
Tim Peters5ca576e2001-06-18 22:08:13 +00001203#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001204 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001205#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 if (throwflag) /* support for generator.throw() */
1210 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001211
Victor Stinnerace47d72013-07-18 01:41:08 +02001212#ifdef Py_DEBUG
1213 /* PyEval_EvalFrameEx() must not be called with an exception set,
1214 because it may clear it (directly or indirectly) and so the
1215 caller looses its exception */
1216 assert(!PyErr_Occurred());
1217#endif
1218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001220#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (inst1 == 0) {
1222 /* Almost surely, the opcode executed a break
1223 or a continue, preventing inst1 from being set
1224 on the way out of the loop.
1225 */
1226 READ_TIMESTAMP(inst1);
1227 loop1 = inst1;
1228 }
1229 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1230 intr0, intr1);
1231 ticked = 0;
1232 inst1 = 0;
1233 intr0 = 0;
1234 intr1 = 0;
1235 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001236#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1238 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001239 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 /* Do periodic things. Doing this every time through
1242 the loop would add too much overhead, so we do it
1243 only every Nth instruction. We also do it if
1244 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1245 event needs attention (e.g. a signal handler or
1246 async I/O handler); see Py_AddPendingCall() and
1247 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1250 if (*next_instr == SETUP_FINALLY) {
1251 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001252 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 goto fast_next_opcode;
1254 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001255#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001259 if (Py_MakePendingCalls() < 0)
1260 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001262#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001263 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* Give another thread a chance */
1265 if (PyThreadState_Swap(NULL) != tstate)
1266 Py_FatalError("ceval: tstate mix-up");
1267 drop_gil(tstate);
1268
1269 /* Other threads may run now */
1270
1271 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001272
1273 /* Check if we should make a quick exit. */
1274 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1275 drop_gil(tstate);
1276 PyThread_exit_thread();
1277 }
1278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (PyThreadState_Swap(tstate) != NULL)
1280 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001282#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 /* Check for asynchronous exceptions. */
1284 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 tstate->async_exc = NULL;
1287 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 PyErr_SetNone(exc);
1289 Py_DECREF(exc);
1290 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
1292 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 fast_next_opcode:
1295 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001300 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001301 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 /* see maybe_call_line_trace
1303 for expository comments */
1304 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 err = maybe_call_line_trace(tstate->c_tracefunc,
1307 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001308 tstate, f,
1309 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* Reload possibly changed frame fields */
1311 JUMPTO(f->f_lasti);
1312 if (f->f_stacktop != NULL) {
1313 stack_pointer = f->f_stacktop;
1314 f->f_stacktop = NULL;
1315 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 opcode = NEXTOP();
1324 oparg = 0; /* allows oparg to be stored in a register because
1325 it doesn't have to be remembered across a full loop */
1326 if (HAS_ARG(opcode))
1327 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001328 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001329#ifdef DYNAMIC_EXECUTION_PROFILE
1330#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 dxpairs[lastopcode][opcode]++;
1332 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001333#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001335#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001336
Guido van Rossum96a42c81992-01-12 02:29:51 +00001337#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (lltrace) {
1341 if (HAS_ARG(opcode)) {
1342 printf("%d: %d, %d\n",
1343 f->f_lasti, opcode, oparg);
1344 }
1345 else {
1346 printf("%d: %d\n",
1347 f->f_lasti, opcode);
1348 }
1349 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001350#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 /* Main switch on opcode */
1353 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 /* BEWARE!
1358 It is essential that any operation that fails sets either
1359 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1360 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 TARGET(NOP)
1363 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001364
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 TARGET(LOAD_FAST) {
1366 PyObject *value = GETLOCAL(oparg);
1367 if (value == NULL) {
1368 format_exc_check_arg(PyExc_UnboundLocalError,
1369 UNBOUNDLOCAL_ERROR_MSG,
1370 PyTuple_GetItem(co->co_varnames, oparg));
1371 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 Py_INCREF(value);
1374 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 }
1377
1378 TARGET(LOAD_CONST) {
1379 PyObject *value = GETITEM(consts, oparg);
1380 Py_INCREF(value);
1381 PUSH(value);
1382 FAST_DISPATCH();
1383 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 TARGET(STORE_FAST) {
1387 PyObject *value = POP();
1388 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001392 TARGET(POP_TOP) {
1393 PyObject *value = POP();
1394 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 TARGET(ROT_TWO) {
1399 PyObject *top = TOP();
1400 PyObject *second = SECOND();
1401 SET_TOP(second);
1402 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 TARGET(ROT_THREE) {
1407 PyObject *top = TOP();
1408 PyObject *second = SECOND();
1409 PyObject *third = THIRD();
1410 SET_TOP(second);
1411 SET_SECOND(third);
1412 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001416 TARGET(DUP_TOP) {
1417 PyObject *top = TOP();
1418 Py_INCREF(top);
1419 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 TARGET(DUP_TOP_TWO) {
1424 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001426 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001428 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 SET_TOP(top);
1430 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001431 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001433
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 TARGET(UNARY_POSITIVE) {
1435 PyObject *value = TOP();
1436 PyObject *res = PyNumber_Positive(value);
1437 Py_DECREF(value);
1438 SET_TOP(res);
1439 if (res == NULL)
1440 goto error;
1441 DISPATCH();
1442 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 TARGET(UNARY_NEGATIVE) {
1445 PyObject *value = TOP();
1446 PyObject *res = PyNumber_Negative(value);
1447 Py_DECREF(value);
1448 SET_TOP(res);
1449 if (res == NULL)
1450 goto error;
1451 DISPATCH();
1452 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 TARGET(UNARY_NOT) {
1455 PyObject *value = TOP();
1456 int err = PyObject_IsTrue(value);
1457 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (err == 0) {
1459 Py_INCREF(Py_True);
1460 SET_TOP(Py_True);
1461 DISPATCH();
1462 }
1463 else if (err > 0) {
1464 Py_INCREF(Py_False);
1465 SET_TOP(Py_False);
1466 err = 0;
1467 DISPATCH();
1468 }
1469 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 goto error;
1471 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 TARGET(UNARY_INVERT) {
1474 PyObject *value = TOP();
1475 PyObject *res = PyNumber_Invert(value);
1476 Py_DECREF(value);
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_POWER) {
1484 PyObject *exp = POP();
1485 PyObject *base = TOP();
1486 PyObject *res = PyNumber_Power(base, exp, Py_None);
1487 Py_DECREF(base);
1488 Py_DECREF(exp);
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_MULTIPLY) {
1496 PyObject *right = POP();
1497 PyObject *left = TOP();
1498 PyObject *res = PyNumber_Multiply(left, right);
1499 Py_DECREF(left);
1500 Py_DECREF(right);
1501 SET_TOP(res);
1502 if (res == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Petersond51374e2014-04-09 23:55:56 -04001507 TARGET(BINARY_MATRIX_MULTIPLY) {
1508 PyObject *right = POP();
1509 PyObject *left = TOP();
1510 PyObject *res = PyNumber_MatrixMultiply(left, right);
1511 Py_DECREF(left);
1512 Py_DECREF(right);
1513 SET_TOP(res);
1514 if (res == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
1518
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 TARGET(BINARY_TRUE_DIVIDE) {
1520 PyObject *divisor = POP();
1521 PyObject *dividend = TOP();
1522 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1523 Py_DECREF(dividend);
1524 Py_DECREF(divisor);
1525 SET_TOP(quotient);
1526 if (quotient == NULL)
1527 goto error;
1528 DISPATCH();
1529 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001530
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 TARGET(BINARY_FLOOR_DIVIDE) {
1532 PyObject *divisor = POP();
1533 PyObject *dividend = TOP();
1534 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1535 Py_DECREF(dividend);
1536 Py_DECREF(divisor);
1537 SET_TOP(quotient);
1538 if (quotient == NULL)
1539 goto error;
1540 DISPATCH();
1541 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001542
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 TARGET(BINARY_MODULO) {
1544 PyObject *divisor = POP();
1545 PyObject *dividend = TOP();
1546 PyObject *res = PyUnicode_CheckExact(dividend) ?
1547 PyUnicode_Format(dividend, divisor) :
1548 PyNumber_Remainder(dividend, divisor);
1549 Py_DECREF(divisor);
1550 Py_DECREF(dividend);
1551 SET_TOP(res);
1552 if (res == NULL)
1553 goto error;
1554 DISPATCH();
1555 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001556
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001557 TARGET(BINARY_ADD) {
1558 PyObject *right = POP();
1559 PyObject *left = TOP();
1560 PyObject *sum;
1561 if (PyUnicode_CheckExact(left) &&
1562 PyUnicode_CheckExact(right)) {
1563 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001564 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001565 }
1566 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001567 sum = PyNumber_Add(left, right);
1568 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001569 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001570 Py_DECREF(right);
1571 SET_TOP(sum);
1572 if (sum == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
1576
1577 TARGET(BINARY_SUBTRACT) {
1578 PyObject *right = POP();
1579 PyObject *left = TOP();
1580 PyObject *diff = PyNumber_Subtract(left, right);
1581 Py_DECREF(right);
1582 Py_DECREF(left);
1583 SET_TOP(diff);
1584 if (diff == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
1588
1589 TARGET(BINARY_SUBSCR) {
1590 PyObject *sub = POP();
1591 PyObject *container = TOP();
1592 PyObject *res = PyObject_GetItem(container, sub);
1593 Py_DECREF(container);
1594 Py_DECREF(sub);
1595 SET_TOP(res);
1596 if (res == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
1600
1601 TARGET(BINARY_LSHIFT) {
1602 PyObject *right = POP();
1603 PyObject *left = TOP();
1604 PyObject *res = PyNumber_Lshift(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_RSHIFT) {
1614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_Rshift(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_AND) {
1626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_And(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(BINARY_XOR) {
1638 PyObject *right = POP();
1639 PyObject *left = TOP();
1640 PyObject *res = PyNumber_Xor(left, right);
1641 Py_DECREF(left);
1642 Py_DECREF(right);
1643 SET_TOP(res);
1644 if (res == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
1648
1649 TARGET(BINARY_OR) {
1650 PyObject *right = POP();
1651 PyObject *left = TOP();
1652 PyObject *res = PyNumber_Or(left, right);
1653 Py_DECREF(left);
1654 Py_DECREF(right);
1655 SET_TOP(res);
1656 if (res == NULL)
1657 goto error;
1658 DISPATCH();
1659 }
1660
1661 TARGET(LIST_APPEND) {
1662 PyObject *v = POP();
1663 PyObject *list = PEEK(oparg);
1664 int err;
1665 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 if (err != 0)
1668 goto error;
1669 PREDICT(JUMP_ABSOLUTE);
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 TARGET(SET_ADD) {
1674 PyObject *v = POP();
1675 PyObject *set = stack_pointer[-oparg];
1676 int err;
1677 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 if (err != 0)
1680 goto error;
1681 PREDICT(JUMP_ABSOLUTE);
1682 DISPATCH();
1683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 TARGET(INPLACE_POWER) {
1686 PyObject *exp = POP();
1687 PyObject *base = TOP();
1688 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1689 Py_DECREF(base);
1690 Py_DECREF(exp);
1691 SET_TOP(res);
1692 if (res == 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_MULTIPLY) {
1698 PyObject *right = POP();
1699 PyObject *left = TOP();
1700 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1701 Py_DECREF(left);
1702 Py_DECREF(right);
1703 SET_TOP(res);
1704 if (res == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Benjamin Petersond51374e2014-04-09 23:55:56 -04001709 TARGET(INPLACE_MATRIX_MULTIPLY) {
1710 PyObject *right = POP();
1711 PyObject *left = TOP();
1712 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1713 Py_DECREF(left);
1714 Py_DECREF(right);
1715 SET_TOP(res);
1716 if (res == NULL)
1717 goto error;
1718 DISPATCH();
1719 }
1720
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 TARGET(INPLACE_TRUE_DIVIDE) {
1722 PyObject *divisor = POP();
1723 PyObject *dividend = TOP();
1724 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1725 Py_DECREF(dividend);
1726 Py_DECREF(divisor);
1727 SET_TOP(quotient);
1728 if (quotient == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 TARGET(INPLACE_FLOOR_DIVIDE) {
1734 PyObject *divisor = POP();
1735 PyObject *dividend = TOP();
1736 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1737 Py_DECREF(dividend);
1738 Py_DECREF(divisor);
1739 SET_TOP(quotient);
1740 if (quotient == NULL)
1741 goto error;
1742 DISPATCH();
1743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 TARGET(INPLACE_MODULO) {
1746 PyObject *right = POP();
1747 PyObject *left = TOP();
1748 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1749 Py_DECREF(left);
1750 Py_DECREF(right);
1751 SET_TOP(mod);
1752 if (mod == NULL)
1753 goto error;
1754 DISPATCH();
1755 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001756
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 TARGET(INPLACE_ADD) {
1758 PyObject *right = POP();
1759 PyObject *left = TOP();
1760 PyObject *sum;
1761 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1762 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001763 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001764 }
1765 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001766 sum = PyNumber_InPlaceAdd(left, right);
1767 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001768 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 Py_DECREF(right);
1770 SET_TOP(sum);
1771 if (sum == 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_SUBTRACT) {
1777 PyObject *right = POP();
1778 PyObject *left = TOP();
1779 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1780 Py_DECREF(left);
1781 Py_DECREF(right);
1782 SET_TOP(diff);
1783 if (diff == 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_LSHIFT) {
1789 PyObject *right = POP();
1790 PyObject *left = TOP();
1791 PyObject *res = PyNumber_InPlaceLshift(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_RSHIFT) {
1801 PyObject *right = POP();
1802 PyObject *left = TOP();
1803 PyObject *res = PyNumber_InPlaceRshift(left, right);
1804 Py_DECREF(left);
1805 Py_DECREF(right);
1806 SET_TOP(res);
1807 if (res == NULL)
1808 goto error;
1809 DISPATCH();
1810 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 TARGET(INPLACE_AND) {
1813 PyObject *right = POP();
1814 PyObject *left = TOP();
1815 PyObject *res = PyNumber_InPlaceAnd(left, right);
1816 Py_DECREF(left);
1817 Py_DECREF(right);
1818 SET_TOP(res);
1819 if (res == NULL)
1820 goto error;
1821 DISPATCH();
1822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001823
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 TARGET(INPLACE_XOR) {
1825 PyObject *right = POP();
1826 PyObject *left = TOP();
1827 PyObject *res = PyNumber_InPlaceXor(left, right);
1828 Py_DECREF(left);
1829 Py_DECREF(right);
1830 SET_TOP(res);
1831 if (res == NULL)
1832 goto error;
1833 DISPATCH();
1834 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001835
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 TARGET(INPLACE_OR) {
1837 PyObject *right = POP();
1838 PyObject *left = TOP();
1839 PyObject *res = PyNumber_InPlaceOr(left, right);
1840 Py_DECREF(left);
1841 Py_DECREF(right);
1842 SET_TOP(res);
1843 if (res == NULL)
1844 goto error;
1845 DISPATCH();
1846 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001847
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 TARGET(STORE_SUBSCR) {
1849 PyObject *sub = TOP();
1850 PyObject *container = SECOND();
1851 PyObject *v = THIRD();
1852 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 STACKADJ(-3);
1854 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 Py_DECREF(container);
1858 Py_DECREF(sub);
1859 if (err != 0)
1860 goto error;
1861 DISPATCH();
1862 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001863
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 TARGET(DELETE_SUBSCR) {
1865 PyObject *sub = TOP();
1866 PyObject *container = SECOND();
1867 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 STACKADJ(-2);
1869 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001870 err = PyObject_DelItem(container, sub);
1871 Py_DECREF(container);
1872 Py_DECREF(sub);
1873 if (err != 0)
1874 goto error;
1875 DISPATCH();
1876 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001877
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001879 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001881 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001882 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 PyErr_SetString(PyExc_RuntimeError,
1885 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 Py_DECREF(value);
1887 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001889 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 Py_DECREF(value);
1891 if (res == NULL)
1892 goto error;
1893 Py_DECREF(res);
1894 DISPATCH();
1895 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001896
Thomas Wouters434d0822000-08-24 20:11:32 +00001897#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001899#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 TARGET(RAISE_VARARGS) {
1901 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 switch (oparg) {
1903 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 if (do_raise(exc, cause)) {
1909 why = WHY_EXCEPTION;
1910 goto fast_block_end;
1911 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 break;
1913 default:
1914 PyErr_SetString(PyExc_SystemError,
1915 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 break;
1917 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001918 goto error;
1919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001920
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 retval = POP();
1923 why = WHY_RETURN;
1924 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001926
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 TARGET(YIELD_FROM) {
1928 PyObject *v = POP();
1929 PyObject *reciever = TOP();
1930 int err;
1931 if (PyGen_CheckExact(reciever)) {
1932 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001933 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001934 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 if (v == Py_None)
1936 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001937 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04001938 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 Py_DECREF(v);
1941 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001942 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001943 if (tstate->c_tracefunc != NULL
1944 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001945 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001946 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 if (err < 0)
1948 goto error;
1949 Py_DECREF(reciever);
1950 SET_TOP(val);
1951 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001952 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001953 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001954 f->f_stacktop = stack_pointer;
1955 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001956 /* and repeat... */
1957 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001958 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001960
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 retval = POP();
1963 f->f_stacktop = stack_pointer;
1964 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001967
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 TARGET(POP_EXCEPT) {
1969 PyTryBlock *b = PyFrame_BlockPop(f);
1970 if (b->b_type != EXCEPT_HANDLER) {
1971 PyErr_SetString(PyExc_SystemError,
1972 "popped block is not an except handler");
1973 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001977 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001978
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 TARGET(POP_BLOCK) {
1980 PyTryBlock *b = PyFrame_BlockPop(f);
1981 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001985 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 TARGET(END_FINALLY) {
1987 PyObject *status = POP();
1988 if (PyLong_Check(status)) {
1989 why = (enum why_code) PyLong_AS_LONG(status);
1990 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 if (why == WHY_RETURN ||
1992 why == WHY_CONTINUE)
1993 retval = POP();
1994 if (why == WHY_SILENCED) {
1995 /* An exception was silenced by 'with', we must
1996 manually unwind the EXCEPT_HANDLER block which was
1997 created when the exception was caught, otherwise
1998 the stack will be in an inconsistent state. */
1999 PyTryBlock *b = PyFrame_BlockPop(f);
2000 assert(b->b_type == EXCEPT_HANDLER);
2001 UNWIND_EXCEPT_HANDLER(b);
2002 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 Py_DECREF(status);
2004 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002005 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002006 Py_DECREF(status);
2007 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 else if (PyExceptionClass_Check(status)) {
2010 PyObject *exc = POP();
2011 PyObject *tb = POP();
2012 PyErr_Restore(status, exc, tb);
2013 why = WHY_EXCEPTION;
2014 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 PyErr_SetString(PyExc_SystemError,
2018 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 Py_DECREF(status);
2020 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 Py_DECREF(status);
2023 DISPATCH();
2024 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002027 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002028
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002030 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2032 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002033 PyErr_SetString(PyExc_NameError,
2034 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002036 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002037 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002038 }
2039 else {
2040 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2041 if (build_class_str == NULL)
2042 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2044 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002045 if (PyErr_ExceptionMatches(PyExc_KeyError))
2046 PyErr_SetString(PyExc_NameError,
2047 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002049 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002051 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002052 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002053 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002054
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 TARGET(STORE_NAME) {
2056 PyObject *name = GETITEM(names, oparg);
2057 PyObject *v = POP();
2058 PyObject *ns = f->f_locals;
2059 int err;
2060 if (ns == NULL) {
2061 PyErr_Format(PyExc_SystemError,
2062 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 if (PyDict_CheckExact(ns))
2067 err = PyDict_SetItem(ns, name, v);
2068 else
2069 err = PyObject_SetItem(ns, name, v);
2070 Py_DECREF(v);
2071 if (err != 0)
2072 goto error;
2073 DISPATCH();
2074 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002075
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 TARGET(DELETE_NAME) {
2077 PyObject *name = GETITEM(names, oparg);
2078 PyObject *ns = f->f_locals;
2079 int err;
2080 if (ns == NULL) {
2081 PyErr_Format(PyExc_SystemError,
2082 "no locals when deleting %R", name);
2083 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 err = PyObject_DelItem(ns, name);
2086 if (err != 0) {
2087 format_exc_check_arg(PyExc_NameError,
2088 NAME_ERROR_MSG,
2089 name);
2090 goto error;
2091 }
2092 DISPATCH();
2093 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 TARGET(UNPACK_SEQUENCE) {
2097 PyObject *seq = POP(), *item, **items;
2098 if (PyTuple_CheckExact(seq) &&
2099 PyTuple_GET_SIZE(seq) == oparg) {
2100 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 item = items[oparg];
2103 Py_INCREF(item);
2104 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 } else if (PyList_CheckExact(seq) &&
2107 PyList_GET_SIZE(seq) == oparg) {
2108 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 item = items[oparg];
2111 Py_INCREF(item);
2112 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 stack_pointer + oparg)) {
2116 STACKADJ(oparg);
2117 } else {
2118 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 Py_DECREF(seq);
2120 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002123 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002125
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 TARGET(UNPACK_EX) {
2127 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2128 PyObject *seq = POP();
2129
2130 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2131 stack_pointer + totalargs)) {
2132 stack_pointer += totalargs;
2133 } else {
2134 Py_DECREF(seq);
2135 goto error;
2136 }
2137 Py_DECREF(seq);
2138 DISPATCH();
2139 }
2140
2141 TARGET(STORE_ATTR) {
2142 PyObject *name = GETITEM(names, oparg);
2143 PyObject *owner = TOP();
2144 PyObject *v = SECOND();
2145 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 Py_DECREF(owner);
2150 if (err != 0)
2151 goto error;
2152 DISPATCH();
2153 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002154
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 TARGET(DELETE_ATTR) {
2156 PyObject *name = GETITEM(names, oparg);
2157 PyObject *owner = POP();
2158 int err;
2159 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2160 Py_DECREF(owner);
2161 if (err != 0)
2162 goto error;
2163 DISPATCH();
2164 }
2165
2166 TARGET(STORE_GLOBAL) {
2167 PyObject *name = GETITEM(names, oparg);
2168 PyObject *v = POP();
2169 int err;
2170 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 if (err != 0)
2173 goto error;
2174 DISPATCH();
2175 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002176
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 TARGET(DELETE_GLOBAL) {
2178 PyObject *name = GETITEM(names, oparg);
2179 int err;
2180 err = PyDict_DelItem(f->f_globals, name);
2181 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002183 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002185 }
2186 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002188
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 TARGET(LOAD_NAME) {
2190 PyObject *name = GETITEM(names, oparg);
2191 PyObject *locals = f->f_locals;
2192 PyObject *v;
2193 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 "no locals when loading %R", name);
2196 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 if (PyDict_CheckExact(locals)) {
2199 v = PyDict_GetItem(locals, name);
2200 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 }
2202 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 v = PyObject_GetItem(locals, name);
Antoine Pitrou1cfa0ba2013-10-07 20:40:59 +02002204 if (v == NULL && _PyErr_OCCURRED()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002205 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2206 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 PyErr_Clear();
2208 }
2209 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 if (v == NULL) {
2211 v = PyDict_GetItem(f->f_globals, name);
2212 Py_XINCREF(v);
2213 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002214 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002215 v = PyDict_GetItem(f->f_builtins, name);
2216 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002217 format_exc_check_arg(
2218 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 NAME_ERROR_MSG, name);
2220 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002221 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002223 }
2224 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 v = PyObject_GetItem(f->f_builtins, name);
2226 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002227 if (PyErr_ExceptionMatches(PyExc_KeyError))
2228 format_exc_check_arg(
2229 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 NAME_ERROR_MSG, name);
2231 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002232 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002233 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002239
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 TARGET(LOAD_GLOBAL) {
2241 PyObject *name = GETITEM(names, oparg);
2242 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002243 if (PyDict_CheckExact(f->f_globals)
2244 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002246 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 name);
2248 if (v == NULL) {
Antoine Pitrou59c900d2013-10-07 20:38:51 +02002249 if (!_PyErr_OCCURRED())
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002250 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002251 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002254 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002256 else {
2257 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 v = PyObject_GetItem(f->f_globals, name);
2259 if (v == NULL) {
2260 v = PyObject_GetItem(f->f_builtins, name);
2261 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002262 if (PyErr_ExceptionMatches(PyExc_KeyError))
2263 format_exc_check_arg(
2264 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002265 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002267 }
2268 }
2269 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002273
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 TARGET(DELETE_FAST) {
2275 PyObject *v = GETLOCAL(oparg);
2276 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 SETLOCAL(oparg, NULL);
2278 DISPATCH();
2279 }
2280 format_exc_check_arg(
2281 PyExc_UnboundLocalError,
2282 UNBOUNDLOCAL_ERROR_MSG,
2283 PyTuple_GetItem(co->co_varnames, oparg)
2284 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 goto error;
2286 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002287
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 TARGET(DELETE_DEREF) {
2289 PyObject *cell = freevars[oparg];
2290 if (PyCell_GET(cell) != NULL) {
2291 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002292 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002293 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002294 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 goto error;
2296 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002297
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 TARGET(LOAD_CLOSURE) {
2299 PyObject *cell = freevars[oparg];
2300 Py_INCREF(cell);
2301 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002304
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002305 TARGET(LOAD_CLASSDEREF) {
2306 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002307 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002308 assert(locals);
2309 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2310 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2311 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2312 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2313 if (PyDict_CheckExact(locals)) {
2314 value = PyDict_GetItem(locals, name);
2315 Py_XINCREF(value);
2316 }
2317 else {
2318 value = PyObject_GetItem(locals, name);
2319 if (value == NULL && PyErr_Occurred()) {
2320 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2321 goto error;
2322 PyErr_Clear();
2323 }
2324 }
2325 if (!value) {
2326 PyObject *cell = freevars[oparg];
2327 value = PyCell_GET(cell);
2328 if (value == NULL) {
2329 format_exc_unbound(co, oparg);
2330 goto error;
2331 }
2332 Py_INCREF(value);
2333 }
2334 PUSH(value);
2335 DISPATCH();
2336 }
2337
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 TARGET(LOAD_DEREF) {
2339 PyObject *cell = freevars[oparg];
2340 PyObject *value = PyCell_GET(cell);
2341 if (value == NULL) {
2342 format_exc_unbound(co, oparg);
2343 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 Py_INCREF(value);
2346 PUSH(value);
2347 DISPATCH();
2348 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002349
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 TARGET(STORE_DEREF) {
2351 PyObject *v = POP();
2352 PyObject *cell = freevars[oparg];
2353 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 DISPATCH();
2356 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002357
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002358 TARGET(BUILD_TUPLE) {
2359 PyObject *tup = PyTuple_New(oparg);
2360 if (tup == NULL)
2361 goto error;
2362 while (--oparg >= 0) {
2363 PyObject *item = POP();
2364 PyTuple_SET_ITEM(tup, oparg, item);
2365 }
2366 PUSH(tup);
2367 DISPATCH();
2368 }
2369
2370 TARGET(BUILD_LIST) {
2371 PyObject *list = PyList_New(oparg);
2372 if (list == NULL)
2373 goto error;
2374 while (--oparg >= 0) {
2375 PyObject *item = POP();
2376 PyList_SET_ITEM(list, oparg, item);
2377 }
2378 PUSH(list);
2379 DISPATCH();
2380 }
2381
2382 TARGET(BUILD_SET) {
2383 PyObject *set = PySet_New(NULL);
2384 int err = 0;
2385 if (set == NULL)
2386 goto error;
2387 while (--oparg >= 0) {
2388 PyObject *item = POP();
2389 if (err == 0)
2390 err = PySet_Add(set, item);
2391 Py_DECREF(item);
2392 }
2393 if (err != 0) {
2394 Py_DECREF(set);
2395 goto error;
2396 }
2397 PUSH(set);
2398 DISPATCH();
2399 }
2400
2401 TARGET(BUILD_MAP) {
2402 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2403 if (map == NULL)
2404 goto error;
2405 PUSH(map);
2406 DISPATCH();
2407 }
2408
2409 TARGET(STORE_MAP) {
2410 PyObject *key = TOP();
2411 PyObject *value = SECOND();
2412 PyObject *map = THIRD();
2413 int err;
2414 STACKADJ(-2);
2415 assert(PyDict_CheckExact(map));
2416 err = PyDict_SetItem(map, key, value);
2417 Py_DECREF(value);
2418 Py_DECREF(key);
2419 if (err != 0)
2420 goto error;
2421 DISPATCH();
2422 }
2423
2424 TARGET(MAP_ADD) {
2425 PyObject *key = TOP();
2426 PyObject *value = SECOND();
2427 PyObject *map;
2428 int err;
2429 STACKADJ(-2);
2430 map = stack_pointer[-oparg]; /* dict */
2431 assert(PyDict_CheckExact(map));
2432 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2433 Py_DECREF(value);
2434 Py_DECREF(key);
2435 if (err != 0)
2436 goto error;
2437 PREDICT(JUMP_ABSOLUTE);
2438 DISPATCH();
2439 }
2440
2441 TARGET(LOAD_ATTR) {
2442 PyObject *name = GETITEM(names, oparg);
2443 PyObject *owner = TOP();
2444 PyObject *res = PyObject_GetAttr(owner, name);
2445 Py_DECREF(owner);
2446 SET_TOP(res);
2447 if (res == NULL)
2448 goto error;
2449 DISPATCH();
2450 }
2451
2452 TARGET(COMPARE_OP) {
2453 PyObject *right = POP();
2454 PyObject *left = TOP();
2455 PyObject *res = cmp_outcome(oparg, left, right);
2456 Py_DECREF(left);
2457 Py_DECREF(right);
2458 SET_TOP(res);
2459 if (res == NULL)
2460 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 PREDICT(POP_JUMP_IF_FALSE);
2462 PREDICT(POP_JUMP_IF_TRUE);
2463 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 TARGET(IMPORT_NAME) {
2467 _Py_IDENTIFIER(__import__);
2468 PyObject *name = GETITEM(names, oparg);
2469 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002470 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 if (func == NULL) {
2472 PyErr_SetString(PyExc_ImportError,
2473 "__import__ not found");
2474 goto error;
2475 }
2476 Py_INCREF(func);
2477 from = POP();
2478 level = TOP();
2479 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2480 args = PyTuple_Pack(5,
2481 name,
2482 f->f_globals,
2483 f->f_locals == NULL ?
2484 Py_None : f->f_locals,
2485 from,
2486 level);
2487 else
2488 args = PyTuple_Pack(4,
2489 name,
2490 f->f_globals,
2491 f->f_locals == NULL ?
2492 Py_None : f->f_locals,
2493 from);
2494 Py_DECREF(level);
2495 Py_DECREF(from);
2496 if (args == NULL) {
2497 Py_DECREF(func);
2498 STACKADJ(-1);
2499 goto error;
2500 }
2501 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002502 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 READ_TIMESTAMP(intr1);
2504 Py_DECREF(args);
2505 Py_DECREF(func);
2506 SET_TOP(res);
2507 if (res == NULL)
2508 goto error;
2509 DISPATCH();
2510 }
2511
2512 TARGET(IMPORT_STAR) {
2513 PyObject *from = POP(), *locals;
2514 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002515 if (PyFrame_FastToLocalsWithError(f) < 0)
2516 goto error;
2517
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002518 locals = f->f_locals;
2519 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 PyErr_SetString(PyExc_SystemError,
2521 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002522 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 }
2524 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002526 READ_TIMESTAMP(intr1);
2527 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 Py_DECREF(from);
2529 if (err != 0)
2530 goto error;
2531 DISPATCH();
2532 }
Guido van Rossum25831651993-05-19 14:50:45 +00002533
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 TARGET(IMPORT_FROM) {
2535 PyObject *name = GETITEM(names, oparg);
2536 PyObject *from = TOP();
2537 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 PUSH(res);
2542 if (res == NULL)
2543 goto error;
2544 DISPATCH();
2545 }
Thomas Wouters52152252000-08-17 22:55:00 +00002546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002547 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002548 JUMPBY(oparg);
2549 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 TARGET(POP_JUMP_IF_FALSE) {
2554 PyObject *cond = POP();
2555 int err;
2556 if (cond == Py_True) {
2557 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 FAST_DISPATCH();
2559 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 if (cond == Py_False) {
2561 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 JUMPTO(oparg);
2563 FAST_DISPATCH();
2564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 err = PyObject_IsTrue(cond);
2566 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 if (err > 0)
2568 err = 0;
2569 else if (err == 0)
2570 JUMPTO(oparg);
2571 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002572 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002576 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 TARGET(POP_JUMP_IF_TRUE) {
2578 PyObject *cond = POP();
2579 int err;
2580 if (cond == Py_False) {
2581 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 FAST_DISPATCH();
2583 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 if (cond == Py_True) {
2585 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 JUMPTO(oparg);
2587 FAST_DISPATCH();
2588 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 err = PyObject_IsTrue(cond);
2590 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 if (err > 0) {
2592 err = 0;
2593 JUMPTO(oparg);
2594 }
2595 else if (err == 0)
2596 ;
2597 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002599 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002601
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 TARGET(JUMP_IF_FALSE_OR_POP) {
2603 PyObject *cond = TOP();
2604 int err;
2605 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002607 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002608 FAST_DISPATCH();
2609 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002611 JUMPTO(oparg);
2612 FAST_DISPATCH();
2613 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002614 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002615 if (err > 0) {
2616 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002617 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 err = 0;
2619 }
2620 else if (err == 0)
2621 JUMPTO(oparg);
2622 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002626
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002627 TARGET(JUMP_IF_TRUE_OR_POP) {
2628 PyObject *cond = TOP();
2629 int err;
2630 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002632 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 FAST_DISPATCH();
2634 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 JUMPTO(oparg);
2637 FAST_DISPATCH();
2638 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 if (err > 0) {
2641 err = 0;
2642 JUMPTO(oparg);
2643 }
2644 else if (err == 0) {
2645 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 }
2648 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002656#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 /* Enabling this path speeds-up all while and for-loops by bypassing
2658 the per-loop checks for signals. By default, this should be turned-off
2659 because it prevents detection of a control-break in tight loops like
2660 "while 1: pass". Compile with this option turned-on when you need
2661 the speed-up and do not need break checking inside tight loops (ones
2662 that contain only instructions ending with FAST_DISPATCH).
2663 */
2664 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002665#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002667#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002669
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 PyObject *iterable = TOP();
2673 PyObject *iter = PyObject_GetIter(iterable);
2674 Py_DECREF(iterable);
2675 SET_TOP(iter);
2676 if (iter == NULL)
2677 goto error;
2678 PREDICT(FOR_ITER);
2679 DISPATCH();
2680 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002683 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 PyObject *iter = TOP();
2686 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2687 if (next != NULL) {
2688 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 PREDICT(STORE_FAST);
2690 PREDICT(UNPACK_SEQUENCE);
2691 DISPATCH();
2692 }
2693 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2695 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002696 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002697 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 PyErr_Clear();
2699 }
2700 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 STACKADJ(-1);
2702 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 JUMPBY(oparg);
2704 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002706
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002707 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 why = WHY_BREAK;
2709 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002711
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002714 if (retval == NULL)
2715 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 why = WHY_CONTINUE;
2717 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002718 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2721 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2722 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 /* NOTE: If you add any new block-setup opcodes that
2725 are not try/except/finally handlers, you may need
2726 to update the PyGen_NeedsFinalizing() function.
2727 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2730 STACK_LEVEL());
2731 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002733
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002735 _Py_IDENTIFIER(__exit__);
2736 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 PyObject *mgr = TOP();
2738 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2739 PyObject *res;
2740 if (exit == NULL)
2741 goto error;
2742 SET_TOP(exit);
2743 enter = special_lookup(mgr, &PyId___enter__);
2744 Py_DECREF(mgr);
2745 if (enter == NULL)
2746 goto error;
2747 res = PyObject_CallFunctionObjArgs(enter, NULL);
2748 Py_DECREF(enter);
2749 if (res == NULL)
2750 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 /* Setup the finally block before pushing the result
2752 of __enter__ on the stack. */
2753 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2754 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002755
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 DISPATCH();
2758 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002759
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002760 TARGET(WITH_CLEANUP) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002761 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 how/why we entered the finally clause:
2763 - TOP = None
2764 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2765 - TOP = WHY_*; no retval below it
2766 - (TOP, SECOND, THIRD) = exc_info()
2767 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2768 Below them is EXIT, the context.__exit__ bound method.
2769 In the last case, we must call
2770 EXIT(TOP, SECOND, THIRD)
2771 otherwise we must call
2772 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002773
Benjamin Peterson8f169482013-10-29 22:25:06 -04002774 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002776 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 In addition, if the stack represents an exception,
2780 *and* the function call returns a 'true' value, we
2781 push WHY_SILENCED onto the stack. END_FINALLY will
2782 then not re-raise the exception. (But non-local
2783 gotos should still be resumed.)
2784 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2788 int err;
2789 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 (void)POP();
2791 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 else if (PyLong_Check(exc)) {
2795 STACKADJ(-1);
2796 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 case WHY_RETURN:
2798 case WHY_CONTINUE:
2799 /* Retval in TOP. */
2800 exit_func = SECOND();
2801 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 break;
2804 default:
2805 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 break;
2808 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 }
2811 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 val = SECOND();
2815 tb = THIRD();
2816 tp2 = FOURTH();
2817 exc2 = PEEK(5);
2818 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 SET_VALUE(7, tb2);
2821 SET_VALUE(6, exc2);
2822 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2824 SET_FOURTH(NULL);
2825 /* We just shifted the stack down, so we have
2826 to tell the except handler block that the
2827 values are lower than it expects. */
2828 block = &f->f_blockstack[f->f_iblock - 1];
2829 assert(block->b_type == EXCEPT_HANDLER);
2830 block->b_level--;
2831 }
2832 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 if (res == NULL)
2836 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002837
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 if (exc != Py_None)
2839 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 else
2841 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 else if (err > 0) {
2847 err = 0;
2848 /* There was an exception and a True return */
2849 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2850 }
2851 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002852 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002854
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 TARGET(CALL_FUNCTION) {
2856 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 PCALL(PCALL_ALL);
2858 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002859#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002861#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002863#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 PUSH(res);
2866 if (res == NULL)
2867 goto error;
2868 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2872 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2873 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 int na = oparg & 0xff;
2876 int nk = (oparg>>8) & 0xff;
2877 int flags = (opcode - CALL_FUNCTION) & 3;
2878 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 PCALL(PCALL_ALL);
2881 if (flags & CALL_FLAG_VAR)
2882 n++;
2883 if (flags & CALL_FLAG_KW)
2884 n++;
2885 pfunc = stack_pointer - n - 1;
2886 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002889 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 PyObject *self = PyMethod_GET_SELF(func);
2891 Py_INCREF(self);
2892 func = PyMethod_GET_FUNCTION(func);
2893 Py_INCREF(func);
2894 Py_DECREF(*pfunc);
2895 *pfunc = self;
2896 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002897 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 } else
2899 Py_INCREF(func);
2900 sp = stack_pointer;
2901 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 READ_TIMESTAMP(intr1);
2904 stack_pointer = sp;
2905 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 PyObject *o = POP();
2909 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 PUSH(res);
2912 if (res == NULL)
2913 goto error;
2914 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2918 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 int posdefaults = oparg & 0xff;
2921 int kwdefaults = (oparg>>8) & 0xff;
2922 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002923
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 PyObject *qualname = POP(); /* qualname */
2925 PyObject *code = POP(); /* code object */
2926 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2927 Py_DECREF(code);
2928 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002929
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 if (func == NULL)
2931 goto error;
2932
2933 if (opcode == MAKE_CLOSURE) {
2934 PyObject *closure = POP();
2935 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 Py_DECREF(func);
2938 Py_DECREF(closure);
2939 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002943
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 PyObject *names = POP(); /* names of args with annotations */
2947 PyObject *anns = PyDict_New();
2948 if (anns == NULL) {
2949 Py_DECREF(func);
2950 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 assert(num_annotations == name_ix+1);
2954 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 PyObject *name, *value;
2956 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 name = PyTuple_GET_ITEM(names, name_ix);
2959 value = POP();
2960 err = PyDict_SetItem(anns, name, value);
2961 Py_DECREF(value);
2962 if (err != 0) {
2963 Py_DECREF(anns);
2964 Py_DECREF(func);
2965 goto error;
2966 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002968
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 /* Can't happen unless
2971 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 Py_DECREF(anns);
2973 Py_DECREF(func);
2974 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 Py_DECREF(anns);
2977 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 if (kwdefaults > 0) {
2982 PyObject *defs = PyDict_New();
2983 if (defs == NULL) {
2984 Py_DECREF(func);
2985 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 }
2987 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 PyObject *v = POP(); /* default value */
2989 PyObject *key = POP(); /* kw only arg name */
2990 int err = PyDict_SetItem(defs, key, v);
2991 Py_DECREF(v);
2992 Py_DECREF(key);
2993 if (err != 0) {
2994 Py_DECREF(defs);
2995 Py_DECREF(func);
2996 goto error;
2997 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 /* Can't happen unless
3001 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 Py_DECREF(func);
3003 Py_DECREF(defs);
3004 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003006 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05003008 if (posdefaults > 0) {
3009 PyObject *defs = PyTuple_New(posdefaults);
3010 if (defs == NULL) {
3011 Py_DECREF(func);
3012 goto error;
3013 }
3014 while (--posdefaults >= 0)
3015 PyTuple_SET_ITEM(defs, posdefaults, POP());
3016 if (PyFunction_SetDefaults(func, defs) != 0) {
3017 /* Can't happen unless
3018 PyFunction_SetDefaults changes. */
3019 Py_DECREF(defs);
3020 Py_DECREF(func);
3021 goto error;
3022 }
3023 Py_DECREF(defs);
3024 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003025 PUSH(func);
3026 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003028
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 TARGET(BUILD_SLICE) {
3030 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 step = NULL;
3035 stop = POP();
3036 start = TOP();
3037 slice = PySlice_New(start, stop, step);
3038 Py_DECREF(start);
3039 Py_DECREF(stop);
3040 Py_XDECREF(step);
3041 SET_TOP(slice);
3042 if (slice == NULL)
3043 goto error;
3044 DISPATCH();
3045 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003046
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 opcode = NEXTOP();
3049 oparg = oparg<<16 | NEXTARG();
3050 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003052
Antoine Pitrou042b1282010-08-13 21:15:58 +00003053#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003055#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 default:
3057 fprintf(stderr,
3058 "XXX lineno: %d, opcode: %d\n",
3059 PyFrame_GetLineNumber(f),
3060 opcode);
3061 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003063
3064#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003066#endif
3067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003069
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 /* This should never be reached. Every opcode should end with DISPATCH()
3071 or goto error. */
3072 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003073
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003076
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 assert(why == WHY_NOT);
3078 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003079
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003081#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 if (!PyErr_Occurred())
3083 PyErr_SetString(PyExc_SystemError,
3084 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003085#else
3086 assert(PyErr_Occurred());
3087#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003088
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003089 /* Log traceback info. */
3090 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003091
Benjamin Peterson51f46162013-01-23 08:38:47 -05003092 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003093 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3094 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003095
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003096fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 assert(why != WHY_NOT);
3098
3099 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003100 while (why != WHY_NOT && f->f_iblock > 0) {
3101 /* Peek at the current block. */
3102 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 assert(why != WHY_YIELD);
3105 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3106 why = WHY_NOT;
3107 JUMPTO(PyLong_AS_LONG(retval));
3108 Py_DECREF(retval);
3109 break;
3110 }
3111 /* Now we have to pop the block. */
3112 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 if (b->b_type == EXCEPT_HANDLER) {
3115 UNWIND_EXCEPT_HANDLER(b);
3116 continue;
3117 }
3118 UNWIND_BLOCK(b);
3119 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3120 why = WHY_NOT;
3121 JUMPTO(b->b_handler);
3122 break;
3123 }
3124 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3125 || b->b_type == SETUP_FINALLY)) {
3126 PyObject *exc, *val, *tb;
3127 int handler = b->b_handler;
3128 /* Beware, this invalidates all b->b_* fields */
3129 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3130 PUSH(tstate->exc_traceback);
3131 PUSH(tstate->exc_value);
3132 if (tstate->exc_type != NULL) {
3133 PUSH(tstate->exc_type);
3134 }
3135 else {
3136 Py_INCREF(Py_None);
3137 PUSH(Py_None);
3138 }
3139 PyErr_Fetch(&exc, &val, &tb);
3140 /* Make the raw exception data
3141 available to the handler,
3142 so a program can emulate the
3143 Python main loop. */
3144 PyErr_NormalizeException(
3145 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003146 if (tb != NULL)
3147 PyException_SetTraceback(val, tb);
3148 else
3149 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 Py_INCREF(exc);
3151 tstate->exc_type = exc;
3152 Py_INCREF(val);
3153 tstate->exc_value = val;
3154 tstate->exc_traceback = tb;
3155 if (tb == NULL)
3156 tb = Py_None;
3157 Py_INCREF(tb);
3158 PUSH(tb);
3159 PUSH(val);
3160 PUSH(exc);
3161 why = WHY_NOT;
3162 JUMPTO(handler);
3163 break;
3164 }
3165 if (b->b_type == SETUP_FINALLY) {
3166 if (why & (WHY_RETURN | WHY_CONTINUE))
3167 PUSH(retval);
3168 PUSH(PyLong_FromLong((long)why));
3169 why = WHY_NOT;
3170 JUMPTO(b->b_handler);
3171 break;
3172 }
3173 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 if (why != WHY_NOT)
3178 break;
3179 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003180
Victor Stinnerace47d72013-07-18 01:41:08 +02003181 assert(!PyErr_Occurred());
3182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 assert(why != WHY_YIELD);
3186 /* Pop remaining stack entries. */
3187 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 PyObject *o = POP();
3189 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 if (why != WHY_RETURN)
3193 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003194
Victor Stinner4a7cc882015-03-06 23:35:27 +01003195 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003196
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003197fast_yield:
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003198 if (co->co_flags & CO_GENERATOR) {
3199
Benjamin Petersonac913412011-07-03 16:25:11 -05003200 /* The purpose of this block is to put aside the generator's exception
3201 state and restore that of the calling frame. If the current
3202 exception state is from the caller, we clear the exception values
3203 on the generator frame, so they are not swapped back in latter. The
3204 origin of the current exception state is determined by checking for
3205 except handler blocks, which we must be in iff a new exception
3206 state came into existence in this frame. (An uncaught exception
3207 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3208 int i;
3209 for (i = 0; i < f->f_iblock; i++)
3210 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3211 break;
3212 if (i == f->f_iblock)
3213 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003214 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003215 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003216 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003217 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003220 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003222 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3223 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003225 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 why = WHY_EXCEPTION;
3227 }
3228 }
3229 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003230 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3231 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 PyTrace_RETURN, NULL);
3233 }
3234 }
3235 if (tstate->c_profilefunc) {
3236 if (why == WHY_EXCEPTION)
3237 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003238 tstate->c_profileobj,
3239 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003241 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3242 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003244 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003245 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 }
3247 }
3248 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003251exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003253 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003255
Victor Stinner4a7cc882015-03-06 23:35:27 +01003256 return _Py_CheckFunctionResult(retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003257}
3258
Benjamin Petersonb204a422011-06-05 22:04:07 -05003259static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003260format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3261{
3262 int err;
3263 Py_ssize_t len = PyList_GET_SIZE(names);
3264 PyObject *name_str, *comma, *tail, *tmp;
3265
3266 assert(PyList_CheckExact(names));
3267 assert(len >= 1);
3268 /* Deal with the joys of natural language. */
3269 switch (len) {
3270 case 1:
3271 name_str = PyList_GET_ITEM(names, 0);
3272 Py_INCREF(name_str);
3273 break;
3274 case 2:
3275 name_str = PyUnicode_FromFormat("%U and %U",
3276 PyList_GET_ITEM(names, len - 2),
3277 PyList_GET_ITEM(names, len - 1));
3278 break;
3279 default:
3280 tail = PyUnicode_FromFormat(", %U, and %U",
3281 PyList_GET_ITEM(names, len - 2),
3282 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003283 if (tail == NULL)
3284 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003285 /* Chop off the last two objects in the list. This shouldn't actually
3286 fail, but we can't be too careful. */
3287 err = PyList_SetSlice(names, len - 2, len, NULL);
3288 if (err == -1) {
3289 Py_DECREF(tail);
3290 return;
3291 }
3292 /* Stitch everything up into a nice comma-separated list. */
3293 comma = PyUnicode_FromString(", ");
3294 if (comma == NULL) {
3295 Py_DECREF(tail);
3296 return;
3297 }
3298 tmp = PyUnicode_Join(comma, names);
3299 Py_DECREF(comma);
3300 if (tmp == NULL) {
3301 Py_DECREF(tail);
3302 return;
3303 }
3304 name_str = PyUnicode_Concat(tmp, tail);
3305 Py_DECREF(tmp);
3306 Py_DECREF(tail);
3307 break;
3308 }
3309 if (name_str == NULL)
3310 return;
3311 PyErr_Format(PyExc_TypeError,
3312 "%U() missing %i required %s argument%s: %U",
3313 co->co_name,
3314 len,
3315 kind,
3316 len == 1 ? "" : "s",
3317 name_str);
3318 Py_DECREF(name_str);
3319}
3320
3321static void
3322missing_arguments(PyCodeObject *co, int missing, int defcount,
3323 PyObject **fastlocals)
3324{
3325 int i, j = 0;
3326 int start, end;
3327 int positional = defcount != -1;
3328 const char *kind = positional ? "positional" : "keyword-only";
3329 PyObject *missing_names;
3330
3331 /* Compute the names of the arguments that are missing. */
3332 missing_names = PyList_New(missing);
3333 if (missing_names == NULL)
3334 return;
3335 if (positional) {
3336 start = 0;
3337 end = co->co_argcount - defcount;
3338 }
3339 else {
3340 start = co->co_argcount;
3341 end = start + co->co_kwonlyargcount;
3342 }
3343 for (i = start; i < end; i++) {
3344 if (GETLOCAL(i) == NULL) {
3345 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3346 PyObject *name = PyObject_Repr(raw);
3347 if (name == NULL) {
3348 Py_DECREF(missing_names);
3349 return;
3350 }
3351 PyList_SET_ITEM(missing_names, j++, name);
3352 }
3353 }
3354 assert(j == missing);
3355 format_missing(kind, co, missing_names);
3356 Py_DECREF(missing_names);
3357}
3358
3359static void
3360too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003361{
3362 int plural;
3363 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003364 int i;
3365 PyObject *sig, *kwonly_sig;
3366
Benjamin Petersone109c702011-06-24 09:37:26 -05003367 assert((co->co_flags & CO_VARARGS) == 0);
3368 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003369 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003370 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003371 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003372 if (defcount) {
3373 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003374 plural = 1;
3375 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3376 }
3377 else {
3378 plural = co->co_argcount != 1;
3379 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3380 }
3381 if (sig == NULL)
3382 return;
3383 if (kwonly_given) {
3384 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3385 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3386 kwonly_given != 1 ? "s" : "");
3387 if (kwonly_sig == NULL) {
3388 Py_DECREF(sig);
3389 return;
3390 }
3391 }
3392 else {
3393 /* This will not fail. */
3394 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003395 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003396 }
3397 PyErr_Format(PyExc_TypeError,
3398 "%U() takes %U positional argument%s but %d%U %s given",
3399 co->co_name,
3400 sig,
3401 plural ? "s" : "",
3402 given,
3403 kwonly_sig,
3404 given == 1 && !kwonly_given ? "was" : "were");
3405 Py_DECREF(sig);
3406 Py_DECREF(kwonly_sig);
3407}
3408
Guido van Rossumc2e20742006-02-27 22:32:47 +00003409/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003410 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003411 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003412
Victor Stinner40ee3012014-06-16 15:59:28 +02003413static PyObject *
3414_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003416 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3417 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003418{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003419 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003420 PyFrameObject *f;
3421 PyObject *retval = NULL;
3422 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003423 PyThreadState *tstate = PyThreadState_GET();
3424 PyObject *x, *u;
3425 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003426 int i;
3427 int n = argcount;
3428 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 if (globals == NULL) {
3431 PyErr_SetString(PyExc_SystemError,
3432 "PyEval_EvalCodeEx: NULL globals");
3433 return NULL;
3434 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 assert(tstate != NULL);
3437 assert(globals != NULL);
3438 f = PyFrame_New(tstate, co, globals, locals);
3439 if (f == NULL)
3440 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 fastlocals = f->f_localsplus;
3443 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003444
Benjamin Petersonb204a422011-06-05 22:04:07 -05003445 /* Parse arguments. */
3446 if (co->co_flags & CO_VARKEYWORDS) {
3447 kwdict = PyDict_New();
3448 if (kwdict == NULL)
3449 goto fail;
3450 i = total_args;
3451 if (co->co_flags & CO_VARARGS)
3452 i++;
3453 SETLOCAL(i, kwdict);
3454 }
3455 if (argcount > co->co_argcount)
3456 n = co->co_argcount;
3457 for (i = 0; i < n; i++) {
3458 x = args[i];
3459 Py_INCREF(x);
3460 SETLOCAL(i, x);
3461 }
3462 if (co->co_flags & CO_VARARGS) {
3463 u = PyTuple_New(argcount - n);
3464 if (u == NULL)
3465 goto fail;
3466 SETLOCAL(total_args, u);
3467 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 x = args[i];
3469 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003470 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003472 }
3473 for (i = 0; i < kwcount; i++) {
3474 PyObject **co_varnames;
3475 PyObject *keyword = kws[2*i];
3476 PyObject *value = kws[2*i + 1];
3477 int j;
3478 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3479 PyErr_Format(PyExc_TypeError,
3480 "%U() keywords must be strings",
3481 co->co_name);
3482 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003484 /* Speed hack: do raw pointer compares. As names are
3485 normally interned this should almost always hit. */
3486 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3487 for (j = 0; j < total_args; j++) {
3488 PyObject *nm = co_varnames[j];
3489 if (nm == keyword)
3490 goto kw_found;
3491 }
3492 /* Slow fallback, just in case */
3493 for (j = 0; j < total_args; j++) {
3494 PyObject *nm = co_varnames[j];
3495 int cmp = PyObject_RichCompareBool(
3496 keyword, nm, Py_EQ);
3497 if (cmp > 0)
3498 goto kw_found;
3499 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003501 }
3502 if (j >= total_args && kwdict == NULL) {
3503 PyErr_Format(PyExc_TypeError,
3504 "%U() got an unexpected "
3505 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 co->co_name,
3507 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003508 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003510 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3511 goto fail;
3512 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003513 continue;
3514 kw_found:
3515 if (GETLOCAL(j) != NULL) {
3516 PyErr_Format(PyExc_TypeError,
3517 "%U() got multiple "
3518 "values for argument '%S'",
3519 co->co_name,
3520 keyword);
3521 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003522 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003523 Py_INCREF(value);
3524 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003525 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003526 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003527 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 goto fail;
3529 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003530 if (argcount < co->co_argcount) {
3531 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003532 int missing = 0;
3533 for (i = argcount; i < m; i++)
3534 if (GETLOCAL(i) == NULL)
3535 missing++;
3536 if (missing) {
3537 missing_arguments(co, missing, defcount, fastlocals);
3538 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003539 }
3540 if (n > m)
3541 i = n - m;
3542 else
3543 i = 0;
3544 for (; i < defcount; i++) {
3545 if (GETLOCAL(m+i) == NULL) {
3546 PyObject *def = defs[i];
3547 Py_INCREF(def);
3548 SETLOCAL(m+i, def);
3549 }
3550 }
3551 }
3552 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003553 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003554 for (i = co->co_argcount; i < total_args; i++) {
3555 PyObject *name;
3556 if (GETLOCAL(i) != NULL)
3557 continue;
3558 name = PyTuple_GET_ITEM(co->co_varnames, i);
3559 if (kwdefs != NULL) {
3560 PyObject *def = PyDict_GetItem(kwdefs, name);
3561 if (def) {
3562 Py_INCREF(def);
3563 SETLOCAL(i, def);
3564 continue;
3565 }
3566 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003567 missing++;
3568 }
3569 if (missing) {
3570 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003571 goto fail;
3572 }
3573 }
3574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003576 vars into frame. */
3577 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003579 int arg;
3580 /* Possibly account for the cell variable being an argument. */
3581 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003582 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003583 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003584 /* Clear the local copy. */
3585 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003586 }
3587 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003588 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003589 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003590 if (c == NULL)
3591 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003592 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003594 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3595 PyObject *o = PyTuple_GET_ITEM(closure, i);
3596 Py_INCREF(o);
3597 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 if (co->co_flags & CO_GENERATOR) {
3601 /* Don't need to keep the reference to f_back, it will be set
3602 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003603 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 /* Create a new generator that owns the ready to run frame
3608 * and return that as the value. */
Victor Stinner40ee3012014-06-16 15:59:28 +02003609 return PyGen_NewWithQualName(f, name, qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003613
Thomas Woutersce272b62007-09-19 21:19:28 +00003614fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 /* decref'ing the frame can cause __del__ methods to get invoked,
3617 which can call back into Python. While we're done with the
3618 current Python frame (f), the associated C stack is still in use,
3619 so recursion_depth must be boosted for the duration.
3620 */
3621 assert(tstate != NULL);
3622 ++tstate->recursion_depth;
3623 Py_DECREF(f);
3624 --tstate->recursion_depth;
3625 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003626}
3627
Victor Stinner40ee3012014-06-16 15:59:28 +02003628PyObject *
3629PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
3630 PyObject **args, int argcount, PyObject **kws, int kwcount,
3631 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
3632{
3633 return _PyEval_EvalCodeWithName(_co, globals, locals,
3634 args, argcount, kws, kwcount,
3635 defs, defcount, kwdefs, closure,
3636 NULL, NULL);
3637}
Tim Peters5ca576e2001-06-18 22:08:13 +00003638
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003639static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003640special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003643 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003645 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 return NULL;
3647 }
3648 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003649}
3650
3651
Benjamin Peterson87880242011-07-03 16:48:31 -05003652/* These 3 functions deal with the exception state of generators. */
3653
3654static void
3655save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3656{
3657 PyObject *type, *value, *traceback;
3658 Py_XINCREF(tstate->exc_type);
3659 Py_XINCREF(tstate->exc_value);
3660 Py_XINCREF(tstate->exc_traceback);
3661 type = f->f_exc_type;
3662 value = f->f_exc_value;
3663 traceback = f->f_exc_traceback;
3664 f->f_exc_type = tstate->exc_type;
3665 f->f_exc_value = tstate->exc_value;
3666 f->f_exc_traceback = tstate->exc_traceback;
3667 Py_XDECREF(type);
3668 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003669 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003670}
3671
3672static void
3673swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3674{
3675 PyObject *tmp;
3676 tmp = tstate->exc_type;
3677 tstate->exc_type = f->f_exc_type;
3678 f->f_exc_type = tmp;
3679 tmp = tstate->exc_value;
3680 tstate->exc_value = f->f_exc_value;
3681 f->f_exc_value = tmp;
3682 tmp = tstate->exc_traceback;
3683 tstate->exc_traceback = f->f_exc_traceback;
3684 f->f_exc_traceback = tmp;
3685}
3686
3687static void
3688restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3689{
3690 PyObject *type, *value, *tb;
3691 type = tstate->exc_type;
3692 value = tstate->exc_value;
3693 tb = tstate->exc_traceback;
3694 tstate->exc_type = f->f_exc_type;
3695 tstate->exc_value = f->f_exc_value;
3696 tstate->exc_traceback = f->f_exc_traceback;
3697 f->f_exc_type = NULL;
3698 f->f_exc_value = NULL;
3699 f->f_exc_traceback = NULL;
3700 Py_XDECREF(type);
3701 Py_XDECREF(value);
3702 Py_XDECREF(tb);
3703}
3704
3705
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003706/* Logic for the raise statement (too complicated for inlining).
3707 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003708static int
Collin Winter828f04a2007-08-31 00:04:24 +00003709do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 if (exc == NULL) {
3714 /* Reraise */
3715 PyThreadState *tstate = PyThreadState_GET();
3716 PyObject *tb;
3717 type = tstate->exc_type;
3718 value = tstate->exc_value;
3719 tb = tstate->exc_traceback;
3720 if (type == Py_None) {
3721 PyErr_SetString(PyExc_RuntimeError,
3722 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003723 return 0;
3724 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003725 Py_XINCREF(type);
3726 Py_XINCREF(value);
3727 Py_XINCREF(tb);
3728 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003729 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 /* We support the following forms of raise:
3733 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003734 raise <instance>
3735 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 if (PyExceptionClass_Check(exc)) {
3738 type = exc;
3739 value = PyObject_CallObject(exc, NULL);
3740 if (value == NULL)
3741 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003742 if (!PyExceptionInstance_Check(value)) {
3743 PyErr_Format(PyExc_TypeError,
3744 "calling %R should have returned an instance of "
3745 "BaseException, not %R",
3746 type, Py_TYPE(value));
3747 goto raise_error;
3748 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 }
3750 else if (PyExceptionInstance_Check(exc)) {
3751 value = exc;
3752 type = PyExceptionInstance_Class(exc);
3753 Py_INCREF(type);
3754 }
3755 else {
3756 /* Not something you can raise. You get an exception
3757 anyway, just not what you specified :-) */
3758 Py_DECREF(exc);
3759 PyErr_SetString(PyExc_TypeError,
3760 "exceptions must derive from BaseException");
3761 goto raise_error;
3762 }
Collin Winter828f04a2007-08-31 00:04:24 +00003763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 if (cause) {
3765 PyObject *fixed_cause;
3766 if (PyExceptionClass_Check(cause)) {
3767 fixed_cause = PyObject_CallObject(cause, NULL);
3768 if (fixed_cause == NULL)
3769 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003770 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003772 else if (PyExceptionInstance_Check(cause)) {
3773 fixed_cause = cause;
3774 }
3775 else if (cause == Py_None) {
3776 Py_DECREF(cause);
3777 fixed_cause = NULL;
3778 }
3779 else {
3780 PyErr_SetString(PyExc_TypeError,
3781 "exception causes must derive from "
3782 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 goto raise_error;
3784 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003785 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 }
Collin Winter828f04a2007-08-31 00:04:24 +00003787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 PyErr_SetObject(type, value);
3789 /* PyErr_SetObject incref's its arguments */
3790 Py_XDECREF(value);
3791 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003792 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003793
3794raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 Py_XDECREF(value);
3796 Py_XDECREF(type);
3797 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003798 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003799}
3800
Tim Petersd6d010b2001-06-21 02:49:55 +00003801/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003802 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003803
Guido van Rossum0368b722007-05-11 16:50:42 +00003804 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3805 with a variable target.
3806*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003807
Barry Warsawe42b18f1997-08-25 22:13:04 +00003808static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003809unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 int i = 0, j = 0;
3812 Py_ssize_t ll = 0;
3813 PyObject *it; /* iter(v) */
3814 PyObject *w;
3815 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 it = PyObject_GetIter(v);
3820 if (it == NULL)
3821 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 for (; i < argcnt; i++) {
3824 w = PyIter_Next(it);
3825 if (w == NULL) {
3826 /* Iterator done, via error or exhaustion. */
3827 if (!PyErr_Occurred()) {
3828 PyErr_Format(PyExc_ValueError,
3829 "need more than %d value%s to unpack",
3830 i, i == 1 ? "" : "s");
3831 }
3832 goto Error;
3833 }
3834 *--sp = w;
3835 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 if (argcntafter == -1) {
3838 /* We better have exhausted the iterator now. */
3839 w = PyIter_Next(it);
3840 if (w == NULL) {
3841 if (PyErr_Occurred())
3842 goto Error;
3843 Py_DECREF(it);
3844 return 1;
3845 }
3846 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003847 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3848 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 goto Error;
3850 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 l = PySequence_List(it);
3853 if (l == NULL)
3854 goto Error;
3855 *--sp = l;
3856 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 ll = PyList_GET_SIZE(l);
3859 if (ll < argcntafter) {
3860 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3861 argcnt + ll);
3862 goto Error;
3863 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 /* Pop the "after-variable" args off the list. */
3866 for (j = argcntafter; j > 0; j--, i++) {
3867 *--sp = PyList_GET_ITEM(l, ll - j);
3868 }
3869 /* Resize the list. */
3870 Py_SIZE(l) = ll - argcntafter;
3871 Py_DECREF(it);
3872 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003873
Tim Petersd6d010b2001-06-21 02:49:55 +00003874Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 for (; i > 0; i--, sp++)
3876 Py_DECREF(*sp);
3877 Py_XDECREF(it);
3878 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003879}
3880
3881
Guido van Rossum96a42c81992-01-12 02:29:51 +00003882#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003883static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003884prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003885{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 printf("%s ", str);
3887 if (PyObject_Print(v, stdout, 0) != 0)
3888 PyErr_Clear(); /* Don't know what else to do */
3889 printf("\n");
3890 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003892#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003893
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003894static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003895call_exc_trace(Py_tracefunc func, PyObject *self,
3896 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003897{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003898 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01003900 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 if (value == NULL) {
3902 value = Py_None;
3903 Py_INCREF(value);
3904 }
Antoine Pitrou89335212013-11-23 14:05:23 +01003905 PyErr_NormalizeException(&type, &value, &orig_traceback);
3906 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 arg = PyTuple_Pack(3, type, value, traceback);
3908 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01003909 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 return;
3911 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003912 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 Py_DECREF(arg);
3914 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003915 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 else {
3917 Py_XDECREF(type);
3918 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003919 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003921}
3922
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003923static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003924call_trace_protected(Py_tracefunc func, PyObject *obj,
3925 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 PyObject *type, *value, *traceback;
3929 int err;
3930 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003931 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 if (err == 0)
3933 {
3934 PyErr_Restore(type, value, traceback);
3935 return 0;
3936 }
3937 else {
3938 Py_XDECREF(type);
3939 Py_XDECREF(value);
3940 Py_XDECREF(traceback);
3941 return -1;
3942 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003943}
3944
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003945static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003946call_trace(Py_tracefunc func, PyObject *obj,
3947 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 int result;
3951 if (tstate->tracing)
3952 return 0;
3953 tstate->tracing++;
3954 tstate->use_tracing = 0;
3955 result = func(obj, frame, what, arg);
3956 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3957 || (tstate->c_profilefunc != NULL));
3958 tstate->tracing--;
3959 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003960}
3961
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003962PyObject *
3963_PyEval_CallTracing(PyObject *func, PyObject *args)
3964{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003965 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 int save_tracing = tstate->tracing;
3967 int save_use_tracing = tstate->use_tracing;
3968 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 tstate->tracing = 0;
3971 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3972 || (tstate->c_profilefunc != NULL));
3973 result = PyObject_Call(func, args, NULL);
3974 tstate->tracing = save_tracing;
3975 tstate->use_tracing = save_use_tracing;
3976 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003977}
3978
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003979/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003980static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003981maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003982 PyThreadState *tstate, PyFrameObject *frame,
3983 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 int result = 0;
3986 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 /* If the last instruction executed isn't in the current
3989 instruction window, reset the window.
3990 */
3991 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3992 PyAddrPair bounds;
3993 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3994 &bounds);
3995 *instr_lb = bounds.ap_lower;
3996 *instr_ub = bounds.ap_upper;
3997 }
3998 /* If the last instruction falls at the start of a line or if
3999 it represents a jump backwards, update the frame's line
4000 number and call the trace function. */
4001 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4002 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004003 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 }
4005 *instr_prev = frame->f_lasti;
4006 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004007}
4008
Fred Drake5755ce62001-06-27 19:19:46 +00004009void
4010PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004011{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 PyThreadState *tstate = PyThreadState_GET();
4013 PyObject *temp = tstate->c_profileobj;
4014 Py_XINCREF(arg);
4015 tstate->c_profilefunc = NULL;
4016 tstate->c_profileobj = NULL;
4017 /* Must make sure that tracing is not ignored if 'temp' is freed */
4018 tstate->use_tracing = tstate->c_tracefunc != NULL;
4019 Py_XDECREF(temp);
4020 tstate->c_profilefunc = func;
4021 tstate->c_profileobj = arg;
4022 /* Flag that tracing or profiling is turned on */
4023 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004024}
4025
4026void
4027PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 PyThreadState *tstate = PyThreadState_GET();
4030 PyObject *temp = tstate->c_traceobj;
4031 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4032 Py_XINCREF(arg);
4033 tstate->c_tracefunc = NULL;
4034 tstate->c_traceobj = NULL;
4035 /* Must make sure that profiling is not ignored if 'temp' is freed */
4036 tstate->use_tracing = tstate->c_profilefunc != NULL;
4037 Py_XDECREF(temp);
4038 tstate->c_tracefunc = func;
4039 tstate->c_traceobj = arg;
4040 /* Flag that tracing or profiling is turned on */
4041 tstate->use_tracing = ((func != NULL)
4042 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004043}
4044
Guido van Rossumb209a111997-04-29 18:18:01 +00004045PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004046PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 PyFrameObject *current_frame = PyEval_GetFrame();
4049 if (current_frame == NULL)
4050 return PyThreadState_GET()->interp->builtins;
4051 else
4052 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004053}
4054
Guido van Rossumb209a111997-04-29 18:18:01 +00004055PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004056PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004059 if (current_frame == NULL) {
4060 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004062 }
4063
4064 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4065 return NULL;
4066
4067 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004069}
4070
Guido van Rossumb209a111997-04-29 18:18:01 +00004071PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004072PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004073{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 PyFrameObject *current_frame = PyEval_GetFrame();
4075 if (current_frame == NULL)
4076 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004077
4078 assert(current_frame->f_globals != NULL);
4079 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004080}
4081
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004082PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004083PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 PyThreadState *tstate = PyThreadState_GET();
4086 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004087}
4088
Guido van Rossum6135a871995-01-09 17:53:26 +00004089int
Tim Peters5ba58662001-07-16 02:29:45 +00004090PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 PyFrameObject *current_frame = PyEval_GetFrame();
4093 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 if (current_frame != NULL) {
4096 const int codeflags = current_frame->f_code->co_flags;
4097 const int compilerflags = codeflags & PyCF_MASK;
4098 if (compilerflags) {
4099 result = 1;
4100 cf->cf_flags |= compilerflags;
4101 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004102#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 if (codeflags & CO_GENERATOR_ALLOWED) {
4104 result = 1;
4105 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4106 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 }
4109 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004110}
4111
Guido van Rossum3f5da241990-12-20 15:06:42 +00004112
Guido van Rossum681d79a1995-07-18 14:51:37 +00004113/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004114 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004115
Guido van Rossumb209a111997-04-29 18:18:01 +00004116PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004117PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004120
Victor Stinner59b356d2015-03-16 11:52:32 +01004121#ifdef Py_DEBUG
4122 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4123 set. It raises a new exception if parameters are invalid or if
4124 PyTuple_New() fails, and so the original exception is lost. */
4125 assert(!PyErr_Occurred());
4126#endif
4127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 if (arg == NULL) {
4129 arg = PyTuple_New(0);
4130 if (arg == NULL)
4131 return NULL;
4132 }
4133 else if (!PyTuple_Check(arg)) {
4134 PyErr_SetString(PyExc_TypeError,
4135 "argument list must be a tuple");
4136 return NULL;
4137 }
4138 else
4139 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 if (kw != NULL && !PyDict_Check(kw)) {
4142 PyErr_SetString(PyExc_TypeError,
4143 "keyword list must be a dictionary");
4144 Py_DECREF(arg);
4145 return NULL;
4146 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 result = PyObject_Call(func, arg, kw);
4149 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004152}
4153
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004154const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004155PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 if (PyMethod_Check(func))
4158 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4159 else if (PyFunction_Check(func))
4160 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4161 else if (PyCFunction_Check(func))
4162 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4163 else
4164 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004165}
4166
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004167const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004168PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 if (PyMethod_Check(func))
4171 return "()";
4172 else if (PyFunction_Check(func))
4173 return "()";
4174 else if (PyCFunction_Check(func))
4175 return "()";
4176 else
4177 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004178}
4179
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004180static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004181err_args(PyObject *func, int flags, int nargs)
4182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 if (flags & METH_NOARGS)
4184 PyErr_Format(PyExc_TypeError,
4185 "%.200s() takes no arguments (%d given)",
4186 ((PyCFunctionObject *)func)->m_ml->ml_name,
4187 nargs);
4188 else
4189 PyErr_Format(PyExc_TypeError,
4190 "%.200s() takes exactly one argument (%d given)",
4191 ((PyCFunctionObject *)func)->m_ml->ml_name,
4192 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004193}
4194
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004195#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004196if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004197 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4198 tstate, tstate->frame, \
4199 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 x = NULL; \
4201 } \
4202 else { \
4203 x = call; \
4204 if (tstate->c_profilefunc != NULL) { \
4205 if (x == NULL) { \
4206 call_trace_protected(tstate->c_profilefunc, \
4207 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004208 tstate, tstate->frame, \
4209 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 /* XXX should pass (type, value, tb) */ \
4211 } else { \
4212 if (call_trace(tstate->c_profilefunc, \
4213 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004214 tstate, tstate->frame, \
4215 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 Py_DECREF(x); \
4217 x = NULL; \
4218 } \
4219 } \
4220 } \
4221 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004222} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 x = call; \
4224 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004225
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004226static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004227call_function(PyObject ***pp_stack, int oparg
4228#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004230#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 int na = oparg & 0xff;
4234 int nk = (oparg>>8) & 0xff;
4235 int n = na + 2 * nk;
4236 PyObject **pfunc = (*pp_stack) - n - 1;
4237 PyObject *func = *pfunc;
4238 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 /* Always dispatch PyCFunction first, because these are
4241 presumed to be the most frequent callable object.
4242 */
4243 if (PyCFunction_Check(func) && nk == 0) {
4244 int flags = PyCFunction_GET_FLAGS(func);
4245 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 PCALL(PCALL_CFUNCTION);
4248 if (flags & (METH_NOARGS | METH_O)) {
4249 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4250 PyObject *self = PyCFunction_GET_SELF(func);
4251 if (flags & METH_NOARGS && na == 0) {
4252 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004253
4254 x = _Py_CheckFunctionResult(x, "call_function");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 }
4256 else if (flags & METH_O && na == 1) {
4257 PyObject *arg = EXT_POP(*pp_stack);
4258 C_TRACE(x, (*meth)(self,arg));
4259 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004260
4261 x = _Py_CheckFunctionResult(x, "call_function");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 }
4263 else {
4264 err_args(func, flags, na);
4265 x = NULL;
4266 }
4267 }
4268 else {
4269 PyObject *callargs;
4270 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004271 if (callargs != NULL) {
4272 READ_TIMESTAMP(*pintr0);
4273 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4274 READ_TIMESTAMP(*pintr1);
4275 Py_XDECREF(callargs);
4276 }
4277 else {
4278 x = NULL;
4279 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004281 }
4282 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4284 /* optimize access to bound methods */
4285 PyObject *self = PyMethod_GET_SELF(func);
4286 PCALL(PCALL_METHOD);
4287 PCALL(PCALL_BOUND_METHOD);
4288 Py_INCREF(self);
4289 func = PyMethod_GET_FUNCTION(func);
4290 Py_INCREF(func);
4291 Py_DECREF(*pfunc);
4292 *pfunc = self;
4293 na++;
4294 n++;
4295 } else
4296 Py_INCREF(func);
4297 READ_TIMESTAMP(*pintr0);
4298 if (PyFunction_Check(func))
4299 x = fast_function(func, pp_stack, n, na, nk);
4300 else
4301 x = do_call(func, pp_stack, na, nk);
4302 READ_TIMESTAMP(*pintr1);
4303 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004304
4305 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 /* Clear the stack of the function object. Also removes
4309 the arguments in case they weren't consumed already
4310 (fast_function() and err_args() leave them on the stack).
4311 */
4312 while ((*pp_stack) > pfunc) {
4313 w = EXT_POP(*pp_stack);
4314 Py_DECREF(w);
4315 PCALL(PCALL_POP);
4316 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004317
Victor Stinner4a7cc882015-03-06 23:35:27 +01004318 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004320}
4321
Jeremy Hylton192690e2002-08-16 18:36:11 +00004322/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004323 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004324 For the simplest case -- a function that takes only positional
4325 arguments and is called with only positional arguments -- it
4326 inlines the most primitive frame setup code from
4327 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4328 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004329*/
4330
4331static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004332fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4335 PyObject *globals = PyFunction_GET_GLOBALS(func);
4336 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4337 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Victor Stinner40ee3012014-06-16 15:59:28 +02004338 PyObject *name = ((PyFunctionObject *)func) -> func_name;
4339 PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004340 PyObject **d = NULL;
4341 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 PCALL(PCALL_FUNCTION);
4344 PCALL(PCALL_FAST_FUNCTION);
4345 if (argdefs == NULL && co->co_argcount == n &&
4346 co->co_kwonlyargcount == 0 && nk==0 &&
4347 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4348 PyFrameObject *f;
4349 PyObject *retval = NULL;
4350 PyThreadState *tstate = PyThreadState_GET();
4351 PyObject **fastlocals, **stack;
4352 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 PCALL(PCALL_FASTER_FUNCTION);
4355 assert(globals != NULL);
4356 /* XXX Perhaps we should create a specialized
4357 PyFrame_New() that doesn't take locals, but does
4358 take builtins without sanity checking them.
4359 */
4360 assert(tstate != NULL);
4361 f = PyFrame_New(tstate, co, globals, NULL);
4362 if (f == NULL)
4363 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 fastlocals = f->f_localsplus;
4366 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 for (i = 0; i < n; i++) {
4369 Py_INCREF(*stack);
4370 fastlocals[i] = *stack++;
4371 }
4372 retval = PyEval_EvalFrameEx(f,0);
4373 ++tstate->recursion_depth;
4374 Py_DECREF(f);
4375 --tstate->recursion_depth;
4376 return retval;
4377 }
4378 if (argdefs != NULL) {
4379 d = &PyTuple_GET_ITEM(argdefs, 0);
4380 nd = Py_SIZE(argdefs);
4381 }
Victor Stinner40ee3012014-06-16 15:59:28 +02004382 return _PyEval_EvalCodeWithName((PyObject*)co, globals,
4383 (PyObject *)NULL, (*pp_stack)-n, na,
4384 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4385 PyFunction_GET_CLOSURE(func),
4386 name, qualname);
Jeremy Hylton52820442001-01-03 23:52:36 +00004387}
4388
4389static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004390update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4391 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 PyObject *kwdict = NULL;
4394 if (orig_kwdict == NULL)
4395 kwdict = PyDict_New();
4396 else {
4397 kwdict = PyDict_Copy(orig_kwdict);
4398 Py_DECREF(orig_kwdict);
4399 }
4400 if (kwdict == NULL)
4401 return NULL;
4402 while (--nk >= 0) {
4403 int err;
4404 PyObject *value = EXT_POP(*pp_stack);
4405 PyObject *key = EXT_POP(*pp_stack);
4406 if (PyDict_GetItem(kwdict, key) != NULL) {
4407 PyErr_Format(PyExc_TypeError,
4408 "%.200s%s got multiple values "
4409 "for keyword argument '%U'",
4410 PyEval_GetFuncName(func),
4411 PyEval_GetFuncDesc(func),
4412 key);
4413 Py_DECREF(key);
4414 Py_DECREF(value);
4415 Py_DECREF(kwdict);
4416 return NULL;
4417 }
4418 err = PyDict_SetItem(kwdict, key, value);
4419 Py_DECREF(key);
4420 Py_DECREF(value);
4421 if (err) {
4422 Py_DECREF(kwdict);
4423 return NULL;
4424 }
4425 }
4426 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004427}
4428
4429static PyObject *
4430update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 callargs = PyTuple_New(nstack + nstar);
4436 if (callargs == NULL) {
4437 return NULL;
4438 }
4439 if (nstar) {
4440 int i;
4441 for (i = 0; i < nstar; i++) {
4442 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4443 Py_INCREF(a);
4444 PyTuple_SET_ITEM(callargs, nstack + i, a);
4445 }
4446 }
4447 while (--nstack >= 0) {
4448 w = EXT_POP(*pp_stack);
4449 PyTuple_SET_ITEM(callargs, nstack, w);
4450 }
4451 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004452}
4453
4454static PyObject *
4455load_args(PyObject ***pp_stack, int na)
4456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyObject *args = PyTuple_New(na);
4458 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 if (args == NULL)
4461 return NULL;
4462 while (--na >= 0) {
4463 w = EXT_POP(*pp_stack);
4464 PyTuple_SET_ITEM(args, na, w);
4465 }
4466 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004467}
4468
4469static PyObject *
4470do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 PyObject *callargs = NULL;
4473 PyObject *kwdict = NULL;
4474 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 if (nk > 0) {
4477 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4478 if (kwdict == NULL)
4479 goto call_fail;
4480 }
4481 callargs = load_args(pp_stack, na);
4482 if (callargs == NULL)
4483 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004484#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 /* At this point, we have to look at the type of func to
4486 update the call stats properly. Do it here so as to avoid
4487 exposing the call stats machinery outside ceval.c
4488 */
4489 if (PyFunction_Check(func))
4490 PCALL(PCALL_FUNCTION);
4491 else if (PyMethod_Check(func))
4492 PCALL(PCALL_METHOD);
4493 else if (PyType_Check(func))
4494 PCALL(PCALL_TYPE);
4495 else if (PyCFunction_Check(func))
4496 PCALL(PCALL_CFUNCTION);
4497 else
4498 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004499#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 if (PyCFunction_Check(func)) {
4501 PyThreadState *tstate = PyThreadState_GET();
4502 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4503 }
4504 else
4505 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004506call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 Py_XDECREF(callargs);
4508 Py_XDECREF(kwdict);
4509 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004510}
4511
4512static PyObject *
4513ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 int nstar = 0;
4516 PyObject *callargs = NULL;
4517 PyObject *stararg = NULL;
4518 PyObject *kwdict = NULL;
4519 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 if (flags & CALL_FLAG_KW) {
4522 kwdict = EXT_POP(*pp_stack);
4523 if (!PyDict_Check(kwdict)) {
4524 PyObject *d;
4525 d = PyDict_New();
4526 if (d == NULL)
4527 goto ext_call_fail;
4528 if (PyDict_Update(d, kwdict) != 0) {
4529 Py_DECREF(d);
4530 /* PyDict_Update raises attribute
4531 * error (percolated from an attempt
4532 * to get 'keys' attribute) instead of
4533 * a type error if its second argument
4534 * is not a mapping.
4535 */
4536 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4537 PyErr_Format(PyExc_TypeError,
4538 "%.200s%.200s argument after ** "
4539 "must be a mapping, not %.200s",
4540 PyEval_GetFuncName(func),
4541 PyEval_GetFuncDesc(func),
4542 kwdict->ob_type->tp_name);
4543 }
4544 goto ext_call_fail;
4545 }
4546 Py_DECREF(kwdict);
4547 kwdict = d;
4548 }
4549 }
4550 if (flags & CALL_FLAG_VAR) {
4551 stararg = EXT_POP(*pp_stack);
4552 if (!PyTuple_Check(stararg)) {
4553 PyObject *t = NULL;
4554 t = PySequence_Tuple(stararg);
4555 if (t == NULL) {
4556 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4557 PyErr_Format(PyExc_TypeError,
4558 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004559 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 PyEval_GetFuncName(func),
4561 PyEval_GetFuncDesc(func),
4562 stararg->ob_type->tp_name);
4563 }
4564 goto ext_call_fail;
4565 }
4566 Py_DECREF(stararg);
4567 stararg = t;
4568 }
4569 nstar = PyTuple_GET_SIZE(stararg);
4570 }
4571 if (nk > 0) {
4572 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4573 if (kwdict == NULL)
4574 goto ext_call_fail;
4575 }
4576 callargs = update_star_args(na, nstar, stararg, pp_stack);
4577 if (callargs == NULL)
4578 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004579#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 /* At this point, we have to look at the type of func to
4581 update the call stats properly. Do it here so as to avoid
4582 exposing the call stats machinery outside ceval.c
4583 */
4584 if (PyFunction_Check(func))
4585 PCALL(PCALL_FUNCTION);
4586 else if (PyMethod_Check(func))
4587 PCALL(PCALL_METHOD);
4588 else if (PyType_Check(func))
4589 PCALL(PCALL_TYPE);
4590 else if (PyCFunction_Check(func))
4591 PCALL(PCALL_CFUNCTION);
4592 else
4593 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004594#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 if (PyCFunction_Check(func)) {
4596 PyThreadState *tstate = PyThreadState_GET();
4597 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4598 }
4599 else
4600 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004601ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 Py_XDECREF(callargs);
4603 Py_XDECREF(kwdict);
4604 Py_XDECREF(stararg);
4605 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004606}
4607
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004608/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004609 nb_index slot defined, and store in *pi.
4610 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4611 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 +00004612 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004613*/
Tim Petersb5196382001-12-16 19:44:20 +00004614/* Note: If v is NULL, return success without storing into *pi. This
4615 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4616 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004617*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004618int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004619_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004621 if (v != NULL) {
4622 Py_ssize_t x;
4623 if (PyIndex_Check(v)) {
4624 x = PyNumber_AsSsize_t(v, NULL);
4625 if (x == -1 && PyErr_Occurred())
4626 return 0;
4627 }
4628 else {
4629 PyErr_SetString(PyExc_TypeError,
4630 "slice indices must be integers or "
4631 "None or have an __index__ method");
4632 return 0;
4633 }
4634 *pi = x;
4635 }
4636 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004637}
4638
Guido van Rossum486364b2007-06-30 05:01:58 +00004639#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004641
Guido van Rossumb209a111997-04-29 18:18:01 +00004642static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004643cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 int res = 0;
4646 switch (op) {
4647 case PyCmp_IS:
4648 res = (v == w);
4649 break;
4650 case PyCmp_IS_NOT:
4651 res = (v != w);
4652 break;
4653 case PyCmp_IN:
4654 res = PySequence_Contains(w, v);
4655 if (res < 0)
4656 return NULL;
4657 break;
4658 case PyCmp_NOT_IN:
4659 res = PySequence_Contains(w, v);
4660 if (res < 0)
4661 return NULL;
4662 res = !res;
4663 break;
4664 case PyCmp_EXC_MATCH:
4665 if (PyTuple_Check(w)) {
4666 Py_ssize_t i, length;
4667 length = PyTuple_Size(w);
4668 for (i = 0; i < length; i += 1) {
4669 PyObject *exc = PyTuple_GET_ITEM(w, i);
4670 if (!PyExceptionClass_Check(exc)) {
4671 PyErr_SetString(PyExc_TypeError,
4672 CANNOT_CATCH_MSG);
4673 return NULL;
4674 }
4675 }
4676 }
4677 else {
4678 if (!PyExceptionClass_Check(w)) {
4679 PyErr_SetString(PyExc_TypeError,
4680 CANNOT_CATCH_MSG);
4681 return NULL;
4682 }
4683 }
4684 res = PyErr_GivenExceptionMatches(v, w);
4685 break;
4686 default:
4687 return PyObject_RichCompare(v, w, op);
4688 }
4689 v = res ? Py_True : Py_False;
4690 Py_INCREF(v);
4691 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004692}
4693
Thomas Wouters52152252000-08-17 22:55:00 +00004694static PyObject *
4695import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004698 _Py_IDENTIFIER(__name__);
4699 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004702 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
4703 return x;
4704 /* Issue #17636: in case this failed because of a circular relative
4705 import, try to fallback on reading the module directly from
4706 sys.modules. */
4707 PyErr_Clear();
4708 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
4709 if (pkgname == NULL)
4710 return NULL;
4711 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
4712 Py_DECREF(pkgname);
4713 if (fullmodname == NULL)
4714 return NULL;
4715 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
4716 if (x == NULL)
Brett Cannona79e4fb2013-07-12 11:22:26 -04004717 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004718 else
4719 Py_INCREF(x);
4720 Py_DECREF(fullmodname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004722}
Guido van Rossumac7be682001-01-17 15:42:30 +00004723
Thomas Wouters52152252000-08-17 22:55:00 +00004724static int
4725import_all_from(PyObject *locals, PyObject *v)
4726{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004727 _Py_IDENTIFIER(__all__);
4728 _Py_IDENTIFIER(__dict__);
4729 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004730 PyObject *dict, *name, *value;
4731 int skip_leading_underscores = 0;
4732 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 if (all == NULL) {
4735 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4736 return -1; /* Unexpected error */
4737 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004738 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 if (dict == NULL) {
4740 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4741 return -1;
4742 PyErr_SetString(PyExc_ImportError,
4743 "from-import-* object has no __dict__ and no __all__");
4744 return -1;
4745 }
4746 all = PyMapping_Keys(dict);
4747 Py_DECREF(dict);
4748 if (all == NULL)
4749 return -1;
4750 skip_leading_underscores = 1;
4751 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 for (pos = 0, err = 0; ; pos++) {
4754 name = PySequence_GetItem(all, pos);
4755 if (name == NULL) {
4756 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4757 err = -1;
4758 else
4759 PyErr_Clear();
4760 break;
4761 }
4762 if (skip_leading_underscores &&
4763 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004764 PyUnicode_READY(name) != -1 &&
4765 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 {
4767 Py_DECREF(name);
4768 continue;
4769 }
4770 value = PyObject_GetAttr(v, name);
4771 if (value == NULL)
4772 err = -1;
4773 else if (PyDict_CheckExact(locals))
4774 err = PyDict_SetItem(locals, name, value);
4775 else
4776 err = PyObject_SetItem(locals, name, value);
4777 Py_DECREF(name);
4778 Py_XDECREF(value);
4779 if (err != 0)
4780 break;
4781 }
4782 Py_DECREF(all);
4783 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004784}
4785
Guido van Rossumac7be682001-01-17 15:42:30 +00004786static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004787format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004791 if (!obj)
4792 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 obj_str = _PyUnicode_AsString(obj);
4795 if (!obj_str)
4796 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004799}
Guido van Rossum950361c1997-01-24 13:49:28 +00004800
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004801static void
4802format_exc_unbound(PyCodeObject *co, int oparg)
4803{
4804 PyObject *name;
4805 /* Don't stomp existing exception */
4806 if (PyErr_Occurred())
4807 return;
4808 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4809 name = PyTuple_GET_ITEM(co->co_cellvars,
4810 oparg);
4811 format_exc_check_arg(
4812 PyExc_UnboundLocalError,
4813 UNBOUNDLOCAL_ERROR_MSG,
4814 name);
4815 } else {
4816 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4817 PyTuple_GET_SIZE(co->co_cellvars));
4818 format_exc_check_arg(PyExc_NameError,
4819 UNBOUNDFREE_ERROR_MSG, name);
4820 }
4821}
4822
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004823static PyObject *
4824unicode_concatenate(PyObject *v, PyObject *w,
4825 PyFrameObject *f, unsigned char *next_instr)
4826{
4827 PyObject *res;
4828 if (Py_REFCNT(v) == 2) {
4829 /* In the common case, there are 2 references to the value
4830 * stored in 'variable' when the += is performed: one on the
4831 * value stack (in 'v') and one still stored in the
4832 * 'variable'. We try to delete the variable now to reduce
4833 * the refcnt to 1.
4834 */
4835 switch (*next_instr) {
4836 case STORE_FAST:
4837 {
4838 int oparg = PEEKARG();
4839 PyObject **fastlocals = f->f_localsplus;
4840 if (GETLOCAL(oparg) == v)
4841 SETLOCAL(oparg, NULL);
4842 break;
4843 }
4844 case STORE_DEREF:
4845 {
4846 PyObject **freevars = (f->f_localsplus +
4847 f->f_code->co_nlocals);
4848 PyObject *c = freevars[PEEKARG()];
4849 if (PyCell_GET(c) == v)
4850 PyCell_Set(c, NULL);
4851 break;
4852 }
4853 case STORE_NAME:
4854 {
4855 PyObject *names = f->f_code->co_names;
4856 PyObject *name = GETITEM(names, PEEKARG());
4857 PyObject *locals = f->f_locals;
4858 if (PyDict_CheckExact(locals) &&
4859 PyDict_GetItem(locals, name) == v) {
4860 if (PyDict_DelItem(locals, name) != 0) {
4861 PyErr_Clear();
4862 }
4863 }
4864 break;
4865 }
4866 }
4867 }
4868 res = v;
4869 PyUnicode_Append(&res, w);
4870 return res;
4871}
4872
Guido van Rossum950361c1997-01-24 13:49:28 +00004873#ifdef DYNAMIC_EXECUTION_PROFILE
4874
Skip Montanarof118cb12001-10-15 20:51:38 +00004875static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004876getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 int i;
4879 PyObject *l = PyList_New(256);
4880 if (l == NULL) return NULL;
4881 for (i = 0; i < 256; i++) {
4882 PyObject *x = PyLong_FromLong(a[i]);
4883 if (x == NULL) {
4884 Py_DECREF(l);
4885 return NULL;
4886 }
4887 PyList_SetItem(l, i, x);
4888 }
4889 for (i = 0; i < 256; i++)
4890 a[i] = 0;
4891 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004892}
4893
4894PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004895_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004896{
4897#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004899#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004900 int i;
4901 PyObject *l = PyList_New(257);
4902 if (l == NULL) return NULL;
4903 for (i = 0; i < 257; i++) {
4904 PyObject *x = getarray(dxpairs[i]);
4905 if (x == NULL) {
4906 Py_DECREF(l);
4907 return NULL;
4908 }
4909 PyList_SetItem(l, i, x);
4910 }
4911 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004912#endif
4913}
4914
4915#endif