blob: 0e874b7f21c361c4fa16bf17d984a9a8ab99ea90 [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"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000019#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Thomas Wouters477c8d52006-05-27 19:21:47 +000023#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000024
25#define READ_TIMESTAMP(var)
26
27#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000028
29typedef unsigned long long uint64;
30
Ezio Melotti13925002011-03-16 11:05:33 +020031/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000032 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
33 "__powerpc__" appears to be the correct one for Linux with GCC
34*/
35#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Michael W. Hudson75eabd22005-01-18 15:56:11 +000037#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000038
39static void
40ppc_getcounter(uint64 *v)
41{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020042 unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000043
44 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 asm volatile ("mftbu %0" : "=r" (tbu) );
46 asm volatile ("mftb %0" : "=r" (tb) );
47 asm volatile ("mftbu %0" : "=r" (tbu2));
48 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* The slightly peculiar way of writing the next lines is
51 compiled better by GCC than any other way I tried. */
52 ((long*)(v))[0] = tbu;
53 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000054}
55
Mark Dickinsona25b1312009-10-31 10:18:44 +000056#elif defined(__i386__)
57
58/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000059
Michael W. Hudson75eabd22005-01-18 15:56:11 +000060#define READ_TIMESTAMP(val) \
61 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000062
Mark Dickinsona25b1312009-10-31 10:18:44 +000063#elif defined(__x86_64__)
64
65/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
66 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
67 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
68 32-bit pieces of the result. */
69
Victor Stinner0b881dd2014-12-12 13:17:41 +010070#define READ_TIMESTAMP(val) do { \
71 unsigned int h, l; \
72 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
73 (val) = ((uint64)l) | (((uint64)h) << 32); \
74 } while(0)
Mark Dickinsona25b1312009-10-31 10:18:44 +000075
76
77#else
78
79#error "Don't know how to implement timestamp counter for this architecture"
80
Michael W. Hudson800ba232004-08-12 18:19:17 +000081#endif
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 uint64 intr, inst, loop;
87 PyThreadState *tstate = PyThreadState_Get();
88 if (!tstate->interp->tscdump)
89 return;
90 intr = intr1 - intr0;
91 inst = inst1 - inst0 - intr;
92 loop = loop1 - loop0 - intr;
93 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000094 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095}
Michael W. Hudson800ba232004-08-12 18:19:17 +000096
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000097#endif
98
Guido van Rossum04691fc1992-08-12 15:35:34 +000099/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +0000101
Guido van Rossum408027e1996-12-30 16:17:54 +0000102#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000103/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104#define LLTRACE 1 /* Low-level trace feature */
105#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000106#endif
107
Jeremy Hylton52820442001-01-03 23:52:36 +0000108typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000109
Guido van Rossum374a9221991-04-04 10:40:29 +0000110/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#ifdef WITH_TSC
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700112static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#else
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700114static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000115#endif
Victor Stinnerd8735722016-09-09 12:36:44 -0700116static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700117static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
118static PyObject * create_keyword_args(PyObject *, PyObject ***, PyObject *);
Victor Stinner74319ae2016-08-25 00:04:09 +0200119static PyObject * load_args(PyObject ***, Py_ssize_t);
Jeremy Hylton52820442001-01-03 23:52:36 +0000120
Guido van Rossum0a066c01992-03-27 17:29:15 +0000121#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000122static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200123static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000124#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100125static int call_trace(Py_tracefunc, PyObject *,
126 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100129 PyThreadState *, PyFrameObject *,
130 int, PyObject *);
131static void call_exc_trace(Py_tracefunc, PyObject *,
132 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000133static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100134 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000135
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300137static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000138static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000139static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000140static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000141static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200142static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300143 PyFrameObject *, const unsigned short *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500144static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000145
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000148#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000150#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 "free variable '%.200s' referenced before assignment" \
152 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000153
Guido van Rossum950361c1997-01-24 13:49:28 +0000154/* Dynamic execution profile */
155#ifdef DYNAMIC_EXECUTION_PROFILE
156#ifdef DXPAIRS
157static long dxpairs[257][256];
158#define dxp dxpairs[256]
159#else
160static long dxp[256];
161#endif
162#endif
163
Jeremy Hylton985eba52003-02-05 23:13:00 +0000164/* Function call profile */
165#ifdef CALL_PROFILE
166#define PCALL_NUM 11
167static int pcall[PCALL_NUM];
168
169#define PCALL_ALL 0
170#define PCALL_FUNCTION 1
171#define PCALL_FAST_FUNCTION 2
172#define PCALL_FASTER_FUNCTION 3
173#define PCALL_METHOD 4
174#define PCALL_BOUND_METHOD 5
175#define PCALL_CFUNCTION 6
176#define PCALL_TYPE 7
177#define PCALL_GENERATOR 8
178#define PCALL_OTHER 9
179#define PCALL_POP 10
180
181/* Notes about the statistics
182
183 PCALL_FAST stats
184
185 FAST_FUNCTION means no argument tuple needs to be created.
186 FASTER_FUNCTION means that the fast-path frame setup code is used.
187
188 If there is a method call where the call can be optimized by changing
189 the argument tuple and calling the function directly, it gets recorded
190 twice.
191
192 As a result, the relationship among the statistics appears to be
193 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
194 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
195 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
196 PCALL_METHOD > PCALL_BOUND_METHOD
197*/
198
199#define PCALL(POS) pcall[POS]++
200
201PyObject *
202PyEval_GetCallStats(PyObject *self)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return Py_BuildValue("iiiiiiiiiii",
205 pcall[0], pcall[1], pcall[2], pcall[3],
206 pcall[4], pcall[5], pcall[6], pcall[7],
207 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000208}
209#else
210#define PCALL(O)
211
212PyObject *
213PyEval_GetCallStats(PyObject *self)
214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_INCREF(Py_None);
216 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000217}
218#endif
219
Tim Peters5ca576e2001-06-18 22:08:13 +0000220
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000221#ifdef WITH_THREAD
222#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
223#else
224#define GIL_REQUEST 0
225#endif
226
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000227/* This can set eval_breaker to 0 even though gil_drop_request became
228 1. We believe this is all right because the eval loop will release
229 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000230#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 _Py_atomic_store_relaxed( \
232 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000233 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
235 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000236
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000237#ifdef WITH_THREAD
238
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000239#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 do { \
241 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
242 _Py_atomic_store_relaxed(&eval_breaker, 1); \
243 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000244
245#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 do { \
247 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
248 COMPUTE_EVAL_BREAKER(); \
249 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000250
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000251#endif
252
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000253/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 do { \
256 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
257 _Py_atomic_store_relaxed(&eval_breaker, 1); \
258 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259
260#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 do { \
262 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
263 COMPUTE_EVAL_BREAKER(); \
264 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000265
266#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 do { \
268 pending_async_exc = 1; \
269 _Py_atomic_store_relaxed(&eval_breaker, 1); \
270 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000271
272#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000274
275
Guido van Rossume59214e1994-08-30 08:01:59 +0000276#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000277
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000278#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000280#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000281#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000282
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000283static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000284static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000285/* This single variable consolidates all requests to break out of the fast path
286 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000287static _Py_atomic_int eval_breaker = {0};
288/* Request for dropping the GIL */
289static _Py_atomic_int gil_drop_request = {0};
290/* Request for running pending calls. */
291static _Py_atomic_int pendingcalls_to_do = {0};
292/* Request for looking at the `async_exc` field of the current thread state.
293 Guarded by the GIL. */
294static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000295
296#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000297
Tim Peters7f468f22004-10-11 02:40:51 +0000298int
299PyEval_ThreadsInitialized(void)
300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000302}
303
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (gil_created())
308 return;
309 create_gil();
310 take_gil(PyThreadState_GET());
311 main_thread = PyThread_get_thread_ident();
312 if (!pending_lock)
313 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000314}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000315
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000316void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000317_PyEval_FiniThreads(void)
318{
319 if (!gil_created())
320 return;
321 destroy_gil();
322 assert(!gil_created());
323}
324
325void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PyThreadState *tstate = PyThreadState_GET();
329 if (tstate == NULL)
330 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
331 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000332}
333
334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* This function must succeed when the current thread state is NULL.
338 We therefore avoid PyThreadState_GET() which dumps a fatal error
339 in debug mode.
340 */
341 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
342 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000343}
344
345void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (tstate == NULL)
349 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
350 /* Check someone has called PyEval_InitThreads() to create the lock */
351 assert(gil_created());
352 take_gil(tstate);
353 if (PyThreadState_Swap(tstate) != NULL)
354 Py_FatalError(
355 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000356}
357
358void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (tstate == NULL)
362 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
363 if (PyThreadState_Swap(NULL) != tstate)
364 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
365 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000366}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000367
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200368/* This function is called from PyOS_AfterFork to destroy all threads which are
369 * not running in the child process, and clear internal locks which might be
370 * held by those threads. (This could also be done using pthread_atfork
371 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000372
373void
374PyEval_ReInitThreads(void)
375{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200376 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200378 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (!gil_created())
381 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 recreate_gil();
383 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200384 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 /* Update the threading module with the new state.
388 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200389 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 "threading");
391 if (threading == NULL) {
392 /* threading not imported */
393 PyErr_Clear();
394 return;
395 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200396 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (result == NULL)
398 PyErr_WriteUnraisable(threading);
399 else
400 Py_DECREF(result);
401 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200402
403 /* Destroy all threads except the current one */
404 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000405}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000406
407#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000408static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000409static int pending_async_exc = 0;
410#endif /* WITH_THREAD */
411
412/* This function is used to signal that async exceptions are waiting to be
413 raised, therefore it is also useful in non-threaded builds. */
414
415void
416_PyEval_SignalAsyncExc(void)
417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000419}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000420
Guido van Rossumff4949e1992-08-05 19:58:53 +0000421/* Functions save_thread and restore_thread are always defined so
422 dynamically loaded modules needn't be compiled separately for use
423 with and without threads: */
424
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000425PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000426PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyThreadState *tstate = PyThreadState_Swap(NULL);
429 if (tstate == NULL)
430 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000431#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (gil_created())
433 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000434#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436}
437
438void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000439PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (tstate == NULL)
442 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000443#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 if (gil_created()) {
445 int err = errno;
446 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200447 /* _Py_Finalizing is protected by the GIL */
448 if (_Py_Finalizing && tstate != _Py_Finalizing) {
449 drop_gil(tstate);
450 PyThread_exit_thread();
451 assert(0); /* unreachable */
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 errno = err;
454 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000457}
458
459
Guido van Rossuma9672091994-09-14 13:31:22 +0000460/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
461 signal handlers or Mac I/O completion routines) can schedule calls
462 to a function to be called synchronously.
463 The synchronous function is called with one void* argument.
464 It should return 0 for success or -1 for failure -- failure should
465 be accompanied by an exception.
466
467 If registry succeeds, the registry function returns 0; if it fails
468 (e.g. due to too many pending calls) it returns -1 (without setting
469 an exception condition).
470
471 Note that because registry may occur from within signal handlers,
472 or other asynchronous events, calling malloc() is unsafe!
473
474#ifdef WITH_THREAD
475 Any thread can schedule pending calls, but only the main thread
476 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477 There is no facility to schedule calls to a particular thread, but
478 that should be easy to change, should that ever be required. In
479 that case, the static variables here should go into the python
480 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000481#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000482*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000483
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484#ifdef WITH_THREAD
485
486/* The WITH_THREAD implementation is thread-safe. It allows
487 scheduling to be made from any thread, and even from an executing
488 callback.
489 */
490
491#define NPENDINGCALLS 32
492static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 int (*func)(void *);
494 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495} pendingcalls[NPENDINGCALLS];
496static int pendingfirst = 0;
497static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498
499int
500Py_AddPendingCall(int (*func)(void *), void *arg)
501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 int i, j, result=0;
503 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* try a few times for the lock. Since this mechanism is used
506 * for signal handling (on the main thread), there is a (slim)
507 * chance that a signal is delivered on the same thread while we
508 * hold the lock during the Py_MakePendingCalls() function.
509 * This avoids a deadlock in that case.
510 * Note that signals can be delivered on any thread. In particular,
511 * on Windows, a SIGINT is delivered on a system-created worker
512 * thread.
513 * We also check for lock being NULL, in the unlikely case that
514 * this function is called before any bytecode evaluation takes place.
515 */
516 if (lock != NULL) {
517 for (i = 0; i<100; i++) {
518 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
519 break;
520 }
521 if (i == 100)
522 return -1;
523 }
524
525 i = pendinglast;
526 j = (i + 1) % NPENDINGCALLS;
527 if (j == pendingfirst) {
528 result = -1; /* Queue full */
529 } else {
530 pendingcalls[i].func = func;
531 pendingcalls[i].arg = arg;
532 pendinglast = j;
533 }
534 /* signal main loop */
535 SIGNAL_PENDING_CALLS();
536 if (lock != NULL)
537 PyThread_release_lock(lock);
538 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000539}
540
541int
542Py_MakePendingCalls(void)
543{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200544 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 int i;
546 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (!pending_lock) {
549 /* initial allocation of the lock */
550 pending_lock = PyThread_allocate_lock();
551 if (pending_lock == NULL)
552 return -1;
553 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* only service pending calls on main thread */
556 if (main_thread && PyThread_get_thread_ident() != main_thread)
557 return 0;
558 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200559 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200561 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* perform a bounded number of calls, in case of recursion */
563 for (i=0; i<NPENDINGCALLS; i++) {
564 int j;
565 int (*func)(void *);
566 void *arg = NULL;
567
568 /* pop one item off the queue while holding the lock */
569 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
570 j = pendingfirst;
571 if (j == pendinglast) {
572 func = NULL; /* Queue empty */
573 } else {
574 func = pendingcalls[j].func;
575 arg = pendingcalls[j].arg;
576 pendingfirst = (j + 1) % NPENDINGCALLS;
577 }
578 if (pendingfirst != pendinglast)
579 SIGNAL_PENDING_CALLS();
580 else
581 UNSIGNAL_PENDING_CALLS();
582 PyThread_release_lock(pending_lock);
583 /* having released the lock, perform the callback */
584 if (func == NULL)
585 break;
586 r = func(arg);
587 if (r)
588 break;
589 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200590 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000592}
593
594#else /* if ! defined WITH_THREAD */
595
596/*
597 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
598 This code is used for signal handling in python that isn't built
599 with WITH_THREAD.
600 Don't use this implementation when Py_AddPendingCalls() can happen
601 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602
Guido van Rossuma9672091994-09-14 13:31:22 +0000603 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604 (1) nested asynchronous calls to Py_AddPendingCall()
605 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000607 (1) is very unlikely because typically signal delivery
608 is blocked during signal handling. So it should be impossible.
609 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000610 The current code is safe against (2), but not against (1).
611 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000612 thread is present, interrupted by signals, and that the critical
613 section is protected with the "busy" variable. On Windows, which
614 delivers SIGINT on a system thread, this does not hold and therefore
615 Windows really shouldn't use this version.
616 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000617*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000618
Guido van Rossuma9672091994-09-14 13:31:22 +0000619#define NPENDINGCALLS 32
620static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 int (*func)(void *);
622 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000623} pendingcalls[NPENDINGCALLS];
624static volatile int pendingfirst = 0;
625static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000626static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000627
628int
Thomas Wouters334fb892000-07-25 12:56:38 +0000629Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 static volatile int busy = 0;
632 int i, j;
633 /* XXX Begin critical section */
634 if (busy)
635 return -1;
636 busy = 1;
637 i = pendinglast;
638 j = (i + 1) % NPENDINGCALLS;
639 if (j == pendingfirst) {
640 busy = 0;
641 return -1; /* Queue full */
642 }
643 pendingcalls[i].func = func;
644 pendingcalls[i].arg = arg;
645 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 SIGNAL_PENDING_CALLS();
648 busy = 0;
649 /* XXX End critical section */
650 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000651}
652
Guido van Rossum180d7b41994-09-29 09:45:57 +0000653int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 static int busy = 0;
657 if (busy)
658 return 0;
659 busy = 1;
660 UNSIGNAL_PENDING_CALLS();
661 for (;;) {
662 int i;
663 int (*func)(void *);
664 void *arg;
665 i = pendingfirst;
666 if (i == pendinglast)
667 break; /* Queue empty */
668 func = pendingcalls[i].func;
669 arg = pendingcalls[i].arg;
670 pendingfirst = (i + 1) % NPENDINGCALLS;
671 if (func(arg) < 0) {
672 busy = 0;
673 SIGNAL_PENDING_CALLS(); /* We're not done yet */
674 return -1;
675 }
676 }
677 busy = 0;
678 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000679}
680
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000681#endif /* WITH_THREAD */
682
Guido van Rossuma9672091994-09-14 13:31:22 +0000683
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000684/* The interpreter's recursion limit */
685
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000686#ifndef Py_DEFAULT_RECURSION_LIMIT
687#define Py_DEFAULT_RECURSION_LIMIT 1000
688#endif
689static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
690int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000692int
693Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696}
697
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000698void
699Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 recursion_limit = new_limit;
702 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000703}
704
Armin Rigo2b3eb402003-10-28 12:05:48 +0000705/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
706 if the recursion_depth reaches _Py_CheckRecursionLimit.
707 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
708 to guarantee that _Py_CheckRecursiveCall() is regularly called.
709 Without USE_STACKCHECK, there is no need for this. */
710int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300711_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000714
715#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (PyOS_CheckStack()) {
717 --tstate->recursion_depth;
718 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
719 return -1;
720 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000721#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 _Py_CheckRecursionLimit = recursion_limit;
723 if (tstate->recursion_critical)
724 /* Somebody asked that we don't check for recursion. */
725 return 0;
726 if (tstate->overflowed) {
727 if (tstate->recursion_depth > recursion_limit + 50) {
728 /* Overflowing while handling an overflow. Give up. */
729 Py_FatalError("Cannot recover from stack overflow.");
730 }
731 return 0;
732 }
733 if (tstate->recursion_depth > recursion_limit) {
734 --tstate->recursion_depth;
735 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400736 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 "maximum recursion depth exceeded%s",
738 where);
739 return -1;
740 }
741 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000742}
743
Guido van Rossum374a9221991-04-04 10:40:29 +0000744/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000745enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000746 WHY_NOT = 0x0001, /* No error */
747 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000748 WHY_RETURN = 0x0008, /* 'return' statement */
749 WHY_BREAK = 0x0010, /* 'break' statement */
750 WHY_CONTINUE = 0x0020, /* 'continue' statement */
751 WHY_YIELD = 0x0040, /* 'yield' operator */
752 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000753};
Guido van Rossum374a9221991-04-04 10:40:29 +0000754
Benjamin Peterson87880242011-07-03 16:48:31 -0500755static void save_exc_state(PyThreadState *, PyFrameObject *);
756static void swap_exc_state(PyThreadState *, PyFrameObject *);
757static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400758static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000759static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000760
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000761/* Records whether tracing is on for any thread. Counts the number of
762 threads for which tstate->c_tracefunc is non-NULL, so if the value
763 is 0, we know we don't have to check this thread's c_tracefunc.
764 This speeds up the if statement in PyEval_EvalFrameEx() after
765 fast_next_opcode*/
766static int _Py_TracingPossible = 0;
767
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000768
Guido van Rossum374a9221991-04-04 10:40:29 +0000769
Guido van Rossumb209a111997-04-29 18:18:01 +0000770PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000771PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 return PyEval_EvalCodeEx(co,
774 globals, locals,
775 (PyObject **)NULL, 0,
776 (PyObject **)NULL, 0,
777 (PyObject **)NULL, 0,
778 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000779}
780
781
782/* Interpreter main loop */
783
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000784PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000785PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 /* This is for backward compatibility with extension modules that
787 used this API; core interpreter code should call
788 PyEval_EvalFrameEx() */
789 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000790}
791
792PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000793PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000794{
Brett Cannon3cebf932016-09-05 15:33:46 -0700795 PyThreadState *tstate = PyThreadState_GET();
796 return tstate->interp->eval_frame(f, throwflag);
797}
798
799PyObject *
800_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
801{
Guido van Rossum950361c1997-01-24 13:49:28 +0000802#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000804#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200805 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300806 const unsigned short *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200807 int opcode; /* Current opcode */
808 int oparg; /* Current opcode argument, if any */
809 enum why_code why; /* Reason for block stack unwind */
810 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 PyObject *retval = NULL; /* Return value */
812 PyThreadState *tstate = PyThreadState_GET();
813 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 is true when the line being executed has changed. The
820 initial values are such as to make this false the first
821 time it is tested. */
822 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000823
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300824 const unsigned short *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 PyObject *names;
826 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000827
Brett Cannon368b4b72012-04-02 12:17:59 -0400828#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200829 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400830#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200831
Antoine Pitroub52ec782009-01-25 16:34:23 +0000832/* Computed GOTOs, or
833 the-optimization-commonly-but-improperly-known-as-"threaded code"
834 using gcc's labels-as-values extension
835 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
836
837 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000839 combined with a lookup table of jump addresses. However, since the
840 indirect jump instruction is shared by all opcodes, the CPU will have a
841 hard time making the right prediction for where to jump next (actually,
842 it will be always wrong except in the uncommon case of a sequence of
843 several identical opcodes).
844
845 "Threaded code" in contrast, uses an explicit jump table and an explicit
846 indirect jump instruction at the end of each opcode. Since the jump
847 instruction is at a different address for each opcode, the CPU will make a
848 separate prediction for each of these instructions, which is equivalent to
849 predicting the second opcode of each opcode pair. These predictions have
850 a much better chance to turn out valid, especially in small bytecode loops.
851
852 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000854 and potentially many more instructions (depending on the pipeline width).
855 A correctly predicted branch, however, is nearly free.
856
857 At the time of this writing, the "threaded code" version is up to 15-20%
858 faster than the normal "switch" version, depending on the compiler and the
859 CPU architecture.
860
861 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
862 because it would render the measurements invalid.
863
864
865 NOTE: care must be taken that the compiler doesn't try to "optimize" the
866 indirect jumps by sharing them between all opcodes. Such optimizations
867 can be disabled on gcc by using the -fno-gcse flag (or possibly
868 -fno-crossjumping).
869*/
870
Antoine Pitrou042b1282010-08-13 21:15:58 +0000871#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000873#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000874#endif
875
Antoine Pitrou042b1282010-08-13 21:15:58 +0000876#ifdef HAVE_COMPUTED_GOTOS
877 #ifndef USE_COMPUTED_GOTOS
878 #define USE_COMPUTED_GOTOS 1
879 #endif
880#else
881 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
882 #error "Computed gotos are not supported on this compiler."
883 #endif
884 #undef USE_COMPUTED_GOTOS
885 #define USE_COMPUTED_GOTOS 0
886#endif
887
888#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000889/* Import the static jump table */
890#include "opcode_targets.h"
891
Antoine Pitroub52ec782009-01-25 16:34:23 +0000892#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000895
Antoine Pitroub52ec782009-01-25 16:34:23 +0000896#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 { \
898 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
899 FAST_DISPATCH(); \
900 } \
901 continue; \
902 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000903
904#ifdef LLTRACE
905#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 { \
907 if (!lltrace && !_Py_TracingPossible) { \
908 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300909 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300910 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 } \
912 goto fast_next_opcode; \
913 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000914#else
915#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 { \
917 if (!_Py_TracingPossible) { \
918 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300919 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300920 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 } \
922 goto fast_next_opcode; \
923 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000924#endif
925
926#else
927#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300929
Antoine Pitroub52ec782009-01-25 16:34:23 +0000930#define DISPATCH() continue
931#define FAST_DISPATCH() goto fast_next_opcode
932#endif
933
934
Neal Norwitza81d2202002-07-14 00:27:26 +0000935/* Tuple access macros */
936
937#ifndef Py_DEBUG
938#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
939#else
940#define GETITEM(v, i) PyTuple_GetItem((v), (i))
941#endif
942
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000943#ifdef WITH_TSC
944/* Use Pentium timestamp counter to mark certain events:
945 inst0 -- beginning of switch statement for opcode dispatch
946 inst1 -- end of switch statement (may be skipped)
947 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000948 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000949 (may be skipped)
950 intr1 -- beginning of long interruption
951 intr2 -- end of long interruption
952
953 Many opcodes call out to helper C functions. In some cases, the
954 time in those functions should be counted towards the time for the
955 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
956 calls another Python function; there's no point in charge all the
957 bytecode executed by the called function to the caller.
958
959 It's hard to make a useful judgement statically. In the presence
960 of operator overloading, it's impossible to tell if a call will
961 execute new Python code or not.
962
963 It's a case-by-case judgement. I'll use intr1 for the following
964 cases:
965
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000966 IMPORT_STAR
967 IMPORT_FROM
968 CALL_FUNCTION (and friends)
969
970 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
972 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000973
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 READ_TIMESTAMP(inst0);
975 READ_TIMESTAMP(inst1);
976 READ_TIMESTAMP(loop0);
977 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 /* shut up the compiler */
980 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000981#endif
982
Guido van Rossum374a9221991-04-04 10:40:29 +0000983/* Code access macros */
984
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300985#ifdef WORDS_BIGENDIAN
986 #define OPCODE(word) ((word) >> 8)
987 #define OPARG(word) ((word) & 255)
988#else
989 #define OPCODE(word) ((word) & 255)
990 #define OPARG(word) ((word) >> 8)
991#endif
992/* The integer overflow is checked by an assertion below. */
993#define INSTR_OFFSET() (2*(int)(next_instr - first_instr))
994#define NEXTOPARG() do { \
995 unsigned short word = *next_instr; \
996 opcode = OPCODE(word); \
997 oparg = OPARG(word); \
998 next_instr++; \
999 } while (0)
1000#define JUMPTO(x) (next_instr = first_instr + (x)/2)
1001#define JUMPBY(x) (next_instr += (x)/2)
Guido van Rossum374a9221991-04-04 10:40:29 +00001002
Raymond Hettingerf606f872003-03-16 03:11:04 +00001003/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 Some opcodes tend to come in pairs thus making it possible to
1005 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001006 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
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
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001013 including its unpredictable switch-case branch. Combined with the
1014 processor's internal branch prediction, a successful PREDICT has the
1015 effect of making the two opcodes run as if they were a single new opcode
1016 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
Raymond Hettingera7216982004-02-08 19:59:27 +00001031#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001032#define PREDICT(op) \
1033 do{ \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001034 unsigned short word = *next_instr; \
1035 opcode = OPCODE(word); \
1036 if (opcode == op){ \
1037 oparg = OPARG(word); \
1038 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001039 goto PRED_##op; \
1040 } \
1041 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001042#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001043#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +00001044
Raymond Hettingerf606f872003-03-16 03:11:04 +00001045
Guido van Rossum374a9221991-04-04 10:40:29 +00001046/* Stack manipulation macros */
1047
Martin v. Löwis18e16552006-02-15 17:27:45 +00001048/* The stack can grow at most MAXINT deep, as co_nlocals and
1049 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001050#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1051#define EMPTY() (STACK_LEVEL() == 0)
1052#define TOP() (stack_pointer[-1])
1053#define SECOND() (stack_pointer[-2])
1054#define THIRD() (stack_pointer[-3])
1055#define FOURTH() (stack_pointer[-4])
1056#define PEEK(n) (stack_pointer[-(n)])
1057#define SET_TOP(v) (stack_pointer[-1] = (v))
1058#define SET_SECOND(v) (stack_pointer[-2] = (v))
1059#define SET_THIRD(v) (stack_pointer[-3] = (v))
1060#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1061#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1062#define BASIC_STACKADJ(n) (stack_pointer += n)
1063#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1064#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001065
Guido van Rossum96a42c81992-01-12 02:29:51 +00001066#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001068 lltrace && prtrace(TOP(), "push")); \
1069 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001071 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001073 lltrace && prtrace(TOP(), "stackadj")); \
1074 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001075#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001076 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1077 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001078#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001079#define PUSH(v) BASIC_PUSH(v)
1080#define POP() BASIC_POP()
1081#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001082#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001083#endif
1084
Guido van Rossum681d79a1995-07-18 14:51:37 +00001085/* Local variable macros */
1086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001088
1089/* The SETLOCAL() macro must not DECREF the local variable in-place and
1090 then store the new value; it must copy the old value to a temporary
1091 value, then store the new value, and then DECREF the temporary value.
1092 This is because it is possible that during the DECREF the frame is
1093 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1094 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001096 GETLOCAL(i) = value; \
1097 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001098
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001099
1100#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 while (STACK_LEVEL() > (b)->b_level) { \
1102 PyObject *v = POP(); \
1103 Py_XDECREF(v); \
1104 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001105
1106#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001107 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 PyObject *type, *value, *traceback; \
1109 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1110 while (STACK_LEVEL() > (b)->b_level + 3) { \
1111 value = POP(); \
1112 Py_XDECREF(value); \
1113 } \
1114 type = tstate->exc_type; \
1115 value = tstate->exc_value; \
1116 traceback = tstate->exc_traceback; \
1117 tstate->exc_type = POP(); \
1118 tstate->exc_value = POP(); \
1119 tstate->exc_traceback = POP(); \
1120 Py_XDECREF(type); \
1121 Py_XDECREF(value); \
1122 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001123 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001124
Guido van Rossuma027efa1997-05-05 20:56:21 +00001125/* Start of code */
1126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 /* push frame */
1128 if (Py_EnterRecursiveCall(""))
1129 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 if (tstate->use_tracing) {
1134 if (tstate->c_tracefunc != NULL) {
1135 /* tstate->c_tracefunc, if defined, is a
1136 function that will be called on *every* entry
1137 to a code block. Its return value, if not
1138 None, is a function that will be called at
1139 the start of each executed line of code.
1140 (Actually, the function must return itself
1141 in order to continue tracing.) The trace
1142 functions are called with three arguments:
1143 a pointer to the current frame, a string
1144 indicating why the function is called, and
1145 an argument which depends on the situation.
1146 The global trace function is also called
1147 whenever an exception is detected. */
1148 if (call_trace_protected(tstate->c_tracefunc,
1149 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001150 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 /* Trace function raised an error */
1152 goto exit_eval_frame;
1153 }
1154 }
1155 if (tstate->c_profilefunc != NULL) {
1156 /* Similar for c_profilefunc, except it needn't
1157 return itself and isn't called for "line" events */
1158 if (call_trace_protected(tstate->c_profilefunc,
1159 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001160 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 /* Profile function raised an error */
1162 goto exit_eval_frame;
1163 }
1164 }
1165 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 co = f->f_code;
1168 names = co->co_names;
1169 consts = co->co_consts;
1170 fastlocals = f->f_localsplus;
1171 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001172 assert(PyBytes_Check(co->co_code));
1173 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
1174 assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0);
Serhiy Storchaka74f2fe62016-05-25 20:35:44 +03001175 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2));
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001176 first_instr = (unsigned short*) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001177 /*
1178 f->f_lasti refers to the index of the last instruction,
1179 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001180
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001181 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001182 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 When the PREDICT() macros are enabled, some opcode pairs follow in
1185 direct succession without updating f->f_lasti. A successful
1186 prediction effectively links the two codes together as if they
1187 were a single new opcode; accordingly,f->f_lasti will point to
1188 the first code in the pair (for instance, GET_ITER followed by
1189 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001190 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001192 next_instr = first_instr;
1193 if (f->f_lasti >= 0) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001194 assert(f->f_lasti % 2 == 0);
1195 next_instr += f->f_lasti/2 + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001196 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 stack_pointer = f->f_stacktop;
1198 assert(stack_pointer != NULL);
1199 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001200 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001201
Yury Selivanoveb636452016-09-08 22:01:51 -07001202 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001203 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 /* We were in an except handler when we left,
1205 restore the exception state which was put aside
1206 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001207 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001209 else
1210 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001212
Tim Peters5ca576e2001-06-18 22:08:13 +00001213#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001214 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001215#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001219 if (throwflag) /* support for generator.throw() */
1220 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001221
Victor Stinnerace47d72013-07-18 01:41:08 +02001222#ifdef Py_DEBUG
1223 /* PyEval_EvalFrameEx() must not be called with an exception set,
1224 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001225 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001226 assert(!PyErr_Occurred());
1227#endif
1228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001230#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001231 if (inst1 == 0) {
1232 /* Almost surely, the opcode executed a break
1233 or a continue, preventing inst1 from being set
1234 on the way out of the loop.
1235 */
1236 READ_TIMESTAMP(inst1);
1237 loop1 = inst1;
1238 }
1239 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1240 intr0, intr1);
1241 ticked = 0;
1242 inst1 = 0;
1243 intr0 = 0;
1244 intr1 = 0;
1245 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001246#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1248 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001249 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 /* Do periodic things. Doing this every time through
1252 the loop would add too much overhead, so we do it
1253 only every Nth instruction. We also do it if
1254 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1255 event needs attention (e.g. a signal handler or
1256 async I/O handler); see Py_AddPendingCall() and
1257 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001260 if (OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001262 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 goto fast_next_opcode;
1264 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001265#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001267#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001269 if (Py_MakePendingCalls() < 0)
1270 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001272#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001273 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 /* Give another thread a chance */
1275 if (PyThreadState_Swap(NULL) != tstate)
1276 Py_FatalError("ceval: tstate mix-up");
1277 drop_gil(tstate);
1278
1279 /* Other threads may run now */
1280
1281 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001282
1283 /* Check if we should make a quick exit. */
1284 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1285 drop_gil(tstate);
1286 PyThread_exit_thread();
1287 }
1288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (PyThreadState_Swap(tstate) != NULL)
1290 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001292#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* Check for asynchronous exceptions. */
1294 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001295 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 tstate->async_exc = NULL;
1297 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 PyErr_SetNone(exc);
1299 Py_DECREF(exc);
1300 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 }
1302 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 fast_next_opcode:
1305 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001310 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001311 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* see maybe_call_line_trace
1313 for expository comments */
1314 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 err = maybe_call_line_trace(tstate->c_tracefunc,
1317 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001318 tstate, f,
1319 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 /* Reload possibly changed frame fields */
1321 JUMPTO(f->f_lasti);
1322 if (f->f_stacktop != NULL) {
1323 stack_pointer = f->f_stacktop;
1324 f->f_stacktop = NULL;
1325 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001326 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001328 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001332
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001333 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001334 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001335#ifdef DYNAMIC_EXECUTION_PROFILE
1336#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 dxpairs[lastopcode][opcode]++;
1338 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001339#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001341#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001342
Guido van Rossum96a42c81992-01-12 02:29:51 +00001343#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 if (lltrace) {
1347 if (HAS_ARG(opcode)) {
1348 printf("%d: %d, %d\n",
1349 f->f_lasti, opcode, oparg);
1350 }
1351 else {
1352 printf("%d: %d\n",
1353 f->f_lasti, opcode);
1354 }
1355 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001356#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 /* Main switch on opcode */
1359 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* BEWARE!
1364 It is essential that any operation that fails sets either
1365 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1366 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 TARGET(NOP)
1369 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001370
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 TARGET(LOAD_FAST) {
1372 PyObject *value = GETLOCAL(oparg);
1373 if (value == NULL) {
1374 format_exc_check_arg(PyExc_UnboundLocalError,
1375 UNBOUNDLOCAL_ERROR_MSG,
1376 PyTuple_GetItem(co->co_varnames, oparg));
1377 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001379 Py_INCREF(value);
1380 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001382 }
1383
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001384 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001385 TARGET(LOAD_CONST) {
1386 PyObject *value = GETITEM(consts, oparg);
1387 Py_INCREF(value);
1388 PUSH(value);
1389 FAST_DISPATCH();
1390 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001391
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001392 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 TARGET(STORE_FAST) {
1394 PyObject *value = POP();
1395 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001399 TARGET(POP_TOP) {
1400 PyObject *value = POP();
1401 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 TARGET(ROT_TWO) {
1406 PyObject *top = TOP();
1407 PyObject *second = SECOND();
1408 SET_TOP(second);
1409 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001412
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001413 TARGET(ROT_THREE) {
1414 PyObject *top = TOP();
1415 PyObject *second = SECOND();
1416 PyObject *third = THIRD();
1417 SET_TOP(second);
1418 SET_SECOND(third);
1419 SET_THIRD(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) {
1424 PyObject *top = TOP();
1425 Py_INCREF(top);
1426 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001428 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001429
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 TARGET(DUP_TOP_TWO) {
1431 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001433 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001435 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001436 SET_TOP(top);
1437 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001438 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 TARGET(UNARY_POSITIVE) {
1442 PyObject *value = TOP();
1443 PyObject *res = PyNumber_Positive(value);
1444 Py_DECREF(value);
1445 SET_TOP(res);
1446 if (res == NULL)
1447 goto error;
1448 DISPATCH();
1449 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001451 TARGET(UNARY_NEGATIVE) {
1452 PyObject *value = TOP();
1453 PyObject *res = PyNumber_Negative(value);
1454 Py_DECREF(value);
1455 SET_TOP(res);
1456 if (res == NULL)
1457 goto error;
1458 DISPATCH();
1459 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001460
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 TARGET(UNARY_NOT) {
1462 PyObject *value = TOP();
1463 int err = PyObject_IsTrue(value);
1464 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 if (err == 0) {
1466 Py_INCREF(Py_True);
1467 SET_TOP(Py_True);
1468 DISPATCH();
1469 }
1470 else if (err > 0) {
1471 Py_INCREF(Py_False);
1472 SET_TOP(Py_False);
1473 err = 0;
1474 DISPATCH();
1475 }
1476 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 goto error;
1478 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001479
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001480 TARGET(UNARY_INVERT) {
1481 PyObject *value = TOP();
1482 PyObject *res = PyNumber_Invert(value);
1483 Py_DECREF(value);
1484 SET_TOP(res);
1485 if (res == NULL)
1486 goto error;
1487 DISPATCH();
1488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001489
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 TARGET(BINARY_POWER) {
1491 PyObject *exp = POP();
1492 PyObject *base = TOP();
1493 PyObject *res = PyNumber_Power(base, exp, Py_None);
1494 Py_DECREF(base);
1495 Py_DECREF(exp);
1496 SET_TOP(res);
1497 if (res == NULL)
1498 goto error;
1499 DISPATCH();
1500 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001502 TARGET(BINARY_MULTIPLY) {
1503 PyObject *right = POP();
1504 PyObject *left = TOP();
1505 PyObject *res = PyNumber_Multiply(left, right);
1506 Py_DECREF(left);
1507 Py_DECREF(right);
1508 SET_TOP(res);
1509 if (res == NULL)
1510 goto error;
1511 DISPATCH();
1512 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Benjamin Petersond51374e2014-04-09 23:55:56 -04001514 TARGET(BINARY_MATRIX_MULTIPLY) {
1515 PyObject *right = POP();
1516 PyObject *left = TOP();
1517 PyObject *res = PyNumber_MatrixMultiply(left, right);
1518 Py_DECREF(left);
1519 Py_DECREF(right);
1520 SET_TOP(res);
1521 if (res == NULL)
1522 goto error;
1523 DISPATCH();
1524 }
1525
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001526 TARGET(BINARY_TRUE_DIVIDE) {
1527 PyObject *divisor = POP();
1528 PyObject *dividend = TOP();
1529 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1530 Py_DECREF(dividend);
1531 Py_DECREF(divisor);
1532 SET_TOP(quotient);
1533 if (quotient == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 TARGET(BINARY_FLOOR_DIVIDE) {
1539 PyObject *divisor = POP();
1540 PyObject *dividend = TOP();
1541 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1542 Py_DECREF(dividend);
1543 Py_DECREF(divisor);
1544 SET_TOP(quotient);
1545 if (quotient == NULL)
1546 goto error;
1547 DISPATCH();
1548 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001549
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001550 TARGET(BINARY_MODULO) {
1551 PyObject *divisor = POP();
1552 PyObject *dividend = TOP();
1553 PyObject *res = PyUnicode_CheckExact(dividend) ?
1554 PyUnicode_Format(dividend, divisor) :
1555 PyNumber_Remainder(dividend, divisor);
1556 Py_DECREF(divisor);
1557 Py_DECREF(dividend);
1558 SET_TOP(res);
1559 if (res == NULL)
1560 goto error;
1561 DISPATCH();
1562 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001564 TARGET(BINARY_ADD) {
1565 PyObject *right = POP();
1566 PyObject *left = TOP();
1567 PyObject *sum;
1568 if (PyUnicode_CheckExact(left) &&
1569 PyUnicode_CheckExact(right)) {
1570 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001571 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001572 }
1573 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 sum = PyNumber_Add(left, right);
1575 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001576 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001577 Py_DECREF(right);
1578 SET_TOP(sum);
1579 if (sum == NULL)
1580 goto error;
1581 DISPATCH();
1582 }
1583
1584 TARGET(BINARY_SUBTRACT) {
1585 PyObject *right = POP();
1586 PyObject *left = TOP();
1587 PyObject *diff = PyNumber_Subtract(left, right);
1588 Py_DECREF(right);
1589 Py_DECREF(left);
1590 SET_TOP(diff);
1591 if (diff == NULL)
1592 goto error;
1593 DISPATCH();
1594 }
1595
1596 TARGET(BINARY_SUBSCR) {
1597 PyObject *sub = POP();
1598 PyObject *container = TOP();
1599 PyObject *res = PyObject_GetItem(container, sub);
1600 Py_DECREF(container);
1601 Py_DECREF(sub);
1602 SET_TOP(res);
1603 if (res == NULL)
1604 goto error;
1605 DISPATCH();
1606 }
1607
1608 TARGET(BINARY_LSHIFT) {
1609 PyObject *right = POP();
1610 PyObject *left = TOP();
1611 PyObject *res = PyNumber_Lshift(left, right);
1612 Py_DECREF(left);
1613 Py_DECREF(right);
1614 SET_TOP(res);
1615 if (res == NULL)
1616 goto error;
1617 DISPATCH();
1618 }
1619
1620 TARGET(BINARY_RSHIFT) {
1621 PyObject *right = POP();
1622 PyObject *left = TOP();
1623 PyObject *res = PyNumber_Rshift(left, right);
1624 Py_DECREF(left);
1625 Py_DECREF(right);
1626 SET_TOP(res);
1627 if (res == NULL)
1628 goto error;
1629 DISPATCH();
1630 }
1631
1632 TARGET(BINARY_AND) {
1633 PyObject *right = POP();
1634 PyObject *left = TOP();
1635 PyObject *res = PyNumber_And(left, right);
1636 Py_DECREF(left);
1637 Py_DECREF(right);
1638 SET_TOP(res);
1639 if (res == NULL)
1640 goto error;
1641 DISPATCH();
1642 }
1643
1644 TARGET(BINARY_XOR) {
1645 PyObject *right = POP();
1646 PyObject *left = TOP();
1647 PyObject *res = PyNumber_Xor(left, right);
1648 Py_DECREF(left);
1649 Py_DECREF(right);
1650 SET_TOP(res);
1651 if (res == NULL)
1652 goto error;
1653 DISPATCH();
1654 }
1655
1656 TARGET(BINARY_OR) {
1657 PyObject *right = POP();
1658 PyObject *left = TOP();
1659 PyObject *res = PyNumber_Or(left, right);
1660 Py_DECREF(left);
1661 Py_DECREF(right);
1662 SET_TOP(res);
1663 if (res == NULL)
1664 goto error;
1665 DISPATCH();
1666 }
1667
1668 TARGET(LIST_APPEND) {
1669 PyObject *v = POP();
1670 PyObject *list = PEEK(oparg);
1671 int err;
1672 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001674 if (err != 0)
1675 goto error;
1676 PREDICT(JUMP_ABSOLUTE);
1677 DISPATCH();
1678 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 TARGET(SET_ADD) {
1681 PyObject *v = POP();
1682 PyObject *set = stack_pointer[-oparg];
1683 int err;
1684 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 if (err != 0)
1687 goto error;
1688 PREDICT(JUMP_ABSOLUTE);
1689 DISPATCH();
1690 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001692 TARGET(INPLACE_POWER) {
1693 PyObject *exp = POP();
1694 PyObject *base = TOP();
1695 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1696 Py_DECREF(base);
1697 Py_DECREF(exp);
1698 SET_TOP(res);
1699 if (res == NULL)
1700 goto error;
1701 DISPATCH();
1702 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001703
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 TARGET(INPLACE_MULTIPLY) {
1705 PyObject *right = POP();
1706 PyObject *left = TOP();
1707 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1708 Py_DECREF(left);
1709 Py_DECREF(right);
1710 SET_TOP(res);
1711 if (res == NULL)
1712 goto error;
1713 DISPATCH();
1714 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Benjamin Petersond51374e2014-04-09 23:55:56 -04001716 TARGET(INPLACE_MATRIX_MULTIPLY) {
1717 PyObject *right = POP();
1718 PyObject *left = TOP();
1719 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1720 Py_DECREF(left);
1721 Py_DECREF(right);
1722 SET_TOP(res);
1723 if (res == NULL)
1724 goto error;
1725 DISPATCH();
1726 }
1727
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 TARGET(INPLACE_TRUE_DIVIDE) {
1729 PyObject *divisor = POP();
1730 PyObject *dividend = TOP();
1731 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1732 Py_DECREF(dividend);
1733 Py_DECREF(divisor);
1734 SET_TOP(quotient);
1735 if (quotient == NULL)
1736 goto error;
1737 DISPATCH();
1738 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001740 TARGET(INPLACE_FLOOR_DIVIDE) {
1741 PyObject *divisor = POP();
1742 PyObject *dividend = TOP();
1743 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1744 Py_DECREF(dividend);
1745 Py_DECREF(divisor);
1746 SET_TOP(quotient);
1747 if (quotient == NULL)
1748 goto error;
1749 DISPATCH();
1750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 TARGET(INPLACE_MODULO) {
1753 PyObject *right = POP();
1754 PyObject *left = TOP();
1755 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1756 Py_DECREF(left);
1757 Py_DECREF(right);
1758 SET_TOP(mod);
1759 if (mod == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 TARGET(INPLACE_ADD) {
1765 PyObject *right = POP();
1766 PyObject *left = TOP();
1767 PyObject *sum;
1768 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1769 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001770 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001771 }
1772 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 sum = PyNumber_InPlaceAdd(left, right);
1774 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001775 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 Py_DECREF(right);
1777 SET_TOP(sum);
1778 if (sum == NULL)
1779 goto error;
1780 DISPATCH();
1781 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001782
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001783 TARGET(INPLACE_SUBTRACT) {
1784 PyObject *right = POP();
1785 PyObject *left = TOP();
1786 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1787 Py_DECREF(left);
1788 Py_DECREF(right);
1789 SET_TOP(diff);
1790 if (diff == NULL)
1791 goto error;
1792 DISPATCH();
1793 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001794
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001795 TARGET(INPLACE_LSHIFT) {
1796 PyObject *right = POP();
1797 PyObject *left = TOP();
1798 PyObject *res = PyNumber_InPlaceLshift(left, right);
1799 Py_DECREF(left);
1800 Py_DECREF(right);
1801 SET_TOP(res);
1802 if (res == NULL)
1803 goto error;
1804 DISPATCH();
1805 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001806
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001807 TARGET(INPLACE_RSHIFT) {
1808 PyObject *right = POP();
1809 PyObject *left = TOP();
1810 PyObject *res = PyNumber_InPlaceRshift(left, right);
1811 Py_DECREF(left);
1812 Py_DECREF(right);
1813 SET_TOP(res);
1814 if (res == NULL)
1815 goto error;
1816 DISPATCH();
1817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001819 TARGET(INPLACE_AND) {
1820 PyObject *right = POP();
1821 PyObject *left = TOP();
1822 PyObject *res = PyNumber_InPlaceAnd(left, right);
1823 Py_DECREF(left);
1824 Py_DECREF(right);
1825 SET_TOP(res);
1826 if (res == NULL)
1827 goto error;
1828 DISPATCH();
1829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 TARGET(INPLACE_XOR) {
1832 PyObject *right = POP();
1833 PyObject *left = TOP();
1834 PyObject *res = PyNumber_InPlaceXor(left, right);
1835 Py_DECREF(left);
1836 Py_DECREF(right);
1837 SET_TOP(res);
1838 if (res == NULL)
1839 goto error;
1840 DISPATCH();
1841 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001842
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001843 TARGET(INPLACE_OR) {
1844 PyObject *right = POP();
1845 PyObject *left = TOP();
1846 PyObject *res = PyNumber_InPlaceOr(left, right);
1847 Py_DECREF(left);
1848 Py_DECREF(right);
1849 SET_TOP(res);
1850 if (res == NULL)
1851 goto error;
1852 DISPATCH();
1853 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001854
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 TARGET(STORE_SUBSCR) {
1856 PyObject *sub = TOP();
1857 PyObject *container = SECOND();
1858 PyObject *v = THIRD();
1859 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001861 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 Py_DECREF(container);
1865 Py_DECREF(sub);
1866 if (err != 0)
1867 goto error;
1868 DISPATCH();
1869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001870
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001871 TARGET(STORE_ANNOTATION) {
1872 _Py_IDENTIFIER(__annotations__);
1873 PyObject *ann_dict;
1874 PyObject *ann = POP();
1875 PyObject *name = GETITEM(names, oparg);
1876 int err;
1877 if (f->f_locals == NULL) {
1878 PyErr_Format(PyExc_SystemError,
1879 "no locals found when storing annotation");
1880 Py_DECREF(ann);
1881 goto error;
1882 }
1883 /* first try to get __annotations__ from locals... */
1884 if (PyDict_CheckExact(f->f_locals)) {
1885 ann_dict = _PyDict_GetItemId(f->f_locals,
1886 &PyId___annotations__);
1887 if (ann_dict == NULL) {
1888 PyErr_SetString(PyExc_NameError,
1889 "__annotations__ not found");
1890 Py_DECREF(ann);
1891 goto error;
1892 }
1893 Py_INCREF(ann_dict);
1894 }
1895 else {
1896 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1897 if (ann_str == NULL) {
1898 Py_DECREF(ann);
1899 goto error;
1900 }
1901 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1902 if (ann_dict == NULL) {
1903 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1904 PyErr_SetString(PyExc_NameError,
1905 "__annotations__ not found");
1906 }
1907 Py_DECREF(ann);
1908 goto error;
1909 }
1910 }
1911 /* ...if succeeded, __annotations__[name] = ann */
1912 if (PyDict_CheckExact(ann_dict)) {
1913 err = PyDict_SetItem(ann_dict, name, ann);
1914 }
1915 else {
1916 err = PyObject_SetItem(ann_dict, name, ann);
1917 }
1918 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001919 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001920 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001921 goto error;
1922 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001923 DISPATCH();
1924 }
1925
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 TARGET(DELETE_SUBSCR) {
1927 PyObject *sub = TOP();
1928 PyObject *container = SECOND();
1929 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001931 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 err = PyObject_DelItem(container, sub);
1933 Py_DECREF(container);
1934 Py_DECREF(sub);
1935 if (err != 0)
1936 goto error;
1937 DISPATCH();
1938 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001939
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001941 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001943 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001944 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946 PyErr_SetString(PyExc_RuntimeError,
1947 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001948 Py_DECREF(value);
1949 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001951 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001952 Py_DECREF(value);
1953 if (res == NULL)
1954 goto error;
1955 Py_DECREF(res);
1956 DISPATCH();
1957 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001958
Thomas Wouters434d0822000-08-24 20:11:32 +00001959#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001961#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001962 TARGET(RAISE_VARARGS) {
1963 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 switch (oparg) {
1965 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 if (do_raise(exc, cause)) {
1971 why = WHY_EXCEPTION;
1972 goto fast_block_end;
1973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 break;
1975 default:
1976 PyErr_SetString(PyExc_SystemError,
1977 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 break;
1979 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001980 goto error;
1981 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001982
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 retval = POP();
1985 why = WHY_RETURN;
1986 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001987 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001988
Yury Selivanov75445082015-05-11 22:57:16 -04001989 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001990 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001991 PyObject *iter = NULL;
1992 PyObject *awaitable = NULL;
1993 PyObject *obj = TOP();
1994 PyTypeObject *type = Py_TYPE(obj);
1995
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001996 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001997 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001998 }
Yury Selivanov75445082015-05-11 22:57:16 -04001999
2000 if (getter != NULL) {
2001 iter = (*getter)(obj);
2002 Py_DECREF(obj);
2003 if (iter == NULL) {
2004 SET_TOP(NULL);
2005 goto error;
2006 }
2007 }
2008 else {
2009 SET_TOP(NULL);
2010 PyErr_Format(
2011 PyExc_TypeError,
2012 "'async for' requires an object with "
2013 "__aiter__ method, got %.100s",
2014 type->tp_name);
2015 Py_DECREF(obj);
2016 goto error;
2017 }
2018
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002019 if (Py_TYPE(iter)->tp_as_async != NULL &&
2020 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
2021
2022 /* Starting with CPython 3.5.2 __aiter__ should return
2023 asynchronous iterators directly (not awaitables that
2024 resolve to asynchronous iterators.)
2025
2026 Therefore, we check if the object that was returned
2027 from __aiter__ has an __anext__ method. If it does,
2028 we wrap it in an awaitable that resolves to `iter`.
2029
2030 See http://bugs.python.org/issue27243 for more
2031 details.
2032 */
2033
2034 PyObject *wrapper = _PyAIterWrapper_New(iter);
2035 Py_DECREF(iter);
2036 SET_TOP(wrapper);
2037 DISPATCH();
2038 }
2039
Yury Selivanov5376ba92015-06-22 12:19:30 -04002040 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002041 if (awaitable == NULL) {
2042 SET_TOP(NULL);
2043 PyErr_Format(
2044 PyExc_TypeError,
2045 "'async for' received an invalid object "
2046 "from __aiter__: %.100s",
2047 Py_TYPE(iter)->tp_name);
2048
2049 Py_DECREF(iter);
2050 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002051 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04002052 Py_DECREF(iter);
2053
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002054 if (PyErr_WarnFormat(
2055 PyExc_PendingDeprecationWarning, 1,
2056 "'%.100s' implements legacy __aiter__ protocol; "
2057 "__aiter__ should return an asynchronous "
2058 "iterator, not awaitable",
2059 type->tp_name))
2060 {
2061 /* Warning was converted to an error. */
2062 Py_DECREF(awaitable);
2063 SET_TOP(NULL);
2064 goto error;
2065 }
2066 }
2067
Yury Selivanov75445082015-05-11 22:57:16 -04002068 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002069 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002070 DISPATCH();
2071 }
2072
2073 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002074 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002075 PyObject *next_iter = NULL;
2076 PyObject *awaitable = NULL;
2077 PyObject *aiter = TOP();
2078 PyTypeObject *type = Py_TYPE(aiter);
2079
Yury Selivanoveb636452016-09-08 22:01:51 -07002080 if (PyAsyncGen_CheckExact(aiter)) {
2081 awaitable = type->tp_as_async->am_anext(aiter);
2082 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002083 goto error;
2084 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002085 } else {
2086 if (type->tp_as_async != NULL){
2087 getter = type->tp_as_async->am_anext;
2088 }
Yury Selivanov75445082015-05-11 22:57:16 -04002089
Yury Selivanoveb636452016-09-08 22:01:51 -07002090 if (getter != NULL) {
2091 next_iter = (*getter)(aiter);
2092 if (next_iter == NULL) {
2093 goto error;
2094 }
2095 }
2096 else {
2097 PyErr_Format(
2098 PyExc_TypeError,
2099 "'async for' requires an iterator with "
2100 "__anext__ method, got %.100s",
2101 type->tp_name);
2102 goto error;
2103 }
Yury Selivanov75445082015-05-11 22:57:16 -04002104
Yury Selivanoveb636452016-09-08 22:01:51 -07002105 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2106 if (awaitable == NULL) {
2107 PyErr_Format(
2108 PyExc_TypeError,
2109 "'async for' received an invalid object "
2110 "from __anext__: %.100s",
2111 Py_TYPE(next_iter)->tp_name);
2112
2113 Py_DECREF(next_iter);
2114 goto error;
2115 } else {
2116 Py_DECREF(next_iter);
2117 }
2118 }
Yury Selivanov75445082015-05-11 22:57:16 -04002119
2120 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002121 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002122 DISPATCH();
2123 }
2124
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002125 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002126 TARGET(GET_AWAITABLE) {
2127 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002128 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002129
2130 Py_DECREF(iterable);
2131
Yury Selivanovc724bae2016-03-02 11:30:46 -05002132 if (iter != NULL && PyCoro_CheckExact(iter)) {
2133 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2134 if (yf != NULL) {
2135 /* `iter` is a coroutine object that is being
2136 awaited, `yf` is a pointer to the current awaitable
2137 being awaited on. */
2138 Py_DECREF(yf);
2139 Py_CLEAR(iter);
2140 PyErr_SetString(
2141 PyExc_RuntimeError,
2142 "coroutine is being awaited already");
2143 /* The code below jumps to `error` if `iter` is NULL. */
2144 }
2145 }
2146
Yury Selivanov75445082015-05-11 22:57:16 -04002147 SET_TOP(iter); /* Even if it's NULL */
2148
2149 if (iter == NULL) {
2150 goto error;
2151 }
2152
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002153 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002154 DISPATCH();
2155 }
2156
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 TARGET(YIELD_FROM) {
2158 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002159 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002161 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2162 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002163 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002164 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002166 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002167 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002168 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 Py_DECREF(v);
2171 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002172 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002173 if (tstate->c_tracefunc != NULL
2174 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002175 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002176 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 if (err < 0)
2178 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002179 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 SET_TOP(val);
2181 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002182 }
Martin Panter95f53c12016-07-18 08:23:26 +00002183 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002184 f->f_stacktop = stack_pointer;
2185 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002186 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002187 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002188 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002190
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002193
2194 if (co->co_flags & CO_ASYNC_GENERATOR) {
2195 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2196 Py_DECREF(retval);
2197 if (w == NULL) {
2198 retval = NULL;
2199 goto error;
2200 }
2201 retval = w;
2202 }
2203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 f->f_stacktop = stack_pointer;
2205 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 TARGET(POP_EXCEPT) {
2210 PyTryBlock *b = PyFrame_BlockPop(f);
2211 if (b->b_type != EXCEPT_HANDLER) {
2212 PyErr_SetString(PyExc_SystemError,
2213 "popped block is not an except handler");
2214 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002219
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002220 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 TARGET(POP_BLOCK) {
2222 PyTryBlock *b = PyFrame_BlockPop(f);
2223 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 TARGET(END_FINALLY) {
2229 PyObject *status = POP();
2230 if (PyLong_Check(status)) {
2231 why = (enum why_code) PyLong_AS_LONG(status);
2232 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 if (why == WHY_RETURN ||
2234 why == WHY_CONTINUE)
2235 retval = POP();
2236 if (why == WHY_SILENCED) {
2237 /* An exception was silenced by 'with', we must
2238 manually unwind the EXCEPT_HANDLER block which was
2239 created when the exception was caught, otherwise
2240 the stack will be in an inconsistent state. */
2241 PyTryBlock *b = PyFrame_BlockPop(f);
2242 assert(b->b_type == EXCEPT_HANDLER);
2243 UNWIND_EXCEPT_HANDLER(b);
2244 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 Py_DECREF(status);
2246 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 Py_DECREF(status);
2249 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002251 else if (PyExceptionClass_Check(status)) {
2252 PyObject *exc = POP();
2253 PyObject *tb = POP();
2254 PyErr_Restore(status, exc, tb);
2255 why = WHY_EXCEPTION;
2256 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 PyErr_SetString(PyExc_SystemError,
2260 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 Py_DECREF(status);
2262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 Py_DECREF(status);
2265 DISPATCH();
2266 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002267
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002269 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002270
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002272 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2274 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002275 PyErr_SetString(PyExc_NameError,
2276 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002278 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002280 }
2281 else {
2282 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2283 if (build_class_str == NULL)
2284 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2286 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002287 if (PyErr_ExceptionMatches(PyExc_KeyError))
2288 PyErr_SetString(PyExc_NameError,
2289 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002291 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002294 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002297 TARGET(STORE_NAME) {
2298 PyObject *name = GETITEM(names, oparg);
2299 PyObject *v = POP();
2300 PyObject *ns = f->f_locals;
2301 int err;
2302 if (ns == NULL) {
2303 PyErr_Format(PyExc_SystemError,
2304 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 if (PyDict_CheckExact(ns))
2309 err = PyDict_SetItem(ns, name, v);
2310 else
2311 err = PyObject_SetItem(ns, name, v);
2312 Py_DECREF(v);
2313 if (err != 0)
2314 goto error;
2315 DISPATCH();
2316 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002317
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 TARGET(DELETE_NAME) {
2319 PyObject *name = GETITEM(names, oparg);
2320 PyObject *ns = f->f_locals;
2321 int err;
2322 if (ns == NULL) {
2323 PyErr_Format(PyExc_SystemError,
2324 "no locals when deleting %R", name);
2325 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002326 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002327 err = PyObject_DelItem(ns, name);
2328 if (err != 0) {
2329 format_exc_check_arg(PyExc_NameError,
2330 NAME_ERROR_MSG,
2331 name);
2332 goto error;
2333 }
2334 DISPATCH();
2335 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002336
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002337 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 TARGET(UNPACK_SEQUENCE) {
2339 PyObject *seq = POP(), *item, **items;
2340 if (PyTuple_CheckExact(seq) &&
2341 PyTuple_GET_SIZE(seq) == oparg) {
2342 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 item = items[oparg];
2345 Py_INCREF(item);
2346 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 } else if (PyList_CheckExact(seq) &&
2349 PyList_GET_SIZE(seq) == oparg) {
2350 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 item = items[oparg];
2353 Py_INCREF(item);
2354 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 stack_pointer + oparg)) {
2358 STACKADJ(oparg);
2359 } else {
2360 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 Py_DECREF(seq);
2362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002365 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002367
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 TARGET(UNPACK_EX) {
2369 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2370 PyObject *seq = POP();
2371
2372 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2373 stack_pointer + totalargs)) {
2374 stack_pointer += totalargs;
2375 } else {
2376 Py_DECREF(seq);
2377 goto error;
2378 }
2379 Py_DECREF(seq);
2380 DISPATCH();
2381 }
2382
2383 TARGET(STORE_ATTR) {
2384 PyObject *name = GETITEM(names, oparg);
2385 PyObject *owner = TOP();
2386 PyObject *v = SECOND();
2387 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002390 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002391 Py_DECREF(owner);
2392 if (err != 0)
2393 goto error;
2394 DISPATCH();
2395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 TARGET(DELETE_ATTR) {
2398 PyObject *name = GETITEM(names, oparg);
2399 PyObject *owner = POP();
2400 int err;
2401 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2402 Py_DECREF(owner);
2403 if (err != 0)
2404 goto error;
2405 DISPATCH();
2406 }
2407
2408 TARGET(STORE_GLOBAL) {
2409 PyObject *name = GETITEM(names, oparg);
2410 PyObject *v = POP();
2411 int err;
2412 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002413 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 if (err != 0)
2415 goto error;
2416 DISPATCH();
2417 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002418
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 TARGET(DELETE_GLOBAL) {
2420 PyObject *name = GETITEM(names, oparg);
2421 int err;
2422 err = PyDict_DelItem(f->f_globals, name);
2423 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002425 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002427 }
2428 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 TARGET(LOAD_NAME) {
2432 PyObject *name = GETITEM(names, oparg);
2433 PyObject *locals = f->f_locals;
2434 PyObject *v;
2435 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 "no locals when loading %R", name);
2438 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 if (PyDict_CheckExact(locals)) {
2441 v = PyDict_GetItem(locals, name);
2442 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002443 }
2444 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002446 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002447 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2448 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 PyErr_Clear();
2450 }
2451 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002452 if (v == NULL) {
2453 v = PyDict_GetItem(f->f_globals, name);
2454 Py_XINCREF(v);
2455 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002456 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 v = PyDict_GetItem(f->f_builtins, name);
2458 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002459 format_exc_check_arg(
2460 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 NAME_ERROR_MSG, name);
2462 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002463 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002465 }
2466 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002467 v = PyObject_GetItem(f->f_builtins, name);
2468 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002469 if (PyErr_ExceptionMatches(PyExc_KeyError))
2470 format_exc_check_arg(
2471 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 NAME_ERROR_MSG, name);
2473 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002474 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002475 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002481
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 TARGET(LOAD_GLOBAL) {
2483 PyObject *name = GETITEM(names, oparg);
2484 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002485 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002486 && PyDict_CheckExact(f->f_builtins))
2487 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002489 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 name);
2491 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002492 if (!_PyErr_OCCURRED()) {
2493 /* _PyDict_LoadGlobal() returns NULL without raising
2494 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002495 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002496 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002497 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002500 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002502 else {
2503 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002504
2505 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 v = PyObject_GetItem(f->f_globals, name);
2507 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002508 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2509 goto error;
2510 PyErr_Clear();
2511
Victor Stinnerb4efc962015-11-20 09:24:02 +01002512 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002513 v = PyObject_GetItem(f->f_builtins, name);
2514 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002515 if (PyErr_ExceptionMatches(PyExc_KeyError))
2516 format_exc_check_arg(
2517 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002518 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002520 }
2521 }
2522 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002526
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 TARGET(DELETE_FAST) {
2528 PyObject *v = GETLOCAL(oparg);
2529 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 SETLOCAL(oparg, NULL);
2531 DISPATCH();
2532 }
2533 format_exc_check_arg(
2534 PyExc_UnboundLocalError,
2535 UNBOUNDLOCAL_ERROR_MSG,
2536 PyTuple_GetItem(co->co_varnames, oparg)
2537 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002538 goto error;
2539 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002540
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 TARGET(DELETE_DEREF) {
2542 PyObject *cell = freevars[oparg];
2543 if (PyCell_GET(cell) != NULL) {
2544 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002545 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002546 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002547 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 goto error;
2549 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002550
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 TARGET(LOAD_CLOSURE) {
2552 PyObject *cell = freevars[oparg];
2553 Py_INCREF(cell);
2554 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002555 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002557
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002558 TARGET(LOAD_CLASSDEREF) {
2559 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002560 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002561 assert(locals);
2562 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2563 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2564 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2565 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2566 if (PyDict_CheckExact(locals)) {
2567 value = PyDict_GetItem(locals, name);
2568 Py_XINCREF(value);
2569 }
2570 else {
2571 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002572 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002573 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2574 goto error;
2575 PyErr_Clear();
2576 }
2577 }
2578 if (!value) {
2579 PyObject *cell = freevars[oparg];
2580 value = PyCell_GET(cell);
2581 if (value == NULL) {
2582 format_exc_unbound(co, oparg);
2583 goto error;
2584 }
2585 Py_INCREF(value);
2586 }
2587 PUSH(value);
2588 DISPATCH();
2589 }
2590
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 TARGET(LOAD_DEREF) {
2592 PyObject *cell = freevars[oparg];
2593 PyObject *value = PyCell_GET(cell);
2594 if (value == NULL) {
2595 format_exc_unbound(co, oparg);
2596 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 Py_INCREF(value);
2599 PUSH(value);
2600 DISPATCH();
2601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002602
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 TARGET(STORE_DEREF) {
2604 PyObject *v = POP();
2605 PyObject *cell = freevars[oparg];
2606 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 DISPATCH();
2609 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002610
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002611 TARGET(BUILD_STRING) {
2612 PyObject *str;
2613 PyObject *empty = PyUnicode_New(0, 0);
2614 if (empty == NULL) {
2615 goto error;
2616 }
2617 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2618 Py_DECREF(empty);
2619 if (str == NULL)
2620 goto error;
2621 while (--oparg >= 0) {
2622 PyObject *item = POP();
2623 Py_DECREF(item);
2624 }
2625 PUSH(str);
2626 DISPATCH();
2627 }
2628
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002629 TARGET(BUILD_TUPLE) {
2630 PyObject *tup = PyTuple_New(oparg);
2631 if (tup == NULL)
2632 goto error;
2633 while (--oparg >= 0) {
2634 PyObject *item = POP();
2635 PyTuple_SET_ITEM(tup, oparg, item);
2636 }
2637 PUSH(tup);
2638 DISPATCH();
2639 }
2640
2641 TARGET(BUILD_LIST) {
2642 PyObject *list = PyList_New(oparg);
2643 if (list == NULL)
2644 goto error;
2645 while (--oparg >= 0) {
2646 PyObject *item = POP();
2647 PyList_SET_ITEM(list, oparg, item);
2648 }
2649 PUSH(list);
2650 DISPATCH();
2651 }
2652
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002653 TARGET(BUILD_TUPLE_UNPACK)
2654 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002655 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002656 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002657 PyObject *sum;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002658 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002659
2660 if (convert_to_tuple && oparg == 1 && PyTuple_CheckExact(TOP())) {
2661 DISPATCH();
2662 }
2663
2664 sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002665 if (sum == NULL)
2666 goto error;
2667
2668 for (i = oparg; i > 0; i--) {
2669 PyObject *none_val;
2670
2671 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2672 if (none_val == NULL) {
2673 Py_DECREF(sum);
2674 goto error;
2675 }
2676 Py_DECREF(none_val);
2677 }
2678
2679 if (convert_to_tuple) {
2680 return_value = PyList_AsTuple(sum);
2681 Py_DECREF(sum);
2682 if (return_value == NULL)
2683 goto error;
2684 }
2685 else {
2686 return_value = sum;
2687 }
2688
2689 while (oparg--)
2690 Py_DECREF(POP());
2691 PUSH(return_value);
2692 DISPATCH();
2693 }
2694
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002695 TARGET(BUILD_SET) {
2696 PyObject *set = PySet_New(NULL);
2697 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002698 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 if (set == NULL)
2700 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002701 for (i = oparg; i > 0; i--) {
2702 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 if (err == 0)
2704 err = PySet_Add(set, item);
2705 Py_DECREF(item);
2706 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002707 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 if (err != 0) {
2709 Py_DECREF(set);
2710 goto error;
2711 }
2712 PUSH(set);
2713 DISPATCH();
2714 }
2715
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002717 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002718 PyObject *sum = PySet_New(NULL);
2719 if (sum == NULL)
2720 goto error;
2721
2722 for (i = oparg; i > 0; i--) {
2723 if (_PySet_Update(sum, PEEK(i)) < 0) {
2724 Py_DECREF(sum);
2725 goto error;
2726 }
2727 }
2728
2729 while (oparg--)
2730 Py_DECREF(POP());
2731 PUSH(sum);
2732 DISPATCH();
2733 }
2734
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002736 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2738 if (map == NULL)
2739 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002740 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002741 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002742 PyObject *key = PEEK(2*i);
2743 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002745 if (err != 0) {
2746 Py_DECREF(map);
2747 goto error;
2748 }
2749 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002750
2751 while (oparg--) {
2752 Py_DECREF(POP());
2753 Py_DECREF(POP());
2754 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002755 PUSH(map);
2756 DISPATCH();
2757 }
2758
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002759 TARGET(SETUP_ANNOTATIONS) {
2760 _Py_IDENTIFIER(__annotations__);
2761 int err;
2762 PyObject *ann_dict;
2763 if (f->f_locals == NULL) {
2764 PyErr_Format(PyExc_SystemError,
2765 "no locals found when setting up annotations");
2766 goto error;
2767 }
2768 /* check if __annotations__ in locals()... */
2769 if (PyDict_CheckExact(f->f_locals)) {
2770 ann_dict = _PyDict_GetItemId(f->f_locals,
2771 &PyId___annotations__);
2772 if (ann_dict == NULL) {
2773 /* ...if not, create a new one */
2774 ann_dict = PyDict_New();
2775 if (ann_dict == NULL) {
2776 goto error;
2777 }
2778 err = _PyDict_SetItemId(f->f_locals,
2779 &PyId___annotations__, ann_dict);
2780 Py_DECREF(ann_dict);
2781 if (err != 0) {
2782 goto error;
2783 }
2784 }
2785 }
2786 else {
2787 /* do the same if locals() is not a dict */
2788 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2789 if (ann_str == NULL) {
2790 break;
2791 }
2792 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2793 if (ann_dict == NULL) {
2794 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2795 goto error;
2796 }
2797 PyErr_Clear();
2798 ann_dict = PyDict_New();
2799 if (ann_dict == NULL) {
2800 goto error;
2801 }
2802 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2803 Py_DECREF(ann_dict);
2804 if (err != 0) {
2805 goto error;
2806 }
2807 }
2808 else {
2809 Py_DECREF(ann_dict);
2810 }
2811 }
2812 DISPATCH();
2813 }
2814
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002815 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002816 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002817 PyObject *map;
2818 PyObject *keys = TOP();
2819 if (!PyTuple_CheckExact(keys) ||
2820 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2821 PyErr_SetString(PyExc_SystemError,
2822 "bad BUILD_CONST_KEY_MAP keys argument");
2823 goto error;
2824 }
2825 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2826 if (map == NULL) {
2827 goto error;
2828 }
2829 for (i = oparg; i > 0; i--) {
2830 int err;
2831 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2832 PyObject *value = PEEK(i + 1);
2833 err = PyDict_SetItem(map, key, value);
2834 if (err != 0) {
2835 Py_DECREF(map);
2836 goto error;
2837 }
2838 }
2839
2840 Py_DECREF(POP());
2841 while (oparg--) {
2842 Py_DECREF(POP());
2843 }
2844 PUSH(map);
2845 DISPATCH();
2846 }
2847
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002848 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2849 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002850 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
Victor Stinner74319ae2016-08-25 00:04:09 +02002851 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002852 PyObject *sum;
2853
2854 if (with_call && oparg == 1 && PyDict_CheckExact(TOP())) {
2855 DISPATCH();
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002856 }
2857
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002858 sum = PyDict_New();
2859 if (sum == NULL)
2860 goto error;
2861
2862 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002863 PyObject *arg = PEEK(i);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002864 if (with_call && PyDict_Size(sum)) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002865 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2866
2867 if (intersection == NULL) {
2868 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002869 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002870 PyErr_Format(PyExc_TypeError,
2871 "%.200s%.200s argument after ** "
2872 "must be a mapping, not %.200s",
2873 PyEval_GetFuncName(func),
2874 PyEval_GetFuncDesc(func),
2875 arg->ob_type->tp_name);
2876 }
2877 Py_DECREF(sum);
2878 goto error;
2879 }
2880
2881 if (PySet_GET_SIZE(intersection)) {
2882 Py_ssize_t idx = 0;
2883 PyObject *key;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002884 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002885 Py_hash_t hash;
2886 _PySet_NextEntry(intersection, &idx, &key, &hash);
2887 if (!PyUnicode_Check(key)) {
2888 PyErr_Format(PyExc_TypeError,
2889 "%.200s%.200s keywords must be strings",
2890 PyEval_GetFuncName(func),
2891 PyEval_GetFuncDesc(func));
2892 } else {
2893 PyErr_Format(PyExc_TypeError,
2894 "%.200s%.200s got multiple "
2895 "values for keyword argument '%U'",
2896 PyEval_GetFuncName(func),
2897 PyEval_GetFuncDesc(func),
2898 key);
2899 }
2900 Py_DECREF(intersection);
2901 Py_DECREF(sum);
2902 goto error;
2903 }
2904 Py_DECREF(intersection);
2905 }
2906
2907 if (PyDict_Update(sum, arg) < 0) {
2908 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2909 PyErr_Format(PyExc_TypeError,
2910 "'%.200s' object is not a mapping",
2911 arg->ob_type->tp_name);
2912 }
2913 Py_DECREF(sum);
2914 goto error;
2915 }
2916 }
2917
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002918 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002919 Py_DECREF(POP());
2920 PUSH(sum);
2921 DISPATCH();
2922 }
2923
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 TARGET(MAP_ADD) {
2925 PyObject *key = TOP();
2926 PyObject *value = SECOND();
2927 PyObject *map;
2928 int err;
2929 STACKADJ(-2);
2930 map = stack_pointer[-oparg]; /* dict */
2931 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002932 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 Py_DECREF(value);
2934 Py_DECREF(key);
2935 if (err != 0)
2936 goto error;
2937 PREDICT(JUMP_ABSOLUTE);
2938 DISPATCH();
2939 }
2940
2941 TARGET(LOAD_ATTR) {
2942 PyObject *name = GETITEM(names, oparg);
2943 PyObject *owner = TOP();
2944 PyObject *res = PyObject_GetAttr(owner, name);
2945 Py_DECREF(owner);
2946 SET_TOP(res);
2947 if (res == NULL)
2948 goto error;
2949 DISPATCH();
2950 }
2951
2952 TARGET(COMPARE_OP) {
2953 PyObject *right = POP();
2954 PyObject *left = TOP();
2955 PyObject *res = cmp_outcome(oparg, left, right);
2956 Py_DECREF(left);
2957 Py_DECREF(right);
2958 SET_TOP(res);
2959 if (res == NULL)
2960 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002961 PREDICT(POP_JUMP_IF_FALSE);
2962 PREDICT(POP_JUMP_IF_TRUE);
2963 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002964 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002965
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002968 PyObject *fromlist = POP();
2969 PyObject *level = TOP();
2970 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 READ_TIMESTAMP(intr0);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002972 res = import_name(f, name, fromlist, level);
2973 Py_DECREF(level);
2974 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002976 SET_TOP(res);
2977 if (res == NULL)
2978 goto error;
2979 DISPATCH();
2980 }
2981
2982 TARGET(IMPORT_STAR) {
2983 PyObject *from = POP(), *locals;
2984 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002985 if (PyFrame_FastToLocalsWithError(f) < 0)
2986 goto error;
2987
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 locals = f->f_locals;
2989 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 PyErr_SetString(PyExc_SystemError,
2991 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 }
2994 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 READ_TIMESTAMP(intr1);
2997 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 Py_DECREF(from);
2999 if (err != 0)
3000 goto error;
3001 DISPATCH();
3002 }
Guido van Rossum25831651993-05-19 14:50:45 +00003003
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 TARGET(IMPORT_FROM) {
3005 PyObject *name = GETITEM(names, oparg);
3006 PyObject *from = TOP();
3007 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 PUSH(res);
3012 if (res == NULL)
3013 goto error;
3014 DISPATCH();
3015 }
Thomas Wouters52152252000-08-17 22:55:00 +00003016
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003017 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 JUMPBY(oparg);
3019 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003021
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003022 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 TARGET(POP_JUMP_IF_FALSE) {
3024 PyObject *cond = POP();
3025 int err;
3026 if (cond == Py_True) {
3027 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 FAST_DISPATCH();
3029 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 if (cond == Py_False) {
3031 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 JUMPTO(oparg);
3033 FAST_DISPATCH();
3034 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003035 err = PyObject_IsTrue(cond);
3036 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (err > 0)
3038 err = 0;
3039 else if (err == 0)
3040 JUMPTO(oparg);
3041 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003045
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003046 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 TARGET(POP_JUMP_IF_TRUE) {
3048 PyObject *cond = POP();
3049 int err;
3050 if (cond == Py_False) {
3051 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 FAST_DISPATCH();
3053 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003054 if (cond == Py_True) {
3055 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 JUMPTO(oparg);
3057 FAST_DISPATCH();
3058 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003059 err = PyObject_IsTrue(cond);
3060 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 if (err > 0) {
3062 err = 0;
3063 JUMPTO(oparg);
3064 }
3065 else if (err == 0)
3066 ;
3067 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003071
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 TARGET(JUMP_IF_FALSE_OR_POP) {
3073 PyObject *cond = TOP();
3074 int err;
3075 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 FAST_DISPATCH();
3079 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 JUMPTO(oparg);
3082 FAST_DISPATCH();
3083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003084 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 if (err > 0) {
3086 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 err = 0;
3089 }
3090 else if (err == 0)
3091 JUMPTO(oparg);
3092 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003096
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 TARGET(JUMP_IF_TRUE_OR_POP) {
3098 PyObject *cond = TOP();
3099 int err;
3100 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 FAST_DISPATCH();
3104 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 JUMPTO(oparg);
3107 FAST_DISPATCH();
3108 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003109 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 if (err > 0) {
3111 err = 0;
3112 JUMPTO(oparg);
3113 }
3114 else if (err == 0) {
3115 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 }
3118 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003122
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003123 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003126#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 /* Enabling this path speeds-up all while and for-loops by bypassing
3128 the per-loop checks for signals. By default, this should be turned-off
3129 because it prevents detection of a control-break in tight loops like
3130 "while 1: pass". Compile with this option turned-on when you need
3131 the speed-up and do not need break checking inside tight loops (ones
3132 that contain only instructions ending with FAST_DISPATCH).
3133 */
3134 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003135#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003137#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003139
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003143 PyObject *iter = PyObject_GetIter(iterable);
3144 Py_DECREF(iterable);
3145 SET_TOP(iter);
3146 if (iter == NULL)
3147 goto error;
3148 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003149 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003150 DISPATCH();
3151 }
3152
3153 TARGET(GET_YIELD_FROM_ITER) {
3154 /* before: [obj]; after [getiter(obj)] */
3155 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003156 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003157 if (PyCoro_CheckExact(iterable)) {
3158 /* `iterable` is a coroutine */
3159 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3160 /* and it is used in a 'yield from' expression of a
3161 regular generator. */
3162 Py_DECREF(iterable);
3163 SET_TOP(NULL);
3164 PyErr_SetString(PyExc_TypeError,
3165 "cannot 'yield from' a coroutine object "
3166 "in a non-coroutine generator");
3167 goto error;
3168 }
3169 }
3170 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003171 /* `iterable` is not a generator. */
3172 iter = PyObject_GetIter(iterable);
3173 Py_DECREF(iterable);
3174 SET_TOP(iter);
3175 if (iter == NULL)
3176 goto error;
3177 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003178 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003179 DISPATCH();
3180 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003181
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003182 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 PyObject *iter = TOP();
3186 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3187 if (next != NULL) {
3188 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 PREDICT(STORE_FAST);
3190 PREDICT(UNPACK_SEQUENCE);
3191 DISPATCH();
3192 }
3193 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3195 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003196 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003197 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 PyErr_Clear();
3199 }
3200 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 STACKADJ(-1);
3202 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003204 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003207
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003208 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 why = WHY_BREAK;
3210 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003212
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003215 if (retval == NULL)
3216 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 why = WHY_CONTINUE;
3218 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003220
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003221 TARGET(SETUP_LOOP)
3222 TARGET(SETUP_EXCEPT)
3223 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 /* NOTE: If you add any new block-setup opcodes that
3225 are not try/except/finally handlers, you may need
3226 to update the PyGen_NeedsFinalizing() function.
3227 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3230 STACK_LEVEL());
3231 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003233
Yury Selivanov75445082015-05-11 22:57:16 -04003234 TARGET(BEFORE_ASYNC_WITH) {
3235 _Py_IDENTIFIER(__aexit__);
3236 _Py_IDENTIFIER(__aenter__);
3237
3238 PyObject *mgr = TOP();
3239 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3240 *enter;
3241 PyObject *res;
3242 if (exit == NULL)
3243 goto error;
3244 SET_TOP(exit);
3245 enter = special_lookup(mgr, &PyId___aenter__);
3246 Py_DECREF(mgr);
3247 if (enter == NULL)
3248 goto error;
3249 res = PyObject_CallFunctionObjArgs(enter, NULL);
3250 Py_DECREF(enter);
3251 if (res == NULL)
3252 goto error;
3253 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003254 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003255 DISPATCH();
3256 }
3257
3258 TARGET(SETUP_ASYNC_WITH) {
3259 PyObject *res = POP();
3260 /* Setup the finally block before pushing the result
3261 of __aenter__ on the stack. */
3262 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3263 STACK_LEVEL());
3264 PUSH(res);
3265 DISPATCH();
3266 }
3267
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003269 _Py_IDENTIFIER(__exit__);
3270 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 PyObject *mgr = TOP();
3272 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3273 PyObject *res;
3274 if (exit == NULL)
3275 goto error;
3276 SET_TOP(exit);
3277 enter = special_lookup(mgr, &PyId___enter__);
3278 Py_DECREF(mgr);
3279 if (enter == NULL)
3280 goto error;
3281 res = PyObject_CallFunctionObjArgs(enter, NULL);
3282 Py_DECREF(enter);
3283 if (res == NULL)
3284 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 /* Setup the finally block before pushing the result
3286 of __enter__ on the stack. */
3287 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3288 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003289
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 DISPATCH();
3292 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003293
Yury Selivanov75445082015-05-11 22:57:16 -04003294 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003295 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 how/why we entered the finally clause:
3297 - TOP = None
3298 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3299 - TOP = WHY_*; no retval below it
3300 - (TOP, SECOND, THIRD) = exc_info()
3301 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3302 Below them is EXIT, the context.__exit__ bound method.
3303 In the last case, we must call
3304 EXIT(TOP, SECOND, THIRD)
3305 otherwise we must call
3306 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003307
Benjamin Peterson8f169482013-10-29 22:25:06 -04003308 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003310 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 In addition, if the stack represents an exception,
3314 *and* the function call returns a 'true' value, we
3315 push WHY_SILENCED onto the stack. END_FINALLY will
3316 then not re-raise the exception. (But non-local
3317 gotos should still be resumed.)
3318 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003321 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003322 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 (void)POP();
3324 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003327 else if (PyLong_Check(exc)) {
3328 STACKADJ(-1);
3329 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 case WHY_RETURN:
3331 case WHY_CONTINUE:
3332 /* Retval in TOP. */
3333 exit_func = SECOND();
3334 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 break;
3337 default:
3338 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 break;
3341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 }
3344 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 val = SECOND();
3348 tb = THIRD();
3349 tp2 = FOURTH();
3350 exc2 = PEEK(5);
3351 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003353 SET_VALUE(7, tb2);
3354 SET_VALUE(6, exc2);
3355 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3357 SET_FOURTH(NULL);
3358 /* We just shifted the stack down, so we have
3359 to tell the except handler block that the
3360 values are lower than it expects. */
3361 block = &f->f_blockstack[f->f_iblock - 1];
3362 assert(block->b_type == EXCEPT_HANDLER);
3363 block->b_level--;
3364 }
3365 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003367 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 if (res == NULL)
3369 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003370
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003371 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003372 PUSH(exc);
3373 PUSH(res);
3374 PREDICT(WITH_CLEANUP_FINISH);
3375 DISPATCH();
3376 }
3377
3378 PREDICTED(WITH_CLEANUP_FINISH);
3379 TARGET(WITH_CLEANUP_FINISH) {
3380 PyObject *res = POP();
3381 PyObject *exc = POP();
3382 int err;
3383
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003384 if (exc != Py_None)
3385 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003386 else
3387 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003388
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003389 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003390 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 else if (err > 0) {
3395 err = 0;
3396 /* There was an exception and a True return */
3397 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3398 }
3399 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003402
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003403 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 TARGET(CALL_FUNCTION) {
3405 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 PCALL(PCALL_ALL);
3407 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003408#ifdef WITH_TSC
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003409 res = call_function(&sp, oparg, NULL, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003410#else
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003411 res = call_function(&sp, oparg, NULL);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003412#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003415 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003416 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003420
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003421 TARGET(CALL_FUNCTION_KW) {
3422 PyObject **sp, *res, *names;
3423
3424 names = POP();
3425 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003428#ifdef WITH_TSC
3429 res = call_function(&sp, oparg, names, &intr0, &intr1);
3430#else
3431 res = call_function(&sp, oparg, names);
3432#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003434 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003435 Py_DECREF(names);
3436
3437 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003438 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003439 }
3440 DISPATCH();
3441 }
3442
3443 TARGET(CALL_FUNCTION_EX) {
3444 PyObject *func, *callargs, *kwargs = NULL, *result;
3445 PCALL(PCALL_ALL);
3446 if (oparg & 0x01) {
3447 kwargs = POP();
3448 assert(PyDict_CheckExact(kwargs));
3449 }
3450 callargs = POP();
3451 assert(PyTuple_CheckExact(callargs));
3452 func = TOP();
3453
3454 READ_TIMESTAMP(intr0);
3455 result = do_call_core(func, callargs, kwargs);
3456 READ_TIMESTAMP(intr1);
3457 Py_DECREF(func);
3458 Py_DECREF(callargs);
3459 Py_XDECREF(kwargs);
3460
3461 SET_TOP(result);
3462 if (result == NULL) {
3463 goto error;
3464 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003465 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003467
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003468 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003469 PyObject *qualname = POP();
3470 PyObject *codeobj = POP();
3471 PyFunctionObject *func = (PyFunctionObject *)
3472 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003473
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003474 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003475 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003476 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003479
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003480 if (oparg & 0x08) {
3481 assert(PyTuple_CheckExact(TOP()));
3482 func ->func_closure = POP();
3483 }
3484 if (oparg & 0x04) {
3485 assert(PyDict_CheckExact(TOP()));
3486 func->func_annotations = POP();
3487 }
3488 if (oparg & 0x02) {
3489 assert(PyDict_CheckExact(TOP()));
3490 func->func_kwdefaults = POP();
3491 }
3492 if (oparg & 0x01) {
3493 assert(PyTuple_CheckExact(TOP()));
3494 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003496
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003497 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003498 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003500
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003501 TARGET(BUILD_SLICE) {
3502 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003506 step = NULL;
3507 stop = POP();
3508 start = TOP();
3509 slice = PySlice_New(start, stop, step);
3510 Py_DECREF(start);
3511 Py_DECREF(stop);
3512 Py_XDECREF(step);
3513 SET_TOP(slice);
3514 if (slice == NULL)
3515 goto error;
3516 DISPATCH();
3517 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003518
Eric V. Smitha78c7952015-11-03 12:45:05 -05003519 TARGET(FORMAT_VALUE) {
3520 /* Handles f-string value formatting. */
3521 PyObject *result;
3522 PyObject *fmt_spec;
3523 PyObject *value;
3524 PyObject *(*conv_fn)(PyObject *);
3525 int which_conversion = oparg & FVC_MASK;
3526 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3527
3528 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003529 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003530
3531 /* See if any conversion is specified. */
3532 switch (which_conversion) {
3533 case FVC_STR: conv_fn = PyObject_Str; break;
3534 case FVC_REPR: conv_fn = PyObject_Repr; break;
3535 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3536
3537 /* Must be 0 (meaning no conversion), since only four
3538 values are allowed by (oparg & FVC_MASK). */
3539 default: conv_fn = NULL; break;
3540 }
3541
3542 /* If there's a conversion function, call it and replace
3543 value with that result. Otherwise, just use value,
3544 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003545 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003546 result = conv_fn(value);
3547 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003548 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003549 Py_XDECREF(fmt_spec);
3550 goto error;
3551 }
3552 value = result;
3553 }
3554
3555 /* If value is a unicode object, and there's no fmt_spec,
3556 then we know the result of format(value) is value
3557 itself. In that case, skip calling format(). I plan to
3558 move this optimization in to PyObject_Format()
3559 itself. */
3560 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3561 /* Do nothing, just transfer ownership to result. */
3562 result = value;
3563 } else {
3564 /* Actually call format(). */
3565 result = PyObject_Format(value, fmt_spec);
3566 Py_DECREF(value);
3567 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003568 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003569 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003570 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003571 }
3572
Eric V. Smith135d5f42016-02-05 18:23:08 -05003573 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003574 DISPATCH();
3575 }
3576
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003577 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003578 int oldoparg = oparg;
3579 NEXTOPARG();
3580 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003582 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003583
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003584
Antoine Pitrou042b1282010-08-13 21:15:58 +00003585#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003587#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 default:
3589 fprintf(stderr,
3590 "XXX lineno: %d, opcode: %d\n",
3591 PyFrame_GetLineNumber(f),
3592 opcode);
3593 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003594 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003595
3596#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003598#endif
3599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003601
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 /* This should never be reached. Every opcode should end with DISPATCH()
3603 or goto error. */
3604 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003606error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003608
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003609 assert(why == WHY_NOT);
3610 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003611
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003612 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003613#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003614 if (!PyErr_Occurred())
3615 PyErr_SetString(PyExc_SystemError,
3616 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003617#else
3618 assert(PyErr_Occurred());
3619#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003620
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003621 /* Log traceback info. */
3622 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003623
Benjamin Peterson51f46162013-01-23 08:38:47 -05003624 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003625 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3626 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003627
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003628fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003629 assert(why != WHY_NOT);
3630
3631 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 while (why != WHY_NOT && f->f_iblock > 0) {
3633 /* Peek at the current block. */
3634 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 assert(why != WHY_YIELD);
3637 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3638 why = WHY_NOT;
3639 JUMPTO(PyLong_AS_LONG(retval));
3640 Py_DECREF(retval);
3641 break;
3642 }
3643 /* Now we have to pop the block. */
3644 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 if (b->b_type == EXCEPT_HANDLER) {
3647 UNWIND_EXCEPT_HANDLER(b);
3648 continue;
3649 }
3650 UNWIND_BLOCK(b);
3651 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3652 why = WHY_NOT;
3653 JUMPTO(b->b_handler);
3654 break;
3655 }
3656 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3657 || b->b_type == SETUP_FINALLY)) {
3658 PyObject *exc, *val, *tb;
3659 int handler = b->b_handler;
3660 /* Beware, this invalidates all b->b_* fields */
3661 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3662 PUSH(tstate->exc_traceback);
3663 PUSH(tstate->exc_value);
3664 if (tstate->exc_type != NULL) {
3665 PUSH(tstate->exc_type);
3666 }
3667 else {
3668 Py_INCREF(Py_None);
3669 PUSH(Py_None);
3670 }
3671 PyErr_Fetch(&exc, &val, &tb);
3672 /* Make the raw exception data
3673 available to the handler,
3674 so a program can emulate the
3675 Python main loop. */
3676 PyErr_NormalizeException(
3677 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003678 if (tb != NULL)
3679 PyException_SetTraceback(val, tb);
3680 else
3681 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 Py_INCREF(exc);
3683 tstate->exc_type = exc;
3684 Py_INCREF(val);
3685 tstate->exc_value = val;
3686 tstate->exc_traceback = tb;
3687 if (tb == NULL)
3688 tb = Py_None;
3689 Py_INCREF(tb);
3690 PUSH(tb);
3691 PUSH(val);
3692 PUSH(exc);
3693 why = WHY_NOT;
3694 JUMPTO(handler);
3695 break;
3696 }
3697 if (b->b_type == SETUP_FINALLY) {
3698 if (why & (WHY_RETURN | WHY_CONTINUE))
3699 PUSH(retval);
3700 PUSH(PyLong_FromLong((long)why));
3701 why = WHY_NOT;
3702 JUMPTO(b->b_handler);
3703 break;
3704 }
3705 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 if (why != WHY_NOT)
3710 break;
3711 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003712
Victor Stinnerace47d72013-07-18 01:41:08 +02003713 assert(!PyErr_Occurred());
3714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 assert(why != WHY_YIELD);
3718 /* Pop remaining stack entries. */
3719 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003720 PyObject *o = POP();
3721 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 if (why != WHY_RETURN)
3725 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003726
Victor Stinner4a7cc882015-03-06 23:35:27 +01003727 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003728
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003729fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003730 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003731
Benjamin Petersonac913412011-07-03 16:25:11 -05003732 /* The purpose of this block is to put aside the generator's exception
3733 state and restore that of the calling frame. If the current
3734 exception state is from the caller, we clear the exception values
3735 on the generator frame, so they are not swapped back in latter. The
3736 origin of the current exception state is determined by checking for
3737 except handler blocks, which we must be in iff a new exception
3738 state came into existence in this frame. (An uncaught exception
3739 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3740 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003741 for (i = 0; i < f->f_iblock; i++) {
3742 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003743 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003744 }
3745 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003746 if (i == f->f_iblock)
3747 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003748 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003749 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003750 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003751 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003754 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003756 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3757 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003759 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 why = WHY_EXCEPTION;
3761 }
3762 }
3763 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003764 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3765 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 PyTrace_RETURN, NULL);
3767 }
3768 }
3769 if (tstate->c_profilefunc) {
3770 if (why == WHY_EXCEPTION)
3771 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003772 tstate->c_profileobj,
3773 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003775 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3776 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003778 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003779 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 }
3781 }
3782 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003785exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003787 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003789
Victor Stinnerefde1462015-03-21 15:04:43 +01003790 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003791}
3792
Benjamin Petersonb204a422011-06-05 22:04:07 -05003793static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003794format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3795{
3796 int err;
3797 Py_ssize_t len = PyList_GET_SIZE(names);
3798 PyObject *name_str, *comma, *tail, *tmp;
3799
3800 assert(PyList_CheckExact(names));
3801 assert(len >= 1);
3802 /* Deal with the joys of natural language. */
3803 switch (len) {
3804 case 1:
3805 name_str = PyList_GET_ITEM(names, 0);
3806 Py_INCREF(name_str);
3807 break;
3808 case 2:
3809 name_str = PyUnicode_FromFormat("%U and %U",
3810 PyList_GET_ITEM(names, len - 2),
3811 PyList_GET_ITEM(names, len - 1));
3812 break;
3813 default:
3814 tail = PyUnicode_FromFormat(", %U, and %U",
3815 PyList_GET_ITEM(names, len - 2),
3816 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003817 if (tail == NULL)
3818 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003819 /* Chop off the last two objects in the list. This shouldn't actually
3820 fail, but we can't be too careful. */
3821 err = PyList_SetSlice(names, len - 2, len, NULL);
3822 if (err == -1) {
3823 Py_DECREF(tail);
3824 return;
3825 }
3826 /* Stitch everything up into a nice comma-separated list. */
3827 comma = PyUnicode_FromString(", ");
3828 if (comma == NULL) {
3829 Py_DECREF(tail);
3830 return;
3831 }
3832 tmp = PyUnicode_Join(comma, names);
3833 Py_DECREF(comma);
3834 if (tmp == NULL) {
3835 Py_DECREF(tail);
3836 return;
3837 }
3838 name_str = PyUnicode_Concat(tmp, tail);
3839 Py_DECREF(tmp);
3840 Py_DECREF(tail);
3841 break;
3842 }
3843 if (name_str == NULL)
3844 return;
3845 PyErr_Format(PyExc_TypeError,
3846 "%U() missing %i required %s argument%s: %U",
3847 co->co_name,
3848 len,
3849 kind,
3850 len == 1 ? "" : "s",
3851 name_str);
3852 Py_DECREF(name_str);
3853}
3854
3855static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003856missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003857 PyObject **fastlocals)
3858{
Victor Stinner74319ae2016-08-25 00:04:09 +02003859 Py_ssize_t i, j = 0;
3860 Py_ssize_t start, end;
3861 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003862 const char *kind = positional ? "positional" : "keyword-only";
3863 PyObject *missing_names;
3864
3865 /* Compute the names of the arguments that are missing. */
3866 missing_names = PyList_New(missing);
3867 if (missing_names == NULL)
3868 return;
3869 if (positional) {
3870 start = 0;
3871 end = co->co_argcount - defcount;
3872 }
3873 else {
3874 start = co->co_argcount;
3875 end = start + co->co_kwonlyargcount;
3876 }
3877 for (i = start; i < end; i++) {
3878 if (GETLOCAL(i) == NULL) {
3879 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3880 PyObject *name = PyObject_Repr(raw);
3881 if (name == NULL) {
3882 Py_DECREF(missing_names);
3883 return;
3884 }
3885 PyList_SET_ITEM(missing_names, j++, name);
3886 }
3887 }
3888 assert(j == missing);
3889 format_missing(kind, co, missing_names);
3890 Py_DECREF(missing_names);
3891}
3892
3893static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003894too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3895 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003896{
3897 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003898 Py_ssize_t kwonly_given = 0;
3899 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003901 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003902
Benjamin Petersone109c702011-06-24 09:37:26 -05003903 assert((co->co_flags & CO_VARARGS) == 0);
3904 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003905 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3906 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003907 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003908 }
3909 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003910 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003911 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003912 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003913 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003914 }
3915 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003916 plural = (co_argcount != 1);
3917 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918 }
3919 if (sig == NULL)
3920 return;
3921 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003922 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3923 kwonly_sig = PyUnicode_FromFormat(format,
3924 given != 1 ? "s" : "",
3925 kwonly_given,
3926 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003927 if (kwonly_sig == NULL) {
3928 Py_DECREF(sig);
3929 return;
3930 }
3931 }
3932 else {
3933 /* This will not fail. */
3934 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003935 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003936 }
3937 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003938 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003939 co->co_name,
3940 sig,
3941 plural ? "s" : "",
3942 given,
3943 kwonly_sig,
3944 given == 1 && !kwonly_given ? "was" : "were");
3945 Py_DECREF(sig);
3946 Py_DECREF(kwonly_sig);
3947}
3948
Guido van Rossumc2e20742006-02-27 22:32:47 +00003949/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003950 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003951 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003952
Victor Stinner40ee3012014-06-16 15:59:28 +02003953static PyObject *
3954_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003955 PyObject **args, Py_ssize_t argcount,
3956 PyObject **kws, Py_ssize_t kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003957 PyObject *kwnames, PyObject **kwstack,
Victor Stinner74319ae2016-08-25 00:04:09 +02003958 PyObject **defs, Py_ssize_t defcount,
3959 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003960 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003961{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003962 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003963 PyFrameObject *f;
3964 PyObject *retval = NULL;
3965 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003966 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003968 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3969 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003970 PyObject *kwdict;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003971 Py_ssize_t kwcount2 = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc7020012016-08-16 23:40:29 +02003972
3973 assert((kwcount == 0) || (kws != NULL));
Tim Peters5ca576e2001-06-18 22:08:13 +00003974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 if (globals == NULL) {
3976 PyErr_SetString(PyExc_SystemError,
3977 "PyEval_EvalCodeEx: NULL globals");
3978 return NULL;
3979 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003980
Victor Stinnerc7020012016-08-16 23:40:29 +02003981 /* Create the frame */
3982 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003985 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003987 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 fastlocals = f->f_localsplus;
3989 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003990
Victor Stinnerc7020012016-08-16 23:40:29 +02003991 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003992 if (co->co_flags & CO_VARKEYWORDS) {
3993 kwdict = PyDict_New();
3994 if (kwdict == NULL)
3995 goto fail;
3996 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003997 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003999 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004000 SETLOCAL(i, kwdict);
4001 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004002 else {
4003 kwdict = NULL;
4004 }
4005
4006 /* Copy positional arguments into local variables */
4007 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004008 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004009 }
4010 else {
4011 n = argcount;
4012 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004013 for (i = 0; i < n; i++) {
4014 x = args[i];
4015 Py_INCREF(x);
4016 SETLOCAL(i, x);
4017 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004018
4019 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004020 if (co->co_flags & CO_VARARGS) {
4021 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004022 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004024 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004025 SETLOCAL(total_args, u);
4026 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 x = args[i];
4028 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004031 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004032
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004033 /* Handle keyword arguments passed as an array of (key, value) pairs */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004034 for (i = 0; i < kwcount; i++) {
4035 PyObject **co_varnames;
4036 PyObject *keyword = kws[2*i];
4037 PyObject *value = kws[2*i + 1];
Victor Stinner17061a92016-08-16 23:39:42 +02004038 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004039
Benjamin Petersonb204a422011-06-05 22:04:07 -05004040 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4041 PyErr_Format(PyExc_TypeError,
4042 "%U() keywords must be strings",
4043 co->co_name);
4044 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004046
Benjamin Petersonb204a422011-06-05 22:04:07 -05004047 /* Speed hack: do raw pointer compares. As names are
4048 normally interned this should almost always hit. */
4049 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4050 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004051 PyObject *name = co_varnames[j];
4052 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004053 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004054 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004055 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004056
Benjamin Petersonb204a422011-06-05 22:04:07 -05004057 /* Slow fallback, just in case */
4058 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004059 PyObject *name = co_varnames[j];
4060 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4061 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004062 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004063 }
4064 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004066 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004067 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004068
Benjamin Petersonb204a422011-06-05 22:04:07 -05004069 if (j >= total_args && kwdict == NULL) {
4070 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004071 "%U() got an unexpected keyword argument '%S'",
4072 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004073 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004075
Christian Heimes0bd447f2013-07-20 14:48:10 +02004076 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4077 goto fail;
4078 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004080
Benjamin Petersonb204a422011-06-05 22:04:07 -05004081 kw_found:
4082 if (GETLOCAL(j) != NULL) {
4083 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004084 "%U() got multiple values for argument '%S'",
4085 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004086 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004088 Py_INCREF(value);
4089 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004091
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004092 /* Handle keyword arguments passed as keys tuple + values array */
4093 for (i = 0; i < kwcount2; i++) {
4094 PyObject **co_varnames;
4095 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4096 PyObject *value = kwstack[i];
4097 int j;
4098 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4099 PyErr_Format(PyExc_TypeError,
4100 "%U() keywords must be strings",
4101 co->co_name);
4102 goto fail;
4103 }
4104 /* Speed hack: do raw pointer compares. As names are
4105 normally interned this should almost always hit. */
4106 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4107 for (j = 0; j < total_args; j++) {
4108 PyObject *nm = co_varnames[j];
4109 if (nm == keyword)
4110 goto kw_found2;
4111 }
4112 /* Slow fallback, just in case */
4113 for (j = 0; j < total_args; j++) {
4114 PyObject *nm = co_varnames[j];
4115 int cmp = PyObject_RichCompareBool(
4116 keyword, nm, Py_EQ);
4117 if (cmp > 0)
4118 goto kw_found2;
4119 else if (cmp < 0)
4120 goto fail;
4121 }
4122 if (j >= total_args && kwdict == NULL) {
4123 PyErr_Format(PyExc_TypeError,
4124 "%U() got an unexpected "
4125 "keyword argument '%S'",
4126 co->co_name,
4127 keyword);
4128 goto fail;
4129 }
4130 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4131 goto fail;
4132 }
4133 continue;
4134 kw_found2:
4135 if (GETLOCAL(j) != NULL) {
4136 PyErr_Format(PyExc_TypeError,
4137 "%U() got multiple "
4138 "values for argument '%S'",
4139 co->co_name,
4140 keyword);
4141 goto fail;
4142 }
4143 Py_INCREF(value);
4144 SETLOCAL(j, value);
4145 }
4146
Victor Stinnerc7020012016-08-16 23:40:29 +02004147 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004148 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004149 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 goto fail;
4151 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004152
4153 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004154 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004155 Py_ssize_t m = co->co_argcount - defcount;
4156 Py_ssize_t missing = 0;
4157 for (i = argcount; i < m; i++) {
4158 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004159 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004160 }
4161 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004162 if (missing) {
4163 missing_arguments(co, missing, defcount, fastlocals);
4164 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004165 }
4166 if (n > m)
4167 i = n - m;
4168 else
4169 i = 0;
4170 for (; i < defcount; i++) {
4171 if (GETLOCAL(m+i) == NULL) {
4172 PyObject *def = defs[i];
4173 Py_INCREF(def);
4174 SETLOCAL(m+i, def);
4175 }
4176 }
4177 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004178
4179 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004181 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004182 for (i = co->co_argcount; i < total_args; i++) {
4183 PyObject *name;
4184 if (GETLOCAL(i) != NULL)
4185 continue;
4186 name = PyTuple_GET_ITEM(co->co_varnames, i);
4187 if (kwdefs != NULL) {
4188 PyObject *def = PyDict_GetItem(kwdefs, name);
4189 if (def) {
4190 Py_INCREF(def);
4191 SETLOCAL(i, def);
4192 continue;
4193 }
4194 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004195 missing++;
4196 }
4197 if (missing) {
4198 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004199 goto fail;
4200 }
4201 }
4202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004204 vars into frame. */
4205 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004207 int arg;
4208 /* Possibly account for the cell variable being an argument. */
4209 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004210 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004211 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004212 /* Clear the local copy. */
4213 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004214 }
4215 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004216 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004217 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004218 if (c == NULL)
4219 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004220 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004222
4223 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004224 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4225 PyObject *o = PyTuple_GET_ITEM(closure, i);
4226 Py_INCREF(o);
4227 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004229
Yury Selivanoveb636452016-09-08 22:01:51 -07004230 /* Handle generator/coroutine/asynchronous generator */
4231 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004232 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004233 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004234 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004235
4236 if (is_coro && tstate->in_coroutine_wrapper) {
4237 assert(coro_wrapper != NULL);
4238 PyErr_Format(PyExc_RuntimeError,
4239 "coroutine wrapper %.200R attempted "
4240 "to recursively wrap %.200R",
4241 coro_wrapper,
4242 co);
4243 goto fail;
4244 }
Yury Selivanov75445082015-05-11 22:57:16 -04004245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004246 /* Don't need to keep the reference to f_back, it will be set
4247 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004248 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 /* Create a new generator that owns the ready to run frame
4253 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004254 if (is_coro) {
4255 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004256 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4257 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004258 } else {
4259 gen = PyGen_NewWithQualName(f, name, qualname);
4260 }
Yury Selivanov75445082015-05-11 22:57:16 -04004261 if (gen == NULL)
4262 return NULL;
4263
Yury Selivanov94c22632015-06-04 10:16:51 -04004264 if (is_coro && coro_wrapper != NULL) {
4265 PyObject *wrapped;
4266 tstate->in_coroutine_wrapper = 1;
4267 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4268 tstate->in_coroutine_wrapper = 0;
4269 return wrapped;
4270 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004271
Yury Selivanov75445082015-05-11 22:57:16 -04004272 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004276
Thomas Woutersce272b62007-09-19 21:19:28 +00004277fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004279 /* decref'ing the frame can cause __del__ methods to get invoked,
4280 which can call back into Python. While we're done with the
4281 current Python frame (f), the associated C stack is still in use,
4282 so recursion_depth must be boosted for the duration.
4283 */
4284 assert(tstate != NULL);
4285 ++tstate->recursion_depth;
4286 Py_DECREF(f);
4287 --tstate->recursion_depth;
4288 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004289}
4290
Victor Stinner40ee3012014-06-16 15:59:28 +02004291PyObject *
4292PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4293 PyObject **args, int argcount, PyObject **kws, int kwcount,
4294 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4295{
4296 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004297 args, argcount,
4298 kws, kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004299 NULL, NULL,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004300 defs, defcount,
4301 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004302 NULL, NULL);
4303}
Tim Peters5ca576e2001-06-18 22:08:13 +00004304
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004305static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004306special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004309 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004311 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004312 return NULL;
4313 }
4314 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004315}
4316
4317
Benjamin Peterson87880242011-07-03 16:48:31 -05004318/* These 3 functions deal with the exception state of generators. */
4319
4320static void
4321save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4322{
4323 PyObject *type, *value, *traceback;
4324 Py_XINCREF(tstate->exc_type);
4325 Py_XINCREF(tstate->exc_value);
4326 Py_XINCREF(tstate->exc_traceback);
4327 type = f->f_exc_type;
4328 value = f->f_exc_value;
4329 traceback = f->f_exc_traceback;
4330 f->f_exc_type = tstate->exc_type;
4331 f->f_exc_value = tstate->exc_value;
4332 f->f_exc_traceback = tstate->exc_traceback;
4333 Py_XDECREF(type);
4334 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004335 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004336}
4337
4338static void
4339swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4340{
4341 PyObject *tmp;
4342 tmp = tstate->exc_type;
4343 tstate->exc_type = f->f_exc_type;
4344 f->f_exc_type = tmp;
4345 tmp = tstate->exc_value;
4346 tstate->exc_value = f->f_exc_value;
4347 f->f_exc_value = tmp;
4348 tmp = tstate->exc_traceback;
4349 tstate->exc_traceback = f->f_exc_traceback;
4350 f->f_exc_traceback = tmp;
4351}
4352
4353static void
4354restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4355{
4356 PyObject *type, *value, *tb;
4357 type = tstate->exc_type;
4358 value = tstate->exc_value;
4359 tb = tstate->exc_traceback;
4360 tstate->exc_type = f->f_exc_type;
4361 tstate->exc_value = f->f_exc_value;
4362 tstate->exc_traceback = f->f_exc_traceback;
4363 f->f_exc_type = NULL;
4364 f->f_exc_value = NULL;
4365 f->f_exc_traceback = NULL;
4366 Py_XDECREF(type);
4367 Py_XDECREF(value);
4368 Py_XDECREF(tb);
4369}
4370
4371
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004372/* Logic for the raise statement (too complicated for inlining).
4373 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004374static int
Collin Winter828f04a2007-08-31 00:04:24 +00004375do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 if (exc == NULL) {
4380 /* Reraise */
4381 PyThreadState *tstate = PyThreadState_GET();
4382 PyObject *tb;
4383 type = tstate->exc_type;
4384 value = tstate->exc_value;
4385 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004386 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 PyErr_SetString(PyExc_RuntimeError,
4388 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004389 return 0;
4390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 Py_XINCREF(type);
4392 Py_XINCREF(value);
4393 Py_XINCREF(tb);
4394 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004395 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 /* We support the following forms of raise:
4399 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004400 raise <instance>
4401 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 if (PyExceptionClass_Check(exc)) {
4404 type = exc;
4405 value = PyObject_CallObject(exc, NULL);
4406 if (value == NULL)
4407 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004408 if (!PyExceptionInstance_Check(value)) {
4409 PyErr_Format(PyExc_TypeError,
4410 "calling %R should have returned an instance of "
4411 "BaseException, not %R",
4412 type, Py_TYPE(value));
4413 goto raise_error;
4414 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 }
4416 else if (PyExceptionInstance_Check(exc)) {
4417 value = exc;
4418 type = PyExceptionInstance_Class(exc);
4419 Py_INCREF(type);
4420 }
4421 else {
4422 /* Not something you can raise. You get an exception
4423 anyway, just not what you specified :-) */
4424 Py_DECREF(exc);
4425 PyErr_SetString(PyExc_TypeError,
4426 "exceptions must derive from BaseException");
4427 goto raise_error;
4428 }
Collin Winter828f04a2007-08-31 00:04:24 +00004429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 if (cause) {
4431 PyObject *fixed_cause;
4432 if (PyExceptionClass_Check(cause)) {
4433 fixed_cause = PyObject_CallObject(cause, NULL);
4434 if (fixed_cause == NULL)
4435 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004436 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004438 else if (PyExceptionInstance_Check(cause)) {
4439 fixed_cause = cause;
4440 }
4441 else if (cause == Py_None) {
4442 Py_DECREF(cause);
4443 fixed_cause = NULL;
4444 }
4445 else {
4446 PyErr_SetString(PyExc_TypeError,
4447 "exception causes must derive from "
4448 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004449 goto raise_error;
4450 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004451 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 }
Collin Winter828f04a2007-08-31 00:04:24 +00004453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 PyErr_SetObject(type, value);
4455 /* PyErr_SetObject incref's its arguments */
4456 Py_XDECREF(value);
4457 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004458 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004459
4460raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 Py_XDECREF(value);
4462 Py_XDECREF(type);
4463 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004464 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004465}
4466
Tim Petersd6d010b2001-06-21 02:49:55 +00004467/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004468 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004469
Guido van Rossum0368b722007-05-11 16:50:42 +00004470 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4471 with a variable target.
4472*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004473
Barry Warsawe42b18f1997-08-25 22:13:04 +00004474static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004475unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 int i = 0, j = 0;
4478 Py_ssize_t ll = 0;
4479 PyObject *it; /* iter(v) */
4480 PyObject *w;
4481 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 it = PyObject_GetIter(v);
4486 if (it == NULL)
4487 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 for (; i < argcnt; i++) {
4490 w = PyIter_Next(it);
4491 if (w == NULL) {
4492 /* Iterator done, via error or exhaustion. */
4493 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004494 if (argcntafter == -1) {
4495 PyErr_Format(PyExc_ValueError,
4496 "not enough values to unpack (expected %d, got %d)",
4497 argcnt, i);
4498 }
4499 else {
4500 PyErr_Format(PyExc_ValueError,
4501 "not enough values to unpack "
4502 "(expected at least %d, got %d)",
4503 argcnt + argcntafter, i);
4504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 }
4506 goto Error;
4507 }
4508 *--sp = w;
4509 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 if (argcntafter == -1) {
4512 /* We better have exhausted the iterator now. */
4513 w = PyIter_Next(it);
4514 if (w == NULL) {
4515 if (PyErr_Occurred())
4516 goto Error;
4517 Py_DECREF(it);
4518 return 1;
4519 }
4520 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004521 PyErr_Format(PyExc_ValueError,
4522 "too many values to unpack (expected %d)",
4523 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 goto Error;
4525 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 l = PySequence_List(it);
4528 if (l == NULL)
4529 goto Error;
4530 *--sp = l;
4531 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 ll = PyList_GET_SIZE(l);
4534 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004535 PyErr_Format(PyExc_ValueError,
4536 "not enough values to unpack (expected at least %d, got %zd)",
4537 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 goto Error;
4539 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 /* Pop the "after-variable" args off the list. */
4542 for (j = argcntafter; j > 0; j--, i++) {
4543 *--sp = PyList_GET_ITEM(l, ll - j);
4544 }
4545 /* Resize the list. */
4546 Py_SIZE(l) = ll - argcntafter;
4547 Py_DECREF(it);
4548 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004549
Tim Petersd6d010b2001-06-21 02:49:55 +00004550Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 for (; i > 0; i--, sp++)
4552 Py_DECREF(*sp);
4553 Py_XDECREF(it);
4554 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004555}
4556
4557
Guido van Rossum96a42c81992-01-12 02:29:51 +00004558#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004559static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004560prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 printf("%s ", str);
4563 if (PyObject_Print(v, stdout, 0) != 0)
4564 PyErr_Clear(); /* Don't know what else to do */
4565 printf("\n");
4566 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004567}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004568#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004569
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004570static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004571call_exc_trace(Py_tracefunc func, PyObject *self,
4572 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004573{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004574 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004576 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 if (value == NULL) {
4578 value = Py_None;
4579 Py_INCREF(value);
4580 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004581 PyErr_NormalizeException(&type, &value, &orig_traceback);
4582 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 arg = PyTuple_Pack(3, type, value, traceback);
4584 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004585 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 return;
4587 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004588 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 Py_DECREF(arg);
4590 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004591 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 else {
4593 Py_XDECREF(type);
4594 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004595 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004597}
4598
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004599static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004600call_trace_protected(Py_tracefunc func, PyObject *obj,
4601 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 PyObject *type, *value, *traceback;
4605 int err;
4606 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004607 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 if (err == 0)
4609 {
4610 PyErr_Restore(type, value, traceback);
4611 return 0;
4612 }
4613 else {
4614 Py_XDECREF(type);
4615 Py_XDECREF(value);
4616 Py_XDECREF(traceback);
4617 return -1;
4618 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004619}
4620
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004621static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004622call_trace(Py_tracefunc func, PyObject *obj,
4623 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004626 int result;
4627 if (tstate->tracing)
4628 return 0;
4629 tstate->tracing++;
4630 tstate->use_tracing = 0;
4631 result = func(obj, frame, what, arg);
4632 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4633 || (tstate->c_profilefunc != NULL));
4634 tstate->tracing--;
4635 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004636}
4637
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004638PyObject *
4639_PyEval_CallTracing(PyObject *func, PyObject *args)
4640{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004641 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 int save_tracing = tstate->tracing;
4643 int save_use_tracing = tstate->use_tracing;
4644 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004646 tstate->tracing = 0;
4647 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4648 || (tstate->c_profilefunc != NULL));
4649 result = PyObject_Call(func, args, NULL);
4650 tstate->tracing = save_tracing;
4651 tstate->use_tracing = save_use_tracing;
4652 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004653}
4654
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004655/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004656static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004657maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004658 PyThreadState *tstate, PyFrameObject *frame,
4659 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 int result = 0;
4662 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 /* If the last instruction executed isn't in the current
4665 instruction window, reset the window.
4666 */
4667 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4668 PyAddrPair bounds;
4669 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4670 &bounds);
4671 *instr_lb = bounds.ap_lower;
4672 *instr_ub = bounds.ap_upper;
4673 }
4674 /* If the last instruction falls at the start of a line or if
4675 it represents a jump backwards, update the frame's line
4676 number and call the trace function. */
4677 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4678 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004679 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 }
4681 *instr_prev = frame->f_lasti;
4682 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004683}
4684
Fred Drake5755ce62001-06-27 19:19:46 +00004685void
4686PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 PyThreadState *tstate = PyThreadState_GET();
4689 PyObject *temp = tstate->c_profileobj;
4690 Py_XINCREF(arg);
4691 tstate->c_profilefunc = NULL;
4692 tstate->c_profileobj = NULL;
4693 /* Must make sure that tracing is not ignored if 'temp' is freed */
4694 tstate->use_tracing = tstate->c_tracefunc != NULL;
4695 Py_XDECREF(temp);
4696 tstate->c_profilefunc = func;
4697 tstate->c_profileobj = arg;
4698 /* Flag that tracing or profiling is turned on */
4699 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004700}
4701
4702void
4703PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004705 PyThreadState *tstate = PyThreadState_GET();
4706 PyObject *temp = tstate->c_traceobj;
4707 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4708 Py_XINCREF(arg);
4709 tstate->c_tracefunc = NULL;
4710 tstate->c_traceobj = NULL;
4711 /* Must make sure that profiling is not ignored if 'temp' is freed */
4712 tstate->use_tracing = tstate->c_profilefunc != NULL;
4713 Py_XDECREF(temp);
4714 tstate->c_tracefunc = func;
4715 tstate->c_traceobj = arg;
4716 /* Flag that tracing or profiling is turned on */
4717 tstate->use_tracing = ((func != NULL)
4718 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004719}
4720
Yury Selivanov75445082015-05-11 22:57:16 -04004721void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004722_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004723{
4724 PyThreadState *tstate = PyThreadState_GET();
4725
Yury Selivanov75445082015-05-11 22:57:16 -04004726 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004727 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004728}
4729
4730PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004731_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004732{
4733 PyThreadState *tstate = PyThreadState_GET();
4734 return tstate->coroutine_wrapper;
4735}
4736
Yury Selivanoveb636452016-09-08 22:01:51 -07004737void
4738_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4739{
4740 PyThreadState *tstate = PyThreadState_GET();
4741
4742 Py_XINCREF(firstiter);
4743 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4744}
4745
4746PyObject *
4747_PyEval_GetAsyncGenFirstiter(void)
4748{
4749 PyThreadState *tstate = PyThreadState_GET();
4750 return tstate->async_gen_firstiter;
4751}
4752
4753void
4754_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4755{
4756 PyThreadState *tstate = PyThreadState_GET();
4757
4758 Py_XINCREF(finalizer);
4759 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4760}
4761
4762PyObject *
4763_PyEval_GetAsyncGenFinalizer(void)
4764{
4765 PyThreadState *tstate = PyThreadState_GET();
4766 return tstate->async_gen_finalizer;
4767}
4768
Guido van Rossumb209a111997-04-29 18:18:01 +00004769PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004770PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004772 PyFrameObject *current_frame = PyEval_GetFrame();
4773 if (current_frame == NULL)
4774 return PyThreadState_GET()->interp->builtins;
4775 else
4776 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004777}
4778
Guido van Rossumb209a111997-04-29 18:18:01 +00004779PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004780PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004783 if (current_frame == NULL) {
4784 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004785 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004786 }
4787
4788 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4789 return NULL;
4790
4791 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004793}
4794
Guido van Rossumb209a111997-04-29 18:18:01 +00004795PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004796PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 PyFrameObject *current_frame = PyEval_GetFrame();
4799 if (current_frame == NULL)
4800 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004801
4802 assert(current_frame->f_globals != NULL);
4803 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004804}
4805
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004806PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004807PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004809 PyThreadState *tstate = PyThreadState_GET();
4810 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004811}
4812
Guido van Rossum6135a871995-01-09 17:53:26 +00004813int
Tim Peters5ba58662001-07-16 02:29:45 +00004814PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 PyFrameObject *current_frame = PyEval_GetFrame();
4817 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 if (current_frame != NULL) {
4820 const int codeflags = current_frame->f_code->co_flags;
4821 const int compilerflags = codeflags & PyCF_MASK;
4822 if (compilerflags) {
4823 result = 1;
4824 cf->cf_flags |= compilerflags;
4825 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004826#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004827 if (codeflags & CO_GENERATOR_ALLOWED) {
4828 result = 1;
4829 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4830 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004831#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 }
4833 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004834}
4835
Guido van Rossum3f5da241990-12-20 15:06:42 +00004836
Guido van Rossum681d79a1995-07-18 14:51:37 +00004837/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004838 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004839
Guido van Rossumb209a111997-04-29 18:18:01 +00004840PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004841PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004842{
Victor Stinner59b356d2015-03-16 11:52:32 +01004843#ifdef Py_DEBUG
4844 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4845 set. It raises a new exception if parameters are invalid or if
4846 PyTuple_New() fails, and so the original exception is lost. */
4847 assert(!PyErr_Occurred());
4848#endif
4849
Victor Stinner8a31c822016-08-19 17:12:23 +02004850 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004851 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004852 }
Victor Stinner155ea652016-08-22 23:26:00 +02004853
4854 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 PyErr_SetString(PyExc_TypeError,
4856 "argument list must be a tuple");
4857 return NULL;
4858 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004859
Victor Stinner8a31c822016-08-19 17:12:23 +02004860 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 PyErr_SetString(PyExc_TypeError,
4862 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004863 return NULL;
4864 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004865
Victor Stinner6e2333d2016-08-23 00:25:01 +02004866 return PyObject_Call(func, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004867}
4868
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004869const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004870PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 if (PyMethod_Check(func))
4873 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4874 else if (PyFunction_Check(func))
4875 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4876 else if (PyCFunction_Check(func))
4877 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4878 else
4879 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004880}
4881
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004882const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004883PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004884{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 if (PyMethod_Check(func))
4886 return "()";
4887 else if (PyFunction_Check(func))
4888 return "()";
4889 else if (PyCFunction_Check(func))
4890 return "()";
4891 else
4892 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004893}
4894
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004895static void
Victor Stinner74319ae2016-08-25 00:04:09 +02004896err_args(PyObject *func, int flags, Py_ssize_t nargs)
Jeremy Hylton192690e2002-08-16 18:36:11 +00004897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 if (flags & METH_NOARGS)
4899 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02004900 "%.200s() takes no arguments (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004901 ((PyCFunctionObject *)func)->m_ml->ml_name,
4902 nargs);
4903 else
4904 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02004905 "%.200s() takes exactly one argument (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 ((PyCFunctionObject *)func)->m_ml->ml_name,
4907 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004908}
4909
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004910#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004911if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004912 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4913 tstate, tstate->frame, \
4914 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 x = NULL; \
4916 } \
4917 else { \
4918 x = call; \
4919 if (tstate->c_profilefunc != NULL) { \
4920 if (x == NULL) { \
4921 call_trace_protected(tstate->c_profilefunc, \
4922 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004923 tstate, tstate->frame, \
4924 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004925 /* XXX should pass (type, value, tb) */ \
4926 } else { \
4927 if (call_trace(tstate->c_profilefunc, \
4928 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004929 tstate, tstate->frame, \
4930 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 Py_DECREF(x); \
4932 x = NULL; \
4933 } \
4934 } \
4935 } \
4936 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004937} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 x = call; \
4939 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004940
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004941static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004942call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004943#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004945#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004946 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004947{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004948 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 PyObject *func = *pfunc;
4950 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004951 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4952 Py_ssize_t nargs = oparg - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 /* Always dispatch PyCFunction first, because these are
4955 presumed to be the most frequent callable object.
4956 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004957 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 int flags = PyCFunction_GET_FLAGS(func);
4959 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004961 PCALL(PCALL_CFUNCTION);
Victor Stinnerd8735722016-09-09 12:36:44 -07004962 if (kwnames == NULL && flags & (METH_NOARGS | METH_O)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004963 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4964 PyObject *self = PyCFunction_GET_SELF(func);
Victor Stinner74319ae2016-08-25 00:04:09 +02004965 if (flags & METH_NOARGS && nargs == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004966 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004967
Victor Stinnerefde1462015-03-21 15:04:43 +01004968 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004970 else if (flags & METH_O && nargs == 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 PyObject *arg = EXT_POP(*pp_stack);
4972 C_TRACE(x, (*meth)(self,arg));
4973 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004974
Victor Stinnerefde1462015-03-21 15:04:43 +01004975 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 }
4977 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02004978 err_args(func, flags, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 x = NULL;
4980 }
4981 }
4982 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004983 PyObject *callargs, *kwdict = NULL;
Victor Stinnerd8735722016-09-09 12:36:44 -07004984 if (kwnames != NULL) {
4985 kwdict = create_keyword_args(kwnames, pp_stack, func);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004986 if (kwdict == NULL) {
4987 x = NULL;
4988 goto cfuncerror;
4989 }
4990 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004991 callargs = load_args(pp_stack, nargs);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004992 if (callargs != NULL) {
4993 READ_TIMESTAMP(*pintr0);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004994 C_TRACE(x, PyCFunction_Call(func, callargs, kwdict));
Victor Stinner0ff0f542013-07-08 22:27:42 +02004995 READ_TIMESTAMP(*pintr1);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004996 Py_DECREF(callargs);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004997 }
4998 else {
4999 x = NULL;
5000 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005001 Py_XDECREF(kwdict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005003 }
5004 else {
Victor Stinnerd8735722016-09-09 12:36:44 -07005005 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5006 PyObject **stack;
5007
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005008 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
5009 /* optimize access to bound methods */
5010 PyObject *self = PyMethod_GET_SELF(func);
5011 PCALL(PCALL_METHOD);
5012 PCALL(PCALL_BOUND_METHOD);
5013 Py_INCREF(self);
5014 func = PyMethod_GET_FUNCTION(func);
5015 Py_INCREF(func);
5016 Py_SETREF(*pfunc, self);
5017 nargs++;
5018 }
5019 else {
5020 Py_INCREF(func);
5021 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005022
Victor Stinnerd8735722016-09-09 12:36:44 -07005023 stack = (*pp_stack) - nargs - nkwargs;
5024
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005025 READ_TIMESTAMP(*pintr0);
5026 if (PyFunction_Check(func)) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005027 x = fast_function(func, stack, nargs, kwnames);
5028 }
5029 else {
5030 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005031 }
5032 READ_TIMESTAMP(*pintr1);
5033
5034 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005036
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005037cfuncerror:
5038 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
5039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005040 /* Clear the stack of the function object. Also removes
5041 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02005042 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 */
5044 while ((*pp_stack) > pfunc) {
5045 w = EXT_POP(*pp_stack);
5046 Py_DECREF(w);
5047 PCALL(PCALL_POP);
5048 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005051}
5052
Victor Stinnere90bdb12016-08-25 23:26:50 +02005053/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00005054 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00005055 For the simplest case -- a function that takes only positional
5056 arguments and is called with only positional arguments -- it
5057 inlines the most primitive frame setup code from
5058 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
5059 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00005060*/
5061
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005062static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07005063_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
5064 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005065{
5066 PyFrameObject *f;
5067 PyThreadState *tstate = PyThreadState_GET();
5068 PyObject **fastlocals;
5069 Py_ssize_t i;
5070 PyObject *result;
5071
5072 PCALL(PCALL_FASTER_FUNCTION);
5073 assert(globals != NULL);
5074 /* XXX Perhaps we should create a specialized
5075 PyFrame_New() that doesn't take locals, but does
5076 take builtins without sanity checking them.
5077 */
5078 assert(tstate != NULL);
5079 f = PyFrame_New(tstate, co, globals, NULL);
5080 if (f == NULL) {
5081 return NULL;
5082 }
5083
5084 fastlocals = f->f_localsplus;
5085
Victor Stinner74319ae2016-08-25 00:04:09 +02005086 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005087 Py_INCREF(*args);
5088 fastlocals[i] = *args++;
5089 }
5090 result = PyEval_EvalFrameEx(f,0);
5091
5092 ++tstate->recursion_depth;
5093 Py_DECREF(f);
5094 --tstate->recursion_depth;
5095
5096 return result;
5097}
5098
Victor Stinnere90bdb12016-08-25 23:26:50 +02005099static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07005100fast_function(PyObject *func, PyObject **stack,
5101 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00005102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005103 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5104 PyObject *globals = PyFunction_GET_GLOBALS(func);
5105 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005106 PyObject *kwdefs, *closure, *name, *qualname;
5107 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07005108 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005109 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07005110
5111 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner577e1f82016-08-25 00:29:32 +02005112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005113 PCALL(PCALL_FUNCTION);
5114 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005115
Victor Stinner74319ae2016-08-25 00:04:09 +02005116 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005117 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5118 {
Victor Stinner2eedc112016-08-22 12:29:42 +02005119 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005120 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005121 }
5122 else if (nargs == 0 && argdefs != NULL
5123 && co->co_argcount == Py_SIZE(argdefs)) {
5124 /* function called with no arguments, but all parameters have
5125 a default value: use default values as arguments .*/
5126 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005127 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005128 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005129 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005130
5131 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5132 closure = PyFunction_GET_CLOSURE(func);
5133 name = ((PyFunctionObject *)func) -> func_name;
5134 qualname = ((PyFunctionObject *)func) -> func_qualname;
5135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 if (argdefs != NULL) {
5137 d = &PyTuple_GET_ITEM(argdefs, 0);
5138 nd = Py_SIZE(argdefs);
5139 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005140 else {
5141 d = NULL;
5142 nd = 0;
5143 }
5144 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02005145 stack, nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005146 NULL, 0,
Victor Stinnerd8735722016-09-09 12:36:44 -07005147 kwnames, stack + nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005148 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005149 closure, name, qualname);
5150}
5151
5152PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07005153_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
5154 Py_ssize_t nargs, PyObject *kwnames)
5155{
5156 return fast_function(func, stack, nargs, kwnames);
5157}
5158
5159PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005160_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02005161 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005162{
5163 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5164 PyObject *globals = PyFunction_GET_GLOBALS(func);
5165 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
5166 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02005167 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005168 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02005169 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02005170 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005171
Victor Stinner74319ae2016-08-25 00:04:09 +02005172 assert(func != NULL);
5173 assert(nargs >= 0);
5174 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02005175 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005176
Victor Stinner577e1f82016-08-25 00:29:32 +02005177 PCALL(PCALL_FUNCTION);
5178 PCALL(PCALL_FAST_FUNCTION);
5179
Victor Stinnerb9009392016-08-22 23:15:44 +02005180 if (co->co_kwonlyargcount == 0 &&
5181 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005182 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5183 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005184 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005185 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005186 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005187 }
5188 else if (nargs == 0 && argdefs != NULL
5189 && co->co_argcount == Py_SIZE(argdefs)) {
5190 /* function called with no arguments, but all parameters have
5191 a default value: use default values as arguments .*/
5192 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005193 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005194 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005195 }
5196
Victor Stinnerb9009392016-08-22 23:15:44 +02005197 if (kwargs != NULL) {
5198 Py_ssize_t pos, i;
5199 nk = PyDict_Size(kwargs);
5200
5201 kwtuple = PyTuple_New(2 * nk);
5202 if (kwtuple == NULL) {
5203 return NULL;
5204 }
5205
5206 k = &PyTuple_GET_ITEM(kwtuple, 0);
5207 pos = i = 0;
5208 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5209 Py_INCREF(k[i]);
5210 Py_INCREF(k[i+1]);
5211 i += 2;
5212 }
5213 nk = i / 2;
5214 }
5215 else {
5216 kwtuple = NULL;
5217 k = NULL;
5218 nk = 0;
5219 }
5220
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005221 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5222 closure = PyFunction_GET_CLOSURE(func);
5223 name = ((PyFunctionObject *)func) -> func_name;
5224 qualname = ((PyFunctionObject *)func) -> func_qualname;
5225
5226 if (argdefs != NULL) {
5227 d = &PyTuple_GET_ITEM(argdefs, 0);
5228 nd = Py_SIZE(argdefs);
5229 }
5230 else {
5231 d = NULL;
5232 nd = 0;
5233 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005234
5235 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5236 args, nargs,
Victor Stinner74319ae2016-08-25 00:04:09 +02005237 k, nk,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005238 NULL, NULL,
Victor Stinnerb9009392016-08-22 23:15:44 +02005239 d, nd, kwdefs,
5240 closure, name, qualname);
5241 Py_XDECREF(kwtuple);
5242 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005243}
5244
5245static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005246create_keyword_args(PyObject *names, PyObject ***pp_stack,
Ka-Ping Yee20579702001-01-15 22:14:16 +00005247 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00005248{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005249 Py_ssize_t nk = PyTuple_GET_SIZE(names);
5250 PyObject *kwdict = _PyDict_NewPresized(nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005251 if (kwdict == NULL)
5252 return NULL;
5253 while (--nk >= 0) {
5254 int err;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005255 PyObject *key = PyTuple_GET_ITEM(names, nk);
Victor Stinnerd8735722016-09-09 12:36:44 -07005256 PyObject *value = EXT_POP(*pp_stack);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 if (PyDict_GetItem(kwdict, key) != NULL) {
5258 PyErr_Format(PyExc_TypeError,
5259 "%.200s%s got multiple values "
5260 "for keyword argument '%U'",
5261 PyEval_GetFuncName(func),
5262 PyEval_GetFuncDesc(func),
5263 key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 Py_DECREF(value);
5265 Py_DECREF(kwdict);
5266 return NULL;
5267 }
5268 err = PyDict_SetItem(kwdict, key, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005269 Py_DECREF(value);
5270 if (err) {
5271 Py_DECREF(kwdict);
5272 return NULL;
5273 }
5274 }
5275 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00005276}
5277
5278static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005279load_args(PyObject ***pp_stack, Py_ssize_t nargs)
Jeremy Hylton52820442001-01-03 23:52:36 +00005280{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005281 PyObject *args = PyTuple_New(nargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00005282
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005283 if (args == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005284 return NULL;
5285 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005286
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005287 while (--nargs >= 0) {
5288 PyObject *arg= EXT_POP(*pp_stack);
5289 PyTuple_SET_ITEM(args, nargs, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005290 }
5291 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00005292}
5293
5294static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005295do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005296{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005297#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 /* At this point, we have to look at the type of func to
5299 update the call stats properly. Do it here so as to avoid
5300 exposing the call stats machinery outside ceval.c
5301 */
5302 if (PyFunction_Check(func))
5303 PCALL(PCALL_FUNCTION);
5304 else if (PyMethod_Check(func))
5305 PCALL(PCALL_METHOD);
5306 else if (PyType_Check(func))
5307 PCALL(PCALL_TYPE);
5308 else if (PyCFunction_Check(func))
5309 PCALL(PCALL_CFUNCTION);
5310 else
5311 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005312#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005315 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005316 PyThreadState *tstate = PyThreadState_GET();
5317 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005318 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005319 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005320 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005321 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005322 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005323}
5324
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005325/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005326 nb_index slot defined, and store in *pi.
5327 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5328 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 +00005329 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005330*/
Tim Petersb5196382001-12-16 19:44:20 +00005331/* Note: If v is NULL, return success without storing into *pi. This
5332 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5333 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005334*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005335int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005336_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005338 if (v != NULL) {
5339 Py_ssize_t x;
5340 if (PyIndex_Check(v)) {
5341 x = PyNumber_AsSsize_t(v, NULL);
5342 if (x == -1 && PyErr_Occurred())
5343 return 0;
5344 }
5345 else {
5346 PyErr_SetString(PyExc_TypeError,
5347 "slice indices must be integers or "
5348 "None or have an __index__ method");
5349 return 0;
5350 }
5351 *pi = x;
5352 }
5353 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005354}
5355
Guido van Rossum486364b2007-06-30 05:01:58 +00005356#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005357 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005358
Guido van Rossumb209a111997-04-29 18:18:01 +00005359static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005360cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005361{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 int res = 0;
5363 switch (op) {
5364 case PyCmp_IS:
5365 res = (v == w);
5366 break;
5367 case PyCmp_IS_NOT:
5368 res = (v != w);
5369 break;
5370 case PyCmp_IN:
5371 res = PySequence_Contains(w, v);
5372 if (res < 0)
5373 return NULL;
5374 break;
5375 case PyCmp_NOT_IN:
5376 res = PySequence_Contains(w, v);
5377 if (res < 0)
5378 return NULL;
5379 res = !res;
5380 break;
5381 case PyCmp_EXC_MATCH:
5382 if (PyTuple_Check(w)) {
5383 Py_ssize_t i, length;
5384 length = PyTuple_Size(w);
5385 for (i = 0; i < length; i += 1) {
5386 PyObject *exc = PyTuple_GET_ITEM(w, i);
5387 if (!PyExceptionClass_Check(exc)) {
5388 PyErr_SetString(PyExc_TypeError,
5389 CANNOT_CATCH_MSG);
5390 return NULL;
5391 }
5392 }
5393 }
5394 else {
5395 if (!PyExceptionClass_Check(w)) {
5396 PyErr_SetString(PyExc_TypeError,
5397 CANNOT_CATCH_MSG);
5398 return NULL;
5399 }
5400 }
5401 res = PyErr_GivenExceptionMatches(v, w);
5402 break;
5403 default:
5404 return PyObject_RichCompare(v, w, op);
5405 }
5406 v = res ? Py_True : Py_False;
5407 Py_INCREF(v);
5408 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005409}
5410
Thomas Wouters52152252000-08-17 22:55:00 +00005411static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005412import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5413{
5414 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005415 PyObject *import_func, *res;
5416 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005417
5418 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5419 if (import_func == NULL) {
5420 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5421 return NULL;
5422 }
5423
5424 /* Fast path for not overloaded __import__. */
5425 if (import_func == PyThreadState_GET()->interp->import_func) {
5426 int ilevel = _PyLong_AsInt(level);
5427 if (ilevel == -1 && PyErr_Occurred()) {
5428 return NULL;
5429 }
5430 res = PyImport_ImportModuleLevelObject(
5431 name,
5432 f->f_globals,
5433 f->f_locals == NULL ? Py_None : f->f_locals,
5434 fromlist,
5435 ilevel);
5436 return res;
5437 }
5438
5439 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005440
5441 stack[0] = name;
5442 stack[1] = f->f_globals;
5443 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5444 stack[3] = fromlist;
5445 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005446 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005447 Py_DECREF(import_func);
5448 return res;
5449}
5450
5451static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005452import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005453{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005454 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005455 _Py_IDENTIFIER(__name__);
5456 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005458 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005459 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5460 return x;
5461 /* Issue #17636: in case this failed because of a circular relative
5462 import, try to fallback on reading the module directly from
5463 sys.modules. */
5464 PyErr_Clear();
5465 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005466 if (pkgname == NULL) {
5467 goto error;
5468 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005469 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5470 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005471 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005472 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005473 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005474 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005475 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005476 if (x == NULL) {
5477 goto error;
5478 }
5479 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005480 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005481 error:
5482 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5483 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005484}
Guido van Rossumac7be682001-01-17 15:42:30 +00005485
Thomas Wouters52152252000-08-17 22:55:00 +00005486static int
5487import_all_from(PyObject *locals, PyObject *v)
5488{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005489 _Py_IDENTIFIER(__all__);
5490 _Py_IDENTIFIER(__dict__);
5491 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005492 PyObject *dict, *name, *value;
5493 int skip_leading_underscores = 0;
5494 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005496 if (all == NULL) {
5497 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5498 return -1; /* Unexpected error */
5499 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005500 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005501 if (dict == NULL) {
5502 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5503 return -1;
5504 PyErr_SetString(PyExc_ImportError,
5505 "from-import-* object has no __dict__ and no __all__");
5506 return -1;
5507 }
5508 all = PyMapping_Keys(dict);
5509 Py_DECREF(dict);
5510 if (all == NULL)
5511 return -1;
5512 skip_leading_underscores = 1;
5513 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005515 for (pos = 0, err = 0; ; pos++) {
5516 name = PySequence_GetItem(all, pos);
5517 if (name == NULL) {
5518 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5519 err = -1;
5520 else
5521 PyErr_Clear();
5522 break;
5523 }
5524 if (skip_leading_underscores &&
5525 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005526 PyUnicode_READY(name) != -1 &&
5527 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005528 {
5529 Py_DECREF(name);
5530 continue;
5531 }
5532 value = PyObject_GetAttr(v, name);
5533 if (value == NULL)
5534 err = -1;
5535 else if (PyDict_CheckExact(locals))
5536 err = PyDict_SetItem(locals, name, value);
5537 else
5538 err = PyObject_SetItem(locals, name, value);
5539 Py_DECREF(name);
5540 Py_XDECREF(value);
5541 if (err != 0)
5542 break;
5543 }
5544 Py_DECREF(all);
5545 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005546}
5547
Guido van Rossumac7be682001-01-17 15:42:30 +00005548static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005549format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005551 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 if (!obj)
5554 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005556 obj_str = _PyUnicode_AsString(obj);
5557 if (!obj_str)
5558 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005560 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005561}
Guido van Rossum950361c1997-01-24 13:49:28 +00005562
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005563static void
5564format_exc_unbound(PyCodeObject *co, int oparg)
5565{
5566 PyObject *name;
5567 /* Don't stomp existing exception */
5568 if (PyErr_Occurred())
5569 return;
5570 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5571 name = PyTuple_GET_ITEM(co->co_cellvars,
5572 oparg);
5573 format_exc_check_arg(
5574 PyExc_UnboundLocalError,
5575 UNBOUNDLOCAL_ERROR_MSG,
5576 name);
5577 } else {
5578 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5579 PyTuple_GET_SIZE(co->co_cellvars));
5580 format_exc_check_arg(PyExc_NameError,
5581 UNBOUNDFREE_ERROR_MSG, name);
5582 }
5583}
5584
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005585static PyObject *
5586unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005587 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005588{
5589 PyObject *res;
5590 if (Py_REFCNT(v) == 2) {
5591 /* In the common case, there are 2 references to the value
5592 * stored in 'variable' when the += is performed: one on the
5593 * value stack (in 'v') and one still stored in the
5594 * 'variable'. We try to delete the variable now to reduce
5595 * the refcnt to 1.
5596 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005597 int opcode, oparg;
5598 NEXTOPARG();
5599 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005600 case STORE_FAST:
5601 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005602 PyObject **fastlocals = f->f_localsplus;
5603 if (GETLOCAL(oparg) == v)
5604 SETLOCAL(oparg, NULL);
5605 break;
5606 }
5607 case STORE_DEREF:
5608 {
5609 PyObject **freevars = (f->f_localsplus +
5610 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005611 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005612 if (PyCell_GET(c) == v)
5613 PyCell_Set(c, NULL);
5614 break;
5615 }
5616 case STORE_NAME:
5617 {
5618 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005619 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005620 PyObject *locals = f->f_locals;
5621 if (PyDict_CheckExact(locals) &&
5622 PyDict_GetItem(locals, name) == v) {
5623 if (PyDict_DelItem(locals, name) != 0) {
5624 PyErr_Clear();
5625 }
5626 }
5627 break;
5628 }
5629 }
5630 }
5631 res = v;
5632 PyUnicode_Append(&res, w);
5633 return res;
5634}
5635
Guido van Rossum950361c1997-01-24 13:49:28 +00005636#ifdef DYNAMIC_EXECUTION_PROFILE
5637
Skip Montanarof118cb12001-10-15 20:51:38 +00005638static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005639getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005640{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005641 int i;
5642 PyObject *l = PyList_New(256);
5643 if (l == NULL) return NULL;
5644 for (i = 0; i < 256; i++) {
5645 PyObject *x = PyLong_FromLong(a[i]);
5646 if (x == NULL) {
5647 Py_DECREF(l);
5648 return NULL;
5649 }
5650 PyList_SetItem(l, i, x);
5651 }
5652 for (i = 0; i < 256; i++)
5653 a[i] = 0;
5654 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005655}
5656
5657PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005658_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005659{
5660#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005661 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005662#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005663 int i;
5664 PyObject *l = PyList_New(257);
5665 if (l == NULL) return NULL;
5666 for (i = 0; i < 257; i++) {
5667 PyObject *x = getarray(dxpairs[i]);
5668 if (x == NULL) {
5669 Py_DECREF(l);
5670 return NULL;
5671 }
5672 PyList_SetItem(l, i, x);
5673 }
5674 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005675#endif
5676}
5677
5678#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005679
5680Py_ssize_t
5681_PyEval_RequestCodeExtraIndex(freefunc free)
5682{
5683 PyThreadState *tstate = PyThreadState_Get();
5684 Py_ssize_t new_index;
5685
5686 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5687 return -1;
5688 }
5689 new_index = tstate->co_extra_user_count++;
5690 tstate->co_extra_freefuncs[new_index] = free;
5691 return new_index;
5692}