blob: 4b1d6ca9697b61ffe39f2cf1d5c95a03c1926ad7 [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"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000017#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018
Guido van Rossumc6004111993-11-05 10:22:19 +000019#include <ctype.h>
20
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000022
23#define READ_TIMESTAMP(var)
24
25#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000026
27typedef unsigned long long uint64;
28
Ezio Melotti13925002011-03-16 11:05:33 +020029/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000030 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
31 "__powerpc__" appears to be the correct one for Linux with GCC
32*/
33#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020040 unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100126static int call_trace(Py_tracefunc, PyObject *,
127 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000129static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100130 PyThreadState *, PyFrameObject *,
131 int, PyObject *);
132static void call_exc_trace(Py_tracefunc, PyObject *,
133 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000134static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100135 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000136
Thomas Wouters477c8d52006-05-27 19:21:47 +0000137static PyObject * cmp_outcome(int, PyObject *, PyObject *);
138static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000139static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000140static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000141static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200142static PyObject * unicode_concatenate(PyObject *, PyObject *,
143 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500144static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000145
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000148#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000150#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 "free variable '%.200s' referenced before assignment" \
152 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000153
Guido van Rossum950361c1997-01-24 13:49:28 +0000154/* Dynamic execution profile */
155#ifdef DYNAMIC_EXECUTION_PROFILE
156#ifdef DXPAIRS
157static long dxpairs[257][256];
158#define dxp dxpairs[256]
159#else
160static long dxp[256];
161#endif
162#endif
163
Jeremy Hylton985eba52003-02-05 23:13:00 +0000164/* Function call profile */
165#ifdef CALL_PROFILE
166#define PCALL_NUM 11
167static int pcall[PCALL_NUM];
168
169#define PCALL_ALL 0
170#define PCALL_FUNCTION 1
171#define PCALL_FAST_FUNCTION 2
172#define PCALL_FASTER_FUNCTION 3
173#define PCALL_METHOD 4
174#define PCALL_BOUND_METHOD 5
175#define PCALL_CFUNCTION 6
176#define PCALL_TYPE 7
177#define PCALL_GENERATOR 8
178#define PCALL_OTHER 9
179#define PCALL_POP 10
180
181/* Notes about the statistics
182
183 PCALL_FAST stats
184
185 FAST_FUNCTION means no argument tuple needs to be created.
186 FASTER_FUNCTION means that the fast-path frame setup code is used.
187
188 If there is a method call where the call can be optimized by changing
189 the argument tuple and calling the function directly, it gets recorded
190 twice.
191
192 As a result, the relationship among the statistics appears to be
193 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
194 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
195 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
196 PCALL_METHOD > PCALL_BOUND_METHOD
197*/
198
199#define PCALL(POS) pcall[POS]++
200
201PyObject *
202PyEval_GetCallStats(PyObject *self)
203{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000204 return Py_BuildValue("iiiiiiiiiii",
205 pcall[0], pcall[1], pcall[2], pcall[3],
206 pcall[4], pcall[5], pcall[6], pcall[7],
207 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000208}
209#else
210#define PCALL(O)
211
212PyObject *
213PyEval_GetCallStats(PyObject *self)
214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 Py_INCREF(Py_None);
216 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000217}
218#endif
219
Tim Peters5ca576e2001-06-18 22:08:13 +0000220
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000221#ifdef WITH_THREAD
222#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
223#else
224#define GIL_REQUEST 0
225#endif
226
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000227/* This can set eval_breaker to 0 even though gil_drop_request became
228 1. We believe this is all right because the eval loop will release
229 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000230#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 _Py_atomic_store_relaxed( \
232 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000233 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
235 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000236
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000237#ifdef WITH_THREAD
238
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000239#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000240 do { \
241 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
242 _Py_atomic_store_relaxed(&eval_breaker, 1); \
243 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000244
245#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 do { \
247 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
248 COMPUTE_EVAL_BREAKER(); \
249 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000250
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000251#endif
252
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000253/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 do { \
256 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
257 _Py_atomic_store_relaxed(&eval_breaker, 1); \
258 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259
260#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 do { \
262 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
263 COMPUTE_EVAL_BREAKER(); \
264 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000265
266#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 do { \
268 pending_async_exc = 1; \
269 _Py_atomic_store_relaxed(&eval_breaker, 1); \
270 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000271
272#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000274
275
Guido van Rossume59214e1994-08-30 08:01:59 +0000276#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000277
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000278#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000279#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000280#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000281#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000282
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000283static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000284static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000285/* This single variable consolidates all requests to break out of the fast path
286 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000287static _Py_atomic_int eval_breaker = {0};
288/* Request for dropping the GIL */
289static _Py_atomic_int gil_drop_request = {0};
290/* Request for running pending calls. */
291static _Py_atomic_int pendingcalls_to_do = {0};
292/* Request for looking at the `async_exc` field of the current thread state.
293 Guarded by the GIL. */
294static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000295
296#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000297
Tim Peters7f468f22004-10-11 02:40:51 +0000298int
299PyEval_ThreadsInitialized(void)
300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000302}
303
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 if (gil_created())
308 return;
309 create_gil();
310 take_gil(PyThreadState_GET());
311 main_thread = PyThread_get_thread_ident();
312 if (!pending_lock)
313 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000314}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000315
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000316void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000317_PyEval_FiniThreads(void)
318{
319 if (!gil_created())
320 return;
321 destroy_gil();
322 assert(!gil_created());
323}
324
325void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PyThreadState *tstate = PyThreadState_GET();
329 if (tstate == NULL)
330 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
331 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000332}
333
334void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000335PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* This function must succeed when the current thread state is NULL.
338 We therefore avoid PyThreadState_GET() which dumps a fatal error
339 in debug mode.
340 */
341 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
342 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000343}
344
345void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000346PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000347{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 if (tstate == NULL)
349 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
350 /* Check someone has called PyEval_InitThreads() to create the lock */
351 assert(gil_created());
352 take_gil(tstate);
353 if (PyThreadState_Swap(tstate) != NULL)
354 Py_FatalError(
355 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000356}
357
358void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000359PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000360{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 if (tstate == NULL)
362 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
363 if (PyThreadState_Swap(NULL) != tstate)
364 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
365 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000366}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000367
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200368/* This function is called from PyOS_AfterFork to destroy all threads which are
369 * not running in the child process, and clear internal locks which might be
370 * held by those threads. (This could also be done using pthread_atfork
371 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000372
373void
374PyEval_ReInitThreads(void)
375{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200376 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200378 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 if (!gil_created())
381 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 recreate_gil();
383 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200384 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 /* Update the threading module with the new state.
388 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200389 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 "threading");
391 if (threading == NULL) {
392 /* threading not imported */
393 PyErr_Clear();
394 return;
395 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200396 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (result == NULL)
398 PyErr_WriteUnraisable(threading);
399 else
400 Py_DECREF(result);
401 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200402
403 /* Destroy all threads except the current one */
404 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000405}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000406
407#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000408static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000409static int pending_async_exc = 0;
410#endif /* WITH_THREAD */
411
412/* This function is used to signal that async exceptions are waiting to be
413 raised, therefore it is also useful in non-threaded builds. */
414
415void
416_PyEval_SignalAsyncExc(void)
417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000418 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000419}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000420
Guido van Rossumff4949e1992-08-05 19:58:53 +0000421/* Functions save_thread and restore_thread are always defined so
422 dynamically loaded modules needn't be compiled separately for use
423 with and without threads: */
424
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000425PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000426PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 PyThreadState *tstate = PyThreadState_Swap(NULL);
429 if (tstate == NULL)
430 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000431#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 if (gil_created())
433 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000434#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436}
437
438void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000439PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (tstate == NULL)
442 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000443#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 if (gil_created()) {
445 int err = errno;
446 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200447 /* _Py_Finalizing is protected by the GIL */
448 if (_Py_Finalizing && tstate != _Py_Finalizing) {
449 drop_gil(tstate);
450 PyThread_exit_thread();
451 assert(0); /* unreachable */
452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 errno = err;
454 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000455#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000457}
458
459
Guido van Rossuma9672091994-09-14 13:31:22 +0000460/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
461 signal handlers or Mac I/O completion routines) can schedule calls
462 to a function to be called synchronously.
463 The synchronous function is called with one void* argument.
464 It should return 0 for success or -1 for failure -- failure should
465 be accompanied by an exception.
466
467 If registry succeeds, the registry function returns 0; if it fails
468 (e.g. due to too many pending calls) it returns -1 (without setting
469 an exception condition).
470
471 Note that because registry may occur from within signal handlers,
472 or other asynchronous events, calling malloc() is unsafe!
473
474#ifdef WITH_THREAD
475 Any thread can schedule pending calls, but only the main thread
476 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000477 There is no facility to schedule calls to a particular thread, but
478 that should be easy to change, should that ever be required. In
479 that case, the static variables here should go into the python
480 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000481#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000482*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000483
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484#ifdef WITH_THREAD
485
486/* The WITH_THREAD implementation is thread-safe. It allows
487 scheduling to be made from any thread, and even from an executing
488 callback.
489 */
490
491#define NPENDINGCALLS 32
492static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 int (*func)(void *);
494 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495} pendingcalls[NPENDINGCALLS];
496static int pendingfirst = 0;
497static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498
499int
500Py_AddPendingCall(int (*func)(void *), void *arg)
501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 int i, j, result=0;
503 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 /* try a few times for the lock. Since this mechanism is used
506 * for signal handling (on the main thread), there is a (slim)
507 * chance that a signal is delivered on the same thread while we
508 * hold the lock during the Py_MakePendingCalls() function.
509 * This avoids a deadlock in that case.
510 * Note that signals can be delivered on any thread. In particular,
511 * on Windows, a SIGINT is delivered on a system-created worker
512 * thread.
513 * We also check for lock being NULL, in the unlikely case that
514 * this function is called before any bytecode evaluation takes place.
515 */
516 if (lock != NULL) {
517 for (i = 0; i<100; i++) {
518 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
519 break;
520 }
521 if (i == 100)
522 return -1;
523 }
524
525 i = pendinglast;
526 j = (i + 1) % NPENDINGCALLS;
527 if (j == pendingfirst) {
528 result = -1; /* Queue full */
529 } else {
530 pendingcalls[i].func = func;
531 pendingcalls[i].arg = arg;
532 pendinglast = j;
533 }
534 /* signal main loop */
535 SIGNAL_PENDING_CALLS();
536 if (lock != NULL)
537 PyThread_release_lock(lock);
538 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000539}
540
541int
542Py_MakePendingCalls(void)
543{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200544 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 int i;
546 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 if (!pending_lock) {
549 /* initial allocation of the lock */
550 pending_lock = PyThread_allocate_lock();
551 if (pending_lock == NULL)
552 return -1;
553 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 /* only service pending calls on main thread */
556 if (main_thread && PyThread_get_thread_ident() != main_thread)
557 return 0;
558 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200559 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200561 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* perform a bounded number of calls, in case of recursion */
563 for (i=0; i<NPENDINGCALLS; i++) {
564 int j;
565 int (*func)(void *);
566 void *arg = NULL;
567
568 /* pop one item off the queue while holding the lock */
569 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
570 j = pendingfirst;
571 if (j == pendinglast) {
572 func = NULL; /* Queue empty */
573 } else {
574 func = pendingcalls[j].func;
575 arg = pendingcalls[j].arg;
576 pendingfirst = (j + 1) % NPENDINGCALLS;
577 }
578 if (pendingfirst != pendinglast)
579 SIGNAL_PENDING_CALLS();
580 else
581 UNSIGNAL_PENDING_CALLS();
582 PyThread_release_lock(pending_lock);
583 /* having released the lock, perform the callback */
584 if (func == NULL)
585 break;
586 r = func(arg);
587 if (r)
588 break;
589 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200590 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000592}
593
594#else /* if ! defined WITH_THREAD */
595
596/*
597 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
598 This code is used for signal handling in python that isn't built
599 with WITH_THREAD.
600 Don't use this implementation when Py_AddPendingCalls() can happen
601 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602
Guido van Rossuma9672091994-09-14 13:31:22 +0000603 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604 (1) nested asynchronous calls to Py_AddPendingCall()
605 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000607 (1) is very unlikely because typically signal delivery
608 is blocked during signal handling. So it should be impossible.
609 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000610 The current code is safe against (2), but not against (1).
611 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000612 thread is present, interrupted by signals, and that the critical
613 section is protected with the "busy" variable. On Windows, which
614 delivers SIGINT on a system thread, this does not hold and therefore
615 Windows really shouldn't use this version.
616 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000617*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000618
Guido van Rossuma9672091994-09-14 13:31:22 +0000619#define NPENDINGCALLS 32
620static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000621 int (*func)(void *);
622 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000623} pendingcalls[NPENDINGCALLS];
624static volatile int pendingfirst = 0;
625static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000626static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000627
628int
Thomas Wouters334fb892000-07-25 12:56:38 +0000629Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000631 static volatile int busy = 0;
632 int i, j;
633 /* XXX Begin critical section */
634 if (busy)
635 return -1;
636 busy = 1;
637 i = pendinglast;
638 j = (i + 1) % NPENDINGCALLS;
639 if (j == pendingfirst) {
640 busy = 0;
641 return -1; /* Queue full */
642 }
643 pendingcalls[i].func = func;
644 pendingcalls[i].arg = arg;
645 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 SIGNAL_PENDING_CALLS();
648 busy = 0;
649 /* XXX End critical section */
650 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000651}
652
Guido van Rossum180d7b41994-09-29 09:45:57 +0000653int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000654Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 static int busy = 0;
657 if (busy)
658 return 0;
659 busy = 1;
660 UNSIGNAL_PENDING_CALLS();
661 for (;;) {
662 int i;
663 int (*func)(void *);
664 void *arg;
665 i = pendingfirst;
666 if (i == pendinglast)
667 break; /* Queue empty */
668 func = pendingcalls[i].func;
669 arg = pendingcalls[i].arg;
670 pendingfirst = (i + 1) % NPENDINGCALLS;
671 if (func(arg) < 0) {
672 busy = 0;
673 SIGNAL_PENDING_CALLS(); /* We're not done yet */
674 return -1;
675 }
676 }
677 busy = 0;
678 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000679}
680
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000681#endif /* WITH_THREAD */
682
Guido van Rossuma9672091994-09-14 13:31:22 +0000683
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000684/* The interpreter's recursion limit */
685
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000686#ifndef Py_DEFAULT_RECURSION_LIMIT
687#define Py_DEFAULT_RECURSION_LIMIT 1000
688#endif
689static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
690int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000692int
693Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696}
697
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000698void
699Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 recursion_limit = new_limit;
702 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000703}
704
Armin Rigo2b3eb402003-10-28 12:05:48 +0000705/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
706 if the recursion_depth reaches _Py_CheckRecursionLimit.
707 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
708 to guarantee that _Py_CheckRecursiveCall() is regularly called.
709 Without USE_STACKCHECK, there is no need for this. */
710int
711_Py_CheckRecursiveCall(char *where)
712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000714
715#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000716 if (PyOS_CheckStack()) {
717 --tstate->recursion_depth;
718 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
719 return -1;
720 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000721#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000722 _Py_CheckRecursionLimit = recursion_limit;
723 if (tstate->recursion_critical)
724 /* Somebody asked that we don't check for recursion. */
725 return 0;
726 if (tstate->overflowed) {
727 if (tstate->recursion_depth > recursion_limit + 50) {
728 /* Overflowing while handling an overflow. Give up. */
729 Py_FatalError("Cannot recover from stack overflow.");
730 }
731 return 0;
732 }
733 if (tstate->recursion_depth > recursion_limit) {
734 --tstate->recursion_depth;
735 tstate->overflowed = 1;
736 PyErr_Format(PyExc_RuntimeError,
737 "maximum recursion depth exceeded%s",
738 where);
739 return -1;
740 }
741 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000742}
743
Guido van Rossum374a9221991-04-04 10:40:29 +0000744/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000745enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000746 WHY_NOT = 0x0001, /* No error */
747 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000748 WHY_RETURN = 0x0008, /* 'return' statement */
749 WHY_BREAK = 0x0010, /* 'break' statement */
750 WHY_CONTINUE = 0x0020, /* 'continue' statement */
751 WHY_YIELD = 0x0040, /* 'yield' operator */
752 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000753};
Guido van Rossum374a9221991-04-04 10:40:29 +0000754
Benjamin Peterson87880242011-07-03 16:48:31 -0500755static void save_exc_state(PyThreadState *, PyFrameObject *);
756static void swap_exc_state(PyThreadState *, PyFrameObject *);
757static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400758static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000759static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000760
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000761/* Records whether tracing is on for any thread. Counts the number of
762 threads for which tstate->c_tracefunc is non-NULL, so if the value
763 is 0, we know we don't have to check this thread's c_tracefunc.
764 This speeds up the if statement in PyEval_EvalFrameEx() after
765 fast_next_opcode*/
766static int _Py_TracingPossible = 0;
767
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000768
Guido van Rossum374a9221991-04-04 10:40:29 +0000769
Guido van Rossumb209a111997-04-29 18:18:01 +0000770PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000771PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000772{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 return PyEval_EvalCodeEx(co,
774 globals, locals,
775 (PyObject **)NULL, 0,
776 (PyObject **)NULL, 0,
777 (PyObject **)NULL, 0,
778 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000779}
780
781
782/* Interpreter main loop */
783
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000784PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000785PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 /* This is for backward compatibility with extension modules that
787 used this API; core interpreter code should call
788 PyEval_EvalFrameEx() */
789 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000790}
791
792PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000793PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000794{
Guido van Rossum950361c1997-01-24 13:49:28 +0000795#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000797#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200798 PyObject **stack_pointer; /* Next free slot in value stack */
799 unsigned char *next_instr;
800 int opcode; /* Current opcode */
801 int oparg; /* Current opcode argument, if any */
802 enum why_code why; /* Reason for block stack unwind */
803 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 PyObject *retval = NULL; /* Return value */
805 PyThreadState *tstate = PyThreadState_GET();
806 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 is true when the line being executed has changed. The
813 initial values are such as to make this false the first
814 time it is tested. */
815 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 unsigned char *first_instr;
818 PyObject *names;
819 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000820
Brett Cannon368b4b72012-04-02 12:17:59 -0400821#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200822 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400823#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200824
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825/* Computed GOTOs, or
826 the-optimization-commonly-but-improperly-known-as-"threaded code"
827 using gcc's labels-as-values extension
828 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
829
830 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000832 combined with a lookup table of jump addresses. However, since the
833 indirect jump instruction is shared by all opcodes, the CPU will have a
834 hard time making the right prediction for where to jump next (actually,
835 it will be always wrong except in the uncommon case of a sequence of
836 several identical opcodes).
837
838 "Threaded code" in contrast, uses an explicit jump table and an explicit
839 indirect jump instruction at the end of each opcode. Since the jump
840 instruction is at a different address for each opcode, the CPU will make a
841 separate prediction for each of these instructions, which is equivalent to
842 predicting the second opcode of each opcode pair. These predictions have
843 a much better chance to turn out valid, especially in small bytecode loops.
844
845 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847 and potentially many more instructions (depending on the pipeline width).
848 A correctly predicted branch, however, is nearly free.
849
850 At the time of this writing, the "threaded code" version is up to 15-20%
851 faster than the normal "switch" version, depending on the compiler and the
852 CPU architecture.
853
854 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
855 because it would render the measurements invalid.
856
857
858 NOTE: care must be taken that the compiler doesn't try to "optimize" the
859 indirect jumps by sharing them between all opcodes. Such optimizations
860 can be disabled on gcc by using the -fno-gcse flag (or possibly
861 -fno-crossjumping).
862*/
863
Antoine Pitrou042b1282010-08-13 21:15:58 +0000864#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867#endif
868
Antoine Pitrou042b1282010-08-13 21:15:58 +0000869#ifdef HAVE_COMPUTED_GOTOS
870 #ifndef USE_COMPUTED_GOTOS
871 #define USE_COMPUTED_GOTOS 1
872 #endif
873#else
874 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
875 #error "Computed gotos are not supported on this compiler."
876 #endif
877 #undef USE_COMPUTED_GOTOS
878 #define USE_COMPUTED_GOTOS 0
879#endif
880
881#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000882/* Import the static jump table */
883#include "opcode_targets.h"
884
885/* This macro is used when several opcodes defer to the same implementation
886 (e.g. SETUP_LOOP, SETUP_FINALLY) */
887#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 TARGET_##op: \
889 opcode = op; \
890 if (HAS_ARG(op)) \
891 oparg = NEXTARG(); \
892 case op: \
893 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894
895#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 TARGET_##op: \
897 opcode = op; \
898 if (HAS_ARG(op)) \
899 oparg = NEXTARG(); \
900 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901
902
903#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 { \
905 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
906 FAST_DISPATCH(); \
907 } \
908 continue; \
909 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000910
911#ifdef LLTRACE
912#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 { \
914 if (!lltrace && !_Py_TracingPossible) { \
915 f->f_lasti = INSTR_OFFSET(); \
916 goto *opcode_targets[*next_instr++]; \
917 } \
918 goto fast_next_opcode; \
919 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000920#else
921#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 { \
923 if (!_Py_TracingPossible) { \
924 f->f_lasti = INSTR_OFFSET(); \
925 goto *opcode_targets[*next_instr++]; \
926 } \
927 goto fast_next_opcode; \
928 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000929#endif
930
931#else
932#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000934#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* silence compiler warnings about `impl` unused */ \
936 if (0) goto impl; \
937 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938#define DISPATCH() continue
939#define FAST_DISPATCH() goto fast_next_opcode
940#endif
941
942
Neal Norwitza81d2202002-07-14 00:27:26 +0000943/* Tuple access macros */
944
945#ifndef Py_DEBUG
946#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
947#else
948#define GETITEM(v, i) PyTuple_GetItem((v), (i))
949#endif
950
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000951#ifdef WITH_TSC
952/* Use Pentium timestamp counter to mark certain events:
953 inst0 -- beginning of switch statement for opcode dispatch
954 inst1 -- end of switch statement (may be skipped)
955 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000957 (may be skipped)
958 intr1 -- beginning of long interruption
959 intr2 -- end of long interruption
960
961 Many opcodes call out to helper C functions. In some cases, the
962 time in those functions should be counted towards the time for the
963 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
964 calls another Python function; there's no point in charge all the
965 bytecode executed by the called function to the caller.
966
967 It's hard to make a useful judgement statically. In the presence
968 of operator overloading, it's impossible to tell if a call will
969 execute new Python code or not.
970
971 It's a case-by-case judgement. I'll use intr1 for the following
972 cases:
973
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000974 IMPORT_STAR
975 IMPORT_FROM
976 CALL_FUNCTION (and friends)
977
978 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
980 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 READ_TIMESTAMP(inst0);
983 READ_TIMESTAMP(inst1);
984 READ_TIMESTAMP(loop0);
985 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 /* shut up the compiler */
988 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000989#endif
990
Guido van Rossum374a9221991-04-04 10:40:29 +0000991/* Code access macros */
992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993#define INSTR_OFFSET() ((int)(next_instr - first_instr))
994#define NEXTOP() (*next_instr++)
995#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
996#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
997#define JUMPTO(x) (next_instr = first_instr + (x))
998#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000999
Raymond Hettingerf606f872003-03-16 03:11:04 +00001000/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Some opcodes tend to come in pairs thus making it possible to
1002 predict the second code when the first is run. For example,
1003 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1004 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Verifying the prediction costs a single high-speed test of a register
1007 variable against a constant. If the pairing was good, then the
1008 processor's own internal branch predication has a high likelihood of
1009 success, resulting in a nearly zero-overhead transition to the
1010 next opcode. A successful prediction saves a trip through the eval-loop
1011 including its two unpredictable branches, the HAS_ARG test and the
1012 switch-case. Combined with the processor's internal branch prediction,
1013 a successful PREDICT has the effect of making the two opcodes run as if
1014 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001015
Georg Brandl86b2fb92008-07-16 03:43:04 +00001016 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 predictions turned-on and interpret the results as if some opcodes
1018 had been combined or turn-off predictions so that the opcode frequency
1019 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020
1021 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 the CPU to record separate branch prediction information for each
1023 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024
Raymond Hettingerf606f872003-03-16 03:11:04 +00001025*/
1026
Antoine Pitrou042b1282010-08-13 21:15:58 +00001027#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028#define PREDICT(op) if (0) goto PRED_##op
1029#define PREDICTED(op) PRED_##op:
1030#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001031#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1033#define PREDICTED(op) PRED_##op: next_instr++
1034#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001035#endif
1036
Raymond Hettingerf606f872003-03-16 03:11:04 +00001037
Guido van Rossum374a9221991-04-04 10:40:29 +00001038/* Stack manipulation macros */
1039
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040/* The stack can grow at most MAXINT deep, as co_nlocals and
1041 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001042#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1043#define EMPTY() (STACK_LEVEL() == 0)
1044#define TOP() (stack_pointer[-1])
1045#define SECOND() (stack_pointer[-2])
1046#define THIRD() (stack_pointer[-3])
1047#define FOURTH() (stack_pointer[-4])
1048#define PEEK(n) (stack_pointer[-(n)])
1049#define SET_TOP(v) (stack_pointer[-1] = (v))
1050#define SET_SECOND(v) (stack_pointer[-2] = (v))
1051#define SET_THIRD(v) (stack_pointer[-3] = (v))
1052#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1053#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1054#define BASIC_STACKADJ(n) (stack_pointer += n)
1055#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1056#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001057
Guido van Rossum96a42c81992-01-12 02:29:51 +00001058#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001060 lltrace && prtrace(TOP(), "push")); \
1061 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001063 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 lltrace && prtrace(TOP(), "stackadj")); \
1066 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001067#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001068 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1069 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001070#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001071#define PUSH(v) BASIC_PUSH(v)
1072#define POP() BASIC_POP()
1073#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001074#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001075#endif
1076
Guido van Rossum681d79a1995-07-18 14:51:37 +00001077/* Local variable macros */
1078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001080
1081/* The SETLOCAL() macro must not DECREF the local variable in-place and
1082 then store the new value; it must copy the old value to a temporary
1083 value, then store the new value, and then DECREF the temporary value.
1084 This is because it is possible that during the DECREF the frame is
1085 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1086 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001088 GETLOCAL(i) = value; \
1089 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001090
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001091
1092#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 while (STACK_LEVEL() > (b)->b_level) { \
1094 PyObject *v = POP(); \
1095 Py_XDECREF(v); \
1096 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001097
1098#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 { \
1100 PyObject *type, *value, *traceback; \
1101 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1102 while (STACK_LEVEL() > (b)->b_level + 3) { \
1103 value = POP(); \
1104 Py_XDECREF(value); \
1105 } \
1106 type = tstate->exc_type; \
1107 value = tstate->exc_value; \
1108 traceback = tstate->exc_traceback; \
1109 tstate->exc_type = POP(); \
1110 tstate->exc_value = POP(); \
1111 tstate->exc_traceback = POP(); \
1112 Py_XDECREF(type); \
1113 Py_XDECREF(value); \
1114 Py_XDECREF(traceback); \
1115 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001116
Guido van Rossuma027efa1997-05-05 20:56:21 +00001117/* Start of code */
1118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* push frame */
1120 if (Py_EnterRecursiveCall(""))
1121 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (tstate->use_tracing) {
1126 if (tstate->c_tracefunc != NULL) {
1127 /* tstate->c_tracefunc, if defined, is a
1128 function that will be called on *every* entry
1129 to a code block. Its return value, if not
1130 None, is a function that will be called at
1131 the start of each executed line of code.
1132 (Actually, the function must return itself
1133 in order to continue tracing.) The trace
1134 functions are called with three arguments:
1135 a pointer to the current frame, a string
1136 indicating why the function is called, and
1137 an argument which depends on the situation.
1138 The global trace function is also called
1139 whenever an exception is detected. */
1140 if (call_trace_protected(tstate->c_tracefunc,
1141 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001142 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 /* Trace function raised an error */
1144 goto exit_eval_frame;
1145 }
1146 }
1147 if (tstate->c_profilefunc != NULL) {
1148 /* Similar for c_profilefunc, except it needn't
1149 return itself and isn't called for "line" events */
1150 if (call_trace_protected(tstate->c_profilefunc,
1151 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001152 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 /* Profile function raised an error */
1154 goto exit_eval_frame;
1155 }
1156 }
1157 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 co = f->f_code;
1160 names = co->co_names;
1161 consts = co->co_consts;
1162 fastlocals = f->f_localsplus;
1163 freevars = f->f_localsplus + co->co_nlocals;
1164 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1165 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 f->f_lasti now refers to the index of the last instruction
1168 executed. You might think this was obvious from the name, but
1169 this wasn't always true before 2.3! PyFrame_New now sets
1170 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1171 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1172 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001173 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1174 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 When the PREDICT() macros are enabled, some opcode pairs follow in
1177 direct succession without updating f->f_lasti. A successful
1178 prediction effectively links the two codes together as if they
1179 were a single new opcode; accordingly,f->f_lasti will point to
1180 the first code in the pair (for instance, GET_ITER followed by
1181 FOR_ITER is effectively a single opcode and f->f_lasti will point
1182 at to the beginning of the combined pair.)
1183 */
1184 next_instr = first_instr + f->f_lasti + 1;
1185 stack_pointer = f->f_stacktop;
1186 assert(stack_pointer != NULL);
1187 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001188 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (co->co_flags & CO_GENERATOR && !throwflag) {
1191 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1192 /* We were in an except handler when we left,
1193 restore the exception state which was put aside
1194 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001195 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001197 else
1198 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001200
Tim Peters5ca576e2001-06-18 22:08:13 +00001201#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001202 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001203#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001207 if (throwflag) /* support for generator.throw() */
1208 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001209
Victor Stinnerace47d72013-07-18 01:41:08 +02001210#ifdef Py_DEBUG
1211 /* PyEval_EvalFrameEx() must not be called with an exception set,
1212 because it may clear it (directly or indirectly) and so the
1213 caller looses its exception */
1214 assert(!PyErr_Occurred());
1215#endif
1216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001218#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 if (inst1 == 0) {
1220 /* Almost surely, the opcode executed a break
1221 or a continue, preventing inst1 from being set
1222 on the way out of the loop.
1223 */
1224 READ_TIMESTAMP(inst1);
1225 loop1 = inst1;
1226 }
1227 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1228 intr0, intr1);
1229 ticked = 0;
1230 inst1 = 0;
1231 intr0 = 0;
1232 intr1 = 0;
1233 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001234#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1236 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001237 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 /* Do periodic things. Doing this every time through
1240 the loop would add too much overhead, so we do it
1241 only every Nth instruction. We also do it if
1242 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1243 event needs attention (e.g. a signal handler or
1244 async I/O handler); see Py_AddPendingCall() and
1245 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1248 if (*next_instr == SETUP_FINALLY) {
1249 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001250 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 goto fast_next_opcode;
1252 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001253#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001255#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001257 if (Py_MakePendingCalls() < 0)
1258 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001260#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001261 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 /* Give another thread a chance */
1263 if (PyThreadState_Swap(NULL) != tstate)
1264 Py_FatalError("ceval: tstate mix-up");
1265 drop_gil(tstate);
1266
1267 /* Other threads may run now */
1268
1269 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001270
1271 /* Check if we should make a quick exit. */
1272 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1273 drop_gil(tstate);
1274 PyThread_exit_thread();
1275 }
1276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 if (PyThreadState_Swap(tstate) != NULL)
1278 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001280#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Check for asynchronous exceptions. */
1282 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001283 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 tstate->async_exc = NULL;
1285 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001286 PyErr_SetNone(exc);
1287 Py_DECREF(exc);
1288 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 }
1290 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 fast_next_opcode:
1293 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001298 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001299 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 /* see maybe_call_line_trace
1301 for expository comments */
1302 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 err = maybe_call_line_trace(tstate->c_tracefunc,
1305 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001306 tstate, f,
1307 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* Reload possibly changed frame fields */
1309 JUMPTO(f->f_lasti);
1310 if (f->f_stacktop != NULL) {
1311 stack_pointer = f->f_stacktop;
1312 f->f_stacktop = NULL;
1313 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001314 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 opcode = NEXTOP();
1322 oparg = 0; /* allows oparg to be stored in a register because
1323 it doesn't have to be remembered across a full loop */
1324 if (HAS_ARG(opcode))
1325 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001326 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001327#ifdef DYNAMIC_EXECUTION_PROFILE
1328#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 dxpairs[lastopcode][opcode]++;
1330 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001331#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001333#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001334
Guido van Rossum96a42c81992-01-12 02:29:51 +00001335#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (lltrace) {
1339 if (HAS_ARG(opcode)) {
1340 printf("%d: %d, %d\n",
1341 f->f_lasti, opcode, oparg);
1342 }
1343 else {
1344 printf("%d: %d\n",
1345 f->f_lasti, opcode);
1346 }
1347 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001348#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 /* Main switch on opcode */
1351 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 /* BEWARE!
1356 It is essential that any operation that fails sets either
1357 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1358 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 TARGET(NOP)
1361 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001362
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001363 TARGET(LOAD_FAST) {
1364 PyObject *value = GETLOCAL(oparg);
1365 if (value == NULL) {
1366 format_exc_check_arg(PyExc_UnboundLocalError,
1367 UNBOUNDLOCAL_ERROR_MSG,
1368 PyTuple_GetItem(co->co_varnames, oparg));
1369 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001371 Py_INCREF(value);
1372 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001374 }
1375
1376 TARGET(LOAD_CONST) {
1377 PyObject *value = GETITEM(consts, oparg);
1378 Py_INCREF(value);
1379 PUSH(value);
1380 FAST_DISPATCH();
1381 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 TARGET(STORE_FAST) {
1385 PyObject *value = POP();
1386 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001389
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 TARGET(POP_TOP) {
1391 PyObject *value = POP();
1392 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(ROT_TWO) {
1397 PyObject *top = TOP();
1398 PyObject *second = SECOND();
1399 SET_TOP(second);
1400 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 TARGET(ROT_THREE) {
1405 PyObject *top = TOP();
1406 PyObject *second = SECOND();
1407 PyObject *third = THIRD();
1408 SET_TOP(second);
1409 SET_SECOND(third);
1410 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001413
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 TARGET(DUP_TOP) {
1415 PyObject *top = TOP();
1416 Py_INCREF(top);
1417 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001418 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 TARGET(DUP_TOP_TWO) {
1422 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001424 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001426 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 SET_TOP(top);
1428 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001429 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001430 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001431
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 TARGET(UNARY_POSITIVE) {
1433 PyObject *value = TOP();
1434 PyObject *res = PyNumber_Positive(value);
1435 Py_DECREF(value);
1436 SET_TOP(res);
1437 if (res == NULL)
1438 goto error;
1439 DISPATCH();
1440 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001441
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001442 TARGET(UNARY_NEGATIVE) {
1443 PyObject *value = TOP();
1444 PyObject *res = PyNumber_Negative(value);
1445 Py_DECREF(value);
1446 SET_TOP(res);
1447 if (res == NULL)
1448 goto error;
1449 DISPATCH();
1450 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001451
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001452 TARGET(UNARY_NOT) {
1453 PyObject *value = TOP();
1454 int err = PyObject_IsTrue(value);
1455 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 if (err == 0) {
1457 Py_INCREF(Py_True);
1458 SET_TOP(Py_True);
1459 DISPATCH();
1460 }
1461 else if (err > 0) {
1462 Py_INCREF(Py_False);
1463 SET_TOP(Py_False);
1464 err = 0;
1465 DISPATCH();
1466 }
1467 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001468 goto error;
1469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001471 TARGET(UNARY_INVERT) {
1472 PyObject *value = TOP();
1473 PyObject *res = PyNumber_Invert(value);
1474 Py_DECREF(value);
1475 SET_TOP(res);
1476 if (res == NULL)
1477 goto error;
1478 DISPATCH();
1479 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001481 TARGET(BINARY_POWER) {
1482 PyObject *exp = POP();
1483 PyObject *base = TOP();
1484 PyObject *res = PyNumber_Power(base, exp, Py_None);
1485 Py_DECREF(base);
1486 Py_DECREF(exp);
1487 SET_TOP(res);
1488 if (res == NULL)
1489 goto error;
1490 DISPATCH();
1491 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001493 TARGET(BINARY_MULTIPLY) {
1494 PyObject *right = POP();
1495 PyObject *left = TOP();
1496 PyObject *res = PyNumber_Multiply(left, right);
1497 Py_DECREF(left);
1498 Py_DECREF(right);
1499 SET_TOP(res);
1500 if (res == NULL)
1501 goto error;
1502 DISPATCH();
1503 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001504
Benjamin Petersond51374e2014-04-09 23:55:56 -04001505 TARGET(BINARY_MATRIX_MULTIPLY) {
1506 PyObject *right = POP();
1507 PyObject *left = TOP();
1508 PyObject *res = PyNumber_MatrixMultiply(left, right);
1509 Py_DECREF(left);
1510 Py_DECREF(right);
1511 SET_TOP(res);
1512 if (res == NULL)
1513 goto error;
1514 DISPATCH();
1515 }
1516
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001517 TARGET(BINARY_TRUE_DIVIDE) {
1518 PyObject *divisor = POP();
1519 PyObject *dividend = TOP();
1520 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1521 Py_DECREF(dividend);
1522 Py_DECREF(divisor);
1523 SET_TOP(quotient);
1524 if (quotient == NULL)
1525 goto error;
1526 DISPATCH();
1527 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001529 TARGET(BINARY_FLOOR_DIVIDE) {
1530 PyObject *divisor = POP();
1531 PyObject *dividend = TOP();
1532 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1533 Py_DECREF(dividend);
1534 Py_DECREF(divisor);
1535 SET_TOP(quotient);
1536 if (quotient == NULL)
1537 goto error;
1538 DISPATCH();
1539 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001540
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001541 TARGET(BINARY_MODULO) {
1542 PyObject *divisor = POP();
1543 PyObject *dividend = TOP();
1544 PyObject *res = PyUnicode_CheckExact(dividend) ?
1545 PyUnicode_Format(dividend, divisor) :
1546 PyNumber_Remainder(dividend, divisor);
1547 Py_DECREF(divisor);
1548 Py_DECREF(dividend);
1549 SET_TOP(res);
1550 if (res == NULL)
1551 goto error;
1552 DISPATCH();
1553 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001554
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 TARGET(BINARY_ADD) {
1556 PyObject *right = POP();
1557 PyObject *left = TOP();
1558 PyObject *sum;
1559 if (PyUnicode_CheckExact(left) &&
1560 PyUnicode_CheckExact(right)) {
1561 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001562 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001563 }
1564 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001565 sum = PyNumber_Add(left, right);
1566 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001567 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001568 Py_DECREF(right);
1569 SET_TOP(sum);
1570 if (sum == NULL)
1571 goto error;
1572 DISPATCH();
1573 }
1574
1575 TARGET(BINARY_SUBTRACT) {
1576 PyObject *right = POP();
1577 PyObject *left = TOP();
1578 PyObject *diff = PyNumber_Subtract(left, right);
1579 Py_DECREF(right);
1580 Py_DECREF(left);
1581 SET_TOP(diff);
1582 if (diff == NULL)
1583 goto error;
1584 DISPATCH();
1585 }
1586
1587 TARGET(BINARY_SUBSCR) {
1588 PyObject *sub = POP();
1589 PyObject *container = TOP();
1590 PyObject *res = PyObject_GetItem(container, sub);
1591 Py_DECREF(container);
1592 Py_DECREF(sub);
1593 SET_TOP(res);
1594 if (res == NULL)
1595 goto error;
1596 DISPATCH();
1597 }
1598
1599 TARGET(BINARY_LSHIFT) {
1600 PyObject *right = POP();
1601 PyObject *left = TOP();
1602 PyObject *res = PyNumber_Lshift(left, right);
1603 Py_DECREF(left);
1604 Py_DECREF(right);
1605 SET_TOP(res);
1606 if (res == NULL)
1607 goto error;
1608 DISPATCH();
1609 }
1610
1611 TARGET(BINARY_RSHIFT) {
1612 PyObject *right = POP();
1613 PyObject *left = TOP();
1614 PyObject *res = PyNumber_Rshift(left, right);
1615 Py_DECREF(left);
1616 Py_DECREF(right);
1617 SET_TOP(res);
1618 if (res == NULL)
1619 goto error;
1620 DISPATCH();
1621 }
1622
1623 TARGET(BINARY_AND) {
1624 PyObject *right = POP();
1625 PyObject *left = TOP();
1626 PyObject *res = PyNumber_And(left, right);
1627 Py_DECREF(left);
1628 Py_DECREF(right);
1629 SET_TOP(res);
1630 if (res == NULL)
1631 goto error;
1632 DISPATCH();
1633 }
1634
1635 TARGET(BINARY_XOR) {
1636 PyObject *right = POP();
1637 PyObject *left = TOP();
1638 PyObject *res = PyNumber_Xor(left, right);
1639 Py_DECREF(left);
1640 Py_DECREF(right);
1641 SET_TOP(res);
1642 if (res == NULL)
1643 goto error;
1644 DISPATCH();
1645 }
1646
1647 TARGET(BINARY_OR) {
1648 PyObject *right = POP();
1649 PyObject *left = TOP();
1650 PyObject *res = PyNumber_Or(left, right);
1651 Py_DECREF(left);
1652 Py_DECREF(right);
1653 SET_TOP(res);
1654 if (res == NULL)
1655 goto error;
1656 DISPATCH();
1657 }
1658
1659 TARGET(LIST_APPEND) {
1660 PyObject *v = POP();
1661 PyObject *list = PEEK(oparg);
1662 int err;
1663 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 if (err != 0)
1666 goto error;
1667 PREDICT(JUMP_ABSOLUTE);
1668 DISPATCH();
1669 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001670
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001671 TARGET(SET_ADD) {
1672 PyObject *v = POP();
1673 PyObject *set = stack_pointer[-oparg];
1674 int err;
1675 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 if (err != 0)
1678 goto error;
1679 PREDICT(JUMP_ABSOLUTE);
1680 DISPATCH();
1681 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 TARGET(INPLACE_POWER) {
1684 PyObject *exp = POP();
1685 PyObject *base = TOP();
1686 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1687 Py_DECREF(base);
1688 Py_DECREF(exp);
1689 SET_TOP(res);
1690 if (res == NULL)
1691 goto error;
1692 DISPATCH();
1693 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001694
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001695 TARGET(INPLACE_MULTIPLY) {
1696 PyObject *right = POP();
1697 PyObject *left = TOP();
1698 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1699 Py_DECREF(left);
1700 Py_DECREF(right);
1701 SET_TOP(res);
1702 if (res == NULL)
1703 goto error;
1704 DISPATCH();
1705 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001706
Benjamin Petersond51374e2014-04-09 23:55:56 -04001707 TARGET(INPLACE_MATRIX_MULTIPLY) {
1708 PyObject *right = POP();
1709 PyObject *left = TOP();
1710 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1711 Py_DECREF(left);
1712 Py_DECREF(right);
1713 SET_TOP(res);
1714 if (res == NULL)
1715 goto error;
1716 DISPATCH();
1717 }
1718
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001719 TARGET(INPLACE_TRUE_DIVIDE) {
1720 PyObject *divisor = POP();
1721 PyObject *dividend = TOP();
1722 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1723 Py_DECREF(dividend);
1724 Py_DECREF(divisor);
1725 SET_TOP(quotient);
1726 if (quotient == NULL)
1727 goto error;
1728 DISPATCH();
1729 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001730
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001731 TARGET(INPLACE_FLOOR_DIVIDE) {
1732 PyObject *divisor = POP();
1733 PyObject *dividend = TOP();
1734 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1735 Py_DECREF(dividend);
1736 Py_DECREF(divisor);
1737 SET_TOP(quotient);
1738 if (quotient == NULL)
1739 goto error;
1740 DISPATCH();
1741 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001742
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001743 TARGET(INPLACE_MODULO) {
1744 PyObject *right = POP();
1745 PyObject *left = TOP();
1746 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1747 Py_DECREF(left);
1748 Py_DECREF(right);
1749 SET_TOP(mod);
1750 if (mod == NULL)
1751 goto error;
1752 DISPATCH();
1753 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001755 TARGET(INPLACE_ADD) {
1756 PyObject *right = POP();
1757 PyObject *left = TOP();
1758 PyObject *sum;
1759 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1760 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001761 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001762 }
1763 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 sum = PyNumber_InPlaceAdd(left, right);
1765 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001766 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 Py_DECREF(right);
1768 SET_TOP(sum);
1769 if (sum == NULL)
1770 goto error;
1771 DISPATCH();
1772 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001773
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001774 TARGET(INPLACE_SUBTRACT) {
1775 PyObject *right = POP();
1776 PyObject *left = TOP();
1777 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1778 Py_DECREF(left);
1779 Py_DECREF(right);
1780 SET_TOP(diff);
1781 if (diff == NULL)
1782 goto error;
1783 DISPATCH();
1784 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001786 TARGET(INPLACE_LSHIFT) {
1787 PyObject *right = POP();
1788 PyObject *left = TOP();
1789 PyObject *res = PyNumber_InPlaceLshift(left, right);
1790 Py_DECREF(left);
1791 Py_DECREF(right);
1792 SET_TOP(res);
1793 if (res == NULL)
1794 goto error;
1795 DISPATCH();
1796 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001798 TARGET(INPLACE_RSHIFT) {
1799 PyObject *right = POP();
1800 PyObject *left = TOP();
1801 PyObject *res = PyNumber_InPlaceRshift(left, right);
1802 Py_DECREF(left);
1803 Py_DECREF(right);
1804 SET_TOP(res);
1805 if (res == NULL)
1806 goto error;
1807 DISPATCH();
1808 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001809
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001810 TARGET(INPLACE_AND) {
1811 PyObject *right = POP();
1812 PyObject *left = TOP();
1813 PyObject *res = PyNumber_InPlaceAnd(left, right);
1814 Py_DECREF(left);
1815 Py_DECREF(right);
1816 SET_TOP(res);
1817 if (res == NULL)
1818 goto error;
1819 DISPATCH();
1820 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001821
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 TARGET(INPLACE_XOR) {
1823 PyObject *right = POP();
1824 PyObject *left = TOP();
1825 PyObject *res = PyNumber_InPlaceXor(left, right);
1826 Py_DECREF(left);
1827 Py_DECREF(right);
1828 SET_TOP(res);
1829 if (res == NULL)
1830 goto error;
1831 DISPATCH();
1832 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001833
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 TARGET(INPLACE_OR) {
1835 PyObject *right = POP();
1836 PyObject *left = TOP();
1837 PyObject *res = PyNumber_InPlaceOr(left, right);
1838 Py_DECREF(left);
1839 Py_DECREF(right);
1840 SET_TOP(res);
1841 if (res == NULL)
1842 goto error;
1843 DISPATCH();
1844 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001845
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 TARGET(STORE_SUBSCR) {
1847 PyObject *sub = TOP();
1848 PyObject *container = SECOND();
1849 PyObject *v = THIRD();
1850 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 STACKADJ(-3);
1852 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 Py_DECREF(container);
1856 Py_DECREF(sub);
1857 if (err != 0)
1858 goto error;
1859 DISPATCH();
1860 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001861
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 TARGET(DELETE_SUBSCR) {
1863 PyObject *sub = TOP();
1864 PyObject *container = SECOND();
1865 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 STACKADJ(-2);
1867 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 err = PyObject_DelItem(container, sub);
1869 Py_DECREF(container);
1870 Py_DECREF(sub);
1871 if (err != 0)
1872 goto error;
1873 DISPATCH();
1874 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001875
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001877 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001878 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001879 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001880 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001881 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 PyErr_SetString(PyExc_RuntimeError,
1883 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 Py_DECREF(value);
1885 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001887 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 Py_DECREF(value);
1889 if (res == NULL)
1890 goto error;
1891 Py_DECREF(res);
1892 DISPATCH();
1893 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001894
Thomas Wouters434d0822000-08-24 20:11:32 +00001895#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001897#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 TARGET(RAISE_VARARGS) {
1899 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 switch (oparg) {
1901 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001904 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001905 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001906 if (do_raise(exc, cause)) {
1907 why = WHY_EXCEPTION;
1908 goto fast_block_end;
1909 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 break;
1911 default:
1912 PyErr_SetString(PyExc_SystemError,
1913 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 break;
1915 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 goto error;
1917 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001918
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001920 retval = POP();
1921 why = WHY_RETURN;
1922 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001924
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 TARGET(YIELD_FROM) {
1926 PyObject *v = POP();
1927 PyObject *reciever = TOP();
1928 int err;
1929 if (PyGen_CheckExact(reciever)) {
1930 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001931 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001932 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 if (v == Py_None)
1934 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001935 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04001936 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001937 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 Py_DECREF(v);
1939 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001940 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001941 if (tstate->c_tracefunc != NULL
1942 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001943 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001944 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 if (err < 0)
1946 goto error;
1947 Py_DECREF(reciever);
1948 SET_TOP(val);
1949 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001950 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001951 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001952 f->f_stacktop = stack_pointer;
1953 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001954 /* and repeat... */
1955 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001956 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001957 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001958
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 retval = POP();
1961 f->f_stacktop = stack_pointer;
1962 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001964 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001965
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 TARGET(POP_EXCEPT) {
1967 PyTryBlock *b = PyFrame_BlockPop(f);
1968 if (b->b_type != EXCEPT_HANDLER) {
1969 PyErr_SetString(PyExc_SystemError,
1970 "popped block is not an except handler");
1971 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001976
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001977 TARGET(POP_BLOCK) {
1978 PyTryBlock *b = PyFrame_BlockPop(f);
1979 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 TARGET(END_FINALLY) {
1985 PyObject *status = POP();
1986 if (PyLong_Check(status)) {
1987 why = (enum why_code) PyLong_AS_LONG(status);
1988 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 if (why == WHY_RETURN ||
1990 why == WHY_CONTINUE)
1991 retval = POP();
1992 if (why == WHY_SILENCED) {
1993 /* An exception was silenced by 'with', we must
1994 manually unwind the EXCEPT_HANDLER block which was
1995 created when the exception was caught, otherwise
1996 the stack will be in an inconsistent state. */
1997 PyTryBlock *b = PyFrame_BlockPop(f);
1998 assert(b->b_type == EXCEPT_HANDLER);
1999 UNWIND_EXCEPT_HANDLER(b);
2000 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002001 Py_DECREF(status);
2002 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 Py_DECREF(status);
2005 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 else if (PyExceptionClass_Check(status)) {
2008 PyObject *exc = POP();
2009 PyObject *tb = POP();
2010 PyErr_Restore(status, exc, tb);
2011 why = WHY_EXCEPTION;
2012 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002014 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 PyErr_SetString(PyExc_SystemError,
2016 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 Py_DECREF(status);
2018 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 Py_DECREF(status);
2021 DISPATCH();
2022 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002023
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002025 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002026
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002028 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002029 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2030 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002031 PyErr_SetString(PyExc_NameError,
2032 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002034 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002035 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002036 }
2037 else {
2038 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2039 if (build_class_str == NULL)
2040 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002041 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2042 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002043 if (PyErr_ExceptionMatches(PyExc_KeyError))
2044 PyErr_SetString(PyExc_NameError,
2045 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002047 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002049 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002050 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002051 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002052
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002053 TARGET(STORE_NAME) {
2054 PyObject *name = GETITEM(names, oparg);
2055 PyObject *v = POP();
2056 PyObject *ns = f->f_locals;
2057 int err;
2058 if (ns == NULL) {
2059 PyErr_Format(PyExc_SystemError,
2060 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 if (PyDict_CheckExact(ns))
2065 err = PyDict_SetItem(ns, name, v);
2066 else
2067 err = PyObject_SetItem(ns, name, v);
2068 Py_DECREF(v);
2069 if (err != 0)
2070 goto error;
2071 DISPATCH();
2072 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002073
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 TARGET(DELETE_NAME) {
2075 PyObject *name = GETITEM(names, oparg);
2076 PyObject *ns = f->f_locals;
2077 int err;
2078 if (ns == NULL) {
2079 PyErr_Format(PyExc_SystemError,
2080 "no locals when deleting %R", name);
2081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 err = PyObject_DelItem(ns, name);
2084 if (err != 0) {
2085 format_exc_check_arg(PyExc_NameError,
2086 NAME_ERROR_MSG,
2087 name);
2088 goto error;
2089 }
2090 DISPATCH();
2091 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002094 TARGET(UNPACK_SEQUENCE) {
2095 PyObject *seq = POP(), *item, **items;
2096 if (PyTuple_CheckExact(seq) &&
2097 PyTuple_GET_SIZE(seq) == oparg) {
2098 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002100 item = items[oparg];
2101 Py_INCREF(item);
2102 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002104 } else if (PyList_CheckExact(seq) &&
2105 PyList_GET_SIZE(seq) == oparg) {
2106 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 item = items[oparg];
2109 Py_INCREF(item);
2110 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002111 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002112 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 stack_pointer + oparg)) {
2114 STACKADJ(oparg);
2115 } else {
2116 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 Py_DECREF(seq);
2118 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002120 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002121 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002123
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002124 TARGET(UNPACK_EX) {
2125 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2126 PyObject *seq = POP();
2127
2128 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2129 stack_pointer + totalargs)) {
2130 stack_pointer += totalargs;
2131 } else {
2132 Py_DECREF(seq);
2133 goto error;
2134 }
2135 Py_DECREF(seq);
2136 DISPATCH();
2137 }
2138
2139 TARGET(STORE_ATTR) {
2140 PyObject *name = GETITEM(names, oparg);
2141 PyObject *owner = TOP();
2142 PyObject *v = SECOND();
2143 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002146 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 Py_DECREF(owner);
2148 if (err != 0)
2149 goto error;
2150 DISPATCH();
2151 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 TARGET(DELETE_ATTR) {
2154 PyObject *name = GETITEM(names, oparg);
2155 PyObject *owner = POP();
2156 int err;
2157 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2158 Py_DECREF(owner);
2159 if (err != 0)
2160 goto error;
2161 DISPATCH();
2162 }
2163
2164 TARGET(STORE_GLOBAL) {
2165 PyObject *name = GETITEM(names, oparg);
2166 PyObject *v = POP();
2167 int err;
2168 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 if (err != 0)
2171 goto error;
2172 DISPATCH();
2173 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002174
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 TARGET(DELETE_GLOBAL) {
2176 PyObject *name = GETITEM(names, oparg);
2177 int err;
2178 err = PyDict_DelItem(f->f_globals, name);
2179 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002181 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002183 }
2184 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002186
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 TARGET(LOAD_NAME) {
2188 PyObject *name = GETITEM(names, oparg);
2189 PyObject *locals = f->f_locals;
2190 PyObject *v;
2191 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 "no locals when loading %R", name);
2194 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 if (PyDict_CheckExact(locals)) {
2197 v = PyDict_GetItem(locals, name);
2198 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 }
2200 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 v = PyObject_GetItem(locals, name);
Antoine Pitrou1cfa0ba2013-10-07 20:40:59 +02002202 if (v == NULL && _PyErr_OCCURRED()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002203 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2204 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 PyErr_Clear();
2206 }
2207 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 if (v == NULL) {
2209 v = PyDict_GetItem(f->f_globals, name);
2210 Py_XINCREF(v);
2211 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002212 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002213 v = PyDict_GetItem(f->f_builtins, name);
2214 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002215 format_exc_check_arg(
2216 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 NAME_ERROR_MSG, name);
2218 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002219 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002221 }
2222 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 v = PyObject_GetItem(f->f_builtins, name);
2224 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002225 if (PyErr_ExceptionMatches(PyExc_KeyError))
2226 format_exc_check_arg(
2227 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 NAME_ERROR_MSG, name);
2229 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002230 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002231 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002233 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002236 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002237
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 TARGET(LOAD_GLOBAL) {
2239 PyObject *name = GETITEM(names, oparg);
2240 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002241 if (PyDict_CheckExact(f->f_globals)
2242 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002244 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 name);
2246 if (v == NULL) {
Antoine Pitrou59c900d2013-10-07 20:38:51 +02002247 if (!_PyErr_OCCURRED())
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002248 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002249 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002254 else {
2255 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002256 v = PyObject_GetItem(f->f_globals, name);
2257 if (v == NULL) {
2258 v = PyObject_GetItem(f->f_builtins, name);
2259 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002260 if (PyErr_ExceptionMatches(PyExc_KeyError))
2261 format_exc_check_arg(
2262 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002263 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002265 }
2266 }
2267 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002268 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002271
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 TARGET(DELETE_FAST) {
2273 PyObject *v = GETLOCAL(oparg);
2274 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 SETLOCAL(oparg, NULL);
2276 DISPATCH();
2277 }
2278 format_exc_check_arg(
2279 PyExc_UnboundLocalError,
2280 UNBOUNDLOCAL_ERROR_MSG,
2281 PyTuple_GetItem(co->co_varnames, oparg)
2282 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002283 goto error;
2284 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002285
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002286 TARGET(DELETE_DEREF) {
2287 PyObject *cell = freevars[oparg];
2288 if (PyCell_GET(cell) != NULL) {
2289 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002290 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002291 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002292 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 goto error;
2294 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002295
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 TARGET(LOAD_CLOSURE) {
2297 PyObject *cell = freevars[oparg];
2298 Py_INCREF(cell);
2299 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002300 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002301 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002302
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002303 TARGET(LOAD_CLASSDEREF) {
2304 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002305 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002306 assert(locals);
2307 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2308 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2309 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2310 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2311 if (PyDict_CheckExact(locals)) {
2312 value = PyDict_GetItem(locals, name);
2313 Py_XINCREF(value);
2314 }
2315 else {
2316 value = PyObject_GetItem(locals, name);
2317 if (value == NULL && PyErr_Occurred()) {
2318 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2319 goto error;
2320 PyErr_Clear();
2321 }
2322 }
2323 if (!value) {
2324 PyObject *cell = freevars[oparg];
2325 value = PyCell_GET(cell);
2326 if (value == NULL) {
2327 format_exc_unbound(co, oparg);
2328 goto error;
2329 }
2330 Py_INCREF(value);
2331 }
2332 PUSH(value);
2333 DISPATCH();
2334 }
2335
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002336 TARGET(LOAD_DEREF) {
2337 PyObject *cell = freevars[oparg];
2338 PyObject *value = PyCell_GET(cell);
2339 if (value == NULL) {
2340 format_exc_unbound(co, oparg);
2341 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002343 Py_INCREF(value);
2344 PUSH(value);
2345 DISPATCH();
2346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002347
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002348 TARGET(STORE_DEREF) {
2349 PyObject *v = POP();
2350 PyObject *cell = freevars[oparg];
2351 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002353 DISPATCH();
2354 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002355
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 TARGET(BUILD_TUPLE) {
2357 PyObject *tup = PyTuple_New(oparg);
2358 if (tup == NULL)
2359 goto error;
2360 while (--oparg >= 0) {
2361 PyObject *item = POP();
2362 PyTuple_SET_ITEM(tup, oparg, item);
2363 }
2364 PUSH(tup);
2365 DISPATCH();
2366 }
2367
2368 TARGET(BUILD_LIST) {
2369 PyObject *list = PyList_New(oparg);
2370 if (list == NULL)
2371 goto error;
2372 while (--oparg >= 0) {
2373 PyObject *item = POP();
2374 PyList_SET_ITEM(list, oparg, item);
2375 }
2376 PUSH(list);
2377 DISPATCH();
2378 }
2379
2380 TARGET(BUILD_SET) {
2381 PyObject *set = PySet_New(NULL);
2382 int err = 0;
2383 if (set == NULL)
2384 goto error;
2385 while (--oparg >= 0) {
2386 PyObject *item = POP();
2387 if (err == 0)
2388 err = PySet_Add(set, item);
2389 Py_DECREF(item);
2390 }
2391 if (err != 0) {
2392 Py_DECREF(set);
2393 goto error;
2394 }
2395 PUSH(set);
2396 DISPATCH();
2397 }
2398
2399 TARGET(BUILD_MAP) {
2400 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2401 if (map == NULL)
2402 goto error;
2403 PUSH(map);
2404 DISPATCH();
2405 }
2406
2407 TARGET(STORE_MAP) {
2408 PyObject *key = TOP();
2409 PyObject *value = SECOND();
2410 PyObject *map = THIRD();
2411 int err;
2412 STACKADJ(-2);
2413 assert(PyDict_CheckExact(map));
2414 err = PyDict_SetItem(map, key, value);
2415 Py_DECREF(value);
2416 Py_DECREF(key);
2417 if (err != 0)
2418 goto error;
2419 DISPATCH();
2420 }
2421
2422 TARGET(MAP_ADD) {
2423 PyObject *key = TOP();
2424 PyObject *value = SECOND();
2425 PyObject *map;
2426 int err;
2427 STACKADJ(-2);
2428 map = stack_pointer[-oparg]; /* dict */
2429 assert(PyDict_CheckExact(map));
2430 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2431 Py_DECREF(value);
2432 Py_DECREF(key);
2433 if (err != 0)
2434 goto error;
2435 PREDICT(JUMP_ABSOLUTE);
2436 DISPATCH();
2437 }
2438
2439 TARGET(LOAD_ATTR) {
2440 PyObject *name = GETITEM(names, oparg);
2441 PyObject *owner = TOP();
2442 PyObject *res = PyObject_GetAttr(owner, name);
2443 Py_DECREF(owner);
2444 SET_TOP(res);
2445 if (res == NULL)
2446 goto error;
2447 DISPATCH();
2448 }
2449
2450 TARGET(COMPARE_OP) {
2451 PyObject *right = POP();
2452 PyObject *left = TOP();
2453 PyObject *res = cmp_outcome(oparg, left, right);
2454 Py_DECREF(left);
2455 Py_DECREF(right);
2456 SET_TOP(res);
2457 if (res == NULL)
2458 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 PREDICT(POP_JUMP_IF_FALSE);
2460 PREDICT(POP_JUMP_IF_TRUE);
2461 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002463
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 TARGET(IMPORT_NAME) {
2465 _Py_IDENTIFIER(__import__);
2466 PyObject *name = GETITEM(names, oparg);
2467 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002468 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002469 if (func == NULL) {
2470 PyErr_SetString(PyExc_ImportError,
2471 "__import__ not found");
2472 goto error;
2473 }
2474 Py_INCREF(func);
2475 from = POP();
2476 level = TOP();
2477 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2478 args = PyTuple_Pack(5,
2479 name,
2480 f->f_globals,
2481 f->f_locals == NULL ?
2482 Py_None : f->f_locals,
2483 from,
2484 level);
2485 else
2486 args = PyTuple_Pack(4,
2487 name,
2488 f->f_globals,
2489 f->f_locals == NULL ?
2490 Py_None : f->f_locals,
2491 from);
2492 Py_DECREF(level);
2493 Py_DECREF(from);
2494 if (args == NULL) {
2495 Py_DECREF(func);
2496 STACKADJ(-1);
2497 goto error;
2498 }
2499 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002500 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 READ_TIMESTAMP(intr1);
2502 Py_DECREF(args);
2503 Py_DECREF(func);
2504 SET_TOP(res);
2505 if (res == NULL)
2506 goto error;
2507 DISPATCH();
2508 }
2509
2510 TARGET(IMPORT_STAR) {
2511 PyObject *from = POP(), *locals;
2512 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002513 if (PyFrame_FastToLocalsWithError(f) < 0)
2514 goto error;
2515
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002516 locals = f->f_locals;
2517 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 PyErr_SetString(PyExc_SystemError,
2519 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 }
2522 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 READ_TIMESTAMP(intr1);
2525 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 Py_DECREF(from);
2527 if (err != 0)
2528 goto error;
2529 DISPATCH();
2530 }
Guido van Rossum25831651993-05-19 14:50:45 +00002531
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 TARGET(IMPORT_FROM) {
2533 PyObject *name = GETITEM(names, oparg);
2534 PyObject *from = TOP();
2535 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002537 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 PUSH(res);
2540 if (res == NULL)
2541 goto error;
2542 DISPATCH();
2543 }
Thomas Wouters52152252000-08-17 22:55:00 +00002544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 JUMPBY(oparg);
2547 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 TARGET(POP_JUMP_IF_FALSE) {
2552 PyObject *cond = POP();
2553 int err;
2554 if (cond == Py_True) {
2555 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 FAST_DISPATCH();
2557 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 if (cond == Py_False) {
2559 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002560 JUMPTO(oparg);
2561 FAST_DISPATCH();
2562 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002563 err = PyObject_IsTrue(cond);
2564 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 if (err > 0)
2566 err = 0;
2567 else if (err == 0)
2568 JUMPTO(oparg);
2569 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002572 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002575 TARGET(POP_JUMP_IF_TRUE) {
2576 PyObject *cond = POP();
2577 int err;
2578 if (cond == Py_False) {
2579 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 FAST_DISPATCH();
2581 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002582 if (cond == Py_True) {
2583 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 JUMPTO(oparg);
2585 FAST_DISPATCH();
2586 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002587 err = PyObject_IsTrue(cond);
2588 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 if (err > 0) {
2590 err = 0;
2591 JUMPTO(oparg);
2592 }
2593 else if (err == 0)
2594 ;
2595 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002596 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002598 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002599
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002600 TARGET(JUMP_IF_FALSE_OR_POP) {
2601 PyObject *cond = TOP();
2602 int err;
2603 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002604 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002605 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 FAST_DISPATCH();
2607 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 JUMPTO(oparg);
2610 FAST_DISPATCH();
2611 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002613 if (err > 0) {
2614 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 err = 0;
2617 }
2618 else if (err == 0)
2619 JUMPTO(oparg);
2620 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002621 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002624
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 TARGET(JUMP_IF_TRUE_OR_POP) {
2626 PyObject *cond = TOP();
2627 int err;
2628 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 FAST_DISPATCH();
2632 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002633 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 JUMPTO(oparg);
2635 FAST_DISPATCH();
2636 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 if (err > 0) {
2639 err = 0;
2640 JUMPTO(oparg);
2641 }
2642 else if (err == 0) {
2643 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002644 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 }
2646 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002647 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002654#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 /* Enabling this path speeds-up all while and for-loops by bypassing
2656 the per-loop checks for signals. By default, this should be turned-off
2657 because it prevents detection of a control-break in tight loops like
2658 "while 1: pass". Compile with this option turned-on when you need
2659 the speed-up and do not need break checking inside tight loops (ones
2660 that contain only instructions ending with FAST_DISPATCH).
2661 */
2662 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002663#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002665#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002666 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002667
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002669 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 PyObject *iterable = TOP();
2671 PyObject *iter = PyObject_GetIter(iterable);
2672 Py_DECREF(iterable);
2673 SET_TOP(iter);
2674 if (iter == NULL)
2675 goto error;
2676 PREDICT(FOR_ITER);
2677 DISPATCH();
2678 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002683 PyObject *iter = TOP();
2684 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2685 if (next != NULL) {
2686 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 PREDICT(STORE_FAST);
2688 PREDICT(UNPACK_SEQUENCE);
2689 DISPATCH();
2690 }
2691 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2693 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002694 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002695 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 PyErr_Clear();
2697 }
2698 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 STACKADJ(-1);
2700 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 JUMPBY(oparg);
2702 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002704
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 why = WHY_BREAK;
2707 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002709
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 if (retval == NULL)
2713 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 why = WHY_CONTINUE;
2715 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2719 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2720 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002721 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 /* NOTE: If you add any new block-setup opcodes that
2723 are not try/except/finally handlers, you may need
2724 to update the PyGen_NeedsFinalizing() function.
2725 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2728 STACK_LEVEL());
2729 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002731
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002733 _Py_IDENTIFIER(__exit__);
2734 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 PyObject *mgr = TOP();
2736 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2737 PyObject *res;
2738 if (exit == NULL)
2739 goto error;
2740 SET_TOP(exit);
2741 enter = special_lookup(mgr, &PyId___enter__);
2742 Py_DECREF(mgr);
2743 if (enter == NULL)
2744 goto error;
2745 res = PyObject_CallFunctionObjArgs(enter, NULL);
2746 Py_DECREF(enter);
2747 if (res == NULL)
2748 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 /* Setup the finally block before pushing the result
2750 of __enter__ on the stack. */
2751 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2752 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002753
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 DISPATCH();
2756 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002757
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 TARGET(WITH_CLEANUP) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002759 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 how/why we entered the finally clause:
2761 - TOP = None
2762 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2763 - TOP = WHY_*; no retval below it
2764 - (TOP, SECOND, THIRD) = exc_info()
2765 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2766 Below them is EXIT, the context.__exit__ bound method.
2767 In the last case, we must call
2768 EXIT(TOP, SECOND, THIRD)
2769 otherwise we must call
2770 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002771
Benjamin Peterson8f169482013-10-29 22:25:06 -04002772 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002774 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002776
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 In addition, if the stack represents an exception,
2778 *and* the function call returns a 'true' value, we
2779 push WHY_SILENCED onto the stack. END_FINALLY will
2780 then not re-raise the exception. (But non-local
2781 gotos should still be resumed.)
2782 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002784 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2786 int err;
2787 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 (void)POP();
2789 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002792 else if (PyLong_Check(exc)) {
2793 STACKADJ(-1);
2794 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 case WHY_RETURN:
2796 case WHY_CONTINUE:
2797 /* Retval in TOP. */
2798 exit_func = SECOND();
2799 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 break;
2802 default:
2803 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002805 break;
2806 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002808 }
2809 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 val = SECOND();
2813 tb = THIRD();
2814 tp2 = FOURTH();
2815 exc2 = PEEK(5);
2816 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002817 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 SET_VALUE(7, tb2);
2819 SET_VALUE(6, exc2);
2820 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2822 SET_FOURTH(NULL);
2823 /* We just shifted the stack down, so we have
2824 to tell the except handler block that the
2825 values are lower than it expects. */
2826 block = &f->f_blockstack[f->f_iblock - 1];
2827 assert(block->b_type == EXCEPT_HANDLER);
2828 block->b_level--;
2829 }
2830 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 if (res == NULL)
2834 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002835
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 if (exc != Py_None)
2837 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 else
2839 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002840 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002843 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 else if (err > 0) {
2845 err = 0;
2846 /* There was an exception and a True return */
2847 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2848 }
2849 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002852
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 TARGET(CALL_FUNCTION) {
2854 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 PCALL(PCALL_ALL);
2856 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002857#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002859#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002861#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002863 PUSH(res);
2864 if (res == NULL)
2865 goto error;
2866 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2870 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2871 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002872 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 int na = oparg & 0xff;
2874 int nk = (oparg>>8) & 0xff;
2875 int flags = (opcode - CALL_FUNCTION) & 3;
2876 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002877 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 PCALL(PCALL_ALL);
2879 if (flags & CALL_FLAG_VAR)
2880 n++;
2881 if (flags & CALL_FLAG_KW)
2882 n++;
2883 pfunc = stack_pointer - n - 1;
2884 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002887 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 PyObject *self = PyMethod_GET_SELF(func);
2889 Py_INCREF(self);
2890 func = PyMethod_GET_FUNCTION(func);
2891 Py_INCREF(func);
2892 Py_DECREF(*pfunc);
2893 *pfunc = self;
2894 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002895 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 } else
2897 Py_INCREF(func);
2898 sp = stack_pointer;
2899 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 READ_TIMESTAMP(intr1);
2902 stack_pointer = sp;
2903 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 PyObject *o = POP();
2907 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002909 PUSH(res);
2910 if (res == NULL)
2911 goto error;
2912 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2916 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 int posdefaults = oparg & 0xff;
2919 int kwdefaults = (oparg>>8) & 0xff;
2920 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002921
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 PyObject *qualname = POP(); /* qualname */
2923 PyObject *code = POP(); /* code object */
2924 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2925 Py_DECREF(code);
2926 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002927
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 if (func == NULL)
2929 goto error;
2930
2931 if (opcode == MAKE_CLOSURE) {
2932 PyObject *closure = POP();
2933 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002935 Py_DECREF(func);
2936 Py_DECREF(closure);
2937 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002941
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002944 PyObject *names = POP(); /* names of args with annotations */
2945 PyObject *anns = PyDict_New();
2946 if (anns == NULL) {
2947 Py_DECREF(func);
2948 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 assert(num_annotations == name_ix+1);
2952 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002953 PyObject *name, *value;
2954 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002955 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002956 name = PyTuple_GET_ITEM(names, name_ix);
2957 value = POP();
2958 err = PyDict_SetItem(anns, name, value);
2959 Py_DECREF(value);
2960 if (err != 0) {
2961 Py_DECREF(anns);
2962 Py_DECREF(func);
2963 goto error;
2964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002966
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002967 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002968 /* Can't happen unless
2969 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 Py_DECREF(anns);
2971 Py_DECREF(func);
2972 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 Py_DECREF(anns);
2975 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002978 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 if (kwdefaults > 0) {
2980 PyObject *defs = PyDict_New();
2981 if (defs == NULL) {
2982 Py_DECREF(func);
2983 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002984 }
2985 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002986 PyObject *v = POP(); /* default value */
2987 PyObject *key = POP(); /* kw only arg name */
2988 int err = PyDict_SetItem(defs, key, v);
2989 Py_DECREF(v);
2990 Py_DECREF(key);
2991 if (err != 0) {
2992 Py_DECREF(defs);
2993 Py_DECREF(func);
2994 goto error;
2995 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 /* Can't happen unless
2999 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 Py_DECREF(func);
3001 Py_DECREF(defs);
3002 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003004 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05003006 if (posdefaults > 0) {
3007 PyObject *defs = PyTuple_New(posdefaults);
3008 if (defs == NULL) {
3009 Py_DECREF(func);
3010 goto error;
3011 }
3012 while (--posdefaults >= 0)
3013 PyTuple_SET_ITEM(defs, posdefaults, POP());
3014 if (PyFunction_SetDefaults(func, defs) != 0) {
3015 /* Can't happen unless
3016 PyFunction_SetDefaults changes. */
3017 Py_DECREF(defs);
3018 Py_DECREF(func);
3019 goto error;
3020 }
3021 Py_DECREF(defs);
3022 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 PUSH(func);
3024 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003026
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 TARGET(BUILD_SLICE) {
3028 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 step = NULL;
3033 stop = POP();
3034 start = TOP();
3035 slice = PySlice_New(start, stop, step);
3036 Py_DECREF(start);
3037 Py_DECREF(stop);
3038 Py_XDECREF(step);
3039 SET_TOP(slice);
3040 if (slice == NULL)
3041 goto error;
3042 DISPATCH();
3043 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003044
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 opcode = NEXTOP();
3047 oparg = oparg<<16 | NEXTARG();
3048 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003050
Antoine Pitrou042b1282010-08-13 21:15:58 +00003051#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003053#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 default:
3055 fprintf(stderr,
3056 "XXX lineno: %d, opcode: %d\n",
3057 PyFrame_GetLineNumber(f),
3058 opcode);
3059 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003061
3062#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003064#endif
3065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003067
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 /* This should never be reached. Every opcode should end with DISPATCH()
3069 or goto error. */
3070 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003071
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003074
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003075 assert(why == WHY_NOT);
3076 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003077
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003078 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003079#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003080 if (!PyErr_Occurred())
3081 PyErr_SetString(PyExc_SystemError,
3082 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003083#else
3084 assert(PyErr_Occurred());
3085#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 /* Log traceback info. */
3088 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003089
Benjamin Peterson51f46162013-01-23 08:38:47 -05003090 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003091 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3092 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003093
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003094fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003095 assert(why != WHY_NOT);
3096
3097 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 while (why != WHY_NOT && f->f_iblock > 0) {
3099 /* Peek at the current block. */
3100 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 assert(why != WHY_YIELD);
3103 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3104 why = WHY_NOT;
3105 JUMPTO(PyLong_AS_LONG(retval));
3106 Py_DECREF(retval);
3107 break;
3108 }
3109 /* Now we have to pop the block. */
3110 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003111
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 if (b->b_type == EXCEPT_HANDLER) {
3113 UNWIND_EXCEPT_HANDLER(b);
3114 continue;
3115 }
3116 UNWIND_BLOCK(b);
3117 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3118 why = WHY_NOT;
3119 JUMPTO(b->b_handler);
3120 break;
3121 }
3122 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3123 || b->b_type == SETUP_FINALLY)) {
3124 PyObject *exc, *val, *tb;
3125 int handler = b->b_handler;
3126 /* Beware, this invalidates all b->b_* fields */
3127 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3128 PUSH(tstate->exc_traceback);
3129 PUSH(tstate->exc_value);
3130 if (tstate->exc_type != NULL) {
3131 PUSH(tstate->exc_type);
3132 }
3133 else {
3134 Py_INCREF(Py_None);
3135 PUSH(Py_None);
3136 }
3137 PyErr_Fetch(&exc, &val, &tb);
3138 /* Make the raw exception data
3139 available to the handler,
3140 so a program can emulate the
3141 Python main loop. */
3142 PyErr_NormalizeException(
3143 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003144 if (tb != NULL)
3145 PyException_SetTraceback(val, tb);
3146 else
3147 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 Py_INCREF(exc);
3149 tstate->exc_type = exc;
3150 Py_INCREF(val);
3151 tstate->exc_value = val;
3152 tstate->exc_traceback = tb;
3153 if (tb == NULL)
3154 tb = Py_None;
3155 Py_INCREF(tb);
3156 PUSH(tb);
3157 PUSH(val);
3158 PUSH(exc);
3159 why = WHY_NOT;
3160 JUMPTO(handler);
3161 break;
3162 }
3163 if (b->b_type == SETUP_FINALLY) {
3164 if (why & (WHY_RETURN | WHY_CONTINUE))
3165 PUSH(retval);
3166 PUSH(PyLong_FromLong((long)why));
3167 why = WHY_NOT;
3168 JUMPTO(b->b_handler);
3169 break;
3170 }
3171 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003173 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003175 if (why != WHY_NOT)
3176 break;
3177 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003178
Victor Stinnerace47d72013-07-18 01:41:08 +02003179 assert(!PyErr_Occurred());
3180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003183 assert(why != WHY_YIELD);
3184 /* Pop remaining stack entries. */
3185 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 PyObject *o = POP();
3187 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003190 if (why != WHY_RETURN)
3191 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003192
Victor Stinnerace47d72013-07-18 01:41:08 +02003193 assert((retval != NULL && !PyErr_Occurred())
3194 || (retval == NULL && PyErr_Occurred()));
3195
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003196fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003197 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3198 /* The purpose of this block is to put aside the generator's exception
3199 state and restore that of the calling frame. If the current
3200 exception state is from the caller, we clear the exception values
3201 on the generator frame, so they are not swapped back in latter. The
3202 origin of the current exception state is determined by checking for
3203 except handler blocks, which we must be in iff a new exception
3204 state came into existence in this frame. (An uncaught exception
3205 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3206 int i;
3207 for (i = 0; i < f->f_iblock; i++)
3208 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3209 break;
3210 if (i == f->f_iblock)
3211 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003212 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003213 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003214 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003215 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003218 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003220 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3221 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003223 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003224 why = WHY_EXCEPTION;
3225 }
3226 }
3227 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003228 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3229 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 PyTrace_RETURN, NULL);
3231 }
3232 }
3233 if (tstate->c_profilefunc) {
3234 if (why == WHY_EXCEPTION)
3235 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003236 tstate->c_profileobj,
3237 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003239 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3240 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003241 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003242 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003243 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003244 }
3245 }
3246 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003249exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003251 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003255}
3256
Benjamin Petersonb204a422011-06-05 22:04:07 -05003257static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003258format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3259{
3260 int err;
3261 Py_ssize_t len = PyList_GET_SIZE(names);
3262 PyObject *name_str, *comma, *tail, *tmp;
3263
3264 assert(PyList_CheckExact(names));
3265 assert(len >= 1);
3266 /* Deal with the joys of natural language. */
3267 switch (len) {
3268 case 1:
3269 name_str = PyList_GET_ITEM(names, 0);
3270 Py_INCREF(name_str);
3271 break;
3272 case 2:
3273 name_str = PyUnicode_FromFormat("%U and %U",
3274 PyList_GET_ITEM(names, len - 2),
3275 PyList_GET_ITEM(names, len - 1));
3276 break;
3277 default:
3278 tail = PyUnicode_FromFormat(", %U, and %U",
3279 PyList_GET_ITEM(names, len - 2),
3280 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003281 if (tail == NULL)
3282 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003283 /* Chop off the last two objects in the list. This shouldn't actually
3284 fail, but we can't be too careful. */
3285 err = PyList_SetSlice(names, len - 2, len, NULL);
3286 if (err == -1) {
3287 Py_DECREF(tail);
3288 return;
3289 }
3290 /* Stitch everything up into a nice comma-separated list. */
3291 comma = PyUnicode_FromString(", ");
3292 if (comma == NULL) {
3293 Py_DECREF(tail);
3294 return;
3295 }
3296 tmp = PyUnicode_Join(comma, names);
3297 Py_DECREF(comma);
3298 if (tmp == NULL) {
3299 Py_DECREF(tail);
3300 return;
3301 }
3302 name_str = PyUnicode_Concat(tmp, tail);
3303 Py_DECREF(tmp);
3304 Py_DECREF(tail);
3305 break;
3306 }
3307 if (name_str == NULL)
3308 return;
3309 PyErr_Format(PyExc_TypeError,
3310 "%U() missing %i required %s argument%s: %U",
3311 co->co_name,
3312 len,
3313 kind,
3314 len == 1 ? "" : "s",
3315 name_str);
3316 Py_DECREF(name_str);
3317}
3318
3319static void
3320missing_arguments(PyCodeObject *co, int missing, int defcount,
3321 PyObject **fastlocals)
3322{
3323 int i, j = 0;
3324 int start, end;
3325 int positional = defcount != -1;
3326 const char *kind = positional ? "positional" : "keyword-only";
3327 PyObject *missing_names;
3328
3329 /* Compute the names of the arguments that are missing. */
3330 missing_names = PyList_New(missing);
3331 if (missing_names == NULL)
3332 return;
3333 if (positional) {
3334 start = 0;
3335 end = co->co_argcount - defcount;
3336 }
3337 else {
3338 start = co->co_argcount;
3339 end = start + co->co_kwonlyargcount;
3340 }
3341 for (i = start; i < end; i++) {
3342 if (GETLOCAL(i) == NULL) {
3343 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3344 PyObject *name = PyObject_Repr(raw);
3345 if (name == NULL) {
3346 Py_DECREF(missing_names);
3347 return;
3348 }
3349 PyList_SET_ITEM(missing_names, j++, name);
3350 }
3351 }
3352 assert(j == missing);
3353 format_missing(kind, co, missing_names);
3354 Py_DECREF(missing_names);
3355}
3356
3357static void
3358too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003359{
3360 int plural;
3361 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003362 int i;
3363 PyObject *sig, *kwonly_sig;
3364
Benjamin Petersone109c702011-06-24 09:37:26 -05003365 assert((co->co_flags & CO_VARARGS) == 0);
3366 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003367 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003368 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003369 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003370 if (defcount) {
3371 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003372 plural = 1;
3373 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3374 }
3375 else {
3376 plural = co->co_argcount != 1;
3377 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3378 }
3379 if (sig == NULL)
3380 return;
3381 if (kwonly_given) {
3382 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3383 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3384 kwonly_given != 1 ? "s" : "");
3385 if (kwonly_sig == NULL) {
3386 Py_DECREF(sig);
3387 return;
3388 }
3389 }
3390 else {
3391 /* This will not fail. */
3392 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003393 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003394 }
3395 PyErr_Format(PyExc_TypeError,
3396 "%U() takes %U positional argument%s but %d%U %s given",
3397 co->co_name,
3398 sig,
3399 plural ? "s" : "",
3400 given,
3401 kwonly_sig,
3402 given == 1 && !kwonly_given ? "was" : "were");
3403 Py_DECREF(sig);
3404 Py_DECREF(kwonly_sig);
3405}
3406
Guido van Rossumc2e20742006-02-27 22:32:47 +00003407/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003408 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003409 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003410
Victor Stinner40ee3012014-06-16 15:59:28 +02003411static PyObject *
3412_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 PyObject **args, int argcount, PyObject **kws, int kwcount,
Victor Stinner40ee3012014-06-16 15:59:28 +02003414 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure,
3415 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003416{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003417 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003418 PyFrameObject *f;
3419 PyObject *retval = NULL;
3420 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 PyThreadState *tstate = PyThreadState_GET();
3422 PyObject *x, *u;
3423 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003424 int i;
3425 int n = argcount;
3426 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 if (globals == NULL) {
3429 PyErr_SetString(PyExc_SystemError,
3430 "PyEval_EvalCodeEx: NULL globals");
3431 return NULL;
3432 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003433
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 assert(tstate != NULL);
3435 assert(globals != NULL);
3436 f = PyFrame_New(tstate, co, globals, locals);
3437 if (f == NULL)
3438 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 fastlocals = f->f_localsplus;
3441 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003442
Benjamin Petersonb204a422011-06-05 22:04:07 -05003443 /* Parse arguments. */
3444 if (co->co_flags & CO_VARKEYWORDS) {
3445 kwdict = PyDict_New();
3446 if (kwdict == NULL)
3447 goto fail;
3448 i = total_args;
3449 if (co->co_flags & CO_VARARGS)
3450 i++;
3451 SETLOCAL(i, kwdict);
3452 }
3453 if (argcount > co->co_argcount)
3454 n = co->co_argcount;
3455 for (i = 0; i < n; i++) {
3456 x = args[i];
3457 Py_INCREF(x);
3458 SETLOCAL(i, x);
3459 }
3460 if (co->co_flags & CO_VARARGS) {
3461 u = PyTuple_New(argcount - n);
3462 if (u == NULL)
3463 goto fail;
3464 SETLOCAL(total_args, u);
3465 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 x = args[i];
3467 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003468 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003470 }
3471 for (i = 0; i < kwcount; i++) {
3472 PyObject **co_varnames;
3473 PyObject *keyword = kws[2*i];
3474 PyObject *value = kws[2*i + 1];
3475 int j;
3476 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3477 PyErr_Format(PyExc_TypeError,
3478 "%U() keywords must be strings",
3479 co->co_name);
3480 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003482 /* Speed hack: do raw pointer compares. As names are
3483 normally interned this should almost always hit. */
3484 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3485 for (j = 0; j < total_args; j++) {
3486 PyObject *nm = co_varnames[j];
3487 if (nm == keyword)
3488 goto kw_found;
3489 }
3490 /* Slow fallback, just in case */
3491 for (j = 0; j < total_args; j++) {
3492 PyObject *nm = co_varnames[j];
3493 int cmp = PyObject_RichCompareBool(
3494 keyword, nm, Py_EQ);
3495 if (cmp > 0)
3496 goto kw_found;
3497 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003499 }
3500 if (j >= total_args && kwdict == NULL) {
3501 PyErr_Format(PyExc_TypeError,
3502 "%U() got an unexpected "
3503 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 co->co_name,
3505 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003506 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003508 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3509 goto fail;
3510 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003511 continue;
3512 kw_found:
3513 if (GETLOCAL(j) != NULL) {
3514 PyErr_Format(PyExc_TypeError,
3515 "%U() got multiple "
3516 "values for argument '%S'",
3517 co->co_name,
3518 keyword);
3519 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003521 Py_INCREF(value);
3522 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003523 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003524 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003525 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 goto fail;
3527 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003528 if (argcount < co->co_argcount) {
3529 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003530 int missing = 0;
3531 for (i = argcount; i < m; i++)
3532 if (GETLOCAL(i) == NULL)
3533 missing++;
3534 if (missing) {
3535 missing_arguments(co, missing, defcount, fastlocals);
3536 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003537 }
3538 if (n > m)
3539 i = n - m;
3540 else
3541 i = 0;
3542 for (; i < defcount; i++) {
3543 if (GETLOCAL(m+i) == NULL) {
3544 PyObject *def = defs[i];
3545 Py_INCREF(def);
3546 SETLOCAL(m+i, def);
3547 }
3548 }
3549 }
3550 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003551 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003552 for (i = co->co_argcount; i < total_args; i++) {
3553 PyObject *name;
3554 if (GETLOCAL(i) != NULL)
3555 continue;
3556 name = PyTuple_GET_ITEM(co->co_varnames, i);
3557 if (kwdefs != NULL) {
3558 PyObject *def = PyDict_GetItem(kwdefs, name);
3559 if (def) {
3560 Py_INCREF(def);
3561 SETLOCAL(i, def);
3562 continue;
3563 }
3564 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003565 missing++;
3566 }
3567 if (missing) {
3568 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003569 goto fail;
3570 }
3571 }
3572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003574 vars into frame. */
3575 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003577 int arg;
3578 /* Possibly account for the cell variable being an argument. */
3579 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003580 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003581 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003582 /* Clear the local copy. */
3583 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003584 }
3585 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003586 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003587 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003588 if (c == NULL)
3589 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003590 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003592 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3593 PyObject *o = PyTuple_GET_ITEM(closure, i);
3594 Py_INCREF(o);
3595 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 if (co->co_flags & CO_GENERATOR) {
3599 /* Don't need to keep the reference to f_back, it will be set
3600 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003601 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 /* Create a new generator that owns the ready to run frame
3606 * and return that as the value. */
Victor Stinner40ee3012014-06-16 15:59:28 +02003607 return PyGen_NewWithQualName(f, name, qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003611
Thomas Woutersce272b62007-09-19 21:19:28 +00003612fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 /* decref'ing the frame can cause __del__ methods to get invoked,
3615 which can call back into Python. While we're done with the
3616 current Python frame (f), the associated C stack is still in use,
3617 so recursion_depth must be boosted for the duration.
3618 */
3619 assert(tstate != NULL);
3620 ++tstate->recursion_depth;
3621 Py_DECREF(f);
3622 --tstate->recursion_depth;
3623 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003624}
3625
Victor Stinner40ee3012014-06-16 15:59:28 +02003626PyObject *
3627PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
3628 PyObject **args, int argcount, PyObject **kws, int kwcount,
3629 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
3630{
3631 return _PyEval_EvalCodeWithName(_co, globals, locals,
3632 args, argcount, kws, kwcount,
3633 defs, defcount, kwdefs, closure,
3634 NULL, NULL);
3635}
Tim Peters5ca576e2001-06-18 22:08:13 +00003636
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003637static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003638special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003641 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003643 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003644 return NULL;
3645 }
3646 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003647}
3648
3649
Benjamin Peterson87880242011-07-03 16:48:31 -05003650/* These 3 functions deal with the exception state of generators. */
3651
3652static void
3653save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3654{
3655 PyObject *type, *value, *traceback;
3656 Py_XINCREF(tstate->exc_type);
3657 Py_XINCREF(tstate->exc_value);
3658 Py_XINCREF(tstate->exc_traceback);
3659 type = f->f_exc_type;
3660 value = f->f_exc_value;
3661 traceback = f->f_exc_traceback;
3662 f->f_exc_type = tstate->exc_type;
3663 f->f_exc_value = tstate->exc_value;
3664 f->f_exc_traceback = tstate->exc_traceback;
3665 Py_XDECREF(type);
3666 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003667 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003668}
3669
3670static void
3671swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3672{
3673 PyObject *tmp;
3674 tmp = tstate->exc_type;
3675 tstate->exc_type = f->f_exc_type;
3676 f->f_exc_type = tmp;
3677 tmp = tstate->exc_value;
3678 tstate->exc_value = f->f_exc_value;
3679 f->f_exc_value = tmp;
3680 tmp = tstate->exc_traceback;
3681 tstate->exc_traceback = f->f_exc_traceback;
3682 f->f_exc_traceback = tmp;
3683}
3684
3685static void
3686restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3687{
3688 PyObject *type, *value, *tb;
3689 type = tstate->exc_type;
3690 value = tstate->exc_value;
3691 tb = tstate->exc_traceback;
3692 tstate->exc_type = f->f_exc_type;
3693 tstate->exc_value = f->f_exc_value;
3694 tstate->exc_traceback = f->f_exc_traceback;
3695 f->f_exc_type = NULL;
3696 f->f_exc_value = NULL;
3697 f->f_exc_traceback = NULL;
3698 Py_XDECREF(type);
3699 Py_XDECREF(value);
3700 Py_XDECREF(tb);
3701}
3702
3703
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003704/* Logic for the raise statement (too complicated for inlining).
3705 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003706static int
Collin Winter828f04a2007-08-31 00:04:24 +00003707do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003709 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 if (exc == NULL) {
3712 /* Reraise */
3713 PyThreadState *tstate = PyThreadState_GET();
3714 PyObject *tb;
3715 type = tstate->exc_type;
3716 value = tstate->exc_value;
3717 tb = tstate->exc_traceback;
3718 if (type == Py_None) {
3719 PyErr_SetString(PyExc_RuntimeError,
3720 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003721 return 0;
3722 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 Py_XINCREF(type);
3724 Py_XINCREF(value);
3725 Py_XINCREF(tb);
3726 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003727 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 /* We support the following forms of raise:
3731 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003732 raise <instance>
3733 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003735 if (PyExceptionClass_Check(exc)) {
3736 type = exc;
3737 value = PyObject_CallObject(exc, NULL);
3738 if (value == NULL)
3739 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003740 if (!PyExceptionInstance_Check(value)) {
3741 PyErr_Format(PyExc_TypeError,
3742 "calling %R should have returned an instance of "
3743 "BaseException, not %R",
3744 type, Py_TYPE(value));
3745 goto raise_error;
3746 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 }
3748 else if (PyExceptionInstance_Check(exc)) {
3749 value = exc;
3750 type = PyExceptionInstance_Class(exc);
3751 Py_INCREF(type);
3752 }
3753 else {
3754 /* Not something you can raise. You get an exception
3755 anyway, just not what you specified :-) */
3756 Py_DECREF(exc);
3757 PyErr_SetString(PyExc_TypeError,
3758 "exceptions must derive from BaseException");
3759 goto raise_error;
3760 }
Collin Winter828f04a2007-08-31 00:04:24 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 if (cause) {
3763 PyObject *fixed_cause;
3764 if (PyExceptionClass_Check(cause)) {
3765 fixed_cause = PyObject_CallObject(cause, NULL);
3766 if (fixed_cause == NULL)
3767 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003768 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003770 else if (PyExceptionInstance_Check(cause)) {
3771 fixed_cause = cause;
3772 }
3773 else if (cause == Py_None) {
3774 Py_DECREF(cause);
3775 fixed_cause = NULL;
3776 }
3777 else {
3778 PyErr_SetString(PyExc_TypeError,
3779 "exception causes must derive from "
3780 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 goto raise_error;
3782 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003783 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 }
Collin Winter828f04a2007-08-31 00:04:24 +00003785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003786 PyErr_SetObject(type, value);
3787 /* PyErr_SetObject incref's its arguments */
3788 Py_XDECREF(value);
3789 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003790 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003791
3792raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 Py_XDECREF(value);
3794 Py_XDECREF(type);
3795 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003796 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003797}
3798
Tim Petersd6d010b2001-06-21 02:49:55 +00003799/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003800 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003801
Guido van Rossum0368b722007-05-11 16:50:42 +00003802 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3803 with a variable target.
3804*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003805
Barry Warsawe42b18f1997-08-25 22:13:04 +00003806static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003807unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 int i = 0, j = 0;
3810 Py_ssize_t ll = 0;
3811 PyObject *it; /* iter(v) */
3812 PyObject *w;
3813 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 it = PyObject_GetIter(v);
3818 if (it == NULL)
3819 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 for (; i < argcnt; i++) {
3822 w = PyIter_Next(it);
3823 if (w == NULL) {
3824 /* Iterator done, via error or exhaustion. */
3825 if (!PyErr_Occurred()) {
3826 PyErr_Format(PyExc_ValueError,
3827 "need more than %d value%s to unpack",
3828 i, i == 1 ? "" : "s");
3829 }
3830 goto Error;
3831 }
3832 *--sp = w;
3833 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 if (argcntafter == -1) {
3836 /* We better have exhausted the iterator now. */
3837 w = PyIter_Next(it);
3838 if (w == NULL) {
3839 if (PyErr_Occurred())
3840 goto Error;
3841 Py_DECREF(it);
3842 return 1;
3843 }
3844 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003845 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3846 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 goto Error;
3848 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 l = PySequence_List(it);
3851 if (l == NULL)
3852 goto Error;
3853 *--sp = l;
3854 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 ll = PyList_GET_SIZE(l);
3857 if (ll < argcntafter) {
3858 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3859 argcnt + ll);
3860 goto Error;
3861 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 /* Pop the "after-variable" args off the list. */
3864 for (j = argcntafter; j > 0; j--, i++) {
3865 *--sp = PyList_GET_ITEM(l, ll - j);
3866 }
3867 /* Resize the list. */
3868 Py_SIZE(l) = ll - argcntafter;
3869 Py_DECREF(it);
3870 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003871
Tim Petersd6d010b2001-06-21 02:49:55 +00003872Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 for (; i > 0; i--, sp++)
3874 Py_DECREF(*sp);
3875 Py_XDECREF(it);
3876 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003877}
3878
3879
Guido van Rossum96a42c81992-01-12 02:29:51 +00003880#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003881static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003882prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 printf("%s ", str);
3885 if (PyObject_Print(v, stdout, 0) != 0)
3886 PyErr_Clear(); /* Don't know what else to do */
3887 printf("\n");
3888 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003889}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003890#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003891
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003892static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003893call_exc_trace(Py_tracefunc func, PyObject *self,
3894 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003895{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003896 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01003898 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 if (value == NULL) {
3900 value = Py_None;
3901 Py_INCREF(value);
3902 }
Antoine Pitrou89335212013-11-23 14:05:23 +01003903 PyErr_NormalizeException(&type, &value, &orig_traceback);
3904 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003905 arg = PyTuple_Pack(3, type, value, traceback);
3906 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01003907 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 return;
3909 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003910 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003911 Py_DECREF(arg);
3912 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003913 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 else {
3915 Py_XDECREF(type);
3916 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003917 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003919}
3920
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003921static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003922call_trace_protected(Py_tracefunc func, PyObject *obj,
3923 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003924 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 PyObject *type, *value, *traceback;
3927 int err;
3928 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003929 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 if (err == 0)
3931 {
3932 PyErr_Restore(type, value, traceback);
3933 return 0;
3934 }
3935 else {
3936 Py_XDECREF(type);
3937 Py_XDECREF(value);
3938 Py_XDECREF(traceback);
3939 return -1;
3940 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003941}
3942
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003943static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003944call_trace(Py_tracefunc func, PyObject *obj,
3945 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003947{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003948 int result;
3949 if (tstate->tracing)
3950 return 0;
3951 tstate->tracing++;
3952 tstate->use_tracing = 0;
3953 result = func(obj, frame, what, arg);
3954 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3955 || (tstate->c_profilefunc != NULL));
3956 tstate->tracing--;
3957 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003958}
3959
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003960PyObject *
3961_PyEval_CallTracing(PyObject *func, PyObject *args)
3962{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003963 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 int save_tracing = tstate->tracing;
3965 int save_use_tracing = tstate->use_tracing;
3966 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 tstate->tracing = 0;
3969 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3970 || (tstate->c_profilefunc != NULL));
3971 result = PyObject_Call(func, args, NULL);
3972 tstate->tracing = save_tracing;
3973 tstate->use_tracing = save_use_tracing;
3974 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003975}
3976
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003977/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003978static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003979maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003980 PyThreadState *tstate, PyFrameObject *frame,
3981 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003983 int result = 0;
3984 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003986 /* If the last instruction executed isn't in the current
3987 instruction window, reset the window.
3988 */
3989 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3990 PyAddrPair bounds;
3991 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3992 &bounds);
3993 *instr_lb = bounds.ap_lower;
3994 *instr_ub = bounds.ap_upper;
3995 }
3996 /* If the last instruction falls at the start of a line or if
3997 it represents a jump backwards, update the frame's line
3998 number and call the trace function. */
3999 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4000 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004001 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 }
4003 *instr_prev = frame->f_lasti;
4004 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004005}
4006
Fred Drake5755ce62001-06-27 19:19:46 +00004007void
4008PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 PyThreadState *tstate = PyThreadState_GET();
4011 PyObject *temp = tstate->c_profileobj;
4012 Py_XINCREF(arg);
4013 tstate->c_profilefunc = NULL;
4014 tstate->c_profileobj = NULL;
4015 /* Must make sure that tracing is not ignored if 'temp' is freed */
4016 tstate->use_tracing = tstate->c_tracefunc != NULL;
4017 Py_XDECREF(temp);
4018 tstate->c_profilefunc = func;
4019 tstate->c_profileobj = arg;
4020 /* Flag that tracing or profiling is turned on */
4021 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004022}
4023
4024void
4025PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 PyThreadState *tstate = PyThreadState_GET();
4028 PyObject *temp = tstate->c_traceobj;
4029 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4030 Py_XINCREF(arg);
4031 tstate->c_tracefunc = NULL;
4032 tstate->c_traceobj = NULL;
4033 /* Must make sure that profiling is not ignored if 'temp' is freed */
4034 tstate->use_tracing = tstate->c_profilefunc != NULL;
4035 Py_XDECREF(temp);
4036 tstate->c_tracefunc = func;
4037 tstate->c_traceobj = arg;
4038 /* Flag that tracing or profiling is turned on */
4039 tstate->use_tracing = ((func != NULL)
4040 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004041}
4042
Guido van Rossumb209a111997-04-29 18:18:01 +00004043PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004044PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 PyFrameObject *current_frame = PyEval_GetFrame();
4047 if (current_frame == NULL)
4048 return PyThreadState_GET()->interp->builtins;
4049 else
4050 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004051}
4052
Guido van Rossumb209a111997-04-29 18:18:01 +00004053PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004054PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004055{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004057 if (current_frame == NULL) {
4058 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004060 }
4061
4062 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4063 return NULL;
4064
4065 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004067}
4068
Guido van Rossumb209a111997-04-29 18:18:01 +00004069PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004070PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004071{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 PyFrameObject *current_frame = PyEval_GetFrame();
4073 if (current_frame == NULL)
4074 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004075
4076 assert(current_frame->f_globals != NULL);
4077 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004078}
4079
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004080PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004081PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004082{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 PyThreadState *tstate = PyThreadState_GET();
4084 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004085}
4086
Guido van Rossum6135a871995-01-09 17:53:26 +00004087int
Tim Peters5ba58662001-07-16 02:29:45 +00004088PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004089{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 PyFrameObject *current_frame = PyEval_GetFrame();
4091 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 if (current_frame != NULL) {
4094 const int codeflags = current_frame->f_code->co_flags;
4095 const int compilerflags = codeflags & PyCF_MASK;
4096 if (compilerflags) {
4097 result = 1;
4098 cf->cf_flags |= compilerflags;
4099 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004100#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004101 if (codeflags & CO_GENERATOR_ALLOWED) {
4102 result = 1;
4103 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4104 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004105#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 }
4107 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004108}
4109
Guido van Rossum3f5da241990-12-20 15:06:42 +00004110
Guido van Rossum681d79a1995-07-18 14:51:37 +00004111/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004112 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004113
Guido van Rossumb209a111997-04-29 18:18:01 +00004114PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004115PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004118
Victor Stinnerace47d72013-07-18 01:41:08 +02004119#ifdef Py_DEBUG
4120 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4121 set, because it may clear it (directly or indirectly)
4122 and so the caller looses its exception */
4123 assert(!PyErr_Occurred());
4124#endif
4125
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004126 if (arg == NULL) {
4127 arg = PyTuple_New(0);
4128 if (arg == NULL)
4129 return NULL;
4130 }
4131 else if (!PyTuple_Check(arg)) {
4132 PyErr_SetString(PyExc_TypeError,
4133 "argument list must be a tuple");
4134 return NULL;
4135 }
4136 else
4137 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 if (kw != NULL && !PyDict_Check(kw)) {
4140 PyErr_SetString(PyExc_TypeError,
4141 "keyword list must be a dictionary");
4142 Py_DECREF(arg);
4143 return NULL;
4144 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 result = PyObject_Call(func, arg, kw);
4147 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004148
4149 assert((result != NULL && !PyErr_Occurred())
4150 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004152}
4153
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004154const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004155PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 if (PyMethod_Check(func))
4158 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4159 else if (PyFunction_Check(func))
4160 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4161 else if (PyCFunction_Check(func))
4162 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4163 else
4164 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004165}
4166
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004167const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004168PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004169{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004170 if (PyMethod_Check(func))
4171 return "()";
4172 else if (PyFunction_Check(func))
4173 return "()";
4174 else if (PyCFunction_Check(func))
4175 return "()";
4176 else
4177 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004178}
4179
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004180static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004181err_args(PyObject *func, int flags, int nargs)
4182{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 if (flags & METH_NOARGS)
4184 PyErr_Format(PyExc_TypeError,
4185 "%.200s() takes no arguments (%d given)",
4186 ((PyCFunctionObject *)func)->m_ml->ml_name,
4187 nargs);
4188 else
4189 PyErr_Format(PyExc_TypeError,
4190 "%.200s() takes exactly one argument (%d given)",
4191 ((PyCFunctionObject *)func)->m_ml->ml_name,
4192 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004193}
4194
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004195#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004196if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004197 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4198 tstate, tstate->frame, \
4199 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 x = NULL; \
4201 } \
4202 else { \
4203 x = call; \
4204 if (tstate->c_profilefunc != NULL) { \
4205 if (x == NULL) { \
4206 call_trace_protected(tstate->c_profilefunc, \
4207 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004208 tstate, tstate->frame, \
4209 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 /* XXX should pass (type, value, tb) */ \
4211 } else { \
4212 if (call_trace(tstate->c_profilefunc, \
4213 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004214 tstate, tstate->frame, \
4215 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 Py_DECREF(x); \
4217 x = NULL; \
4218 } \
4219 } \
4220 } \
4221 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004222} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 x = call; \
4224 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004225
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004226static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004227call_function(PyObject ***pp_stack, int oparg
4228#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004230#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 int na = oparg & 0xff;
4234 int nk = (oparg>>8) & 0xff;
4235 int n = na + 2 * nk;
4236 PyObject **pfunc = (*pp_stack) - n - 1;
4237 PyObject *func = *pfunc;
4238 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 /* Always dispatch PyCFunction first, because these are
4241 presumed to be the most frequent callable object.
4242 */
4243 if (PyCFunction_Check(func) && nk == 0) {
4244 int flags = PyCFunction_GET_FLAGS(func);
4245 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 PCALL(PCALL_CFUNCTION);
4248 if (flags & (METH_NOARGS | METH_O)) {
4249 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4250 PyObject *self = PyCFunction_GET_SELF(func);
4251 if (flags & METH_NOARGS && na == 0) {
4252 C_TRACE(x, (*meth)(self,NULL));
4253 }
4254 else if (flags & METH_O && na == 1) {
4255 PyObject *arg = EXT_POP(*pp_stack);
4256 C_TRACE(x, (*meth)(self,arg));
4257 Py_DECREF(arg);
4258 }
4259 else {
4260 err_args(func, flags, na);
4261 x = NULL;
4262 }
4263 }
4264 else {
4265 PyObject *callargs;
4266 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004267 if (callargs != NULL) {
4268 READ_TIMESTAMP(*pintr0);
4269 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4270 READ_TIMESTAMP(*pintr1);
4271 Py_XDECREF(callargs);
4272 }
4273 else {
4274 x = NULL;
4275 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 }
4277 } else {
4278 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4279 /* optimize access to bound methods */
4280 PyObject *self = PyMethod_GET_SELF(func);
4281 PCALL(PCALL_METHOD);
4282 PCALL(PCALL_BOUND_METHOD);
4283 Py_INCREF(self);
4284 func = PyMethod_GET_FUNCTION(func);
4285 Py_INCREF(func);
4286 Py_DECREF(*pfunc);
4287 *pfunc = self;
4288 na++;
4289 n++;
4290 } else
4291 Py_INCREF(func);
4292 READ_TIMESTAMP(*pintr0);
4293 if (PyFunction_Check(func))
4294 x = fast_function(func, pp_stack, n, na, nk);
4295 else
4296 x = do_call(func, pp_stack, na, nk);
4297 READ_TIMESTAMP(*pintr1);
4298 Py_DECREF(func);
4299 }
Victor Stinnerf243ee42013-07-16 01:02:12 +02004300 assert((x != NULL && !PyErr_Occurred())
4301 || (x == NULL && PyErr_Occurred()));
Tim Peters8a5c3c72004-04-05 19:36:21 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 /* Clear the stack of the function object. Also removes
4304 the arguments in case they weren't consumed already
4305 (fast_function() and err_args() leave them on the stack).
4306 */
4307 while ((*pp_stack) > pfunc) {
4308 w = EXT_POP(*pp_stack);
4309 Py_DECREF(w);
4310 PCALL(PCALL_POP);
4311 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004312
4313 assert((x != NULL && !PyErr_Occurred())
4314 || (x == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004316}
4317
Jeremy Hylton192690e2002-08-16 18:36:11 +00004318/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004319 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004320 For the simplest case -- a function that takes only positional
4321 arguments and is called with only positional arguments -- it
4322 inlines the most primitive frame setup code from
4323 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4324 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004325*/
4326
4327static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004328fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4331 PyObject *globals = PyFunction_GET_GLOBALS(func);
4332 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4333 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Victor Stinner40ee3012014-06-16 15:59:28 +02004334 PyObject *name = ((PyFunctionObject *)func) -> func_name;
4335 PyObject *qualname = ((PyFunctionObject *)func) -> func_qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 PyObject **d = NULL;
4337 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 PCALL(PCALL_FUNCTION);
4340 PCALL(PCALL_FAST_FUNCTION);
4341 if (argdefs == NULL && co->co_argcount == n &&
4342 co->co_kwonlyargcount == 0 && nk==0 &&
4343 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4344 PyFrameObject *f;
4345 PyObject *retval = NULL;
4346 PyThreadState *tstate = PyThreadState_GET();
4347 PyObject **fastlocals, **stack;
4348 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 PCALL(PCALL_FASTER_FUNCTION);
4351 assert(globals != NULL);
4352 /* XXX Perhaps we should create a specialized
4353 PyFrame_New() that doesn't take locals, but does
4354 take builtins without sanity checking them.
4355 */
4356 assert(tstate != NULL);
4357 f = PyFrame_New(tstate, co, globals, NULL);
4358 if (f == NULL)
4359 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 fastlocals = f->f_localsplus;
4362 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 for (i = 0; i < n; i++) {
4365 Py_INCREF(*stack);
4366 fastlocals[i] = *stack++;
4367 }
4368 retval = PyEval_EvalFrameEx(f,0);
4369 ++tstate->recursion_depth;
4370 Py_DECREF(f);
4371 --tstate->recursion_depth;
4372 return retval;
4373 }
4374 if (argdefs != NULL) {
4375 d = &PyTuple_GET_ITEM(argdefs, 0);
4376 nd = Py_SIZE(argdefs);
4377 }
Victor Stinner40ee3012014-06-16 15:59:28 +02004378 return _PyEval_EvalCodeWithName((PyObject*)co, globals,
4379 (PyObject *)NULL, (*pp_stack)-n, na,
4380 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4381 PyFunction_GET_CLOSURE(func),
4382 name, qualname);
Jeremy Hylton52820442001-01-03 23:52:36 +00004383}
4384
4385static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004386update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4387 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004388{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 PyObject *kwdict = NULL;
4390 if (orig_kwdict == NULL)
4391 kwdict = PyDict_New();
4392 else {
4393 kwdict = PyDict_Copy(orig_kwdict);
4394 Py_DECREF(orig_kwdict);
4395 }
4396 if (kwdict == NULL)
4397 return NULL;
4398 while (--nk >= 0) {
4399 int err;
4400 PyObject *value = EXT_POP(*pp_stack);
4401 PyObject *key = EXT_POP(*pp_stack);
4402 if (PyDict_GetItem(kwdict, key) != NULL) {
4403 PyErr_Format(PyExc_TypeError,
4404 "%.200s%s got multiple values "
4405 "for keyword argument '%U'",
4406 PyEval_GetFuncName(func),
4407 PyEval_GetFuncDesc(func),
4408 key);
4409 Py_DECREF(key);
4410 Py_DECREF(value);
4411 Py_DECREF(kwdict);
4412 return NULL;
4413 }
4414 err = PyDict_SetItem(kwdict, key, value);
4415 Py_DECREF(key);
4416 Py_DECREF(value);
4417 if (err) {
4418 Py_DECREF(kwdict);
4419 return NULL;
4420 }
4421 }
4422 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004423}
4424
4425static PyObject *
4426update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004427 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004428{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004431 callargs = PyTuple_New(nstack + nstar);
4432 if (callargs == NULL) {
4433 return NULL;
4434 }
4435 if (nstar) {
4436 int i;
4437 for (i = 0; i < nstar; i++) {
4438 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4439 Py_INCREF(a);
4440 PyTuple_SET_ITEM(callargs, nstack + i, a);
4441 }
4442 }
4443 while (--nstack >= 0) {
4444 w = EXT_POP(*pp_stack);
4445 PyTuple_SET_ITEM(callargs, nstack, w);
4446 }
4447 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004448}
4449
4450static PyObject *
4451load_args(PyObject ***pp_stack, int na)
4452{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 PyObject *args = PyTuple_New(na);
4454 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 if (args == NULL)
4457 return NULL;
4458 while (--na >= 0) {
4459 w = EXT_POP(*pp_stack);
4460 PyTuple_SET_ITEM(args, na, w);
4461 }
4462 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004463}
4464
4465static PyObject *
4466do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4467{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 PyObject *callargs = NULL;
4469 PyObject *kwdict = NULL;
4470 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 if (nk > 0) {
4473 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4474 if (kwdict == NULL)
4475 goto call_fail;
4476 }
4477 callargs = load_args(pp_stack, na);
4478 if (callargs == NULL)
4479 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004480#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 /* At this point, we have to look at the type of func to
4482 update the call stats properly. Do it here so as to avoid
4483 exposing the call stats machinery outside ceval.c
4484 */
4485 if (PyFunction_Check(func))
4486 PCALL(PCALL_FUNCTION);
4487 else if (PyMethod_Check(func))
4488 PCALL(PCALL_METHOD);
4489 else if (PyType_Check(func))
4490 PCALL(PCALL_TYPE);
4491 else if (PyCFunction_Check(func))
4492 PCALL(PCALL_CFUNCTION);
4493 else
4494 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004495#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004496 if (PyCFunction_Check(func)) {
4497 PyThreadState *tstate = PyThreadState_GET();
4498 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4499 }
4500 else
4501 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004502call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 Py_XDECREF(callargs);
4504 Py_XDECREF(kwdict);
4505 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004506}
4507
4508static PyObject *
4509ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 int nstar = 0;
4512 PyObject *callargs = NULL;
4513 PyObject *stararg = NULL;
4514 PyObject *kwdict = NULL;
4515 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004517 if (flags & CALL_FLAG_KW) {
4518 kwdict = EXT_POP(*pp_stack);
4519 if (!PyDict_Check(kwdict)) {
4520 PyObject *d;
4521 d = PyDict_New();
4522 if (d == NULL)
4523 goto ext_call_fail;
4524 if (PyDict_Update(d, kwdict) != 0) {
4525 Py_DECREF(d);
4526 /* PyDict_Update raises attribute
4527 * error (percolated from an attempt
4528 * to get 'keys' attribute) instead of
4529 * a type error if its second argument
4530 * is not a mapping.
4531 */
4532 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4533 PyErr_Format(PyExc_TypeError,
4534 "%.200s%.200s argument after ** "
4535 "must be a mapping, not %.200s",
4536 PyEval_GetFuncName(func),
4537 PyEval_GetFuncDesc(func),
4538 kwdict->ob_type->tp_name);
4539 }
4540 goto ext_call_fail;
4541 }
4542 Py_DECREF(kwdict);
4543 kwdict = d;
4544 }
4545 }
4546 if (flags & CALL_FLAG_VAR) {
4547 stararg = EXT_POP(*pp_stack);
4548 if (!PyTuple_Check(stararg)) {
4549 PyObject *t = NULL;
4550 t = PySequence_Tuple(stararg);
4551 if (t == NULL) {
4552 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4553 PyErr_Format(PyExc_TypeError,
4554 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004555 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 PyEval_GetFuncName(func),
4557 PyEval_GetFuncDesc(func),
4558 stararg->ob_type->tp_name);
4559 }
4560 goto ext_call_fail;
4561 }
4562 Py_DECREF(stararg);
4563 stararg = t;
4564 }
4565 nstar = PyTuple_GET_SIZE(stararg);
4566 }
4567 if (nk > 0) {
4568 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4569 if (kwdict == NULL)
4570 goto ext_call_fail;
4571 }
4572 callargs = update_star_args(na, nstar, stararg, pp_stack);
4573 if (callargs == NULL)
4574 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004575#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 /* At this point, we have to look at the type of func to
4577 update the call stats properly. Do it here so as to avoid
4578 exposing the call stats machinery outside ceval.c
4579 */
4580 if (PyFunction_Check(func))
4581 PCALL(PCALL_FUNCTION);
4582 else if (PyMethod_Check(func))
4583 PCALL(PCALL_METHOD);
4584 else if (PyType_Check(func))
4585 PCALL(PCALL_TYPE);
4586 else if (PyCFunction_Check(func))
4587 PCALL(PCALL_CFUNCTION);
4588 else
4589 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004590#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 if (PyCFunction_Check(func)) {
4592 PyThreadState *tstate = PyThreadState_GET();
4593 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4594 }
4595 else
4596 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004597ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 Py_XDECREF(callargs);
4599 Py_XDECREF(kwdict);
4600 Py_XDECREF(stararg);
Victor Stinnerf243ee42013-07-16 01:02:12 +02004601 assert((result != NULL && !PyErr_Occurred())
4602 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004604}
4605
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004606/* Extract a slice index from a PyInt or PyLong or an object with the
4607 nb_index slot defined, and store in *pi.
4608 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4609 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 +00004610 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004611*/
Tim Petersb5196382001-12-16 19:44:20 +00004612/* Note: If v is NULL, return success without storing into *pi. This
4613 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4614 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004615*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004616int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004617_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004619 if (v != NULL) {
4620 Py_ssize_t x;
4621 if (PyIndex_Check(v)) {
4622 x = PyNumber_AsSsize_t(v, NULL);
4623 if (x == -1 && PyErr_Occurred())
4624 return 0;
4625 }
4626 else {
4627 PyErr_SetString(PyExc_TypeError,
4628 "slice indices must be integers or "
4629 "None or have an __index__ method");
4630 return 0;
4631 }
4632 *pi = x;
4633 }
4634 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004635}
4636
Guido van Rossum486364b2007-06-30 05:01:58 +00004637#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004639
Guido van Rossumb209a111997-04-29 18:18:01 +00004640static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004641cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 int res = 0;
4644 switch (op) {
4645 case PyCmp_IS:
4646 res = (v == w);
4647 break;
4648 case PyCmp_IS_NOT:
4649 res = (v != w);
4650 break;
4651 case PyCmp_IN:
4652 res = PySequence_Contains(w, v);
4653 if (res < 0)
4654 return NULL;
4655 break;
4656 case PyCmp_NOT_IN:
4657 res = PySequence_Contains(w, v);
4658 if (res < 0)
4659 return NULL;
4660 res = !res;
4661 break;
4662 case PyCmp_EXC_MATCH:
4663 if (PyTuple_Check(w)) {
4664 Py_ssize_t i, length;
4665 length = PyTuple_Size(w);
4666 for (i = 0; i < length; i += 1) {
4667 PyObject *exc = PyTuple_GET_ITEM(w, i);
4668 if (!PyExceptionClass_Check(exc)) {
4669 PyErr_SetString(PyExc_TypeError,
4670 CANNOT_CATCH_MSG);
4671 return NULL;
4672 }
4673 }
4674 }
4675 else {
4676 if (!PyExceptionClass_Check(w)) {
4677 PyErr_SetString(PyExc_TypeError,
4678 CANNOT_CATCH_MSG);
4679 return NULL;
4680 }
4681 }
4682 res = PyErr_GivenExceptionMatches(v, w);
4683 break;
4684 default:
4685 return PyObject_RichCompare(v, w, op);
4686 }
4687 v = res ? Py_True : Py_False;
4688 Py_INCREF(v);
4689 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004690}
4691
Thomas Wouters52152252000-08-17 22:55:00 +00004692static PyObject *
4693import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004696 _Py_IDENTIFIER(__name__);
4697 PyObject *fullmodname, *pkgname;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004699 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004700 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
4701 return x;
4702 /* Issue #17636: in case this failed because of a circular relative
4703 import, try to fallback on reading the module directly from
4704 sys.modules. */
4705 PyErr_Clear();
4706 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
4707 if (pkgname == NULL)
4708 return NULL;
4709 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
4710 Py_DECREF(pkgname);
4711 if (fullmodname == NULL)
4712 return NULL;
4713 x = PyDict_GetItem(PyImport_GetModuleDict(), fullmodname);
4714 if (x == NULL)
Brett Cannona79e4fb2013-07-12 11:22:26 -04004715 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004716 else
4717 Py_INCREF(x);
4718 Py_DECREF(fullmodname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004720}
Guido van Rossumac7be682001-01-17 15:42:30 +00004721
Thomas Wouters52152252000-08-17 22:55:00 +00004722static int
4723import_all_from(PyObject *locals, PyObject *v)
4724{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004725 _Py_IDENTIFIER(__all__);
4726 _Py_IDENTIFIER(__dict__);
4727 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004728 PyObject *dict, *name, *value;
4729 int skip_leading_underscores = 0;
4730 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 if (all == NULL) {
4733 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4734 return -1; /* Unexpected error */
4735 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004736 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004737 if (dict == NULL) {
4738 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4739 return -1;
4740 PyErr_SetString(PyExc_ImportError,
4741 "from-import-* object has no __dict__ and no __all__");
4742 return -1;
4743 }
4744 all = PyMapping_Keys(dict);
4745 Py_DECREF(dict);
4746 if (all == NULL)
4747 return -1;
4748 skip_leading_underscores = 1;
4749 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 for (pos = 0, err = 0; ; pos++) {
4752 name = PySequence_GetItem(all, pos);
4753 if (name == NULL) {
4754 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4755 err = -1;
4756 else
4757 PyErr_Clear();
4758 break;
4759 }
4760 if (skip_leading_underscores &&
4761 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004762 PyUnicode_READY(name) != -1 &&
4763 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 {
4765 Py_DECREF(name);
4766 continue;
4767 }
4768 value = PyObject_GetAttr(v, name);
4769 if (value == NULL)
4770 err = -1;
4771 else if (PyDict_CheckExact(locals))
4772 err = PyDict_SetItem(locals, name, value);
4773 else
4774 err = PyObject_SetItem(locals, name, value);
4775 Py_DECREF(name);
4776 Py_XDECREF(value);
4777 if (err != 0)
4778 break;
4779 }
4780 Py_DECREF(all);
4781 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004782}
4783
Guido van Rossumac7be682001-01-17 15:42:30 +00004784static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004785format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004787 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004789 if (!obj)
4790 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004792 obj_str = _PyUnicode_AsString(obj);
4793 if (!obj_str)
4794 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004796 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004797}
Guido van Rossum950361c1997-01-24 13:49:28 +00004798
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004799static void
4800format_exc_unbound(PyCodeObject *co, int oparg)
4801{
4802 PyObject *name;
4803 /* Don't stomp existing exception */
4804 if (PyErr_Occurred())
4805 return;
4806 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4807 name = PyTuple_GET_ITEM(co->co_cellvars,
4808 oparg);
4809 format_exc_check_arg(
4810 PyExc_UnboundLocalError,
4811 UNBOUNDLOCAL_ERROR_MSG,
4812 name);
4813 } else {
4814 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4815 PyTuple_GET_SIZE(co->co_cellvars));
4816 format_exc_check_arg(PyExc_NameError,
4817 UNBOUNDFREE_ERROR_MSG, name);
4818 }
4819}
4820
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004821static PyObject *
4822unicode_concatenate(PyObject *v, PyObject *w,
4823 PyFrameObject *f, unsigned char *next_instr)
4824{
4825 PyObject *res;
4826 if (Py_REFCNT(v) == 2) {
4827 /* In the common case, there are 2 references to the value
4828 * stored in 'variable' when the += is performed: one on the
4829 * value stack (in 'v') and one still stored in the
4830 * 'variable'. We try to delete the variable now to reduce
4831 * the refcnt to 1.
4832 */
4833 switch (*next_instr) {
4834 case STORE_FAST:
4835 {
4836 int oparg = PEEKARG();
4837 PyObject **fastlocals = f->f_localsplus;
4838 if (GETLOCAL(oparg) == v)
4839 SETLOCAL(oparg, NULL);
4840 break;
4841 }
4842 case STORE_DEREF:
4843 {
4844 PyObject **freevars = (f->f_localsplus +
4845 f->f_code->co_nlocals);
4846 PyObject *c = freevars[PEEKARG()];
4847 if (PyCell_GET(c) == v)
4848 PyCell_Set(c, NULL);
4849 break;
4850 }
4851 case STORE_NAME:
4852 {
4853 PyObject *names = f->f_code->co_names;
4854 PyObject *name = GETITEM(names, PEEKARG());
4855 PyObject *locals = f->f_locals;
4856 if (PyDict_CheckExact(locals) &&
4857 PyDict_GetItem(locals, name) == v) {
4858 if (PyDict_DelItem(locals, name) != 0) {
4859 PyErr_Clear();
4860 }
4861 }
4862 break;
4863 }
4864 }
4865 }
4866 res = v;
4867 PyUnicode_Append(&res, w);
4868 return res;
4869}
4870
Guido van Rossum950361c1997-01-24 13:49:28 +00004871#ifdef DYNAMIC_EXECUTION_PROFILE
4872
Skip Montanarof118cb12001-10-15 20:51:38 +00004873static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004874getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004876 int i;
4877 PyObject *l = PyList_New(256);
4878 if (l == NULL) return NULL;
4879 for (i = 0; i < 256; i++) {
4880 PyObject *x = PyLong_FromLong(a[i]);
4881 if (x == NULL) {
4882 Py_DECREF(l);
4883 return NULL;
4884 }
4885 PyList_SetItem(l, i, x);
4886 }
4887 for (i = 0; i < 256; i++)
4888 a[i] = 0;
4889 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004890}
4891
4892PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004893_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004894{
4895#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004896 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004897#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 int i;
4899 PyObject *l = PyList_New(257);
4900 if (l == NULL) return NULL;
4901 for (i = 0; i < 257; i++) {
4902 PyObject *x = getarray(dxpairs[i]);
4903 if (x == NULL) {
4904 Py_DECREF(l);
4905 return NULL;
4906 }
4907 PyList_SetItem(l, i, x);
4908 }
4909 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004910#endif
4911}
4912
4913#endif