blob: fd456ceed1ec5f9a209757b8d0c99b2b3fc6a3c2 [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
Victor Stinner9be7e7b2016-08-19 16:11:43 +0200116static PyObject * fast_function(PyObject *, PyObject **, int, int, int);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static 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 *);
Serhiy Storchaka133138a2016-08-02 22:51:21 +0300142static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *, PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000143static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000144static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000145static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000146static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200147static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300148 PyFrameObject *, const unsigned short *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500149static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000150
Paul Prescode68140d2000-08-30 20:25:01 +0000151#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000153#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000155#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 "free variable '%.200s' referenced before assignment" \
157 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000158
Guido van Rossum950361c1997-01-24 13:49:28 +0000159/* Dynamic execution profile */
160#ifdef DYNAMIC_EXECUTION_PROFILE
161#ifdef DXPAIRS
162static long dxpairs[257][256];
163#define dxp dxpairs[256]
164#else
165static long dxp[256];
166#endif
167#endif
168
Jeremy Hylton985eba52003-02-05 23:13:00 +0000169/* Function call profile */
170#ifdef CALL_PROFILE
171#define PCALL_NUM 11
172static int pcall[PCALL_NUM];
173
174#define PCALL_ALL 0
175#define PCALL_FUNCTION 1
176#define PCALL_FAST_FUNCTION 2
177#define PCALL_FASTER_FUNCTION 3
178#define PCALL_METHOD 4
179#define PCALL_BOUND_METHOD 5
180#define PCALL_CFUNCTION 6
181#define PCALL_TYPE 7
182#define PCALL_GENERATOR 8
183#define PCALL_OTHER 9
184#define PCALL_POP 10
185
186/* Notes about the statistics
187
188 PCALL_FAST stats
189
190 FAST_FUNCTION means no argument tuple needs to be created.
191 FASTER_FUNCTION means that the fast-path frame setup code is used.
192
193 If there is a method call where the call can be optimized by changing
194 the argument tuple and calling the function directly, it gets recorded
195 twice.
196
197 As a result, the relationship among the statistics appears to be
198 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
199 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
200 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
201 PCALL_METHOD > PCALL_BOUND_METHOD
202*/
203
204#define PCALL(POS) pcall[POS]++
205
206PyObject *
207PyEval_GetCallStats(PyObject *self)
208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 return Py_BuildValue("iiiiiiiiiii",
210 pcall[0], pcall[1], pcall[2], pcall[3],
211 pcall[4], pcall[5], pcall[6], pcall[7],
212 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000213}
214#else
215#define PCALL(O)
216
217PyObject *
218PyEval_GetCallStats(PyObject *self)
219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 Py_INCREF(Py_None);
221 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000222}
223#endif
224
Tim Peters5ca576e2001-06-18 22:08:13 +0000225
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000226#ifdef WITH_THREAD
227#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
228#else
229#define GIL_REQUEST 0
230#endif
231
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000232/* This can set eval_breaker to 0 even though gil_drop_request became
233 1. We believe this is all right because the eval loop will release
234 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 _Py_atomic_store_relaxed( \
237 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000238 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
240 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000242#ifdef WITH_THREAD
243
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000244#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
247 _Py_atomic_store_relaxed(&eval_breaker, 1); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
250#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000251 do { \
252 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
253 COMPUTE_EVAL_BREAKER(); \
254 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000255
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000256#endif
257
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000258/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
262 _Py_atomic_store_relaxed(&eval_breaker, 1); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
268 COMPUTE_EVAL_BREAKER(); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { \
273 pending_async_exc = 1; \
274 _Py_atomic_store_relaxed(&eval_breaker, 1); \
275 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000276
277#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000279
280
Guido van Rossume59214e1994-08-30 08:01:59 +0000281#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000282
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000284#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000285#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000286#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000287
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000288static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000289static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000290/* This single variable consolidates all requests to break out of the fast path
291 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000292static _Py_atomic_int eval_breaker = {0};
293/* Request for dropping the GIL */
294static _Py_atomic_int gil_drop_request = {0};
295/* Request for running pending calls. */
296static _Py_atomic_int pendingcalls_to_do = {0};
297/* Request for looking at the `async_exc` field of the current thread state.
298 Guarded by the GIL. */
299static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000300
301#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302
Tim Peters7f468f22004-10-11 02:40:51 +0000303int
304PyEval_ThreadsInitialized(void)
305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000307}
308
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000310PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000312 if (gil_created())
313 return;
314 create_gil();
315 take_gil(PyThreadState_GET());
316 main_thread = PyThread_get_thread_ident();
317 if (!pending_lock)
318 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000320
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000321void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000322_PyEval_FiniThreads(void)
323{
324 if (!gil_created())
325 return;
326 destroy_gil();
327 assert(!gil_created());
328}
329
330void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000331PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000332{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000333 PyThreadState *tstate = PyThreadState_GET();
334 if (tstate == NULL)
335 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
336 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000337}
338
339void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000341{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 /* This function must succeed when the current thread state is NULL.
343 We therefore avoid PyThreadState_GET() which dumps a fatal error
344 in debug mode.
345 */
346 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
347 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000348}
349
350void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000351PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 if (tstate == NULL)
354 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
355 /* Check someone has called PyEval_InitThreads() to create the lock */
356 assert(gil_created());
357 take_gil(tstate);
358 if (PyThreadState_Swap(tstate) != NULL)
359 Py_FatalError(
360 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000361}
362
363void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000364PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (tstate == NULL)
367 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
368 if (PyThreadState_Swap(NULL) != tstate)
369 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
370 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000371}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000372
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200373/* This function is called from PyOS_AfterFork to destroy all threads which are
374 * not running in the child process, and clear internal locks which might be
375 * held by those threads. (This could also be done using pthread_atfork
376 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000377
378void
379PyEval_ReInitThreads(void)
380{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200381 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200383 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 if (!gil_created())
386 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 recreate_gil();
388 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200389 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 /* Update the threading module with the new state.
393 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200394 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 "threading");
396 if (threading == NULL) {
397 /* threading not imported */
398 PyErr_Clear();
399 return;
400 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200401 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000402 if (result == NULL)
403 PyErr_WriteUnraisable(threading);
404 else
405 Py_DECREF(result);
406 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200407
408 /* Destroy all threads except the current one */
409 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000410}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000411
412#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000413static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000414static int pending_async_exc = 0;
415#endif /* WITH_THREAD */
416
417/* This function is used to signal that async exceptions are waiting to be
418 raised, therefore it is also useful in non-threaded builds. */
419
420void
421_PyEval_SignalAsyncExc(void)
422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000424}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000425
Guido van Rossumff4949e1992-08-05 19:58:53 +0000426/* Functions save_thread and restore_thread are always defined so
427 dynamically loaded modules needn't be compiled separately for use
428 with and without threads: */
429
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000430PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000431PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 PyThreadState *tstate = PyThreadState_Swap(NULL);
434 if (tstate == NULL)
435 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000436#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (gil_created())
438 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000439#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000441}
442
443void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000444PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (tstate == NULL)
447 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000448#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 if (gil_created()) {
450 int err = errno;
451 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200452 /* _Py_Finalizing is protected by the GIL */
453 if (_Py_Finalizing && tstate != _Py_Finalizing) {
454 drop_gil(tstate);
455 PyThread_exit_thread();
456 assert(0); /* unreachable */
457 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 errno = err;
459 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000460#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000462}
463
464
Guido van Rossuma9672091994-09-14 13:31:22 +0000465/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
466 signal handlers or Mac I/O completion routines) can schedule calls
467 to a function to be called synchronously.
468 The synchronous function is called with one void* argument.
469 It should return 0 for success or -1 for failure -- failure should
470 be accompanied by an exception.
471
472 If registry succeeds, the registry function returns 0; if it fails
473 (e.g. due to too many pending calls) it returns -1 (without setting
474 an exception condition).
475
476 Note that because registry may occur from within signal handlers,
477 or other asynchronous events, calling malloc() is unsafe!
478
479#ifdef WITH_THREAD
480 Any thread can schedule pending calls, but only the main thread
481 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000482 There is no facility to schedule calls to a particular thread, but
483 that should be easy to change, should that ever be required. In
484 that case, the static variables here should go into the python
485 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000486#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000488
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000489#ifdef WITH_THREAD
490
491/* The WITH_THREAD implementation is thread-safe. It allows
492 scheduling to be made from any thread, and even from an executing
493 callback.
494 */
495
496#define NPENDINGCALLS 32
497static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 int (*func)(void *);
499 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000500} pendingcalls[NPENDINGCALLS];
501static int pendingfirst = 0;
502static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000503
504int
505Py_AddPendingCall(int (*func)(void *), void *arg)
506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 int i, j, result=0;
508 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 /* try a few times for the lock. Since this mechanism is used
511 * for signal handling (on the main thread), there is a (slim)
512 * chance that a signal is delivered on the same thread while we
513 * hold the lock during the Py_MakePendingCalls() function.
514 * This avoids a deadlock in that case.
515 * Note that signals can be delivered on any thread. In particular,
516 * on Windows, a SIGINT is delivered on a system-created worker
517 * thread.
518 * We also check for lock being NULL, in the unlikely case that
519 * this function is called before any bytecode evaluation takes place.
520 */
521 if (lock != NULL) {
522 for (i = 0; i<100; i++) {
523 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
524 break;
525 }
526 if (i == 100)
527 return -1;
528 }
529
530 i = pendinglast;
531 j = (i + 1) % NPENDINGCALLS;
532 if (j == pendingfirst) {
533 result = -1; /* Queue full */
534 } else {
535 pendingcalls[i].func = func;
536 pendingcalls[i].arg = arg;
537 pendinglast = j;
538 }
539 /* signal main loop */
540 SIGNAL_PENDING_CALLS();
541 if (lock != NULL)
542 PyThread_release_lock(lock);
543 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544}
545
546int
547Py_MakePendingCalls(void)
548{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200549 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 int i;
551 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (!pending_lock) {
554 /* initial allocation of the lock */
555 pending_lock = PyThread_allocate_lock();
556 if (pending_lock == NULL)
557 return -1;
558 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* only service pending calls on main thread */
561 if (main_thread && PyThread_get_thread_ident() != main_thread)
562 return 0;
563 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200564 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200566 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 /* perform a bounded number of calls, in case of recursion */
568 for (i=0; i<NPENDINGCALLS; i++) {
569 int j;
570 int (*func)(void *);
571 void *arg = NULL;
572
573 /* pop one item off the queue while holding the lock */
574 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
575 j = pendingfirst;
576 if (j == pendinglast) {
577 func = NULL; /* Queue empty */
578 } else {
579 func = pendingcalls[j].func;
580 arg = pendingcalls[j].arg;
581 pendingfirst = (j + 1) % NPENDINGCALLS;
582 }
583 if (pendingfirst != pendinglast)
584 SIGNAL_PENDING_CALLS();
585 else
586 UNSIGNAL_PENDING_CALLS();
587 PyThread_release_lock(pending_lock);
588 /* having released the lock, perform the callback */
589 if (func == NULL)
590 break;
591 r = func(arg);
592 if (r)
593 break;
594 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200595 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000597}
598
599#else /* if ! defined WITH_THREAD */
600
601/*
602 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
603 This code is used for signal handling in python that isn't built
604 with WITH_THREAD.
605 Don't use this implementation when Py_AddPendingCalls() can happen
606 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607
Guido van Rossuma9672091994-09-14 13:31:22 +0000608 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 (1) nested asynchronous calls to Py_AddPendingCall()
610 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000612 (1) is very unlikely because typically signal delivery
613 is blocked during signal handling. So it should be impossible.
614 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000615 The current code is safe against (2), but not against (1).
616 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000617 thread is present, interrupted by signals, and that the critical
618 section is protected with the "busy" variable. On Windows, which
619 delivers SIGINT on a system thread, this does not hold and therefore
620 Windows really shouldn't use this version.
621 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000622*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000623
Guido van Rossuma9672091994-09-14 13:31:22 +0000624#define NPENDINGCALLS 32
625static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 int (*func)(void *);
627 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000628} pendingcalls[NPENDINGCALLS];
629static volatile int pendingfirst = 0;
630static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000631static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000632
633int
Thomas Wouters334fb892000-07-25 12:56:38 +0000634Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 static volatile int busy = 0;
637 int i, j;
638 /* XXX Begin critical section */
639 if (busy)
640 return -1;
641 busy = 1;
642 i = pendinglast;
643 j = (i + 1) % NPENDINGCALLS;
644 if (j == pendingfirst) {
645 busy = 0;
646 return -1; /* Queue full */
647 }
648 pendingcalls[i].func = func;
649 pendingcalls[i].arg = arg;
650 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 SIGNAL_PENDING_CALLS();
653 busy = 0;
654 /* XXX End critical section */
655 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000656}
657
Guido van Rossum180d7b41994-09-29 09:45:57 +0000658int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000659Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 static int busy = 0;
662 if (busy)
663 return 0;
664 busy = 1;
665 UNSIGNAL_PENDING_CALLS();
666 for (;;) {
667 int i;
668 int (*func)(void *);
669 void *arg;
670 i = pendingfirst;
671 if (i == pendinglast)
672 break; /* Queue empty */
673 func = pendingcalls[i].func;
674 arg = pendingcalls[i].arg;
675 pendingfirst = (i + 1) % NPENDINGCALLS;
676 if (func(arg) < 0) {
677 busy = 0;
678 SIGNAL_PENDING_CALLS(); /* We're not done yet */
679 return -1;
680 }
681 }
682 busy = 0;
683 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000684}
685
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000686#endif /* WITH_THREAD */
687
Guido van Rossuma9672091994-09-14 13:31:22 +0000688
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000689/* The interpreter's recursion limit */
690
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000691#ifndef Py_DEFAULT_RECURSION_LIMIT
692#define Py_DEFAULT_RECURSION_LIMIT 1000
693#endif
694static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
695int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000697int
698Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000700 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000701}
702
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000703void
704Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 recursion_limit = new_limit;
707 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000708}
709
Armin Rigo2b3eb402003-10-28 12:05:48 +0000710/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
711 if the recursion_depth reaches _Py_CheckRecursionLimit.
712 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
713 to guarantee that _Py_CheckRecursiveCall() is regularly called.
714 Without USE_STACKCHECK, there is no need for this. */
715int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300716_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000719
720#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 if (PyOS_CheckStack()) {
722 --tstate->recursion_depth;
723 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
724 return -1;
725 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000726#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 _Py_CheckRecursionLimit = recursion_limit;
728 if (tstate->recursion_critical)
729 /* Somebody asked that we don't check for recursion. */
730 return 0;
731 if (tstate->overflowed) {
732 if (tstate->recursion_depth > recursion_limit + 50) {
733 /* Overflowing while handling an overflow. Give up. */
734 Py_FatalError("Cannot recover from stack overflow.");
735 }
736 return 0;
737 }
738 if (tstate->recursion_depth > recursion_limit) {
739 --tstate->recursion_depth;
740 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400741 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 "maximum recursion depth exceeded%s",
743 where);
744 return -1;
745 }
746 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000747}
748
Guido van Rossum374a9221991-04-04 10:40:29 +0000749/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000750enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000751 WHY_NOT = 0x0001, /* No error */
752 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000753 WHY_RETURN = 0x0008, /* 'return' statement */
754 WHY_BREAK = 0x0010, /* 'break' statement */
755 WHY_CONTINUE = 0x0020, /* 'continue' statement */
756 WHY_YIELD = 0x0040, /* 'yield' operator */
757 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000758};
Guido van Rossum374a9221991-04-04 10:40:29 +0000759
Benjamin Peterson87880242011-07-03 16:48:31 -0500760static void save_exc_state(PyThreadState *, PyFrameObject *);
761static void swap_exc_state(PyThreadState *, PyFrameObject *);
762static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400763static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000764static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000765
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000766/* Records whether tracing is on for any thread. Counts the number of
767 threads for which tstate->c_tracefunc is non-NULL, so if the value
768 is 0, we know we don't have to check this thread's c_tracefunc.
769 This speeds up the if statement in PyEval_EvalFrameEx() after
770 fast_next_opcode*/
771static int _Py_TracingPossible = 0;
772
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000773
Guido van Rossum374a9221991-04-04 10:40:29 +0000774
Guido van Rossumb209a111997-04-29 18:18:01 +0000775PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000776PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 return PyEval_EvalCodeEx(co,
779 globals, locals,
780 (PyObject **)NULL, 0,
781 (PyObject **)NULL, 0,
782 (PyObject **)NULL, 0,
783 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000784}
785
786
787/* Interpreter main loop */
788
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000789PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000790PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 /* This is for backward compatibility with extension modules that
792 used this API; core interpreter code should call
793 PyEval_EvalFrameEx() */
794 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000795}
796
797PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000798PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000799{
Guido van Rossum950361c1997-01-24 13:49:28 +0000800#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000802#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200803 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300804 const unsigned short *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200805 int opcode; /* Current opcode */
806 int oparg; /* Current opcode argument, if any */
807 enum why_code why; /* Reason for block stack unwind */
808 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 PyObject *retval = NULL; /* Return value */
810 PyThreadState *tstate = PyThreadState_GET();
811 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 is true when the line being executed has changed. The
818 initial values are such as to make this false the first
819 time it is tested. */
820 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000821
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300822 const unsigned short *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 PyObject *names;
824 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000825
Brett Cannon368b4b72012-04-02 12:17:59 -0400826#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200827 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400828#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200829
Antoine Pitroub52ec782009-01-25 16:34:23 +0000830/* Computed GOTOs, or
831 the-optimization-commonly-but-improperly-known-as-"threaded code"
832 using gcc's labels-as-values extension
833 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
834
835 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000837 combined with a lookup table of jump addresses. However, since the
838 indirect jump instruction is shared by all opcodes, the CPU will have a
839 hard time making the right prediction for where to jump next (actually,
840 it will be always wrong except in the uncommon case of a sequence of
841 several identical opcodes).
842
843 "Threaded code" in contrast, uses an explicit jump table and an explicit
844 indirect jump instruction at the end of each opcode. Since the jump
845 instruction is at a different address for each opcode, the CPU will make a
846 separate prediction for each of these instructions, which is equivalent to
847 predicting the second opcode of each opcode pair. These predictions have
848 a much better chance to turn out valid, especially in small bytecode loops.
849
850 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000852 and potentially many more instructions (depending on the pipeline width).
853 A correctly predicted branch, however, is nearly free.
854
855 At the time of this writing, the "threaded code" version is up to 15-20%
856 faster than the normal "switch" version, depending on the compiler and the
857 CPU architecture.
858
859 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
860 because it would render the measurements invalid.
861
862
863 NOTE: care must be taken that the compiler doesn't try to "optimize" the
864 indirect jumps by sharing them between all opcodes. Such optimizations
865 can be disabled on gcc by using the -fno-gcse flag (or possibly
866 -fno-crossjumping).
867*/
868
Antoine Pitrou042b1282010-08-13 21:15:58 +0000869#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000870#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000871#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872#endif
873
Antoine Pitrou042b1282010-08-13 21:15:58 +0000874#ifdef HAVE_COMPUTED_GOTOS
875 #ifndef USE_COMPUTED_GOTOS
876 #define USE_COMPUTED_GOTOS 1
877 #endif
878#else
879 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
880 #error "Computed gotos are not supported on this compiler."
881 #endif
882 #undef USE_COMPUTED_GOTOS
883 #define USE_COMPUTED_GOTOS 0
884#endif
885
886#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000887/* Import the static jump table */
888#include "opcode_targets.h"
889
Antoine Pitroub52ec782009-01-25 16:34:23 +0000890#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 { \
896 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
897 FAST_DISPATCH(); \
898 } \
899 continue; \
900 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901
902#ifdef LLTRACE
903#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 { \
905 if (!lltrace && !_Py_TracingPossible) { \
906 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300907 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300908 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 } \
910 goto fast_next_opcode; \
911 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912#else
913#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 { \
915 if (!_Py_TracingPossible) { \
916 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300917 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300918 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 } \
920 goto fast_next_opcode; \
921 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922#endif
923
924#else
925#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300927
Antoine Pitroub52ec782009-01-25 16:34:23 +0000928#define DISPATCH() continue
929#define FAST_DISPATCH() goto fast_next_opcode
930#endif
931
932
Neal Norwitza81d2202002-07-14 00:27:26 +0000933/* Tuple access macros */
934
935#ifndef Py_DEBUG
936#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
937#else
938#define GETITEM(v, i) PyTuple_GetItem((v), (i))
939#endif
940
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000941#ifdef WITH_TSC
942/* Use Pentium timestamp counter to mark certain events:
943 inst0 -- beginning of switch statement for opcode dispatch
944 inst1 -- end of switch statement (may be skipped)
945 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000946 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000947 (may be skipped)
948 intr1 -- beginning of long interruption
949 intr2 -- end of long interruption
950
951 Many opcodes call out to helper C functions. In some cases, the
952 time in those functions should be counted towards the time for the
953 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
954 calls another Python function; there's no point in charge all the
955 bytecode executed by the called function to the caller.
956
957 It's hard to make a useful judgement statically. In the presence
958 of operator overloading, it's impossible to tell if a call will
959 execute new Python code or not.
960
961 It's a case-by-case judgement. I'll use intr1 for the following
962 cases:
963
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000964 IMPORT_STAR
965 IMPORT_FROM
966 CALL_FUNCTION (and friends)
967
968 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
970 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 READ_TIMESTAMP(inst0);
973 READ_TIMESTAMP(inst1);
974 READ_TIMESTAMP(loop0);
975 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000976
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 /* shut up the compiler */
978 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000979#endif
980
Guido van Rossum374a9221991-04-04 10:40:29 +0000981/* Code access macros */
982
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300983#ifdef WORDS_BIGENDIAN
984 #define OPCODE(word) ((word) >> 8)
985 #define OPARG(word) ((word) & 255)
986#else
987 #define OPCODE(word) ((word) & 255)
988 #define OPARG(word) ((word) >> 8)
989#endif
990/* The integer overflow is checked by an assertion below. */
991#define INSTR_OFFSET() (2*(int)(next_instr - first_instr))
992#define NEXTOPARG() do { \
993 unsigned short word = *next_instr; \
994 opcode = OPCODE(word); \
995 oparg = OPARG(word); \
996 next_instr++; \
997 } while (0)
998#define JUMPTO(x) (next_instr = first_instr + (x)/2)
999#define JUMPBY(x) (next_instr += (x)/2)
Guido van Rossum374a9221991-04-04 10:40:29 +00001000
Raymond Hettingerf606f872003-03-16 03:11:04 +00001001/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Some opcodes tend to come in pairs thus making it possible to
1003 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001004 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Verifying the prediction costs a single high-speed test of a register
1007 variable against a constant. If the pairing was good, then the
1008 processor's own internal branch predication has a high likelihood of
1009 success, resulting in a nearly zero-overhead transition to the
1010 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001011 including its unpredictable switch-case branch. Combined with the
1012 processor's internal branch prediction, a successful PREDICT has the
1013 effect of making the two opcodes run as if they were a single new opcode
1014 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001015
Georg Brandl86b2fb92008-07-16 03:43:04 +00001016 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 predictions turned-on and interpret the results as if some opcodes
1018 had been combined or turn-off predictions so that the opcode frequency
1019 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020
1021 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 the CPU to record separate branch prediction information for each
1023 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024
Raymond Hettingerf606f872003-03-16 03:11:04 +00001025*/
1026
Antoine Pitrou042b1282010-08-13 21:15:58 +00001027#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +00001029#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001030#define PREDICT(op) \
1031 do{ \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001032 unsigned short word = *next_instr; \
1033 opcode = OPCODE(word); \
1034 if (opcode == op){ \
1035 oparg = OPARG(word); \
1036 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001037 goto PRED_##op; \
1038 } \
1039 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001040#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001041#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +00001042
Raymond Hettingerf606f872003-03-16 03:11:04 +00001043
Guido van Rossum374a9221991-04-04 10:40:29 +00001044/* Stack manipulation macros */
1045
Martin v. Löwis18e16552006-02-15 17:27:45 +00001046/* The stack can grow at most MAXINT deep, as co_nlocals and
1047 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001048#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1049#define EMPTY() (STACK_LEVEL() == 0)
1050#define TOP() (stack_pointer[-1])
1051#define SECOND() (stack_pointer[-2])
1052#define THIRD() (stack_pointer[-3])
1053#define FOURTH() (stack_pointer[-4])
1054#define PEEK(n) (stack_pointer[-(n)])
1055#define SET_TOP(v) (stack_pointer[-1] = (v))
1056#define SET_SECOND(v) (stack_pointer[-2] = (v))
1057#define SET_THIRD(v) (stack_pointer[-3] = (v))
1058#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1059#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1060#define BASIC_STACKADJ(n) (stack_pointer += n)
1061#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1062#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001063
Guido van Rossum96a42c81992-01-12 02:29:51 +00001064#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001066 lltrace && prtrace(TOP(), "push")); \
1067 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001069 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001071 lltrace && prtrace(TOP(), "stackadj")); \
1072 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001073#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001074 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1075 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001076#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001077#define PUSH(v) BASIC_PUSH(v)
1078#define POP() BASIC_POP()
1079#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001080#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001081#endif
1082
Guido van Rossum681d79a1995-07-18 14:51:37 +00001083/* Local variable macros */
1084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001086
1087/* The SETLOCAL() macro must not DECREF the local variable in-place and
1088 then store the new value; it must copy the old value to a temporary
1089 value, then store the new value, and then DECREF the temporary value.
1090 This is because it is possible that during the DECREF the frame is
1091 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1092 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001094 GETLOCAL(i) = value; \
1095 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001096
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001097
1098#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 while (STACK_LEVEL() > (b)->b_level) { \
1100 PyObject *v = POP(); \
1101 Py_XDECREF(v); \
1102 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001103
1104#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001105 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 PyObject *type, *value, *traceback; \
1107 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1108 while (STACK_LEVEL() > (b)->b_level + 3) { \
1109 value = POP(); \
1110 Py_XDECREF(value); \
1111 } \
1112 type = tstate->exc_type; \
1113 value = tstate->exc_value; \
1114 traceback = tstate->exc_traceback; \
1115 tstate->exc_type = POP(); \
1116 tstate->exc_value = POP(); \
1117 tstate->exc_traceback = POP(); \
1118 Py_XDECREF(type); \
1119 Py_XDECREF(value); \
1120 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001121 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001122
Guido van Rossuma027efa1997-05-05 20:56:21 +00001123/* Start of code */
1124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 /* push frame */
1126 if (Py_EnterRecursiveCall(""))
1127 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (tstate->use_tracing) {
1132 if (tstate->c_tracefunc != NULL) {
1133 /* tstate->c_tracefunc, if defined, is a
1134 function that will be called on *every* entry
1135 to a code block. Its return value, if not
1136 None, is a function that will be called at
1137 the start of each executed line of code.
1138 (Actually, the function must return itself
1139 in order to continue tracing.) The trace
1140 functions are called with three arguments:
1141 a pointer to the current frame, a string
1142 indicating why the function is called, and
1143 an argument which depends on the situation.
1144 The global trace function is also called
1145 whenever an exception is detected. */
1146 if (call_trace_protected(tstate->c_tracefunc,
1147 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001148 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 /* Trace function raised an error */
1150 goto exit_eval_frame;
1151 }
1152 }
1153 if (tstate->c_profilefunc != NULL) {
1154 /* Similar for c_profilefunc, except it needn't
1155 return itself and isn't called for "line" events */
1156 if (call_trace_protected(tstate->c_profilefunc,
1157 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001158 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 /* Profile function raised an error */
1160 goto exit_eval_frame;
1161 }
1162 }
1163 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001165 co = f->f_code;
1166 names = co->co_names;
1167 consts = co->co_consts;
1168 fastlocals = f->f_localsplus;
1169 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001170 assert(PyBytes_Check(co->co_code));
1171 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
1172 assert(PyBytes_GET_SIZE(co->co_code) % 2 == 0);
Serhiy Storchaka74f2fe62016-05-25 20:35:44 +03001173 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), 2));
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001174 first_instr = (unsigned short*) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001175 /*
1176 f->f_lasti refers to the index of the last instruction,
1177 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001178
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001179 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001180 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 When the PREDICT() macros are enabled, some opcode pairs follow in
1183 direct succession without updating f->f_lasti. A successful
1184 prediction effectively links the two codes together as if they
1185 were a single new opcode; accordingly,f->f_lasti will point to
1186 the first code in the pair (for instance, GET_ITER followed by
1187 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001188 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001190 next_instr = first_instr;
1191 if (f->f_lasti >= 0) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001192 assert(f->f_lasti % 2 == 0);
1193 next_instr += f->f_lasti/2 + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 stack_pointer = f->f_stacktop;
1196 assert(stack_pointer != NULL);
1197 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001198 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001199
Yury Selivanov5376ba92015-06-22 12:19:30 -04001200 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001201 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 /* We were in an except handler when we left,
1203 restore the exception state which was put aside
1204 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001205 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001207 else
1208 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001210
Tim Peters5ca576e2001-06-18 22:08:13 +00001211#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001212 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001213#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001215 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001217 if (throwflag) /* support for generator.throw() */
1218 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001219
Victor Stinnerace47d72013-07-18 01:41:08 +02001220#ifdef Py_DEBUG
1221 /* PyEval_EvalFrameEx() must not be called with an exception set,
1222 because it may clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001223 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001224 assert(!PyErr_Occurred());
1225#endif
1226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001228#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 if (inst1 == 0) {
1230 /* Almost surely, the opcode executed a break
1231 or a continue, preventing inst1 from being set
1232 on the way out of the loop.
1233 */
1234 READ_TIMESTAMP(inst1);
1235 loop1 = inst1;
1236 }
1237 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1238 intr0, intr1);
1239 ticked = 0;
1240 inst1 = 0;
1241 intr0 = 0;
1242 intr1 = 0;
1243 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1246 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001247 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 /* Do periodic things. Doing this every time through
1250 the loop would add too much overhead, so we do it
1251 only every Nth instruction. We also do it if
1252 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1253 event needs attention (e.g. a signal handler or
1254 async I/O handler); see Py_AddPendingCall() and
1255 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001258 if (OPCODE(*next_instr) == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001260 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 goto fast_next_opcode;
1262 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001263#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001265#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001267 if (Py_MakePendingCalls() < 0)
1268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001270#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001271 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 /* Give another thread a chance */
1273 if (PyThreadState_Swap(NULL) != tstate)
1274 Py_FatalError("ceval: tstate mix-up");
1275 drop_gil(tstate);
1276
1277 /* Other threads may run now */
1278
1279 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001280
1281 /* Check if we should make a quick exit. */
1282 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1283 drop_gil(tstate);
1284 PyThread_exit_thread();
1285 }
1286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (PyThreadState_Swap(tstate) != NULL)
1288 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 /* Check for asynchronous exceptions. */
1292 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 tstate->async_exc = NULL;
1295 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 PyErr_SetNone(exc);
1297 Py_DECREF(exc);
1298 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
1300 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 fast_next_opcode:
1303 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001306
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001308 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* see maybe_call_line_trace
1311 for expository comments */
1312 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 err = maybe_call_line_trace(tstate->c_tracefunc,
1315 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001316 tstate, f,
1317 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 /* Reload possibly changed frame fields */
1319 JUMPTO(f->f_lasti);
1320 if (f->f_stacktop != NULL) {
1321 stack_pointer = f->f_stacktop;
1322 f->f_stacktop = NULL;
1323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001324 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001326 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001330
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001331 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001332 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001333#ifdef DYNAMIC_EXECUTION_PROFILE
1334#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 dxpairs[lastopcode][opcode]++;
1336 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001337#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001339#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001340
Guido van Rossum96a42c81992-01-12 02:29:51 +00001341#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (lltrace) {
1345 if (HAS_ARG(opcode)) {
1346 printf("%d: %d, %d\n",
1347 f->f_lasti, opcode, oparg);
1348 }
1349 else {
1350 printf("%d: %d\n",
1351 f->f_lasti, opcode);
1352 }
1353 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001354#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 /* Main switch on opcode */
1357 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 /* BEWARE!
1362 It is essential that any operation that fails sets either
1363 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1364 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 TARGET(NOP)
1367 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 TARGET(LOAD_FAST) {
1370 PyObject *value = GETLOCAL(oparg);
1371 if (value == NULL) {
1372 format_exc_check_arg(PyExc_UnboundLocalError,
1373 UNBOUNDLOCAL_ERROR_MSG,
1374 PyTuple_GetItem(co->co_varnames, oparg));
1375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001377 Py_INCREF(value);
1378 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 }
1381
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001382 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001383 TARGET(LOAD_CONST) {
1384 PyObject *value = GETITEM(consts, oparg);
1385 Py_INCREF(value);
1386 PUSH(value);
1387 FAST_DISPATCH();
1388 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001389
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001390 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 TARGET(STORE_FAST) {
1392 PyObject *value = POP();
1393 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 TARGET(POP_TOP) {
1398 PyObject *value = POP();
1399 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 TARGET(ROT_TWO) {
1404 PyObject *top = TOP();
1405 PyObject *second = SECOND();
1406 SET_TOP(second);
1407 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001411 TARGET(ROT_THREE) {
1412 PyObject *top = TOP();
1413 PyObject *second = SECOND();
1414 PyObject *third = THIRD();
1415 SET_TOP(second);
1416 SET_SECOND(third);
1417 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 TARGET(DUP_TOP) {
1422 PyObject *top = TOP();
1423 Py_INCREF(top);
1424 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001426 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001428 TARGET(DUP_TOP_TWO) {
1429 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001431 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001433 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 SET_TOP(top);
1435 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001436 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001437 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001438
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001439 TARGET(UNARY_POSITIVE) {
1440 PyObject *value = TOP();
1441 PyObject *res = PyNumber_Positive(value);
1442 Py_DECREF(value);
1443 SET_TOP(res);
1444 if (res == NULL)
1445 goto error;
1446 DISPATCH();
1447 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001449 TARGET(UNARY_NEGATIVE) {
1450 PyObject *value = TOP();
1451 PyObject *res = PyNumber_Negative(value);
1452 Py_DECREF(value);
1453 SET_TOP(res);
1454 if (res == NULL)
1455 goto error;
1456 DISPATCH();
1457 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001459 TARGET(UNARY_NOT) {
1460 PyObject *value = TOP();
1461 int err = PyObject_IsTrue(value);
1462 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 if (err == 0) {
1464 Py_INCREF(Py_True);
1465 SET_TOP(Py_True);
1466 DISPATCH();
1467 }
1468 else if (err > 0) {
1469 Py_INCREF(Py_False);
1470 SET_TOP(Py_False);
1471 err = 0;
1472 DISPATCH();
1473 }
1474 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 goto error;
1476 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001478 TARGET(UNARY_INVERT) {
1479 PyObject *value = TOP();
1480 PyObject *res = PyNumber_Invert(value);
1481 Py_DECREF(value);
1482 SET_TOP(res);
1483 if (res == NULL)
1484 goto error;
1485 DISPATCH();
1486 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001487
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001488 TARGET(BINARY_POWER) {
1489 PyObject *exp = POP();
1490 PyObject *base = TOP();
1491 PyObject *res = PyNumber_Power(base, exp, Py_None);
1492 Py_DECREF(base);
1493 Py_DECREF(exp);
1494 SET_TOP(res);
1495 if (res == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 TARGET(BINARY_MULTIPLY) {
1501 PyObject *right = POP();
1502 PyObject *left = TOP();
1503 PyObject *res = PyNumber_Multiply(left, right);
1504 Py_DECREF(left);
1505 Py_DECREF(right);
1506 SET_TOP(res);
1507 if (res == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Petersond51374e2014-04-09 23:55:56 -04001512 TARGET(BINARY_MATRIX_MULTIPLY) {
1513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_MatrixMultiply(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
1523
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 TARGET(BINARY_TRUE_DIVIDE) {
1525 PyObject *divisor = POP();
1526 PyObject *dividend = TOP();
1527 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1528 Py_DECREF(dividend);
1529 Py_DECREF(divisor);
1530 SET_TOP(quotient);
1531 if (quotient == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 TARGET(BINARY_FLOOR_DIVIDE) {
1537 PyObject *divisor = POP();
1538 PyObject *dividend = TOP();
1539 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1540 Py_DECREF(dividend);
1541 Py_DECREF(divisor);
1542 SET_TOP(quotient);
1543 if (quotient == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001547
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 TARGET(BINARY_MODULO) {
1549 PyObject *divisor = POP();
1550 PyObject *dividend = TOP();
1551 PyObject *res = PyUnicode_CheckExact(dividend) ?
1552 PyUnicode_Format(dividend, divisor) :
1553 PyNumber_Remainder(dividend, divisor);
1554 Py_DECREF(divisor);
1555 Py_DECREF(dividend);
1556 SET_TOP(res);
1557 if (res == NULL)
1558 goto error;
1559 DISPATCH();
1560 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001562 TARGET(BINARY_ADD) {
1563 PyObject *right = POP();
1564 PyObject *left = TOP();
1565 PyObject *sum;
1566 if (PyUnicode_CheckExact(left) &&
1567 PyUnicode_CheckExact(right)) {
1568 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001569 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001570 }
1571 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 sum = PyNumber_Add(left, right);
1573 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001574 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 Py_DECREF(right);
1576 SET_TOP(sum);
1577 if (sum == NULL)
1578 goto error;
1579 DISPATCH();
1580 }
1581
1582 TARGET(BINARY_SUBTRACT) {
1583 PyObject *right = POP();
1584 PyObject *left = TOP();
1585 PyObject *diff = PyNumber_Subtract(left, right);
1586 Py_DECREF(right);
1587 Py_DECREF(left);
1588 SET_TOP(diff);
1589 if (diff == NULL)
1590 goto error;
1591 DISPATCH();
1592 }
1593
1594 TARGET(BINARY_SUBSCR) {
1595 PyObject *sub = POP();
1596 PyObject *container = TOP();
1597 PyObject *res = PyObject_GetItem(container, sub);
1598 Py_DECREF(container);
1599 Py_DECREF(sub);
1600 SET_TOP(res);
1601 if (res == NULL)
1602 goto error;
1603 DISPATCH();
1604 }
1605
1606 TARGET(BINARY_LSHIFT) {
1607 PyObject *right = POP();
1608 PyObject *left = TOP();
1609 PyObject *res = PyNumber_Lshift(left, right);
1610 Py_DECREF(left);
1611 Py_DECREF(right);
1612 SET_TOP(res);
1613 if (res == NULL)
1614 goto error;
1615 DISPATCH();
1616 }
1617
1618 TARGET(BINARY_RSHIFT) {
1619 PyObject *right = POP();
1620 PyObject *left = TOP();
1621 PyObject *res = PyNumber_Rshift(left, right);
1622 Py_DECREF(left);
1623 Py_DECREF(right);
1624 SET_TOP(res);
1625 if (res == NULL)
1626 goto error;
1627 DISPATCH();
1628 }
1629
1630 TARGET(BINARY_AND) {
1631 PyObject *right = POP();
1632 PyObject *left = TOP();
1633 PyObject *res = PyNumber_And(left, right);
1634 Py_DECREF(left);
1635 Py_DECREF(right);
1636 SET_TOP(res);
1637 if (res == NULL)
1638 goto error;
1639 DISPATCH();
1640 }
1641
1642 TARGET(BINARY_XOR) {
1643 PyObject *right = POP();
1644 PyObject *left = TOP();
1645 PyObject *res = PyNumber_Xor(left, right);
1646 Py_DECREF(left);
1647 Py_DECREF(right);
1648 SET_TOP(res);
1649 if (res == NULL)
1650 goto error;
1651 DISPATCH();
1652 }
1653
1654 TARGET(BINARY_OR) {
1655 PyObject *right = POP();
1656 PyObject *left = TOP();
1657 PyObject *res = PyNumber_Or(left, right);
1658 Py_DECREF(left);
1659 Py_DECREF(right);
1660 SET_TOP(res);
1661 if (res == NULL)
1662 goto error;
1663 DISPATCH();
1664 }
1665
1666 TARGET(LIST_APPEND) {
1667 PyObject *v = POP();
1668 PyObject *list = PEEK(oparg);
1669 int err;
1670 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001672 if (err != 0)
1673 goto error;
1674 PREDICT(JUMP_ABSOLUTE);
1675 DISPATCH();
1676 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001678 TARGET(SET_ADD) {
1679 PyObject *v = POP();
1680 PyObject *set = stack_pointer[-oparg];
1681 int err;
1682 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 if (err != 0)
1685 goto error;
1686 PREDICT(JUMP_ABSOLUTE);
1687 DISPATCH();
1688 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001690 TARGET(INPLACE_POWER) {
1691 PyObject *exp = POP();
1692 PyObject *base = TOP();
1693 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1694 Py_DECREF(base);
1695 Py_DECREF(exp);
1696 SET_TOP(res);
1697 if (res == NULL)
1698 goto error;
1699 DISPATCH();
1700 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 TARGET(INPLACE_MULTIPLY) {
1703 PyObject *right = POP();
1704 PyObject *left = TOP();
1705 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1706 Py_DECREF(left);
1707 Py_DECREF(right);
1708 SET_TOP(res);
1709 if (res == NULL)
1710 goto error;
1711 DISPATCH();
1712 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Benjamin Petersond51374e2014-04-09 23:55:56 -04001714 TARGET(INPLACE_MATRIX_MULTIPLY) {
1715 PyObject *right = POP();
1716 PyObject *left = TOP();
1717 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1718 Py_DECREF(left);
1719 Py_DECREF(right);
1720 SET_TOP(res);
1721 if (res == NULL)
1722 goto error;
1723 DISPATCH();
1724 }
1725
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001726 TARGET(INPLACE_TRUE_DIVIDE) {
1727 PyObject *divisor = POP();
1728 PyObject *dividend = TOP();
1729 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1730 Py_DECREF(dividend);
1731 Py_DECREF(divisor);
1732 SET_TOP(quotient);
1733 if (quotient == NULL)
1734 goto error;
1735 DISPATCH();
1736 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001737
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001738 TARGET(INPLACE_FLOOR_DIVIDE) {
1739 PyObject *divisor = POP();
1740 PyObject *dividend = TOP();
1741 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1742 Py_DECREF(dividend);
1743 Py_DECREF(divisor);
1744 SET_TOP(quotient);
1745 if (quotient == NULL)
1746 goto error;
1747 DISPATCH();
1748 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001750 TARGET(INPLACE_MODULO) {
1751 PyObject *right = POP();
1752 PyObject *left = TOP();
1753 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1754 Py_DECREF(left);
1755 Py_DECREF(right);
1756 SET_TOP(mod);
1757 if (mod == NULL)
1758 goto error;
1759 DISPATCH();
1760 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001762 TARGET(INPLACE_ADD) {
1763 PyObject *right = POP();
1764 PyObject *left = TOP();
1765 PyObject *sum;
1766 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1767 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001768 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001769 }
1770 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001771 sum = PyNumber_InPlaceAdd(left, right);
1772 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001773 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 Py_DECREF(right);
1775 SET_TOP(sum);
1776 if (sum == NULL)
1777 goto error;
1778 DISPATCH();
1779 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001780
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001781 TARGET(INPLACE_SUBTRACT) {
1782 PyObject *right = POP();
1783 PyObject *left = TOP();
1784 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1785 Py_DECREF(left);
1786 Py_DECREF(right);
1787 SET_TOP(diff);
1788 if (diff == NULL)
1789 goto error;
1790 DISPATCH();
1791 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001793 TARGET(INPLACE_LSHIFT) {
1794 PyObject *right = POP();
1795 PyObject *left = TOP();
1796 PyObject *res = PyNumber_InPlaceLshift(left, right);
1797 Py_DECREF(left);
1798 Py_DECREF(right);
1799 SET_TOP(res);
1800 if (res == NULL)
1801 goto error;
1802 DISPATCH();
1803 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 TARGET(INPLACE_RSHIFT) {
1806 PyObject *right = POP();
1807 PyObject *left = TOP();
1808 PyObject *res = PyNumber_InPlaceRshift(left, right);
1809 Py_DECREF(left);
1810 Py_DECREF(right);
1811 SET_TOP(res);
1812 if (res == NULL)
1813 goto error;
1814 DISPATCH();
1815 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001816
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001817 TARGET(INPLACE_AND) {
1818 PyObject *right = POP();
1819 PyObject *left = TOP();
1820 PyObject *res = PyNumber_InPlaceAnd(left, right);
1821 Py_DECREF(left);
1822 Py_DECREF(right);
1823 SET_TOP(res);
1824 if (res == NULL)
1825 goto error;
1826 DISPATCH();
1827 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001829 TARGET(INPLACE_XOR) {
1830 PyObject *right = POP();
1831 PyObject *left = TOP();
1832 PyObject *res = PyNumber_InPlaceXor(left, right);
1833 Py_DECREF(left);
1834 Py_DECREF(right);
1835 SET_TOP(res);
1836 if (res == NULL)
1837 goto error;
1838 DISPATCH();
1839 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 TARGET(INPLACE_OR) {
1842 PyObject *right = POP();
1843 PyObject *left = TOP();
1844 PyObject *res = PyNumber_InPlaceOr(left, right);
1845 Py_DECREF(left);
1846 Py_DECREF(right);
1847 SET_TOP(res);
1848 if (res == NULL)
1849 goto error;
1850 DISPATCH();
1851 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001852
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 TARGET(STORE_SUBSCR) {
1854 PyObject *sub = TOP();
1855 PyObject *container = SECOND();
1856 PyObject *v = THIRD();
1857 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001859 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 Py_DECREF(container);
1863 Py_DECREF(sub);
1864 if (err != 0)
1865 goto error;
1866 DISPATCH();
1867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001869 TARGET(DELETE_SUBSCR) {
1870 PyObject *sub = TOP();
1871 PyObject *container = SECOND();
1872 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001874 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 err = PyObject_DelItem(container, sub);
1876 Py_DECREF(container);
1877 Py_DECREF(sub);
1878 if (err != 0)
1879 goto error;
1880 DISPATCH();
1881 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001882
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001884 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001886 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001887 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 PyErr_SetString(PyExc_RuntimeError,
1890 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 Py_DECREF(value);
1892 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001894 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 Py_DECREF(value);
1896 if (res == NULL)
1897 goto error;
1898 Py_DECREF(res);
1899 DISPATCH();
1900 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001901
Thomas Wouters434d0822000-08-24 20:11:32 +00001902#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001904#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 TARGET(RAISE_VARARGS) {
1906 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 switch (oparg) {
1908 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001913 if (do_raise(exc, cause)) {
1914 why = WHY_EXCEPTION;
1915 goto fast_block_end;
1916 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001917 break;
1918 default:
1919 PyErr_SetString(PyExc_SystemError,
1920 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 break;
1922 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 goto error;
1924 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 retval = POP();
1928 why = WHY_RETURN;
1929 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001931
Yury Selivanov75445082015-05-11 22:57:16 -04001932 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001933 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001934 PyObject *iter = NULL;
1935 PyObject *awaitable = NULL;
1936 PyObject *obj = TOP();
1937 PyTypeObject *type = Py_TYPE(obj);
1938
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001939 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001940 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001941 }
Yury Selivanov75445082015-05-11 22:57:16 -04001942
1943 if (getter != NULL) {
1944 iter = (*getter)(obj);
1945 Py_DECREF(obj);
1946 if (iter == NULL) {
1947 SET_TOP(NULL);
1948 goto error;
1949 }
1950 }
1951 else {
1952 SET_TOP(NULL);
1953 PyErr_Format(
1954 PyExc_TypeError,
1955 "'async for' requires an object with "
1956 "__aiter__ method, got %.100s",
1957 type->tp_name);
1958 Py_DECREF(obj);
1959 goto error;
1960 }
1961
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001962 if (Py_TYPE(iter)->tp_as_async != NULL &&
1963 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1964
1965 /* Starting with CPython 3.5.2 __aiter__ should return
1966 asynchronous iterators directly (not awaitables that
1967 resolve to asynchronous iterators.)
1968
1969 Therefore, we check if the object that was returned
1970 from __aiter__ has an __anext__ method. If it does,
1971 we wrap it in an awaitable that resolves to `iter`.
1972
1973 See http://bugs.python.org/issue27243 for more
1974 details.
1975 */
1976
1977 PyObject *wrapper = _PyAIterWrapper_New(iter);
1978 Py_DECREF(iter);
1979 SET_TOP(wrapper);
1980 DISPATCH();
1981 }
1982
Yury Selivanov5376ba92015-06-22 12:19:30 -04001983 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001984 if (awaitable == NULL) {
1985 SET_TOP(NULL);
1986 PyErr_Format(
1987 PyExc_TypeError,
1988 "'async for' received an invalid object "
1989 "from __aiter__: %.100s",
1990 Py_TYPE(iter)->tp_name);
1991
1992 Py_DECREF(iter);
1993 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001994 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001995 Py_DECREF(iter);
1996
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001997 if (PyErr_WarnFormat(
1998 PyExc_PendingDeprecationWarning, 1,
1999 "'%.100s' implements legacy __aiter__ protocol; "
2000 "__aiter__ should return an asynchronous "
2001 "iterator, not awaitable",
2002 type->tp_name))
2003 {
2004 /* Warning was converted to an error. */
2005 Py_DECREF(awaitable);
2006 SET_TOP(NULL);
2007 goto error;
2008 }
2009 }
2010
Yury Selivanov75445082015-05-11 22:57:16 -04002011 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002012 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002013 DISPATCH();
2014 }
2015
2016 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002017 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002018 PyObject *next_iter = NULL;
2019 PyObject *awaitable = NULL;
2020 PyObject *aiter = TOP();
2021 PyTypeObject *type = Py_TYPE(aiter);
2022
2023 if (type->tp_as_async != NULL)
2024 getter = type->tp_as_async->am_anext;
2025
2026 if (getter != NULL) {
2027 next_iter = (*getter)(aiter);
2028 if (next_iter == NULL) {
2029 goto error;
2030 }
2031 }
2032 else {
2033 PyErr_Format(
2034 PyExc_TypeError,
2035 "'async for' requires an iterator with "
2036 "__anext__ method, got %.100s",
2037 type->tp_name);
2038 goto error;
2039 }
2040
Yury Selivanov5376ba92015-06-22 12:19:30 -04002041 awaitable = _PyCoro_GetAwaitableIter(next_iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002042 if (awaitable == NULL) {
2043 PyErr_Format(
2044 PyExc_TypeError,
2045 "'async for' received an invalid object "
2046 "from __anext__: %.100s",
2047 Py_TYPE(next_iter)->tp_name);
2048
2049 Py_DECREF(next_iter);
2050 goto error;
2051 } else
2052 Py_DECREF(next_iter);
2053
2054 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002055 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002056 DISPATCH();
2057 }
2058
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002059 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002060 TARGET(GET_AWAITABLE) {
2061 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002062 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002063
2064 Py_DECREF(iterable);
2065
Yury Selivanovc724bae2016-03-02 11:30:46 -05002066 if (iter != NULL && PyCoro_CheckExact(iter)) {
2067 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2068 if (yf != NULL) {
2069 /* `iter` is a coroutine object that is being
2070 awaited, `yf` is a pointer to the current awaitable
2071 being awaited on. */
2072 Py_DECREF(yf);
2073 Py_CLEAR(iter);
2074 PyErr_SetString(
2075 PyExc_RuntimeError,
2076 "coroutine is being awaited already");
2077 /* The code below jumps to `error` if `iter` is NULL. */
2078 }
2079 }
2080
Yury Selivanov75445082015-05-11 22:57:16 -04002081 SET_TOP(iter); /* Even if it's NULL */
2082
2083 if (iter == NULL) {
2084 goto error;
2085 }
2086
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002087 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002088 DISPATCH();
2089 }
2090
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002091 TARGET(YIELD_FROM) {
2092 PyObject *v = POP();
2093 PyObject *reciever = TOP();
2094 int err;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002095 if (PyGen_CheckExact(reciever) || PyCoro_CheckExact(reciever)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002097 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002098 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002099 if (v == Py_None)
2100 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002101 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04002102 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002103 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 Py_DECREF(v);
2105 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002106 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002107 if (tstate->c_tracefunc != NULL
2108 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002109 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002110 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 if (err < 0)
2112 goto error;
2113 Py_DECREF(reciever);
2114 SET_TOP(val);
2115 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002116 }
Martin Panter95f53c12016-07-18 08:23:26 +00002117 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002118 f->f_stacktop = stack_pointer;
2119 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002120 /* and repeat... */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002121 f->f_lasti -= 2;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002122 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002124
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 retval = POP();
2127 f->f_stacktop = stack_pointer;
2128 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002129 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002130 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002131
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 TARGET(POP_EXCEPT) {
2133 PyTryBlock *b = PyFrame_BlockPop(f);
2134 if (b->b_type != EXCEPT_HANDLER) {
2135 PyErr_SetString(PyExc_SystemError,
2136 "popped block is not an except handler");
2137 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002141 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002142
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002143 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 TARGET(POP_BLOCK) {
2145 PyTryBlock *b = PyFrame_BlockPop(f);
2146 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002150 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 TARGET(END_FINALLY) {
2152 PyObject *status = POP();
2153 if (PyLong_Check(status)) {
2154 why = (enum why_code) PyLong_AS_LONG(status);
2155 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 if (why == WHY_RETURN ||
2157 why == WHY_CONTINUE)
2158 retval = POP();
2159 if (why == WHY_SILENCED) {
2160 /* An exception was silenced by 'with', we must
2161 manually unwind the EXCEPT_HANDLER block which was
2162 created when the exception was caught, otherwise
2163 the stack will be in an inconsistent state. */
2164 PyTryBlock *b = PyFrame_BlockPop(f);
2165 assert(b->b_type == EXCEPT_HANDLER);
2166 UNWIND_EXCEPT_HANDLER(b);
2167 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 Py_DECREF(status);
2169 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 Py_DECREF(status);
2172 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 else if (PyExceptionClass_Check(status)) {
2175 PyObject *exc = POP();
2176 PyObject *tb = POP();
2177 PyErr_Restore(status, exc, tb);
2178 why = WHY_EXCEPTION;
2179 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 PyErr_SetString(PyExc_SystemError,
2183 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 Py_DECREF(status);
2185 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 Py_DECREF(status);
2188 DISPATCH();
2189 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002190
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002192 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002193
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002194 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002195 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2197 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002198 PyErr_SetString(PyExc_NameError,
2199 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002201 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002203 }
2204 else {
2205 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2206 if (build_class_str == NULL)
2207 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2209 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002210 if (PyErr_ExceptionMatches(PyExc_KeyError))
2211 PyErr_SetString(PyExc_NameError,
2212 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002214 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002217 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002218 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 TARGET(STORE_NAME) {
2221 PyObject *name = GETITEM(names, oparg);
2222 PyObject *v = POP();
2223 PyObject *ns = f->f_locals;
2224 int err;
2225 if (ns == NULL) {
2226 PyErr_Format(PyExc_SystemError,
2227 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002228 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002229 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002231 if (PyDict_CheckExact(ns))
2232 err = PyDict_SetItem(ns, name, v);
2233 else
2234 err = PyObject_SetItem(ns, name, v);
2235 Py_DECREF(v);
2236 if (err != 0)
2237 goto error;
2238 DISPATCH();
2239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 TARGET(DELETE_NAME) {
2242 PyObject *name = GETITEM(names, oparg);
2243 PyObject *ns = f->f_locals;
2244 int err;
2245 if (ns == NULL) {
2246 PyErr_Format(PyExc_SystemError,
2247 "no locals when deleting %R", name);
2248 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 err = PyObject_DelItem(ns, name);
2251 if (err != 0) {
2252 format_exc_check_arg(PyExc_NameError,
2253 NAME_ERROR_MSG,
2254 name);
2255 goto error;
2256 }
2257 DISPATCH();
2258 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002259
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002260 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 TARGET(UNPACK_SEQUENCE) {
2262 PyObject *seq = POP(), *item, **items;
2263 if (PyTuple_CheckExact(seq) &&
2264 PyTuple_GET_SIZE(seq) == oparg) {
2265 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002267 item = items[oparg];
2268 Py_INCREF(item);
2269 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002270 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 } else if (PyList_CheckExact(seq) &&
2272 PyList_GET_SIZE(seq) == oparg) {
2273 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 item = items[oparg];
2276 Py_INCREF(item);
2277 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 stack_pointer + oparg)) {
2281 STACKADJ(oparg);
2282 } else {
2283 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002284 Py_DECREF(seq);
2285 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002288 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002290
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002291 TARGET(UNPACK_EX) {
2292 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2293 PyObject *seq = POP();
2294
2295 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2296 stack_pointer + totalargs)) {
2297 stack_pointer += totalargs;
2298 } else {
2299 Py_DECREF(seq);
2300 goto error;
2301 }
2302 Py_DECREF(seq);
2303 DISPATCH();
2304 }
2305
2306 TARGET(STORE_ATTR) {
2307 PyObject *name = GETITEM(names, oparg);
2308 PyObject *owner = TOP();
2309 PyObject *v = SECOND();
2310 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002313 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 Py_DECREF(owner);
2315 if (err != 0)
2316 goto error;
2317 DISPATCH();
2318 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002319
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 TARGET(DELETE_ATTR) {
2321 PyObject *name = GETITEM(names, oparg);
2322 PyObject *owner = POP();
2323 int err;
2324 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2325 Py_DECREF(owner);
2326 if (err != 0)
2327 goto error;
2328 DISPATCH();
2329 }
2330
2331 TARGET(STORE_GLOBAL) {
2332 PyObject *name = GETITEM(names, oparg);
2333 PyObject *v = POP();
2334 int err;
2335 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002337 if (err != 0)
2338 goto error;
2339 DISPATCH();
2340 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002341
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002342 TARGET(DELETE_GLOBAL) {
2343 PyObject *name = GETITEM(names, oparg);
2344 int err;
2345 err = PyDict_DelItem(f->f_globals, name);
2346 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002348 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002349 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002350 }
2351 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002353
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002354 TARGET(LOAD_NAME) {
2355 PyObject *name = GETITEM(names, oparg);
2356 PyObject *locals = f->f_locals;
2357 PyObject *v;
2358 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002360 "no locals when loading %R", name);
2361 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 if (PyDict_CheckExact(locals)) {
2364 v = PyDict_GetItem(locals, name);
2365 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 }
2367 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002369 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002370 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2371 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 PyErr_Clear();
2373 }
2374 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002375 if (v == NULL) {
2376 v = PyDict_GetItem(f->f_globals, name);
2377 Py_XINCREF(v);
2378 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002379 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 v = PyDict_GetItem(f->f_builtins, name);
2381 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002382 format_exc_check_arg(
2383 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 NAME_ERROR_MSG, name);
2385 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002386 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002387 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002388 }
2389 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002390 v = PyObject_GetItem(f->f_builtins, name);
2391 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002392 if (PyErr_ExceptionMatches(PyExc_KeyError))
2393 format_exc_check_arg(
2394 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002395 NAME_ERROR_MSG, name);
2396 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002397 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002398 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002399 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002400 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002403 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002404
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 TARGET(LOAD_GLOBAL) {
2406 PyObject *name = GETITEM(names, oparg);
2407 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002408 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002409 && PyDict_CheckExact(f->f_builtins))
2410 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002412 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002413 name);
2414 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002415 if (!_PyErr_OCCURRED()) {
2416 /* _PyDict_LoadGlobal() returns NULL without raising
2417 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002418 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002419 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002420 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002425 else {
2426 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002427
2428 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 v = PyObject_GetItem(f->f_globals, name);
2430 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002431 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2432 goto error;
2433 PyErr_Clear();
2434
Victor Stinnerb4efc962015-11-20 09:24:02 +01002435 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002436 v = PyObject_GetItem(f->f_builtins, name);
2437 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002438 if (PyErr_ExceptionMatches(PyExc_KeyError))
2439 format_exc_check_arg(
2440 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002441 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002443 }
2444 }
2445 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002446 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002447 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002448 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002449
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 TARGET(DELETE_FAST) {
2451 PyObject *v = GETLOCAL(oparg);
2452 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 SETLOCAL(oparg, NULL);
2454 DISPATCH();
2455 }
2456 format_exc_check_arg(
2457 PyExc_UnboundLocalError,
2458 UNBOUNDLOCAL_ERROR_MSG,
2459 PyTuple_GetItem(co->co_varnames, oparg)
2460 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 goto error;
2462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002463
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 TARGET(DELETE_DEREF) {
2465 PyObject *cell = freevars[oparg];
2466 if (PyCell_GET(cell) != NULL) {
2467 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002468 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002469 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002470 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 goto error;
2472 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002473
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002474 TARGET(LOAD_CLOSURE) {
2475 PyObject *cell = freevars[oparg];
2476 Py_INCREF(cell);
2477 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002480
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002481 TARGET(LOAD_CLASSDEREF) {
2482 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002483 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002484 assert(locals);
2485 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2486 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2487 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2488 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2489 if (PyDict_CheckExact(locals)) {
2490 value = PyDict_GetItem(locals, name);
2491 Py_XINCREF(value);
2492 }
2493 else {
2494 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002495 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002496 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2497 goto error;
2498 PyErr_Clear();
2499 }
2500 }
2501 if (!value) {
2502 PyObject *cell = freevars[oparg];
2503 value = PyCell_GET(cell);
2504 if (value == NULL) {
2505 format_exc_unbound(co, oparg);
2506 goto error;
2507 }
2508 Py_INCREF(value);
2509 }
2510 PUSH(value);
2511 DISPATCH();
2512 }
2513
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 TARGET(LOAD_DEREF) {
2515 PyObject *cell = freevars[oparg];
2516 PyObject *value = PyCell_GET(cell);
2517 if (value == NULL) {
2518 format_exc_unbound(co, oparg);
2519 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002521 Py_INCREF(value);
2522 PUSH(value);
2523 DISPATCH();
2524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002525
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 TARGET(STORE_DEREF) {
2527 PyObject *v = POP();
2528 PyObject *cell = freevars[oparg];
2529 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002531 DISPATCH();
2532 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002533
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002534 TARGET(BUILD_TUPLE) {
2535 PyObject *tup = PyTuple_New(oparg);
2536 if (tup == NULL)
2537 goto error;
2538 while (--oparg >= 0) {
2539 PyObject *item = POP();
2540 PyTuple_SET_ITEM(tup, oparg, item);
2541 }
2542 PUSH(tup);
2543 DISPATCH();
2544 }
2545
2546 TARGET(BUILD_LIST) {
2547 PyObject *list = PyList_New(oparg);
2548 if (list == NULL)
2549 goto error;
2550 while (--oparg >= 0) {
2551 PyObject *item = POP();
2552 PyList_SET_ITEM(list, oparg, item);
2553 }
2554 PUSH(list);
2555 DISPATCH();
2556 }
2557
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002558 TARGET(BUILD_TUPLE_UNPACK)
2559 TARGET(BUILD_LIST_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002560 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
2561 int i;
2562 PyObject *sum = PyList_New(0);
2563 PyObject *return_value;
2564 if (sum == NULL)
2565 goto error;
2566
2567 for (i = oparg; i > 0; i--) {
2568 PyObject *none_val;
2569
2570 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2571 if (none_val == NULL) {
2572 Py_DECREF(sum);
2573 goto error;
2574 }
2575 Py_DECREF(none_val);
2576 }
2577
2578 if (convert_to_tuple) {
2579 return_value = PyList_AsTuple(sum);
2580 Py_DECREF(sum);
2581 if (return_value == NULL)
2582 goto error;
2583 }
2584 else {
2585 return_value = sum;
2586 }
2587
2588 while (oparg--)
2589 Py_DECREF(POP());
2590 PUSH(return_value);
2591 DISPATCH();
2592 }
2593
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002594 TARGET(BUILD_SET) {
2595 PyObject *set = PySet_New(NULL);
2596 int err = 0;
2597 if (set == NULL)
2598 goto error;
2599 while (--oparg >= 0) {
2600 PyObject *item = POP();
2601 if (err == 0)
2602 err = PySet_Add(set, item);
2603 Py_DECREF(item);
2604 }
2605 if (err != 0) {
2606 Py_DECREF(set);
2607 goto error;
2608 }
2609 PUSH(set);
2610 DISPATCH();
2611 }
2612
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002613 TARGET(BUILD_SET_UNPACK) {
2614 int i;
2615 PyObject *sum = PySet_New(NULL);
2616 if (sum == NULL)
2617 goto error;
2618
2619 for (i = oparg; i > 0; i--) {
2620 if (_PySet_Update(sum, PEEK(i)) < 0) {
2621 Py_DECREF(sum);
2622 goto error;
2623 }
2624 }
2625
2626 while (oparg--)
2627 Py_DECREF(POP());
2628 PUSH(sum);
2629 DISPATCH();
2630 }
2631
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002632 TARGET(BUILD_MAP) {
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002633 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002634 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2635 if (map == NULL)
2636 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002637 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002638 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002639 PyObject *key = PEEK(2*i);
2640 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002641 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002642 if (err != 0) {
2643 Py_DECREF(map);
2644 goto error;
2645 }
2646 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002647
2648 while (oparg--) {
2649 Py_DECREF(POP());
2650 Py_DECREF(POP());
2651 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 PUSH(map);
2653 DISPATCH();
2654 }
2655
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002656 TARGET(BUILD_CONST_KEY_MAP) {
2657 int i;
2658 PyObject *map;
2659 PyObject *keys = TOP();
2660 if (!PyTuple_CheckExact(keys) ||
2661 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2662 PyErr_SetString(PyExc_SystemError,
2663 "bad BUILD_CONST_KEY_MAP keys argument");
2664 goto error;
2665 }
2666 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2667 if (map == NULL) {
2668 goto error;
2669 }
2670 for (i = oparg; i > 0; i--) {
2671 int err;
2672 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2673 PyObject *value = PEEK(i + 1);
2674 err = PyDict_SetItem(map, key, value);
2675 if (err != 0) {
2676 Py_DECREF(map);
2677 goto error;
2678 }
2679 }
2680
2681 Py_DECREF(POP());
2682 while (oparg--) {
2683 Py_DECREF(POP());
2684 }
2685 PUSH(map);
2686 DISPATCH();
2687 }
2688
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002689 TARGET(BUILD_MAP_UNPACK_WITH_CALL)
2690 TARGET(BUILD_MAP_UNPACK) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002691 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
2692 int num_maps;
2693 int function_location;
2694 int i;
2695 PyObject *sum = PyDict_New();
2696 if (sum == NULL)
2697 goto error;
2698 if (with_call) {
2699 num_maps = oparg & 0xff;
2700 function_location = (oparg>>8) & 0xff;
2701 }
2702 else {
2703 num_maps = oparg;
2704 }
2705
2706 for (i = num_maps; i > 0; i--) {
2707 PyObject *arg = PEEK(i);
2708 if (with_call) {
2709 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2710
2711 if (intersection == NULL) {
2712 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2713 PyObject *func = (
2714 PEEK(function_location + num_maps));
2715 PyErr_Format(PyExc_TypeError,
2716 "%.200s%.200s argument after ** "
2717 "must be a mapping, not %.200s",
2718 PyEval_GetFuncName(func),
2719 PyEval_GetFuncDesc(func),
2720 arg->ob_type->tp_name);
2721 }
2722 Py_DECREF(sum);
2723 goto error;
2724 }
2725
2726 if (PySet_GET_SIZE(intersection)) {
2727 Py_ssize_t idx = 0;
2728 PyObject *key;
2729 PyObject *func = PEEK(function_location + num_maps);
2730 Py_hash_t hash;
2731 _PySet_NextEntry(intersection, &idx, &key, &hash);
2732 if (!PyUnicode_Check(key)) {
2733 PyErr_Format(PyExc_TypeError,
2734 "%.200s%.200s keywords must be strings",
2735 PyEval_GetFuncName(func),
2736 PyEval_GetFuncDesc(func));
2737 } else {
2738 PyErr_Format(PyExc_TypeError,
2739 "%.200s%.200s got multiple "
2740 "values for keyword argument '%U'",
2741 PyEval_GetFuncName(func),
2742 PyEval_GetFuncDesc(func),
2743 key);
2744 }
2745 Py_DECREF(intersection);
2746 Py_DECREF(sum);
2747 goto error;
2748 }
2749 Py_DECREF(intersection);
2750 }
2751
2752 if (PyDict_Update(sum, arg) < 0) {
2753 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2754 PyErr_Format(PyExc_TypeError,
2755 "'%.200s' object is not a mapping",
2756 arg->ob_type->tp_name);
2757 }
2758 Py_DECREF(sum);
2759 goto error;
2760 }
2761 }
2762
2763 while (num_maps--)
2764 Py_DECREF(POP());
2765 PUSH(sum);
2766 DISPATCH();
2767 }
2768
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 TARGET(MAP_ADD) {
2770 PyObject *key = TOP();
2771 PyObject *value = SECOND();
2772 PyObject *map;
2773 int err;
2774 STACKADJ(-2);
2775 map = stack_pointer[-oparg]; /* dict */
2776 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002777 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 Py_DECREF(value);
2779 Py_DECREF(key);
2780 if (err != 0)
2781 goto error;
2782 PREDICT(JUMP_ABSOLUTE);
2783 DISPATCH();
2784 }
2785
2786 TARGET(LOAD_ATTR) {
2787 PyObject *name = GETITEM(names, oparg);
2788 PyObject *owner = TOP();
2789 PyObject *res = PyObject_GetAttr(owner, name);
2790 Py_DECREF(owner);
2791 SET_TOP(res);
2792 if (res == NULL)
2793 goto error;
2794 DISPATCH();
2795 }
2796
2797 TARGET(COMPARE_OP) {
2798 PyObject *right = POP();
2799 PyObject *left = TOP();
2800 PyObject *res = cmp_outcome(oparg, left, right);
2801 Py_DECREF(left);
2802 Py_DECREF(right);
2803 SET_TOP(res);
2804 if (res == NULL)
2805 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 PREDICT(POP_JUMP_IF_FALSE);
2807 PREDICT(POP_JUMP_IF_TRUE);
2808 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002809 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002810
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002813 PyObject *fromlist = POP();
2814 PyObject *level = TOP();
2815 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 READ_TIMESTAMP(intr0);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002817 res = import_name(f, name, fromlist, level);
2818 Py_DECREF(level);
2819 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 SET_TOP(res);
2822 if (res == NULL)
2823 goto error;
2824 DISPATCH();
2825 }
2826
2827 TARGET(IMPORT_STAR) {
2828 PyObject *from = POP(), *locals;
2829 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002830 if (PyFrame_FastToLocalsWithError(f) < 0)
2831 goto error;
2832
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 locals = f->f_locals;
2834 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 PyErr_SetString(PyExc_SystemError,
2836 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 }
2839 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 READ_TIMESTAMP(intr1);
2842 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 Py_DECREF(from);
2844 if (err != 0)
2845 goto error;
2846 DISPATCH();
2847 }
Guido van Rossum25831651993-05-19 14:50:45 +00002848
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002849 TARGET(IMPORT_FROM) {
2850 PyObject *name = GETITEM(names, oparg);
2851 PyObject *from = TOP();
2852 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002856 PUSH(res);
2857 if (res == NULL)
2858 goto error;
2859 DISPATCH();
2860 }
Thomas Wouters52152252000-08-17 22:55:00 +00002861
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 JUMPBY(oparg);
2864 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002865 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002866
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002867 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 TARGET(POP_JUMP_IF_FALSE) {
2869 PyObject *cond = POP();
2870 int err;
2871 if (cond == Py_True) {
2872 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 FAST_DISPATCH();
2874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 if (cond == Py_False) {
2876 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 JUMPTO(oparg);
2878 FAST_DISPATCH();
2879 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 err = PyObject_IsTrue(cond);
2881 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 if (err > 0)
2883 err = 0;
2884 else if (err == 0)
2885 JUMPTO(oparg);
2886 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002890
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002891 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 TARGET(POP_JUMP_IF_TRUE) {
2893 PyObject *cond = POP();
2894 int err;
2895 if (cond == Py_False) {
2896 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 FAST_DISPATCH();
2898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 if (cond == Py_True) {
2900 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 JUMPTO(oparg);
2902 FAST_DISPATCH();
2903 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 err = PyObject_IsTrue(cond);
2905 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 if (err > 0) {
2907 err = 0;
2908 JUMPTO(oparg);
2909 }
2910 else if (err == 0)
2911 ;
2912 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002916
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 TARGET(JUMP_IF_FALSE_OR_POP) {
2918 PyObject *cond = TOP();
2919 int err;
2920 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 FAST_DISPATCH();
2924 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 JUMPTO(oparg);
2927 FAST_DISPATCH();
2928 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 if (err > 0) {
2931 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 err = 0;
2934 }
2935 else if (err == 0)
2936 JUMPTO(oparg);
2937 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002938 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002939 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002940 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002941
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 TARGET(JUMP_IF_TRUE_OR_POP) {
2943 PyObject *cond = TOP();
2944 int err;
2945 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002948 FAST_DISPATCH();
2949 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 JUMPTO(oparg);
2952 FAST_DISPATCH();
2953 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 if (err > 0) {
2956 err = 0;
2957 JUMPTO(oparg);
2958 }
2959 else if (err == 0) {
2960 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 }
2963 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002967
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002968 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002971#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 /* Enabling this path speeds-up all while and for-loops by bypassing
2973 the per-loop checks for signals. By default, this should be turned-off
2974 because it prevents detection of a control-break in tight loops like
2975 "while 1: pass". Compile with this option turned-on when you need
2976 the speed-up and do not need break checking inside tight loops (ones
2977 that contain only instructions ending with FAST_DISPATCH).
2978 */
2979 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002980#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002982#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002984
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002988 PyObject *iter = PyObject_GetIter(iterable);
2989 Py_DECREF(iterable);
2990 SET_TOP(iter);
2991 if (iter == NULL)
2992 goto error;
2993 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002994 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002995 DISPATCH();
2996 }
2997
2998 TARGET(GET_YIELD_FROM_ITER) {
2999 /* before: [obj]; after [getiter(obj)] */
3000 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003001 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003002 if (PyCoro_CheckExact(iterable)) {
3003 /* `iterable` is a coroutine */
3004 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3005 /* and it is used in a 'yield from' expression of a
3006 regular generator. */
3007 Py_DECREF(iterable);
3008 SET_TOP(NULL);
3009 PyErr_SetString(PyExc_TypeError,
3010 "cannot 'yield from' a coroutine object "
3011 "in a non-coroutine generator");
3012 goto error;
3013 }
3014 }
3015 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003016 /* `iterable` is not a generator. */
3017 iter = PyObject_GetIter(iterable);
3018 Py_DECREF(iterable);
3019 SET_TOP(iter);
3020 if (iter == NULL)
3021 goto error;
3022 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003023 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 DISPATCH();
3025 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003026
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003027 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 PyObject *iter = TOP();
3031 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
3032 if (next != NULL) {
3033 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 PREDICT(STORE_FAST);
3035 PREDICT(UNPACK_SEQUENCE);
3036 DISPATCH();
3037 }
3038 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
3040 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08003041 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003042 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 PyErr_Clear();
3044 }
3045 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 STACKADJ(-1);
3047 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003048 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003049 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003050 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003051 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003052
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 why = WHY_BREAK;
3055 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003057
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 if (retval == NULL)
3061 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 why = WHY_CONTINUE;
3063 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00003065
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003066 TARGET(SETUP_LOOP)
3067 TARGET(SETUP_EXCEPT)
3068 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 /* NOTE: If you add any new block-setup opcodes that
3070 are not try/except/finally handlers, you may need
3071 to update the PyGen_NeedsFinalizing() function.
3072 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3075 STACK_LEVEL());
3076 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003078
Yury Selivanov75445082015-05-11 22:57:16 -04003079 TARGET(BEFORE_ASYNC_WITH) {
3080 _Py_IDENTIFIER(__aexit__);
3081 _Py_IDENTIFIER(__aenter__);
3082
3083 PyObject *mgr = TOP();
3084 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3085 *enter;
3086 PyObject *res;
3087 if (exit == NULL)
3088 goto error;
3089 SET_TOP(exit);
3090 enter = special_lookup(mgr, &PyId___aenter__);
3091 Py_DECREF(mgr);
3092 if (enter == NULL)
3093 goto error;
3094 res = PyObject_CallFunctionObjArgs(enter, NULL);
3095 Py_DECREF(enter);
3096 if (res == NULL)
3097 goto error;
3098 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003099 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003100 DISPATCH();
3101 }
3102
3103 TARGET(SETUP_ASYNC_WITH) {
3104 PyObject *res = POP();
3105 /* Setup the finally block before pushing the result
3106 of __aenter__ on the stack. */
3107 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3108 STACK_LEVEL());
3109 PUSH(res);
3110 DISPATCH();
3111 }
3112
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003113 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003114 _Py_IDENTIFIER(__exit__);
3115 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003116 PyObject *mgr = TOP();
3117 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3118 PyObject *res;
3119 if (exit == NULL)
3120 goto error;
3121 SET_TOP(exit);
3122 enter = special_lookup(mgr, &PyId___enter__);
3123 Py_DECREF(mgr);
3124 if (enter == NULL)
3125 goto error;
3126 res = PyObject_CallFunctionObjArgs(enter, NULL);
3127 Py_DECREF(enter);
3128 if (res == NULL)
3129 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003130 /* Setup the finally block before pushing the result
3131 of __enter__ on the stack. */
3132 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3133 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003135 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003136 DISPATCH();
3137 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003138
Yury Selivanov75445082015-05-11 22:57:16 -04003139 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003140 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 how/why we entered the finally clause:
3142 - TOP = None
3143 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3144 - TOP = WHY_*; no retval below it
3145 - (TOP, SECOND, THIRD) = exc_info()
3146 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3147 Below them is EXIT, the context.__exit__ bound method.
3148 In the last case, we must call
3149 EXIT(TOP, SECOND, THIRD)
3150 otherwise we must call
3151 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003152
Benjamin Peterson8f169482013-10-29 22:25:06 -04003153 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003155 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 In addition, if the stack represents an exception,
3159 *and* the function call returns a 'true' value, we
3160 push WHY_SILENCED onto the stack. END_FINALLY will
3161 then not re-raise the exception. (But non-local
3162 gotos should still be resumed.)
3163 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003166 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003167 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 (void)POP();
3169 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003170 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003171 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003172 else if (PyLong_Check(exc)) {
3173 STACKADJ(-1);
3174 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 case WHY_RETURN:
3176 case WHY_CONTINUE:
3177 /* Retval in TOP. */
3178 exit_func = SECOND();
3179 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003180 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 break;
3182 default:
3183 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 break;
3186 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003187 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 }
3189 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003190 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003192 val = SECOND();
3193 tb = THIRD();
3194 tp2 = FOURTH();
3195 exc2 = PEEK(5);
3196 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003198 SET_VALUE(7, tb2);
3199 SET_VALUE(6, exc2);
3200 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3202 SET_FOURTH(NULL);
3203 /* We just shifted the stack down, so we have
3204 to tell the except handler block that the
3205 values are lower than it expects. */
3206 block = &f->f_blockstack[f->f_iblock - 1];
3207 assert(block->b_type == EXCEPT_HANDLER);
3208 block->b_level--;
3209 }
3210 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003211 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003212 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003213 if (res == NULL)
3214 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003215
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003216 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003217 PUSH(exc);
3218 PUSH(res);
3219 PREDICT(WITH_CLEANUP_FINISH);
3220 DISPATCH();
3221 }
3222
3223 PREDICTED(WITH_CLEANUP_FINISH);
3224 TARGET(WITH_CLEANUP_FINISH) {
3225 PyObject *res = POP();
3226 PyObject *exc = POP();
3227 int err;
3228
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 if (exc != Py_None)
3230 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 else
3232 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003233
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003235 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003238 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 else if (err > 0) {
3240 err = 0;
3241 /* There was an exception and a True return */
3242 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3243 }
3244 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003245 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003246 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003247
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003248 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003249 TARGET(CALL_FUNCTION) {
3250 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 PCALL(PCALL_ALL);
3252 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003253#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003254 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003255#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003259 PUSH(res);
3260 if (res == NULL)
3261 goto error;
3262 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003264
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003265 TARGET(CALL_FUNCTION_VAR)
3266 TARGET(CALL_FUNCTION_KW)
3267 TARGET(CALL_FUNCTION_VAR_KW) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 int na = oparg & 0xff;
3269 int nk = (oparg>>8) & 0xff;
3270 int flags = (opcode - CALL_FUNCTION) & 3;
3271 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003272 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003273 PCALL(PCALL_ALL);
3274 if (flags & CALL_FLAG_VAR)
3275 n++;
3276 if (flags & CALL_FLAG_KW)
3277 n++;
3278 pfunc = stack_pointer - n - 1;
3279 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003281 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00003282 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 PyObject *self = PyMethod_GET_SELF(func);
3284 Py_INCREF(self);
3285 func = PyMethod_GET_FUNCTION(func);
3286 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03003287 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00003289 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 } else
3291 Py_INCREF(func);
3292 sp = stack_pointer;
3293 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 READ_TIMESTAMP(intr1);
3296 stack_pointer = sp;
3297 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003299 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003300 PyObject *o = POP();
3301 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003303 PUSH(res);
3304 if (res == NULL)
3305 goto error;
3306 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003308
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003309 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003310 PyObject *qualname = POP();
3311 PyObject *codeobj = POP();
3312 PyFunctionObject *func = (PyFunctionObject *)
3313 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003314
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003315 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003316 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003317 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003320
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003321 if (oparg & 0x08) {
3322 assert(PyTuple_CheckExact(TOP()));
3323 func ->func_closure = POP();
3324 }
3325 if (oparg & 0x04) {
3326 assert(PyDict_CheckExact(TOP()));
3327 func->func_annotations = POP();
3328 }
3329 if (oparg & 0x02) {
3330 assert(PyDict_CheckExact(TOP()));
3331 func->func_kwdefaults = POP();
3332 }
3333 if (oparg & 0x01) {
3334 assert(PyTuple_CheckExact(TOP()));
3335 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003337
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003338 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003339 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003340 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003341
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003342 TARGET(BUILD_SLICE) {
3343 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003345 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003347 step = NULL;
3348 stop = POP();
3349 start = TOP();
3350 slice = PySlice_New(start, stop, step);
3351 Py_DECREF(start);
3352 Py_DECREF(stop);
3353 Py_XDECREF(step);
3354 SET_TOP(slice);
3355 if (slice == NULL)
3356 goto error;
3357 DISPATCH();
3358 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003359
Eric V. Smitha78c7952015-11-03 12:45:05 -05003360 TARGET(FORMAT_VALUE) {
3361 /* Handles f-string value formatting. */
3362 PyObject *result;
3363 PyObject *fmt_spec;
3364 PyObject *value;
3365 PyObject *(*conv_fn)(PyObject *);
3366 int which_conversion = oparg & FVC_MASK;
3367 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3368
3369 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003370 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003371
3372 /* See if any conversion is specified. */
3373 switch (which_conversion) {
3374 case FVC_STR: conv_fn = PyObject_Str; break;
3375 case FVC_REPR: conv_fn = PyObject_Repr; break;
3376 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3377
3378 /* Must be 0 (meaning no conversion), since only four
3379 values are allowed by (oparg & FVC_MASK). */
3380 default: conv_fn = NULL; break;
3381 }
3382
3383 /* If there's a conversion function, call it and replace
3384 value with that result. Otherwise, just use value,
3385 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003386 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003387 result = conv_fn(value);
3388 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003389 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003390 Py_XDECREF(fmt_spec);
3391 goto error;
3392 }
3393 value = result;
3394 }
3395
3396 /* If value is a unicode object, and there's no fmt_spec,
3397 then we know the result of format(value) is value
3398 itself. In that case, skip calling format(). I plan to
3399 move this optimization in to PyObject_Format()
3400 itself. */
3401 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3402 /* Do nothing, just transfer ownership to result. */
3403 result = value;
3404 } else {
3405 /* Actually call format(). */
3406 result = PyObject_Format(value, fmt_spec);
3407 Py_DECREF(value);
3408 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003409 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003410 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003411 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003412 }
3413
Eric V. Smith135d5f42016-02-05 18:23:08 -05003414 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003415 DISPATCH();
3416 }
3417
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003418 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003419 int oldoparg = oparg;
3420 NEXTOPARG();
3421 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003423 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003424
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003425
Antoine Pitrou042b1282010-08-13 21:15:58 +00003426#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003427 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003428#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 default:
3430 fprintf(stderr,
3431 "XXX lineno: %d, opcode: %d\n",
3432 PyFrame_GetLineNumber(f),
3433 opcode);
3434 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003435 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003436
3437#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003439#endif
3440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003442
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003443 /* This should never be reached. Every opcode should end with DISPATCH()
3444 or goto error. */
3445 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003446
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003447error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003449
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003450 assert(why == WHY_NOT);
3451 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003452
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003453 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003454#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003455 if (!PyErr_Occurred())
3456 PyErr_SetString(PyExc_SystemError,
3457 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003458#else
3459 assert(PyErr_Occurred());
3460#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003461
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003462 /* Log traceback info. */
3463 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003464
Benjamin Peterson51f46162013-01-23 08:38:47 -05003465 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003466 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3467 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003468
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003469fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003470 assert(why != WHY_NOT);
3471
3472 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003473 while (why != WHY_NOT && f->f_iblock > 0) {
3474 /* Peek at the current block. */
3475 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 assert(why != WHY_YIELD);
3478 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3479 why = WHY_NOT;
3480 JUMPTO(PyLong_AS_LONG(retval));
3481 Py_DECREF(retval);
3482 break;
3483 }
3484 /* Now we have to pop the block. */
3485 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 if (b->b_type == EXCEPT_HANDLER) {
3488 UNWIND_EXCEPT_HANDLER(b);
3489 continue;
3490 }
3491 UNWIND_BLOCK(b);
3492 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3493 why = WHY_NOT;
3494 JUMPTO(b->b_handler);
3495 break;
3496 }
3497 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3498 || b->b_type == SETUP_FINALLY)) {
3499 PyObject *exc, *val, *tb;
3500 int handler = b->b_handler;
3501 /* Beware, this invalidates all b->b_* fields */
3502 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3503 PUSH(tstate->exc_traceback);
3504 PUSH(tstate->exc_value);
3505 if (tstate->exc_type != NULL) {
3506 PUSH(tstate->exc_type);
3507 }
3508 else {
3509 Py_INCREF(Py_None);
3510 PUSH(Py_None);
3511 }
3512 PyErr_Fetch(&exc, &val, &tb);
3513 /* Make the raw exception data
3514 available to the handler,
3515 so a program can emulate the
3516 Python main loop. */
3517 PyErr_NormalizeException(
3518 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003519 if (tb != NULL)
3520 PyException_SetTraceback(val, tb);
3521 else
3522 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 Py_INCREF(exc);
3524 tstate->exc_type = exc;
3525 Py_INCREF(val);
3526 tstate->exc_value = val;
3527 tstate->exc_traceback = tb;
3528 if (tb == NULL)
3529 tb = Py_None;
3530 Py_INCREF(tb);
3531 PUSH(tb);
3532 PUSH(val);
3533 PUSH(exc);
3534 why = WHY_NOT;
3535 JUMPTO(handler);
3536 break;
3537 }
3538 if (b->b_type == SETUP_FINALLY) {
3539 if (why & (WHY_RETURN | WHY_CONTINUE))
3540 PUSH(retval);
3541 PUSH(PyLong_FromLong((long)why));
3542 why = WHY_NOT;
3543 JUMPTO(b->b_handler);
3544 break;
3545 }
3546 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 if (why != WHY_NOT)
3551 break;
3552 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003553
Victor Stinnerace47d72013-07-18 01:41:08 +02003554 assert(!PyErr_Occurred());
3555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 assert(why != WHY_YIELD);
3559 /* Pop remaining stack entries. */
3560 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003561 PyObject *o = POP();
3562 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 if (why != WHY_RETURN)
3566 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003567
Victor Stinner4a7cc882015-03-06 23:35:27 +01003568 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003569
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003570fast_yield:
Yury Selivanov5376ba92015-06-22 12:19:30 -04003571 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003572
Benjamin Petersonac913412011-07-03 16:25:11 -05003573 /* The purpose of this block is to put aside the generator's exception
3574 state and restore that of the calling frame. If the current
3575 exception state is from the caller, we clear the exception values
3576 on the generator frame, so they are not swapped back in latter. The
3577 origin of the current exception state is determined by checking for
3578 except handler blocks, which we must be in iff a new exception
3579 state came into existence in this frame. (An uncaught exception
3580 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3581 int i;
3582 for (i = 0; i < f->f_iblock; i++)
3583 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3584 break;
3585 if (i == f->f_iblock)
3586 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003587 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003588 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003589 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003590 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003593 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003595 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3596 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003598 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003599 why = WHY_EXCEPTION;
3600 }
3601 }
3602 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003603 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3604 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 PyTrace_RETURN, NULL);
3606 }
3607 }
3608 if (tstate->c_profilefunc) {
3609 if (why == WHY_EXCEPTION)
3610 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003611 tstate->c_profileobj,
3612 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003614 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3615 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003617 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003618 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 }
3620 }
3621 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003623 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003624exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003626 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003628
Victor Stinnerefde1462015-03-21 15:04:43 +01003629 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003630}
3631
Benjamin Petersonb204a422011-06-05 22:04:07 -05003632static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003633format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3634{
3635 int err;
3636 Py_ssize_t len = PyList_GET_SIZE(names);
3637 PyObject *name_str, *comma, *tail, *tmp;
3638
3639 assert(PyList_CheckExact(names));
3640 assert(len >= 1);
3641 /* Deal with the joys of natural language. */
3642 switch (len) {
3643 case 1:
3644 name_str = PyList_GET_ITEM(names, 0);
3645 Py_INCREF(name_str);
3646 break;
3647 case 2:
3648 name_str = PyUnicode_FromFormat("%U and %U",
3649 PyList_GET_ITEM(names, len - 2),
3650 PyList_GET_ITEM(names, len - 1));
3651 break;
3652 default:
3653 tail = PyUnicode_FromFormat(", %U, and %U",
3654 PyList_GET_ITEM(names, len - 2),
3655 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003656 if (tail == NULL)
3657 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003658 /* Chop off the last two objects in the list. This shouldn't actually
3659 fail, but we can't be too careful. */
3660 err = PyList_SetSlice(names, len - 2, len, NULL);
3661 if (err == -1) {
3662 Py_DECREF(tail);
3663 return;
3664 }
3665 /* Stitch everything up into a nice comma-separated list. */
3666 comma = PyUnicode_FromString(", ");
3667 if (comma == NULL) {
3668 Py_DECREF(tail);
3669 return;
3670 }
3671 tmp = PyUnicode_Join(comma, names);
3672 Py_DECREF(comma);
3673 if (tmp == NULL) {
3674 Py_DECREF(tail);
3675 return;
3676 }
3677 name_str = PyUnicode_Concat(tmp, tail);
3678 Py_DECREF(tmp);
3679 Py_DECREF(tail);
3680 break;
3681 }
3682 if (name_str == NULL)
3683 return;
3684 PyErr_Format(PyExc_TypeError,
3685 "%U() missing %i required %s argument%s: %U",
3686 co->co_name,
3687 len,
3688 kind,
3689 len == 1 ? "" : "s",
3690 name_str);
3691 Py_DECREF(name_str);
3692}
3693
3694static void
3695missing_arguments(PyCodeObject *co, int missing, int defcount,
3696 PyObject **fastlocals)
3697{
3698 int i, j = 0;
3699 int start, end;
3700 int positional = defcount != -1;
3701 const char *kind = positional ? "positional" : "keyword-only";
3702 PyObject *missing_names;
3703
3704 /* Compute the names of the arguments that are missing. */
3705 missing_names = PyList_New(missing);
3706 if (missing_names == NULL)
3707 return;
3708 if (positional) {
3709 start = 0;
3710 end = co->co_argcount - defcount;
3711 }
3712 else {
3713 start = co->co_argcount;
3714 end = start + co->co_kwonlyargcount;
3715 }
3716 for (i = start; i < end; i++) {
3717 if (GETLOCAL(i) == NULL) {
3718 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3719 PyObject *name = PyObject_Repr(raw);
3720 if (name == NULL) {
3721 Py_DECREF(missing_names);
3722 return;
3723 }
3724 PyList_SET_ITEM(missing_names, j++, name);
3725 }
3726 }
3727 assert(j == missing);
3728 format_missing(kind, co, missing_names);
3729 Py_DECREF(missing_names);
3730}
3731
3732static void
3733too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003734{
3735 int plural;
3736 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003737 int i;
3738 PyObject *sig, *kwonly_sig;
3739
Benjamin Petersone109c702011-06-24 09:37:26 -05003740 assert((co->co_flags & CO_VARARGS) == 0);
3741 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003742 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003743 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003744 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003745 if (defcount) {
3746 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 plural = 1;
3748 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3749 }
3750 else {
3751 plural = co->co_argcount != 1;
3752 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3753 }
3754 if (sig == NULL)
3755 return;
3756 if (kwonly_given) {
3757 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3758 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3759 kwonly_given != 1 ? "s" : "");
3760 if (kwonly_sig == NULL) {
3761 Py_DECREF(sig);
3762 return;
3763 }
3764 }
3765 else {
3766 /* This will not fail. */
3767 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003768 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003769 }
3770 PyErr_Format(PyExc_TypeError,
3771 "%U() takes %U positional argument%s but %d%U %s given",
3772 co->co_name,
3773 sig,
3774 plural ? "s" : "",
3775 given,
3776 kwonly_sig,
3777 given == 1 && !kwonly_given ? "was" : "were");
3778 Py_DECREF(sig);
3779 Py_DECREF(kwonly_sig);
3780}
3781
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003782
Guido van Rossumc2e20742006-02-27 22:32:47 +00003783/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003784 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003785 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003786
Victor Stinner40ee3012014-06-16 15:59:28 +02003787static PyObject *
3788_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003790 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3791 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003792{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003793 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003794 PyFrameObject *f;
3795 PyObject *retval = NULL;
3796 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003797 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003799 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3800 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003801 PyObject *kwdict;
3802
3803 assert((kwcount == 0) || (kws != NULL));
Tim Peters5ca576e2001-06-18 22:08:13 +00003804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 if (globals == NULL) {
3806 PyErr_SetString(PyExc_SystemError,
3807 "PyEval_EvalCodeEx: NULL globals");
3808 return NULL;
3809 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003810
Victor Stinnerc7020012016-08-16 23:40:29 +02003811 /* Create the frame */
3812 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 assert(tstate != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 f = PyFrame_New(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003815 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003817 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 fastlocals = f->f_localsplus;
3819 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003820
Victor Stinnerc7020012016-08-16 23:40:29 +02003821 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003822 if (co->co_flags & CO_VARKEYWORDS) {
3823 kwdict = PyDict_New();
3824 if (kwdict == NULL)
3825 goto fail;
3826 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003827 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003828 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003829 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003830 SETLOCAL(i, kwdict);
3831 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003832 else {
3833 kwdict = NULL;
3834 }
3835
3836 /* Copy positional arguments into local variables */
3837 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003838 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003839 }
3840 else {
3841 n = argcount;
3842 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003843 for (i = 0; i < n; i++) {
3844 x = args[i];
3845 Py_INCREF(x);
3846 SETLOCAL(i, x);
3847 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003848
3849 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 if (co->co_flags & CO_VARARGS) {
3851 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003852 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003853 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003854 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 SETLOCAL(total_args, u);
3856 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003857 x = args[i];
3858 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003859 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003861 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003862
3863 /* Handle keyword arguments (passed as an array of (key, value)) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003864 for (i = 0; i < kwcount; i++) {
3865 PyObject **co_varnames;
3866 PyObject *keyword = kws[2*i];
3867 PyObject *value = kws[2*i + 1];
Victor Stinner17061a92016-08-16 23:39:42 +02003868 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003869
Benjamin Petersonb204a422011-06-05 22:04:07 -05003870 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3871 PyErr_Format(PyExc_TypeError,
3872 "%U() keywords must be strings",
3873 co->co_name);
3874 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003876
Benjamin Petersonb204a422011-06-05 22:04:07 -05003877 /* Speed hack: do raw pointer compares. As names are
3878 normally interned this should almost always hit. */
3879 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3880 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003881 PyObject *name = co_varnames[j];
3882 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003883 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003884 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003885 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003886
Benjamin Petersonb204a422011-06-05 22:04:07 -05003887 /* Slow fallback, just in case */
3888 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003889 PyObject *name = co_varnames[j];
3890 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3891 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003892 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003893 }
3894 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003896 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003897 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003898
Benjamin Petersonb204a422011-06-05 22:04:07 -05003899 if (j >= total_args && kwdict == NULL) {
3900 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003901 "%U() got an unexpected keyword argument '%S'",
3902 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003903 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003905
Christian Heimes0bd447f2013-07-20 14:48:10 +02003906 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3907 goto fail;
3908 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003909 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003910
Benjamin Petersonb204a422011-06-05 22:04:07 -05003911 kw_found:
3912 if (GETLOCAL(j) != NULL) {
3913 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003914 "%U() got multiple values for argument '%S'",
3915 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003918 Py_INCREF(value);
3919 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003921
3922 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003923 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003924 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 goto fail;
3926 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003927
3928 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003929 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003930 Py_ssize_t m = co->co_argcount - defcount;
3931 Py_ssize_t missing = 0;
3932 for (i = argcount; i < m; i++) {
3933 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003934 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003935 }
3936 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003937 if (missing) {
3938 missing_arguments(co, missing, defcount, fastlocals);
3939 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003940 }
3941 if (n > m)
3942 i = n - m;
3943 else
3944 i = 0;
3945 for (; i < defcount; i++) {
3946 if (GETLOCAL(m+i) == NULL) {
3947 PyObject *def = defs[i];
3948 Py_INCREF(def);
3949 SETLOCAL(m+i, def);
3950 }
3951 }
3952 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003953
3954 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003955 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003956 Py_ssize_t 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 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003997
3998 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003999 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4000 PyObject *o = PyTuple_GET_ITEM(closure, i);
4001 Py_INCREF(o);
4002 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004004
Victor Stinnerc7020012016-08-16 23:40:29 +02004005 /* Handle generator/coroutine */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004006 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004007 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004008 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004009 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004010
4011 if (is_coro && tstate->in_coroutine_wrapper) {
4012 assert(coro_wrapper != NULL);
4013 PyErr_Format(PyExc_RuntimeError,
4014 "coroutine wrapper %.200R attempted "
4015 "to recursively wrap %.200R",
4016 coro_wrapper,
4017 co);
4018 goto fail;
4019 }
Yury Selivanov75445082015-05-11 22:57:16 -04004020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 /* Don't need to keep the reference to f_back, it will be set
4022 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004023 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 /* Create a new generator that owns the ready to run frame
4028 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004029 if (is_coro) {
4030 gen = PyCoro_New(f, name, qualname);
4031 } else {
4032 gen = PyGen_NewWithQualName(f, name, qualname);
4033 }
Yury Selivanov75445082015-05-11 22:57:16 -04004034 if (gen == NULL)
4035 return NULL;
4036
Yury Selivanov94c22632015-06-04 10:16:51 -04004037 if (is_coro && coro_wrapper != NULL) {
4038 PyObject *wrapped;
4039 tstate->in_coroutine_wrapper = 1;
4040 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4041 tstate->in_coroutine_wrapper = 0;
4042 return wrapped;
4043 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004044
Yury Selivanov75445082015-05-11 22:57:16 -04004045 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004049
Thomas Woutersce272b62007-09-19 21:19:28 +00004050fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 /* decref'ing the frame can cause __del__ methods to get invoked,
4053 which can call back into Python. While we're done with the
4054 current Python frame (f), the associated C stack is still in use,
4055 so recursion_depth must be boosted for the duration.
4056 */
4057 assert(tstate != NULL);
4058 ++tstate->recursion_depth;
4059 Py_DECREF(f);
4060 --tstate->recursion_depth;
4061 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004062}
4063
Victor Stinner40ee3012014-06-16 15:59:28 +02004064PyObject *
4065PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4066 PyObject **args, int argcount, PyObject **kws, int kwcount,
4067 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4068{
4069 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004070 args, argcount,
4071 kws, kwcount,
4072 defs, defcount,
4073 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004074 NULL, NULL);
4075}
Tim Peters5ca576e2001-06-18 22:08:13 +00004076
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004077static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004078special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004079{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004081 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004083 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 return NULL;
4085 }
4086 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004087}
4088
4089
Benjamin Peterson87880242011-07-03 16:48:31 -05004090/* These 3 functions deal with the exception state of generators. */
4091
4092static void
4093save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4094{
4095 PyObject *type, *value, *traceback;
4096 Py_XINCREF(tstate->exc_type);
4097 Py_XINCREF(tstate->exc_value);
4098 Py_XINCREF(tstate->exc_traceback);
4099 type = f->f_exc_type;
4100 value = f->f_exc_value;
4101 traceback = f->f_exc_traceback;
4102 f->f_exc_type = tstate->exc_type;
4103 f->f_exc_value = tstate->exc_value;
4104 f->f_exc_traceback = tstate->exc_traceback;
4105 Py_XDECREF(type);
4106 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004107 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004108}
4109
4110static void
4111swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4112{
4113 PyObject *tmp;
4114 tmp = tstate->exc_type;
4115 tstate->exc_type = f->f_exc_type;
4116 f->f_exc_type = tmp;
4117 tmp = tstate->exc_value;
4118 tstate->exc_value = f->f_exc_value;
4119 f->f_exc_value = tmp;
4120 tmp = tstate->exc_traceback;
4121 tstate->exc_traceback = f->f_exc_traceback;
4122 f->f_exc_traceback = tmp;
4123}
4124
4125static void
4126restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4127{
4128 PyObject *type, *value, *tb;
4129 type = tstate->exc_type;
4130 value = tstate->exc_value;
4131 tb = tstate->exc_traceback;
4132 tstate->exc_type = f->f_exc_type;
4133 tstate->exc_value = f->f_exc_value;
4134 tstate->exc_traceback = f->f_exc_traceback;
4135 f->f_exc_type = NULL;
4136 f->f_exc_value = NULL;
4137 f->f_exc_traceback = NULL;
4138 Py_XDECREF(type);
4139 Py_XDECREF(value);
4140 Py_XDECREF(tb);
4141}
4142
4143
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004144/* Logic for the raise statement (too complicated for inlining).
4145 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004146static int
Collin Winter828f04a2007-08-31 00:04:24 +00004147do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004148{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 if (exc == NULL) {
4152 /* Reraise */
4153 PyThreadState *tstate = PyThreadState_GET();
4154 PyObject *tb;
4155 type = tstate->exc_type;
4156 value = tstate->exc_value;
4157 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004158 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 PyErr_SetString(PyExc_RuntimeError,
4160 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004161 return 0;
4162 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 Py_XINCREF(type);
4164 Py_XINCREF(value);
4165 Py_XINCREF(tb);
4166 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004167 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 /* We support the following forms of raise:
4171 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004172 raise <instance>
4173 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 if (PyExceptionClass_Check(exc)) {
4176 type = exc;
4177 value = PyObject_CallObject(exc, NULL);
4178 if (value == NULL)
4179 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004180 if (!PyExceptionInstance_Check(value)) {
4181 PyErr_Format(PyExc_TypeError,
4182 "calling %R should have returned an instance of "
4183 "BaseException, not %R",
4184 type, Py_TYPE(value));
4185 goto raise_error;
4186 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 }
4188 else if (PyExceptionInstance_Check(exc)) {
4189 value = exc;
4190 type = PyExceptionInstance_Class(exc);
4191 Py_INCREF(type);
4192 }
4193 else {
4194 /* Not something you can raise. You get an exception
4195 anyway, just not what you specified :-) */
4196 Py_DECREF(exc);
4197 PyErr_SetString(PyExc_TypeError,
4198 "exceptions must derive from BaseException");
4199 goto raise_error;
4200 }
Collin Winter828f04a2007-08-31 00:04:24 +00004201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004202 if (cause) {
4203 PyObject *fixed_cause;
4204 if (PyExceptionClass_Check(cause)) {
4205 fixed_cause = PyObject_CallObject(cause, NULL);
4206 if (fixed_cause == NULL)
4207 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004208 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004210 else if (PyExceptionInstance_Check(cause)) {
4211 fixed_cause = cause;
4212 }
4213 else if (cause == Py_None) {
4214 Py_DECREF(cause);
4215 fixed_cause = NULL;
4216 }
4217 else {
4218 PyErr_SetString(PyExc_TypeError,
4219 "exception causes must derive from "
4220 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004221 goto raise_error;
4222 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004223 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004224 }
Collin Winter828f04a2007-08-31 00:04:24 +00004225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 PyErr_SetObject(type, value);
4227 /* PyErr_SetObject incref's its arguments */
4228 Py_XDECREF(value);
4229 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004230 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004231
4232raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 Py_XDECREF(value);
4234 Py_XDECREF(type);
4235 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004236 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004237}
4238
Tim Petersd6d010b2001-06-21 02:49:55 +00004239/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004240 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004241
Guido van Rossum0368b722007-05-11 16:50:42 +00004242 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4243 with a variable target.
4244*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004245
Barry Warsawe42b18f1997-08-25 22:13:04 +00004246static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004247unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004248{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004249 int i = 0, j = 0;
4250 Py_ssize_t ll = 0;
4251 PyObject *it; /* iter(v) */
4252 PyObject *w;
4253 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 it = PyObject_GetIter(v);
4258 if (it == NULL)
4259 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004261 for (; i < argcnt; i++) {
4262 w = PyIter_Next(it);
4263 if (w == NULL) {
4264 /* Iterator done, via error or exhaustion. */
4265 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004266 if (argcntafter == -1) {
4267 PyErr_Format(PyExc_ValueError,
4268 "not enough values to unpack (expected %d, got %d)",
4269 argcnt, i);
4270 }
4271 else {
4272 PyErr_Format(PyExc_ValueError,
4273 "not enough values to unpack "
4274 "(expected at least %d, got %d)",
4275 argcnt + argcntafter, i);
4276 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 }
4278 goto Error;
4279 }
4280 *--sp = w;
4281 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 if (argcntafter == -1) {
4284 /* We better have exhausted the iterator now. */
4285 w = PyIter_Next(it);
4286 if (w == NULL) {
4287 if (PyErr_Occurred())
4288 goto Error;
4289 Py_DECREF(it);
4290 return 1;
4291 }
4292 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004293 PyErr_Format(PyExc_ValueError,
4294 "too many values to unpack (expected %d)",
4295 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 goto Error;
4297 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 l = PySequence_List(it);
4300 if (l == NULL)
4301 goto Error;
4302 *--sp = l;
4303 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 ll = PyList_GET_SIZE(l);
4306 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004307 PyErr_Format(PyExc_ValueError,
4308 "not enough values to unpack (expected at least %d, got %zd)",
4309 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 goto Error;
4311 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 /* Pop the "after-variable" args off the list. */
4314 for (j = argcntafter; j > 0; j--, i++) {
4315 *--sp = PyList_GET_ITEM(l, ll - j);
4316 }
4317 /* Resize the list. */
4318 Py_SIZE(l) = ll - argcntafter;
4319 Py_DECREF(it);
4320 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004321
Tim Petersd6d010b2001-06-21 02:49:55 +00004322Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 for (; i > 0; i--, sp++)
4324 Py_DECREF(*sp);
4325 Py_XDECREF(it);
4326 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004327}
4328
4329
Guido van Rossum96a42c81992-01-12 02:29:51 +00004330#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004331static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004332prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 printf("%s ", str);
4335 if (PyObject_Print(v, stdout, 0) != 0)
4336 PyErr_Clear(); /* Don't know what else to do */
4337 printf("\n");
4338 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004339}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004340#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004341
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004342static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004343call_exc_trace(Py_tracefunc func, PyObject *self,
4344 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004345{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004346 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004348 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 if (value == NULL) {
4350 value = Py_None;
4351 Py_INCREF(value);
4352 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004353 PyErr_NormalizeException(&type, &value, &orig_traceback);
4354 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 arg = PyTuple_Pack(3, type, value, traceback);
4356 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004357 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 return;
4359 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004360 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 Py_DECREF(arg);
4362 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004363 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 else {
4365 Py_XDECREF(type);
4366 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004367 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004368 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004369}
4370
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004371static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004372call_trace_protected(Py_tracefunc func, PyObject *obj,
4373 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 PyObject *type, *value, *traceback;
4377 int err;
4378 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004379 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004380 if (err == 0)
4381 {
4382 PyErr_Restore(type, value, traceback);
4383 return 0;
4384 }
4385 else {
4386 Py_XDECREF(type);
4387 Py_XDECREF(value);
4388 Py_XDECREF(traceback);
4389 return -1;
4390 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004391}
4392
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004393static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004394call_trace(Py_tracefunc func, PyObject *obj,
4395 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 int result;
4399 if (tstate->tracing)
4400 return 0;
4401 tstate->tracing++;
4402 tstate->use_tracing = 0;
4403 result = func(obj, frame, what, arg);
4404 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4405 || (tstate->c_profilefunc != NULL));
4406 tstate->tracing--;
4407 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004408}
4409
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004410PyObject *
4411_PyEval_CallTracing(PyObject *func, PyObject *args)
4412{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004413 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 int save_tracing = tstate->tracing;
4415 int save_use_tracing = tstate->use_tracing;
4416 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 tstate->tracing = 0;
4419 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4420 || (tstate->c_profilefunc != NULL));
4421 result = PyObject_Call(func, args, NULL);
4422 tstate->tracing = save_tracing;
4423 tstate->use_tracing = save_use_tracing;
4424 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004425}
4426
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004427/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004428static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004429maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004430 PyThreadState *tstate, PyFrameObject *frame,
4431 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 int result = 0;
4434 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 /* If the last instruction executed isn't in the current
4437 instruction window, reset the window.
4438 */
4439 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4440 PyAddrPair bounds;
4441 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4442 &bounds);
4443 *instr_lb = bounds.ap_lower;
4444 *instr_ub = bounds.ap_upper;
4445 }
4446 /* If the last instruction falls at the start of a line or if
4447 it represents a jump backwards, update the frame's line
4448 number and call the trace function. */
4449 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4450 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004451 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 }
4453 *instr_prev = frame->f_lasti;
4454 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004455}
4456
Fred Drake5755ce62001-06-27 19:19:46 +00004457void
4458PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 PyThreadState *tstate = PyThreadState_GET();
4461 PyObject *temp = tstate->c_profileobj;
4462 Py_XINCREF(arg);
4463 tstate->c_profilefunc = NULL;
4464 tstate->c_profileobj = NULL;
4465 /* Must make sure that tracing is not ignored if 'temp' is freed */
4466 tstate->use_tracing = tstate->c_tracefunc != NULL;
4467 Py_XDECREF(temp);
4468 tstate->c_profilefunc = func;
4469 tstate->c_profileobj = arg;
4470 /* Flag that tracing or profiling is turned on */
4471 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004472}
4473
4474void
4475PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 PyThreadState *tstate = PyThreadState_GET();
4478 PyObject *temp = tstate->c_traceobj;
4479 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4480 Py_XINCREF(arg);
4481 tstate->c_tracefunc = NULL;
4482 tstate->c_traceobj = NULL;
4483 /* Must make sure that profiling is not ignored if 'temp' is freed */
4484 tstate->use_tracing = tstate->c_profilefunc != NULL;
4485 Py_XDECREF(temp);
4486 tstate->c_tracefunc = func;
4487 tstate->c_traceobj = arg;
4488 /* Flag that tracing or profiling is turned on */
4489 tstate->use_tracing = ((func != NULL)
4490 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004491}
4492
Yury Selivanov75445082015-05-11 22:57:16 -04004493void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004494_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004495{
4496 PyThreadState *tstate = PyThreadState_GET();
4497
Yury Selivanov75445082015-05-11 22:57:16 -04004498 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004499 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004500}
4501
4502PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004503_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004504{
4505 PyThreadState *tstate = PyThreadState_GET();
4506 return tstate->coroutine_wrapper;
4507}
4508
Guido van Rossumb209a111997-04-29 18:18:01 +00004509PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004510PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 PyFrameObject *current_frame = PyEval_GetFrame();
4513 if (current_frame == NULL)
4514 return PyThreadState_GET()->interp->builtins;
4515 else
4516 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004517}
4518
Guido van Rossumb209a111997-04-29 18:18:01 +00004519PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004520PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004521{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004523 if (current_frame == NULL) {
4524 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004525 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004526 }
4527
4528 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4529 return NULL;
4530
4531 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004532 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004533}
4534
Guido van Rossumb209a111997-04-29 18:18:01 +00004535PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004536PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 PyFrameObject *current_frame = PyEval_GetFrame();
4539 if (current_frame == NULL)
4540 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004541
4542 assert(current_frame->f_globals != NULL);
4543 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004544}
4545
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004546PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004547PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 PyThreadState *tstate = PyThreadState_GET();
4550 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004551}
4552
Guido van Rossum6135a871995-01-09 17:53:26 +00004553int
Tim Peters5ba58662001-07-16 02:29:45 +00004554PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 PyFrameObject *current_frame = PyEval_GetFrame();
4557 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 if (current_frame != NULL) {
4560 const int codeflags = current_frame->f_code->co_flags;
4561 const int compilerflags = codeflags & PyCF_MASK;
4562 if (compilerflags) {
4563 result = 1;
4564 cf->cf_flags |= compilerflags;
4565 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004566#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 if (codeflags & CO_GENERATOR_ALLOWED) {
4568 result = 1;
4569 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4570 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004571#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 }
4573 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004574}
4575
Guido van Rossum3f5da241990-12-20 15:06:42 +00004576
Guido van Rossum681d79a1995-07-18 14:51:37 +00004577/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004578 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004579
Guido van Rossumb209a111997-04-29 18:18:01 +00004580PyObject *
Victor Stinner8a31c822016-08-19 17:12:23 +02004581PyEval_CallObjectWithKeywords(PyObject *func, PyObject *args, PyObject *kwargs)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004584
Victor Stinner59b356d2015-03-16 11:52:32 +01004585#ifdef Py_DEBUG
4586 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4587 set. It raises a new exception if parameters are invalid or if
4588 PyTuple_New() fails, and so the original exception is lost. */
4589 assert(!PyErr_Occurred());
4590#endif
4591
Victor Stinner8a31c822016-08-19 17:12:23 +02004592 if (args == NULL) {
Victor Stinner155ea652016-08-22 23:26:00 +02004593 return _PyObject_FastCallDict(func, NULL, 0, kwargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 }
Victor Stinner155ea652016-08-22 23:26:00 +02004595
4596 if (!PyTuple_Check(args)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004597 PyErr_SetString(PyExc_TypeError,
4598 "argument list must be a tuple");
4599 return NULL;
4600 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00004601
Victor Stinner8a31c822016-08-19 17:12:23 +02004602 if (kwargs != NULL && !PyDict_Check(kwargs)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 PyErr_SetString(PyExc_TypeError,
4604 "keyword list must be a dictionary");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 return NULL;
4606 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004607
Victor Stinner155ea652016-08-22 23:26:00 +02004608 Py_INCREF(args);
Victor Stinner8a31c822016-08-19 17:12:23 +02004609 result = PyObject_Call(func, args, kwargs);
4610 Py_DECREF(args);
Victor Stinnerace47d72013-07-18 01:41:08 +02004611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004612 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004613}
4614
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004615const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004616PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 if (PyMethod_Check(func))
4619 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4620 else if (PyFunction_Check(func))
4621 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4622 else if (PyCFunction_Check(func))
4623 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4624 else
4625 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004626}
4627
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004628const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004629PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 if (PyMethod_Check(func))
4632 return "()";
4633 else if (PyFunction_Check(func))
4634 return "()";
4635 else if (PyCFunction_Check(func))
4636 return "()";
4637 else
4638 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004639}
4640
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004641static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004642err_args(PyObject *func, int flags, int nargs)
4643{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 if (flags & METH_NOARGS)
4645 PyErr_Format(PyExc_TypeError,
4646 "%.200s() takes no arguments (%d given)",
4647 ((PyCFunctionObject *)func)->m_ml->ml_name,
4648 nargs);
4649 else
4650 PyErr_Format(PyExc_TypeError,
4651 "%.200s() takes exactly one argument (%d given)",
4652 ((PyCFunctionObject *)func)->m_ml->ml_name,
4653 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004654}
4655
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004656#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004657if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004658 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4659 tstate, tstate->frame, \
4660 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 x = NULL; \
4662 } \
4663 else { \
4664 x = call; \
4665 if (tstate->c_profilefunc != NULL) { \
4666 if (x == NULL) { \
4667 call_trace_protected(tstate->c_profilefunc, \
4668 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004669 tstate, tstate->frame, \
4670 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 /* XXX should pass (type, value, tb) */ \
4672 } else { \
4673 if (call_trace(tstate->c_profilefunc, \
4674 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004675 tstate, tstate->frame, \
4676 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 Py_DECREF(x); \
4678 x = NULL; \
4679 } \
4680 } \
4681 } \
4682 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004683} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 x = call; \
4685 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004686
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004687static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004688call_function(PyObject ***pp_stack, int oparg
4689#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004691#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004692 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 int na = oparg & 0xff;
4695 int nk = (oparg>>8) & 0xff;
4696 int n = na + 2 * nk;
4697 PyObject **pfunc = (*pp_stack) - n - 1;
4698 PyObject *func = *pfunc;
4699 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 /* Always dispatch PyCFunction first, because these are
4702 presumed to be the most frequent callable object.
4703 */
4704 if (PyCFunction_Check(func) && nk == 0) {
4705 int flags = PyCFunction_GET_FLAGS(func);
4706 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 PCALL(PCALL_CFUNCTION);
4709 if (flags & (METH_NOARGS | METH_O)) {
4710 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4711 PyObject *self = PyCFunction_GET_SELF(func);
4712 if (flags & METH_NOARGS && na == 0) {
4713 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004714
Victor Stinnerefde1462015-03-21 15:04:43 +01004715 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 }
4717 else if (flags & METH_O && na == 1) {
4718 PyObject *arg = EXT_POP(*pp_stack);
4719 C_TRACE(x, (*meth)(self,arg));
4720 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004721
Victor Stinnerefde1462015-03-21 15:04:43 +01004722 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004723 }
4724 else {
4725 err_args(func, flags, na);
4726 x = NULL;
4727 }
4728 }
4729 else {
4730 PyObject *callargs;
4731 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004732 if (callargs != NULL) {
4733 READ_TIMESTAMP(*pintr0);
4734 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4735 READ_TIMESTAMP(*pintr1);
4736 Py_XDECREF(callargs);
4737 }
4738 else {
4739 x = NULL;
4740 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004741 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004742 }
4743 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4745 /* optimize access to bound methods */
4746 PyObject *self = PyMethod_GET_SELF(func);
4747 PCALL(PCALL_METHOD);
4748 PCALL(PCALL_BOUND_METHOD);
4749 Py_INCREF(self);
4750 func = PyMethod_GET_FUNCTION(func);
4751 Py_INCREF(func);
Serhiy Storchaka57a01d32016-04-10 18:05:40 +03004752 Py_SETREF(*pfunc, self);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004753 na++;
4754 n++;
4755 } else
4756 Py_INCREF(func);
4757 READ_TIMESTAMP(*pintr0);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004758 if (PyFunction_Check(func)) {
4759 x = fast_function(func, (*pp_stack) - n, n, na, nk);
4760 }
4761 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 x = do_call(func, pp_stack, na, nk);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004763 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 READ_TIMESTAMP(*pintr1);
4765 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004766
4767 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004768 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770 /* Clear the stack of the function object. Also removes
4771 the arguments in case they weren't consumed already
4772 (fast_function() and err_args() leave them on the stack).
4773 */
4774 while ((*pp_stack) > pfunc) {
4775 w = EXT_POP(*pp_stack);
4776 Py_DECREF(w);
4777 PCALL(PCALL_POP);
4778 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004779
Victor Stinner4a7cc882015-03-06 23:35:27 +01004780 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004781 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004782}
4783
Jeremy Hylton192690e2002-08-16 18:36:11 +00004784/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004785 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004786 For the simplest case -- a function that takes only positional
4787 arguments and is called with only positional arguments -- it
4788 inlines the most primitive frame setup code from
4789 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4790 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004791*/
4792
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004793static PyObject*
Victor Stinner2eedc112016-08-22 12:29:42 +02004794_PyFunction_FastCallNoKw(PyCodeObject *co, PyObject **args, Py_ssize_t na,
4795 PyObject *globals)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004796{
4797 PyFrameObject *f;
4798 PyThreadState *tstate = PyThreadState_GET();
4799 PyObject **fastlocals;
4800 Py_ssize_t i;
4801 PyObject *result;
4802
4803 PCALL(PCALL_FASTER_FUNCTION);
4804 assert(globals != NULL);
4805 /* XXX Perhaps we should create a specialized
4806 PyFrame_New() that doesn't take locals, but does
4807 take builtins without sanity checking them.
4808 */
4809 assert(tstate != NULL);
4810 f = PyFrame_New(tstate, co, globals, NULL);
4811 if (f == NULL) {
4812 return NULL;
4813 }
4814
4815 fastlocals = f->f_localsplus;
4816
4817 for (i = 0; i < na; i++) {
4818 Py_INCREF(*args);
4819 fastlocals[i] = *args++;
4820 }
4821 result = PyEval_EvalFrameEx(f,0);
4822
4823 ++tstate->recursion_depth;
4824 Py_DECREF(f);
4825 --tstate->recursion_depth;
4826
4827 return result;
4828}
4829
Victor Stinner2eedc112016-08-22 12:29:42 +02004830/* Similar to _PyFunction_FastCall() but keywords are passed a (key, value)
4831 pairs in stack */
Jeremy Hylton52820442001-01-03 23:52:36 +00004832static PyObject *
Victor Stinner2eedc112016-08-22 12:29:42 +02004833fast_function(PyObject *func, PyObject **stack, int n, int nargs, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004834{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004835 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4836 PyObject *globals = PyFunction_GET_GLOBALS(func);
4837 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004838 PyObject *kwdefs, *closure, *name, *qualname;
4839 PyObject **d;
4840 int nd;
Jeremy Hylton52820442001-01-03 23:52:36 +00004841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 PCALL(PCALL_FUNCTION);
4843 PCALL(PCALL_FAST_FUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004844
Victor Stinner2eedc112016-08-22 12:29:42 +02004845 if (co->co_kwonlyargcount == 0 && nk == 0 &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004846 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4847 {
Victor Stinner2eedc112016-08-22 12:29:42 +02004848 if (argdefs == NULL && co->co_argcount == nargs) {
4849 return _PyFunction_FastCallNoKw(co, stack, nargs, globals);
4850 }
4851 else if (nargs == 0 && argdefs != NULL
4852 && co->co_argcount == Py_SIZE(argdefs)) {
4853 /* function called with no arguments, but all parameters have
4854 a default value: use default values as arguments .*/
4855 stack = &PyTuple_GET_ITEM(argdefs, 0);
4856 return _PyFunction_FastCallNoKw(co, stack, Py_SIZE(argdefs),
4857 globals);
4858 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004859 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004860
4861 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4862 closure = PyFunction_GET_CLOSURE(func);
4863 name = ((PyFunctionObject *)func) -> func_name;
4864 qualname = ((PyFunctionObject *)func) -> func_qualname;
4865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004866 if (argdefs != NULL) {
4867 d = &PyTuple_GET_ITEM(argdefs, 0);
4868 nd = Py_SIZE(argdefs);
4869 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004870 else {
4871 d = NULL;
4872 nd = 0;
4873 }
4874 return _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
Victor Stinner2eedc112016-08-22 12:29:42 +02004875 stack, nargs,
4876 stack + nargs, nk,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004877 d, nd, kwdefs,
4878 closure, name, qualname);
4879}
4880
4881PyObject *
Victor Stinnerb9009392016-08-22 23:15:44 +02004882_PyFunction_FastCallDict(PyObject *func, PyObject **args, int nargs,
4883 PyObject *kwargs)
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004884{
4885 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4886 PyObject *globals = PyFunction_GET_GLOBALS(func);
4887 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4888 PyObject *kwdefs, *closure, *name, *qualname;
Victor Stinnerb9009392016-08-22 23:15:44 +02004889 PyObject *kwtuple, **k;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004890 PyObject **d;
4891 int nd;
Victor Stinnerb9009392016-08-22 23:15:44 +02004892 Py_ssize_t nk;
4893 PyObject *result;
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004894
4895 PCALL(PCALL_FUNCTION);
4896 PCALL(PCALL_FAST_FUNCTION);
4897
Victor Stinnerb9009392016-08-22 23:15:44 +02004898 assert(kwargs == NULL || PyDict_Check(kwargs));
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004899
Victor Stinnerb9009392016-08-22 23:15:44 +02004900 if (co->co_kwonlyargcount == 0 &&
4901 (kwargs == NULL || PyDict_Size(kwargs) == 0) &&
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004902 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE))
4903 {
Victor Stinnerb9009392016-08-22 23:15:44 +02004904 /* Fast paths */
Victor Stinner2eedc112016-08-22 12:29:42 +02004905 if (argdefs == NULL && co->co_argcount == nargs) {
4906 return _PyFunction_FastCallNoKw(co, args, nargs, globals);
4907 }
4908 else if (nargs == 0 && argdefs != NULL
4909 && co->co_argcount == Py_SIZE(argdefs)) {
4910 /* function called with no arguments, but all parameters have
4911 a default value: use default values as arguments .*/
4912 args = &PyTuple_GET_ITEM(argdefs, 0);
4913 return _PyFunction_FastCallNoKw(co, args, Py_SIZE(argdefs),
4914 globals);
4915 }
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004916 }
4917
Victor Stinnerb9009392016-08-22 23:15:44 +02004918 if (kwargs != NULL) {
4919 Py_ssize_t pos, i;
4920 nk = PyDict_Size(kwargs);
4921
4922 kwtuple = PyTuple_New(2 * nk);
4923 if (kwtuple == NULL) {
4924 return NULL;
4925 }
4926
4927 k = &PyTuple_GET_ITEM(kwtuple, 0);
4928 pos = i = 0;
4929 while (PyDict_Next(kwargs, &pos, &k[i], &k[i+1])) {
4930 Py_INCREF(k[i]);
4931 Py_INCREF(k[i+1]);
4932 i += 2;
4933 }
4934 nk = i / 2;
4935 }
4936 else {
4937 kwtuple = NULL;
4938 k = NULL;
4939 nk = 0;
4940 }
4941
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004942 kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4943 closure = PyFunction_GET_CLOSURE(func);
4944 name = ((PyFunctionObject *)func) -> func_name;
4945 qualname = ((PyFunctionObject *)func) -> func_qualname;
4946
4947 if (argdefs != NULL) {
4948 d = &PyTuple_GET_ITEM(argdefs, 0);
4949 nd = Py_SIZE(argdefs);
4950 }
4951 else {
4952 d = NULL;
4953 nd = 0;
4954 }
Victor Stinnerb9009392016-08-22 23:15:44 +02004955
4956 result = _PyEval_EvalCodeWithName((PyObject*)co, globals, (PyObject *)NULL,
4957 args, nargs,
4958 k, (int)nk,
4959 d, nd, kwdefs,
4960 closure, name, qualname);
4961 Py_XDECREF(kwtuple);
4962 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004963}
4964
4965static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004966update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4967 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004969 PyObject *kwdict = NULL;
4970 if (orig_kwdict == NULL)
4971 kwdict = PyDict_New();
4972 else {
4973 kwdict = PyDict_Copy(orig_kwdict);
4974 Py_DECREF(orig_kwdict);
4975 }
4976 if (kwdict == NULL)
4977 return NULL;
4978 while (--nk >= 0) {
4979 int err;
4980 PyObject *value = EXT_POP(*pp_stack);
4981 PyObject *key = EXT_POP(*pp_stack);
4982 if (PyDict_GetItem(kwdict, key) != NULL) {
4983 PyErr_Format(PyExc_TypeError,
4984 "%.200s%s got multiple values "
4985 "for keyword argument '%U'",
4986 PyEval_GetFuncName(func),
4987 PyEval_GetFuncDesc(func),
4988 key);
4989 Py_DECREF(key);
4990 Py_DECREF(value);
4991 Py_DECREF(kwdict);
4992 return NULL;
4993 }
4994 err = PyDict_SetItem(kwdict, key, value);
4995 Py_DECREF(key);
4996 Py_DECREF(value);
4997 if (err) {
4998 Py_DECREF(kwdict);
4999 return NULL;
5000 }
5001 }
5002 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00005003}
5004
5005static PyObject *
5006update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00005008{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005009 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00005010
Serhiy Storchaka79d6e8d2016-04-19 23:37:17 +03005011 if (!nstack) {
5012 if (!stararg) {
5013 /* There are no positional arguments on the stack and there is no
5014 sequence to be unpacked. */
5015 return PyTuple_New(0);
5016 }
5017 if (PyTuple_CheckExact(stararg)) {
5018 /* No arguments are passed on the stack and the sequence is not a
5019 tuple subclass so we can just pass the stararg tuple directly
5020 to the function. */
5021 Py_INCREF(stararg);
5022 return stararg;
5023 }
5024 }
5025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005026 callargs = PyTuple_New(nstack + nstar);
5027 if (callargs == NULL) {
5028 return NULL;
5029 }
5030 if (nstar) {
5031 int i;
5032 for (i = 0; i < nstar; i++) {
5033 PyObject *a = PyTuple_GET_ITEM(stararg, i);
5034 Py_INCREF(a);
5035 PyTuple_SET_ITEM(callargs, nstack + i, a);
5036 }
5037 }
5038 while (--nstack >= 0) {
5039 w = EXT_POP(*pp_stack);
5040 PyTuple_SET_ITEM(callargs, nstack, w);
5041 }
5042 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00005043}
5044
5045static PyObject *
5046load_args(PyObject ***pp_stack, int na)
5047{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005048 PyObject *args = PyTuple_New(na);
5049 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00005050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005051 if (args == NULL)
5052 return NULL;
5053 while (--na >= 0) {
5054 w = EXT_POP(*pp_stack);
5055 PyTuple_SET_ITEM(args, na, w);
5056 }
5057 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00005058}
5059
5060static PyObject *
5061do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
5062{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 PyObject *callargs = NULL;
5064 PyObject *kwdict = NULL;
5065 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00005066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005067 if (nk > 0) {
5068 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
5069 if (kwdict == NULL)
5070 goto call_fail;
5071 }
5072 callargs = load_args(pp_stack, na);
5073 if (callargs == NULL)
5074 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005075#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005076 /* At this point, we have to look at the type of func to
5077 update the call stats properly. Do it here so as to avoid
5078 exposing the call stats machinery outside ceval.c
5079 */
5080 if (PyFunction_Check(func))
5081 PCALL(PCALL_FUNCTION);
5082 else if (PyMethod_Check(func))
5083 PCALL(PCALL_METHOD);
5084 else if (PyType_Check(func))
5085 PCALL(PCALL_TYPE);
5086 else if (PyCFunction_Check(func))
5087 PCALL(PCALL_CFUNCTION);
5088 else
5089 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005090#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005091 if (PyCFunction_Check(func)) {
5092 PyThreadState *tstate = PyThreadState_GET();
5093 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5094 }
5095 else
5096 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00005097call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005098 Py_XDECREF(callargs);
5099 Py_XDECREF(kwdict);
5100 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005101}
5102
5103static PyObject *
5104ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
5105{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 int nstar = 0;
5107 PyObject *callargs = NULL;
5108 PyObject *stararg = NULL;
5109 PyObject *kwdict = NULL;
5110 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00005111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005112 if (flags & CALL_FLAG_KW) {
5113 kwdict = EXT_POP(*pp_stack);
Serhiy Storchakace412872016-05-08 23:36:44 +03005114 if (!PyDict_CheckExact(kwdict)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005115 PyObject *d;
5116 d = PyDict_New();
5117 if (d == NULL)
5118 goto ext_call_fail;
5119 if (PyDict_Update(d, kwdict) != 0) {
5120 Py_DECREF(d);
5121 /* PyDict_Update raises attribute
5122 * error (percolated from an attempt
5123 * to get 'keys' attribute) instead of
5124 * a type error if its second argument
5125 * is not a mapping.
5126 */
5127 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
5128 PyErr_Format(PyExc_TypeError,
5129 "%.200s%.200s argument after ** "
5130 "must be a mapping, not %.200s",
5131 PyEval_GetFuncName(func),
5132 PyEval_GetFuncDesc(func),
5133 kwdict->ob_type->tp_name);
5134 }
5135 goto ext_call_fail;
5136 }
5137 Py_DECREF(kwdict);
5138 kwdict = d;
5139 }
5140 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005141 if (nk > 0) {
5142 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
5143 if (kwdict == NULL)
5144 goto ext_call_fail;
5145 }
5146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005147 if (flags & CALL_FLAG_VAR) {
5148 stararg = EXT_POP(*pp_stack);
5149 if (!PyTuple_Check(stararg)) {
5150 PyObject *t = NULL;
Martin Panterb5944222016-01-31 06:30:56 +00005151 if (Py_TYPE(stararg)->tp_iter == NULL &&
5152 !PySequence_Check(stararg)) {
5153 PyErr_Format(PyExc_TypeError,
5154 "%.200s%.200s argument after * "
5155 "must be an iterable, not %.200s",
5156 PyEval_GetFuncName(func),
5157 PyEval_GetFuncDesc(func),
5158 stararg->ob_type->tp_name);
5159 goto ext_call_fail;
5160 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005161 t = PySequence_Tuple(stararg);
5162 if (t == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005163 goto ext_call_fail;
5164 }
5165 Py_DECREF(stararg);
5166 stararg = t;
5167 }
5168 nstar = PyTuple_GET_SIZE(stararg);
5169 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005170 callargs = update_star_args(na, nstar, stararg, pp_stack);
5171 if (callargs == NULL)
5172 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00005173#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005174 /* At this point, we have to look at the type of func to
5175 update the call stats properly. Do it here so as to avoid
5176 exposing the call stats machinery outside ceval.c
5177 */
5178 if (PyFunction_Check(func))
5179 PCALL(PCALL_FUNCTION);
5180 else if (PyMethod_Check(func))
5181 PCALL(PCALL_METHOD);
5182 else if (PyType_Check(func))
5183 PCALL(PCALL_TYPE);
5184 else if (PyCFunction_Check(func))
5185 PCALL(PCALL_CFUNCTION);
5186 else
5187 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00005188#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 if (PyCFunction_Check(func)) {
5190 PyThreadState *tstate = PyThreadState_GET();
5191 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
5192 }
5193 else
5194 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00005195ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005196 Py_XDECREF(callargs);
5197 Py_XDECREF(kwdict);
5198 Py_XDECREF(stararg);
5199 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00005200}
5201
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005202/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005203 nb_index slot defined, and store in *pi.
5204 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
5205 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 +00005206 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005207*/
Tim Petersb5196382001-12-16 19:44:20 +00005208/* Note: If v is NULL, return success without storing into *pi. This
5209 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
5210 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00005211*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005212int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005213_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005215 if (v != NULL) {
5216 Py_ssize_t x;
5217 if (PyIndex_Check(v)) {
5218 x = PyNumber_AsSsize_t(v, NULL);
5219 if (x == -1 && PyErr_Occurred())
5220 return 0;
5221 }
5222 else {
5223 PyErr_SetString(PyExc_TypeError,
5224 "slice indices must be integers or "
5225 "None or have an __index__ method");
5226 return 0;
5227 }
5228 *pi = x;
5229 }
5230 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005231}
5232
Guido van Rossum486364b2007-06-30 05:01:58 +00005233#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005235
Guido van Rossumb209a111997-04-29 18:18:01 +00005236static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005237cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005238{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005239 int res = 0;
5240 switch (op) {
5241 case PyCmp_IS:
5242 res = (v == w);
5243 break;
5244 case PyCmp_IS_NOT:
5245 res = (v != w);
5246 break;
5247 case PyCmp_IN:
5248 res = PySequence_Contains(w, v);
5249 if (res < 0)
5250 return NULL;
5251 break;
5252 case PyCmp_NOT_IN:
5253 res = PySequence_Contains(w, v);
5254 if (res < 0)
5255 return NULL;
5256 res = !res;
5257 break;
5258 case PyCmp_EXC_MATCH:
5259 if (PyTuple_Check(w)) {
5260 Py_ssize_t i, length;
5261 length = PyTuple_Size(w);
5262 for (i = 0; i < length; i += 1) {
5263 PyObject *exc = PyTuple_GET_ITEM(w, i);
5264 if (!PyExceptionClass_Check(exc)) {
5265 PyErr_SetString(PyExc_TypeError,
5266 CANNOT_CATCH_MSG);
5267 return NULL;
5268 }
5269 }
5270 }
5271 else {
5272 if (!PyExceptionClass_Check(w)) {
5273 PyErr_SetString(PyExc_TypeError,
5274 CANNOT_CATCH_MSG);
5275 return NULL;
5276 }
5277 }
5278 res = PyErr_GivenExceptionMatches(v, w);
5279 break;
5280 default:
5281 return PyObject_RichCompare(v, w, op);
5282 }
5283 v = res ? Py_True : Py_False;
5284 Py_INCREF(v);
5285 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005286}
5287
Thomas Wouters52152252000-08-17 22:55:00 +00005288static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005289import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
5290{
5291 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005292 PyObject *import_func, *res;
5293 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005294
5295 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
5296 if (import_func == NULL) {
5297 PyErr_SetString(PyExc_ImportError, "__import__ not found");
5298 return NULL;
5299 }
5300
5301 /* Fast path for not overloaded __import__. */
5302 if (import_func == PyThreadState_GET()->interp->import_func) {
5303 int ilevel = _PyLong_AsInt(level);
5304 if (ilevel == -1 && PyErr_Occurred()) {
5305 return NULL;
5306 }
5307 res = PyImport_ImportModuleLevelObject(
5308 name,
5309 f->f_globals,
5310 f->f_locals == NULL ? Py_None : f->f_locals,
5311 fromlist,
5312 ilevel);
5313 return res;
5314 }
5315
5316 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005317
5318 stack[0] = name;
5319 stack[1] = f->f_globals;
5320 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5321 stack[3] = fromlist;
5322 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005323 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005324 Py_DECREF(import_func);
5325 return res;
5326}
5327
5328static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00005329import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005330{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005331 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005332 _Py_IDENTIFIER(__name__);
5333 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005335 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005336 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5337 return x;
5338 /* Issue #17636: in case this failed because of a circular relative
5339 import, try to fallback on reading the module directly from
5340 sys.modules. */
5341 PyErr_Clear();
5342 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005343 if (pkgname == NULL) {
5344 goto error;
5345 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005346 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5347 Py_DECREF(pkgname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005348 if (fullmodname == NULL) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005349 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005350 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005351 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005352 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07005353 if (x == NULL) {
5354 goto error;
5355 }
5356 Py_INCREF(x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005357 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005358 error:
5359 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
5360 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005361}
Guido van Rossumac7be682001-01-17 15:42:30 +00005362
Thomas Wouters52152252000-08-17 22:55:00 +00005363static int
5364import_all_from(PyObject *locals, PyObject *v)
5365{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005366 _Py_IDENTIFIER(__all__);
5367 _Py_IDENTIFIER(__dict__);
5368 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005369 PyObject *dict, *name, *value;
5370 int skip_leading_underscores = 0;
5371 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005373 if (all == NULL) {
5374 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5375 return -1; /* Unexpected error */
5376 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005377 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005378 if (dict == NULL) {
5379 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5380 return -1;
5381 PyErr_SetString(PyExc_ImportError,
5382 "from-import-* object has no __dict__ and no __all__");
5383 return -1;
5384 }
5385 all = PyMapping_Keys(dict);
5386 Py_DECREF(dict);
5387 if (all == NULL)
5388 return -1;
5389 skip_leading_underscores = 1;
5390 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 for (pos = 0, err = 0; ; pos++) {
5393 name = PySequence_GetItem(all, pos);
5394 if (name == NULL) {
5395 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5396 err = -1;
5397 else
5398 PyErr_Clear();
5399 break;
5400 }
5401 if (skip_leading_underscores &&
5402 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005403 PyUnicode_READY(name) != -1 &&
5404 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 {
5406 Py_DECREF(name);
5407 continue;
5408 }
5409 value = PyObject_GetAttr(v, name);
5410 if (value == NULL)
5411 err = -1;
5412 else if (PyDict_CheckExact(locals))
5413 err = PyDict_SetItem(locals, name, value);
5414 else
5415 err = PyObject_SetItem(locals, name, value);
5416 Py_DECREF(name);
5417 Py_XDECREF(value);
5418 if (err != 0)
5419 break;
5420 }
5421 Py_DECREF(all);
5422 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005423}
5424
Guido van Rossumac7be682001-01-17 15:42:30 +00005425static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005426format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005428 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005430 if (!obj)
5431 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 obj_str = _PyUnicode_AsString(obj);
5434 if (!obj_str)
5435 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005437 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005438}
Guido van Rossum950361c1997-01-24 13:49:28 +00005439
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005440static void
5441format_exc_unbound(PyCodeObject *co, int oparg)
5442{
5443 PyObject *name;
5444 /* Don't stomp existing exception */
5445 if (PyErr_Occurred())
5446 return;
5447 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5448 name = PyTuple_GET_ITEM(co->co_cellvars,
5449 oparg);
5450 format_exc_check_arg(
5451 PyExc_UnboundLocalError,
5452 UNBOUNDLOCAL_ERROR_MSG,
5453 name);
5454 } else {
5455 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5456 PyTuple_GET_SIZE(co->co_cellvars));
5457 format_exc_check_arg(PyExc_NameError,
5458 UNBOUNDFREE_ERROR_MSG, name);
5459 }
5460}
5461
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005462static PyObject *
5463unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005464 PyFrameObject *f, const unsigned short *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005465{
5466 PyObject *res;
5467 if (Py_REFCNT(v) == 2) {
5468 /* In the common case, there are 2 references to the value
5469 * stored in 'variable' when the += is performed: one on the
5470 * value stack (in 'v') and one still stored in the
5471 * 'variable'. We try to delete the variable now to reduce
5472 * the refcnt to 1.
5473 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005474 int opcode, oparg;
5475 NEXTOPARG();
5476 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005477 case STORE_FAST:
5478 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005479 PyObject **fastlocals = f->f_localsplus;
5480 if (GETLOCAL(oparg) == v)
5481 SETLOCAL(oparg, NULL);
5482 break;
5483 }
5484 case STORE_DEREF:
5485 {
5486 PyObject **freevars = (f->f_localsplus +
5487 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005488 PyObject *c = freevars[oparg];
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005489 if (PyCell_GET(c) == v)
5490 PyCell_Set(c, NULL);
5491 break;
5492 }
5493 case STORE_NAME:
5494 {
5495 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005496 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005497 PyObject *locals = f->f_locals;
5498 if (PyDict_CheckExact(locals) &&
5499 PyDict_GetItem(locals, name) == v) {
5500 if (PyDict_DelItem(locals, name) != 0) {
5501 PyErr_Clear();
5502 }
5503 }
5504 break;
5505 }
5506 }
5507 }
5508 res = v;
5509 PyUnicode_Append(&res, w);
5510 return res;
5511}
5512
Guido van Rossum950361c1997-01-24 13:49:28 +00005513#ifdef DYNAMIC_EXECUTION_PROFILE
5514
Skip Montanarof118cb12001-10-15 20:51:38 +00005515static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005516getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005518 int i;
5519 PyObject *l = PyList_New(256);
5520 if (l == NULL) return NULL;
5521 for (i = 0; i < 256; i++) {
5522 PyObject *x = PyLong_FromLong(a[i]);
5523 if (x == NULL) {
5524 Py_DECREF(l);
5525 return NULL;
5526 }
5527 PyList_SetItem(l, i, x);
5528 }
5529 for (i = 0; i < 256; i++)
5530 a[i] = 0;
5531 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005532}
5533
5534PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005535_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005536{
5537#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005538 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005539#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005540 int i;
5541 PyObject *l = PyList_New(257);
5542 if (l == NULL) return NULL;
5543 for (i = 0; i < 257; i++) {
5544 PyObject *x = getarray(dxpairs[i]);
5545 if (x == NULL) {
5546 Py_DECREF(l);
5547 return NULL;
5548 }
5549 PyList_SetItem(l, i, x);
5550 }
5551 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005552#endif
5553}
5554
5555#endif