blob: 9f1af788aa15172b70062c28eb4a4f35107c7cf2 [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;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000128static int prtrace(PyObject *, 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 *,
147 PyFrameObject *, unsigned char *);
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 */
803 unsigned char *next_instr;
804 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 unsigned char *first_instr;
822 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
889/* This macro is used when several opcodes defer to the same implementation
890 (e.g. SETUP_LOOP, SETUP_FINALLY) */
891#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 TARGET_##op: \
893 opcode = op; \
894 if (HAS_ARG(op)) \
895 oparg = NEXTARG(); \
896 case op: \
897 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000898
899#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 TARGET_##op: \
901 opcode = op; \
902 if (HAS_ARG(op)) \
903 oparg = NEXTARG(); \
904 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000905
906
907#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 { \
909 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
910 FAST_DISPATCH(); \
911 } \
912 continue; \
913 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000914
915#ifdef LLTRACE
916#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 { \
918 if (!lltrace && !_Py_TracingPossible) { \
919 f->f_lasti = INSTR_OFFSET(); \
920 goto *opcode_targets[*next_instr++]; \
921 } \
922 goto fast_next_opcode; \
923 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000924#else
925#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 { \
927 if (!_Py_TracingPossible) { \
928 f->f_lasti = INSTR_OFFSET(); \
929 goto *opcode_targets[*next_instr++]; \
930 } \
931 goto fast_next_opcode; \
932 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000933#endif
934
935#else
936#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 /* silence compiler warnings about `impl` unused */ \
940 if (0) goto impl; \
941 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000942#define DISPATCH() continue
943#define FAST_DISPATCH() goto fast_next_opcode
944#endif
945
946
Neal Norwitza81d2202002-07-14 00:27:26 +0000947/* Tuple access macros */
948
949#ifndef Py_DEBUG
950#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
951#else
952#define GETITEM(v, i) PyTuple_GetItem((v), (i))
953#endif
954
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000955#ifdef WITH_TSC
956/* Use Pentium timestamp counter to mark certain events:
957 inst0 -- beginning of switch statement for opcode dispatch
958 inst1 -- end of switch statement (may be skipped)
959 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000960 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000961 (may be skipped)
962 intr1 -- beginning of long interruption
963 intr2 -- end of long interruption
964
965 Many opcodes call out to helper C functions. In some cases, the
966 time in those functions should be counted towards the time for the
967 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
968 calls another Python function; there's no point in charge all the
969 bytecode executed by the called function to the caller.
970
971 It's hard to make a useful judgement statically. In the presence
972 of operator overloading, it's impossible to tell if a call will
973 execute new Python code or not.
974
975 It's a case-by-case judgement. I'll use intr1 for the following
976 cases:
977
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978 IMPORT_STAR
979 IMPORT_FROM
980 CALL_FUNCTION (and friends)
981
982 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
984 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 READ_TIMESTAMP(inst0);
987 READ_TIMESTAMP(inst1);
988 READ_TIMESTAMP(loop0);
989 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 /* shut up the compiler */
992 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000993#endif
994
Guido van Rossum374a9221991-04-04 10:40:29 +0000995/* Code access macros */
996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997#define INSTR_OFFSET() ((int)(next_instr - first_instr))
998#define NEXTOP() (*next_instr++)
999#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
1000#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
1001#define JUMPTO(x) (next_instr = first_instr + (x))
1002#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001003
Raymond Hettingerf606f872003-03-16 03:11:04 +00001004/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Some opcodes tend to come in pairs thus making it possible to
1006 predict the second code when the first is run. For example,
1007 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1008 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 Verifying the prediction costs a single high-speed test of a register
1011 variable against a constant. If the pairing was good, then the
1012 processor's own internal branch predication has a high likelihood of
1013 success, resulting in a nearly zero-overhead transition to the
1014 next opcode. A successful prediction saves a trip through the eval-loop
1015 including its two unpredictable branches, the HAS_ARG test and the
1016 switch-case. Combined with the processor's internal branch prediction,
1017 a successful PREDICT has the effect of making the two opcodes run as if
1018 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001019
Georg Brandl86b2fb92008-07-16 03:43:04 +00001020 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 predictions turned-on and interpret the results as if some opcodes
1022 had been combined or turn-off predictions so that the opcode frequency
1023 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024
1025 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 the CPU to record separate branch prediction information for each
1027 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001028
Raymond Hettingerf606f872003-03-16 03:11:04 +00001029*/
1030
Antoine Pitrou042b1282010-08-13 21:15:58 +00001031#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032#define PREDICT(op) if (0) goto PRED_##op
1033#define PREDICTED(op) PRED_##op:
1034#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001035#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1037#define PREDICTED(op) PRED_##op: next_instr++
1038#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001039#endif
1040
Raymond Hettingerf606f872003-03-16 03:11:04 +00001041
Guido van Rossum374a9221991-04-04 10:40:29 +00001042/* Stack manipulation macros */
1043
Martin v. Löwis18e16552006-02-15 17:27:45 +00001044/* The stack can grow at most MAXINT deep, as co_nlocals and
1045 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001046#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1047#define EMPTY() (STACK_LEVEL() == 0)
1048#define TOP() (stack_pointer[-1])
1049#define SECOND() (stack_pointer[-2])
1050#define THIRD() (stack_pointer[-3])
1051#define FOURTH() (stack_pointer[-4])
1052#define PEEK(n) (stack_pointer[-(n)])
1053#define SET_TOP(v) (stack_pointer[-1] = (v))
1054#define SET_SECOND(v) (stack_pointer[-2] = (v))
1055#define SET_THIRD(v) (stack_pointer[-3] = (v))
1056#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1057#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1058#define BASIC_STACKADJ(n) (stack_pointer += n)
1059#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1060#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001061
Guido van Rossum96a42c81992-01-12 02:29:51 +00001062#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001064 lltrace && prtrace(TOP(), "push")); \
1065 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001067 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001069 lltrace && prtrace(TOP(), "stackadj")); \
1070 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001071#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001072 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1073 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001074#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001075#define PUSH(v) BASIC_PUSH(v)
1076#define POP() BASIC_POP()
1077#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001078#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001079#endif
1080
Guido van Rossum681d79a1995-07-18 14:51:37 +00001081/* Local variable macros */
1082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001084
1085/* The SETLOCAL() macro must not DECREF the local variable in-place and
1086 then store the new value; it must copy the old value to a temporary
1087 value, then store the new value, and then DECREF the temporary value.
1088 This is because it is possible that during the DECREF the frame is
1089 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1090 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001092 GETLOCAL(i) = value; \
1093 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001094
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001095
1096#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 while (STACK_LEVEL() > (b)->b_level) { \
1098 PyObject *v = POP(); \
1099 Py_XDECREF(v); \
1100 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001101
1102#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 { \
1104 PyObject *type, *value, *traceback; \
1105 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1106 while (STACK_LEVEL() > (b)->b_level + 3) { \
1107 value = POP(); \
1108 Py_XDECREF(value); \
1109 } \
1110 type = tstate->exc_type; \
1111 value = tstate->exc_value; \
1112 traceback = tstate->exc_traceback; \
1113 tstate->exc_type = POP(); \
1114 tstate->exc_value = POP(); \
1115 tstate->exc_traceback = POP(); \
1116 Py_XDECREF(type); \
1117 Py_XDECREF(value); \
1118 Py_XDECREF(traceback); \
1119 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001120
Guido van Rossuma027efa1997-05-05 20:56:21 +00001121/* Start of code */
1122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 /* push frame */
1124 if (Py_EnterRecursiveCall(""))
1125 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 if (tstate->use_tracing) {
1130 if (tstate->c_tracefunc != NULL) {
1131 /* tstate->c_tracefunc, if defined, is a
1132 function that will be called on *every* entry
1133 to a code block. Its return value, if not
1134 None, is a function that will be called at
1135 the start of each executed line of code.
1136 (Actually, the function must return itself
1137 in order to continue tracing.) The trace
1138 functions are called with three arguments:
1139 a pointer to the current frame, a string
1140 indicating why the function is called, and
1141 an argument which depends on the situation.
1142 The global trace function is also called
1143 whenever an exception is detected. */
1144 if (call_trace_protected(tstate->c_tracefunc,
1145 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001146 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 /* Trace function raised an error */
1148 goto exit_eval_frame;
1149 }
1150 }
1151 if (tstate->c_profilefunc != NULL) {
1152 /* Similar for c_profilefunc, except it needn't
1153 return itself and isn't called for "line" events */
1154 if (call_trace_protected(tstate->c_profilefunc,
1155 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001156 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 /* Profile function raised an error */
1158 goto exit_eval_frame;
1159 }
1160 }
1161 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 co = f->f_code;
1164 names = co->co_names;
1165 consts = co->co_consts;
1166 fastlocals = f->f_localsplus;
1167 freevars = f->f_localsplus + co->co_nlocals;
1168 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1169 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001170
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 f->f_lasti now refers to the index of the last instruction
1172 executed. You might think this was obvious from the name, but
1173 this wasn't always true before 2.3! PyFrame_New now sets
1174 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1175 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1176 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001177 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1178 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 When the PREDICT() macros are enabled, some opcode pairs follow in
1181 direct succession without updating f->f_lasti. A successful
1182 prediction effectively links the two codes together as if they
1183 were a single new opcode; accordingly,f->f_lasti will point to
1184 the first code in the pair (for instance, GET_ITER followed by
1185 FOR_ITER is effectively a single opcode and f->f_lasti will point
1186 at to the beginning of the combined pair.)
1187 */
1188 next_instr = first_instr + f->f_lasti + 1;
1189 stack_pointer = f->f_stacktop;
1190 assert(stack_pointer != NULL);
1191 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001192 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001193
Yury Selivanov5376ba92015-06-22 12:19:30 -04001194 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001195 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 /* We were in an except handler when we left,
1197 restore the exception state which was put aside
1198 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001199 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001201 else
1202 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001203 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001204
Tim Peters5ca576e2001-06-18 22:08:13 +00001205#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001206 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001207#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001211 if (throwflag) /* support for generator.throw() */
1212 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001213
Victor Stinnerace47d72013-07-18 01:41:08 +02001214#ifdef Py_DEBUG
1215 /* PyEval_EvalFrameEx() must not be called with an exception set,
1216 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001217 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001218 assert(!PyErr_Occurred());
1219#endif
1220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001222#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 if (inst1 == 0) {
1224 /* Almost surely, the opcode executed a break
1225 or a continue, preventing inst1 from being set
1226 on the way out of the loop.
1227 */
1228 READ_TIMESTAMP(inst1);
1229 loop1 = inst1;
1230 }
1231 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1232 intr0, intr1);
1233 ticked = 0;
1234 inst1 = 0;
1235 intr0 = 0;
1236 intr1 = 0;
1237 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001238#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1240 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001241 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 /* Do periodic things. Doing this every time through
1244 the loop would add too much overhead, so we do it
1245 only every Nth instruction. We also do it if
1246 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1247 event needs attention (e.g. a signal handler or
1248 async I/O handler); see Py_AddPendingCall() and
1249 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1252 if (*next_instr == SETUP_FINALLY) {
1253 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001254 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 goto fast_next_opcode;
1256 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001257#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001259#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001261 if (Py_MakePendingCalls() < 0)
1262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001264#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001265 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 /* Give another thread a chance */
1267 if (PyThreadState_Swap(NULL) != tstate)
1268 Py_FatalError("ceval: tstate mix-up");
1269 drop_gil(tstate);
1270
1271 /* Other threads may run now */
1272
1273 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001274
1275 /* Check if we should make a quick exit. */
1276 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1277 drop_gil(tstate);
1278 PyThread_exit_thread();
1279 }
1280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (PyThreadState_Swap(tstate) != NULL)
1282 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001284#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 /* Check for asynchronous exceptions. */
1286 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 tstate->async_exc = NULL;
1289 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001290 PyErr_SetNone(exc);
1291 Py_DECREF(exc);
1292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
1294 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 fast_next_opcode:
1297 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001302 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001303 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 /* see maybe_call_line_trace
1305 for expository comments */
1306 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 err = maybe_call_line_trace(tstate->c_tracefunc,
1309 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001310 tstate, f,
1311 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* Reload possibly changed frame fields */
1313 JUMPTO(f->f_lasti);
1314 if (f->f_stacktop != NULL) {
1315 stack_pointer = f->f_stacktop;
1316 f->f_stacktop = NULL;
1317 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001318 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001320 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 opcode = NEXTOP();
1326 oparg = 0; /* allows oparg to be stored in a register because
1327 it doesn't have to be remembered across a full loop */
1328 if (HAS_ARG(opcode))
1329 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001330 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001331#ifdef DYNAMIC_EXECUTION_PROFILE
1332#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 dxpairs[lastopcode][opcode]++;
1334 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001335#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001337#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001338
Guido van Rossum96a42c81992-01-12 02:29:51 +00001339#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (lltrace) {
1343 if (HAS_ARG(opcode)) {
1344 printf("%d: %d, %d\n",
1345 f->f_lasti, opcode, oparg);
1346 }
1347 else {
1348 printf("%d: %d\n",
1349 f->f_lasti, opcode);
1350 }
1351 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001352#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 /* Main switch on opcode */
1355 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 /* BEWARE!
1360 It is essential that any operation that fails sets either
1361 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1362 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 TARGET(NOP)
1365 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001366
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001367 TARGET(LOAD_FAST) {
1368 PyObject *value = GETLOCAL(oparg);
1369 if (value == NULL) {
1370 format_exc_check_arg(PyExc_UnboundLocalError,
1371 UNBOUNDLOCAL_ERROR_MSG,
1372 PyTuple_GetItem(co->co_varnames, oparg));
1373 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001375 Py_INCREF(value);
1376 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 }
1379
1380 TARGET(LOAD_CONST) {
1381 PyObject *value = GETITEM(consts, oparg);
1382 Py_INCREF(value);
1383 PUSH(value);
1384 FAST_DISPATCH();
1385 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 TARGET(STORE_FAST) {
1389 PyObject *value = POP();
1390 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001392 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001393
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 TARGET(POP_TOP) {
1395 PyObject *value = POP();
1396 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 TARGET(ROT_TWO) {
1401 PyObject *top = TOP();
1402 PyObject *second = SECOND();
1403 SET_TOP(second);
1404 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 TARGET(ROT_THREE) {
1409 PyObject *top = TOP();
1410 PyObject *second = SECOND();
1411 PyObject *third = THIRD();
1412 SET_TOP(second);
1413 SET_SECOND(third);
1414 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001416 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 TARGET(DUP_TOP) {
1419 PyObject *top = TOP();
1420 Py_INCREF(top);
1421 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 TARGET(DUP_TOP_TWO) {
1426 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001428 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001430 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 SET_TOP(top);
1432 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001433 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001435
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001436 TARGET(UNARY_POSITIVE) {
1437 PyObject *value = TOP();
1438 PyObject *res = PyNumber_Positive(value);
1439 Py_DECREF(value);
1440 SET_TOP(res);
1441 if (res == NULL)
1442 goto error;
1443 DISPATCH();
1444 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001445
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001446 TARGET(UNARY_NEGATIVE) {
1447 PyObject *value = TOP();
1448 PyObject *res = PyNumber_Negative(value);
1449 Py_DECREF(value);
1450 SET_TOP(res);
1451 if (res == NULL)
1452 goto error;
1453 DISPATCH();
1454 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001456 TARGET(UNARY_NOT) {
1457 PyObject *value = TOP();
1458 int err = PyObject_IsTrue(value);
1459 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001460 if (err == 0) {
1461 Py_INCREF(Py_True);
1462 SET_TOP(Py_True);
1463 DISPATCH();
1464 }
1465 else if (err > 0) {
1466 Py_INCREF(Py_False);
1467 SET_TOP(Py_False);
1468 err = 0;
1469 DISPATCH();
1470 }
1471 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001472 goto error;
1473 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 TARGET(UNARY_INVERT) {
1476 PyObject *value = TOP();
1477 PyObject *res = PyNumber_Invert(value);
1478 Py_DECREF(value);
1479 SET_TOP(res);
1480 if (res == NULL)
1481 goto error;
1482 DISPATCH();
1483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001484
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001485 TARGET(BINARY_POWER) {
1486 PyObject *exp = POP();
1487 PyObject *base = TOP();
1488 PyObject *res = PyNumber_Power(base, exp, Py_None);
1489 Py_DECREF(base);
1490 Py_DECREF(exp);
1491 SET_TOP(res);
1492 if (res == NULL)
1493 goto error;
1494 DISPATCH();
1495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001497 TARGET(BINARY_MULTIPLY) {
1498 PyObject *right = POP();
1499 PyObject *left = TOP();
1500 PyObject *res = PyNumber_Multiply(left, right);
1501 Py_DECREF(left);
1502 Py_DECREF(right);
1503 SET_TOP(res);
1504 if (res == NULL)
1505 goto error;
1506 DISPATCH();
1507 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Benjamin Petersond51374e2014-04-09 23:55:56 -04001509 TARGET(BINARY_MATRIX_MULTIPLY) {
1510 PyObject *right = POP();
1511 PyObject *left = TOP();
1512 PyObject *res = PyNumber_MatrixMultiply(left, right);
1513 Py_DECREF(left);
1514 Py_DECREF(right);
1515 SET_TOP(res);
1516 if (res == NULL)
1517 goto error;
1518 DISPATCH();
1519 }
1520
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001521 TARGET(BINARY_TRUE_DIVIDE) {
1522 PyObject *divisor = POP();
1523 PyObject *dividend = TOP();
1524 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1525 Py_DECREF(dividend);
1526 Py_DECREF(divisor);
1527 SET_TOP(quotient);
1528 if (quotient == NULL)
1529 goto error;
1530 DISPATCH();
1531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001533 TARGET(BINARY_FLOOR_DIVIDE) {
1534 PyObject *divisor = POP();
1535 PyObject *dividend = TOP();
1536 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1537 Py_DECREF(dividend);
1538 Py_DECREF(divisor);
1539 SET_TOP(quotient);
1540 if (quotient == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001545 TARGET(BINARY_MODULO) {
1546 PyObject *divisor = POP();
1547 PyObject *dividend = TOP();
1548 PyObject *res = PyUnicode_CheckExact(dividend) ?
1549 PyUnicode_Format(dividend, divisor) :
1550 PyNumber_Remainder(dividend, divisor);
1551 Py_DECREF(divisor);
1552 Py_DECREF(dividend);
1553 SET_TOP(res);
1554 if (res == NULL)
1555 goto error;
1556 DISPATCH();
1557 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001559 TARGET(BINARY_ADD) {
1560 PyObject *right = POP();
1561 PyObject *left = TOP();
1562 PyObject *sum;
1563 if (PyUnicode_CheckExact(left) &&
1564 PyUnicode_CheckExact(right)) {
1565 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001566 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001567 }
1568 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 sum = PyNumber_Add(left, right);
1570 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001571 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 Py_DECREF(right);
1573 SET_TOP(sum);
1574 if (sum == NULL)
1575 goto error;
1576 DISPATCH();
1577 }
1578
1579 TARGET(BINARY_SUBTRACT) {
1580 PyObject *right = POP();
1581 PyObject *left = TOP();
1582 PyObject *diff = PyNumber_Subtract(left, right);
1583 Py_DECREF(right);
1584 Py_DECREF(left);
1585 SET_TOP(diff);
1586 if (diff == NULL)
1587 goto error;
1588 DISPATCH();
1589 }
1590
1591 TARGET(BINARY_SUBSCR) {
1592 PyObject *sub = POP();
1593 PyObject *container = TOP();
1594 PyObject *res = PyObject_GetItem(container, sub);
1595 Py_DECREF(container);
1596 Py_DECREF(sub);
1597 SET_TOP(res);
1598 if (res == NULL)
1599 goto error;
1600 DISPATCH();
1601 }
1602
1603 TARGET(BINARY_LSHIFT) {
1604 PyObject *right = POP();
1605 PyObject *left = TOP();
1606 PyObject *res = PyNumber_Lshift(left, right);
1607 Py_DECREF(left);
1608 Py_DECREF(right);
1609 SET_TOP(res);
1610 if (res == NULL)
1611 goto error;
1612 DISPATCH();
1613 }
1614
1615 TARGET(BINARY_RSHIFT) {
1616 PyObject *right = POP();
1617 PyObject *left = TOP();
1618 PyObject *res = PyNumber_Rshift(left, right);
1619 Py_DECREF(left);
1620 Py_DECREF(right);
1621 SET_TOP(res);
1622 if (res == NULL)
1623 goto error;
1624 DISPATCH();
1625 }
1626
1627 TARGET(BINARY_AND) {
1628 PyObject *right = POP();
1629 PyObject *left = TOP();
1630 PyObject *res = PyNumber_And(left, right);
1631 Py_DECREF(left);
1632 Py_DECREF(right);
1633 SET_TOP(res);
1634 if (res == NULL)
1635 goto error;
1636 DISPATCH();
1637 }
1638
1639 TARGET(BINARY_XOR) {
1640 PyObject *right = POP();
1641 PyObject *left = TOP();
1642 PyObject *res = PyNumber_Xor(left, right);
1643 Py_DECREF(left);
1644 Py_DECREF(right);
1645 SET_TOP(res);
1646 if (res == NULL)
1647 goto error;
1648 DISPATCH();
1649 }
1650
1651 TARGET(BINARY_OR) {
1652 PyObject *right = POP();
1653 PyObject *left = TOP();
1654 PyObject *res = PyNumber_Or(left, right);
1655 Py_DECREF(left);
1656 Py_DECREF(right);
1657 SET_TOP(res);
1658 if (res == NULL)
1659 goto error;
1660 DISPATCH();
1661 }
1662
1663 TARGET(LIST_APPEND) {
1664 PyObject *v = POP();
1665 PyObject *list = PEEK(oparg);
1666 int err;
1667 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001668 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 if (err != 0)
1670 goto error;
1671 PREDICT(JUMP_ABSOLUTE);
1672 DISPATCH();
1673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001675 TARGET(SET_ADD) {
1676 PyObject *v = POP();
1677 PyObject *set = stack_pointer[-oparg];
1678 int err;
1679 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001681 if (err != 0)
1682 goto error;
1683 PREDICT(JUMP_ABSOLUTE);
1684 DISPATCH();
1685 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001687 TARGET(INPLACE_POWER) {
1688 PyObject *exp = POP();
1689 PyObject *base = TOP();
1690 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1691 Py_DECREF(base);
1692 Py_DECREF(exp);
1693 SET_TOP(res);
1694 if (res == NULL)
1695 goto error;
1696 DISPATCH();
1697 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 TARGET(INPLACE_MULTIPLY) {
1700 PyObject *right = POP();
1701 PyObject *left = TOP();
1702 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1703 Py_DECREF(left);
1704 Py_DECREF(right);
1705 SET_TOP(res);
1706 if (res == NULL)
1707 goto error;
1708 DISPATCH();
1709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001710
Benjamin Petersond51374e2014-04-09 23:55:56 -04001711 TARGET(INPLACE_MATRIX_MULTIPLY) {
1712 PyObject *right = POP();
1713 PyObject *left = TOP();
1714 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1715 Py_DECREF(left);
1716 Py_DECREF(right);
1717 SET_TOP(res);
1718 if (res == NULL)
1719 goto error;
1720 DISPATCH();
1721 }
1722
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001723 TARGET(INPLACE_TRUE_DIVIDE) {
1724 PyObject *divisor = POP();
1725 PyObject *dividend = TOP();
1726 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1727 Py_DECREF(dividend);
1728 Py_DECREF(divisor);
1729 SET_TOP(quotient);
1730 if (quotient == NULL)
1731 goto error;
1732 DISPATCH();
1733 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001734
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001735 TARGET(INPLACE_FLOOR_DIVIDE) {
1736 PyObject *divisor = POP();
1737 PyObject *dividend = TOP();
1738 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1739 Py_DECREF(dividend);
1740 Py_DECREF(divisor);
1741 SET_TOP(quotient);
1742 if (quotient == NULL)
1743 goto error;
1744 DISPATCH();
1745 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001746
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001747 TARGET(INPLACE_MODULO) {
1748 PyObject *right = POP();
1749 PyObject *left = TOP();
1750 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1751 Py_DECREF(left);
1752 Py_DECREF(right);
1753 SET_TOP(mod);
1754 if (mod == NULL)
1755 goto error;
1756 DISPATCH();
1757 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001759 TARGET(INPLACE_ADD) {
1760 PyObject *right = POP();
1761 PyObject *left = TOP();
1762 PyObject *sum;
1763 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1764 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001765 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001766 }
1767 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001768 sum = PyNumber_InPlaceAdd(left, right);
1769 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001770 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 Py_DECREF(right);
1772 SET_TOP(sum);
1773 if (sum == NULL)
1774 goto error;
1775 DISPATCH();
1776 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001777
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 TARGET(INPLACE_SUBTRACT) {
1779 PyObject *right = POP();
1780 PyObject *left = TOP();
1781 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1782 Py_DECREF(left);
1783 Py_DECREF(right);
1784 SET_TOP(diff);
1785 if (diff == NULL)
1786 goto error;
1787 DISPATCH();
1788 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 TARGET(INPLACE_LSHIFT) {
1791 PyObject *right = POP();
1792 PyObject *left = TOP();
1793 PyObject *res = PyNumber_InPlaceLshift(left, right);
1794 Py_DECREF(left);
1795 Py_DECREF(right);
1796 SET_TOP(res);
1797 if (res == NULL)
1798 goto error;
1799 DISPATCH();
1800 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 TARGET(INPLACE_RSHIFT) {
1803 PyObject *right = POP();
1804 PyObject *left = TOP();
1805 PyObject *res = PyNumber_InPlaceRshift(left, right);
1806 Py_DECREF(left);
1807 Py_DECREF(right);
1808 SET_TOP(res);
1809 if (res == NULL)
1810 goto error;
1811 DISPATCH();
1812 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001813
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001814 TARGET(INPLACE_AND) {
1815 PyObject *right = POP();
1816 PyObject *left = TOP();
1817 PyObject *res = PyNumber_InPlaceAnd(left, right);
1818 Py_DECREF(left);
1819 Py_DECREF(right);
1820 SET_TOP(res);
1821 if (res == NULL)
1822 goto error;
1823 DISPATCH();
1824 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001825
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 TARGET(INPLACE_XOR) {
1827 PyObject *right = POP();
1828 PyObject *left = TOP();
1829 PyObject *res = PyNumber_InPlaceXor(left, right);
1830 Py_DECREF(left);
1831 Py_DECREF(right);
1832 SET_TOP(res);
1833 if (res == NULL)
1834 goto error;
1835 DISPATCH();
1836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001837
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 TARGET(INPLACE_OR) {
1839 PyObject *right = POP();
1840 PyObject *left = TOP();
1841 PyObject *res = PyNumber_InPlaceOr(left, right);
1842 Py_DECREF(left);
1843 Py_DECREF(right);
1844 SET_TOP(res);
1845 if (res == NULL)
1846 goto error;
1847 DISPATCH();
1848 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001849
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001850 TARGET(STORE_SUBSCR) {
1851 PyObject *sub = TOP();
1852 PyObject *container = SECOND();
1853 PyObject *v = THIRD();
1854 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001856 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 Py_DECREF(container);
1860 Py_DECREF(sub);
1861 if (err != 0)
1862 goto error;
1863 DISPATCH();
1864 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 TARGET(DELETE_SUBSCR) {
1867 PyObject *sub = TOP();
1868 PyObject *container = SECOND();
1869 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001871 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 err = PyObject_DelItem(container, sub);
1873 Py_DECREF(container);
1874 Py_DECREF(sub);
1875 if (err != 0)
1876 goto error;
1877 DISPATCH();
1878 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001879
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001881 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001883 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001884 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 PyErr_SetString(PyExc_RuntimeError,
1887 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 Py_DECREF(value);
1889 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001891 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 Py_DECREF(value);
1893 if (res == NULL)
1894 goto error;
1895 Py_DECREF(res);
1896 DISPATCH();
1897 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001898
Thomas Wouters434d0822000-08-24 20:11:32 +00001899#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001901#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 TARGET(RAISE_VARARGS) {
1903 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 switch (oparg) {
1905 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 if (do_raise(exc, cause)) {
1911 why = WHY_EXCEPTION;
1912 goto fast_block_end;
1913 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 break;
1915 default:
1916 PyErr_SetString(PyExc_SystemError,
1917 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 break;
1919 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001920 goto error;
1921 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 retval = POP();
1925 why = WHY_RETURN;
1926 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001928
Yury Selivanov75445082015-05-11 22:57:16 -04001929 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001930 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001931 PyObject *iter = NULL;
1932 PyObject *awaitable = NULL;
1933 PyObject *obj = TOP();
1934 PyTypeObject *type = Py_TYPE(obj);
1935
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001936 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001937 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001938 }
Yury Selivanov75445082015-05-11 22:57:16 -04001939
1940 if (getter != NULL) {
1941 iter = (*getter)(obj);
1942 Py_DECREF(obj);
1943 if (iter == NULL) {
1944 SET_TOP(NULL);
1945 goto error;
1946 }
1947 }
1948 else {
1949 SET_TOP(NULL);
1950 PyErr_Format(
1951 PyExc_TypeError,
1952 "'async for' requires an object with "
1953 "__aiter__ method, got %.100s",
1954 type->tp_name);
1955 Py_DECREF(obj);
1956 goto error;
1957 }
1958
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001959 if (Py_TYPE(iter)->tp_as_async != NULL &&
1960 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1961
1962 /* Starting with CPython 3.5.2 __aiter__ should return
1963 asynchronous iterators directly (not awaitables that
1964 resolve to asynchronous iterators.)
1965
1966 Therefore, we check if the object that was returned
1967 from __aiter__ has an __anext__ method. If it does,
1968 we wrap it in an awaitable that resolves to `iter`.
1969
1970 See http://bugs.python.org/issue27243 for more
1971 details.
1972 */
1973
1974 PyObject *wrapper = _PyAIterWrapper_New(iter);
1975 Py_DECREF(iter);
1976 SET_TOP(wrapper);
1977 DISPATCH();
1978 }
1979
Yury Selivanov5376ba92015-06-22 12:19:30 -04001980 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001981 if (awaitable == NULL) {
1982 SET_TOP(NULL);
1983 PyErr_Format(
1984 PyExc_TypeError,
1985 "'async for' received an invalid object "
1986 "from __aiter__: %.100s",
1987 Py_TYPE(iter)->tp_name);
1988
1989 Py_DECREF(iter);
1990 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001991 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001992 Py_DECREF(iter);
1993
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001994 if (PyErr_WarnFormat(
1995 PyExc_PendingDeprecationWarning, 1,
1996 "'%.100s' implements legacy __aiter__ protocol; "
1997 "__aiter__ should return an asynchronous "
1998 "iterator, not awaitable",
1999 type->tp_name))
2000 {
2001 /* Warning was converted to an error. */
2002 Py_DECREF(awaitable);
2003 SET_TOP(NULL);
2004 goto error;
2005 }
2006 }
2007
Yury Selivanov75445082015-05-11 22:57:16 -04002008 SET_TOP(awaitable);
2009 DISPATCH();
2010 }
2011
2012 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002013 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002014 PyObject *next_iter = NULL;
2015 PyObject *awaitable = NULL;
2016 PyObject *aiter = TOP();
2017 PyTypeObject *type = Py_TYPE(aiter);
2018
2019 if (type->tp_as_async != NULL)
2020 getter = type->tp_as_async->am_anext;
2021
2022 if (getter != NULL) {
2023 next_iter = (*getter)(aiter);
2024 if (next_iter == NULL) {
2025 goto error;
2026 }
2027 }
2028 else {
2029 PyErr_Format(
2030 PyExc_TypeError,
2031 "'async for' requires an iterator with "
2032 "__anext__ method, got %.100s",
2033 type->tp_name);
2034 goto error;
2035 }
2036
Yury Selivanov5376ba92015-06-22 12:19:30 -04002037 awaitable = _PyCoro_GetAwaitableIter(next_iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002038 if (awaitable == NULL) {
2039 PyErr_Format(
2040 PyExc_TypeError,
2041 "'async for' received an invalid object "
2042 "from __anext__: %.100s",
2043 Py_TYPE(next_iter)->tp_name);
2044
2045 Py_DECREF(next_iter);
2046 goto error;
2047 } else
2048 Py_DECREF(next_iter);
2049
2050 PUSH(awaitable);
2051 DISPATCH();
2052 }
2053
2054 TARGET(GET_AWAITABLE) {
2055 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002056 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002057
2058 Py_DECREF(iterable);
2059
Yury Selivanovc724bae2016-03-02 11:30:46 -05002060 if (iter != NULL && PyCoro_CheckExact(iter)) {
2061 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2062 if (yf != NULL) {
2063 /* `iter` is a coroutine object that is being
2064 awaited, `yf` is a pointer to the current awaitable
2065 being awaited on. */
2066 Py_DECREF(yf);
2067 Py_CLEAR(iter);
2068 PyErr_SetString(
2069 PyExc_RuntimeError,
2070 "coroutine is being awaited already");
2071 /* The code below jumps to `error` if `iter` is NULL. */
2072 }
2073 }
2074
Yury Selivanov75445082015-05-11 22:57:16 -04002075 SET_TOP(iter); /* Even if it's NULL */
2076
2077 if (iter == NULL) {
2078 goto error;
2079 }
2080
2081 DISPATCH();
2082 }
2083
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 TARGET(YIELD_FROM) {
2085 PyObject *v = POP();
2086 PyObject *reciever = TOP();
2087 int err;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002088 if (PyGen_CheckExact(reciever) || PyCoro_CheckExact(reciever)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002090 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002091 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 if (v == Py_None)
2093 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002094 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04002095 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002096 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 Py_DECREF(v);
2098 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002099 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002100 if (tstate->c_tracefunc != NULL
2101 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002102 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002103 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 if (err < 0)
2105 goto error;
2106 Py_DECREF(reciever);
2107 SET_TOP(val);
2108 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002109 }
Martin Panter95f53c12016-07-18 08:23:26 +00002110 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002111 f->f_stacktop = stack_pointer;
2112 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002113 /* and repeat... */
2114 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002115 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002117
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002118 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 retval = POP();
2120 f->f_stacktop = stack_pointer;
2121 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002124
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 TARGET(POP_EXCEPT) {
2126 PyTryBlock *b = PyFrame_BlockPop(f);
2127 if (b->b_type != EXCEPT_HANDLER) {
2128 PyErr_SetString(PyExc_SystemError,
2129 "popped block is not an except handler");
2130 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002135
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002136 TARGET(POP_BLOCK) {
2137 PyTryBlock *b = PyFrame_BlockPop(f);
2138 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002139 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 TARGET(END_FINALLY) {
2144 PyObject *status = POP();
2145 if (PyLong_Check(status)) {
2146 why = (enum why_code) PyLong_AS_LONG(status);
2147 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002148 if (why == WHY_RETURN ||
2149 why == WHY_CONTINUE)
2150 retval = POP();
2151 if (why == WHY_SILENCED) {
2152 /* An exception was silenced by 'with', we must
2153 manually unwind the EXCEPT_HANDLER block which was
2154 created when the exception was caught, otherwise
2155 the stack will be in an inconsistent state. */
2156 PyTryBlock *b = PyFrame_BlockPop(f);
2157 assert(b->b_type == EXCEPT_HANDLER);
2158 UNWIND_EXCEPT_HANDLER(b);
2159 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 Py_DECREF(status);
2161 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 Py_DECREF(status);
2164 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002166 else if (PyExceptionClass_Check(status)) {
2167 PyObject *exc = POP();
2168 PyObject *tb = POP();
2169 PyErr_Restore(status, exc, tb);
2170 why = WHY_EXCEPTION;
2171 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002173 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 PyErr_SetString(PyExc_SystemError,
2175 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 Py_DECREF(status);
2177 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 Py_DECREF(status);
2180 DISPATCH();
2181 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002182
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002184 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002185
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002187 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2189 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002190 PyErr_SetString(PyExc_NameError,
2191 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002193 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002195 }
2196 else {
2197 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2198 if (build_class_str == NULL)
2199 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2201 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002202 if (PyErr_ExceptionMatches(PyExc_KeyError))
2203 PyErr_SetString(PyExc_NameError,
2204 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002206 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002207 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002209 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002210 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 TARGET(STORE_NAME) {
2213 PyObject *name = GETITEM(names, oparg);
2214 PyObject *v = POP();
2215 PyObject *ns = f->f_locals;
2216 int err;
2217 if (ns == NULL) {
2218 PyErr_Format(PyExc_SystemError,
2219 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 if (PyDict_CheckExact(ns))
2224 err = PyDict_SetItem(ns, name, v);
2225 else
2226 err = PyObject_SetItem(ns, name, v);
2227 Py_DECREF(v);
2228 if (err != 0)
2229 goto error;
2230 DISPATCH();
2231 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002232
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 TARGET(DELETE_NAME) {
2234 PyObject *name = GETITEM(names, oparg);
2235 PyObject *ns = f->f_locals;
2236 int err;
2237 if (ns == NULL) {
2238 PyErr_Format(PyExc_SystemError,
2239 "no locals when deleting %R", name);
2240 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 err = PyObject_DelItem(ns, name);
2243 if (err != 0) {
2244 format_exc_check_arg(PyExc_NameError,
2245 NAME_ERROR_MSG,
2246 name);
2247 goto error;
2248 }
2249 DISPATCH();
2250 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 TARGET(UNPACK_SEQUENCE) {
2254 PyObject *seq = POP(), *item, **items;
2255 if (PyTuple_CheckExact(seq) &&
2256 PyTuple_GET_SIZE(seq) == oparg) {
2257 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002258 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002259 item = items[oparg];
2260 Py_INCREF(item);
2261 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 } else if (PyList_CheckExact(seq) &&
2264 PyList_GET_SIZE(seq) == oparg) {
2265 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 item = items[oparg];
2268 Py_INCREF(item);
2269 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 stack_pointer + oparg)) {
2273 STACKADJ(oparg);
2274 } else {
2275 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 Py_DECREF(seq);
2277 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002280 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002282
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 TARGET(UNPACK_EX) {
2284 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2285 PyObject *seq = POP();
2286
2287 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2288 stack_pointer + totalargs)) {
2289 stack_pointer += totalargs;
2290 } else {
2291 Py_DECREF(seq);
2292 goto error;
2293 }
2294 Py_DECREF(seq);
2295 DISPATCH();
2296 }
2297
2298 TARGET(STORE_ATTR) {
2299 PyObject *name = GETITEM(names, oparg);
2300 PyObject *owner = TOP();
2301 PyObject *v = SECOND();
2302 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002304 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002306 Py_DECREF(owner);
2307 if (err != 0)
2308 goto error;
2309 DISPATCH();
2310 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 TARGET(DELETE_ATTR) {
2313 PyObject *name = GETITEM(names, oparg);
2314 PyObject *owner = POP();
2315 int err;
2316 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2317 Py_DECREF(owner);
2318 if (err != 0)
2319 goto error;
2320 DISPATCH();
2321 }
2322
2323 TARGET(STORE_GLOBAL) {
2324 PyObject *name = GETITEM(names, oparg);
2325 PyObject *v = POP();
2326 int err;
2327 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 if (err != 0)
2330 goto error;
2331 DISPATCH();
2332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 TARGET(DELETE_GLOBAL) {
2335 PyObject *name = GETITEM(names, oparg);
2336 int err;
2337 err = PyDict_DelItem(f->f_globals, name);
2338 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002340 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002342 }
2343 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002345
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 TARGET(LOAD_NAME) {
2347 PyObject *name = GETITEM(names, oparg);
2348 PyObject *locals = f->f_locals;
2349 PyObject *v;
2350 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002351 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 "no locals when loading %R", name);
2353 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002355 if (PyDict_CheckExact(locals)) {
2356 v = PyDict_GetItem(locals, name);
2357 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 }
2359 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 v = PyObject_GetItem(locals, name);
Antoine Pitrou1cfa0ba2013-10-07 20:40:59 +02002361 if (v == NULL && _PyErr_OCCURRED()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002362 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2363 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 PyErr_Clear();
2365 }
2366 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 if (v == NULL) {
2368 v = PyDict_GetItem(f->f_globals, name);
2369 Py_XINCREF(v);
2370 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002371 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002372 v = PyDict_GetItem(f->f_builtins, name);
2373 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002374 format_exc_check_arg(
2375 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 NAME_ERROR_MSG, name);
2377 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002380 }
2381 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002382 v = PyObject_GetItem(f->f_builtins, name);
2383 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002384 if (PyErr_ExceptionMatches(PyExc_KeyError))
2385 format_exc_check_arg(
2386 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 NAME_ERROR_MSG, name);
2388 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002389 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002390 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002397 TARGET(LOAD_GLOBAL) {
2398 PyObject *name = GETITEM(names, oparg);
2399 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002400 if (PyDict_CheckExact(f->f_globals)
2401 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002403 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 name);
2405 if (v == NULL) {
Antoine Pitrou59c900d2013-10-07 20:38:51 +02002406 if (!_PyErr_OCCURRED())
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002407 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002408 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002413 else {
2414 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002415 v = PyObject_GetItem(f->f_globals, name);
2416 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002417 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2418 goto error;
2419 PyErr_Clear();
2420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 v = PyObject_GetItem(f->f_builtins, name);
2422 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002423 if (PyErr_ExceptionMatches(PyExc_KeyError))
2424 format_exc_check_arg(
2425 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002426 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002428 }
2429 }
2430 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002432 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002434
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 TARGET(DELETE_FAST) {
2436 PyObject *v = GETLOCAL(oparg);
2437 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 SETLOCAL(oparg, NULL);
2439 DISPATCH();
2440 }
2441 format_exc_check_arg(
2442 PyExc_UnboundLocalError,
2443 UNBOUNDLOCAL_ERROR_MSG,
2444 PyTuple_GetItem(co->co_varnames, oparg)
2445 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 goto error;
2447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002449 TARGET(DELETE_DEREF) {
2450 PyObject *cell = freevars[oparg];
2451 if (PyCell_GET(cell) != NULL) {
2452 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002453 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002454 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002455 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002456 goto error;
2457 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 TARGET(LOAD_CLOSURE) {
2460 PyObject *cell = freevars[oparg];
2461 Py_INCREF(cell);
2462 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002465
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002466 TARGET(LOAD_CLASSDEREF) {
2467 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002468 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002469 assert(locals);
2470 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2471 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2472 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2473 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2474 if (PyDict_CheckExact(locals)) {
2475 value = PyDict_GetItem(locals, name);
2476 Py_XINCREF(value);
2477 }
2478 else {
2479 value = PyObject_GetItem(locals, name);
2480 if (value == NULL && PyErr_Occurred()) {
2481 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2482 goto error;
2483 PyErr_Clear();
2484 }
2485 }
2486 if (!value) {
2487 PyObject *cell = freevars[oparg];
2488 value = PyCell_GET(cell);
2489 if (value == NULL) {
2490 format_exc_unbound(co, oparg);
2491 goto error;
2492 }
2493 Py_INCREF(value);
2494 }
2495 PUSH(value);
2496 DISPATCH();
2497 }
2498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 TARGET(LOAD_DEREF) {
2500 PyObject *cell = freevars[oparg];
2501 PyObject *value = PyCell_GET(cell);
2502 if (value == NULL) {
2503 format_exc_unbound(co, oparg);
2504 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 Py_INCREF(value);
2507 PUSH(value);
2508 DISPATCH();
2509 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002510
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002511 TARGET(STORE_DEREF) {
2512 PyObject *v = POP();
2513 PyObject *cell = freevars[oparg];
2514 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 DISPATCH();
2517 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002518
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002519 TARGET(BUILD_TUPLE) {
2520 PyObject *tup = PyTuple_New(oparg);
2521 if (tup == NULL)
2522 goto error;
2523 while (--oparg >= 0) {
2524 PyObject *item = POP();
2525 PyTuple_SET_ITEM(tup, oparg, item);
2526 }
2527 PUSH(tup);
2528 DISPATCH();
2529 }
2530
2531 TARGET(BUILD_LIST) {
2532 PyObject *list = PyList_New(oparg);
2533 if (list == NULL)
2534 goto error;
2535 while (--oparg >= 0) {
2536 PyObject *item = POP();
2537 PyList_SET_ITEM(list, oparg, item);
2538 }
2539 PUSH(list);
2540 DISPATCH();
2541 }
2542
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002543 TARGET_WITH_IMPL(BUILD_TUPLE_UNPACK, _build_list_unpack)
2544 TARGET(BUILD_LIST_UNPACK)
2545 _build_list_unpack: {
2546 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
2547 int i;
2548 PyObject *sum = PyList_New(0);
2549 PyObject *return_value;
2550 if (sum == NULL)
2551 goto error;
2552
2553 for (i = oparg; i > 0; i--) {
2554 PyObject *none_val;
2555
2556 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2557 if (none_val == NULL) {
2558 Py_DECREF(sum);
2559 goto error;
2560 }
2561 Py_DECREF(none_val);
2562 }
2563
2564 if (convert_to_tuple) {
2565 return_value = PyList_AsTuple(sum);
2566 Py_DECREF(sum);
2567 if (return_value == NULL)
2568 goto error;
2569 }
2570 else {
2571 return_value = sum;
2572 }
2573
2574 while (oparg--)
2575 Py_DECREF(POP());
2576 PUSH(return_value);
2577 DISPATCH();
2578 }
2579
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002580 TARGET(BUILD_SET) {
2581 PyObject *set = PySet_New(NULL);
2582 int err = 0;
Raymond Hettinger262b6792016-09-08 14:40:36 -07002583 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 if (set == NULL)
2585 goto error;
Raymond Hettinger262b6792016-09-08 14:40:36 -07002586 for (i = oparg; i > 0; i--) {
2587 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002588 if (err == 0)
2589 err = PySet_Add(set, item);
2590 Py_DECREF(item);
2591 }
Raymond Hettinger262b6792016-09-08 14:40:36 -07002592 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 if (err != 0) {
2594 Py_DECREF(set);
2595 goto error;
2596 }
2597 PUSH(set);
2598 DISPATCH();
2599 }
2600
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002601 TARGET(BUILD_SET_UNPACK) {
2602 int i;
2603 PyObject *sum = PySet_New(NULL);
2604 if (sum == NULL)
2605 goto error;
2606
2607 for (i = oparg; i > 0; i--) {
2608 if (_PySet_Update(sum, PEEK(i)) < 0) {
2609 Py_DECREF(sum);
2610 goto error;
2611 }
2612 }
2613
2614 while (oparg--)
2615 Py_DECREF(POP());
2616 PUSH(sum);
2617 DISPATCH();
2618 }
2619
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002620 TARGET(BUILD_MAP) {
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002621 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2623 if (map == NULL)
2624 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002625 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002626 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002627 PyObject *key = PEEK(2*i);
2628 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002629 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002630 if (err != 0) {
2631 Py_DECREF(map);
2632 goto error;
2633 }
2634 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002635
2636 while (oparg--) {
2637 Py_DECREF(POP());
2638 Py_DECREF(POP());
2639 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 PUSH(map);
2641 DISPATCH();
2642 }
2643
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002644 TARGET_WITH_IMPL(BUILD_MAP_UNPACK_WITH_CALL, _build_map_unpack)
2645 TARGET(BUILD_MAP_UNPACK)
2646 _build_map_unpack: {
2647 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
2648 int num_maps;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002649 int i;
2650 PyObject *sum = PyDict_New();
2651 if (sum == NULL)
2652 goto error;
2653 if (with_call) {
2654 num_maps = oparg & 0xff;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002655 }
2656 else {
2657 num_maps = oparg;
2658 }
2659
2660 for (i = num_maps; i > 0; i--) {
2661 PyObject *arg = PEEK(i);
2662 if (with_call) {
2663 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2664
2665 if (intersection == NULL) {
2666 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka208bbd22016-09-22 19:59:46 +03002667 int function_location = (oparg>>8) & 0xff;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002668 PyObject *func = (
2669 PEEK(function_location + num_maps));
2670 PyErr_Format(PyExc_TypeError,
2671 "%.200s%.200s argument after ** "
2672 "must be a mapping, not %.200s",
2673 PyEval_GetFuncName(func),
2674 PyEval_GetFuncDesc(func),
2675 arg->ob_type->tp_name);
2676 }
2677 Py_DECREF(sum);
2678 goto error;
2679 }
2680
2681 if (PySet_GET_SIZE(intersection)) {
2682 Py_ssize_t idx = 0;
2683 PyObject *key;
Serhiy Storchaka208bbd22016-09-22 19:59:46 +03002684 int function_location = (oparg>>8) & 0xff;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002685 PyObject *func = PEEK(function_location + num_maps);
2686 Py_hash_t hash;
2687 _PySet_NextEntry(intersection, &idx, &key, &hash);
2688 if (!PyUnicode_Check(key)) {
2689 PyErr_Format(PyExc_TypeError,
2690 "%.200s%.200s keywords must be strings",
2691 PyEval_GetFuncName(func),
2692 PyEval_GetFuncDesc(func));
2693 } else {
2694 PyErr_Format(PyExc_TypeError,
2695 "%.200s%.200s got multiple "
2696 "values for keyword argument '%U'",
2697 PyEval_GetFuncName(func),
2698 PyEval_GetFuncDesc(func),
2699 key);
2700 }
2701 Py_DECREF(intersection);
2702 Py_DECREF(sum);
2703 goto error;
2704 }
2705 Py_DECREF(intersection);
2706 }
2707
2708 if (PyDict_Update(sum, arg) < 0) {
2709 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2710 PyErr_Format(PyExc_TypeError,
2711 "'%.200s' object is not a mapping",
2712 arg->ob_type->tp_name);
2713 }
2714 Py_DECREF(sum);
2715 goto error;
2716 }
2717 }
2718
2719 while (num_maps--)
2720 Py_DECREF(POP());
2721 PUSH(sum);
2722 DISPATCH();
2723 }
2724
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 TARGET(MAP_ADD) {
2726 PyObject *key = TOP();
2727 PyObject *value = SECOND();
2728 PyObject *map;
2729 int err;
2730 STACKADJ(-2);
2731 map = stack_pointer[-oparg]; /* dict */
2732 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002733 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 Py_DECREF(value);
2735 Py_DECREF(key);
2736 if (err != 0)
2737 goto error;
2738 PREDICT(JUMP_ABSOLUTE);
2739 DISPATCH();
2740 }
2741
2742 TARGET(LOAD_ATTR) {
2743 PyObject *name = GETITEM(names, oparg);
2744 PyObject *owner = TOP();
2745 PyObject *res = PyObject_GetAttr(owner, name);
2746 Py_DECREF(owner);
2747 SET_TOP(res);
2748 if (res == NULL)
2749 goto error;
2750 DISPATCH();
2751 }
2752
2753 TARGET(COMPARE_OP) {
2754 PyObject *right = POP();
2755 PyObject *left = TOP();
2756 PyObject *res = cmp_outcome(oparg, left, right);
2757 Py_DECREF(left);
2758 Py_DECREF(right);
2759 SET_TOP(res);
2760 if (res == NULL)
2761 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 PREDICT(POP_JUMP_IF_FALSE);
2763 PREDICT(POP_JUMP_IF_TRUE);
2764 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002765 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002766
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 TARGET(IMPORT_NAME) {
2768 _Py_IDENTIFIER(__import__);
2769 PyObject *name = GETITEM(names, oparg);
2770 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002771 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002772 if (func == NULL) {
2773 PyErr_SetString(PyExc_ImportError,
2774 "__import__ not found");
2775 goto error;
2776 }
2777 Py_INCREF(func);
2778 from = POP();
2779 level = TOP();
2780 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2781 args = PyTuple_Pack(5,
2782 name,
2783 f->f_globals,
2784 f->f_locals == NULL ?
2785 Py_None : f->f_locals,
2786 from,
2787 level);
2788 else
2789 args = PyTuple_Pack(4,
2790 name,
2791 f->f_globals,
2792 f->f_locals == NULL ?
2793 Py_None : f->f_locals,
2794 from);
2795 Py_DECREF(level);
2796 Py_DECREF(from);
2797 if (args == NULL) {
2798 Py_DECREF(func);
2799 STACKADJ(-1);
2800 goto error;
2801 }
2802 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002803 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 READ_TIMESTAMP(intr1);
2805 Py_DECREF(args);
2806 Py_DECREF(func);
2807 SET_TOP(res);
2808 if (res == NULL)
2809 goto error;
2810 DISPATCH();
2811 }
2812
2813 TARGET(IMPORT_STAR) {
2814 PyObject *from = POP(), *locals;
2815 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002816 if (PyFrame_FastToLocalsWithError(f) < 0)
2817 goto error;
2818
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 locals = f->f_locals;
2820 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 PyErr_SetString(PyExc_SystemError,
2822 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 }
2825 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 READ_TIMESTAMP(intr1);
2828 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 Py_DECREF(from);
2830 if (err != 0)
2831 goto error;
2832 DISPATCH();
2833 }
Guido van Rossum25831651993-05-19 14:50:45 +00002834
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 TARGET(IMPORT_FROM) {
2836 PyObject *name = GETITEM(names, oparg);
2837 PyObject *from = TOP();
2838 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 PUSH(res);
2843 if (res == NULL)
2844 goto error;
2845 DISPATCH();
2846 }
Thomas Wouters52152252000-08-17 22:55:00 +00002847
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002848 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 JUMPBY(oparg);
2850 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002851 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 TARGET(POP_JUMP_IF_FALSE) {
2855 PyObject *cond = POP();
2856 int err;
2857 if (cond == Py_True) {
2858 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 FAST_DISPATCH();
2860 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 if (cond == Py_False) {
2862 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 JUMPTO(oparg);
2864 FAST_DISPATCH();
2865 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 err = PyObject_IsTrue(cond);
2867 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 if (err > 0)
2869 err = 0;
2870 else if (err == 0)
2871 JUMPTO(oparg);
2872 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 TARGET(POP_JUMP_IF_TRUE) {
2879 PyObject *cond = POP();
2880 int err;
2881 if (cond == Py_False) {
2882 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 FAST_DISPATCH();
2884 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 if (cond == Py_True) {
2886 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 JUMPTO(oparg);
2888 FAST_DISPATCH();
2889 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 err = PyObject_IsTrue(cond);
2891 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (err > 0) {
2893 err = 0;
2894 JUMPTO(oparg);
2895 }
2896 else if (err == 0)
2897 ;
2898 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002902
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002903 TARGET(JUMP_IF_FALSE_OR_POP) {
2904 PyObject *cond = TOP();
2905 int err;
2906 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 FAST_DISPATCH();
2910 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 JUMPTO(oparg);
2913 FAST_DISPATCH();
2914 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 if (err > 0) {
2917 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 err = 0;
2920 }
2921 else if (err == 0)
2922 JUMPTO(oparg);
2923 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002926 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002927
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 TARGET(JUMP_IF_TRUE_OR_POP) {
2929 PyObject *cond = TOP();
2930 int err;
2931 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 FAST_DISPATCH();
2935 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 JUMPTO(oparg);
2938 FAST_DISPATCH();
2939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 if (err > 0) {
2942 err = 0;
2943 JUMPTO(oparg);
2944 }
2945 else if (err == 0) {
2946 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 }
2949 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002957#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 /* Enabling this path speeds-up all while and for-loops by bypassing
2959 the per-loop checks for signals. By default, this should be turned-off
2960 because it prevents detection of a control-break in tight loops like
2961 "while 1: pass". Compile with this option turned-on when you need
2962 the speed-up and do not need break checking inside tight loops (ones
2963 that contain only instructions ending with FAST_DISPATCH).
2964 */
2965 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002966#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002968#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002970
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002974 PyObject *iter = PyObject_GetIter(iterable);
2975 Py_DECREF(iterable);
2976 SET_TOP(iter);
2977 if (iter == NULL)
2978 goto error;
2979 PREDICT(FOR_ITER);
2980 DISPATCH();
2981 }
2982
2983 TARGET(GET_YIELD_FROM_ITER) {
2984 /* before: [obj]; after [getiter(obj)] */
2985 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002986 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002987 if (PyCoro_CheckExact(iterable)) {
2988 /* `iterable` is a coroutine */
2989 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2990 /* and it is used in a 'yield from' expression of a
2991 regular generator. */
2992 Py_DECREF(iterable);
2993 SET_TOP(NULL);
2994 PyErr_SetString(PyExc_TypeError,
2995 "cannot 'yield from' a coroutine object "
2996 "in a non-coroutine generator");
2997 goto error;
2998 }
2999 }
3000 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003001 /* `iterable` is not a generator. */
3002 iter = PyObject_GetIter(iterable);
3003 Py_DECREF(iterable);
3004 SET_TOP(iter);
3005 if (iter == NULL)
3006 goto error;
3007 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 DISPATCH();
3009 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 PyObject *iter = TOP();
3015 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3016 if (next != NULL) {
3017 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 PREDICT(STORE_FAST);
3019 PREDICT(UNPACK_SEQUENCE);
3020 DISPATCH();
3021 }
3022 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3024 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003025 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003026 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 PyErr_Clear();
3028 }
3029 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 STACKADJ(-1);
3031 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 JUMPBY(oparg);
3033 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003035
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 why = WHY_BREAK;
3038 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003040
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 if (retval == NULL)
3044 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 why = WHY_CONTINUE;
3046 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
3050 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
3051 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003052 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 /* NOTE: If you add any new block-setup opcodes that
3054 are not try/except/finally handlers, you may need
3055 to update the PyGen_NeedsFinalizing() function.
3056 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3059 STACK_LEVEL());
3060 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003062
Yury Selivanov75445082015-05-11 22:57:16 -04003063 TARGET(BEFORE_ASYNC_WITH) {
3064 _Py_IDENTIFIER(__aexit__);
3065 _Py_IDENTIFIER(__aenter__);
3066
3067 PyObject *mgr = TOP();
3068 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3069 *enter;
3070 PyObject *res;
3071 if (exit == NULL)
3072 goto error;
3073 SET_TOP(exit);
3074 enter = special_lookup(mgr, &PyId___aenter__);
3075 Py_DECREF(mgr);
3076 if (enter == NULL)
3077 goto error;
3078 res = PyObject_CallFunctionObjArgs(enter, NULL);
3079 Py_DECREF(enter);
3080 if (res == NULL)
3081 goto error;
3082 PUSH(res);
3083 DISPATCH();
3084 }
3085
3086 TARGET(SETUP_ASYNC_WITH) {
3087 PyObject *res = POP();
3088 /* Setup the finally block before pushing the result
3089 of __aenter__ on the stack. */
3090 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3091 STACK_LEVEL());
3092 PUSH(res);
3093 DISPATCH();
3094 }
3095
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003097 _Py_IDENTIFIER(__exit__);
3098 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003099 PyObject *mgr = TOP();
3100 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3101 PyObject *res;
3102 if (exit == NULL)
3103 goto error;
3104 SET_TOP(exit);
3105 enter = special_lookup(mgr, &PyId___enter__);
3106 Py_DECREF(mgr);
3107 if (enter == NULL)
3108 goto error;
3109 res = PyObject_CallFunctionObjArgs(enter, NULL);
3110 Py_DECREF(enter);
3111 if (res == NULL)
3112 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003113 /* Setup the finally block before pushing the result
3114 of __enter__ on the stack. */
3115 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3116 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003117
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 DISPATCH();
3120 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003121
Yury Selivanov75445082015-05-11 22:57:16 -04003122 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003123 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003124 how/why we entered the finally clause:
3125 - TOP = None
3126 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3127 - TOP = WHY_*; no retval below it
3128 - (TOP, SECOND, THIRD) = exc_info()
3129 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3130 Below them is EXIT, the context.__exit__ bound method.
3131 In the last case, we must call
3132 EXIT(TOP, SECOND, THIRD)
3133 otherwise we must call
3134 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003135
Benjamin Peterson8f169482013-10-29 22:25:06 -04003136 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003138 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 In addition, if the stack represents an exception,
3142 *and* the function call returns a 'true' value, we
3143 push WHY_SILENCED onto the stack. END_FINALLY will
3144 then not re-raise the exception. (But non-local
3145 gotos should still be resumed.)
3146 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003149 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 (void)POP();
3152 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003153 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 else if (PyLong_Check(exc)) {
3156 STACKADJ(-1);
3157 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 case WHY_RETURN:
3159 case WHY_CONTINUE:
3160 /* Retval in TOP. */
3161 exit_func = SECOND();
3162 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 break;
3165 default:
3166 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 break;
3169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 }
3172 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003173 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003175 val = SECOND();
3176 tb = THIRD();
3177 tp2 = FOURTH();
3178 exc2 = PEEK(5);
3179 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 SET_VALUE(7, tb2);
3182 SET_VALUE(6, exc2);
3183 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3185 SET_FOURTH(NULL);
3186 /* We just shifted the stack down, so we have
3187 to tell the except handler block that the
3188 values are lower than it expects. */
3189 block = &f->f_blockstack[f->f_iblock - 1];
3190 assert(block->b_type == EXCEPT_HANDLER);
3191 block->b_level--;
3192 }
3193 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003194 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003196 if (res == NULL)
3197 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003198
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003199 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003200 PUSH(exc);
3201 PUSH(res);
3202 PREDICT(WITH_CLEANUP_FINISH);
3203 DISPATCH();
3204 }
3205
3206 PREDICTED(WITH_CLEANUP_FINISH);
3207 TARGET(WITH_CLEANUP_FINISH) {
3208 PyObject *res = POP();
3209 PyObject *exc = POP();
3210 int err;
3211
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003212 if (exc != Py_None)
3213 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 else
3215 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003216
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003218 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003219
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003221 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 else if (err > 0) {
3223 err = 0;
3224 /* There was an exception and a True return */
3225 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3226 }
3227 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003228 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 TARGET(CALL_FUNCTION) {
3232 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 PCALL(PCALL_ALL);
3234 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003235#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003236 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003237#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003239#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003241 PUSH(res);
3242 if (res == NULL)
3243 goto error;
3244 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
3248 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
3249 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003250 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 int na = oparg & 0xff;
3252 int nk = (oparg>>8) & 0xff;
3253 int flags = (opcode - CALL_FUNCTION) & 3;
3254 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 PCALL(PCALL_ALL);
3257 if (flags & CALL_FLAG_VAR)
3258 n++;
3259 if (flags & CALL_FLAG_KW)
3260 n++;
3261 pfunc = stack_pointer - n - 1;
3262 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00003265 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003266 PyObject *self = PyMethod_GET_SELF(func);
3267 Py_INCREF(self);
3268 func = PyMethod_GET_FUNCTION(func);
3269 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03003270 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003271 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00003272 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 } else
3274 Py_INCREF(func);
3275 sp = stack_pointer;
3276 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003277 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 READ_TIMESTAMP(intr1);
3279 stack_pointer = sp;
3280 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 PyObject *o = POP();
3284 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003286 PUSH(res);
3287 if (res == NULL)
3288 goto error;
3289 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
3293 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 int posdefaults = oparg & 0xff;
3296 int kwdefaults = (oparg>>8) & 0xff;
3297 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003298
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 PyObject *qualname = POP(); /* qualname */
3300 PyObject *code = POP(); /* code object */
3301 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
3302 Py_DECREF(code);
3303 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003304
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003305 if (func == NULL)
3306 goto error;
3307
3308 if (opcode == MAKE_CLOSURE) {
3309 PyObject *closure = POP();
3310 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 Py_DECREF(func);
3313 Py_DECREF(closure);
3314 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003318
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003319 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003320 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003321 PyObject *names = POP(); /* names of args with annotations */
3322 PyObject *anns = PyDict_New();
3323 if (anns == NULL) {
3324 Py_DECREF(func);
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003325 Py_DECREF(names);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003326 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003328 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 assert(num_annotations == name_ix+1);
3330 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003331 PyObject *name, *value;
3332 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003334 name = PyTuple_GET_ITEM(names, name_ix);
3335 value = POP();
3336 err = PyDict_SetItem(anns, name, value);
3337 Py_DECREF(value);
3338 if (err != 0) {
3339 Py_DECREF(anns);
3340 Py_DECREF(func);
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003341 Py_DECREF(names);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 goto error;
3343 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 }
Benjamin Petersonad887cf2016-05-16 22:52:40 -07003345 Py_DECREF(names);
Neal Norwitzc1505362006-12-28 06:47:50 +00003346
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 /* Can't happen unless
3349 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 Py_DECREF(anns);
3351 Py_DECREF(func);
3352 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003354 Py_DECREF(anns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003357 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003358 if (kwdefaults > 0) {
3359 PyObject *defs = PyDict_New();
3360 if (defs == NULL) {
3361 Py_DECREF(func);
3362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003363 }
3364 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003365 PyObject *v = POP(); /* default value */
3366 PyObject *key = POP(); /* kw only arg name */
3367 int err = PyDict_SetItem(defs, key, v);
3368 Py_DECREF(v);
3369 Py_DECREF(key);
3370 if (err != 0) {
3371 Py_DECREF(defs);
3372 Py_DECREF(func);
3373 goto error;
3374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 /* Can't happen unless
3378 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003379 Py_DECREF(func);
3380 Py_DECREF(defs);
3381 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003383 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05003385 if (posdefaults > 0) {
3386 PyObject *defs = PyTuple_New(posdefaults);
3387 if (defs == NULL) {
3388 Py_DECREF(func);
3389 goto error;
3390 }
3391 while (--posdefaults >= 0)
3392 PyTuple_SET_ITEM(defs, posdefaults, POP());
3393 if (PyFunction_SetDefaults(func, defs) != 0) {
3394 /* Can't happen unless
3395 PyFunction_SetDefaults changes. */
3396 Py_DECREF(defs);
3397 Py_DECREF(func);
3398 goto error;
3399 }
3400 Py_DECREF(defs);
3401 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 PUSH(func);
3403 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 TARGET(BUILD_SLICE) {
3407 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003408 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003409 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 step = NULL;
3412 stop = POP();
3413 start = TOP();
3414 slice = PySlice_New(start, stop, step);
3415 Py_DECREF(start);
3416 Py_DECREF(stop);
3417 Py_XDECREF(step);
3418 SET_TOP(slice);
3419 if (slice == NULL)
3420 goto error;
3421 DISPATCH();
3422 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003423
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003424 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 opcode = NEXTOP();
3426 oparg = oparg<<16 | NEXTARG();
3427 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003429
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003430
Antoine Pitrou042b1282010-08-13 21:15:58 +00003431#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003433#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 default:
3435 fprintf(stderr,
3436 "XXX lineno: %d, opcode: %d\n",
3437 PyFrame_GetLineNumber(f),
3438 opcode);
3439 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003440 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003441
3442#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003444#endif
3445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003447
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003448 /* This should never be reached. Every opcode should end with DISPATCH()
3449 or goto error. */
3450 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003451
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003454
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455 assert(why == WHY_NOT);
3456 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003457
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003458 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003459#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003460 if (!PyErr_Occurred())
3461 PyErr_SetString(PyExc_SystemError,
3462 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003463#else
3464 assert(PyErr_Occurred());
3465#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003466
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003467 /* Log traceback info. */
3468 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003469
Benjamin Peterson51f46162013-01-23 08:38:47 -05003470 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003471 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3472 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003473
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003474fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003475 assert(why != WHY_NOT);
3476
3477 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 while (why != WHY_NOT && f->f_iblock > 0) {
3479 /* Peek at the current block. */
3480 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 assert(why != WHY_YIELD);
3483 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3484 why = WHY_NOT;
3485 JUMPTO(PyLong_AS_LONG(retval));
3486 Py_DECREF(retval);
3487 break;
3488 }
3489 /* Now we have to pop the block. */
3490 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003492 if (b->b_type == EXCEPT_HANDLER) {
3493 UNWIND_EXCEPT_HANDLER(b);
3494 continue;
3495 }
3496 UNWIND_BLOCK(b);
3497 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3498 why = WHY_NOT;
3499 JUMPTO(b->b_handler);
3500 break;
3501 }
3502 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3503 || b->b_type == SETUP_FINALLY)) {
3504 PyObject *exc, *val, *tb;
3505 int handler = b->b_handler;
3506 /* Beware, this invalidates all b->b_* fields */
3507 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3508 PUSH(tstate->exc_traceback);
3509 PUSH(tstate->exc_value);
3510 if (tstate->exc_type != NULL) {
3511 PUSH(tstate->exc_type);
3512 }
3513 else {
3514 Py_INCREF(Py_None);
3515 PUSH(Py_None);
3516 }
3517 PyErr_Fetch(&exc, &val, &tb);
3518 /* Make the raw exception data
3519 available to the handler,
3520 so a program can emulate the
3521 Python main loop. */
3522 PyErr_NormalizeException(
3523 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003524 if (tb != NULL)
3525 PyException_SetTraceback(val, tb);
3526 else
3527 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 Py_INCREF(exc);
3529 tstate->exc_type = exc;
3530 Py_INCREF(val);
3531 tstate->exc_value = val;
3532 tstate->exc_traceback = tb;
3533 if (tb == NULL)
3534 tb = Py_None;
3535 Py_INCREF(tb);
3536 PUSH(tb);
3537 PUSH(val);
3538 PUSH(exc);
3539 why = WHY_NOT;
3540 JUMPTO(handler);
3541 break;
3542 }
3543 if (b->b_type == SETUP_FINALLY) {
3544 if (why & (WHY_RETURN | WHY_CONTINUE))
3545 PUSH(retval);
3546 PUSH(PyLong_FromLong((long)why));
3547 why = WHY_NOT;
3548 JUMPTO(b->b_handler);
3549 break;
3550 }
3551 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 if (why != WHY_NOT)
3556 break;
3557 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003558
Victor Stinnerace47d72013-07-18 01:41:08 +02003559 assert(!PyErr_Occurred());
3560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 assert(why != WHY_YIELD);
3564 /* Pop remaining stack entries. */
3565 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003566 PyObject *o = POP();
3567 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 if (why != WHY_RETURN)
3571 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003572
Victor Stinner4a7cc882015-03-06 23:35:27 +01003573 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003574
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003575fast_yield:
Yury Selivanov5376ba92015-06-22 12:19:30 -04003576 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003577
Benjamin Petersonac913412011-07-03 16:25:11 -05003578 /* The purpose of this block is to put aside the generator's exception
3579 state and restore that of the calling frame. If the current
3580 exception state is from the caller, we clear the exception values
3581 on the generator frame, so they are not swapped back in latter. The
3582 origin of the current exception state is determined by checking for
3583 except handler blocks, which we must be in iff a new exception
3584 state came into existence in this frame. (An uncaught exception
3585 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3586 int i;
3587 for (i = 0; i < f->f_iblock; i++)
3588 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3589 break;
3590 if (i == f->f_iblock)
3591 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003592 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003593 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003594 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003595 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003598 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003600 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3601 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003603 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 why = WHY_EXCEPTION;
3605 }
3606 }
3607 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003608 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3609 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 PyTrace_RETURN, NULL);
3611 }
3612 }
3613 if (tstate->c_profilefunc) {
3614 if (why == WHY_EXCEPTION)
3615 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003616 tstate->c_profileobj,
3617 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003619 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3620 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003622 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003623 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 }
3625 }
3626 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003628 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003629exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003630 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003631 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003633
Victor Stinnerefde1462015-03-21 15:04:43 +01003634 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003635}
3636
Benjamin Petersonb204a422011-06-05 22:04:07 -05003637static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003638format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3639{
3640 int err;
3641 Py_ssize_t len = PyList_GET_SIZE(names);
3642 PyObject *name_str, *comma, *tail, *tmp;
3643
3644 assert(PyList_CheckExact(names));
3645 assert(len >= 1);
3646 /* Deal with the joys of natural language. */
3647 switch (len) {
3648 case 1:
3649 name_str = PyList_GET_ITEM(names, 0);
3650 Py_INCREF(name_str);
3651 break;
3652 case 2:
3653 name_str = PyUnicode_FromFormat("%U and %U",
3654 PyList_GET_ITEM(names, len - 2),
3655 PyList_GET_ITEM(names, len - 1));
3656 break;
3657 default:
3658 tail = PyUnicode_FromFormat(", %U, and %U",
3659 PyList_GET_ITEM(names, len - 2),
3660 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003661 if (tail == NULL)
3662 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003663 /* Chop off the last two objects in the list. This shouldn't actually
3664 fail, but we can't be too careful. */
3665 err = PyList_SetSlice(names, len - 2, len, NULL);
3666 if (err == -1) {
3667 Py_DECREF(tail);
3668 return;
3669 }
3670 /* Stitch everything up into a nice comma-separated list. */
3671 comma = PyUnicode_FromString(", ");
3672 if (comma == NULL) {
3673 Py_DECREF(tail);
3674 return;
3675 }
3676 tmp = PyUnicode_Join(comma, names);
3677 Py_DECREF(comma);
3678 if (tmp == NULL) {
3679 Py_DECREF(tail);
3680 return;
3681 }
3682 name_str = PyUnicode_Concat(tmp, tail);
3683 Py_DECREF(tmp);
3684 Py_DECREF(tail);
3685 break;
3686 }
3687 if (name_str == NULL)
3688 return;
3689 PyErr_Format(PyExc_TypeError,
3690 "%U() missing %i required %s argument%s: %U",
3691 co->co_name,
3692 len,
3693 kind,
3694 len == 1 ? "" : "s",
3695 name_str);
3696 Py_DECREF(name_str);
3697}
3698
3699static void
3700missing_arguments(PyCodeObject *co, int missing, int defcount,
3701 PyObject **fastlocals)
3702{
3703 int i, j = 0;
3704 int start, end;
3705 int positional = defcount != -1;
3706 const char *kind = positional ? "positional" : "keyword-only";
3707 PyObject *missing_names;
3708
3709 /* Compute the names of the arguments that are missing. */
3710 missing_names = PyList_New(missing);
3711 if (missing_names == NULL)
3712 return;
3713 if (positional) {
3714 start = 0;
3715 end = co->co_argcount - defcount;
3716 }
3717 else {
3718 start = co->co_argcount;
3719 end = start + co->co_kwonlyargcount;
3720 }
3721 for (i = start; i < end; i++) {
3722 if (GETLOCAL(i) == NULL) {
3723 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3724 PyObject *name = PyObject_Repr(raw);
3725 if (name == NULL) {
3726 Py_DECREF(missing_names);
3727 return;
3728 }
3729 PyList_SET_ITEM(missing_names, j++, name);
3730 }
3731 }
3732 assert(j == missing);
3733 format_missing(kind, co, missing_names);
3734 Py_DECREF(missing_names);
3735}
3736
3737static void
3738too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003739{
3740 int plural;
3741 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003742 int i;
3743 PyObject *sig, *kwonly_sig;
3744
Benjamin Petersone109c702011-06-24 09:37:26 -05003745 assert((co->co_flags & CO_VARARGS) == 0);
3746 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003748 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003749 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003750 if (defcount) {
3751 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003752 plural = 1;
3753 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3754 }
3755 else {
3756 plural = co->co_argcount != 1;
3757 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3758 }
3759 if (sig == NULL)
3760 return;
3761 if (kwonly_given) {
3762 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3763 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3764 kwonly_given != 1 ? "s" : "");
3765 if (kwonly_sig == NULL) {
3766 Py_DECREF(sig);
3767 return;
3768 }
3769 }
3770 else {
3771 /* This will not fail. */
3772 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003773 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 }
3775 PyErr_Format(PyExc_TypeError,
3776 "%U() takes %U positional argument%s but %d%U %s given",
3777 co->co_name,
3778 sig,
3779 plural ? "s" : "",
3780 given,
3781 kwonly_sig,
3782 given == 1 && !kwonly_given ? "was" : "were");
3783 Py_DECREF(sig);
3784 Py_DECREF(kwonly_sig);
3785}
3786
Guido van Rossumc2e20742006-02-27 22:32:47 +00003787/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003788 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003789 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003790
Victor Stinner40ee3012014-06-16 15:59:28 +02003791static PyObject *
3792_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003794 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3795 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003796{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003797 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003798 PyFrameObject *f;
3799 PyObject *retval = NULL;
3800 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 PyThreadState *tstate = PyThreadState_GET();
3802 PyObject *x, *u;
3803 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003804 int i;
3805 int n = argcount;
3806 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 if (globals == NULL) {
3809 PyErr_SetString(PyExc_SystemError,
3810 "PyEval_EvalCodeEx: NULL globals");
3811 return NULL;
3812 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 assert(tstate != NULL);
3815 assert(globals != NULL);
3816 f = PyFrame_New(tstate, co, globals, locals);
3817 if (f == NULL)
3818 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 fastlocals = f->f_localsplus;
3821 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003822
Benjamin Petersonb204a422011-06-05 22:04:07 -05003823 /* Parse arguments. */
3824 if (co->co_flags & CO_VARKEYWORDS) {
3825 kwdict = PyDict_New();
3826 if (kwdict == NULL)
3827 goto fail;
3828 i = total_args;
3829 if (co->co_flags & CO_VARARGS)
3830 i++;
3831 SETLOCAL(i, kwdict);
3832 }
3833 if (argcount > co->co_argcount)
3834 n = co->co_argcount;
3835 for (i = 0; i < n; i++) {
3836 x = args[i];
3837 Py_INCREF(x);
3838 SETLOCAL(i, x);
3839 }
3840 if (co->co_flags & CO_VARARGS) {
3841 u = PyTuple_New(argcount - n);
3842 if (u == NULL)
3843 goto fail;
3844 SETLOCAL(total_args, u);
3845 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 x = args[i];
3847 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 }
3851 for (i = 0; i < kwcount; i++) {
3852 PyObject **co_varnames;
3853 PyObject *keyword = kws[2*i];
3854 PyObject *value = kws[2*i + 1];
3855 int j;
3856 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3857 PyErr_Format(PyExc_TypeError,
3858 "%U() keywords must be strings",
3859 co->co_name);
3860 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 /* Speed hack: do raw pointer compares. As names are
3863 normally interned this should almost always hit. */
3864 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3865 for (j = 0; j < total_args; j++) {
3866 PyObject *nm = co_varnames[j];
3867 if (nm == keyword)
3868 goto kw_found;
3869 }
3870 /* Slow fallback, just in case */
3871 for (j = 0; j < total_args; j++) {
3872 PyObject *nm = co_varnames[j];
3873 int cmp = PyObject_RichCompareBool(
3874 keyword, nm, Py_EQ);
3875 if (cmp > 0)
3876 goto kw_found;
3877 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003879 }
3880 if (j >= total_args && kwdict == NULL) {
3881 PyErr_Format(PyExc_TypeError,
3882 "%U() got an unexpected "
3883 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 co->co_name,
3885 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003886 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003888 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3889 goto fail;
3890 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003891 continue;
3892 kw_found:
3893 if (GETLOCAL(j) != NULL) {
3894 PyErr_Format(PyExc_TypeError,
3895 "%U() got multiple "
3896 "values for argument '%S'",
3897 co->co_name,
3898 keyword);
3899 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003901 Py_INCREF(value);
3902 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003905 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 goto fail;
3907 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003908 if (argcount < co->co_argcount) {
3909 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003910 int missing = 0;
3911 for (i = argcount; i < m; i++)
3912 if (GETLOCAL(i) == NULL)
3913 missing++;
3914 if (missing) {
3915 missing_arguments(co, missing, defcount, fastlocals);
3916 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917 }
3918 if (n > m)
3919 i = n - m;
3920 else
3921 i = 0;
3922 for (; i < defcount; i++) {
3923 if (GETLOCAL(m+i) == NULL) {
3924 PyObject *def = defs[i];
3925 Py_INCREF(def);
3926 SETLOCAL(m+i, def);
3927 }
3928 }
3929 }
3930 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003931 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003932 for (i = co->co_argcount; i < total_args; i++) {
3933 PyObject *name;
3934 if (GETLOCAL(i) != NULL)
3935 continue;
3936 name = PyTuple_GET_ITEM(co->co_varnames, i);
3937 if (kwdefs != NULL) {
3938 PyObject *def = PyDict_GetItem(kwdefs, name);
3939 if (def) {
3940 Py_INCREF(def);
3941 SETLOCAL(i, def);
3942 continue;
3943 }
3944 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003945 missing++;
3946 }
3947 if (missing) {
3948 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003949 goto fail;
3950 }
3951 }
3952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003954 vars into frame. */
3955 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003957 int arg;
3958 /* Possibly account for the cell variable being an argument. */
3959 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003960 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003961 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003962 /* Clear the local copy. */
3963 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003964 }
3965 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003966 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003967 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003968 if (c == NULL)
3969 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003970 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003972 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3973 PyObject *o = PyTuple_GET_ITEM(closure, i);
3974 Py_INCREF(o);
3975 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003977
Yury Selivanov5376ba92015-06-22 12:19:30 -04003978 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003979 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003980 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003981 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003982
3983 if (is_coro && tstate->in_coroutine_wrapper) {
3984 assert(coro_wrapper != NULL);
3985 PyErr_Format(PyExc_RuntimeError,
3986 "coroutine wrapper %.200R attempted "
3987 "to recursively wrap %.200R",
3988 coro_wrapper,
3989 co);
3990 goto fail;
3991 }
Yury Selivanov75445082015-05-11 22:57:16 -04003992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 /* Don't need to keep the reference to f_back, it will be set
3994 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003995 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003997 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003999 /* Create a new generator that owns the ready to run frame
4000 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004001 if (is_coro) {
4002 gen = PyCoro_New(f, name, qualname);
4003 } else {
4004 gen = PyGen_NewWithQualName(f, name, qualname);
4005 }
Yury Selivanov75445082015-05-11 22:57:16 -04004006 if (gen == NULL)
4007 return NULL;
4008
Yury Selivanov94c22632015-06-04 10:16:51 -04004009 if (is_coro && coro_wrapper != NULL) {
4010 PyObject *wrapped;
4011 tstate->in_coroutine_wrapper = 1;
4012 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4013 tstate->in_coroutine_wrapper = 0;
4014 return wrapped;
4015 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004016
Yury Selivanov75445082015-05-11 22:57:16 -04004017 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004021
Thomas Woutersce272b62007-09-19 21:19:28 +00004022fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 /* decref'ing the frame can cause __del__ methods to get invoked,
4025 which can call back into Python. While we're done with the
4026 current Python frame (f), the associated C stack is still in use,
4027 so recursion_depth must be boosted for the duration.
4028 */
4029 assert(tstate != NULL);
4030 ++tstate->recursion_depth;
4031 Py_DECREF(f);
4032 --tstate->recursion_depth;
4033 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004034}
4035
Victor Stinner40ee3012014-06-16 15:59:28 +02004036PyObject *
4037PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4038 PyObject **args, int argcount, PyObject **kws, int kwcount,
4039 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4040{
4041 return _PyEval_EvalCodeWithName(_co, globals, locals,
4042 args, argcount, kws, kwcount,
4043 defs, defcount, kwdefs, closure,
4044 NULL, NULL);
4045}
Tim Peters5ca576e2001-06-18 22:08:13 +00004046
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004047static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004048special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004051 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004053 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 return NULL;
4055 }
4056 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004057}
4058
4059
Benjamin Peterson87880242011-07-03 16:48:31 -05004060/* These 3 functions deal with the exception state of generators. */
4061
4062static void
4063save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4064{
4065 PyObject *type, *value, *traceback;
4066 Py_XINCREF(tstate->exc_type);
4067 Py_XINCREF(tstate->exc_value);
4068 Py_XINCREF(tstate->exc_traceback);
4069 type = f->f_exc_type;
4070 value = f->f_exc_value;
4071 traceback = f->f_exc_traceback;
4072 f->f_exc_type = tstate->exc_type;
4073 f->f_exc_value = tstate->exc_value;
4074 f->f_exc_traceback = tstate->exc_traceback;
4075 Py_XDECREF(type);
4076 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004077 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004078}
4079
4080static void
4081swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4082{
4083 PyObject *tmp;
4084 tmp = tstate->exc_type;
4085 tstate->exc_type = f->f_exc_type;
4086 f->f_exc_type = tmp;
4087 tmp = tstate->exc_value;
4088 tstate->exc_value = f->f_exc_value;
4089 f->f_exc_value = tmp;
4090 tmp = tstate->exc_traceback;
4091 tstate->exc_traceback = f->f_exc_traceback;
4092 f->f_exc_traceback = tmp;
4093}
4094
4095static void
4096restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4097{
4098 PyObject *type, *value, *tb;
4099 type = tstate->exc_type;
4100 value = tstate->exc_value;
4101 tb = tstate->exc_traceback;
4102 tstate->exc_type = f->f_exc_type;
4103 tstate->exc_value = f->f_exc_value;
4104 tstate->exc_traceback = f->f_exc_traceback;
4105 f->f_exc_type = NULL;
4106 f->f_exc_value = NULL;
4107 f->f_exc_traceback = NULL;
4108 Py_XDECREF(type);
4109 Py_XDECREF(value);
4110 Py_XDECREF(tb);
4111}
4112
4113
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004114/* Logic for the raise statement (too complicated for inlining).
4115 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004116static int
Collin Winter828f04a2007-08-31 00:04:24 +00004117do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 if (exc == NULL) {
4122 /* Reraise */
4123 PyThreadState *tstate = PyThreadState_GET();
4124 PyObject *tb;
4125 type = tstate->exc_type;
4126 value = tstate->exc_value;
4127 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004128 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 PyErr_SetString(PyExc_RuntimeError,
4130 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004131 return 0;
4132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 Py_XINCREF(type);
4134 Py_XINCREF(value);
4135 Py_XINCREF(tb);
4136 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004137 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 /* We support the following forms of raise:
4141 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004142 raise <instance>
4143 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 if (PyExceptionClass_Check(exc)) {
4146 type = exc;
4147 value = PyObject_CallObject(exc, NULL);
4148 if (value == NULL)
4149 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004150 if (!PyExceptionInstance_Check(value)) {
4151 PyErr_Format(PyExc_TypeError,
4152 "calling %R should have returned an instance of "
4153 "BaseException, not %R",
4154 type, Py_TYPE(value));
4155 goto raise_error;
4156 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 }
4158 else if (PyExceptionInstance_Check(exc)) {
4159 value = exc;
4160 type = PyExceptionInstance_Class(exc);
4161 Py_INCREF(type);
4162 }
4163 else {
4164 /* Not something you can raise. You get an exception
4165 anyway, just not what you specified :-) */
4166 Py_DECREF(exc);
4167 PyErr_SetString(PyExc_TypeError,
4168 "exceptions must derive from BaseException");
4169 goto raise_error;
4170 }
Collin Winter828f04a2007-08-31 00:04:24 +00004171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 if (cause) {
4173 PyObject *fixed_cause;
4174 if (PyExceptionClass_Check(cause)) {
4175 fixed_cause = PyObject_CallObject(cause, NULL);
4176 if (fixed_cause == NULL)
4177 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004178 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004180 else if (PyExceptionInstance_Check(cause)) {
4181 fixed_cause = cause;
4182 }
4183 else if (cause == Py_None) {
4184 Py_DECREF(cause);
4185 fixed_cause = NULL;
4186 }
4187 else {
4188 PyErr_SetString(PyExc_TypeError,
4189 "exception causes must derive from "
4190 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 goto raise_error;
4192 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004193 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 }
Collin Winter828f04a2007-08-31 00:04:24 +00004195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 PyErr_SetObject(type, value);
4197 /* PyErr_SetObject incref's its arguments */
4198 Py_XDECREF(value);
4199 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004200 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004201
4202raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004203 Py_XDECREF(value);
4204 Py_XDECREF(type);
4205 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004206 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004207}
4208
Tim Petersd6d010b2001-06-21 02:49:55 +00004209/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004210 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004211
Guido van Rossum0368b722007-05-11 16:50:42 +00004212 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4213 with a variable target.
4214*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004215
Barry Warsawe42b18f1997-08-25 22:13:04 +00004216static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004217unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 int i = 0, j = 0;
4220 Py_ssize_t ll = 0;
4221 PyObject *it; /* iter(v) */
4222 PyObject *w;
4223 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 it = PyObject_GetIter(v);
4228 if (it == NULL)
4229 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 for (; i < argcnt; i++) {
4232 w = PyIter_Next(it);
4233 if (w == NULL) {
4234 /* Iterator done, via error or exhaustion. */
4235 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004236 if (argcntafter == -1) {
4237 PyErr_Format(PyExc_ValueError,
4238 "not enough values to unpack (expected %d, got %d)",
4239 argcnt, i);
4240 }
4241 else {
4242 PyErr_Format(PyExc_ValueError,
4243 "not enough values to unpack "
4244 "(expected at least %d, got %d)",
4245 argcnt + argcntafter, i);
4246 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 }
4248 goto Error;
4249 }
4250 *--sp = w;
4251 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 if (argcntafter == -1) {
4254 /* We better have exhausted the iterator now. */
4255 w = PyIter_Next(it);
4256 if (w == NULL) {
4257 if (PyErr_Occurred())
4258 goto Error;
4259 Py_DECREF(it);
4260 return 1;
4261 }
4262 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004263 PyErr_Format(PyExc_ValueError,
4264 "too many values to unpack (expected %d)",
4265 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 goto Error;
4267 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004268
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 l = PySequence_List(it);
4270 if (l == NULL)
4271 goto Error;
4272 *--sp = l;
4273 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 ll = PyList_GET_SIZE(l);
4276 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004277 PyErr_Format(PyExc_ValueError,
4278 "not enough values to unpack (expected at least %d, got %zd)",
4279 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 goto Error;
4281 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 /* Pop the "after-variable" args off the list. */
4284 for (j = argcntafter; j > 0; j--, i++) {
4285 *--sp = PyList_GET_ITEM(l, ll - j);
4286 }
4287 /* Resize the list. */
4288 Py_SIZE(l) = ll - argcntafter;
4289 Py_DECREF(it);
4290 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004291
Tim Petersd6d010b2001-06-21 02:49:55 +00004292Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 for (; i > 0; i--, sp++)
4294 Py_DECREF(*sp);
4295 Py_XDECREF(it);
4296 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004297}
4298
4299
Guido van Rossum96a42c81992-01-12 02:29:51 +00004300#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004301static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004302prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004303{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 printf("%s ", str);
4305 if (PyObject_Print(v, stdout, 0) != 0)
4306 PyErr_Clear(); /* Don't know what else to do */
4307 printf("\n");
4308 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004309}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004310#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004311
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004312static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004313call_exc_trace(Py_tracefunc func, PyObject *self,
4314 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004315{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004316 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004318 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004319 if (value == NULL) {
4320 value = Py_None;
4321 Py_INCREF(value);
4322 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004323 PyErr_NormalizeException(&type, &value, &orig_traceback);
4324 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 arg = PyTuple_Pack(3, type, value, traceback);
4326 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004327 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 return;
4329 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004330 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 Py_DECREF(arg);
4332 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004333 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 else {
4335 Py_XDECREF(type);
4336 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004337 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004339}
4340
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004341static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004342call_trace_protected(Py_tracefunc func, PyObject *obj,
4343 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004346 PyObject *type, *value, *traceback;
4347 int err;
4348 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004349 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 if (err == 0)
4351 {
4352 PyErr_Restore(type, value, traceback);
4353 return 0;
4354 }
4355 else {
4356 Py_XDECREF(type);
4357 Py_XDECREF(value);
4358 Py_XDECREF(traceback);
4359 return -1;
4360 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004361}
4362
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004363static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004364call_trace(Py_tracefunc func, PyObject *obj,
4365 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 int result;
4369 if (tstate->tracing)
4370 return 0;
4371 tstate->tracing++;
4372 tstate->use_tracing = 0;
4373 result = func(obj, frame, what, arg);
4374 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4375 || (tstate->c_profilefunc != NULL));
4376 tstate->tracing--;
4377 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004378}
4379
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004380PyObject *
4381_PyEval_CallTracing(PyObject *func, PyObject *args)
4382{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004383 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 int save_tracing = tstate->tracing;
4385 int save_use_tracing = tstate->use_tracing;
4386 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 tstate->tracing = 0;
4389 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4390 || (tstate->c_profilefunc != NULL));
4391 result = PyObject_Call(func, args, NULL);
4392 tstate->tracing = save_tracing;
4393 tstate->use_tracing = save_use_tracing;
4394 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004395}
4396
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004397/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004398static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004399maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004400 PyThreadState *tstate, PyFrameObject *frame,
4401 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 int result = 0;
4404 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 /* If the last instruction executed isn't in the current
4407 instruction window, reset the window.
4408 */
4409 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4410 PyAddrPair bounds;
4411 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4412 &bounds);
4413 *instr_lb = bounds.ap_lower;
4414 *instr_ub = bounds.ap_upper;
4415 }
4416 /* If the last instruction falls at the start of a line or if
4417 it represents a jump backwards, update the frame's line
4418 number and call the trace function. */
4419 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4420 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004421 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 }
4423 *instr_prev = frame->f_lasti;
4424 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004425}
4426
Fred Drake5755ce62001-06-27 19:19:46 +00004427void
4428PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 PyThreadState *tstate = PyThreadState_GET();
4431 PyObject *temp = tstate->c_profileobj;
4432 Py_XINCREF(arg);
4433 tstate->c_profilefunc = NULL;
4434 tstate->c_profileobj = NULL;
4435 /* Must make sure that tracing is not ignored if 'temp' is freed */
4436 tstate->use_tracing = tstate->c_tracefunc != NULL;
4437 Py_XDECREF(temp);
4438 tstate->c_profilefunc = func;
4439 tstate->c_profileobj = arg;
4440 /* Flag that tracing or profiling is turned on */
4441 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004442}
4443
4444void
4445PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 PyThreadState *tstate = PyThreadState_GET();
4448 PyObject *temp = tstate->c_traceobj;
4449 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4450 Py_XINCREF(arg);
4451 tstate->c_tracefunc = NULL;
4452 tstate->c_traceobj = NULL;
4453 /* Must make sure that profiling is not ignored if 'temp' is freed */
4454 tstate->use_tracing = tstate->c_profilefunc != NULL;
4455 Py_XDECREF(temp);
4456 tstate->c_tracefunc = func;
4457 tstate->c_traceobj = arg;
4458 /* Flag that tracing or profiling is turned on */
4459 tstate->use_tracing = ((func != NULL)
4460 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004461}
4462
Yury Selivanov75445082015-05-11 22:57:16 -04004463void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004464_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004465{
4466 PyThreadState *tstate = PyThreadState_GET();
4467
Yury Selivanov75445082015-05-11 22:57:16 -04004468 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004469 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004470}
4471
4472PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004473_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004474{
4475 PyThreadState *tstate = PyThreadState_GET();
4476 return tstate->coroutine_wrapper;
4477}
4478
Guido van Rossumb209a111997-04-29 18:18:01 +00004479PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004480PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004481{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 PyFrameObject *current_frame = PyEval_GetFrame();
4483 if (current_frame == NULL)
4484 return PyThreadState_GET()->interp->builtins;
4485 else
4486 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004487}
4488
Guido van Rossumb209a111997-04-29 18:18:01 +00004489PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004490PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004493 if (current_frame == NULL) {
4494 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004496 }
4497
4498 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4499 return NULL;
4500
4501 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004503}
4504
Guido van Rossumb209a111997-04-29 18:18:01 +00004505PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004506PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 PyFrameObject *current_frame = PyEval_GetFrame();
4509 if (current_frame == NULL)
4510 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004511
4512 assert(current_frame->f_globals != NULL);
4513 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004514}
4515
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004516PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004517PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 PyThreadState *tstate = PyThreadState_GET();
4520 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004521}
4522
Guido van Rossum6135a871995-01-09 17:53:26 +00004523int
Tim Peters5ba58662001-07-16 02:29:45 +00004524PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 PyFrameObject *current_frame = PyEval_GetFrame();
4527 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 if (current_frame != NULL) {
4530 const int codeflags = current_frame->f_code->co_flags;
4531 const int compilerflags = codeflags & PyCF_MASK;
4532 if (compilerflags) {
4533 result = 1;
4534 cf->cf_flags |= compilerflags;
4535 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004536#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 if (codeflags & CO_GENERATOR_ALLOWED) {
4538 result = 1;
4539 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4540 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004541#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004542 }
4543 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004544}
4545
Guido van Rossum3f5da241990-12-20 15:06:42 +00004546
Guido van Rossum681d79a1995-07-18 14:51:37 +00004547/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004548 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004549
Guido van Rossumb209a111997-04-29 18:18:01 +00004550PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004551PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004554
Victor Stinner59b356d2015-03-16 11:52:32 +01004555#ifdef Py_DEBUG
4556 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4557 set. It raises a new exception if parameters are invalid or if
4558 PyTuple_New() fails, and so the original exception is lost. */
4559 assert(!PyErr_Occurred());
4560#endif
4561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 if (arg == NULL) {
4563 arg = PyTuple_New(0);
4564 if (arg == NULL)
4565 return NULL;
4566 }
4567 else if (!PyTuple_Check(arg)) {
4568 PyErr_SetString(PyExc_TypeError,
4569 "argument list must be a tuple");
4570 return NULL;
4571 }
4572 else
4573 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 if (kw != NULL && !PyDict_Check(kw)) {
4576 PyErr_SetString(PyExc_TypeError,
4577 "keyword list must be a dictionary");
4578 Py_DECREF(arg);
4579 return NULL;
4580 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 result = PyObject_Call(func, arg, kw);
4583 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004586}
4587
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004588const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004589PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 if (PyMethod_Check(func))
4592 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4593 else if (PyFunction_Check(func))
4594 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4595 else if (PyCFunction_Check(func))
4596 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4597 else
4598 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004599}
4600
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004601const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004602PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004604 if (PyMethod_Check(func))
4605 return "()";
4606 else if (PyFunction_Check(func))
4607 return "()";
4608 else if (PyCFunction_Check(func))
4609 return "()";
4610 else
4611 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004612}
4613
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004614static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004615err_args(PyObject *func, int flags, int nargs)
4616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 if (flags & METH_NOARGS)
4618 PyErr_Format(PyExc_TypeError,
4619 "%.200s() takes no arguments (%d given)",
4620 ((PyCFunctionObject *)func)->m_ml->ml_name,
4621 nargs);
4622 else
4623 PyErr_Format(PyExc_TypeError,
4624 "%.200s() takes exactly one argument (%d given)",
4625 ((PyCFunctionObject *)func)->m_ml->ml_name,
4626 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004627}
4628
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004629#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004630if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004631 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4632 tstate, tstate->frame, \
4633 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 x = NULL; \
4635 } \
4636 else { \
4637 x = call; \
4638 if (tstate->c_profilefunc != NULL) { \
4639 if (x == NULL) { \
4640 call_trace_protected(tstate->c_profilefunc, \
4641 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004642 tstate, tstate->frame, \
4643 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 /* XXX should pass (type, value, tb) */ \
4645 } else { \
4646 if (call_trace(tstate->c_profilefunc, \
4647 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004648 tstate, tstate->frame, \
4649 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 Py_DECREF(x); \
4651 x = NULL; \
4652 } \
4653 } \
4654 } \
4655 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004656} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004657 x = call; \
4658 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004659
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004660static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004661call_function(PyObject ***pp_stack, int oparg
4662#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004664#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 int na = oparg & 0xff;
4668 int nk = (oparg>>8) & 0xff;
4669 int n = na + 2 * nk;
4670 PyObject **pfunc = (*pp_stack) - n - 1;
4671 PyObject *func = *pfunc;
4672 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 /* Always dispatch PyCFunction first, because these are
4675 presumed to be the most frequent callable object.
4676 */
4677 if (PyCFunction_Check(func) && nk == 0) {
4678 int flags = PyCFunction_GET_FLAGS(func);
4679 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 PCALL(PCALL_CFUNCTION);
4682 if (flags & (METH_NOARGS | METH_O)) {
4683 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4684 PyObject *self = PyCFunction_GET_SELF(func);
4685 if (flags & METH_NOARGS && na == 0) {
4686 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004687
Victor Stinnerefde1462015-03-21 15:04:43 +01004688 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 }
4690 else if (flags & METH_O && na == 1) {
4691 PyObject *arg = EXT_POP(*pp_stack);
4692 C_TRACE(x, (*meth)(self,arg));
4693 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004694
Victor Stinnerefde1462015-03-21 15:04:43 +01004695 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 }
4697 else {
4698 err_args(func, flags, na);
4699 x = NULL;
4700 }
4701 }
4702 else {
4703 PyObject *callargs;
4704 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004705 if (callargs != NULL) {
4706 READ_TIMESTAMP(*pintr0);
4707 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4708 READ_TIMESTAMP(*pintr1);
4709 Py_XDECREF(callargs);
4710 }
4711 else {
4712 x = NULL;
4713 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004715 }
4716 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004717 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4718 /* optimize access to bound methods */
4719 PyObject *self = PyMethod_GET_SELF(func);
4720 PCALL(PCALL_METHOD);
4721 PCALL(PCALL_BOUND_METHOD);
4722 Py_INCREF(self);
4723 func = PyMethod_GET_FUNCTION(func);
4724 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03004725 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 na++;
4727 n++;
4728 } else
4729 Py_INCREF(func);
4730 READ_TIMESTAMP(*pintr0);
4731 if (PyFunction_Check(func))
4732 x = fast_function(func, pp_stack, n, na, nk);
4733 else
4734 x = do_call(func, pp_stack, na, nk);
4735 READ_TIMESTAMP(*pintr1);
4736 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004737
4738 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 /* Clear the stack of the function object. Also removes
4742 the arguments in case they weren't consumed already
4743 (fast_function() and err_args() leave them on the stack).
4744 */
4745 while ((*pp_stack) > pfunc) {
4746 w = EXT_POP(*pp_stack);
4747 Py_DECREF(w);
4748 PCALL(PCALL_POP);
4749 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004750
Victor Stinner4a7cc882015-03-06 23:35:27 +01004751 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004753}
4754
Jeremy Hylton192690e2002-08-16 18:36:11 +00004755/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004756 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004757 For the simplest case -- a function that takes only positional
4758 arguments and is called with only positional arguments -- it
4759 inlines the most primitive frame setup code from
4760 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4761 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004762*/
4763
4764static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004765fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4768 PyObject *globals = PyFunction_GET_GLOBALS(func);
4769 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4770 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Victor Stinner40ee3012014-06-16 15:59:28 +02004771 PyObject *name = ((PyFunctionObject *)func) -> func_name;
4772 PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 PyObject **d = NULL;
4774 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004776 PCALL(PCALL_FUNCTION);
4777 PCALL(PCALL_FAST_FUNCTION);
4778 if (argdefs == NULL && co->co_argcount == n &&
4779 co->co_kwonlyargcount == 0 && nk==0 &&
4780 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4781 PyFrameObject *f;
4782 PyObject *retval = NULL;
4783 PyThreadState *tstate = PyThreadState_GET();
4784 PyObject **fastlocals, **stack;
4785 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 PCALL(PCALL_FASTER_FUNCTION);
4788 assert(globals != NULL);
4789 /* XXX Perhaps we should create a specialized
4790 PyFrame_New() that doesn't take locals, but does
4791 take builtins without sanity checking them.
4792 */
4793 assert(tstate != NULL);
4794 f = PyFrame_New(tstate, co, globals, NULL);
4795 if (f == NULL)
4796 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004797
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 fastlocals = f->f_localsplus;
4799 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 for (i = 0; i < n; i++) {
4802 Py_INCREF(*stack);
4803 fastlocals[i] = *stack++;
4804 }
4805 retval = PyEval_EvalFrameEx(f,0);
4806 ++tstate->recursion_depth;
4807 Py_DECREF(f);
4808 --tstate->recursion_depth;
4809 return retval;
4810 }
4811 if (argdefs != NULL) {
4812 d = &PyTuple_GET_ITEM(argdefs, 0);
4813 nd = Py_SIZE(argdefs);
4814 }
Victor Stinner40ee3012014-06-16 15:59:28 +02004815 return _PyEval_EvalCodeWithName((PyObject*)co, globals,
4816 (PyObject *)NULL, (*pp_stack)-n, na,
4817 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4818 PyFunction_GET_CLOSURE(func),
4819 name, qualname);
Jeremy Hylton52820442001-01-03 23:52:36 +00004820}
4821
4822static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004823update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4824 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 PyObject *kwdict = NULL;
4827 if (orig_kwdict == NULL)
4828 kwdict = PyDict_New();
4829 else {
4830 kwdict = PyDict_Copy(orig_kwdict);
4831 Py_DECREF(orig_kwdict);
4832 }
4833 if (kwdict == NULL)
4834 return NULL;
4835 while (--nk >= 0) {
4836 int err;
4837 PyObject *value = EXT_POP(*pp_stack);
4838 PyObject *key = EXT_POP(*pp_stack);
4839 if (PyDict_GetItem(kwdict, key) != NULL) {
4840 PyErr_Format(PyExc_TypeError,
4841 "%.200s%s got multiple values "
4842 "for keyword argument '%U'",
4843 PyEval_GetFuncName(func),
4844 PyEval_GetFuncDesc(func),
4845 key);
4846 Py_DECREF(key);
4847 Py_DECREF(value);
4848 Py_DECREF(kwdict);
4849 return NULL;
4850 }
4851 err = PyDict_SetItem(kwdict, key, value);
4852 Py_DECREF(key);
4853 Py_DECREF(value);
4854 if (err) {
4855 Py_DECREF(kwdict);
4856 return NULL;
4857 }
4858 }
4859 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004860}
4861
4862static PyObject *
4863update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004865{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 callargs = PyTuple_New(nstack + nstar);
4869 if (callargs == NULL) {
4870 return NULL;
4871 }
4872 if (nstar) {
4873 int i;
4874 for (i = 0; i < nstar; i++) {
4875 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4876 Py_INCREF(a);
4877 PyTuple_SET_ITEM(callargs, nstack + i, a);
4878 }
4879 }
4880 while (--nstack >= 0) {
4881 w = EXT_POP(*pp_stack);
4882 PyTuple_SET_ITEM(callargs, nstack, w);
4883 }
4884 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004885}
4886
4887static PyObject *
4888load_args(PyObject ***pp_stack, int na)
4889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004890 PyObject *args = PyTuple_New(na);
4891 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 if (args == NULL)
4894 return NULL;
4895 while (--na >= 0) {
4896 w = EXT_POP(*pp_stack);
4897 PyTuple_SET_ITEM(args, na, w);
4898 }
4899 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004900}
4901
4902static PyObject *
4903do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4904{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004905 PyObject *callargs = NULL;
4906 PyObject *kwdict = NULL;
4907 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 if (nk > 0) {
4910 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4911 if (kwdict == NULL)
4912 goto call_fail;
4913 }
4914 callargs = load_args(pp_stack, na);
4915 if (callargs == NULL)
4916 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004917#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 /* At this point, we have to look at the type of func to
4919 update the call stats properly. Do it here so as to avoid
4920 exposing the call stats machinery outside ceval.c
4921 */
4922 if (PyFunction_Check(func))
4923 PCALL(PCALL_FUNCTION);
4924 else if (PyMethod_Check(func))
4925 PCALL(PCALL_METHOD);
4926 else if (PyType_Check(func))
4927 PCALL(PCALL_TYPE);
4928 else if (PyCFunction_Check(func))
4929 PCALL(PCALL_CFUNCTION);
4930 else
4931 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004932#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004933 if (PyCFunction_Check(func)) {
4934 PyThreadState *tstate = PyThreadState_GET();
4935 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4936 }
4937 else
4938 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004939call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004940 Py_XDECREF(callargs);
4941 Py_XDECREF(kwdict);
4942 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004943}
4944
4945static PyObject *
4946ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004948 int nstar = 0;
4949 PyObject *callargs = NULL;
4950 PyObject *stararg = NULL;
4951 PyObject *kwdict = NULL;
4952 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 if (flags & CALL_FLAG_KW) {
4955 kwdict = EXT_POP(*pp_stack);
4956 if (!PyDict_Check(kwdict)) {
4957 PyObject *d;
4958 d = PyDict_New();
4959 if (d == NULL)
4960 goto ext_call_fail;
4961 if (PyDict_Update(d, kwdict) != 0) {
4962 Py_DECREF(d);
4963 /* PyDict_Update raises attribute
4964 * error (percolated from an attempt
4965 * to get 'keys' attribute) instead of
4966 * a type error if its second argument
4967 * is not a mapping.
4968 */
4969 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4970 PyErr_Format(PyExc_TypeError,
4971 "%.200s%.200s argument after ** "
4972 "must be a mapping, not %.200s",
4973 PyEval_GetFuncName(func),
4974 PyEval_GetFuncDesc(func),
4975 kwdict->ob_type->tp_name);
4976 }
4977 goto ext_call_fail;
4978 }
4979 Py_DECREF(kwdict);
4980 kwdict = d;
4981 }
4982 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004983 if (nk > 0) {
4984 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4985 if (kwdict == NULL)
4986 goto ext_call_fail;
4987 }
4988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004989 if (flags & CALL_FLAG_VAR) {
4990 stararg = EXT_POP(*pp_stack);
4991 if (!PyTuple_Check(stararg)) {
4992 PyObject *t = NULL;
Martin Panterb5944222016-01-31 06:30:56 +00004993 if (Py_TYPE(stararg)->tp_iter == NULL &&
4994 !PySequence_Check(stararg)) {
4995 PyErr_Format(PyExc_TypeError,
4996 "%.200s%.200s argument after * "
4997 "must be an iterable, not %.200s",
4998 PyEval_GetFuncName(func),
4999 PyEval_GetFuncDesc(func),
5000 stararg->ob_type->tp_name);
5001 goto ext_call_fail;
5002 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 t = PySequence_Tuple(stararg);
5004 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 goto ext_call_fail;
5006 }
5007 Py_DECREF(stararg);
5008 stararg = t;
5009 }
5010 nstar = PyTuple_GET_SIZE(stararg);
5011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005012 callargs = update_star_args(na, nstar, stararg, pp_stack);
5013 if (callargs == NULL)
5014 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005015#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 /* At this point, we have to look at the type of func to
5017 update the call stats properly. Do it here so as to avoid
5018 exposing the call stats machinery outside ceval.c
5019 */
5020 if (PyFunction_Check(func))
5021 PCALL(PCALL_FUNCTION);
5022 else if (PyMethod_Check(func))
5023 PCALL(PCALL_METHOD);
5024 else if (PyType_Check(func))
5025 PCALL(PCALL_TYPE);
5026 else if (PyCFunction_Check(func))
5027 PCALL(PCALL_CFUNCTION);
5028 else
5029 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005030#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 if (PyCFunction_Check(func)) {
5032 PyThreadState *tstate = PyThreadState_GET();
5033 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5034 }
5035 else
5036 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00005037ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005038 Py_XDECREF(callargs);
5039 Py_XDECREF(kwdict);
5040 Py_XDECREF(stararg);
5041 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005042}
5043
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005044/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005045 nb_index slot defined, and store in *pi.
5046 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5047 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 +00005048 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005049*/
Tim Petersb5196382001-12-16 19:44:20 +00005050/* Note: If v is NULL, return success without storing into *pi. This
5051 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5052 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005053*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005054int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005055_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005057 if (v != NULL) {
5058 Py_ssize_t x;
5059 if (PyIndex_Check(v)) {
5060 x = PyNumber_AsSsize_t(v, NULL);
5061 if (x == -1 && PyErr_Occurred())
5062 return 0;
5063 }
5064 else {
5065 PyErr_SetString(PyExc_TypeError,
5066 "slice indices must be integers or "
5067 "None or have an __index__ method");
5068 return 0;
5069 }
5070 *pi = x;
5071 }
5072 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005073}
5074
Guido van Rossum486364b2007-06-30 05:01:58 +00005075#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005077
Guido van Rossumb209a111997-04-29 18:18:01 +00005078static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005079cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 int res = 0;
5082 switch (op) {
5083 case PyCmp_IS:
5084 res = (v == w);
5085 break;
5086 case PyCmp_IS_NOT:
5087 res = (v != w);
5088 break;
5089 case PyCmp_IN:
5090 res = PySequence_Contains(w, v);
5091 if (res < 0)
5092 return NULL;
5093 break;
5094 case PyCmp_NOT_IN:
5095 res = PySequence_Contains(w, v);
5096 if (res < 0)
5097 return NULL;
5098 res = !res;
5099 break;
5100 case PyCmp_EXC_MATCH:
5101 if (PyTuple_Check(w)) {
5102 Py_ssize_t i, length;
5103 length = PyTuple_Size(w);
5104 for (i = 0; i < length; i += 1) {
5105 PyObject *exc = PyTuple_GET_ITEM(w, i);
5106 if (!PyExceptionClass_Check(exc)) {
5107 PyErr_SetString(PyExc_TypeError,
5108 CANNOT_CATCH_MSG);
5109 return NULL;
5110 }
5111 }
5112 }
5113 else {
5114 if (!PyExceptionClass_Check(w)) {
5115 PyErr_SetString(PyExc_TypeError,
5116 CANNOT_CATCH_MSG);
5117 return NULL;
5118 }
5119 }
5120 res = PyErr_GivenExceptionMatches(v, w);
5121 break;
5122 default:
5123 return PyObject_RichCompare(v, w, op);
5124 }
5125 v = res ? Py_True : Py_False;
5126 Py_INCREF(v);
5127 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005128}
5129
Thomas Wouters52152252000-08-17 22:55:00 +00005130static PyObject *
5131import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005134 _Py_IDENTIFIER(__name__);
5135 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005137 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005138 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5139 return x;
5140 /* Issue #17636: in case this failed because of a circular relative
5141 import, try to fallback on reading the module directly from
5142 sys.modules. */
5143 PyErr_Clear();
5144 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005145 if (pkgname == NULL) {
5146 goto error;
5147 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005148 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5149 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005150 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005151 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005152 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005153 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005154 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005155 if (x == NULL) {
5156 goto error;
5157 }
5158 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005160 error:
5161 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5162 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005163}
Guido van Rossumac7be682001-01-17 15:42:30 +00005164
Thomas Wouters52152252000-08-17 22:55:00 +00005165static int
5166import_all_from(PyObject *locals, PyObject *v)
5167{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005168 _Py_IDENTIFIER(__all__);
5169 _Py_IDENTIFIER(__dict__);
5170 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005171 PyObject *dict, *name, *value;
5172 int skip_leading_underscores = 0;
5173 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005175 if (all == NULL) {
5176 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5177 return -1; /* Unexpected error */
5178 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005179 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005180 if (dict == NULL) {
5181 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5182 return -1;
5183 PyErr_SetString(PyExc_ImportError,
5184 "from-import-* object has no __dict__ and no __all__");
5185 return -1;
5186 }
5187 all = PyMapping_Keys(dict);
5188 Py_DECREF(dict);
5189 if (all == NULL)
5190 return -1;
5191 skip_leading_underscores = 1;
5192 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005194 for (pos = 0, err = 0; ; pos++) {
5195 name = PySequence_GetItem(all, pos);
5196 if (name == NULL) {
5197 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5198 err = -1;
5199 else
5200 PyErr_Clear();
5201 break;
5202 }
5203 if (skip_leading_underscores &&
5204 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005205 PyUnicode_READY(name) != -1 &&
5206 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005207 {
5208 Py_DECREF(name);
5209 continue;
5210 }
5211 value = PyObject_GetAttr(v, name);
5212 if (value == NULL)
5213 err = -1;
5214 else if (PyDict_CheckExact(locals))
5215 err = PyDict_SetItem(locals, name, value);
5216 else
5217 err = PyObject_SetItem(locals, name, value);
5218 Py_DECREF(name);
5219 Py_XDECREF(value);
5220 if (err != 0)
5221 break;
5222 }
5223 Py_DECREF(all);
5224 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005225}
5226
Guido van Rossumac7be682001-01-17 15:42:30 +00005227static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005228format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005230 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 if (!obj)
5233 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005235 obj_str = _PyUnicode_AsString(obj);
5236 if (!obj_str)
5237 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005240}
Guido van Rossum950361c1997-01-24 13:49:28 +00005241
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005242static void
5243format_exc_unbound(PyCodeObject *co, int oparg)
5244{
5245 PyObject *name;
5246 /* Don't stomp existing exception */
5247 if (PyErr_Occurred())
5248 return;
5249 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5250 name = PyTuple_GET_ITEM(co->co_cellvars,
5251 oparg);
5252 format_exc_check_arg(
5253 PyExc_UnboundLocalError,
5254 UNBOUNDLOCAL_ERROR_MSG,
5255 name);
5256 } else {
5257 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5258 PyTuple_GET_SIZE(co->co_cellvars));
5259 format_exc_check_arg(PyExc_NameError,
5260 UNBOUNDFREE_ERROR_MSG, name);
5261 }
5262}
5263
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005264static PyObject *
5265unicode_concatenate(PyObject *v, PyObject *w,
5266 PyFrameObject *f, unsigned char *next_instr)
5267{
5268 PyObject *res;
5269 if (Py_REFCNT(v) == 2) {
5270 /* In the common case, there are 2 references to the value
5271 * stored in 'variable' when the += is performed: one on the
5272 * value stack (in 'v') and one still stored in the
5273 * 'variable'. We try to delete the variable now to reduce
5274 * the refcnt to 1.
5275 */
5276 switch (*next_instr) {
5277 case STORE_FAST:
5278 {
5279 int oparg = PEEKARG();
5280 PyObject **fastlocals = f->f_localsplus;
5281 if (GETLOCAL(oparg) == v)
5282 SETLOCAL(oparg, NULL);
5283 break;
5284 }
5285 case STORE_DEREF:
5286 {
5287 PyObject **freevars = (f->f_localsplus +
5288 f->f_code->co_nlocals);
5289 PyObject *c = freevars[PEEKARG()];
5290 if (PyCell_GET(c) == v)
5291 PyCell_Set(c, NULL);
5292 break;
5293 }
5294 case STORE_NAME:
5295 {
5296 PyObject *names = f->f_code->co_names;
5297 PyObject *name = GETITEM(names, PEEKARG());
5298 PyObject *locals = f->f_locals;
5299 if (PyDict_CheckExact(locals) &&
5300 PyDict_GetItem(locals, name) == v) {
5301 if (PyDict_DelItem(locals, name) != 0) {
5302 PyErr_Clear();
5303 }
5304 }
5305 break;
5306 }
5307 }
5308 }
5309 res = v;
5310 PyUnicode_Append(&res, w);
5311 return res;
5312}
5313
Guido van Rossum950361c1997-01-24 13:49:28 +00005314#ifdef DYNAMIC_EXECUTION_PROFILE
5315
Skip Montanarof118cb12001-10-15 20:51:38 +00005316static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005317getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005318{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005319 int i;
5320 PyObject *l = PyList_New(256);
5321 if (l == NULL) return NULL;
5322 for (i = 0; i < 256; i++) {
5323 PyObject *x = PyLong_FromLong(a[i]);
5324 if (x == NULL) {
5325 Py_DECREF(l);
5326 return NULL;
5327 }
5328 PyList_SetItem(l, i, x);
5329 }
5330 for (i = 0; i < 256; i++)
5331 a[i] = 0;
5332 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005333}
5334
5335PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005336_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005337{
5338#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005339 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005340#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005341 int i;
5342 PyObject *l = PyList_New(257);
5343 if (l == NULL) return NULL;
5344 for (i = 0; i < 257; i++) {
5345 PyObject *x = getarray(dxpairs[i]);
5346 if (x == NULL) {
5347 Py_DECREF(l);
5348 return NULL;
5349 }
5350 PyList_SetItem(l, i, x);
5351 }
5352 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005353#endif
5354}
5355
5356#endif