blob: b2ddaa3170cd97af09b5db75b5d4c879b6a41865 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000019#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Thomas Wouters477c8d52006-05-27 19:21:47 +000023#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000024
25#define READ_TIMESTAMP(var)
26
27#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000028
29typedef unsigned long long uint64;
30
Ezio Melotti13925002011-03-16 11:05:33 +020031/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000032 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
33 "__powerpc__" appears to be the correct one for Linux with GCC
34*/
35#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Michael W. Hudson75eabd22005-01-18 15:56:11 +000037#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000038
39static void
40ppc_getcounter(uint64 *v)
41{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020042 unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000043
44 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 asm volatile ("mftbu %0" : "=r" (tbu) );
46 asm volatile ("mftb %0" : "=r" (tb) );
47 asm volatile ("mftbu %0" : "=r" (tbu2));
48 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* The slightly peculiar way of writing the next lines is
51 compiled better by GCC than any other way I tried. */
52 ((long*)(v))[0] = tbu;
53 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000054}
55
Mark Dickinsona25b1312009-10-31 10:18:44 +000056#elif defined(__i386__)
57
58/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000059
Michael W. Hudson75eabd22005-01-18 15:56:11 +000060#define READ_TIMESTAMP(val) \
61 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000062
Mark Dickinsona25b1312009-10-31 10:18:44 +000063#elif defined(__x86_64__)
64
65/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
66 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
67 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
68 32-bit pieces of the result. */
69
Victor Stinner0b881dd2014-12-12 13:17:41 +010070#define READ_TIMESTAMP(val) do { \
71 unsigned int h, l; \
72 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
73 (val) = ((uint64)l) | (((uint64)h) << 32); \
74 } while(0)
Mark Dickinsona25b1312009-10-31 10:18:44 +000075
76
77#else
78
79#error "Don't know how to implement timestamp counter for this architecture"
80
Michael W. Hudson800ba232004-08-12 18:19:17 +000081#endif
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 uint64 intr, inst, loop;
87 PyThreadState *tstate = PyThreadState_Get();
88 if (!tstate->interp->tscdump)
89 return;
90 intr = intr1 - intr0;
91 inst = inst1 - inst0 - intr;
92 loop = loop1 - loop0 - intr;
93 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000094 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095}
Michael W. Hudson800ba232004-08-12 18:19:17 +000096
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000097#endif
98
Guido van Rossum04691fc1992-08-12 15:35:34 +000099/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +0000101
Guido van Rossum408027e1996-12-30 16:17:54 +0000102#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000103/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104#define LLTRACE 1 /* Low-level trace feature */
105#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000106#endif
107
Jeremy Hylton52820442001-01-03 23:52:36 +0000108typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000109
Guido van Rossum374a9221991-04-04 10:40:29 +0000110/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#ifdef WITH_TSC
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700112static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#else
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700114static PyObject * call_function(PyObject ***, Py_ssize_t, PyObject *);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000115#endif
Victor Stinnerd8735722016-09-09 12:36:44 -0700116static PyObject * fast_function(PyObject *, PyObject **, Py_ssize_t, PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -0700117static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +0000118
Guido van Rossum0a066c01992-03-27 17:29:15 +0000119#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000120static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200121static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100123static int call_trace(Py_tracefunc, PyObject *,
124 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000126static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100127 PyThreadState *, PyFrameObject *,
128 int, PyObject *);
129static void call_exc_trace(Py_tracefunc, PyObject *,
130 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100132 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300135static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000136static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000137static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000138static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000139static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200140static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300141 PyFrameObject *, const unsigned short *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500142static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000143
Paul Prescode68140d2000-08-30 20:25:01 +0000144#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000148#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "free variable '%.200s' referenced before assignment" \
150 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Guido van Rossum950361c1997-01-24 13:49:28 +0000152/* Dynamic execution profile */
153#ifdef DYNAMIC_EXECUTION_PROFILE
154#ifdef DXPAIRS
155static long dxpairs[257][256];
156#define dxp dxpairs[256]
157#else
158static long dxp[256];
159#endif
160#endif
161
Jeremy Hylton985eba52003-02-05 23:13:00 +0000162/* Function call profile */
163#ifdef CALL_PROFILE
164#define PCALL_NUM 11
165static int pcall[PCALL_NUM];
166
167#define PCALL_ALL 0
168#define PCALL_FUNCTION 1
169#define PCALL_FAST_FUNCTION 2
170#define PCALL_FASTER_FUNCTION 3
171#define PCALL_METHOD 4
172#define PCALL_BOUND_METHOD 5
173#define PCALL_CFUNCTION 6
174#define PCALL_TYPE 7
175#define PCALL_GENERATOR 8
176#define PCALL_OTHER 9
177#define PCALL_POP 10
178
179/* Notes about the statistics
180
181 PCALL_FAST stats
182
183 FAST_FUNCTION means no argument tuple needs to be created.
184 FASTER_FUNCTION means that the fast-path frame setup code is used.
185
186 If there is a method call where the call can be optimized by changing
187 the argument tuple and calling the function directly, it gets recorded
188 twice.
189
190 As a result, the relationship among the statistics appears to be
191 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
192 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
193 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
194 PCALL_METHOD > PCALL_BOUND_METHOD
195*/
196
197#define PCALL(POS) pcall[POS]++
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return Py_BuildValue("iiiiiiiiiii",
203 pcall[0], pcall[1], pcall[2], pcall[3],
204 pcall[4], pcall[5], pcall[6], pcall[7],
205 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000206}
207#else
208#define PCALL(O)
209
210PyObject *
211PyEval_GetCallStats(PyObject *self)
212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000215}
216#endif
217
Tim Peters5ca576e2001-06-18 22:08:13 +0000218
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000219#ifdef WITH_THREAD
220#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
221#else
222#define GIL_REQUEST 0
223#endif
224
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000225/* This can set eval_breaker to 0 even though gil_drop_request became
226 1. We believe this is all right because the eval loop will release
227 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000228#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 _Py_atomic_store_relaxed( \
230 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000231 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
233 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000234
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000235#ifdef WITH_THREAD
236
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000237#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 do { \
239 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
240 _Py_atomic_store_relaxed(&eval_breaker, 1); \
241 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242
243#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 do { \
245 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
246 COMPUTE_EVAL_BREAKER(); \
247 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000249#endif
250
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000251/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 do { \
254 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
255 _Py_atomic_store_relaxed(&eval_breaker, 1); \
256 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000257
258#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 do { \
260 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
261 COMPUTE_EVAL_BREAKER(); \
262 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000263
264#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 do { \
266 pending_async_exc = 1; \
267 _Py_atomic_store_relaxed(&eval_breaker, 1); \
268 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000269
270#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000272
273
Guido van Rossume59214e1994-08-30 08:01:59 +0000274#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000275
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000277#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000278#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000279#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000280
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000281static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000282static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000283/* This single variable consolidates all requests to break out of the fast path
284 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000285static _Py_atomic_int eval_breaker = {0};
286/* Request for dropping the GIL */
287static _Py_atomic_int gil_drop_request = {0};
288/* Request for running pending calls. */
289static _Py_atomic_int pendingcalls_to_do = {0};
290/* Request for looking at the `async_exc` field of the current thread state.
291 Guarded by the GIL. */
292static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000293
294#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295
Tim Peters7f468f22004-10-11 02:40:51 +0000296int
297PyEval_ThreadsInitialized(void)
298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000300}
301
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (gil_created())
306 return;
307 create_gil();
308 take_gil(PyThreadState_GET());
309 main_thread = PyThread_get_thread_ident();
310 if (!pending_lock)
311 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000312}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000313
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000314void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000315_PyEval_FiniThreads(void)
316{
317 if (!gil_created())
318 return;
319 destroy_gil();
320 assert(!gil_created());
321}
322
323void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000324PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PyThreadState *tstate = PyThreadState_GET();
327 if (tstate == NULL)
328 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
329 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000330}
331
332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* This function must succeed when the current thread state is NULL.
336 We therefore avoid PyThreadState_GET() which dumps a fatal error
337 in debug mode.
338 */
339 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
340 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000341}
342
343void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000344PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (tstate == NULL)
347 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
348 /* Check someone has called PyEval_InitThreads() to create the lock */
349 assert(gil_created());
350 take_gil(tstate);
351 if (PyThreadState_Swap(tstate) != NULL)
352 Py_FatalError(
353 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000354}
355
356void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (tstate == NULL)
360 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
361 if (PyThreadState_Swap(NULL) != tstate)
362 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
363 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000364}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000365
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200366/* This function is called from PyOS_AfterFork to destroy all threads which are
367 * not running in the child process, and clear internal locks which might be
368 * held by those threads. (This could also be done using pthread_atfork
369 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000370
371void
372PyEval_ReInitThreads(void)
373{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200374 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200376 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (!gil_created())
379 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 recreate_gil();
381 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200382 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* Update the threading module with the new state.
386 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200387 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 "threading");
389 if (threading == NULL) {
390 /* threading not imported */
391 PyErr_Clear();
392 return;
393 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200394 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 if (result == NULL)
396 PyErr_WriteUnraisable(threading);
397 else
398 Py_DECREF(result);
399 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200400
401 /* Destroy all threads except the current one */
402 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000403}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000404
405#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000406static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000407static int pending_async_exc = 0;
408#endif /* WITH_THREAD */
409
410/* This function is used to signal that async exceptions are waiting to be
411 raised, therefore it is also useful in non-threaded builds. */
412
413void
414_PyEval_SignalAsyncExc(void)
415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000417}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000418
Guido van Rossumff4949e1992-08-05 19:58:53 +0000419/* Functions save_thread and restore_thread are always defined so
420 dynamically loaded modules needn't be compiled separately for use
421 with and without threads: */
422
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000423PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000424PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 PyThreadState *tstate = PyThreadState_Swap(NULL);
427 if (tstate == NULL)
428 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000429#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 if (gil_created())
431 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000434}
435
436void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000437PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (tstate == NULL)
440 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000441#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 if (gil_created()) {
443 int err = errno;
444 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200445 /* _Py_Finalizing is protected by the GIL */
446 if (_Py_Finalizing && tstate != _Py_Finalizing) {
447 drop_gil(tstate);
448 PyThread_exit_thread();
449 assert(0); /* unreachable */
450 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 errno = err;
452 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000453#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455}
456
457
Guido van Rossuma9672091994-09-14 13:31:22 +0000458/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
459 signal handlers or Mac I/O completion routines) can schedule calls
460 to a function to be called synchronously.
461 The synchronous function is called with one void* argument.
462 It should return 0 for success or -1 for failure -- failure should
463 be accompanied by an exception.
464
465 If registry succeeds, the registry function returns 0; if it fails
466 (e.g. due to too many pending calls) it returns -1 (without setting
467 an exception condition).
468
469 Note that because registry may occur from within signal handlers,
470 or other asynchronous events, calling malloc() is unsafe!
471
472#ifdef WITH_THREAD
473 Any thread can schedule pending calls, but only the main thread
474 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000475 There is no facility to schedule calls to a particular thread, but
476 that should be easy to change, should that ever be required. In
477 that case, the static variables here should go into the python
478 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000479#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000481
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000482#ifdef WITH_THREAD
483
484/* The WITH_THREAD implementation is thread-safe. It allows
485 scheduling to be made from any thread, and even from an executing
486 callback.
487 */
488
489#define NPENDINGCALLS 32
490static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 int (*func)(void *);
492 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000493} pendingcalls[NPENDINGCALLS];
494static int pendingfirst = 0;
495static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000496
497int
498Py_AddPendingCall(int (*func)(void *), void *arg)
499{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 int i, j, result=0;
501 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 /* try a few times for the lock. Since this mechanism is used
504 * for signal handling (on the main thread), there is a (slim)
505 * chance that a signal is delivered on the same thread while we
506 * hold the lock during the Py_MakePendingCalls() function.
507 * This avoids a deadlock in that case.
508 * Note that signals can be delivered on any thread. In particular,
509 * on Windows, a SIGINT is delivered on a system-created worker
510 * thread.
511 * We also check for lock being NULL, in the unlikely case that
512 * this function is called before any bytecode evaluation takes place.
513 */
514 if (lock != NULL) {
515 for (i = 0; i<100; i++) {
516 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
517 break;
518 }
519 if (i == 100)
520 return -1;
521 }
522
523 i = pendinglast;
524 j = (i + 1) % NPENDINGCALLS;
525 if (j == pendingfirst) {
526 result = -1; /* Queue full */
527 } else {
528 pendingcalls[i].func = func;
529 pendingcalls[i].arg = arg;
530 pendinglast = j;
531 }
532 /* signal main loop */
533 SIGNAL_PENDING_CALLS();
534 if (lock != NULL)
535 PyThread_release_lock(lock);
536 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000537}
538
539int
540Py_MakePendingCalls(void)
541{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200542 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 int i;
544 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 if (!pending_lock) {
547 /* initial allocation of the lock */
548 pending_lock = PyThread_allocate_lock();
549 if (pending_lock == NULL)
550 return -1;
551 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 /* only service pending calls on main thread */
554 if (main_thread && PyThread_get_thread_ident() != main_thread)
555 return 0;
556 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200557 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200559 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* perform a bounded number of calls, in case of recursion */
561 for (i=0; i<NPENDINGCALLS; i++) {
562 int j;
563 int (*func)(void *);
564 void *arg = NULL;
565
566 /* pop one item off the queue while holding the lock */
567 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
568 j = pendingfirst;
569 if (j == pendinglast) {
570 func = NULL; /* Queue empty */
571 } else {
572 func = pendingcalls[j].func;
573 arg = pendingcalls[j].arg;
574 pendingfirst = (j + 1) % NPENDINGCALLS;
575 }
576 if (pendingfirst != pendinglast)
577 SIGNAL_PENDING_CALLS();
578 else
579 UNSIGNAL_PENDING_CALLS();
580 PyThread_release_lock(pending_lock);
581 /* having released the lock, perform the callback */
582 if (func == NULL)
583 break;
584 r = func(arg);
585 if (r)
586 break;
587 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200588 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000590}
591
592#else /* if ! defined WITH_THREAD */
593
594/*
595 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
596 This code is used for signal handling in python that isn't built
597 with WITH_THREAD.
598 Don't use this implementation when Py_AddPendingCalls() can happen
599 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600
Guido van Rossuma9672091994-09-14 13:31:22 +0000601 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000602 (1) nested asynchronous calls to Py_AddPendingCall()
603 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000605 (1) is very unlikely because typically signal delivery
606 is blocked during signal handling. So it should be impossible.
607 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000608 The current code is safe against (2), but not against (1).
609 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000610 thread is present, interrupted by signals, and that the critical
611 section is protected with the "busy" variable. On Windows, which
612 delivers SIGINT on a system thread, this does not hold and therefore
613 Windows really shouldn't use this version.
614 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000615*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000616
Guido van Rossuma9672091994-09-14 13:31:22 +0000617#define NPENDINGCALLS 32
618static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 int (*func)(void *);
620 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000621} pendingcalls[NPENDINGCALLS];
622static volatile int pendingfirst = 0;
623static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000624static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000625
626int
Thomas Wouters334fb892000-07-25 12:56:38 +0000627Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 static volatile int busy = 0;
630 int i, j;
631 /* XXX Begin critical section */
632 if (busy)
633 return -1;
634 busy = 1;
635 i = pendinglast;
636 j = (i + 1) % NPENDINGCALLS;
637 if (j == pendingfirst) {
638 busy = 0;
639 return -1; /* Queue full */
640 }
641 pendingcalls[i].func = func;
642 pendingcalls[i].arg = arg;
643 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 SIGNAL_PENDING_CALLS();
646 busy = 0;
647 /* XXX End critical section */
648 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000649}
650
Guido van Rossum180d7b41994-09-29 09:45:57 +0000651int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000652Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000653{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 static int busy = 0;
655 if (busy)
656 return 0;
657 busy = 1;
658 UNSIGNAL_PENDING_CALLS();
659 for (;;) {
660 int i;
661 int (*func)(void *);
662 void *arg;
663 i = pendingfirst;
664 if (i == pendinglast)
665 break; /* Queue empty */
666 func = pendingcalls[i].func;
667 arg = pendingcalls[i].arg;
668 pendingfirst = (i + 1) % NPENDINGCALLS;
669 if (func(arg) < 0) {
670 busy = 0;
671 SIGNAL_PENDING_CALLS(); /* We're not done yet */
672 return -1;
673 }
674 }
675 busy = 0;
676 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000677}
678
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000679#endif /* WITH_THREAD */
680
Guido van Rossuma9672091994-09-14 13:31:22 +0000681
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000682/* The interpreter's recursion limit */
683
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000684#ifndef Py_DEFAULT_RECURSION_LIMIT
685#define Py_DEFAULT_RECURSION_LIMIT 1000
686#endif
687static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
688int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000689
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000690int
691Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000694}
695
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000696void
697Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 recursion_limit = new_limit;
700 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000701}
702
Armin Rigo2b3eb402003-10-28 12:05:48 +0000703/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
704 if the recursion_depth reaches _Py_CheckRecursionLimit.
705 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
706 to guarantee that _Py_CheckRecursiveCall() is regularly called.
707 Without USE_STACKCHECK, there is no need for this. */
708int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300709_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000712
713#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 if (PyOS_CheckStack()) {
715 --tstate->recursion_depth;
716 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
717 return -1;
718 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000719#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 _Py_CheckRecursionLimit = recursion_limit;
721 if (tstate->recursion_critical)
722 /* Somebody asked that we don't check for recursion. */
723 return 0;
724 if (tstate->overflowed) {
725 if (tstate->recursion_depth > recursion_limit + 50) {
726 /* Overflowing while handling an overflow. Give up. */
727 Py_FatalError("Cannot recover from stack overflow.");
728 }
729 return 0;
730 }
731 if (tstate->recursion_depth > recursion_limit) {
732 --tstate->recursion_depth;
733 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400734 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 "maximum recursion depth exceeded%s",
736 where);
737 return -1;
738 }
739 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000740}
741
Guido van Rossum374a9221991-04-04 10:40:29 +0000742/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000743enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000744 WHY_NOT = 0x0001, /* No error */
745 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000746 WHY_RETURN = 0x0008, /* 'return' statement */
747 WHY_BREAK = 0x0010, /* 'break' statement */
748 WHY_CONTINUE = 0x0020, /* 'continue' statement */
749 WHY_YIELD = 0x0040, /* 'yield' operator */
750 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000751};
Guido van Rossum374a9221991-04-04 10:40:29 +0000752
Benjamin Peterson87880242011-07-03 16:48:31 -0500753static void save_exc_state(PyThreadState *, PyFrameObject *);
754static void swap_exc_state(PyThreadState *, PyFrameObject *);
755static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400756static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000757static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000758
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000759/* Records whether tracing is on for any thread. Counts the number of
760 threads for which tstate->c_tracefunc is non-NULL, so if the value
761 is 0, we know we don't have to check this thread's c_tracefunc.
762 This speeds up the if statement in PyEval_EvalFrameEx() after
763 fast_next_opcode*/
764static int _Py_TracingPossible = 0;
765
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000766
Guido van Rossum374a9221991-04-04 10:40:29 +0000767
Guido van Rossumb209a111997-04-29 18:18:01 +0000768PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000769PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return PyEval_EvalCodeEx(co,
772 globals, locals,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 (PyObject **)NULL, 0,
776 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777}
778
779
780/* Interpreter main loop */
781
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000782PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000783PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* This is for backward compatibility with extension modules that
785 used this API; core interpreter code should call
786 PyEval_EvalFrameEx() */
787 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000788}
789
790PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000792{
Brett Cannon3cebf932016-09-05 15:33:46 -0700793 PyThreadState *tstate = PyThreadState_GET();
794 return tstate->interp->eval_frame(f, throwflag);
795}
796
797PyObject *
798_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
799{
Guido van Rossum950361c1997-01-24 13:49:28 +0000800#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000802#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200803 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300804 const unsigned short *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200805 int opcode; /* Current opcode */
806 int oparg; /* Current opcode argument, if any */
807 enum why_code why; /* Reason for block stack unwind */
808 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 PyObject *retval = NULL; /* Return value */
810 PyThreadState *tstate = PyThreadState_GET();
811 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 is true when the line being executed has changed. The
818 initial values are such as to make this false the first
819 time it is tested. */
820 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000821
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300822 const unsigned short *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyObject *names;
824 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000825
Brett Cannon368b4b72012-04-02 12:17:59 -0400826#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200827 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400828#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200829
Antoine Pitroub52ec782009-01-25 16:34:23 +0000830/* Computed GOTOs, or
831 the-optimization-commonly-but-improperly-known-as-"threaded code"
832 using gcc's labels-as-values extension
833 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
834
835 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837 combined with a lookup table of jump addresses. However, since the
838 indirect jump instruction is shared by all opcodes, the CPU will have a
839 hard time making the right prediction for where to jump next (actually,
840 it will be always wrong except in the uncommon case of a sequence of
841 several identical opcodes).
842
843 "Threaded code" in contrast, uses an explicit jump table and an explicit
844 indirect jump instruction at the end of each opcode. Since the jump
845 instruction is at a different address for each opcode, the CPU will make a
846 separate prediction for each of these instructions, which is equivalent to
847 predicting the second opcode of each opcode pair. These predictions have
848 a much better chance to turn out valid, especially in small bytecode loops.
849
850 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000852 and potentially many more instructions (depending on the pipeline width).
853 A correctly predicted branch, however, is nearly free.
854
855 At the time of this writing, the "threaded code" version is up to 15-20%
856 faster than the normal "switch" version, depending on the compiler and the
857 CPU architecture.
858
859 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
860 because it would render the measurements invalid.
861
862
863 NOTE: care must be taken that the compiler doesn't try to "optimize" the
864 indirect jumps by sharing them between all opcodes. Such optimizations
865 can be disabled on gcc by using the -fno-gcse flag (or possibly
866 -fno-crossjumping).
867*/
868
Antoine Pitrou042b1282010-08-13 21:15:58 +0000869#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000870#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000871#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872#endif
873
Antoine Pitrou042b1282010-08-13 21:15:58 +0000874#ifdef HAVE_COMPUTED_GOTOS
875 #ifndef USE_COMPUTED_GOTOS
876 #define USE_COMPUTED_GOTOS 1
877 #endif
878#else
879 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
880 #error "Computed gotos are not supported on this compiler."
881 #endif
882 #undef USE_COMPUTED_GOTOS
883 #define USE_COMPUTED_GOTOS 0
884#endif
885
886#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000887/* Import the static jump table */
888#include "opcode_targets.h"
889
Antoine Pitroub52ec782009-01-25 16:34:23 +0000890#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 { \
896 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
897 FAST_DISPATCH(); \
898 } \
899 continue; \
900 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901
902#ifdef LLTRACE
903#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 { \
905 if (!lltrace && !_Py_TracingPossible) { \
906 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300907 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300908 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 } \
910 goto fast_next_opcode; \
911 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912#else
913#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 { \
915 if (!_Py_TracingPossible) { \
916 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300917 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300918 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 } \
920 goto fast_next_opcode; \
921 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922#endif
923
924#else
925#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300927
Antoine Pitroub52ec782009-01-25 16:34:23 +0000928#define DISPATCH() continue
929#define FAST_DISPATCH() goto fast_next_opcode
930#endif
931
932
Neal Norwitza81d2202002-07-14 00:27:26 +0000933/* Tuple access macros */
934
935#ifndef Py_DEBUG
936#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
937#else
938#define GETITEM(v, i) PyTuple_GetItem((v), (i))
939#endif
940
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000941#ifdef WITH_TSC
942/* Use Pentium timestamp counter to mark certain events:
943 inst0 -- beginning of switch statement for opcode dispatch
944 inst1 -- end of switch statement (may be skipped)
945 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000947 (may be skipped)
948 intr1 -- beginning of long interruption
949 intr2 -- end of long interruption
950
951 Many opcodes call out to helper C functions. In some cases, the
952 time in those functions should be counted towards the time for the
953 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
954 calls another Python function; there's no point in charge all the
955 bytecode executed by the called function to the caller.
956
957 It's hard to make a useful judgement statically. In the presence
958 of operator overloading, it's impossible to tell if a call will
959 execute new Python code or not.
960
961 It's a case-by-case judgement. I'll use intr1 for the following
962 cases:
963
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000964 IMPORT_STAR
965 IMPORT_FROM
966 CALL_FUNCTION (and friends)
967
968 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
970 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 READ_TIMESTAMP(inst0);
973 READ_TIMESTAMP(inst1);
974 READ_TIMESTAMP(loop0);
975 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* shut up the compiler */
978 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000979#endif
980
Guido van Rossum374a9221991-04-04 10:40:29 +0000981/* Code access macros */
982
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300983#ifdef WORDS_BIGENDIAN
984 #define OPCODE(word) ((word) >> 8)
985 #define OPARG(word) ((word) & 255)
986#else
987 #define OPCODE(word) ((word) & 255)
988 #define OPARG(word) ((word) >> 8)
989#endif
990/* The integer overflow is checked by an assertion below. */
991#define INSTR_OFFSET() (2*(int)(next_instr - first_instr))
992#define NEXTOPARG() do { \
993 unsigned short word = *next_instr; \
994 opcode = OPCODE(word); \
995 oparg = OPARG(word); \
996 next_instr++; \
997 } while (0)
998#define JUMPTO(x) (next_instr = first_instr + (x)/2)
999#define JUMPBY(x) (next_instr += (x)/2)
Guido van Rossum374a9221991-04-04 10:40:29 +00001000
Raymond Hettingerf606f872003-03-16 03:11:04 +00001001/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Some opcodes tend to come in pairs thus making it possible to
1003 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001004 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Verifying the prediction costs a single high-speed test of a register
1007 variable against a constant. If the pairing was good, then the
1008 processor's own internal branch predication has a high likelihood of
1009 success, resulting in a nearly zero-overhead transition to the
1010 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001011 including its unpredictable switch-case branch. Combined with the
1012 processor's internal branch prediction, a successful PREDICT has the
1013 effect of making the two opcodes run as if they were a single new opcode
1014 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001015
Georg Brandl86b2fb92008-07-16 03:43:04 +00001016 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 predictions turned-on and interpret the results as if some opcodes
1018 had been combined or turn-off predictions so that the opcode frequency
1019 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020
1021 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 the CPU to record separate branch prediction information for each
1023 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024
Raymond Hettingerf606f872003-03-16 03:11:04 +00001025*/
1026
Antoine Pitrou042b1282010-08-13 21:15:58 +00001027#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +00001029#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001030#define PREDICT(op) \
1031 do{ \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001032 unsigned short word = *next_instr; \
1033 opcode = OPCODE(word); \
1034 if (opcode == op){ \
1035 oparg = OPARG(word); \
1036 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001037 goto PRED_##op; \
1038 } \
1039 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001040#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001041#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +00001042
Raymond Hettingerf606f872003-03-16 03:11:04 +00001043
Guido van Rossum374a9221991-04-04 10:40:29 +00001044/* Stack manipulation macros */
1045
Martin v. Löwis18e16552006-02-15 17:27:45 +00001046/* The stack can grow at most MAXINT deep, as co_nlocals and
1047 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001048#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1049#define EMPTY() (STACK_LEVEL() == 0)
1050#define TOP() (stack_pointer[-1])
1051#define SECOND() (stack_pointer[-2])
1052#define THIRD() (stack_pointer[-3])
1053#define FOURTH() (stack_pointer[-4])
1054#define PEEK(n) (stack_pointer[-(n)])
1055#define SET_TOP(v) (stack_pointer[-1] = (v))
1056#define SET_SECOND(v) (stack_pointer[-2] = (v))
1057#define SET_THIRD(v) (stack_pointer[-3] = (v))
1058#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1059#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1060#define BASIC_STACKADJ(n) (stack_pointer += n)
1061#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1062#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001063
Guido van Rossum96a42c81992-01-12 02:29:51 +00001064#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001066 lltrace && prtrace(TOP(), "push")); \
1067 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001069 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001071 lltrace && prtrace(TOP(), "stackadj")); \
1072 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001073#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001074 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1075 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001076#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001077#define PUSH(v) BASIC_PUSH(v)
1078#define POP() BASIC_POP()
1079#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001080#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001081#endif
1082
Guido van Rossum681d79a1995-07-18 14:51:37 +00001083/* Local variable macros */
1084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001086
1087/* The SETLOCAL() macro must not DECREF the local variable in-place and
1088 then store the new value; it must copy the old value to a temporary
1089 value, then store the new value, and then DECREF the temporary value.
1090 This is because it is possible that during the DECREF the frame is
1091 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1092 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001094 GETLOCAL(i) = value; \
1095 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001096
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001097
1098#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 while (STACK_LEVEL() > (b)->b_level) { \
1100 PyObject *v = POP(); \
1101 Py_XDECREF(v); \
1102 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001103
1104#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001105 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 PyObject *type, *value, *traceback; \
1107 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1108 while (STACK_LEVEL() > (b)->b_level + 3) { \
1109 value = POP(); \
1110 Py_XDECREF(value); \
1111 } \
1112 type = tstate->exc_type; \
1113 value = tstate->exc_value; \
1114 traceback = tstate->exc_traceback; \
1115 tstate->exc_type = POP(); \
1116 tstate->exc_value = POP(); \
1117 tstate->exc_traceback = POP(); \
1118 Py_XDECREF(type); \
1119 Py_XDECREF(value); \
1120 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001121 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001122
Guido van Rossuma027efa1997-05-05 20:56:21 +00001123/* Start of code */
1124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* push frame */
1126 if (Py_EnterRecursiveCall(""))
1127 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (tstate->use_tracing) {
1132 if (tstate->c_tracefunc != NULL) {
1133 /* tstate->c_tracefunc, if defined, is a
1134 function that will be called on *every* entry
1135 to a code block. Its return value, if not
1136 None, is a function that will be called at
1137 the start of each executed line of code.
1138 (Actually, the function must return itself
1139 in order to continue tracing.) The trace
1140 functions are called with three arguments:
1141 a pointer to the current frame, a string
1142 indicating why the function is called, and
1143 an argument which depends on the situation.
1144 The global trace function is also called
1145 whenever an exception is detected. */
1146 if (call_trace_protected(tstate->c_tracefunc,
1147 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001148 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 /* Trace function raised an error */
1150 goto exit_eval_frame;
1151 }
1152 }
1153 if (tstate->c_profilefunc != NULL) {
1154 /* Similar for c_profilefunc, except it needn't
1155 return itself and isn't called for "line" events */
1156 if (call_trace_protected(tstate->c_profilefunc,
1157 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001158 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 /* Profile function raised an error */
1160 goto exit_eval_frame;
1161 }
1162 }
1163 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 co = f->f_code;
1166 names = co->co_names;
1167 consts = co->co_consts;
1168 fastlocals = f->f_localsplus;
1169 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001170 assert(PyBytes_Check(co->co_code));
1171 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
1172 assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0);
Serhiy Storchaka74f2fe62016-05-25 20:35:44 +03001173 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2));
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001174 first_instr = (unsigned short*) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001175 /*
1176 f->f_lasti refers to the index of the last instruction,
1177 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001178
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001179 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001180 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 When the PREDICT() macros are enabled, some opcode pairs follow in
1183 direct succession without updating f->f_lasti. A successful
1184 prediction effectively links the two codes together as if they
1185 were a single new opcode; accordingly,f->f_lasti will point to
1186 the first code in the pair (for instance, GET_ITER followed by
1187 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001188 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001190 next_instr = first_instr;
1191 if (f->f_lasti >= 0) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001192 assert(f->f_lasti % 2 == 0);
1193 next_instr += f->f_lasti/2 + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 stack_pointer = f->f_stacktop;
1196 assert(stack_pointer != NULL);
1197 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001198 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001199
Yury Selivanoveb636452016-09-08 22:01:51 -07001200 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001201 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 /* We were in an except handler when we left,
1203 restore the exception state which was put aside
1204 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001205 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001207 else
1208 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001210
Tim Peters5ca576e2001-06-18 22:08:13 +00001211#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001212 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001213#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 if (throwflag) /* support for generator.throw() */
1218 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001219
Victor Stinnerace47d72013-07-18 01:41:08 +02001220#ifdef Py_DEBUG
1221 /* PyEval_EvalFrameEx() must not be called with an exception set,
1222 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001223 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001224 assert(!PyErr_Occurred());
1225#endif
1226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001228#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (inst1 == 0) {
1230 /* Almost surely, the opcode executed a break
1231 or a continue, preventing inst1 from being set
1232 on the way out of the loop.
1233 */
1234 READ_TIMESTAMP(inst1);
1235 loop1 = inst1;
1236 }
1237 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1238 intr0, intr1);
1239 ticked = 0;
1240 inst1 = 0;
1241 intr0 = 0;
1242 intr1 = 0;
1243 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1246 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001247 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 /* Do periodic things. Doing this every time through
1250 the loop would add too much overhead, so we do it
1251 only every Nth instruction. We also do it if
1252 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1253 event needs attention (e.g. a signal handler or
1254 async I/O handler); see Py_AddPendingCall() and
1255 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001258 if (OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001260 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 goto fast_next_opcode;
1262 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001263#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001265#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001267 if (Py_MakePendingCalls() < 0)
1268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001270#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001271 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 /* Give another thread a chance */
1273 if (PyThreadState_Swap(NULL) != tstate)
1274 Py_FatalError("ceval: tstate mix-up");
1275 drop_gil(tstate);
1276
1277 /* Other threads may run now */
1278
1279 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001280
1281 /* Check if we should make a quick exit. */
1282 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1283 drop_gil(tstate);
1284 PyThread_exit_thread();
1285 }
1286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (PyThreadState_Swap(tstate) != NULL)
1288 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* Check for asynchronous exceptions. */
1292 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 tstate->async_exc = NULL;
1295 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 PyErr_SetNone(exc);
1297 Py_DECREF(exc);
1298 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
1300 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 fast_next_opcode:
1303 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001308 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* see maybe_call_line_trace
1311 for expository comments */
1312 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 err = maybe_call_line_trace(tstate->c_tracefunc,
1315 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001316 tstate, f,
1317 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 /* Reload possibly changed frame fields */
1319 JUMPTO(f->f_lasti);
1320 if (f->f_stacktop != NULL) {
1321 stack_pointer = f->f_stacktop;
1322 f->f_stacktop = NULL;
1323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001324 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001326 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001330
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001331 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001332 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001333#ifdef DYNAMIC_EXECUTION_PROFILE
1334#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 dxpairs[lastopcode][opcode]++;
1336 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001337#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001339#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001340
Guido van Rossum96a42c81992-01-12 02:29:51 +00001341#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (lltrace) {
1345 if (HAS_ARG(opcode)) {
1346 printf("%d: %d, %d\n",
1347 f->f_lasti, opcode, oparg);
1348 }
1349 else {
1350 printf("%d: %d\n",
1351 f->f_lasti, opcode);
1352 }
1353 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001354#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 /* Main switch on opcode */
1357 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 /* BEWARE!
1362 It is essential that any operation that fails sets either
1363 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1364 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 TARGET(NOP)
1367 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 TARGET(LOAD_FAST) {
1370 PyObject *value = GETLOCAL(oparg);
1371 if (value == NULL) {
1372 format_exc_check_arg(PyExc_UnboundLocalError,
1373 UNBOUNDLOCAL_ERROR_MSG,
1374 PyTuple_GetItem(co->co_varnames, oparg));
1375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001377 Py_INCREF(value);
1378 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 }
1381
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001382 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001383 TARGET(LOAD_CONST) {
1384 PyObject *value = GETITEM(consts, oparg);
1385 Py_INCREF(value);
1386 PUSH(value);
1387 FAST_DISPATCH();
1388 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001389
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001390 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 TARGET(STORE_FAST) {
1392 PyObject *value = POP();
1393 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 TARGET(POP_TOP) {
1398 PyObject *value = POP();
1399 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 TARGET(ROT_TWO) {
1404 PyObject *top = TOP();
1405 PyObject *second = SECOND();
1406 SET_TOP(second);
1407 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 TARGET(ROT_THREE) {
1412 PyObject *top = TOP();
1413 PyObject *second = SECOND();
1414 PyObject *third = THIRD();
1415 SET_TOP(second);
1416 SET_SECOND(third);
1417 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 TARGET(DUP_TOP) {
1422 PyObject *top = TOP();
1423 Py_INCREF(top);
1424 PUSH(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_TWO) {
1429 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001431 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001433 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 SET_TOP(top);
1435 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001436 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001438
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 TARGET(UNARY_POSITIVE) {
1440 PyObject *value = TOP();
1441 PyObject *res = PyNumber_Positive(value);
1442 Py_DECREF(value);
1443 SET_TOP(res);
1444 if (res == NULL)
1445 goto error;
1446 DISPATCH();
1447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 TARGET(UNARY_NEGATIVE) {
1450 PyObject *value = TOP();
1451 PyObject *res = PyNumber_Negative(value);
1452 Py_DECREF(value);
1453 SET_TOP(res);
1454 if (res == NULL)
1455 goto error;
1456 DISPATCH();
1457 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 TARGET(UNARY_NOT) {
1460 PyObject *value = TOP();
1461 int err = PyObject_IsTrue(value);
1462 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 if (err == 0) {
1464 Py_INCREF(Py_True);
1465 SET_TOP(Py_True);
1466 DISPATCH();
1467 }
1468 else if (err > 0) {
1469 Py_INCREF(Py_False);
1470 SET_TOP(Py_False);
1471 err = 0;
1472 DISPATCH();
1473 }
1474 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 goto error;
1476 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 TARGET(UNARY_INVERT) {
1479 PyObject *value = TOP();
1480 PyObject *res = PyNumber_Invert(value);
1481 Py_DECREF(value);
1482 SET_TOP(res);
1483 if (res == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 TARGET(BINARY_POWER) {
1489 PyObject *exp = POP();
1490 PyObject *base = TOP();
1491 PyObject *res = PyNumber_Power(base, exp, Py_None);
1492 Py_DECREF(base);
1493 Py_DECREF(exp);
1494 SET_TOP(res);
1495 if (res == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 TARGET(BINARY_MULTIPLY) {
1501 PyObject *right = POP();
1502 PyObject *left = TOP();
1503 PyObject *res = PyNumber_Multiply(left, right);
1504 Py_DECREF(left);
1505 Py_DECREF(right);
1506 SET_TOP(res);
1507 if (res == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Petersond51374e2014-04-09 23:55:56 -04001512 TARGET(BINARY_MATRIX_MULTIPLY) {
1513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_MatrixMultiply(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
1523
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 TARGET(BINARY_TRUE_DIVIDE) {
1525 PyObject *divisor = POP();
1526 PyObject *dividend = TOP();
1527 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1528 Py_DECREF(dividend);
1529 Py_DECREF(divisor);
1530 SET_TOP(quotient);
1531 if (quotient == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 TARGET(BINARY_FLOOR_DIVIDE) {
1537 PyObject *divisor = POP();
1538 PyObject *dividend = TOP();
1539 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1540 Py_DECREF(dividend);
1541 Py_DECREF(divisor);
1542 SET_TOP(quotient);
1543 if (quotient == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001547
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 TARGET(BINARY_MODULO) {
1549 PyObject *divisor = POP();
1550 PyObject *dividend = TOP();
1551 PyObject *res = PyUnicode_CheckExact(dividend) ?
1552 PyUnicode_Format(dividend, divisor) :
1553 PyNumber_Remainder(dividend, divisor);
1554 Py_DECREF(divisor);
1555 Py_DECREF(dividend);
1556 SET_TOP(res);
1557 if (res == NULL)
1558 goto error;
1559 DISPATCH();
1560 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 TARGET(BINARY_ADD) {
1563 PyObject *right = POP();
1564 PyObject *left = TOP();
1565 PyObject *sum;
1566 if (PyUnicode_CheckExact(left) &&
1567 PyUnicode_CheckExact(right)) {
1568 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001569 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001570 }
1571 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 sum = PyNumber_Add(left, right);
1573 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001574 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 Py_DECREF(right);
1576 SET_TOP(sum);
1577 if (sum == NULL)
1578 goto error;
1579 DISPATCH();
1580 }
1581
1582 TARGET(BINARY_SUBTRACT) {
1583 PyObject *right = POP();
1584 PyObject *left = TOP();
1585 PyObject *diff = PyNumber_Subtract(left, right);
1586 Py_DECREF(right);
1587 Py_DECREF(left);
1588 SET_TOP(diff);
1589 if (diff == NULL)
1590 goto error;
1591 DISPATCH();
1592 }
1593
1594 TARGET(BINARY_SUBSCR) {
1595 PyObject *sub = POP();
1596 PyObject *container = TOP();
1597 PyObject *res = PyObject_GetItem(container, sub);
1598 Py_DECREF(container);
1599 Py_DECREF(sub);
1600 SET_TOP(res);
1601 if (res == NULL)
1602 goto error;
1603 DISPATCH();
1604 }
1605
1606 TARGET(BINARY_LSHIFT) {
1607 PyObject *right = POP();
1608 PyObject *left = TOP();
1609 PyObject *res = PyNumber_Lshift(left, right);
1610 Py_DECREF(left);
1611 Py_DECREF(right);
1612 SET_TOP(res);
1613 if (res == NULL)
1614 goto error;
1615 DISPATCH();
1616 }
1617
1618 TARGET(BINARY_RSHIFT) {
1619 PyObject *right = POP();
1620 PyObject *left = TOP();
1621 PyObject *res = PyNumber_Rshift(left, right);
1622 Py_DECREF(left);
1623 Py_DECREF(right);
1624 SET_TOP(res);
1625 if (res == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
1629
1630 TARGET(BINARY_AND) {
1631 PyObject *right = POP();
1632 PyObject *left = TOP();
1633 PyObject *res = PyNumber_And(left, right);
1634 Py_DECREF(left);
1635 Py_DECREF(right);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
1641
1642 TARGET(BINARY_XOR) {
1643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_Xor(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
1653
1654 TARGET(BINARY_OR) {
1655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_Or(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
1665
1666 TARGET(LIST_APPEND) {
1667 PyObject *v = POP();
1668 PyObject *list = PEEK(oparg);
1669 int err;
1670 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 if (err != 0)
1673 goto error;
1674 PREDICT(JUMP_ABSOLUTE);
1675 DISPATCH();
1676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 TARGET(SET_ADD) {
1679 PyObject *v = POP();
1680 PyObject *set = stack_pointer[-oparg];
1681 int err;
1682 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 if (err != 0)
1685 goto error;
1686 PREDICT(JUMP_ABSOLUTE);
1687 DISPATCH();
1688 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 TARGET(INPLACE_POWER) {
1691 PyObject *exp = POP();
1692 PyObject *base = TOP();
1693 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1694 Py_DECREF(base);
1695 Py_DECREF(exp);
1696 SET_TOP(res);
1697 if (res == NULL)
1698 goto error;
1699 DISPATCH();
1700 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 TARGET(INPLACE_MULTIPLY) {
1703 PyObject *right = POP();
1704 PyObject *left = TOP();
1705 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1706 Py_DECREF(left);
1707 Py_DECREF(right);
1708 SET_TOP(res);
1709 if (res == NULL)
1710 goto error;
1711 DISPATCH();
1712 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Benjamin Petersond51374e2014-04-09 23:55:56 -04001714 TARGET(INPLACE_MATRIX_MULTIPLY) {
1715 PyObject *right = POP();
1716 PyObject *left = TOP();
1717 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1718 Py_DECREF(left);
1719 Py_DECREF(right);
1720 SET_TOP(res);
1721 if (res == NULL)
1722 goto error;
1723 DISPATCH();
1724 }
1725
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 TARGET(INPLACE_TRUE_DIVIDE) {
1727 PyObject *divisor = POP();
1728 PyObject *dividend = TOP();
1729 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1730 Py_DECREF(dividend);
1731 Py_DECREF(divisor);
1732 SET_TOP(quotient);
1733 if (quotient == NULL)
1734 goto error;
1735 DISPATCH();
1736 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001737
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 TARGET(INPLACE_FLOOR_DIVIDE) {
1739 PyObject *divisor = POP();
1740 PyObject *dividend = TOP();
1741 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1742 Py_DECREF(dividend);
1743 Py_DECREF(divisor);
1744 SET_TOP(quotient);
1745 if (quotient == NULL)
1746 goto error;
1747 DISPATCH();
1748 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001750 TARGET(INPLACE_MODULO) {
1751 PyObject *right = POP();
1752 PyObject *left = TOP();
1753 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1754 Py_DECREF(left);
1755 Py_DECREF(right);
1756 SET_TOP(mod);
1757 if (mod == NULL)
1758 goto error;
1759 DISPATCH();
1760 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001762 TARGET(INPLACE_ADD) {
1763 PyObject *right = POP();
1764 PyObject *left = TOP();
1765 PyObject *sum;
1766 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1767 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001768 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001769 }
1770 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 sum = PyNumber_InPlaceAdd(left, right);
1772 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001773 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 Py_DECREF(right);
1775 SET_TOP(sum);
1776 if (sum == NULL)
1777 goto error;
1778 DISPATCH();
1779 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 TARGET(INPLACE_SUBTRACT) {
1782 PyObject *right = POP();
1783 PyObject *left = TOP();
1784 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1785 Py_DECREF(left);
1786 Py_DECREF(right);
1787 SET_TOP(diff);
1788 if (diff == NULL)
1789 goto error;
1790 DISPATCH();
1791 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001793 TARGET(INPLACE_LSHIFT) {
1794 PyObject *right = POP();
1795 PyObject *left = TOP();
1796 PyObject *res = PyNumber_InPlaceLshift(left, right);
1797 Py_DECREF(left);
1798 Py_DECREF(right);
1799 SET_TOP(res);
1800 if (res == NULL)
1801 goto error;
1802 DISPATCH();
1803 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 TARGET(INPLACE_RSHIFT) {
1806 PyObject *right = POP();
1807 PyObject *left = TOP();
1808 PyObject *res = PyNumber_InPlaceRshift(left, right);
1809 Py_DECREF(left);
1810 Py_DECREF(right);
1811 SET_TOP(res);
1812 if (res == NULL)
1813 goto error;
1814 DISPATCH();
1815 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 TARGET(INPLACE_AND) {
1818 PyObject *right = POP();
1819 PyObject *left = TOP();
1820 PyObject *res = PyNumber_InPlaceAnd(left, right);
1821 Py_DECREF(left);
1822 Py_DECREF(right);
1823 SET_TOP(res);
1824 if (res == NULL)
1825 goto error;
1826 DISPATCH();
1827 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001829 TARGET(INPLACE_XOR) {
1830 PyObject *right = POP();
1831 PyObject *left = TOP();
1832 PyObject *res = PyNumber_InPlaceXor(left, right);
1833 Py_DECREF(left);
1834 Py_DECREF(right);
1835 SET_TOP(res);
1836 if (res == NULL)
1837 goto error;
1838 DISPATCH();
1839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 TARGET(INPLACE_OR) {
1842 PyObject *right = POP();
1843 PyObject *left = TOP();
1844 PyObject *res = PyNumber_InPlaceOr(left, right);
1845 Py_DECREF(left);
1846 Py_DECREF(right);
1847 SET_TOP(res);
1848 if (res == NULL)
1849 goto error;
1850 DISPATCH();
1851 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001852
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 TARGET(STORE_SUBSCR) {
1854 PyObject *sub = TOP();
1855 PyObject *container = SECOND();
1856 PyObject *v = THIRD();
1857 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001859 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 Py_DECREF(container);
1863 Py_DECREF(sub);
1864 if (err != 0)
1865 goto error;
1866 DISPATCH();
1867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001869 TARGET(STORE_ANNOTATION) {
1870 _Py_IDENTIFIER(__annotations__);
1871 PyObject *ann_dict;
1872 PyObject *ann = POP();
1873 PyObject *name = GETITEM(names, oparg);
1874 int err;
1875 if (f->f_locals == NULL) {
1876 PyErr_Format(PyExc_SystemError,
1877 "no locals found when storing annotation");
1878 Py_DECREF(ann);
1879 goto error;
1880 }
1881 /* first try to get __annotations__ from locals... */
1882 if (PyDict_CheckExact(f->f_locals)) {
1883 ann_dict = _PyDict_GetItemId(f->f_locals,
1884 &PyId___annotations__);
1885 if (ann_dict == NULL) {
1886 PyErr_SetString(PyExc_NameError,
1887 "__annotations__ not found");
1888 Py_DECREF(ann);
1889 goto error;
1890 }
1891 Py_INCREF(ann_dict);
1892 }
1893 else {
1894 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1895 if (ann_str == NULL) {
1896 Py_DECREF(ann);
1897 goto error;
1898 }
1899 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1900 if (ann_dict == NULL) {
1901 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1902 PyErr_SetString(PyExc_NameError,
1903 "__annotations__ not found");
1904 }
1905 Py_DECREF(ann);
1906 goto error;
1907 }
1908 }
1909 /* ...if succeeded, __annotations__[name] = ann */
1910 if (PyDict_CheckExact(ann_dict)) {
1911 err = PyDict_SetItem(ann_dict, name, ann);
1912 }
1913 else {
1914 err = PyObject_SetItem(ann_dict, name, ann);
1915 }
1916 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001917 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001918 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001919 goto error;
1920 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001921 DISPATCH();
1922 }
1923
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001924 TARGET(DELETE_SUBSCR) {
1925 PyObject *sub = TOP();
1926 PyObject *container = SECOND();
1927 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001929 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 err = PyObject_DelItem(container, sub);
1931 Py_DECREF(container);
1932 Py_DECREF(sub);
1933 if (err != 0)
1934 goto error;
1935 DISPATCH();
1936 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001937
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001939 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001941 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001942 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 PyErr_SetString(PyExc_RuntimeError,
1945 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 Py_DECREF(value);
1947 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001949 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 Py_DECREF(value);
1951 if (res == NULL)
1952 goto error;
1953 Py_DECREF(res);
1954 DISPATCH();
1955 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001956
Thomas Wouters434d0822000-08-24 20:11:32 +00001957#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001959#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001960 TARGET(RAISE_VARARGS) {
1961 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 switch (oparg) {
1963 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 if (do_raise(exc, cause)) {
1969 why = WHY_EXCEPTION;
1970 goto fast_block_end;
1971 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 break;
1973 default:
1974 PyErr_SetString(PyExc_SystemError,
1975 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001976 break;
1977 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 goto error;
1979 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001980
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 retval = POP();
1983 why = WHY_RETURN;
1984 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001986
Yury Selivanov75445082015-05-11 22:57:16 -04001987 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001988 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001989 PyObject *iter = NULL;
1990 PyObject *awaitable = NULL;
1991 PyObject *obj = TOP();
1992 PyTypeObject *type = Py_TYPE(obj);
1993
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001994 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001995 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001996 }
Yury Selivanov75445082015-05-11 22:57:16 -04001997
1998 if (getter != NULL) {
1999 iter = (*getter)(obj);
2000 Py_DECREF(obj);
2001 if (iter == NULL) {
2002 SET_TOP(NULL);
2003 goto error;
2004 }
2005 }
2006 else {
2007 SET_TOP(NULL);
2008 PyErr_Format(
2009 PyExc_TypeError,
2010 "'async for' requires an object with "
2011 "__aiter__ method, got %.100s",
2012 type->tp_name);
2013 Py_DECREF(obj);
2014 goto error;
2015 }
2016
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002017 if (Py_TYPE(iter)->tp_as_async != NULL &&
2018 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
2019
2020 /* Starting with CPython 3.5.2 __aiter__ should return
2021 asynchronous iterators directly (not awaitables that
2022 resolve to asynchronous iterators.)
2023
2024 Therefore, we check if the object that was returned
2025 from __aiter__ has an __anext__ method. If it does,
2026 we wrap it in an awaitable that resolves to `iter`.
2027
2028 See http://bugs.python.org/issue27243 for more
2029 details.
2030 */
2031
2032 PyObject *wrapper = _PyAIterWrapper_New(iter);
2033 Py_DECREF(iter);
2034 SET_TOP(wrapper);
2035 DISPATCH();
2036 }
2037
Yury Selivanov5376ba92015-06-22 12:19:30 -04002038 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002039 if (awaitable == NULL) {
2040 SET_TOP(NULL);
2041 PyErr_Format(
2042 PyExc_TypeError,
2043 "'async for' received an invalid object "
2044 "from __aiter__: %.100s",
2045 Py_TYPE(iter)->tp_name);
2046
2047 Py_DECREF(iter);
2048 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002049 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04002050 Py_DECREF(iter);
2051
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002052 if (PyErr_WarnFormat(
2053 PyExc_PendingDeprecationWarning, 1,
2054 "'%.100s' implements legacy __aiter__ protocol; "
2055 "__aiter__ should return an asynchronous "
2056 "iterator, not awaitable",
2057 type->tp_name))
2058 {
2059 /* Warning was converted to an error. */
2060 Py_DECREF(awaitable);
2061 SET_TOP(NULL);
2062 goto error;
2063 }
2064 }
2065
Yury Selivanov75445082015-05-11 22:57:16 -04002066 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002067 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002068 DISPATCH();
2069 }
2070
2071 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002072 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002073 PyObject *next_iter = NULL;
2074 PyObject *awaitable = NULL;
2075 PyObject *aiter = TOP();
2076 PyTypeObject *type = Py_TYPE(aiter);
2077
Yury Selivanoveb636452016-09-08 22:01:51 -07002078 if (PyAsyncGen_CheckExact(aiter)) {
2079 awaitable = type->tp_as_async->am_anext(aiter);
2080 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002081 goto error;
2082 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002083 } else {
2084 if (type->tp_as_async != NULL){
2085 getter = type->tp_as_async->am_anext;
2086 }
Yury Selivanov75445082015-05-11 22:57:16 -04002087
Yury Selivanoveb636452016-09-08 22:01:51 -07002088 if (getter != NULL) {
2089 next_iter = (*getter)(aiter);
2090 if (next_iter == NULL) {
2091 goto error;
2092 }
2093 }
2094 else {
2095 PyErr_Format(
2096 PyExc_TypeError,
2097 "'async for' requires an iterator with "
2098 "__anext__ method, got %.100s",
2099 type->tp_name);
2100 goto error;
2101 }
Yury Selivanov75445082015-05-11 22:57:16 -04002102
Yury Selivanoveb636452016-09-08 22:01:51 -07002103 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2104 if (awaitable == NULL) {
2105 PyErr_Format(
2106 PyExc_TypeError,
2107 "'async for' received an invalid object "
2108 "from __anext__: %.100s",
2109 Py_TYPE(next_iter)->tp_name);
2110
2111 Py_DECREF(next_iter);
2112 goto error;
2113 } else {
2114 Py_DECREF(next_iter);
2115 }
2116 }
Yury Selivanov75445082015-05-11 22:57:16 -04002117
2118 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002119 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002120 DISPATCH();
2121 }
2122
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002123 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002124 TARGET(GET_AWAITABLE) {
2125 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002126 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002127
2128 Py_DECREF(iterable);
2129
Yury Selivanovc724bae2016-03-02 11:30:46 -05002130 if (iter != NULL && PyCoro_CheckExact(iter)) {
2131 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2132 if (yf != NULL) {
2133 /* `iter` is a coroutine object that is being
2134 awaited, `yf` is a pointer to the current awaitable
2135 being awaited on. */
2136 Py_DECREF(yf);
2137 Py_CLEAR(iter);
2138 PyErr_SetString(
2139 PyExc_RuntimeError,
2140 "coroutine is being awaited already");
2141 /* The code below jumps to `error` if `iter` is NULL. */
2142 }
2143 }
2144
Yury Selivanov75445082015-05-11 22:57:16 -04002145 SET_TOP(iter); /* Even if it's NULL */
2146
2147 if (iter == NULL) {
2148 goto error;
2149 }
2150
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002151 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002152 DISPATCH();
2153 }
2154
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 TARGET(YIELD_FROM) {
2156 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002157 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002159 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
2160 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002161 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002162 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002164 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002165 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002166 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 Py_DECREF(v);
2169 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002170 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002171 if (tstate->c_tracefunc != NULL
2172 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002173 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002174 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 if (err < 0)
2176 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002177 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 SET_TOP(val);
2179 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002180 }
Martin Panter95f53c12016-07-18 08:23:26 +00002181 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002182 f->f_stacktop = stack_pointer;
2183 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002184 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002185 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002186 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002188
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002191
2192 if (co->co_flags & CO_ASYNC_GENERATOR) {
2193 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2194 Py_DECREF(retval);
2195 if (w == NULL) {
2196 retval = NULL;
2197 goto error;
2198 }
2199 retval = w;
2200 }
2201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 f->f_stacktop = stack_pointer;
2203 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002206
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 TARGET(POP_EXCEPT) {
2208 PyTryBlock *b = PyFrame_BlockPop(f);
2209 if (b->b_type != EXCEPT_HANDLER) {
2210 PyErr_SetString(PyExc_SystemError,
2211 "popped block is not an except handler");
2212 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002217
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002218 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 TARGET(POP_BLOCK) {
2220 PyTryBlock *b = PyFrame_BlockPop(f);
2221 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002225 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 TARGET(END_FINALLY) {
2227 PyObject *status = POP();
2228 if (PyLong_Check(status)) {
2229 why = (enum why_code) PyLong_AS_LONG(status);
2230 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 if (why == WHY_RETURN ||
2232 why == WHY_CONTINUE)
2233 retval = POP();
2234 if (why == WHY_SILENCED) {
2235 /* An exception was silenced by 'with', we must
2236 manually unwind the EXCEPT_HANDLER block which was
2237 created when the exception was caught, otherwise
2238 the stack will be in an inconsistent state. */
2239 PyTryBlock *b = PyFrame_BlockPop(f);
2240 assert(b->b_type == EXCEPT_HANDLER);
2241 UNWIND_EXCEPT_HANDLER(b);
2242 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 Py_DECREF(status);
2244 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 Py_DECREF(status);
2247 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 else if (PyExceptionClass_Check(status)) {
2250 PyObject *exc = POP();
2251 PyObject *tb = POP();
2252 PyErr_Restore(status, exc, tb);
2253 why = WHY_EXCEPTION;
2254 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 PyErr_SetString(PyExc_SystemError,
2258 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 Py_DECREF(status);
2260 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002261 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 Py_DECREF(status);
2263 DISPATCH();
2264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002265
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002266 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002267 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002268
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002270 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2272 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002273 PyErr_SetString(PyExc_NameError,
2274 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002276 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002278 }
2279 else {
2280 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2281 if (build_class_str == NULL)
2282 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2284 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002285 if (PyErr_ExceptionMatches(PyExc_KeyError))
2286 PyErr_SetString(PyExc_NameError,
2287 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002292 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002293 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002294
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 TARGET(STORE_NAME) {
2296 PyObject *name = GETITEM(names, oparg);
2297 PyObject *v = POP();
2298 PyObject *ns = f->f_locals;
2299 int err;
2300 if (ns == NULL) {
2301 PyErr_Format(PyExc_SystemError,
2302 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 if (PyDict_CheckExact(ns))
2307 err = PyDict_SetItem(ns, name, v);
2308 else
2309 err = PyObject_SetItem(ns, name, v);
2310 Py_DECREF(v);
2311 if (err != 0)
2312 goto error;
2313 DISPATCH();
2314 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002315
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 TARGET(DELETE_NAME) {
2317 PyObject *name = GETITEM(names, oparg);
2318 PyObject *ns = f->f_locals;
2319 int err;
2320 if (ns == NULL) {
2321 PyErr_Format(PyExc_SystemError,
2322 "no locals when deleting %R", name);
2323 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 err = PyObject_DelItem(ns, name);
2326 if (err != 0) {
2327 format_exc_check_arg(PyExc_NameError,
2328 NAME_ERROR_MSG,
2329 name);
2330 goto error;
2331 }
2332 DISPATCH();
2333 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002334
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002335 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 TARGET(UNPACK_SEQUENCE) {
2337 PyObject *seq = POP(), *item, **items;
2338 if (PyTuple_CheckExact(seq) &&
2339 PyTuple_GET_SIZE(seq) == oparg) {
2340 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 item = items[oparg];
2343 Py_INCREF(item);
2344 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002345 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 } else if (PyList_CheckExact(seq) &&
2347 PyList_GET_SIZE(seq) == oparg) {
2348 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002349 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002350 item = items[oparg];
2351 Py_INCREF(item);
2352 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 stack_pointer + oparg)) {
2356 STACKADJ(oparg);
2357 } else {
2358 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 Py_DECREF(seq);
2360 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002363 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002365
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 TARGET(UNPACK_EX) {
2367 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2368 PyObject *seq = POP();
2369
2370 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2371 stack_pointer + totalargs)) {
2372 stack_pointer += totalargs;
2373 } else {
2374 Py_DECREF(seq);
2375 goto error;
2376 }
2377 Py_DECREF(seq);
2378 DISPATCH();
2379 }
2380
2381 TARGET(STORE_ATTR) {
2382 PyObject *name = GETITEM(names, oparg);
2383 PyObject *owner = TOP();
2384 PyObject *v = SECOND();
2385 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 Py_DECREF(owner);
2390 if (err != 0)
2391 goto error;
2392 DISPATCH();
2393 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002394
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 TARGET(DELETE_ATTR) {
2396 PyObject *name = GETITEM(names, oparg);
2397 PyObject *owner = POP();
2398 int err;
2399 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2400 Py_DECREF(owner);
2401 if (err != 0)
2402 goto error;
2403 DISPATCH();
2404 }
2405
2406 TARGET(STORE_GLOBAL) {
2407 PyObject *name = GETITEM(names, oparg);
2408 PyObject *v = POP();
2409 int err;
2410 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002412 if (err != 0)
2413 goto error;
2414 DISPATCH();
2415 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002416
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 TARGET(DELETE_GLOBAL) {
2418 PyObject *name = GETITEM(names, oparg);
2419 int err;
2420 err = PyDict_DelItem(f->f_globals, name);
2421 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002423 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002425 }
2426 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002428
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 TARGET(LOAD_NAME) {
2430 PyObject *name = GETITEM(names, oparg);
2431 PyObject *locals = f->f_locals;
2432 PyObject *v;
2433 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 "no locals when loading %R", name);
2436 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 if (PyDict_CheckExact(locals)) {
2439 v = PyDict_GetItem(locals, name);
2440 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 }
2442 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002444 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002445 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2446 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 PyErr_Clear();
2448 }
2449 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 if (v == NULL) {
2451 v = PyDict_GetItem(f->f_globals, name);
2452 Py_XINCREF(v);
2453 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002454 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 v = PyDict_GetItem(f->f_builtins, name);
2456 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002457 format_exc_check_arg(
2458 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 NAME_ERROR_MSG, name);
2460 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002461 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002462 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002463 }
2464 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002465 v = PyObject_GetItem(f->f_builtins, name);
2466 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002467 if (PyErr_ExceptionMatches(PyExc_KeyError))
2468 format_exc_check_arg(
2469 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 NAME_ERROR_MSG, name);
2471 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002472 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002473 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002479
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002480 TARGET(LOAD_GLOBAL) {
2481 PyObject *name = GETITEM(names, oparg);
2482 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002483 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002484 && PyDict_CheckExact(f->f_builtins))
2485 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002486 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002487 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002488 name);
2489 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002490 if (!_PyErr_OCCURRED()) {
2491 /* _PyDict_LoadGlobal() returns NULL without raising
2492 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002493 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002494 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002495 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002500 else {
2501 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002502
2503 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 v = PyObject_GetItem(f->f_globals, name);
2505 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002506 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2507 goto error;
2508 PyErr_Clear();
2509
Victor Stinnerb4efc962015-11-20 09:24:02 +01002510 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 v = PyObject_GetItem(f->f_builtins, name);
2512 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002513 if (PyErr_ExceptionMatches(PyExc_KeyError))
2514 format_exc_check_arg(
2515 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002516 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002518 }
2519 }
2520 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002524
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 TARGET(DELETE_FAST) {
2526 PyObject *v = GETLOCAL(oparg);
2527 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 SETLOCAL(oparg, NULL);
2529 DISPATCH();
2530 }
2531 format_exc_check_arg(
2532 PyExc_UnboundLocalError,
2533 UNBOUNDLOCAL_ERROR_MSG,
2534 PyTuple_GetItem(co->co_varnames, oparg)
2535 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 goto error;
2537 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002538
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 TARGET(DELETE_DEREF) {
2540 PyObject *cell = freevars[oparg];
2541 if (PyCell_GET(cell) != NULL) {
2542 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002543 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002544 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002545 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002546 goto error;
2547 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002548
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002549 TARGET(LOAD_CLOSURE) {
2550 PyObject *cell = freevars[oparg];
2551 Py_INCREF(cell);
2552 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002555
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002556 TARGET(LOAD_CLASSDEREF) {
2557 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002558 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002559 assert(locals);
2560 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2561 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2562 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2563 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2564 if (PyDict_CheckExact(locals)) {
2565 value = PyDict_GetItem(locals, name);
2566 Py_XINCREF(value);
2567 }
2568 else {
2569 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002570 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002571 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2572 goto error;
2573 PyErr_Clear();
2574 }
2575 }
2576 if (!value) {
2577 PyObject *cell = freevars[oparg];
2578 value = PyCell_GET(cell);
2579 if (value == NULL) {
2580 format_exc_unbound(co, oparg);
2581 goto error;
2582 }
2583 Py_INCREF(value);
2584 }
2585 PUSH(value);
2586 DISPATCH();
2587 }
2588
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 TARGET(LOAD_DEREF) {
2590 PyObject *cell = freevars[oparg];
2591 PyObject *value = PyCell_GET(cell);
2592 if (value == NULL) {
2593 format_exc_unbound(co, oparg);
2594 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 Py_INCREF(value);
2597 PUSH(value);
2598 DISPATCH();
2599 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002600
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 TARGET(STORE_DEREF) {
2602 PyObject *v = POP();
2603 PyObject *cell = freevars[oparg];
2604 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002605 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 DISPATCH();
2607 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002608
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002609 TARGET(BUILD_STRING) {
2610 PyObject *str;
2611 PyObject *empty = PyUnicode_New(0, 0);
2612 if (empty == NULL) {
2613 goto error;
2614 }
2615 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2616 Py_DECREF(empty);
2617 if (str == NULL)
2618 goto error;
2619 while (--oparg >= 0) {
2620 PyObject *item = POP();
2621 Py_DECREF(item);
2622 }
2623 PUSH(str);
2624 DISPATCH();
2625 }
2626
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002627 TARGET(BUILD_TUPLE) {
2628 PyObject *tup = PyTuple_New(oparg);
2629 if (tup == NULL)
2630 goto error;
2631 while (--oparg >= 0) {
2632 PyObject *item = POP();
2633 PyTuple_SET_ITEM(tup, oparg, item);
2634 }
2635 PUSH(tup);
2636 DISPATCH();
2637 }
2638
2639 TARGET(BUILD_LIST) {
2640 PyObject *list = PyList_New(oparg);
2641 if (list == NULL)
2642 goto error;
2643 while (--oparg >= 0) {
2644 PyObject *item = POP();
2645 PyList_SET_ITEM(list, oparg, item);
2646 }
2647 PUSH(list);
2648 DISPATCH();
2649 }
2650
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002651 TARGET(BUILD_TUPLE_UNPACK)
2652 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002653 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002654 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002655 PyObject *sum;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002656 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002657
2658 if (convert_to_tuple && oparg == 1 && PyTuple_CheckExact(TOP())) {
2659 DISPATCH();
2660 }
2661
2662 sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002663 if (sum == NULL)
2664 goto error;
2665
2666 for (i = oparg; i > 0; i--) {
2667 PyObject *none_val;
2668
2669 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2670 if (none_val == NULL) {
2671 Py_DECREF(sum);
2672 goto error;
2673 }
2674 Py_DECREF(none_val);
2675 }
2676
2677 if (convert_to_tuple) {
2678 return_value = PyList_AsTuple(sum);
2679 Py_DECREF(sum);
2680 if (return_value == NULL)
2681 goto error;
2682 }
2683 else {
2684 return_value = sum;
2685 }
2686
2687 while (oparg--)
2688 Py_DECREF(POP());
2689 PUSH(return_value);
2690 DISPATCH();
2691 }
2692
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 TARGET(BUILD_SET) {
2694 PyObject *set = PySet_New(NULL);
2695 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002696 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 if (set == NULL)
2698 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002699 for (i = oparg; i > 0; i--) {
2700 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 if (err == 0)
2702 err = PySet_Add(set, item);
2703 Py_DECREF(item);
2704 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002705 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002706 if (err != 0) {
2707 Py_DECREF(set);
2708 goto error;
2709 }
2710 PUSH(set);
2711 DISPATCH();
2712 }
2713
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002714 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002715 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002716 PyObject *sum = PySet_New(NULL);
2717 if (sum == NULL)
2718 goto error;
2719
2720 for (i = oparg; i > 0; i--) {
2721 if (_PySet_Update(sum, PEEK(i)) < 0) {
2722 Py_DECREF(sum);
2723 goto error;
2724 }
2725 }
2726
2727 while (oparg--)
2728 Py_DECREF(POP());
2729 PUSH(sum);
2730 DISPATCH();
2731 }
2732
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002733 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002734 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2736 if (map == NULL)
2737 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002738 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002739 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002740 PyObject *key = PEEK(2*i);
2741 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002742 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002743 if (err != 0) {
2744 Py_DECREF(map);
2745 goto error;
2746 }
2747 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002748
2749 while (oparg--) {
2750 Py_DECREF(POP());
2751 Py_DECREF(POP());
2752 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 PUSH(map);
2754 DISPATCH();
2755 }
2756
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002757 TARGET(SETUP_ANNOTATIONS) {
2758 _Py_IDENTIFIER(__annotations__);
2759 int err;
2760 PyObject *ann_dict;
2761 if (f->f_locals == NULL) {
2762 PyErr_Format(PyExc_SystemError,
2763 "no locals found when setting up annotations");
2764 goto error;
2765 }
2766 /* check if __annotations__ in locals()... */
2767 if (PyDict_CheckExact(f->f_locals)) {
2768 ann_dict = _PyDict_GetItemId(f->f_locals,
2769 &PyId___annotations__);
2770 if (ann_dict == NULL) {
2771 /* ...if not, create a new one */
2772 ann_dict = PyDict_New();
2773 if (ann_dict == NULL) {
2774 goto error;
2775 }
2776 err = _PyDict_SetItemId(f->f_locals,
2777 &PyId___annotations__, ann_dict);
2778 Py_DECREF(ann_dict);
2779 if (err != 0) {
2780 goto error;
2781 }
2782 }
2783 }
2784 else {
2785 /* do the same if locals() is not a dict */
2786 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2787 if (ann_str == NULL) {
2788 break;
2789 }
2790 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2791 if (ann_dict == NULL) {
2792 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2793 goto error;
2794 }
2795 PyErr_Clear();
2796 ann_dict = PyDict_New();
2797 if (ann_dict == NULL) {
2798 goto error;
2799 }
2800 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2801 Py_DECREF(ann_dict);
2802 if (err != 0) {
2803 goto error;
2804 }
2805 }
2806 else {
2807 Py_DECREF(ann_dict);
2808 }
2809 }
2810 DISPATCH();
2811 }
2812
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002813 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002814 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002815 PyObject *map;
2816 PyObject *keys = TOP();
2817 if (!PyTuple_CheckExact(keys) ||
2818 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2819 PyErr_SetString(PyExc_SystemError,
2820 "bad BUILD_CONST_KEY_MAP keys argument");
2821 goto error;
2822 }
2823 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2824 if (map == NULL) {
2825 goto error;
2826 }
2827 for (i = oparg; i > 0; i--) {
2828 int err;
2829 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2830 PyObject *value = PEEK(i + 1);
2831 err = PyDict_SetItem(map, key, value);
2832 if (err != 0) {
2833 Py_DECREF(map);
2834 goto error;
2835 }
2836 }
2837
2838 Py_DECREF(POP());
2839 while (oparg--) {
2840 Py_DECREF(POP());
2841 }
2842 PUSH(map);
2843 DISPATCH();
2844 }
2845
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002846 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2847 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002848 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
Victor Stinner74319ae2016-08-25 00:04:09 +02002849 Py_ssize_t i;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002850 PyObject *sum;
2851
2852 if (with_call && oparg == 1 && PyDict_CheckExact(TOP())) {
2853 DISPATCH();
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002854 }
2855
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002856 sum = PyDict_New();
2857 if (sum == NULL)
2858 goto error;
2859
2860 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002861 PyObject *arg = PEEK(i);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002862 if (with_call && PyDict_Size(sum)) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002863 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2864
2865 if (intersection == NULL) {
2866 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002867 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002868 PyErr_Format(PyExc_TypeError,
2869 "%.200s%.200s argument after ** "
2870 "must be a mapping, not %.200s",
2871 PyEval_GetFuncName(func),
2872 PyEval_GetFuncDesc(func),
2873 arg->ob_type->tp_name);
2874 }
2875 Py_DECREF(sum);
2876 goto error;
2877 }
2878
2879 if (PySet_GET_SIZE(intersection)) {
2880 Py_ssize_t idx = 0;
2881 PyObject *key;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002882 PyObject *func = PEEK(2 + oparg);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002883 Py_hash_t hash;
2884 _PySet_NextEntry(intersection, &idx, &key, &hash);
2885 if (!PyUnicode_Check(key)) {
2886 PyErr_Format(PyExc_TypeError,
2887 "%.200s%.200s keywords must be strings",
2888 PyEval_GetFuncName(func),
2889 PyEval_GetFuncDesc(func));
2890 } else {
2891 PyErr_Format(PyExc_TypeError,
2892 "%.200s%.200s got multiple "
2893 "values for keyword argument '%U'",
2894 PyEval_GetFuncName(func),
2895 PyEval_GetFuncDesc(func),
2896 key);
2897 }
2898 Py_DECREF(intersection);
2899 Py_DECREF(sum);
2900 goto error;
2901 }
2902 Py_DECREF(intersection);
2903 }
2904
2905 if (PyDict_Update(sum, arg) < 0) {
2906 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2907 PyErr_Format(PyExc_TypeError,
2908 "'%.200s' object is not a mapping",
2909 arg->ob_type->tp_name);
2910 }
2911 Py_DECREF(sum);
2912 goto error;
2913 }
2914 }
2915
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002916 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002917 Py_DECREF(POP());
2918 PUSH(sum);
2919 DISPATCH();
2920 }
2921
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 TARGET(MAP_ADD) {
2923 PyObject *key = TOP();
2924 PyObject *value = SECOND();
2925 PyObject *map;
2926 int err;
2927 STACKADJ(-2);
2928 map = stack_pointer[-oparg]; /* dict */
2929 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002930 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 Py_DECREF(value);
2932 Py_DECREF(key);
2933 if (err != 0)
2934 goto error;
2935 PREDICT(JUMP_ABSOLUTE);
2936 DISPATCH();
2937 }
2938
2939 TARGET(LOAD_ATTR) {
2940 PyObject *name = GETITEM(names, oparg);
2941 PyObject *owner = TOP();
2942 PyObject *res = PyObject_GetAttr(owner, name);
2943 Py_DECREF(owner);
2944 SET_TOP(res);
2945 if (res == NULL)
2946 goto error;
2947 DISPATCH();
2948 }
2949
2950 TARGET(COMPARE_OP) {
2951 PyObject *right = POP();
2952 PyObject *left = TOP();
2953 PyObject *res = cmp_outcome(oparg, left, right);
2954 Py_DECREF(left);
2955 Py_DECREF(right);
2956 SET_TOP(res);
2957 if (res == NULL)
2958 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 PREDICT(POP_JUMP_IF_FALSE);
2960 PREDICT(POP_JUMP_IF_TRUE);
2961 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002962 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002963
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002965 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002966 PyObject *fromlist = POP();
2967 PyObject *level = TOP();
2968 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 READ_TIMESTAMP(intr0);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002970 res = import_name(f, name, fromlist, level);
2971 Py_DECREF(level);
2972 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 SET_TOP(res);
2975 if (res == NULL)
2976 goto error;
2977 DISPATCH();
2978 }
2979
2980 TARGET(IMPORT_STAR) {
2981 PyObject *from = POP(), *locals;
2982 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002983 if (PyFrame_FastToLocalsWithError(f) < 0)
2984 goto error;
2985
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 locals = f->f_locals;
2987 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 PyErr_SetString(PyExc_SystemError,
2989 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 }
2992 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 READ_TIMESTAMP(intr1);
2995 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 Py_DECREF(from);
2997 if (err != 0)
2998 goto error;
2999 DISPATCH();
3000 }
Guido van Rossum25831651993-05-19 14:50:45 +00003001
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003002 TARGET(IMPORT_FROM) {
3003 PyObject *name = GETITEM(names, oparg);
3004 PyObject *from = TOP();
3005 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 PUSH(res);
3010 if (res == NULL)
3011 goto error;
3012 DISPATCH();
3013 }
Thomas Wouters52152252000-08-17 22:55:00 +00003014
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 JUMPBY(oparg);
3017 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003019
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003020 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 TARGET(POP_JUMP_IF_FALSE) {
3022 PyObject *cond = POP();
3023 int err;
3024 if (cond == Py_True) {
3025 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 FAST_DISPATCH();
3027 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 if (cond == Py_False) {
3029 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 JUMPTO(oparg);
3031 FAST_DISPATCH();
3032 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003033 err = PyObject_IsTrue(cond);
3034 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 if (err > 0)
3036 err = 0;
3037 else if (err == 0)
3038 JUMPTO(oparg);
3039 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003042 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003043
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003044 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 TARGET(POP_JUMP_IF_TRUE) {
3046 PyObject *cond = POP();
3047 int err;
3048 if (cond == Py_False) {
3049 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 FAST_DISPATCH();
3051 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 if (cond == Py_True) {
3053 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 JUMPTO(oparg);
3055 FAST_DISPATCH();
3056 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003057 err = PyObject_IsTrue(cond);
3058 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 if (err > 0) {
3060 err = 0;
3061 JUMPTO(oparg);
3062 }
3063 else if (err == 0)
3064 ;
3065 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003066 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003069
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 TARGET(JUMP_IF_FALSE_OR_POP) {
3071 PyObject *cond = TOP();
3072 int err;
3073 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 FAST_DISPATCH();
3077 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003078 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 JUMPTO(oparg);
3080 FAST_DISPATCH();
3081 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 if (err > 0) {
3084 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 err = 0;
3087 }
3088 else if (err == 0)
3089 JUMPTO(oparg);
3090 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003093 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003094
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 TARGET(JUMP_IF_TRUE_OR_POP) {
3096 PyObject *cond = TOP();
3097 int err;
3098 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003099 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 FAST_DISPATCH();
3102 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003103 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003104 JUMPTO(oparg);
3105 FAST_DISPATCH();
3106 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003107 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 if (err > 0) {
3109 err = 0;
3110 JUMPTO(oparg);
3111 }
3112 else if (err == 0) {
3113 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 }
3116 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003120
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003121 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003124#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125 /* Enabling this path speeds-up all while and for-loops by bypassing
3126 the per-loop checks for signals. By default, this should be turned-off
3127 because it prevents detection of a control-break in tight loops like
3128 "while 1: pass". Compile with this option turned-on when you need
3129 the speed-up and do not need break checking inside tight loops (ones
3130 that contain only instructions ending with FAST_DISPATCH).
3131 */
3132 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003133#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003135#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003137
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003140 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003141 PyObject *iter = PyObject_GetIter(iterable);
3142 Py_DECREF(iterable);
3143 SET_TOP(iter);
3144 if (iter == NULL)
3145 goto error;
3146 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003147 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003148 DISPATCH();
3149 }
3150
3151 TARGET(GET_YIELD_FROM_ITER) {
3152 /* before: [obj]; after [getiter(obj)] */
3153 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003154 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003155 if (PyCoro_CheckExact(iterable)) {
3156 /* `iterable` is a coroutine */
3157 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3158 /* and it is used in a 'yield from' expression of a
3159 regular generator. */
3160 Py_DECREF(iterable);
3161 SET_TOP(NULL);
3162 PyErr_SetString(PyExc_TypeError,
3163 "cannot 'yield from' a coroutine object "
3164 "in a non-coroutine generator");
3165 goto error;
3166 }
3167 }
3168 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003169 /* `iterable` is not a generator. */
3170 iter = PyObject_GetIter(iterable);
3171 Py_DECREF(iterable);
3172 SET_TOP(iter);
3173 if (iter == NULL)
3174 goto error;
3175 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003176 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 DISPATCH();
3178 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003179
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003180 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 PyObject *iter = TOP();
3184 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3185 if (next != NULL) {
3186 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 PREDICT(STORE_FAST);
3188 PREDICT(UNPACK_SEQUENCE);
3189 DISPATCH();
3190 }
3191 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3193 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003194 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003195 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 PyErr_Clear();
3197 }
3198 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 STACKADJ(-1);
3200 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003202 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003205
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 why = WHY_BREAK;
3208 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003210
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 if (retval == NULL)
3214 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 why = WHY_CONTINUE;
3216 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003218
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003219 TARGET(SETUP_LOOP)
3220 TARGET(SETUP_EXCEPT)
3221 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 /* NOTE: If you add any new block-setup opcodes that
3223 are not try/except/finally handlers, you may need
3224 to update the PyGen_NeedsFinalizing() function.
3225 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3228 STACK_LEVEL());
3229 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003230 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003231
Yury Selivanov75445082015-05-11 22:57:16 -04003232 TARGET(BEFORE_ASYNC_WITH) {
3233 _Py_IDENTIFIER(__aexit__);
3234 _Py_IDENTIFIER(__aenter__);
3235
3236 PyObject *mgr = TOP();
3237 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3238 *enter;
3239 PyObject *res;
3240 if (exit == NULL)
3241 goto error;
3242 SET_TOP(exit);
3243 enter = special_lookup(mgr, &PyId___aenter__);
3244 Py_DECREF(mgr);
3245 if (enter == NULL)
3246 goto error;
3247 res = PyObject_CallFunctionObjArgs(enter, NULL);
3248 Py_DECREF(enter);
3249 if (res == NULL)
3250 goto error;
3251 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003252 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003253 DISPATCH();
3254 }
3255
3256 TARGET(SETUP_ASYNC_WITH) {
3257 PyObject *res = POP();
3258 /* Setup the finally block before pushing the result
3259 of __aenter__ on the stack. */
3260 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3261 STACK_LEVEL());
3262 PUSH(res);
3263 DISPATCH();
3264 }
3265
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003266 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003267 _Py_IDENTIFIER(__exit__);
3268 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 PyObject *mgr = TOP();
3270 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3271 PyObject *res;
3272 if (exit == NULL)
3273 goto error;
3274 SET_TOP(exit);
3275 enter = special_lookup(mgr, &PyId___enter__);
3276 Py_DECREF(mgr);
3277 if (enter == NULL)
3278 goto error;
3279 res = PyObject_CallFunctionObjArgs(enter, NULL);
3280 Py_DECREF(enter);
3281 if (res == NULL)
3282 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 /* Setup the finally block before pushing the result
3284 of __enter__ on the stack. */
3285 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3286 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003287
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 DISPATCH();
3290 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003291
Yury Selivanov75445082015-05-11 22:57:16 -04003292 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003293 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 how/why we entered the finally clause:
3295 - TOP = None
3296 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3297 - TOP = WHY_*; no retval below it
3298 - (TOP, SECOND, THIRD) = exc_info()
3299 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3300 Below them is EXIT, the context.__exit__ bound method.
3301 In the last case, we must call
3302 EXIT(TOP, SECOND, THIRD)
3303 otherwise we must call
3304 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003305
Benjamin Peterson8f169482013-10-29 22:25:06 -04003306 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003308 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 In addition, if the stack represents an exception,
3312 *and* the function call returns a 'true' value, we
3313 push WHY_SILENCED onto the stack. END_FINALLY will
3314 then not re-raise the exception. (But non-local
3315 gotos should still be resumed.)
3316 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003319 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003320 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 (void)POP();
3322 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003323 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 else if (PyLong_Check(exc)) {
3326 STACKADJ(-1);
3327 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 case WHY_RETURN:
3329 case WHY_CONTINUE:
3330 /* Retval in TOP. */
3331 exit_func = SECOND();
3332 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 break;
3335 default:
3336 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003337 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 break;
3339 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 }
3342 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003343 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 val = SECOND();
3346 tb = THIRD();
3347 tp2 = FOURTH();
3348 exc2 = PEEK(5);
3349 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 SET_VALUE(7, tb2);
3352 SET_VALUE(6, exc2);
3353 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3355 SET_FOURTH(NULL);
3356 /* We just shifted the stack down, so we have
3357 to tell the except handler block that the
3358 values are lower than it expects. */
3359 block = &f->f_blockstack[f->f_iblock - 1];
3360 assert(block->b_type == EXCEPT_HANDLER);
3361 block->b_level--;
3362 }
3363 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003364 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 if (res == NULL)
3367 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003368
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003369 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003370 PUSH(exc);
3371 PUSH(res);
3372 PREDICT(WITH_CLEANUP_FINISH);
3373 DISPATCH();
3374 }
3375
3376 PREDICTED(WITH_CLEANUP_FINISH);
3377 TARGET(WITH_CLEANUP_FINISH) {
3378 PyObject *res = POP();
3379 PyObject *exc = POP();
3380 int err;
3381
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 if (exc != Py_None)
3383 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 else
3385 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003386
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003387 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003388 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003391 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 else if (err > 0) {
3393 err = 0;
3394 /* There was an exception and a True return */
3395 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3396 }
3397 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003400
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003401 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 TARGET(CALL_FUNCTION) {
3403 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 PCALL(PCALL_ALL);
3405 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003406#ifdef WITH_TSC
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003407 res = call_function(&sp, oparg, NULL, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003408#else
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003409 res = call_function(&sp, oparg, NULL);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003410#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003412 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003413 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003415 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003416 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003418
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003419 TARGET(CALL_FUNCTION_KW) {
3420 PyObject **sp, *res, *names;
3421
3422 names = POP();
3423 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 PCALL(PCALL_ALL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003426#ifdef WITH_TSC
3427 res = call_function(&sp, oparg, names, &intr0, &intr1);
3428#else
3429 res = call_function(&sp, oparg, names);
3430#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003431 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003432 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003433 Py_DECREF(names);
3434
3435 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003436 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003437 }
3438 DISPATCH();
3439 }
3440
3441 TARGET(CALL_FUNCTION_EX) {
3442 PyObject *func, *callargs, *kwargs = NULL, *result;
3443 PCALL(PCALL_ALL);
3444 if (oparg & 0x01) {
3445 kwargs = POP();
3446 assert(PyDict_CheckExact(kwargs));
3447 }
3448 callargs = POP();
3449 assert(PyTuple_CheckExact(callargs));
3450 func = TOP();
3451
3452 READ_TIMESTAMP(intr0);
3453 result = do_call_core(func, callargs, kwargs);
3454 READ_TIMESTAMP(intr1);
3455 Py_DECREF(func);
3456 Py_DECREF(callargs);
3457 Py_XDECREF(kwargs);
3458
3459 SET_TOP(result);
3460 if (result == NULL) {
3461 goto error;
3462 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003463 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003465
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003466 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003467 PyObject *qualname = POP();
3468 PyObject *codeobj = POP();
3469 PyFunctionObject *func = (PyFunctionObject *)
3470 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003471
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003472 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003474 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003475 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003477
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003478 if (oparg & 0x08) {
3479 assert(PyTuple_CheckExact(TOP()));
3480 func ->func_closure = POP();
3481 }
3482 if (oparg & 0x04) {
3483 assert(PyDict_CheckExact(TOP()));
3484 func->func_annotations = POP();
3485 }
3486 if (oparg & 0x02) {
3487 assert(PyDict_CheckExact(TOP()));
3488 func->func_kwdefaults = POP();
3489 }
3490 if (oparg & 0x01) {
3491 assert(PyTuple_CheckExact(TOP()));
3492 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003494
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003495 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003496 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003499 TARGET(BUILD_SLICE) {
3500 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003502 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504 step = NULL;
3505 stop = POP();
3506 start = TOP();
3507 slice = PySlice_New(start, stop, step);
3508 Py_DECREF(start);
3509 Py_DECREF(stop);
3510 Py_XDECREF(step);
3511 SET_TOP(slice);
3512 if (slice == NULL)
3513 goto error;
3514 DISPATCH();
3515 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003516
Eric V. Smitha78c7952015-11-03 12:45:05 -05003517 TARGET(FORMAT_VALUE) {
3518 /* Handles f-string value formatting. */
3519 PyObject *result;
3520 PyObject *fmt_spec;
3521 PyObject *value;
3522 PyObject *(*conv_fn)(PyObject *);
3523 int which_conversion = oparg & FVC_MASK;
3524 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3525
3526 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003527 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003528
3529 /* See if any conversion is specified. */
3530 switch (which_conversion) {
3531 case FVC_STR: conv_fn = PyObject_Str; break;
3532 case FVC_REPR: conv_fn = PyObject_Repr; break;
3533 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3534
3535 /* Must be 0 (meaning no conversion), since only four
3536 values are allowed by (oparg & FVC_MASK). */
3537 default: conv_fn = NULL; break;
3538 }
3539
3540 /* If there's a conversion function, call it and replace
3541 value with that result. Otherwise, just use value,
3542 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003543 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003544 result = conv_fn(value);
3545 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003546 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003547 Py_XDECREF(fmt_spec);
3548 goto error;
3549 }
3550 value = result;
3551 }
3552
3553 /* If value is a unicode object, and there's no fmt_spec,
3554 then we know the result of format(value) is value
3555 itself. In that case, skip calling format(). I plan to
3556 move this optimization in to PyObject_Format()
3557 itself. */
3558 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3559 /* Do nothing, just transfer ownership to result. */
3560 result = value;
3561 } else {
3562 /* Actually call format(). */
3563 result = PyObject_Format(value, fmt_spec);
3564 Py_DECREF(value);
3565 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003566 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003567 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003568 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003569 }
3570
Eric V. Smith135d5f42016-02-05 18:23:08 -05003571 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003572 DISPATCH();
3573 }
3574
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003575 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003576 int oldoparg = oparg;
3577 NEXTOPARG();
3578 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003581
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003582
Antoine Pitrou042b1282010-08-13 21:15:58 +00003583#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003585#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 default:
3587 fprintf(stderr,
3588 "XXX lineno: %d, opcode: %d\n",
3589 PyFrame_GetLineNumber(f),
3590 opcode);
3591 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003592 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003593
3594#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003596#endif
3597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003599
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003600 /* This should never be reached. Every opcode should end with DISPATCH()
3601 or goto error. */
3602 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003603
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003604error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003606
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003607 assert(why == WHY_NOT);
3608 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003609
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003610 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003611#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003612 if (!PyErr_Occurred())
3613 PyErr_SetString(PyExc_SystemError,
3614 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003615#else
3616 assert(PyErr_Occurred());
3617#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003618
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003619 /* Log traceback info. */
3620 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003621
Benjamin Peterson51f46162013-01-23 08:38:47 -05003622 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003623 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3624 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003625
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003626fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003627 assert(why != WHY_NOT);
3628
3629 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 while (why != WHY_NOT && f->f_iblock > 0) {
3631 /* Peek at the current block. */
3632 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 assert(why != WHY_YIELD);
3635 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3636 why = WHY_NOT;
3637 JUMPTO(PyLong_AS_LONG(retval));
3638 Py_DECREF(retval);
3639 break;
3640 }
3641 /* Now we have to pop the block. */
3642 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 if (b->b_type == EXCEPT_HANDLER) {
3645 UNWIND_EXCEPT_HANDLER(b);
3646 continue;
3647 }
3648 UNWIND_BLOCK(b);
3649 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3650 why = WHY_NOT;
3651 JUMPTO(b->b_handler);
3652 break;
3653 }
3654 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3655 || b->b_type == SETUP_FINALLY)) {
3656 PyObject *exc, *val, *tb;
3657 int handler = b->b_handler;
3658 /* Beware, this invalidates all b->b_* fields */
3659 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3660 PUSH(tstate->exc_traceback);
3661 PUSH(tstate->exc_value);
3662 if (tstate->exc_type != NULL) {
3663 PUSH(tstate->exc_type);
3664 }
3665 else {
3666 Py_INCREF(Py_None);
3667 PUSH(Py_None);
3668 }
3669 PyErr_Fetch(&exc, &val, &tb);
3670 /* Make the raw exception data
3671 available to the handler,
3672 so a program can emulate the
3673 Python main loop. */
3674 PyErr_NormalizeException(
3675 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003676 if (tb != NULL)
3677 PyException_SetTraceback(val, tb);
3678 else
3679 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 Py_INCREF(exc);
3681 tstate->exc_type = exc;
3682 Py_INCREF(val);
3683 tstate->exc_value = val;
3684 tstate->exc_traceback = tb;
3685 if (tb == NULL)
3686 tb = Py_None;
3687 Py_INCREF(tb);
3688 PUSH(tb);
3689 PUSH(val);
3690 PUSH(exc);
3691 why = WHY_NOT;
3692 JUMPTO(handler);
3693 break;
3694 }
3695 if (b->b_type == SETUP_FINALLY) {
3696 if (why & (WHY_RETURN | WHY_CONTINUE))
3697 PUSH(retval);
3698 PUSH(PyLong_FromLong((long)why));
3699 why = WHY_NOT;
3700 JUMPTO(b->b_handler);
3701 break;
3702 }
3703 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 if (why != WHY_NOT)
3708 break;
3709 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003710
Victor Stinnerace47d72013-07-18 01:41:08 +02003711 assert(!PyErr_Occurred());
3712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 assert(why != WHY_YIELD);
3716 /* Pop remaining stack entries. */
3717 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003718 PyObject *o = POP();
3719 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 if (why != WHY_RETURN)
3723 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003724
Victor Stinner4a7cc882015-03-06 23:35:27 +01003725 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003726
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003727fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003728 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003729
Benjamin Petersonac913412011-07-03 16:25:11 -05003730 /* The purpose of this block is to put aside the generator's exception
3731 state and restore that of the calling frame. If the current
3732 exception state is from the caller, we clear the exception values
3733 on the generator frame, so they are not swapped back in latter. The
3734 origin of the current exception state is determined by checking for
3735 except handler blocks, which we must be in iff a new exception
3736 state came into existence in this frame. (An uncaught exception
3737 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3738 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003739 for (i = 0; i < f->f_iblock; i++) {
3740 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003741 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003742 }
3743 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003744 if (i == f->f_iblock)
3745 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003746 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003747 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003748 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003749 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003752 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003754 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3755 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003757 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 why = WHY_EXCEPTION;
3759 }
3760 }
3761 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003762 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3763 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 PyTrace_RETURN, NULL);
3765 }
3766 }
3767 if (tstate->c_profilefunc) {
3768 if (why == WHY_EXCEPTION)
3769 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003770 tstate->c_profileobj,
3771 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003772 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003773 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3774 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003776 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003777 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 }
3779 }
3780 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003783exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003785 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003787
Victor Stinnerefde1462015-03-21 15:04:43 +01003788 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003789}
3790
Benjamin Petersonb204a422011-06-05 22:04:07 -05003791static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003792format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3793{
3794 int err;
3795 Py_ssize_t len = PyList_GET_SIZE(names);
3796 PyObject *name_str, *comma, *tail, *tmp;
3797
3798 assert(PyList_CheckExact(names));
3799 assert(len >= 1);
3800 /* Deal with the joys of natural language. */
3801 switch (len) {
3802 case 1:
3803 name_str = PyList_GET_ITEM(names, 0);
3804 Py_INCREF(name_str);
3805 break;
3806 case 2:
3807 name_str = PyUnicode_FromFormat("%U and %U",
3808 PyList_GET_ITEM(names, len - 2),
3809 PyList_GET_ITEM(names, len - 1));
3810 break;
3811 default:
3812 tail = PyUnicode_FromFormat(", %U, and %U",
3813 PyList_GET_ITEM(names, len - 2),
3814 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003815 if (tail == NULL)
3816 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003817 /* Chop off the last two objects in the list. This shouldn't actually
3818 fail, but we can't be too careful. */
3819 err = PyList_SetSlice(names, len - 2, len, NULL);
3820 if (err == -1) {
3821 Py_DECREF(tail);
3822 return;
3823 }
3824 /* Stitch everything up into a nice comma-separated list. */
3825 comma = PyUnicode_FromString(", ");
3826 if (comma == NULL) {
3827 Py_DECREF(tail);
3828 return;
3829 }
3830 tmp = PyUnicode_Join(comma, names);
3831 Py_DECREF(comma);
3832 if (tmp == NULL) {
3833 Py_DECREF(tail);
3834 return;
3835 }
3836 name_str = PyUnicode_Concat(tmp, tail);
3837 Py_DECREF(tmp);
3838 Py_DECREF(tail);
3839 break;
3840 }
3841 if (name_str == NULL)
3842 return;
3843 PyErr_Format(PyExc_TypeError,
3844 "%U() missing %i required %s argument%s: %U",
3845 co->co_name,
3846 len,
3847 kind,
3848 len == 1 ? "" : "s",
3849 name_str);
3850 Py_DECREF(name_str);
3851}
3852
3853static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003854missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003855 PyObject **fastlocals)
3856{
Victor Stinner74319ae2016-08-25 00:04:09 +02003857 Py_ssize_t i, j = 0;
3858 Py_ssize_t start, end;
3859 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003860 const char *kind = positional ? "positional" : "keyword-only";
3861 PyObject *missing_names;
3862
3863 /* Compute the names of the arguments that are missing. */
3864 missing_names = PyList_New(missing);
3865 if (missing_names == NULL)
3866 return;
3867 if (positional) {
3868 start = 0;
3869 end = co->co_argcount - defcount;
3870 }
3871 else {
3872 start = co->co_argcount;
3873 end = start + co->co_kwonlyargcount;
3874 }
3875 for (i = start; i < end; i++) {
3876 if (GETLOCAL(i) == NULL) {
3877 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3878 PyObject *name = PyObject_Repr(raw);
3879 if (name == NULL) {
3880 Py_DECREF(missing_names);
3881 return;
3882 }
3883 PyList_SET_ITEM(missing_names, j++, name);
3884 }
3885 }
3886 assert(j == missing);
3887 format_missing(kind, co, missing_names);
3888 Py_DECREF(missing_names);
3889}
3890
3891static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003892too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3893 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003894{
3895 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003896 Py_ssize_t kwonly_given = 0;
3897 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003898 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003899 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900
Benjamin Petersone109c702011-06-24 09:37:26 -05003901 assert((co->co_flags & CO_VARARGS) == 0);
3902 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003903 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3904 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003905 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003906 }
3907 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003908 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003909 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003910 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003911 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003912 }
3913 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003914 plural = (co_argcount != 1);
3915 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 }
3917 if (sig == NULL)
3918 return;
3919 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003920 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3921 kwonly_sig = PyUnicode_FromFormat(format,
3922 given != 1 ? "s" : "",
3923 kwonly_given,
3924 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003925 if (kwonly_sig == NULL) {
3926 Py_DECREF(sig);
3927 return;
3928 }
3929 }
3930 else {
3931 /* This will not fail. */
3932 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003933 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003934 }
3935 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003936 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003937 co->co_name,
3938 sig,
3939 plural ? "s" : "",
3940 given,
3941 kwonly_sig,
3942 given == 1 && !kwonly_given ? "was" : "were");
3943 Py_DECREF(sig);
3944 Py_DECREF(kwonly_sig);
3945}
3946
Guido van Rossumc2e20742006-02-27 22:32:47 +00003947/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003948 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003949 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003950
Victor Stinner40ee3012014-06-16 15:59:28 +02003951static PyObject *
3952_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003953 PyObject **args, Py_ssize_t argcount,
3954 PyObject **kws, Py_ssize_t kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003955 PyObject *kwnames, PyObject **kwstack,
Victor Stinner74319ae2016-08-25 00:04:09 +02003956 PyObject **defs, Py_ssize_t defcount,
3957 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003958 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003959{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003960 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003961 PyFrameObject *f;
3962 PyObject *retval = NULL;
3963 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003964 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003966 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3967 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003968 PyObject *kwdict;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003969 Py_ssize_t kwcount2 = kwnames == NULL ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerc7020012016-08-16 23:40:29 +02003970
3971 assert((kwcount == 0) || (kws != NULL));
Tim Peters5ca576e2001-06-18 22:08:13 +00003972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 if (globals == NULL) {
3974 PyErr_SetString(PyExc_SystemError,
3975 "PyEval_EvalCodeEx: NULL globals");
3976 return NULL;
3977 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003978
Victor Stinnerc7020012016-08-16 23:40:29 +02003979 /* Create the frame */
3980 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003983 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003984 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003985 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 fastlocals = f->f_localsplus;
3987 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003988
Victor Stinnerc7020012016-08-16 23:40:29 +02003989 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003990 if (co->co_flags & CO_VARKEYWORDS) {
3991 kwdict = PyDict_New();
3992 if (kwdict == NULL)
3993 goto fail;
3994 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003995 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003996 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003997 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 SETLOCAL(i, kwdict);
3999 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004000 else {
4001 kwdict = NULL;
4002 }
4003
4004 /* Copy positional arguments into local variables */
4005 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004006 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004007 }
4008 else {
4009 n = argcount;
4010 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 for (i = 0; i < n; i++) {
4012 x = args[i];
4013 Py_INCREF(x);
4014 SETLOCAL(i, x);
4015 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004016
4017 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004018 if (co->co_flags & CO_VARARGS) {
4019 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004020 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004021 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004022 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004023 SETLOCAL(total_args, u);
4024 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 x = args[i];
4026 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004027 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004029 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004030
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004031 /* Handle keyword arguments passed as an array of (key, value) pairs */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004032 for (i = 0; i < kwcount; i++) {
4033 PyObject **co_varnames;
4034 PyObject *keyword = kws[2*i];
4035 PyObject *value = kws[2*i + 1];
Victor Stinner17061a92016-08-16 23:39:42 +02004036 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004037
Benjamin Petersonb204a422011-06-05 22:04:07 -05004038 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4039 PyErr_Format(PyExc_TypeError,
4040 "%U() keywords must be strings",
4041 co->co_name);
4042 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004044
Benjamin Petersonb204a422011-06-05 22:04:07 -05004045 /* Speed hack: do raw pointer compares. As names are
4046 normally interned this should almost always hit. */
4047 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4048 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004049 PyObject *name = co_varnames[j];
4050 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004051 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004052 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004053 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004054
Benjamin Petersonb204a422011-06-05 22:04:07 -05004055 /* Slow fallback, just in case */
4056 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02004057 PyObject *name = co_varnames[j];
4058 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
4059 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004060 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004061 }
4062 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004064 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004065 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004066
Benjamin Petersonb204a422011-06-05 22:04:07 -05004067 if (j >= total_args && kwdict == NULL) {
4068 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004069 "%U() got an unexpected keyword argument '%S'",
4070 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004071 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004073
Christian Heimes0bd447f2013-07-20 14:48:10 +02004074 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4075 goto fail;
4076 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004077 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004078
Benjamin Petersonb204a422011-06-05 22:04:07 -05004079 kw_found:
4080 if (GETLOCAL(j) != NULL) {
4081 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02004082 "%U() got multiple values for argument '%S'",
4083 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004084 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004086 Py_INCREF(value);
4087 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004089
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004090 /* Handle keyword arguments passed as keys tuple + values array */
4091 for (i = 0; i < kwcount2; i++) {
4092 PyObject **co_varnames;
4093 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4094 PyObject *value = kwstack[i];
4095 int j;
4096 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4097 PyErr_Format(PyExc_TypeError,
4098 "%U() keywords must be strings",
4099 co->co_name);
4100 goto fail;
4101 }
4102 /* Speed hack: do raw pointer compares. As names are
4103 normally interned this should almost always hit. */
4104 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4105 for (j = 0; j < total_args; j++) {
4106 PyObject *nm = co_varnames[j];
4107 if (nm == keyword)
4108 goto kw_found2;
4109 }
4110 /* Slow fallback, just in case */
4111 for (j = 0; j < total_args; j++) {
4112 PyObject *nm = co_varnames[j];
4113 int cmp = PyObject_RichCompareBool(
4114 keyword, nm, Py_EQ);
4115 if (cmp > 0)
4116 goto kw_found2;
4117 else if (cmp < 0)
4118 goto fail;
4119 }
4120 if (j >= total_args && kwdict == NULL) {
4121 PyErr_Format(PyExc_TypeError,
4122 "%U() got an unexpected "
4123 "keyword argument '%S'",
4124 co->co_name,
4125 keyword);
4126 goto fail;
4127 }
4128 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4129 goto fail;
4130 }
4131 continue;
4132 kw_found2:
4133 if (GETLOCAL(j) != NULL) {
4134 PyErr_Format(PyExc_TypeError,
4135 "%U() got multiple "
4136 "values for argument '%S'",
4137 co->co_name,
4138 keyword);
4139 goto fail;
4140 }
4141 Py_INCREF(value);
4142 SETLOCAL(j, value);
4143 }
4144
Victor Stinnerc7020012016-08-16 23:40:29 +02004145 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004146 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004147 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 goto fail;
4149 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004150
4151 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004152 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02004153 Py_ssize_t m = co->co_argcount - defcount;
4154 Py_ssize_t missing = 0;
4155 for (i = argcount; i < m; i++) {
4156 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004157 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004158 }
4159 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004160 if (missing) {
4161 missing_arguments(co, missing, defcount, fastlocals);
4162 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004163 }
4164 if (n > m)
4165 i = n - m;
4166 else
4167 i = 0;
4168 for (; i < defcount; i++) {
4169 if (GETLOCAL(m+i) == NULL) {
4170 PyObject *def = defs[i];
4171 Py_INCREF(def);
4172 SETLOCAL(m+i, def);
4173 }
4174 }
4175 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004176
4177 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004178 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004179 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004180 for (i = co->co_argcount; i < total_args; i++) {
4181 PyObject *name;
4182 if (GETLOCAL(i) != NULL)
4183 continue;
4184 name = PyTuple_GET_ITEM(co->co_varnames, i);
4185 if (kwdefs != NULL) {
4186 PyObject *def = PyDict_GetItem(kwdefs, name);
4187 if (def) {
4188 Py_INCREF(def);
4189 SETLOCAL(i, def);
4190 continue;
4191 }
4192 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004193 missing++;
4194 }
4195 if (missing) {
4196 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004197 goto fail;
4198 }
4199 }
4200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004202 vars into frame. */
4203 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004205 int arg;
4206 /* Possibly account for the cell variable being an argument. */
4207 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004208 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004209 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004210 /* Clear the local copy. */
4211 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004212 }
4213 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004214 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004215 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004216 if (c == NULL)
4217 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004218 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004220
4221 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004222 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4223 PyObject *o = PyTuple_GET_ITEM(closure, i);
4224 Py_INCREF(o);
4225 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004227
Yury Selivanoveb636452016-09-08 22:01:51 -07004228 /* Handle generator/coroutine/asynchronous generator */
4229 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004230 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004231 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004232 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004233
4234 if (is_coro && tstate->in_coroutine_wrapper) {
4235 assert(coro_wrapper != NULL);
4236 PyErr_Format(PyExc_RuntimeError,
4237 "coroutine wrapper %.200R attempted "
4238 "to recursively wrap %.200R",
4239 coro_wrapper,
4240 co);
4241 goto fail;
4242 }
Yury Selivanov75445082015-05-11 22:57:16 -04004243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 /* Don't need to keep the reference to f_back, it will be set
4245 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004246 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 /* Create a new generator that owns the ready to run frame
4251 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004252 if (is_coro) {
4253 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004254 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4255 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004256 } else {
4257 gen = PyGen_NewWithQualName(f, name, qualname);
4258 }
Yury Selivanov75445082015-05-11 22:57:16 -04004259 if (gen == NULL)
4260 return NULL;
4261
Yury Selivanov94c22632015-06-04 10:16:51 -04004262 if (is_coro && coro_wrapper != NULL) {
4263 PyObject *wrapped;
4264 tstate->in_coroutine_wrapper = 1;
4265 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4266 tstate->in_coroutine_wrapper = 0;
4267 return wrapped;
4268 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004269
Yury Selivanov75445082015-05-11 22:57:16 -04004270 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004274
Thomas Woutersce272b62007-09-19 21:19:28 +00004275fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 /* decref'ing the frame can cause __del__ methods to get invoked,
4278 which can call back into Python. While we're done with the
4279 current Python frame (f), the associated C stack is still in use,
4280 so recursion_depth must be boosted for the duration.
4281 */
4282 assert(tstate != NULL);
4283 ++tstate->recursion_depth;
4284 Py_DECREF(f);
4285 --tstate->recursion_depth;
4286 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004287}
4288
Victor Stinner40ee3012014-06-16 15:59:28 +02004289PyObject *
4290PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4291 PyObject **args, int argcount, PyObject **kws, int kwcount,
4292 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4293{
4294 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004295 args, argcount,
4296 kws, kwcount,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004297 NULL, NULL,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004298 defs, defcount,
4299 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004300 NULL, NULL);
4301}
Tim Peters5ca576e2001-06-18 22:08:13 +00004302
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004303static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004304special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004307 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004309 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 return NULL;
4311 }
4312 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004313}
4314
4315
Benjamin Peterson87880242011-07-03 16:48:31 -05004316/* These 3 functions deal with the exception state of generators. */
4317
4318static void
4319save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4320{
4321 PyObject *type, *value, *traceback;
4322 Py_XINCREF(tstate->exc_type);
4323 Py_XINCREF(tstate->exc_value);
4324 Py_XINCREF(tstate->exc_traceback);
4325 type = f->f_exc_type;
4326 value = f->f_exc_value;
4327 traceback = f->f_exc_traceback;
4328 f->f_exc_type = tstate->exc_type;
4329 f->f_exc_value = tstate->exc_value;
4330 f->f_exc_traceback = tstate->exc_traceback;
4331 Py_XDECREF(type);
4332 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004333 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004334}
4335
4336static void
4337swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4338{
4339 PyObject *tmp;
4340 tmp = tstate->exc_type;
4341 tstate->exc_type = f->f_exc_type;
4342 f->f_exc_type = tmp;
4343 tmp = tstate->exc_value;
4344 tstate->exc_value = f->f_exc_value;
4345 f->f_exc_value = tmp;
4346 tmp = tstate->exc_traceback;
4347 tstate->exc_traceback = f->f_exc_traceback;
4348 f->f_exc_traceback = tmp;
4349}
4350
4351static void
4352restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4353{
4354 PyObject *type, *value, *tb;
4355 type = tstate->exc_type;
4356 value = tstate->exc_value;
4357 tb = tstate->exc_traceback;
4358 tstate->exc_type = f->f_exc_type;
4359 tstate->exc_value = f->f_exc_value;
4360 tstate->exc_traceback = f->f_exc_traceback;
4361 f->f_exc_type = NULL;
4362 f->f_exc_value = NULL;
4363 f->f_exc_traceback = NULL;
4364 Py_XDECREF(type);
4365 Py_XDECREF(value);
4366 Py_XDECREF(tb);
4367}
4368
4369
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004370/* Logic for the raise statement (too complicated for inlining).
4371 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004372static int
Collin Winter828f04a2007-08-31 00:04:24 +00004373do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 if (exc == NULL) {
4378 /* Reraise */
4379 PyThreadState *tstate = PyThreadState_GET();
4380 PyObject *tb;
4381 type = tstate->exc_type;
4382 value = tstate->exc_value;
4383 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004384 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 PyErr_SetString(PyExc_RuntimeError,
4386 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004387 return 0;
4388 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 Py_XINCREF(type);
4390 Py_XINCREF(value);
4391 Py_XINCREF(tb);
4392 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004393 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 /* We support the following forms of raise:
4397 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004398 raise <instance>
4399 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 if (PyExceptionClass_Check(exc)) {
4402 type = exc;
4403 value = PyObject_CallObject(exc, NULL);
4404 if (value == NULL)
4405 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004406 if (!PyExceptionInstance_Check(value)) {
4407 PyErr_Format(PyExc_TypeError,
4408 "calling %R should have returned an instance of "
4409 "BaseException, not %R",
4410 type, Py_TYPE(value));
4411 goto raise_error;
4412 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 }
4414 else if (PyExceptionInstance_Check(exc)) {
4415 value = exc;
4416 type = PyExceptionInstance_Class(exc);
4417 Py_INCREF(type);
4418 }
4419 else {
4420 /* Not something you can raise. You get an exception
4421 anyway, just not what you specified :-) */
4422 Py_DECREF(exc);
4423 PyErr_SetString(PyExc_TypeError,
4424 "exceptions must derive from BaseException");
4425 goto raise_error;
4426 }
Collin Winter828f04a2007-08-31 00:04:24 +00004427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 if (cause) {
4429 PyObject *fixed_cause;
4430 if (PyExceptionClass_Check(cause)) {
4431 fixed_cause = PyObject_CallObject(cause, NULL);
4432 if (fixed_cause == NULL)
4433 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004434 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004436 else if (PyExceptionInstance_Check(cause)) {
4437 fixed_cause = cause;
4438 }
4439 else if (cause == Py_None) {
4440 Py_DECREF(cause);
4441 fixed_cause = NULL;
4442 }
4443 else {
4444 PyErr_SetString(PyExc_TypeError,
4445 "exception causes must derive from "
4446 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 goto raise_error;
4448 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004449 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 }
Collin Winter828f04a2007-08-31 00:04:24 +00004451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 PyErr_SetObject(type, value);
4453 /* PyErr_SetObject incref's its arguments */
4454 Py_XDECREF(value);
4455 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004456 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004457
4458raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004459 Py_XDECREF(value);
4460 Py_XDECREF(type);
4461 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004462 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004463}
4464
Tim Petersd6d010b2001-06-21 02:49:55 +00004465/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004466 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004467
Guido van Rossum0368b722007-05-11 16:50:42 +00004468 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4469 with a variable target.
4470*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004471
Barry Warsawe42b18f1997-08-25 22:13:04 +00004472static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004473unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 int i = 0, j = 0;
4476 Py_ssize_t ll = 0;
4477 PyObject *it; /* iter(v) */
4478 PyObject *w;
4479 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004483 it = PyObject_GetIter(v);
4484 if (it == NULL)
4485 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 for (; i < argcnt; i++) {
4488 w = PyIter_Next(it);
4489 if (w == NULL) {
4490 /* Iterator done, via error or exhaustion. */
4491 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004492 if (argcntafter == -1) {
4493 PyErr_Format(PyExc_ValueError,
4494 "not enough values to unpack (expected %d, got %d)",
4495 argcnt, i);
4496 }
4497 else {
4498 PyErr_Format(PyExc_ValueError,
4499 "not enough values to unpack "
4500 "(expected at least %d, got %d)",
4501 argcnt + argcntafter, i);
4502 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 }
4504 goto Error;
4505 }
4506 *--sp = w;
4507 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 if (argcntafter == -1) {
4510 /* We better have exhausted the iterator now. */
4511 w = PyIter_Next(it);
4512 if (w == NULL) {
4513 if (PyErr_Occurred())
4514 goto Error;
4515 Py_DECREF(it);
4516 return 1;
4517 }
4518 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004519 PyErr_Format(PyExc_ValueError,
4520 "too many values to unpack (expected %d)",
4521 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 goto Error;
4523 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 l = PySequence_List(it);
4526 if (l == NULL)
4527 goto Error;
4528 *--sp = l;
4529 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 ll = PyList_GET_SIZE(l);
4532 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004533 PyErr_Format(PyExc_ValueError,
4534 "not enough values to unpack (expected at least %d, got %zd)",
4535 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 goto Error;
4537 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 /* Pop the "after-variable" args off the list. */
4540 for (j = argcntafter; j > 0; j--, i++) {
4541 *--sp = PyList_GET_ITEM(l, ll - j);
4542 }
4543 /* Resize the list. */
4544 Py_SIZE(l) = ll - argcntafter;
4545 Py_DECREF(it);
4546 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004547
Tim Petersd6d010b2001-06-21 02:49:55 +00004548Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 for (; i > 0; i--, sp++)
4550 Py_DECREF(*sp);
4551 Py_XDECREF(it);
4552 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004553}
4554
4555
Guido van Rossum96a42c81992-01-12 02:29:51 +00004556#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004557static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004558prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004560 printf("%s ", str);
4561 if (PyObject_Print(v, stdout, 0) != 0)
4562 PyErr_Clear(); /* Don't know what else to do */
4563 printf("\n");
4564 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004565}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004566#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004567
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004568static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004569call_exc_trace(Py_tracefunc func, PyObject *self,
4570 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004571{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004572 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004574 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 if (value == NULL) {
4576 value = Py_None;
4577 Py_INCREF(value);
4578 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004579 PyErr_NormalizeException(&type, &value, &orig_traceback);
4580 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 arg = PyTuple_Pack(3, type, value, traceback);
4582 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004583 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 return;
4585 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004586 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 Py_DECREF(arg);
4588 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004589 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 else {
4591 Py_XDECREF(type);
4592 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004593 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004595}
4596
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004597static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004598call_trace_protected(Py_tracefunc func, PyObject *obj,
4599 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 PyObject *type, *value, *traceback;
4603 int err;
4604 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004605 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 if (err == 0)
4607 {
4608 PyErr_Restore(type, value, traceback);
4609 return 0;
4610 }
4611 else {
4612 Py_XDECREF(type);
4613 Py_XDECREF(value);
4614 Py_XDECREF(traceback);
4615 return -1;
4616 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004617}
4618
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004619static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004620call_trace(Py_tracefunc func, PyObject *obj,
4621 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 int result;
4625 if (tstate->tracing)
4626 return 0;
4627 tstate->tracing++;
4628 tstate->use_tracing = 0;
4629 result = func(obj, frame, what, arg);
4630 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4631 || (tstate->c_profilefunc != NULL));
4632 tstate->tracing--;
4633 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004634}
4635
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004636PyObject *
4637_PyEval_CallTracing(PyObject *func, PyObject *args)
4638{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004639 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 int save_tracing = tstate->tracing;
4641 int save_use_tracing = tstate->use_tracing;
4642 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 tstate->tracing = 0;
4645 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4646 || (tstate->c_profilefunc != NULL));
4647 result = PyObject_Call(func, args, NULL);
4648 tstate->tracing = save_tracing;
4649 tstate->use_tracing = save_use_tracing;
4650 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004651}
4652
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004653/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004654static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004655maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004656 PyThreadState *tstate, PyFrameObject *frame,
4657 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 int result = 0;
4660 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 /* If the last instruction executed isn't in the current
4663 instruction window, reset the window.
4664 */
4665 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4666 PyAddrPair bounds;
4667 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4668 &bounds);
4669 *instr_lb = bounds.ap_lower;
4670 *instr_ub = bounds.ap_upper;
4671 }
4672 /* If the last instruction falls at the start of a line or if
4673 it represents a jump backwards, update the frame's line
4674 number and call the trace function. */
4675 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4676 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004677 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 }
4679 *instr_prev = frame->f_lasti;
4680 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004681}
4682
Fred Drake5755ce62001-06-27 19:19:46 +00004683void
4684PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004686 PyThreadState *tstate = PyThreadState_GET();
4687 PyObject *temp = tstate->c_profileobj;
4688 Py_XINCREF(arg);
4689 tstate->c_profilefunc = NULL;
4690 tstate->c_profileobj = NULL;
4691 /* Must make sure that tracing is not ignored if 'temp' is freed */
4692 tstate->use_tracing = tstate->c_tracefunc != NULL;
4693 Py_XDECREF(temp);
4694 tstate->c_profilefunc = func;
4695 tstate->c_profileobj = arg;
4696 /* Flag that tracing or profiling is turned on */
4697 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004698}
4699
4700void
4701PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 PyThreadState *tstate = PyThreadState_GET();
4704 PyObject *temp = tstate->c_traceobj;
4705 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4706 Py_XINCREF(arg);
4707 tstate->c_tracefunc = NULL;
4708 tstate->c_traceobj = NULL;
4709 /* Must make sure that profiling is not ignored if 'temp' is freed */
4710 tstate->use_tracing = tstate->c_profilefunc != NULL;
4711 Py_XDECREF(temp);
4712 tstate->c_tracefunc = func;
4713 tstate->c_traceobj = arg;
4714 /* Flag that tracing or profiling is turned on */
4715 tstate->use_tracing = ((func != NULL)
4716 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004717}
4718
Yury Selivanov75445082015-05-11 22:57:16 -04004719void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004720_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004721{
4722 PyThreadState *tstate = PyThreadState_GET();
4723
Yury Selivanov75445082015-05-11 22:57:16 -04004724 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004725 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004726}
4727
4728PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004729_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004730{
4731 PyThreadState *tstate = PyThreadState_GET();
4732 return tstate->coroutine_wrapper;
4733}
4734
Yury Selivanoveb636452016-09-08 22:01:51 -07004735void
4736_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4737{
4738 PyThreadState *tstate = PyThreadState_GET();
4739
4740 Py_XINCREF(firstiter);
4741 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4742}
4743
4744PyObject *
4745_PyEval_GetAsyncGenFirstiter(void)
4746{
4747 PyThreadState *tstate = PyThreadState_GET();
4748 return tstate->async_gen_firstiter;
4749}
4750
4751void
4752_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4753{
4754 PyThreadState *tstate = PyThreadState_GET();
4755
4756 Py_XINCREF(finalizer);
4757 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4758}
4759
4760PyObject *
4761_PyEval_GetAsyncGenFinalizer(void)
4762{
4763 PyThreadState *tstate = PyThreadState_GET();
4764 return tstate->async_gen_finalizer;
4765}
4766
Guido van Rossumb209a111997-04-29 18:18:01 +00004767PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004768PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770 PyFrameObject *current_frame = PyEval_GetFrame();
4771 if (current_frame == NULL)
4772 return PyThreadState_GET()->interp->builtins;
4773 else
4774 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004775}
4776
Guido van Rossumb209a111997-04-29 18:18:01 +00004777PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004778PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004780 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004781 if (current_frame == NULL) {
4782 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004784 }
4785
4786 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4787 return NULL;
4788
4789 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004791}
4792
Guido van Rossumb209a111997-04-29 18:18:01 +00004793PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004794PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004796 PyFrameObject *current_frame = PyEval_GetFrame();
4797 if (current_frame == NULL)
4798 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004799
4800 assert(current_frame->f_globals != NULL);
4801 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004802}
4803
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004804PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004805PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004806{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 PyThreadState *tstate = PyThreadState_GET();
4808 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004809}
4810
Guido van Rossum6135a871995-01-09 17:53:26 +00004811int
Tim Peters5ba58662001-07-16 02:29:45 +00004812PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004814 PyFrameObject *current_frame = PyEval_GetFrame();
4815 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004817 if (current_frame != NULL) {
4818 const int codeflags = current_frame->f_code->co_flags;
4819 const int compilerflags = codeflags & PyCF_MASK;
4820 if (compilerflags) {
4821 result = 1;
4822 cf->cf_flags |= compilerflags;
4823 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004824#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004825 if (codeflags & CO_GENERATOR_ALLOWED) {
4826 result = 1;
4827 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4828 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004829#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004830 }
4831 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004832}
4833
Guido van Rossum3f5da241990-12-20 15:06:42 +00004834
Guido van Rossum681d79a1995-07-18 14:51:37 +00004835/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004836 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004837
Guido van Rossumb209a111997-04-29 18:18:01 +00004838PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004839PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004840{
Victor Stinner59b356d2015-03-16 11:52:32 +01004841#ifdef Py_DEBUG
4842 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4843 set. It raises a new exception if parameters are invalid or if
4844 PyTuple_New() fails, and so the original exception is lost. */
4845 assert(!PyErr_Occurred());
4846#endif
4847
Victor Stinner8a31c822016-08-19 17:12:23 +02004848 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004849 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004850 }
Victor Stinner155ea652016-08-22 23:26:00 +02004851
4852 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004853 PyErr_SetString(PyExc_TypeError,
4854 "argument list must be a tuple");
4855 return NULL;
4856 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004857
Victor Stinner8a31c822016-08-19 17:12:23 +02004858 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 PyErr_SetString(PyExc_TypeError,
4860 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 return NULL;
4862 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004863
Victor Stinner6e2333d2016-08-23 00:25:01 +02004864 return PyObject_Call(func, args, kwargs);
Jeremy Hylton52820442001-01-03 23:52:36 +00004865}
4866
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004867const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004868PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004869{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 if (PyMethod_Check(func))
4871 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4872 else if (PyFunction_Check(func))
4873 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4874 else if (PyCFunction_Check(func))
4875 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4876 else
4877 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004878}
4879
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004880const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004881PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004883 if (PyMethod_Check(func))
4884 return "()";
4885 else if (PyFunction_Check(func))
4886 return "()";
4887 else if (PyCFunction_Check(func))
4888 return "()";
4889 else
4890 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004891}
4892
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004893#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004894if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004895 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4896 tstate, tstate->frame, \
4897 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 x = NULL; \
4899 } \
4900 else { \
4901 x = call; \
4902 if (tstate->c_profilefunc != NULL) { \
4903 if (x == NULL) { \
4904 call_trace_protected(tstate->c_profilefunc, \
4905 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004906 tstate, tstate->frame, \
4907 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 /* XXX should pass (type, value, tb) */ \
4909 } else { \
4910 if (call_trace(tstate->c_profilefunc, \
4911 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004912 tstate, tstate->frame, \
4913 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004914 Py_DECREF(x); \
4915 x = NULL; \
4916 } \
4917 } \
4918 } \
4919 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004920} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004921 x = call; \
4922 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004923
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004924static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07004925call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004926#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004927 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004928#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004929 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004930{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004931 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004932 PyObject *func = *pfunc;
4933 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004934 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4935 Py_ssize_t nargs = oparg - nkwargs;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004936 PyObject **stack;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 /* Always dispatch PyCFunction first, because these are
4939 presumed to be the most frequent callable object.
4940 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004941 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 PCALL(PCALL_CFUNCTION);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004945
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004946 stack = (*pp_stack) - nargs - nkwargs;
4947 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004948 }
4949 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004950 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4951 /* optimize access to bound methods */
4952 PyObject *self = PyMethod_GET_SELF(func);
4953 PCALL(PCALL_METHOD);
4954 PCALL(PCALL_BOUND_METHOD);
4955 Py_INCREF(self);
4956 func = PyMethod_GET_FUNCTION(func);
4957 Py_INCREF(func);
4958 Py_SETREF(*pfunc, self);
4959 nargs++;
4960 }
4961 else {
4962 Py_INCREF(func);
4963 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004964
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004965 stack = (*pp_stack) - nargs - nkwargs;
Victor Stinner4a7cc882015-03-06 23:35:27 +01004966
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004967 READ_TIMESTAMP(*pintr0);
4968 if (PyFunction_Check(func)) {
4969 x = fast_function(func, stack, nargs, kwnames);
4970 }
4971 else {
4972 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4973 }
4974 READ_TIMESTAMP(*pintr1);
Victor Stinnerd8735722016-09-09 12:36:44 -07004975
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004976 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004978
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004979 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004981 /* Clear the stack of the function object. Also removes
4982 the arguments in case they weren't consumed already
Victor Stinnere90bdb12016-08-25 23:26:50 +02004983 (fast_function() and err_args() leave them on the stack).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004984 */
4985 while ((*pp_stack) > pfunc) {
4986 w = EXT_POP(*pp_stack);
4987 Py_DECREF(w);
4988 PCALL(PCALL_POP);
4989 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004991 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004992}
4993
Victor Stinnere90bdb12016-08-25 23:26:50 +02004994/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004995 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004996 For the simplest case -- a function that takes only positional
4997 arguments and is called with only positional arguments -- it
4998 inlines the most primitive frame setup code from
4999 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
5000 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00005001*/
5002
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005003static PyObject*
Victor Stinnerd8735722016-09-09 12:36:44 -07005004_PyFunction_FastCall(PyCodeObject *co, PyObject **args, Py_ssize_t nargs,
5005 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005006{
5007 PyFrameObject *f;
5008 PyThreadState *tstate = PyThreadState_GET();
5009 PyObject **fastlocals;
5010 Py_ssize_t i;
5011 PyObject *result;
5012
5013 PCALL(PCALL_FASTER_FUNCTION);
5014 assert(globals != NULL);
5015 /* XXX Perhaps we should create a specialized
5016 PyFrame_New() that doesn't take locals, but does
5017 take builtins without sanity checking them.
5018 */
5019 assert(tstate != NULL);
5020 f = PyFrame_New(tstate, co, globals, NULL);
5021 if (f == NULL) {
5022 return NULL;
5023 }
5024
5025 fastlocals = f->f_localsplus;
5026
Victor Stinner74319ae2016-08-25 00:04:09 +02005027 for (i = 0; i < nargs; i++) {
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005028 Py_INCREF(*args);
5029 fastlocals[i] = *args++;
5030 }
5031 result = PyEval_EvalFrameEx(f,0);
5032
5033 ++tstate->recursion_depth;
5034 Py_DECREF(f);
5035 --tstate->recursion_depth;
5036
5037 return result;
5038}
5039
Victor Stinnere90bdb12016-08-25 23:26:50 +02005040static PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07005041fast_function(PyObject *func, PyObject **stack,
5042 Py_ssize_t nargs, PyObject *kwnames)
Jeremy Hylton52820442001-01-03 23:52:36 +00005043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005044 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5045 PyObject *globals = PyFunction_GET_GLOBALS(func);
5046 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005047 PyObject *kwdefs, *closure, *name, *qualname;
5048 PyObject **d;
Victor Stinnerd8735722016-09-09 12:36:44 -07005049 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005050 Py_ssize_t nd;
Victor Stinnerd8735722016-09-09 12:36:44 -07005051
5052 assert((nargs == 0 && nkwargs == 0) || stack != NULL);
Victor Stinner577e1f82016-08-25 00:29:32 +02005053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005054 PCALL(PCALL_FUNCTION);
5055 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005056
Victor Stinner74319ae2016-08-25 00:04:09 +02005057 if (co->co_kwonlyargcount == 0 && nkwargs == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005058 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5059 {
Victor Stinner2eedc112016-08-22 12:29:42 +02005060 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005061 return _PyFunction_FastCall(co, stack, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005062 }
5063 else if (nargs == 0 && argdefs != NULL
5064 && co->co_argcount == Py_SIZE(argdefs)) {
5065 /* function called with no arguments, but all parameters have
5066 a default value: use default values as arguments .*/
5067 stack = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005068 return _PyFunction_FastCall(co, stack, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005069 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005070 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005071
5072 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5073 closure = PyFunction_GET_CLOSURE(func);
5074 name = ((PyFunctionObject *)func) -> func_name;
5075 qualname = ((PyFunctionObject *)func) -> func_qualname;
5076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 if (argdefs != NULL) {
5078 d = &PyTuple_GET_ITEM(argdefs, 0);
5079 nd = Py_SIZE(argdefs);
5080 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005081 else {
5082 d = NULL;
5083 nd = 0;
5084 }
5085 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02005086 stack, nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005087 NULL, 0,
Victor Stinnerd8735722016-09-09 12:36:44 -07005088 kwnames, stack + nargs,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005089 d, (int)nd, kwdefs,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005090 closure, name, qualname);
5091}
5092
5093PyObject *
Victor Stinnerd8735722016-09-09 12:36:44 -07005094_PyFunction_FastCallKeywords(PyObject *func, PyObject **stack,
5095 Py_ssize_t nargs, PyObject *kwnames)
5096{
5097 return fast_function(func, stack, nargs, kwnames);
5098}
5099
5100PyObject *
Victor Stinner74319ae2016-08-25 00:04:09 +02005101_PyFunction_FastCallDict(PyObject *func, PyObject **args, Py_ssize_t nargs,
Victor Stinnerb9009392016-08-22 23:15:44 +02005102 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005103{
5104 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
5105 PyObject *globals = PyFunction_GET_GLOBALS(func);
5106 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
5107 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02005108 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005109 PyObject **d;
Victor Stinner74319ae2016-08-25 00:04:09 +02005110 Py_ssize_t nd, nk;
Victor Stinnerb9009392016-08-22 23:15:44 +02005111 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005112
Victor Stinner74319ae2016-08-25 00:04:09 +02005113 assert(func != NULL);
5114 assert(nargs >= 0);
5115 assert(nargs == 0 || args != NULL);
Victor Stinnerb9009392016-08-22 23:15:44 +02005116 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005117
Victor Stinner577e1f82016-08-25 00:29:32 +02005118 PCALL(PCALL_FUNCTION);
5119 PCALL(PCALL_FAST_FUNCTION);
5120
Victor Stinnerb9009392016-08-22 23:15:44 +02005121 if (co->co_kwonlyargcount == 0 &&
5122 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005123 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
5124 {
Victor Stinnerb9009392016-08-22 23:15:44 +02005125 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02005126 if (argdefs == NULL && co->co_argcount == nargs) {
Victor Stinnerd8735722016-09-09 12:36:44 -07005127 return _PyFunction_FastCall(co, args, nargs, globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005128 }
5129 else if (nargs == 0 && argdefs != NULL
5130 && co->co_argcount == Py_SIZE(argdefs)) {
5131 /* function called with no arguments, but all parameters have
5132 a default value: use default values as arguments .*/
5133 args = &PyTuple_GET_ITEM(argdefs, 0);
Victor Stinnerd8735722016-09-09 12:36:44 -07005134 return _PyFunction_FastCall(co, args, Py_SIZE(argdefs), globals);
Victor Stinner2eedc112016-08-22 12:29:42 +02005135 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005136 }
5137
Victor Stinnerb9009392016-08-22 23:15:44 +02005138 if (kwargs != NULL) {
5139 Py_ssize_t pos, i;
5140 nk = PyDict_Size(kwargs);
5141
5142 kwtuple = PyTuple_New(2 * nk);
5143 if (kwtuple == NULL) {
5144 return NULL;
5145 }
5146
5147 k = &PyTuple_GET_ITEM(kwtuple, 0);
5148 pos = i = 0;
5149 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
5150 Py_INCREF(k[i]);
5151 Py_INCREF(k[i+1]);
5152 i += 2;
5153 }
5154 nk = i / 2;
5155 }
5156 else {
5157 kwtuple = NULL;
5158 k = NULL;
5159 nk = 0;
5160 }
5161
Victor Stinner9be7e7b2016-08-19 16:11:43 +02005162 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
5163 closure = PyFunction_GET_CLOSURE(func);
5164 name = ((PyFunctionObject *)func) -> func_name;
5165 qualname = ((PyFunctionObject *)func) -> func_qualname;
5166
5167 if (argdefs != NULL) {
5168 d = &PyTuple_GET_ITEM(argdefs, 0);
5169 nd = Py_SIZE(argdefs);
5170 }
5171 else {
5172 d = NULL;
5173 nd = 0;
5174 }
Victor Stinnerb9009392016-08-22 23:15:44 +02005175
5176 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
5177 args, nargs,
Victor Stinner74319ae2016-08-25 00:04:09 +02005178 k, nk,
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005179 NULL, NULL,
Victor Stinnerb9009392016-08-22 23:15:44 +02005180 d, nd, kwdefs,
5181 closure, name, qualname);
5182 Py_XDECREF(kwtuple);
5183 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005184}
5185
5186static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005187do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005188{
Jeremy Hylton985eba52003-02-05 23:13:00 +00005189#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 /* At this point, we have to look at the type of func to
5191 update the call stats properly. Do it here so as to avoid
5192 exposing the call stats machinery outside ceval.c
5193 */
5194 if (PyFunction_Check(func))
5195 PCALL(PCALL_FUNCTION);
5196 else if (PyMethod_Check(func))
5197 PCALL(PCALL_METHOD);
5198 else if (PyType_Check(func))
5199 PCALL(PCALL_TYPE);
5200 else if (PyCFunction_Check(func))
5201 PCALL(PCALL_CFUNCTION);
5202 else
5203 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005204#endif
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005206 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005207 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 PyThreadState *tstate = PyThreadState_GET();
5209 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005210 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005212 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005213 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02005214 }
Jeremy Hylton52820442001-01-03 23:52:36 +00005215}
5216
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005217/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005218 nb_index slot defined, and store in *pi.
5219 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5220 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 +00005221 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005222*/
Tim Petersb5196382001-12-16 19:44:20 +00005223/* Note: If v is NULL, return success without storing into *pi. This
5224 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5225 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005226*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005227int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005228_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 if (v != NULL) {
5231 Py_ssize_t x;
5232 if (PyIndex_Check(v)) {
5233 x = PyNumber_AsSsize_t(v, NULL);
5234 if (x == -1 && PyErr_Occurred())
5235 return 0;
5236 }
5237 else {
5238 PyErr_SetString(PyExc_TypeError,
5239 "slice indices must be integers or "
5240 "None or have an __index__ method");
5241 return 0;
5242 }
5243 *pi = x;
5244 }
5245 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005246}
5247
Guido van Rossum486364b2007-06-30 05:01:58 +00005248#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005250
Guido van Rossumb209a111997-04-29 18:18:01 +00005251static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005252cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005253{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005254 int res = 0;
5255 switch (op) {
5256 case PyCmp_IS:
5257 res = (v == w);
5258 break;
5259 case PyCmp_IS_NOT:
5260 res = (v != w);
5261 break;
5262 case PyCmp_IN:
5263 res = PySequence_Contains(w, v);
5264 if (res < 0)
5265 return NULL;
5266 break;
5267 case PyCmp_NOT_IN:
5268 res = PySequence_Contains(w, v);
5269 if (res < 0)
5270 return NULL;
5271 res = !res;
5272 break;
5273 case PyCmp_EXC_MATCH:
5274 if (PyTuple_Check(w)) {
5275 Py_ssize_t i, length;
5276 length = PyTuple_Size(w);
5277 for (i = 0; i < length; i += 1) {
5278 PyObject *exc = PyTuple_GET_ITEM(w, i);
5279 if (!PyExceptionClass_Check(exc)) {
5280 PyErr_SetString(PyExc_TypeError,
5281 CANNOT_CATCH_MSG);
5282 return NULL;
5283 }
5284 }
5285 }
5286 else {
5287 if (!PyExceptionClass_Check(w)) {
5288 PyErr_SetString(PyExc_TypeError,
5289 CANNOT_CATCH_MSG);
5290 return NULL;
5291 }
5292 }
5293 res = PyErr_GivenExceptionMatches(v, w);
5294 break;
5295 default:
5296 return PyObject_RichCompare(v, w, op);
5297 }
5298 v = res ? Py_True : Py_False;
5299 Py_INCREF(v);
5300 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005301}
5302
Thomas Wouters52152252000-08-17 22:55:00 +00005303static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005304import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5305{
5306 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005307 PyObject *import_func, *res;
5308 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005309
5310 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5311 if (import_func == NULL) {
5312 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5313 return NULL;
5314 }
5315
5316 /* Fast path for not overloaded __import__. */
5317 if (import_func == PyThreadState_GET()->interp->import_func) {
5318 int ilevel = _PyLong_AsInt(level);
5319 if (ilevel == -1 && PyErr_Occurred()) {
5320 return NULL;
5321 }
5322 res = PyImport_ImportModuleLevelObject(
5323 name,
5324 f->f_globals,
5325 f->f_locals == NULL ? Py_None : f->f_locals,
5326 fromlist,
5327 ilevel);
5328 return res;
5329 }
5330
5331 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005332
5333 stack[0] = name;
5334 stack[1] = f->f_globals;
5335 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5336 stack[3] = fromlist;
5337 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005338 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005339 Py_DECREF(import_func);
5340 return res;
5341}
5342
5343static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005344import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005346 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005347 _Py_IDENTIFIER(__name__);
5348 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005350 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005351 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5352 return x;
5353 /* Issue #17636: in case this failed because of a circular relative
5354 import, try to fallback on reading the module directly from
5355 sys.modules. */
5356 PyErr_Clear();
5357 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005358 if (pkgname == NULL) {
5359 goto error;
5360 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005361 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5362 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005363 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005364 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005365 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005366 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005367 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005368 if (x == NULL) {
5369 goto error;
5370 }
5371 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005373 error:
5374 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5375 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005376}
Guido van Rossumac7be682001-01-17 15:42:30 +00005377
Thomas Wouters52152252000-08-17 22:55:00 +00005378static int
5379import_all_from(PyObject *locals, PyObject *v)
5380{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005381 _Py_IDENTIFIER(__all__);
5382 _Py_IDENTIFIER(__dict__);
5383 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 PyObject *dict, *name, *value;
5385 int skip_leading_underscores = 0;
5386 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005388 if (all == NULL) {
5389 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5390 return -1; /* Unexpected error */
5391 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005392 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005393 if (dict == NULL) {
5394 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5395 return -1;
5396 PyErr_SetString(PyExc_ImportError,
5397 "from-import-* object has no __dict__ and no __all__");
5398 return -1;
5399 }
5400 all = PyMapping_Keys(dict);
5401 Py_DECREF(dict);
5402 if (all == NULL)
5403 return -1;
5404 skip_leading_underscores = 1;
5405 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 for (pos = 0, err = 0; ; pos++) {
5408 name = PySequence_GetItem(all, pos);
5409 if (name == NULL) {
5410 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5411 err = -1;
5412 else
5413 PyErr_Clear();
5414 break;
5415 }
5416 if (skip_leading_underscores &&
5417 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005418 PyUnicode_READY(name) != -1 &&
5419 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005420 {
5421 Py_DECREF(name);
5422 continue;
5423 }
5424 value = PyObject_GetAttr(v, name);
5425 if (value == NULL)
5426 err = -1;
5427 else if (PyDict_CheckExact(locals))
5428 err = PyDict_SetItem(locals, name, value);
5429 else
5430 err = PyObject_SetItem(locals, name, value);
5431 Py_DECREF(name);
5432 Py_XDECREF(value);
5433 if (err != 0)
5434 break;
5435 }
5436 Py_DECREF(all);
5437 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005438}
5439
Guido van Rossumac7be682001-01-17 15:42:30 +00005440static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005441format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005443 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005445 if (!obj)
5446 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005448 obj_str = _PyUnicode_AsString(obj);
5449 if (!obj_str)
5450 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005451
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005453}
Guido van Rossum950361c1997-01-24 13:49:28 +00005454
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005455static void
5456format_exc_unbound(PyCodeObject *co, int oparg)
5457{
5458 PyObject *name;
5459 /* Don't stomp existing exception */
5460 if (PyErr_Occurred())
5461 return;
5462 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5463 name = PyTuple_GET_ITEM(co->co_cellvars,
5464 oparg);
5465 format_exc_check_arg(
5466 PyExc_UnboundLocalError,
5467 UNBOUNDLOCAL_ERROR_MSG,
5468 name);
5469 } else {
5470 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5471 PyTuple_GET_SIZE(co->co_cellvars));
5472 format_exc_check_arg(PyExc_NameError,
5473 UNBOUNDFREE_ERROR_MSG, name);
5474 }
5475}
5476
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005477static PyObject *
5478unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005479 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005480{
5481 PyObject *res;
5482 if (Py_REFCNT(v) == 2) {
5483 /* In the common case, there are 2 references to the value
5484 * stored in 'variable' when the += is performed: one on the
5485 * value stack (in 'v') and one still stored in the
5486 * 'variable'. We try to delete the variable now to reduce
5487 * the refcnt to 1.
5488 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005489 int opcode, oparg;
5490 NEXTOPARG();
5491 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005492 case STORE_FAST:
5493 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005494 PyObject **fastlocals = f->f_localsplus;
5495 if (GETLOCAL(oparg) == v)
5496 SETLOCAL(oparg, NULL);
5497 break;
5498 }
5499 case STORE_DEREF:
5500 {
5501 PyObject **freevars = (f->f_localsplus +
5502 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005503 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005504 if (PyCell_GET(c) == v)
5505 PyCell_Set(c, NULL);
5506 break;
5507 }
5508 case STORE_NAME:
5509 {
5510 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005511 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005512 PyObject *locals = f->f_locals;
5513 if (PyDict_CheckExact(locals) &&
5514 PyDict_GetItem(locals, name) == v) {
5515 if (PyDict_DelItem(locals, name) != 0) {
5516 PyErr_Clear();
5517 }
5518 }
5519 break;
5520 }
5521 }
5522 }
5523 res = v;
5524 PyUnicode_Append(&res, w);
5525 return res;
5526}
5527
Guido van Rossum950361c1997-01-24 13:49:28 +00005528#ifdef DYNAMIC_EXECUTION_PROFILE
5529
Skip Montanarof118cb12001-10-15 20:51:38 +00005530static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005531getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005533 int i;
5534 PyObject *l = PyList_New(256);
5535 if (l == NULL) return NULL;
5536 for (i = 0; i < 256; i++) {
5537 PyObject *x = PyLong_FromLong(a[i]);
5538 if (x == NULL) {
5539 Py_DECREF(l);
5540 return NULL;
5541 }
5542 PyList_SetItem(l, i, x);
5543 }
5544 for (i = 0; i < 256; i++)
5545 a[i] = 0;
5546 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005547}
5548
5549PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005550_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005551{
5552#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005554#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 int i;
5556 PyObject *l = PyList_New(257);
5557 if (l == NULL) return NULL;
5558 for (i = 0; i < 257; i++) {
5559 PyObject *x = getarray(dxpairs[i]);
5560 if (x == NULL) {
5561 Py_DECREF(l);
5562 return NULL;
5563 }
5564 PyList_SetItem(l, i, x);
5565 }
5566 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005567#endif
5568}
5569
5570#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005571
5572Py_ssize_t
5573_PyEval_RequestCodeExtraIndex(freefunc free)
5574{
5575 PyThreadState *tstate = PyThreadState_Get();
5576 Py_ssize_t new_index;
5577
5578 if (tstate->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
5579 return -1;
5580 }
5581 new_index = tstate->co_extra_user_count++;
5582 tstate->co_extra_freefuncs[new_index] = free;
5583 return new_index;
5584}