blob: c83743d92186b52a6a46f299d6a2ffd36a02e2b0 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000019#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Thomas Wouters477c8d52006-05-27 19:21:47 +000023#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000024
25#define READ_TIMESTAMP(var)
26
27#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000028
29typedef unsigned long long uint64;
30
Ezio Melotti13925002011-03-16 11:05:33 +020031/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000032 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
33 "__powerpc__" appears to be the correct one for Linux with GCC
34*/
35#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Michael W. Hudson75eabd22005-01-18 15:56:11 +000037#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000038
39static void
40ppc_getcounter(uint64 *v)
41{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020042 unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000043
44 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 asm volatile ("mftbu %0" : "=r" (tbu) );
46 asm volatile ("mftb %0" : "=r" (tb) );
47 asm volatile ("mftbu %0" : "=r" (tbu2));
48 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* The slightly peculiar way of writing the next lines is
51 compiled better by GCC than any other way I tried. */
52 ((long*)(v))[0] = tbu;
53 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000054}
55
Mark Dickinsona25b1312009-10-31 10:18:44 +000056#elif defined(__i386__)
57
58/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000059
Michael W. Hudson75eabd22005-01-18 15:56:11 +000060#define READ_TIMESTAMP(val) \
61 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000062
Mark Dickinsona25b1312009-10-31 10:18:44 +000063#elif defined(__x86_64__)
64
65/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
66 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
67 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
68 32-bit pieces of the result. */
69
Victor Stinner0b881dd2014-12-12 13:17:41 +010070#define READ_TIMESTAMP(val) do { \
71 unsigned int h, l; \
72 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
73 (val) = ((uint64)l) | (((uint64)h) << 32); \
74 } while(0)
Mark Dickinsona25b1312009-10-31 10:18:44 +000075
76
77#else
78
79#error "Don't know how to implement timestamp counter for this architecture"
80
Michael W. Hudson800ba232004-08-12 18:19:17 +000081#endif
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 uint64 intr, inst, loop;
87 PyThreadState *tstate = PyThreadState_Get();
88 if (!tstate->interp->tscdump)
89 return;
90 intr = intr1 - intr0;
91 inst = inst1 - inst0 - intr;
92 loop = loop1 - loop0 - intr;
93 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000094 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095}
Michael W. Hudson800ba232004-08-12 18:19:17 +000096
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000097#endif
98
Guido van Rossum04691fc1992-08-12 15:35:34 +000099/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +0000101
Guido van Rossum408027e1996-12-30 16:17:54 +0000102#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000103/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104#define LLTRACE 1 /* Low-level trace feature */
105#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000106#endif
107
Jeremy Hylton52820442001-01-03 23:52:36 +0000108typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000109
Guido van Rossum374a9221991-04-04 10:40:29 +0000110/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000115#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
117static PyObject * do_call(PyObject *, PyObject ***, int, int);
118static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000119static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
122static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000123#define CALL_FLAG_VAR 1
124#define CALL_FLAG_KW 2
125
Guido van Rossum0a066c01992-03-27 17:29:15 +0000126#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000127static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +0200128static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000129#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100130static int call_trace(Py_tracefunc, PyObject *,
131 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000133static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100134 PyThreadState *, PyFrameObject *,
135 int, PyObject *);
136static void call_exc_trace(Py_tracefunc, PyObject *,
137 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000138static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100139 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000140
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141static PyObject * cmp_outcome(int, PyObject *, PyObject *);
142static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000143static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000144static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000145static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200146static PyObject * unicode_concatenate(PyObject *, PyObject *,
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);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001566 /* unicode_concatenate consumed the ref to v */
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);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001765 /* unicode_concatenate consumed the ref to v */
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);
1856 /* v[w] = u */
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);
1871 /* del v[w] */
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
1936 if (type->tp_as_async != NULL)
1937 getter = type->tp_as_async->am_aiter;
1938
1939 if (getter != NULL) {
1940 iter = (*getter)(obj);
1941 Py_DECREF(obj);
1942 if (iter == NULL) {
1943 SET_TOP(NULL);
1944 goto error;
1945 }
1946 }
1947 else {
1948 SET_TOP(NULL);
1949 PyErr_Format(
1950 PyExc_TypeError,
1951 "'async for' requires an object with "
1952 "__aiter__ method, got %.100s",
1953 type->tp_name);
1954 Py_DECREF(obj);
1955 goto error;
1956 }
1957
Yury Selivanov5376ba92015-06-22 12:19:30 -04001958 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001959 if (awaitable == NULL) {
1960 SET_TOP(NULL);
1961 PyErr_Format(
1962 PyExc_TypeError,
1963 "'async for' received an invalid object "
1964 "from __aiter__: %.100s",
1965 Py_TYPE(iter)->tp_name);
1966
1967 Py_DECREF(iter);
1968 goto error;
1969 } else
1970 Py_DECREF(iter);
1971
1972 SET_TOP(awaitable);
1973 DISPATCH();
1974 }
1975
1976 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001977 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001978 PyObject *next_iter = NULL;
1979 PyObject *awaitable = NULL;
1980 PyObject *aiter = TOP();
1981 PyTypeObject *type = Py_TYPE(aiter);
1982
1983 if (type->tp_as_async != NULL)
1984 getter = type->tp_as_async->am_anext;
1985
1986 if (getter != NULL) {
1987 next_iter = (*getter)(aiter);
1988 if (next_iter == NULL) {
1989 goto error;
1990 }
1991 }
1992 else {
1993 PyErr_Format(
1994 PyExc_TypeError,
1995 "'async for' requires an iterator with "
1996 "__anext__ method, got %.100s",
1997 type->tp_name);
1998 goto error;
1999 }
2000
Yury Selivanov5376ba92015-06-22 12:19:30 -04002001 awaitable = _PyCoro_GetAwaitableIter(next_iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002002 if (awaitable == NULL) {
2003 PyErr_Format(
2004 PyExc_TypeError,
2005 "'async for' received an invalid object "
2006 "from __anext__: %.100s",
2007 Py_TYPE(next_iter)->tp_name);
2008
2009 Py_DECREF(next_iter);
2010 goto error;
2011 } else
2012 Py_DECREF(next_iter);
2013
2014 PUSH(awaitable);
2015 DISPATCH();
2016 }
2017
2018 TARGET(GET_AWAITABLE) {
2019 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002020 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002021
2022 Py_DECREF(iterable);
2023
Yury Selivanovc724bae2016-03-02 11:30:46 -05002024 if (iter != NULL && PyCoro_CheckExact(iter)) {
2025 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2026 if (yf != NULL) {
2027 /* `iter` is a coroutine object that is being
2028 awaited, `yf` is a pointer to the current awaitable
2029 being awaited on. */
2030 Py_DECREF(yf);
2031 Py_CLEAR(iter);
2032 PyErr_SetString(
2033 PyExc_RuntimeError,
2034 "coroutine is being awaited already");
2035 /* The code below jumps to `error` if `iter` is NULL. */
2036 }
2037 }
2038
Yury Selivanov75445082015-05-11 22:57:16 -04002039 SET_TOP(iter); /* Even if it's NULL */
2040
2041 if (iter == NULL) {
2042 goto error;
2043 }
2044
2045 DISPATCH();
2046 }
2047
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002048 TARGET(YIELD_FROM) {
2049 PyObject *v = POP();
2050 PyObject *reciever = TOP();
2051 int err;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002052 if (PyGen_CheckExact(reciever) || PyCoro_CheckExact(reciever)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002054 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002055 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 if (v == Py_None)
2057 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002058 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04002059 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002060 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 Py_DECREF(v);
2062 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002063 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002064 if (tstate->c_tracefunc != NULL
2065 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002066 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002067 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 if (err < 0)
2069 goto error;
2070 Py_DECREF(reciever);
2071 SET_TOP(val);
2072 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002073 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002074 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002075 f->f_stacktop = stack_pointer;
2076 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002077 /* and repeat... */
2078 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002079 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002081
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 retval = POP();
2084 f->f_stacktop = stack_pointer;
2085 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002088
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 TARGET(POP_EXCEPT) {
2090 PyTryBlock *b = PyFrame_BlockPop(f);
2091 if (b->b_type != EXCEPT_HANDLER) {
2092 PyErr_SetString(PyExc_SystemError,
2093 "popped block is not an except handler");
2094 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002099
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 TARGET(POP_BLOCK) {
2101 PyTryBlock *b = PyFrame_BlockPop(f);
2102 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 TARGET(END_FINALLY) {
2108 PyObject *status = POP();
2109 if (PyLong_Check(status)) {
2110 why = (enum why_code) PyLong_AS_LONG(status);
2111 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 if (why == WHY_RETURN ||
2113 why == WHY_CONTINUE)
2114 retval = POP();
2115 if (why == WHY_SILENCED) {
2116 /* An exception was silenced by 'with', we must
2117 manually unwind the EXCEPT_HANDLER block which was
2118 created when the exception was caught, otherwise
2119 the stack will be in an inconsistent state. */
2120 PyTryBlock *b = PyFrame_BlockPop(f);
2121 assert(b->b_type == EXCEPT_HANDLER);
2122 UNWIND_EXCEPT_HANDLER(b);
2123 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 Py_DECREF(status);
2125 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 Py_DECREF(status);
2128 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 else if (PyExceptionClass_Check(status)) {
2131 PyObject *exc = POP();
2132 PyObject *tb = POP();
2133 PyErr_Restore(status, exc, tb);
2134 why = WHY_EXCEPTION;
2135 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002136 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 PyErr_SetString(PyExc_SystemError,
2139 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 Py_DECREF(status);
2141 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 Py_DECREF(status);
2144 DISPATCH();
2145 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002146
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002148 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002149
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002151 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2153 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002154 PyErr_SetString(PyExc_NameError,
2155 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002159 }
2160 else {
2161 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2162 if (build_class_str == NULL)
2163 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2165 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002166 if (PyErr_ExceptionMatches(PyExc_KeyError))
2167 PyErr_SetString(PyExc_NameError,
2168 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002171 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002173 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002174 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002175
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 TARGET(STORE_NAME) {
2177 PyObject *name = GETITEM(names, oparg);
2178 PyObject *v = POP();
2179 PyObject *ns = f->f_locals;
2180 int err;
2181 if (ns == NULL) {
2182 PyErr_Format(PyExc_SystemError,
2183 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 if (PyDict_CheckExact(ns))
2188 err = PyDict_SetItem(ns, name, v);
2189 else
2190 err = PyObject_SetItem(ns, name, v);
2191 Py_DECREF(v);
2192 if (err != 0)
2193 goto error;
2194 DISPATCH();
2195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002196
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 TARGET(DELETE_NAME) {
2198 PyObject *name = GETITEM(names, oparg);
2199 PyObject *ns = f->f_locals;
2200 int err;
2201 if (ns == NULL) {
2202 PyErr_Format(PyExc_SystemError,
2203 "no locals when deleting %R", name);
2204 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 err = PyObject_DelItem(ns, name);
2207 if (err != 0) {
2208 format_exc_check_arg(PyExc_NameError,
2209 NAME_ERROR_MSG,
2210 name);
2211 goto error;
2212 }
2213 DISPATCH();
2214 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002216 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 TARGET(UNPACK_SEQUENCE) {
2218 PyObject *seq = POP(), *item, **items;
2219 if (PyTuple_CheckExact(seq) &&
2220 PyTuple_GET_SIZE(seq) == oparg) {
2221 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 item = items[oparg];
2224 Py_INCREF(item);
2225 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002227 } else if (PyList_CheckExact(seq) &&
2228 PyList_GET_SIZE(seq) == oparg) {
2229 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 item = items[oparg];
2232 Py_INCREF(item);
2233 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002234 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 stack_pointer + oparg)) {
2237 STACKADJ(oparg);
2238 } else {
2239 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 Py_DECREF(seq);
2241 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002244 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002246
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 TARGET(UNPACK_EX) {
2248 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2249 PyObject *seq = POP();
2250
2251 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2252 stack_pointer + totalargs)) {
2253 stack_pointer += totalargs;
2254 } else {
2255 Py_DECREF(seq);
2256 goto error;
2257 }
2258 Py_DECREF(seq);
2259 DISPATCH();
2260 }
2261
2262 TARGET(STORE_ATTR) {
2263 PyObject *name = GETITEM(names, oparg);
2264 PyObject *owner = TOP();
2265 PyObject *v = SECOND();
2266 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002267 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 Py_DECREF(owner);
2271 if (err != 0)
2272 goto error;
2273 DISPATCH();
2274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002275
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002276 TARGET(DELETE_ATTR) {
2277 PyObject *name = GETITEM(names, oparg);
2278 PyObject *owner = POP();
2279 int err;
2280 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2281 Py_DECREF(owner);
2282 if (err != 0)
2283 goto error;
2284 DISPATCH();
2285 }
2286
2287 TARGET(STORE_GLOBAL) {
2288 PyObject *name = GETITEM(names, oparg);
2289 PyObject *v = POP();
2290 int err;
2291 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 if (err != 0)
2294 goto error;
2295 DISPATCH();
2296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002297
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 TARGET(DELETE_GLOBAL) {
2299 PyObject *name = GETITEM(names, oparg);
2300 int err;
2301 err = PyDict_DelItem(f->f_globals, name);
2302 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002303 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002304 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002306 }
2307 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 TARGET(LOAD_NAME) {
2311 PyObject *name = GETITEM(names, oparg);
2312 PyObject *locals = f->f_locals;
2313 PyObject *v;
2314 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002315 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002316 "no locals when loading %R", name);
2317 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 if (PyDict_CheckExact(locals)) {
2320 v = PyDict_GetItem(locals, name);
2321 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 }
2323 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002325 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002326 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2327 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 PyErr_Clear();
2329 }
2330 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 if (v == NULL) {
2332 v = PyDict_GetItem(f->f_globals, name);
2333 Py_XINCREF(v);
2334 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002335 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 v = PyDict_GetItem(f->f_builtins, name);
2337 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002338 format_exc_check_arg(
2339 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002340 NAME_ERROR_MSG, name);
2341 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002344 }
2345 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002346 v = PyObject_GetItem(f->f_builtins, name);
2347 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002348 if (PyErr_ExceptionMatches(PyExc_KeyError))
2349 format_exc_check_arg(
2350 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 NAME_ERROR_MSG, name);
2352 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002353 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002354 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002357 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002359 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002360
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002361 TARGET(LOAD_GLOBAL) {
2362 PyObject *name = GETITEM(names, oparg);
2363 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002364 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002365 && PyDict_CheckExact(f->f_builtins))
2366 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002367 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002368 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002369 name);
2370 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002371 if (!_PyErr_OCCURRED()) {
2372 /* _PyDict_LoadGlobal() returns NULL without raising
2373 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002374 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002375 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002377 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002378 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002379 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002381 else {
2382 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002383
2384 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 v = PyObject_GetItem(f->f_globals, name);
2386 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002387 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2388 goto error;
2389 PyErr_Clear();
2390
Victor Stinnerb4efc962015-11-20 09:24:02 +01002391 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 v = PyObject_GetItem(f->f_builtins, name);
2393 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002394 if (PyErr_ExceptionMatches(PyExc_KeyError))
2395 format_exc_check_arg(
2396 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002397 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002399 }
2400 }
2401 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002404 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 TARGET(DELETE_FAST) {
2407 PyObject *v = GETLOCAL(oparg);
2408 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 SETLOCAL(oparg, NULL);
2410 DISPATCH();
2411 }
2412 format_exc_check_arg(
2413 PyExc_UnboundLocalError,
2414 UNBOUNDLOCAL_ERROR_MSG,
2415 PyTuple_GetItem(co->co_varnames, oparg)
2416 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 goto error;
2418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002419
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 TARGET(DELETE_DEREF) {
2421 PyObject *cell = freevars[oparg];
2422 if (PyCell_GET(cell) != NULL) {
2423 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002424 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002425 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002426 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 goto error;
2428 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002429
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 TARGET(LOAD_CLOSURE) {
2431 PyObject *cell = freevars[oparg];
2432 Py_INCREF(cell);
2433 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002434 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002436
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002437 TARGET(LOAD_CLASSDEREF) {
2438 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002439 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002440 assert(locals);
2441 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2442 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2443 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2444 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2445 if (PyDict_CheckExact(locals)) {
2446 value = PyDict_GetItem(locals, name);
2447 Py_XINCREF(value);
2448 }
2449 else {
2450 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002451 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002452 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2453 goto error;
2454 PyErr_Clear();
2455 }
2456 }
2457 if (!value) {
2458 PyObject *cell = freevars[oparg];
2459 value = PyCell_GET(cell);
2460 if (value == NULL) {
2461 format_exc_unbound(co, oparg);
2462 goto error;
2463 }
2464 Py_INCREF(value);
2465 }
2466 PUSH(value);
2467 DISPATCH();
2468 }
2469
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 TARGET(LOAD_DEREF) {
2471 PyObject *cell = freevars[oparg];
2472 PyObject *value = PyCell_GET(cell);
2473 if (value == NULL) {
2474 format_exc_unbound(co, oparg);
2475 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002477 Py_INCREF(value);
2478 PUSH(value);
2479 DISPATCH();
2480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002481
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002482 TARGET(STORE_DEREF) {
2483 PyObject *v = POP();
2484 PyObject *cell = freevars[oparg];
2485 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002486 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002487 DISPATCH();
2488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002489
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 TARGET(BUILD_TUPLE) {
2491 PyObject *tup = PyTuple_New(oparg);
2492 if (tup == NULL)
2493 goto error;
2494 while (--oparg >= 0) {
2495 PyObject *item = POP();
2496 PyTuple_SET_ITEM(tup, oparg, item);
2497 }
2498 PUSH(tup);
2499 DISPATCH();
2500 }
2501
2502 TARGET(BUILD_LIST) {
2503 PyObject *list = PyList_New(oparg);
2504 if (list == NULL)
2505 goto error;
2506 while (--oparg >= 0) {
2507 PyObject *item = POP();
2508 PyList_SET_ITEM(list, oparg, item);
2509 }
2510 PUSH(list);
2511 DISPATCH();
2512 }
2513
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002514 TARGET_WITH_IMPL(BUILD_TUPLE_UNPACK, _build_list_unpack)
2515 TARGET(BUILD_LIST_UNPACK)
2516 _build_list_unpack: {
2517 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
2518 int i;
2519 PyObject *sum = PyList_New(0);
2520 PyObject *return_value;
2521 if (sum == NULL)
2522 goto error;
2523
2524 for (i = oparg; i > 0; i--) {
2525 PyObject *none_val;
2526
2527 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2528 if (none_val == NULL) {
2529 Py_DECREF(sum);
2530 goto error;
2531 }
2532 Py_DECREF(none_val);
2533 }
2534
2535 if (convert_to_tuple) {
2536 return_value = PyList_AsTuple(sum);
2537 Py_DECREF(sum);
2538 if (return_value == NULL)
2539 goto error;
2540 }
2541 else {
2542 return_value = sum;
2543 }
2544
2545 while (oparg--)
2546 Py_DECREF(POP());
2547 PUSH(return_value);
2548 DISPATCH();
2549 }
2550
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 TARGET(BUILD_SET) {
2552 PyObject *set = PySet_New(NULL);
2553 int err = 0;
2554 if (set == NULL)
2555 goto error;
2556 while (--oparg >= 0) {
2557 PyObject *item = POP();
2558 if (err == 0)
2559 err = PySet_Add(set, item);
2560 Py_DECREF(item);
2561 }
2562 if (err != 0) {
2563 Py_DECREF(set);
2564 goto error;
2565 }
2566 PUSH(set);
2567 DISPATCH();
2568 }
2569
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002570 TARGET(BUILD_SET_UNPACK) {
2571 int i;
2572 PyObject *sum = PySet_New(NULL);
2573 if (sum == NULL)
2574 goto error;
2575
2576 for (i = oparg; i > 0; i--) {
2577 if (_PySet_Update(sum, PEEK(i)) < 0) {
2578 Py_DECREF(sum);
2579 goto error;
2580 }
2581 }
2582
2583 while (oparg--)
2584 Py_DECREF(POP());
2585 PUSH(sum);
2586 DISPATCH();
2587 }
2588
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002589 TARGET(BUILD_MAP) {
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002590 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002591 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2592 if (map == NULL)
2593 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002594 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002595 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002596 PyObject *key = PEEK(2*i);
2597 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002598 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002599 if (err != 0) {
2600 Py_DECREF(map);
2601 goto error;
2602 }
2603 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002604
2605 while (oparg--) {
2606 Py_DECREF(POP());
2607 Py_DECREF(POP());
2608 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 PUSH(map);
2610 DISPATCH();
2611 }
2612
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002613 TARGET_WITH_IMPL(BUILD_MAP_UNPACK_WITH_CALL, _build_map_unpack)
2614 TARGET(BUILD_MAP_UNPACK)
2615 _build_map_unpack: {
2616 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
2617 int num_maps;
2618 int function_location;
2619 int i;
2620 PyObject *sum = PyDict_New();
2621 if (sum == NULL)
2622 goto error;
2623 if (with_call) {
2624 num_maps = oparg & 0xff;
2625 function_location = (oparg>>8) & 0xff;
2626 }
2627 else {
2628 num_maps = oparg;
2629 }
2630
2631 for (i = num_maps; i > 0; i--) {
2632 PyObject *arg = PEEK(i);
2633 if (with_call) {
2634 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2635
2636 if (intersection == NULL) {
2637 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2638 PyObject *func = (
2639 PEEK(function_location + num_maps));
2640 PyErr_Format(PyExc_TypeError,
2641 "%.200s%.200s argument after ** "
2642 "must be a mapping, not %.200s",
2643 PyEval_GetFuncName(func),
2644 PyEval_GetFuncDesc(func),
2645 arg->ob_type->tp_name);
2646 }
2647 Py_DECREF(sum);
2648 goto error;
2649 }
2650
2651 if (PySet_GET_SIZE(intersection)) {
2652 Py_ssize_t idx = 0;
2653 PyObject *key;
2654 PyObject *func = PEEK(function_location + num_maps);
2655 Py_hash_t hash;
2656 _PySet_NextEntry(intersection, &idx, &key, &hash);
2657 if (!PyUnicode_Check(key)) {
2658 PyErr_Format(PyExc_TypeError,
2659 "%.200s%.200s keywords must be strings",
2660 PyEval_GetFuncName(func),
2661 PyEval_GetFuncDesc(func));
2662 } else {
2663 PyErr_Format(PyExc_TypeError,
2664 "%.200s%.200s got multiple "
2665 "values for keyword argument '%U'",
2666 PyEval_GetFuncName(func),
2667 PyEval_GetFuncDesc(func),
2668 key);
2669 }
2670 Py_DECREF(intersection);
2671 Py_DECREF(sum);
2672 goto error;
2673 }
2674 Py_DECREF(intersection);
2675 }
2676
2677 if (PyDict_Update(sum, arg) < 0) {
2678 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2679 PyErr_Format(PyExc_TypeError,
2680 "'%.200s' object is not a mapping",
2681 arg->ob_type->tp_name);
2682 }
2683 Py_DECREF(sum);
2684 goto error;
2685 }
2686 }
2687
2688 while (num_maps--)
2689 Py_DECREF(POP());
2690 PUSH(sum);
2691 DISPATCH();
2692 }
2693
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 TARGET(MAP_ADD) {
2695 PyObject *key = TOP();
2696 PyObject *value = SECOND();
2697 PyObject *map;
2698 int err;
2699 STACKADJ(-2);
2700 map = stack_pointer[-oparg]; /* dict */
2701 assert(PyDict_CheckExact(map));
2702 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2703 Py_DECREF(value);
2704 Py_DECREF(key);
2705 if (err != 0)
2706 goto error;
2707 PREDICT(JUMP_ABSOLUTE);
2708 DISPATCH();
2709 }
2710
2711 TARGET(LOAD_ATTR) {
2712 PyObject *name = GETITEM(names, oparg);
2713 PyObject *owner = TOP();
2714 PyObject *res = PyObject_GetAttr(owner, name);
2715 Py_DECREF(owner);
2716 SET_TOP(res);
2717 if (res == NULL)
2718 goto error;
2719 DISPATCH();
2720 }
2721
2722 TARGET(COMPARE_OP) {
2723 PyObject *right = POP();
2724 PyObject *left = TOP();
2725 PyObject *res = cmp_outcome(oparg, left, right);
2726 Py_DECREF(left);
2727 Py_DECREF(right);
2728 SET_TOP(res);
2729 if (res == NULL)
2730 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 PREDICT(POP_JUMP_IF_FALSE);
2732 PREDICT(POP_JUMP_IF_TRUE);
2733 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002734 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002735
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 TARGET(IMPORT_NAME) {
2737 _Py_IDENTIFIER(__import__);
2738 PyObject *name = GETITEM(names, oparg);
2739 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002740 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 if (func == NULL) {
2742 PyErr_SetString(PyExc_ImportError,
2743 "__import__ not found");
2744 goto error;
2745 }
2746 Py_INCREF(func);
2747 from = POP();
2748 level = TOP();
2749 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2750 args = PyTuple_Pack(5,
2751 name,
2752 f->f_globals,
2753 f->f_locals == NULL ?
2754 Py_None : f->f_locals,
2755 from,
2756 level);
2757 else
2758 args = PyTuple_Pack(4,
2759 name,
2760 f->f_globals,
2761 f->f_locals == NULL ?
2762 Py_None : f->f_locals,
2763 from);
2764 Py_DECREF(level);
2765 Py_DECREF(from);
2766 if (args == NULL) {
2767 Py_DECREF(func);
2768 STACKADJ(-1);
2769 goto error;
2770 }
2771 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002772 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 READ_TIMESTAMP(intr1);
2774 Py_DECREF(args);
2775 Py_DECREF(func);
2776 SET_TOP(res);
2777 if (res == NULL)
2778 goto error;
2779 DISPATCH();
2780 }
2781
2782 TARGET(IMPORT_STAR) {
2783 PyObject *from = POP(), *locals;
2784 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002785 if (PyFrame_FastToLocalsWithError(f) < 0)
2786 goto error;
2787
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 locals = f->f_locals;
2789 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 PyErr_SetString(PyExc_SystemError,
2791 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002793 }
2794 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 READ_TIMESTAMP(intr1);
2797 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 Py_DECREF(from);
2799 if (err != 0)
2800 goto error;
2801 DISPATCH();
2802 }
Guido van Rossum25831651993-05-19 14:50:45 +00002803
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 TARGET(IMPORT_FROM) {
2805 PyObject *name = GETITEM(names, oparg);
2806 PyObject *from = TOP();
2807 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 PUSH(res);
2812 if (res == NULL)
2813 goto error;
2814 DISPATCH();
2815 }
Thomas Wouters52152252000-08-17 22:55:00 +00002816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 JUMPBY(oparg);
2819 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002821
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 TARGET(POP_JUMP_IF_FALSE) {
2824 PyObject *cond = POP();
2825 int err;
2826 if (cond == Py_True) {
2827 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 FAST_DISPATCH();
2829 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002830 if (cond == Py_False) {
2831 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 JUMPTO(oparg);
2833 FAST_DISPATCH();
2834 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 err = PyObject_IsTrue(cond);
2836 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 if (err > 0)
2838 err = 0;
2839 else if (err == 0)
2840 JUMPTO(oparg);
2841 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 TARGET(POP_JUMP_IF_TRUE) {
2848 PyObject *cond = POP();
2849 int err;
2850 if (cond == Py_False) {
2851 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 FAST_DISPATCH();
2853 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 if (cond == Py_True) {
2855 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 JUMPTO(oparg);
2857 FAST_DISPATCH();
2858 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 err = PyObject_IsTrue(cond);
2860 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 if (err > 0) {
2862 err = 0;
2863 JUMPTO(oparg);
2864 }
2865 else if (err == 0)
2866 ;
2867 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002870 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002871
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 TARGET(JUMP_IF_FALSE_OR_POP) {
2873 PyObject *cond = TOP();
2874 int err;
2875 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 FAST_DISPATCH();
2879 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 JUMPTO(oparg);
2882 FAST_DISPATCH();
2883 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 if (err > 0) {
2886 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 err = 0;
2889 }
2890 else if (err == 0)
2891 JUMPTO(oparg);
2892 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002896
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 TARGET(JUMP_IF_TRUE_OR_POP) {
2898 PyObject *cond = TOP();
2899 int err;
2900 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 FAST_DISPATCH();
2904 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002905 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 JUMPTO(oparg);
2907 FAST_DISPATCH();
2908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (err > 0) {
2911 err = 0;
2912 JUMPTO(oparg);
2913 }
2914 else if (err == 0) {
2915 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002916 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 }
2918 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002924 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002926#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 /* Enabling this path speeds-up all while and for-loops by bypassing
2928 the per-loop checks for signals. By default, this should be turned-off
2929 because it prevents detection of a control-break in tight loops like
2930 "while 1: pass". Compile with this option turned-on when you need
2931 the speed-up and do not need break checking inside tight loops (ones
2932 that contain only instructions ending with FAST_DISPATCH).
2933 */
2934 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002935#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002936 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002937#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002939
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002943 PyObject *iter = PyObject_GetIter(iterable);
2944 Py_DECREF(iterable);
2945 SET_TOP(iter);
2946 if (iter == NULL)
2947 goto error;
2948 PREDICT(FOR_ITER);
2949 DISPATCH();
2950 }
2951
2952 TARGET(GET_YIELD_FROM_ITER) {
2953 /* before: [obj]; after [getiter(obj)] */
2954 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002955 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002956 if (PyCoro_CheckExact(iterable)) {
2957 /* `iterable` is a coroutine */
2958 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2959 /* and it is used in a 'yield from' expression of a
2960 regular generator. */
2961 Py_DECREF(iterable);
2962 SET_TOP(NULL);
2963 PyErr_SetString(PyExc_TypeError,
2964 "cannot 'yield from' a coroutine object "
2965 "in a non-coroutine generator");
2966 goto error;
2967 }
2968 }
2969 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002970 /* `iterable` is not a generator. */
2971 iter = PyObject_GetIter(iterable);
2972 Py_DECREF(iterable);
2973 SET_TOP(iter);
2974 if (iter == NULL)
2975 goto error;
2976 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 DISPATCH();
2978 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002980 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002982 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 PyObject *iter = TOP();
2984 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2985 if (next != NULL) {
2986 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 PREDICT(STORE_FAST);
2988 PREDICT(UNPACK_SEQUENCE);
2989 DISPATCH();
2990 }
2991 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2993 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002994 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002995 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 PyErr_Clear();
2997 }
2998 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 STACKADJ(-1);
3000 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 JUMPBY(oparg);
3002 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003004
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 why = WHY_BREAK;
3007 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003009
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 if (retval == NULL)
3013 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003014 why = WHY_CONTINUE;
3015 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003016 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
3019 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
3020 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003021 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 /* NOTE: If you add any new block-setup opcodes that
3023 are not try/except/finally handlers, you may need
3024 to update the PyGen_NeedsFinalizing() function.
3025 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3028 STACK_LEVEL());
3029 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003031
Yury Selivanov75445082015-05-11 22:57:16 -04003032 TARGET(BEFORE_ASYNC_WITH) {
3033 _Py_IDENTIFIER(__aexit__);
3034 _Py_IDENTIFIER(__aenter__);
3035
3036 PyObject *mgr = TOP();
3037 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3038 *enter;
3039 PyObject *res;
3040 if (exit == NULL)
3041 goto error;
3042 SET_TOP(exit);
3043 enter = special_lookup(mgr, &PyId___aenter__);
3044 Py_DECREF(mgr);
3045 if (enter == NULL)
3046 goto error;
3047 res = PyObject_CallFunctionObjArgs(enter, NULL);
3048 Py_DECREF(enter);
3049 if (res == NULL)
3050 goto error;
3051 PUSH(res);
3052 DISPATCH();
3053 }
3054
3055 TARGET(SETUP_ASYNC_WITH) {
3056 PyObject *res = POP();
3057 /* Setup the finally block before pushing the result
3058 of __aenter__ on the stack. */
3059 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3060 STACK_LEVEL());
3061 PUSH(res);
3062 DISPATCH();
3063 }
3064
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003066 _Py_IDENTIFIER(__exit__);
3067 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 PyObject *mgr = TOP();
3069 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3070 PyObject *res;
3071 if (exit == NULL)
3072 goto error;
3073 SET_TOP(exit);
3074 enter = special_lookup(mgr, &PyId___enter__);
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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 /* Setup the finally block before pushing the result
3083 of __enter__ on the stack. */
3084 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3085 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 DISPATCH();
3089 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003090
Yury Selivanov75445082015-05-11 22:57:16 -04003091 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003092 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 how/why we entered the finally clause:
3094 - TOP = None
3095 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3096 - TOP = WHY_*; no retval below it
3097 - (TOP, SECOND, THIRD) = exc_info()
3098 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3099 Below them is EXIT, the context.__exit__ bound method.
3100 In the last case, we must call
3101 EXIT(TOP, SECOND, THIRD)
3102 otherwise we must call
3103 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003104
Benjamin Peterson8f169482013-10-29 22:25:06 -04003105 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003107 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003110 In addition, if the stack represents an exception,
3111 *and* the function call returns a 'true' value, we
3112 push WHY_SILENCED onto the stack. END_FINALLY will
3113 then not re-raise the exception. (But non-local
3114 gotos should still be resumed.)
3115 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003118 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 (void)POP();
3121 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003123 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003124 else if (PyLong_Check(exc)) {
3125 STACKADJ(-1);
3126 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 case WHY_RETURN:
3128 case WHY_CONTINUE:
3129 /* Retval in TOP. */
3130 exit_func = SECOND();
3131 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003132 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133 break;
3134 default:
3135 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003136 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003137 break;
3138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003139 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003140 }
3141 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003142 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003143 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003144 val = SECOND();
3145 tb = THIRD();
3146 tp2 = FOURTH();
3147 exc2 = PEEK(5);
3148 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003149 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 SET_VALUE(7, tb2);
3151 SET_VALUE(6, exc2);
3152 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3154 SET_FOURTH(NULL);
3155 /* We just shifted the stack down, so we have
3156 to tell the except handler block that the
3157 values are lower than it expects. */
3158 block = &f->f_blockstack[f->f_iblock - 1];
3159 assert(block->b_type == EXCEPT_HANDLER);
3160 block->b_level--;
3161 }
3162 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003163 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003164 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003165 if (res == NULL)
3166 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003167
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003168 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003169 PUSH(exc);
3170 PUSH(res);
3171 PREDICT(WITH_CLEANUP_FINISH);
3172 DISPATCH();
3173 }
3174
3175 PREDICTED(WITH_CLEANUP_FINISH);
3176 TARGET(WITH_CLEANUP_FINISH) {
3177 PyObject *res = POP();
3178 PyObject *exc = POP();
3179 int err;
3180
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003181 if (exc != Py_None)
3182 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 else
3184 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003185
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003187 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003189 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 else if (err > 0) {
3192 err = 0;
3193 /* There was an exception and a True return */
3194 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3195 }
3196 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003199
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003200 TARGET(CALL_FUNCTION) {
3201 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 PCALL(PCALL_ALL);
3203 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003204#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003205 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003206#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003207 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003208#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003210 PUSH(res);
3211 if (res == NULL)
3212 goto error;
3213 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
3217 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
3218 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003219 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 int na = oparg & 0xff;
3221 int nk = (oparg>>8) & 0xff;
3222 int flags = (opcode - CALL_FUNCTION) & 3;
3223 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 PCALL(PCALL_ALL);
3226 if (flags & CALL_FLAG_VAR)
3227 n++;
3228 if (flags & CALL_FLAG_KW)
3229 n++;
3230 pfunc = stack_pointer - n - 1;
3231 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00003234 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 PyObject *self = PyMethod_GET_SELF(func);
3236 Py_INCREF(self);
3237 func = PyMethod_GET_FUNCTION(func);
3238 Py_INCREF(func);
Serhiy Storchaka48842712016-04-06 09:45:48 +03003239 Py_XSETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00003241 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 } else
3243 Py_INCREF(func);
3244 sp = stack_pointer;
3245 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003246 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 READ_TIMESTAMP(intr1);
3248 stack_pointer = sp;
3249 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003252 PyObject *o = POP();
3253 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003255 PUSH(res);
3256 if (res == NULL)
3257 goto error;
3258 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
3262 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003263 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 int posdefaults = oparg & 0xff;
3265 int kwdefaults = (oparg>>8) & 0xff;
3266 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003267
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 PyObject *qualname = POP(); /* qualname */
3269 PyObject *code = POP(); /* code object */
3270 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
3271 Py_DECREF(code);
3272 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003273
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003274 if (func == NULL)
3275 goto error;
3276
3277 if (opcode == MAKE_CLOSURE) {
3278 PyObject *closure = POP();
3279 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 Py_DECREF(func);
3282 Py_DECREF(closure);
3283 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003287
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003288 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003290 PyObject *names = POP(); /* names of args with annotations */
3291 PyObject *anns = PyDict_New();
3292 if (anns == NULL) {
3293 Py_DECREF(func);
3294 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003296 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 assert(num_annotations == name_ix+1);
3298 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 PyObject *name, *value;
3300 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003302 name = PyTuple_GET_ITEM(names, name_ix);
3303 value = POP();
3304 err = PyDict_SetItem(anns, name, value);
3305 Py_DECREF(value);
3306 if (err != 0) {
3307 Py_DECREF(anns);
3308 Py_DECREF(func);
3309 goto error;
3310 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003312
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003313 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 /* Can't happen unless
3315 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 Py_DECREF(anns);
3317 Py_DECREF(func);
3318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003320 Py_DECREF(anns);
3321 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 if (kwdefaults > 0) {
3326 PyObject *defs = PyDict_New();
3327 if (defs == NULL) {
3328 Py_DECREF(func);
3329 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 }
3331 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003332 PyObject *v = POP(); /* default value */
3333 PyObject *key = POP(); /* kw only arg name */
3334 int err = PyDict_SetItem(defs, key, v);
3335 Py_DECREF(v);
3336 Py_DECREF(key);
3337 if (err != 0) {
3338 Py_DECREF(defs);
3339 Py_DECREF(func);
3340 goto error;
3341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003343 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 /* Can't happen unless
3345 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003346 Py_DECREF(func);
3347 Py_DECREF(defs);
3348 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003350 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05003352 if (posdefaults > 0) {
3353 PyObject *defs = PyTuple_New(posdefaults);
3354 if (defs == NULL) {
3355 Py_DECREF(func);
3356 goto error;
3357 }
3358 while (--posdefaults >= 0)
3359 PyTuple_SET_ITEM(defs, posdefaults, POP());
3360 if (PyFunction_SetDefaults(func, defs) != 0) {
3361 /* Can't happen unless
3362 PyFunction_SetDefaults changes. */
3363 Py_DECREF(defs);
3364 Py_DECREF(func);
3365 goto error;
3366 }
3367 Py_DECREF(defs);
3368 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003369 PUSH(func);
3370 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003372
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003373 TARGET(BUILD_SLICE) {
3374 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003376 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003378 step = NULL;
3379 stop = POP();
3380 start = TOP();
3381 slice = PySlice_New(start, stop, step);
3382 Py_DECREF(start);
3383 Py_DECREF(stop);
3384 Py_XDECREF(step);
3385 SET_TOP(slice);
3386 if (slice == NULL)
3387 goto error;
3388 DISPATCH();
3389 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003390
Eric V. Smitha78c7952015-11-03 12:45:05 -05003391 TARGET(FORMAT_VALUE) {
3392 /* Handles f-string value formatting. */
3393 PyObject *result;
3394 PyObject *fmt_spec;
3395 PyObject *value;
3396 PyObject *(*conv_fn)(PyObject *);
3397 int which_conversion = oparg & FVC_MASK;
3398 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3399
3400 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003401 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003402
3403 /* See if any conversion is specified. */
3404 switch (which_conversion) {
3405 case FVC_STR: conv_fn = PyObject_Str; break;
3406 case FVC_REPR: conv_fn = PyObject_Repr; break;
3407 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3408
3409 /* Must be 0 (meaning no conversion), since only four
3410 values are allowed by (oparg & FVC_MASK). */
3411 default: conv_fn = NULL; break;
3412 }
3413
3414 /* If there's a conversion function, call it and replace
3415 value with that result. Otherwise, just use value,
3416 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003417 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003418 result = conv_fn(value);
3419 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003420 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003421 Py_XDECREF(fmt_spec);
3422 goto error;
3423 }
3424 value = result;
3425 }
3426
3427 /* If value is a unicode object, and there's no fmt_spec,
3428 then we know the result of format(value) is value
3429 itself. In that case, skip calling format(). I plan to
3430 move this optimization in to PyObject_Format()
3431 itself. */
3432 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3433 /* Do nothing, just transfer ownership to result. */
3434 result = value;
3435 } else {
3436 /* Actually call format(). */
3437 result = PyObject_Format(value, fmt_spec);
3438 Py_DECREF(value);
3439 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003440 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003441 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003442 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003443 }
3444
Eric V. Smith135d5f42016-02-05 18:23:08 -05003445 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003446 DISPATCH();
3447 }
3448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003449 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 opcode = NEXTOP();
3451 oparg = oparg<<16 | NEXTARG();
3452 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003454
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003455
Antoine Pitrou042b1282010-08-13 21:15:58 +00003456#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003458#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 default:
3460 fprintf(stderr,
3461 "XXX lineno: %d, opcode: %d\n",
3462 PyFrame_GetLineNumber(f),
3463 opcode);
3464 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003465 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003466
3467#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003469#endif
3470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 /* This should never be reached. Every opcode should end with DISPATCH()
3474 or goto error. */
3475 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003476
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003477error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003479
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 assert(why == WHY_NOT);
3481 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003482
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003484#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003485 if (!PyErr_Occurred())
3486 PyErr_SetString(PyExc_SystemError,
3487 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003488#else
3489 assert(PyErr_Occurred());
3490#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003491
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003492 /* Log traceback info. */
3493 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003494
Benjamin Peterson51f46162013-01-23 08:38:47 -05003495 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003496 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3497 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003498
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003499fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 assert(why != WHY_NOT);
3501
3502 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 while (why != WHY_NOT && f->f_iblock > 0) {
3504 /* Peek at the current block. */
3505 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 assert(why != WHY_YIELD);
3508 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3509 why = WHY_NOT;
3510 JUMPTO(PyLong_AS_LONG(retval));
3511 Py_DECREF(retval);
3512 break;
3513 }
3514 /* Now we have to pop the block. */
3515 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 if (b->b_type == EXCEPT_HANDLER) {
3518 UNWIND_EXCEPT_HANDLER(b);
3519 continue;
3520 }
3521 UNWIND_BLOCK(b);
3522 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3523 why = WHY_NOT;
3524 JUMPTO(b->b_handler);
3525 break;
3526 }
3527 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3528 || b->b_type == SETUP_FINALLY)) {
3529 PyObject *exc, *val, *tb;
3530 int handler = b->b_handler;
3531 /* Beware, this invalidates all b->b_* fields */
3532 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3533 PUSH(tstate->exc_traceback);
3534 PUSH(tstate->exc_value);
3535 if (tstate->exc_type != NULL) {
3536 PUSH(tstate->exc_type);
3537 }
3538 else {
3539 Py_INCREF(Py_None);
3540 PUSH(Py_None);
3541 }
3542 PyErr_Fetch(&exc, &val, &tb);
3543 /* Make the raw exception data
3544 available to the handler,
3545 so a program can emulate the
3546 Python main loop. */
3547 PyErr_NormalizeException(
3548 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003549 if (tb != NULL)
3550 PyException_SetTraceback(val, tb);
3551 else
3552 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 Py_INCREF(exc);
3554 tstate->exc_type = exc;
3555 Py_INCREF(val);
3556 tstate->exc_value = val;
3557 tstate->exc_traceback = tb;
3558 if (tb == NULL)
3559 tb = Py_None;
3560 Py_INCREF(tb);
3561 PUSH(tb);
3562 PUSH(val);
3563 PUSH(exc);
3564 why = WHY_NOT;
3565 JUMPTO(handler);
3566 break;
3567 }
3568 if (b->b_type == SETUP_FINALLY) {
3569 if (why & (WHY_RETURN | WHY_CONTINUE))
3570 PUSH(retval);
3571 PUSH(PyLong_FromLong((long)why));
3572 why = WHY_NOT;
3573 JUMPTO(b->b_handler);
3574 break;
3575 }
3576 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 if (why != WHY_NOT)
3581 break;
3582 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003583
Victor Stinnerace47d72013-07-18 01:41:08 +02003584 assert(!PyErr_Occurred());
3585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 assert(why != WHY_YIELD);
3589 /* Pop remaining stack entries. */
3590 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003591 PyObject *o = POP();
3592 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003595 if (why != WHY_RETURN)
3596 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003597
Victor Stinner4a7cc882015-03-06 23:35:27 +01003598 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003599
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003600fast_yield:
Yury Selivanov5376ba92015-06-22 12:19:30 -04003601 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003602
Benjamin Petersonac913412011-07-03 16:25:11 -05003603 /* The purpose of this block is to put aside the generator's exception
3604 state and restore that of the calling frame. If the current
3605 exception state is from the caller, we clear the exception values
3606 on the generator frame, so they are not swapped back in latter. The
3607 origin of the current exception state is determined by checking for
3608 except handler blocks, which we must be in iff a new exception
3609 state came into existence in this frame. (An uncaught exception
3610 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3611 int i;
3612 for (i = 0; i < f->f_iblock; i++)
3613 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3614 break;
3615 if (i == f->f_iblock)
3616 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003617 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003618 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003619 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003620 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003623 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003625 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3626 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003628 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003629 why = WHY_EXCEPTION;
3630 }
3631 }
3632 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003633 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3634 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 PyTrace_RETURN, NULL);
3636 }
3637 }
3638 if (tstate->c_profilefunc) {
3639 if (why == WHY_EXCEPTION)
3640 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003641 tstate->c_profileobj,
3642 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003644 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3645 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003647 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003648 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 }
3650 }
3651 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003653 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003654exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003656 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003658
Victor Stinnerefde1462015-03-21 15:04:43 +01003659 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003660}
3661
Benjamin Petersonb204a422011-06-05 22:04:07 -05003662static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003663format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3664{
3665 int err;
3666 Py_ssize_t len = PyList_GET_SIZE(names);
3667 PyObject *name_str, *comma, *tail, *tmp;
3668
3669 assert(PyList_CheckExact(names));
3670 assert(len >= 1);
3671 /* Deal with the joys of natural language. */
3672 switch (len) {
3673 case 1:
3674 name_str = PyList_GET_ITEM(names, 0);
3675 Py_INCREF(name_str);
3676 break;
3677 case 2:
3678 name_str = PyUnicode_FromFormat("%U and %U",
3679 PyList_GET_ITEM(names, len - 2),
3680 PyList_GET_ITEM(names, len - 1));
3681 break;
3682 default:
3683 tail = PyUnicode_FromFormat(", %U, and %U",
3684 PyList_GET_ITEM(names, len - 2),
3685 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003686 if (tail == NULL)
3687 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003688 /* Chop off the last two objects in the list. This shouldn't actually
3689 fail, but we can't be too careful. */
3690 err = PyList_SetSlice(names, len - 2, len, NULL);
3691 if (err == -1) {
3692 Py_DECREF(tail);
3693 return;
3694 }
3695 /* Stitch everything up into a nice comma-separated list. */
3696 comma = PyUnicode_FromString(", ");
3697 if (comma == NULL) {
3698 Py_DECREF(tail);
3699 return;
3700 }
3701 tmp = PyUnicode_Join(comma, names);
3702 Py_DECREF(comma);
3703 if (tmp == NULL) {
3704 Py_DECREF(tail);
3705 return;
3706 }
3707 name_str = PyUnicode_Concat(tmp, tail);
3708 Py_DECREF(tmp);
3709 Py_DECREF(tail);
3710 break;
3711 }
3712 if (name_str == NULL)
3713 return;
3714 PyErr_Format(PyExc_TypeError,
3715 "%U() missing %i required %s argument%s: %U",
3716 co->co_name,
3717 len,
3718 kind,
3719 len == 1 ? "" : "s",
3720 name_str);
3721 Py_DECREF(name_str);
3722}
3723
3724static void
3725missing_arguments(PyCodeObject *co, int missing, int defcount,
3726 PyObject **fastlocals)
3727{
3728 int i, j = 0;
3729 int start, end;
3730 int positional = defcount != -1;
3731 const char *kind = positional ? "positional" : "keyword-only";
3732 PyObject *missing_names;
3733
3734 /* Compute the names of the arguments that are missing. */
3735 missing_names = PyList_New(missing);
3736 if (missing_names == NULL)
3737 return;
3738 if (positional) {
3739 start = 0;
3740 end = co->co_argcount - defcount;
3741 }
3742 else {
3743 start = co->co_argcount;
3744 end = start + co->co_kwonlyargcount;
3745 }
3746 for (i = start; i < end; i++) {
3747 if (GETLOCAL(i) == NULL) {
3748 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3749 PyObject *name = PyObject_Repr(raw);
3750 if (name == NULL) {
3751 Py_DECREF(missing_names);
3752 return;
3753 }
3754 PyList_SET_ITEM(missing_names, j++, name);
3755 }
3756 }
3757 assert(j == missing);
3758 format_missing(kind, co, missing_names);
3759 Py_DECREF(missing_names);
3760}
3761
3762static void
3763too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003764{
3765 int plural;
3766 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003767 int i;
3768 PyObject *sig, *kwonly_sig;
3769
Benjamin Petersone109c702011-06-24 09:37:26 -05003770 assert((co->co_flags & CO_VARARGS) == 0);
3771 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003772 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003773 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003774 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003775 if (defcount) {
3776 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003777 plural = 1;
3778 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3779 }
3780 else {
3781 plural = co->co_argcount != 1;
3782 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3783 }
3784 if (sig == NULL)
3785 return;
3786 if (kwonly_given) {
3787 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3788 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3789 kwonly_given != 1 ? "s" : "");
3790 if (kwonly_sig == NULL) {
3791 Py_DECREF(sig);
3792 return;
3793 }
3794 }
3795 else {
3796 /* This will not fail. */
3797 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003798 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003799 }
3800 PyErr_Format(PyExc_TypeError,
3801 "%U() takes %U positional argument%s but %d%U %s given",
3802 co->co_name,
3803 sig,
3804 plural ? "s" : "",
3805 given,
3806 kwonly_sig,
3807 given == 1 && !kwonly_given ? "was" : "were");
3808 Py_DECREF(sig);
3809 Py_DECREF(kwonly_sig);
3810}
3811
Guido van Rossumc2e20742006-02-27 22:32:47 +00003812/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003813 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003814 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003815
Victor Stinner40ee3012014-06-16 15:59:28 +02003816static PyObject *
3817_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003819 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3820 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003821{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003822 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003823 PyFrameObject *f;
3824 PyObject *retval = NULL;
3825 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003826 PyThreadState *tstate = PyThreadState_GET();
3827 PyObject *x, *u;
3828 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 int i;
3830 int n = argcount;
3831 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 if (globals == NULL) {
3834 PyErr_SetString(PyExc_SystemError,
3835 "PyEval_EvalCodeEx: NULL globals");
3836 return NULL;
3837 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 assert(tstate != NULL);
3840 assert(globals != NULL);
3841 f = PyFrame_New(tstate, co, globals, locals);
3842 if (f == NULL)
3843 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 fastlocals = f->f_localsplus;
3846 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003847
Benjamin Petersonb204a422011-06-05 22:04:07 -05003848 /* Parse arguments. */
3849 if (co->co_flags & CO_VARKEYWORDS) {
3850 kwdict = PyDict_New();
3851 if (kwdict == NULL)
3852 goto fail;
3853 i = total_args;
3854 if (co->co_flags & CO_VARARGS)
3855 i++;
3856 SETLOCAL(i, kwdict);
3857 }
3858 if (argcount > co->co_argcount)
3859 n = co->co_argcount;
3860 for (i = 0; i < n; i++) {
3861 x = args[i];
3862 Py_INCREF(x);
3863 SETLOCAL(i, x);
3864 }
3865 if (co->co_flags & CO_VARARGS) {
3866 u = PyTuple_New(argcount - n);
3867 if (u == NULL)
3868 goto fail;
3869 SETLOCAL(total_args, u);
3870 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 x = args[i];
3872 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003873 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003875 }
3876 for (i = 0; i < kwcount; i++) {
3877 PyObject **co_varnames;
3878 PyObject *keyword = kws[2*i];
3879 PyObject *value = kws[2*i + 1];
3880 int j;
3881 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3882 PyErr_Format(PyExc_TypeError,
3883 "%U() keywords must be strings",
3884 co->co_name);
3885 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003887 /* Speed hack: do raw pointer compares. As names are
3888 normally interned this should almost always hit. */
3889 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3890 for (j = 0; j < total_args; j++) {
3891 PyObject *nm = co_varnames[j];
3892 if (nm == keyword)
3893 goto kw_found;
3894 }
3895 /* Slow fallback, just in case */
3896 for (j = 0; j < total_args; j++) {
3897 PyObject *nm = co_varnames[j];
3898 int cmp = PyObject_RichCompareBool(
3899 keyword, nm, Py_EQ);
3900 if (cmp > 0)
3901 goto kw_found;
3902 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003904 }
3905 if (j >= total_args && kwdict == NULL) {
3906 PyErr_Format(PyExc_TypeError,
3907 "%U() got an unexpected "
3908 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 co->co_name,
3910 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003911 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003912 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003913 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3914 goto fail;
3915 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 continue;
3917 kw_found:
3918 if (GETLOCAL(j) != NULL) {
3919 PyErr_Format(PyExc_TypeError,
3920 "%U() got multiple "
3921 "values for argument '%S'",
3922 co->co_name,
3923 keyword);
3924 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003926 Py_INCREF(value);
3927 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003930 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 goto fail;
3932 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003933 if (argcount < co->co_argcount) {
3934 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003935 int missing = 0;
3936 for (i = argcount; i < m; i++)
3937 if (GETLOCAL(i) == NULL)
3938 missing++;
3939 if (missing) {
3940 missing_arguments(co, missing, defcount, fastlocals);
3941 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003942 }
3943 if (n > m)
3944 i = n - m;
3945 else
3946 i = 0;
3947 for (; i < defcount; i++) {
3948 if (GETLOCAL(m+i) == NULL) {
3949 PyObject *def = defs[i];
3950 Py_INCREF(def);
3951 SETLOCAL(m+i, def);
3952 }
3953 }
3954 }
3955 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003956 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003957 for (i = co->co_argcount; i < total_args; i++) {
3958 PyObject *name;
3959 if (GETLOCAL(i) != NULL)
3960 continue;
3961 name = PyTuple_GET_ITEM(co->co_varnames, i);
3962 if (kwdefs != NULL) {
3963 PyObject *def = PyDict_GetItem(kwdefs, name);
3964 if (def) {
3965 Py_INCREF(def);
3966 SETLOCAL(i, def);
3967 continue;
3968 }
3969 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003970 missing++;
3971 }
3972 if (missing) {
3973 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003974 goto fail;
3975 }
3976 }
3977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003979 vars into frame. */
3980 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003982 int arg;
3983 /* Possibly account for the cell variable being an argument. */
3984 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003985 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003986 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003987 /* Clear the local copy. */
3988 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003989 }
3990 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003991 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003992 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003993 if (c == NULL)
3994 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003995 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003996 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003997 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3998 PyObject *o = PyTuple_GET_ITEM(closure, i);
3999 Py_INCREF(o);
4000 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004002
Yury Selivanov5376ba92015-06-22 12:19:30 -04004003 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004004 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004005 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004006 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004007
4008 if (is_coro && tstate->in_coroutine_wrapper) {
4009 assert(coro_wrapper != NULL);
4010 PyErr_Format(PyExc_RuntimeError,
4011 "coroutine wrapper %.200R attempted "
4012 "to recursively wrap %.200R",
4013 coro_wrapper,
4014 co);
4015 goto fail;
4016 }
Yury Selivanov75445082015-05-11 22:57:16 -04004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 /* Don't need to keep the reference to f_back, it will be set
4019 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004020 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 /* Create a new generator that owns the ready to run frame
4025 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004026 if (is_coro) {
4027 gen = PyCoro_New(f, name, qualname);
4028 } else {
4029 gen = PyGen_NewWithQualName(f, name, qualname);
4030 }
Yury Selivanov75445082015-05-11 22:57:16 -04004031 if (gen == NULL)
4032 return NULL;
4033
Yury Selivanov94c22632015-06-04 10:16:51 -04004034 if (is_coro && coro_wrapper != NULL) {
4035 PyObject *wrapped;
4036 tstate->in_coroutine_wrapper = 1;
4037 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4038 tstate->in_coroutine_wrapper = 0;
4039 return wrapped;
4040 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004041
Yury Selivanov75445082015-05-11 22:57:16 -04004042 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004046
Thomas Woutersce272b62007-09-19 21:19:28 +00004047fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 /* decref'ing the frame can cause __del__ methods to get invoked,
4050 which can call back into Python. While we're done with the
4051 current Python frame (f), the associated C stack is still in use,
4052 so recursion_depth must be boosted for the duration.
4053 */
4054 assert(tstate != NULL);
4055 ++tstate->recursion_depth;
4056 Py_DECREF(f);
4057 --tstate->recursion_depth;
4058 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004059}
4060
Victor Stinner40ee3012014-06-16 15:59:28 +02004061PyObject *
4062PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4063 PyObject **args, int argcount, PyObject **kws, int kwcount,
4064 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4065{
4066 return _PyEval_EvalCodeWithName(_co, globals, locals,
4067 args, argcount, kws, kwcount,
4068 defs, defcount, kwdefs, closure,
4069 NULL, NULL);
4070}
Tim Peters5ca576e2001-06-18 22:08:13 +00004071
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004072static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004073special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004075 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004076 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004078 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004079 return NULL;
4080 }
4081 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004082}
4083
4084
Benjamin Peterson87880242011-07-03 16:48:31 -05004085/* These 3 functions deal with the exception state of generators. */
4086
4087static void
4088save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4089{
4090 PyObject *type, *value, *traceback;
4091 Py_XINCREF(tstate->exc_type);
4092 Py_XINCREF(tstate->exc_value);
4093 Py_XINCREF(tstate->exc_traceback);
4094 type = f->f_exc_type;
4095 value = f->f_exc_value;
4096 traceback = f->f_exc_traceback;
4097 f->f_exc_type = tstate->exc_type;
4098 f->f_exc_value = tstate->exc_value;
4099 f->f_exc_traceback = tstate->exc_traceback;
4100 Py_XDECREF(type);
4101 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004102 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004103}
4104
4105static void
4106swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4107{
4108 PyObject *tmp;
4109 tmp = tstate->exc_type;
4110 tstate->exc_type = f->f_exc_type;
4111 f->f_exc_type = tmp;
4112 tmp = tstate->exc_value;
4113 tstate->exc_value = f->f_exc_value;
4114 f->f_exc_value = tmp;
4115 tmp = tstate->exc_traceback;
4116 tstate->exc_traceback = f->f_exc_traceback;
4117 f->f_exc_traceback = tmp;
4118}
4119
4120static void
4121restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4122{
4123 PyObject *type, *value, *tb;
4124 type = tstate->exc_type;
4125 value = tstate->exc_value;
4126 tb = tstate->exc_traceback;
4127 tstate->exc_type = f->f_exc_type;
4128 tstate->exc_value = f->f_exc_value;
4129 tstate->exc_traceback = f->f_exc_traceback;
4130 f->f_exc_type = NULL;
4131 f->f_exc_value = NULL;
4132 f->f_exc_traceback = NULL;
4133 Py_XDECREF(type);
4134 Py_XDECREF(value);
4135 Py_XDECREF(tb);
4136}
4137
4138
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004139/* Logic for the raise statement (too complicated for inlining).
4140 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004141static int
Collin Winter828f04a2007-08-31 00:04:24 +00004142do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 if (exc == NULL) {
4147 /* Reraise */
4148 PyThreadState *tstate = PyThreadState_GET();
4149 PyObject *tb;
4150 type = tstate->exc_type;
4151 value = tstate->exc_value;
4152 tb = tstate->exc_traceback;
4153 if (type == Py_None) {
4154 PyErr_SetString(PyExc_RuntimeError,
4155 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004156 return 0;
4157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 Py_XINCREF(type);
4159 Py_XINCREF(value);
4160 Py_XINCREF(tb);
4161 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004162 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 /* We support the following forms of raise:
4166 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004167 raise <instance>
4168 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 if (PyExceptionClass_Check(exc)) {
4171 type = exc;
4172 value = PyObject_CallObject(exc, NULL);
4173 if (value == NULL)
4174 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004175 if (!PyExceptionInstance_Check(value)) {
4176 PyErr_Format(PyExc_TypeError,
4177 "calling %R should have returned an instance of "
4178 "BaseException, not %R",
4179 type, Py_TYPE(value));
4180 goto raise_error;
4181 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 }
4183 else if (PyExceptionInstance_Check(exc)) {
4184 value = exc;
4185 type = PyExceptionInstance_Class(exc);
4186 Py_INCREF(type);
4187 }
4188 else {
4189 /* Not something you can raise. You get an exception
4190 anyway, just not what you specified :-) */
4191 Py_DECREF(exc);
4192 PyErr_SetString(PyExc_TypeError,
4193 "exceptions must derive from BaseException");
4194 goto raise_error;
4195 }
Collin Winter828f04a2007-08-31 00:04:24 +00004196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 if (cause) {
4198 PyObject *fixed_cause;
4199 if (PyExceptionClass_Check(cause)) {
4200 fixed_cause = PyObject_CallObject(cause, NULL);
4201 if (fixed_cause == NULL)
4202 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004203 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004205 else if (PyExceptionInstance_Check(cause)) {
4206 fixed_cause = cause;
4207 }
4208 else if (cause == Py_None) {
4209 Py_DECREF(cause);
4210 fixed_cause = NULL;
4211 }
4212 else {
4213 PyErr_SetString(PyExc_TypeError,
4214 "exception causes must derive from "
4215 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 goto raise_error;
4217 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004218 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 }
Collin Winter828f04a2007-08-31 00:04:24 +00004220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 PyErr_SetObject(type, value);
4222 /* PyErr_SetObject incref's its arguments */
4223 Py_XDECREF(value);
4224 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004225 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004226
4227raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 Py_XDECREF(value);
4229 Py_XDECREF(type);
4230 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004231 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004232}
4233
Tim Petersd6d010b2001-06-21 02:49:55 +00004234/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004235 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004236
Guido van Rossum0368b722007-05-11 16:50:42 +00004237 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4238 with a variable target.
4239*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004240
Barry Warsawe42b18f1997-08-25 22:13:04 +00004241static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004242unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004243{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 int i = 0, j = 0;
4245 Py_ssize_t ll = 0;
4246 PyObject *it; /* iter(v) */
4247 PyObject *w;
4248 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004252 it = PyObject_GetIter(v);
4253 if (it == NULL)
4254 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 for (; i < argcnt; i++) {
4257 w = PyIter_Next(it);
4258 if (w == NULL) {
4259 /* Iterator done, via error or exhaustion. */
4260 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004261 if (argcntafter == -1) {
4262 PyErr_Format(PyExc_ValueError,
4263 "not enough values to unpack (expected %d, got %d)",
4264 argcnt, i);
4265 }
4266 else {
4267 PyErr_Format(PyExc_ValueError,
4268 "not enough values to unpack "
4269 "(expected at least %d, got %d)",
4270 argcnt + argcntafter, i);
4271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 }
4273 goto Error;
4274 }
4275 *--sp = w;
4276 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 if (argcntafter == -1) {
4279 /* We better have exhausted the iterator now. */
4280 w = PyIter_Next(it);
4281 if (w == NULL) {
4282 if (PyErr_Occurred())
4283 goto Error;
4284 Py_DECREF(it);
4285 return 1;
4286 }
4287 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004288 PyErr_Format(PyExc_ValueError,
4289 "too many values to unpack (expected %d)",
4290 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 goto Error;
4292 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 l = PySequence_List(it);
4295 if (l == NULL)
4296 goto Error;
4297 *--sp = l;
4298 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 ll = PyList_GET_SIZE(l);
4301 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004302 PyErr_Format(PyExc_ValueError,
4303 "not enough values to unpack (expected at least %d, got %zd)",
4304 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 goto Error;
4306 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004308 /* Pop the "after-variable" args off the list. */
4309 for (j = argcntafter; j > 0; j--, i++) {
4310 *--sp = PyList_GET_ITEM(l, ll - j);
4311 }
4312 /* Resize the list. */
4313 Py_SIZE(l) = ll - argcntafter;
4314 Py_DECREF(it);
4315 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004316
Tim Petersd6d010b2001-06-21 02:49:55 +00004317Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 for (; i > 0; i--, sp++)
4319 Py_DECREF(*sp);
4320 Py_XDECREF(it);
4321 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004322}
4323
4324
Guido van Rossum96a42c81992-01-12 02:29:51 +00004325#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004326static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004327prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 printf("%s ", str);
4330 if (PyObject_Print(v, stdout, 0) != 0)
4331 PyErr_Clear(); /* Don't know what else to do */
4332 printf("\n");
4333 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004334}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004335#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004336
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004337static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004338call_exc_trace(Py_tracefunc func, PyObject *self,
4339 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004340{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004341 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004342 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004343 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 if (value == NULL) {
4345 value = Py_None;
4346 Py_INCREF(value);
4347 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004348 PyErr_NormalizeException(&type, &value, &orig_traceback);
4349 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 arg = PyTuple_Pack(3, type, value, traceback);
4351 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004352 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 return;
4354 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004355 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 Py_DECREF(arg);
4357 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004358 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 else {
4360 Py_XDECREF(type);
4361 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004362 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004363 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004364}
4365
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004366static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004367call_trace_protected(Py_tracefunc func, PyObject *obj,
4368 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 PyObject *type, *value, *traceback;
4372 int err;
4373 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004374 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 if (err == 0)
4376 {
4377 PyErr_Restore(type, value, traceback);
4378 return 0;
4379 }
4380 else {
4381 Py_XDECREF(type);
4382 Py_XDECREF(value);
4383 Py_XDECREF(traceback);
4384 return -1;
4385 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004386}
4387
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004388static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004389call_trace(Py_tracefunc func, PyObject *obj,
4390 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 int result;
4394 if (tstate->tracing)
4395 return 0;
4396 tstate->tracing++;
4397 tstate->use_tracing = 0;
4398 result = func(obj, frame, what, arg);
4399 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4400 || (tstate->c_profilefunc != NULL));
4401 tstate->tracing--;
4402 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004403}
4404
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004405PyObject *
4406_PyEval_CallTracing(PyObject *func, PyObject *args)
4407{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004408 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 int save_tracing = tstate->tracing;
4410 int save_use_tracing = tstate->use_tracing;
4411 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 tstate->tracing = 0;
4414 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4415 || (tstate->c_profilefunc != NULL));
4416 result = PyObject_Call(func, args, NULL);
4417 tstate->tracing = save_tracing;
4418 tstate->use_tracing = save_use_tracing;
4419 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004420}
4421
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004422/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004423static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004424maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004425 PyThreadState *tstate, PyFrameObject *frame,
4426 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 int result = 0;
4429 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 /* If the last instruction executed isn't in the current
4432 instruction window, reset the window.
4433 */
4434 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4435 PyAddrPair bounds;
4436 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4437 &bounds);
4438 *instr_lb = bounds.ap_lower;
4439 *instr_ub = bounds.ap_upper;
4440 }
4441 /* If the last instruction falls at the start of a line or if
4442 it represents a jump backwards, update the frame's line
4443 number and call the trace function. */
4444 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4445 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004446 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 }
4448 *instr_prev = frame->f_lasti;
4449 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004450}
4451
Fred Drake5755ce62001-06-27 19:19:46 +00004452void
4453PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 PyThreadState *tstate = PyThreadState_GET();
4456 PyObject *temp = tstate->c_profileobj;
4457 Py_XINCREF(arg);
4458 tstate->c_profilefunc = NULL;
4459 tstate->c_profileobj = NULL;
4460 /* Must make sure that tracing is not ignored if 'temp' is freed */
4461 tstate->use_tracing = tstate->c_tracefunc != NULL;
4462 Py_XDECREF(temp);
4463 tstate->c_profilefunc = func;
4464 tstate->c_profileobj = arg;
4465 /* Flag that tracing or profiling is turned on */
4466 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004467}
4468
4469void
4470PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 PyThreadState *tstate = PyThreadState_GET();
4473 PyObject *temp = tstate->c_traceobj;
4474 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4475 Py_XINCREF(arg);
4476 tstate->c_tracefunc = NULL;
4477 tstate->c_traceobj = NULL;
4478 /* Must make sure that profiling is not ignored if 'temp' is freed */
4479 tstate->use_tracing = tstate->c_profilefunc != NULL;
4480 Py_XDECREF(temp);
4481 tstate->c_tracefunc = func;
4482 tstate->c_traceobj = arg;
4483 /* Flag that tracing or profiling is turned on */
4484 tstate->use_tracing = ((func != NULL)
4485 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004486}
4487
Yury Selivanov75445082015-05-11 22:57:16 -04004488void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004489_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004490{
4491 PyThreadState *tstate = PyThreadState_GET();
4492
Yury Selivanov75445082015-05-11 22:57:16 -04004493 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004494 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004495}
4496
4497PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004498_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004499{
4500 PyThreadState *tstate = PyThreadState_GET();
4501 return tstate->coroutine_wrapper;
4502}
4503
Guido van Rossumb209a111997-04-29 18:18:01 +00004504PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004505PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 PyFrameObject *current_frame = PyEval_GetFrame();
4508 if (current_frame == NULL)
4509 return PyThreadState_GET()->interp->builtins;
4510 else
4511 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004512}
4513
Guido van Rossumb209a111997-04-29 18:18:01 +00004514PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004515PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004518 if (current_frame == NULL) {
4519 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004521 }
4522
4523 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4524 return NULL;
4525
4526 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004527 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004528}
4529
Guido van Rossumb209a111997-04-29 18:18:01 +00004530PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004531PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004532{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004533 PyFrameObject *current_frame = PyEval_GetFrame();
4534 if (current_frame == NULL)
4535 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004536
4537 assert(current_frame->f_globals != NULL);
4538 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004539}
4540
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004541PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004542PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 PyThreadState *tstate = PyThreadState_GET();
4545 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004546}
4547
Guido van Rossum6135a871995-01-09 17:53:26 +00004548int
Tim Peters5ba58662001-07-16 02:29:45 +00004549PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004550{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004551 PyFrameObject *current_frame = PyEval_GetFrame();
4552 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004554 if (current_frame != NULL) {
4555 const int codeflags = current_frame->f_code->co_flags;
4556 const int compilerflags = codeflags & PyCF_MASK;
4557 if (compilerflags) {
4558 result = 1;
4559 cf->cf_flags |= compilerflags;
4560 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004561#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 if (codeflags & CO_GENERATOR_ALLOWED) {
4563 result = 1;
4564 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4565 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004566#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 }
4568 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004569}
4570
Guido van Rossum3f5da241990-12-20 15:06:42 +00004571
Guido van Rossum681d79a1995-07-18 14:51:37 +00004572/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004573 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004574
Guido van Rossumb209a111997-04-29 18:18:01 +00004575PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004576PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004578 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004579
Victor Stinner59b356d2015-03-16 11:52:32 +01004580#ifdef Py_DEBUG
4581 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4582 set. It raises a new exception if parameters are invalid or if
4583 PyTuple_New() fails, and so the original exception is lost. */
4584 assert(!PyErr_Occurred());
4585#endif
4586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004587 if (arg == NULL) {
4588 arg = PyTuple_New(0);
4589 if (arg == NULL)
4590 return NULL;
4591 }
4592 else if (!PyTuple_Check(arg)) {
4593 PyErr_SetString(PyExc_TypeError,
4594 "argument list must be a tuple");
4595 return NULL;
4596 }
4597 else
4598 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004600 if (kw != NULL && !PyDict_Check(kw)) {
4601 PyErr_SetString(PyExc_TypeError,
4602 "keyword list must be a dictionary");
4603 Py_DECREF(arg);
4604 return NULL;
4605 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 result = PyObject_Call(func, arg, kw);
4608 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004610 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004611}
4612
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004613const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004614PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004615{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 if (PyMethod_Check(func))
4617 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4618 else if (PyFunction_Check(func))
4619 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4620 else if (PyCFunction_Check(func))
4621 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4622 else
4623 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004624}
4625
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004626const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004627PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004628{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 if (PyMethod_Check(func))
4630 return "()";
4631 else if (PyFunction_Check(func))
4632 return "()";
4633 else if (PyCFunction_Check(func))
4634 return "()";
4635 else
4636 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004637}
4638
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004639static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004640err_args(PyObject *func, int flags, int nargs)
4641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004642 if (flags & METH_NOARGS)
4643 PyErr_Format(PyExc_TypeError,
4644 "%.200s() takes no arguments (%d given)",
4645 ((PyCFunctionObject *)func)->m_ml->ml_name,
4646 nargs);
4647 else
4648 PyErr_Format(PyExc_TypeError,
4649 "%.200s() takes exactly one argument (%d given)",
4650 ((PyCFunctionObject *)func)->m_ml->ml_name,
4651 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004652}
4653
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004654#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004655if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004656 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4657 tstate, tstate->frame, \
4658 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 x = NULL; \
4660 } \
4661 else { \
4662 x = call; \
4663 if (tstate->c_profilefunc != NULL) { \
4664 if (x == NULL) { \
4665 call_trace_protected(tstate->c_profilefunc, \
4666 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004667 tstate, tstate->frame, \
4668 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 /* XXX should pass (type, value, tb) */ \
4670 } else { \
4671 if (call_trace(tstate->c_profilefunc, \
4672 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004673 tstate, tstate->frame, \
4674 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 Py_DECREF(x); \
4676 x = NULL; \
4677 } \
4678 } \
4679 } \
4680 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004681} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004682 x = call; \
4683 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004684
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004685static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004686call_function(PyObject ***pp_stack, int oparg
4687#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004688 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004689#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 int na = oparg & 0xff;
4693 int nk = (oparg>>8) & 0xff;
4694 int n = na + 2 * nk;
4695 PyObject **pfunc = (*pp_stack) - n - 1;
4696 PyObject *func = *pfunc;
4697 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 /* Always dispatch PyCFunction first, because these are
4700 presumed to be the most frequent callable object.
4701 */
4702 if (PyCFunction_Check(func) && nk == 0) {
4703 int flags = PyCFunction_GET_FLAGS(func);
4704 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 PCALL(PCALL_CFUNCTION);
4707 if (flags & (METH_NOARGS | METH_O)) {
4708 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4709 PyObject *self = PyCFunction_GET_SELF(func);
4710 if (flags & METH_NOARGS && na == 0) {
4711 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004712
Victor Stinnerefde1462015-03-21 15:04:43 +01004713 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 }
4715 else if (flags & METH_O && na == 1) {
4716 PyObject *arg = EXT_POP(*pp_stack);
4717 C_TRACE(x, (*meth)(self,arg));
4718 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004719
Victor Stinnerefde1462015-03-21 15:04:43 +01004720 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 }
4722 else {
4723 err_args(func, flags, na);
4724 x = NULL;
4725 }
4726 }
4727 else {
4728 PyObject *callargs;
4729 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004730 if (callargs != NULL) {
4731 READ_TIMESTAMP(*pintr0);
4732 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4733 READ_TIMESTAMP(*pintr1);
4734 Py_XDECREF(callargs);
4735 }
4736 else {
4737 x = NULL;
4738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004740 }
4741 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4743 /* optimize access to bound methods */
4744 PyObject *self = PyMethod_GET_SELF(func);
4745 PCALL(PCALL_METHOD);
4746 PCALL(PCALL_BOUND_METHOD);
4747 Py_INCREF(self);
4748 func = PyMethod_GET_FUNCTION(func);
4749 Py_INCREF(func);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004750 Py_XSETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 na++;
4752 n++;
4753 } else
4754 Py_INCREF(func);
4755 READ_TIMESTAMP(*pintr0);
4756 if (PyFunction_Check(func))
4757 x = fast_function(func, pp_stack, n, na, nk);
4758 else
4759 x = do_call(func, pp_stack, na, nk);
4760 READ_TIMESTAMP(*pintr1);
4761 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004762
4763 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004766 /* Clear the stack of the function object. Also removes
4767 the arguments in case they weren't consumed already
4768 (fast_function() and err_args() leave them on the stack).
4769 */
4770 while ((*pp_stack) > pfunc) {
4771 w = EXT_POP(*pp_stack);
4772 Py_DECREF(w);
4773 PCALL(PCALL_POP);
4774 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004775
Victor Stinner4a7cc882015-03-06 23:35:27 +01004776 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004777 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004778}
4779
Jeremy Hylton192690e2002-08-16 18:36:11 +00004780/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004781 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004782 For the simplest case -- a function that takes only positional
4783 arguments and is called with only positional arguments -- it
4784 inlines the most primitive frame setup code from
4785 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4786 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004787*/
4788
4789static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004790fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4793 PyObject *globals = PyFunction_GET_GLOBALS(func);
4794 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4795 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Victor Stinner40ee3012014-06-16 15:59:28 +02004796 PyObject *name = ((PyFunctionObject *)func) -> func_name;
4797 PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004798 PyObject **d = NULL;
4799 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004801 PCALL(PCALL_FUNCTION);
4802 PCALL(PCALL_FAST_FUNCTION);
4803 if (argdefs == NULL && co->co_argcount == n &&
4804 co->co_kwonlyargcount == 0 && nk==0 &&
4805 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4806 PyFrameObject *f;
4807 PyObject *retval = NULL;
4808 PyThreadState *tstate = PyThreadState_GET();
4809 PyObject **fastlocals, **stack;
4810 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004812 PCALL(PCALL_FASTER_FUNCTION);
4813 assert(globals != NULL);
4814 /* XXX Perhaps we should create a specialized
4815 PyFrame_New() that doesn't take locals, but does
4816 take builtins without sanity checking them.
4817 */
4818 assert(tstate != NULL);
4819 f = PyFrame_New(tstate, co, globals, NULL);
4820 if (f == NULL)
4821 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 fastlocals = f->f_localsplus;
4824 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 for (i = 0; i < n; i++) {
4827 Py_INCREF(*stack);
4828 fastlocals[i] = *stack++;
4829 }
4830 retval = PyEval_EvalFrameEx(f,0);
4831 ++tstate->recursion_depth;
4832 Py_DECREF(f);
4833 --tstate->recursion_depth;
4834 return retval;
4835 }
4836 if (argdefs != NULL) {
4837 d = &PyTuple_GET_ITEM(argdefs, 0);
4838 nd = Py_SIZE(argdefs);
4839 }
Victor Stinner40ee3012014-06-16 15:59:28 +02004840 return _PyEval_EvalCodeWithName((PyObject*)co, globals,
4841 (PyObject *)NULL, (*pp_stack)-n, na,
4842 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4843 PyFunction_GET_CLOSURE(func),
4844 name, qualname);
Jeremy Hylton52820442001-01-03 23:52:36 +00004845}
4846
4847static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004848update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4849 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004851 PyObject *kwdict = NULL;
4852 if (orig_kwdict == NULL)
4853 kwdict = PyDict_New();
4854 else {
4855 kwdict = PyDict_Copy(orig_kwdict);
4856 Py_DECREF(orig_kwdict);
4857 }
4858 if (kwdict == NULL)
4859 return NULL;
4860 while (--nk >= 0) {
4861 int err;
4862 PyObject *value = EXT_POP(*pp_stack);
4863 PyObject *key = EXT_POP(*pp_stack);
4864 if (PyDict_GetItem(kwdict, key) != NULL) {
4865 PyErr_Format(PyExc_TypeError,
4866 "%.200s%s got multiple values "
4867 "for keyword argument '%U'",
4868 PyEval_GetFuncName(func),
4869 PyEval_GetFuncDesc(func),
4870 key);
4871 Py_DECREF(key);
4872 Py_DECREF(value);
4873 Py_DECREF(kwdict);
4874 return NULL;
4875 }
4876 err = PyDict_SetItem(kwdict, key, value);
4877 Py_DECREF(key);
4878 Py_DECREF(value);
4879 if (err) {
4880 Py_DECREF(kwdict);
4881 return NULL;
4882 }
4883 }
4884 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004885}
4886
4887static PyObject *
4888update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004889 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004891 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 callargs = PyTuple_New(nstack + nstar);
4894 if (callargs == NULL) {
4895 return NULL;
4896 }
4897 if (nstar) {
4898 int i;
4899 for (i = 0; i < nstar; i++) {
4900 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4901 Py_INCREF(a);
4902 PyTuple_SET_ITEM(callargs, nstack + i, a);
4903 }
4904 }
4905 while (--nstack >= 0) {
4906 w = EXT_POP(*pp_stack);
4907 PyTuple_SET_ITEM(callargs, nstack, w);
4908 }
4909 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004910}
4911
4912static PyObject *
4913load_args(PyObject ***pp_stack, int na)
4914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 PyObject *args = PyTuple_New(na);
4916 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 if (args == NULL)
4919 return NULL;
4920 while (--na >= 0) {
4921 w = EXT_POP(*pp_stack);
4922 PyTuple_SET_ITEM(args, na, w);
4923 }
4924 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004925}
4926
4927static PyObject *
4928do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 PyObject *callargs = NULL;
4931 PyObject *kwdict = NULL;
4932 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004934 if (nk > 0) {
4935 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4936 if (kwdict == NULL)
4937 goto call_fail;
4938 }
4939 callargs = load_args(pp_stack, na);
4940 if (callargs == NULL)
4941 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004942#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 /* At this point, we have to look at the type of func to
4944 update the call stats properly. Do it here so as to avoid
4945 exposing the call stats machinery outside ceval.c
4946 */
4947 if (PyFunction_Check(func))
4948 PCALL(PCALL_FUNCTION);
4949 else if (PyMethod_Check(func))
4950 PCALL(PCALL_METHOD);
4951 else if (PyType_Check(func))
4952 PCALL(PCALL_TYPE);
4953 else if (PyCFunction_Check(func))
4954 PCALL(PCALL_CFUNCTION);
4955 else
4956 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004957#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 if (PyCFunction_Check(func)) {
4959 PyThreadState *tstate = PyThreadState_GET();
4960 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4961 }
4962 else
4963 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004964call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004965 Py_XDECREF(callargs);
4966 Py_XDECREF(kwdict);
4967 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004968}
4969
4970static PyObject *
4971ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4972{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004973 int nstar = 0;
4974 PyObject *callargs = NULL;
4975 PyObject *stararg = NULL;
4976 PyObject *kwdict = NULL;
4977 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004979 if (flags & CALL_FLAG_KW) {
4980 kwdict = EXT_POP(*pp_stack);
4981 if (!PyDict_Check(kwdict)) {
4982 PyObject *d;
4983 d = PyDict_New();
4984 if (d == NULL)
4985 goto ext_call_fail;
4986 if (PyDict_Update(d, kwdict) != 0) {
4987 Py_DECREF(d);
4988 /* PyDict_Update raises attribute
4989 * error (percolated from an attempt
4990 * to get 'keys' attribute) instead of
4991 * a type error if its second argument
4992 * is not a mapping.
4993 */
4994 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4995 PyErr_Format(PyExc_TypeError,
4996 "%.200s%.200s argument after ** "
4997 "must be a mapping, not %.200s",
4998 PyEval_GetFuncName(func),
4999 PyEval_GetFuncDesc(func),
5000 kwdict->ob_type->tp_name);
5001 }
5002 goto ext_call_fail;
5003 }
5004 Py_DECREF(kwdict);
5005 kwdict = d;
5006 }
5007 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005008 if (nk > 0) {
5009 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
5010 if (kwdict == NULL)
5011 goto ext_call_fail;
5012 }
5013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 if (flags & CALL_FLAG_VAR) {
5015 stararg = EXT_POP(*pp_stack);
5016 if (!PyTuple_Check(stararg)) {
5017 PyObject *t = NULL;
Martin Panterb5944222016-01-31 06:30:56 +00005018 if (Py_TYPE(stararg)->tp_iter == NULL &&
5019 !PySequence_Check(stararg)) {
5020 PyErr_Format(PyExc_TypeError,
5021 "%.200s%.200s argument after * "
5022 "must be an iterable, not %.200s",
5023 PyEval_GetFuncName(func),
5024 PyEval_GetFuncDesc(func),
5025 stararg->ob_type->tp_name);
5026 goto ext_call_fail;
5027 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005028 t = PySequence_Tuple(stararg);
5029 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 goto ext_call_fail;
5031 }
5032 Py_DECREF(stararg);
5033 stararg = t;
5034 }
5035 nstar = PyTuple_GET_SIZE(stararg);
5036 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005037 callargs = update_star_args(na, nstar, stararg, pp_stack);
5038 if (callargs == NULL)
5039 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005040#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 /* At this point, we have to look at the type of func to
5042 update the call stats properly. Do it here so as to avoid
5043 exposing the call stats machinery outside ceval.c
5044 */
5045 if (PyFunction_Check(func))
5046 PCALL(PCALL_FUNCTION);
5047 else if (PyMethod_Check(func))
5048 PCALL(PCALL_METHOD);
5049 else if (PyType_Check(func))
5050 PCALL(PCALL_TYPE);
5051 else if (PyCFunction_Check(func))
5052 PCALL(PCALL_CFUNCTION);
5053 else
5054 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005055#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005056 if (PyCFunction_Check(func)) {
5057 PyThreadState *tstate = PyThreadState_GET();
5058 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5059 }
5060 else
5061 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00005062ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 Py_XDECREF(callargs);
5064 Py_XDECREF(kwdict);
5065 Py_XDECREF(stararg);
5066 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005067}
5068
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005069/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005070 nb_index slot defined, and store in *pi.
5071 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5072 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 +00005073 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005074*/
Tim Petersb5196382001-12-16 19:44:20 +00005075/* Note: If v is NULL, return success without storing into *pi. This
5076 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5077 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005078*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005079int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005080_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 if (v != NULL) {
5083 Py_ssize_t x;
5084 if (PyIndex_Check(v)) {
5085 x = PyNumber_AsSsize_t(v, NULL);
5086 if (x == -1 && PyErr_Occurred())
5087 return 0;
5088 }
5089 else {
5090 PyErr_SetString(PyExc_TypeError,
5091 "slice indices must be integers or "
5092 "None or have an __index__ method");
5093 return 0;
5094 }
5095 *pi = x;
5096 }
5097 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005098}
5099
Guido van Rossum486364b2007-06-30 05:01:58 +00005100#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005101 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005102
Guido van Rossumb209a111997-04-29 18:18:01 +00005103static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005104cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 int res = 0;
5107 switch (op) {
5108 case PyCmp_IS:
5109 res = (v == w);
5110 break;
5111 case PyCmp_IS_NOT:
5112 res = (v != w);
5113 break;
5114 case PyCmp_IN:
5115 res = PySequence_Contains(w, v);
5116 if (res < 0)
5117 return NULL;
5118 break;
5119 case PyCmp_NOT_IN:
5120 res = PySequence_Contains(w, v);
5121 if (res < 0)
5122 return NULL;
5123 res = !res;
5124 break;
5125 case PyCmp_EXC_MATCH:
5126 if (PyTuple_Check(w)) {
5127 Py_ssize_t i, length;
5128 length = PyTuple_Size(w);
5129 for (i = 0; i < length; i += 1) {
5130 PyObject *exc = PyTuple_GET_ITEM(w, i);
5131 if (!PyExceptionClass_Check(exc)) {
5132 PyErr_SetString(PyExc_TypeError,
5133 CANNOT_CATCH_MSG);
5134 return NULL;
5135 }
5136 }
5137 }
5138 else {
5139 if (!PyExceptionClass_Check(w)) {
5140 PyErr_SetString(PyExc_TypeError,
5141 CANNOT_CATCH_MSG);
5142 return NULL;
5143 }
5144 }
5145 res = PyErr_GivenExceptionMatches(v, w);
5146 break;
5147 default:
5148 return PyObject_RichCompare(v, w, op);
5149 }
5150 v = res ? Py_True : Py_False;
5151 Py_INCREF(v);
5152 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005153}
5154
Thomas Wouters52152252000-08-17 22:55:00 +00005155static PyObject *
5156import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005158 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005159 _Py_IDENTIFIER(__name__);
5160 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005162 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005163 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5164 return x;
5165 /* Issue #17636: in case this failed because of a circular relative
5166 import, try to fallback on reading the module directly from
5167 sys.modules. */
5168 PyErr_Clear();
5169 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005170 if (pkgname == NULL) {
5171 goto error;
5172 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005173 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5174 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005175 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005176 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005177 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005178 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005179 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005180 if (x == NULL) {
5181 goto error;
5182 }
5183 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005184 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005185 error:
5186 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5187 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005188}
Guido van Rossumac7be682001-01-17 15:42:30 +00005189
Thomas Wouters52152252000-08-17 22:55:00 +00005190static int
5191import_all_from(PyObject *locals, PyObject *v)
5192{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005193 _Py_IDENTIFIER(__all__);
5194 _Py_IDENTIFIER(__dict__);
5195 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005196 PyObject *dict, *name, *value;
5197 int skip_leading_underscores = 0;
5198 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005200 if (all == NULL) {
5201 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5202 return -1; /* Unexpected error */
5203 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005204 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 if (dict == NULL) {
5206 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5207 return -1;
5208 PyErr_SetString(PyExc_ImportError,
5209 "from-import-* object has no __dict__ and no __all__");
5210 return -1;
5211 }
5212 all = PyMapping_Keys(dict);
5213 Py_DECREF(dict);
5214 if (all == NULL)
5215 return -1;
5216 skip_leading_underscores = 1;
5217 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005219 for (pos = 0, err = 0; ; pos++) {
5220 name = PySequence_GetItem(all, pos);
5221 if (name == NULL) {
5222 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5223 err = -1;
5224 else
5225 PyErr_Clear();
5226 break;
5227 }
5228 if (skip_leading_underscores &&
5229 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005230 PyUnicode_READY(name) != -1 &&
5231 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005232 {
5233 Py_DECREF(name);
5234 continue;
5235 }
5236 value = PyObject_GetAttr(v, name);
5237 if (value == NULL)
5238 err = -1;
5239 else if (PyDict_CheckExact(locals))
5240 err = PyDict_SetItem(locals, name, value);
5241 else
5242 err = PyObject_SetItem(locals, name, value);
5243 Py_DECREF(name);
5244 Py_XDECREF(value);
5245 if (err != 0)
5246 break;
5247 }
5248 Py_DECREF(all);
5249 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005250}
5251
Guido van Rossumac7be682001-01-17 15:42:30 +00005252static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005253format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005254{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005255 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 if (!obj)
5258 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005260 obj_str = _PyUnicode_AsString(obj);
5261 if (!obj_str)
5262 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005264 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005265}
Guido van Rossum950361c1997-01-24 13:49:28 +00005266
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005267static void
5268format_exc_unbound(PyCodeObject *co, int oparg)
5269{
5270 PyObject *name;
5271 /* Don't stomp existing exception */
5272 if (PyErr_Occurred())
5273 return;
5274 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5275 name = PyTuple_GET_ITEM(co->co_cellvars,
5276 oparg);
5277 format_exc_check_arg(
5278 PyExc_UnboundLocalError,
5279 UNBOUNDLOCAL_ERROR_MSG,
5280 name);
5281 } else {
5282 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5283 PyTuple_GET_SIZE(co->co_cellvars));
5284 format_exc_check_arg(PyExc_NameError,
5285 UNBOUNDFREE_ERROR_MSG, name);
5286 }
5287}
5288
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005289static PyObject *
5290unicode_concatenate(PyObject *v, PyObject *w,
5291 PyFrameObject *f, unsigned char *next_instr)
5292{
5293 PyObject *res;
5294 if (Py_REFCNT(v) == 2) {
5295 /* In the common case, there are 2 references to the value
5296 * stored in 'variable' when the += is performed: one on the
5297 * value stack (in 'v') and one still stored in the
5298 * 'variable'. We try to delete the variable now to reduce
5299 * the refcnt to 1.
5300 */
5301 switch (*next_instr) {
5302 case STORE_FAST:
5303 {
5304 int oparg = PEEKARG();
5305 PyObject **fastlocals = f->f_localsplus;
5306 if (GETLOCAL(oparg) == v)
5307 SETLOCAL(oparg, NULL);
5308 break;
5309 }
5310 case STORE_DEREF:
5311 {
5312 PyObject **freevars = (f->f_localsplus +
5313 f->f_code->co_nlocals);
5314 PyObject *c = freevars[PEEKARG()];
5315 if (PyCell_GET(c) == v)
5316 PyCell_Set(c, NULL);
5317 break;
5318 }
5319 case STORE_NAME:
5320 {
5321 PyObject *names = f->f_code->co_names;
5322 PyObject *name = GETITEM(names, PEEKARG());
5323 PyObject *locals = f->f_locals;
5324 if (PyDict_CheckExact(locals) &&
5325 PyDict_GetItem(locals, name) == v) {
5326 if (PyDict_DelItem(locals, name) != 0) {
5327 PyErr_Clear();
5328 }
5329 }
5330 break;
5331 }
5332 }
5333 }
5334 res = v;
5335 PyUnicode_Append(&res, w);
5336 return res;
5337}
5338
Guido van Rossum950361c1997-01-24 13:49:28 +00005339#ifdef DYNAMIC_EXECUTION_PROFILE
5340
Skip Montanarof118cb12001-10-15 20:51:38 +00005341static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005342getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005344 int i;
5345 PyObject *l = PyList_New(256);
5346 if (l == NULL) return NULL;
5347 for (i = 0; i < 256; i++) {
5348 PyObject *x = PyLong_FromLong(a[i]);
5349 if (x == NULL) {
5350 Py_DECREF(l);
5351 return NULL;
5352 }
5353 PyList_SetItem(l, i, x);
5354 }
5355 for (i = 0; i < 256; i++)
5356 a[i] = 0;
5357 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005358}
5359
5360PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005361_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005362{
5363#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005365#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005366 int i;
5367 PyObject *l = PyList_New(257);
5368 if (l == NULL) return NULL;
5369 for (i = 0; i < 257; i++) {
5370 PyObject *x = getarray(dxpairs[i]);
5371 if (x == NULL) {
5372 Py_DECREF(l);
5373 return NULL;
5374 }
5375 PyList_SetItem(l, i, x);
5376 }
5377 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005378#endif
5379}
5380
5381#endif