blob: 96ed6ed4ee2831099977cf98f399ed16d8333844 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040015#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000016#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000019#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020
Guido van Rossumc6004111993-11-05 10:22:19 +000021#include <ctype.h>
22
Thomas Wouters477c8d52006-05-27 19:21:47 +000023#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000024
25#define READ_TIMESTAMP(var)
26
27#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000028
29typedef unsigned long long uint64;
30
Ezio Melotti13925002011-03-16 11:05:33 +020031/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000032 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
33 "__powerpc__" appears to be the correct one for Linux with GCC
34*/
35#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
Michael W. Hudson75eabd22005-01-18 15:56:11 +000037#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000038
39static void
40ppc_getcounter(uint64 *v)
41{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020042 unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000043
44 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 asm volatile ("mftbu %0" : "=r" (tbu) );
46 asm volatile ("mftb %0" : "=r" (tb) );
47 asm volatile ("mftbu %0" : "=r" (tbu2));
48 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 /* The slightly peculiar way of writing the next lines is
51 compiled better by GCC than any other way I tried. */
52 ((long*)(v))[0] = tbu;
53 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000054}
55
Mark Dickinsona25b1312009-10-31 10:18:44 +000056#elif defined(__i386__)
57
58/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000059
Michael W. Hudson75eabd22005-01-18 15:56:11 +000060#define READ_TIMESTAMP(val) \
61 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000062
Mark Dickinsona25b1312009-10-31 10:18:44 +000063#elif defined(__x86_64__)
64
65/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
66 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
67 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
68 32-bit pieces of the result. */
69
Victor Stinner0b881dd2014-12-12 13:17:41 +010070#define READ_TIMESTAMP(val) do { \
71 unsigned int h, l; \
72 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
73 (val) = ((uint64)l) | (((uint64)h) << 32); \
74 } while(0)
Mark Dickinsona25b1312009-10-31 10:18:44 +000075
76
77#else
78
79#error "Don't know how to implement timestamp counter for this architecture"
80
Michael W. Hudson800ba232004-08-12 18:19:17 +000081#endif
82
Thomas Wouters477c8d52006-05-27 19:21:47 +000083void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 uint64 intr, inst, loop;
87 PyThreadState *tstate = PyThreadState_Get();
88 if (!tstate->interp->tscdump)
89 return;
90 intr = intr1 - intr0;
91 inst = inst1 - inst0 - intr;
92 loop = loop1 - loop0 - intr;
93 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000094 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095}
Michael W. Hudson800ba232004-08-12 18:19:17 +000096
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000097#endif
98
Guido van Rossum04691fc1992-08-12 15:35:34 +000099/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000100/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +0000101
Guido van Rossum408027e1996-12-30 16:17:54 +0000102#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000103/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104#define LLTRACE 1 /* Low-level trace feature */
105#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000106#endif
107
Jeremy Hylton52820442001-01-03 23:52:36 +0000108typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000109
Guido van Rossum374a9221991-04-04 10:40:29 +0000110/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000115#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
117static PyObject * do_call(PyObject *, PyObject ***, int, int);
118static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000119static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000121static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
122static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000123#define CALL_FLAG_VAR 1
124#define CALL_FLAG_KW 2
125
Guido van Rossum0a066c01992-03-27 17:29:15 +0000126#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000127static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000128static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000129#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100130static int call_trace(Py_tracefunc, PyObject *,
131 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000133static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100134 PyThreadState *, PyFrameObject *,
135 int, PyObject *);
136static void call_exc_trace(Py_tracefunc, PyObject *,
137 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000138static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100139 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000140
Thomas Wouters477c8d52006-05-27 19:21:47 +0000141static PyObject * cmp_outcome(int, PyObject *, PyObject *);
142static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000143static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000144static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000145static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200146static PyObject * unicode_concatenate(PyObject *, PyObject *,
147 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500148static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Yury Selivanoveb698fe2015-06-02 22:30:31 -0400149static PyObject * apply_coroutine_wrapper(PyObject *);
150
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Paul Prescode68140d2000-08-30 20:25:01 +0000152#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000154#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000156#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 "free variable '%.200s' referenced before assignment" \
158 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000159
Guido van Rossum950361c1997-01-24 13:49:28 +0000160/* Dynamic execution profile */
161#ifdef DYNAMIC_EXECUTION_PROFILE
162#ifdef DXPAIRS
163static long dxpairs[257][256];
164#define dxp dxpairs[256]
165#else
166static long dxp[256];
167#endif
168#endif
169
Jeremy Hylton985eba52003-02-05 23:13:00 +0000170/* Function call profile */
171#ifdef CALL_PROFILE
172#define PCALL_NUM 11
173static int pcall[PCALL_NUM];
174
175#define PCALL_ALL 0
176#define PCALL_FUNCTION 1
177#define PCALL_FAST_FUNCTION 2
178#define PCALL_FASTER_FUNCTION 3
179#define PCALL_METHOD 4
180#define PCALL_BOUND_METHOD 5
181#define PCALL_CFUNCTION 6
182#define PCALL_TYPE 7
183#define PCALL_GENERATOR 8
184#define PCALL_OTHER 9
185#define PCALL_POP 10
186
187/* Notes about the statistics
188
189 PCALL_FAST stats
190
191 FAST_FUNCTION means no argument tuple needs to be created.
192 FASTER_FUNCTION means that the fast-path frame setup code is used.
193
194 If there is a method call where the call can be optimized by changing
195 the argument tuple and calling the function directly, it gets recorded
196 twice.
197
198 As a result, the relationship among the statistics appears to be
199 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
200 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
201 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
202 PCALL_METHOD > PCALL_BOUND_METHOD
203*/
204
205#define PCALL(POS) pcall[POS]++
206
207PyObject *
208PyEval_GetCallStats(PyObject *self)
209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000210 return Py_BuildValue("iiiiiiiiiii",
211 pcall[0], pcall[1], pcall[2], pcall[3],
212 pcall[4], pcall[5], pcall[6], pcall[7],
213 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000214}
215#else
216#define PCALL(O)
217
218PyObject *
219PyEval_GetCallStats(PyObject *self)
220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 Py_INCREF(Py_None);
222 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000223}
224#endif
225
Tim Peters5ca576e2001-06-18 22:08:13 +0000226
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000227#ifdef WITH_THREAD
228#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
229#else
230#define GIL_REQUEST 0
231#endif
232
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000233/* This can set eval_breaker to 0 even though gil_drop_request became
234 1. We believe this is all right because the eval loop will release
235 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000236#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 _Py_atomic_store_relaxed( \
238 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000239 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
241 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000243#ifdef WITH_THREAD
244
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000245#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 do { \
247 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
248 _Py_atomic_store_relaxed(&eval_breaker, 1); \
249 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000250
251#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 do { \
253 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
254 COMPUTE_EVAL_BREAKER(); \
255 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000257#endif
258
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000259/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000260#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 do { \
262 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
263 _Py_atomic_store_relaxed(&eval_breaker, 1); \
264 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000265
266#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 do { \
268 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
269 COMPUTE_EVAL_BREAKER(); \
270 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000271
272#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 do { \
274 pending_async_exc = 1; \
275 _Py_atomic_store_relaxed(&eval_breaker, 1); \
276 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000277
278#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000280
281
Guido van Rossume59214e1994-08-30 08:01:59 +0000282#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000283
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000286#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000287#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000288
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000289static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000290static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000291/* This single variable consolidates all requests to break out of the fast path
292 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000293static _Py_atomic_int eval_breaker = {0};
294/* Request for dropping the GIL */
295static _Py_atomic_int gil_drop_request = {0};
296/* Request for running pending calls. */
297static _Py_atomic_int pendingcalls_to_do = {0};
298/* Request for looking at the `async_exc` field of the current thread state.
299 Guarded by the GIL. */
300static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000301
302#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303
Tim Peters7f468f22004-10-11 02:40:51 +0000304int
305PyEval_ThreadsInitialized(void)
306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000308}
309
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000310void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000311PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000312{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000313 if (gil_created())
314 return;
315 create_gil();
316 take_gil(PyThreadState_GET());
317 main_thread = PyThread_get_thread_ident();
318 if (!pending_lock)
319 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000321
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000322void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000323_PyEval_FiniThreads(void)
324{
325 if (!gil_created())
326 return;
327 destroy_gil();
328 assert(!gil_created());
329}
330
331void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000332PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000333{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 PyThreadState *tstate = PyThreadState_GET();
335 if (tstate == NULL)
336 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
337 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000338}
339
340void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000341PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 /* This function must succeed when the current thread state is NULL.
344 We therefore avoid PyThreadState_GET() which dumps a fatal error
345 in debug mode.
346 */
347 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
348 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000349}
350
351void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000352PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 if (tstate == NULL)
355 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
356 /* Check someone has called PyEval_InitThreads() to create the lock */
357 assert(gil_created());
358 take_gil(tstate);
359 if (PyThreadState_Swap(tstate) != NULL)
360 Py_FatalError(
361 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000362}
363
364void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000365PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000366{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 if (tstate == NULL)
368 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
369 if (PyThreadState_Swap(NULL) != tstate)
370 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
371 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000372}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000373
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200374/* This function is called from PyOS_AfterFork to destroy all threads which are
375 * not running in the child process, and clear internal locks which might be
376 * held by those threads. (This could also be done using pthread_atfork
377 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000378
379void
380PyEval_ReInitThreads(void)
381{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200382 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200384 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (!gil_created())
387 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 recreate_gil();
389 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200390 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000391 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 /* Update the threading module with the new state.
394 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200395 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 "threading");
397 if (threading == NULL) {
398 /* threading not imported */
399 PyErr_Clear();
400 return;
401 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200402 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 if (result == NULL)
404 PyErr_WriteUnraisable(threading);
405 else
406 Py_DECREF(result);
407 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200408
409 /* Destroy all threads except the current one */
410 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000411}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000412
413#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000414static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000415static int pending_async_exc = 0;
416#endif /* WITH_THREAD */
417
418/* This function is used to signal that async exceptions are waiting to be
419 raised, therefore it is also useful in non-threaded builds. */
420
421void
422_PyEval_SignalAsyncExc(void)
423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000425}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000426
Guido van Rossumff4949e1992-08-05 19:58:53 +0000427/* Functions save_thread and restore_thread are always defined so
428 dynamically loaded modules needn't be compiled separately for use
429 with and without threads: */
430
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000431PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000432PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 PyThreadState *tstate = PyThreadState_Swap(NULL);
435 if (tstate == NULL)
436 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000437#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (gil_created())
439 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000442}
443
444void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000445PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000446{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 if (tstate == NULL)
448 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000449#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 if (gil_created()) {
451 int err = errno;
452 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200453 /* _Py_Finalizing is protected by the GIL */
454 if (_Py_Finalizing && tstate != _Py_Finalizing) {
455 drop_gil(tstate);
456 PyThread_exit_thread();
457 assert(0); /* unreachable */
458 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 errno = err;
460 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000461#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000463}
464
465
Guido van Rossuma9672091994-09-14 13:31:22 +0000466/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
467 signal handlers or Mac I/O completion routines) can schedule calls
468 to a function to be called synchronously.
469 The synchronous function is called with one void* argument.
470 It should return 0 for success or -1 for failure -- failure should
471 be accompanied by an exception.
472
473 If registry succeeds, the registry function returns 0; if it fails
474 (e.g. due to too many pending calls) it returns -1 (without setting
475 an exception condition).
476
477 Note that because registry may occur from within signal handlers,
478 or other asynchronous events, calling malloc() is unsafe!
479
480#ifdef WITH_THREAD
481 Any thread can schedule pending calls, but only the main thread
482 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000483 There is no facility to schedule calls to a particular thread, but
484 that should be easy to change, should that ever be required. In
485 that case, the static variables here should go into the python
486 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000487#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000488*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000489
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000490#ifdef WITH_THREAD
491
492/* The WITH_THREAD implementation is thread-safe. It allows
493 scheduling to be made from any thread, and even from an executing
494 callback.
495 */
496
497#define NPENDINGCALLS 32
498static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int (*func)(void *);
500 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501} pendingcalls[NPENDINGCALLS];
502static int pendingfirst = 0;
503static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000504
505int
506Py_AddPendingCall(int (*func)(void *), void *arg)
507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 int i, j, result=0;
509 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 /* try a few times for the lock. Since this mechanism is used
512 * for signal handling (on the main thread), there is a (slim)
513 * chance that a signal is delivered on the same thread while we
514 * hold the lock during the Py_MakePendingCalls() function.
515 * This avoids a deadlock in that case.
516 * Note that signals can be delivered on any thread. In particular,
517 * on Windows, a SIGINT is delivered on a system-created worker
518 * thread.
519 * We also check for lock being NULL, in the unlikely case that
520 * this function is called before any bytecode evaluation takes place.
521 */
522 if (lock != NULL) {
523 for (i = 0; i<100; i++) {
524 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
525 break;
526 }
527 if (i == 100)
528 return -1;
529 }
530
531 i = pendinglast;
532 j = (i + 1) % NPENDINGCALLS;
533 if (j == pendingfirst) {
534 result = -1; /* Queue full */
535 } else {
536 pendingcalls[i].func = func;
537 pendingcalls[i].arg = arg;
538 pendinglast = j;
539 }
540 /* signal main loop */
541 SIGNAL_PENDING_CALLS();
542 if (lock != NULL)
543 PyThread_release_lock(lock);
544 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000545}
546
547int
548Py_MakePendingCalls(void)
549{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200550 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 int i;
552 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000554 if (!pending_lock) {
555 /* initial allocation of the lock */
556 pending_lock = PyThread_allocate_lock();
557 if (pending_lock == NULL)
558 return -1;
559 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 /* only service pending calls on main thread */
562 if (main_thread && PyThread_get_thread_ident() != main_thread)
563 return 0;
564 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200565 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200567 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 /* perform a bounded number of calls, in case of recursion */
569 for (i=0; i<NPENDINGCALLS; i++) {
570 int j;
571 int (*func)(void *);
572 void *arg = NULL;
573
574 /* pop one item off the queue while holding the lock */
575 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
576 j = pendingfirst;
577 if (j == pendinglast) {
578 func = NULL; /* Queue empty */
579 } else {
580 func = pendingcalls[j].func;
581 arg = pendingcalls[j].arg;
582 pendingfirst = (j + 1) % NPENDINGCALLS;
583 }
584 if (pendingfirst != pendinglast)
585 SIGNAL_PENDING_CALLS();
586 else
587 UNSIGNAL_PENDING_CALLS();
588 PyThread_release_lock(pending_lock);
589 /* having released the lock, perform the callback */
590 if (func == NULL)
591 break;
592 r = func(arg);
593 if (r)
594 break;
595 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200596 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000598}
599
600#else /* if ! defined WITH_THREAD */
601
602/*
603 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
604 This code is used for signal handling in python that isn't built
605 with WITH_THREAD.
606 Don't use this implementation when Py_AddPendingCalls() can happen
607 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608
Guido van Rossuma9672091994-09-14 13:31:22 +0000609 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000610 (1) nested asynchronous calls to Py_AddPendingCall()
611 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000613 (1) is very unlikely because typically signal delivery
614 is blocked during signal handling. So it should be impossible.
615 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000616 The current code is safe against (2), but not against (1).
617 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000618 thread is present, interrupted by signals, and that the critical
619 section is protected with the "busy" variable. On Windows, which
620 delivers SIGINT on a system thread, this does not hold and therefore
621 Windows really shouldn't use this version.
622 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000623*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000624
Guido van Rossuma9672091994-09-14 13:31:22 +0000625#define NPENDINGCALLS 32
626static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 int (*func)(void *);
628 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000629} pendingcalls[NPENDINGCALLS];
630static volatile int pendingfirst = 0;
631static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000632static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000633
634int
Thomas Wouters334fb892000-07-25 12:56:38 +0000635Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000636{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 static volatile int busy = 0;
638 int i, j;
639 /* XXX Begin critical section */
640 if (busy)
641 return -1;
642 busy = 1;
643 i = pendinglast;
644 j = (i + 1) % NPENDINGCALLS;
645 if (j == pendingfirst) {
646 busy = 0;
647 return -1; /* Queue full */
648 }
649 pendingcalls[i].func = func;
650 pendingcalls[i].arg = arg;
651 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 SIGNAL_PENDING_CALLS();
654 busy = 0;
655 /* XXX End critical section */
656 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000657}
658
Guido van Rossum180d7b41994-09-29 09:45:57 +0000659int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000660Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 static int busy = 0;
663 if (busy)
664 return 0;
665 busy = 1;
666 UNSIGNAL_PENDING_CALLS();
667 for (;;) {
668 int i;
669 int (*func)(void *);
670 void *arg;
671 i = pendingfirst;
672 if (i == pendinglast)
673 break; /* Queue empty */
674 func = pendingcalls[i].func;
675 arg = pendingcalls[i].arg;
676 pendingfirst = (i + 1) % NPENDINGCALLS;
677 if (func(arg) < 0) {
678 busy = 0;
679 SIGNAL_PENDING_CALLS(); /* We're not done yet */
680 return -1;
681 }
682 }
683 busy = 0;
684 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000685}
686
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000687#endif /* WITH_THREAD */
688
Guido van Rossuma9672091994-09-14 13:31:22 +0000689
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690/* The interpreter's recursion limit */
691
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000692#ifndef Py_DEFAULT_RECURSION_LIMIT
693#define Py_DEFAULT_RECURSION_LIMIT 1000
694#endif
695static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
696int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000697
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000698int
699Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000702}
703
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000704void
705Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000706{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707 recursion_limit = new_limit;
708 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000709}
710
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
712 if the recursion_depth reaches _Py_CheckRecursionLimit.
713 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
714 to guarantee that _Py_CheckRecursiveCall() is regularly called.
715 Without USE_STACKCHECK, there is no need for this. */
716int
717_Py_CheckRecursiveCall(char *where)
718{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000720
721#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 if (PyOS_CheckStack()) {
723 --tstate->recursion_depth;
724 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
725 return -1;
726 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000727#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728 _Py_CheckRecursionLimit = recursion_limit;
729 if (tstate->recursion_critical)
730 /* Somebody asked that we don't check for recursion. */
731 return 0;
732 if (tstate->overflowed) {
733 if (tstate->recursion_depth > recursion_limit + 50) {
734 /* Overflowing while handling an overflow. Give up. */
735 Py_FatalError("Cannot recover from stack overflow.");
736 }
737 return 0;
738 }
739 if (tstate->recursion_depth > recursion_limit) {
740 --tstate->recursion_depth;
741 tstate->overflowed = 1;
742 PyErr_Format(PyExc_RuntimeError,
743 "maximum recursion depth exceeded%s",
744 where);
745 return -1;
746 }
747 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000748}
749
Guido van Rossum374a9221991-04-04 10:40:29 +0000750/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000751enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000752 WHY_NOT = 0x0001, /* No error */
753 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000754 WHY_RETURN = 0x0008, /* 'return' statement */
755 WHY_BREAK = 0x0010, /* 'break' statement */
756 WHY_CONTINUE = 0x0020, /* 'continue' statement */
757 WHY_YIELD = 0x0040, /* 'yield' operator */
758 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000759};
Guido van Rossum374a9221991-04-04 10:40:29 +0000760
Benjamin Peterson87880242011-07-03 16:48:31 -0500761static void save_exc_state(PyThreadState *, PyFrameObject *);
762static void swap_exc_state(PyThreadState *, PyFrameObject *);
763static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400764static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000765static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000766
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000767/* Records whether tracing is on for any thread. Counts the number of
768 threads for which tstate->c_tracefunc is non-NULL, so if the value
769 is 0, we know we don't have to check this thread's c_tracefunc.
770 This speeds up the if statement in PyEval_EvalFrameEx() after
771 fast_next_opcode*/
772static int _Py_TracingPossible = 0;
773
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000774
Guido van Rossum374a9221991-04-04 10:40:29 +0000775
Guido van Rossumb209a111997-04-29 18:18:01 +0000776PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000777PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779 return PyEval_EvalCodeEx(co,
780 globals, locals,
781 (PyObject **)NULL, 0,
782 (PyObject **)NULL, 0,
783 (PyObject **)NULL, 0,
784 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000785}
786
787
788/* Interpreter main loop */
789
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000790PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000791PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 /* This is for backward compatibility with extension modules that
793 used this API; core interpreter code should call
794 PyEval_EvalFrameEx() */
795 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000796}
797
798PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000799PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000800{
Guido van Rossum950361c1997-01-24 13:49:28 +0000801#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000803#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200804 PyObject **stack_pointer; /* Next free slot in value stack */
805 unsigned char *next_instr;
806 int opcode; /* Current opcode */
807 int oparg; /* Current opcode argument, if any */
808 enum why_code why; /* Reason for block stack unwind */
809 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyObject *retval = NULL; /* Return value */
811 PyThreadState *tstate = PyThreadState_GET();
812 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 is true when the line being executed has changed. The
819 initial values are such as to make this false the first
820 time it is tested. */
821 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 unsigned char *first_instr;
824 PyObject *names;
825 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000826
Brett Cannon368b4b72012-04-02 12:17:59 -0400827#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200828 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400829#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200830
Antoine Pitroub52ec782009-01-25 16:34:23 +0000831/* Computed GOTOs, or
832 the-optimization-commonly-but-improperly-known-as-"threaded code"
833 using gcc's labels-as-values extension
834 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
835
836 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000837 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000838 combined with a lookup table of jump addresses. However, since the
839 indirect jump instruction is shared by all opcodes, the CPU will have a
840 hard time making the right prediction for where to jump next (actually,
841 it will be always wrong except in the uncommon case of a sequence of
842 several identical opcodes).
843
844 "Threaded code" in contrast, uses an explicit jump table and an explicit
845 indirect jump instruction at the end of each opcode. Since the jump
846 instruction is at a different address for each opcode, the CPU will make a
847 separate prediction for each of these instructions, which is equivalent to
848 predicting the second opcode of each opcode pair. These predictions have
849 a much better chance to turn out valid, especially in small bytecode loops.
850
851 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000853 and potentially many more instructions (depending on the pipeline width).
854 A correctly predicted branch, however, is nearly free.
855
856 At the time of this writing, the "threaded code" version is up to 15-20%
857 faster than the normal "switch" version, depending on the compiler and the
858 CPU architecture.
859
860 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
861 because it would render the measurements invalid.
862
863
864 NOTE: care must be taken that the compiler doesn't try to "optimize" the
865 indirect jumps by sharing them between all opcodes. Such optimizations
866 can be disabled on gcc by using the -fno-gcse flag (or possibly
867 -fno-crossjumping).
868*/
869
Antoine Pitrou042b1282010-08-13 21:15:58 +0000870#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000871#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000872#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000873#endif
874
Antoine Pitrou042b1282010-08-13 21:15:58 +0000875#ifdef HAVE_COMPUTED_GOTOS
876 #ifndef USE_COMPUTED_GOTOS
877 #define USE_COMPUTED_GOTOS 1
878 #endif
879#else
880 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
881 #error "Computed gotos are not supported on this compiler."
882 #endif
883 #undef USE_COMPUTED_GOTOS
884 #define USE_COMPUTED_GOTOS 0
885#endif
886
887#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000888/* Import the static jump table */
889#include "opcode_targets.h"
890
891/* This macro is used when several opcodes defer to the same implementation
892 (e.g. SETUP_LOOP, SETUP_FINALLY) */
893#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 TARGET_##op: \
895 opcode = op; \
896 if (HAS_ARG(op)) \
897 oparg = NEXTARG(); \
898 case op: \
899 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900
901#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 TARGET_##op: \
903 opcode = op; \
904 if (HAS_ARG(op)) \
905 oparg = NEXTARG(); \
906 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000907
908
909#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 { \
911 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
912 FAST_DISPATCH(); \
913 } \
914 continue; \
915 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000916
917#ifdef LLTRACE
918#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 { \
920 if (!lltrace && !_Py_TracingPossible) { \
921 f->f_lasti = INSTR_OFFSET(); \
922 goto *opcode_targets[*next_instr++]; \
923 } \
924 goto fast_next_opcode; \
925 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#else
927#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 { \
929 if (!_Py_TracingPossible) { \
930 f->f_lasti = INSTR_OFFSET(); \
931 goto *opcode_targets[*next_instr++]; \
932 } \
933 goto fast_next_opcode; \
934 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000935#endif
936
937#else
938#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000940#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 /* silence compiler warnings about `impl` unused */ \
942 if (0) goto impl; \
943 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000944#define DISPATCH() continue
945#define FAST_DISPATCH() goto fast_next_opcode
946#endif
947
948
Neal Norwitza81d2202002-07-14 00:27:26 +0000949/* Tuple access macros */
950
951#ifndef Py_DEBUG
952#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
953#else
954#define GETITEM(v, i) PyTuple_GetItem((v), (i))
955#endif
956
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000957#ifdef WITH_TSC
958/* Use Pentium timestamp counter to mark certain events:
959 inst0 -- beginning of switch statement for opcode dispatch
960 inst1 -- end of switch statement (may be skipped)
961 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000962 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000963 (may be skipped)
964 intr1 -- beginning of long interruption
965 intr2 -- end of long interruption
966
967 Many opcodes call out to helper C functions. In some cases, the
968 time in those functions should be counted towards the time for the
969 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
970 calls another Python function; there's no point in charge all the
971 bytecode executed by the called function to the caller.
972
973 It's hard to make a useful judgement statically. In the presence
974 of operator overloading, it's impossible to tell if a call will
975 execute new Python code or not.
976
977 It's a case-by-case judgement. I'll use intr1 for the following
978 cases:
979
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000980 IMPORT_STAR
981 IMPORT_FROM
982 CALL_FUNCTION (and friends)
983
984 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
986 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000987
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 READ_TIMESTAMP(inst0);
989 READ_TIMESTAMP(inst1);
990 READ_TIMESTAMP(loop0);
991 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 /* shut up the compiler */
994 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000995#endif
996
Guido van Rossum374a9221991-04-04 10:40:29 +0000997/* Code access macros */
998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999#define INSTR_OFFSET() ((int)(next_instr - first_instr))
1000#define NEXTOP() (*next_instr++)
1001#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
1002#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
1003#define JUMPTO(x) (next_instr = first_instr + (x))
1004#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001005
Raymond Hettingerf606f872003-03-16 03:11:04 +00001006/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 Some opcodes tend to come in pairs thus making it possible to
1008 predict the second code when the first is run. For example,
1009 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1010 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 Verifying the prediction costs a single high-speed test of a register
1013 variable against a constant. If the pairing was good, then the
1014 processor's own internal branch predication has a high likelihood of
1015 success, resulting in a nearly zero-overhead transition to the
1016 next opcode. A successful prediction saves a trip through the eval-loop
1017 including its two unpredictable branches, the HAS_ARG test and the
1018 switch-case. Combined with the processor's internal branch prediction,
1019 a successful PREDICT has the effect of making the two opcodes run as if
1020 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001021
Georg Brandl86b2fb92008-07-16 03:43:04 +00001022 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 predictions turned-on and interpret the results as if some opcodes
1024 had been combined or turn-off predictions so that the opcode frequency
1025 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001026
1027 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 the CPU to record separate branch prediction information for each
1029 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001030
Raymond Hettingerf606f872003-03-16 03:11:04 +00001031*/
1032
Antoine Pitrou042b1282010-08-13 21:15:58 +00001033#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034#define PREDICT(op) if (0) goto PRED_##op
1035#define PREDICTED(op) PRED_##op:
1036#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001037#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1039#define PREDICTED(op) PRED_##op: next_instr++
1040#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001041#endif
1042
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) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001105 { \
1106 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); \
1121 }
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;
1170 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1171 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 f->f_lasti now refers to the index of the last instruction
1174 executed. You might think this was obvious from the name, but
1175 this wasn't always true before 2.3! PyFrame_New now sets
1176 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1177 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1178 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001179 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1180 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
1188 at to the beginning of the combined pair.)
1189 */
1190 next_instr = first_instr + f->f_lasti + 1;
1191 stack_pointer = f->f_stacktop;
1192 assert(stack_pointer != NULL);
1193 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001194 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001195
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001196 if (co->co_flags & CO_GENERATOR) {
1197 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 /* We were in an except handler when we left,
1199 restore the exception state which was put aside
1200 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001201 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001203 else
1204 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001206
Tim Peters5ca576e2001-06-18 22:08:13 +00001207#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001208 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001209#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001212
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001213 if (throwflag) /* support for generator.throw() */
1214 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001215
Victor Stinnerace47d72013-07-18 01:41:08 +02001216#ifdef Py_DEBUG
1217 /* PyEval_EvalFrameEx() must not be called with an exception set,
1218 because it may clear it (directly or indirectly) and so the
1219 caller looses its exception */
1220 assert(!PyErr_Occurred());
1221#endif
1222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001224#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 if (inst1 == 0) {
1226 /* Almost surely, the opcode executed a break
1227 or a continue, preventing inst1 from being set
1228 on the way out of the loop.
1229 */
1230 READ_TIMESTAMP(inst1);
1231 loop1 = inst1;
1232 }
1233 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1234 intr0, intr1);
1235 ticked = 0;
1236 inst1 = 0;
1237 intr0 = 0;
1238 intr1 = 0;
1239 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001240#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1242 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001243 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 /* Do periodic things. Doing this every time through
1246 the loop would add too much overhead, so we do it
1247 only every Nth instruction. We also do it if
1248 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1249 event needs attention (e.g. a signal handler or
1250 async I/O handler); see Py_AddPendingCall() and
1251 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1254 if (*next_instr == SETUP_FINALLY) {
1255 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001256 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 goto fast_next_opcode;
1258 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001259#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001261#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001263 if (Py_MakePendingCalls() < 0)
1264 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001266#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001267 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 /* Give another thread a chance */
1269 if (PyThreadState_Swap(NULL) != tstate)
1270 Py_FatalError("ceval: tstate mix-up");
1271 drop_gil(tstate);
1272
1273 /* Other threads may run now */
1274
1275 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001276
1277 /* Check if we should make a quick exit. */
1278 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1279 drop_gil(tstate);
1280 PyThread_exit_thread();
1281 }
1282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (PyThreadState_Swap(tstate) != NULL)
1284 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001286#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 /* Check for asynchronous exceptions. */
1288 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001289 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 tstate->async_exc = NULL;
1291 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 PyErr_SetNone(exc);
1293 Py_DECREF(exc);
1294 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 }
1296 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 fast_next_opcode:
1299 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001304 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001305 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* see maybe_call_line_trace
1307 for expository comments */
1308 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 err = maybe_call_line_trace(tstate->c_tracefunc,
1311 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001312 tstate, f,
1313 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* Reload possibly changed frame fields */
1315 JUMPTO(f->f_lasti);
1316 if (f->f_stacktop != NULL) {
1317 stack_pointer = f->f_stacktop;
1318 f->f_stacktop = NULL;
1319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001320 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001322 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 opcode = NEXTOP();
1328 oparg = 0; /* allows oparg to be stored in a register because
1329 it doesn't have to be remembered across a full loop */
1330 if (HAS_ARG(opcode))
1331 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001332 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001333#ifdef DYNAMIC_EXECUTION_PROFILE
1334#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 dxpairs[lastopcode][opcode]++;
1336 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001337#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001339#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001340
Guido van Rossum96a42c81992-01-12 02:29:51 +00001341#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (lltrace) {
1345 if (HAS_ARG(opcode)) {
1346 printf("%d: %d, %d\n",
1347 f->f_lasti, opcode, oparg);
1348 }
1349 else {
1350 printf("%d: %d\n",
1351 f->f_lasti, opcode);
1352 }
1353 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001354#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 /* Main switch on opcode */
1357 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001361 /* BEWARE!
1362 It is essential that any operation that fails sets either
1363 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1364 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 TARGET(NOP)
1367 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001368
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001369 TARGET(LOAD_FAST) {
1370 PyObject *value = GETLOCAL(oparg);
1371 if (value == NULL) {
1372 format_exc_check_arg(PyExc_UnboundLocalError,
1373 UNBOUNDLOCAL_ERROR_MSG,
1374 PyTuple_GetItem(co->co_varnames, oparg));
1375 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001377 Py_INCREF(value);
1378 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001380 }
1381
1382 TARGET(LOAD_CONST) {
1383 PyObject *value = GETITEM(consts, oparg);
1384 Py_INCREF(value);
1385 PUSH(value);
1386 FAST_DISPATCH();
1387 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 TARGET(STORE_FAST) {
1391 PyObject *value = POP();
1392 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(POP_TOP) {
1397 PyObject *value = POP();
1398 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001401
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 TARGET(ROT_TWO) {
1403 PyObject *top = TOP();
1404 PyObject *second = SECOND();
1405 SET_TOP(second);
1406 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001408 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001409
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001410 TARGET(ROT_THREE) {
1411 PyObject *top = TOP();
1412 PyObject *second = SECOND();
1413 PyObject *third = THIRD();
1414 SET_TOP(second);
1415 SET_SECOND(third);
1416 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001419
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 TARGET(DUP_TOP) {
1421 PyObject *top = TOP();
1422 Py_INCREF(top);
1423 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 TARGET(DUP_TOP_TWO) {
1428 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001430 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001431 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001432 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001433 SET_TOP(top);
1434 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001435 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001436 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001437
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001438 TARGET(UNARY_POSITIVE) {
1439 PyObject *value = TOP();
1440 PyObject *res = PyNumber_Positive(value);
1441 Py_DECREF(value);
1442 SET_TOP(res);
1443 if (res == NULL)
1444 goto error;
1445 DISPATCH();
1446 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 TARGET(UNARY_NEGATIVE) {
1449 PyObject *value = TOP();
1450 PyObject *res = PyNumber_Negative(value);
1451 Py_DECREF(value);
1452 SET_TOP(res);
1453 if (res == NULL)
1454 goto error;
1455 DISPATCH();
1456 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001458 TARGET(UNARY_NOT) {
1459 PyObject *value = TOP();
1460 int err = PyObject_IsTrue(value);
1461 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001462 if (err == 0) {
1463 Py_INCREF(Py_True);
1464 SET_TOP(Py_True);
1465 DISPATCH();
1466 }
1467 else if (err > 0) {
1468 Py_INCREF(Py_False);
1469 SET_TOP(Py_False);
1470 err = 0;
1471 DISPATCH();
1472 }
1473 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 goto error;
1475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001477 TARGET(UNARY_INVERT) {
1478 PyObject *value = TOP();
1479 PyObject *res = PyNumber_Invert(value);
1480 Py_DECREF(value);
1481 SET_TOP(res);
1482 if (res == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 TARGET(BINARY_POWER) {
1488 PyObject *exp = POP();
1489 PyObject *base = TOP();
1490 PyObject *res = PyNumber_Power(base, exp, Py_None);
1491 Py_DECREF(base);
1492 Py_DECREF(exp);
1493 SET_TOP(res);
1494 if (res == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 TARGET(BINARY_MULTIPLY) {
1500 PyObject *right = POP();
1501 PyObject *left = TOP();
1502 PyObject *res = PyNumber_Multiply(left, right);
1503 Py_DECREF(left);
1504 Py_DECREF(right);
1505 SET_TOP(res);
1506 if (res == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001510
Benjamin Petersond51374e2014-04-09 23:55:56 -04001511 TARGET(BINARY_MATRIX_MULTIPLY) {
1512 PyObject *right = POP();
1513 PyObject *left = TOP();
1514 PyObject *res = PyNumber_MatrixMultiply(left, right);
1515 Py_DECREF(left);
1516 Py_DECREF(right);
1517 SET_TOP(res);
1518 if (res == NULL)
1519 goto error;
1520 DISPATCH();
1521 }
1522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001523 TARGET(BINARY_TRUE_DIVIDE) {
1524 PyObject *divisor = POP();
1525 PyObject *dividend = TOP();
1526 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1527 Py_DECREF(dividend);
1528 Py_DECREF(divisor);
1529 SET_TOP(quotient);
1530 if (quotient == NULL)
1531 goto error;
1532 DISPATCH();
1533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 TARGET(BINARY_FLOOR_DIVIDE) {
1536 PyObject *divisor = POP();
1537 PyObject *dividend = TOP();
1538 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1539 Py_DECREF(dividend);
1540 Py_DECREF(divisor);
1541 SET_TOP(quotient);
1542 if (quotient == NULL)
1543 goto error;
1544 DISPATCH();
1545 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001546
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001547 TARGET(BINARY_MODULO) {
1548 PyObject *divisor = POP();
1549 PyObject *dividend = TOP();
1550 PyObject *res = PyUnicode_CheckExact(dividend) ?
1551 PyUnicode_Format(dividend, divisor) :
1552 PyNumber_Remainder(dividend, divisor);
1553 Py_DECREF(divisor);
1554 Py_DECREF(dividend);
1555 SET_TOP(res);
1556 if (res == NULL)
1557 goto error;
1558 DISPATCH();
1559 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001561 TARGET(BINARY_ADD) {
1562 PyObject *right = POP();
1563 PyObject *left = TOP();
1564 PyObject *sum;
1565 if (PyUnicode_CheckExact(left) &&
1566 PyUnicode_CheckExact(right)) {
1567 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001568 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001569 }
1570 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 sum = PyNumber_Add(left, right);
1572 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001573 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001574 Py_DECREF(right);
1575 SET_TOP(sum);
1576 if (sum == NULL)
1577 goto error;
1578 DISPATCH();
1579 }
1580
1581 TARGET(BINARY_SUBTRACT) {
1582 PyObject *right = POP();
1583 PyObject *left = TOP();
1584 PyObject *diff = PyNumber_Subtract(left, right);
1585 Py_DECREF(right);
1586 Py_DECREF(left);
1587 SET_TOP(diff);
1588 if (diff == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
1592
1593 TARGET(BINARY_SUBSCR) {
1594 PyObject *sub = POP();
1595 PyObject *container = TOP();
1596 PyObject *res = PyObject_GetItem(container, sub);
1597 Py_DECREF(container);
1598 Py_DECREF(sub);
1599 SET_TOP(res);
1600 if (res == NULL)
1601 goto error;
1602 DISPATCH();
1603 }
1604
1605 TARGET(BINARY_LSHIFT) {
1606 PyObject *right = POP();
1607 PyObject *left = TOP();
1608 PyObject *res = PyNumber_Lshift(left, right);
1609 Py_DECREF(left);
1610 Py_DECREF(right);
1611 SET_TOP(res);
1612 if (res == NULL)
1613 goto error;
1614 DISPATCH();
1615 }
1616
1617 TARGET(BINARY_RSHIFT) {
1618 PyObject *right = POP();
1619 PyObject *left = TOP();
1620 PyObject *res = PyNumber_Rshift(left, right);
1621 Py_DECREF(left);
1622 Py_DECREF(right);
1623 SET_TOP(res);
1624 if (res == NULL)
1625 goto error;
1626 DISPATCH();
1627 }
1628
1629 TARGET(BINARY_AND) {
1630 PyObject *right = POP();
1631 PyObject *left = TOP();
1632 PyObject *res = PyNumber_And(left, right);
1633 Py_DECREF(left);
1634 Py_DECREF(right);
1635 SET_TOP(res);
1636 if (res == NULL)
1637 goto error;
1638 DISPATCH();
1639 }
1640
1641 TARGET(BINARY_XOR) {
1642 PyObject *right = POP();
1643 PyObject *left = TOP();
1644 PyObject *res = PyNumber_Xor(left, right);
1645 Py_DECREF(left);
1646 Py_DECREF(right);
1647 SET_TOP(res);
1648 if (res == NULL)
1649 goto error;
1650 DISPATCH();
1651 }
1652
1653 TARGET(BINARY_OR) {
1654 PyObject *right = POP();
1655 PyObject *left = TOP();
1656 PyObject *res = PyNumber_Or(left, right);
1657 Py_DECREF(left);
1658 Py_DECREF(right);
1659 SET_TOP(res);
1660 if (res == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
1664
1665 TARGET(LIST_APPEND) {
1666 PyObject *v = POP();
1667 PyObject *list = PEEK(oparg);
1668 int err;
1669 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 if (err != 0)
1672 goto error;
1673 PREDICT(JUMP_ABSOLUTE);
1674 DISPATCH();
1675 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 TARGET(SET_ADD) {
1678 PyObject *v = POP();
1679 PyObject *set = stack_pointer[-oparg];
1680 int err;
1681 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 if (err != 0)
1684 goto error;
1685 PREDICT(JUMP_ABSOLUTE);
1686 DISPATCH();
1687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 TARGET(INPLACE_POWER) {
1690 PyObject *exp = POP();
1691 PyObject *base = TOP();
1692 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1693 Py_DECREF(base);
1694 Py_DECREF(exp);
1695 SET_TOP(res);
1696 if (res == NULL)
1697 goto error;
1698 DISPATCH();
1699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 TARGET(INPLACE_MULTIPLY) {
1702 PyObject *right = POP();
1703 PyObject *left = TOP();
1704 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1705 Py_DECREF(left);
1706 Py_DECREF(right);
1707 SET_TOP(res);
1708 if (res == NULL)
1709 goto error;
1710 DISPATCH();
1711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Benjamin Petersond51374e2014-04-09 23:55:56 -04001713 TARGET(INPLACE_MATRIX_MULTIPLY) {
1714 PyObject *right = POP();
1715 PyObject *left = TOP();
1716 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1717 Py_DECREF(left);
1718 Py_DECREF(right);
1719 SET_TOP(res);
1720 if (res == NULL)
1721 goto error;
1722 DISPATCH();
1723 }
1724
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 TARGET(INPLACE_TRUE_DIVIDE) {
1726 PyObject *divisor = POP();
1727 PyObject *dividend = TOP();
1728 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1729 Py_DECREF(dividend);
1730 Py_DECREF(divisor);
1731 SET_TOP(quotient);
1732 if (quotient == NULL)
1733 goto error;
1734 DISPATCH();
1735 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001736
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001737 TARGET(INPLACE_FLOOR_DIVIDE) {
1738 PyObject *divisor = POP();
1739 PyObject *dividend = TOP();
1740 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1741 Py_DECREF(dividend);
1742 Py_DECREF(divisor);
1743 SET_TOP(quotient);
1744 if (quotient == NULL)
1745 goto error;
1746 DISPATCH();
1747 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001748
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001749 TARGET(INPLACE_MODULO) {
1750 PyObject *right = POP();
1751 PyObject *left = TOP();
1752 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1753 Py_DECREF(left);
1754 Py_DECREF(right);
1755 SET_TOP(mod);
1756 if (mod == NULL)
1757 goto error;
1758 DISPATCH();
1759 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001760
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001761 TARGET(INPLACE_ADD) {
1762 PyObject *right = POP();
1763 PyObject *left = TOP();
1764 PyObject *sum;
1765 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1766 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001767 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001768 }
1769 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001770 sum = PyNumber_InPlaceAdd(left, right);
1771 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001772 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001773 Py_DECREF(right);
1774 SET_TOP(sum);
1775 if (sum == NULL)
1776 goto error;
1777 DISPATCH();
1778 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001780 TARGET(INPLACE_SUBTRACT) {
1781 PyObject *right = POP();
1782 PyObject *left = TOP();
1783 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1784 Py_DECREF(left);
1785 Py_DECREF(right);
1786 SET_TOP(diff);
1787 if (diff == NULL)
1788 goto error;
1789 DISPATCH();
1790 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001791
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 TARGET(INPLACE_LSHIFT) {
1793 PyObject *right = POP();
1794 PyObject *left = TOP();
1795 PyObject *res = PyNumber_InPlaceLshift(left, right);
1796 Py_DECREF(left);
1797 Py_DECREF(right);
1798 SET_TOP(res);
1799 if (res == NULL)
1800 goto error;
1801 DISPATCH();
1802 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001803
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 TARGET(INPLACE_RSHIFT) {
1805 PyObject *right = POP();
1806 PyObject *left = TOP();
1807 PyObject *res = PyNumber_InPlaceRshift(left, right);
1808 Py_DECREF(left);
1809 Py_DECREF(right);
1810 SET_TOP(res);
1811 if (res == NULL)
1812 goto error;
1813 DISPATCH();
1814 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001815
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001816 TARGET(INPLACE_AND) {
1817 PyObject *right = POP();
1818 PyObject *left = TOP();
1819 PyObject *res = PyNumber_InPlaceAnd(left, right);
1820 Py_DECREF(left);
1821 Py_DECREF(right);
1822 SET_TOP(res);
1823 if (res == NULL)
1824 goto error;
1825 DISPATCH();
1826 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001828 TARGET(INPLACE_XOR) {
1829 PyObject *right = POP();
1830 PyObject *left = TOP();
1831 PyObject *res = PyNumber_InPlaceXor(left, right);
1832 Py_DECREF(left);
1833 Py_DECREF(right);
1834 SET_TOP(res);
1835 if (res == NULL)
1836 goto error;
1837 DISPATCH();
1838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 TARGET(INPLACE_OR) {
1841 PyObject *right = POP();
1842 PyObject *left = TOP();
1843 PyObject *res = PyNumber_InPlaceOr(left, right);
1844 Py_DECREF(left);
1845 Py_DECREF(right);
1846 SET_TOP(res);
1847 if (res == NULL)
1848 goto error;
1849 DISPATCH();
1850 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001851
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001852 TARGET(STORE_SUBSCR) {
1853 PyObject *sub = TOP();
1854 PyObject *container = SECOND();
1855 PyObject *v = THIRD();
1856 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 STACKADJ(-3);
1858 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 Py_DECREF(container);
1862 Py_DECREF(sub);
1863 if (err != 0)
1864 goto error;
1865 DISPATCH();
1866 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001867
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 TARGET(DELETE_SUBSCR) {
1869 PyObject *sub = TOP();
1870 PyObject *container = SECOND();
1871 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 STACKADJ(-2);
1873 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001874 err = PyObject_DelItem(container, sub);
1875 Py_DECREF(container);
1876 Py_DECREF(sub);
1877 if (err != 0)
1878 goto error;
1879 DISPATCH();
1880 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001881
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001883 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001885 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001886 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001887 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 PyErr_SetString(PyExc_RuntimeError,
1889 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 Py_DECREF(value);
1891 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001893 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 Py_DECREF(value);
1895 if (res == NULL)
1896 goto error;
1897 Py_DECREF(res);
1898 DISPATCH();
1899 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001900
Thomas Wouters434d0822000-08-24 20:11:32 +00001901#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001903#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 TARGET(RAISE_VARARGS) {
1905 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 switch (oparg) {
1907 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001912 if (do_raise(exc, cause)) {
1913 why = WHY_EXCEPTION;
1914 goto fast_block_end;
1915 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916 break;
1917 default:
1918 PyErr_SetString(PyExc_SystemError,
1919 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 break;
1921 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001922 goto error;
1923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001924
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 retval = POP();
1927 why = WHY_RETURN;
1928 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001930
Yury Selivanov75445082015-05-11 22:57:16 -04001931 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001932 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001933 PyObject *iter = NULL;
1934 PyObject *awaitable = NULL;
1935 PyObject *obj = TOP();
1936 PyTypeObject *type = Py_TYPE(obj);
1937
1938 if (type->tp_as_async != NULL)
1939 getter = type->tp_as_async->am_aiter;
1940
1941 if (getter != NULL) {
1942 iter = (*getter)(obj);
1943 Py_DECREF(obj);
1944 if (iter == NULL) {
1945 SET_TOP(NULL);
1946 goto error;
1947 }
1948 }
1949 else {
1950 SET_TOP(NULL);
1951 PyErr_Format(
1952 PyExc_TypeError,
1953 "'async for' requires an object with "
1954 "__aiter__ method, got %.100s",
1955 type->tp_name);
1956 Py_DECREF(obj);
1957 goto error;
1958 }
1959
1960 awaitable = _PyGen_GetAwaitableIter(iter);
1961 if (awaitable == NULL) {
1962 SET_TOP(NULL);
1963 PyErr_Format(
1964 PyExc_TypeError,
1965 "'async for' received an invalid object "
1966 "from __aiter__: %.100s",
1967 Py_TYPE(iter)->tp_name);
1968
1969 Py_DECREF(iter);
1970 goto error;
1971 } else
1972 Py_DECREF(iter);
1973
1974 SET_TOP(awaitable);
1975 DISPATCH();
1976 }
1977
1978 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001979 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001980 PyObject *next_iter = NULL;
1981 PyObject *awaitable = NULL;
1982 PyObject *aiter = TOP();
1983 PyTypeObject *type = Py_TYPE(aiter);
1984
1985 if (type->tp_as_async != NULL)
1986 getter = type->tp_as_async->am_anext;
1987
1988 if (getter != NULL) {
1989 next_iter = (*getter)(aiter);
1990 if (next_iter == NULL) {
1991 goto error;
1992 }
1993 }
1994 else {
1995 PyErr_Format(
1996 PyExc_TypeError,
1997 "'async for' requires an iterator with "
1998 "__anext__ method, got %.100s",
1999 type->tp_name);
2000 goto error;
2001 }
2002
2003 awaitable = _PyGen_GetAwaitableIter(next_iter);
2004 if (awaitable == NULL) {
2005 PyErr_Format(
2006 PyExc_TypeError,
2007 "'async for' received an invalid object "
2008 "from __anext__: %.100s",
2009 Py_TYPE(next_iter)->tp_name);
2010
2011 Py_DECREF(next_iter);
2012 goto error;
2013 } else
2014 Py_DECREF(next_iter);
2015
2016 PUSH(awaitable);
2017 DISPATCH();
2018 }
2019
2020 TARGET(GET_AWAITABLE) {
2021 PyObject *iterable = TOP();
2022 PyObject *iter = _PyGen_GetAwaitableIter(iterable);
2023
2024 Py_DECREF(iterable);
2025
2026 SET_TOP(iter); /* Even if it's NULL */
2027
2028 if (iter == NULL) {
2029 goto error;
2030 }
2031
2032 DISPATCH();
2033 }
2034
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 TARGET(YIELD_FROM) {
2036 PyObject *v = POP();
2037 PyObject *reciever = TOP();
2038 int err;
2039 if (PyGen_CheckExact(reciever)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002040 if (
2041 (((PyCodeObject*) \
2042 ((PyGenObject*)reciever)->gi_code)->co_flags &
2043 CO_COROUTINE)
2044 && !(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE)))
2045 {
2046 /* If we're yielding-from a coroutine object from a regular
2047 generator object - raise an error. */
2048
2049 Py_CLEAR(v);
2050 Py_CLEAR(reciever);
2051 SET_TOP(NULL);
2052
2053 PyErr_SetString(PyExc_TypeError,
2054 "cannot 'yield from' a coroutine object "
2055 "from a generator");
2056 goto error;
2057 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002059 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04002060 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 if (v == Py_None)
2062 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002063 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04002064 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 Py_DECREF(v);
2067 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002068 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08002069 if (tstate->c_tracefunc != NULL
2070 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002071 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10002072 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 if (err < 0)
2074 goto error;
2075 Py_DECREF(reciever);
2076 SET_TOP(val);
2077 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002078 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002079 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002080 f->f_stacktop = stack_pointer;
2081 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002082 /* and repeat... */
2083 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002084 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002085 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002087 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 retval = POP();
2089 f->f_stacktop = stack_pointer;
2090 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002092 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002093
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 TARGET(POP_EXCEPT) {
2095 PyTryBlock *b = PyFrame_BlockPop(f);
2096 if (b->b_type != EXCEPT_HANDLER) {
2097 PyErr_SetString(PyExc_SystemError,
2098 "popped block is not an except handler");
2099 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002104
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002105 TARGET(POP_BLOCK) {
2106 PyTryBlock *b = PyFrame_BlockPop(f);
2107 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 TARGET(END_FINALLY) {
2113 PyObject *status = POP();
2114 if (PyLong_Check(status)) {
2115 why = (enum why_code) PyLong_AS_LONG(status);
2116 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002117 if (why == WHY_RETURN ||
2118 why == WHY_CONTINUE)
2119 retval = POP();
2120 if (why == WHY_SILENCED) {
2121 /* An exception was silenced by 'with', we must
2122 manually unwind the EXCEPT_HANDLER block which was
2123 created when the exception was caught, otherwise
2124 the stack will be in an inconsistent state. */
2125 PyTryBlock *b = PyFrame_BlockPop(f);
2126 assert(b->b_type == EXCEPT_HANDLER);
2127 UNWIND_EXCEPT_HANDLER(b);
2128 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002129 Py_DECREF(status);
2130 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 Py_DECREF(status);
2133 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 else if (PyExceptionClass_Check(status)) {
2136 PyObject *exc = POP();
2137 PyObject *tb = POP();
2138 PyErr_Restore(status, exc, tb);
2139 why = WHY_EXCEPTION;
2140 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002141 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002142 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 PyErr_SetString(PyExc_SystemError,
2144 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 Py_DECREF(status);
2146 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 Py_DECREF(status);
2149 DISPATCH();
2150 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002151
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002152 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002153 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002154
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002156 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2158 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002159 PyErr_SetString(PyExc_NameError,
2160 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002161 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002162 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002164 }
2165 else {
2166 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2167 if (build_class_str == NULL)
2168 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2170 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002171 if (PyErr_ExceptionMatches(PyExc_KeyError))
2172 PyErr_SetString(PyExc_NameError,
2173 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002175 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002178 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002179 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002180
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 TARGET(STORE_NAME) {
2182 PyObject *name = GETITEM(names, oparg);
2183 PyObject *v = POP();
2184 PyObject *ns = f->f_locals;
2185 int err;
2186 if (ns == NULL) {
2187 PyErr_Format(PyExc_SystemError,
2188 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002189 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 if (PyDict_CheckExact(ns))
2193 err = PyDict_SetItem(ns, name, v);
2194 else
2195 err = PyObject_SetItem(ns, name, v);
2196 Py_DECREF(v);
2197 if (err != 0)
2198 goto error;
2199 DISPATCH();
2200 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 TARGET(DELETE_NAME) {
2203 PyObject *name = GETITEM(names, oparg);
2204 PyObject *ns = f->f_locals;
2205 int err;
2206 if (ns == NULL) {
2207 PyErr_Format(PyExc_SystemError,
2208 "no locals when deleting %R", name);
2209 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 err = PyObject_DelItem(ns, name);
2212 if (err != 0) {
2213 format_exc_check_arg(PyExc_NameError,
2214 NAME_ERROR_MSG,
2215 name);
2216 goto error;
2217 }
2218 DISPATCH();
2219 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002222 TARGET(UNPACK_SEQUENCE) {
2223 PyObject *seq = POP(), *item, **items;
2224 if (PyTuple_CheckExact(seq) &&
2225 PyTuple_GET_SIZE(seq) == oparg) {
2226 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 item = items[oparg];
2229 Py_INCREF(item);
2230 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 } else if (PyList_CheckExact(seq) &&
2233 PyList_GET_SIZE(seq) == oparg) {
2234 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 item = items[oparg];
2237 Py_INCREF(item);
2238 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 stack_pointer + oparg)) {
2242 STACKADJ(oparg);
2243 } else {
2244 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 Py_DECREF(seq);
2246 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002249 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002251
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 TARGET(UNPACK_EX) {
2253 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2254 PyObject *seq = POP();
2255
2256 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2257 stack_pointer + totalargs)) {
2258 stack_pointer += totalargs;
2259 } else {
2260 Py_DECREF(seq);
2261 goto error;
2262 }
2263 Py_DECREF(seq);
2264 DISPATCH();
2265 }
2266
2267 TARGET(STORE_ATTR) {
2268 PyObject *name = GETITEM(names, oparg);
2269 PyObject *owner = TOP();
2270 PyObject *v = SECOND();
2271 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 Py_DECREF(owner);
2276 if (err != 0)
2277 goto error;
2278 DISPATCH();
2279 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002280
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002281 TARGET(DELETE_ATTR) {
2282 PyObject *name = GETITEM(names, oparg);
2283 PyObject *owner = POP();
2284 int err;
2285 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2286 Py_DECREF(owner);
2287 if (err != 0)
2288 goto error;
2289 DISPATCH();
2290 }
2291
2292 TARGET(STORE_GLOBAL) {
2293 PyObject *name = GETITEM(names, oparg);
2294 PyObject *v = POP();
2295 int err;
2296 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002297 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002298 if (err != 0)
2299 goto error;
2300 DISPATCH();
2301 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002302
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002303 TARGET(DELETE_GLOBAL) {
2304 PyObject *name = GETITEM(names, oparg);
2305 int err;
2306 err = PyDict_DelItem(f->f_globals, name);
2307 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002309 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002310 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002311 }
2312 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002314
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002315 TARGET(LOAD_NAME) {
2316 PyObject *name = GETITEM(names, oparg);
2317 PyObject *locals = f->f_locals;
2318 PyObject *v;
2319 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002321 "no locals when loading %R", name);
2322 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002323 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002324 if (PyDict_CheckExact(locals)) {
2325 v = PyDict_GetItem(locals, name);
2326 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002327 }
2328 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002329 v = PyObject_GetItem(locals, name);
Antoine Pitrou1cfa0ba2013-10-07 20:40:59 +02002330 if (v == NULL && _PyErr_OCCURRED()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002331 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2332 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 PyErr_Clear();
2334 }
2335 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 if (v == NULL) {
2337 v = PyDict_GetItem(f->f_globals, name);
2338 Py_XINCREF(v);
2339 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002340 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002341 v = PyDict_GetItem(f->f_builtins, name);
2342 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002343 format_exc_check_arg(
2344 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002345 NAME_ERROR_MSG, name);
2346 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002349 }
2350 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002351 v = PyObject_GetItem(f->f_builtins, name);
2352 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002353 if (PyErr_ExceptionMatches(PyExc_KeyError))
2354 format_exc_check_arg(
2355 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 NAME_ERROR_MSG, name);
2357 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002358 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002361 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002362 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002363 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002366 TARGET(LOAD_GLOBAL) {
2367 PyObject *name = GETITEM(names, oparg);
2368 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002369 if (PyDict_CheckExact(f->f_globals)
2370 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002371 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002372 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002373 name);
2374 if (v == NULL) {
Antoine Pitrou59c900d2013-10-07 20:38:51 +02002375 if (!_PyErr_OCCURRED())
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002376 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002377 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002378 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002379 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002380 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002381 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002382 else {
2383 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 v = PyObject_GetItem(f->f_globals, name);
2385 if (v == NULL) {
2386 v = PyObject_GetItem(f->f_builtins, name);
2387 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002388 if (PyErr_ExceptionMatches(PyExc_KeyError))
2389 format_exc_check_arg(
2390 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002391 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002392 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002393 }
2394 }
2395 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002396 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002397 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002399
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002400 TARGET(DELETE_FAST) {
2401 PyObject *v = GETLOCAL(oparg);
2402 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002403 SETLOCAL(oparg, NULL);
2404 DISPATCH();
2405 }
2406 format_exc_check_arg(
2407 PyExc_UnboundLocalError,
2408 UNBOUNDLOCAL_ERROR_MSG,
2409 PyTuple_GetItem(co->co_varnames, oparg)
2410 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002411 goto error;
2412 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002413
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002414 TARGET(DELETE_DEREF) {
2415 PyObject *cell = freevars[oparg];
2416 if (PyCell_GET(cell) != NULL) {
2417 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002418 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002419 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002420 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 goto error;
2422 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002423
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002424 TARGET(LOAD_CLOSURE) {
2425 PyObject *cell = freevars[oparg];
2426 Py_INCREF(cell);
2427 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002429 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002430
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002431 TARGET(LOAD_CLASSDEREF) {
2432 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002433 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002434 assert(locals);
2435 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2436 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2437 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2438 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2439 if (PyDict_CheckExact(locals)) {
2440 value = PyDict_GetItem(locals, name);
2441 Py_XINCREF(value);
2442 }
2443 else {
2444 value = PyObject_GetItem(locals, name);
2445 if (value == NULL && PyErr_Occurred()) {
2446 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2447 goto error;
2448 PyErr_Clear();
2449 }
2450 }
2451 if (!value) {
2452 PyObject *cell = freevars[oparg];
2453 value = PyCell_GET(cell);
2454 if (value == NULL) {
2455 format_exc_unbound(co, oparg);
2456 goto error;
2457 }
2458 Py_INCREF(value);
2459 }
2460 PUSH(value);
2461 DISPATCH();
2462 }
2463
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 TARGET(LOAD_DEREF) {
2465 PyObject *cell = freevars[oparg];
2466 PyObject *value = PyCell_GET(cell);
2467 if (value == NULL) {
2468 format_exc_unbound(co, oparg);
2469 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002471 Py_INCREF(value);
2472 PUSH(value);
2473 DISPATCH();
2474 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002475
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002476 TARGET(STORE_DEREF) {
2477 PyObject *v = POP();
2478 PyObject *cell = freevars[oparg];
2479 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002481 DISPATCH();
2482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002483
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002484 TARGET(BUILD_TUPLE) {
2485 PyObject *tup = PyTuple_New(oparg);
2486 if (tup == NULL)
2487 goto error;
2488 while (--oparg >= 0) {
2489 PyObject *item = POP();
2490 PyTuple_SET_ITEM(tup, oparg, item);
2491 }
2492 PUSH(tup);
2493 DISPATCH();
2494 }
2495
2496 TARGET(BUILD_LIST) {
2497 PyObject *list = PyList_New(oparg);
2498 if (list == NULL)
2499 goto error;
2500 while (--oparg >= 0) {
2501 PyObject *item = POP();
2502 PyList_SET_ITEM(list, oparg, item);
2503 }
2504 PUSH(list);
2505 DISPATCH();
2506 }
2507
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002508 TARGET_WITH_IMPL(BUILD_TUPLE_UNPACK, _build_list_unpack)
2509 TARGET(BUILD_LIST_UNPACK)
2510 _build_list_unpack: {
2511 int convert_to_tuple = opcode == BUILD_TUPLE_UNPACK;
2512 int i;
2513 PyObject *sum = PyList_New(0);
2514 PyObject *return_value;
2515 if (sum == NULL)
2516 goto error;
2517
2518 for (i = oparg; i > 0; i--) {
2519 PyObject *none_val;
2520
2521 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2522 if (none_val == NULL) {
2523 Py_DECREF(sum);
2524 goto error;
2525 }
2526 Py_DECREF(none_val);
2527 }
2528
2529 if (convert_to_tuple) {
2530 return_value = PyList_AsTuple(sum);
2531 Py_DECREF(sum);
2532 if (return_value == NULL)
2533 goto error;
2534 }
2535 else {
2536 return_value = sum;
2537 }
2538
2539 while (oparg--)
2540 Py_DECREF(POP());
2541 PUSH(return_value);
2542 DISPATCH();
2543 }
2544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 TARGET(BUILD_SET) {
2546 PyObject *set = PySet_New(NULL);
2547 int err = 0;
2548 if (set == NULL)
2549 goto error;
2550 while (--oparg >= 0) {
2551 PyObject *item = POP();
2552 if (err == 0)
2553 err = PySet_Add(set, item);
2554 Py_DECREF(item);
2555 }
2556 if (err != 0) {
2557 Py_DECREF(set);
2558 goto error;
2559 }
2560 PUSH(set);
2561 DISPATCH();
2562 }
2563
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002564 TARGET(BUILD_SET_UNPACK) {
2565 int i;
2566 PyObject *sum = PySet_New(NULL);
2567 if (sum == NULL)
2568 goto error;
2569
2570 for (i = oparg; i > 0; i--) {
2571 if (_PySet_Update(sum, PEEK(i)) < 0) {
2572 Py_DECREF(sum);
2573 goto error;
2574 }
2575 }
2576
2577 while (oparg--)
2578 Py_DECREF(POP());
2579 PUSH(sum);
2580 DISPATCH();
2581 }
2582
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 TARGET(BUILD_MAP) {
2584 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2585 if (map == NULL)
2586 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002587 while (--oparg >= 0) {
2588 int err;
Benjamin Petersonee853392015-05-28 14:30:26 -05002589 PyObject *value = TOP();
2590 PyObject *key = SECOND();
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002591 STACKADJ(-2);
2592 err = PyDict_SetItem(map, key, value);
2593 Py_DECREF(value);
2594 Py_DECREF(key);
2595 if (err != 0) {
2596 Py_DECREF(map);
2597 goto error;
2598 }
2599 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 PUSH(map);
2601 DISPATCH();
2602 }
2603
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002604 TARGET_WITH_IMPL(BUILD_MAP_UNPACK_WITH_CALL, _build_map_unpack)
2605 TARGET(BUILD_MAP_UNPACK)
2606 _build_map_unpack: {
2607 int with_call = opcode == BUILD_MAP_UNPACK_WITH_CALL;
2608 int num_maps;
2609 int function_location;
2610 int i;
2611 PyObject *sum = PyDict_New();
2612 if (sum == NULL)
2613 goto error;
2614 if (with_call) {
2615 num_maps = oparg & 0xff;
2616 function_location = (oparg>>8) & 0xff;
2617 }
2618 else {
2619 num_maps = oparg;
2620 }
2621
2622 for (i = num_maps; i > 0; i--) {
2623 PyObject *arg = PEEK(i);
2624 if (with_call) {
2625 PyObject *intersection = _PyDictView_Intersect(sum, arg);
2626
2627 if (intersection == NULL) {
2628 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2629 PyObject *func = (
2630 PEEK(function_location + num_maps));
2631 PyErr_Format(PyExc_TypeError,
2632 "%.200s%.200s argument after ** "
2633 "must be a mapping, not %.200s",
2634 PyEval_GetFuncName(func),
2635 PyEval_GetFuncDesc(func),
2636 arg->ob_type->tp_name);
2637 }
2638 Py_DECREF(sum);
2639 goto error;
2640 }
2641
2642 if (PySet_GET_SIZE(intersection)) {
2643 Py_ssize_t idx = 0;
2644 PyObject *key;
2645 PyObject *func = PEEK(function_location + num_maps);
2646 Py_hash_t hash;
2647 _PySet_NextEntry(intersection, &idx, &key, &hash);
2648 if (!PyUnicode_Check(key)) {
2649 PyErr_Format(PyExc_TypeError,
2650 "%.200s%.200s keywords must be strings",
2651 PyEval_GetFuncName(func),
2652 PyEval_GetFuncDesc(func));
2653 } else {
2654 PyErr_Format(PyExc_TypeError,
2655 "%.200s%.200s got multiple "
2656 "values for keyword argument '%U'",
2657 PyEval_GetFuncName(func),
2658 PyEval_GetFuncDesc(func),
2659 key);
2660 }
2661 Py_DECREF(intersection);
2662 Py_DECREF(sum);
2663 goto error;
2664 }
2665 Py_DECREF(intersection);
2666 }
2667
2668 if (PyDict_Update(sum, arg) < 0) {
2669 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2670 PyErr_Format(PyExc_TypeError,
2671 "'%.200s' object is not a mapping",
2672 arg->ob_type->tp_name);
2673 }
2674 Py_DECREF(sum);
2675 goto error;
2676 }
2677 }
2678
2679 while (num_maps--)
2680 Py_DECREF(POP());
2681 PUSH(sum);
2682 DISPATCH();
2683 }
2684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 TARGET(MAP_ADD) {
2686 PyObject *key = TOP();
2687 PyObject *value = SECOND();
2688 PyObject *map;
2689 int err;
2690 STACKADJ(-2);
2691 map = stack_pointer[-oparg]; /* dict */
2692 assert(PyDict_CheckExact(map));
2693 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2694 Py_DECREF(value);
2695 Py_DECREF(key);
2696 if (err != 0)
2697 goto error;
2698 PREDICT(JUMP_ABSOLUTE);
2699 DISPATCH();
2700 }
2701
2702 TARGET(LOAD_ATTR) {
2703 PyObject *name = GETITEM(names, oparg);
2704 PyObject *owner = TOP();
2705 PyObject *res = PyObject_GetAttr(owner, name);
2706 Py_DECREF(owner);
2707 SET_TOP(res);
2708 if (res == NULL)
2709 goto error;
2710 DISPATCH();
2711 }
2712
2713 TARGET(COMPARE_OP) {
2714 PyObject *right = POP();
2715 PyObject *left = TOP();
2716 PyObject *res = cmp_outcome(oparg, left, right);
2717 Py_DECREF(left);
2718 Py_DECREF(right);
2719 SET_TOP(res);
2720 if (res == NULL)
2721 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 PREDICT(POP_JUMP_IF_FALSE);
2723 PREDICT(POP_JUMP_IF_TRUE);
2724 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002725 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002726
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 TARGET(IMPORT_NAME) {
2728 _Py_IDENTIFIER(__import__);
2729 PyObject *name = GETITEM(names, oparg);
2730 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002731 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 if (func == NULL) {
2733 PyErr_SetString(PyExc_ImportError,
2734 "__import__ not found");
2735 goto error;
2736 }
2737 Py_INCREF(func);
2738 from = POP();
2739 level = TOP();
2740 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2741 args = PyTuple_Pack(5,
2742 name,
2743 f->f_globals,
2744 f->f_locals == NULL ?
2745 Py_None : f->f_locals,
2746 from,
2747 level);
2748 else
2749 args = PyTuple_Pack(4,
2750 name,
2751 f->f_globals,
2752 f->f_locals == NULL ?
2753 Py_None : f->f_locals,
2754 from);
2755 Py_DECREF(level);
2756 Py_DECREF(from);
2757 if (args == NULL) {
2758 Py_DECREF(func);
2759 STACKADJ(-1);
2760 goto error;
2761 }
2762 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002763 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 READ_TIMESTAMP(intr1);
2765 Py_DECREF(args);
2766 Py_DECREF(func);
2767 SET_TOP(res);
2768 if (res == NULL)
2769 goto error;
2770 DISPATCH();
2771 }
2772
2773 TARGET(IMPORT_STAR) {
2774 PyObject *from = POP(), *locals;
2775 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002776 if (PyFrame_FastToLocalsWithError(f) < 0)
2777 goto error;
2778
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 locals = f->f_locals;
2780 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 PyErr_SetString(PyExc_SystemError,
2782 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 }
2785 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 READ_TIMESTAMP(intr1);
2788 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 Py_DECREF(from);
2790 if (err != 0)
2791 goto error;
2792 DISPATCH();
2793 }
Guido van Rossum25831651993-05-19 14:50:45 +00002794
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 TARGET(IMPORT_FROM) {
2796 PyObject *name = GETITEM(names, oparg);
2797 PyObject *from = TOP();
2798 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 PUSH(res);
2803 if (res == NULL)
2804 goto error;
2805 DISPATCH();
2806 }
Thomas Wouters52152252000-08-17 22:55:00 +00002807
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002808 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 JUMPBY(oparg);
2810 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 TARGET(POP_JUMP_IF_FALSE) {
2815 PyObject *cond = POP();
2816 int err;
2817 if (cond == Py_True) {
2818 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 FAST_DISPATCH();
2820 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 if (cond == Py_False) {
2822 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 JUMPTO(oparg);
2824 FAST_DISPATCH();
2825 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 err = PyObject_IsTrue(cond);
2827 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 if (err > 0)
2829 err = 0;
2830 else if (err == 0)
2831 JUMPTO(oparg);
2832 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 TARGET(POP_JUMP_IF_TRUE) {
2839 PyObject *cond = POP();
2840 int err;
2841 if (cond == Py_False) {
2842 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 FAST_DISPATCH();
2844 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002845 if (cond == Py_True) {
2846 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 JUMPTO(oparg);
2848 FAST_DISPATCH();
2849 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 err = PyObject_IsTrue(cond);
2851 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 if (err > 0) {
2853 err = 0;
2854 JUMPTO(oparg);
2855 }
2856 else if (err == 0)
2857 ;
2858 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002861 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002862
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 TARGET(JUMP_IF_FALSE_OR_POP) {
2864 PyObject *cond = TOP();
2865 int err;
2866 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 FAST_DISPATCH();
2870 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002871 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 JUMPTO(oparg);
2873 FAST_DISPATCH();
2874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 if (err > 0) {
2877 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 err = 0;
2880 }
2881 else if (err == 0)
2882 JUMPTO(oparg);
2883 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002887
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002888 TARGET(JUMP_IF_TRUE_OR_POP) {
2889 PyObject *cond = TOP();
2890 int err;
2891 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002893 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 FAST_DISPATCH();
2895 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 JUMPTO(oparg);
2898 FAST_DISPATCH();
2899 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (err > 0) {
2902 err = 0;
2903 JUMPTO(oparg);
2904 }
2905 else if (err == 0) {
2906 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 }
2909 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002912 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002917#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 /* Enabling this path speeds-up all while and for-loops by bypassing
2919 the per-loop checks for signals. By default, this should be turned-off
2920 because it prevents detection of a control-break in tight loops like
2921 "while 1: pass". Compile with this option turned-on when you need
2922 the speed-up and do not need break checking inside tight loops (ones
2923 that contain only instructions ending with FAST_DISPATCH).
2924 */
2925 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002926#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002928#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002930
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002934 PyObject *iter;
2935 /* If we have a generator object on top -- keep it there,
2936 it's already an iterator.
2937
2938 This is needed to allow use of 'async def' coroutines
2939 in 'yield from' expression from generator-based coroutines
2940 (decorated with types.coroutine()).
2941
2942 'yield from' is compiled to GET_ITER..YIELD_FROM combination,
2943 but since coroutines raise TypeError in their 'tp_iter' we
2944 need a way for them to "pass through" the GET_ITER.
2945 */
2946 if (!PyGen_CheckExact(iterable)) {
2947 /* `iterable` is not a generator. */
2948 iter = PyObject_GetIter(iterable);
2949 Py_DECREF(iterable);
2950 SET_TOP(iter);
2951 if (iter == NULL)
2952 goto error;
2953 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 PREDICT(FOR_ITER);
2955 DISPATCH();
2956 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002958 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002959 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002960 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 PyObject *iter = TOP();
2962 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2963 if (next != NULL) {
2964 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 PREDICT(STORE_FAST);
2966 PREDICT(UNPACK_SEQUENCE);
2967 DISPATCH();
2968 }
2969 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2971 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002972 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002973 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 PyErr_Clear();
2975 }
2976 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 STACKADJ(-1);
2978 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 JUMPBY(oparg);
2980 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002981 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002982
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 why = WHY_BREAK;
2985 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002987
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 if (retval == NULL)
2991 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 why = WHY_CONTINUE;
2993 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002994 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2997 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2998 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 /* NOTE: If you add any new block-setup opcodes that
3001 are not try/except/finally handlers, you may need
3002 to update the PyGen_NeedsFinalizing() function.
3003 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
3006 STACK_LEVEL());
3007 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003009
Yury Selivanov75445082015-05-11 22:57:16 -04003010 TARGET(BEFORE_ASYNC_WITH) {
3011 _Py_IDENTIFIER(__aexit__);
3012 _Py_IDENTIFIER(__aenter__);
3013
3014 PyObject *mgr = TOP();
3015 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
3016 *enter;
3017 PyObject *res;
3018 if (exit == NULL)
3019 goto error;
3020 SET_TOP(exit);
3021 enter = special_lookup(mgr, &PyId___aenter__);
3022 Py_DECREF(mgr);
3023 if (enter == NULL)
3024 goto error;
3025 res = PyObject_CallFunctionObjArgs(enter, NULL);
3026 Py_DECREF(enter);
3027 if (res == NULL)
3028 goto error;
3029 PUSH(res);
3030 DISPATCH();
3031 }
3032
3033 TARGET(SETUP_ASYNC_WITH) {
3034 PyObject *res = POP();
3035 /* Setup the finally block before pushing the result
3036 of __aenter__ on the stack. */
3037 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3038 STACK_LEVEL());
3039 PUSH(res);
3040 DISPATCH();
3041 }
3042
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003043 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003044 _Py_IDENTIFIER(__exit__);
3045 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 PyObject *mgr = TOP();
3047 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
3048 PyObject *res;
3049 if (exit == NULL)
3050 goto error;
3051 SET_TOP(exit);
3052 enter = special_lookup(mgr, &PyId___enter__);
3053 Py_DECREF(mgr);
3054 if (enter == NULL)
3055 goto error;
3056 res = PyObject_CallFunctionObjArgs(enter, NULL);
3057 Py_DECREF(enter);
3058 if (res == NULL)
3059 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003060 /* Setup the finally block before pushing the result
3061 of __enter__ on the stack. */
3062 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3063 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003064
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 DISPATCH();
3067 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003068
Yury Selivanov75445082015-05-11 22:57:16 -04003069 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003070 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 how/why we entered the finally clause:
3072 - TOP = None
3073 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3074 - TOP = WHY_*; no retval below it
3075 - (TOP, SECOND, THIRD) = exc_info()
3076 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3077 Below them is EXIT, the context.__exit__ bound method.
3078 In the last case, we must call
3079 EXIT(TOP, SECOND, THIRD)
3080 otherwise we must call
3081 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003082
Benjamin Peterson8f169482013-10-29 22:25:06 -04003083 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003085 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 In addition, if the stack represents an exception,
3089 *and* the function call returns a 'true' value, we
3090 push WHY_SILENCED onto the stack. END_FINALLY will
3091 then not re-raise the exception. (But non-local
3092 gotos should still be resumed.)
3093 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003094
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003095 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003096 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003097 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 (void)POP();
3099 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003100 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003101 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003102 else if (PyLong_Check(exc)) {
3103 STACKADJ(-1);
3104 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 case WHY_RETURN:
3106 case WHY_CONTINUE:
3107 /* Retval in TOP. */
3108 exit_func = SECOND();
3109 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003110 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 break;
3112 default:
3113 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003114 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 break;
3116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 }
3119 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003120 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003122 val = SECOND();
3123 tb = THIRD();
3124 tp2 = FOURTH();
3125 exc2 = PEEK(5);
3126 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003127 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 SET_VALUE(7, tb2);
3129 SET_VALUE(6, exc2);
3130 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3132 SET_FOURTH(NULL);
3133 /* We just shifted the stack down, so we have
3134 to tell the except handler block that the
3135 values are lower than it expects. */
3136 block = &f->f_blockstack[f->f_iblock - 1];
3137 assert(block->b_type == EXCEPT_HANDLER);
3138 block->b_level--;
3139 }
3140 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 if (res == NULL)
3144 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003145
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003146 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003147 PUSH(exc);
3148 PUSH(res);
3149 PREDICT(WITH_CLEANUP_FINISH);
3150 DISPATCH();
3151 }
3152
3153 PREDICTED(WITH_CLEANUP_FINISH);
3154 TARGET(WITH_CLEANUP_FINISH) {
3155 PyObject *res = POP();
3156 PyObject *exc = POP();
3157 int err;
3158
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003159 if (exc != Py_None)
3160 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 else
3162 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003163
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003165 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003167 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003168 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003169 else if (err > 0) {
3170 err = 0;
3171 /* There was an exception and a True return */
3172 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3173 }
3174 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003175 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003176 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003177
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003178 TARGET(CALL_FUNCTION) {
3179 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003180 PCALL(PCALL_ALL);
3181 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003182#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003183 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003184#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003185 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003186#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003188 PUSH(res);
3189 if (res == NULL)
3190 goto error;
3191 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
3195 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
3196 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 int na = oparg & 0xff;
3199 int nk = (oparg>>8) & 0xff;
3200 int flags = (opcode - CALL_FUNCTION) & 3;
3201 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003202 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 PCALL(PCALL_ALL);
3204 if (flags & CALL_FLAG_VAR)
3205 n++;
3206 if (flags & CALL_FLAG_KW)
3207 n++;
3208 pfunc = stack_pointer - n - 1;
3209 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00003212 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 PyObject *self = PyMethod_GET_SELF(func);
3214 Py_INCREF(self);
3215 func = PyMethod_GET_FUNCTION(func);
3216 Py_INCREF(func);
3217 Py_DECREF(*pfunc);
3218 *pfunc = self;
3219 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00003220 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 } else
3222 Py_INCREF(func);
3223 sp = stack_pointer;
3224 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003225 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 READ_TIMESTAMP(intr1);
3227 stack_pointer = sp;
3228 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003231 PyObject *o = POP();
3232 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003234 PUSH(res);
3235 if (res == NULL)
3236 goto error;
3237 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003240 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
3241 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003242 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 int posdefaults = oparg & 0xff;
3244 int kwdefaults = (oparg>>8) & 0xff;
3245 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003246
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003247 PyObject *qualname = POP(); /* qualname */
3248 PyObject *code = POP(); /* code object */
3249 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
3250 Py_DECREF(code);
3251 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003252
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003253 if (func == NULL)
3254 goto error;
3255
3256 if (opcode == MAKE_CLOSURE) {
3257 PyObject *closure = POP();
3258 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003260 Py_DECREF(func);
3261 Py_DECREF(closure);
3262 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003264 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003266
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003267 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003269 PyObject *names = POP(); /* names of args with annotations */
3270 PyObject *anns = PyDict_New();
3271 if (anns == NULL) {
3272 Py_DECREF(func);
3273 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003275 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003276 assert(num_annotations == name_ix+1);
3277 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003278 PyObject *name, *value;
3279 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003280 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003281 name = PyTuple_GET_ITEM(names, name_ix);
3282 value = POP();
3283 err = PyDict_SetItem(anns, name, value);
3284 Py_DECREF(value);
3285 if (err != 0) {
3286 Py_DECREF(anns);
3287 Py_DECREF(func);
3288 goto error;
3289 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003291
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 /* Can't happen unless
3294 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003295 Py_DECREF(anns);
3296 Py_DECREF(func);
3297 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003299 Py_DECREF(anns);
3300 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 if (kwdefaults > 0) {
3305 PyObject *defs = PyDict_New();
3306 if (defs == NULL) {
3307 Py_DECREF(func);
3308 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 }
3310 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003311 PyObject *v = POP(); /* default value */
3312 PyObject *key = POP(); /* kw only arg name */
3313 int err = PyDict_SetItem(defs, key, v);
3314 Py_DECREF(v);
3315 Py_DECREF(key);
3316 if (err != 0) {
3317 Py_DECREF(defs);
3318 Py_DECREF(func);
3319 goto error;
3320 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003322 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 /* Can't happen unless
3324 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003325 Py_DECREF(func);
3326 Py_DECREF(defs);
3327 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003329 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05003331 if (posdefaults > 0) {
3332 PyObject *defs = PyTuple_New(posdefaults);
3333 if (defs == NULL) {
3334 Py_DECREF(func);
3335 goto error;
3336 }
3337 while (--posdefaults >= 0)
3338 PyTuple_SET_ITEM(defs, posdefaults, POP());
3339 if (PyFunction_SetDefaults(func, defs) != 0) {
3340 /* Can't happen unless
3341 PyFunction_SetDefaults changes. */
3342 Py_DECREF(defs);
3343 Py_DECREF(func);
3344 goto error;
3345 }
3346 Py_DECREF(defs);
3347 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003348 PUSH(func);
3349 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003350 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003351
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003352 TARGET(BUILD_SLICE) {
3353 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003355 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003356 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003357 step = NULL;
3358 stop = POP();
3359 start = TOP();
3360 slice = PySlice_New(start, stop, step);
3361 Py_DECREF(start);
3362 Py_DECREF(stop);
3363 Py_XDECREF(step);
3364 SET_TOP(slice);
3365 if (slice == NULL)
3366 goto error;
3367 DISPATCH();
3368 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003369
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003370 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 opcode = NEXTOP();
3372 oparg = oparg<<16 | NEXTARG();
3373 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003375
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003376
Antoine Pitrou042b1282010-08-13 21:15:58 +00003377#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003379#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 default:
3381 fprintf(stderr,
3382 "XXX lineno: %d, opcode: %d\n",
3383 PyFrame_GetLineNumber(f),
3384 opcode);
3385 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003387
3388#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003390#endif
3391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003393
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003394 /* This should never be reached. Every opcode should end with DISPATCH()
3395 or goto error. */
3396 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003397
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003398error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003400
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 assert(why == WHY_NOT);
3402 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003405#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 if (!PyErr_Occurred())
3407 PyErr_SetString(PyExc_SystemError,
3408 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003409#else
3410 assert(PyErr_Occurred());
3411#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003412
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003413 /* Log traceback info. */
3414 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003415
Benjamin Peterson51f46162013-01-23 08:38:47 -05003416 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003417 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3418 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003419
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003420fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003421 assert(why != WHY_NOT);
3422
3423 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 while (why != WHY_NOT && f->f_iblock > 0) {
3425 /* Peek at the current block. */
3426 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 assert(why != WHY_YIELD);
3429 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3430 why = WHY_NOT;
3431 JUMPTO(PyLong_AS_LONG(retval));
3432 Py_DECREF(retval);
3433 break;
3434 }
3435 /* Now we have to pop the block. */
3436 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 if (b->b_type == EXCEPT_HANDLER) {
3439 UNWIND_EXCEPT_HANDLER(b);
3440 continue;
3441 }
3442 UNWIND_BLOCK(b);
3443 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3444 why = WHY_NOT;
3445 JUMPTO(b->b_handler);
3446 break;
3447 }
3448 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3449 || b->b_type == SETUP_FINALLY)) {
3450 PyObject *exc, *val, *tb;
3451 int handler = b->b_handler;
3452 /* Beware, this invalidates all b->b_* fields */
3453 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3454 PUSH(tstate->exc_traceback);
3455 PUSH(tstate->exc_value);
3456 if (tstate->exc_type != NULL) {
3457 PUSH(tstate->exc_type);
3458 }
3459 else {
3460 Py_INCREF(Py_None);
3461 PUSH(Py_None);
3462 }
3463 PyErr_Fetch(&exc, &val, &tb);
3464 /* Make the raw exception data
3465 available to the handler,
3466 so a program can emulate the
3467 Python main loop. */
3468 PyErr_NormalizeException(
3469 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003470 if (tb != NULL)
3471 PyException_SetTraceback(val, tb);
3472 else
3473 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 Py_INCREF(exc);
3475 tstate->exc_type = exc;
3476 Py_INCREF(val);
3477 tstate->exc_value = val;
3478 tstate->exc_traceback = tb;
3479 if (tb == NULL)
3480 tb = Py_None;
3481 Py_INCREF(tb);
3482 PUSH(tb);
3483 PUSH(val);
3484 PUSH(exc);
3485 why = WHY_NOT;
3486 JUMPTO(handler);
3487 break;
3488 }
3489 if (b->b_type == SETUP_FINALLY) {
3490 if (why & (WHY_RETURN | WHY_CONTINUE))
3491 PUSH(retval);
3492 PUSH(PyLong_FromLong((long)why));
3493 why = WHY_NOT;
3494 JUMPTO(b->b_handler);
3495 break;
3496 }
3497 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 if (why != WHY_NOT)
3502 break;
3503 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003504
Victor Stinnerace47d72013-07-18 01:41:08 +02003505 assert(!PyErr_Occurred());
3506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 assert(why != WHY_YIELD);
3510 /* Pop remaining stack entries. */
3511 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003512 PyObject *o = POP();
3513 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 if (why != WHY_RETURN)
3517 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003518
Victor Stinner4a7cc882015-03-06 23:35:27 +01003519 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003520
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003521fast_yield:
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003522 if (co->co_flags & CO_GENERATOR) {
3523
Benjamin Petersonac913412011-07-03 16:25:11 -05003524 /* The purpose of this block is to put aside the generator's exception
3525 state and restore that of the calling frame. If the current
3526 exception state is from the caller, we clear the exception values
3527 on the generator frame, so they are not swapped back in latter. The
3528 origin of the current exception state is determined by checking for
3529 except handler blocks, which we must be in iff a new exception
3530 state came into existence in this frame. (An uncaught exception
3531 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3532 int i;
3533 for (i = 0; i < f->f_iblock; i++)
3534 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3535 break;
3536 if (i == f->f_iblock)
3537 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003538 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003539 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003540 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003541 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003544 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003546 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3547 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003549 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 why = WHY_EXCEPTION;
3551 }
3552 }
3553 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003554 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3555 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 PyTrace_RETURN, NULL);
3557 }
3558 }
3559 if (tstate->c_profilefunc) {
3560 if (why == WHY_EXCEPTION)
3561 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003562 tstate->c_profileobj,
3563 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003565 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3566 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003568 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003569 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003570 }
3571 }
3572 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003575exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003577 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003578 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003579
Victor Stinnerefde1462015-03-21 15:04:43 +01003580 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003581}
3582
Benjamin Petersonb204a422011-06-05 22:04:07 -05003583static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003584format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3585{
3586 int err;
3587 Py_ssize_t len = PyList_GET_SIZE(names);
3588 PyObject *name_str, *comma, *tail, *tmp;
3589
3590 assert(PyList_CheckExact(names));
3591 assert(len >= 1);
3592 /* Deal with the joys of natural language. */
3593 switch (len) {
3594 case 1:
3595 name_str = PyList_GET_ITEM(names, 0);
3596 Py_INCREF(name_str);
3597 break;
3598 case 2:
3599 name_str = PyUnicode_FromFormat("%U and %U",
3600 PyList_GET_ITEM(names, len - 2),
3601 PyList_GET_ITEM(names, len - 1));
3602 break;
3603 default:
3604 tail = PyUnicode_FromFormat(", %U, and %U",
3605 PyList_GET_ITEM(names, len - 2),
3606 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003607 if (tail == NULL)
3608 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003609 /* Chop off the last two objects in the list. This shouldn't actually
3610 fail, but we can't be too careful. */
3611 err = PyList_SetSlice(names, len - 2, len, NULL);
3612 if (err == -1) {
3613 Py_DECREF(tail);
3614 return;
3615 }
3616 /* Stitch everything up into a nice comma-separated list. */
3617 comma = PyUnicode_FromString(", ");
3618 if (comma == NULL) {
3619 Py_DECREF(tail);
3620 return;
3621 }
3622 tmp = PyUnicode_Join(comma, names);
3623 Py_DECREF(comma);
3624 if (tmp == NULL) {
3625 Py_DECREF(tail);
3626 return;
3627 }
3628 name_str = PyUnicode_Concat(tmp, tail);
3629 Py_DECREF(tmp);
3630 Py_DECREF(tail);
3631 break;
3632 }
3633 if (name_str == NULL)
3634 return;
3635 PyErr_Format(PyExc_TypeError,
3636 "%U() missing %i required %s argument%s: %U",
3637 co->co_name,
3638 len,
3639 kind,
3640 len == 1 ? "" : "s",
3641 name_str);
3642 Py_DECREF(name_str);
3643}
3644
3645static void
3646missing_arguments(PyCodeObject *co, int missing, int defcount,
3647 PyObject **fastlocals)
3648{
3649 int i, j = 0;
3650 int start, end;
3651 int positional = defcount != -1;
3652 const char *kind = positional ? "positional" : "keyword-only";
3653 PyObject *missing_names;
3654
3655 /* Compute the names of the arguments that are missing. */
3656 missing_names = PyList_New(missing);
3657 if (missing_names == NULL)
3658 return;
3659 if (positional) {
3660 start = 0;
3661 end = co->co_argcount - defcount;
3662 }
3663 else {
3664 start = co->co_argcount;
3665 end = start + co->co_kwonlyargcount;
3666 }
3667 for (i = start; i < end; i++) {
3668 if (GETLOCAL(i) == NULL) {
3669 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3670 PyObject *name = PyObject_Repr(raw);
3671 if (name == NULL) {
3672 Py_DECREF(missing_names);
3673 return;
3674 }
3675 PyList_SET_ITEM(missing_names, j++, name);
3676 }
3677 }
3678 assert(j == missing);
3679 format_missing(kind, co, missing_names);
3680 Py_DECREF(missing_names);
3681}
3682
3683static void
3684too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003685{
3686 int plural;
3687 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003688 int i;
3689 PyObject *sig, *kwonly_sig;
3690
Benjamin Petersone109c702011-06-24 09:37:26 -05003691 assert((co->co_flags & CO_VARARGS) == 0);
3692 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003693 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003694 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003695 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003696 if (defcount) {
3697 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003698 plural = 1;
3699 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3700 }
3701 else {
3702 plural = co->co_argcount != 1;
3703 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3704 }
3705 if (sig == NULL)
3706 return;
3707 if (kwonly_given) {
3708 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3709 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3710 kwonly_given != 1 ? "s" : "");
3711 if (kwonly_sig == NULL) {
3712 Py_DECREF(sig);
3713 return;
3714 }
3715 }
3716 else {
3717 /* This will not fail. */
3718 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003719 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003720 }
3721 PyErr_Format(PyExc_TypeError,
3722 "%U() takes %U positional argument%s but %d%U %s given",
3723 co->co_name,
3724 sig,
3725 plural ? "s" : "",
3726 given,
3727 kwonly_sig,
3728 given == 1 && !kwonly_given ? "was" : "were");
3729 Py_DECREF(sig);
3730 Py_DECREF(kwonly_sig);
3731}
3732
Guido van Rossumc2e20742006-02-27 22:32:47 +00003733/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003734 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003735 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003736
Victor Stinner40ee3012014-06-16 15:59:28 +02003737static PyObject *
3738_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003740 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3741 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003742{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003743 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003744 PyFrameObject *f;
3745 PyObject *retval = NULL;
3746 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 PyThreadState *tstate = PyThreadState_GET();
3748 PyObject *x, *u;
3749 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003750 int i;
3751 int n = argcount;
3752 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 if (globals == NULL) {
3755 PyErr_SetString(PyExc_SystemError,
3756 "PyEval_EvalCodeEx: NULL globals");
3757 return NULL;
3758 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 assert(tstate != NULL);
3761 assert(globals != NULL);
3762 f = PyFrame_New(tstate, co, globals, locals);
3763 if (f == NULL)
3764 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 fastlocals = f->f_localsplus;
3767 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003768
Benjamin Petersonb204a422011-06-05 22:04:07 -05003769 /* Parse arguments. */
3770 if (co->co_flags & CO_VARKEYWORDS) {
3771 kwdict = PyDict_New();
3772 if (kwdict == NULL)
3773 goto fail;
3774 i = total_args;
3775 if (co->co_flags & CO_VARARGS)
3776 i++;
3777 SETLOCAL(i, kwdict);
3778 }
3779 if (argcount > co->co_argcount)
3780 n = co->co_argcount;
3781 for (i = 0; i < n; i++) {
3782 x = args[i];
3783 Py_INCREF(x);
3784 SETLOCAL(i, x);
3785 }
3786 if (co->co_flags & CO_VARARGS) {
3787 u = PyTuple_New(argcount - n);
3788 if (u == NULL)
3789 goto fail;
3790 SETLOCAL(total_args, u);
3791 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003792 x = args[i];
3793 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003794 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003796 }
3797 for (i = 0; i < kwcount; i++) {
3798 PyObject **co_varnames;
3799 PyObject *keyword = kws[2*i];
3800 PyObject *value = kws[2*i + 1];
3801 int j;
3802 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3803 PyErr_Format(PyExc_TypeError,
3804 "%U() keywords must be strings",
3805 co->co_name);
3806 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003808 /* Speed hack: do raw pointer compares. As names are
3809 normally interned this should almost always hit. */
3810 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3811 for (j = 0; j < total_args; j++) {
3812 PyObject *nm = co_varnames[j];
3813 if (nm == keyword)
3814 goto kw_found;
3815 }
3816 /* Slow fallback, just in case */
3817 for (j = 0; j < total_args; j++) {
3818 PyObject *nm = co_varnames[j];
3819 int cmp = PyObject_RichCompareBool(
3820 keyword, nm, Py_EQ);
3821 if (cmp > 0)
3822 goto kw_found;
3823 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003825 }
3826 if (j >= total_args && kwdict == NULL) {
3827 PyErr_Format(PyExc_TypeError,
3828 "%U() got an unexpected "
3829 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 co->co_name,
3831 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003832 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003833 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003834 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3835 goto fail;
3836 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 continue;
3838 kw_found:
3839 if (GETLOCAL(j) != NULL) {
3840 PyErr_Format(PyExc_TypeError,
3841 "%U() got multiple "
3842 "values for argument '%S'",
3843 co->co_name,
3844 keyword);
3845 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003847 Py_INCREF(value);
3848 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003850 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003851 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 goto fail;
3853 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003854 if (argcount < co->co_argcount) {
3855 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003856 int missing = 0;
3857 for (i = argcount; i < m; i++)
3858 if (GETLOCAL(i) == NULL)
3859 missing++;
3860 if (missing) {
3861 missing_arguments(co, missing, defcount, fastlocals);
3862 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003863 }
3864 if (n > m)
3865 i = n - m;
3866 else
3867 i = 0;
3868 for (; i < defcount; i++) {
3869 if (GETLOCAL(m+i) == NULL) {
3870 PyObject *def = defs[i];
3871 Py_INCREF(def);
3872 SETLOCAL(m+i, def);
3873 }
3874 }
3875 }
3876 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003877 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003878 for (i = co->co_argcount; i < total_args; i++) {
3879 PyObject *name;
3880 if (GETLOCAL(i) != NULL)
3881 continue;
3882 name = PyTuple_GET_ITEM(co->co_varnames, i);
3883 if (kwdefs != NULL) {
3884 PyObject *def = PyDict_GetItem(kwdefs, name);
3885 if (def) {
3886 Py_INCREF(def);
3887 SETLOCAL(i, def);
3888 continue;
3889 }
3890 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003891 missing++;
3892 }
3893 if (missing) {
3894 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003895 goto fail;
3896 }
3897 }
3898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003900 vars into frame. */
3901 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003903 int arg;
3904 /* Possibly account for the cell variable being an argument. */
3905 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003906 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003907 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003908 /* Clear the local copy. */
3909 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003910 }
3911 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003912 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003913 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003914 if (c == NULL)
3915 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003916 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003918 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3919 PyObject *o = PyTuple_GET_ITEM(closure, i);
3920 Py_INCREF(o);
3921 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 if (co->co_flags & CO_GENERATOR) {
Yury Selivanov75445082015-05-11 22:57:16 -04003925 PyObject *gen;
Yury Selivanov75445082015-05-11 22:57:16 -04003926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003927 /* Don't need to keep the reference to f_back, it will be set
3928 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003929 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 /* Create a new generator that owns the ready to run frame
3934 * and return that as the value. */
Yury Selivanov75445082015-05-11 22:57:16 -04003935 gen = PyGen_NewWithQualName(f, name, qualname);
3936 if (gen == NULL)
3937 return NULL;
3938
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003939 if (co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))
Yury Selivanoveb698fe2015-06-02 22:30:31 -04003940 return apply_coroutine_wrapper(gen);
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003941
Yury Selivanov75445082015-05-11 22:57:16 -04003942 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003946
Thomas Woutersce272b62007-09-19 21:19:28 +00003947fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 /* decref'ing the frame can cause __del__ methods to get invoked,
3950 which can call back into Python. While we're done with the
3951 current Python frame (f), the associated C stack is still in use,
3952 so recursion_depth must be boosted for the duration.
3953 */
3954 assert(tstate != NULL);
3955 ++tstate->recursion_depth;
3956 Py_DECREF(f);
3957 --tstate->recursion_depth;
3958 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003959}
3960
Victor Stinner40ee3012014-06-16 15:59:28 +02003961PyObject *
3962PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
3963 PyObject **args, int argcount, PyObject **kws, int kwcount,
3964 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
3965{
3966 return _PyEval_EvalCodeWithName(_co, globals, locals,
3967 args, argcount, kws, kwcount,
3968 defs, defcount, kwdefs, closure,
3969 NULL, NULL);
3970}
Tim Peters5ca576e2001-06-18 22:08:13 +00003971
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003972static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003973special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003974{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003975 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003976 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003978 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 return NULL;
3980 }
3981 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003982}
3983
3984
Benjamin Peterson87880242011-07-03 16:48:31 -05003985/* These 3 functions deal with the exception state of generators. */
3986
3987static void
3988save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3989{
3990 PyObject *type, *value, *traceback;
3991 Py_XINCREF(tstate->exc_type);
3992 Py_XINCREF(tstate->exc_value);
3993 Py_XINCREF(tstate->exc_traceback);
3994 type = f->f_exc_type;
3995 value = f->f_exc_value;
3996 traceback = f->f_exc_traceback;
3997 f->f_exc_type = tstate->exc_type;
3998 f->f_exc_value = tstate->exc_value;
3999 f->f_exc_traceback = tstate->exc_traceback;
4000 Py_XDECREF(type);
4001 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004002 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004003}
4004
4005static void
4006swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4007{
4008 PyObject *tmp;
4009 tmp = tstate->exc_type;
4010 tstate->exc_type = f->f_exc_type;
4011 f->f_exc_type = tmp;
4012 tmp = tstate->exc_value;
4013 tstate->exc_value = f->f_exc_value;
4014 f->f_exc_value = tmp;
4015 tmp = tstate->exc_traceback;
4016 tstate->exc_traceback = f->f_exc_traceback;
4017 f->f_exc_traceback = tmp;
4018}
4019
4020static void
4021restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4022{
4023 PyObject *type, *value, *tb;
4024 type = tstate->exc_type;
4025 value = tstate->exc_value;
4026 tb = tstate->exc_traceback;
4027 tstate->exc_type = f->f_exc_type;
4028 tstate->exc_value = f->f_exc_value;
4029 tstate->exc_traceback = f->f_exc_traceback;
4030 f->f_exc_type = NULL;
4031 f->f_exc_value = NULL;
4032 f->f_exc_traceback = NULL;
4033 Py_XDECREF(type);
4034 Py_XDECREF(value);
4035 Py_XDECREF(tb);
4036}
4037
4038
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004039/* Logic for the raise statement (too complicated for inlining).
4040 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004041static int
Collin Winter828f04a2007-08-31 00:04:24 +00004042do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 if (exc == NULL) {
4047 /* Reraise */
4048 PyThreadState *tstate = PyThreadState_GET();
4049 PyObject *tb;
4050 type = tstate->exc_type;
4051 value = tstate->exc_value;
4052 tb = tstate->exc_traceback;
4053 if (type == Py_None) {
4054 PyErr_SetString(PyExc_RuntimeError,
4055 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004056 return 0;
4057 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 Py_XINCREF(type);
4059 Py_XINCREF(value);
4060 Py_XINCREF(tb);
4061 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004062 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004063 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 /* We support the following forms of raise:
4066 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004067 raise <instance>
4068 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 if (PyExceptionClass_Check(exc)) {
4071 type = exc;
4072 value = PyObject_CallObject(exc, NULL);
4073 if (value == NULL)
4074 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004075 if (!PyExceptionInstance_Check(value)) {
4076 PyErr_Format(PyExc_TypeError,
4077 "calling %R should have returned an instance of "
4078 "BaseException, not %R",
4079 type, Py_TYPE(value));
4080 goto raise_error;
4081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 }
4083 else if (PyExceptionInstance_Check(exc)) {
4084 value = exc;
4085 type = PyExceptionInstance_Class(exc);
4086 Py_INCREF(type);
4087 }
4088 else {
4089 /* Not something you can raise. You get an exception
4090 anyway, just not what you specified :-) */
4091 Py_DECREF(exc);
4092 PyErr_SetString(PyExc_TypeError,
4093 "exceptions must derive from BaseException");
4094 goto raise_error;
4095 }
Collin Winter828f04a2007-08-31 00:04:24 +00004096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 if (cause) {
4098 PyObject *fixed_cause;
4099 if (PyExceptionClass_Check(cause)) {
4100 fixed_cause = PyObject_CallObject(cause, NULL);
4101 if (fixed_cause == NULL)
4102 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004103 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004105 else if (PyExceptionInstance_Check(cause)) {
4106 fixed_cause = cause;
4107 }
4108 else if (cause == Py_None) {
4109 Py_DECREF(cause);
4110 fixed_cause = NULL;
4111 }
4112 else {
4113 PyErr_SetString(PyExc_TypeError,
4114 "exception causes must derive from "
4115 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 goto raise_error;
4117 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004118 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 }
Collin Winter828f04a2007-08-31 00:04:24 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 PyErr_SetObject(type, value);
4122 /* PyErr_SetObject incref's its arguments */
4123 Py_XDECREF(value);
4124 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004125 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004126
4127raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 Py_XDECREF(value);
4129 Py_XDECREF(type);
4130 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004131 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004132}
4133
Tim Petersd6d010b2001-06-21 02:49:55 +00004134/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004135 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004136
Guido van Rossum0368b722007-05-11 16:50:42 +00004137 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4138 with a variable target.
4139*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004140
Barry Warsawe42b18f1997-08-25 22:13:04 +00004141static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004142unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 int i = 0, j = 0;
4145 Py_ssize_t ll = 0;
4146 PyObject *it; /* iter(v) */
4147 PyObject *w;
4148 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004152 it = PyObject_GetIter(v);
4153 if (it == NULL)
4154 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 for (; i < argcnt; i++) {
4157 w = PyIter_Next(it);
4158 if (w == NULL) {
4159 /* Iterator done, via error or exhaustion. */
4160 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004161 if (argcntafter == -1) {
4162 PyErr_Format(PyExc_ValueError,
4163 "not enough values to unpack (expected %d, got %d)",
4164 argcnt, i);
4165 }
4166 else {
4167 PyErr_Format(PyExc_ValueError,
4168 "not enough values to unpack "
4169 "(expected at least %d, got %d)",
4170 argcnt + argcntafter, i);
4171 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 }
4173 goto Error;
4174 }
4175 *--sp = w;
4176 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 if (argcntafter == -1) {
4179 /* We better have exhausted the iterator now. */
4180 w = PyIter_Next(it);
4181 if (w == NULL) {
4182 if (PyErr_Occurred())
4183 goto Error;
4184 Py_DECREF(it);
4185 return 1;
4186 }
4187 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004188 PyErr_Format(PyExc_ValueError,
4189 "too many values to unpack (expected %d)",
4190 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 goto Error;
4192 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 l = PySequence_List(it);
4195 if (l == NULL)
4196 goto Error;
4197 *--sp = l;
4198 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 ll = PyList_GET_SIZE(l);
4201 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004202 PyErr_Format(PyExc_ValueError,
4203 "not enough values to unpack (expected at least %d, got %zd)",
4204 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 goto Error;
4206 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 /* Pop the "after-variable" args off the list. */
4209 for (j = argcntafter; j > 0; j--, i++) {
4210 *--sp = PyList_GET_ITEM(l, ll - j);
4211 }
4212 /* Resize the list. */
4213 Py_SIZE(l) = ll - argcntafter;
4214 Py_DECREF(it);
4215 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004216
Tim Petersd6d010b2001-06-21 02:49:55 +00004217Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 for (; i > 0; i--, sp++)
4219 Py_DECREF(*sp);
4220 Py_XDECREF(it);
4221 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004222}
4223
4224
Guido van Rossum96a42c81992-01-12 02:29:51 +00004225#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004226static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004227prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004228{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 printf("%s ", str);
4230 if (PyObject_Print(v, stdout, 0) != 0)
4231 PyErr_Clear(); /* Don't know what else to do */
4232 printf("\n");
4233 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004234}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004235#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004236
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004237static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004238call_exc_trace(Py_tracefunc func, PyObject *self,
4239 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004240{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004241 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004243 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 if (value == NULL) {
4245 value = Py_None;
4246 Py_INCREF(value);
4247 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004248 PyErr_NormalizeException(&type, &value, &orig_traceback);
4249 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 arg = PyTuple_Pack(3, type, value, traceback);
4251 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004252 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 return;
4254 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004255 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 Py_DECREF(arg);
4257 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004258 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 else {
4260 Py_XDECREF(type);
4261 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004262 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004264}
4265
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004266static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004267call_trace_protected(Py_tracefunc func, PyObject *obj,
4268 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004270{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 PyObject *type, *value, *traceback;
4272 int err;
4273 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004274 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 if (err == 0)
4276 {
4277 PyErr_Restore(type, value, traceback);
4278 return 0;
4279 }
4280 else {
4281 Py_XDECREF(type);
4282 Py_XDECREF(value);
4283 Py_XDECREF(traceback);
4284 return -1;
4285 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004286}
4287
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004288static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004289call_trace(Py_tracefunc func, PyObject *obj,
4290 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 int result;
4294 if (tstate->tracing)
4295 return 0;
4296 tstate->tracing++;
4297 tstate->use_tracing = 0;
4298 result = func(obj, frame, what, arg);
4299 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4300 || (tstate->c_profilefunc != NULL));
4301 tstate->tracing--;
4302 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004303}
4304
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004305PyObject *
4306_PyEval_CallTracing(PyObject *func, PyObject *args)
4307{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004308 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 int save_tracing = tstate->tracing;
4310 int save_use_tracing = tstate->use_tracing;
4311 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004313 tstate->tracing = 0;
4314 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4315 || (tstate->c_profilefunc != NULL));
4316 result = PyObject_Call(func, args, NULL);
4317 tstate->tracing = save_tracing;
4318 tstate->use_tracing = save_use_tracing;
4319 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004320}
4321
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004322/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004323static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004324maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004325 PyThreadState *tstate, PyFrameObject *frame,
4326 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 int result = 0;
4329 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 /* If the last instruction executed isn't in the current
4332 instruction window, reset the window.
4333 */
4334 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4335 PyAddrPair bounds;
4336 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4337 &bounds);
4338 *instr_lb = bounds.ap_lower;
4339 *instr_ub = bounds.ap_upper;
4340 }
4341 /* If the last instruction falls at the start of a line or if
4342 it represents a jump backwards, update the frame's line
4343 number and call the trace function. */
4344 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4345 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004346 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 }
4348 *instr_prev = frame->f_lasti;
4349 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004350}
4351
Fred Drake5755ce62001-06-27 19:19:46 +00004352void
4353PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004354{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 PyThreadState *tstate = PyThreadState_GET();
4356 PyObject *temp = tstate->c_profileobj;
4357 Py_XINCREF(arg);
4358 tstate->c_profilefunc = NULL;
4359 tstate->c_profileobj = NULL;
4360 /* Must make sure that tracing is not ignored if 'temp' is freed */
4361 tstate->use_tracing = tstate->c_tracefunc != NULL;
4362 Py_XDECREF(temp);
4363 tstate->c_profilefunc = func;
4364 tstate->c_profileobj = arg;
4365 /* Flag that tracing or profiling is turned on */
4366 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004367}
4368
4369void
4370PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 PyThreadState *tstate = PyThreadState_GET();
4373 PyObject *temp = tstate->c_traceobj;
4374 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4375 Py_XINCREF(arg);
4376 tstate->c_tracefunc = NULL;
4377 tstate->c_traceobj = NULL;
4378 /* Must make sure that profiling is not ignored if 'temp' is freed */
4379 tstate->use_tracing = tstate->c_profilefunc != NULL;
4380 Py_XDECREF(temp);
4381 tstate->c_tracefunc = func;
4382 tstate->c_traceobj = arg;
4383 /* Flag that tracing or profiling is turned on */
4384 tstate->use_tracing = ((func != NULL)
4385 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004386}
4387
Yury Selivanov75445082015-05-11 22:57:16 -04004388void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004389_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004390{
4391 PyThreadState *tstate = PyThreadState_GET();
4392
4393 Py_CLEAR(tstate->coroutine_wrapper);
4394
4395 Py_XINCREF(wrapper);
4396 tstate->coroutine_wrapper = wrapper;
4397}
4398
4399PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004400_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004401{
4402 PyThreadState *tstate = PyThreadState_GET();
4403 return tstate->coroutine_wrapper;
4404}
4405
Guido van Rossumb209a111997-04-29 18:18:01 +00004406PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004407PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 PyFrameObject *current_frame = PyEval_GetFrame();
4410 if (current_frame == NULL)
4411 return PyThreadState_GET()->interp->builtins;
4412 else
4413 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004414}
4415
Guido van Rossumb209a111997-04-29 18:18:01 +00004416PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004417PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004418{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004420 if (current_frame == NULL) {
4421 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004422 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004423 }
4424
4425 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4426 return NULL;
4427
4428 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004430}
4431
Guido van Rossumb209a111997-04-29 18:18:01 +00004432PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004433PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004434{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004435 PyFrameObject *current_frame = PyEval_GetFrame();
4436 if (current_frame == NULL)
4437 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004438
4439 assert(current_frame->f_globals != NULL);
4440 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004441}
4442
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004443PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004444PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 PyThreadState *tstate = PyThreadState_GET();
4447 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004448}
4449
Guido van Rossum6135a871995-01-09 17:53:26 +00004450int
Tim Peters5ba58662001-07-16 02:29:45 +00004451PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 PyFrameObject *current_frame = PyEval_GetFrame();
4454 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 if (current_frame != NULL) {
4457 const int codeflags = current_frame->f_code->co_flags;
4458 const int compilerflags = codeflags & PyCF_MASK;
4459 if (compilerflags) {
4460 result = 1;
4461 cf->cf_flags |= compilerflags;
4462 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004463#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 if (codeflags & CO_GENERATOR_ALLOWED) {
4465 result = 1;
4466 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4467 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004468#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 }
4470 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004471}
4472
Guido van Rossum3f5da241990-12-20 15:06:42 +00004473
Guido van Rossum681d79a1995-07-18 14:51:37 +00004474/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004475 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004476
Guido van Rossumb209a111997-04-29 18:18:01 +00004477PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004478PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004479{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004481
Victor Stinner59b356d2015-03-16 11:52:32 +01004482#ifdef Py_DEBUG
4483 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4484 set. It raises a new exception if parameters are invalid or if
4485 PyTuple_New() fails, and so the original exception is lost. */
4486 assert(!PyErr_Occurred());
4487#endif
4488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 if (arg == NULL) {
4490 arg = PyTuple_New(0);
4491 if (arg == NULL)
4492 return NULL;
4493 }
4494 else if (!PyTuple_Check(arg)) {
4495 PyErr_SetString(PyExc_TypeError,
4496 "argument list must be a tuple");
4497 return NULL;
4498 }
4499 else
4500 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 if (kw != NULL && !PyDict_Check(kw)) {
4503 PyErr_SetString(PyExc_TypeError,
4504 "keyword list must be a dictionary");
4505 Py_DECREF(arg);
4506 return NULL;
4507 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 result = PyObject_Call(func, arg, kw);
4510 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004512 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004513}
4514
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004515const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004516PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004518 if (PyMethod_Check(func))
4519 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4520 else if (PyFunction_Check(func))
4521 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4522 else if (PyCFunction_Check(func))
4523 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4524 else
4525 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004526}
4527
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004528const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004529PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 if (PyMethod_Check(func))
4532 return "()";
4533 else if (PyFunction_Check(func))
4534 return "()";
4535 else if (PyCFunction_Check(func))
4536 return "()";
4537 else
4538 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004539}
4540
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004541static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004542err_args(PyObject *func, int flags, int nargs)
4543{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 if (flags & METH_NOARGS)
4545 PyErr_Format(PyExc_TypeError,
4546 "%.200s() takes no arguments (%d given)",
4547 ((PyCFunctionObject *)func)->m_ml->ml_name,
4548 nargs);
4549 else
4550 PyErr_Format(PyExc_TypeError,
4551 "%.200s() takes exactly one argument (%d given)",
4552 ((PyCFunctionObject *)func)->m_ml->ml_name,
4553 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004554}
4555
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004556#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004557if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004558 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4559 tstate, tstate->frame, \
4560 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 x = NULL; \
4562 } \
4563 else { \
4564 x = call; \
4565 if (tstate->c_profilefunc != NULL) { \
4566 if (x == NULL) { \
4567 call_trace_protected(tstate->c_profilefunc, \
4568 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004569 tstate, tstate->frame, \
4570 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 /* XXX should pass (type, value, tb) */ \
4572 } else { \
4573 if (call_trace(tstate->c_profilefunc, \
4574 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004575 tstate, tstate->frame, \
4576 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 Py_DECREF(x); \
4578 x = NULL; \
4579 } \
4580 } \
4581 } \
4582 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004583} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 x = call; \
4585 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004586
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004587static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004588call_function(PyObject ***pp_stack, int oparg
4589#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004590 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004591#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 int na = oparg & 0xff;
4595 int nk = (oparg>>8) & 0xff;
4596 int n = na + 2 * nk;
4597 PyObject **pfunc = (*pp_stack) - n - 1;
4598 PyObject *func = *pfunc;
4599 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 /* Always dispatch PyCFunction first, because these are
4602 presumed to be the most frequent callable object.
4603 */
4604 if (PyCFunction_Check(func) && nk == 0) {
4605 int flags = PyCFunction_GET_FLAGS(func);
4606 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 PCALL(PCALL_CFUNCTION);
4609 if (flags & (METH_NOARGS | METH_O)) {
4610 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4611 PyObject *self = PyCFunction_GET_SELF(func);
4612 if (flags & METH_NOARGS && na == 0) {
4613 C_TRACE(x, (*meth)(self,NULL));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004614
Victor Stinnerefde1462015-03-21 15:04:43 +01004615 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 }
4617 else if (flags & METH_O && na == 1) {
4618 PyObject *arg = EXT_POP(*pp_stack);
4619 C_TRACE(x, (*meth)(self,arg));
4620 Py_DECREF(arg);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004621
Victor Stinnerefde1462015-03-21 15:04:43 +01004622 x = _Py_CheckFunctionResult(func, x, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 }
4624 else {
4625 err_args(func, flags, na);
4626 x = NULL;
4627 }
4628 }
4629 else {
4630 PyObject *callargs;
4631 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004632 if (callargs != NULL) {
4633 READ_TIMESTAMP(*pintr0);
4634 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4635 READ_TIMESTAMP(*pintr1);
4636 Py_XDECREF(callargs);
4637 }
4638 else {
4639 x = NULL;
4640 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004641 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004642 }
4643 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004644 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4645 /* optimize access to bound methods */
4646 PyObject *self = PyMethod_GET_SELF(func);
4647 PCALL(PCALL_METHOD);
4648 PCALL(PCALL_BOUND_METHOD);
4649 Py_INCREF(self);
4650 func = PyMethod_GET_FUNCTION(func);
4651 Py_INCREF(func);
4652 Py_DECREF(*pfunc);
4653 *pfunc = self;
4654 na++;
4655 n++;
4656 } else
4657 Py_INCREF(func);
4658 READ_TIMESTAMP(*pintr0);
4659 if (PyFunction_Check(func))
4660 x = fast_function(func, pp_stack, n, na, nk);
4661 else
4662 x = do_call(func, pp_stack, na, nk);
4663 READ_TIMESTAMP(*pintr1);
4664 Py_DECREF(func);
Victor Stinner4a7cc882015-03-06 23:35:27 +01004665
4666 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004667 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 /* Clear the stack of the function object. Also removes
4670 the arguments in case they weren't consumed already
4671 (fast_function() and err_args() leave them on the stack).
4672 */
4673 while ((*pp_stack) > pfunc) {
4674 w = EXT_POP(*pp_stack);
4675 Py_DECREF(w);
4676 PCALL(PCALL_POP);
4677 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004678
Victor Stinner4a7cc882015-03-06 23:35:27 +01004679 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004681}
4682
Jeremy Hylton192690e2002-08-16 18:36:11 +00004683/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004684 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004685 For the simplest case -- a function that takes only positional
4686 arguments and is called with only positional arguments -- it
4687 inlines the most primitive frame setup code from
4688 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4689 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004690*/
4691
4692static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004693fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4696 PyObject *globals = PyFunction_GET_GLOBALS(func);
4697 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4698 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Victor Stinner40ee3012014-06-16 15:59:28 +02004699 PyObject *name = ((PyFunctionObject *)func) -> func_name;
4700 PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 PyObject **d = NULL;
4702 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 PCALL(PCALL_FUNCTION);
4705 PCALL(PCALL_FAST_FUNCTION);
4706 if (argdefs == NULL && co->co_argcount == n &&
4707 co->co_kwonlyargcount == 0 && nk==0 &&
4708 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4709 PyFrameObject *f;
4710 PyObject *retval = NULL;
4711 PyThreadState *tstate = PyThreadState_GET();
4712 PyObject **fastlocals, **stack;
4713 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004715 PCALL(PCALL_FASTER_FUNCTION);
4716 assert(globals != NULL);
4717 /* XXX Perhaps we should create a specialized
4718 PyFrame_New() that doesn't take locals, but does
4719 take builtins without sanity checking them.
4720 */
4721 assert(tstate != NULL);
4722 f = PyFrame_New(tstate, co, globals, NULL);
4723 if (f == NULL)
4724 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 fastlocals = f->f_localsplus;
4727 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 for (i = 0; i < n; i++) {
4730 Py_INCREF(*stack);
4731 fastlocals[i] = *stack++;
4732 }
4733 retval = PyEval_EvalFrameEx(f,0);
4734 ++tstate->recursion_depth;
4735 Py_DECREF(f);
4736 --tstate->recursion_depth;
4737 return retval;
4738 }
4739 if (argdefs != NULL) {
4740 d = &PyTuple_GET_ITEM(argdefs, 0);
4741 nd = Py_SIZE(argdefs);
4742 }
Victor Stinner40ee3012014-06-16 15:59:28 +02004743 return _PyEval_EvalCodeWithName((PyObject*)co, globals,
4744 (PyObject *)NULL, (*pp_stack)-n, na,
4745 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4746 PyFunction_GET_CLOSURE(func),
4747 name, qualname);
Jeremy Hylton52820442001-01-03 23:52:36 +00004748}
4749
4750static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004751update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4752 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 PyObject *kwdict = NULL;
4755 if (orig_kwdict == NULL)
4756 kwdict = PyDict_New();
4757 else {
4758 kwdict = PyDict_Copy(orig_kwdict);
4759 Py_DECREF(orig_kwdict);
4760 }
4761 if (kwdict == NULL)
4762 return NULL;
4763 while (--nk >= 0) {
4764 int err;
4765 PyObject *value = EXT_POP(*pp_stack);
4766 PyObject *key = EXT_POP(*pp_stack);
4767 if (PyDict_GetItem(kwdict, key) != NULL) {
4768 PyErr_Format(PyExc_TypeError,
4769 "%.200s%s got multiple values "
4770 "for keyword argument '%U'",
4771 PyEval_GetFuncName(func),
4772 PyEval_GetFuncDesc(func),
4773 key);
4774 Py_DECREF(key);
4775 Py_DECREF(value);
4776 Py_DECREF(kwdict);
4777 return NULL;
4778 }
4779 err = PyDict_SetItem(kwdict, key, value);
4780 Py_DECREF(key);
4781 Py_DECREF(value);
4782 if (err) {
4783 Py_DECREF(kwdict);
4784 return NULL;
4785 }
4786 }
4787 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004788}
4789
4790static PyObject *
4791update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004794 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004796 callargs = PyTuple_New(nstack + nstar);
4797 if (callargs == NULL) {
4798 return NULL;
4799 }
4800 if (nstar) {
4801 int i;
4802 for (i = 0; i < nstar; i++) {
4803 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4804 Py_INCREF(a);
4805 PyTuple_SET_ITEM(callargs, nstack + i, a);
4806 }
4807 }
4808 while (--nstack >= 0) {
4809 w = EXT_POP(*pp_stack);
4810 PyTuple_SET_ITEM(callargs, nstack, w);
4811 }
4812 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004813}
4814
4815static PyObject *
4816load_args(PyObject ***pp_stack, int na)
4817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004818 PyObject *args = PyTuple_New(na);
4819 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 if (args == NULL)
4822 return NULL;
4823 while (--na >= 0) {
4824 w = EXT_POP(*pp_stack);
4825 PyTuple_SET_ITEM(args, na, w);
4826 }
4827 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004828}
4829
4830static PyObject *
4831do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4832{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004833 PyObject *callargs = NULL;
4834 PyObject *kwdict = NULL;
4835 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004836
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004837 if (nk > 0) {
4838 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4839 if (kwdict == NULL)
4840 goto call_fail;
4841 }
4842 callargs = load_args(pp_stack, na);
4843 if (callargs == NULL)
4844 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004845#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004846 /* At this point, we have to look at the type of func to
4847 update the call stats properly. Do it here so as to avoid
4848 exposing the call stats machinery outside ceval.c
4849 */
4850 if (PyFunction_Check(func))
4851 PCALL(PCALL_FUNCTION);
4852 else if (PyMethod_Check(func))
4853 PCALL(PCALL_METHOD);
4854 else if (PyType_Check(func))
4855 PCALL(PCALL_TYPE);
4856 else if (PyCFunction_Check(func))
4857 PCALL(PCALL_CFUNCTION);
4858 else
4859 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004860#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004861 if (PyCFunction_Check(func)) {
4862 PyThreadState *tstate = PyThreadState_GET();
4863 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4864 }
4865 else
4866 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004867call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004868 Py_XDECREF(callargs);
4869 Py_XDECREF(kwdict);
4870 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004871}
4872
4873static PyObject *
4874ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 int nstar = 0;
4877 PyObject *callargs = NULL;
4878 PyObject *stararg = NULL;
4879 PyObject *kwdict = NULL;
4880 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 if (flags & CALL_FLAG_KW) {
4883 kwdict = EXT_POP(*pp_stack);
4884 if (!PyDict_Check(kwdict)) {
4885 PyObject *d;
4886 d = PyDict_New();
4887 if (d == NULL)
4888 goto ext_call_fail;
4889 if (PyDict_Update(d, kwdict) != 0) {
4890 Py_DECREF(d);
4891 /* PyDict_Update raises attribute
4892 * error (percolated from an attempt
4893 * to get 'keys' attribute) instead of
4894 * a type error if its second argument
4895 * is not a mapping.
4896 */
4897 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4898 PyErr_Format(PyExc_TypeError,
4899 "%.200s%.200s argument after ** "
4900 "must be a mapping, not %.200s",
4901 PyEval_GetFuncName(func),
4902 PyEval_GetFuncDesc(func),
4903 kwdict->ob_type->tp_name);
4904 }
4905 goto ext_call_fail;
4906 }
4907 Py_DECREF(kwdict);
4908 kwdict = d;
4909 }
4910 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004911 if (nk > 0) {
4912 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4913 if (kwdict == NULL)
4914 goto ext_call_fail;
4915 }
4916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004917 if (flags & CALL_FLAG_VAR) {
4918 stararg = EXT_POP(*pp_stack);
4919 if (!PyTuple_Check(stararg)) {
4920 PyObject *t = NULL;
4921 t = PySequence_Tuple(stararg);
4922 if (t == NULL) {
4923 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4924 PyErr_Format(PyExc_TypeError,
4925 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004926 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004927 PyEval_GetFuncName(func),
4928 PyEval_GetFuncDesc(func),
4929 stararg->ob_type->tp_name);
4930 }
4931 goto ext_call_fail;
4932 }
4933 Py_DECREF(stararg);
4934 stararg = t;
4935 }
4936 nstar = PyTuple_GET_SIZE(stararg);
4937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 callargs = update_star_args(na, nstar, stararg, pp_stack);
4939 if (callargs == NULL)
4940 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004941#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 /* At this point, we have to look at the type of func to
4943 update the call stats properly. Do it here so as to avoid
4944 exposing the call stats machinery outside ceval.c
4945 */
4946 if (PyFunction_Check(func))
4947 PCALL(PCALL_FUNCTION);
4948 else if (PyMethod_Check(func))
4949 PCALL(PCALL_METHOD);
4950 else if (PyType_Check(func))
4951 PCALL(PCALL_TYPE);
4952 else if (PyCFunction_Check(func))
4953 PCALL(PCALL_CFUNCTION);
4954 else
4955 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004956#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 if (PyCFunction_Check(func)) {
4958 PyThreadState *tstate = PyThreadState_GET();
4959 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4960 }
4961 else
4962 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004963ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004964 Py_XDECREF(callargs);
4965 Py_XDECREF(kwdict);
4966 Py_XDECREF(stararg);
4967 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004968}
4969
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004970/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004971 nb_index slot defined, and store in *pi.
4972 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4973 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 +00004974 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004975*/
Tim Petersb5196382001-12-16 19:44:20 +00004976/* Note: If v is NULL, return success without storing into *pi. This
4977 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4978 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004979*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004980int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004981_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004983 if (v != NULL) {
4984 Py_ssize_t x;
4985 if (PyIndex_Check(v)) {
4986 x = PyNumber_AsSsize_t(v, NULL);
4987 if (x == -1 && PyErr_Occurred())
4988 return 0;
4989 }
4990 else {
4991 PyErr_SetString(PyExc_TypeError,
4992 "slice indices must be integers or "
4993 "None or have an __index__ method");
4994 return 0;
4995 }
4996 *pi = x;
4997 }
4998 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004999}
5000
Guido van Rossum486364b2007-06-30 05:01:58 +00005001#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005002 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00005003
Guido van Rossumb209a111997-04-29 18:18:01 +00005004static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02005005cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005007 int res = 0;
5008 switch (op) {
5009 case PyCmp_IS:
5010 res = (v == w);
5011 break;
5012 case PyCmp_IS_NOT:
5013 res = (v != w);
5014 break;
5015 case PyCmp_IN:
5016 res = PySequence_Contains(w, v);
5017 if (res < 0)
5018 return NULL;
5019 break;
5020 case PyCmp_NOT_IN:
5021 res = PySequence_Contains(w, v);
5022 if (res < 0)
5023 return NULL;
5024 res = !res;
5025 break;
5026 case PyCmp_EXC_MATCH:
5027 if (PyTuple_Check(w)) {
5028 Py_ssize_t i, length;
5029 length = PyTuple_Size(w);
5030 for (i = 0; i < length; i += 1) {
5031 PyObject *exc = PyTuple_GET_ITEM(w, i);
5032 if (!PyExceptionClass_Check(exc)) {
5033 PyErr_SetString(PyExc_TypeError,
5034 CANNOT_CATCH_MSG);
5035 return NULL;
5036 }
5037 }
5038 }
5039 else {
5040 if (!PyExceptionClass_Check(w)) {
5041 PyErr_SetString(PyExc_TypeError,
5042 CANNOT_CATCH_MSG);
5043 return NULL;
5044 }
5045 }
5046 res = PyErr_GivenExceptionMatches(v, w);
5047 break;
5048 default:
5049 return PyObject_RichCompare(v, w, op);
5050 }
5051 v = res ? Py_True : Py_False;
5052 Py_INCREF(v);
5053 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005054}
5055
Thomas Wouters52152252000-08-17 22:55:00 +00005056static PyObject *
5057import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005059 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02005060 _Py_IDENTIFIER(__name__);
5061 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005062
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005063 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005064 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
5065 return x;
5066 /* Issue #17636: in case this failed because of a circular relative
5067 import, try to fallback on reading the module directly from
5068 sys.modules. */
5069 PyErr_Clear();
5070 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
5071 if (pkgname == NULL)
5072 return NULL;
5073 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
5074 Py_DECREF(pkgname);
5075 if (fullmodname == NULL)
5076 return NULL;
5077 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
5078 if (x == NULL)
Brett Cannona79e4fb2013-07-12 11:22:26 -04005079 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005080 else
5081 Py_INCREF(x);
5082 Py_DECREF(fullmodname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005083 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00005084}
Guido van Rossumac7be682001-01-17 15:42:30 +00005085
Thomas Wouters52152252000-08-17 22:55:00 +00005086static int
5087import_all_from(PyObject *locals, PyObject *v)
5088{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005089 _Py_IDENTIFIER(__all__);
5090 _Py_IDENTIFIER(__dict__);
5091 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 PyObject *dict, *name, *value;
5093 int skip_leading_underscores = 0;
5094 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005096 if (all == NULL) {
5097 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5098 return -1; /* Unexpected error */
5099 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005100 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005101 if (dict == NULL) {
5102 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
5103 return -1;
5104 PyErr_SetString(PyExc_ImportError,
5105 "from-import-* object has no __dict__ and no __all__");
5106 return -1;
5107 }
5108 all = PyMapping_Keys(dict);
5109 Py_DECREF(dict);
5110 if (all == NULL)
5111 return -1;
5112 skip_leading_underscores = 1;
5113 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005114
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005115 for (pos = 0, err = 0; ; pos++) {
5116 name = PySequence_GetItem(all, pos);
5117 if (name == NULL) {
5118 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5119 err = -1;
5120 else
5121 PyErr_Clear();
5122 break;
5123 }
5124 if (skip_leading_underscores &&
5125 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02005126 PyUnicode_READY(name) != -1 &&
5127 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005128 {
5129 Py_DECREF(name);
5130 continue;
5131 }
5132 value = PyObject_GetAttr(v, name);
5133 if (value == NULL)
5134 err = -1;
5135 else if (PyDict_CheckExact(locals))
5136 err = PyDict_SetItem(locals, name, value);
5137 else
5138 err = PyObject_SetItem(locals, name, value);
5139 Py_DECREF(name);
5140 Py_XDECREF(value);
5141 if (err != 0)
5142 break;
5143 }
5144 Py_DECREF(all);
5145 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005146}
5147
Guido van Rossumac7be682001-01-17 15:42:30 +00005148static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005149format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005151 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 if (!obj)
5154 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005156 obj_str = _PyUnicode_AsString(obj);
5157 if (!obj_str)
5158 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005159
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005160 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005161}
Guido van Rossum950361c1997-01-24 13:49:28 +00005162
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005163static void
5164format_exc_unbound(PyCodeObject *co, int oparg)
5165{
5166 PyObject *name;
5167 /* Don't stomp existing exception */
5168 if (PyErr_Occurred())
5169 return;
5170 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5171 name = PyTuple_GET_ITEM(co->co_cellvars,
5172 oparg);
5173 format_exc_check_arg(
5174 PyExc_UnboundLocalError,
5175 UNBOUNDLOCAL_ERROR_MSG,
5176 name);
5177 } else {
5178 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5179 PyTuple_GET_SIZE(co->co_cellvars));
5180 format_exc_check_arg(PyExc_NameError,
5181 UNBOUNDFREE_ERROR_MSG, name);
5182 }
5183}
5184
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005185static PyObject *
5186unicode_concatenate(PyObject *v, PyObject *w,
5187 PyFrameObject *f, unsigned char *next_instr)
5188{
5189 PyObject *res;
5190 if (Py_REFCNT(v) == 2) {
5191 /* In the common case, there are 2 references to the value
5192 * stored in 'variable' when the += is performed: one on the
5193 * value stack (in 'v') and one still stored in the
5194 * 'variable'. We try to delete the variable now to reduce
5195 * the refcnt to 1.
5196 */
5197 switch (*next_instr) {
5198 case STORE_FAST:
5199 {
5200 int oparg = PEEKARG();
5201 PyObject **fastlocals = f->f_localsplus;
5202 if (GETLOCAL(oparg) == v)
5203 SETLOCAL(oparg, NULL);
5204 break;
5205 }
5206 case STORE_DEREF:
5207 {
5208 PyObject **freevars = (f->f_localsplus +
5209 f->f_code->co_nlocals);
5210 PyObject *c = freevars[PEEKARG()];
5211 if (PyCell_GET(c) == v)
5212 PyCell_Set(c, NULL);
5213 break;
5214 }
5215 case STORE_NAME:
5216 {
5217 PyObject *names = f->f_code->co_names;
5218 PyObject *name = GETITEM(names, PEEKARG());
5219 PyObject *locals = f->f_locals;
5220 if (PyDict_CheckExact(locals) &&
5221 PyDict_GetItem(locals, name) == v) {
5222 if (PyDict_DelItem(locals, name) != 0) {
5223 PyErr_Clear();
5224 }
5225 }
5226 break;
5227 }
5228 }
5229 }
5230 res = v;
5231 PyUnicode_Append(&res, w);
5232 return res;
5233}
5234
Yury Selivanoveb698fe2015-06-02 22:30:31 -04005235static PyObject *
5236apply_coroutine_wrapper(PyObject *gen)
5237{
5238 PyObject *wrapped;
5239 PyThreadState *tstate = PyThreadState_GET();
5240 PyObject *wrapper = tstate->coroutine_wrapper;
5241
5242 if (tstate->in_coroutine_wrapper) {
5243 assert(wrapper != NULL);
5244 PyErr_Format(PyExc_RuntimeError,
5245 "coroutine wrapper %.200R attempted "
5246 "to recursively wrap %.200R",
5247 wrapper,
5248 gen);
5249 return NULL;
5250 }
5251
5252 if (wrapper == NULL) {
5253 return gen;
5254 }
5255
5256 tstate->in_coroutine_wrapper = 1;
5257 wrapped = PyObject_CallFunction(wrapper, "N", gen);
5258 tstate->in_coroutine_wrapper = 0;
5259 return wrapped;
5260}
5261
Guido van Rossum950361c1997-01-24 13:49:28 +00005262#ifdef DYNAMIC_EXECUTION_PROFILE
5263
Skip Montanarof118cb12001-10-15 20:51:38 +00005264static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005265getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005266{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005267 int i;
5268 PyObject *l = PyList_New(256);
5269 if (l == NULL) return NULL;
5270 for (i = 0; i < 256; i++) {
5271 PyObject *x = PyLong_FromLong(a[i]);
5272 if (x == NULL) {
5273 Py_DECREF(l);
5274 return NULL;
5275 }
5276 PyList_SetItem(l, i, x);
5277 }
5278 for (i = 0; i < 256; i++)
5279 a[i] = 0;
5280 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005281}
5282
5283PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005284_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005285{
5286#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005287 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005288#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005289 int i;
5290 PyObject *l = PyList_New(257);
5291 if (l == NULL) return NULL;
5292 for (i = 0; i < 257; i++) {
5293 PyObject *x = getarray(dxpairs[i]);
5294 if (x == NULL) {
5295 Py_DECREF(l);
5296 return NULL;
5297 }
5298 PyList_SetItem(l, i, x);
5299 }
5300 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005301#endif
5302}
5303
5304#endif