blob: 1d3bc90093d637fd8e910896004b8f051f03749d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000019#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Thomas Wouters477c8d52006-05-27 19:21:47 +000023#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000024
25#define READ_TIMESTAMP(var)
26
27#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000028
29typedef unsigned long long uint64;
30
Ezio Melotti13925002011-03-16 11:05:33 +020031/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000032 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
33 "__powerpc__" appears to be the correct one for Linux with GCC
34*/
35#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Michael W. Hudson75eabd22005-01-18 15:56:11 +000037#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000038
39static void
40ppc_getcounter(uint64 *v)
41{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020042 unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000043
44 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 asm volatile ("mftbu %0" : "=r" (tbu) );
46 asm volatile ("mftb %0" : "=r" (tb) );
47 asm volatile ("mftbu %0" : "=r" (tbu2));
48 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* The slightly peculiar way of writing the next lines is
51 compiled better by GCC than any other way I tried. */
52 ((long*)(v))[0] = tbu;
53 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000054}
55
Mark Dickinsona25b1312009-10-31 10:18:44 +000056#elif defined(__i386__)
57
58/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000059
Michael W. Hudson75eabd22005-01-18 15:56:11 +000060#define READ_TIMESTAMP(val) \
61 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000062
Mark Dickinsona25b1312009-10-31 10:18:44 +000063#elif defined(__x86_64__)
64
65/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
66 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
67 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
68 32-bit pieces of the result. */
69
Victor Stinner0b881dd2014-12-12 13:17:41 +010070#define READ_TIMESTAMP(val) do { \
71 unsigned int h, l; \
72 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
73 (val) = ((uint64)l) | (((uint64)h) << 32); \
74 } while(0)
Mark Dickinsona25b1312009-10-31 10:18:44 +000075
76
77#else
78
79#error "Don't know how to implement timestamp counter for this architecture"
80
Michael W. Hudson800ba232004-08-12 18:19:17 +000081#endif
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 uint64 intr, inst, loop;
87 PyThreadState *tstate = PyThreadState_Get();
88 if (!tstate->interp->tscdump)
89 return;
90 intr = intr1 - intr0;
91 inst = inst1 - inst0 - intr;
92 loop = loop1 - loop0 - intr;
93 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000094 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095}
Michael W. Hudson800ba232004-08-12 18:19:17 +000096
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000097#endif
98
Guido van Rossum04691fc1992-08-12 15:35:34 +000099/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +0000101
Guido van Rossum408027e1996-12-30 16:17:54 +0000102#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000103/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104#define LLTRACE 1 /* Low-level trace feature */
105#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000106#endif
107
Jeremy Hylton52820442001-01-03 23:52:36 +0000108typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000109
Guido van Rossum374a9221991-04-04 10:40:29 +0000110/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000115#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
117static PyObject * do_call(PyObject *, PyObject ***, int, int);
118static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000119static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
122static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000123#define CALL_FLAG_VAR 1
124#define CALL_FLAG_KW 2
125
Guido van Rossum0a066c01992-03-27 17:29:15 +0000126#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000127static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200128static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000129#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100130static int call_trace(Py_tracefunc, PyObject *,
131 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000133static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100134 PyThreadState *, PyFrameObject *,
135 int, PyObject *);
136static void call_exc_trace(Py_tracefunc, PyObject *,
137 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000138static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100139 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000140
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141static PyObject * cmp_outcome(int, PyObject *, PyObject *);
142static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000143static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000144static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000145static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200146static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300147 PyFrameObject *, const unsigned short *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500148static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000149
Paul Prescode68140d2000-08-30 20:25:01 +0000150#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000152#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000154#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 "free variable '%.200s' referenced before assignment" \
156 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000157
Guido van Rossum950361c1997-01-24 13:49:28 +0000158/* Dynamic execution profile */
159#ifdef DYNAMIC_EXECUTION_PROFILE
160#ifdef DXPAIRS
161static long dxpairs[257][256];
162#define dxp dxpairs[256]
163#else
164static long dxp[256];
165#endif
166#endif
167
Jeremy Hylton985eba52003-02-05 23:13:00 +0000168/* Function call profile */
169#ifdef CALL_PROFILE
170#define PCALL_NUM 11
171static int pcall[PCALL_NUM];
172
173#define PCALL_ALL 0
174#define PCALL_FUNCTION 1
175#define PCALL_FAST_FUNCTION 2
176#define PCALL_FASTER_FUNCTION 3
177#define PCALL_METHOD 4
178#define PCALL_BOUND_METHOD 5
179#define PCALL_CFUNCTION 6
180#define PCALL_TYPE 7
181#define PCALL_GENERATOR 8
182#define PCALL_OTHER 9
183#define PCALL_POP 10
184
185/* Notes about the statistics
186
187 PCALL_FAST stats
188
189 FAST_FUNCTION means no argument tuple needs to be created.
190 FASTER_FUNCTION means that the fast-path frame setup code is used.
191
192 If there is a method call where the call can be optimized by changing
193 the argument tuple and calling the function directly, it gets recorded
194 twice.
195
196 As a result, the relationship among the statistics appears to be
197 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
198 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
199 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
200 PCALL_METHOD > PCALL_BOUND_METHOD
201*/
202
203#define PCALL(POS) pcall[POS]++
204
205PyObject *
206PyEval_GetCallStats(PyObject *self)
207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 return Py_BuildValue("iiiiiiiiiii",
209 pcall[0], pcall[1], pcall[2], pcall[3],
210 pcall[4], pcall[5], pcall[6], pcall[7],
211 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000212}
213#else
214#define PCALL(O)
215
216PyObject *
217PyEval_GetCallStats(PyObject *self)
218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 Py_INCREF(Py_None);
220 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000221}
222#endif
223
Tim Peters5ca576e2001-06-18 22:08:13 +0000224
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000225#ifdef WITH_THREAD
226#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
227#else
228#define GIL_REQUEST 0
229#endif
230
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000231/* This can set eval_breaker to 0 even though gil_drop_request became
232 1. We believe this is all right because the eval loop will release
233 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000234#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 _Py_atomic_store_relaxed( \
236 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000237 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
239 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000240
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000241#ifdef WITH_THREAD
242
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 do { \
245 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
246 _Py_atomic_store_relaxed(&eval_breaker, 1); \
247 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248
249#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 do { \
251 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
252 COMPUTE_EVAL_BREAKER(); \
253 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000255#endif
256
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000257/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 do { \
260 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
261 _Py_atomic_store_relaxed(&eval_breaker, 1); \
262 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000263
264#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 do { \
266 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
267 COMPUTE_EVAL_BREAKER(); \
268 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000269
270#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 do { \
272 pending_async_exc = 1; \
273 _Py_atomic_store_relaxed(&eval_breaker, 1); \
274 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000275
276#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000278
279
Guido van Rossume59214e1994-08-30 08:01:59 +0000280#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000283#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000284#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000285#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000286
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000287static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000288static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000289/* This single variable consolidates all requests to break out of the fast path
290 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000291static _Py_atomic_int eval_breaker = {0};
292/* Request for dropping the GIL */
293static _Py_atomic_int gil_drop_request = {0};
294/* Request for running pending calls. */
295static _Py_atomic_int pendingcalls_to_do = {0};
296/* Request for looking at the `async_exc` field of the current thread state.
297 Guarded by the GIL. */
298static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000299
300#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000301
Tim Peters7f468f22004-10-11 02:40:51 +0000302int
303PyEval_ThreadsInitialized(void)
304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000306}
307
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000309PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000311 if (gil_created())
312 return;
313 create_gil();
314 take_gil(PyThreadState_GET());
315 main_thread = PyThread_get_thread_ident();
316 if (!pending_lock)
317 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000318}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000319
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000320void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000321_PyEval_FiniThreads(void)
322{
323 if (!gil_created())
324 return;
325 destroy_gil();
326 assert(!gil_created());
327}
328
329void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000330PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 PyThreadState *tstate = PyThreadState_GET();
333 if (tstate == NULL)
334 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
335 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000336}
337
338void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 /* This function must succeed when the current thread state is NULL.
342 We therefore avoid PyThreadState_GET() which dumps a fatal error
343 in debug mode.
344 */
345 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
346 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000347}
348
349void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000350PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 if (tstate == NULL)
353 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
354 /* Check someone has called PyEval_InitThreads() to create the lock */
355 assert(gil_created());
356 take_gil(tstate);
357 if (PyThreadState_Swap(tstate) != NULL)
358 Py_FatalError(
359 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000360}
361
362void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000363PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 if (tstate == NULL)
366 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
367 if (PyThreadState_Swap(NULL) != tstate)
368 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
369 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000370}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000371
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200372/* This function is called from PyOS_AfterFork to destroy all threads which are
373 * not running in the child process, and clear internal locks which might be
374 * held by those threads. (This could also be done using pthread_atfork
375 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000376
377void
378PyEval_ReInitThreads(void)
379{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200380 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200382 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 if (!gil_created())
385 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 recreate_gil();
387 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200388 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 /* Update the threading module with the new state.
392 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200393 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 "threading");
395 if (threading == NULL) {
396 /* threading not imported */
397 PyErr_Clear();
398 return;
399 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200400 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 if (result == NULL)
402 PyErr_WriteUnraisable(threading);
403 else
404 Py_DECREF(result);
405 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200406
407 /* Destroy all threads except the current one */
408 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000409}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000410
411#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000412static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000413static int pending_async_exc = 0;
414#endif /* WITH_THREAD */
415
416/* This function is used to signal that async exceptions are waiting to be
417 raised, therefore it is also useful in non-threaded builds. */
418
419void
420_PyEval_SignalAsyncExc(void)
421{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000423}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424
Guido van Rossumff4949e1992-08-05 19:58:53 +0000425/* Functions save_thread and restore_thread are always defined so
426 dynamically loaded modules needn't be compiled separately for use
427 with and without threads: */
428
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000429PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000430PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 PyThreadState *tstate = PyThreadState_Swap(NULL);
433 if (tstate == NULL)
434 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000435#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (gil_created())
437 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000438#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440}
441
442void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000443PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 if (tstate == NULL)
446 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000447#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 if (gil_created()) {
449 int err = errno;
450 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200451 /* _Py_Finalizing is protected by the GIL */
452 if (_Py_Finalizing && tstate != _Py_Finalizing) {
453 drop_gil(tstate);
454 PyThread_exit_thread();
455 assert(0); /* unreachable */
456 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 errno = err;
458 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000459#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000461}
462
463
Guido van Rossuma9672091994-09-14 13:31:22 +0000464/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
465 signal handlers or Mac I/O completion routines) can schedule calls
466 to a function to be called synchronously.
467 The synchronous function is called with one void* argument.
468 It should return 0 for success or -1 for failure -- failure should
469 be accompanied by an exception.
470
471 If registry succeeds, the registry function returns 0; if it fails
472 (e.g. due to too many pending calls) it returns -1 (without setting
473 an exception condition).
474
475 Note that because registry may occur from within signal handlers,
476 or other asynchronous events, calling malloc() is unsafe!
477
478#ifdef WITH_THREAD
479 Any thread can schedule pending calls, but only the main thread
480 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481 There is no facility to schedule calls to a particular thread, but
482 that should be easy to change, should that ever be required. In
483 that case, the static variables here should go into the python
484 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000485#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000486*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000487
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488#ifdef WITH_THREAD
489
490/* The WITH_THREAD implementation is thread-safe. It allows
491 scheduling to be made from any thread, and even from an executing
492 callback.
493 */
494
495#define NPENDINGCALLS 32
496static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 int (*func)(void *);
498 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000499} pendingcalls[NPENDINGCALLS];
500static int pendingfirst = 0;
501static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000502
503int
504Py_AddPendingCall(int (*func)(void *), void *arg)
505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 int i, j, result=0;
507 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 /* try a few times for the lock. Since this mechanism is used
510 * for signal handling (on the main thread), there is a (slim)
511 * chance that a signal is delivered on the same thread while we
512 * hold the lock during the Py_MakePendingCalls() function.
513 * This avoids a deadlock in that case.
514 * Note that signals can be delivered on any thread. In particular,
515 * on Windows, a SIGINT is delivered on a system-created worker
516 * thread.
517 * We also check for lock being NULL, in the unlikely case that
518 * this function is called before any bytecode evaluation takes place.
519 */
520 if (lock != NULL) {
521 for (i = 0; i<100; i++) {
522 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
523 break;
524 }
525 if (i == 100)
526 return -1;
527 }
528
529 i = pendinglast;
530 j = (i + 1) % NPENDINGCALLS;
531 if (j == pendingfirst) {
532 result = -1; /* Queue full */
533 } else {
534 pendingcalls[i].func = func;
535 pendingcalls[i].arg = arg;
536 pendinglast = j;
537 }
538 /* signal main loop */
539 SIGNAL_PENDING_CALLS();
540 if (lock != NULL)
541 PyThread_release_lock(lock);
542 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000543}
544
545int
546Py_MakePendingCalls(void)
547{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200548 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 int i;
550 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 if (!pending_lock) {
553 /* initial allocation of the lock */
554 pending_lock = PyThread_allocate_lock();
555 if (pending_lock == NULL)
556 return -1;
557 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* only service pending calls on main thread */
560 if (main_thread && PyThread_get_thread_ident() != main_thread)
561 return 0;
562 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200563 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200565 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 /* perform a bounded number of calls, in case of recursion */
567 for (i=0; i<NPENDINGCALLS; i++) {
568 int j;
569 int (*func)(void *);
570 void *arg = NULL;
571
572 /* pop one item off the queue while holding the lock */
573 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
574 j = pendingfirst;
575 if (j == pendinglast) {
576 func = NULL; /* Queue empty */
577 } else {
578 func = pendingcalls[j].func;
579 arg = pendingcalls[j].arg;
580 pendingfirst = (j + 1) % NPENDINGCALLS;
581 }
582 if (pendingfirst != pendinglast)
583 SIGNAL_PENDING_CALLS();
584 else
585 UNSIGNAL_PENDING_CALLS();
586 PyThread_release_lock(pending_lock);
587 /* having released the lock, perform the callback */
588 if (func == NULL)
589 break;
590 r = func(arg);
591 if (r)
592 break;
593 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200594 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000596}
597
598#else /* if ! defined WITH_THREAD */
599
600/*
601 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
602 This code is used for signal handling in python that isn't built
603 with WITH_THREAD.
604 Don't use this implementation when Py_AddPendingCalls() can happen
605 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606
Guido van Rossuma9672091994-09-14 13:31:22 +0000607 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000608 (1) nested asynchronous calls to Py_AddPendingCall()
609 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000611 (1) is very unlikely because typically signal delivery
612 is blocked during signal handling. So it should be impossible.
613 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000614 The current code is safe against (2), but not against (1).
615 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000616 thread is present, interrupted by signals, and that the critical
617 section is protected with the "busy" variable. On Windows, which
618 delivers SIGINT on a system thread, this does not hold and therefore
619 Windows really shouldn't use this version.
620 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000621*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000622
Guido van Rossuma9672091994-09-14 13:31:22 +0000623#define NPENDINGCALLS 32
624static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 int (*func)(void *);
626 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000627} pendingcalls[NPENDINGCALLS];
628static volatile int pendingfirst = 0;
629static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000630static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000631
632int
Thomas Wouters334fb892000-07-25 12:56:38 +0000633Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 static volatile int busy = 0;
636 int i, j;
637 /* XXX Begin critical section */
638 if (busy)
639 return -1;
640 busy = 1;
641 i = pendinglast;
642 j = (i + 1) % NPENDINGCALLS;
643 if (j == pendingfirst) {
644 busy = 0;
645 return -1; /* Queue full */
646 }
647 pendingcalls[i].func = func;
648 pendingcalls[i].arg = arg;
649 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651 SIGNAL_PENDING_CALLS();
652 busy = 0;
653 /* XXX End critical section */
654 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000655}
656
Guido van Rossum180d7b41994-09-29 09:45:57 +0000657int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000658Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 static int busy = 0;
661 if (busy)
662 return 0;
663 busy = 1;
664 UNSIGNAL_PENDING_CALLS();
665 for (;;) {
666 int i;
667 int (*func)(void *);
668 void *arg;
669 i = pendingfirst;
670 if (i == pendinglast)
671 break; /* Queue empty */
672 func = pendingcalls[i].func;
673 arg = pendingcalls[i].arg;
674 pendingfirst = (i + 1) % NPENDINGCALLS;
675 if (func(arg) < 0) {
676 busy = 0;
677 SIGNAL_PENDING_CALLS(); /* We're not done yet */
678 return -1;
679 }
680 }
681 busy = 0;
682 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000683}
684
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000685#endif /* WITH_THREAD */
686
Guido van Rossuma9672091994-09-14 13:31:22 +0000687
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688/* The interpreter's recursion limit */
689
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000690#ifndef Py_DEFAULT_RECURSION_LIMIT
691#define Py_DEFAULT_RECURSION_LIMIT 1000
692#endif
693static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
694int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000695
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000696int
697Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700}
701
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000702void
703Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 recursion_limit = new_limit;
706 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000707}
708
Armin Rigo2b3eb402003-10-28 12:05:48 +0000709/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
710 if the recursion_depth reaches _Py_CheckRecursionLimit.
711 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
712 to guarantee that _Py_CheckRecursiveCall() is regularly called.
713 Without USE_STACKCHECK, there is no need for this. */
714int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300715_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000718
719#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 if (PyOS_CheckStack()) {
721 --tstate->recursion_depth;
722 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
723 return -1;
724 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000725#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 _Py_CheckRecursionLimit = recursion_limit;
727 if (tstate->recursion_critical)
728 /* Somebody asked that we don't check for recursion. */
729 return 0;
730 if (tstate->overflowed) {
731 if (tstate->recursion_depth > recursion_limit + 50) {
732 /* Overflowing while handling an overflow. Give up. */
733 Py_FatalError("Cannot recover from stack overflow.");
734 }
735 return 0;
736 }
737 if (tstate->recursion_depth > recursion_limit) {
738 --tstate->recursion_depth;
739 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400740 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 "maximum recursion depth exceeded%s",
742 where);
743 return -1;
744 }
745 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000746}
747
Guido van Rossum374a9221991-04-04 10:40:29 +0000748/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000749enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000750 WHY_NOT = 0x0001, /* No error */
751 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000752 WHY_RETURN = 0x0008, /* 'return' statement */
753 WHY_BREAK = 0x0010, /* 'break' statement */
754 WHY_CONTINUE = 0x0020, /* 'continue' statement */
755 WHY_YIELD = 0x0040, /* 'yield' operator */
756 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000757};
Guido van Rossum374a9221991-04-04 10:40:29 +0000758
Benjamin Peterson87880242011-07-03 16:48:31 -0500759static void save_exc_state(PyThreadState *, PyFrameObject *);
760static void swap_exc_state(PyThreadState *, PyFrameObject *);
761static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400762static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000763static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000764
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000765/* Records whether tracing is on for any thread. Counts the number of
766 threads for which tstate->c_tracefunc is non-NULL, so if the value
767 is 0, we know we don't have to check this thread's c_tracefunc.
768 This speeds up the if statement in PyEval_EvalFrameEx() after
769 fast_next_opcode*/
770static int _Py_TracingPossible = 0;
771
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000772
Guido van Rossum374a9221991-04-04 10:40:29 +0000773
Guido van Rossumb209a111997-04-29 18:18:01 +0000774PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000775PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777 return PyEval_EvalCodeEx(co,
778 globals, locals,
779 (PyObject **)NULL, 0,
780 (PyObject **)NULL, 0,
781 (PyObject **)NULL, 0,
782 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000783}
784
785
786/* Interpreter main loop */
787
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000788PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000789PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 /* This is for backward compatibility with extension modules that
791 used this API; core interpreter code should call
792 PyEval_EvalFrameEx() */
793 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000794}
795
796PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000797PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000798{
Guido van Rossum950361c1997-01-24 13:49:28 +0000799#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000801#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200802 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300803 const unsigned short *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200804 int opcode; /* Current opcode */
805 int oparg; /* Current opcode argument, if any */
806 enum why_code why; /* Reason for block stack unwind */
807 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 PyObject *retval = NULL; /* Return value */
809 PyThreadState *tstate = PyThreadState_GET();
810 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 is true when the line being executed has changed. The
817 initial values are such as to make this false the first
818 time it is tested. */
819 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000820
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300821 const unsigned short *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 PyObject *names;
823 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000824
Brett Cannon368b4b72012-04-02 12:17:59 -0400825#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200826 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400827#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200828
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829/* Computed GOTOs, or
830 the-optimization-commonly-but-improperly-known-as-"threaded code"
831 using gcc's labels-as-values extension
832 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
833
834 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000836 combined with a lookup table of jump addresses. However, since the
837 indirect jump instruction is shared by all opcodes, the CPU will have a
838 hard time making the right prediction for where to jump next (actually,
839 it will be always wrong except in the uncommon case of a sequence of
840 several identical opcodes).
841
842 "Threaded code" in contrast, uses an explicit jump table and an explicit
843 indirect jump instruction at the end of each opcode. Since the jump
844 instruction is at a different address for each opcode, the CPU will make a
845 separate prediction for each of these instructions, which is equivalent to
846 predicting the second opcode of each opcode pair. These predictions have
847 a much better chance to turn out valid, especially in small bytecode loops.
848
849 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000851 and potentially many more instructions (depending on the pipeline width).
852 A correctly predicted branch, however, is nearly free.
853
854 At the time of this writing, the "threaded code" version is up to 15-20%
855 faster than the normal "switch" version, depending on the compiler and the
856 CPU architecture.
857
858 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
859 because it would render the measurements invalid.
860
861
862 NOTE: care must be taken that the compiler doesn't try to "optimize" the
863 indirect jumps by sharing them between all opcodes. Such optimizations
864 can be disabled on gcc by using the -fno-gcse flag (or possibly
865 -fno-crossjumping).
866*/
867
Antoine Pitrou042b1282010-08-13 21:15:58 +0000868#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000869#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000870#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#endif
872
Antoine Pitrou042b1282010-08-13 21:15:58 +0000873#ifdef HAVE_COMPUTED_GOTOS
874 #ifndef USE_COMPUTED_GOTOS
875 #define USE_COMPUTED_GOTOS 1
876 #endif
877#else
878 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
879 #error "Computed gotos are not supported on this compiler."
880 #endif
881 #undef USE_COMPUTED_GOTOS
882 #define USE_COMPUTED_GOTOS 0
883#endif
884
885#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000886/* Import the static jump table */
887#include "opcode_targets.h"
888
Antoine Pitroub52ec782009-01-25 16:34:23 +0000889#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000892
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 { \
895 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
896 FAST_DISPATCH(); \
897 } \
898 continue; \
899 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900
901#ifdef LLTRACE
902#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 { \
904 if (!lltrace && !_Py_TracingPossible) { \
905 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300906 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300907 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 } \
909 goto fast_next_opcode; \
910 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000911#else
912#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 { \
914 if (!_Py_TracingPossible) { \
915 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300916 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300917 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 } \
919 goto fast_next_opcode; \
920 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000921#endif
922
923#else
924#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000925 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300926
Antoine Pitroub52ec782009-01-25 16:34:23 +0000927#define DISPATCH() continue
928#define FAST_DISPATCH() goto fast_next_opcode
929#endif
930
931
Neal Norwitza81d2202002-07-14 00:27:26 +0000932/* Tuple access macros */
933
934#ifndef Py_DEBUG
935#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
936#else
937#define GETITEM(v, i) PyTuple_GetItem((v), (i))
938#endif
939
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000940#ifdef WITH_TSC
941/* Use Pentium timestamp counter to mark certain events:
942 inst0 -- beginning of switch statement for opcode dispatch
943 inst1 -- end of switch statement (may be skipped)
944 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000945 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000946 (may be skipped)
947 intr1 -- beginning of long interruption
948 intr2 -- end of long interruption
949
950 Many opcodes call out to helper C functions. In some cases, the
951 time in those functions should be counted towards the time for the
952 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
953 calls another Python function; there's no point in charge all the
954 bytecode executed by the called function to the caller.
955
956 It's hard to make a useful judgement statically. In the presence
957 of operator overloading, it's impossible to tell if a call will
958 execute new Python code or not.
959
960 It's a case-by-case judgement. I'll use intr1 for the following
961 cases:
962
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000963 IMPORT_STAR
964 IMPORT_FROM
965 CALL_FUNCTION (and friends)
966
967 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
969 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 READ_TIMESTAMP(inst0);
972 READ_TIMESTAMP(inst1);
973 READ_TIMESTAMP(loop0);
974 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 /* shut up the compiler */
977 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978#endif
979
Guido van Rossum374a9221991-04-04 10:40:29 +0000980/* Code access macros */
981
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300982#ifdef WORDS_BIGENDIAN
983 #define OPCODE(word) ((word) >> 8)
984 #define OPARG(word) ((word) & 255)
985#else
986 #define OPCODE(word) ((word) & 255)
987 #define OPARG(word) ((word) >> 8)
988#endif
989/* The integer overflow is checked by an assertion below. */
990#define INSTR_OFFSET() (2*(int)(next_instr - first_instr))
991#define NEXTOPARG() do { \
992 unsigned short word = *next_instr; \
993 opcode = OPCODE(word); \
994 oparg = OPARG(word); \
995 next_instr++; \
996 } while (0)
997#define JUMPTO(x) (next_instr = first_instr + (x)/2)
998#define JUMPBY(x) (next_instr += (x)/2)
Guido van Rossum374a9221991-04-04 10:40:29 +0000999
Raymond Hettingerf606f872003-03-16 03:11:04 +00001000/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Some opcodes tend to come in pairs thus making it possible to
1002 predict the second code when the first is run. For example,
1003 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1004 those opcodes are often followed by a POP_TOP.
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 Selivanov5376ba92015-06-22 12:19:30 -04001200 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
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
1382 TARGET(LOAD_CONST) {
1383 PyObject *value = GETITEM(consts, oparg);
1384 Py_INCREF(value);
1385 PUSH(value);
1386 FAST_DISPATCH();
1387 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001388
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001389 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 TARGET(STORE_FAST) {
1391 PyObject *value = POP();
1392 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(POP_TOP) {
1397 PyObject *value = POP();
1398 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 TARGET(ROT_TWO) {
1403 PyObject *top = TOP();
1404 PyObject *second = SECOND();
1405 SET_TOP(second);
1406 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 TARGET(ROT_THREE) {
1411 PyObject *top = TOP();
1412 PyObject *second = SECOND();
1413 PyObject *third = THIRD();
1414 SET_TOP(second);
1415 SET_SECOND(third);
1416 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001419
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 TARGET(DUP_TOP) {
1421 PyObject *top = TOP();
1422 Py_INCREF(top);
1423 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 TARGET(DUP_TOP_TWO) {
1428 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001430 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001432 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 SET_TOP(top);
1434 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001435 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001436 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001437
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001438 TARGET(UNARY_POSITIVE) {
1439 PyObject *value = TOP();
1440 PyObject *res = PyNumber_Positive(value);
1441 Py_DECREF(value);
1442 SET_TOP(res);
1443 if (res == NULL)
1444 goto error;
1445 DISPATCH();
1446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 TARGET(UNARY_NEGATIVE) {
1449 PyObject *value = TOP();
1450 PyObject *res = PyNumber_Negative(value);
1451 Py_DECREF(value);
1452 SET_TOP(res);
1453 if (res == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 TARGET(UNARY_NOT) {
1459 PyObject *value = TOP();
1460 int err = PyObject_IsTrue(value);
1461 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 if (err == 0) {
1463 Py_INCREF(Py_True);
1464 SET_TOP(Py_True);
1465 DISPATCH();
1466 }
1467 else if (err > 0) {
1468 Py_INCREF(Py_False);
1469 SET_TOP(Py_False);
1470 err = 0;
1471 DISPATCH();
1472 }
1473 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 goto error;
1475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 TARGET(UNARY_INVERT) {
1478 PyObject *value = TOP();
1479 PyObject *res = PyNumber_Invert(value);
1480 Py_DECREF(value);
1481 SET_TOP(res);
1482 if (res == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 TARGET(BINARY_POWER) {
1488 PyObject *exp = POP();
1489 PyObject *base = TOP();
1490 PyObject *res = PyNumber_Power(base, exp, Py_None);
1491 Py_DECREF(base);
1492 Py_DECREF(exp);
1493 SET_TOP(res);
1494 if (res == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 TARGET(BINARY_MULTIPLY) {
1500 PyObject *right = POP();
1501 PyObject *left = TOP();
1502 PyObject *res = PyNumber_Multiply(left, right);
1503 Py_DECREF(left);
1504 Py_DECREF(right);
1505 SET_TOP(res);
1506 if (res == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Benjamin Petersond51374e2014-04-09 23:55:56 -04001511 TARGET(BINARY_MATRIX_MULTIPLY) {
1512 PyObject *right = POP();
1513 PyObject *left = TOP();
1514 PyObject *res = PyNumber_MatrixMultiply(left, right);
1515 Py_DECREF(left);
1516 Py_DECREF(right);
1517 SET_TOP(res);
1518 if (res == NULL)
1519 goto error;
1520 DISPATCH();
1521 }
1522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 TARGET(BINARY_TRUE_DIVIDE) {
1524 PyObject *divisor = POP();
1525 PyObject *dividend = TOP();
1526 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1527 Py_DECREF(dividend);
1528 Py_DECREF(divisor);
1529 SET_TOP(quotient);
1530 if (quotient == NULL)
1531 goto error;
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 TARGET(BINARY_FLOOR_DIVIDE) {
1536 PyObject *divisor = POP();
1537 PyObject *dividend = TOP();
1538 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1539 Py_DECREF(dividend);
1540 Py_DECREF(divisor);
1541 SET_TOP(quotient);
1542 if (quotient == NULL)
1543 goto error;
1544 DISPATCH();
1545 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 TARGET(BINARY_MODULO) {
1548 PyObject *divisor = POP();
1549 PyObject *dividend = TOP();
1550 PyObject *res = PyUnicode_CheckExact(dividend) ?
1551 PyUnicode_Format(dividend, divisor) :
1552 PyNumber_Remainder(dividend, divisor);
1553 Py_DECREF(divisor);
1554 Py_DECREF(dividend);
1555 SET_TOP(res);
1556 if (res == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 TARGET(BINARY_ADD) {
1562 PyObject *right = POP();
1563 PyObject *left = TOP();
1564 PyObject *sum;
1565 if (PyUnicode_CheckExact(left) &&
1566 PyUnicode_CheckExact(right)) {
1567 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001568 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001569 }
1570 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 sum = PyNumber_Add(left, right);
1572 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001573 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 Py_DECREF(right);
1575 SET_TOP(sum);
1576 if (sum == NULL)
1577 goto error;
1578 DISPATCH();
1579 }
1580
1581 TARGET(BINARY_SUBTRACT) {
1582 PyObject *right = POP();
1583 PyObject *left = TOP();
1584 PyObject *diff = PyNumber_Subtract(left, right);
1585 Py_DECREF(right);
1586 Py_DECREF(left);
1587 SET_TOP(diff);
1588 if (diff == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
1592
1593 TARGET(BINARY_SUBSCR) {
1594 PyObject *sub = POP();
1595 PyObject *container = TOP();
1596 PyObject *res = PyObject_GetItem(container, sub);
1597 Py_DECREF(container);
1598 Py_DECREF(sub);
1599 SET_TOP(res);
1600 if (res == NULL)
1601 goto error;
1602 DISPATCH();
1603 }
1604
1605 TARGET(BINARY_LSHIFT) {
1606 PyObject *right = POP();
1607 PyObject *left = TOP();
1608 PyObject *res = PyNumber_Lshift(left, right);
1609 Py_DECREF(left);
1610 Py_DECREF(right);
1611 SET_TOP(res);
1612 if (res == NULL)
1613 goto error;
1614 DISPATCH();
1615 }
1616
1617 TARGET(BINARY_RSHIFT) {
1618 PyObject *right = POP();
1619 PyObject *left = TOP();
1620 PyObject *res = PyNumber_Rshift(left, right);
1621 Py_DECREF(left);
1622 Py_DECREF(right);
1623 SET_TOP(res);
1624 if (res == NULL)
1625 goto error;
1626 DISPATCH();
1627 }
1628
1629 TARGET(BINARY_AND) {
1630 PyObject *right = POP();
1631 PyObject *left = TOP();
1632 PyObject *res = PyNumber_And(left, right);
1633 Py_DECREF(left);
1634 Py_DECREF(right);
1635 SET_TOP(res);
1636 if (res == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
1640
1641 TARGET(BINARY_XOR) {
1642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *res = PyNumber_Xor(left, right);
1645 Py_DECREF(left);
1646 Py_DECREF(right);
1647 SET_TOP(res);
1648 if (res == NULL)
1649 goto error;
1650 DISPATCH();
1651 }
1652
1653 TARGET(BINARY_OR) {
1654 PyObject *right = POP();
1655 PyObject *left = TOP();
1656 PyObject *res = PyNumber_Or(left, right);
1657 Py_DECREF(left);
1658 Py_DECREF(right);
1659 SET_TOP(res);
1660 if (res == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
1664
1665 TARGET(LIST_APPEND) {
1666 PyObject *v = POP();
1667 PyObject *list = PEEK(oparg);
1668 int err;
1669 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 if (err != 0)
1672 goto error;
1673 PREDICT(JUMP_ABSOLUTE);
1674 DISPATCH();
1675 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 TARGET(SET_ADD) {
1678 PyObject *v = POP();
1679 PyObject *set = stack_pointer[-oparg];
1680 int err;
1681 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 if (err != 0)
1684 goto error;
1685 PREDICT(JUMP_ABSOLUTE);
1686 DISPATCH();
1687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 TARGET(INPLACE_POWER) {
1690 PyObject *exp = POP();
1691 PyObject *base = TOP();
1692 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1693 Py_DECREF(base);
1694 Py_DECREF(exp);
1695 SET_TOP(res);
1696 if (res == NULL)
1697 goto error;
1698 DISPATCH();
1699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 TARGET(INPLACE_MULTIPLY) {
1702 PyObject *right = POP();
1703 PyObject *left = TOP();
1704 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1705 Py_DECREF(left);
1706 Py_DECREF(right);
1707 SET_TOP(res);
1708 if (res == NULL)
1709 goto error;
1710 DISPATCH();
1711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Benjamin Petersond51374e2014-04-09 23:55:56 -04001713 TARGET(INPLACE_MATRIX_MULTIPLY) {
1714 PyObject *right = POP();
1715 PyObject *left = TOP();
1716 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1717 Py_DECREF(left);
1718 Py_DECREF(right);
1719 SET_TOP(res);
1720 if (res == NULL)
1721 goto error;
1722 DISPATCH();
1723 }
1724
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 TARGET(INPLACE_TRUE_DIVIDE) {
1726 PyObject *divisor = POP();
1727 PyObject *dividend = TOP();
1728 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1729 Py_DECREF(dividend);
1730 Py_DECREF(divisor);
1731 SET_TOP(quotient);
1732 if (quotient == NULL)
1733 goto error;
1734 DISPATCH();
1735 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 TARGET(INPLACE_FLOOR_DIVIDE) {
1738 PyObject *divisor = POP();
1739 PyObject *dividend = TOP();
1740 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1741 Py_DECREF(dividend);
1742 Py_DECREF(divisor);
1743 SET_TOP(quotient);
1744 if (quotient == NULL)
1745 goto error;
1746 DISPATCH();
1747 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001748
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 TARGET(INPLACE_MODULO) {
1750 PyObject *right = POP();
1751 PyObject *left = TOP();
1752 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1753 Py_DECREF(left);
1754 Py_DECREF(right);
1755 SET_TOP(mod);
1756 if (mod == NULL)
1757 goto error;
1758 DISPATCH();
1759 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 TARGET(INPLACE_ADD) {
1762 PyObject *right = POP();
1763 PyObject *left = TOP();
1764 PyObject *sum;
1765 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1766 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001767 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001768 }
1769 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 sum = PyNumber_InPlaceAdd(left, right);
1771 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001772 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 Py_DECREF(right);
1774 SET_TOP(sum);
1775 if (sum == NULL)
1776 goto error;
1777 DISPATCH();
1778 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001780 TARGET(INPLACE_SUBTRACT) {
1781 PyObject *right = POP();
1782 PyObject *left = TOP();
1783 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1784 Py_DECREF(left);
1785 Py_DECREF(right);
1786 SET_TOP(diff);
1787 if (diff == NULL)
1788 goto error;
1789 DISPATCH();
1790 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001791
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 TARGET(INPLACE_LSHIFT) {
1793 PyObject *right = POP();
1794 PyObject *left = TOP();
1795 PyObject *res = PyNumber_InPlaceLshift(left, right);
1796 Py_DECREF(left);
1797 Py_DECREF(right);
1798 SET_TOP(res);
1799 if (res == NULL)
1800 goto error;
1801 DISPATCH();
1802 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001803
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 TARGET(INPLACE_RSHIFT) {
1805 PyObject *right = POP();
1806 PyObject *left = TOP();
1807 PyObject *res = PyNumber_InPlaceRshift(left, right);
1808 Py_DECREF(left);
1809 Py_DECREF(right);
1810 SET_TOP(res);
1811 if (res == NULL)
1812 goto error;
1813 DISPATCH();
1814 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001815
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 TARGET(INPLACE_AND) {
1817 PyObject *right = POP();
1818 PyObject *left = TOP();
1819 PyObject *res = PyNumber_InPlaceAnd(left, right);
1820 Py_DECREF(left);
1821 Py_DECREF(right);
1822 SET_TOP(res);
1823 if (res == NULL)
1824 goto error;
1825 DISPATCH();
1826 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 TARGET(INPLACE_XOR) {
1829 PyObject *right = POP();
1830 PyObject *left = TOP();
1831 PyObject *res = PyNumber_InPlaceXor(left, right);
1832 Py_DECREF(left);
1833 Py_DECREF(right);
1834 SET_TOP(res);
1835 if (res == NULL)
1836 goto error;
1837 DISPATCH();
1838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 TARGET(INPLACE_OR) {
1841 PyObject *right = POP();
1842 PyObject *left = TOP();
1843 PyObject *res = PyNumber_InPlaceOr(left, right);
1844 Py_DECREF(left);
1845 Py_DECREF(right);
1846 SET_TOP(res);
1847 if (res == NULL)
1848 goto error;
1849 DISPATCH();
1850 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001851
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 TARGET(STORE_SUBSCR) {
1853 PyObject *sub = TOP();
1854 PyObject *container = SECOND();
1855 PyObject *v = THIRD();
1856 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 STACKADJ(-3);
1858 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 Py_DECREF(container);
1862 Py_DECREF(sub);
1863 if (err != 0)
1864 goto error;
1865 DISPATCH();
1866 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001867
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 TARGET(DELETE_SUBSCR) {
1869 PyObject *sub = TOP();
1870 PyObject *container = SECOND();
1871 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 STACKADJ(-2);
1873 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 err = PyObject_DelItem(container, sub);
1875 Py_DECREF(container);
1876 Py_DECREF(sub);
1877 if (err != 0)
1878 goto error;
1879 DISPATCH();
1880 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001881
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001883 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001885 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001886 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PyErr_SetString(PyExc_RuntimeError,
1889 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 Py_DECREF(value);
1891 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001893 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 Py_DECREF(value);
1895 if (res == NULL)
1896 goto error;
1897 Py_DECREF(res);
1898 DISPATCH();
1899 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001900
Thomas Wouters434d0822000-08-24 20:11:32 +00001901#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001903#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 TARGET(RAISE_VARARGS) {
1905 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 switch (oparg) {
1907 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 if (do_raise(exc, cause)) {
1913 why = WHY_EXCEPTION;
1914 goto fast_block_end;
1915 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 break;
1917 default:
1918 PyErr_SetString(PyExc_SystemError,
1919 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 break;
1921 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 goto error;
1923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 retval = POP();
1927 why = WHY_RETURN;
1928 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001930
Yury Selivanov75445082015-05-11 22:57:16 -04001931 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001932 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001933 PyObject *iter = NULL;
1934 PyObject *awaitable = NULL;
1935 PyObject *obj = TOP();
1936 PyTypeObject *type = Py_TYPE(obj);
1937
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001938 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001939 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001940 }
Yury Selivanov75445082015-05-11 22:57:16 -04001941
1942 if (getter != NULL) {
1943 iter = (*getter)(obj);
1944 Py_DECREF(obj);
1945 if (iter == NULL) {
1946 SET_TOP(NULL);
1947 goto error;
1948 }
1949 }
1950 else {
1951 SET_TOP(NULL);
1952 PyErr_Format(
1953 PyExc_TypeError,
1954 "'async for' requires an object with "
1955 "__aiter__ method, got %.100s",
1956 type->tp_name);
1957 Py_DECREF(obj);
1958 goto error;
1959 }
1960
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001961 if (Py_TYPE(iter)->tp_as_async != NULL &&
1962 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1963
1964 /* Starting with CPython 3.5.2 __aiter__ should return
1965 asynchronous iterators directly (not awaitables that
1966 resolve to asynchronous iterators.)
1967
1968 Therefore, we check if the object that was returned
1969 from __aiter__ has an __anext__ method. If it does,
1970 we wrap it in an awaitable that resolves to `iter`.
1971
1972 See http://bugs.python.org/issue27243 for more
1973 details.
1974 */
1975
1976 PyObject *wrapper = _PyAIterWrapper_New(iter);
1977 Py_DECREF(iter);
1978 SET_TOP(wrapper);
1979 DISPATCH();
1980 }
1981
Yury Selivanov5376ba92015-06-22 12:19:30 -04001982 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001983 if (awaitable == NULL) {
1984 SET_TOP(NULL);
1985 PyErr_Format(
1986 PyExc_TypeError,
1987 "'async for' received an invalid object "
1988 "from __aiter__: %.100s",
1989 Py_TYPE(iter)->tp_name);
1990
1991 Py_DECREF(iter);
1992 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001993 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001994 Py_DECREF(iter);
1995
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001996 if (PyErr_WarnFormat(
1997 PyExc_PendingDeprecationWarning, 1,
1998 "'%.100s' implements legacy __aiter__ protocol; "
1999 "__aiter__ should return an asynchronous "
2000 "iterator, not awaitable",
2001 type->tp_name))
2002 {
2003 /* Warning was converted to an error. */
2004 Py_DECREF(awaitable);
2005 SET_TOP(NULL);
2006 goto error;
2007 }
2008 }
2009
Yury Selivanov75445082015-05-11 22:57:16 -04002010 SET_TOP(awaitable);
2011 DISPATCH();
2012 }
2013
2014 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002015 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002016 PyObject *next_iter = NULL;
2017 PyObject *awaitable = NULL;
2018 PyObject *aiter = TOP();
2019 PyTypeObject *type = Py_TYPE(aiter);
2020
2021 if (type->tp_as_async != NULL)
2022 getter = type->tp_as_async->am_anext;
2023
2024 if (getter != NULL) {
2025 next_iter = (*getter)(aiter);
2026 if (next_iter == NULL) {
2027 goto error;
2028 }
2029 }
2030 else {
2031 PyErr_Format(
2032 PyExc_TypeError,
2033 "'async for' requires an iterator with "
2034 "__anext__ method, got %.100s",
2035 type->tp_name);
2036 goto error;
2037 }
2038
Yury Selivanov5376ba92015-06-22 12:19:30 -04002039 awaitable = _PyCoro_GetAwaitableIter(next_iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002040 if (awaitable == NULL) {
2041 PyErr_Format(
2042 PyExc_TypeError,
2043 "'async for' received an invalid object "
2044 "from __anext__: %.100s",
2045 Py_TYPE(next_iter)->tp_name);
2046
2047 Py_DECREF(next_iter);
2048 goto error;
2049 } else
2050 Py_DECREF(next_iter);
2051
2052 PUSH(awaitable);
2053 DISPATCH();
2054 }
2055
2056 TARGET(GET_AWAITABLE) {
2057 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002058 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002059
2060 Py_DECREF(iterable);
2061
Yury Selivanovc724bae2016-03-02 11:30:46 -05002062 if (iter != NULL && PyCoro_CheckExact(iter)) {
2063 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2064 if (yf != NULL) {
2065 /* `iter` is a coroutine object that is being
2066 awaited, `yf` is a pointer to the current awaitable
2067 being awaited on. */
2068 Py_DECREF(yf);
2069 Py_CLEAR(iter);
2070 PyErr_SetString(
2071 PyExc_RuntimeError,
2072 "coroutine is being awaited already");
2073 /* The code below jumps to `error` if `iter` is NULL. */
2074 }
2075 }
2076
Yury Selivanov75445082015-05-11 22:57:16 -04002077 SET_TOP(iter); /* Even if it's NULL */
2078
2079 if (iter == NULL) {
2080 goto error;
2081 }
2082
2083 DISPATCH();
2084 }
2085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 TARGET(YIELD_FROM) {
2087 PyObject *v = POP();
2088 PyObject *reciever = TOP();
2089 int err;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002090 if (PyGen_CheckExact(reciever) || PyCoro_CheckExact(reciever)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002092 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002093 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 if (v == Py_None)
2095 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002096 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04002097 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002098 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 Py_DECREF(v);
2100 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002101 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002102 if (tstate->c_tracefunc != NULL
2103 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002104 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002105 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 if (err < 0)
2107 goto error;
2108 Py_DECREF(reciever);
2109 SET_TOP(val);
2110 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002111 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002112 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002113 f->f_stacktop = stack_pointer;
2114 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002115 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002116 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002117 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002119
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 retval = POP();
2122 f->f_stacktop = stack_pointer;
2123 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002126
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 TARGET(POP_EXCEPT) {
2128 PyTryBlock *b = PyFrame_BlockPop(f);
2129 if (b->b_type != EXCEPT_HANDLER) {
2130 PyErr_SetString(PyExc_SystemError,
2131 "popped block is not an except handler");
2132 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002135 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002137
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002138 TARGET(POP_BLOCK) {
2139 PyTryBlock *b = PyFrame_BlockPop(f);
2140 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 TARGET(END_FINALLY) {
2146 PyObject *status = POP();
2147 if (PyLong_Check(status)) {
2148 why = (enum why_code) PyLong_AS_LONG(status);
2149 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 if (why == WHY_RETURN ||
2151 why == WHY_CONTINUE)
2152 retval = POP();
2153 if (why == WHY_SILENCED) {
2154 /* An exception was silenced by 'with', we must
2155 manually unwind the EXCEPT_HANDLER block which was
2156 created when the exception was caught, otherwise
2157 the stack will be in an inconsistent state. */
2158 PyTryBlock *b = PyFrame_BlockPop(f);
2159 assert(b->b_type == EXCEPT_HANDLER);
2160 UNWIND_EXCEPT_HANDLER(b);
2161 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 Py_DECREF(status);
2163 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 Py_DECREF(status);
2166 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 else if (PyExceptionClass_Check(status)) {
2169 PyObject *exc = POP();
2170 PyObject *tb = POP();
2171 PyErr_Restore(status, exc, tb);
2172 why = WHY_EXCEPTION;
2173 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 PyErr_SetString(PyExc_SystemError,
2177 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 Py_DECREF(status);
2179 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 Py_DECREF(status);
2182 DISPATCH();
2183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002184
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002186 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002187
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002189 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2191 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002192 PyErr_SetString(PyExc_NameError,
2193 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002197 }
2198 else {
2199 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2200 if (build_class_str == NULL)
2201 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2203 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002204 if (PyErr_ExceptionMatches(PyExc_KeyError))
2205 PyErr_SetString(PyExc_NameError,
2206 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002208 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002209 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002211 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002212 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 TARGET(STORE_NAME) {
2215 PyObject *name = GETITEM(names, oparg);
2216 PyObject *v = POP();
2217 PyObject *ns = f->f_locals;
2218 int err;
2219 if (ns == NULL) {
2220 PyErr_Format(PyExc_SystemError,
2221 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 if (PyDict_CheckExact(ns))
2226 err = PyDict_SetItem(ns, name, v);
2227 else
2228 err = PyObject_SetItem(ns, name, v);
2229 Py_DECREF(v);
2230 if (err != 0)
2231 goto error;
2232 DISPATCH();
2233 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002234
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 TARGET(DELETE_NAME) {
2236 PyObject *name = GETITEM(names, oparg);
2237 PyObject *ns = f->f_locals;
2238 int err;
2239 if (ns == NULL) {
2240 PyErr_Format(PyExc_SystemError,
2241 "no locals when deleting %R", name);
2242 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 err = PyObject_DelItem(ns, name);
2245 if (err != 0) {
2246 format_exc_check_arg(PyExc_NameError,
2247 NAME_ERROR_MSG,
2248 name);
2249 goto error;
2250 }
2251 DISPATCH();
2252 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002253
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002254 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 TARGET(UNPACK_SEQUENCE) {
2256 PyObject *seq = POP(), *item, **items;
2257 if (PyTuple_CheckExact(seq) &&
2258 PyTuple_GET_SIZE(seq) == oparg) {
2259 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 item = items[oparg];
2262 Py_INCREF(item);
2263 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002264 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 } else if (PyList_CheckExact(seq) &&
2266 PyList_GET_SIZE(seq) == oparg) {
2267 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 item = items[oparg];
2270 Py_INCREF(item);
2271 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 stack_pointer + oparg)) {
2275 STACKADJ(oparg);
2276 } else {
2277 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 Py_DECREF(seq);
2279 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002282 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 TARGET(UNPACK_EX) {
2286 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2287 PyObject *seq = POP();
2288
2289 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2290 stack_pointer + totalargs)) {
2291 stack_pointer += totalargs;
2292 } else {
2293 Py_DECREF(seq);
2294 goto error;
2295 }
2296 Py_DECREF(seq);
2297 DISPATCH();
2298 }
2299
2300 TARGET(STORE_ATTR) {
2301 PyObject *name = GETITEM(names, oparg);
2302 PyObject *owner = TOP();
2303 PyObject *v = SECOND();
2304 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002307 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 Py_DECREF(owner);
2309 if (err != 0)
2310 goto error;
2311 DISPATCH();
2312 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002313
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 TARGET(DELETE_ATTR) {
2315 PyObject *name = GETITEM(names, oparg);
2316 PyObject *owner = POP();
2317 int err;
2318 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2319 Py_DECREF(owner);
2320 if (err != 0)
2321 goto error;
2322 DISPATCH();
2323 }
2324
2325 TARGET(STORE_GLOBAL) {
2326 PyObject *name = GETITEM(names, oparg);
2327 PyObject *v = POP();
2328 int err;
2329 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 if (err != 0)
2332 goto error;
2333 DISPATCH();
2334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002335
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 TARGET(DELETE_GLOBAL) {
2337 PyObject *name = GETITEM(names, oparg);
2338 int err;
2339 err = PyDict_DelItem(f->f_globals, name);
2340 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002341 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002342 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002344 }
2345 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002347
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 TARGET(LOAD_NAME) {
2349 PyObject *name = GETITEM(names, oparg);
2350 PyObject *locals = f->f_locals;
2351 PyObject *v;
2352 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002353 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 "no locals when loading %R", name);
2355 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 if (PyDict_CheckExact(locals)) {
2358 v = PyDict_GetItem(locals, name);
2359 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 }
2361 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002363 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002364 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2365 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 PyErr_Clear();
2367 }
2368 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 if (v == NULL) {
2370 v = PyDict_GetItem(f->f_globals, name);
2371 Py_XINCREF(v);
2372 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002373 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 v = PyDict_GetItem(f->f_builtins, name);
2375 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002376 format_exc_check_arg(
2377 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 NAME_ERROR_MSG, name);
2379 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002380 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002382 }
2383 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 v = PyObject_GetItem(f->f_builtins, name);
2385 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002386 if (PyErr_ExceptionMatches(PyExc_KeyError))
2387 format_exc_check_arg(
2388 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 NAME_ERROR_MSG, name);
2390 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002391 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002392 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002393 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002396 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 TARGET(LOAD_GLOBAL) {
2400 PyObject *name = GETITEM(names, oparg);
2401 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002402 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002403 && PyDict_CheckExact(f->f_builtins))
2404 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002406 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002407 name);
2408 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002409 if (!_PyErr_OCCURRED()) {
2410 /* _PyDict_LoadGlobal() returns NULL without raising
2411 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002412 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002413 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002414 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002419 else {
2420 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002421
2422 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 v = PyObject_GetItem(f->f_globals, name);
2424 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002425 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2426 goto error;
2427 PyErr_Clear();
2428
Victor Stinnerb4efc962015-11-20 09:24:02 +01002429 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 v = PyObject_GetItem(f->f_builtins, name);
2431 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002432 if (PyErr_ExceptionMatches(PyExc_KeyError))
2433 format_exc_check_arg(
2434 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002435 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002437 }
2438 }
2439 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002440 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002444 TARGET(DELETE_FAST) {
2445 PyObject *v = GETLOCAL(oparg);
2446 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 SETLOCAL(oparg, NULL);
2448 DISPATCH();
2449 }
2450 format_exc_check_arg(
2451 PyExc_UnboundLocalError,
2452 UNBOUNDLOCAL_ERROR_MSG,
2453 PyTuple_GetItem(co->co_varnames, oparg)
2454 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002455 goto error;
2456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002457
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002458 TARGET(DELETE_DEREF) {
2459 PyObject *cell = freevars[oparg];
2460 if (PyCell_GET(cell) != NULL) {
2461 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002462 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002463 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002464 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002465 goto error;
2466 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002467
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002468 TARGET(LOAD_CLOSURE) {
2469 PyObject *cell = freevars[oparg];
2470 Py_INCREF(cell);
2471 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002473 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002474
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002475 TARGET(LOAD_CLASSDEREF) {
2476 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002477 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002478 assert(locals);
2479 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2480 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2481 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2482 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2483 if (PyDict_CheckExact(locals)) {
2484 value = PyDict_GetItem(locals, name);
2485 Py_XINCREF(value);
2486 }
2487 else {
2488 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002489 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002490 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2491 goto error;
2492 PyErr_Clear();
2493 }
2494 }
2495 if (!value) {
2496 PyObject *cell = freevars[oparg];
2497 value = PyCell_GET(cell);
2498 if (value == NULL) {
2499 format_exc_unbound(co, oparg);
2500 goto error;
2501 }
2502 Py_INCREF(value);
2503 }
2504 PUSH(value);
2505 DISPATCH();
2506 }
2507
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 TARGET(LOAD_DEREF) {
2509 PyObject *cell = freevars[oparg];
2510 PyObject *value = PyCell_GET(cell);
2511 if (value == NULL) {
2512 format_exc_unbound(co, oparg);
2513 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 Py_INCREF(value);
2516 PUSH(value);
2517 DISPATCH();
2518 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002519
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 TARGET(STORE_DEREF) {
2521 PyObject *v = POP();
2522 PyObject *cell = freevars[oparg];
2523 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 DISPATCH();
2526 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002527
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002528 TARGET(BUILD_TUPLE) {
2529 PyObject *tup = PyTuple_New(oparg);
2530 if (tup == NULL)
2531 goto error;
2532 while (--oparg >= 0) {
2533 PyObject *item = POP();
2534 PyTuple_SET_ITEM(tup, oparg, item);
2535 }
2536 PUSH(tup);
2537 DISPATCH();
2538 }
2539
2540 TARGET(BUILD_LIST) {
2541 PyObject *list = PyList_New(oparg);
2542 if (list == NULL)
2543 goto error;
2544 while (--oparg >= 0) {
2545 PyObject *item = POP();
2546 PyList_SET_ITEM(list, oparg, item);
2547 }
2548 PUSH(list);
2549 DISPATCH();
2550 }
2551
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002552 TARGET(BUILD_TUPLE_UNPACK)
2553 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002554 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
2555 int i;
2556 PyObject *sum = PyList_New(0);
2557 PyObject *return_value;
2558 if (sum == NULL)
2559 goto error;
2560
2561 for (i = oparg; i > 0; i--) {
2562 PyObject *none_val;
2563
2564 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2565 if (none_val == NULL) {
2566 Py_DECREF(sum);
2567 goto error;
2568 }
2569 Py_DECREF(none_val);
2570 }
2571
2572 if (convert_to_tuple) {
2573 return_value = PyList_AsTuple(sum);
2574 Py_DECREF(sum);
2575 if (return_value == NULL)
2576 goto error;
2577 }
2578 else {
2579 return_value = sum;
2580 }
2581
2582 while (oparg--)
2583 Py_DECREF(POP());
2584 PUSH(return_value);
2585 DISPATCH();
2586 }
2587
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 TARGET(BUILD_SET) {
2589 PyObject *set = PySet_New(NULL);
2590 int err = 0;
2591 if (set == NULL)
2592 goto error;
2593 while (--oparg >= 0) {
2594 PyObject *item = POP();
2595 if (err == 0)
2596 err = PySet_Add(set, item);
2597 Py_DECREF(item);
2598 }
2599 if (err != 0) {
2600 Py_DECREF(set);
2601 goto error;
2602 }
2603 PUSH(set);
2604 DISPATCH();
2605 }
2606
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002607 TARGET(BUILD_SET_UNPACK) {
2608 int i;
2609 PyObject *sum = PySet_New(NULL);
2610 if (sum == NULL)
2611 goto error;
2612
2613 for (i = oparg; i > 0; i--) {
2614 if (_PySet_Update(sum, PEEK(i)) < 0) {
2615 Py_DECREF(sum);
2616 goto error;
2617 }
2618 }
2619
2620 while (oparg--)
2621 Py_DECREF(POP());
2622 PUSH(sum);
2623 DISPATCH();
2624 }
2625
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 TARGET(BUILD_MAP) {
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002627 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2629 if (map == NULL)
2630 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002631 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002632 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002633 PyObject *key = PEEK(2*i);
2634 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002635 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002636 if (err != 0) {
2637 Py_DECREF(map);
2638 goto error;
2639 }
2640 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002641
2642 while (oparg--) {
2643 Py_DECREF(POP());
2644 Py_DECREF(POP());
2645 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 PUSH(map);
2647 DISPATCH();
2648 }
2649
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002650 TARGET(BUILD_CONST_KEY_MAP) {
2651 int i;
2652 PyObject *map;
2653 PyObject *keys = TOP();
2654 if (!PyTuple_CheckExact(keys) ||
2655 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2656 PyErr_SetString(PyExc_SystemError,
2657 "bad BUILD_CONST_KEY_MAP keys argument");
2658 goto error;
2659 }
2660 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2661 if (map == NULL) {
2662 goto error;
2663 }
2664 for (i = oparg; i > 0; i--) {
2665 int err;
2666 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2667 PyObject *value = PEEK(i + 1);
2668 err = PyDict_SetItem(map, key, value);
2669 if (err != 0) {
2670 Py_DECREF(map);
2671 goto error;
2672 }
2673 }
2674
2675 Py_DECREF(POP());
2676 while (oparg--) {
2677 Py_DECREF(POP());
2678 }
2679 PUSH(map);
2680 DISPATCH();
2681 }
2682
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002683 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2684 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002685 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
2686 int num_maps;
2687 int function_location;
2688 int i;
2689 PyObject *sum = PyDict_New();
2690 if (sum == NULL)
2691 goto error;
2692 if (with_call) {
2693 num_maps = oparg & 0xff;
2694 function_location = (oparg>>8) & 0xff;
2695 }
2696 else {
2697 num_maps = oparg;
2698 }
2699
2700 for (i = num_maps; i > 0; i--) {
2701 PyObject *arg = PEEK(i);
2702 if (with_call) {
2703 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2704
2705 if (intersection == NULL) {
2706 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2707 PyObject *func = (
2708 PEEK(function_location + num_maps));
2709 PyErr_Format(PyExc_TypeError,
2710 "%.200s%.200s argument after ** "
2711 "must be a mapping, not %.200s",
2712 PyEval_GetFuncName(func),
2713 PyEval_GetFuncDesc(func),
2714 arg->ob_type->tp_name);
2715 }
2716 Py_DECREF(sum);
2717 goto error;
2718 }
2719
2720 if (PySet_GET_SIZE(intersection)) {
2721 Py_ssize_t idx = 0;
2722 PyObject *key;
2723 PyObject *func = PEEK(function_location + num_maps);
2724 Py_hash_t hash;
2725 _PySet_NextEntry(intersection, &idx, &key, &hash);
2726 if (!PyUnicode_Check(key)) {
2727 PyErr_Format(PyExc_TypeError,
2728 "%.200s%.200s keywords must be strings",
2729 PyEval_GetFuncName(func),
2730 PyEval_GetFuncDesc(func));
2731 } else {
2732 PyErr_Format(PyExc_TypeError,
2733 "%.200s%.200s got multiple "
2734 "values for keyword argument '%U'",
2735 PyEval_GetFuncName(func),
2736 PyEval_GetFuncDesc(func),
2737 key);
2738 }
2739 Py_DECREF(intersection);
2740 Py_DECREF(sum);
2741 goto error;
2742 }
2743 Py_DECREF(intersection);
2744 }
2745
2746 if (PyDict_Update(sum, arg) < 0) {
2747 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2748 PyErr_Format(PyExc_TypeError,
2749 "'%.200s' object is not a mapping",
2750 arg->ob_type->tp_name);
2751 }
2752 Py_DECREF(sum);
2753 goto error;
2754 }
2755 }
2756
2757 while (num_maps--)
2758 Py_DECREF(POP());
2759 PUSH(sum);
2760 DISPATCH();
2761 }
2762
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 TARGET(MAP_ADD) {
2764 PyObject *key = TOP();
2765 PyObject *value = SECOND();
2766 PyObject *map;
2767 int err;
2768 STACKADJ(-2);
2769 map = stack_pointer[-oparg]; /* dict */
2770 assert(PyDict_CheckExact(map));
2771 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2772 Py_DECREF(value);
2773 Py_DECREF(key);
2774 if (err != 0)
2775 goto error;
2776 PREDICT(JUMP_ABSOLUTE);
2777 DISPATCH();
2778 }
2779
2780 TARGET(LOAD_ATTR) {
2781 PyObject *name = GETITEM(names, oparg);
2782 PyObject *owner = TOP();
2783 PyObject *res = PyObject_GetAttr(owner, name);
2784 Py_DECREF(owner);
2785 SET_TOP(res);
2786 if (res == NULL)
2787 goto error;
2788 DISPATCH();
2789 }
2790
2791 TARGET(COMPARE_OP) {
2792 PyObject *right = POP();
2793 PyObject *left = TOP();
2794 PyObject *res = cmp_outcome(oparg, left, right);
2795 Py_DECREF(left);
2796 Py_DECREF(right);
2797 SET_TOP(res);
2798 if (res == NULL)
2799 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 PREDICT(POP_JUMP_IF_FALSE);
2801 PREDICT(POP_JUMP_IF_TRUE);
2802 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002803 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002804
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 TARGET(IMPORT_NAME) {
2806 _Py_IDENTIFIER(__import__);
2807 PyObject *name = GETITEM(names, oparg);
2808 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002809 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 if (func == NULL) {
2811 PyErr_SetString(PyExc_ImportError,
2812 "__import__ not found");
2813 goto error;
2814 }
2815 Py_INCREF(func);
2816 from = POP();
2817 level = TOP();
2818 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2819 args = PyTuple_Pack(5,
2820 name,
2821 f->f_globals,
2822 f->f_locals == NULL ?
2823 Py_None : f->f_locals,
2824 from,
2825 level);
2826 else
2827 args = PyTuple_Pack(4,
2828 name,
2829 f->f_globals,
2830 f->f_locals == NULL ?
2831 Py_None : f->f_locals,
2832 from);
2833 Py_DECREF(level);
2834 Py_DECREF(from);
2835 if (args == NULL) {
2836 Py_DECREF(func);
2837 STACKADJ(-1);
2838 goto error;
2839 }
2840 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002841 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 READ_TIMESTAMP(intr1);
2843 Py_DECREF(args);
2844 Py_DECREF(func);
2845 SET_TOP(res);
2846 if (res == NULL)
2847 goto error;
2848 DISPATCH();
2849 }
2850
2851 TARGET(IMPORT_STAR) {
2852 PyObject *from = POP(), *locals;
2853 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002854 if (PyFrame_FastToLocalsWithError(f) < 0)
2855 goto error;
2856
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 locals = f->f_locals;
2858 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 PyErr_SetString(PyExc_SystemError,
2860 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 }
2863 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 READ_TIMESTAMP(intr1);
2866 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 Py_DECREF(from);
2868 if (err != 0)
2869 goto error;
2870 DISPATCH();
2871 }
Guido van Rossum25831651993-05-19 14:50:45 +00002872
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 TARGET(IMPORT_FROM) {
2874 PyObject *name = GETITEM(names, oparg);
2875 PyObject *from = TOP();
2876 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 PUSH(res);
2881 if (res == NULL)
2882 goto error;
2883 DISPATCH();
2884 }
Thomas Wouters52152252000-08-17 22:55:00 +00002885
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 JUMPBY(oparg);
2888 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002890
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002891 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 TARGET(POP_JUMP_IF_FALSE) {
2893 PyObject *cond = POP();
2894 int err;
2895 if (cond == Py_True) {
2896 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 FAST_DISPATCH();
2898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 if (cond == Py_False) {
2900 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 JUMPTO(oparg);
2902 FAST_DISPATCH();
2903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 err = PyObject_IsTrue(cond);
2905 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 if (err > 0)
2907 err = 0;
2908 else if (err == 0)
2909 JUMPTO(oparg);
2910 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002914
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002915 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 TARGET(POP_JUMP_IF_TRUE) {
2917 PyObject *cond = POP();
2918 int err;
2919 if (cond == Py_False) {
2920 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 FAST_DISPATCH();
2922 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 if (cond == Py_True) {
2924 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
2926 FAST_DISPATCH();
2927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 err = PyObject_IsTrue(cond);
2929 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 if (err > 0) {
2931 err = 0;
2932 JUMPTO(oparg);
2933 }
2934 else if (err == 0)
2935 ;
2936 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002937 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002940
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 TARGET(JUMP_IF_FALSE_OR_POP) {
2942 PyObject *cond = TOP();
2943 int err;
2944 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 FAST_DISPATCH();
2948 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 JUMPTO(oparg);
2951 FAST_DISPATCH();
2952 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 if (err > 0) {
2955 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 err = 0;
2958 }
2959 else if (err == 0)
2960 JUMPTO(oparg);
2961 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002962 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002963 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002965
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 TARGET(JUMP_IF_TRUE_OR_POP) {
2967 PyObject *cond = TOP();
2968 int err;
2969 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 FAST_DISPATCH();
2973 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002975 JUMPTO(oparg);
2976 FAST_DISPATCH();
2977 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 if (err > 0) {
2980 err = 0;
2981 JUMPTO(oparg);
2982 }
2983 else if (err == 0) {
2984 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 }
2987 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002991
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002992 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002995#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 /* Enabling this path speeds-up all while and for-loops by bypassing
2997 the per-loop checks for signals. By default, this should be turned-off
2998 because it prevents detection of a control-break in tight loops like
2999 "while 1: pass". Compile with this option turned-on when you need
3000 the speed-up and do not need break checking inside tight loops (ones
3001 that contain only instructions ending with FAST_DISPATCH).
3002 */
3003 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003004#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003006#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003011 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003012 PyObject *iter = PyObject_GetIter(iterable);
3013 Py_DECREF(iterable);
3014 SET_TOP(iter);
3015 if (iter == NULL)
3016 goto error;
3017 PREDICT(FOR_ITER);
3018 DISPATCH();
3019 }
3020
3021 TARGET(GET_YIELD_FROM_ITER) {
3022 /* before: [obj]; after [getiter(obj)] */
3023 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003024 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003025 if (PyCoro_CheckExact(iterable)) {
3026 /* `iterable` is a coroutine */
3027 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3028 /* and it is used in a 'yield from' expression of a
3029 regular generator. */
3030 Py_DECREF(iterable);
3031 SET_TOP(NULL);
3032 PyErr_SetString(PyExc_TypeError,
3033 "cannot 'yield from' a coroutine object "
3034 "in a non-coroutine generator");
3035 goto error;
3036 }
3037 }
3038 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003039 /* `iterable` is not a generator. */
3040 iter = PyObject_GetIter(iterable);
3041 Py_DECREF(iterable);
3042 SET_TOP(iter);
3043 if (iter == NULL)
3044 goto error;
3045 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 DISPATCH();
3047 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003048
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003049 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 PyObject *iter = TOP();
3053 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3054 if (next != NULL) {
3055 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 PREDICT(STORE_FAST);
3057 PREDICT(UNPACK_SEQUENCE);
3058 DISPATCH();
3059 }
3060 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3062 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003063 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003064 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 PyErr_Clear();
3066 }
3067 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 STACKADJ(-1);
3069 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 JUMPBY(oparg);
3071 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003073
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003074 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 why = WHY_BREAK;
3076 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003078
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003079 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 if (retval == NULL)
3082 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 why = WHY_CONTINUE;
3084 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003085 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003086
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003087 TARGET(SETUP_LOOP)
3088 TARGET(SETUP_EXCEPT)
3089 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 /* NOTE: If you add any new block-setup opcodes that
3091 are not try/except/finally handlers, you may need
3092 to update the PyGen_NeedsFinalizing() function.
3093 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3096 STACK_LEVEL());
3097 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003098 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003099
Yury Selivanov75445082015-05-11 22:57:16 -04003100 TARGET(BEFORE_ASYNC_WITH) {
3101 _Py_IDENTIFIER(__aexit__);
3102 _Py_IDENTIFIER(__aenter__);
3103
3104 PyObject *mgr = TOP();
3105 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3106 *enter;
3107 PyObject *res;
3108 if (exit == NULL)
3109 goto error;
3110 SET_TOP(exit);
3111 enter = special_lookup(mgr, &PyId___aenter__);
3112 Py_DECREF(mgr);
3113 if (enter == NULL)
3114 goto error;
3115 res = PyObject_CallFunctionObjArgs(enter, NULL);
3116 Py_DECREF(enter);
3117 if (res == NULL)
3118 goto error;
3119 PUSH(res);
3120 DISPATCH();
3121 }
3122
3123 TARGET(SETUP_ASYNC_WITH) {
3124 PyObject *res = POP();
3125 /* Setup the finally block before pushing the result
3126 of __aenter__ on the stack. */
3127 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3128 STACK_LEVEL());
3129 PUSH(res);
3130 DISPATCH();
3131 }
3132
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003133 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003134 _Py_IDENTIFIER(__exit__);
3135 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 PyObject *mgr = TOP();
3137 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3138 PyObject *res;
3139 if (exit == NULL)
3140 goto error;
3141 SET_TOP(exit);
3142 enter = special_lookup(mgr, &PyId___enter__);
3143 Py_DECREF(mgr);
3144 if (enter == NULL)
3145 goto error;
3146 res = PyObject_CallFunctionObjArgs(enter, NULL);
3147 Py_DECREF(enter);
3148 if (res == NULL)
3149 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 /* Setup the finally block before pushing the result
3151 of __enter__ on the stack. */
3152 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3153 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003154
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 DISPATCH();
3157 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003158
Yury Selivanov75445082015-05-11 22:57:16 -04003159 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003160 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 how/why we entered the finally clause:
3162 - TOP = None
3163 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3164 - TOP = WHY_*; no retval below it
3165 - (TOP, SECOND, THIRD) = exc_info()
3166 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3167 Below them is EXIT, the context.__exit__ bound method.
3168 In the last case, we must call
3169 EXIT(TOP, SECOND, THIRD)
3170 otherwise we must call
3171 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003172
Benjamin Peterson8f169482013-10-29 22:25:06 -04003173 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003175 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003178 In addition, if the stack represents an exception,
3179 *and* the function call returns a 'true' value, we
3180 push WHY_SILENCED onto the stack. END_FINALLY will
3181 then not re-raise the exception. (But non-local
3182 gotos should still be resumed.)
3183 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 (void)POP();
3189 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 else if (PyLong_Check(exc)) {
3193 STACKADJ(-1);
3194 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 case WHY_RETURN:
3196 case WHY_CONTINUE:
3197 /* Retval in TOP. */
3198 exit_func = SECOND();
3199 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 break;
3202 default:
3203 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003205 break;
3206 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 }
3209 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 val = SECOND();
3213 tb = THIRD();
3214 tp2 = FOURTH();
3215 exc2 = PEEK(5);
3216 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 SET_VALUE(7, tb2);
3219 SET_VALUE(6, exc2);
3220 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3222 SET_FOURTH(NULL);
3223 /* We just shifted the stack down, so we have
3224 to tell the except handler block that the
3225 values are lower than it expects. */
3226 block = &f->f_blockstack[f->f_iblock - 1];
3227 assert(block->b_type == EXCEPT_HANDLER);
3228 block->b_level--;
3229 }
3230 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003233 if (res == NULL)
3234 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003235
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003236 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003237 PUSH(exc);
3238 PUSH(res);
3239 PREDICT(WITH_CLEANUP_FINISH);
3240 DISPATCH();
3241 }
3242
3243 PREDICTED(WITH_CLEANUP_FINISH);
3244 TARGET(WITH_CLEANUP_FINISH) {
3245 PyObject *res = POP();
3246 PyObject *exc = POP();
3247 int err;
3248
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003249 if (exc != Py_None)
3250 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 else
3252 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003253
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003255 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003258 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 else if (err > 0) {
3260 err = 0;
3261 /* There was an exception and a True return */
3262 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3263 }
3264 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003267
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 TARGET(CALL_FUNCTION) {
3269 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 PCALL(PCALL_ALL);
3271 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003272#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003274#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003276#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 PUSH(res);
3279 if (res == NULL)
3280 goto error;
3281 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003283
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003284 TARGET(CALL_FUNCTION_VAR)
3285 TARGET(CALL_FUNCTION_KW)
3286 TARGET(CALL_FUNCTION_VAR_KW) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 int na = oparg & 0xff;
3288 int nk = (oparg>>8) & 0xff;
3289 int flags = (opcode - CALL_FUNCTION) & 3;
3290 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 PCALL(PCALL_ALL);
3293 if (flags & CALL_FLAG_VAR)
3294 n++;
3295 if (flags & CALL_FLAG_KW)
3296 n++;
3297 pfunc = stack_pointer - n - 1;
3298 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003300 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00003301 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 PyObject *self = PyMethod_GET_SELF(func);
3303 Py_INCREF(self);
3304 func = PyMethod_GET_FUNCTION(func);
3305 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03003306 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00003308 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 } else
3310 Py_INCREF(func);
3311 sp = stack_pointer;
3312 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003313 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 READ_TIMESTAMP(intr1);
3315 stack_pointer = sp;
3316 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003319 PyObject *o = POP();
3320 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003322 PUSH(res);
3323 if (res == NULL)
3324 goto error;
3325 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003327
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003328 TARGET(MAKE_CLOSURE)
3329 TARGET(MAKE_FUNCTION) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 int posdefaults = oparg & 0xff;
3331 int kwdefaults = (oparg>>8) & 0xff;
3332 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003333
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 PyObject *qualname = POP(); /* qualname */
3335 PyObject *code = POP(); /* code object */
3336 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
3337 Py_DECREF(code);
3338 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003339
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003340 if (func == NULL)
3341 goto error;
3342
3343 if (opcode == MAKE_CLOSURE) {
3344 PyObject *closure = POP();
3345 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 Py_DECREF(func);
3348 Py_DECREF(closure);
3349 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003351 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003353
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003356 PyObject *names = POP(); /* names of args with annotations */
3357 PyObject *anns = PyDict_New();
3358 if (anns == NULL) {
3359 Py_DECREF(func);
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003360 Py_DECREF(names);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003361 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003363 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003364 assert(num_annotations == name_ix+1);
3365 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003366 PyObject *name, *value;
3367 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003369 name = PyTuple_GET_ITEM(names, name_ix);
3370 value = POP();
3371 err = PyDict_SetItem(anns, name, value);
3372 Py_DECREF(value);
3373 if (err != 0) {
3374 Py_DECREF(anns);
3375 Py_DECREF(func);
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003376 Py_DECREF(names);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 goto error;
3378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 }
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003380 Py_DECREF(names);
Neal Norwitzc1505362006-12-28 06:47:50 +00003381
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003382 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 /* Can't happen unless
3384 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003385 Py_DECREF(anns);
3386 Py_DECREF(func);
3387 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003389 Py_DECREF(anns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 if (kwdefaults > 0) {
3394 PyObject *defs = PyDict_New();
3395 if (defs == NULL) {
3396 Py_DECREF(func);
3397 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 }
3399 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 PyObject *v = POP(); /* default value */
3401 PyObject *key = POP(); /* kw only arg name */
3402 int err = PyDict_SetItem(defs, key, v);
3403 Py_DECREF(v);
3404 Py_DECREF(key);
3405 if (err != 0) {
3406 Py_DECREF(defs);
3407 Py_DECREF(func);
3408 goto error;
3409 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 /* Can't happen unless
3413 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003414 Py_DECREF(func);
3415 Py_DECREF(defs);
3416 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05003420 if (posdefaults > 0) {
3421 PyObject *defs = PyTuple_New(posdefaults);
3422 if (defs == NULL) {
3423 Py_DECREF(func);
3424 goto error;
3425 }
3426 while (--posdefaults >= 0)
3427 PyTuple_SET_ITEM(defs, posdefaults, POP());
3428 if (PyFunction_SetDefaults(func, defs) != 0) {
3429 /* Can't happen unless
3430 PyFunction_SetDefaults changes. */
3431 Py_DECREF(defs);
3432 Py_DECREF(func);
3433 goto error;
3434 }
3435 Py_DECREF(defs);
3436 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003437 PUSH(func);
3438 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003440
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003441 TARGET(BUILD_SLICE) {
3442 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003444 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 step = NULL;
3447 stop = POP();
3448 start = TOP();
3449 slice = PySlice_New(start, stop, step);
3450 Py_DECREF(start);
3451 Py_DECREF(stop);
3452 Py_XDECREF(step);
3453 SET_TOP(slice);
3454 if (slice == NULL)
3455 goto error;
3456 DISPATCH();
3457 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003458
Eric V. Smitha78c7952015-11-03 12:45:05 -05003459 TARGET(FORMAT_VALUE) {
3460 /* Handles f-string value formatting. */
3461 PyObject *result;
3462 PyObject *fmt_spec;
3463 PyObject *value;
3464 PyObject *(*conv_fn)(PyObject *);
3465 int which_conversion = oparg & FVC_MASK;
3466 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3467
3468 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003469 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003470
3471 /* See if any conversion is specified. */
3472 switch (which_conversion) {
3473 case FVC_STR: conv_fn = PyObject_Str; break;
3474 case FVC_REPR: conv_fn = PyObject_Repr; break;
3475 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3476
3477 /* Must be 0 (meaning no conversion), since only four
3478 values are allowed by (oparg & FVC_MASK). */
3479 default: conv_fn = NULL; break;
3480 }
3481
3482 /* If there's a conversion function, call it and replace
3483 value with that result. Otherwise, just use value,
3484 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003485 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003486 result = conv_fn(value);
3487 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003488 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003489 Py_XDECREF(fmt_spec);
3490 goto error;
3491 }
3492 value = result;
3493 }
3494
3495 /* If value is a unicode object, and there's no fmt_spec,
3496 then we know the result of format(value) is value
3497 itself. In that case, skip calling format(). I plan to
3498 move this optimization in to PyObject_Format()
3499 itself. */
3500 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3501 /* Do nothing, just transfer ownership to result. */
3502 result = value;
3503 } else {
3504 /* Actually call format(). */
3505 result = PyObject_Format(value, fmt_spec);
3506 Py_DECREF(value);
3507 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003508 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003509 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003510 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003511 }
3512
Eric V. Smith135d5f42016-02-05 18:23:08 -05003513 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003514 DISPATCH();
3515 }
3516
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003517 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003518 int oldoparg = oparg;
3519 NEXTOPARG();
3520 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003522 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003523
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003524
Antoine Pitrou042b1282010-08-13 21:15:58 +00003525#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003527#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 default:
3529 fprintf(stderr,
3530 "XXX lineno: %d, opcode: %d\n",
3531 PyFrame_GetLineNumber(f),
3532 opcode);
3533 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003534 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003535
3536#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003538#endif
3539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003541
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003542 /* This should never be reached. Every opcode should end with DISPATCH()
3543 or goto error. */
3544 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003545
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003546error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003548
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003549 assert(why == WHY_NOT);
3550 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003551
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003552 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003553#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003554 if (!PyErr_Occurred())
3555 PyErr_SetString(PyExc_SystemError,
3556 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003557#else
3558 assert(PyErr_Occurred());
3559#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 /* Log traceback info. */
3562 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003563
Benjamin Peterson51f46162013-01-23 08:38:47 -05003564 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003565 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3566 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003567
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003568fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003569 assert(why != WHY_NOT);
3570
3571 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 while (why != WHY_NOT && f->f_iblock > 0) {
3573 /* Peek at the current block. */
3574 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 assert(why != WHY_YIELD);
3577 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3578 why = WHY_NOT;
3579 JUMPTO(PyLong_AS_LONG(retval));
3580 Py_DECREF(retval);
3581 break;
3582 }
3583 /* Now we have to pop the block. */
3584 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 if (b->b_type == EXCEPT_HANDLER) {
3587 UNWIND_EXCEPT_HANDLER(b);
3588 continue;
3589 }
3590 UNWIND_BLOCK(b);
3591 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3592 why = WHY_NOT;
3593 JUMPTO(b->b_handler);
3594 break;
3595 }
3596 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3597 || b->b_type == SETUP_FINALLY)) {
3598 PyObject *exc, *val, *tb;
3599 int handler = b->b_handler;
3600 /* Beware, this invalidates all b->b_* fields */
3601 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3602 PUSH(tstate->exc_traceback);
3603 PUSH(tstate->exc_value);
3604 if (tstate->exc_type != NULL) {
3605 PUSH(tstate->exc_type);
3606 }
3607 else {
3608 Py_INCREF(Py_None);
3609 PUSH(Py_None);
3610 }
3611 PyErr_Fetch(&exc, &val, &tb);
3612 /* Make the raw exception data
3613 available to the handler,
3614 so a program can emulate the
3615 Python main loop. */
3616 PyErr_NormalizeException(
3617 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003618 if (tb != NULL)
3619 PyException_SetTraceback(val, tb);
3620 else
3621 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 Py_INCREF(exc);
3623 tstate->exc_type = exc;
3624 Py_INCREF(val);
3625 tstate->exc_value = val;
3626 tstate->exc_traceback = tb;
3627 if (tb == NULL)
3628 tb = Py_None;
3629 Py_INCREF(tb);
3630 PUSH(tb);
3631 PUSH(val);
3632 PUSH(exc);
3633 why = WHY_NOT;
3634 JUMPTO(handler);
3635 break;
3636 }
3637 if (b->b_type == SETUP_FINALLY) {
3638 if (why & (WHY_RETURN | WHY_CONTINUE))
3639 PUSH(retval);
3640 PUSH(PyLong_FromLong((long)why));
3641 why = WHY_NOT;
3642 JUMPTO(b->b_handler);
3643 break;
3644 }
3645 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 if (why != WHY_NOT)
3650 break;
3651 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003652
Victor Stinnerace47d72013-07-18 01:41:08 +02003653 assert(!PyErr_Occurred());
3654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 assert(why != WHY_YIELD);
3658 /* Pop remaining stack entries. */
3659 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003660 PyObject *o = POP();
3661 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 if (why != WHY_RETURN)
3665 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003666
Victor Stinner4a7cc882015-03-06 23:35:27 +01003667 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003668
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003669fast_yield:
Yury Selivanov5376ba92015-06-22 12:19:30 -04003670 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003671
Benjamin Petersonac913412011-07-03 16:25:11 -05003672 /* The purpose of this block is to put aside the generator's exception
3673 state and restore that of the calling frame. If the current
3674 exception state is from the caller, we clear the exception values
3675 on the generator frame, so they are not swapped back in latter. The
3676 origin of the current exception state is determined by checking for
3677 except handler blocks, which we must be in iff a new exception
3678 state came into existence in this frame. (An uncaught exception
3679 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3680 int i;
3681 for (i = 0; i < f->f_iblock; i++)
3682 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3683 break;
3684 if (i == f->f_iblock)
3685 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003686 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003687 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003688 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003689 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003692 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003693 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003694 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3695 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003697 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 why = WHY_EXCEPTION;
3699 }
3700 }
3701 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003702 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3703 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 PyTrace_RETURN, NULL);
3705 }
3706 }
3707 if (tstate->c_profilefunc) {
3708 if (why == WHY_EXCEPTION)
3709 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003710 tstate->c_profileobj,
3711 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003713 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3714 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003716 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003717 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 }
3719 }
3720 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003722 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003723exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003725 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003726 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003727
Victor Stinnerefde1462015-03-21 15:04:43 +01003728 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003729}
3730
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003732format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3733{
3734 int err;
3735 Py_ssize_t len = PyList_GET_SIZE(names);
3736 PyObject *name_str, *comma, *tail, *tmp;
3737
3738 assert(PyList_CheckExact(names));
3739 assert(len >= 1);
3740 /* Deal with the joys of natural language. */
3741 switch (len) {
3742 case 1:
3743 name_str = PyList_GET_ITEM(names, 0);
3744 Py_INCREF(name_str);
3745 break;
3746 case 2:
3747 name_str = PyUnicode_FromFormat("%U and %U",
3748 PyList_GET_ITEM(names, len - 2),
3749 PyList_GET_ITEM(names, len - 1));
3750 break;
3751 default:
3752 tail = PyUnicode_FromFormat(", %U, and %U",
3753 PyList_GET_ITEM(names, len - 2),
3754 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003755 if (tail == NULL)
3756 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003757 /* Chop off the last two objects in the list. This shouldn't actually
3758 fail, but we can't be too careful. */
3759 err = PyList_SetSlice(names, len - 2, len, NULL);
3760 if (err == -1) {
3761 Py_DECREF(tail);
3762 return;
3763 }
3764 /* Stitch everything up into a nice comma-separated list. */
3765 comma = PyUnicode_FromString(", ");
3766 if (comma == NULL) {
3767 Py_DECREF(tail);
3768 return;
3769 }
3770 tmp = PyUnicode_Join(comma, names);
3771 Py_DECREF(comma);
3772 if (tmp == NULL) {
3773 Py_DECREF(tail);
3774 return;
3775 }
3776 name_str = PyUnicode_Concat(tmp, tail);
3777 Py_DECREF(tmp);
3778 Py_DECREF(tail);
3779 break;
3780 }
3781 if (name_str == NULL)
3782 return;
3783 PyErr_Format(PyExc_TypeError,
3784 "%U() missing %i required %s argument%s: %U",
3785 co->co_name,
3786 len,
3787 kind,
3788 len == 1 ? "" : "s",
3789 name_str);
3790 Py_DECREF(name_str);
3791}
3792
3793static void
3794missing_arguments(PyCodeObject *co, int missing, int defcount,
3795 PyObject **fastlocals)
3796{
3797 int i, j = 0;
3798 int start, end;
3799 int positional = defcount != -1;
3800 const char *kind = positional ? "positional" : "keyword-only";
3801 PyObject *missing_names;
3802
3803 /* Compute the names of the arguments that are missing. */
3804 missing_names = PyList_New(missing);
3805 if (missing_names == NULL)
3806 return;
3807 if (positional) {
3808 start = 0;
3809 end = co->co_argcount - defcount;
3810 }
3811 else {
3812 start = co->co_argcount;
3813 end = start + co->co_kwonlyargcount;
3814 }
3815 for (i = start; i < end; i++) {
3816 if (GETLOCAL(i) == NULL) {
3817 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3818 PyObject *name = PyObject_Repr(raw);
3819 if (name == NULL) {
3820 Py_DECREF(missing_names);
3821 return;
3822 }
3823 PyList_SET_ITEM(missing_names, j++, name);
3824 }
3825 }
3826 assert(j == missing);
3827 format_missing(kind, co, missing_names);
3828 Py_DECREF(missing_names);
3829}
3830
3831static void
3832too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003833{
3834 int plural;
3835 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 int i;
3837 PyObject *sig, *kwonly_sig;
3838
Benjamin Petersone109c702011-06-24 09:37:26 -05003839 assert((co->co_flags & CO_VARARGS) == 0);
3840 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003841 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003842 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003844 if (defcount) {
3845 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 plural = 1;
3847 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3848 }
3849 else {
3850 plural = co->co_argcount != 1;
3851 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3852 }
3853 if (sig == NULL)
3854 return;
3855 if (kwonly_given) {
3856 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3857 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3858 kwonly_given != 1 ? "s" : "");
3859 if (kwonly_sig == NULL) {
3860 Py_DECREF(sig);
3861 return;
3862 }
3863 }
3864 else {
3865 /* This will not fail. */
3866 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003867 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003868 }
3869 PyErr_Format(PyExc_TypeError,
3870 "%U() takes %U positional argument%s but %d%U %s given",
3871 co->co_name,
3872 sig,
3873 plural ? "s" : "",
3874 given,
3875 kwonly_sig,
3876 given == 1 && !kwonly_given ? "was" : "were");
3877 Py_DECREF(sig);
3878 Py_DECREF(kwonly_sig);
3879}
3880
Guido van Rossumc2e20742006-02-27 22:32:47 +00003881/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003882 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003883 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003884
Victor Stinner40ee3012014-06-16 15:59:28 +02003885static PyObject *
3886_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003888 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3889 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003890{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003891 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003892 PyFrameObject *f;
3893 PyObject *retval = NULL;
3894 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 PyThreadState *tstate = PyThreadState_GET();
3896 PyObject *x, *u;
3897 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003898 int i;
3899 int n = argcount;
3900 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 if (globals == NULL) {
3903 PyErr_SetString(PyExc_SystemError,
3904 "PyEval_EvalCodeEx: NULL globals");
3905 return NULL;
3906 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 assert(tstate != NULL);
3909 assert(globals != NULL);
3910 f = PyFrame_New(tstate, co, globals, locals);
3911 if (f == NULL)
3912 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 fastlocals = f->f_localsplus;
3915 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003916
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917 /* Parse arguments. */
3918 if (co->co_flags & CO_VARKEYWORDS) {
3919 kwdict = PyDict_New();
3920 if (kwdict == NULL)
3921 goto fail;
3922 i = total_args;
3923 if (co->co_flags & CO_VARARGS)
3924 i++;
3925 SETLOCAL(i, kwdict);
3926 }
3927 if (argcount > co->co_argcount)
3928 n = co->co_argcount;
3929 for (i = 0; i < n; i++) {
3930 x = args[i];
3931 Py_INCREF(x);
3932 SETLOCAL(i, x);
3933 }
3934 if (co->co_flags & CO_VARARGS) {
3935 u = PyTuple_New(argcount - n);
3936 if (u == NULL)
3937 goto fail;
3938 SETLOCAL(total_args, u);
3939 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 x = args[i];
3941 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003944 }
3945 for (i = 0; i < kwcount; i++) {
3946 PyObject **co_varnames;
3947 PyObject *keyword = kws[2*i];
3948 PyObject *value = kws[2*i + 1];
3949 int j;
3950 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3951 PyErr_Format(PyExc_TypeError,
3952 "%U() keywords must be strings",
3953 co->co_name);
3954 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003956 /* Speed hack: do raw pointer compares. As names are
3957 normally interned this should almost always hit. */
3958 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3959 for (j = 0; j < total_args; j++) {
3960 PyObject *nm = co_varnames[j];
3961 if (nm == keyword)
3962 goto kw_found;
3963 }
3964 /* Slow fallback, just in case */
3965 for (j = 0; j < total_args; j++) {
3966 PyObject *nm = co_varnames[j];
3967 int cmp = PyObject_RichCompareBool(
3968 keyword, nm, Py_EQ);
3969 if (cmp > 0)
3970 goto kw_found;
3971 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003973 }
3974 if (j >= total_args && kwdict == NULL) {
3975 PyErr_Format(PyExc_TypeError,
3976 "%U() got an unexpected "
3977 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 co->co_name,
3979 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003980 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003982 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3983 goto fail;
3984 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003985 continue;
3986 kw_found:
3987 if (GETLOCAL(j) != NULL) {
3988 PyErr_Format(PyExc_TypeError,
3989 "%U() got multiple "
3990 "values for argument '%S'",
3991 co->co_name,
3992 keyword);
3993 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003995 Py_INCREF(value);
3996 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003998 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003999 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004000 goto fail;
4001 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004002 if (argcount < co->co_argcount) {
4003 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004004 int missing = 0;
4005 for (i = argcount; i < m; i++)
4006 if (GETLOCAL(i) == NULL)
4007 missing++;
4008 if (missing) {
4009 missing_arguments(co, missing, defcount, fastlocals);
4010 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004011 }
4012 if (n > m)
4013 i = n - m;
4014 else
4015 i = 0;
4016 for (; i < defcount; i++) {
4017 if (GETLOCAL(m+i) == NULL) {
4018 PyObject *def = defs[i];
4019 Py_INCREF(def);
4020 SETLOCAL(m+i, def);
4021 }
4022 }
4023 }
4024 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004025 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004026 for (i = co->co_argcount; i < total_args; i++) {
4027 PyObject *name;
4028 if (GETLOCAL(i) != NULL)
4029 continue;
4030 name = PyTuple_GET_ITEM(co->co_varnames, i);
4031 if (kwdefs != NULL) {
4032 PyObject *def = PyDict_GetItem(kwdefs, name);
4033 if (def) {
4034 Py_INCREF(def);
4035 SETLOCAL(i, def);
4036 continue;
4037 }
4038 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004039 missing++;
4040 }
4041 if (missing) {
4042 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004043 goto fail;
4044 }
4045 }
4046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004048 vars into frame. */
4049 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05004051 int arg;
4052 /* Possibly account for the cell variable being an argument. */
4053 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004054 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004055 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004056 /* Clear the local copy. */
4057 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004058 }
4059 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004060 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004061 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004062 if (c == NULL)
4063 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004064 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 }
Benjamin Peterson90037602011-06-25 22:54:45 -05004066 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4067 PyObject *o = PyTuple_GET_ITEM(closure, i);
4068 Py_INCREF(o);
4069 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004071
Yury Selivanov5376ba92015-06-22 12:19:30 -04004072 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004073 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004074 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004075 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004076
4077 if (is_coro && tstate->in_coroutine_wrapper) {
4078 assert(coro_wrapper != NULL);
4079 PyErr_Format(PyExc_RuntimeError,
4080 "coroutine wrapper %.200R attempted "
4081 "to recursively wrap %.200R",
4082 coro_wrapper,
4083 co);
4084 goto fail;
4085 }
Yury Selivanov75445082015-05-11 22:57:16 -04004086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 /* Don't need to keep the reference to f_back, it will be set
4088 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004089 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004091 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 /* Create a new generator that owns the ready to run frame
4094 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004095 if (is_coro) {
4096 gen = PyCoro_New(f, name, qualname);
4097 } else {
4098 gen = PyGen_NewWithQualName(f, name, qualname);
4099 }
Yury Selivanov75445082015-05-11 22:57:16 -04004100 if (gen == NULL)
4101 return NULL;
4102
Yury Selivanov94c22632015-06-04 10:16:51 -04004103 if (is_coro && coro_wrapper != NULL) {
4104 PyObject *wrapped;
4105 tstate->in_coroutine_wrapper = 1;
4106 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4107 tstate->in_coroutine_wrapper = 0;
4108 return wrapped;
4109 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004110
Yury Selivanov75445082015-05-11 22:57:16 -04004111 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004115
Thomas Woutersce272b62007-09-19 21:19:28 +00004116fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 /* decref'ing the frame can cause __del__ methods to get invoked,
4119 which can call back into Python. While we're done with the
4120 current Python frame (f), the associated C stack is still in use,
4121 so recursion_depth must be boosted for the duration.
4122 */
4123 assert(tstate != NULL);
4124 ++tstate->recursion_depth;
4125 Py_DECREF(f);
4126 --tstate->recursion_depth;
4127 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004128}
4129
Victor Stinner40ee3012014-06-16 15:59:28 +02004130PyObject *
4131PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4132 PyObject **args, int argcount, PyObject **kws, int kwcount,
4133 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4134{
4135 return _PyEval_EvalCodeWithName(_co, globals, locals,
4136 args, argcount, kws, kwcount,
4137 defs, defcount, kwdefs, closure,
4138 NULL, NULL);
4139}
Tim Peters5ca576e2001-06-18 22:08:13 +00004140
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004141static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004142special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004145 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004147 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 return NULL;
4149 }
4150 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004151}
4152
4153
Benjamin Peterson87880242011-07-03 16:48:31 -05004154/* These 3 functions deal with the exception state of generators. */
4155
4156static void
4157save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4158{
4159 PyObject *type, *value, *traceback;
4160 Py_XINCREF(tstate->exc_type);
4161 Py_XINCREF(tstate->exc_value);
4162 Py_XINCREF(tstate->exc_traceback);
4163 type = f->f_exc_type;
4164 value = f->f_exc_value;
4165 traceback = f->f_exc_traceback;
4166 f->f_exc_type = tstate->exc_type;
4167 f->f_exc_value = tstate->exc_value;
4168 f->f_exc_traceback = tstate->exc_traceback;
4169 Py_XDECREF(type);
4170 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004171 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004172}
4173
4174static void
4175swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4176{
4177 PyObject *tmp;
4178 tmp = tstate->exc_type;
4179 tstate->exc_type = f->f_exc_type;
4180 f->f_exc_type = tmp;
4181 tmp = tstate->exc_value;
4182 tstate->exc_value = f->f_exc_value;
4183 f->f_exc_value = tmp;
4184 tmp = tstate->exc_traceback;
4185 tstate->exc_traceback = f->f_exc_traceback;
4186 f->f_exc_traceback = tmp;
4187}
4188
4189static void
4190restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4191{
4192 PyObject *type, *value, *tb;
4193 type = tstate->exc_type;
4194 value = tstate->exc_value;
4195 tb = tstate->exc_traceback;
4196 tstate->exc_type = f->f_exc_type;
4197 tstate->exc_value = f->f_exc_value;
4198 tstate->exc_traceback = f->f_exc_traceback;
4199 f->f_exc_type = NULL;
4200 f->f_exc_value = NULL;
4201 f->f_exc_traceback = NULL;
4202 Py_XDECREF(type);
4203 Py_XDECREF(value);
4204 Py_XDECREF(tb);
4205}
4206
4207
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004208/* Logic for the raise statement (too complicated for inlining).
4209 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004210static int
Collin Winter828f04a2007-08-31 00:04:24 +00004211do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 if (exc == NULL) {
4216 /* Reraise */
4217 PyThreadState *tstate = PyThreadState_GET();
4218 PyObject *tb;
4219 type = tstate->exc_type;
4220 value = tstate->exc_value;
4221 tb = tstate->exc_traceback;
4222 if (type == Py_None) {
4223 PyErr_SetString(PyExc_RuntimeError,
4224 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004225 return 0;
4226 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 Py_XINCREF(type);
4228 Py_XINCREF(value);
4229 Py_XINCREF(tb);
4230 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004231 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 /* We support the following forms of raise:
4235 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004236 raise <instance>
4237 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 if (PyExceptionClass_Check(exc)) {
4240 type = exc;
4241 value = PyObject_CallObject(exc, NULL);
4242 if (value == NULL)
4243 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004244 if (!PyExceptionInstance_Check(value)) {
4245 PyErr_Format(PyExc_TypeError,
4246 "calling %R should have returned an instance of "
4247 "BaseException, not %R",
4248 type, Py_TYPE(value));
4249 goto raise_error;
4250 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004251 }
4252 else if (PyExceptionInstance_Check(exc)) {
4253 value = exc;
4254 type = PyExceptionInstance_Class(exc);
4255 Py_INCREF(type);
4256 }
4257 else {
4258 /* Not something you can raise. You get an exception
4259 anyway, just not what you specified :-) */
4260 Py_DECREF(exc);
4261 PyErr_SetString(PyExc_TypeError,
4262 "exceptions must derive from BaseException");
4263 goto raise_error;
4264 }
Collin Winter828f04a2007-08-31 00:04:24 +00004265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 if (cause) {
4267 PyObject *fixed_cause;
4268 if (PyExceptionClass_Check(cause)) {
4269 fixed_cause = PyObject_CallObject(cause, NULL);
4270 if (fixed_cause == NULL)
4271 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004272 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004273 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004274 else if (PyExceptionInstance_Check(cause)) {
4275 fixed_cause = cause;
4276 }
4277 else if (cause == Py_None) {
4278 Py_DECREF(cause);
4279 fixed_cause = NULL;
4280 }
4281 else {
4282 PyErr_SetString(PyExc_TypeError,
4283 "exception causes must derive from "
4284 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 goto raise_error;
4286 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004287 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 }
Collin Winter828f04a2007-08-31 00:04:24 +00004289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004290 PyErr_SetObject(type, value);
4291 /* PyErr_SetObject incref's its arguments */
4292 Py_XDECREF(value);
4293 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004294 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004295
4296raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 Py_XDECREF(value);
4298 Py_XDECREF(type);
4299 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004300 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004301}
4302
Tim Petersd6d010b2001-06-21 02:49:55 +00004303/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004304 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004305
Guido van Rossum0368b722007-05-11 16:50:42 +00004306 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4307 with a variable target.
4308*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004309
Barry Warsawe42b18f1997-08-25 22:13:04 +00004310static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004311unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 int i = 0, j = 0;
4314 Py_ssize_t ll = 0;
4315 PyObject *it; /* iter(v) */
4316 PyObject *w;
4317 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 it = PyObject_GetIter(v);
4322 if (it == NULL)
4323 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 for (; i < argcnt; i++) {
4326 w = PyIter_Next(it);
4327 if (w == NULL) {
4328 /* Iterator done, via error or exhaustion. */
4329 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004330 if (argcntafter == -1) {
4331 PyErr_Format(PyExc_ValueError,
4332 "not enough values to unpack (expected %d, got %d)",
4333 argcnt, i);
4334 }
4335 else {
4336 PyErr_Format(PyExc_ValueError,
4337 "not enough values to unpack "
4338 "(expected at least %d, got %d)",
4339 argcnt + argcntafter, i);
4340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 }
4342 goto Error;
4343 }
4344 *--sp = w;
4345 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 if (argcntafter == -1) {
4348 /* We better have exhausted the iterator now. */
4349 w = PyIter_Next(it);
4350 if (w == NULL) {
4351 if (PyErr_Occurred())
4352 goto Error;
4353 Py_DECREF(it);
4354 return 1;
4355 }
4356 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004357 PyErr_Format(PyExc_ValueError,
4358 "too many values to unpack (expected %d)",
4359 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 goto Error;
4361 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004362
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 l = PySequence_List(it);
4364 if (l == NULL)
4365 goto Error;
4366 *--sp = l;
4367 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 ll = PyList_GET_SIZE(l);
4370 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004371 PyErr_Format(PyExc_ValueError,
4372 "not enough values to unpack (expected at least %d, got %zd)",
4373 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 goto Error;
4375 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 /* Pop the "after-variable" args off the list. */
4378 for (j = argcntafter; j > 0; j--, i++) {
4379 *--sp = PyList_GET_ITEM(l, ll - j);
4380 }
4381 /* Resize the list. */
4382 Py_SIZE(l) = ll - argcntafter;
4383 Py_DECREF(it);
4384 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004385
Tim Petersd6d010b2001-06-21 02:49:55 +00004386Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 for (; i > 0; i--, sp++)
4388 Py_DECREF(*sp);
4389 Py_XDECREF(it);
4390 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004391}
4392
4393
Guido van Rossum96a42c81992-01-12 02:29:51 +00004394#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004395static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004396prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 printf("%s ", str);
4399 if (PyObject_Print(v, stdout, 0) != 0)
4400 PyErr_Clear(); /* Don't know what else to do */
4401 printf("\n");
4402 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004403}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004404#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004405
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004406static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004407call_exc_trace(Py_tracefunc func, PyObject *self,
4408 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004409{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004410 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004412 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 if (value == NULL) {
4414 value = Py_None;
4415 Py_INCREF(value);
4416 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004417 PyErr_NormalizeException(&type, &value, &orig_traceback);
4418 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 arg = PyTuple_Pack(3, type, value, traceback);
4420 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004421 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 return;
4423 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004424 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 Py_DECREF(arg);
4426 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004427 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 else {
4429 Py_XDECREF(type);
4430 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004431 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004433}
4434
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004435static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004436call_trace_protected(Py_tracefunc func, PyObject *obj,
4437 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 PyObject *type, *value, *traceback;
4441 int err;
4442 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004443 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 if (err == 0)
4445 {
4446 PyErr_Restore(type, value, traceback);
4447 return 0;
4448 }
4449 else {
4450 Py_XDECREF(type);
4451 Py_XDECREF(value);
4452 Py_XDECREF(traceback);
4453 return -1;
4454 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004455}
4456
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004457static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004458call_trace(Py_tracefunc func, PyObject *obj,
4459 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 int result;
4463 if (tstate->tracing)
4464 return 0;
4465 tstate->tracing++;
4466 tstate->use_tracing = 0;
4467 result = func(obj, frame, what, arg);
4468 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4469 || (tstate->c_profilefunc != NULL));
4470 tstate->tracing--;
4471 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004472}
4473
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004474PyObject *
4475_PyEval_CallTracing(PyObject *func, PyObject *args)
4476{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004477 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 int save_tracing = tstate->tracing;
4479 int save_use_tracing = tstate->use_tracing;
4480 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 tstate->tracing = 0;
4483 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4484 || (tstate->c_profilefunc != NULL));
4485 result = PyObject_Call(func, args, NULL);
4486 tstate->tracing = save_tracing;
4487 tstate->use_tracing = save_use_tracing;
4488 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004489}
4490
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004491/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004492static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004493maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004494 PyThreadState *tstate, PyFrameObject *frame,
4495 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004496{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 int result = 0;
4498 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 /* If the last instruction executed isn't in the current
4501 instruction window, reset the window.
4502 */
4503 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4504 PyAddrPair bounds;
4505 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4506 &bounds);
4507 *instr_lb = bounds.ap_lower;
4508 *instr_ub = bounds.ap_upper;
4509 }
4510 /* If the last instruction falls at the start of a line or if
4511 it represents a jump backwards, update the frame's line
4512 number and call the trace function. */
4513 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4514 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004515 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004516 }
4517 *instr_prev = frame->f_lasti;
4518 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004519}
4520
Fred Drake5755ce62001-06-27 19:19:46 +00004521void
4522PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 PyThreadState *tstate = PyThreadState_GET();
4525 PyObject *temp = tstate->c_profileobj;
4526 Py_XINCREF(arg);
4527 tstate->c_profilefunc = NULL;
4528 tstate->c_profileobj = NULL;
4529 /* Must make sure that tracing is not ignored if 'temp' is freed */
4530 tstate->use_tracing = tstate->c_tracefunc != NULL;
4531 Py_XDECREF(temp);
4532 tstate->c_profilefunc = func;
4533 tstate->c_profileobj = arg;
4534 /* Flag that tracing or profiling is turned on */
4535 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004536}
4537
4538void
4539PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 PyThreadState *tstate = PyThreadState_GET();
4542 PyObject *temp = tstate->c_traceobj;
4543 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4544 Py_XINCREF(arg);
4545 tstate->c_tracefunc = NULL;
4546 tstate->c_traceobj = NULL;
4547 /* Must make sure that profiling is not ignored if 'temp' is freed */
4548 tstate->use_tracing = tstate->c_profilefunc != NULL;
4549 Py_XDECREF(temp);
4550 tstate->c_tracefunc = func;
4551 tstate->c_traceobj = arg;
4552 /* Flag that tracing or profiling is turned on */
4553 tstate->use_tracing = ((func != NULL)
4554 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004555}
4556
Yury Selivanov75445082015-05-11 22:57:16 -04004557void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004558_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004559{
4560 PyThreadState *tstate = PyThreadState_GET();
4561
Yury Selivanov75445082015-05-11 22:57:16 -04004562 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004563 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004564}
4565
4566PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004567_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004568{
4569 PyThreadState *tstate = PyThreadState_GET();
4570 return tstate->coroutine_wrapper;
4571}
4572
Guido van Rossumb209a111997-04-29 18:18:01 +00004573PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004574PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 PyFrameObject *current_frame = PyEval_GetFrame();
4577 if (current_frame == NULL)
4578 return PyThreadState_GET()->interp->builtins;
4579 else
4580 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004581}
4582
Guido van Rossumb209a111997-04-29 18:18:01 +00004583PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004584PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004585{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004586 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004587 if (current_frame == NULL) {
4588 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004589 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004590 }
4591
4592 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4593 return NULL;
4594
4595 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004597}
4598
Guido van Rossumb209a111997-04-29 18:18:01 +00004599PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004600PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 PyFrameObject *current_frame = PyEval_GetFrame();
4603 if (current_frame == NULL)
4604 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004605
4606 assert(current_frame->f_globals != NULL);
4607 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004608}
4609
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004610PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004611PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 PyThreadState *tstate = PyThreadState_GET();
4614 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004615}
4616
Guido van Rossum6135a871995-01-09 17:53:26 +00004617int
Tim Peters5ba58662001-07-16 02:29:45 +00004618PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 PyFrameObject *current_frame = PyEval_GetFrame();
4621 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 if (current_frame != NULL) {
4624 const int codeflags = current_frame->f_code->co_flags;
4625 const int compilerflags = codeflags & PyCF_MASK;
4626 if (compilerflags) {
4627 result = 1;
4628 cf->cf_flags |= compilerflags;
4629 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004630#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 if (codeflags & CO_GENERATOR_ALLOWED) {
4632 result = 1;
4633 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4634 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004635#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 }
4637 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004638}
4639
Guido van Rossum3f5da241990-12-20 15:06:42 +00004640
Guido van Rossum681d79a1995-07-18 14:51:37 +00004641/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004642 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004643
Guido van Rossumb209a111997-04-29 18:18:01 +00004644PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004645PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004646{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004648
Victor Stinner59b356d2015-03-16 11:52:32 +01004649#ifdef Py_DEBUG
4650 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4651 set. It raises a new exception if parameters are invalid or if
4652 PyTuple_New() fails, and so the original exception is lost. */
4653 assert(!PyErr_Occurred());
4654#endif
4655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 if (arg == NULL) {
4657 arg = PyTuple_New(0);
4658 if (arg == NULL)
4659 return NULL;
4660 }
4661 else if (!PyTuple_Check(arg)) {
4662 PyErr_SetString(PyExc_TypeError,
4663 "argument list must be a tuple");
4664 return NULL;
4665 }
4666 else
4667 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 if (kw != NULL && !PyDict_Check(kw)) {
4670 PyErr_SetString(PyExc_TypeError,
4671 "keyword list must be a dictionary");
4672 Py_DECREF(arg);
4673 return NULL;
4674 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004676 result = PyObject_Call(func, arg, kw);
4677 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004680}
4681
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004682const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004683PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004685 if (PyMethod_Check(func))
4686 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4687 else if (PyFunction_Check(func))
4688 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4689 else if (PyCFunction_Check(func))
4690 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4691 else
4692 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004693}
4694
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004695const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004696PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 if (PyMethod_Check(func))
4699 return "()";
4700 else if (PyFunction_Check(func))
4701 return "()";
4702 else if (PyCFunction_Check(func))
4703 return "()";
4704 else
4705 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004706}
4707
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004708static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004709err_args(PyObject *func, int flags, int nargs)
4710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 if (flags & METH_NOARGS)
4712 PyErr_Format(PyExc_TypeError,
4713 "%.200s() takes no arguments (%d given)",
4714 ((PyCFunctionObject *)func)->m_ml->ml_name,
4715 nargs);
4716 else
4717 PyErr_Format(PyExc_TypeError,
4718 "%.200s() takes exactly one argument (%d given)",
4719 ((PyCFunctionObject *)func)->m_ml->ml_name,
4720 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004721}
4722
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004723#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004724if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004725 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4726 tstate, tstate->frame, \
4727 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 x = NULL; \
4729 } \
4730 else { \
4731 x = call; \
4732 if (tstate->c_profilefunc != NULL) { \
4733 if (x == NULL) { \
4734 call_trace_protected(tstate->c_profilefunc, \
4735 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004736 tstate, tstate->frame, \
4737 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 /* XXX should pass (type, value, tb) */ \
4739 } else { \
4740 if (call_trace(tstate->c_profilefunc, \
4741 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004742 tstate, tstate->frame, \
4743 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 Py_DECREF(x); \
4745 x = NULL; \
4746 } \
4747 } \
4748 } \
4749 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004750} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 x = call; \
4752 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004753
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004754static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004755call_function(PyObject ***pp_stack, int oparg
4756#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004757 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004758#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004759 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004761 int na = oparg & 0xff;
4762 int nk = (oparg>>8) & 0xff;
4763 int n = na + 2 * nk;
4764 PyObject **pfunc = (*pp_stack) - n - 1;
4765 PyObject *func = *pfunc;
4766 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 /* Always dispatch PyCFunction first, because these are
4769 presumed to be the most frequent callable object.
4770 */
4771 if (PyCFunction_Check(func) && nk == 0) {
4772 int flags = PyCFunction_GET_FLAGS(func);
4773 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004775 PCALL(PCALL_CFUNCTION);
4776 if (flags & (METH_NOARGS | METH_O)) {
4777 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4778 PyObject *self = PyCFunction_GET_SELF(func);
4779 if (flags & METH_NOARGS && na == 0) {
4780 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004781
Victor Stinnerefde1462015-03-21 15:04:43 +01004782 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 }
4784 else if (flags & METH_O && na == 1) {
4785 PyObject *arg = EXT_POP(*pp_stack);
4786 C_TRACE(x, (*meth)(self,arg));
4787 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004788
Victor Stinnerefde1462015-03-21 15:04:43 +01004789 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004790 }
4791 else {
4792 err_args(func, flags, na);
4793 x = NULL;
4794 }
4795 }
4796 else {
4797 PyObject *callargs;
4798 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004799 if (callargs != NULL) {
4800 READ_TIMESTAMP(*pintr0);
4801 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4802 READ_TIMESTAMP(*pintr1);
4803 Py_XDECREF(callargs);
4804 }
4805 else {
4806 x = NULL;
4807 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004808 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004809 }
4810 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004811 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4812 /* optimize access to bound methods */
4813 PyObject *self = PyMethod_GET_SELF(func);
4814 PCALL(PCALL_METHOD);
4815 PCALL(PCALL_BOUND_METHOD);
4816 Py_INCREF(self);
4817 func = PyMethod_GET_FUNCTION(func);
4818 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03004819 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004820 na++;
4821 n++;
4822 } else
4823 Py_INCREF(func);
4824 READ_TIMESTAMP(*pintr0);
4825 if (PyFunction_Check(func))
4826 x = fast_function(func, pp_stack, n, na, nk);
4827 else
4828 x = do_call(func, pp_stack, na, nk);
4829 READ_TIMESTAMP(*pintr1);
4830 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004831
4832 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 /* Clear the stack of the function object. Also removes
4836 the arguments in case they weren't consumed already
4837 (fast_function() and err_args() leave them on the stack).
4838 */
4839 while ((*pp_stack) > pfunc) {
4840 w = EXT_POP(*pp_stack);
4841 Py_DECREF(w);
4842 PCALL(PCALL_POP);
4843 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004844
Victor Stinner4a7cc882015-03-06 23:35:27 +01004845 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004846 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004847}
4848
Jeremy Hylton192690e2002-08-16 18:36:11 +00004849/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004850 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004851 For the simplest case -- a function that takes only positional
4852 arguments and is called with only positional arguments -- it
4853 inlines the most primitive frame setup code from
4854 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4855 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004856*/
4857
4858static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004859fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004860{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4862 PyObject *globals = PyFunction_GET_GLOBALS(func);
4863 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4864 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Victor Stinner40ee3012014-06-16 15:59:28 +02004865 PyObject *name = ((PyFunctionObject *)func) -> func_name;
4866 PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004867 PyObject **d = NULL;
4868 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004870 PCALL(PCALL_FUNCTION);
4871 PCALL(PCALL_FAST_FUNCTION);
4872 if (argdefs == NULL && co->co_argcount == n &&
4873 co->co_kwonlyargcount == 0 && nk==0 &&
4874 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4875 PyFrameObject *f;
4876 PyObject *retval = NULL;
4877 PyThreadState *tstate = PyThreadState_GET();
4878 PyObject **fastlocals, **stack;
4879 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004881 PCALL(PCALL_FASTER_FUNCTION);
4882 assert(globals != NULL);
4883 /* XXX Perhaps we should create a specialized
4884 PyFrame_New() that doesn't take locals, but does
4885 take builtins without sanity checking them.
4886 */
4887 assert(tstate != NULL);
4888 f = PyFrame_New(tstate, co, globals, NULL);
4889 if (f == NULL)
4890 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004892 fastlocals = f->f_localsplus;
4893 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004895 for (i = 0; i < n; i++) {
4896 Py_INCREF(*stack);
4897 fastlocals[i] = *stack++;
4898 }
4899 retval = PyEval_EvalFrameEx(f,0);
4900 ++tstate->recursion_depth;
4901 Py_DECREF(f);
4902 --tstate->recursion_depth;
4903 return retval;
4904 }
4905 if (argdefs != NULL) {
4906 d = &PyTuple_GET_ITEM(argdefs, 0);
4907 nd = Py_SIZE(argdefs);
4908 }
Victor Stinner40ee3012014-06-16 15:59:28 +02004909 return _PyEval_EvalCodeWithName((PyObject*)co, globals,
4910 (PyObject *)NULL, (*pp_stack)-n, na,
4911 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4912 PyFunction_GET_CLOSURE(func),
4913 name, qualname);
Jeremy Hylton52820442001-01-03 23:52:36 +00004914}
4915
4916static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004917update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4918 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 PyObject *kwdict = NULL;
4921 if (orig_kwdict == NULL)
4922 kwdict = PyDict_New();
4923 else {
4924 kwdict = PyDict_Copy(orig_kwdict);
4925 Py_DECREF(orig_kwdict);
4926 }
4927 if (kwdict == NULL)
4928 return NULL;
4929 while (--nk >= 0) {
4930 int err;
4931 PyObject *value = EXT_POP(*pp_stack);
4932 PyObject *key = EXT_POP(*pp_stack);
4933 if (PyDict_GetItem(kwdict, key) != NULL) {
4934 PyErr_Format(PyExc_TypeError,
4935 "%.200s%s got multiple values "
4936 "for keyword argument '%U'",
4937 PyEval_GetFuncName(func),
4938 PyEval_GetFuncDesc(func),
4939 key);
4940 Py_DECREF(key);
4941 Py_DECREF(value);
4942 Py_DECREF(kwdict);
4943 return NULL;
4944 }
4945 err = PyDict_SetItem(kwdict, key, value);
4946 Py_DECREF(key);
4947 Py_DECREF(value);
4948 if (err) {
4949 Py_DECREF(kwdict);
4950 return NULL;
4951 }
4952 }
4953 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004954}
4955
4956static PyObject *
4957update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004961
Serhiy Storchaka79d6e8d2016-04-19 23:37:17 +03004962 if (!nstack) {
4963 if (!stararg) {
4964 /* There are no positional arguments on the stack and there is no
4965 sequence to be unpacked. */
4966 return PyTuple_New(0);
4967 }
4968 if (PyTuple_CheckExact(stararg)) {
4969 /* No arguments are passed on the stack and the sequence is not a
4970 tuple subclass so we can just pass the stararg tuple directly
4971 to the function. */
4972 Py_INCREF(stararg);
4973 return stararg;
4974 }
4975 }
4976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004977 callargs = PyTuple_New(nstack + nstar);
4978 if (callargs == NULL) {
4979 return NULL;
4980 }
4981 if (nstar) {
4982 int i;
4983 for (i = 0; i < nstar; i++) {
4984 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4985 Py_INCREF(a);
4986 PyTuple_SET_ITEM(callargs, nstack + i, a);
4987 }
4988 }
4989 while (--nstack >= 0) {
4990 w = EXT_POP(*pp_stack);
4991 PyTuple_SET_ITEM(callargs, nstack, w);
4992 }
4993 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004994}
4995
4996static PyObject *
4997load_args(PyObject ***pp_stack, int na)
4998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 PyObject *args = PyTuple_New(na);
5000 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00005001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 if (args == NULL)
5003 return NULL;
5004 while (--na >= 0) {
5005 w = EXT_POP(*pp_stack);
5006 PyTuple_SET_ITEM(args, na, w);
5007 }
5008 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00005009}
5010
5011static PyObject *
5012do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
5013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 PyObject *callargs = NULL;
5015 PyObject *kwdict = NULL;
5016 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00005017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 if (nk > 0) {
5019 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
5020 if (kwdict == NULL)
5021 goto call_fail;
5022 }
5023 callargs = load_args(pp_stack, na);
5024 if (callargs == NULL)
5025 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005026#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 /* At this point, we have to look at the type of func to
5028 update the call stats properly. Do it here so as to avoid
5029 exposing the call stats machinery outside ceval.c
5030 */
5031 if (PyFunction_Check(func))
5032 PCALL(PCALL_FUNCTION);
5033 else if (PyMethod_Check(func))
5034 PCALL(PCALL_METHOD);
5035 else if (PyType_Check(func))
5036 PCALL(PCALL_TYPE);
5037 else if (PyCFunction_Check(func))
5038 PCALL(PCALL_CFUNCTION);
5039 else
5040 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005041#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005042 if (PyCFunction_Check(func)) {
5043 PyThreadState *tstate = PyThreadState_GET();
5044 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5045 }
5046 else
5047 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00005048call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005049 Py_XDECREF(callargs);
5050 Py_XDECREF(kwdict);
5051 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005052}
5053
5054static PyObject *
5055ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
5056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 int nstar = 0;
5058 PyObject *callargs = NULL;
5059 PyObject *stararg = NULL;
5060 PyObject *kwdict = NULL;
5061 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00005062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 if (flags & CALL_FLAG_KW) {
5064 kwdict = EXT_POP(*pp_stack);
Serhiy Storchakace412872016-05-08 23:36:44 +03005065 if (!PyDict_CheckExact(kwdict)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005066 PyObject *d;
5067 d = PyDict_New();
5068 if (d == NULL)
5069 goto ext_call_fail;
5070 if (PyDict_Update(d, kwdict) != 0) {
5071 Py_DECREF(d);
5072 /* PyDict_Update raises attribute
5073 * error (percolated from an attempt
5074 * to get 'keys' attribute) instead of
5075 * a type error if its second argument
5076 * is not a mapping.
5077 */
5078 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5079 PyErr_Format(PyExc_TypeError,
5080 "%.200s%.200s argument after ** "
5081 "must be a mapping, not %.200s",
5082 PyEval_GetFuncName(func),
5083 PyEval_GetFuncDesc(func),
5084 kwdict->ob_type->tp_name);
5085 }
5086 goto ext_call_fail;
5087 }
5088 Py_DECREF(kwdict);
5089 kwdict = d;
5090 }
5091 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005092 if (nk > 0) {
5093 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
5094 if (kwdict == NULL)
5095 goto ext_call_fail;
5096 }
5097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 if (flags & CALL_FLAG_VAR) {
5099 stararg = EXT_POP(*pp_stack);
5100 if (!PyTuple_Check(stararg)) {
5101 PyObject *t = NULL;
Martin Panterb5944222016-01-31 06:30:56 +00005102 if (Py_TYPE(stararg)->tp_iter == NULL &&
5103 !PySequence_Check(stararg)) {
5104 PyErr_Format(PyExc_TypeError,
5105 "%.200s%.200s argument after * "
5106 "must be an iterable, not %.200s",
5107 PyEval_GetFuncName(func),
5108 PyEval_GetFuncDesc(func),
5109 stararg->ob_type->tp_name);
5110 goto ext_call_fail;
5111 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005112 t = PySequence_Tuple(stararg);
5113 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005114 goto ext_call_fail;
5115 }
5116 Py_DECREF(stararg);
5117 stararg = t;
5118 }
5119 nstar = PyTuple_GET_SIZE(stararg);
5120 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 callargs = update_star_args(na, nstar, stararg, pp_stack);
5122 if (callargs == NULL)
5123 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005124#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005125 /* At this point, we have to look at the type of func to
5126 update the call stats properly. Do it here so as to avoid
5127 exposing the call stats machinery outside ceval.c
5128 */
5129 if (PyFunction_Check(func))
5130 PCALL(PCALL_FUNCTION);
5131 else if (PyMethod_Check(func))
5132 PCALL(PCALL_METHOD);
5133 else if (PyType_Check(func))
5134 PCALL(PCALL_TYPE);
5135 else if (PyCFunction_Check(func))
5136 PCALL(PCALL_CFUNCTION);
5137 else
5138 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005139#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005140 if (PyCFunction_Check(func)) {
5141 PyThreadState *tstate = PyThreadState_GET();
5142 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5143 }
5144 else
5145 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00005146ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005147 Py_XDECREF(callargs);
5148 Py_XDECREF(kwdict);
5149 Py_XDECREF(stararg);
5150 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005151}
5152
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005153/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005154 nb_index slot defined, and store in *pi.
5155 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5156 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 +00005157 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005158*/
Tim Petersb5196382001-12-16 19:44:20 +00005159/* Note: If v is NULL, return success without storing into *pi. This
5160 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5161 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005162*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005163int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005164_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005165{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005166 if (v != NULL) {
5167 Py_ssize_t x;
5168 if (PyIndex_Check(v)) {
5169 x = PyNumber_AsSsize_t(v, NULL);
5170 if (x == -1 && PyErr_Occurred())
5171 return 0;
5172 }
5173 else {
5174 PyErr_SetString(PyExc_TypeError,
5175 "slice indices must be integers or "
5176 "None or have an __index__ method");
5177 return 0;
5178 }
5179 *pi = x;
5180 }
5181 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005182}
5183
Guido van Rossum486364b2007-06-30 05:01:58 +00005184#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005185 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005186
Guido van Rossumb209a111997-04-29 18:18:01 +00005187static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005188cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005189{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005190 int res = 0;
5191 switch (op) {
5192 case PyCmp_IS:
5193 res = (v == w);
5194 break;
5195 case PyCmp_IS_NOT:
5196 res = (v != w);
5197 break;
5198 case PyCmp_IN:
5199 res = PySequence_Contains(w, v);
5200 if (res < 0)
5201 return NULL;
5202 break;
5203 case PyCmp_NOT_IN:
5204 res = PySequence_Contains(w, v);
5205 if (res < 0)
5206 return NULL;
5207 res = !res;
5208 break;
5209 case PyCmp_EXC_MATCH:
5210 if (PyTuple_Check(w)) {
5211 Py_ssize_t i, length;
5212 length = PyTuple_Size(w);
5213 for (i = 0; i < length; i += 1) {
5214 PyObject *exc = PyTuple_GET_ITEM(w, i);
5215 if (!PyExceptionClass_Check(exc)) {
5216 PyErr_SetString(PyExc_TypeError,
5217 CANNOT_CATCH_MSG);
5218 return NULL;
5219 }
5220 }
5221 }
5222 else {
5223 if (!PyExceptionClass_Check(w)) {
5224 PyErr_SetString(PyExc_TypeError,
5225 CANNOT_CATCH_MSG);
5226 return NULL;
5227 }
5228 }
5229 res = PyErr_GivenExceptionMatches(v, w);
5230 break;
5231 default:
5232 return PyObject_RichCompare(v, w, op);
5233 }
5234 v = res ? Py_True : Py_False;
5235 Py_INCREF(v);
5236 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005237}
5238
Thomas Wouters52152252000-08-17 22:55:00 +00005239static PyObject *
5240import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005243 _Py_IDENTIFIER(__name__);
5244 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005246 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005247 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5248 return x;
5249 /* Issue #17636: in case this failed because of a circular relative
5250 import, try to fallback on reading the module directly from
5251 sys.modules. */
5252 PyErr_Clear();
5253 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005254 if (pkgname == NULL) {
5255 goto error;
5256 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005257 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5258 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005259 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005260 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005261 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005262 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005263 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005264 if (x == NULL) {
5265 goto error;
5266 }
5267 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005268 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005269 error:
5270 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5271 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005272}
Guido van Rossumac7be682001-01-17 15:42:30 +00005273
Thomas Wouters52152252000-08-17 22:55:00 +00005274static int
5275import_all_from(PyObject *locals, PyObject *v)
5276{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005277 _Py_IDENTIFIER(__all__);
5278 _Py_IDENTIFIER(__dict__);
5279 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005280 PyObject *dict, *name, *value;
5281 int skip_leading_underscores = 0;
5282 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005284 if (all == NULL) {
5285 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5286 return -1; /* Unexpected error */
5287 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005288 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 if (dict == NULL) {
5290 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5291 return -1;
5292 PyErr_SetString(PyExc_ImportError,
5293 "from-import-* object has no __dict__ and no __all__");
5294 return -1;
5295 }
5296 all = PyMapping_Keys(dict);
5297 Py_DECREF(dict);
5298 if (all == NULL)
5299 return -1;
5300 skip_leading_underscores = 1;
5301 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005303 for (pos = 0, err = 0; ; pos++) {
5304 name = PySequence_GetItem(all, pos);
5305 if (name == NULL) {
5306 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5307 err = -1;
5308 else
5309 PyErr_Clear();
5310 break;
5311 }
5312 if (skip_leading_underscores &&
5313 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005314 PyUnicode_READY(name) != -1 &&
5315 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005316 {
5317 Py_DECREF(name);
5318 continue;
5319 }
5320 value = PyObject_GetAttr(v, name);
5321 if (value == NULL)
5322 err = -1;
5323 else if (PyDict_CheckExact(locals))
5324 err = PyDict_SetItem(locals, name, value);
5325 else
5326 err = PyObject_SetItem(locals, name, value);
5327 Py_DECREF(name);
5328 Py_XDECREF(value);
5329 if (err != 0)
5330 break;
5331 }
5332 Py_DECREF(all);
5333 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005334}
5335
Guido van Rossumac7be682001-01-17 15:42:30 +00005336static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005337format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005339 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005341 if (!obj)
5342 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 obj_str = _PyUnicode_AsString(obj);
5345 if (!obj_str)
5346 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005348 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005349}
Guido van Rossum950361c1997-01-24 13:49:28 +00005350
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005351static void
5352format_exc_unbound(PyCodeObject *co, int oparg)
5353{
5354 PyObject *name;
5355 /* Don't stomp existing exception */
5356 if (PyErr_Occurred())
5357 return;
5358 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5359 name = PyTuple_GET_ITEM(co->co_cellvars,
5360 oparg);
5361 format_exc_check_arg(
5362 PyExc_UnboundLocalError,
5363 UNBOUNDLOCAL_ERROR_MSG,
5364 name);
5365 } else {
5366 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5367 PyTuple_GET_SIZE(co->co_cellvars));
5368 format_exc_check_arg(PyExc_NameError,
5369 UNBOUNDFREE_ERROR_MSG, name);
5370 }
5371}
5372
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005373static PyObject *
5374unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005375 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005376{
5377 PyObject *res;
5378 if (Py_REFCNT(v) == 2) {
5379 /* In the common case, there are 2 references to the value
5380 * stored in 'variable' when the += is performed: one on the
5381 * value stack (in 'v') and one still stored in the
5382 * 'variable'. We try to delete the variable now to reduce
5383 * the refcnt to 1.
5384 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005385 int opcode, oparg;
5386 NEXTOPARG();
5387 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005388 case STORE_FAST:
5389 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005390 PyObject **fastlocals = f->f_localsplus;
5391 if (GETLOCAL(oparg) == v)
5392 SETLOCAL(oparg, NULL);
5393 break;
5394 }
5395 case STORE_DEREF:
5396 {
5397 PyObject **freevars = (f->f_localsplus +
5398 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005399 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005400 if (PyCell_GET(c) == v)
5401 PyCell_Set(c, NULL);
5402 break;
5403 }
5404 case STORE_NAME:
5405 {
5406 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005407 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005408 PyObject *locals = f->f_locals;
5409 if (PyDict_CheckExact(locals) &&
5410 PyDict_GetItem(locals, name) == v) {
5411 if (PyDict_DelItem(locals, name) != 0) {
5412 PyErr_Clear();
5413 }
5414 }
5415 break;
5416 }
5417 }
5418 }
5419 res = v;
5420 PyUnicode_Append(&res, w);
5421 return res;
5422}
5423
Guido van Rossum950361c1997-01-24 13:49:28 +00005424#ifdef DYNAMIC_EXECUTION_PROFILE
5425
Skip Montanarof118cb12001-10-15 20:51:38 +00005426static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005427getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005429 int i;
5430 PyObject *l = PyList_New(256);
5431 if (l == NULL) return NULL;
5432 for (i = 0; i < 256; i++) {
5433 PyObject *x = PyLong_FromLong(a[i]);
5434 if (x == NULL) {
5435 Py_DECREF(l);
5436 return NULL;
5437 }
5438 PyList_SetItem(l, i, x);
5439 }
5440 for (i = 0; i < 256; i++)
5441 a[i] = 0;
5442 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005443}
5444
5445PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005446_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005447{
5448#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005450#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005451 int i;
5452 PyObject *l = PyList_New(257);
5453 if (l == NULL) return NULL;
5454 for (i = 0; i < 257; i++) {
5455 PyObject *x = getarray(dxpairs[i]);
5456 if (x == NULL) {
5457 Py_DECREF(l);
5458 return NULL;
5459 }
5460 PyList_SetItem(l, i, x);
5461 }
5462 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005463#endif
5464}
5465
5466#endif