blob: 75ec7b2abed5aec1dcc54c1b7352ac2f816e1dff [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
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000115#endif
Victor Stinnere90bdb12016-08-25 23:26:50 +0200116static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, Py_ssize_t);
Victor Stinner74319ae2016-08-25 00:04:09 +0200117static PyObject * do_call(PyObject *, PyObject ***, Py_ssize_t, Py_ssize_t);
118static PyObject * ext_do_call(PyObject *, PyObject ***, int, Py_ssize_t, Py_ssize_t);
119static PyObject * update_keyword_args(PyObject *, Py_ssize_t, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject *);
Victor Stinner74319ae2016-08-25 00:04:09 +0200121static PyObject * update_star_args(Py_ssize_t, Py_ssize_t, PyObject *, PyObject ***);
122static PyObject * load_args(PyObject ***, Py_ssize_t);
Jeremy Hylton52820442001-01-03 23:52:36 +0000123#define CALL_FLAG_VAR 1
124#define CALL_FLAG_KW 2
125
Guido van Rossum0a066c01992-03-27 17:29:15 +0000126#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000127static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200128static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000129#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100130static int call_trace(Py_tracefunc, PyObject *,
131 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000133static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100134 PyThreadState *, PyFrameObject *,
135 int, PyObject *);
136static void call_exc_trace(Py_tracefunc, PyObject *,
137 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000138static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100139 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000140
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300142static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000144static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000145static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000146static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200147static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300148 PyFrameObject *, const unsigned short *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500149static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000150
Paul Prescode68140d2000-08-30 20:25:01 +0000151#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000153#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000155#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 "free variable '%.200s' referenced before assignment" \
157 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000158
Guido van Rossum950361c1997-01-24 13:49:28 +0000159/* Dynamic execution profile */
160#ifdef DYNAMIC_EXECUTION_PROFILE
161#ifdef DXPAIRS
162static long dxpairs[257][256];
163#define dxp dxpairs[256]
164#else
165static long dxp[256];
166#endif
167#endif
168
Jeremy Hylton985eba52003-02-05 23:13:00 +0000169/* Function call profile */
170#ifdef CALL_PROFILE
171#define PCALL_NUM 11
172static int pcall[PCALL_NUM];
173
174#define PCALL_ALL 0
175#define PCALL_FUNCTION 1
176#define PCALL_FAST_FUNCTION 2
177#define PCALL_FASTER_FUNCTION 3
178#define PCALL_METHOD 4
179#define PCALL_BOUND_METHOD 5
180#define PCALL_CFUNCTION 6
181#define PCALL_TYPE 7
182#define PCALL_GENERATOR 8
183#define PCALL_OTHER 9
184#define PCALL_POP 10
185
186/* Notes about the statistics
187
188 PCALL_FAST stats
189
190 FAST_FUNCTION means no argument tuple needs to be created.
191 FASTER_FUNCTION means that the fast-path frame setup code is used.
192
193 If there is a method call where the call can be optimized by changing
194 the argument tuple and calling the function directly, it gets recorded
195 twice.
196
197 As a result, the relationship among the statistics appears to be
198 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
199 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
200 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
201 PCALL_METHOD > PCALL_BOUND_METHOD
202*/
203
204#define PCALL(POS) pcall[POS]++
205
206PyObject *
207PyEval_GetCallStats(PyObject *self)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return Py_BuildValue("iiiiiiiiiii",
210 pcall[0], pcall[1], pcall[2], pcall[3],
211 pcall[4], pcall[5], pcall[6], pcall[7],
212 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000213}
214#else
215#define PCALL(O)
216
217PyObject *
218PyEval_GetCallStats(PyObject *self)
219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 Py_INCREF(Py_None);
221 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000222}
223#endif
224
Tim Peters5ca576e2001-06-18 22:08:13 +0000225
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000226#ifdef WITH_THREAD
227#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
228#else
229#define GIL_REQUEST 0
230#endif
231
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000232/* This can set eval_breaker to 0 even though gil_drop_request became
233 1. We believe this is all right because the eval loop will release
234 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 _Py_atomic_store_relaxed( \
237 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000238 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
240 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000242#ifdef WITH_THREAD
243
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000244#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
247 _Py_atomic_store_relaxed(&eval_breaker, 1); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
250#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 do { \
252 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
253 COMPUTE_EVAL_BREAKER(); \
254 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000255
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000256#endif
257
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000258/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
262 _Py_atomic_store_relaxed(&eval_breaker, 1); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
268 COMPUTE_EVAL_BREAKER(); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { \
273 pending_async_exc = 1; \
274 _Py_atomic_store_relaxed(&eval_breaker, 1); \
275 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000276
277#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000279
280
Guido van Rossume59214e1994-08-30 08:01:59 +0000281#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000282
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000284#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000285#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000286#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000287
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000288static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000289static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000290/* This single variable consolidates all requests to break out of the fast path
291 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000292static _Py_atomic_int eval_breaker = {0};
293/* Request for dropping the GIL */
294static _Py_atomic_int gil_drop_request = {0};
295/* Request for running pending calls. */
296static _Py_atomic_int pendingcalls_to_do = {0};
297/* Request for looking at the `async_exc` field of the current thread state.
298 Guarded by the GIL. */
299static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000300
301#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302
Tim Peters7f468f22004-10-11 02:40:51 +0000303int
304PyEval_ThreadsInitialized(void)
305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000307}
308
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000310PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (gil_created())
313 return;
314 create_gil();
315 take_gil(PyThreadState_GET());
316 main_thread = PyThread_get_thread_ident();
317 if (!pending_lock)
318 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000320
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000321void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000322_PyEval_FiniThreads(void)
323{
324 if (!gil_created())
325 return;
326 destroy_gil();
327 assert(!gil_created());
328}
329
330void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000331PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 PyThreadState *tstate = PyThreadState_GET();
334 if (tstate == NULL)
335 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
336 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000337}
338
339void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 /* This function must succeed when the current thread state is NULL.
343 We therefore avoid PyThreadState_GET() which dumps a fatal error
344 in debug mode.
345 */
346 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
347 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000348}
349
350void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (tstate == NULL)
354 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
355 /* Check someone has called PyEval_InitThreads() to create the lock */
356 assert(gil_created());
357 take_gil(tstate);
358 if (PyThreadState_Swap(tstate) != NULL)
359 Py_FatalError(
360 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000361}
362
363void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000364PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (tstate == NULL)
367 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
368 if (PyThreadState_Swap(NULL) != tstate)
369 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
370 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000371}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000372
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200373/* This function is called from PyOS_AfterFork to destroy all threads which are
374 * not running in the child process, and clear internal locks which might be
375 * held by those threads. (This could also be done using pthread_atfork
376 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000377
378void
379PyEval_ReInitThreads(void)
380{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200381 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200383 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (!gil_created())
386 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 recreate_gil();
388 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200389 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 /* Update the threading module with the new state.
393 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200394 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 "threading");
396 if (threading == NULL) {
397 /* threading not imported */
398 PyErr_Clear();
399 return;
400 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200401 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (result == NULL)
403 PyErr_WriteUnraisable(threading);
404 else
405 Py_DECREF(result);
406 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200407
408 /* Destroy all threads except the current one */
409 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000410}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000411
412#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000413static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000414static int pending_async_exc = 0;
415#endif /* WITH_THREAD */
416
417/* This function is used to signal that async exceptions are waiting to be
418 raised, therefore it is also useful in non-threaded builds. */
419
420void
421_PyEval_SignalAsyncExc(void)
422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000424}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000425
Guido van Rossumff4949e1992-08-05 19:58:53 +0000426/* Functions save_thread and restore_thread are always defined so
427 dynamically loaded modules needn't be compiled separately for use
428 with and without threads: */
429
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000430PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000431PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyThreadState *tstate = PyThreadState_Swap(NULL);
434 if (tstate == NULL)
435 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000436#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (gil_created())
438 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000439#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000441}
442
443void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000444PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (tstate == NULL)
447 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000448#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (gil_created()) {
450 int err = errno;
451 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200452 /* _Py_Finalizing is protected by the GIL */
453 if (_Py_Finalizing && tstate != _Py_Finalizing) {
454 drop_gil(tstate);
455 PyThread_exit_thread();
456 assert(0); /* unreachable */
457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 errno = err;
459 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000460#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000462}
463
464
Guido van Rossuma9672091994-09-14 13:31:22 +0000465/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
466 signal handlers or Mac I/O completion routines) can schedule calls
467 to a function to be called synchronously.
468 The synchronous function is called with one void* argument.
469 It should return 0 for success or -1 for failure -- failure should
470 be accompanied by an exception.
471
472 If registry succeeds, the registry function returns 0; if it fails
473 (e.g. due to too many pending calls) it returns -1 (without setting
474 an exception condition).
475
476 Note that because registry may occur from within signal handlers,
477 or other asynchronous events, calling malloc() is unsafe!
478
479#ifdef WITH_THREAD
480 Any thread can schedule pending calls, but only the main thread
481 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000482 There is no facility to schedule calls to a particular thread, but
483 that should be easy to change, should that ever be required. In
484 that case, the static variables here should go into the python
485 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000486#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000488
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000489#ifdef WITH_THREAD
490
491/* The WITH_THREAD implementation is thread-safe. It allows
492 scheduling to be made from any thread, and even from an executing
493 callback.
494 */
495
496#define NPENDINGCALLS 32
497static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 int (*func)(void *);
499 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000500} pendingcalls[NPENDINGCALLS];
501static int pendingfirst = 0;
502static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000503
504int
505Py_AddPendingCall(int (*func)(void *), void *arg)
506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 int i, j, result=0;
508 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* try a few times for the lock. Since this mechanism is used
511 * for signal handling (on the main thread), there is a (slim)
512 * chance that a signal is delivered on the same thread while we
513 * hold the lock during the Py_MakePendingCalls() function.
514 * This avoids a deadlock in that case.
515 * Note that signals can be delivered on any thread. In particular,
516 * on Windows, a SIGINT is delivered on a system-created worker
517 * thread.
518 * We also check for lock being NULL, in the unlikely case that
519 * this function is called before any bytecode evaluation takes place.
520 */
521 if (lock != NULL) {
522 for (i = 0; i<100; i++) {
523 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
524 break;
525 }
526 if (i == 100)
527 return -1;
528 }
529
530 i = pendinglast;
531 j = (i + 1) % NPENDINGCALLS;
532 if (j == pendingfirst) {
533 result = -1; /* Queue full */
534 } else {
535 pendingcalls[i].func = func;
536 pendingcalls[i].arg = arg;
537 pendinglast = j;
538 }
539 /* signal main loop */
540 SIGNAL_PENDING_CALLS();
541 if (lock != NULL)
542 PyThread_release_lock(lock);
543 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544}
545
546int
547Py_MakePendingCalls(void)
548{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200549 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 int i;
551 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (!pending_lock) {
554 /* initial allocation of the lock */
555 pending_lock = PyThread_allocate_lock();
556 if (pending_lock == NULL)
557 return -1;
558 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* only service pending calls on main thread */
561 if (main_thread && PyThread_get_thread_ident() != main_thread)
562 return 0;
563 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200564 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200566 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 /* perform a bounded number of calls, in case of recursion */
568 for (i=0; i<NPENDINGCALLS; i++) {
569 int j;
570 int (*func)(void *);
571 void *arg = NULL;
572
573 /* pop one item off the queue while holding the lock */
574 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
575 j = pendingfirst;
576 if (j == pendinglast) {
577 func = NULL; /* Queue empty */
578 } else {
579 func = pendingcalls[j].func;
580 arg = pendingcalls[j].arg;
581 pendingfirst = (j + 1) % NPENDINGCALLS;
582 }
583 if (pendingfirst != pendinglast)
584 SIGNAL_PENDING_CALLS();
585 else
586 UNSIGNAL_PENDING_CALLS();
587 PyThread_release_lock(pending_lock);
588 /* having released the lock, perform the callback */
589 if (func == NULL)
590 break;
591 r = func(arg);
592 if (r)
593 break;
594 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200595 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000597}
598
599#else /* if ! defined WITH_THREAD */
600
601/*
602 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
603 This code is used for signal handling in python that isn't built
604 with WITH_THREAD.
605 Don't use this implementation when Py_AddPendingCalls() can happen
606 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607
Guido van Rossuma9672091994-09-14 13:31:22 +0000608 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 (1) nested asynchronous calls to Py_AddPendingCall()
610 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000612 (1) is very unlikely because typically signal delivery
613 is blocked during signal handling. So it should be impossible.
614 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000615 The current code is safe against (2), but not against (1).
616 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000617 thread is present, interrupted by signals, and that the critical
618 section is protected with the "busy" variable. On Windows, which
619 delivers SIGINT on a system thread, this does not hold and therefore
620 Windows really shouldn't use this version.
621 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000622*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000623
Guido van Rossuma9672091994-09-14 13:31:22 +0000624#define NPENDINGCALLS 32
625static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 int (*func)(void *);
627 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000628} pendingcalls[NPENDINGCALLS];
629static volatile int pendingfirst = 0;
630static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000631static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000632
633int
Thomas Wouters334fb892000-07-25 12:56:38 +0000634Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 static volatile int busy = 0;
637 int i, j;
638 /* XXX Begin critical section */
639 if (busy)
640 return -1;
641 busy = 1;
642 i = pendinglast;
643 j = (i + 1) % NPENDINGCALLS;
644 if (j == pendingfirst) {
645 busy = 0;
646 return -1; /* Queue full */
647 }
648 pendingcalls[i].func = func;
649 pendingcalls[i].arg = arg;
650 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 SIGNAL_PENDING_CALLS();
653 busy = 0;
654 /* XXX End critical section */
655 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000656}
657
Guido van Rossum180d7b41994-09-29 09:45:57 +0000658int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000659Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 static int busy = 0;
662 if (busy)
663 return 0;
664 busy = 1;
665 UNSIGNAL_PENDING_CALLS();
666 for (;;) {
667 int i;
668 int (*func)(void *);
669 void *arg;
670 i = pendingfirst;
671 if (i == pendinglast)
672 break; /* Queue empty */
673 func = pendingcalls[i].func;
674 arg = pendingcalls[i].arg;
675 pendingfirst = (i + 1) % NPENDINGCALLS;
676 if (func(arg) < 0) {
677 busy = 0;
678 SIGNAL_PENDING_CALLS(); /* We're not done yet */
679 return -1;
680 }
681 }
682 busy = 0;
683 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000684}
685
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000686#endif /* WITH_THREAD */
687
Guido van Rossuma9672091994-09-14 13:31:22 +0000688
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000689/* The interpreter's recursion limit */
690
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000691#ifndef Py_DEFAULT_RECURSION_LIMIT
692#define Py_DEFAULT_RECURSION_LIMIT 1000
693#endif
694static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
695int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000697int
698Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000701}
702
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000703void
704Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 recursion_limit = new_limit;
707 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000708}
709
Armin Rigo2b3eb402003-10-28 12:05:48 +0000710/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
711 if the recursion_depth reaches _Py_CheckRecursionLimit.
712 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
713 to guarantee that _Py_CheckRecursiveCall() is regularly called.
714 Without USE_STACKCHECK, there is no need for this. */
715int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300716_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000719
720#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 if (PyOS_CheckStack()) {
722 --tstate->recursion_depth;
723 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
724 return -1;
725 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000726#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 _Py_CheckRecursionLimit = recursion_limit;
728 if (tstate->recursion_critical)
729 /* Somebody asked that we don't check for recursion. */
730 return 0;
731 if (tstate->overflowed) {
732 if (tstate->recursion_depth > recursion_limit + 50) {
733 /* Overflowing while handling an overflow. Give up. */
734 Py_FatalError("Cannot recover from stack overflow.");
735 }
736 return 0;
737 }
738 if (tstate->recursion_depth > recursion_limit) {
739 --tstate->recursion_depth;
740 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400741 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 "maximum recursion depth exceeded%s",
743 where);
744 return -1;
745 }
746 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000747}
748
Guido van Rossum374a9221991-04-04 10:40:29 +0000749/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000750enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000751 WHY_NOT = 0x0001, /* No error */
752 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000753 WHY_RETURN = 0x0008, /* 'return' statement */
754 WHY_BREAK = 0x0010, /* 'break' statement */
755 WHY_CONTINUE = 0x0020, /* 'continue' statement */
756 WHY_YIELD = 0x0040, /* 'yield' operator */
757 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000758};
Guido van Rossum374a9221991-04-04 10:40:29 +0000759
Benjamin Peterson87880242011-07-03 16:48:31 -0500760static void save_exc_state(PyThreadState *, PyFrameObject *);
761static void swap_exc_state(PyThreadState *, PyFrameObject *);
762static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400763static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000764static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000765
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000766/* Records whether tracing is on for any thread. Counts the number of
767 threads for which tstate->c_tracefunc is non-NULL, so if the value
768 is 0, we know we don't have to check this thread's c_tracefunc.
769 This speeds up the if statement in PyEval_EvalFrameEx() after
770 fast_next_opcode*/
771static int _Py_TracingPossible = 0;
772
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000773
Guido van Rossum374a9221991-04-04 10:40:29 +0000774
Guido van Rossumb209a111997-04-29 18:18:01 +0000775PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000776PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 return PyEval_EvalCodeEx(co,
779 globals, locals,
780 (PyObject **)NULL, 0,
781 (PyObject **)NULL, 0,
782 (PyObject **)NULL, 0,
783 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000784}
785
786
787/* Interpreter main loop */
788
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000789PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000790PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 /* This is for backward compatibility with extension modules that
792 used this API; core interpreter code should call
793 PyEval_EvalFrameEx() */
794 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000795}
796
797PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000799{
Brett Cannon3cebf932016-09-05 15:33:46 -0700800 PyThreadState *tstate = PyThreadState_GET();
801 return tstate->interp->eval_frame(f, throwflag);
802}
803
804PyObject *
805_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
806{
Guido van Rossum950361c1997-01-24 13:49:28 +0000807#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000809#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200810 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300811 const unsigned short *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200812 int opcode; /* Current opcode */
813 int oparg; /* Current opcode argument, if any */
814 enum why_code why; /* Reason for block stack unwind */
815 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 PyObject *retval = NULL; /* Return value */
817 PyThreadState *tstate = PyThreadState_GET();
818 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 is true when the line being executed has changed. The
825 initial values are such as to make this false the first
826 time it is tested. */
827 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000828
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300829 const unsigned short *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 PyObject *names;
831 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000832
Brett Cannon368b4b72012-04-02 12:17:59 -0400833#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200834 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400835#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200836
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837/* Computed GOTOs, or
838 the-optimization-commonly-but-improperly-known-as-"threaded code"
839 using gcc's labels-as-values extension
840 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
841
842 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844 combined with a lookup table of jump addresses. However, since the
845 indirect jump instruction is shared by all opcodes, the CPU will have a
846 hard time making the right prediction for where to jump next (actually,
847 it will be always wrong except in the uncommon case of a sequence of
848 several identical opcodes).
849
850 "Threaded code" in contrast, uses an explicit jump table and an explicit
851 indirect jump instruction at the end of each opcode. Since the jump
852 instruction is at a different address for each opcode, the CPU will make a
853 separate prediction for each of these instructions, which is equivalent to
854 predicting the second opcode of each opcode pair. These predictions have
855 a much better chance to turn out valid, especially in small bytecode loops.
856
857 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000859 and potentially many more instructions (depending on the pipeline width).
860 A correctly predicted branch, however, is nearly free.
861
862 At the time of this writing, the "threaded code" version is up to 15-20%
863 faster than the normal "switch" version, depending on the compiler and the
864 CPU architecture.
865
866 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
867 because it would render the measurements invalid.
868
869
870 NOTE: care must be taken that the compiler doesn't try to "optimize" the
871 indirect jumps by sharing them between all opcodes. Such optimizations
872 can be disabled on gcc by using the -fno-gcse flag (or possibly
873 -fno-crossjumping).
874*/
875
Antoine Pitrou042b1282010-08-13 21:15:58 +0000876#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000877#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000878#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879#endif
880
Antoine Pitrou042b1282010-08-13 21:15:58 +0000881#ifdef HAVE_COMPUTED_GOTOS
882 #ifndef USE_COMPUTED_GOTOS
883 #define USE_COMPUTED_GOTOS 1
884 #endif
885#else
886 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
887 #error "Computed gotos are not supported on this compiler."
888 #endif
889 #undef USE_COMPUTED_GOTOS
890 #define USE_COMPUTED_GOTOS 0
891#endif
892
893#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894/* Import the static jump table */
895#include "opcode_targets.h"
896
Antoine Pitroub52ec782009-01-25 16:34:23 +0000897#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 { \
903 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
904 FAST_DISPATCH(); \
905 } \
906 continue; \
907 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000908
909#ifdef LLTRACE
910#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 { \
912 if (!lltrace && !_Py_TracingPossible) { \
913 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300914 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300915 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 } \
917 goto fast_next_opcode; \
918 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919#else
920#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 { \
922 if (!_Py_TracingPossible) { \
923 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300924 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300925 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 } \
927 goto fast_next_opcode; \
928 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000929#endif
930
931#else
932#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300934
Antoine Pitroub52ec782009-01-25 16:34:23 +0000935#define DISPATCH() continue
936#define FAST_DISPATCH() goto fast_next_opcode
937#endif
938
939
Neal Norwitza81d2202002-07-14 00:27:26 +0000940/* Tuple access macros */
941
942#ifndef Py_DEBUG
943#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
944#else
945#define GETITEM(v, i) PyTuple_GetItem((v), (i))
946#endif
947
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000948#ifdef WITH_TSC
949/* Use Pentium timestamp counter to mark certain events:
950 inst0 -- beginning of switch statement for opcode dispatch
951 inst1 -- end of switch statement (may be skipped)
952 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000954 (may be skipped)
955 intr1 -- beginning of long interruption
956 intr2 -- end of long interruption
957
958 Many opcodes call out to helper C functions. In some cases, the
959 time in those functions should be counted towards the time for the
960 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
961 calls another Python function; there's no point in charge all the
962 bytecode executed by the called function to the caller.
963
964 It's hard to make a useful judgement statically. In the presence
965 of operator overloading, it's impossible to tell if a call will
966 execute new Python code or not.
967
968 It's a case-by-case judgement. I'll use intr1 for the following
969 cases:
970
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000971 IMPORT_STAR
972 IMPORT_FROM
973 CALL_FUNCTION (and friends)
974
975 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
977 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 READ_TIMESTAMP(inst0);
980 READ_TIMESTAMP(inst1);
981 READ_TIMESTAMP(loop0);
982 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* shut up the compiler */
985 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000986#endif
987
Guido van Rossum374a9221991-04-04 10:40:29 +0000988/* Code access macros */
989
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300990#ifdef WORDS_BIGENDIAN
991 #define OPCODE(word) ((word) >> 8)
992 #define OPARG(word) ((word) & 255)
993#else
994 #define OPCODE(word) ((word) & 255)
995 #define OPARG(word) ((word) >> 8)
996#endif
997/* The integer overflow is checked by an assertion below. */
998#define INSTR_OFFSET() (2*(int)(next_instr - first_instr))
999#define NEXTOPARG() do { \
1000 unsigned short word = *next_instr; \
1001 opcode = OPCODE(word); \
1002 oparg = OPARG(word); \
1003 next_instr++; \
1004 } while (0)
1005#define JUMPTO(x) (next_instr = first_instr + (x)/2)
1006#define JUMPBY(x) (next_instr += (x)/2)
Guido van Rossum374a9221991-04-04 10:40:29 +00001007
Raymond Hettingerf606f872003-03-16 03:11:04 +00001008/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 Some opcodes tend to come in pairs thus making it possible to
1010 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001011 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 Verifying the prediction costs a single high-speed test of a register
1014 variable against a constant. If the pairing was good, then the
1015 processor's own internal branch predication has a high likelihood of
1016 success, resulting in a nearly zero-overhead transition to the
1017 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001018 including its unpredictable switch-case branch. Combined with the
1019 processor's internal branch prediction, a successful PREDICT has the
1020 effect of making the two opcodes run as if they were a single new opcode
1021 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001022
Georg Brandl86b2fb92008-07-16 03:43:04 +00001023 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 predictions turned-on and interpret the results as if some opcodes
1025 had been combined or turn-off predictions so that the opcode frequency
1026 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001027
1028 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 the CPU to record separate branch prediction information for each
1030 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001031
Raymond Hettingerf606f872003-03-16 03:11:04 +00001032*/
1033
Antoine Pitrou042b1282010-08-13 21:15:58 +00001034#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +00001036#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001037#define PREDICT(op) \
1038 do{ \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001039 unsigned short word = *next_instr; \
1040 opcode = OPCODE(word); \
1041 if (opcode == op){ \
1042 oparg = OPARG(word); \
1043 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001044 goto PRED_##op; \
1045 } \
1046 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001047#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001048#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +00001049
Raymond Hettingerf606f872003-03-16 03:11:04 +00001050
Guido van Rossum374a9221991-04-04 10:40:29 +00001051/* Stack manipulation macros */
1052
Martin v. Löwis18e16552006-02-15 17:27:45 +00001053/* The stack can grow at most MAXINT deep, as co_nlocals and
1054 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001055#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1056#define EMPTY() (STACK_LEVEL() == 0)
1057#define TOP() (stack_pointer[-1])
1058#define SECOND() (stack_pointer[-2])
1059#define THIRD() (stack_pointer[-3])
1060#define FOURTH() (stack_pointer[-4])
1061#define PEEK(n) (stack_pointer[-(n)])
1062#define SET_TOP(v) (stack_pointer[-1] = (v))
1063#define SET_SECOND(v) (stack_pointer[-2] = (v))
1064#define SET_THIRD(v) (stack_pointer[-3] = (v))
1065#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1066#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1067#define BASIC_STACKADJ(n) (stack_pointer += n)
1068#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1069#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001070
Guido van Rossum96a42c81992-01-12 02:29:51 +00001071#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001073 lltrace && prtrace(TOP(), "push")); \
1074 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001076 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001078 lltrace && prtrace(TOP(), "stackadj")); \
1079 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001080#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001081 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1082 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001083#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001084#define PUSH(v) BASIC_PUSH(v)
1085#define POP() BASIC_POP()
1086#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001087#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001088#endif
1089
Guido van Rossum681d79a1995-07-18 14:51:37 +00001090/* Local variable macros */
1091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001093
1094/* The SETLOCAL() macro must not DECREF the local variable in-place and
1095 then store the new value; it must copy the old value to a temporary
1096 value, then store the new value, and then DECREF the temporary value.
1097 This is because it is possible that during the DECREF the frame is
1098 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1099 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001101 GETLOCAL(i) = value; \
1102 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001103
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001104
1105#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 while (STACK_LEVEL() > (b)->b_level) { \
1107 PyObject *v = POP(); \
1108 Py_XDECREF(v); \
1109 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001110
1111#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001112 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 PyObject *type, *value, *traceback; \
1114 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1115 while (STACK_LEVEL() > (b)->b_level + 3) { \
1116 value = POP(); \
1117 Py_XDECREF(value); \
1118 } \
1119 type = tstate->exc_type; \
1120 value = tstate->exc_value; \
1121 traceback = tstate->exc_traceback; \
1122 tstate->exc_type = POP(); \
1123 tstate->exc_value = POP(); \
1124 tstate->exc_traceback = POP(); \
1125 Py_XDECREF(type); \
1126 Py_XDECREF(value); \
1127 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001128 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001129
Guido van Rossuma027efa1997-05-05 20:56:21 +00001130/* Start of code */
1131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 /* push frame */
1133 if (Py_EnterRecursiveCall(""))
1134 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001135
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (tstate->use_tracing) {
1139 if (tstate->c_tracefunc != NULL) {
1140 /* tstate->c_tracefunc, if defined, is a
1141 function that will be called on *every* entry
1142 to a code block. Its return value, if not
1143 None, is a function that will be called at
1144 the start of each executed line of code.
1145 (Actually, the function must return itself
1146 in order to continue tracing.) The trace
1147 functions are called with three arguments:
1148 a pointer to the current frame, a string
1149 indicating why the function is called, and
1150 an argument which depends on the situation.
1151 The global trace function is also called
1152 whenever an exception is detected. */
1153 if (call_trace_protected(tstate->c_tracefunc,
1154 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001155 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 /* Trace function raised an error */
1157 goto exit_eval_frame;
1158 }
1159 }
1160 if (tstate->c_profilefunc != NULL) {
1161 /* Similar for c_profilefunc, except it needn't
1162 return itself and isn't called for "line" events */
1163 if (call_trace_protected(tstate->c_profilefunc,
1164 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001165 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* Profile function raised an error */
1167 goto exit_eval_frame;
1168 }
1169 }
1170 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 co = f->f_code;
1173 names = co->co_names;
1174 consts = co->co_consts;
1175 fastlocals = f->f_localsplus;
1176 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001177 assert(PyBytes_Check(co->co_code));
1178 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
1179 assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0);
Serhiy Storchaka74f2fe62016-05-25 20:35:44 +03001180 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2));
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001181 first_instr = (unsigned short*) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001182 /*
1183 f->f_lasti refers to the index of the last instruction,
1184 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001185
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001186 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001187 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 When the PREDICT() macros are enabled, some opcode pairs follow in
1190 direct succession without updating f->f_lasti. A successful
1191 prediction effectively links the two codes together as if they
1192 were a single new opcode; accordingly,f->f_lasti will point to
1193 the first code in the pair (for instance, GET_ITER followed by
1194 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001195 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001197 next_instr = first_instr;
1198 if (f->f_lasti >= 0) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001199 assert(f->f_lasti % 2 == 0);
1200 next_instr += f->f_lasti/2 + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 stack_pointer = f->f_stacktop;
1203 assert(stack_pointer != NULL);
1204 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001205 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001206
Yury Selivanoveb636452016-09-08 22:01:51 -07001207 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001208 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 /* We were in an except handler when we left,
1210 restore the exception state which was put aside
1211 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001212 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001214 else
1215 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001217
Tim Peters5ca576e2001-06-18 22:08:13 +00001218#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001219 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001220#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001223
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001224 if (throwflag) /* support for generator.throw() */
1225 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001226
Victor Stinnerace47d72013-07-18 01:41:08 +02001227#ifdef Py_DEBUG
1228 /* PyEval_EvalFrameEx() must not be called with an exception set,
1229 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001230 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001231 assert(!PyErr_Occurred());
1232#endif
1233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001235#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 if (inst1 == 0) {
1237 /* Almost surely, the opcode executed a break
1238 or a continue, preventing inst1 from being set
1239 on the way out of the loop.
1240 */
1241 READ_TIMESTAMP(inst1);
1242 loop1 = inst1;
1243 }
1244 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1245 intr0, intr1);
1246 ticked = 0;
1247 inst1 = 0;
1248 intr0 = 0;
1249 intr1 = 0;
1250 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001251#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1253 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001254 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 /* Do periodic things. Doing this every time through
1257 the loop would add too much overhead, so we do it
1258 only every Nth instruction. We also do it if
1259 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1260 event needs attention (e.g. a signal handler or
1261 async I/O handler); see Py_AddPendingCall() and
1262 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001265 if (OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001267 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 goto fast_next_opcode;
1269 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001270#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001272#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001274 if (Py_MakePendingCalls() < 0)
1275 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001277#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001278 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 /* Give another thread a chance */
1280 if (PyThreadState_Swap(NULL) != tstate)
1281 Py_FatalError("ceval: tstate mix-up");
1282 drop_gil(tstate);
1283
1284 /* Other threads may run now */
1285
1286 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001287
1288 /* Check if we should make a quick exit. */
1289 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1290 drop_gil(tstate);
1291 PyThread_exit_thread();
1292 }
1293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (PyThreadState_Swap(tstate) != NULL)
1295 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001297#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 /* Check for asynchronous exceptions. */
1299 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001300 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 tstate->async_exc = NULL;
1302 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001303 PyErr_SetNone(exc);
1304 Py_DECREF(exc);
1305 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 }
1307 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 fast_next_opcode:
1310 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001315 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 /* see maybe_call_line_trace
1318 for expository comments */
1319 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 err = maybe_call_line_trace(tstate->c_tracefunc,
1322 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001323 tstate, f,
1324 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* Reload possibly changed frame fields */
1326 JUMPTO(f->f_lasti);
1327 if (f->f_stacktop != NULL) {
1328 stack_pointer = f->f_stacktop;
1329 f->f_stacktop = NULL;
1330 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001331 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001333 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001337
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001338 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001339 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001340#ifdef DYNAMIC_EXECUTION_PROFILE
1341#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 dxpairs[lastopcode][opcode]++;
1343 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001344#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001346#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001347
Guido van Rossum96a42c81992-01-12 02:29:51 +00001348#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001349 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 if (lltrace) {
1352 if (HAS_ARG(opcode)) {
1353 printf("%d: %d, %d\n",
1354 f->f_lasti, opcode, oparg);
1355 }
1356 else {
1357 printf("%d: %d\n",
1358 f->f_lasti, opcode);
1359 }
1360 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001361#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 /* Main switch on opcode */
1364 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 /* BEWARE!
1369 It is essential that any operation that fails sets either
1370 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1371 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 TARGET(NOP)
1374 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001375
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 TARGET(LOAD_FAST) {
1377 PyObject *value = GETLOCAL(oparg);
1378 if (value == NULL) {
1379 format_exc_check_arg(PyExc_UnboundLocalError,
1380 UNBOUNDLOCAL_ERROR_MSG,
1381 PyTuple_GetItem(co->co_varnames, oparg));
1382 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 Py_INCREF(value);
1385 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 }
1388
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001389 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 TARGET(LOAD_CONST) {
1391 PyObject *value = GETITEM(consts, oparg);
1392 Py_INCREF(value);
1393 PUSH(value);
1394 FAST_DISPATCH();
1395 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001396
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001397 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 TARGET(STORE_FAST) {
1399 PyObject *value = POP();
1400 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 TARGET(POP_TOP) {
1405 PyObject *value = POP();
1406 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 TARGET(ROT_TWO) {
1411 PyObject *top = TOP();
1412 PyObject *second = SECOND();
1413 SET_TOP(second);
1414 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001416 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 TARGET(ROT_THREE) {
1419 PyObject *top = TOP();
1420 PyObject *second = SECOND();
1421 PyObject *third = THIRD();
1422 SET_TOP(second);
1423 SET_SECOND(third);
1424 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001426 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001428 TARGET(DUP_TOP) {
1429 PyObject *top = TOP();
1430 Py_INCREF(top);
1431 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 TARGET(DUP_TOP_TWO) {
1436 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001438 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001440 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001441 SET_TOP(top);
1442 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001443 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001445
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 TARGET(UNARY_POSITIVE) {
1447 PyObject *value = TOP();
1448 PyObject *res = PyNumber_Positive(value);
1449 Py_DECREF(value);
1450 SET_TOP(res);
1451 if (res == NULL)
1452 goto error;
1453 DISPATCH();
1454 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 TARGET(UNARY_NEGATIVE) {
1457 PyObject *value = TOP();
1458 PyObject *res = PyNumber_Negative(value);
1459 Py_DECREF(value);
1460 SET_TOP(res);
1461 if (res == NULL)
1462 goto error;
1463 DISPATCH();
1464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001466 TARGET(UNARY_NOT) {
1467 PyObject *value = TOP();
1468 int err = PyObject_IsTrue(value);
1469 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 if (err == 0) {
1471 Py_INCREF(Py_True);
1472 SET_TOP(Py_True);
1473 DISPATCH();
1474 }
1475 else if (err > 0) {
1476 Py_INCREF(Py_False);
1477 SET_TOP(Py_False);
1478 err = 0;
1479 DISPATCH();
1480 }
1481 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001482 goto error;
1483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 TARGET(UNARY_INVERT) {
1486 PyObject *value = TOP();
1487 PyObject *res = PyNumber_Invert(value);
1488 Py_DECREF(value);
1489 SET_TOP(res);
1490 if (res == NULL)
1491 goto error;
1492 DISPATCH();
1493 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001494
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 TARGET(BINARY_POWER) {
1496 PyObject *exp = POP();
1497 PyObject *base = TOP();
1498 PyObject *res = PyNumber_Power(base, exp, Py_None);
1499 Py_DECREF(base);
1500 Py_DECREF(exp);
1501 SET_TOP(res);
1502 if (res == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 TARGET(BINARY_MULTIPLY) {
1508 PyObject *right = POP();
1509 PyObject *left = TOP();
1510 PyObject *res = PyNumber_Multiply(left, right);
1511 Py_DECREF(left);
1512 Py_DECREF(right);
1513 SET_TOP(res);
1514 if (res == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Benjamin Petersond51374e2014-04-09 23:55:56 -04001519 TARGET(BINARY_MATRIX_MULTIPLY) {
1520 PyObject *right = POP();
1521 PyObject *left = TOP();
1522 PyObject *res = PyNumber_MatrixMultiply(left, right);
1523 Py_DECREF(left);
1524 Py_DECREF(right);
1525 SET_TOP(res);
1526 if (res == NULL)
1527 goto error;
1528 DISPATCH();
1529 }
1530
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 TARGET(BINARY_TRUE_DIVIDE) {
1532 PyObject *divisor = POP();
1533 PyObject *dividend = TOP();
1534 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1535 Py_DECREF(dividend);
1536 Py_DECREF(divisor);
1537 SET_TOP(quotient);
1538 if (quotient == NULL)
1539 goto error;
1540 DISPATCH();
1541 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001542
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001543 TARGET(BINARY_FLOOR_DIVIDE) {
1544 PyObject *divisor = POP();
1545 PyObject *dividend = TOP();
1546 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1547 Py_DECREF(dividend);
1548 Py_DECREF(divisor);
1549 SET_TOP(quotient);
1550 if (quotient == NULL)
1551 goto error;
1552 DISPATCH();
1553 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 TARGET(BINARY_MODULO) {
1556 PyObject *divisor = POP();
1557 PyObject *dividend = TOP();
1558 PyObject *res = PyUnicode_CheckExact(dividend) ?
1559 PyUnicode_Format(dividend, divisor) :
1560 PyNumber_Remainder(dividend, divisor);
1561 Py_DECREF(divisor);
1562 Py_DECREF(dividend);
1563 SET_TOP(res);
1564 if (res == NULL)
1565 goto error;
1566 DISPATCH();
1567 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001568
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 TARGET(BINARY_ADD) {
1570 PyObject *right = POP();
1571 PyObject *left = TOP();
1572 PyObject *sum;
1573 if (PyUnicode_CheckExact(left) &&
1574 PyUnicode_CheckExact(right)) {
1575 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001576 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001577 }
1578 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 sum = PyNumber_Add(left, right);
1580 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001581 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001582 Py_DECREF(right);
1583 SET_TOP(sum);
1584 if (sum == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
1588
1589 TARGET(BINARY_SUBTRACT) {
1590 PyObject *right = POP();
1591 PyObject *left = TOP();
1592 PyObject *diff = PyNumber_Subtract(left, right);
1593 Py_DECREF(right);
1594 Py_DECREF(left);
1595 SET_TOP(diff);
1596 if (diff == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
1600
1601 TARGET(BINARY_SUBSCR) {
1602 PyObject *sub = POP();
1603 PyObject *container = TOP();
1604 PyObject *res = PyObject_GetItem(container, sub);
1605 Py_DECREF(container);
1606 Py_DECREF(sub);
1607 SET_TOP(res);
1608 if (res == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
1612
1613 TARGET(BINARY_LSHIFT) {
1614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_Lshift(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(res);
1620 if (res == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
1624
1625 TARGET(BINARY_RSHIFT) {
1626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_Rshift(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
1636
1637 TARGET(BINARY_AND) {
1638 PyObject *right = POP();
1639 PyObject *left = TOP();
1640 PyObject *res = PyNumber_And(left, right);
1641 Py_DECREF(left);
1642 Py_DECREF(right);
1643 SET_TOP(res);
1644 if (res == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
1648
1649 TARGET(BINARY_XOR) {
1650 PyObject *right = POP();
1651 PyObject *left = TOP();
1652 PyObject *res = PyNumber_Xor(left, right);
1653 Py_DECREF(left);
1654 Py_DECREF(right);
1655 SET_TOP(res);
1656 if (res == NULL)
1657 goto error;
1658 DISPATCH();
1659 }
1660
1661 TARGET(BINARY_OR) {
1662 PyObject *right = POP();
1663 PyObject *left = TOP();
1664 PyObject *res = PyNumber_Or(left, right);
1665 Py_DECREF(left);
1666 Py_DECREF(right);
1667 SET_TOP(res);
1668 if (res == NULL)
1669 goto error;
1670 DISPATCH();
1671 }
1672
1673 TARGET(LIST_APPEND) {
1674 PyObject *v = POP();
1675 PyObject *list = PEEK(oparg);
1676 int err;
1677 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 if (err != 0)
1680 goto error;
1681 PREDICT(JUMP_ABSOLUTE);
1682 DISPATCH();
1683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 TARGET(SET_ADD) {
1686 PyObject *v = POP();
1687 PyObject *set = stack_pointer[-oparg];
1688 int err;
1689 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 if (err != 0)
1692 goto error;
1693 PREDICT(JUMP_ABSOLUTE);
1694 DISPATCH();
1695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 TARGET(INPLACE_POWER) {
1698 PyObject *exp = POP();
1699 PyObject *base = TOP();
1700 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1701 Py_DECREF(base);
1702 Py_DECREF(exp);
1703 SET_TOP(res);
1704 if (res == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 TARGET(INPLACE_MULTIPLY) {
1710 PyObject *right = POP();
1711 PyObject *left = TOP();
1712 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1713 Py_DECREF(left);
1714 Py_DECREF(right);
1715 SET_TOP(res);
1716 if (res == NULL)
1717 goto error;
1718 DISPATCH();
1719 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001720
Benjamin Petersond51374e2014-04-09 23:55:56 -04001721 TARGET(INPLACE_MATRIX_MULTIPLY) {
1722 PyObject *right = POP();
1723 PyObject *left = TOP();
1724 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1725 Py_DECREF(left);
1726 Py_DECREF(right);
1727 SET_TOP(res);
1728 if (res == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
1732
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 TARGET(INPLACE_TRUE_DIVIDE) {
1734 PyObject *divisor = POP();
1735 PyObject *dividend = TOP();
1736 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1737 Py_DECREF(dividend);
1738 Py_DECREF(divisor);
1739 SET_TOP(quotient);
1740 if (quotient == NULL)
1741 goto error;
1742 DISPATCH();
1743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 TARGET(INPLACE_FLOOR_DIVIDE) {
1746 PyObject *divisor = POP();
1747 PyObject *dividend = TOP();
1748 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1749 Py_DECREF(dividend);
1750 Py_DECREF(divisor);
1751 SET_TOP(quotient);
1752 if (quotient == NULL)
1753 goto error;
1754 DISPATCH();
1755 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001756
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001757 TARGET(INPLACE_MODULO) {
1758 PyObject *right = POP();
1759 PyObject *left = TOP();
1760 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1761 Py_DECREF(left);
1762 Py_DECREF(right);
1763 SET_TOP(mod);
1764 if (mod == NULL)
1765 goto error;
1766 DISPATCH();
1767 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001769 TARGET(INPLACE_ADD) {
1770 PyObject *right = POP();
1771 PyObject *left = TOP();
1772 PyObject *sum;
1773 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1774 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001775 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001776 }
1777 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 sum = PyNumber_InPlaceAdd(left, right);
1779 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001780 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 Py_DECREF(right);
1782 SET_TOP(sum);
1783 if (sum == NULL)
1784 goto error;
1785 DISPATCH();
1786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 TARGET(INPLACE_SUBTRACT) {
1789 PyObject *right = POP();
1790 PyObject *left = TOP();
1791 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1792 Py_DECREF(left);
1793 Py_DECREF(right);
1794 SET_TOP(diff);
1795 if (diff == NULL)
1796 goto error;
1797 DISPATCH();
1798 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 TARGET(INPLACE_LSHIFT) {
1801 PyObject *right = POP();
1802 PyObject *left = TOP();
1803 PyObject *res = PyNumber_InPlaceLshift(left, right);
1804 Py_DECREF(left);
1805 Py_DECREF(right);
1806 SET_TOP(res);
1807 if (res == NULL)
1808 goto error;
1809 DISPATCH();
1810 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 TARGET(INPLACE_RSHIFT) {
1813 PyObject *right = POP();
1814 PyObject *left = TOP();
1815 PyObject *res = PyNumber_InPlaceRshift(left, right);
1816 Py_DECREF(left);
1817 Py_DECREF(right);
1818 SET_TOP(res);
1819 if (res == NULL)
1820 goto error;
1821 DISPATCH();
1822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001823
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 TARGET(INPLACE_AND) {
1825 PyObject *right = POP();
1826 PyObject *left = TOP();
1827 PyObject *res = PyNumber_InPlaceAnd(left, right);
1828 Py_DECREF(left);
1829 Py_DECREF(right);
1830 SET_TOP(res);
1831 if (res == NULL)
1832 goto error;
1833 DISPATCH();
1834 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001835
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001836 TARGET(INPLACE_XOR) {
1837 PyObject *right = POP();
1838 PyObject *left = TOP();
1839 PyObject *res = PyNumber_InPlaceXor(left, right);
1840 Py_DECREF(left);
1841 Py_DECREF(right);
1842 SET_TOP(res);
1843 if (res == NULL)
1844 goto error;
1845 DISPATCH();
1846 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001847
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 TARGET(INPLACE_OR) {
1849 PyObject *right = POP();
1850 PyObject *left = TOP();
1851 PyObject *res = PyNumber_InPlaceOr(left, right);
1852 Py_DECREF(left);
1853 Py_DECREF(right);
1854 SET_TOP(res);
1855 if (res == NULL)
1856 goto error;
1857 DISPATCH();
1858 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001859
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 TARGET(STORE_SUBSCR) {
1861 PyObject *sub = TOP();
1862 PyObject *container = SECOND();
1863 PyObject *v = THIRD();
1864 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001866 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 Py_DECREF(container);
1870 Py_DECREF(sub);
1871 if (err != 0)
1872 goto error;
1873 DISPATCH();
1874 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001876 TARGET(STORE_ANNOTATION) {
1877 _Py_IDENTIFIER(__annotations__);
1878 PyObject *ann_dict;
1879 PyObject *ann = POP();
1880 PyObject *name = GETITEM(names, oparg);
1881 int err;
1882 if (f->f_locals == NULL) {
1883 PyErr_Format(PyExc_SystemError,
1884 "no locals found when storing annotation");
1885 Py_DECREF(ann);
1886 goto error;
1887 }
1888 /* first try to get __annotations__ from locals... */
1889 if (PyDict_CheckExact(f->f_locals)) {
1890 ann_dict = _PyDict_GetItemId(f->f_locals,
1891 &PyId___annotations__);
1892 if (ann_dict == NULL) {
1893 PyErr_SetString(PyExc_NameError,
1894 "__annotations__ not found");
1895 Py_DECREF(ann);
1896 goto error;
1897 }
1898 Py_INCREF(ann_dict);
1899 }
1900 else {
1901 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1902 if (ann_str == NULL) {
1903 Py_DECREF(ann);
1904 goto error;
1905 }
1906 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1907 if (ann_dict == NULL) {
1908 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1909 PyErr_SetString(PyExc_NameError,
1910 "__annotations__ not found");
1911 }
1912 Py_DECREF(ann);
1913 goto error;
1914 }
1915 }
1916 /* ...if succeeded, __annotations__[name] = ann */
1917 if (PyDict_CheckExact(ann_dict)) {
1918 err = PyDict_SetItem(ann_dict, name, ann);
1919 }
1920 else {
1921 err = PyObject_SetItem(ann_dict, name, ann);
1922 }
1923 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001924 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001925 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001926 goto error;
1927 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001928 DISPATCH();
1929 }
1930
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001931 TARGET(DELETE_SUBSCR) {
1932 PyObject *sub = TOP();
1933 PyObject *container = SECOND();
1934 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001936 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 err = PyObject_DelItem(container, sub);
1938 Py_DECREF(container);
1939 Py_DECREF(sub);
1940 if (err != 0)
1941 goto error;
1942 DISPATCH();
1943 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001944
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001946 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001947 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001948 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001949 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 PyErr_SetString(PyExc_RuntimeError,
1952 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 Py_DECREF(value);
1954 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001956 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001957 Py_DECREF(value);
1958 if (res == NULL)
1959 goto error;
1960 Py_DECREF(res);
1961 DISPATCH();
1962 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001963
Thomas Wouters434d0822000-08-24 20:11:32 +00001964#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001966#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 TARGET(RAISE_VARARGS) {
1968 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 switch (oparg) {
1970 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 if (do_raise(exc, cause)) {
1976 why = WHY_EXCEPTION;
1977 goto fast_block_end;
1978 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 break;
1980 default:
1981 PyErr_SetString(PyExc_SystemError,
1982 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 break;
1984 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 goto error;
1986 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001987
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 retval = POP();
1990 why = WHY_RETURN;
1991 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001992 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001993
Yury Selivanov75445082015-05-11 22:57:16 -04001994 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001995 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001996 PyObject *iter = NULL;
1997 PyObject *awaitable = NULL;
1998 PyObject *obj = TOP();
1999 PyTypeObject *type = Py_TYPE(obj);
2000
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002001 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002002 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002003 }
Yury Selivanov75445082015-05-11 22:57:16 -04002004
2005 if (getter != NULL) {
2006 iter = (*getter)(obj);
2007 Py_DECREF(obj);
2008 if (iter == NULL) {
2009 SET_TOP(NULL);
2010 goto error;
2011 }
2012 }
2013 else {
2014 SET_TOP(NULL);
2015 PyErr_Format(
2016 PyExc_TypeError,
2017 "'async for' requires an object with "
2018 "__aiter__ method, got %.100s",
2019 type->tp_name);
2020 Py_DECREF(obj);
2021 goto error;
2022 }
2023
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002024 if (Py_TYPE(iter)->tp_as_async != NULL &&
2025 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
2026
2027 /* Starting with CPython 3.5.2 __aiter__ should return
2028 asynchronous iterators directly (not awaitables that
2029 resolve to asynchronous iterators.)
2030
2031 Therefore, we check if the object that was returned
2032 from __aiter__ has an __anext__ method. If it does,
2033 we wrap it in an awaitable that resolves to `iter`.
2034
2035 See http://bugs.python.org/issue27243 for more
2036 details.
2037 */
2038
2039 PyObject *wrapper = _PyAIterWrapper_New(iter);
2040 Py_DECREF(iter);
2041 SET_TOP(wrapper);
2042 DISPATCH();
2043 }
2044
Yury Selivanov5376ba92015-06-22 12:19:30 -04002045 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002046 if (awaitable == NULL) {
2047 SET_TOP(NULL);
2048 PyErr_Format(
2049 PyExc_TypeError,
2050 "'async for' received an invalid object "
2051 "from __aiter__: %.100s",
2052 Py_TYPE(iter)->tp_name);
2053
2054 Py_DECREF(iter);
2055 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002056 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04002057 Py_DECREF(iter);
2058
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002059 if (PyErr_WarnFormat(
2060 PyExc_PendingDeprecationWarning, 1,
2061 "'%.100s' implements legacy __aiter__ protocol; "
2062 "__aiter__ should return an asynchronous "
2063 "iterator, not awaitable",
2064 type->tp_name))
2065 {
2066 /* Warning was converted to an error. */
2067 Py_DECREF(awaitable);
2068 SET_TOP(NULL);
2069 goto error;
2070 }
2071 }
2072
Yury Selivanov75445082015-05-11 22:57:16 -04002073 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002074 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002075 DISPATCH();
2076 }
2077
2078 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002079 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002080 PyObject *next_iter = NULL;
2081 PyObject *awaitable = NULL;
2082 PyObject *aiter = TOP();
2083 PyTypeObject *type = Py_TYPE(aiter);
2084
Yury Selivanoveb636452016-09-08 22:01:51 -07002085 if (PyAsyncGen_CheckExact(aiter)) {
2086 awaitable = type->tp_as_async->am_anext(aiter);
2087 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002088 goto error;
2089 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002090 } else {
2091 if (type->tp_as_async != NULL){
2092 getter = type->tp_as_async->am_anext;
2093 }
Yury Selivanov75445082015-05-11 22:57:16 -04002094
Yury Selivanoveb636452016-09-08 22:01:51 -07002095 if (getter != NULL) {
2096 next_iter = (*getter)(aiter);
2097 if (next_iter == NULL) {
2098 goto error;
2099 }
2100 }
2101 else {
2102 PyErr_Format(
2103 PyExc_TypeError,
2104 "'async for' requires an iterator with "
2105 "__anext__ method, got %.100s",
2106 type->tp_name);
2107 goto error;
2108 }
Yury Selivanov75445082015-05-11 22:57:16 -04002109
Yury Selivanoveb636452016-09-08 22:01:51 -07002110 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2111 if (awaitable == NULL) {
2112 PyErr_Format(
2113 PyExc_TypeError,
2114 "'async for' received an invalid object "
2115 "from __anext__: %.100s",
2116 Py_TYPE(next_iter)->tp_name);
2117
2118 Py_DECREF(next_iter);
2119 goto error;
2120 } else {
2121 Py_DECREF(next_iter);
2122 }
2123 }
Yury Selivanov75445082015-05-11 22:57:16 -04002124
2125 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002126 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002127 DISPATCH();
2128 }
2129
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002130 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002131 TARGET(GET_AWAITABLE) {
2132 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002133 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002134
2135 Py_DECREF(iterable);
2136
Yury Selivanovc724bae2016-03-02 11:30:46 -05002137 if (iter != NULL && PyCoro_CheckExact(iter)) {
2138 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2139 if (yf != NULL) {
2140 /* `iter` is a coroutine object that is being
2141 awaited, `yf` is a pointer to the current awaitable
2142 being awaited on. */
2143 Py_DECREF(yf);
2144 Py_CLEAR(iter);
2145 PyErr_SetString(
2146 PyExc_RuntimeError,
2147 "coroutine is being awaited already");
2148 /* The code below jumps to `error` if `iter` is NULL. */
2149 }
2150 }
2151
Yury Selivanov75445082015-05-11 22:57:16 -04002152 SET_TOP(iter); /* Even if it's NULL */
2153
2154 if (iter == NULL) {
2155 goto error;
2156 }
2157
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002158 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002159 DISPATCH();
2160 }
2161
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 TARGET(YIELD_FROM) {
2163 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002164 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002166 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2167 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002168 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002169 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002171 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002172 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002173 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002174 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 Py_DECREF(v);
2176 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002177 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002178 if (tstate->c_tracefunc != NULL
2179 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002180 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002181 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 if (err < 0)
2183 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002184 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 SET_TOP(val);
2186 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002187 }
Martin Panter95f53c12016-07-18 08:23:26 +00002188 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002189 f->f_stacktop = stack_pointer;
2190 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002191 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002192 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002193 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002195
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002198
2199 if (co->co_flags & CO_ASYNC_GENERATOR) {
2200 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2201 Py_DECREF(retval);
2202 if (w == NULL) {
2203 retval = NULL;
2204 goto error;
2205 }
2206 retval = w;
2207 }
2208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 f->f_stacktop = stack_pointer;
2210 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002213
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 TARGET(POP_EXCEPT) {
2215 PyTryBlock *b = PyFrame_BlockPop(f);
2216 if (b->b_type != EXCEPT_HANDLER) {
2217 PyErr_SetString(PyExc_SystemError,
2218 "popped block is not an except handler");
2219 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002224
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002225 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 TARGET(POP_BLOCK) {
2227 PyTryBlock *b = PyFrame_BlockPop(f);
2228 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 TARGET(END_FINALLY) {
2234 PyObject *status = POP();
2235 if (PyLong_Check(status)) {
2236 why = (enum why_code) PyLong_AS_LONG(status);
2237 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 if (why == WHY_RETURN ||
2239 why == WHY_CONTINUE)
2240 retval = POP();
2241 if (why == WHY_SILENCED) {
2242 /* An exception was silenced by 'with', we must
2243 manually unwind the EXCEPT_HANDLER block which was
2244 created when the exception was caught, otherwise
2245 the stack will be in an inconsistent state. */
2246 PyTryBlock *b = PyFrame_BlockPop(f);
2247 assert(b->b_type == EXCEPT_HANDLER);
2248 UNWIND_EXCEPT_HANDLER(b);
2249 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 Py_DECREF(status);
2251 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 Py_DECREF(status);
2254 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 else if (PyExceptionClass_Check(status)) {
2257 PyObject *exc = POP();
2258 PyObject *tb = POP();
2259 PyErr_Restore(status, exc, tb);
2260 why = WHY_EXCEPTION;
2261 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 PyErr_SetString(PyExc_SystemError,
2265 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 Py_DECREF(status);
2267 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 Py_DECREF(status);
2270 DISPATCH();
2271 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002272
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002274 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002275
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002277 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2279 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002280 PyErr_SetString(PyExc_NameError,
2281 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002283 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002285 }
2286 else {
2287 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2288 if (build_class_str == NULL)
2289 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002290 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2291 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002292 if (PyErr_ExceptionMatches(PyExc_KeyError))
2293 PyErr_SetString(PyExc_NameError,
2294 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002299 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002302 TARGET(STORE_NAME) {
2303 PyObject *name = GETITEM(names, oparg);
2304 PyObject *v = POP();
2305 PyObject *ns = f->f_locals;
2306 int err;
2307 if (ns == NULL) {
2308 PyErr_Format(PyExc_SystemError,
2309 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002311 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002312 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 if (PyDict_CheckExact(ns))
2314 err = PyDict_SetItem(ns, name, v);
2315 else
2316 err = PyObject_SetItem(ns, name, v);
2317 Py_DECREF(v);
2318 if (err != 0)
2319 goto error;
2320 DISPATCH();
2321 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002322
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002323 TARGET(DELETE_NAME) {
2324 PyObject *name = GETITEM(names, oparg);
2325 PyObject *ns = f->f_locals;
2326 int err;
2327 if (ns == NULL) {
2328 PyErr_Format(PyExc_SystemError,
2329 "no locals when deleting %R", name);
2330 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 err = PyObject_DelItem(ns, name);
2333 if (err != 0) {
2334 format_exc_check_arg(PyExc_NameError,
2335 NAME_ERROR_MSG,
2336 name);
2337 goto error;
2338 }
2339 DISPATCH();
2340 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002341
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002342 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 TARGET(UNPACK_SEQUENCE) {
2344 PyObject *seq = POP(), *item, **items;
2345 if (PyTuple_CheckExact(seq) &&
2346 PyTuple_GET_SIZE(seq) == oparg) {
2347 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 item = items[oparg];
2350 Py_INCREF(item);
2351 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 } else if (PyList_CheckExact(seq) &&
2354 PyList_GET_SIZE(seq) == oparg) {
2355 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 item = items[oparg];
2358 Py_INCREF(item);
2359 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 stack_pointer + oparg)) {
2363 STACKADJ(oparg);
2364 } else {
2365 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 Py_DECREF(seq);
2367 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002370 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002371 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002372
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 TARGET(UNPACK_EX) {
2374 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2375 PyObject *seq = POP();
2376
2377 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2378 stack_pointer + totalargs)) {
2379 stack_pointer += totalargs;
2380 } else {
2381 Py_DECREF(seq);
2382 goto error;
2383 }
2384 Py_DECREF(seq);
2385 DISPATCH();
2386 }
2387
2388 TARGET(STORE_ATTR) {
2389 PyObject *name = GETITEM(names, oparg);
2390 PyObject *owner = TOP();
2391 PyObject *v = SECOND();
2392 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 Py_DECREF(owner);
2397 if (err != 0)
2398 goto error;
2399 DISPATCH();
2400 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002401
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 TARGET(DELETE_ATTR) {
2403 PyObject *name = GETITEM(names, oparg);
2404 PyObject *owner = POP();
2405 int err;
2406 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2407 Py_DECREF(owner);
2408 if (err != 0)
2409 goto error;
2410 DISPATCH();
2411 }
2412
2413 TARGET(STORE_GLOBAL) {
2414 PyObject *name = GETITEM(names, oparg);
2415 PyObject *v = POP();
2416 int err;
2417 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 if (err != 0)
2420 goto error;
2421 DISPATCH();
2422 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002423
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 TARGET(DELETE_GLOBAL) {
2425 PyObject *name = GETITEM(names, oparg);
2426 int err;
2427 err = PyDict_DelItem(f->f_globals, name);
2428 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002430 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002432 }
2433 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002434 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002435
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 TARGET(LOAD_NAME) {
2437 PyObject *name = GETITEM(names, oparg);
2438 PyObject *locals = f->f_locals;
2439 PyObject *v;
2440 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 "no locals when loading %R", name);
2443 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 if (PyDict_CheckExact(locals)) {
2446 v = PyDict_GetItem(locals, name);
2447 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 }
2449 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002451 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002452 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2453 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002454 PyErr_Clear();
2455 }
2456 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002457 if (v == NULL) {
2458 v = PyDict_GetItem(f->f_globals, name);
2459 Py_XINCREF(v);
2460 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002461 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 v = PyDict_GetItem(f->f_builtins, name);
2463 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002464 format_exc_check_arg(
2465 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 NAME_ERROR_MSG, name);
2467 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002468 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002470 }
2471 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 v = PyObject_GetItem(f->f_builtins, name);
2473 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002474 if (PyErr_ExceptionMatches(PyExc_KeyError))
2475 format_exc_check_arg(
2476 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 NAME_ERROR_MSG, name);
2478 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002479 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002480 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002484 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 TARGET(LOAD_GLOBAL) {
2488 PyObject *name = GETITEM(names, oparg);
2489 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002490 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002491 && PyDict_CheckExact(f->f_builtins))
2492 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002493 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002494 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 name);
2496 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002497 if (!_PyErr_OCCURRED()) {
2498 /* _PyDict_LoadGlobal() returns NULL without raising
2499 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002500 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002501 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002502 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002503 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002504 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002505 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002506 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002507 else {
2508 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002509
2510 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 v = PyObject_GetItem(f->f_globals, name);
2512 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002513 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2514 goto error;
2515 PyErr_Clear();
2516
Victor Stinnerb4efc962015-11-20 09:24:02 +01002517 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002518 v = PyObject_GetItem(f->f_builtins, name);
2519 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002520 if (PyErr_ExceptionMatches(PyExc_KeyError))
2521 format_exc_check_arg(
2522 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002523 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002524 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002525 }
2526 }
2527 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002530 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002531
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 TARGET(DELETE_FAST) {
2533 PyObject *v = GETLOCAL(oparg);
2534 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 SETLOCAL(oparg, NULL);
2536 DISPATCH();
2537 }
2538 format_exc_check_arg(
2539 PyExc_UnboundLocalError,
2540 UNBOUNDLOCAL_ERROR_MSG,
2541 PyTuple_GetItem(co->co_varnames, oparg)
2542 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002543 goto error;
2544 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002545
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 TARGET(DELETE_DEREF) {
2547 PyObject *cell = freevars[oparg];
2548 if (PyCell_GET(cell) != NULL) {
2549 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002550 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002551 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002552 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 goto error;
2554 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002555
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 TARGET(LOAD_CLOSURE) {
2557 PyObject *cell = freevars[oparg];
2558 Py_INCREF(cell);
2559 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002561 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002562
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002563 TARGET(LOAD_CLASSDEREF) {
2564 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002565 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002566 assert(locals);
2567 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2568 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2569 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2570 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2571 if (PyDict_CheckExact(locals)) {
2572 value = PyDict_GetItem(locals, name);
2573 Py_XINCREF(value);
2574 }
2575 else {
2576 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002577 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002578 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2579 goto error;
2580 PyErr_Clear();
2581 }
2582 }
2583 if (!value) {
2584 PyObject *cell = freevars[oparg];
2585 value = PyCell_GET(cell);
2586 if (value == NULL) {
2587 format_exc_unbound(co, oparg);
2588 goto error;
2589 }
2590 Py_INCREF(value);
2591 }
2592 PUSH(value);
2593 DISPATCH();
2594 }
2595
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 TARGET(LOAD_DEREF) {
2597 PyObject *cell = freevars[oparg];
2598 PyObject *value = PyCell_GET(cell);
2599 if (value == NULL) {
2600 format_exc_unbound(co, oparg);
2601 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002602 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 Py_INCREF(value);
2604 PUSH(value);
2605 DISPATCH();
2606 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002607
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 TARGET(STORE_DEREF) {
2609 PyObject *v = POP();
2610 PyObject *cell = freevars[oparg];
2611 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 DISPATCH();
2614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002615
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002616 TARGET(BUILD_STRING) {
2617 PyObject *str;
2618 PyObject *empty = PyUnicode_New(0, 0);
2619 if (empty == NULL) {
2620 goto error;
2621 }
2622 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2623 Py_DECREF(empty);
2624 if (str == NULL)
2625 goto error;
2626 while (--oparg >= 0) {
2627 PyObject *item = POP();
2628 Py_DECREF(item);
2629 }
2630 PUSH(str);
2631 DISPATCH();
2632 }
2633
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002634 TARGET(BUILD_TUPLE) {
2635 PyObject *tup = PyTuple_New(oparg);
2636 if (tup == NULL)
2637 goto error;
2638 while (--oparg >= 0) {
2639 PyObject *item = POP();
2640 PyTuple_SET_ITEM(tup, oparg, item);
2641 }
2642 PUSH(tup);
2643 DISPATCH();
2644 }
2645
2646 TARGET(BUILD_LIST) {
2647 PyObject *list = PyList_New(oparg);
2648 if (list == NULL)
2649 goto error;
2650 while (--oparg >= 0) {
2651 PyObject *item = POP();
2652 PyList_SET_ITEM(list, oparg, item);
2653 }
2654 PUSH(list);
2655 DISPATCH();
2656 }
2657
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002658 TARGET(BUILD_TUPLE_UNPACK)
2659 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002660 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002661 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002662 PyObject *sum = PyList_New(0);
2663 PyObject *return_value;
2664 if (sum == NULL)
2665 goto error;
2666
2667 for (i = oparg; i > 0; i--) {
2668 PyObject *none_val;
2669
2670 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2671 if (none_val == NULL) {
2672 Py_DECREF(sum);
2673 goto error;
2674 }
2675 Py_DECREF(none_val);
2676 }
2677
2678 if (convert_to_tuple) {
2679 return_value = PyList_AsTuple(sum);
2680 Py_DECREF(sum);
2681 if (return_value == NULL)
2682 goto error;
2683 }
2684 else {
2685 return_value = sum;
2686 }
2687
2688 while (oparg--)
2689 Py_DECREF(POP());
2690 PUSH(return_value);
2691 DISPATCH();
2692 }
2693
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 TARGET(BUILD_SET) {
2695 PyObject *set = PySet_New(NULL);
2696 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002697 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002698 if (set == NULL)
2699 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002700 for (i = oparg; i > 0; i--) {
2701 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 if (err == 0)
2703 err = PySet_Add(set, item);
2704 Py_DECREF(item);
2705 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002706 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002707 if (err != 0) {
2708 Py_DECREF(set);
2709 goto error;
2710 }
2711 PUSH(set);
2712 DISPATCH();
2713 }
2714
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002715 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002716 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002717 PyObject *sum = PySet_New(NULL);
2718 if (sum == NULL)
2719 goto error;
2720
2721 for (i = oparg; i > 0; i--) {
2722 if (_PySet_Update(sum, PEEK(i)) < 0) {
2723 Py_DECREF(sum);
2724 goto error;
2725 }
2726 }
2727
2728 while (oparg--)
2729 Py_DECREF(POP());
2730 PUSH(sum);
2731 DISPATCH();
2732 }
2733
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002735 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2737 if (map == NULL)
2738 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002739 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002740 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002741 PyObject *key = PEEK(2*i);
2742 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002744 if (err != 0) {
2745 Py_DECREF(map);
2746 goto error;
2747 }
2748 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002749
2750 while (oparg--) {
2751 Py_DECREF(POP());
2752 Py_DECREF(POP());
2753 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 PUSH(map);
2755 DISPATCH();
2756 }
2757
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002758 TARGET(SETUP_ANNOTATIONS) {
2759 _Py_IDENTIFIER(__annotations__);
2760 int err;
2761 PyObject *ann_dict;
2762 if (f->f_locals == NULL) {
2763 PyErr_Format(PyExc_SystemError,
2764 "no locals found when setting up annotations");
2765 goto error;
2766 }
2767 /* check if __annotations__ in locals()... */
2768 if (PyDict_CheckExact(f->f_locals)) {
2769 ann_dict = _PyDict_GetItemId(f->f_locals,
2770 &PyId___annotations__);
2771 if (ann_dict == NULL) {
2772 /* ...if not, create a new one */
2773 ann_dict = PyDict_New();
2774 if (ann_dict == NULL) {
2775 goto error;
2776 }
2777 err = _PyDict_SetItemId(f->f_locals,
2778 &PyId___annotations__, ann_dict);
2779 Py_DECREF(ann_dict);
2780 if (err != 0) {
2781 goto error;
2782 }
2783 }
2784 }
2785 else {
2786 /* do the same if locals() is not a dict */
2787 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2788 if (ann_str == NULL) {
2789 break;
2790 }
2791 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2792 if (ann_dict == NULL) {
2793 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2794 goto error;
2795 }
2796 PyErr_Clear();
2797 ann_dict = PyDict_New();
2798 if (ann_dict == NULL) {
2799 goto error;
2800 }
2801 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2802 Py_DECREF(ann_dict);
2803 if (err != 0) {
2804 goto error;
2805 }
2806 }
2807 else {
2808 Py_DECREF(ann_dict);
2809 }
2810 }
2811 DISPATCH();
2812 }
2813
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002814 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002815 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002816 PyObject *map;
2817 PyObject *keys = TOP();
2818 if (!PyTuple_CheckExact(keys) ||
2819 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2820 PyErr_SetString(PyExc_SystemError,
2821 "bad BUILD_CONST_KEY_MAP keys argument");
2822 goto error;
2823 }
2824 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2825 if (map == NULL) {
2826 goto error;
2827 }
2828 for (i = oparg; i > 0; i--) {
2829 int err;
2830 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2831 PyObject *value = PEEK(i + 1);
2832 err = PyDict_SetItem(map, key, value);
2833 if (err != 0) {
2834 Py_DECREF(map);
2835 goto error;
2836 }
2837 }
2838
2839 Py_DECREF(POP());
2840 while (oparg--) {
2841 Py_DECREF(POP());
2842 }
2843 PUSH(map);
2844 DISPATCH();
2845 }
2846
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002847 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2848 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002849 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
2850 int num_maps;
2851 int function_location;
Victor Stinner74319ae2016-08-25 00:04:09 +02002852 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002853 PyObject *sum = PyDict_New();
2854 if (sum == NULL)
2855 goto error;
2856 if (with_call) {
2857 num_maps = oparg & 0xff;
2858 function_location = (oparg>>8) & 0xff;
2859 }
2860 else {
2861 num_maps = oparg;
2862 }
2863
2864 for (i = num_maps; i > 0; i--) {
2865 PyObject *arg = PEEK(i);
2866 if (with_call) {
2867 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2868
2869 if (intersection == NULL) {
2870 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2871 PyObject *func = (
2872 PEEK(function_location + num_maps));
2873 PyErr_Format(PyExc_TypeError,
2874 "%.200s%.200s argument after ** "
2875 "must be a mapping, not %.200s",
2876 PyEval_GetFuncName(func),
2877 PyEval_GetFuncDesc(func),
2878 arg->ob_type->tp_name);
2879 }
2880 Py_DECREF(sum);
2881 goto error;
2882 }
2883
2884 if (PySet_GET_SIZE(intersection)) {
2885 Py_ssize_t idx = 0;
2886 PyObject *key;
2887 PyObject *func = PEEK(function_location + num_maps);
2888 Py_hash_t hash;
2889 _PySet_NextEntry(intersection, &idx, &key, &hash);
2890 if (!PyUnicode_Check(key)) {
2891 PyErr_Format(PyExc_TypeError,
2892 "%.200s%.200s keywords must be strings",
2893 PyEval_GetFuncName(func),
2894 PyEval_GetFuncDesc(func));
2895 } else {
2896 PyErr_Format(PyExc_TypeError,
2897 "%.200s%.200s got multiple "
2898 "values for keyword argument '%U'",
2899 PyEval_GetFuncName(func),
2900 PyEval_GetFuncDesc(func),
2901 key);
2902 }
2903 Py_DECREF(intersection);
2904 Py_DECREF(sum);
2905 goto error;
2906 }
2907 Py_DECREF(intersection);
2908 }
2909
2910 if (PyDict_Update(sum, arg) < 0) {
2911 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2912 PyErr_Format(PyExc_TypeError,
2913 "'%.200s' object is not a mapping",
2914 arg->ob_type->tp_name);
2915 }
2916 Py_DECREF(sum);
2917 goto error;
2918 }
2919 }
2920
2921 while (num_maps--)
2922 Py_DECREF(POP());
2923 PUSH(sum);
2924 DISPATCH();
2925 }
2926
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002927 TARGET(MAP_ADD) {
2928 PyObject *key = TOP();
2929 PyObject *value = SECOND();
2930 PyObject *map;
2931 int err;
2932 STACKADJ(-2);
2933 map = stack_pointer[-oparg]; /* dict */
2934 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002935 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 Py_DECREF(value);
2937 Py_DECREF(key);
2938 if (err != 0)
2939 goto error;
2940 PREDICT(JUMP_ABSOLUTE);
2941 DISPATCH();
2942 }
2943
2944 TARGET(LOAD_ATTR) {
2945 PyObject *name = GETITEM(names, oparg);
2946 PyObject *owner = TOP();
2947 PyObject *res = PyObject_GetAttr(owner, name);
2948 Py_DECREF(owner);
2949 SET_TOP(res);
2950 if (res == NULL)
2951 goto error;
2952 DISPATCH();
2953 }
2954
2955 TARGET(COMPARE_OP) {
2956 PyObject *right = POP();
2957 PyObject *left = TOP();
2958 PyObject *res = cmp_outcome(oparg, left, right);
2959 Py_DECREF(left);
2960 Py_DECREF(right);
2961 SET_TOP(res);
2962 if (res == NULL)
2963 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002964 PREDICT(POP_JUMP_IF_FALSE);
2965 PREDICT(POP_JUMP_IF_TRUE);
2966 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002967 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002968
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002971 PyObject *fromlist = POP();
2972 PyObject *level = TOP();
2973 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 READ_TIMESTAMP(intr0);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002975 res = import_name(f, name, fromlist, level);
2976 Py_DECREF(level);
2977 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 SET_TOP(res);
2980 if (res == NULL)
2981 goto error;
2982 DISPATCH();
2983 }
2984
2985 TARGET(IMPORT_STAR) {
2986 PyObject *from = POP(), *locals;
2987 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002988 if (PyFrame_FastToLocalsWithError(f) < 0)
2989 goto error;
2990
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002991 locals = f->f_locals;
2992 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 PyErr_SetString(PyExc_SystemError,
2994 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002995 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 }
2997 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 READ_TIMESTAMP(intr1);
3000 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 Py_DECREF(from);
3002 if (err != 0)
3003 goto error;
3004 DISPATCH();
3005 }
Guido van Rossum25831651993-05-19 14:50:45 +00003006
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 TARGET(IMPORT_FROM) {
3008 PyObject *name = GETITEM(names, oparg);
3009 PyObject *from = TOP();
3010 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 PUSH(res);
3015 if (res == NULL)
3016 goto error;
3017 DISPATCH();
3018 }
Thomas Wouters52152252000-08-17 22:55:00 +00003019
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 JUMPBY(oparg);
3022 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003024
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003025 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003026 TARGET(POP_JUMP_IF_FALSE) {
3027 PyObject *cond = POP();
3028 int err;
3029 if (cond == Py_True) {
3030 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 FAST_DISPATCH();
3032 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 if (cond == Py_False) {
3034 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 JUMPTO(oparg);
3036 FAST_DISPATCH();
3037 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 err = PyObject_IsTrue(cond);
3039 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 if (err > 0)
3041 err = 0;
3042 else if (err == 0)
3043 JUMPTO(oparg);
3044 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003048
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003049 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 TARGET(POP_JUMP_IF_TRUE) {
3051 PyObject *cond = POP();
3052 int err;
3053 if (cond == Py_False) {
3054 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 FAST_DISPATCH();
3056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 if (cond == Py_True) {
3058 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 JUMPTO(oparg);
3060 FAST_DISPATCH();
3061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 err = PyObject_IsTrue(cond);
3063 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 if (err > 0) {
3065 err = 0;
3066 JUMPTO(oparg);
3067 }
3068 else if (err == 0)
3069 ;
3070 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003071 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003074
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 TARGET(JUMP_IF_FALSE_OR_POP) {
3076 PyObject *cond = TOP();
3077 int err;
3078 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 FAST_DISPATCH();
3082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003083 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 JUMPTO(oparg);
3085 FAST_DISPATCH();
3086 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 if (err > 0) {
3089 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003090 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003091 err = 0;
3092 }
3093 else if (err == 0)
3094 JUMPTO(oparg);
3095 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003099
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 TARGET(JUMP_IF_TRUE_OR_POP) {
3101 PyObject *cond = TOP();
3102 int err;
3103 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003105 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 FAST_DISPATCH();
3107 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003108 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 JUMPTO(oparg);
3110 FAST_DISPATCH();
3111 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003112 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 if (err > 0) {
3114 err = 0;
3115 JUMPTO(oparg);
3116 }
3117 else if (err == 0) {
3118 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 }
3121 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003125
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003126 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003127 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003129#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 /* Enabling this path speeds-up all while and for-loops by bypassing
3131 the per-loop checks for signals. By default, this should be turned-off
3132 because it prevents detection of a control-break in tight loops like
3133 "while 1: pass". Compile with this option turned-on when you need
3134 the speed-up and do not need break checking inside tight loops (ones
3135 that contain only instructions ending with FAST_DISPATCH).
3136 */
3137 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003138#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003140#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003142
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003145 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003146 PyObject *iter = PyObject_GetIter(iterable);
3147 Py_DECREF(iterable);
3148 SET_TOP(iter);
3149 if (iter == NULL)
3150 goto error;
3151 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003152 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003153 DISPATCH();
3154 }
3155
3156 TARGET(GET_YIELD_FROM_ITER) {
3157 /* before: [obj]; after [getiter(obj)] */
3158 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003159 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003160 if (PyCoro_CheckExact(iterable)) {
3161 /* `iterable` is a coroutine */
3162 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3163 /* and it is used in a 'yield from' expression of a
3164 regular generator. */
3165 Py_DECREF(iterable);
3166 SET_TOP(NULL);
3167 PyErr_SetString(PyExc_TypeError,
3168 "cannot 'yield from' a coroutine object "
3169 "in a non-coroutine generator");
3170 goto error;
3171 }
3172 }
3173 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003174 /* `iterable` is not a generator. */
3175 iter = PyObject_GetIter(iterable);
3176 Py_DECREF(iterable);
3177 SET_TOP(iter);
3178 if (iter == NULL)
3179 goto error;
3180 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003181 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 DISPATCH();
3183 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003184
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003185 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 PyObject *iter = TOP();
3189 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3190 if (next != NULL) {
3191 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 PREDICT(STORE_FAST);
3193 PREDICT(UNPACK_SEQUENCE);
3194 DISPATCH();
3195 }
3196 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3198 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003199 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003200 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 PyErr_Clear();
3202 }
3203 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 STACKADJ(-1);
3205 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003207 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003210
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 why = WHY_BREAK;
3213 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003215
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003216 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 if (retval == NULL)
3219 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 why = WHY_CONTINUE;
3221 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003223
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003224 TARGET(SETUP_LOOP)
3225 TARGET(SETUP_EXCEPT)
3226 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 /* NOTE: If you add any new block-setup opcodes that
3228 are not try/except/finally handlers, you may need
3229 to update the PyGen_NeedsFinalizing() function.
3230 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3233 STACK_LEVEL());
3234 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003236
Yury Selivanov75445082015-05-11 22:57:16 -04003237 TARGET(BEFORE_ASYNC_WITH) {
3238 _Py_IDENTIFIER(__aexit__);
3239 _Py_IDENTIFIER(__aenter__);
3240
3241 PyObject *mgr = TOP();
3242 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3243 *enter;
3244 PyObject *res;
3245 if (exit == NULL)
3246 goto error;
3247 SET_TOP(exit);
3248 enter = special_lookup(mgr, &PyId___aenter__);
3249 Py_DECREF(mgr);
3250 if (enter == NULL)
3251 goto error;
3252 res = PyObject_CallFunctionObjArgs(enter, NULL);
3253 Py_DECREF(enter);
3254 if (res == NULL)
3255 goto error;
3256 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003257 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003258 DISPATCH();
3259 }
3260
3261 TARGET(SETUP_ASYNC_WITH) {
3262 PyObject *res = POP();
3263 /* Setup the finally block before pushing the result
3264 of __aenter__ on the stack. */
3265 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3266 STACK_LEVEL());
3267 PUSH(res);
3268 DISPATCH();
3269 }
3270
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003272 _Py_IDENTIFIER(__exit__);
3273 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003274 PyObject *mgr = TOP();
3275 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3276 PyObject *res;
3277 if (exit == NULL)
3278 goto error;
3279 SET_TOP(exit);
3280 enter = special_lookup(mgr, &PyId___enter__);
3281 Py_DECREF(mgr);
3282 if (enter == NULL)
3283 goto error;
3284 res = PyObject_CallFunctionObjArgs(enter, NULL);
3285 Py_DECREF(enter);
3286 if (res == NULL)
3287 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 /* Setup the finally block before pushing the result
3289 of __enter__ on the stack. */
3290 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3291 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003292
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003293 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 DISPATCH();
3295 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003296
Yury Selivanov75445082015-05-11 22:57:16 -04003297 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003298 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 how/why we entered the finally clause:
3300 - TOP = None
3301 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3302 - TOP = WHY_*; no retval below it
3303 - (TOP, SECOND, THIRD) = exc_info()
3304 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3305 Below them is EXIT, the context.__exit__ bound method.
3306 In the last case, we must call
3307 EXIT(TOP, SECOND, THIRD)
3308 otherwise we must call
3309 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003310
Benjamin Peterson8f169482013-10-29 22:25:06 -04003311 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003313 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 In addition, if the stack represents an exception,
3317 *and* the function call returns a 'true' value, we
3318 push WHY_SILENCED onto the stack. END_FINALLY will
3319 then not re-raise the exception. (But non-local
3320 gotos should still be resumed.)
3321 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 (void)POP();
3327 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330 else if (PyLong_Check(exc)) {
3331 STACKADJ(-1);
3332 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 case WHY_RETURN:
3334 case WHY_CONTINUE:
3335 /* Retval in TOP. */
3336 exit_func = SECOND();
3337 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003338 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 break;
3340 default:
3341 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 break;
3344 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 }
3347 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 val = SECOND();
3351 tb = THIRD();
3352 tp2 = FOURTH();
3353 exc2 = PEEK(5);
3354 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003356 SET_VALUE(7, tb2);
3357 SET_VALUE(6, exc2);
3358 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3360 SET_FOURTH(NULL);
3361 /* We just shifted the stack down, so we have
3362 to tell the except handler block that the
3363 values are lower than it expects. */
3364 block = &f->f_blockstack[f->f_iblock - 1];
3365 assert(block->b_type == EXCEPT_HANDLER);
3366 block->b_level--;
3367 }
3368 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003369 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003371 if (res == NULL)
3372 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003373
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003374 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003375 PUSH(exc);
3376 PUSH(res);
3377 PREDICT(WITH_CLEANUP_FINISH);
3378 DISPATCH();
3379 }
3380
3381 PREDICTED(WITH_CLEANUP_FINISH);
3382 TARGET(WITH_CLEANUP_FINISH) {
3383 PyObject *res = POP();
3384 PyObject *exc = POP();
3385 int err;
3386
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003387 if (exc != Py_None)
3388 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 else
3390 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003392 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003393 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003396 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003397 else if (err > 0) {
3398 err = 0;
3399 /* There was an exception and a True return */
3400 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3401 }
3402 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003403 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003405
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003406 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003407 TARGET(CALL_FUNCTION) {
3408 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 PCALL(PCALL_ALL);
3410 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003411#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003413#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003415#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003417 PUSH(res);
3418 if (res == NULL)
3419 goto error;
3420 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003422
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003423 TARGET(CALL_FUNCTION_VAR)
3424 TARGET(CALL_FUNCTION_KW)
3425 TARGET(CALL_FUNCTION_VAR_KW) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003426 Py_ssize_t nargs = oparg & 0xff;
3427 Py_ssize_t nkwargs = (oparg>>8) & 0xff;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 int flags = (opcode - CALL_FUNCTION) & 3;
Victor Stinner74319ae2016-08-25 00:04:09 +02003429 Py_ssize_t n;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003430 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 PCALL(PCALL_ALL);
Victor Stinner74319ae2016-08-25 00:04:09 +02003432
3433 n = nargs + 2 * nkwargs;
3434 if (flags & CALL_FLAG_VAR) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 n++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003436 }
3437 if (flags & CALL_FLAG_KW) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 n++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003439 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 pfunc = stack_pointer - n - 1;
3441 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00003444 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 PyObject *self = PyMethod_GET_SELF(func);
3446 Py_INCREF(self);
3447 func = PyMethod_GET_FUNCTION(func);
3448 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03003449 Py_SETREF(*pfunc, self);
Victor Stinner74319ae2016-08-25 00:04:09 +02003450 nargs++;
3451 }
3452 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 Py_INCREF(func);
Victor Stinner74319ae2016-08-25 00:04:09 +02003454 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 sp = stack_pointer;
3456 READ_TIMESTAMP(intr0);
Victor Stinner74319ae2016-08-25 00:04:09 +02003457 res = ext_do_call(func, &sp, flags, nargs, nkwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 READ_TIMESTAMP(intr1);
3459 stack_pointer = sp;
3460 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003463 PyObject *o = POP();
3464 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003466 PUSH(res);
3467 if (res == NULL)
3468 goto error;
3469 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003471
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003472 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003473 PyObject *qualname = POP();
3474 PyObject *codeobj = POP();
3475 PyFunctionObject *func = (PyFunctionObject *)
3476 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003477
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003478 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003479 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003480 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003481 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003483
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003484 if (oparg & 0x08) {
3485 assert(PyTuple_CheckExact(TOP()));
3486 func ->func_closure = POP();
3487 }
3488 if (oparg & 0x04) {
3489 assert(PyDict_CheckExact(TOP()));
3490 func->func_annotations = POP();
3491 }
3492 if (oparg & 0x02) {
3493 assert(PyDict_CheckExact(TOP()));
3494 func->func_kwdefaults = POP();
3495 }
3496 if (oparg & 0x01) {
3497 assert(PyTuple_CheckExact(TOP()));
3498 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003500
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003501 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003502 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003504
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003505 TARGET(BUILD_SLICE) {
3506 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003508 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003510 step = NULL;
3511 stop = POP();
3512 start = TOP();
3513 slice = PySlice_New(start, stop, step);
3514 Py_DECREF(start);
3515 Py_DECREF(stop);
3516 Py_XDECREF(step);
3517 SET_TOP(slice);
3518 if (slice == NULL)
3519 goto error;
3520 DISPATCH();
3521 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003522
Eric V. Smitha78c7952015-11-03 12:45:05 -05003523 TARGET(FORMAT_VALUE) {
3524 /* Handles f-string value formatting. */
3525 PyObject *result;
3526 PyObject *fmt_spec;
3527 PyObject *value;
3528 PyObject *(*conv_fn)(PyObject *);
3529 int which_conversion = oparg & FVC_MASK;
3530 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3531
3532 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003533 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003534
3535 /* See if any conversion is specified. */
3536 switch (which_conversion) {
3537 case FVC_STR: conv_fn = PyObject_Str; break;
3538 case FVC_REPR: conv_fn = PyObject_Repr; break;
3539 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3540
3541 /* Must be 0 (meaning no conversion), since only four
3542 values are allowed by (oparg & FVC_MASK). */
3543 default: conv_fn = NULL; break;
3544 }
3545
3546 /* If there's a conversion function, call it and replace
3547 value with that result. Otherwise, just use value,
3548 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003549 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003550 result = conv_fn(value);
3551 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003552 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003553 Py_XDECREF(fmt_spec);
3554 goto error;
3555 }
3556 value = result;
3557 }
3558
3559 /* If value is a unicode object, and there's no fmt_spec,
3560 then we know the result of format(value) is value
3561 itself. In that case, skip calling format(). I plan to
3562 move this optimization in to PyObject_Format()
3563 itself. */
3564 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3565 /* Do nothing, just transfer ownership to result. */
3566 result = value;
3567 } else {
3568 /* Actually call format(). */
3569 result = PyObject_Format(value, fmt_spec);
3570 Py_DECREF(value);
3571 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003572 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003573 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003574 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003575 }
3576
Eric V. Smith135d5f42016-02-05 18:23:08 -05003577 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003578 DISPATCH();
3579 }
3580
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003581 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003582 int oldoparg = oparg;
3583 NEXTOPARG();
3584 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003587
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003588
Antoine Pitrou042b1282010-08-13 21:15:58 +00003589#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003591#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 default:
3593 fprintf(stderr,
3594 "XXX lineno: %d, opcode: %d\n",
3595 PyFrame_GetLineNumber(f),
3596 opcode);
3597 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003598 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003599
3600#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003602#endif
3603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003605
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003606 /* This should never be reached. Every opcode should end with DISPATCH()
3607 or goto error. */
3608 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003609
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003610error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003612
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003613 assert(why == WHY_NOT);
3614 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003615
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003616 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003617#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003618 if (!PyErr_Occurred())
3619 PyErr_SetString(PyExc_SystemError,
3620 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003621#else
3622 assert(PyErr_Occurred());
3623#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003624
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003625 /* Log traceback info. */
3626 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003627
Benjamin Peterson51f46162013-01-23 08:38:47 -05003628 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003629 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3630 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003631
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003632fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003633 assert(why != WHY_NOT);
3634
3635 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 while (why != WHY_NOT && f->f_iblock > 0) {
3637 /* Peek at the current block. */
3638 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 assert(why != WHY_YIELD);
3641 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3642 why = WHY_NOT;
3643 JUMPTO(PyLong_AS_LONG(retval));
3644 Py_DECREF(retval);
3645 break;
3646 }
3647 /* Now we have to pop the block. */
3648 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 if (b->b_type == EXCEPT_HANDLER) {
3651 UNWIND_EXCEPT_HANDLER(b);
3652 continue;
3653 }
3654 UNWIND_BLOCK(b);
3655 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3656 why = WHY_NOT;
3657 JUMPTO(b->b_handler);
3658 break;
3659 }
3660 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3661 || b->b_type == SETUP_FINALLY)) {
3662 PyObject *exc, *val, *tb;
3663 int handler = b->b_handler;
3664 /* Beware, this invalidates all b->b_* fields */
3665 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3666 PUSH(tstate->exc_traceback);
3667 PUSH(tstate->exc_value);
3668 if (tstate->exc_type != NULL) {
3669 PUSH(tstate->exc_type);
3670 }
3671 else {
3672 Py_INCREF(Py_None);
3673 PUSH(Py_None);
3674 }
3675 PyErr_Fetch(&exc, &val, &tb);
3676 /* Make the raw exception data
3677 available to the handler,
3678 so a program can emulate the
3679 Python main loop. */
3680 PyErr_NormalizeException(
3681 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003682 if (tb != NULL)
3683 PyException_SetTraceback(val, tb);
3684 else
3685 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 Py_INCREF(exc);
3687 tstate->exc_type = exc;
3688 Py_INCREF(val);
3689 tstate->exc_value = val;
3690 tstate->exc_traceback = tb;
3691 if (tb == NULL)
3692 tb = Py_None;
3693 Py_INCREF(tb);
3694 PUSH(tb);
3695 PUSH(val);
3696 PUSH(exc);
3697 why = WHY_NOT;
3698 JUMPTO(handler);
3699 break;
3700 }
3701 if (b->b_type == SETUP_FINALLY) {
3702 if (why & (WHY_RETURN | WHY_CONTINUE))
3703 PUSH(retval);
3704 PUSH(PyLong_FromLong((long)why));
3705 why = WHY_NOT;
3706 JUMPTO(b->b_handler);
3707 break;
3708 }
3709 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 if (why != WHY_NOT)
3714 break;
3715 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003716
Victor Stinnerace47d72013-07-18 01:41:08 +02003717 assert(!PyErr_Occurred());
3718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 assert(why != WHY_YIELD);
3722 /* Pop remaining stack entries. */
3723 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003724 PyObject *o = POP();
3725 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 if (why != WHY_RETURN)
3729 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003730
Victor Stinner4a7cc882015-03-06 23:35:27 +01003731 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003732
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003733fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003734 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003735
Benjamin Petersonac913412011-07-03 16:25:11 -05003736 /* The purpose of this block is to put aside the generator's exception
3737 state and restore that of the calling frame. If the current
3738 exception state is from the caller, we clear the exception values
3739 on the generator frame, so they are not swapped back in latter. The
3740 origin of the current exception state is determined by checking for
3741 except handler blocks, which we must be in iff a new exception
3742 state came into existence in this frame. (An uncaught exception
3743 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3744 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003745 for (i = 0; i < f->f_iblock; i++) {
3746 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003747 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003748 }
3749 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003750 if (i == f->f_iblock)
3751 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003752 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003753 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003754 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003755 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003758 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003760 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3761 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003763 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 why = WHY_EXCEPTION;
3765 }
3766 }
3767 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003768 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3769 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 PyTrace_RETURN, NULL);
3771 }
3772 }
3773 if (tstate->c_profilefunc) {
3774 if (why == WHY_EXCEPTION)
3775 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003776 tstate->c_profileobj,
3777 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003779 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3780 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003782 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003783 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 }
3785 }
3786 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003789exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003790 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003791 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003793
Victor Stinnerefde1462015-03-21 15:04:43 +01003794 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003795}
3796
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003798format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3799{
3800 int err;
3801 Py_ssize_t len = PyList_GET_SIZE(names);
3802 PyObject *name_str, *comma, *tail, *tmp;
3803
3804 assert(PyList_CheckExact(names));
3805 assert(len >= 1);
3806 /* Deal with the joys of natural language. */
3807 switch (len) {
3808 case 1:
3809 name_str = PyList_GET_ITEM(names, 0);
3810 Py_INCREF(name_str);
3811 break;
3812 case 2:
3813 name_str = PyUnicode_FromFormat("%U and %U",
3814 PyList_GET_ITEM(names, len - 2),
3815 PyList_GET_ITEM(names, len - 1));
3816 break;
3817 default:
3818 tail = PyUnicode_FromFormat(", %U, and %U",
3819 PyList_GET_ITEM(names, len - 2),
3820 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003821 if (tail == NULL)
3822 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003823 /* Chop off the last two objects in the list. This shouldn't actually
3824 fail, but we can't be too careful. */
3825 err = PyList_SetSlice(names, len - 2, len, NULL);
3826 if (err == -1) {
3827 Py_DECREF(tail);
3828 return;
3829 }
3830 /* Stitch everything up into a nice comma-separated list. */
3831 comma = PyUnicode_FromString(", ");
3832 if (comma == NULL) {
3833 Py_DECREF(tail);
3834 return;
3835 }
3836 tmp = PyUnicode_Join(comma, names);
3837 Py_DECREF(comma);
3838 if (tmp == NULL) {
3839 Py_DECREF(tail);
3840 return;
3841 }
3842 name_str = PyUnicode_Concat(tmp, tail);
3843 Py_DECREF(tmp);
3844 Py_DECREF(tail);
3845 break;
3846 }
3847 if (name_str == NULL)
3848 return;
3849 PyErr_Format(PyExc_TypeError,
3850 "%U() missing %i required %s argument%s: %U",
3851 co->co_name,
3852 len,
3853 kind,
3854 len == 1 ? "" : "s",
3855 name_str);
3856 Py_DECREF(name_str);
3857}
3858
3859static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003860missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003861 PyObject **fastlocals)
3862{
Victor Stinner74319ae2016-08-25 00:04:09 +02003863 Py_ssize_t i, j = 0;
3864 Py_ssize_t start, end;
3865 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003866 const char *kind = positional ? "positional" : "keyword-only";
3867 PyObject *missing_names;
3868
3869 /* Compute the names of the arguments that are missing. */
3870 missing_names = PyList_New(missing);
3871 if (missing_names == NULL)
3872 return;
3873 if (positional) {
3874 start = 0;
3875 end = co->co_argcount - defcount;
3876 }
3877 else {
3878 start = co->co_argcount;
3879 end = start + co->co_kwonlyargcount;
3880 }
3881 for (i = start; i < end; i++) {
3882 if (GETLOCAL(i) == NULL) {
3883 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3884 PyObject *name = PyObject_Repr(raw);
3885 if (name == NULL) {
3886 Py_DECREF(missing_names);
3887 return;
3888 }
3889 PyList_SET_ITEM(missing_names, j++, name);
3890 }
3891 }
3892 assert(j == missing);
3893 format_missing(kind, co, missing_names);
3894 Py_DECREF(missing_names);
3895}
3896
3897static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003898too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3899 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900{
3901 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003902 Py_ssize_t kwonly_given = 0;
3903 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003905 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003906
Benjamin Petersone109c702011-06-24 09:37:26 -05003907 assert((co->co_flags & CO_VARARGS) == 0);
3908 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003909 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3910 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003911 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003912 }
3913 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003914 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003915 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003917 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918 }
3919 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003920 plural = (co_argcount != 1);
3921 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003922 }
3923 if (sig == NULL)
3924 return;
3925 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003926 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3927 kwonly_sig = PyUnicode_FromFormat(format,
3928 given != 1 ? "s" : "",
3929 kwonly_given,
3930 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003931 if (kwonly_sig == NULL) {
3932 Py_DECREF(sig);
3933 return;
3934 }
3935 }
3936 else {
3937 /* This will not fail. */
3938 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003939 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 }
3941 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003942 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003943 co->co_name,
3944 sig,
3945 plural ? "s" : "",
3946 given,
3947 kwonly_sig,
3948 given == 1 && !kwonly_given ? "was" : "were");
3949 Py_DECREF(sig);
3950 Py_DECREF(kwonly_sig);
3951}
3952
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003953
Guido van Rossumc2e20742006-02-27 22:32:47 +00003954/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003955 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003956 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003957
Victor Stinner40ee3012014-06-16 15:59:28 +02003958static PyObject *
3959_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003960 PyObject **args, Py_ssize_t argcount,
3961 PyObject **kws, Py_ssize_t kwcount,
3962 PyObject **defs, Py_ssize_t defcount,
3963 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003964 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003965{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003966 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003967 PyFrameObject *f;
3968 PyObject *retval = NULL;
3969 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003970 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003972 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3973 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003974 PyObject *kwdict;
3975
3976 assert((kwcount == 0) || (kws != NULL));
Tim Peters5ca576e2001-06-18 22:08:13 +00003977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 if (globals == NULL) {
3979 PyErr_SetString(PyExc_SystemError,
3980 "PyEval_EvalCodeEx: NULL globals");
3981 return NULL;
3982 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003983
Victor Stinnerc7020012016-08-16 23:40:29 +02003984 /* Create the frame */
3985 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003988 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003990 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 fastlocals = f->f_localsplus;
3992 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003993
Victor Stinnerc7020012016-08-16 23:40:29 +02003994 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 if (co->co_flags & CO_VARKEYWORDS) {
3996 kwdict = PyDict_New();
3997 if (kwdict == NULL)
3998 goto fail;
3999 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004000 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004001 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004002 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004003 SETLOCAL(i, kwdict);
4004 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004005 else {
4006 kwdict = NULL;
4007 }
4008
4009 /* Copy positional arguments into local variables */
4010 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004012 }
4013 else {
4014 n = argcount;
4015 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004016 for (i = 0; i < n; i++) {
4017 x = args[i];
4018 Py_INCREF(x);
4019 SETLOCAL(i, x);
4020 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004021
4022 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 if (co->co_flags & CO_VARARGS) {
4024 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004025 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004027 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004028 SETLOCAL(total_args, u);
4029 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 x = args[i];
4031 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004034 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004035
4036 /* Handle keyword arguments (passed as an array of (key, value)) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004037 for (i = 0; i < kwcount; i++) {
4038 PyObject **co_varnames;
4039 PyObject *keyword = kws[2*i];
4040 PyObject *value = kws[2*i + 1];
Victor Stinner17061a92016-08-16 23:39:42 +02004041 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004042
Benjamin Petersonb204a422011-06-05 22:04:07 -05004043 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4044 PyErr_Format(PyExc_TypeError,
4045 "%U() keywords must be strings",
4046 co->co_name);
4047 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004049
Benjamin Petersonb204a422011-06-05 22:04:07 -05004050 /* Speed hack: do raw pointer compares. As names are
4051 normally interned this should almost always hit. */
4052 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4053 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004054 PyObject *name = co_varnames[j];
4055 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004056 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004057 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004058 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004059
Benjamin Petersonb204a422011-06-05 22:04:07 -05004060 /* Slow fallback, just in case */
4061 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004062 PyObject *name = co_varnames[j];
4063 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4064 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004065 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004066 }
4067 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004069 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004070 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004071
Benjamin Petersonb204a422011-06-05 22:04:07 -05004072 if (j >= total_args && kwdict == NULL) {
4073 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004074 "%U() got an unexpected keyword argument '%S'",
4075 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004076 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004078
Christian Heimes0bd447f2013-07-20 14:48:10 +02004079 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4080 goto fail;
4081 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004082 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004083
Benjamin Petersonb204a422011-06-05 22:04:07 -05004084 kw_found:
4085 if (GETLOCAL(j) != NULL) {
4086 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004087 "%U() got multiple values for argument '%S'",
4088 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004089 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004091 Py_INCREF(value);
4092 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004094
4095 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004096 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004097 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 goto fail;
4099 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004100
4101 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004102 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004103 Py_ssize_t m = co->co_argcount - defcount;
4104 Py_ssize_t missing = 0;
4105 for (i = argcount; i < m; i++) {
4106 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004107 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004108 }
4109 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004110 if (missing) {
4111 missing_arguments(co, missing, defcount, fastlocals);
4112 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004113 }
4114 if (n > m)
4115 i = n - m;
4116 else
4117 i = 0;
4118 for (; i < defcount; i++) {
4119 if (GETLOCAL(m+i) == NULL) {
4120 PyObject *def = defs[i];
4121 Py_INCREF(def);
4122 SETLOCAL(m+i, def);
4123 }
4124 }
4125 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004126
4127 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004128 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004129 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004130 for (i = co->co_argcount; i < total_args; i++) {
4131 PyObject *name;
4132 if (GETLOCAL(i) != NULL)
4133 continue;
4134 name = PyTuple_GET_ITEM(co->co_varnames, i);
4135 if (kwdefs != NULL) {
4136 PyObject *def = PyDict_GetItem(kwdefs, name);
4137 if (def) {
4138 Py_INCREF(def);
4139 SETLOCAL(i, def);
4140 continue;
4141 }
4142 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004143 missing++;
4144 }
4145 if (missing) {
4146 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004147 goto fail;
4148 }
4149 }
4150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004152 vars into frame. */
4153 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004154 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004155 int arg;
4156 /* Possibly account for the cell variable being an argument. */
4157 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004158 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004159 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004160 /* Clear the local copy. */
4161 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004162 }
4163 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004164 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004165 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004166 if (c == NULL)
4167 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004168 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004170
4171 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004172 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4173 PyObject *o = PyTuple_GET_ITEM(closure, i);
4174 Py_INCREF(o);
4175 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004177
Yury Selivanoveb636452016-09-08 22:01:51 -07004178 /* Handle generator/coroutine/asynchronous generator */
4179 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004180 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004181 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004182 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004183
4184 if (is_coro && tstate->in_coroutine_wrapper) {
4185 assert(coro_wrapper != NULL);
4186 PyErr_Format(PyExc_RuntimeError,
4187 "coroutine wrapper %.200R attempted "
4188 "to recursively wrap %.200R",
4189 coro_wrapper,
4190 co);
4191 goto fail;
4192 }
Yury Selivanov75445082015-05-11 22:57:16 -04004193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 /* Don't need to keep the reference to f_back, it will be set
4195 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004196 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 /* Create a new generator that owns the ready to run frame
4201 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004202 if (is_coro) {
4203 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004204 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4205 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004206 } else {
4207 gen = PyGen_NewWithQualName(f, name, qualname);
4208 }
Yury Selivanov75445082015-05-11 22:57:16 -04004209 if (gen == NULL)
4210 return NULL;
4211
Yury Selivanov94c22632015-06-04 10:16:51 -04004212 if (is_coro && coro_wrapper != NULL) {
4213 PyObject *wrapped;
4214 tstate->in_coroutine_wrapper = 1;
4215 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4216 tstate->in_coroutine_wrapper = 0;
4217 return wrapped;
4218 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004219
Yury Selivanov75445082015-05-11 22:57:16 -04004220 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004224
Thomas Woutersce272b62007-09-19 21:19:28 +00004225fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 /* decref'ing the frame can cause __del__ methods to get invoked,
4228 which can call back into Python. While we're done with the
4229 current Python frame (f), the associated C stack is still in use,
4230 so recursion_depth must be boosted for the duration.
4231 */
4232 assert(tstate != NULL);
4233 ++tstate->recursion_depth;
4234 Py_DECREF(f);
4235 --tstate->recursion_depth;
4236 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004237}
4238
Victor Stinner40ee3012014-06-16 15:59:28 +02004239PyObject *
4240PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4241 PyObject **args, int argcount, PyObject **kws, int kwcount,
4242 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4243{
4244 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004245 args, argcount,
4246 kws, kwcount,
4247 defs, defcount,
4248 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004249 NULL, NULL);
4250}
Tim Peters5ca576e2001-06-18 22:08:13 +00004251
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004252static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004253special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004256 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004258 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 return NULL;
4260 }
4261 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004262}
4263
4264
Benjamin Peterson87880242011-07-03 16:48:31 -05004265/* These 3 functions deal with the exception state of generators. */
4266
4267static void
4268save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4269{
4270 PyObject *type, *value, *traceback;
4271 Py_XINCREF(tstate->exc_type);
4272 Py_XINCREF(tstate->exc_value);
4273 Py_XINCREF(tstate->exc_traceback);
4274 type = f->f_exc_type;
4275 value = f->f_exc_value;
4276 traceback = f->f_exc_traceback;
4277 f->f_exc_type = tstate->exc_type;
4278 f->f_exc_value = tstate->exc_value;
4279 f->f_exc_traceback = tstate->exc_traceback;
4280 Py_XDECREF(type);
4281 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004282 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004283}
4284
4285static void
4286swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4287{
4288 PyObject *tmp;
4289 tmp = tstate->exc_type;
4290 tstate->exc_type = f->f_exc_type;
4291 f->f_exc_type = tmp;
4292 tmp = tstate->exc_value;
4293 tstate->exc_value = f->f_exc_value;
4294 f->f_exc_value = tmp;
4295 tmp = tstate->exc_traceback;
4296 tstate->exc_traceback = f->f_exc_traceback;
4297 f->f_exc_traceback = tmp;
4298}
4299
4300static void
4301restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4302{
4303 PyObject *type, *value, *tb;
4304 type = tstate->exc_type;
4305 value = tstate->exc_value;
4306 tb = tstate->exc_traceback;
4307 tstate->exc_type = f->f_exc_type;
4308 tstate->exc_value = f->f_exc_value;
4309 tstate->exc_traceback = f->f_exc_traceback;
4310 f->f_exc_type = NULL;
4311 f->f_exc_value = NULL;
4312 f->f_exc_traceback = NULL;
4313 Py_XDECREF(type);
4314 Py_XDECREF(value);
4315 Py_XDECREF(tb);
4316}
4317
4318
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004319/* Logic for the raise statement (too complicated for inlining).
4320 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004321static int
Collin Winter828f04a2007-08-31 00:04:24 +00004322do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (exc == NULL) {
4327 /* Reraise */
4328 PyThreadState *tstate = PyThreadState_GET();
4329 PyObject *tb;
4330 type = tstate->exc_type;
4331 value = tstate->exc_value;
4332 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004333 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 PyErr_SetString(PyExc_RuntimeError,
4335 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004336 return 0;
4337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 Py_XINCREF(type);
4339 Py_XINCREF(value);
4340 Py_XINCREF(tb);
4341 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004342 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 /* We support the following forms of raise:
4346 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004347 raise <instance>
4348 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 if (PyExceptionClass_Check(exc)) {
4351 type = exc;
4352 value = PyObject_CallObject(exc, NULL);
4353 if (value == NULL)
4354 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004355 if (!PyExceptionInstance_Check(value)) {
4356 PyErr_Format(PyExc_TypeError,
4357 "calling %R should have returned an instance of "
4358 "BaseException, not %R",
4359 type, Py_TYPE(value));
4360 goto raise_error;
4361 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 }
4363 else if (PyExceptionInstance_Check(exc)) {
4364 value = exc;
4365 type = PyExceptionInstance_Class(exc);
4366 Py_INCREF(type);
4367 }
4368 else {
4369 /* Not something you can raise. You get an exception
4370 anyway, just not what you specified :-) */
4371 Py_DECREF(exc);
4372 PyErr_SetString(PyExc_TypeError,
4373 "exceptions must derive from BaseException");
4374 goto raise_error;
4375 }
Collin Winter828f04a2007-08-31 00:04:24 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 if (cause) {
4378 PyObject *fixed_cause;
4379 if (PyExceptionClass_Check(cause)) {
4380 fixed_cause = PyObject_CallObject(cause, NULL);
4381 if (fixed_cause == NULL)
4382 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004383 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004385 else if (PyExceptionInstance_Check(cause)) {
4386 fixed_cause = cause;
4387 }
4388 else if (cause == Py_None) {
4389 Py_DECREF(cause);
4390 fixed_cause = NULL;
4391 }
4392 else {
4393 PyErr_SetString(PyExc_TypeError,
4394 "exception causes must derive from "
4395 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 goto raise_error;
4397 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004398 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 }
Collin Winter828f04a2007-08-31 00:04:24 +00004400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 PyErr_SetObject(type, value);
4402 /* PyErr_SetObject incref's its arguments */
4403 Py_XDECREF(value);
4404 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004405 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004406
4407raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 Py_XDECREF(value);
4409 Py_XDECREF(type);
4410 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004411 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004412}
4413
Tim Petersd6d010b2001-06-21 02:49:55 +00004414/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004415 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004416
Guido van Rossum0368b722007-05-11 16:50:42 +00004417 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4418 with a variable target.
4419*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004420
Barry Warsawe42b18f1997-08-25 22:13:04 +00004421static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004422unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 int i = 0, j = 0;
4425 Py_ssize_t ll = 0;
4426 PyObject *it; /* iter(v) */
4427 PyObject *w;
4428 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 it = PyObject_GetIter(v);
4433 if (it == NULL)
4434 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 for (; i < argcnt; i++) {
4437 w = PyIter_Next(it);
4438 if (w == NULL) {
4439 /* Iterator done, via error or exhaustion. */
4440 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004441 if (argcntafter == -1) {
4442 PyErr_Format(PyExc_ValueError,
4443 "not enough values to unpack (expected %d, got %d)",
4444 argcnt, i);
4445 }
4446 else {
4447 PyErr_Format(PyExc_ValueError,
4448 "not enough values to unpack "
4449 "(expected at least %d, got %d)",
4450 argcnt + argcntafter, i);
4451 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 }
4453 goto Error;
4454 }
4455 *--sp = w;
4456 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004457
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004458 if (argcntafter == -1) {
4459 /* We better have exhausted the iterator now. */
4460 w = PyIter_Next(it);
4461 if (w == NULL) {
4462 if (PyErr_Occurred())
4463 goto Error;
4464 Py_DECREF(it);
4465 return 1;
4466 }
4467 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004468 PyErr_Format(PyExc_ValueError,
4469 "too many values to unpack (expected %d)",
4470 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 goto Error;
4472 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 l = PySequence_List(it);
4475 if (l == NULL)
4476 goto Error;
4477 *--sp = l;
4478 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 ll = PyList_GET_SIZE(l);
4481 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004482 PyErr_Format(PyExc_ValueError,
4483 "not enough values to unpack (expected at least %d, got %zd)",
4484 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 goto Error;
4486 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 /* Pop the "after-variable" args off the list. */
4489 for (j = argcntafter; j > 0; j--, i++) {
4490 *--sp = PyList_GET_ITEM(l, ll - j);
4491 }
4492 /* Resize the list. */
4493 Py_SIZE(l) = ll - argcntafter;
4494 Py_DECREF(it);
4495 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004496
Tim Petersd6d010b2001-06-21 02:49:55 +00004497Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 for (; i > 0; i--, sp++)
4499 Py_DECREF(*sp);
4500 Py_XDECREF(it);
4501 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004502}
4503
4504
Guido van Rossum96a42c81992-01-12 02:29:51 +00004505#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004506static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004507prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 printf("%s ", str);
4510 if (PyObject_Print(v, stdout, 0) != 0)
4511 PyErr_Clear(); /* Don't know what else to do */
4512 printf("\n");
4513 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004514}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004515#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004516
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004517static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004518call_exc_trace(Py_tracefunc func, PyObject *self,
4519 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004520{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004521 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004523 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 if (value == NULL) {
4525 value = Py_None;
4526 Py_INCREF(value);
4527 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004528 PyErr_NormalizeException(&type, &value, &orig_traceback);
4529 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004530 arg = PyTuple_Pack(3, type, value, traceback);
4531 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004532 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 return;
4534 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004535 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 Py_DECREF(arg);
4537 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004538 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 else {
4540 Py_XDECREF(type);
4541 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004542 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004544}
4545
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004546static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004547call_trace_protected(Py_tracefunc func, PyObject *obj,
4548 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 PyObject *type, *value, *traceback;
4552 int err;
4553 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004554 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 if (err == 0)
4556 {
4557 PyErr_Restore(type, value, traceback);
4558 return 0;
4559 }
4560 else {
4561 Py_XDECREF(type);
4562 Py_XDECREF(value);
4563 Py_XDECREF(traceback);
4564 return -1;
4565 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004566}
4567
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004568static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004569call_trace(Py_tracefunc func, PyObject *obj,
4570 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 int result;
4574 if (tstate->tracing)
4575 return 0;
4576 tstate->tracing++;
4577 tstate->use_tracing = 0;
4578 result = func(obj, frame, what, arg);
4579 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4580 || (tstate->c_profilefunc != NULL));
4581 tstate->tracing--;
4582 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004583}
4584
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004585PyObject *
4586_PyEval_CallTracing(PyObject *func, PyObject *args)
4587{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004588 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 int save_tracing = tstate->tracing;
4590 int save_use_tracing = tstate->use_tracing;
4591 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 tstate->tracing = 0;
4594 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4595 || (tstate->c_profilefunc != NULL));
4596 result = PyObject_Call(func, args, NULL);
4597 tstate->tracing = save_tracing;
4598 tstate->use_tracing = save_use_tracing;
4599 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004600}
4601
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004602/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004603static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004604maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004605 PyThreadState *tstate, PyFrameObject *frame,
4606 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 int result = 0;
4609 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004611 /* If the last instruction executed isn't in the current
4612 instruction window, reset the window.
4613 */
4614 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4615 PyAddrPair bounds;
4616 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4617 &bounds);
4618 *instr_lb = bounds.ap_lower;
4619 *instr_ub = bounds.ap_upper;
4620 }
4621 /* If the last instruction falls at the start of a line or if
4622 it represents a jump backwards, update the frame's line
4623 number and call the trace function. */
4624 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4625 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004626 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004627 }
4628 *instr_prev = frame->f_lasti;
4629 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004630}
4631
Fred Drake5755ce62001-06-27 19:19:46 +00004632void
4633PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 PyThreadState *tstate = PyThreadState_GET();
4636 PyObject *temp = tstate->c_profileobj;
4637 Py_XINCREF(arg);
4638 tstate->c_profilefunc = NULL;
4639 tstate->c_profileobj = NULL;
4640 /* Must make sure that tracing is not ignored if 'temp' is freed */
4641 tstate->use_tracing = tstate->c_tracefunc != NULL;
4642 Py_XDECREF(temp);
4643 tstate->c_profilefunc = func;
4644 tstate->c_profileobj = arg;
4645 /* Flag that tracing or profiling is turned on */
4646 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004647}
4648
4649void
4650PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 PyThreadState *tstate = PyThreadState_GET();
4653 PyObject *temp = tstate->c_traceobj;
4654 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4655 Py_XINCREF(arg);
4656 tstate->c_tracefunc = NULL;
4657 tstate->c_traceobj = NULL;
4658 /* Must make sure that profiling is not ignored if 'temp' is freed */
4659 tstate->use_tracing = tstate->c_profilefunc != NULL;
4660 Py_XDECREF(temp);
4661 tstate->c_tracefunc = func;
4662 tstate->c_traceobj = arg;
4663 /* Flag that tracing or profiling is turned on */
4664 tstate->use_tracing = ((func != NULL)
4665 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004666}
4667
Yury Selivanov75445082015-05-11 22:57:16 -04004668void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004669_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004670{
4671 PyThreadState *tstate = PyThreadState_GET();
4672
Yury Selivanov75445082015-05-11 22:57:16 -04004673 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004674 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004675}
4676
4677PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004678_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004679{
4680 PyThreadState *tstate = PyThreadState_GET();
4681 return tstate->coroutine_wrapper;
4682}
4683
Yury Selivanoveb636452016-09-08 22:01:51 -07004684void
4685_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4686{
4687 PyThreadState *tstate = PyThreadState_GET();
4688
4689 Py_XINCREF(firstiter);
4690 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4691}
4692
4693PyObject *
4694_PyEval_GetAsyncGenFirstiter(void)
4695{
4696 PyThreadState *tstate = PyThreadState_GET();
4697 return tstate->async_gen_firstiter;
4698}
4699
4700void
4701_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4702{
4703 PyThreadState *tstate = PyThreadState_GET();
4704
4705 Py_XINCREF(finalizer);
4706 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4707}
4708
4709PyObject *
4710_PyEval_GetAsyncGenFinalizer(void)
4711{
4712 PyThreadState *tstate = PyThreadState_GET();
4713 return tstate->async_gen_finalizer;
4714}
4715
Guido van Rossumb209a111997-04-29 18:18:01 +00004716PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004717PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 PyFrameObject *current_frame = PyEval_GetFrame();
4720 if (current_frame == NULL)
4721 return PyThreadState_GET()->interp->builtins;
4722 else
4723 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004724}
4725
Guido van Rossumb209a111997-04-29 18:18:01 +00004726PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004727PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004728{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004730 if (current_frame == NULL) {
4731 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004733 }
4734
4735 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4736 return NULL;
4737
4738 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004740}
4741
Guido van Rossumb209a111997-04-29 18:18:01 +00004742PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004743PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004744{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 PyFrameObject *current_frame = PyEval_GetFrame();
4746 if (current_frame == NULL)
4747 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004748
4749 assert(current_frame->f_globals != NULL);
4750 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004751}
4752
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004753PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004754PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 PyThreadState *tstate = PyThreadState_GET();
4757 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004758}
4759
Guido van Rossum6135a871995-01-09 17:53:26 +00004760int
Tim Peters5ba58662001-07-16 02:29:45 +00004761PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 PyFrameObject *current_frame = PyEval_GetFrame();
4764 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 if (current_frame != NULL) {
4767 const int codeflags = current_frame->f_code->co_flags;
4768 const int compilerflags = codeflags & PyCF_MASK;
4769 if (compilerflags) {
4770 result = 1;
4771 cf->cf_flags |= compilerflags;
4772 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004773#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004774 if (codeflags & CO_GENERATOR_ALLOWED) {
4775 result = 1;
4776 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4777 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004778#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004779 }
4780 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004781}
4782
Guido van Rossum3f5da241990-12-20 15:06:42 +00004783
Guido van Rossum681d79a1995-07-18 14:51:37 +00004784/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004785 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004786
Guido van Rossumb209a111997-04-29 18:18:01 +00004787PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004788PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004789{
Victor Stinner59b356d2015-03-16 11:52:32 +01004790#ifdef Py_DEBUG
4791 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4792 set. It raises a new exception if parameters are invalid or if
4793 PyTuple_New() fails, and so the original exception is lost. */
4794 assert(!PyErr_Occurred());
4795#endif
4796
Victor Stinner8a31c822016-08-19 17:12:23 +02004797 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004798 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 }
Victor Stinner155ea652016-08-22 23:26:00 +02004800
4801 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004802 PyErr_SetString(PyExc_TypeError,
4803 "argument list must be a tuple");
4804 return NULL;
4805 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004806
Victor Stinner8a31c822016-08-19 17:12:23 +02004807 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 PyErr_SetString(PyExc_TypeError,
4809 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004810 return NULL;
4811 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004812
Victor Stinner6e2333d2016-08-23 00:25:01 +02004813 return PyObject_Call(func, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004814}
4815
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004816const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004817PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004818{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 if (PyMethod_Check(func))
4820 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4821 else if (PyFunction_Check(func))
4822 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4823 else if (PyCFunction_Check(func))
4824 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4825 else
4826 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004827}
4828
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004829const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004830PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004832 if (PyMethod_Check(func))
4833 return "()";
4834 else if (PyFunction_Check(func))
4835 return "()";
4836 else if (PyCFunction_Check(func))
4837 return "()";
4838 else
4839 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004840}
4841
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004842static void
Victor Stinner74319ae2016-08-25 00:04:09 +02004843err_args(PyObject *func, int flags, Py_ssize_t nargs)
Jeremy Hylton192690e2002-08-16 18:36:11 +00004844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 if (flags & METH_NOARGS)
4846 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02004847 "%.200s() takes no arguments (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004848 ((PyCFunctionObject *)func)->m_ml->ml_name,
4849 nargs);
4850 else
4851 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02004852 "%.200s() takes exactly one argument (%zd given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 ((PyCFunctionObject *)func)->m_ml->ml_name,
4854 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004855}
4856
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004857#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004858if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004859 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4860 tstate, tstate->frame, \
4861 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004862 x = NULL; \
4863 } \
4864 else { \
4865 x = call; \
4866 if (tstate->c_profilefunc != NULL) { \
4867 if (x == NULL) { \
4868 call_trace_protected(tstate->c_profilefunc, \
4869 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004870 tstate, tstate->frame, \
4871 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 /* XXX should pass (type, value, tb) */ \
4873 } else { \
4874 if (call_trace(tstate->c_profilefunc, \
4875 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004876 tstate, tstate->frame, \
4877 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 Py_DECREF(x); \
4879 x = NULL; \
4880 } \
4881 } \
4882 } \
4883 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004884} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004885 x = call; \
4886 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004887
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004888static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004889call_function(PyObject ***pp_stack, int oparg
4890#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004892#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004894{
Victor Stinner74319ae2016-08-25 00:04:09 +02004895 Py_ssize_t nargs = oparg & 0xff;
4896 Py_ssize_t nkwargs = (oparg>>8) & 0xff;
4897 int n = nargs + 2 * nkwargs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 PyObject **pfunc = (*pp_stack) - n - 1;
4899 PyObject *func = *pfunc;
4900 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004902 /* Always dispatch PyCFunction first, because these are
4903 presumed to be the most frequent callable object.
4904 */
Victor Stinner74319ae2016-08-25 00:04:09 +02004905 if (PyCFunction_Check(func) && nkwargs == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004906 int flags = PyCFunction_GET_FLAGS(func);
4907 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 PCALL(PCALL_CFUNCTION);
4910 if (flags & (METH_NOARGS | METH_O)) {
4911 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4912 PyObject *self = PyCFunction_GET_SELF(func);
Victor Stinner74319ae2016-08-25 00:04:09 +02004913 if (flags & METH_NOARGS && nargs == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004915
Victor Stinnerefde1462015-03-21 15:04:43 +01004916 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004918 else if (flags & METH_O && nargs == 1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 PyObject *arg = EXT_POP(*pp_stack);
4920 C_TRACE(x, (*meth)(self,arg));
4921 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004922
Victor Stinnerefde1462015-03-21 15:04:43 +01004923 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004924 }
4925 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02004926 err_args(func, flags, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004927 x = NULL;
4928 }
4929 }
4930 else {
4931 PyObject *callargs;
Victor Stinner74319ae2016-08-25 00:04:09 +02004932 callargs = load_args(pp_stack, nargs);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004933 if (callargs != NULL) {
4934 READ_TIMESTAMP(*pintr0);
4935 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4936 READ_TIMESTAMP(*pintr1);
4937 Py_XDECREF(callargs);
4938 }
4939 else {
4940 x = NULL;
4941 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004943 }
4944 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4946 /* optimize access to bound methods */
4947 PyObject *self = PyMethod_GET_SELF(func);
4948 PCALL(PCALL_METHOD);
4949 PCALL(PCALL_BOUND_METHOD);
4950 Py_INCREF(self);
4951 func = PyMethod_GET_FUNCTION(func);
4952 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03004953 Py_SETREF(*pfunc, self);
Victor Stinner74319ae2016-08-25 00:04:09 +02004954 nargs++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004955 n++;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004956 }
4957 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02004958 Py_INCREF(func);
4959 }
4960 READ_TIMESTAMP(*pintr0);
4961 if (PyFunction_Check(func)) {
Victor Stinnere90bdb12016-08-25 23:26:50 +02004962 x = fast_function(func, (*pp_stack) - n, nargs, nkwargs);
Victor Stinner74319ae2016-08-25 00:04:09 +02004963 }
4964 else {
4965 x = do_call(func, pp_stack, nargs, nkwargs);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004966 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004967 READ_TIMESTAMP(*pintr1);
4968 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004969
4970 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004971 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 /* Clear the stack of the function object. Also removes
4974 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004975 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 */
4977 while ((*pp_stack) > pfunc) {
4978 w = EXT_POP(*pp_stack);
4979 Py_DECREF(w);
4980 PCALL(PCALL_POP);
4981 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004982
Victor Stinner4a7cc882015-03-06 23:35:27 +01004983 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004985}
4986
Victor Stinnere90bdb12016-08-25 23:26:50 +02004987/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004988 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004989 For the simplest case -- a function that takes only positional
4990 arguments and is called with only positional arguments -- it
4991 inlines the most primitive frame setup code from
4992 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4993 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004994*/
4995
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004996static PyObject*
Victor Stinner74319ae2016-08-25 00:04:09 +02004997_PyFunction_FastCallNoKw(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
Victor Stinner2eedc112016-08-22 12:29:42 +02004998 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004999{
5000 PyFrameObject *f;
5001 PyThreadState *tstate = PyThreadState_GET();
5002 PyObject **fastlocals;
5003 Py_ssize_t i;
5004 PyObject *result;
5005
5006 PCALL(PCALL_FASTER_FUNCTION);
5007 assert(globals != NULL);
5008 /* XXX Perhaps we should create a specialized
5009 PyFrame_New() that doesn't take locals, but does
5010 take builtins without sanity checking them.
5011 */
5012 assert(tstate != NULL);
5013 f = PyFrame_New(tstate, co, globals, NULL);
5014 if (f == NULL) {
5015 return NULL;
5016 }
5017
5018 fastlocals = f->f_localsplus;
5019
Victor Stinner74319ae2016-08-25 00:04:09 +02005020 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005021 Py_INCREF(*args);
5022 fastlocals[i] = *args++;
5023 }
5024 result = PyEval_EvalFrameEx(f,0);
5025
5026 ++tstate->recursion_depth;
5027 Py_DECREF(f);
5028 --tstate->recursion_depth;
5029
5030 return result;
5031}
5032
Victor Stinner2eedc112016-08-22 12:29:42 +02005033/* Similar to _PyFunction_FastCall() but keywords are passed a (key, value)
5034 pairs in stack */
Victor Stinnere90bdb12016-08-25 23:26:50 +02005035static PyObject *
5036fast_function(PyObject *func, PyObject **stack, Py_ssize_t nargs, Py_ssize_t nkwargs)
Jeremy Hylton52820442001-01-03 23:52:36 +00005037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5039 PyObject *globals = PyFunction_GET_GLOBALS(func);
5040 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005041 PyObject *kwdefs, *closure, *name, *qualname;
5042 PyObject **d;
5043 int nd;
Jeremy Hylton52820442001-01-03 23:52:36 +00005044
Victor Stinner577e1f82016-08-25 00:29:32 +02005045 assert(func != NULL);
5046 assert(nargs >= 0);
5047 assert(nkwargs >= 0);
5048 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
5049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 PCALL(PCALL_FUNCTION);
5051 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005052
Victor Stinner74319ae2016-08-25 00:04:09 +02005053 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005054 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5055 {
Victor Stinner2eedc112016-08-22 12:29:42 +02005056 if (argdefs == NULL && co->co_argcount == nargs) {
5057 return _PyFunction_FastCallNoKw(co, stack, nargs, globals);
5058 }
5059 else if (nargs == 0 && argdefs != NULL
5060 && co->co_argcount == Py_SIZE(argdefs)) {
5061 /* function called with no arguments, but all parameters have
5062 a default value: use default values as arguments .*/
5063 stack = &PyTuple_GET_ITEM(argdefs, 0);
5064 return _PyFunction_FastCallNoKw(co, stack, Py_SIZE(argdefs),
5065 globals);
5066 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005067 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005068
5069 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5070 closure = PyFunction_GET_CLOSURE(func);
5071 name = ((PyFunctionObject *)func) -> func_name;
5072 qualname = ((PyFunctionObject *)func) -> func_qualname;
5073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005074 if (argdefs != NULL) {
5075 d = &PyTuple_GET_ITEM(argdefs, 0);
5076 nd = Py_SIZE(argdefs);
5077 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005078 else {
5079 d = NULL;
5080 nd = 0;
5081 }
5082 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02005083 stack, nargs,
Victor Stinner74319ae2016-08-25 00:04:09 +02005084 stack + nargs, nkwargs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005085 d, nd, kwdefs,
5086 closure, name, qualname);
5087}
5088
5089PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005090_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02005091 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005092{
5093 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5094 PyObject *globals = PyFunction_GET_GLOBALS(func);
5095 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
5096 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02005097 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005098 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02005099 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02005100 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005101
Victor Stinner74319ae2016-08-25 00:04:09 +02005102 assert(func != NULL);
5103 assert(nargs >= 0);
5104 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02005105 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005106
Victor Stinner577e1f82016-08-25 00:29:32 +02005107 PCALL(PCALL_FUNCTION);
5108 PCALL(PCALL_FAST_FUNCTION);
5109
Victor Stinnerb9009392016-08-22 23:15:44 +02005110 if (co->co_kwonlyargcount == 0 &&
5111 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005112 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5113 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005114 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005115 if (argdefs == NULL && co->co_argcount == nargs) {
5116 return _PyFunction_FastCallNoKw(co, args, nargs, globals);
5117 }
5118 else if (nargs == 0 && argdefs != NULL
5119 && co->co_argcount == Py_SIZE(argdefs)) {
5120 /* function called with no arguments, but all parameters have
5121 a default value: use default values as arguments .*/
5122 args = &PyTuple_GET_ITEM(argdefs, 0);
5123 return _PyFunction_FastCallNoKw(co, args, Py_SIZE(argdefs),
5124 globals);
5125 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005126 }
5127
Victor Stinnerb9009392016-08-22 23:15:44 +02005128 if (kwargs != NULL) {
5129 Py_ssize_t pos, i;
5130 nk = PyDict_Size(kwargs);
5131
5132 kwtuple = PyTuple_New(2 * nk);
5133 if (kwtuple == NULL) {
5134 return NULL;
5135 }
5136
5137 k = &PyTuple_GET_ITEM(kwtuple, 0);
5138 pos = i = 0;
5139 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5140 Py_INCREF(k[i]);
5141 Py_INCREF(k[i+1]);
5142 i += 2;
5143 }
5144 nk = i / 2;
5145 }
5146 else {
5147 kwtuple = NULL;
5148 k = NULL;
5149 nk = 0;
5150 }
5151
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005152 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5153 closure = PyFunction_GET_CLOSURE(func);
5154 name = ((PyFunctionObject *)func) -> func_name;
5155 qualname = ((PyFunctionObject *)func) -> func_qualname;
5156
5157 if (argdefs != NULL) {
5158 d = &PyTuple_GET_ITEM(argdefs, 0);
5159 nd = Py_SIZE(argdefs);
5160 }
5161 else {
5162 d = NULL;
5163 nd = 0;
5164 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005165
5166 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5167 args, nargs,
Victor Stinner74319ae2016-08-25 00:04:09 +02005168 k, nk,
Victor Stinnerb9009392016-08-22 23:15:44 +02005169 d, nd, kwdefs,
5170 closure, name, qualname);
5171 Py_XDECREF(kwtuple);
5172 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005173}
5174
5175static PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005176update_keyword_args(PyObject *orig_kwdict, Py_ssize_t nk, PyObject ***pp_stack,
Ka-Ping Yee20579702001-01-15 22:14:16 +00005177 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00005178{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 PyObject *kwdict = NULL;
5180 if (orig_kwdict == NULL)
5181 kwdict = PyDict_New();
5182 else {
5183 kwdict = PyDict_Copy(orig_kwdict);
5184 Py_DECREF(orig_kwdict);
5185 }
5186 if (kwdict == NULL)
5187 return NULL;
5188 while (--nk >= 0) {
5189 int err;
5190 PyObject *value = EXT_POP(*pp_stack);
5191 PyObject *key = EXT_POP(*pp_stack);
5192 if (PyDict_GetItem(kwdict, key) != NULL) {
5193 PyErr_Format(PyExc_TypeError,
5194 "%.200s%s got multiple values "
5195 "for keyword argument '%U'",
5196 PyEval_GetFuncName(func),
5197 PyEval_GetFuncDesc(func),
5198 key);
5199 Py_DECREF(key);
5200 Py_DECREF(value);
5201 Py_DECREF(kwdict);
5202 return NULL;
5203 }
5204 err = PyDict_SetItem(kwdict, key, value);
5205 Py_DECREF(key);
5206 Py_DECREF(value);
5207 if (err) {
5208 Py_DECREF(kwdict);
5209 return NULL;
5210 }
5211 }
5212 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00005213}
5214
5215static PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005216update_star_args(Py_ssize_t nstack, Py_ssize_t nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005217 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00005218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00005220
Serhiy Storchaka79d6e8d2016-04-19 23:37:17 +03005221 if (!nstack) {
5222 if (!stararg) {
5223 /* There are no positional arguments on the stack and there is no
5224 sequence to be unpacked. */
5225 return PyTuple_New(0);
5226 }
5227 if (PyTuple_CheckExact(stararg)) {
5228 /* No arguments are passed on the stack and the sequence is not a
5229 tuple subclass so we can just pass the stararg tuple directly
5230 to the function. */
5231 Py_INCREF(stararg);
5232 return stararg;
5233 }
5234 }
5235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005236 callargs = PyTuple_New(nstack + nstar);
5237 if (callargs == NULL) {
5238 return NULL;
5239 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005241 if (nstar) {
Victor Stinner74319ae2016-08-25 00:04:09 +02005242 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005243 for (i = 0; i < nstar; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02005244 PyObject *arg = PyTuple_GET_ITEM(stararg, i);
5245 Py_INCREF(arg);
5246 PyTuple_SET_ITEM(callargs, nstack + i, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 }
5248 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005250 while (--nstack >= 0) {
5251 w = EXT_POP(*pp_stack);
5252 PyTuple_SET_ITEM(callargs, nstack, w);
5253 }
5254 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00005255}
5256
5257static PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005258load_args(PyObject ***pp_stack, Py_ssize_t na)
Jeremy Hylton52820442001-01-03 23:52:36 +00005259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 PyObject *args = PyTuple_New(na);
5261 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00005262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005263 if (args == NULL)
5264 return NULL;
5265 while (--na >= 0) {
5266 w = EXT_POP(*pp_stack);
5267 PyTuple_SET_ITEM(args, na, w);
5268 }
5269 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00005270}
5271
5272static PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005273do_call(PyObject *func, PyObject ***pp_stack, Py_ssize_t nargs, Py_ssize_t nkwargs)
Jeremy Hylton52820442001-01-03 23:52:36 +00005274{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005275 PyObject *callargs = NULL;
5276 PyObject *kwdict = NULL;
5277 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00005278
Victor Stinner74319ae2016-08-25 00:04:09 +02005279 if (nkwargs > 0) {
5280 kwdict = update_keyword_args(NULL, nkwargs, pp_stack, func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005281 if (kwdict == NULL)
5282 goto call_fail;
5283 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005284 callargs = load_args(pp_stack, nargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 if (callargs == NULL)
5286 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005287#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005288 /* At this point, we have to look at the type of func to
5289 update the call stats properly. Do it here so as to avoid
5290 exposing the call stats machinery outside ceval.c
5291 */
5292 if (PyFunction_Check(func))
5293 PCALL(PCALL_FUNCTION);
5294 else if (PyMethod_Check(func))
5295 PCALL(PCALL_METHOD);
5296 else if (PyType_Check(func))
5297 PCALL(PCALL_TYPE);
5298 else if (PyCFunction_Check(func))
5299 PCALL(PCALL_CFUNCTION);
5300 else
5301 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005302#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 if (PyCFunction_Check(func)) {
5304 PyThreadState *tstate = PyThreadState_GET();
5305 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5306 }
5307 else
5308 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00005309call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 Py_XDECREF(callargs);
5311 Py_XDECREF(kwdict);
5312 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005313}
5314
5315static PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005316ext_do_call(PyObject *func, PyObject ***pp_stack, int flags,
5317 Py_ssize_t nargs, Py_ssize_t nkwargs)
Jeremy Hylton52820442001-01-03 23:52:36 +00005318{
Victor Stinner74319ae2016-08-25 00:04:09 +02005319 Py_ssize_t nstar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005320 PyObject *callargs = NULL;
5321 PyObject *stararg = NULL;
5322 PyObject *kwdict = NULL;
5323 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00005324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 if (flags & CALL_FLAG_KW) {
5326 kwdict = EXT_POP(*pp_stack);
Serhiy Storchakace412872016-05-08 23:36:44 +03005327 if (!PyDict_CheckExact(kwdict)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005328 PyObject *d;
5329 d = PyDict_New();
5330 if (d == NULL)
5331 goto ext_call_fail;
5332 if (PyDict_Update(d, kwdict) != 0) {
5333 Py_DECREF(d);
5334 /* PyDict_Update raises attribute
5335 * error (percolated from an attempt
5336 * to get 'keys' attribute) instead of
5337 * a type error if its second argument
5338 * is not a mapping.
5339 */
5340 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5341 PyErr_Format(PyExc_TypeError,
5342 "%.200s%.200s argument after ** "
5343 "must be a mapping, not %.200s",
5344 PyEval_GetFuncName(func),
5345 PyEval_GetFuncDesc(func),
5346 kwdict->ob_type->tp_name);
5347 }
5348 goto ext_call_fail;
5349 }
5350 Py_DECREF(kwdict);
5351 kwdict = d;
5352 }
5353 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005354
5355 if (nkwargs > 0) {
5356 kwdict = update_keyword_args(kwdict, nkwargs, pp_stack, func);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005357 if (kwdict == NULL)
5358 goto ext_call_fail;
5359 }
5360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005361 if (flags & CALL_FLAG_VAR) {
5362 stararg = EXT_POP(*pp_stack);
5363 if (!PyTuple_Check(stararg)) {
5364 PyObject *t = NULL;
Martin Panterb5944222016-01-31 06:30:56 +00005365 if (Py_TYPE(stararg)->tp_iter == NULL &&
5366 !PySequence_Check(stararg)) {
5367 PyErr_Format(PyExc_TypeError,
5368 "%.200s%.200s argument after * "
5369 "must be an iterable, not %.200s",
5370 PyEval_GetFuncName(func),
5371 PyEval_GetFuncDesc(func),
5372 stararg->ob_type->tp_name);
5373 goto ext_call_fail;
5374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005375 t = PySequence_Tuple(stararg);
5376 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005377 goto ext_call_fail;
5378 }
5379 Py_DECREF(stararg);
5380 stararg = t;
5381 }
5382 nstar = PyTuple_GET_SIZE(stararg);
5383 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005384 else {
5385 nstar = 0;
5386 }
5387
5388 callargs = update_star_args(nargs, nstar, stararg, pp_stack);
5389 if (callargs == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005390 goto ext_call_fail;
Victor Stinner74319ae2016-08-25 00:04:09 +02005391 }
5392
Jeremy Hylton985eba52003-02-05 23:13:00 +00005393#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005394 /* At this point, we have to look at the type of func to
5395 update the call stats properly. Do it here so as to avoid
5396 exposing the call stats machinery outside ceval.c
5397 */
5398 if (PyFunction_Check(func))
5399 PCALL(PCALL_FUNCTION);
5400 else if (PyMethod_Check(func))
5401 PCALL(PCALL_METHOD);
5402 else if (PyType_Check(func))
5403 PCALL(PCALL_TYPE);
5404 else if (PyCFunction_Check(func))
5405 PCALL(PCALL_CFUNCTION);
5406 else
5407 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005408#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 if (PyCFunction_Check(func)) {
5410 PyThreadState *tstate = PyThreadState_GET();
5411 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5412 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005413 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 result = PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005415 }
5416
Thomas Woutersce272b62007-09-19 21:19:28 +00005417ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005418 Py_XDECREF(callargs);
5419 Py_XDECREF(kwdict);
5420 Py_XDECREF(stararg);
5421 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005422}
5423
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005424/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005425 nb_index slot defined, and store in *pi.
5426 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5427 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 +00005428 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005429*/
Tim Petersb5196382001-12-16 19:44:20 +00005430/* Note: If v is NULL, return success without storing into *pi. This
5431 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5432 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005433*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005434int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005435_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005437 if (v != NULL) {
5438 Py_ssize_t x;
5439 if (PyIndex_Check(v)) {
5440 x = PyNumber_AsSsize_t(v, NULL);
5441 if (x == -1 && PyErr_Occurred())
5442 return 0;
5443 }
5444 else {
5445 PyErr_SetString(PyExc_TypeError,
5446 "slice indices must be integers or "
5447 "None or have an __index__ method");
5448 return 0;
5449 }
5450 *pi = x;
5451 }
5452 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005453}
5454
Guido van Rossum486364b2007-06-30 05:01:58 +00005455#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005456 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005457
Guido van Rossumb209a111997-04-29 18:18:01 +00005458static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005459cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005461 int res = 0;
5462 switch (op) {
5463 case PyCmp_IS:
5464 res = (v == w);
5465 break;
5466 case PyCmp_IS_NOT:
5467 res = (v != w);
5468 break;
5469 case PyCmp_IN:
5470 res = PySequence_Contains(w, v);
5471 if (res < 0)
5472 return NULL;
5473 break;
5474 case PyCmp_NOT_IN:
5475 res = PySequence_Contains(w, v);
5476 if (res < 0)
5477 return NULL;
5478 res = !res;
5479 break;
5480 case PyCmp_EXC_MATCH:
5481 if (PyTuple_Check(w)) {
5482 Py_ssize_t i, length;
5483 length = PyTuple_Size(w);
5484 for (i = 0; i < length; i += 1) {
5485 PyObject *exc = PyTuple_GET_ITEM(w, i);
5486 if (!PyExceptionClass_Check(exc)) {
5487 PyErr_SetString(PyExc_TypeError,
5488 CANNOT_CATCH_MSG);
5489 return NULL;
5490 }
5491 }
5492 }
5493 else {
5494 if (!PyExceptionClass_Check(w)) {
5495 PyErr_SetString(PyExc_TypeError,
5496 CANNOT_CATCH_MSG);
5497 return NULL;
5498 }
5499 }
5500 res = PyErr_GivenExceptionMatches(v, w);
5501 break;
5502 default:
5503 return PyObject_RichCompare(v, w, op);
5504 }
5505 v = res ? Py_True : Py_False;
5506 Py_INCREF(v);
5507 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005508}
5509
Thomas Wouters52152252000-08-17 22:55:00 +00005510static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005511import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5512{
5513 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005514 PyObject *import_func, *res;
5515 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005516
5517 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5518 if (import_func == NULL) {
5519 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5520 return NULL;
5521 }
5522
5523 /* Fast path for not overloaded __import__. */
5524 if (import_func == PyThreadState_GET()->interp->import_func) {
5525 int ilevel = _PyLong_AsInt(level);
5526 if (ilevel == -1 && PyErr_Occurred()) {
5527 return NULL;
5528 }
5529 res = PyImport_ImportModuleLevelObject(
5530 name,
5531 f->f_globals,
5532 f->f_locals == NULL ? Py_None : f->f_locals,
5533 fromlist,
5534 ilevel);
5535 return res;
5536 }
5537
5538 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005539
5540 stack[0] = name;
5541 stack[1] = f->f_globals;
5542 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5543 stack[3] = fromlist;
5544 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005545 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005546 Py_DECREF(import_func);
5547 return res;
5548}
5549
5550static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005551import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005554 _Py_IDENTIFIER(__name__);
5555 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005557 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005558 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5559 return x;
5560 /* Issue #17636: in case this failed because of a circular relative
5561 import, try to fallback on reading the module directly from
5562 sys.modules. */
5563 PyErr_Clear();
5564 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005565 if (pkgname == NULL) {
5566 goto error;
5567 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005568 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5569 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005570 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005571 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005572 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005573 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005574 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005575 if (x == NULL) {
5576 goto error;
5577 }
5578 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005579 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005580 error:
5581 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5582 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005583}
Guido van Rossumac7be682001-01-17 15:42:30 +00005584
Thomas Wouters52152252000-08-17 22:55:00 +00005585static int
5586import_all_from(PyObject *locals, PyObject *v)
5587{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005588 _Py_IDENTIFIER(__all__);
5589 _Py_IDENTIFIER(__dict__);
5590 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005591 PyObject *dict, *name, *value;
5592 int skip_leading_underscores = 0;
5593 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005595 if (all == NULL) {
5596 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5597 return -1; /* Unexpected error */
5598 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005599 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005600 if (dict == NULL) {
5601 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5602 return -1;
5603 PyErr_SetString(PyExc_ImportError,
5604 "from-import-* object has no __dict__ and no __all__");
5605 return -1;
5606 }
5607 all = PyMapping_Keys(dict);
5608 Py_DECREF(dict);
5609 if (all == NULL)
5610 return -1;
5611 skip_leading_underscores = 1;
5612 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005614 for (pos = 0, err = 0; ; pos++) {
5615 name = PySequence_GetItem(all, pos);
5616 if (name == NULL) {
5617 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5618 err = -1;
5619 else
5620 PyErr_Clear();
5621 break;
5622 }
5623 if (skip_leading_underscores &&
5624 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005625 PyUnicode_READY(name) != -1 &&
5626 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005627 {
5628 Py_DECREF(name);
5629 continue;
5630 }
5631 value = PyObject_GetAttr(v, name);
5632 if (value == NULL)
5633 err = -1;
5634 else if (PyDict_CheckExact(locals))
5635 err = PyDict_SetItem(locals, name, value);
5636 else
5637 err = PyObject_SetItem(locals, name, value);
5638 Py_DECREF(name);
5639 Py_XDECREF(value);
5640 if (err != 0)
5641 break;
5642 }
5643 Py_DECREF(all);
5644 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005645}
5646
Guido van Rossumac7be682001-01-17 15:42:30 +00005647static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005648format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005650 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005652 if (!obj)
5653 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005655 obj_str = _PyUnicode_AsString(obj);
5656 if (!obj_str)
5657 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005659 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005660}
Guido van Rossum950361c1997-01-24 13:49:28 +00005661
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005662static void
5663format_exc_unbound(PyCodeObject *co, int oparg)
5664{
5665 PyObject *name;
5666 /* Don't stomp existing exception */
5667 if (PyErr_Occurred())
5668 return;
5669 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5670 name = PyTuple_GET_ITEM(co->co_cellvars,
5671 oparg);
5672 format_exc_check_arg(
5673 PyExc_UnboundLocalError,
5674 UNBOUNDLOCAL_ERROR_MSG,
5675 name);
5676 } else {
5677 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5678 PyTuple_GET_SIZE(co->co_cellvars));
5679 format_exc_check_arg(PyExc_NameError,
5680 UNBOUNDFREE_ERROR_MSG, name);
5681 }
5682}
5683
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005684static PyObject *
5685unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005686 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005687{
5688 PyObject *res;
5689 if (Py_REFCNT(v) == 2) {
5690 /* In the common case, there are 2 references to the value
5691 * stored in 'variable' when the += is performed: one on the
5692 * value stack (in 'v') and one still stored in the
5693 * 'variable'. We try to delete the variable now to reduce
5694 * the refcnt to 1.
5695 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005696 int opcode, oparg;
5697 NEXTOPARG();
5698 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005699 case STORE_FAST:
5700 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005701 PyObject **fastlocals = f->f_localsplus;
5702 if (GETLOCAL(oparg) == v)
5703 SETLOCAL(oparg, NULL);
5704 break;
5705 }
5706 case STORE_DEREF:
5707 {
5708 PyObject **freevars = (f->f_localsplus +
5709 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005710 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005711 if (PyCell_GET(c) == v)
5712 PyCell_Set(c, NULL);
5713 break;
5714 }
5715 case STORE_NAME:
5716 {
5717 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005718 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005719 PyObject *locals = f->f_locals;
5720 if (PyDict_CheckExact(locals) &&
5721 PyDict_GetItem(locals, name) == v) {
5722 if (PyDict_DelItem(locals, name) != 0) {
5723 PyErr_Clear();
5724 }
5725 }
5726 break;
5727 }
5728 }
5729 }
5730 res = v;
5731 PyUnicode_Append(&res, w);
5732 return res;
5733}
5734
Guido van Rossum950361c1997-01-24 13:49:28 +00005735#ifdef DYNAMIC_EXECUTION_PROFILE
5736
Skip Montanarof118cb12001-10-15 20:51:38 +00005737static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005738getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005740 int i;
5741 PyObject *l = PyList_New(256);
5742 if (l == NULL) return NULL;
5743 for (i = 0; i < 256; i++) {
5744 PyObject *x = PyLong_FromLong(a[i]);
5745 if (x == NULL) {
5746 Py_DECREF(l);
5747 return NULL;
5748 }
5749 PyList_SetItem(l, i, x);
5750 }
5751 for (i = 0; i < 256; i++)
5752 a[i] = 0;
5753 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005754}
5755
5756PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005757_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005758{
5759#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005760 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005761#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005762 int i;
5763 PyObject *l = PyList_New(257);
5764 if (l == NULL) return NULL;
5765 for (i = 0; i < 257; i++) {
5766 PyObject *x = getarray(dxpairs[i]);
5767 if (x == NULL) {
5768 Py_DECREF(l);
5769 return NULL;
5770 }
5771 PyList_SetItem(l, i, x);
5772 }
5773 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005774#endif
5775}
5776
5777#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005778
5779Py_ssize_t
5780_PyEval_RequestCodeExtraIndex(freefunc free)
5781{
5782 PyThreadState *tstate = PyThreadState_Get();
5783 Py_ssize_t new_index;
5784
5785 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5786 return -1;
5787 }
5788 new_index = tstate->co_extra_user_count++;
5789 tstate->co_extra_freefuncs[new_index] = free;
5790 return new_index;
5791}