blob: 34c52b032fd73258b3fc0df9e46d5b0bb0b4c37b [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);
1270 if (PyThreadState_Swap(tstate) != NULL)
1271 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001273#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 /* Check for asynchronous exceptions. */
1275 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001276 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 tstate->async_exc = NULL;
1278 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001279 PyErr_SetNone(exc);
1280 Py_DECREF(exc);
1281 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 }
1283 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 fast_next_opcode:
1286 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001291 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001292 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 /* see maybe_call_line_trace
1294 for expository comments */
1295 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 err = maybe_call_line_trace(tstate->c_tracefunc,
1298 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001299 tstate, f,
1300 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 /* Reload possibly changed frame fields */
1302 JUMPTO(f->f_lasti);
1303 if (f->f_stacktop != NULL) {
1304 stack_pointer = f->f_stacktop;
1305 f->f_stacktop = NULL;
1306 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001307 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001309 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 opcode = NEXTOP();
1315 oparg = 0; /* allows oparg to be stored in a register because
1316 it doesn't have to be remembered across a full loop */
1317 if (HAS_ARG(opcode))
1318 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001319 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001320#ifdef DYNAMIC_EXECUTION_PROFILE
1321#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 dxpairs[lastopcode][opcode]++;
1323 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001324#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001326#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001327
Guido van Rossum96a42c81992-01-12 02:29:51 +00001328#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (lltrace) {
1332 if (HAS_ARG(opcode)) {
1333 printf("%d: %d, %d\n",
1334 f->f_lasti, opcode, oparg);
1335 }
1336 else {
1337 printf("%d: %d\n",
1338 f->f_lasti, opcode);
1339 }
1340 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001341#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 /* Main switch on opcode */
1344 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 /* BEWARE!
1349 It is essential that any operation that fails sets either
1350 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1351 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 TARGET(NOP)
1354 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001355
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 TARGET(LOAD_FAST) {
1357 PyObject *value = GETLOCAL(oparg);
1358 if (value == NULL) {
1359 format_exc_check_arg(PyExc_UnboundLocalError,
1360 UNBOUNDLOCAL_ERROR_MSG,
1361 PyTuple_GetItem(co->co_varnames, oparg));
1362 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 Py_INCREF(value);
1365 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001367 }
1368
1369 TARGET(LOAD_CONST) {
1370 PyObject *value = GETITEM(consts, oparg);
1371 Py_INCREF(value);
1372 PUSH(value);
1373 FAST_DISPATCH();
1374 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001377 TARGET(STORE_FAST) {
1378 PyObject *value = POP();
1379 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001381 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001382
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001383 TARGET(POP_TOP) {
1384 PyObject *value = POP();
1385 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001387 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001389 TARGET(ROT_TWO) {
1390 PyObject *top = TOP();
1391 PyObject *second = SECOND();
1392 SET_TOP(second);
1393 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 TARGET(ROT_THREE) {
1398 PyObject *top = TOP();
1399 PyObject *second = SECOND();
1400 PyObject *third = THIRD();
1401 SET_TOP(second);
1402 SET_SECOND(third);
1403 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 TARGET(DUP_TOP) {
1408 PyObject *top = TOP();
1409 Py_INCREF(top);
1410 PUSH(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_TWO) {
1415 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001416 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001417 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001418 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001419 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001420 SET_TOP(top);
1421 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001422 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001424
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 TARGET(UNARY_POSITIVE) {
1426 PyObject *value = TOP();
1427 PyObject *res = PyNumber_Positive(value);
1428 Py_DECREF(value);
1429 SET_TOP(res);
1430 if (res == NULL)
1431 goto error;
1432 DISPATCH();
1433 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001435 TARGET(UNARY_NEGATIVE) {
1436 PyObject *value = TOP();
1437 PyObject *res = PyNumber_Negative(value);
1438 Py_DECREF(value);
1439 SET_TOP(res);
1440 if (res == NULL)
1441 goto error;
1442 DISPATCH();
1443 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 TARGET(UNARY_NOT) {
1446 PyObject *value = TOP();
1447 int err = PyObject_IsTrue(value);
1448 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 if (err == 0) {
1450 Py_INCREF(Py_True);
1451 SET_TOP(Py_True);
1452 DISPATCH();
1453 }
1454 else if (err > 0) {
1455 Py_INCREF(Py_False);
1456 SET_TOP(Py_False);
1457 err = 0;
1458 DISPATCH();
1459 }
1460 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001461 goto error;
1462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001464 TARGET(UNARY_INVERT) {
1465 PyObject *value = TOP();
1466 PyObject *res = PyNumber_Invert(value);
1467 Py_DECREF(value);
1468 SET_TOP(res);
1469 if (res == NULL)
1470 goto error;
1471 DISPATCH();
1472 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001473
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001474 TARGET(BINARY_POWER) {
1475 PyObject *exp = POP();
1476 PyObject *base = TOP();
1477 PyObject *res = PyNumber_Power(base, exp, Py_None);
1478 Py_DECREF(base);
1479 Py_DECREF(exp);
1480 SET_TOP(res);
1481 if (res == NULL)
1482 goto error;
1483 DISPATCH();
1484 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001486 TARGET(BINARY_MULTIPLY) {
1487 PyObject *right = POP();
1488 PyObject *left = TOP();
1489 PyObject *res = PyNumber_Multiply(left, right);
1490 Py_DECREF(left);
1491 Py_DECREF(right);
1492 SET_TOP(res);
1493 if (res == NULL)
1494 goto error;
1495 DISPATCH();
1496 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001497
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001498 TARGET(BINARY_TRUE_DIVIDE) {
1499 PyObject *divisor = POP();
1500 PyObject *dividend = TOP();
1501 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1502 Py_DECREF(dividend);
1503 Py_DECREF(divisor);
1504 SET_TOP(quotient);
1505 if (quotient == NULL)
1506 goto error;
1507 DISPATCH();
1508 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001510 TARGET(BINARY_FLOOR_DIVIDE) {
1511 PyObject *divisor = POP();
1512 PyObject *dividend = TOP();
1513 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1514 Py_DECREF(dividend);
1515 Py_DECREF(divisor);
1516 SET_TOP(quotient);
1517 if (quotient == NULL)
1518 goto error;
1519 DISPATCH();
1520 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001521
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001522 TARGET(BINARY_MODULO) {
1523 PyObject *divisor = POP();
1524 PyObject *dividend = TOP();
1525 PyObject *res = PyUnicode_CheckExact(dividend) ?
1526 PyUnicode_Format(dividend, divisor) :
1527 PyNumber_Remainder(dividend, divisor);
1528 Py_DECREF(divisor);
1529 Py_DECREF(dividend);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 TARGET(BINARY_ADD) {
1537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *sum;
1540 if (PyUnicode_CheckExact(left) &&
1541 PyUnicode_CheckExact(right)) {
1542 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001543 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001544 }
1545 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001546 sum = PyNumber_Add(left, right);
1547 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001548 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001549 Py_DECREF(right);
1550 SET_TOP(sum);
1551 if (sum == NULL)
1552 goto error;
1553 DISPATCH();
1554 }
1555
1556 TARGET(BINARY_SUBTRACT) {
1557 PyObject *right = POP();
1558 PyObject *left = TOP();
1559 PyObject *diff = PyNumber_Subtract(left, right);
1560 Py_DECREF(right);
1561 Py_DECREF(left);
1562 SET_TOP(diff);
1563 if (diff == NULL)
1564 goto error;
1565 DISPATCH();
1566 }
1567
1568 TARGET(BINARY_SUBSCR) {
1569 PyObject *sub = POP();
1570 PyObject *container = TOP();
1571 PyObject *res = PyObject_GetItem(container, sub);
1572 Py_DECREF(container);
1573 Py_DECREF(sub);
1574 SET_TOP(res);
1575 if (res == NULL)
1576 goto error;
1577 DISPATCH();
1578 }
1579
1580 TARGET(BINARY_LSHIFT) {
1581 PyObject *right = POP();
1582 PyObject *left = TOP();
1583 PyObject *res = PyNumber_Lshift(left, right);
1584 Py_DECREF(left);
1585 Py_DECREF(right);
1586 SET_TOP(res);
1587 if (res == NULL)
1588 goto error;
1589 DISPATCH();
1590 }
1591
1592 TARGET(BINARY_RSHIFT) {
1593 PyObject *right = POP();
1594 PyObject *left = TOP();
1595 PyObject *res = PyNumber_Rshift(left, right);
1596 Py_DECREF(left);
1597 Py_DECREF(right);
1598 SET_TOP(res);
1599 if (res == NULL)
1600 goto error;
1601 DISPATCH();
1602 }
1603
1604 TARGET(BINARY_AND) {
1605 PyObject *right = POP();
1606 PyObject *left = TOP();
1607 PyObject *res = PyNumber_And(left, right);
1608 Py_DECREF(left);
1609 Py_DECREF(right);
1610 SET_TOP(res);
1611 if (res == NULL)
1612 goto error;
1613 DISPATCH();
1614 }
1615
1616 TARGET(BINARY_XOR) {
1617 PyObject *right = POP();
1618 PyObject *left = TOP();
1619 PyObject *res = PyNumber_Xor(left, right);
1620 Py_DECREF(left);
1621 Py_DECREF(right);
1622 SET_TOP(res);
1623 if (res == NULL)
1624 goto error;
1625 DISPATCH();
1626 }
1627
1628 TARGET(BINARY_OR) {
1629 PyObject *right = POP();
1630 PyObject *left = TOP();
1631 PyObject *res = PyNumber_Or(left, right);
1632 Py_DECREF(left);
1633 Py_DECREF(right);
1634 SET_TOP(res);
1635 if (res == NULL)
1636 goto error;
1637 DISPATCH();
1638 }
1639
1640 TARGET(LIST_APPEND) {
1641 PyObject *v = POP();
1642 PyObject *list = PEEK(oparg);
1643 int err;
1644 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001646 if (err != 0)
1647 goto error;
1648 PREDICT(JUMP_ABSOLUTE);
1649 DISPATCH();
1650 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001651
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001652 TARGET(SET_ADD) {
1653 PyObject *v = POP();
1654 PyObject *set = stack_pointer[-oparg];
1655 int err;
1656 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001658 if (err != 0)
1659 goto error;
1660 PREDICT(JUMP_ABSOLUTE);
1661 DISPATCH();
1662 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001664 TARGET(INPLACE_POWER) {
1665 PyObject *exp = POP();
1666 PyObject *base = TOP();
1667 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1668 Py_DECREF(base);
1669 Py_DECREF(exp);
1670 SET_TOP(res);
1671 if (res == NULL)
1672 goto error;
1673 DISPATCH();
1674 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 TARGET(INPLACE_MULTIPLY) {
1677 PyObject *right = POP();
1678 PyObject *left = TOP();
1679 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1680 Py_DECREF(left);
1681 Py_DECREF(right);
1682 SET_TOP(res);
1683 if (res == NULL)
1684 goto error;
1685 DISPATCH();
1686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 TARGET(INPLACE_TRUE_DIVIDE) {
1689 PyObject *divisor = POP();
1690 PyObject *dividend = TOP();
1691 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1692 Py_DECREF(dividend);
1693 Py_DECREF(divisor);
1694 SET_TOP(quotient);
1695 if (quotient == NULL)
1696 goto error;
1697 DISPATCH();
1698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001700 TARGET(INPLACE_FLOOR_DIVIDE) {
1701 PyObject *divisor = POP();
1702 PyObject *dividend = TOP();
1703 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1704 Py_DECREF(dividend);
1705 Py_DECREF(divisor);
1706 SET_TOP(quotient);
1707 if (quotient == NULL)
1708 goto error;
1709 DISPATCH();
1710 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001711
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001712 TARGET(INPLACE_MODULO) {
1713 PyObject *right = POP();
1714 PyObject *left = TOP();
1715 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1716 Py_DECREF(left);
1717 Py_DECREF(right);
1718 SET_TOP(mod);
1719 if (mod == NULL)
1720 goto error;
1721 DISPATCH();
1722 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001724 TARGET(INPLACE_ADD) {
1725 PyObject *right = POP();
1726 PyObject *left = TOP();
1727 PyObject *sum;
1728 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1729 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001730 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001731 }
1732 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 sum = PyNumber_InPlaceAdd(left, right);
1734 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001735 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001736 Py_DECREF(right);
1737 SET_TOP(sum);
1738 if (sum == 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_SUBTRACT) {
1744 PyObject *right = POP();
1745 PyObject *left = TOP();
1746 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1747 Py_DECREF(left);
1748 Py_DECREF(right);
1749 SET_TOP(diff);
1750 if (diff == 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_LSHIFT) {
1756 PyObject *right = POP();
1757 PyObject *left = TOP();
1758 PyObject *res = PyNumber_InPlaceLshift(left, right);
1759 Py_DECREF(left);
1760 Py_DECREF(right);
1761 SET_TOP(res);
1762 if (res == NULL)
1763 goto error;
1764 DISPATCH();
1765 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001766
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001767 TARGET(INPLACE_RSHIFT) {
1768 PyObject *right = POP();
1769 PyObject *left = TOP();
1770 PyObject *res = PyNumber_InPlaceRshift(left, right);
1771 Py_DECREF(left);
1772 Py_DECREF(right);
1773 SET_TOP(res);
1774 if (res == NULL)
1775 goto error;
1776 DISPATCH();
1777 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001779 TARGET(INPLACE_AND) {
1780 PyObject *right = POP();
1781 PyObject *left = TOP();
1782 PyObject *res = PyNumber_InPlaceAnd(left, right);
1783 Py_DECREF(left);
1784 Py_DECREF(right);
1785 SET_TOP(res);
1786 if (res == NULL)
1787 goto error;
1788 DISPATCH();
1789 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001790
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 TARGET(INPLACE_XOR) {
1792 PyObject *right = POP();
1793 PyObject *left = TOP();
1794 PyObject *res = PyNumber_InPlaceXor(left, right);
1795 Py_DECREF(left);
1796 Py_DECREF(right);
1797 SET_TOP(res);
1798 if (res == NULL)
1799 goto error;
1800 DISPATCH();
1801 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001802
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001803 TARGET(INPLACE_OR) {
1804 PyObject *right = POP();
1805 PyObject *left = TOP();
1806 PyObject *res = PyNumber_InPlaceOr(left, right);
1807 Py_DECREF(left);
1808 Py_DECREF(right);
1809 SET_TOP(res);
1810 if (res == NULL)
1811 goto error;
1812 DISPATCH();
1813 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001814
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001815 TARGET(STORE_SUBSCR) {
1816 PyObject *sub = TOP();
1817 PyObject *container = SECOND();
1818 PyObject *v = THIRD();
1819 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001820 STACKADJ(-3);
1821 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 Py_DECREF(container);
1825 Py_DECREF(sub);
1826 if (err != 0)
1827 goto error;
1828 DISPATCH();
1829 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 TARGET(DELETE_SUBSCR) {
1832 PyObject *sub = TOP();
1833 PyObject *container = SECOND();
1834 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 STACKADJ(-2);
1836 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 err = PyObject_DelItem(container, sub);
1838 Py_DECREF(container);
1839 Py_DECREF(sub);
1840 if (err != 0)
1841 goto error;
1842 DISPATCH();
1843 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001844
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001846 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001847 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001848 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001849 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001850 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 PyErr_SetString(PyExc_RuntimeError,
1852 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001853 Py_DECREF(value);
1854 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001856 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001857 Py_DECREF(value);
1858 if (res == NULL)
1859 goto error;
1860 Py_DECREF(res);
1861 DISPATCH();
1862 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001863
Thomas Wouters434d0822000-08-24 20:11:32 +00001864#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001866#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001867 TARGET(RAISE_VARARGS) {
1868 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 switch (oparg) {
1870 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 if (do_raise(exc, cause)) {
1876 why = WHY_EXCEPTION;
1877 goto fast_block_end;
1878 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 break;
1880 default:
1881 PyErr_SetString(PyExc_SystemError,
1882 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 break;
1884 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001885 goto error;
1886 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001887
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 retval = POP();
1890 why = WHY_RETURN;
1891 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001893
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 TARGET(YIELD_FROM) {
1895 PyObject *v = POP();
1896 PyObject *reciever = TOP();
1897 int err;
1898 if (PyGen_CheckExact(reciever)) {
1899 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001900 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001901 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001902 if (v == Py_None)
1903 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001904 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001905 retval = _PyObject_CallMethodId(reciever, &PyId_send, "O", v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001906 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 Py_DECREF(v);
1908 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001909 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001910 if (tstate->c_tracefunc != NULL
1911 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001912 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001913 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001914 if (err < 0)
1915 goto error;
1916 Py_DECREF(reciever);
1917 SET_TOP(val);
1918 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001919 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001920 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001921 f->f_stacktop = stack_pointer;
1922 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001923 /* and repeat... */
1924 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001925 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001927
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 retval = POP();
1930 f->f_stacktop = stack_pointer;
1931 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001934
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 TARGET(POP_EXCEPT) {
1936 PyTryBlock *b = PyFrame_BlockPop(f);
1937 if (b->b_type != EXCEPT_HANDLER) {
1938 PyErr_SetString(PyExc_SystemError,
1939 "popped block is not an except handler");
1940 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001945
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 TARGET(POP_BLOCK) {
1947 PyTryBlock *b = PyFrame_BlockPop(f);
1948 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 TARGET(END_FINALLY) {
1954 PyObject *status = POP();
1955 if (PyLong_Check(status)) {
1956 why = (enum why_code) PyLong_AS_LONG(status);
1957 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 if (why == WHY_RETURN ||
1959 why == WHY_CONTINUE)
1960 retval = POP();
1961 if (why == WHY_SILENCED) {
1962 /* An exception was silenced by 'with', we must
1963 manually unwind the EXCEPT_HANDLER block which was
1964 created when the exception was caught, otherwise
1965 the stack will be in an inconsistent state. */
1966 PyTryBlock *b = PyFrame_BlockPop(f);
1967 assert(b->b_type == EXCEPT_HANDLER);
1968 UNWIND_EXCEPT_HANDLER(b);
1969 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001970 Py_DECREF(status);
1971 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001973 Py_DECREF(status);
1974 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 else if (PyExceptionClass_Check(status)) {
1977 PyObject *exc = POP();
1978 PyObject *tb = POP();
1979 PyErr_Restore(status, exc, tb);
1980 why = WHY_EXCEPTION;
1981 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001983 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 PyErr_SetString(PyExc_SystemError,
1985 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 Py_DECREF(status);
1987 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001988 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 Py_DECREF(status);
1990 DISPATCH();
1991 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001992
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001994 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001995
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001997 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1999 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002000 PyErr_SetString(PyExc_NameError,
2001 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002003 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002005 }
2006 else {
2007 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2008 if (build_class_str == NULL)
2009 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002010 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2011 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002012 if (PyErr_ExceptionMatches(PyExc_KeyError))
2013 PyErr_SetString(PyExc_NameError,
2014 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002016 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002018 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002019 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002020 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002022 TARGET(STORE_NAME) {
2023 PyObject *name = GETITEM(names, oparg);
2024 PyObject *v = POP();
2025 PyObject *ns = f->f_locals;
2026 int err;
2027 if (ns == NULL) {
2028 PyErr_Format(PyExc_SystemError,
2029 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002033 if (PyDict_CheckExact(ns))
2034 err = PyDict_SetItem(ns, name, v);
2035 else
2036 err = PyObject_SetItem(ns, name, v);
2037 Py_DECREF(v);
2038 if (err != 0)
2039 goto error;
2040 DISPATCH();
2041 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002042
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 TARGET(DELETE_NAME) {
2044 PyObject *name = GETITEM(names, oparg);
2045 PyObject *ns = f->f_locals;
2046 int err;
2047 if (ns == NULL) {
2048 PyErr_Format(PyExc_SystemError,
2049 "no locals when deleting %R", name);
2050 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 err = PyObject_DelItem(ns, name);
2053 if (err != 0) {
2054 format_exc_check_arg(PyExc_NameError,
2055 NAME_ERROR_MSG,
2056 name);
2057 goto error;
2058 }
2059 DISPATCH();
2060 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 TARGET(UNPACK_SEQUENCE) {
2064 PyObject *seq = POP(), *item, **items;
2065 if (PyTuple_CheckExact(seq) &&
2066 PyTuple_GET_SIZE(seq) == oparg) {
2067 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002069 item = items[oparg];
2070 Py_INCREF(item);
2071 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 } else if (PyList_CheckExact(seq) &&
2074 PyList_GET_SIZE(seq) == oparg) {
2075 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002077 item = items[oparg];
2078 Py_INCREF(item);
2079 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002080 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 stack_pointer + oparg)) {
2083 STACKADJ(oparg);
2084 } else {
2085 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 Py_DECREF(seq);
2087 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002090 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002092
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002093 TARGET(UNPACK_EX) {
2094 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2095 PyObject *seq = POP();
2096
2097 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2098 stack_pointer + totalargs)) {
2099 stack_pointer += totalargs;
2100 } else {
2101 Py_DECREF(seq);
2102 goto error;
2103 }
2104 Py_DECREF(seq);
2105 DISPATCH();
2106 }
2107
2108 TARGET(STORE_ATTR) {
2109 PyObject *name = GETITEM(names, oparg);
2110 PyObject *owner = TOP();
2111 PyObject *v = SECOND();
2112 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002114 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002115 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002116 Py_DECREF(owner);
2117 if (err != 0)
2118 goto error;
2119 DISPATCH();
2120 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002121
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 TARGET(DELETE_ATTR) {
2123 PyObject *name = GETITEM(names, oparg);
2124 PyObject *owner = POP();
2125 int err;
2126 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2127 Py_DECREF(owner);
2128 if (err != 0)
2129 goto error;
2130 DISPATCH();
2131 }
2132
2133 TARGET(STORE_GLOBAL) {
2134 PyObject *name = GETITEM(names, oparg);
2135 PyObject *v = POP();
2136 int err;
2137 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002138 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 if (err != 0)
2140 goto error;
2141 DISPATCH();
2142 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002143
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002144 TARGET(DELETE_GLOBAL) {
2145 PyObject *name = GETITEM(names, oparg);
2146 int err;
2147 err = PyDict_DelItem(f->f_globals, name);
2148 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002149 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002150 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002152 }
2153 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002155
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 TARGET(LOAD_NAME) {
2157 PyObject *name = GETITEM(names, oparg);
2158 PyObject *locals = f->f_locals;
2159 PyObject *v;
2160 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 "no locals when loading %R", name);
2163 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 if (PyDict_CheckExact(locals)) {
2166 v = PyDict_GetItem(locals, name);
2167 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 }
2169 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 v = PyObject_GetItem(locals, name);
Antoine Pitrou1cfa0ba2013-10-07 20:40:59 +02002171 if (v == NULL && _PyErr_OCCURRED()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002172 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2173 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 PyErr_Clear();
2175 }
2176 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 if (v == NULL) {
2178 v = PyDict_GetItem(f->f_globals, name);
2179 Py_XINCREF(v);
2180 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 v = PyDict_GetItem(f->f_builtins, name);
2183 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002184 format_exc_check_arg(
2185 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 NAME_ERROR_MSG, name);
2187 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002188 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002189 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002190 }
2191 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 v = PyObject_GetItem(f->f_builtins, name);
2193 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002194 if (PyErr_ExceptionMatches(PyExc_KeyError))
2195 format_exc_check_arg(
2196 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 NAME_ERROR_MSG, name);
2198 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002199 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002200 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002201 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002203 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002204 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002206
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 TARGET(LOAD_GLOBAL) {
2208 PyObject *name = GETITEM(names, oparg);
2209 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002210 if (PyDict_CheckExact(f->f_globals)
2211 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002213 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 name);
2215 if (v == NULL) {
Antoine Pitrou59c900d2013-10-07 20:38:51 +02002216 if (!_PyErr_OCCURRED())
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002217 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002218 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002222 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002223 else {
2224 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 v = PyObject_GetItem(f->f_globals, name);
2226 if (v == NULL) {
2227 v = PyObject_GetItem(f->f_builtins, name);
2228 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002229 if (PyErr_ExceptionMatches(PyExc_KeyError))
2230 format_exc_check_arg(
2231 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002232 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002234 }
2235 }
2236 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002237 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002240
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 TARGET(DELETE_FAST) {
2242 PyObject *v = GETLOCAL(oparg);
2243 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 SETLOCAL(oparg, NULL);
2245 DISPATCH();
2246 }
2247 format_exc_check_arg(
2248 PyExc_UnboundLocalError,
2249 UNBOUNDLOCAL_ERROR_MSG,
2250 PyTuple_GetItem(co->co_varnames, oparg)
2251 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002252 goto error;
2253 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002254
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 TARGET(DELETE_DEREF) {
2256 PyObject *cell = freevars[oparg];
2257 if (PyCell_GET(cell) != NULL) {
2258 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002259 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002260 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002261 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002262 goto error;
2263 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 TARGET(LOAD_CLOSURE) {
2266 PyObject *cell = freevars[oparg];
2267 Py_INCREF(cell);
2268 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002269 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002271
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002272 TARGET(LOAD_CLASSDEREF) {
2273 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002274 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002275 assert(locals);
2276 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2277 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2278 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2279 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2280 if (PyDict_CheckExact(locals)) {
2281 value = PyDict_GetItem(locals, name);
2282 Py_XINCREF(value);
2283 }
2284 else {
2285 value = PyObject_GetItem(locals, name);
2286 if (value == NULL && PyErr_Occurred()) {
2287 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2288 goto error;
2289 PyErr_Clear();
2290 }
2291 }
2292 if (!value) {
2293 PyObject *cell = freevars[oparg];
2294 value = PyCell_GET(cell);
2295 if (value == NULL) {
2296 format_exc_unbound(co, oparg);
2297 goto error;
2298 }
2299 Py_INCREF(value);
2300 }
2301 PUSH(value);
2302 DISPATCH();
2303 }
2304
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002305 TARGET(LOAD_DEREF) {
2306 PyObject *cell = freevars[oparg];
2307 PyObject *value = PyCell_GET(cell);
2308 if (value == NULL) {
2309 format_exc_unbound(co, oparg);
2310 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002312 Py_INCREF(value);
2313 PUSH(value);
2314 DISPATCH();
2315 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002316
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002317 TARGET(STORE_DEREF) {
2318 PyObject *v = POP();
2319 PyObject *cell = freevars[oparg];
2320 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002321 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002322 DISPATCH();
2323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 TARGET(BUILD_TUPLE) {
2326 PyObject *tup = PyTuple_New(oparg);
2327 if (tup == NULL)
2328 goto error;
2329 while (--oparg >= 0) {
2330 PyObject *item = POP();
2331 PyTuple_SET_ITEM(tup, oparg, item);
2332 }
2333 PUSH(tup);
2334 DISPATCH();
2335 }
2336
2337 TARGET(BUILD_LIST) {
2338 PyObject *list = PyList_New(oparg);
2339 if (list == NULL)
2340 goto error;
2341 while (--oparg >= 0) {
2342 PyObject *item = POP();
2343 PyList_SET_ITEM(list, oparg, item);
2344 }
2345 PUSH(list);
2346 DISPATCH();
2347 }
2348
2349 TARGET(BUILD_SET) {
2350 PyObject *set = PySet_New(NULL);
2351 int err = 0;
2352 if (set == NULL)
2353 goto error;
2354 while (--oparg >= 0) {
2355 PyObject *item = POP();
2356 if (err == 0)
2357 err = PySet_Add(set, item);
2358 Py_DECREF(item);
2359 }
2360 if (err != 0) {
2361 Py_DECREF(set);
2362 goto error;
2363 }
2364 PUSH(set);
2365 DISPATCH();
2366 }
2367
2368 TARGET(BUILD_MAP) {
2369 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2370 if (map == NULL)
2371 goto error;
2372 PUSH(map);
2373 DISPATCH();
2374 }
2375
2376 TARGET(STORE_MAP) {
2377 PyObject *key = TOP();
2378 PyObject *value = SECOND();
2379 PyObject *map = THIRD();
2380 int err;
2381 STACKADJ(-2);
2382 assert(PyDict_CheckExact(map));
2383 err = PyDict_SetItem(map, key, value);
2384 Py_DECREF(value);
2385 Py_DECREF(key);
2386 if (err != 0)
2387 goto error;
2388 DISPATCH();
2389 }
2390
2391 TARGET(MAP_ADD) {
2392 PyObject *key = TOP();
2393 PyObject *value = SECOND();
2394 PyObject *map;
2395 int err;
2396 STACKADJ(-2);
2397 map = stack_pointer[-oparg]; /* dict */
2398 assert(PyDict_CheckExact(map));
2399 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2400 Py_DECREF(value);
2401 Py_DECREF(key);
2402 if (err != 0)
2403 goto error;
2404 PREDICT(JUMP_ABSOLUTE);
2405 DISPATCH();
2406 }
2407
2408 TARGET(LOAD_ATTR) {
2409 PyObject *name = GETITEM(names, oparg);
2410 PyObject *owner = TOP();
2411 PyObject *res = PyObject_GetAttr(owner, name);
2412 Py_DECREF(owner);
2413 SET_TOP(res);
2414 if (res == NULL)
2415 goto error;
2416 DISPATCH();
2417 }
2418
2419 TARGET(COMPARE_OP) {
2420 PyObject *right = POP();
2421 PyObject *left = TOP();
2422 PyObject *res = cmp_outcome(oparg, left, right);
2423 Py_DECREF(left);
2424 Py_DECREF(right);
2425 SET_TOP(res);
2426 if (res == NULL)
2427 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 PREDICT(POP_JUMP_IF_FALSE);
2429 PREDICT(POP_JUMP_IF_TRUE);
2430 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002431 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002432
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002433 TARGET(IMPORT_NAME) {
2434 _Py_IDENTIFIER(__import__);
2435 PyObject *name = GETITEM(names, oparg);
2436 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002437 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002438 if (func == NULL) {
2439 PyErr_SetString(PyExc_ImportError,
2440 "__import__ not found");
2441 goto error;
2442 }
2443 Py_INCREF(func);
2444 from = POP();
2445 level = TOP();
2446 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2447 args = PyTuple_Pack(5,
2448 name,
2449 f->f_globals,
2450 f->f_locals == NULL ?
2451 Py_None : f->f_locals,
2452 from,
2453 level);
2454 else
2455 args = PyTuple_Pack(4,
2456 name,
2457 f->f_globals,
2458 f->f_locals == NULL ?
2459 Py_None : f->f_locals,
2460 from);
2461 Py_DECREF(level);
2462 Py_DECREF(from);
2463 if (args == NULL) {
2464 Py_DECREF(func);
2465 STACKADJ(-1);
2466 goto error;
2467 }
2468 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002469 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002470 READ_TIMESTAMP(intr1);
2471 Py_DECREF(args);
2472 Py_DECREF(func);
2473 SET_TOP(res);
2474 if (res == NULL)
2475 goto error;
2476 DISPATCH();
2477 }
2478
2479 TARGET(IMPORT_STAR) {
2480 PyObject *from = POP(), *locals;
2481 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002482 if (PyFrame_FastToLocalsWithError(f) < 0)
2483 goto error;
2484
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 locals = f->f_locals;
2486 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 PyErr_SetString(PyExc_SystemError,
2488 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002489 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002490 }
2491 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002492 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 READ_TIMESTAMP(intr1);
2494 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002495 Py_DECREF(from);
2496 if (err != 0)
2497 goto error;
2498 DISPATCH();
2499 }
Guido van Rossum25831651993-05-19 14:50:45 +00002500
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 TARGET(IMPORT_FROM) {
2502 PyObject *name = GETITEM(names, oparg);
2503 PyObject *from = TOP();
2504 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002506 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002508 PUSH(res);
2509 if (res == NULL)
2510 goto error;
2511 DISPATCH();
2512 }
Thomas Wouters52152252000-08-17 22:55:00 +00002513
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 JUMPBY(oparg);
2516 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002520 TARGET(POP_JUMP_IF_FALSE) {
2521 PyObject *cond = POP();
2522 int err;
2523 if (cond == Py_True) {
2524 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 FAST_DISPATCH();
2526 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 if (cond == Py_False) {
2528 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 JUMPTO(oparg);
2530 FAST_DISPATCH();
2531 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 err = PyObject_IsTrue(cond);
2533 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 if (err > 0)
2535 err = 0;
2536 else if (err == 0)
2537 JUMPTO(oparg);
2538 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002544 TARGET(POP_JUMP_IF_TRUE) {
2545 PyObject *cond = POP();
2546 int err;
2547 if (cond == Py_False) {
2548 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 FAST_DISPATCH();
2550 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002551 if (cond == Py_True) {
2552 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 JUMPTO(oparg);
2554 FAST_DISPATCH();
2555 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002556 err = PyObject_IsTrue(cond);
2557 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 if (err > 0) {
2559 err = 0;
2560 JUMPTO(oparg);
2561 }
2562 else if (err == 0)
2563 ;
2564 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002567 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002568
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002569 TARGET(JUMP_IF_FALSE_OR_POP) {
2570 PyObject *cond = TOP();
2571 int err;
2572 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 FAST_DISPATCH();
2576 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002577 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 JUMPTO(oparg);
2579 FAST_DISPATCH();
2580 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002581 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 if (err > 0) {
2583 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 err = 0;
2586 }
2587 else if (err == 0)
2588 JUMPTO(oparg);
2589 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002592 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002593
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002594 TARGET(JUMP_IF_TRUE_OR_POP) {
2595 PyObject *cond = TOP();
2596 int err;
2597 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 FAST_DISPATCH();
2601 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002602 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002603 JUMPTO(oparg);
2604 FAST_DISPATCH();
2605 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002606 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 if (err > 0) {
2608 err = 0;
2609 JUMPTO(oparg);
2610 }
2611 else if (err == 0) {
2612 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002613 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 }
2615 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002616 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002618 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002620 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002621 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002622 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002623#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002624 /* Enabling this path speeds-up all while and for-loops by bypassing
2625 the per-loop checks for signals. By default, this should be turned-off
2626 because it prevents detection of a control-break in tight loops like
2627 "while 1: pass". Compile with this option turned-on when you need
2628 the speed-up and do not need break checking inside tight loops (ones
2629 that contain only instructions ending with FAST_DISPATCH).
2630 */
2631 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002632#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002634#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002636
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002639 PyObject *iterable = TOP();
2640 PyObject *iter = PyObject_GetIter(iterable);
2641 Py_DECREF(iterable);
2642 SET_TOP(iter);
2643 if (iter == NULL)
2644 goto error;
2645 PREDICT(FOR_ITER);
2646 DISPATCH();
2647 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002649 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 PyObject *iter = TOP();
2653 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2654 if (next != NULL) {
2655 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 PREDICT(STORE_FAST);
2657 PREDICT(UNPACK_SEQUENCE);
2658 DISPATCH();
2659 }
2660 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2662 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002663 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002664 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 PyErr_Clear();
2666 }
2667 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002668 STACKADJ(-1);
2669 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 JUMPBY(oparg);
2671 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002672 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002673
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 why = WHY_BREAK;
2676 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002678
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 if (retval == NULL)
2682 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 why = WHY_CONTINUE;
2684 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002685 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002687 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2688 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2689 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 /* NOTE: If you add any new block-setup opcodes that
2692 are not try/except/finally handlers, you may need
2693 to update the PyGen_NeedsFinalizing() function.
2694 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2697 STACK_LEVEL());
2698 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002702 _Py_IDENTIFIER(__exit__);
2703 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002704 PyObject *mgr = TOP();
2705 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2706 PyObject *res;
2707 if (exit == NULL)
2708 goto error;
2709 SET_TOP(exit);
2710 enter = special_lookup(mgr, &PyId___enter__);
2711 Py_DECREF(mgr);
2712 if (enter == NULL)
2713 goto error;
2714 res = PyObject_CallFunctionObjArgs(enter, NULL);
2715 Py_DECREF(enter);
2716 if (res == NULL)
2717 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 /* Setup the finally block before pushing the result
2719 of __enter__ on the stack. */
2720 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2721 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002722
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002723 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 DISPATCH();
2725 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002726
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 TARGET(WITH_CLEANUP) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002728 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 how/why we entered the finally clause:
2730 - TOP = None
2731 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2732 - TOP = WHY_*; no retval below it
2733 - (TOP, SECOND, THIRD) = exc_info()
2734 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2735 Below them is EXIT, the context.__exit__ bound method.
2736 In the last case, we must call
2737 EXIT(TOP, SECOND, THIRD)
2738 otherwise we must call
2739 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002740
Benjamin Peterson8f169482013-10-29 22:25:06 -04002741 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002743 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 In addition, if the stack represents an exception,
2747 *and* the function call returns a 'true' value, we
2748 push WHY_SILENCED onto the stack. END_FINALLY will
2749 then not re-raise the exception. (But non-local
2750 gotos should still be resumed.)
2751 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2755 int err;
2756 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 (void)POP();
2758 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 else if (PyLong_Check(exc)) {
2762 STACKADJ(-1);
2763 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002764 case WHY_RETURN:
2765 case WHY_CONTINUE:
2766 /* Retval in TOP. */
2767 exit_func = SECOND();
2768 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002769 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 break;
2771 default:
2772 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 break;
2775 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002776 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002777 }
2778 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 val = SECOND();
2782 tb = THIRD();
2783 tp2 = FOURTH();
2784 exc2 = PEEK(5);
2785 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 SET_VALUE(7, tb2);
2788 SET_VALUE(6, exc2);
2789 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2791 SET_FOURTH(NULL);
2792 /* We just shifted the stack down, so we have
2793 to tell the except handler block that the
2794 values are lower than it expects. */
2795 block = &f->f_blockstack[f->f_iblock - 1];
2796 assert(block->b_type == EXCEPT_HANDLER);
2797 block->b_level--;
2798 }
2799 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002801 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 if (res == NULL)
2803 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002804
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 if (exc != Py_None)
2806 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 else
2808 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 else if (err > 0) {
2814 err = 0;
2815 /* There was an exception and a True return */
2816 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2817 }
2818 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002821
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 TARGET(CALL_FUNCTION) {
2823 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 PCALL(PCALL_ALL);
2825 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002826#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002828#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002829 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002830#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002831 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 PUSH(res);
2833 if (res == NULL)
2834 goto error;
2835 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2839 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2840 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 int na = oparg & 0xff;
2843 int nk = (oparg>>8) & 0xff;
2844 int flags = (opcode - CALL_FUNCTION) & 3;
2845 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 PCALL(PCALL_ALL);
2848 if (flags & CALL_FLAG_VAR)
2849 n++;
2850 if (flags & CALL_FLAG_KW)
2851 n++;
2852 pfunc = stack_pointer - n - 1;
2853 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002856 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 PyObject *self = PyMethod_GET_SELF(func);
2858 Py_INCREF(self);
2859 func = PyMethod_GET_FUNCTION(func);
2860 Py_INCREF(func);
2861 Py_DECREF(*pfunc);
2862 *pfunc = self;
2863 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002864 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 } else
2866 Py_INCREF(func);
2867 sp = stack_pointer;
2868 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 READ_TIMESTAMP(intr1);
2871 stack_pointer = sp;
2872 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 PyObject *o = POP();
2876 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 PUSH(res);
2879 if (res == NULL)
2880 goto error;
2881 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2885 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 int posdefaults = oparg & 0xff;
2888 int kwdefaults = (oparg>>8) & 0xff;
2889 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002890
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002891 PyObject *qualname = POP(); /* qualname */
2892 PyObject *code = POP(); /* code object */
2893 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2894 Py_DECREF(code);
2895 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002896
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 if (func == NULL)
2898 goto error;
2899
2900 if (opcode == MAKE_CLOSURE) {
2901 PyObject *closure = POP();
2902 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 Py_DECREF(func);
2905 Py_DECREF(closure);
2906 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002907 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002908 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002910
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 PyObject *names = POP(); /* names of args with annotations */
2914 PyObject *anns = PyDict_New();
2915 if (anns == NULL) {
2916 Py_DECREF(func);
2917 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 assert(num_annotations == name_ix+1);
2921 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 PyObject *name, *value;
2923 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 name = PyTuple_GET_ITEM(names, name_ix);
2926 value = POP();
2927 err = PyDict_SetItem(anns, name, value);
2928 Py_DECREF(value);
2929 if (err != 0) {
2930 Py_DECREF(anns);
2931 Py_DECREF(func);
2932 goto error;
2933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002935
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 /* Can't happen unless
2938 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 Py_DECREF(anns);
2940 Py_DECREF(func);
2941 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002942 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 Py_DECREF(anns);
2944 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 if (kwdefaults > 0) {
2949 PyObject *defs = PyDict_New();
2950 if (defs == NULL) {
2951 Py_DECREF(func);
2952 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 }
2954 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 PyObject *v = POP(); /* default value */
2956 PyObject *key = POP(); /* kw only arg name */
2957 int err = PyDict_SetItem(defs, key, v);
2958 Py_DECREF(v);
2959 Py_DECREF(key);
2960 if (err != 0) {
2961 Py_DECREF(defs);
2962 Py_DECREF(func);
2963 goto error;
2964 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002966 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967 /* Can't happen unless
2968 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002969 Py_DECREF(func);
2970 Py_DECREF(defs);
2971 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002973 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05002975 if (posdefaults > 0) {
2976 PyObject *defs = PyTuple_New(posdefaults);
2977 if (defs == NULL) {
2978 Py_DECREF(func);
2979 goto error;
2980 }
2981 while (--posdefaults >= 0)
2982 PyTuple_SET_ITEM(defs, posdefaults, POP());
2983 if (PyFunction_SetDefaults(func, defs) != 0) {
2984 /* Can't happen unless
2985 PyFunction_SetDefaults changes. */
2986 Py_DECREF(defs);
2987 Py_DECREF(func);
2988 goto error;
2989 }
2990 Py_DECREF(defs);
2991 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002992 PUSH(func);
2993 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002995
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 TARGET(BUILD_SLICE) {
2997 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 step = NULL;
3002 stop = POP();
3003 start = TOP();
3004 slice = PySlice_New(start, stop, step);
3005 Py_DECREF(start);
3006 Py_DECREF(stop);
3007 Py_XDECREF(step);
3008 SET_TOP(slice);
3009 if (slice == NULL)
3010 goto error;
3011 DISPATCH();
3012 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003013
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003014 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 opcode = NEXTOP();
3016 oparg = oparg<<16 | NEXTARG();
3017 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003018 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003019
Antoine Pitrou042b1282010-08-13 21:15:58 +00003020#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003022#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003023 default:
3024 fprintf(stderr,
3025 "XXX lineno: %d, opcode: %d\n",
3026 PyFrame_GetLineNumber(f),
3027 opcode);
3028 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003029 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003030
3031#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003033#endif
3034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003036
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003037 /* This should never be reached. Every opcode should end with DISPATCH()
3038 or goto error. */
3039 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003040
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003041error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003043
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003044 assert(why == WHY_NOT);
3045 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003046
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003047 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003048#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 if (!PyErr_Occurred())
3050 PyErr_SetString(PyExc_SystemError,
3051 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003052#else
3053 assert(PyErr_Occurred());
3054#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003055
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 /* Log traceback info. */
3057 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003058
Benjamin Peterson51f46162013-01-23 08:38:47 -05003059 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003060 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3061 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003062
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003063fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003064 assert(why != WHY_NOT);
3065
3066 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 while (why != WHY_NOT && f->f_iblock > 0) {
3068 /* Peek at the current block. */
3069 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 assert(why != WHY_YIELD);
3072 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3073 why = WHY_NOT;
3074 JUMPTO(PyLong_AS_LONG(retval));
3075 Py_DECREF(retval);
3076 break;
3077 }
3078 /* Now we have to pop the block. */
3079 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 if (b->b_type == EXCEPT_HANDLER) {
3082 UNWIND_EXCEPT_HANDLER(b);
3083 continue;
3084 }
3085 UNWIND_BLOCK(b);
3086 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3087 why = WHY_NOT;
3088 JUMPTO(b->b_handler);
3089 break;
3090 }
3091 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3092 || b->b_type == SETUP_FINALLY)) {
3093 PyObject *exc, *val, *tb;
3094 int handler = b->b_handler;
3095 /* Beware, this invalidates all b->b_* fields */
3096 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3097 PUSH(tstate->exc_traceback);
3098 PUSH(tstate->exc_value);
3099 if (tstate->exc_type != NULL) {
3100 PUSH(tstate->exc_type);
3101 }
3102 else {
3103 Py_INCREF(Py_None);
3104 PUSH(Py_None);
3105 }
3106 PyErr_Fetch(&exc, &val, &tb);
3107 /* Make the raw exception data
3108 available to the handler,
3109 so a program can emulate the
3110 Python main loop. */
3111 PyErr_NormalizeException(
3112 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003113 if (tb != NULL)
3114 PyException_SetTraceback(val, tb);
3115 else
3116 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003117 Py_INCREF(exc);
3118 tstate->exc_type = exc;
3119 Py_INCREF(val);
3120 tstate->exc_value = val;
3121 tstate->exc_traceback = tb;
3122 if (tb == NULL)
3123 tb = Py_None;
3124 Py_INCREF(tb);
3125 PUSH(tb);
3126 PUSH(val);
3127 PUSH(exc);
3128 why = WHY_NOT;
3129 JUMPTO(handler);
3130 break;
3131 }
3132 if (b->b_type == SETUP_FINALLY) {
3133 if (why & (WHY_RETURN | WHY_CONTINUE))
3134 PUSH(retval);
3135 PUSH(PyLong_FromLong((long)why));
3136 why = WHY_NOT;
3137 JUMPTO(b->b_handler);
3138 break;
3139 }
3140 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003143
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144 if (why != WHY_NOT)
3145 break;
3146 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003147
Victor Stinnerace47d72013-07-18 01:41:08 +02003148 assert(!PyErr_Occurred());
3149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003150 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 assert(why != WHY_YIELD);
3153 /* Pop remaining stack entries. */
3154 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003155 PyObject *o = POP();
3156 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 if (why != WHY_RETURN)
3160 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003161
Victor Stinnerace47d72013-07-18 01:41:08 +02003162 assert((retval != NULL && !PyErr_Occurred())
3163 || (retval == NULL && PyErr_Occurred()));
3164
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003165fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003166 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3167 /* The purpose of this block is to put aside the generator's exception
3168 state and restore that of the calling frame. If the current
3169 exception state is from the caller, we clear the exception values
3170 on the generator frame, so they are not swapped back in latter. The
3171 origin of the current exception state is determined by checking for
3172 except handler blocks, which we must be in iff a new exception
3173 state came into existence in this frame. (An uncaught exception
3174 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3175 int i;
3176 for (i = 0; i < f->f_iblock; i++)
3177 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3178 break;
3179 if (i == f->f_iblock)
3180 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003181 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003182 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003183 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003184 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003187 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003189 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3190 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003191 PyTrace_RETURN, retval)) {
3192 Py_XDECREF(retval);
3193 retval = NULL;
3194 why = WHY_EXCEPTION;
3195 }
3196 }
3197 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003198 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3199 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 PyTrace_RETURN, NULL);
3201 }
3202 }
3203 if (tstate->c_profilefunc) {
3204 if (why == WHY_EXCEPTION)
3205 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003206 tstate->c_profileobj,
3207 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003209 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3210 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 PyTrace_RETURN, retval)) {
3212 Py_XDECREF(retval);
3213 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003214 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003215 }
3216 }
3217 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003220exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003222 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003226}
3227
Benjamin Petersonb204a422011-06-05 22:04:07 -05003228static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003229format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3230{
3231 int err;
3232 Py_ssize_t len = PyList_GET_SIZE(names);
3233 PyObject *name_str, *comma, *tail, *tmp;
3234
3235 assert(PyList_CheckExact(names));
3236 assert(len >= 1);
3237 /* Deal with the joys of natural language. */
3238 switch (len) {
3239 case 1:
3240 name_str = PyList_GET_ITEM(names, 0);
3241 Py_INCREF(name_str);
3242 break;
3243 case 2:
3244 name_str = PyUnicode_FromFormat("%U and %U",
3245 PyList_GET_ITEM(names, len - 2),
3246 PyList_GET_ITEM(names, len - 1));
3247 break;
3248 default:
3249 tail = PyUnicode_FromFormat(", %U, and %U",
3250 PyList_GET_ITEM(names, len - 2),
3251 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003252 if (tail == NULL)
3253 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003254 /* Chop off the last two objects in the list. This shouldn't actually
3255 fail, but we can't be too careful. */
3256 err = PyList_SetSlice(names, len - 2, len, NULL);
3257 if (err == -1) {
3258 Py_DECREF(tail);
3259 return;
3260 }
3261 /* Stitch everything up into a nice comma-separated list. */
3262 comma = PyUnicode_FromString(", ");
3263 if (comma == NULL) {
3264 Py_DECREF(tail);
3265 return;
3266 }
3267 tmp = PyUnicode_Join(comma, names);
3268 Py_DECREF(comma);
3269 if (tmp == NULL) {
3270 Py_DECREF(tail);
3271 return;
3272 }
3273 name_str = PyUnicode_Concat(tmp, tail);
3274 Py_DECREF(tmp);
3275 Py_DECREF(tail);
3276 break;
3277 }
3278 if (name_str == NULL)
3279 return;
3280 PyErr_Format(PyExc_TypeError,
3281 "%U() missing %i required %s argument%s: %U",
3282 co->co_name,
3283 len,
3284 kind,
3285 len == 1 ? "" : "s",
3286 name_str);
3287 Py_DECREF(name_str);
3288}
3289
3290static void
3291missing_arguments(PyCodeObject *co, int missing, int defcount,
3292 PyObject **fastlocals)
3293{
3294 int i, j = 0;
3295 int start, end;
3296 int positional = defcount != -1;
3297 const char *kind = positional ? "positional" : "keyword-only";
3298 PyObject *missing_names;
3299
3300 /* Compute the names of the arguments that are missing. */
3301 missing_names = PyList_New(missing);
3302 if (missing_names == NULL)
3303 return;
3304 if (positional) {
3305 start = 0;
3306 end = co->co_argcount - defcount;
3307 }
3308 else {
3309 start = co->co_argcount;
3310 end = start + co->co_kwonlyargcount;
3311 }
3312 for (i = start; i < end; i++) {
3313 if (GETLOCAL(i) == NULL) {
3314 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3315 PyObject *name = PyObject_Repr(raw);
3316 if (name == NULL) {
3317 Py_DECREF(missing_names);
3318 return;
3319 }
3320 PyList_SET_ITEM(missing_names, j++, name);
3321 }
3322 }
3323 assert(j == missing);
3324 format_missing(kind, co, missing_names);
3325 Py_DECREF(missing_names);
3326}
3327
3328static void
3329too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003330{
3331 int plural;
3332 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003333 int i;
3334 PyObject *sig, *kwonly_sig;
3335
Benjamin Petersone109c702011-06-24 09:37:26 -05003336 assert((co->co_flags & CO_VARARGS) == 0);
3337 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003338 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003339 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003340 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003341 if (defcount) {
3342 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003343 plural = 1;
3344 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3345 }
3346 else {
3347 plural = co->co_argcount != 1;
3348 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3349 }
3350 if (sig == NULL)
3351 return;
3352 if (kwonly_given) {
3353 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3354 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3355 kwonly_given != 1 ? "s" : "");
3356 if (kwonly_sig == NULL) {
3357 Py_DECREF(sig);
3358 return;
3359 }
3360 }
3361 else {
3362 /* This will not fail. */
3363 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003364 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003365 }
3366 PyErr_Format(PyExc_TypeError,
3367 "%U() takes %U positional argument%s but %d%U %s given",
3368 co->co_name,
3369 sig,
3370 plural ? "s" : "",
3371 given,
3372 kwonly_sig,
3373 given == 1 && !kwonly_given ? "was" : "were");
3374 Py_DECREF(sig);
3375 Py_DECREF(kwonly_sig);
3376}
3377
Guido van Rossumc2e20742006-02-27 22:32:47 +00003378/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003379 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003380 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003381
Tim Peters6d6c1a32001-08-02 04:15:00 +00003382PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003383PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003384 PyObject **args, int argcount, PyObject **kws, int kwcount,
3385 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003386{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003387 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003388 PyFrameObject *f;
3389 PyObject *retval = NULL;
3390 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 PyThreadState *tstate = PyThreadState_GET();
3392 PyObject *x, *u;
3393 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003394 int i;
3395 int n = argcount;
3396 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 if (globals == NULL) {
3399 PyErr_SetString(PyExc_SystemError,
3400 "PyEval_EvalCodeEx: NULL globals");
3401 return NULL;
3402 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 assert(tstate != NULL);
3405 assert(globals != NULL);
3406 f = PyFrame_New(tstate, co, globals, locals);
3407 if (f == NULL)
3408 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 fastlocals = f->f_localsplus;
3411 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003412
Benjamin Petersonb204a422011-06-05 22:04:07 -05003413 /* Parse arguments. */
3414 if (co->co_flags & CO_VARKEYWORDS) {
3415 kwdict = PyDict_New();
3416 if (kwdict == NULL)
3417 goto fail;
3418 i = total_args;
3419 if (co->co_flags & CO_VARARGS)
3420 i++;
3421 SETLOCAL(i, kwdict);
3422 }
3423 if (argcount > co->co_argcount)
3424 n = co->co_argcount;
3425 for (i = 0; i < n; i++) {
3426 x = args[i];
3427 Py_INCREF(x);
3428 SETLOCAL(i, x);
3429 }
3430 if (co->co_flags & CO_VARARGS) {
3431 u = PyTuple_New(argcount - n);
3432 if (u == NULL)
3433 goto fail;
3434 SETLOCAL(total_args, u);
3435 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 x = args[i];
3437 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003438 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003440 }
3441 for (i = 0; i < kwcount; i++) {
3442 PyObject **co_varnames;
3443 PyObject *keyword = kws[2*i];
3444 PyObject *value = kws[2*i + 1];
3445 int j;
3446 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3447 PyErr_Format(PyExc_TypeError,
3448 "%U() keywords must be strings",
3449 co->co_name);
3450 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003451 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003452 /* Speed hack: do raw pointer compares. As names are
3453 normally interned this should almost always hit. */
3454 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3455 for (j = 0; j < total_args; j++) {
3456 PyObject *nm = co_varnames[j];
3457 if (nm == keyword)
3458 goto kw_found;
3459 }
3460 /* Slow fallback, just in case */
3461 for (j = 0; j < total_args; j++) {
3462 PyObject *nm = co_varnames[j];
3463 int cmp = PyObject_RichCompareBool(
3464 keyword, nm, Py_EQ);
3465 if (cmp > 0)
3466 goto kw_found;
3467 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003469 }
3470 if (j >= total_args && kwdict == NULL) {
3471 PyErr_Format(PyExc_TypeError,
3472 "%U() got an unexpected "
3473 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 co->co_name,
3475 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003476 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003477 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003478 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3479 goto fail;
3480 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003481 continue;
3482 kw_found:
3483 if (GETLOCAL(j) != NULL) {
3484 PyErr_Format(PyExc_TypeError,
3485 "%U() got multiple "
3486 "values for argument '%S'",
3487 co->co_name,
3488 keyword);
3489 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003491 Py_INCREF(value);
3492 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003493 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003494 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003495 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 goto fail;
3497 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003498 if (argcount < co->co_argcount) {
3499 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003500 int missing = 0;
3501 for (i = argcount; i < m; i++)
3502 if (GETLOCAL(i) == NULL)
3503 missing++;
3504 if (missing) {
3505 missing_arguments(co, missing, defcount, fastlocals);
3506 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003507 }
3508 if (n > m)
3509 i = n - m;
3510 else
3511 i = 0;
3512 for (; i < defcount; i++) {
3513 if (GETLOCAL(m+i) == NULL) {
3514 PyObject *def = defs[i];
3515 Py_INCREF(def);
3516 SETLOCAL(m+i, def);
3517 }
3518 }
3519 }
3520 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003521 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003522 for (i = co->co_argcount; i < total_args; i++) {
3523 PyObject *name;
3524 if (GETLOCAL(i) != NULL)
3525 continue;
3526 name = PyTuple_GET_ITEM(co->co_varnames, i);
3527 if (kwdefs != NULL) {
3528 PyObject *def = PyDict_GetItem(kwdefs, name);
3529 if (def) {
3530 Py_INCREF(def);
3531 SETLOCAL(i, def);
3532 continue;
3533 }
3534 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003535 missing++;
3536 }
3537 if (missing) {
3538 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003539 goto fail;
3540 }
3541 }
3542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003543 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003544 vars into frame. */
3545 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003547 int arg;
3548 /* Possibly account for the cell variable being an argument. */
3549 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003550 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003551 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003552 /* Clear the local copy. */
3553 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003554 }
3555 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003556 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003557 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003558 if (c == NULL)
3559 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003560 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003562 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3563 PyObject *o = PyTuple_GET_ITEM(closure, i);
3564 Py_INCREF(o);
3565 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 if (co->co_flags & CO_GENERATOR) {
3569 /* Don't need to keep the reference to f_back, it will be set
3570 * when the generator is resumed. */
3571 Py_XDECREF(f->f_back);
3572 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 /* Create a new generator that owns the ready to run frame
3577 * and return that as the value. */
3578 return PyGen_New(f);
3579 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003582
Thomas Woutersce272b62007-09-19 21:19:28 +00003583fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 /* decref'ing the frame can cause __del__ methods to get invoked,
3586 which can call back into Python. While we're done with the
3587 current Python frame (f), the associated C stack is still in use,
3588 so recursion_depth must be boosted for the duration.
3589 */
3590 assert(tstate != NULL);
3591 ++tstate->recursion_depth;
3592 Py_DECREF(f);
3593 --tstate->recursion_depth;
3594 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003595}
3596
3597
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003598static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003599special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003602 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003604 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 return NULL;
3606 }
3607 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003608}
3609
3610
Benjamin Peterson87880242011-07-03 16:48:31 -05003611/* These 3 functions deal with the exception state of generators. */
3612
3613static void
3614save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3615{
3616 PyObject *type, *value, *traceback;
3617 Py_XINCREF(tstate->exc_type);
3618 Py_XINCREF(tstate->exc_value);
3619 Py_XINCREF(tstate->exc_traceback);
3620 type = f->f_exc_type;
3621 value = f->f_exc_value;
3622 traceback = f->f_exc_traceback;
3623 f->f_exc_type = tstate->exc_type;
3624 f->f_exc_value = tstate->exc_value;
3625 f->f_exc_traceback = tstate->exc_traceback;
3626 Py_XDECREF(type);
3627 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003628 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003629}
3630
3631static void
3632swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3633{
3634 PyObject *tmp;
3635 tmp = tstate->exc_type;
3636 tstate->exc_type = f->f_exc_type;
3637 f->f_exc_type = tmp;
3638 tmp = tstate->exc_value;
3639 tstate->exc_value = f->f_exc_value;
3640 f->f_exc_value = tmp;
3641 tmp = tstate->exc_traceback;
3642 tstate->exc_traceback = f->f_exc_traceback;
3643 f->f_exc_traceback = tmp;
3644}
3645
3646static void
3647restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3648{
3649 PyObject *type, *value, *tb;
3650 type = tstate->exc_type;
3651 value = tstate->exc_value;
3652 tb = tstate->exc_traceback;
3653 tstate->exc_type = f->f_exc_type;
3654 tstate->exc_value = f->f_exc_value;
3655 tstate->exc_traceback = f->f_exc_traceback;
3656 f->f_exc_type = NULL;
3657 f->f_exc_value = NULL;
3658 f->f_exc_traceback = NULL;
3659 Py_XDECREF(type);
3660 Py_XDECREF(value);
3661 Py_XDECREF(tb);
3662}
3663
3664
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003665/* Logic for the raise statement (too complicated for inlining).
3666 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003667static int
Collin Winter828f04a2007-08-31 00:04:24 +00003668do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 if (exc == NULL) {
3673 /* Reraise */
3674 PyThreadState *tstate = PyThreadState_GET();
3675 PyObject *tb;
3676 type = tstate->exc_type;
3677 value = tstate->exc_value;
3678 tb = tstate->exc_traceback;
3679 if (type == Py_None) {
3680 PyErr_SetString(PyExc_RuntimeError,
3681 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003682 return 0;
3683 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 Py_XINCREF(type);
3685 Py_XINCREF(value);
3686 Py_XINCREF(tb);
3687 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003688 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 /* We support the following forms of raise:
3692 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003693 raise <instance>
3694 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 if (PyExceptionClass_Check(exc)) {
3697 type = exc;
3698 value = PyObject_CallObject(exc, NULL);
3699 if (value == NULL)
3700 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003701 if (!PyExceptionInstance_Check(value)) {
3702 PyErr_Format(PyExc_TypeError,
3703 "calling %R should have returned an instance of "
3704 "BaseException, not %R",
3705 type, Py_TYPE(value));
3706 goto raise_error;
3707 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 }
3709 else if (PyExceptionInstance_Check(exc)) {
3710 value = exc;
3711 type = PyExceptionInstance_Class(exc);
3712 Py_INCREF(type);
3713 }
3714 else {
3715 /* Not something you can raise. You get an exception
3716 anyway, just not what you specified :-) */
3717 Py_DECREF(exc);
3718 PyErr_SetString(PyExc_TypeError,
3719 "exceptions must derive from BaseException");
3720 goto raise_error;
3721 }
Collin Winter828f04a2007-08-31 00:04:24 +00003722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 if (cause) {
3724 PyObject *fixed_cause;
3725 if (PyExceptionClass_Check(cause)) {
3726 fixed_cause = PyObject_CallObject(cause, NULL);
3727 if (fixed_cause == NULL)
3728 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003729 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003731 else if (PyExceptionInstance_Check(cause)) {
3732 fixed_cause = cause;
3733 }
3734 else if (cause == Py_None) {
3735 Py_DECREF(cause);
3736 fixed_cause = NULL;
3737 }
3738 else {
3739 PyErr_SetString(PyExc_TypeError,
3740 "exception causes must derive from "
3741 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 goto raise_error;
3743 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003744 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003745 }
Collin Winter828f04a2007-08-31 00:04:24 +00003746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 PyErr_SetObject(type, value);
3748 /* PyErr_SetObject incref's its arguments */
3749 Py_XDECREF(value);
3750 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003751 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003752
3753raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 Py_XDECREF(value);
3755 Py_XDECREF(type);
3756 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003757 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003758}
3759
Tim Petersd6d010b2001-06-21 02:49:55 +00003760/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003761 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003762
Guido van Rossum0368b722007-05-11 16:50:42 +00003763 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3764 with a variable target.
3765*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003766
Barry Warsawe42b18f1997-08-25 22:13:04 +00003767static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003768unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 int i = 0, j = 0;
3771 Py_ssize_t ll = 0;
3772 PyObject *it; /* iter(v) */
3773 PyObject *w;
3774 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 it = PyObject_GetIter(v);
3779 if (it == NULL)
3780 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 for (; i < argcnt; i++) {
3783 w = PyIter_Next(it);
3784 if (w == NULL) {
3785 /* Iterator done, via error or exhaustion. */
3786 if (!PyErr_Occurred()) {
3787 PyErr_Format(PyExc_ValueError,
3788 "need more than %d value%s to unpack",
3789 i, i == 1 ? "" : "s");
3790 }
3791 goto Error;
3792 }
3793 *--sp = w;
3794 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 if (argcntafter == -1) {
3797 /* We better have exhausted the iterator now. */
3798 w = PyIter_Next(it);
3799 if (w == NULL) {
3800 if (PyErr_Occurred())
3801 goto Error;
3802 Py_DECREF(it);
3803 return 1;
3804 }
3805 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003806 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3807 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 goto Error;
3809 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 l = PySequence_List(it);
3812 if (l == NULL)
3813 goto Error;
3814 *--sp = l;
3815 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 ll = PyList_GET_SIZE(l);
3818 if (ll < argcntafter) {
3819 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3820 argcnt + ll);
3821 goto Error;
3822 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 /* Pop the "after-variable" args off the list. */
3825 for (j = argcntafter; j > 0; j--, i++) {
3826 *--sp = PyList_GET_ITEM(l, ll - j);
3827 }
3828 /* Resize the list. */
3829 Py_SIZE(l) = ll - argcntafter;
3830 Py_DECREF(it);
3831 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003832
Tim Petersd6d010b2001-06-21 02:49:55 +00003833Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 for (; i > 0; i--, sp++)
3835 Py_DECREF(*sp);
3836 Py_XDECREF(it);
3837 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003838}
3839
3840
Guido van Rossum96a42c81992-01-12 02:29:51 +00003841#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003842static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003843prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003844{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 printf("%s ", str);
3846 if (PyObject_Print(v, stdout, 0) != 0)
3847 PyErr_Clear(); /* Don't know what else to do */
3848 printf("\n");
3849 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003850}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003851#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003852
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003853static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003854call_exc_trace(Py_tracefunc func, PyObject *self,
3855 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003856{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003857 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01003859 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 if (value == NULL) {
3861 value = Py_None;
3862 Py_INCREF(value);
3863 }
Antoine Pitrou89335212013-11-23 14:05:23 +01003864 PyErr_NormalizeException(&type, &value, &orig_traceback);
3865 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 arg = PyTuple_Pack(3, type, value, traceback);
3867 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01003868 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 return;
3870 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003871 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 Py_DECREF(arg);
3873 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003874 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 else {
3876 Py_XDECREF(type);
3877 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003878 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003880}
3881
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003882static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003883call_trace_protected(Py_tracefunc func, PyObject *obj,
3884 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 PyObject *type, *value, *traceback;
3888 int err;
3889 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003890 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 if (err == 0)
3892 {
3893 PyErr_Restore(type, value, traceback);
3894 return 0;
3895 }
3896 else {
3897 Py_XDECREF(type);
3898 Py_XDECREF(value);
3899 Py_XDECREF(traceback);
3900 return -1;
3901 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003902}
3903
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003904static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003905call_trace(Py_tracefunc func, PyObject *obj,
3906 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003908{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 int result;
3910 if (tstate->tracing)
3911 return 0;
3912 tstate->tracing++;
3913 tstate->use_tracing = 0;
3914 result = func(obj, frame, what, arg);
3915 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3916 || (tstate->c_profilefunc != NULL));
3917 tstate->tracing--;
3918 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003919}
3920
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003921PyObject *
3922_PyEval_CallTracing(PyObject *func, PyObject *args)
3923{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003924 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 int save_tracing = tstate->tracing;
3926 int save_use_tracing = tstate->use_tracing;
3927 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003929 tstate->tracing = 0;
3930 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3931 || (tstate->c_profilefunc != NULL));
3932 result = PyObject_Call(func, args, NULL);
3933 tstate->tracing = save_tracing;
3934 tstate->use_tracing = save_use_tracing;
3935 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003936}
3937
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003938/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003939static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003940maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003941 PyThreadState *tstate, PyFrameObject *frame,
3942 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003943{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 int result = 0;
3945 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003947 /* If the last instruction executed isn't in the current
3948 instruction window, reset the window.
3949 */
3950 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3951 PyAddrPair bounds;
3952 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3953 &bounds);
3954 *instr_lb = bounds.ap_lower;
3955 *instr_ub = bounds.ap_upper;
3956 }
3957 /* If the last instruction falls at the start of a line or if
3958 it represents a jump backwards, update the frame's line
3959 number and call the trace function. */
3960 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3961 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003962 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 }
3964 *instr_prev = frame->f_lasti;
3965 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003966}
3967
Fred Drake5755ce62001-06-27 19:19:46 +00003968void
3969PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003971 PyThreadState *tstate = PyThreadState_GET();
3972 PyObject *temp = tstate->c_profileobj;
3973 Py_XINCREF(arg);
3974 tstate->c_profilefunc = NULL;
3975 tstate->c_profileobj = NULL;
3976 /* Must make sure that tracing is not ignored if 'temp' is freed */
3977 tstate->use_tracing = tstate->c_tracefunc != NULL;
3978 Py_XDECREF(temp);
3979 tstate->c_profilefunc = func;
3980 tstate->c_profileobj = arg;
3981 /* Flag that tracing or profiling is turned on */
3982 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003983}
3984
3985void
3986PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3987{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003988 PyThreadState *tstate = PyThreadState_GET();
3989 PyObject *temp = tstate->c_traceobj;
3990 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3991 Py_XINCREF(arg);
3992 tstate->c_tracefunc = NULL;
3993 tstate->c_traceobj = NULL;
3994 /* Must make sure that profiling is not ignored if 'temp' is freed */
3995 tstate->use_tracing = tstate->c_profilefunc != NULL;
3996 Py_XDECREF(temp);
3997 tstate->c_tracefunc = func;
3998 tstate->c_traceobj = arg;
3999 /* Flag that tracing or profiling is turned on */
4000 tstate->use_tracing = ((func != NULL)
4001 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004002}
4003
Guido van Rossumb209a111997-04-29 18:18:01 +00004004PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004005PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004007 PyFrameObject *current_frame = PyEval_GetFrame();
4008 if (current_frame == NULL)
4009 return PyThreadState_GET()->interp->builtins;
4010 else
4011 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004012}
4013
Guido van Rossumb209a111997-04-29 18:18:01 +00004014PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004015PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004018 if (current_frame == NULL) {
4019 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004021 }
4022
4023 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4024 return NULL;
4025
4026 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004028}
4029
Guido van Rossumb209a111997-04-29 18:18:01 +00004030PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004031PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 PyFrameObject *current_frame = PyEval_GetFrame();
4034 if (current_frame == NULL)
4035 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004036
4037 assert(current_frame->f_globals != NULL);
4038 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004039}
4040
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004041PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004042PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004043{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 PyThreadState *tstate = PyThreadState_GET();
4045 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004046}
4047
Guido van Rossum6135a871995-01-09 17:53:26 +00004048int
Tim Peters5ba58662001-07-16 02:29:45 +00004049PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 PyFrameObject *current_frame = PyEval_GetFrame();
4052 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 if (current_frame != NULL) {
4055 const int codeflags = current_frame->f_code->co_flags;
4056 const int compilerflags = codeflags & PyCF_MASK;
4057 if (compilerflags) {
4058 result = 1;
4059 cf->cf_flags |= compilerflags;
4060 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004061#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004062 if (codeflags & CO_GENERATOR_ALLOWED) {
4063 result = 1;
4064 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4065 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004066#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004067 }
4068 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004069}
4070
Guido van Rossum3f5da241990-12-20 15:06:42 +00004071
Guido van Rossum681d79a1995-07-18 14:51:37 +00004072/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004073 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004074
Guido van Rossumb209a111997-04-29 18:18:01 +00004075PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004076PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004077{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004079
Victor Stinnerace47d72013-07-18 01:41:08 +02004080#ifdef Py_DEBUG
4081 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4082 set, because it may clear it (directly or indirectly)
4083 and so the caller looses its exception */
4084 assert(!PyErr_Occurred());
4085#endif
4086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004087 if (arg == NULL) {
4088 arg = PyTuple_New(0);
4089 if (arg == NULL)
4090 return NULL;
4091 }
4092 else if (!PyTuple_Check(arg)) {
4093 PyErr_SetString(PyExc_TypeError,
4094 "argument list must be a tuple");
4095 return NULL;
4096 }
4097 else
4098 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 if (kw != NULL && !PyDict_Check(kw)) {
4101 PyErr_SetString(PyExc_TypeError,
4102 "keyword list must be a dictionary");
4103 Py_DECREF(arg);
4104 return NULL;
4105 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 result = PyObject_Call(func, arg, kw);
4108 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004109
4110 assert((result != NULL && !PyErr_Occurred())
4111 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004113}
4114
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004115const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004116PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 if (PyMethod_Check(func))
4119 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4120 else if (PyFunction_Check(func))
4121 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4122 else if (PyCFunction_Check(func))
4123 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4124 else
4125 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004126}
4127
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004128const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004129PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004130{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 if (PyMethod_Check(func))
4132 return "()";
4133 else if (PyFunction_Check(func))
4134 return "()";
4135 else if (PyCFunction_Check(func))
4136 return "()";
4137 else
4138 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004139}
4140
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004141static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004142err_args(PyObject *func, int flags, int nargs)
4143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 if (flags & METH_NOARGS)
4145 PyErr_Format(PyExc_TypeError,
4146 "%.200s() takes no arguments (%d given)",
4147 ((PyCFunctionObject *)func)->m_ml->ml_name,
4148 nargs);
4149 else
4150 PyErr_Format(PyExc_TypeError,
4151 "%.200s() takes exactly one argument (%d given)",
4152 ((PyCFunctionObject *)func)->m_ml->ml_name,
4153 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004154}
4155
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004156#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004157if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004158 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4159 tstate, tstate->frame, \
4160 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 x = NULL; \
4162 } \
4163 else { \
4164 x = call; \
4165 if (tstate->c_profilefunc != NULL) { \
4166 if (x == NULL) { \
4167 call_trace_protected(tstate->c_profilefunc, \
4168 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004169 tstate, tstate->frame, \
4170 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 /* XXX should pass (type, value, tb) */ \
4172 } else { \
4173 if (call_trace(tstate->c_profilefunc, \
4174 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004175 tstate, tstate->frame, \
4176 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 Py_DECREF(x); \
4178 x = NULL; \
4179 } \
4180 } \
4181 } \
4182 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004183} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 x = call; \
4185 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004186
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004187static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004188call_function(PyObject ***pp_stack, int oparg
4189#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004191#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004193{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 int na = oparg & 0xff;
4195 int nk = (oparg>>8) & 0xff;
4196 int n = na + 2 * nk;
4197 PyObject **pfunc = (*pp_stack) - n - 1;
4198 PyObject *func = *pfunc;
4199 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 /* Always dispatch PyCFunction first, because these are
4202 presumed to be the most frequent callable object.
4203 */
4204 if (PyCFunction_Check(func) && nk == 0) {
4205 int flags = PyCFunction_GET_FLAGS(func);
4206 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 PCALL(PCALL_CFUNCTION);
4209 if (flags & (METH_NOARGS | METH_O)) {
4210 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4211 PyObject *self = PyCFunction_GET_SELF(func);
4212 if (flags & METH_NOARGS && na == 0) {
4213 C_TRACE(x, (*meth)(self,NULL));
4214 }
4215 else if (flags & METH_O && na == 1) {
4216 PyObject *arg = EXT_POP(*pp_stack);
4217 C_TRACE(x, (*meth)(self,arg));
4218 Py_DECREF(arg);
4219 }
4220 else {
4221 err_args(func, flags, na);
4222 x = NULL;
4223 }
4224 }
4225 else {
4226 PyObject *callargs;
4227 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004228 if (callargs != NULL) {
4229 READ_TIMESTAMP(*pintr0);
4230 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4231 READ_TIMESTAMP(*pintr1);
4232 Py_XDECREF(callargs);
4233 }
4234 else {
4235 x = NULL;
4236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 }
4238 } else {
4239 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4240 /* optimize access to bound methods */
4241 PyObject *self = PyMethod_GET_SELF(func);
4242 PCALL(PCALL_METHOD);
4243 PCALL(PCALL_BOUND_METHOD);
4244 Py_INCREF(self);
4245 func = PyMethod_GET_FUNCTION(func);
4246 Py_INCREF(func);
4247 Py_DECREF(*pfunc);
4248 *pfunc = self;
4249 na++;
4250 n++;
4251 } else
4252 Py_INCREF(func);
4253 READ_TIMESTAMP(*pintr0);
4254 if (PyFunction_Check(func))
4255 x = fast_function(func, pp_stack, n, na, nk);
4256 else
4257 x = do_call(func, pp_stack, na, nk);
4258 READ_TIMESTAMP(*pintr1);
4259 Py_DECREF(func);
4260 }
Victor Stinnerf243ee42013-07-16 01:02:12 +02004261 assert((x != NULL && !PyErr_Occurred())
4262 || (x == NULL && PyErr_Occurred()));
Tim Peters8a5c3c72004-04-05 19:36:21 +00004263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 /* Clear the stack of the function object. Also removes
4265 the arguments in case they weren't consumed already
4266 (fast_function() and err_args() leave them on the stack).
4267 */
4268 while ((*pp_stack) > pfunc) {
4269 w = EXT_POP(*pp_stack);
4270 Py_DECREF(w);
4271 PCALL(PCALL_POP);
4272 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004273
4274 assert((x != NULL && !PyErr_Occurred())
4275 || (x == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004277}
4278
Jeremy Hylton192690e2002-08-16 18:36:11 +00004279/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004280 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004281 For the simplest case -- a function that takes only positional
4282 arguments and is called with only positional arguments -- it
4283 inlines the most primitive frame setup code from
4284 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4285 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004286*/
4287
4288static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004289fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004290{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4292 PyObject *globals = PyFunction_GET_GLOBALS(func);
4293 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4294 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4295 PyObject **d = NULL;
4296 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 PCALL(PCALL_FUNCTION);
4299 PCALL(PCALL_FAST_FUNCTION);
4300 if (argdefs == NULL && co->co_argcount == n &&
4301 co->co_kwonlyargcount == 0 && nk==0 &&
4302 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4303 PyFrameObject *f;
4304 PyObject *retval = NULL;
4305 PyThreadState *tstate = PyThreadState_GET();
4306 PyObject **fastlocals, **stack;
4307 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004309 PCALL(PCALL_FASTER_FUNCTION);
4310 assert(globals != NULL);
4311 /* XXX Perhaps we should create a specialized
4312 PyFrame_New() that doesn't take locals, but does
4313 take builtins without sanity checking them.
4314 */
4315 assert(tstate != NULL);
4316 f = PyFrame_New(tstate, co, globals, NULL);
4317 if (f == NULL)
4318 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 fastlocals = f->f_localsplus;
4321 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 for (i = 0; i < n; i++) {
4324 Py_INCREF(*stack);
4325 fastlocals[i] = *stack++;
4326 }
4327 retval = PyEval_EvalFrameEx(f,0);
4328 ++tstate->recursion_depth;
4329 Py_DECREF(f);
4330 --tstate->recursion_depth;
4331 return retval;
4332 }
4333 if (argdefs != NULL) {
4334 d = &PyTuple_GET_ITEM(argdefs, 0);
4335 nd = Py_SIZE(argdefs);
4336 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004337 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004338 (PyObject *)NULL, (*pp_stack)-n, na,
4339 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4340 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004341}
4342
4343static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004344update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4345 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 PyObject *kwdict = NULL;
4348 if (orig_kwdict == NULL)
4349 kwdict = PyDict_New();
4350 else {
4351 kwdict = PyDict_Copy(orig_kwdict);
4352 Py_DECREF(orig_kwdict);
4353 }
4354 if (kwdict == NULL)
4355 return NULL;
4356 while (--nk >= 0) {
4357 int err;
4358 PyObject *value = EXT_POP(*pp_stack);
4359 PyObject *key = EXT_POP(*pp_stack);
4360 if (PyDict_GetItem(kwdict, key) != NULL) {
4361 PyErr_Format(PyExc_TypeError,
4362 "%.200s%s got multiple values "
4363 "for keyword argument '%U'",
4364 PyEval_GetFuncName(func),
4365 PyEval_GetFuncDesc(func),
4366 key);
4367 Py_DECREF(key);
4368 Py_DECREF(value);
4369 Py_DECREF(kwdict);
4370 return NULL;
4371 }
4372 err = PyDict_SetItem(kwdict, key, value);
4373 Py_DECREF(key);
4374 Py_DECREF(value);
4375 if (err) {
4376 Py_DECREF(kwdict);
4377 return NULL;
4378 }
4379 }
4380 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004381}
4382
4383static PyObject *
4384update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004389 callargs = PyTuple_New(nstack + nstar);
4390 if (callargs == NULL) {
4391 return NULL;
4392 }
4393 if (nstar) {
4394 int i;
4395 for (i = 0; i < nstar; i++) {
4396 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4397 Py_INCREF(a);
4398 PyTuple_SET_ITEM(callargs, nstack + i, a);
4399 }
4400 }
4401 while (--nstack >= 0) {
4402 w = EXT_POP(*pp_stack);
4403 PyTuple_SET_ITEM(callargs, nstack, w);
4404 }
4405 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004406}
4407
4408static PyObject *
4409load_args(PyObject ***pp_stack, int na)
4410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004411 PyObject *args = PyTuple_New(na);
4412 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 if (args == NULL)
4415 return NULL;
4416 while (--na >= 0) {
4417 w = EXT_POP(*pp_stack);
4418 PyTuple_SET_ITEM(args, na, w);
4419 }
4420 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004421}
4422
4423static PyObject *
4424do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4425{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004426 PyObject *callargs = NULL;
4427 PyObject *kwdict = NULL;
4428 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004430 if (nk > 0) {
4431 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4432 if (kwdict == NULL)
4433 goto call_fail;
4434 }
4435 callargs = load_args(pp_stack, na);
4436 if (callargs == NULL)
4437 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004438#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004439 /* At this point, we have to look at the type of func to
4440 update the call stats properly. Do it here so as to avoid
4441 exposing the call stats machinery outside ceval.c
4442 */
4443 if (PyFunction_Check(func))
4444 PCALL(PCALL_FUNCTION);
4445 else if (PyMethod_Check(func))
4446 PCALL(PCALL_METHOD);
4447 else if (PyType_Check(func))
4448 PCALL(PCALL_TYPE);
4449 else if (PyCFunction_Check(func))
4450 PCALL(PCALL_CFUNCTION);
4451 else
4452 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004453#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 if (PyCFunction_Check(func)) {
4455 PyThreadState *tstate = PyThreadState_GET();
4456 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4457 }
4458 else
4459 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004460call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 Py_XDECREF(callargs);
4462 Py_XDECREF(kwdict);
4463 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004464}
4465
4466static PyObject *
4467ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 int nstar = 0;
4470 PyObject *callargs = NULL;
4471 PyObject *stararg = NULL;
4472 PyObject *kwdict = NULL;
4473 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 if (flags & CALL_FLAG_KW) {
4476 kwdict = EXT_POP(*pp_stack);
4477 if (!PyDict_Check(kwdict)) {
4478 PyObject *d;
4479 d = PyDict_New();
4480 if (d == NULL)
4481 goto ext_call_fail;
4482 if (PyDict_Update(d, kwdict) != 0) {
4483 Py_DECREF(d);
4484 /* PyDict_Update raises attribute
4485 * error (percolated from an attempt
4486 * to get 'keys' attribute) instead of
4487 * a type error if its second argument
4488 * is not a mapping.
4489 */
4490 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4491 PyErr_Format(PyExc_TypeError,
4492 "%.200s%.200s argument after ** "
4493 "must be a mapping, not %.200s",
4494 PyEval_GetFuncName(func),
4495 PyEval_GetFuncDesc(func),
4496 kwdict->ob_type->tp_name);
4497 }
4498 goto ext_call_fail;
4499 }
4500 Py_DECREF(kwdict);
4501 kwdict = d;
4502 }
4503 }
4504 if (flags & CALL_FLAG_VAR) {
4505 stararg = EXT_POP(*pp_stack);
4506 if (!PyTuple_Check(stararg)) {
4507 PyObject *t = NULL;
4508 t = PySequence_Tuple(stararg);
4509 if (t == NULL) {
4510 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4511 PyErr_Format(PyExc_TypeError,
4512 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004513 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 PyEval_GetFuncName(func),
4515 PyEval_GetFuncDesc(func),
4516 stararg->ob_type->tp_name);
4517 }
4518 goto ext_call_fail;
4519 }
4520 Py_DECREF(stararg);
4521 stararg = t;
4522 }
4523 nstar = PyTuple_GET_SIZE(stararg);
4524 }
4525 if (nk > 0) {
4526 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4527 if (kwdict == NULL)
4528 goto ext_call_fail;
4529 }
4530 callargs = update_star_args(na, nstar, stararg, pp_stack);
4531 if (callargs == NULL)
4532 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004533#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 /* At this point, we have to look at the type of func to
4535 update the call stats properly. Do it here so as to avoid
4536 exposing the call stats machinery outside ceval.c
4537 */
4538 if (PyFunction_Check(func))
4539 PCALL(PCALL_FUNCTION);
4540 else if (PyMethod_Check(func))
4541 PCALL(PCALL_METHOD);
4542 else if (PyType_Check(func))
4543 PCALL(PCALL_TYPE);
4544 else if (PyCFunction_Check(func))
4545 PCALL(PCALL_CFUNCTION);
4546 else
4547 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004548#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004549 if (PyCFunction_Check(func)) {
4550 PyThreadState *tstate = PyThreadState_GET();
4551 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4552 }
4553 else
4554 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004555ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 Py_XDECREF(callargs);
4557 Py_XDECREF(kwdict);
4558 Py_XDECREF(stararg);
Victor Stinnerf243ee42013-07-16 01:02:12 +02004559 assert((result != NULL && !PyErr_Occurred())
4560 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004562}
4563
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004564/* Extract a slice index from a PyInt or PyLong or an object with the
4565 nb_index slot defined, and store in *pi.
4566 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4567 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 +00004568 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004569*/
Tim Petersb5196382001-12-16 19:44:20 +00004570/* Note: If v is NULL, return success without storing into *pi. This
4571 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4572 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004573*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004574int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004575_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004576{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004577 if (v != NULL) {
4578 Py_ssize_t x;
4579 if (PyIndex_Check(v)) {
4580 x = PyNumber_AsSsize_t(v, NULL);
4581 if (x == -1 && PyErr_Occurred())
4582 return 0;
4583 }
4584 else {
4585 PyErr_SetString(PyExc_TypeError,
4586 "slice indices must be integers or "
4587 "None or have an __index__ method");
4588 return 0;
4589 }
4590 *pi = x;
4591 }
4592 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004593}
4594
Guido van Rossum486364b2007-06-30 05:01:58 +00004595#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004597
Guido van Rossumb209a111997-04-29 18:18:01 +00004598static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004599cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004600{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004601 int res = 0;
4602 switch (op) {
4603 case PyCmp_IS:
4604 res = (v == w);
4605 break;
4606 case PyCmp_IS_NOT:
4607 res = (v != w);
4608 break;
4609 case PyCmp_IN:
4610 res = PySequence_Contains(w, v);
4611 if (res < 0)
4612 return NULL;
4613 break;
4614 case PyCmp_NOT_IN:
4615 res = PySequence_Contains(w, v);
4616 if (res < 0)
4617 return NULL;
4618 res = !res;
4619 break;
4620 case PyCmp_EXC_MATCH:
4621 if (PyTuple_Check(w)) {
4622 Py_ssize_t i, length;
4623 length = PyTuple_Size(w);
4624 for (i = 0; i < length; i += 1) {
4625 PyObject *exc = PyTuple_GET_ITEM(w, i);
4626 if (!PyExceptionClass_Check(exc)) {
4627 PyErr_SetString(PyExc_TypeError,
4628 CANNOT_CATCH_MSG);
4629 return NULL;
4630 }
4631 }
4632 }
4633 else {
4634 if (!PyExceptionClass_Check(w)) {
4635 PyErr_SetString(PyExc_TypeError,
4636 CANNOT_CATCH_MSG);
4637 return NULL;
4638 }
4639 }
4640 res = PyErr_GivenExceptionMatches(v, w);
4641 break;
4642 default:
4643 return PyObject_RichCompare(v, w, op);
4644 }
4645 v = res ? Py_True : Py_False;
4646 Py_INCREF(v);
4647 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004648}
4649
Thomas Wouters52152252000-08-17 22:55:00 +00004650static PyObject *
4651import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004655 x = PyObject_GetAttr(v, name);
4656 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Brett Cannona79e4fb2013-07-12 11:22:26 -04004657 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 }
4659 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004660}
Guido van Rossumac7be682001-01-17 15:42:30 +00004661
Thomas Wouters52152252000-08-17 22:55:00 +00004662static int
4663import_all_from(PyObject *locals, PyObject *v)
4664{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004665 _Py_IDENTIFIER(__all__);
4666 _Py_IDENTIFIER(__dict__);
4667 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 PyObject *dict, *name, *value;
4669 int skip_leading_underscores = 0;
4670 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004672 if (all == NULL) {
4673 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4674 return -1; /* Unexpected error */
4675 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004676 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004677 if (dict == NULL) {
4678 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4679 return -1;
4680 PyErr_SetString(PyExc_ImportError,
4681 "from-import-* object has no __dict__ and no __all__");
4682 return -1;
4683 }
4684 all = PyMapping_Keys(dict);
4685 Py_DECREF(dict);
4686 if (all == NULL)
4687 return -1;
4688 skip_leading_underscores = 1;
4689 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 for (pos = 0, err = 0; ; pos++) {
4692 name = PySequence_GetItem(all, pos);
4693 if (name == NULL) {
4694 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4695 err = -1;
4696 else
4697 PyErr_Clear();
4698 break;
4699 }
4700 if (skip_leading_underscores &&
4701 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004702 PyUnicode_READY(name) != -1 &&
4703 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 {
4705 Py_DECREF(name);
4706 continue;
4707 }
4708 value = PyObject_GetAttr(v, name);
4709 if (value == NULL)
4710 err = -1;
4711 else if (PyDict_CheckExact(locals))
4712 err = PyDict_SetItem(locals, name, value);
4713 else
4714 err = PyObject_SetItem(locals, name, value);
4715 Py_DECREF(name);
4716 Py_XDECREF(value);
4717 if (err != 0)
4718 break;
4719 }
4720 Py_DECREF(all);
4721 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004722}
4723
Guido van Rossumac7be682001-01-17 15:42:30 +00004724static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004725format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004726{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004727 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 if (!obj)
4730 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004732 obj_str = _PyUnicode_AsString(obj);
4733 if (!obj_str)
4734 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004737}
Guido van Rossum950361c1997-01-24 13:49:28 +00004738
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004739static void
4740format_exc_unbound(PyCodeObject *co, int oparg)
4741{
4742 PyObject *name;
4743 /* Don't stomp existing exception */
4744 if (PyErr_Occurred())
4745 return;
4746 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4747 name = PyTuple_GET_ITEM(co->co_cellvars,
4748 oparg);
4749 format_exc_check_arg(
4750 PyExc_UnboundLocalError,
4751 UNBOUNDLOCAL_ERROR_MSG,
4752 name);
4753 } else {
4754 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4755 PyTuple_GET_SIZE(co->co_cellvars));
4756 format_exc_check_arg(PyExc_NameError,
4757 UNBOUNDFREE_ERROR_MSG, name);
4758 }
4759}
4760
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004761static PyObject *
4762unicode_concatenate(PyObject *v, PyObject *w,
4763 PyFrameObject *f, unsigned char *next_instr)
4764{
4765 PyObject *res;
4766 if (Py_REFCNT(v) == 2) {
4767 /* In the common case, there are 2 references to the value
4768 * stored in 'variable' when the += is performed: one on the
4769 * value stack (in 'v') and one still stored in the
4770 * 'variable'. We try to delete the variable now to reduce
4771 * the refcnt to 1.
4772 */
4773 switch (*next_instr) {
4774 case STORE_FAST:
4775 {
4776 int oparg = PEEKARG();
4777 PyObject **fastlocals = f->f_localsplus;
4778 if (GETLOCAL(oparg) == v)
4779 SETLOCAL(oparg, NULL);
4780 break;
4781 }
4782 case STORE_DEREF:
4783 {
4784 PyObject **freevars = (f->f_localsplus +
4785 f->f_code->co_nlocals);
4786 PyObject *c = freevars[PEEKARG()];
4787 if (PyCell_GET(c) == v)
4788 PyCell_Set(c, NULL);
4789 break;
4790 }
4791 case STORE_NAME:
4792 {
4793 PyObject *names = f->f_code->co_names;
4794 PyObject *name = GETITEM(names, PEEKARG());
4795 PyObject *locals = f->f_locals;
4796 if (PyDict_CheckExact(locals) &&
4797 PyDict_GetItem(locals, name) == v) {
4798 if (PyDict_DelItem(locals, name) != 0) {
4799 PyErr_Clear();
4800 }
4801 }
4802 break;
4803 }
4804 }
4805 }
4806 res = v;
4807 PyUnicode_Append(&res, w);
4808 return res;
4809}
4810
Guido van Rossum950361c1997-01-24 13:49:28 +00004811#ifdef DYNAMIC_EXECUTION_PROFILE
4812
Skip Montanarof118cb12001-10-15 20:51:38 +00004813static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004814getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004816 int i;
4817 PyObject *l = PyList_New(256);
4818 if (l == NULL) return NULL;
4819 for (i = 0; i < 256; i++) {
4820 PyObject *x = PyLong_FromLong(a[i]);
4821 if (x == NULL) {
4822 Py_DECREF(l);
4823 return NULL;
4824 }
4825 PyList_SetItem(l, i, x);
4826 }
4827 for (i = 0; i < 256; i++)
4828 a[i] = 0;
4829 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004830}
4831
4832PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004833_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004834{
4835#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004837#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004838 int i;
4839 PyObject *l = PyList_New(257);
4840 if (l == NULL) return NULL;
4841 for (i = 0; i < 257; i++) {
4842 PyObject *x = getarray(dxpairs[i]);
4843 if (x == NULL) {
4844 Py_DECREF(l);
4845 return NULL;
4846 }
4847 PyList_SetItem(l, i, x);
4848 }
4849 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004850#endif
4851}
4852
4853#endif