blob: 5f8dbcd1526163e9aa64c2fc0611fef37b0643b5 [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
1938 if (type->tp_as_async != NULL)
1939 getter = type->tp_as_async->am_aiter;
1940
1941 if (getter != NULL) {
1942 iter = (*getter)(obj);
1943 Py_DECREF(obj);
1944 if (iter == NULL) {
1945 SET_TOP(NULL);
1946 goto error;
1947 }
1948 }
1949 else {
1950 SET_TOP(NULL);
1951 PyErr_Format(
1952 PyExc_TypeError,
1953 "'async for' requires an object with "
1954 "__aiter__ method, got %.100s",
1955 type->tp_name);
1956 Py_DECREF(obj);
1957 goto error;
1958 }
1959
Yury Selivanov5376ba92015-06-22 12:19:30 -04001960 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001961 if (awaitable == NULL) {
1962 SET_TOP(NULL);
1963 PyErr_Format(
1964 PyExc_TypeError,
1965 "'async for' received an invalid object "
1966 "from __aiter__: %.100s",
1967 Py_TYPE(iter)->tp_name);
1968
1969 Py_DECREF(iter);
1970 goto error;
1971 } else
1972 Py_DECREF(iter);
1973
1974 SET_TOP(awaitable);
1975 DISPATCH();
1976 }
1977
1978 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001979 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001980 PyObject *next_iter = NULL;
1981 PyObject *awaitable = NULL;
1982 PyObject *aiter = TOP();
1983 PyTypeObject *type = Py_TYPE(aiter);
1984
1985 if (type->tp_as_async != NULL)
1986 getter = type->tp_as_async->am_anext;
1987
1988 if (getter != NULL) {
1989 next_iter = (*getter)(aiter);
1990 if (next_iter == NULL) {
1991 goto error;
1992 }
1993 }
1994 else {
1995 PyErr_Format(
1996 PyExc_TypeError,
1997 "'async for' requires an iterator with "
1998 "__anext__ method, got %.100s",
1999 type->tp_name);
2000 goto error;
2001 }
2002
Yury Selivanov5376ba92015-06-22 12:19:30 -04002003 awaitable = _PyCoro_GetAwaitableIter(next_iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002004 if (awaitable == NULL) {
2005 PyErr_Format(
2006 PyExc_TypeError,
2007 "'async for' received an invalid object "
2008 "from __anext__: %.100s",
2009 Py_TYPE(next_iter)->tp_name);
2010
2011 Py_DECREF(next_iter);
2012 goto error;
2013 } else
2014 Py_DECREF(next_iter);
2015
2016 PUSH(awaitable);
2017 DISPATCH();
2018 }
2019
2020 TARGET(GET_AWAITABLE) {
2021 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002022 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002023
2024 Py_DECREF(iterable);
2025
Yury Selivanovc724bae2016-03-02 11:30:46 -05002026 if (iter != NULL && PyCoro_CheckExact(iter)) {
2027 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2028 if (yf != NULL) {
2029 /* `iter` is a coroutine object that is being
2030 awaited, `yf` is a pointer to the current awaitable
2031 being awaited on. */
2032 Py_DECREF(yf);
2033 Py_CLEAR(iter);
2034 PyErr_SetString(
2035 PyExc_RuntimeError,
2036 "coroutine is being awaited already");
2037 /* The code below jumps to `error` if `iter` is NULL. */
2038 }
2039 }
2040
Yury Selivanov75445082015-05-11 22:57:16 -04002041 SET_TOP(iter); /* Even if it's NULL */
2042
2043 if (iter == NULL) {
2044 goto error;
2045 }
2046
2047 DISPATCH();
2048 }
2049
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 TARGET(YIELD_FROM) {
2051 PyObject *v = POP();
2052 PyObject *reciever = TOP();
2053 int err;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002054 if (PyGen_CheckExact(reciever) || PyCoro_CheckExact(reciever)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002055 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002056 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002057 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 if (v == Py_None)
2059 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002060 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04002061 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002062 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 Py_DECREF(v);
2064 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002065 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002066 if (tstate->c_tracefunc != NULL
2067 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002068 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002069 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 if (err < 0)
2071 goto error;
2072 Py_DECREF(reciever);
2073 SET_TOP(val);
2074 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002075 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002076 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002077 f->f_stacktop = stack_pointer;
2078 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002079 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002080 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002081 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002083
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 retval = POP();
2086 f->f_stacktop = stack_pointer;
2087 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002090
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 TARGET(POP_EXCEPT) {
2092 PyTryBlock *b = PyFrame_BlockPop(f);
2093 if (b->b_type != EXCEPT_HANDLER) {
2094 PyErr_SetString(PyExc_SystemError,
2095 "popped block is not an except handler");
2096 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002101
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 TARGET(POP_BLOCK) {
2103 PyTryBlock *b = PyFrame_BlockPop(f);
2104 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002106 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 TARGET(END_FINALLY) {
2110 PyObject *status = POP();
2111 if (PyLong_Check(status)) {
2112 why = (enum why_code) PyLong_AS_LONG(status);
2113 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002114 if (why == WHY_RETURN ||
2115 why == WHY_CONTINUE)
2116 retval = POP();
2117 if (why == WHY_SILENCED) {
2118 /* An exception was silenced by 'with', we must
2119 manually unwind the EXCEPT_HANDLER block which was
2120 created when the exception was caught, otherwise
2121 the stack will be in an inconsistent state. */
2122 PyTryBlock *b = PyFrame_BlockPop(f);
2123 assert(b->b_type == EXCEPT_HANDLER);
2124 UNWIND_EXCEPT_HANDLER(b);
2125 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002126 Py_DECREF(status);
2127 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002128 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 Py_DECREF(status);
2130 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 else if (PyExceptionClass_Check(status)) {
2133 PyObject *exc = POP();
2134 PyObject *tb = POP();
2135 PyErr_Restore(status, exc, tb);
2136 why = WHY_EXCEPTION;
2137 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 PyErr_SetString(PyExc_SystemError,
2141 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 Py_DECREF(status);
2143 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 Py_DECREF(status);
2146 DISPATCH();
2147 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002150 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002151
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002153 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2155 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002156 PyErr_SetString(PyExc_NameError,
2157 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002161 }
2162 else {
2163 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2164 if (build_class_str == NULL)
2165 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2167 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002168 if (PyErr_ExceptionMatches(PyExc_KeyError))
2169 PyErr_SetString(PyExc_NameError,
2170 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002175 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002176 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002177
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002178 TARGET(STORE_NAME) {
2179 PyObject *name = GETITEM(names, oparg);
2180 PyObject *v = POP();
2181 PyObject *ns = f->f_locals;
2182 int err;
2183 if (ns == NULL) {
2184 PyErr_Format(PyExc_SystemError,
2185 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 if (PyDict_CheckExact(ns))
2190 err = PyDict_SetItem(ns, name, v);
2191 else
2192 err = PyObject_SetItem(ns, name, v);
2193 Py_DECREF(v);
2194 if (err != 0)
2195 goto error;
2196 DISPATCH();
2197 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002198
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 TARGET(DELETE_NAME) {
2200 PyObject *name = GETITEM(names, oparg);
2201 PyObject *ns = f->f_locals;
2202 int err;
2203 if (ns == NULL) {
2204 PyErr_Format(PyExc_SystemError,
2205 "no locals when deleting %R", name);
2206 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 err = PyObject_DelItem(ns, name);
2209 if (err != 0) {
2210 format_exc_check_arg(PyExc_NameError,
2211 NAME_ERROR_MSG,
2212 name);
2213 goto error;
2214 }
2215 DISPATCH();
2216 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002217
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002218 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 TARGET(UNPACK_SEQUENCE) {
2220 PyObject *seq = POP(), *item, **items;
2221 if (PyTuple_CheckExact(seq) &&
2222 PyTuple_GET_SIZE(seq) == oparg) {
2223 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 item = items[oparg];
2226 Py_INCREF(item);
2227 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 } else if (PyList_CheckExact(seq) &&
2230 PyList_GET_SIZE(seq) == oparg) {
2231 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 item = items[oparg];
2234 Py_INCREF(item);
2235 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 stack_pointer + oparg)) {
2239 STACKADJ(oparg);
2240 } else {
2241 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 Py_DECREF(seq);
2243 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002246 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002248
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002249 TARGET(UNPACK_EX) {
2250 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2251 PyObject *seq = POP();
2252
2253 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2254 stack_pointer + totalargs)) {
2255 stack_pointer += totalargs;
2256 } else {
2257 Py_DECREF(seq);
2258 goto error;
2259 }
2260 Py_DECREF(seq);
2261 DISPATCH();
2262 }
2263
2264 TARGET(STORE_ATTR) {
2265 PyObject *name = GETITEM(names, oparg);
2266 PyObject *owner = TOP();
2267 PyObject *v = SECOND();
2268 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 Py_DECREF(owner);
2273 if (err != 0)
2274 goto error;
2275 DISPATCH();
2276 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002277
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 TARGET(DELETE_ATTR) {
2279 PyObject *name = GETITEM(names, oparg);
2280 PyObject *owner = POP();
2281 int err;
2282 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2283 Py_DECREF(owner);
2284 if (err != 0)
2285 goto error;
2286 DISPATCH();
2287 }
2288
2289 TARGET(STORE_GLOBAL) {
2290 PyObject *name = GETITEM(names, oparg);
2291 PyObject *v = POP();
2292 int err;
2293 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002295 if (err != 0)
2296 goto error;
2297 DISPATCH();
2298 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002299
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002300 TARGET(DELETE_GLOBAL) {
2301 PyObject *name = GETITEM(names, oparg);
2302 int err;
2303 err = PyDict_DelItem(f->f_globals, name);
2304 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002306 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002307 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002308 }
2309 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 TARGET(LOAD_NAME) {
2313 PyObject *name = GETITEM(names, oparg);
2314 PyObject *locals = f->f_locals;
2315 PyObject *v;
2316 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002317 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002318 "no locals when loading %R", name);
2319 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002321 if (PyDict_CheckExact(locals)) {
2322 v = PyDict_GetItem(locals, name);
2323 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002324 }
2325 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002327 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002328 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2329 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 PyErr_Clear();
2331 }
2332 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002333 if (v == NULL) {
2334 v = PyDict_GetItem(f->f_globals, name);
2335 Py_XINCREF(v);
2336 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002337 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002338 v = PyDict_GetItem(f->f_builtins, name);
2339 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002340 format_exc_check_arg(
2341 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 NAME_ERROR_MSG, name);
2343 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002344 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002346 }
2347 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 v = PyObject_GetItem(f->f_builtins, name);
2349 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002350 if (PyErr_ExceptionMatches(PyExc_KeyError))
2351 format_exc_check_arg(
2352 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 NAME_ERROR_MSG, name);
2354 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002355 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002356 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002357 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002362
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 TARGET(LOAD_GLOBAL) {
2364 PyObject *name = GETITEM(names, oparg);
2365 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002366 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002367 && PyDict_CheckExact(f->f_builtins))
2368 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002370 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 name);
2372 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002373 if (!_PyErr_OCCURRED()) {
2374 /* _PyDict_LoadGlobal() returns NULL without raising
2375 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002376 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002377 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002383 else {
2384 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002385
2386 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 v = PyObject_GetItem(f->f_globals, name);
2388 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002389 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2390 goto error;
2391 PyErr_Clear();
2392
Victor Stinnerb4efc962015-11-20 09:24:02 +01002393 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 v = PyObject_GetItem(f->f_builtins, name);
2395 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002396 if (PyErr_ExceptionMatches(PyExc_KeyError))
2397 format_exc_check_arg(
2398 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002399 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002401 }
2402 }
2403 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002405 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002407
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002408 TARGET(DELETE_FAST) {
2409 PyObject *v = GETLOCAL(oparg);
2410 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 SETLOCAL(oparg, NULL);
2412 DISPATCH();
2413 }
2414 format_exc_check_arg(
2415 PyExc_UnboundLocalError,
2416 UNBOUNDLOCAL_ERROR_MSG,
2417 PyTuple_GetItem(co->co_varnames, oparg)
2418 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 goto error;
2420 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002421
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 TARGET(DELETE_DEREF) {
2423 PyObject *cell = freevars[oparg];
2424 if (PyCell_GET(cell) != NULL) {
2425 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002426 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002427 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002428 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 goto error;
2430 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002431
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 TARGET(LOAD_CLOSURE) {
2433 PyObject *cell = freevars[oparg];
2434 Py_INCREF(cell);
2435 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002436 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002437 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002438
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002439 TARGET(LOAD_CLASSDEREF) {
2440 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002441 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002442 assert(locals);
2443 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2444 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2445 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2446 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2447 if (PyDict_CheckExact(locals)) {
2448 value = PyDict_GetItem(locals, name);
2449 Py_XINCREF(value);
2450 }
2451 else {
2452 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002453 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002454 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2455 goto error;
2456 PyErr_Clear();
2457 }
2458 }
2459 if (!value) {
2460 PyObject *cell = freevars[oparg];
2461 value = PyCell_GET(cell);
2462 if (value == NULL) {
2463 format_exc_unbound(co, oparg);
2464 goto error;
2465 }
2466 Py_INCREF(value);
2467 }
2468 PUSH(value);
2469 DISPATCH();
2470 }
2471
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 TARGET(LOAD_DEREF) {
2473 PyObject *cell = freevars[oparg];
2474 PyObject *value = PyCell_GET(cell);
2475 if (value == NULL) {
2476 format_exc_unbound(co, oparg);
2477 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 Py_INCREF(value);
2480 PUSH(value);
2481 DISPATCH();
2482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002483
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 TARGET(STORE_DEREF) {
2485 PyObject *v = POP();
2486 PyObject *cell = freevars[oparg];
2487 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 DISPATCH();
2490 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002491
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 TARGET(BUILD_TUPLE) {
2493 PyObject *tup = PyTuple_New(oparg);
2494 if (tup == NULL)
2495 goto error;
2496 while (--oparg >= 0) {
2497 PyObject *item = POP();
2498 PyTuple_SET_ITEM(tup, oparg, item);
2499 }
2500 PUSH(tup);
2501 DISPATCH();
2502 }
2503
2504 TARGET(BUILD_LIST) {
2505 PyObject *list = PyList_New(oparg);
2506 if (list == NULL)
2507 goto error;
2508 while (--oparg >= 0) {
2509 PyObject *item = POP();
2510 PyList_SET_ITEM(list, oparg, item);
2511 }
2512 PUSH(list);
2513 DISPATCH();
2514 }
2515
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002516 TARGET(BUILD_TUPLE_UNPACK)
2517 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002518 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
2519 int i;
2520 PyObject *sum = PyList_New(0);
2521 PyObject *return_value;
2522 if (sum == NULL)
2523 goto error;
2524
2525 for (i = oparg; i > 0; i--) {
2526 PyObject *none_val;
2527
2528 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2529 if (none_val == NULL) {
2530 Py_DECREF(sum);
2531 goto error;
2532 }
2533 Py_DECREF(none_val);
2534 }
2535
2536 if (convert_to_tuple) {
2537 return_value = PyList_AsTuple(sum);
2538 Py_DECREF(sum);
2539 if (return_value == NULL)
2540 goto error;
2541 }
2542 else {
2543 return_value = sum;
2544 }
2545
2546 while (oparg--)
2547 Py_DECREF(POP());
2548 PUSH(return_value);
2549 DISPATCH();
2550 }
2551
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 TARGET(BUILD_SET) {
2553 PyObject *set = PySet_New(NULL);
2554 int err = 0;
2555 if (set == NULL)
2556 goto error;
2557 while (--oparg >= 0) {
2558 PyObject *item = POP();
2559 if (err == 0)
2560 err = PySet_Add(set, item);
2561 Py_DECREF(item);
2562 }
2563 if (err != 0) {
2564 Py_DECREF(set);
2565 goto error;
2566 }
2567 PUSH(set);
2568 DISPATCH();
2569 }
2570
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002571 TARGET(BUILD_SET_UNPACK) {
2572 int i;
2573 PyObject *sum = PySet_New(NULL);
2574 if (sum == NULL)
2575 goto error;
2576
2577 for (i = oparg; i > 0; i--) {
2578 if (_PySet_Update(sum, PEEK(i)) < 0) {
2579 Py_DECREF(sum);
2580 goto error;
2581 }
2582 }
2583
2584 while (oparg--)
2585 Py_DECREF(POP());
2586 PUSH(sum);
2587 DISPATCH();
2588 }
2589
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 TARGET(BUILD_MAP) {
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002591 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2593 if (map == NULL)
2594 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002595 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002596 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002597 PyObject *key = PEEK(2*i);
2598 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002599 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002600 if (err != 0) {
2601 Py_DECREF(map);
2602 goto error;
2603 }
2604 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002605
2606 while (oparg--) {
2607 Py_DECREF(POP());
2608 Py_DECREF(POP());
2609 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 PUSH(map);
2611 DISPATCH();
2612 }
2613
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002614 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2615 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002616 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
2617 int num_maps;
2618 int function_location;
2619 int i;
2620 PyObject *sum = PyDict_New();
2621 if (sum == NULL)
2622 goto error;
2623 if (with_call) {
2624 num_maps = oparg & 0xff;
2625 function_location = (oparg>>8) & 0xff;
2626 }
2627 else {
2628 num_maps = oparg;
2629 }
2630
2631 for (i = num_maps; i > 0; i--) {
2632 PyObject *arg = PEEK(i);
2633 if (with_call) {
2634 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2635
2636 if (intersection == NULL) {
2637 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2638 PyObject *func = (
2639 PEEK(function_location + num_maps));
2640 PyErr_Format(PyExc_TypeError,
2641 "%.200s%.200s argument after ** "
2642 "must be a mapping, not %.200s",
2643 PyEval_GetFuncName(func),
2644 PyEval_GetFuncDesc(func),
2645 arg->ob_type->tp_name);
2646 }
2647 Py_DECREF(sum);
2648 goto error;
2649 }
2650
2651 if (PySet_GET_SIZE(intersection)) {
2652 Py_ssize_t idx = 0;
2653 PyObject *key;
2654 PyObject *func = PEEK(function_location + num_maps);
2655 Py_hash_t hash;
2656 _PySet_NextEntry(intersection, &idx, &key, &hash);
2657 if (!PyUnicode_Check(key)) {
2658 PyErr_Format(PyExc_TypeError,
2659 "%.200s%.200s keywords must be strings",
2660 PyEval_GetFuncName(func),
2661 PyEval_GetFuncDesc(func));
2662 } else {
2663 PyErr_Format(PyExc_TypeError,
2664 "%.200s%.200s got multiple "
2665 "values for keyword argument '%U'",
2666 PyEval_GetFuncName(func),
2667 PyEval_GetFuncDesc(func),
2668 key);
2669 }
2670 Py_DECREF(intersection);
2671 Py_DECREF(sum);
2672 goto error;
2673 }
2674 Py_DECREF(intersection);
2675 }
2676
2677 if (PyDict_Update(sum, arg) < 0) {
2678 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2679 PyErr_Format(PyExc_TypeError,
2680 "'%.200s' object is not a mapping",
2681 arg->ob_type->tp_name);
2682 }
2683 Py_DECREF(sum);
2684 goto error;
2685 }
2686 }
2687
2688 while (num_maps--)
2689 Py_DECREF(POP());
2690 PUSH(sum);
2691 DISPATCH();
2692 }
2693
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 TARGET(MAP_ADD) {
2695 PyObject *key = TOP();
2696 PyObject *value = SECOND();
2697 PyObject *map;
2698 int err;
2699 STACKADJ(-2);
2700 map = stack_pointer[-oparg]; /* dict */
2701 assert(PyDict_CheckExact(map));
2702 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2703 Py_DECREF(value);
2704 Py_DECREF(key);
2705 if (err != 0)
2706 goto error;
2707 PREDICT(JUMP_ABSOLUTE);
2708 DISPATCH();
2709 }
2710
2711 TARGET(LOAD_ATTR) {
2712 PyObject *name = GETITEM(names, oparg);
2713 PyObject *owner = TOP();
2714 PyObject *res = PyObject_GetAttr(owner, name);
2715 Py_DECREF(owner);
2716 SET_TOP(res);
2717 if (res == NULL)
2718 goto error;
2719 DISPATCH();
2720 }
2721
2722 TARGET(COMPARE_OP) {
2723 PyObject *right = POP();
2724 PyObject *left = TOP();
2725 PyObject *res = cmp_outcome(oparg, left, right);
2726 Py_DECREF(left);
2727 Py_DECREF(right);
2728 SET_TOP(res);
2729 if (res == NULL)
2730 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 PREDICT(POP_JUMP_IF_FALSE);
2732 PREDICT(POP_JUMP_IF_TRUE);
2733 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002734 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002735
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 TARGET(IMPORT_NAME) {
2737 _Py_IDENTIFIER(__import__);
2738 PyObject *name = GETITEM(names, oparg);
2739 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002740 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 if (func == NULL) {
2742 PyErr_SetString(PyExc_ImportError,
2743 "__import__ not found");
2744 goto error;
2745 }
2746 Py_INCREF(func);
2747 from = POP();
2748 level = TOP();
2749 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2750 args = PyTuple_Pack(5,
2751 name,
2752 f->f_globals,
2753 f->f_locals == NULL ?
2754 Py_None : f->f_locals,
2755 from,
2756 level);
2757 else
2758 args = PyTuple_Pack(4,
2759 name,
2760 f->f_globals,
2761 f->f_locals == NULL ?
2762 Py_None : f->f_locals,
2763 from);
2764 Py_DECREF(level);
2765 Py_DECREF(from);
2766 if (args == NULL) {
2767 Py_DECREF(func);
2768 STACKADJ(-1);
2769 goto error;
2770 }
2771 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002772 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 READ_TIMESTAMP(intr1);
2774 Py_DECREF(args);
2775 Py_DECREF(func);
2776 SET_TOP(res);
2777 if (res == NULL)
2778 goto error;
2779 DISPATCH();
2780 }
2781
2782 TARGET(IMPORT_STAR) {
2783 PyObject *from = POP(), *locals;
2784 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002785 if (PyFrame_FastToLocalsWithError(f) < 0)
2786 goto error;
2787
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 locals = f->f_locals;
2789 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 PyErr_SetString(PyExc_SystemError,
2791 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 }
2794 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 READ_TIMESTAMP(intr1);
2797 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 Py_DECREF(from);
2799 if (err != 0)
2800 goto error;
2801 DISPATCH();
2802 }
Guido van Rossum25831651993-05-19 14:50:45 +00002803
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 TARGET(IMPORT_FROM) {
2805 PyObject *name = GETITEM(names, oparg);
2806 PyObject *from = TOP();
2807 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 PUSH(res);
2812 if (res == NULL)
2813 goto error;
2814 DISPATCH();
2815 }
Thomas Wouters52152252000-08-17 22:55:00 +00002816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 JUMPBY(oparg);
2819 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002821
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002822 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 TARGET(POP_JUMP_IF_FALSE) {
2824 PyObject *cond = POP();
2825 int err;
2826 if (cond == Py_True) {
2827 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 FAST_DISPATCH();
2829 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 if (cond == Py_False) {
2831 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 JUMPTO(oparg);
2833 FAST_DISPATCH();
2834 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 err = PyObject_IsTrue(cond);
2836 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (err > 0)
2838 err = 0;
2839 else if (err == 0)
2840 JUMPTO(oparg);
2841 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002845
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002846 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 TARGET(POP_JUMP_IF_TRUE) {
2848 PyObject *cond = POP();
2849 int err;
2850 if (cond == Py_False) {
2851 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 FAST_DISPATCH();
2853 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 if (cond == Py_True) {
2855 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 JUMPTO(oparg);
2857 FAST_DISPATCH();
2858 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 err = PyObject_IsTrue(cond);
2860 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 if (err > 0) {
2862 err = 0;
2863 JUMPTO(oparg);
2864 }
2865 else if (err == 0)
2866 ;
2867 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002871
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 TARGET(JUMP_IF_FALSE_OR_POP) {
2873 PyObject *cond = TOP();
2874 int err;
2875 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 FAST_DISPATCH();
2879 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 JUMPTO(oparg);
2882 FAST_DISPATCH();
2883 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 if (err > 0) {
2886 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 err = 0;
2889 }
2890 else if (err == 0)
2891 JUMPTO(oparg);
2892 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002896
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 TARGET(JUMP_IF_TRUE_OR_POP) {
2898 PyObject *cond = TOP();
2899 int err;
2900 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 FAST_DISPATCH();
2904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 JUMPTO(oparg);
2907 FAST_DISPATCH();
2908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (err > 0) {
2911 err = 0;
2912 JUMPTO(oparg);
2913 }
2914 else if (err == 0) {
2915 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 }
2918 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002922
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002923 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002926#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 /* Enabling this path speeds-up all while and for-loops by bypassing
2928 the per-loop checks for signals. By default, this should be turned-off
2929 because it prevents detection of a control-break in tight loops like
2930 "while 1: pass". Compile with this option turned-on when you need
2931 the speed-up and do not need break checking inside tight loops (ones
2932 that contain only instructions ending with FAST_DISPATCH).
2933 */
2934 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002935#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002937#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002939
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002943 PyObject *iter = PyObject_GetIter(iterable);
2944 Py_DECREF(iterable);
2945 SET_TOP(iter);
2946 if (iter == NULL)
2947 goto error;
2948 PREDICT(FOR_ITER);
2949 DISPATCH();
2950 }
2951
2952 TARGET(GET_YIELD_FROM_ITER) {
2953 /* before: [obj]; after [getiter(obj)] */
2954 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002955 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002956 if (PyCoro_CheckExact(iterable)) {
2957 /* `iterable` is a coroutine */
2958 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2959 /* and it is used in a 'yield from' expression of a
2960 regular generator. */
2961 Py_DECREF(iterable);
2962 SET_TOP(NULL);
2963 PyErr_SetString(PyExc_TypeError,
2964 "cannot 'yield from' a coroutine object "
2965 "in a non-coroutine generator");
2966 goto error;
2967 }
2968 }
2969 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002970 /* `iterable` is not a generator. */
2971 iter = PyObject_GetIter(iterable);
2972 Py_DECREF(iterable);
2973 SET_TOP(iter);
2974 if (iter == NULL)
2975 goto error;
2976 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 DISPATCH();
2978 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002979
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002980 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 PyObject *iter = TOP();
2984 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2985 if (next != NULL) {
2986 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PREDICT(STORE_FAST);
2988 PREDICT(UNPACK_SEQUENCE);
2989 DISPATCH();
2990 }
2991 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2993 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002994 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002995 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 PyErr_Clear();
2997 }
2998 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 STACKADJ(-1);
3000 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 JUMPBY(oparg);
3002 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003004
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 why = WHY_BREAK;
3007 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003009
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 if (retval == NULL)
3013 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 why = WHY_CONTINUE;
3015 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003017
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003018 TARGET(SETUP_LOOP)
3019 TARGET(SETUP_EXCEPT)
3020 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 /* NOTE: If you add any new block-setup opcodes that
3022 are not try/except/finally handlers, you may need
3023 to update the PyGen_NeedsFinalizing() function.
3024 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3027 STACK_LEVEL());
3028 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003030
Yury Selivanov75445082015-05-11 22:57:16 -04003031 TARGET(BEFORE_ASYNC_WITH) {
3032 _Py_IDENTIFIER(__aexit__);
3033 _Py_IDENTIFIER(__aenter__);
3034
3035 PyObject *mgr = TOP();
3036 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3037 *enter;
3038 PyObject *res;
3039 if (exit == NULL)
3040 goto error;
3041 SET_TOP(exit);
3042 enter = special_lookup(mgr, &PyId___aenter__);
3043 Py_DECREF(mgr);
3044 if (enter == NULL)
3045 goto error;
3046 res = PyObject_CallFunctionObjArgs(enter, NULL);
3047 Py_DECREF(enter);
3048 if (res == NULL)
3049 goto error;
3050 PUSH(res);
3051 DISPATCH();
3052 }
3053
3054 TARGET(SETUP_ASYNC_WITH) {
3055 PyObject *res = POP();
3056 /* Setup the finally block before pushing the result
3057 of __aenter__ on the stack. */
3058 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3059 STACK_LEVEL());
3060 PUSH(res);
3061 DISPATCH();
3062 }
3063
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003065 _Py_IDENTIFIER(__exit__);
3066 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003067 PyObject *mgr = TOP();
3068 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3069 PyObject *res;
3070 if (exit == NULL)
3071 goto error;
3072 SET_TOP(exit);
3073 enter = special_lookup(mgr, &PyId___enter__);
3074 Py_DECREF(mgr);
3075 if (enter == NULL)
3076 goto error;
3077 res = PyObject_CallFunctionObjArgs(enter, NULL);
3078 Py_DECREF(enter);
3079 if (res == NULL)
3080 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 /* Setup the finally block before pushing the result
3082 of __enter__ on the stack. */
3083 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3084 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003086 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 DISPATCH();
3088 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003089
Yury Selivanov75445082015-05-11 22:57:16 -04003090 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003091 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 how/why we entered the finally clause:
3093 - TOP = None
3094 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3095 - TOP = WHY_*; no retval below it
3096 - (TOP, SECOND, THIRD) = exc_info()
3097 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3098 Below them is EXIT, the context.__exit__ bound method.
3099 In the last case, we must call
3100 EXIT(TOP, SECOND, THIRD)
3101 otherwise we must call
3102 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003103
Benjamin Peterson8f169482013-10-29 22:25:06 -04003104 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003106 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003109 In addition, if the stack represents an exception,
3110 *and* the function call returns a 'true' value, we
3111 push WHY_SILENCED onto the stack. END_FINALLY will
3112 then not re-raise the exception. (But non-local
3113 gotos should still be resumed.)
3114 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 (void)POP();
3120 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003121 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003122 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 else if (PyLong_Check(exc)) {
3124 STACKADJ(-1);
3125 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 case WHY_RETURN:
3127 case WHY_CONTINUE:
3128 /* Retval in TOP. */
3129 exit_func = SECOND();
3130 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003131 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003132 break;
3133 default:
3134 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 break;
3137 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003138 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 }
3140 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 val = SECOND();
3144 tb = THIRD();
3145 tp2 = FOURTH();
3146 exc2 = PEEK(5);
3147 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003149 SET_VALUE(7, tb2);
3150 SET_VALUE(6, exc2);
3151 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3153 SET_FOURTH(NULL);
3154 /* We just shifted the stack down, so we have
3155 to tell the except handler block that the
3156 values are lower than it expects. */
3157 block = &f->f_blockstack[f->f_iblock - 1];
3158 assert(block->b_type == EXCEPT_HANDLER);
3159 block->b_level--;
3160 }
3161 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 if (res == NULL)
3165 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003166
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003167 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003168 PUSH(exc);
3169 PUSH(res);
3170 PREDICT(WITH_CLEANUP_FINISH);
3171 DISPATCH();
3172 }
3173
3174 PREDICTED(WITH_CLEANUP_FINISH);
3175 TARGET(WITH_CLEANUP_FINISH) {
3176 PyObject *res = POP();
3177 PyObject *exc = POP();
3178 int err;
3179
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 if (exc != Py_None)
3181 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003182 else
3183 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003184
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003186 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003189 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 else if (err > 0) {
3191 err = 0;
3192 /* There was an exception and a True return */
3193 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3194 }
3195 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003198
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 TARGET(CALL_FUNCTION) {
3200 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 PCALL(PCALL_ALL);
3202 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003203#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003204 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003205#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003206 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003207#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003209 PUSH(res);
3210 if (res == NULL)
3211 goto error;
3212 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003214
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003215 TARGET(CALL_FUNCTION_VAR)
3216 TARGET(CALL_FUNCTION_KW)
3217 TARGET(CALL_FUNCTION_VAR_KW) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003218 int na = oparg & 0xff;
3219 int nk = (oparg>>8) & 0xff;
3220 int flags = (opcode - CALL_FUNCTION) & 3;
3221 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 PCALL(PCALL_ALL);
3224 if (flags & CALL_FLAG_VAR)
3225 n++;
3226 if (flags & CALL_FLAG_KW)
3227 n++;
3228 pfunc = stack_pointer - n - 1;
3229 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00003232 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 PyObject *self = PyMethod_GET_SELF(func);
3234 Py_INCREF(self);
3235 func = PyMethod_GET_FUNCTION(func);
3236 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03003237 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00003239 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 } else
3241 Py_INCREF(func);
3242 sp = stack_pointer;
3243 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003244 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 READ_TIMESTAMP(intr1);
3246 stack_pointer = sp;
3247 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 PyObject *o = POP();
3251 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 PUSH(res);
3254 if (res == NULL)
3255 goto error;
3256 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003258
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003259 TARGET(MAKE_CLOSURE)
3260 TARGET(MAKE_FUNCTION) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 int posdefaults = oparg & 0xff;
3262 int kwdefaults = (oparg>>8) & 0xff;
3263 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003265 PyObject *qualname = POP(); /* qualname */
3266 PyObject *code = POP(); /* code object */
3267 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
3268 Py_DECREF(code);
3269 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003270
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003271 if (func == NULL)
3272 goto error;
3273
3274 if (opcode == MAKE_CLOSURE) {
3275 PyObject *closure = POP();
3276 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 Py_DECREF(func);
3279 Py_DECREF(closure);
3280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003282 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003287 PyObject *names = POP(); /* names of args with annotations */
3288 PyObject *anns = PyDict_New();
3289 if (anns == NULL) {
3290 Py_DECREF(func);
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003291 Py_DECREF(names);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 assert(num_annotations == name_ix+1);
3296 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 PyObject *name, *value;
3298 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 name = PyTuple_GET_ITEM(names, name_ix);
3301 value = POP();
3302 err = PyDict_SetItem(anns, name, value);
3303 Py_DECREF(value);
3304 if (err != 0) {
3305 Py_DECREF(anns);
3306 Py_DECREF(func);
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003307 Py_DECREF(names);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003308 goto error;
3309 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 }
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003311 Py_DECREF(names);
Neal Norwitzc1505362006-12-28 06:47:50 +00003312
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003313 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 /* Can't happen unless
3315 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 Py_DECREF(anns);
3317 Py_DECREF(func);
3318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003320 Py_DECREF(anns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003324 if (kwdefaults > 0) {
3325 PyObject *defs = PyDict_New();
3326 if (defs == NULL) {
3327 Py_DECREF(func);
3328 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 }
3330 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 PyObject *v = POP(); /* default value */
3332 PyObject *key = POP(); /* kw only arg name */
3333 int err = PyDict_SetItem(defs, key, v);
3334 Py_DECREF(v);
3335 Py_DECREF(key);
3336 if (err != 0) {
3337 Py_DECREF(defs);
3338 Py_DECREF(func);
3339 goto error;
3340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 /* Can't happen unless
3344 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 Py_DECREF(func);
3346 Py_DECREF(defs);
3347 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003349 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05003351 if (posdefaults > 0) {
3352 PyObject *defs = PyTuple_New(posdefaults);
3353 if (defs == NULL) {
3354 Py_DECREF(func);
3355 goto error;
3356 }
3357 while (--posdefaults >= 0)
3358 PyTuple_SET_ITEM(defs, posdefaults, POP());
3359 if (PyFunction_SetDefaults(func, defs) != 0) {
3360 /* Can't happen unless
3361 PyFunction_SetDefaults changes. */
3362 Py_DECREF(defs);
3363 Py_DECREF(func);
3364 goto error;
3365 }
3366 Py_DECREF(defs);
3367 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 PUSH(func);
3369 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003371
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003372 TARGET(BUILD_SLICE) {
3373 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003375 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003376 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003377 step = NULL;
3378 stop = POP();
3379 start = TOP();
3380 slice = PySlice_New(start, stop, step);
3381 Py_DECREF(start);
3382 Py_DECREF(stop);
3383 Py_XDECREF(step);
3384 SET_TOP(slice);
3385 if (slice == NULL)
3386 goto error;
3387 DISPATCH();
3388 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003389
Eric V. Smitha78c7952015-11-03 12:45:05 -05003390 TARGET(FORMAT_VALUE) {
3391 /* Handles f-string value formatting. */
3392 PyObject *result;
3393 PyObject *fmt_spec;
3394 PyObject *value;
3395 PyObject *(*conv_fn)(PyObject *);
3396 int which_conversion = oparg & FVC_MASK;
3397 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3398
3399 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003400 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003401
3402 /* See if any conversion is specified. */
3403 switch (which_conversion) {
3404 case FVC_STR: conv_fn = PyObject_Str; break;
3405 case FVC_REPR: conv_fn = PyObject_Repr; break;
3406 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3407
3408 /* Must be 0 (meaning no conversion), since only four
3409 values are allowed by (oparg & FVC_MASK). */
3410 default: conv_fn = NULL; break;
3411 }
3412
3413 /* If there's a conversion function, call it and replace
3414 value with that result. Otherwise, just use value,
3415 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003416 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003417 result = conv_fn(value);
3418 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003419 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003420 Py_XDECREF(fmt_spec);
3421 goto error;
3422 }
3423 value = result;
3424 }
3425
3426 /* If value is a unicode object, and there's no fmt_spec,
3427 then we know the result of format(value) is value
3428 itself. In that case, skip calling format(). I plan to
3429 move this optimization in to PyObject_Format()
3430 itself. */
3431 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3432 /* Do nothing, just transfer ownership to result. */
3433 result = value;
3434 } else {
3435 /* Actually call format(). */
3436 result = PyObject_Format(value, fmt_spec);
3437 Py_DECREF(value);
3438 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003439 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003440 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003441 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003442 }
3443
Eric V. Smith135d5f42016-02-05 18:23:08 -05003444 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003445 DISPATCH();
3446 }
3447
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003448 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003449 int oldoparg = oparg;
3450 NEXTOPARG();
3451 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003452 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003454
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003455
Antoine Pitrou042b1282010-08-13 21:15:58 +00003456#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003458#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 default:
3460 fprintf(stderr,
3461 "XXX lineno: %d, opcode: %d\n",
3462 PyFrame_GetLineNumber(f),
3463 opcode);
3464 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003465 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003466
3467#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003469#endif
3470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 /* This should never be reached. Every opcode should end with DISPATCH()
3474 or goto error. */
3475 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003476
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003479
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 assert(why == WHY_NOT);
3481 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003482
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003484#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 if (!PyErr_Occurred())
3486 PyErr_SetString(PyExc_SystemError,
3487 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003488#else
3489 assert(PyErr_Occurred());
3490#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003491
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 /* Log traceback info. */
3493 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003494
Benjamin Peterson51f46162013-01-23 08:38:47 -05003495 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003496 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3497 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003498
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003499fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 assert(why != WHY_NOT);
3501
3502 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 while (why != WHY_NOT && f->f_iblock > 0) {
3504 /* Peek at the current block. */
3505 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 assert(why != WHY_YIELD);
3508 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3509 why = WHY_NOT;
3510 JUMPTO(PyLong_AS_LONG(retval));
3511 Py_DECREF(retval);
3512 break;
3513 }
3514 /* Now we have to pop the block. */
3515 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 if (b->b_type == EXCEPT_HANDLER) {
3518 UNWIND_EXCEPT_HANDLER(b);
3519 continue;
3520 }
3521 UNWIND_BLOCK(b);
3522 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3523 why = WHY_NOT;
3524 JUMPTO(b->b_handler);
3525 break;
3526 }
3527 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3528 || b->b_type == SETUP_FINALLY)) {
3529 PyObject *exc, *val, *tb;
3530 int handler = b->b_handler;
3531 /* Beware, this invalidates all b->b_* fields */
3532 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3533 PUSH(tstate->exc_traceback);
3534 PUSH(tstate->exc_value);
3535 if (tstate->exc_type != NULL) {
3536 PUSH(tstate->exc_type);
3537 }
3538 else {
3539 Py_INCREF(Py_None);
3540 PUSH(Py_None);
3541 }
3542 PyErr_Fetch(&exc, &val, &tb);
3543 /* Make the raw exception data
3544 available to the handler,
3545 so a program can emulate the
3546 Python main loop. */
3547 PyErr_NormalizeException(
3548 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003549 if (tb != NULL)
3550 PyException_SetTraceback(val, tb);
3551 else
3552 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 Py_INCREF(exc);
3554 tstate->exc_type = exc;
3555 Py_INCREF(val);
3556 tstate->exc_value = val;
3557 tstate->exc_traceback = tb;
3558 if (tb == NULL)
3559 tb = Py_None;
3560 Py_INCREF(tb);
3561 PUSH(tb);
3562 PUSH(val);
3563 PUSH(exc);
3564 why = WHY_NOT;
3565 JUMPTO(handler);
3566 break;
3567 }
3568 if (b->b_type == SETUP_FINALLY) {
3569 if (why & (WHY_RETURN | WHY_CONTINUE))
3570 PUSH(retval);
3571 PUSH(PyLong_FromLong((long)why));
3572 why = WHY_NOT;
3573 JUMPTO(b->b_handler);
3574 break;
3575 }
3576 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 if (why != WHY_NOT)
3581 break;
3582 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003583
Victor Stinnerace47d72013-07-18 01:41:08 +02003584 assert(!PyErr_Occurred());
3585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 assert(why != WHY_YIELD);
3589 /* Pop remaining stack entries. */
3590 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003591 PyObject *o = POP();
3592 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 if (why != WHY_RETURN)
3596 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003597
Victor Stinner4a7cc882015-03-06 23:35:27 +01003598 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003599
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003600fast_yield:
Yury Selivanov5376ba92015-06-22 12:19:30 -04003601 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003602
Benjamin Petersonac913412011-07-03 16:25:11 -05003603 /* The purpose of this block is to put aside the generator's exception
3604 state and restore that of the calling frame. If the current
3605 exception state is from the caller, we clear the exception values
3606 on the generator frame, so they are not swapped back in latter. The
3607 origin of the current exception state is determined by checking for
3608 except handler blocks, which we must be in iff a new exception
3609 state came into existence in this frame. (An uncaught exception
3610 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3611 int i;
3612 for (i = 0; i < f->f_iblock; i++)
3613 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3614 break;
3615 if (i == f->f_iblock)
3616 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003617 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003618 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003619 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003620 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003623 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003625 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3626 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003628 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 why = WHY_EXCEPTION;
3630 }
3631 }
3632 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003633 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3634 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 PyTrace_RETURN, NULL);
3636 }
3637 }
3638 if (tstate->c_profilefunc) {
3639 if (why == WHY_EXCEPTION)
3640 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003641 tstate->c_profileobj,
3642 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003644 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3645 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003647 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003648 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 }
3650 }
3651 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003654exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003656 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003658
Victor Stinnerefde1462015-03-21 15:04:43 +01003659 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003660}
3661
Benjamin Petersonb204a422011-06-05 22:04:07 -05003662static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003663format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3664{
3665 int err;
3666 Py_ssize_t len = PyList_GET_SIZE(names);
3667 PyObject *name_str, *comma, *tail, *tmp;
3668
3669 assert(PyList_CheckExact(names));
3670 assert(len >= 1);
3671 /* Deal with the joys of natural language. */
3672 switch (len) {
3673 case 1:
3674 name_str = PyList_GET_ITEM(names, 0);
3675 Py_INCREF(name_str);
3676 break;
3677 case 2:
3678 name_str = PyUnicode_FromFormat("%U and %U",
3679 PyList_GET_ITEM(names, len - 2),
3680 PyList_GET_ITEM(names, len - 1));
3681 break;
3682 default:
3683 tail = PyUnicode_FromFormat(", %U, and %U",
3684 PyList_GET_ITEM(names, len - 2),
3685 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003686 if (tail == NULL)
3687 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003688 /* Chop off the last two objects in the list. This shouldn't actually
3689 fail, but we can't be too careful. */
3690 err = PyList_SetSlice(names, len - 2, len, NULL);
3691 if (err == -1) {
3692 Py_DECREF(tail);
3693 return;
3694 }
3695 /* Stitch everything up into a nice comma-separated list. */
3696 comma = PyUnicode_FromString(", ");
3697 if (comma == NULL) {
3698 Py_DECREF(tail);
3699 return;
3700 }
3701 tmp = PyUnicode_Join(comma, names);
3702 Py_DECREF(comma);
3703 if (tmp == NULL) {
3704 Py_DECREF(tail);
3705 return;
3706 }
3707 name_str = PyUnicode_Concat(tmp, tail);
3708 Py_DECREF(tmp);
3709 Py_DECREF(tail);
3710 break;
3711 }
3712 if (name_str == NULL)
3713 return;
3714 PyErr_Format(PyExc_TypeError,
3715 "%U() missing %i required %s argument%s: %U",
3716 co->co_name,
3717 len,
3718 kind,
3719 len == 1 ? "" : "s",
3720 name_str);
3721 Py_DECREF(name_str);
3722}
3723
3724static void
3725missing_arguments(PyCodeObject *co, int missing, int defcount,
3726 PyObject **fastlocals)
3727{
3728 int i, j = 0;
3729 int start, end;
3730 int positional = defcount != -1;
3731 const char *kind = positional ? "positional" : "keyword-only";
3732 PyObject *missing_names;
3733
3734 /* Compute the names of the arguments that are missing. */
3735 missing_names = PyList_New(missing);
3736 if (missing_names == NULL)
3737 return;
3738 if (positional) {
3739 start = 0;
3740 end = co->co_argcount - defcount;
3741 }
3742 else {
3743 start = co->co_argcount;
3744 end = start + co->co_kwonlyargcount;
3745 }
3746 for (i = start; i < end; i++) {
3747 if (GETLOCAL(i) == NULL) {
3748 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3749 PyObject *name = PyObject_Repr(raw);
3750 if (name == NULL) {
3751 Py_DECREF(missing_names);
3752 return;
3753 }
3754 PyList_SET_ITEM(missing_names, j++, name);
3755 }
3756 }
3757 assert(j == missing);
3758 format_missing(kind, co, missing_names);
3759 Py_DECREF(missing_names);
3760}
3761
3762static void
3763too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003764{
3765 int plural;
3766 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767 int i;
3768 PyObject *sig, *kwonly_sig;
3769
Benjamin Petersone109c702011-06-24 09:37:26 -05003770 assert((co->co_flags & CO_VARARGS) == 0);
3771 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003772 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003773 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003775 if (defcount) {
3776 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 plural = 1;
3778 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3779 }
3780 else {
3781 plural = co->co_argcount != 1;
3782 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3783 }
3784 if (sig == NULL)
3785 return;
3786 if (kwonly_given) {
3787 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3788 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3789 kwonly_given != 1 ? "s" : "");
3790 if (kwonly_sig == NULL) {
3791 Py_DECREF(sig);
3792 return;
3793 }
3794 }
3795 else {
3796 /* This will not fail. */
3797 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003798 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003799 }
3800 PyErr_Format(PyExc_TypeError,
3801 "%U() takes %U positional argument%s but %d%U %s given",
3802 co->co_name,
3803 sig,
3804 plural ? "s" : "",
3805 given,
3806 kwonly_sig,
3807 given == 1 && !kwonly_given ? "was" : "were");
3808 Py_DECREF(sig);
3809 Py_DECREF(kwonly_sig);
3810}
3811
Guido van Rossumc2e20742006-02-27 22:32:47 +00003812/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003813 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003814 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003815
Victor Stinner40ee3012014-06-16 15:59:28 +02003816static PyObject *
3817_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003819 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3820 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003821{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003822 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003823 PyFrameObject *f;
3824 PyObject *retval = NULL;
3825 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 PyThreadState *tstate = PyThreadState_GET();
3827 PyObject *x, *u;
3828 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 int i;
3830 int n = argcount;
3831 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 if (globals == NULL) {
3834 PyErr_SetString(PyExc_SystemError,
3835 "PyEval_EvalCodeEx: NULL globals");
3836 return NULL;
3837 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 assert(tstate != NULL);
3840 assert(globals != NULL);
3841 f = PyFrame_New(tstate, co, globals, locals);
3842 if (f == NULL)
3843 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 fastlocals = f->f_localsplus;
3846 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003847
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 /* Parse arguments. */
3849 if (co->co_flags & CO_VARKEYWORDS) {
3850 kwdict = PyDict_New();
3851 if (kwdict == NULL)
3852 goto fail;
3853 i = total_args;
3854 if (co->co_flags & CO_VARARGS)
3855 i++;
3856 SETLOCAL(i, kwdict);
3857 }
3858 if (argcount > co->co_argcount)
3859 n = co->co_argcount;
3860 for (i = 0; i < n; i++) {
3861 x = args[i];
3862 Py_INCREF(x);
3863 SETLOCAL(i, x);
3864 }
3865 if (co->co_flags & CO_VARARGS) {
3866 u = PyTuple_New(argcount - n);
3867 if (u == NULL)
3868 goto fail;
3869 SETLOCAL(total_args, u);
3870 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 x = args[i];
3872 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 }
3876 for (i = 0; i < kwcount; i++) {
3877 PyObject **co_varnames;
3878 PyObject *keyword = kws[2*i];
3879 PyObject *value = kws[2*i + 1];
3880 int j;
3881 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3882 PyErr_Format(PyExc_TypeError,
3883 "%U() keywords must be strings",
3884 co->co_name);
3885 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003887 /* Speed hack: do raw pointer compares. As names are
3888 normally interned this should almost always hit. */
3889 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3890 for (j = 0; j < total_args; j++) {
3891 PyObject *nm = co_varnames[j];
3892 if (nm == keyword)
3893 goto kw_found;
3894 }
3895 /* Slow fallback, just in case */
3896 for (j = 0; j < total_args; j++) {
3897 PyObject *nm = co_varnames[j];
3898 int cmp = PyObject_RichCompareBool(
3899 keyword, nm, Py_EQ);
3900 if (cmp > 0)
3901 goto kw_found;
3902 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 }
3905 if (j >= total_args && kwdict == NULL) {
3906 PyErr_Format(PyExc_TypeError,
3907 "%U() got an unexpected "
3908 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 co->co_name,
3910 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003911 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003913 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3914 goto fail;
3915 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 continue;
3917 kw_found:
3918 if (GETLOCAL(j) != NULL) {
3919 PyErr_Format(PyExc_TypeError,
3920 "%U() got multiple "
3921 "values for argument '%S'",
3922 co->co_name,
3923 keyword);
3924 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003926 Py_INCREF(value);
3927 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003930 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 goto fail;
3932 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933 if (argcount < co->co_argcount) {
3934 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003935 int missing = 0;
3936 for (i = argcount; i < m; i++)
3937 if (GETLOCAL(i) == NULL)
3938 missing++;
3939 if (missing) {
3940 missing_arguments(co, missing, defcount, fastlocals);
3941 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 }
3943 if (n > m)
3944 i = n - m;
3945 else
3946 i = 0;
3947 for (; i < defcount; i++) {
3948 if (GETLOCAL(m+i) == NULL) {
3949 PyObject *def = defs[i];
3950 Py_INCREF(def);
3951 SETLOCAL(m+i, def);
3952 }
3953 }
3954 }
3955 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003956 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003957 for (i = co->co_argcount; i < total_args; i++) {
3958 PyObject *name;
3959 if (GETLOCAL(i) != NULL)
3960 continue;
3961 name = PyTuple_GET_ITEM(co->co_varnames, i);
3962 if (kwdefs != NULL) {
3963 PyObject *def = PyDict_GetItem(kwdefs, name);
3964 if (def) {
3965 Py_INCREF(def);
3966 SETLOCAL(i, def);
3967 continue;
3968 }
3969 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003970 missing++;
3971 }
3972 if (missing) {
3973 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 goto fail;
3975 }
3976 }
3977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003979 vars into frame. */
3980 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003982 int arg;
3983 /* Possibly account for the cell variable being an argument. */
3984 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003985 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003986 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003987 /* Clear the local copy. */
3988 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003989 }
3990 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003991 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003992 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003993 if (c == NULL)
3994 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003995 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003997 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3998 PyObject *o = PyTuple_GET_ITEM(closure, i);
3999 Py_INCREF(o);
4000 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004002
Yury Selivanov5376ba92015-06-22 12:19:30 -04004003 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004004 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004005 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004006 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004007
4008 if (is_coro && tstate->in_coroutine_wrapper) {
4009 assert(coro_wrapper != NULL);
4010 PyErr_Format(PyExc_RuntimeError,
4011 "coroutine wrapper %.200R attempted "
4012 "to recursively wrap %.200R",
4013 coro_wrapper,
4014 co);
4015 goto fail;
4016 }
Yury Selivanov75445082015-05-11 22:57:16 -04004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 /* Don't need to keep the reference to f_back, it will be set
4019 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004020 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 /* Create a new generator that owns the ready to run frame
4025 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004026 if (is_coro) {
4027 gen = PyCoro_New(f, name, qualname);
4028 } else {
4029 gen = PyGen_NewWithQualName(f, name, qualname);
4030 }
Yury Selivanov75445082015-05-11 22:57:16 -04004031 if (gen == NULL)
4032 return NULL;
4033
Yury Selivanov94c22632015-06-04 10:16:51 -04004034 if (is_coro && coro_wrapper != NULL) {
4035 PyObject *wrapped;
4036 tstate->in_coroutine_wrapper = 1;
4037 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4038 tstate->in_coroutine_wrapper = 0;
4039 return wrapped;
4040 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004041
Yury Selivanov75445082015-05-11 22:57:16 -04004042 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004046
Thomas Woutersce272b62007-09-19 21:19:28 +00004047fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 /* decref'ing the frame can cause __del__ methods to get invoked,
4050 which can call back into Python. While we're done with the
4051 current Python frame (f), the associated C stack is still in use,
4052 so recursion_depth must be boosted for the duration.
4053 */
4054 assert(tstate != NULL);
4055 ++tstate->recursion_depth;
4056 Py_DECREF(f);
4057 --tstate->recursion_depth;
4058 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004059}
4060
Victor Stinner40ee3012014-06-16 15:59:28 +02004061PyObject *
4062PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4063 PyObject **args, int argcount, PyObject **kws, int kwcount,
4064 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4065{
4066 return _PyEval_EvalCodeWithName(_co, globals, locals,
4067 args, argcount, kws, kwcount,
4068 defs, defcount, kwdefs, closure,
4069 NULL, NULL);
4070}
Tim Peters5ca576e2001-06-18 22:08:13 +00004071
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004072static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004073special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004076 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004078 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 return NULL;
4080 }
4081 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004082}
4083
4084
Benjamin Peterson87880242011-07-03 16:48:31 -05004085/* These 3 functions deal with the exception state of generators. */
4086
4087static void
4088save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4089{
4090 PyObject *type, *value, *traceback;
4091 Py_XINCREF(tstate->exc_type);
4092 Py_XINCREF(tstate->exc_value);
4093 Py_XINCREF(tstate->exc_traceback);
4094 type = f->f_exc_type;
4095 value = f->f_exc_value;
4096 traceback = f->f_exc_traceback;
4097 f->f_exc_type = tstate->exc_type;
4098 f->f_exc_value = tstate->exc_value;
4099 f->f_exc_traceback = tstate->exc_traceback;
4100 Py_XDECREF(type);
4101 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004102 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004103}
4104
4105static void
4106swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4107{
4108 PyObject *tmp;
4109 tmp = tstate->exc_type;
4110 tstate->exc_type = f->f_exc_type;
4111 f->f_exc_type = tmp;
4112 tmp = tstate->exc_value;
4113 tstate->exc_value = f->f_exc_value;
4114 f->f_exc_value = tmp;
4115 tmp = tstate->exc_traceback;
4116 tstate->exc_traceback = f->f_exc_traceback;
4117 f->f_exc_traceback = tmp;
4118}
4119
4120static void
4121restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4122{
4123 PyObject *type, *value, *tb;
4124 type = tstate->exc_type;
4125 value = tstate->exc_value;
4126 tb = tstate->exc_traceback;
4127 tstate->exc_type = f->f_exc_type;
4128 tstate->exc_value = f->f_exc_value;
4129 tstate->exc_traceback = f->f_exc_traceback;
4130 f->f_exc_type = NULL;
4131 f->f_exc_value = NULL;
4132 f->f_exc_traceback = NULL;
4133 Py_XDECREF(type);
4134 Py_XDECREF(value);
4135 Py_XDECREF(tb);
4136}
4137
4138
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004139/* Logic for the raise statement (too complicated for inlining).
4140 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004141static int
Collin Winter828f04a2007-08-31 00:04:24 +00004142do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 if (exc == NULL) {
4147 /* Reraise */
4148 PyThreadState *tstate = PyThreadState_GET();
4149 PyObject *tb;
4150 type = tstate->exc_type;
4151 value = tstate->exc_value;
4152 tb = tstate->exc_traceback;
4153 if (type == Py_None) {
4154 PyErr_SetString(PyExc_RuntimeError,
4155 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004156 return 0;
4157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 Py_XINCREF(type);
4159 Py_XINCREF(value);
4160 Py_XINCREF(tb);
4161 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004162 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 /* We support the following forms of raise:
4166 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004167 raise <instance>
4168 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 if (PyExceptionClass_Check(exc)) {
4171 type = exc;
4172 value = PyObject_CallObject(exc, NULL);
4173 if (value == NULL)
4174 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004175 if (!PyExceptionInstance_Check(value)) {
4176 PyErr_Format(PyExc_TypeError,
4177 "calling %R should have returned an instance of "
4178 "BaseException, not %R",
4179 type, Py_TYPE(value));
4180 goto raise_error;
4181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 }
4183 else if (PyExceptionInstance_Check(exc)) {
4184 value = exc;
4185 type = PyExceptionInstance_Class(exc);
4186 Py_INCREF(type);
4187 }
4188 else {
4189 /* Not something you can raise. You get an exception
4190 anyway, just not what you specified :-) */
4191 Py_DECREF(exc);
4192 PyErr_SetString(PyExc_TypeError,
4193 "exceptions must derive from BaseException");
4194 goto raise_error;
4195 }
Collin Winter828f04a2007-08-31 00:04:24 +00004196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 if (cause) {
4198 PyObject *fixed_cause;
4199 if (PyExceptionClass_Check(cause)) {
4200 fixed_cause = PyObject_CallObject(cause, NULL);
4201 if (fixed_cause == NULL)
4202 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004203 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004205 else if (PyExceptionInstance_Check(cause)) {
4206 fixed_cause = cause;
4207 }
4208 else if (cause == Py_None) {
4209 Py_DECREF(cause);
4210 fixed_cause = NULL;
4211 }
4212 else {
4213 PyErr_SetString(PyExc_TypeError,
4214 "exception causes must derive from "
4215 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 goto raise_error;
4217 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004218 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 }
Collin Winter828f04a2007-08-31 00:04:24 +00004220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 PyErr_SetObject(type, value);
4222 /* PyErr_SetObject incref's its arguments */
4223 Py_XDECREF(value);
4224 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004225 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004226
4227raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 Py_XDECREF(value);
4229 Py_XDECREF(type);
4230 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004231 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004232}
4233
Tim Petersd6d010b2001-06-21 02:49:55 +00004234/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004235 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004236
Guido van Rossum0368b722007-05-11 16:50:42 +00004237 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4238 with a variable target.
4239*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004240
Barry Warsawe42b18f1997-08-25 22:13:04 +00004241static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004242unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 int i = 0, j = 0;
4245 Py_ssize_t ll = 0;
4246 PyObject *it; /* iter(v) */
4247 PyObject *w;
4248 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 it = PyObject_GetIter(v);
4253 if (it == NULL)
4254 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 for (; i < argcnt; i++) {
4257 w = PyIter_Next(it);
4258 if (w == NULL) {
4259 /* Iterator done, via error or exhaustion. */
4260 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004261 if (argcntafter == -1) {
4262 PyErr_Format(PyExc_ValueError,
4263 "not enough values to unpack (expected %d, got %d)",
4264 argcnt, i);
4265 }
4266 else {
4267 PyErr_Format(PyExc_ValueError,
4268 "not enough values to unpack "
4269 "(expected at least %d, got %d)",
4270 argcnt + argcntafter, i);
4271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 }
4273 goto Error;
4274 }
4275 *--sp = w;
4276 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 if (argcntafter == -1) {
4279 /* We better have exhausted the iterator now. */
4280 w = PyIter_Next(it);
4281 if (w == NULL) {
4282 if (PyErr_Occurred())
4283 goto Error;
4284 Py_DECREF(it);
4285 return 1;
4286 }
4287 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004288 PyErr_Format(PyExc_ValueError,
4289 "too many values to unpack (expected %d)",
4290 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 goto Error;
4292 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 l = PySequence_List(it);
4295 if (l == NULL)
4296 goto Error;
4297 *--sp = l;
4298 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 ll = PyList_GET_SIZE(l);
4301 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004302 PyErr_Format(PyExc_ValueError,
4303 "not enough values to unpack (expected at least %d, got %zd)",
4304 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 goto Error;
4306 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 /* Pop the "after-variable" args off the list. */
4309 for (j = argcntafter; j > 0; j--, i++) {
4310 *--sp = PyList_GET_ITEM(l, ll - j);
4311 }
4312 /* Resize the list. */
4313 Py_SIZE(l) = ll - argcntafter;
4314 Py_DECREF(it);
4315 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004316
Tim Petersd6d010b2001-06-21 02:49:55 +00004317Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 for (; i > 0; i--, sp++)
4319 Py_DECREF(*sp);
4320 Py_XDECREF(it);
4321 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004322}
4323
4324
Guido van Rossum96a42c81992-01-12 02:29:51 +00004325#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004326static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004327prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 printf("%s ", str);
4330 if (PyObject_Print(v, stdout, 0) != 0)
4331 PyErr_Clear(); /* Don't know what else to do */
4332 printf("\n");
4333 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004334}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004335#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004336
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004337static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004338call_exc_trace(Py_tracefunc func, PyObject *self,
4339 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004340{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004341 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004343 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 if (value == NULL) {
4345 value = Py_None;
4346 Py_INCREF(value);
4347 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004348 PyErr_NormalizeException(&type, &value, &orig_traceback);
4349 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 arg = PyTuple_Pack(3, type, value, traceback);
4351 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004352 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 return;
4354 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004355 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 Py_DECREF(arg);
4357 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004358 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 else {
4360 Py_XDECREF(type);
4361 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004362 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004364}
4365
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004366static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004367call_trace_protected(Py_tracefunc func, PyObject *obj,
4368 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 PyObject *type, *value, *traceback;
4372 int err;
4373 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004374 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 if (err == 0)
4376 {
4377 PyErr_Restore(type, value, traceback);
4378 return 0;
4379 }
4380 else {
4381 Py_XDECREF(type);
4382 Py_XDECREF(value);
4383 Py_XDECREF(traceback);
4384 return -1;
4385 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004386}
4387
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004388static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004389call_trace(Py_tracefunc func, PyObject *obj,
4390 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 int result;
4394 if (tstate->tracing)
4395 return 0;
4396 tstate->tracing++;
4397 tstate->use_tracing = 0;
4398 result = func(obj, frame, what, arg);
4399 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4400 || (tstate->c_profilefunc != NULL));
4401 tstate->tracing--;
4402 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004403}
4404
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004405PyObject *
4406_PyEval_CallTracing(PyObject *func, PyObject *args)
4407{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004408 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 int save_tracing = tstate->tracing;
4410 int save_use_tracing = tstate->use_tracing;
4411 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 tstate->tracing = 0;
4414 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4415 || (tstate->c_profilefunc != NULL));
4416 result = PyObject_Call(func, args, NULL);
4417 tstate->tracing = save_tracing;
4418 tstate->use_tracing = save_use_tracing;
4419 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004420}
4421
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004422/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004423static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004424maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004425 PyThreadState *tstate, PyFrameObject *frame,
4426 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 int result = 0;
4429 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 /* If the last instruction executed isn't in the current
4432 instruction window, reset the window.
4433 */
4434 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4435 PyAddrPair bounds;
4436 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4437 &bounds);
4438 *instr_lb = bounds.ap_lower;
4439 *instr_ub = bounds.ap_upper;
4440 }
4441 /* If the last instruction falls at the start of a line or if
4442 it represents a jump backwards, update the frame's line
4443 number and call the trace function. */
4444 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4445 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004446 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 }
4448 *instr_prev = frame->f_lasti;
4449 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004450}
4451
Fred Drake5755ce62001-06-27 19:19:46 +00004452void
4453PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 PyThreadState *tstate = PyThreadState_GET();
4456 PyObject *temp = tstate->c_profileobj;
4457 Py_XINCREF(arg);
4458 tstate->c_profilefunc = NULL;
4459 tstate->c_profileobj = NULL;
4460 /* Must make sure that tracing is not ignored if 'temp' is freed */
4461 tstate->use_tracing = tstate->c_tracefunc != NULL;
4462 Py_XDECREF(temp);
4463 tstate->c_profilefunc = func;
4464 tstate->c_profileobj = arg;
4465 /* Flag that tracing or profiling is turned on */
4466 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004467}
4468
4469void
4470PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 PyThreadState *tstate = PyThreadState_GET();
4473 PyObject *temp = tstate->c_traceobj;
4474 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4475 Py_XINCREF(arg);
4476 tstate->c_tracefunc = NULL;
4477 tstate->c_traceobj = NULL;
4478 /* Must make sure that profiling is not ignored if 'temp' is freed */
4479 tstate->use_tracing = tstate->c_profilefunc != NULL;
4480 Py_XDECREF(temp);
4481 tstate->c_tracefunc = func;
4482 tstate->c_traceobj = arg;
4483 /* Flag that tracing or profiling is turned on */
4484 tstate->use_tracing = ((func != NULL)
4485 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004486}
4487
Yury Selivanov75445082015-05-11 22:57:16 -04004488void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004489_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004490{
4491 PyThreadState *tstate = PyThreadState_GET();
4492
Yury Selivanov75445082015-05-11 22:57:16 -04004493 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004494 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004495}
4496
4497PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004498_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004499{
4500 PyThreadState *tstate = PyThreadState_GET();
4501 return tstate->coroutine_wrapper;
4502}
4503
Guido van Rossumb209a111997-04-29 18:18:01 +00004504PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004505PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 PyFrameObject *current_frame = PyEval_GetFrame();
4508 if (current_frame == NULL)
4509 return PyThreadState_GET()->interp->builtins;
4510 else
4511 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004512}
4513
Guido van Rossumb209a111997-04-29 18:18:01 +00004514PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004515PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004518 if (current_frame == NULL) {
4519 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004521 }
4522
4523 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4524 return NULL;
4525
4526 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004528}
4529
Guido van Rossumb209a111997-04-29 18:18:01 +00004530PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004531PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 PyFrameObject *current_frame = PyEval_GetFrame();
4534 if (current_frame == NULL)
4535 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004536
4537 assert(current_frame->f_globals != NULL);
4538 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004539}
4540
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004541PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004542PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 PyThreadState *tstate = PyThreadState_GET();
4545 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004546}
4547
Guido van Rossum6135a871995-01-09 17:53:26 +00004548int
Tim Peters5ba58662001-07-16 02:29:45 +00004549PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 PyFrameObject *current_frame = PyEval_GetFrame();
4552 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 if (current_frame != NULL) {
4555 const int codeflags = current_frame->f_code->co_flags;
4556 const int compilerflags = codeflags & PyCF_MASK;
4557 if (compilerflags) {
4558 result = 1;
4559 cf->cf_flags |= compilerflags;
4560 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004561#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 if (codeflags & CO_GENERATOR_ALLOWED) {
4563 result = 1;
4564 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4565 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004566#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 }
4568 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004569}
4570
Guido van Rossum3f5da241990-12-20 15:06:42 +00004571
Guido van Rossum681d79a1995-07-18 14:51:37 +00004572/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004573 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004574
Guido van Rossumb209a111997-04-29 18:18:01 +00004575PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004576PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004579
Victor Stinner59b356d2015-03-16 11:52:32 +01004580#ifdef Py_DEBUG
4581 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4582 set. It raises a new exception if parameters are invalid or if
4583 PyTuple_New() fails, and so the original exception is lost. */
4584 assert(!PyErr_Occurred());
4585#endif
4586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 if (arg == NULL) {
4588 arg = PyTuple_New(0);
4589 if (arg == NULL)
4590 return NULL;
4591 }
4592 else if (!PyTuple_Check(arg)) {
4593 PyErr_SetString(PyExc_TypeError,
4594 "argument list must be a tuple");
4595 return NULL;
4596 }
4597 else
4598 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 if (kw != NULL && !PyDict_Check(kw)) {
4601 PyErr_SetString(PyExc_TypeError,
4602 "keyword list must be a dictionary");
4603 Py_DECREF(arg);
4604 return NULL;
4605 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 result = PyObject_Call(func, arg, kw);
4608 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004611}
4612
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004613const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004614PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 if (PyMethod_Check(func))
4617 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4618 else if (PyFunction_Check(func))
4619 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4620 else if (PyCFunction_Check(func))
4621 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4622 else
4623 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004624}
4625
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004626const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004627PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 if (PyMethod_Check(func))
4630 return "()";
4631 else if (PyFunction_Check(func))
4632 return "()";
4633 else if (PyCFunction_Check(func))
4634 return "()";
4635 else
4636 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004637}
4638
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004639static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004640err_args(PyObject *func, int flags, int nargs)
4641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 if (flags & METH_NOARGS)
4643 PyErr_Format(PyExc_TypeError,
4644 "%.200s() takes no arguments (%d given)",
4645 ((PyCFunctionObject *)func)->m_ml->ml_name,
4646 nargs);
4647 else
4648 PyErr_Format(PyExc_TypeError,
4649 "%.200s() takes exactly one argument (%d given)",
4650 ((PyCFunctionObject *)func)->m_ml->ml_name,
4651 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004652}
4653
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004654#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004655if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004656 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4657 tstate, tstate->frame, \
4658 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 x = NULL; \
4660 } \
4661 else { \
4662 x = call; \
4663 if (tstate->c_profilefunc != NULL) { \
4664 if (x == NULL) { \
4665 call_trace_protected(tstate->c_profilefunc, \
4666 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004667 tstate, tstate->frame, \
4668 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 /* XXX should pass (type, value, tb) */ \
4670 } else { \
4671 if (call_trace(tstate->c_profilefunc, \
4672 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004673 tstate, tstate->frame, \
4674 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 Py_DECREF(x); \
4676 x = NULL; \
4677 } \
4678 } \
4679 } \
4680 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004681} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 x = call; \
4683 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004684
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004685static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004686call_function(PyObject ***pp_stack, int oparg
4687#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004689#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 int na = oparg & 0xff;
4693 int nk = (oparg>>8) & 0xff;
4694 int n = na + 2 * nk;
4695 PyObject **pfunc = (*pp_stack) - n - 1;
4696 PyObject *func = *pfunc;
4697 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 /* Always dispatch PyCFunction first, because these are
4700 presumed to be the most frequent callable object.
4701 */
4702 if (PyCFunction_Check(func) && nk == 0) {
4703 int flags = PyCFunction_GET_FLAGS(func);
4704 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 PCALL(PCALL_CFUNCTION);
4707 if (flags & (METH_NOARGS | METH_O)) {
4708 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4709 PyObject *self = PyCFunction_GET_SELF(func);
4710 if (flags & METH_NOARGS && na == 0) {
4711 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004712
Victor Stinnerefde1462015-03-21 15:04:43 +01004713 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 }
4715 else if (flags & METH_O && na == 1) {
4716 PyObject *arg = EXT_POP(*pp_stack);
4717 C_TRACE(x, (*meth)(self,arg));
4718 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004719
Victor Stinnerefde1462015-03-21 15:04:43 +01004720 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 }
4722 else {
4723 err_args(func, flags, na);
4724 x = NULL;
4725 }
4726 }
4727 else {
4728 PyObject *callargs;
4729 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004730 if (callargs != NULL) {
4731 READ_TIMESTAMP(*pintr0);
4732 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4733 READ_TIMESTAMP(*pintr1);
4734 Py_XDECREF(callargs);
4735 }
4736 else {
4737 x = NULL;
4738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004740 }
4741 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4743 /* optimize access to bound methods */
4744 PyObject *self = PyMethod_GET_SELF(func);
4745 PCALL(PCALL_METHOD);
4746 PCALL(PCALL_BOUND_METHOD);
4747 Py_INCREF(self);
4748 func = PyMethod_GET_FUNCTION(func);
4749 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03004750 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 na++;
4752 n++;
4753 } else
4754 Py_INCREF(func);
4755 READ_TIMESTAMP(*pintr0);
4756 if (PyFunction_Check(func))
4757 x = fast_function(func, pp_stack, n, na, nk);
4758 else
4759 x = do_call(func, pp_stack, na, nk);
4760 READ_TIMESTAMP(*pintr1);
4761 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004762
4763 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 /* Clear the stack of the function object. Also removes
4767 the arguments in case they weren't consumed already
4768 (fast_function() and err_args() leave them on the stack).
4769 */
4770 while ((*pp_stack) > pfunc) {
4771 w = EXT_POP(*pp_stack);
4772 Py_DECREF(w);
4773 PCALL(PCALL_POP);
4774 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004775
Victor Stinner4a7cc882015-03-06 23:35:27 +01004776 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004778}
4779
Jeremy Hylton192690e2002-08-16 18:36:11 +00004780/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004781 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004782 For the simplest case -- a function that takes only positional
4783 arguments and is called with only positional arguments -- it
4784 inlines the most primitive frame setup code from
4785 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4786 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004787*/
4788
4789static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004790fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4793 PyObject *globals = PyFunction_GET_GLOBALS(func);
4794 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4795 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Victor Stinner40ee3012014-06-16 15:59:28 +02004796 PyObject *name = ((PyFunctionObject *)func) -> func_name;
4797 PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 PyObject **d = NULL;
4799 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 PCALL(PCALL_FUNCTION);
4802 PCALL(PCALL_FAST_FUNCTION);
4803 if (argdefs == NULL && co->co_argcount == n &&
4804 co->co_kwonlyargcount == 0 && nk==0 &&
4805 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4806 PyFrameObject *f;
4807 PyObject *retval = NULL;
4808 PyThreadState *tstate = PyThreadState_GET();
4809 PyObject **fastlocals, **stack;
4810 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 PCALL(PCALL_FASTER_FUNCTION);
4813 assert(globals != NULL);
4814 /* XXX Perhaps we should create a specialized
4815 PyFrame_New() that doesn't take locals, but does
4816 take builtins without sanity checking them.
4817 */
4818 assert(tstate != NULL);
4819 f = PyFrame_New(tstate, co, globals, NULL);
4820 if (f == NULL)
4821 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 fastlocals = f->f_localsplus;
4824 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 for (i = 0; i < n; i++) {
4827 Py_INCREF(*stack);
4828 fastlocals[i] = *stack++;
4829 }
4830 retval = PyEval_EvalFrameEx(f,0);
4831 ++tstate->recursion_depth;
4832 Py_DECREF(f);
4833 --tstate->recursion_depth;
4834 return retval;
4835 }
4836 if (argdefs != NULL) {
4837 d = &PyTuple_GET_ITEM(argdefs, 0);
4838 nd = Py_SIZE(argdefs);
4839 }
Victor Stinner40ee3012014-06-16 15:59:28 +02004840 return _PyEval_EvalCodeWithName((PyObject*)co, globals,
4841 (PyObject *)NULL, (*pp_stack)-n, na,
4842 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4843 PyFunction_GET_CLOSURE(func),
4844 name, qualname);
Jeremy Hylton52820442001-01-03 23:52:36 +00004845}
4846
4847static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004848update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4849 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 PyObject *kwdict = NULL;
4852 if (orig_kwdict == NULL)
4853 kwdict = PyDict_New();
4854 else {
4855 kwdict = PyDict_Copy(orig_kwdict);
4856 Py_DECREF(orig_kwdict);
4857 }
4858 if (kwdict == NULL)
4859 return NULL;
4860 while (--nk >= 0) {
4861 int err;
4862 PyObject *value = EXT_POP(*pp_stack);
4863 PyObject *key = EXT_POP(*pp_stack);
4864 if (PyDict_GetItem(kwdict, key) != NULL) {
4865 PyErr_Format(PyExc_TypeError,
4866 "%.200s%s got multiple values "
4867 "for keyword argument '%U'",
4868 PyEval_GetFuncName(func),
4869 PyEval_GetFuncDesc(func),
4870 key);
4871 Py_DECREF(key);
4872 Py_DECREF(value);
4873 Py_DECREF(kwdict);
4874 return NULL;
4875 }
4876 err = PyDict_SetItem(kwdict, key, value);
4877 Py_DECREF(key);
4878 Py_DECREF(value);
4879 if (err) {
4880 Py_DECREF(kwdict);
4881 return NULL;
4882 }
4883 }
4884 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004885}
4886
4887static PyObject *
4888update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004892
Serhiy Storchaka79d6e8d2016-04-19 23:37:17 +03004893 if (!nstack) {
4894 if (!stararg) {
4895 /* There are no positional arguments on the stack and there is no
4896 sequence to be unpacked. */
4897 return PyTuple_New(0);
4898 }
4899 if (PyTuple_CheckExact(stararg)) {
4900 /* No arguments are passed on the stack and the sequence is not a
4901 tuple subclass so we can just pass the stararg tuple directly
4902 to the function. */
4903 Py_INCREF(stararg);
4904 return stararg;
4905 }
4906 }
4907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 callargs = PyTuple_New(nstack + nstar);
4909 if (callargs == NULL) {
4910 return NULL;
4911 }
4912 if (nstar) {
4913 int i;
4914 for (i = 0; i < nstar; i++) {
4915 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4916 Py_INCREF(a);
4917 PyTuple_SET_ITEM(callargs, nstack + i, a);
4918 }
4919 }
4920 while (--nstack >= 0) {
4921 w = EXT_POP(*pp_stack);
4922 PyTuple_SET_ITEM(callargs, nstack, w);
4923 }
4924 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004925}
4926
4927static PyObject *
4928load_args(PyObject ***pp_stack, int na)
4929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 PyObject *args = PyTuple_New(na);
4931 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004933 if (args == NULL)
4934 return NULL;
4935 while (--na >= 0) {
4936 w = EXT_POP(*pp_stack);
4937 PyTuple_SET_ITEM(args, na, w);
4938 }
4939 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004940}
4941
4942static PyObject *
4943do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 PyObject *callargs = NULL;
4946 PyObject *kwdict = NULL;
4947 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 if (nk > 0) {
4950 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4951 if (kwdict == NULL)
4952 goto call_fail;
4953 }
4954 callargs = load_args(pp_stack, na);
4955 if (callargs == NULL)
4956 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004957#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 /* At this point, we have to look at the type of func to
4959 update the call stats properly. Do it here so as to avoid
4960 exposing the call stats machinery outside ceval.c
4961 */
4962 if (PyFunction_Check(func))
4963 PCALL(PCALL_FUNCTION);
4964 else if (PyMethod_Check(func))
4965 PCALL(PCALL_METHOD);
4966 else if (PyType_Check(func))
4967 PCALL(PCALL_TYPE);
4968 else if (PyCFunction_Check(func))
4969 PCALL(PCALL_CFUNCTION);
4970 else
4971 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004972#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 if (PyCFunction_Check(func)) {
4974 PyThreadState *tstate = PyThreadState_GET();
4975 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4976 }
4977 else
4978 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004979call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004980 Py_XDECREF(callargs);
4981 Py_XDECREF(kwdict);
4982 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004983}
4984
4985static PyObject *
4986ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004988 int nstar = 0;
4989 PyObject *callargs = NULL;
4990 PyObject *stararg = NULL;
4991 PyObject *kwdict = NULL;
4992 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004994 if (flags & CALL_FLAG_KW) {
4995 kwdict = EXT_POP(*pp_stack);
Serhiy Storchakace412872016-05-08 23:36:44 +03004996 if (!PyDict_CheckExact(kwdict)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 PyObject *d;
4998 d = PyDict_New();
4999 if (d == NULL)
5000 goto ext_call_fail;
5001 if (PyDict_Update(d, kwdict) != 0) {
5002 Py_DECREF(d);
5003 /* PyDict_Update raises attribute
5004 * error (percolated from an attempt
5005 * to get 'keys' attribute) instead of
5006 * a type error if its second argument
5007 * is not a mapping.
5008 */
5009 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5010 PyErr_Format(PyExc_TypeError,
5011 "%.200s%.200s argument after ** "
5012 "must be a mapping, not %.200s",
5013 PyEval_GetFuncName(func),
5014 PyEval_GetFuncDesc(func),
5015 kwdict->ob_type->tp_name);
5016 }
5017 goto ext_call_fail;
5018 }
5019 Py_DECREF(kwdict);
5020 kwdict = d;
5021 }
5022 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005023 if (nk > 0) {
5024 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
5025 if (kwdict == NULL)
5026 goto ext_call_fail;
5027 }
5028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 if (flags & CALL_FLAG_VAR) {
5030 stararg = EXT_POP(*pp_stack);
5031 if (!PyTuple_Check(stararg)) {
5032 PyObject *t = NULL;
Martin Panterb5944222016-01-31 06:30:56 +00005033 if (Py_TYPE(stararg)->tp_iter == NULL &&
5034 !PySequence_Check(stararg)) {
5035 PyErr_Format(PyExc_TypeError,
5036 "%.200s%.200s argument after * "
5037 "must be an iterable, not %.200s",
5038 PyEval_GetFuncName(func),
5039 PyEval_GetFuncDesc(func),
5040 stararg->ob_type->tp_name);
5041 goto ext_call_fail;
5042 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 t = PySequence_Tuple(stararg);
5044 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005045 goto ext_call_fail;
5046 }
5047 Py_DECREF(stararg);
5048 stararg = t;
5049 }
5050 nstar = PyTuple_GET_SIZE(stararg);
5051 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005052 callargs = update_star_args(na, nstar, stararg, pp_stack);
5053 if (callargs == NULL)
5054 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005055#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 /* At this point, we have to look at the type of func to
5057 update the call stats properly. Do it here so as to avoid
5058 exposing the call stats machinery outside ceval.c
5059 */
5060 if (PyFunction_Check(func))
5061 PCALL(PCALL_FUNCTION);
5062 else if (PyMethod_Check(func))
5063 PCALL(PCALL_METHOD);
5064 else if (PyType_Check(func))
5065 PCALL(PCALL_TYPE);
5066 else if (PyCFunction_Check(func))
5067 PCALL(PCALL_CFUNCTION);
5068 else
5069 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005071 if (PyCFunction_Check(func)) {
5072 PyThreadState *tstate = PyThreadState_GET();
5073 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5074 }
5075 else
5076 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00005077ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 Py_XDECREF(callargs);
5079 Py_XDECREF(kwdict);
5080 Py_XDECREF(stararg);
5081 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005082}
5083
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005084/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005085 nb_index slot defined, and store in *pi.
5086 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5087 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 +00005088 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005089*/
Tim Petersb5196382001-12-16 19:44:20 +00005090/* Note: If v is NULL, return success without storing into *pi. This
5091 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5092 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005093*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005094int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005095_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005097 if (v != NULL) {
5098 Py_ssize_t x;
5099 if (PyIndex_Check(v)) {
5100 x = PyNumber_AsSsize_t(v, NULL);
5101 if (x == -1 && PyErr_Occurred())
5102 return 0;
5103 }
5104 else {
5105 PyErr_SetString(PyExc_TypeError,
5106 "slice indices must be integers or "
5107 "None or have an __index__ method");
5108 return 0;
5109 }
5110 *pi = x;
5111 }
5112 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005113}
5114
Guido van Rossum486364b2007-06-30 05:01:58 +00005115#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005116 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005117
Guido van Rossumb209a111997-04-29 18:18:01 +00005118static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005119cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005121 int res = 0;
5122 switch (op) {
5123 case PyCmp_IS:
5124 res = (v == w);
5125 break;
5126 case PyCmp_IS_NOT:
5127 res = (v != w);
5128 break;
5129 case PyCmp_IN:
5130 res = PySequence_Contains(w, v);
5131 if (res < 0)
5132 return NULL;
5133 break;
5134 case PyCmp_NOT_IN:
5135 res = PySequence_Contains(w, v);
5136 if (res < 0)
5137 return NULL;
5138 res = !res;
5139 break;
5140 case PyCmp_EXC_MATCH:
5141 if (PyTuple_Check(w)) {
5142 Py_ssize_t i, length;
5143 length = PyTuple_Size(w);
5144 for (i = 0; i < length; i += 1) {
5145 PyObject *exc = PyTuple_GET_ITEM(w, i);
5146 if (!PyExceptionClass_Check(exc)) {
5147 PyErr_SetString(PyExc_TypeError,
5148 CANNOT_CATCH_MSG);
5149 return NULL;
5150 }
5151 }
5152 }
5153 else {
5154 if (!PyExceptionClass_Check(w)) {
5155 PyErr_SetString(PyExc_TypeError,
5156 CANNOT_CATCH_MSG);
5157 return NULL;
5158 }
5159 }
5160 res = PyErr_GivenExceptionMatches(v, w);
5161 break;
5162 default:
5163 return PyObject_RichCompare(v, w, op);
5164 }
5165 v = res ? Py_True : Py_False;
5166 Py_INCREF(v);
5167 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005168}
5169
Thomas Wouters52152252000-08-17 22:55:00 +00005170static PyObject *
5171import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005172{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005173 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005174 _Py_IDENTIFIER(__name__);
5175 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005177 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005178 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5179 return x;
5180 /* Issue #17636: in case this failed because of a circular relative
5181 import, try to fallback on reading the module directly from
5182 sys.modules. */
5183 PyErr_Clear();
5184 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005185 if (pkgname == NULL) {
5186 goto error;
5187 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005188 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5189 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005190 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005191 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005192 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005193 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005194 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005195 if (x == NULL) {
5196 goto error;
5197 }
5198 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005199 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005200 error:
5201 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5202 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005203}
Guido van Rossumac7be682001-01-17 15:42:30 +00005204
Thomas Wouters52152252000-08-17 22:55:00 +00005205static int
5206import_all_from(PyObject *locals, PyObject *v)
5207{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005208 _Py_IDENTIFIER(__all__);
5209 _Py_IDENTIFIER(__dict__);
5210 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005211 PyObject *dict, *name, *value;
5212 int skip_leading_underscores = 0;
5213 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 if (all == NULL) {
5216 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5217 return -1; /* Unexpected error */
5218 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005219 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005220 if (dict == NULL) {
5221 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5222 return -1;
5223 PyErr_SetString(PyExc_ImportError,
5224 "from-import-* object has no __dict__ and no __all__");
5225 return -1;
5226 }
5227 all = PyMapping_Keys(dict);
5228 Py_DECREF(dict);
5229 if (all == NULL)
5230 return -1;
5231 skip_leading_underscores = 1;
5232 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 for (pos = 0, err = 0; ; pos++) {
5235 name = PySequence_GetItem(all, pos);
5236 if (name == NULL) {
5237 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5238 err = -1;
5239 else
5240 PyErr_Clear();
5241 break;
5242 }
5243 if (skip_leading_underscores &&
5244 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005245 PyUnicode_READY(name) != -1 &&
5246 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005247 {
5248 Py_DECREF(name);
5249 continue;
5250 }
5251 value = PyObject_GetAttr(v, name);
5252 if (value == NULL)
5253 err = -1;
5254 else if (PyDict_CheckExact(locals))
5255 err = PyDict_SetItem(locals, name, value);
5256 else
5257 err = PyObject_SetItem(locals, name, value);
5258 Py_DECREF(name);
5259 Py_XDECREF(value);
5260 if (err != 0)
5261 break;
5262 }
5263 Py_DECREF(all);
5264 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005265}
5266
Guido van Rossumac7be682001-01-17 15:42:30 +00005267static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005268format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005272 if (!obj)
5273 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005275 obj_str = _PyUnicode_AsString(obj);
5276 if (!obj_str)
5277 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005280}
Guido van Rossum950361c1997-01-24 13:49:28 +00005281
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005282static void
5283format_exc_unbound(PyCodeObject *co, int oparg)
5284{
5285 PyObject *name;
5286 /* Don't stomp existing exception */
5287 if (PyErr_Occurred())
5288 return;
5289 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5290 name = PyTuple_GET_ITEM(co->co_cellvars,
5291 oparg);
5292 format_exc_check_arg(
5293 PyExc_UnboundLocalError,
5294 UNBOUNDLOCAL_ERROR_MSG,
5295 name);
5296 } else {
5297 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5298 PyTuple_GET_SIZE(co->co_cellvars));
5299 format_exc_check_arg(PyExc_NameError,
5300 UNBOUNDFREE_ERROR_MSG, name);
5301 }
5302}
5303
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005304static PyObject *
5305unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005306 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005307{
5308 PyObject *res;
5309 if (Py_REFCNT(v) == 2) {
5310 /* In the common case, there are 2 references to the value
5311 * stored in 'variable' when the += is performed: one on the
5312 * value stack (in 'v') and one still stored in the
5313 * 'variable'. We try to delete the variable now to reduce
5314 * the refcnt to 1.
5315 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005316 int opcode, oparg;
5317 NEXTOPARG();
5318 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005319 case STORE_FAST:
5320 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005321 PyObject **fastlocals = f->f_localsplus;
5322 if (GETLOCAL(oparg) == v)
5323 SETLOCAL(oparg, NULL);
5324 break;
5325 }
5326 case STORE_DEREF:
5327 {
5328 PyObject **freevars = (f->f_localsplus +
5329 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005330 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005331 if (PyCell_GET(c) == v)
5332 PyCell_Set(c, NULL);
5333 break;
5334 }
5335 case STORE_NAME:
5336 {
5337 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005338 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005339 PyObject *locals = f->f_locals;
5340 if (PyDict_CheckExact(locals) &&
5341 PyDict_GetItem(locals, name) == v) {
5342 if (PyDict_DelItem(locals, name) != 0) {
5343 PyErr_Clear();
5344 }
5345 }
5346 break;
5347 }
5348 }
5349 }
5350 res = v;
5351 PyUnicode_Append(&res, w);
5352 return res;
5353}
5354
Guido van Rossum950361c1997-01-24 13:49:28 +00005355#ifdef DYNAMIC_EXECUTION_PROFILE
5356
Skip Montanarof118cb12001-10-15 20:51:38 +00005357static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005358getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005360 int i;
5361 PyObject *l = PyList_New(256);
5362 if (l == NULL) return NULL;
5363 for (i = 0; i < 256; i++) {
5364 PyObject *x = PyLong_FromLong(a[i]);
5365 if (x == NULL) {
5366 Py_DECREF(l);
5367 return NULL;
5368 }
5369 PyList_SetItem(l, i, x);
5370 }
5371 for (i = 0; i < 256; i++)
5372 a[i] = 0;
5373 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005374}
5375
5376PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005377_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005378{
5379#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005380 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005381#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 int i;
5383 PyObject *l = PyList_New(257);
5384 if (l == NULL) return NULL;
5385 for (i = 0; i < 257; i++) {
5386 PyObject *x = getarray(dxpairs[i]);
5387 if (x == NULL) {
5388 Py_DECREF(l);
5389 return NULL;
5390 }
5391 PyList_SetItem(l, i, x);
5392 }
5393 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005394#endif
5395}
5396
5397#endif