blob: 118f2b7f2e3f82f845aa3bacbe4ffc79ed50c47f [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
Victor Stinner0b881dd2014-12-12 13:17:41 +010068#define READ_TIMESTAMP(val) do { \
69 unsigned int h, l; \
70 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
71 (val) = ((uint64)l) | (((uint64)h) << 32); \
72 } while(0)
Mark Dickinsona25b1312009-10-31 10:18:44 +000073
74
75#else
76
77#error "Don't know how to implement timestamp counter for this architecture"
78
Michael W. Hudson800ba232004-08-12 18:19:17 +000079#endif
80
Thomas Wouters477c8d52006-05-27 19:21:47 +000081void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 uint64 intr, inst, loop;
85 PyThreadState *tstate = PyThreadState_Get();
86 if (!tstate->interp->tscdump)
87 return;
88 intr = intr1 - intr0;
89 inst = inst1 - inst0 - intr;
90 loop = loop1 - loop0 - intr;
91 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000092 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093}
Michael W. Hudson800ba232004-08-12 18:19:17 +000094
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000095#endif
96
Guido van Rossum04691fc1992-08-12 15:35:34 +000097/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000098/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000099
Guido van Rossum408027e1996-12-30 16:17:54 +0000100#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000101/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102#define LLTRACE 1 /* Low-level trace feature */
103#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000104#endif
105
Jeremy Hylton52820442001-01-03 23:52:36 +0000106typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000107
Guido van Rossum374a9221991-04-04 10:40:29 +0000108/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000113#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000114static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
115static PyObject * do_call(PyObject *, PyObject ***, int, int);
116static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000117static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000119static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
120static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000121#define CALL_FLAG_VAR 1
122#define CALL_FLAG_KW 2
123
Guido van Rossum0a066c01992-03-27 17:29:15 +0000124#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000125static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000126static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000127#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100128static int call_trace(Py_tracefunc, PyObject *,
129 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000131static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100132 PyThreadState *, PyFrameObject *,
133 int, PyObject *);
134static void call_exc_trace(Py_tracefunc, PyObject *,
135 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000136static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100137 PyThreadState *, PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000138
Thomas Wouters477c8d52006-05-27 19:21:47 +0000139static PyObject * cmp_outcome(int, PyObject *, PyObject *);
140static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000141static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000142static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000143static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200144static PyObject * unicode_concatenate(PyObject *, PyObject *,
145 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500146static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000147
Paul Prescode68140d2000-08-30 20:25:01 +0000148#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000150#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000152#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 "free variable '%.200s' referenced before assignment" \
154 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000155
Guido van Rossum950361c1997-01-24 13:49:28 +0000156/* Dynamic execution profile */
157#ifdef DYNAMIC_EXECUTION_PROFILE
158#ifdef DXPAIRS
159static long dxpairs[257][256];
160#define dxp dxpairs[256]
161#else
162static long dxp[256];
163#endif
164#endif
165
Jeremy Hylton985eba52003-02-05 23:13:00 +0000166/* Function call profile */
167#ifdef CALL_PROFILE
168#define PCALL_NUM 11
169static int pcall[PCALL_NUM];
170
171#define PCALL_ALL 0
172#define PCALL_FUNCTION 1
173#define PCALL_FAST_FUNCTION 2
174#define PCALL_FASTER_FUNCTION 3
175#define PCALL_METHOD 4
176#define PCALL_BOUND_METHOD 5
177#define PCALL_CFUNCTION 6
178#define PCALL_TYPE 7
179#define PCALL_GENERATOR 8
180#define PCALL_OTHER 9
181#define PCALL_POP 10
182
183/* Notes about the statistics
184
185 PCALL_FAST stats
186
187 FAST_FUNCTION means no argument tuple needs to be created.
188 FASTER_FUNCTION means that the fast-path frame setup code is used.
189
190 If there is a method call where the call can be optimized by changing
191 the argument tuple and calling the function directly, it gets recorded
192 twice.
193
194 As a result, the relationship among the statistics appears to be
195 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
196 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
197 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
198 PCALL_METHOD > PCALL_BOUND_METHOD
199*/
200
201#define PCALL(POS) pcall[POS]++
202
203PyObject *
204PyEval_GetCallStats(PyObject *self)
205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000206 return Py_BuildValue("iiiiiiiiiii",
207 pcall[0], pcall[1], pcall[2], pcall[3],
208 pcall[4], pcall[5], pcall[6], pcall[7],
209 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000210}
211#else
212#define PCALL(O)
213
214PyObject *
215PyEval_GetCallStats(PyObject *self)
216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 Py_INCREF(Py_None);
218 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000219}
220#endif
221
Tim Peters5ca576e2001-06-18 22:08:13 +0000222
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000223#ifdef WITH_THREAD
224#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
225#else
226#define GIL_REQUEST 0
227#endif
228
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000229/* This can set eval_breaker to 0 even though gil_drop_request became
230 1. We believe this is all right because the eval loop will release
231 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000232#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_store_relaxed( \
234 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000235 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
237 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000239#ifdef WITH_THREAD
240
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000242 do { \
243 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
244 _Py_atomic_store_relaxed(&eval_breaker, 1); \
245 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000246
247#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 do { \
249 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
250 COMPUTE_EVAL_BREAKER(); \
251 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000253#endif
254
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000255/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000256#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 do { \
258 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
259 _Py_atomic_store_relaxed(&eval_breaker, 1); \
260 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000261
262#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 do { \
264 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
265 COMPUTE_EVAL_BREAKER(); \
266 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000267
268#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 do { \
270 pending_async_exc = 1; \
271 _Py_atomic_store_relaxed(&eval_breaker, 1); \
272 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000275 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000276
277
Guido van Rossume59214e1994-08-30 08:01:59 +0000278#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000279
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000280#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000281#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000282#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000283#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000284
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000285static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000286static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000287/* This single variable consolidates all requests to break out of the fast path
288 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000289static _Py_atomic_int eval_breaker = {0};
290/* Request for dropping the GIL */
291static _Py_atomic_int gil_drop_request = {0};
292/* Request for running pending calls. */
293static _Py_atomic_int pendingcalls_to_do = {0};
294/* Request for looking at the `async_exc` field of the current thread state.
295 Guarded by the GIL. */
296static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000297
298#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000299
Tim Peters7f468f22004-10-11 02:40:51 +0000300int
301PyEval_ThreadsInitialized(void)
302{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000304}
305
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000306void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000307PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000308{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 if (gil_created())
310 return;
311 create_gil();
312 take_gil(PyThreadState_GET());
313 main_thread = PyThread_get_thread_ident();
314 if (!pending_lock)
315 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000316}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000317
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000318void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000319_PyEval_FiniThreads(void)
320{
321 if (!gil_created())
322 return;
323 destroy_gil();
324 assert(!gil_created());
325}
326
327void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000328PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 PyThreadState *tstate = PyThreadState_GET();
331 if (tstate == NULL)
332 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
333 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000334}
335
336void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000337PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 /* This function must succeed when the current thread state is NULL.
340 We therefore avoid PyThreadState_GET() which dumps a fatal error
341 in debug mode.
342 */
343 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
344 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000345}
346
347void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000348PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (tstate == NULL)
351 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
352 /* Check someone has called PyEval_InitThreads() to create the lock */
353 assert(gil_created());
354 take_gil(tstate);
355 if (PyThreadState_Swap(tstate) != NULL)
356 Py_FatalError(
357 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000358}
359
360void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000361PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 if (tstate == NULL)
364 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
365 if (PyThreadState_Swap(NULL) != tstate)
366 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
367 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000368}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000369
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200370/* This function is called from PyOS_AfterFork to destroy all threads which are
371 * not running in the child process, and clear internal locks which might be
372 * held by those threads. (This could also be done using pthread_atfork
373 * mechanism, at least for the pthreads implementation.) */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000374
375void
376PyEval_ReInitThreads(void)
377{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200378 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 PyObject *threading, *result;
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200380 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000382 if (!gil_created())
383 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 recreate_gil();
385 pending_lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200386 take_gil(current_tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 /* Update the threading module with the new state.
390 */
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200391 threading = PyMapping_GetItemString(current_tstate->interp->modules,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 "threading");
393 if (threading == NULL) {
394 /* threading not imported */
395 PyErr_Clear();
396 return;
397 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200398 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 if (result == NULL)
400 PyErr_WriteUnraisable(threading);
401 else
402 Py_DECREF(result);
403 Py_DECREF(threading);
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200404
405 /* Destroy all threads except the current one */
406 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000407}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000408
409#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000410static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000411static int pending_async_exc = 0;
412#endif /* WITH_THREAD */
413
414/* This function is used to signal that async exceptions are waiting to be
415 raised, therefore it is also useful in non-threaded builds. */
416
417void
418_PyEval_SignalAsyncExc(void)
419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000421}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422
Guido van Rossumff4949e1992-08-05 19:58:53 +0000423/* Functions save_thread and restore_thread are always defined so
424 dynamically loaded modules needn't be compiled separately for use
425 with and without threads: */
426
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000427PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000428PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000429{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyThreadState *tstate = PyThreadState_Swap(NULL);
431 if (tstate == NULL)
432 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000433#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 if (gil_created())
435 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000438}
439
440void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000441PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000442{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 if (tstate == NULL)
444 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000445#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 if (gil_created()) {
447 int err = errno;
448 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200449 /* _Py_Finalizing is protected by the GIL */
450 if (_Py_Finalizing && tstate != _Py_Finalizing) {
451 drop_gil(tstate);
452 PyThread_exit_thread();
453 assert(0); /* unreachable */
454 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000455 errno = err;
456 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000457#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000459}
460
461
Guido van Rossuma9672091994-09-14 13:31:22 +0000462/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
463 signal handlers or Mac I/O completion routines) can schedule calls
464 to a function to be called synchronously.
465 The synchronous function is called with one void* argument.
466 It should return 0 for success or -1 for failure -- failure should
467 be accompanied by an exception.
468
469 If registry succeeds, the registry function returns 0; if it fails
470 (e.g. due to too many pending calls) it returns -1 (without setting
471 an exception condition).
472
473 Note that because registry may occur from within signal handlers,
474 or other asynchronous events, calling malloc() is unsafe!
475
476#ifdef WITH_THREAD
477 Any thread can schedule pending calls, but only the main thread
478 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479 There is no facility to schedule calls to a particular thread, but
480 that should be easy to change, should that ever be required. In
481 that case, the static variables here should go into the python
482 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000483#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000485
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000486#ifdef WITH_THREAD
487
488/* The WITH_THREAD implementation is thread-safe. It allows
489 scheduling to be made from any thread, and even from an executing
490 callback.
491 */
492
493#define NPENDINGCALLS 32
494static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 int (*func)(void *);
496 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497} pendingcalls[NPENDINGCALLS];
498static int pendingfirst = 0;
499static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000500
501int
502Py_AddPendingCall(int (*func)(void *), void *arg)
503{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 int i, j, result=0;
505 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 /* try a few times for the lock. Since this mechanism is used
508 * for signal handling (on the main thread), there is a (slim)
509 * chance that a signal is delivered on the same thread while we
510 * hold the lock during the Py_MakePendingCalls() function.
511 * This avoids a deadlock in that case.
512 * Note that signals can be delivered on any thread. In particular,
513 * on Windows, a SIGINT is delivered on a system-created worker
514 * thread.
515 * We also check for lock being NULL, in the unlikely case that
516 * this function is called before any bytecode evaluation takes place.
517 */
518 if (lock != NULL) {
519 for (i = 0; i<100; i++) {
520 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
521 break;
522 }
523 if (i == 100)
524 return -1;
525 }
526
527 i = pendinglast;
528 j = (i + 1) % NPENDINGCALLS;
529 if (j == pendingfirst) {
530 result = -1; /* Queue full */
531 } else {
532 pendingcalls[i].func = func;
533 pendingcalls[i].arg = arg;
534 pendinglast = j;
535 }
536 /* signal main loop */
537 SIGNAL_PENDING_CALLS();
538 if (lock != NULL)
539 PyThread_release_lock(lock);
540 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000541}
542
543int
544Py_MakePendingCalls(void)
545{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200546 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 int i;
548 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000550 if (!pending_lock) {
551 /* initial allocation of the lock */
552 pending_lock = PyThread_allocate_lock();
553 if (pending_lock == NULL)
554 return -1;
555 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 /* only service pending calls on main thread */
558 if (main_thread && PyThread_get_thread_ident() != main_thread)
559 return 0;
560 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200561 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200563 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 /* perform a bounded number of calls, in case of recursion */
565 for (i=0; i<NPENDINGCALLS; i++) {
566 int j;
567 int (*func)(void *);
568 void *arg = NULL;
569
570 /* pop one item off the queue while holding the lock */
571 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
572 j = pendingfirst;
573 if (j == pendinglast) {
574 func = NULL; /* Queue empty */
575 } else {
576 func = pendingcalls[j].func;
577 arg = pendingcalls[j].arg;
578 pendingfirst = (j + 1) % NPENDINGCALLS;
579 }
580 if (pendingfirst != pendinglast)
581 SIGNAL_PENDING_CALLS();
582 else
583 UNSIGNAL_PENDING_CALLS();
584 PyThread_release_lock(pending_lock);
585 /* having released the lock, perform the callback */
586 if (func == NULL)
587 break;
588 r = func(arg);
589 if (r)
590 break;
591 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200592 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000594}
595
596#else /* if ! defined WITH_THREAD */
597
598/*
599 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
600 This code is used for signal handling in python that isn't built
601 with WITH_THREAD.
602 Don't use this implementation when Py_AddPendingCalls() can happen
603 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604
Guido van Rossuma9672091994-09-14 13:31:22 +0000605 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000606 (1) nested asynchronous calls to Py_AddPendingCall()
607 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000608
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 (1) is very unlikely because typically signal delivery
610 is blocked during signal handling. So it should be impossible.
611 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000612 The current code is safe against (2), but not against (1).
613 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000614 thread is present, interrupted by signals, and that the critical
615 section is protected with the "busy" variable. On Windows, which
616 delivers SIGINT on a system thread, this does not hold and therefore
617 Windows really shouldn't use this version.
618 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000619*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000620
Guido van Rossuma9672091994-09-14 13:31:22 +0000621#define NPENDINGCALLS 32
622static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 int (*func)(void *);
624 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000625} pendingcalls[NPENDINGCALLS];
626static volatile int pendingfirst = 0;
627static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000628static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000629
630int
Thomas Wouters334fb892000-07-25 12:56:38 +0000631Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000632{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 static volatile int busy = 0;
634 int i, j;
635 /* XXX Begin critical section */
636 if (busy)
637 return -1;
638 busy = 1;
639 i = pendinglast;
640 j = (i + 1) % NPENDINGCALLS;
641 if (j == pendingfirst) {
642 busy = 0;
643 return -1; /* Queue full */
644 }
645 pendingcalls[i].func = func;
646 pendingcalls[i].arg = arg;
647 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000649 SIGNAL_PENDING_CALLS();
650 busy = 0;
651 /* XXX End critical section */
652 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000653}
654
Guido van Rossum180d7b41994-09-29 09:45:57 +0000655int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000656Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000657{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 static int busy = 0;
659 if (busy)
660 return 0;
661 busy = 1;
662 UNSIGNAL_PENDING_CALLS();
663 for (;;) {
664 int i;
665 int (*func)(void *);
666 void *arg;
667 i = pendingfirst;
668 if (i == pendinglast)
669 break; /* Queue empty */
670 func = pendingcalls[i].func;
671 arg = pendingcalls[i].arg;
672 pendingfirst = (i + 1) % NPENDINGCALLS;
673 if (func(arg) < 0) {
674 busy = 0;
675 SIGNAL_PENDING_CALLS(); /* We're not done yet */
676 return -1;
677 }
678 }
679 busy = 0;
680 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000681}
682
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000683#endif /* WITH_THREAD */
684
Guido van Rossuma9672091994-09-14 13:31:22 +0000685
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000686/* The interpreter's recursion limit */
687
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000688#ifndef Py_DEFAULT_RECURSION_LIMIT
689#define Py_DEFAULT_RECURSION_LIMIT 1000
690#endif
691static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
692int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000694int
695Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000698}
699
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000700void
701Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703 recursion_limit = new_limit;
704 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000705}
706
Armin Rigo2b3eb402003-10-28 12:05:48 +0000707/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
708 if the recursion_depth reaches _Py_CheckRecursionLimit.
709 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
710 to guarantee that _Py_CheckRecursiveCall() is regularly called.
711 Without USE_STACKCHECK, there is no need for this. */
712int
713_Py_CheckRecursiveCall(char *where)
714{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000716
717#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 if (PyOS_CheckStack()) {
719 --tstate->recursion_depth;
720 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
721 return -1;
722 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000723#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724 _Py_CheckRecursionLimit = recursion_limit;
725 if (tstate->recursion_critical)
726 /* Somebody asked that we don't check for recursion. */
727 return 0;
728 if (tstate->overflowed) {
729 if (tstate->recursion_depth > recursion_limit + 50) {
730 /* Overflowing while handling an overflow. Give up. */
731 Py_FatalError("Cannot recover from stack overflow.");
732 }
733 return 0;
734 }
735 if (tstate->recursion_depth > recursion_limit) {
736 --tstate->recursion_depth;
737 tstate->overflowed = 1;
738 PyErr_Format(PyExc_RuntimeError,
739 "maximum recursion depth exceeded%s",
740 where);
741 return -1;
742 }
743 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000744}
745
Guido van Rossum374a9221991-04-04 10:40:29 +0000746/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000747enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000748 WHY_NOT = 0x0001, /* No error */
749 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000750 WHY_RETURN = 0x0008, /* 'return' statement */
751 WHY_BREAK = 0x0010, /* 'break' statement */
752 WHY_CONTINUE = 0x0020, /* 'continue' statement */
753 WHY_YIELD = 0x0040, /* 'yield' operator */
754 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000755};
Guido van Rossum374a9221991-04-04 10:40:29 +0000756
Benjamin Peterson87880242011-07-03 16:48:31 -0500757static void save_exc_state(PyThreadState *, PyFrameObject *);
758static void swap_exc_state(PyThreadState *, PyFrameObject *);
759static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400760static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000761static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000762
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000763/* Records whether tracing is on for any thread. Counts the number of
764 threads for which tstate->c_tracefunc is non-NULL, so if the value
765 is 0, we know we don't have to check this thread's c_tracefunc.
766 This speeds up the if statement in PyEval_EvalFrameEx() after
767 fast_next_opcode*/
768static int _Py_TracingPossible = 0;
769
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000770
Guido van Rossum374a9221991-04-04 10:40:29 +0000771
Guido van Rossumb209a111997-04-29 18:18:01 +0000772PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000773PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 return PyEval_EvalCodeEx(co,
776 globals, locals,
777 (PyObject **)NULL, 0,
778 (PyObject **)NULL, 0,
779 (PyObject **)NULL, 0,
780 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000781}
782
783
784/* Interpreter main loop */
785
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000786PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000787PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 /* This is for backward compatibility with extension modules that
789 used this API; core interpreter code should call
790 PyEval_EvalFrameEx() */
791 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000792}
793
794PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000795PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000796{
Guido van Rossum950361c1997-01-24 13:49:28 +0000797#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000799#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200800 PyObject **stack_pointer; /* Next free slot in value stack */
801 unsigned char *next_instr;
802 int opcode; /* Current opcode */
803 int oparg; /* Current opcode argument, if any */
804 enum why_code why; /* Reason for block stack unwind */
805 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000806 PyObject *retval = NULL; /* Return value */
807 PyThreadState *tstate = PyThreadState_GET();
808 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 is true when the line being executed has changed. The
815 initial values are such as to make this false the first
816 time it is tested. */
817 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 unsigned char *first_instr;
820 PyObject *names;
821 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000822
Brett Cannon368b4b72012-04-02 12:17:59 -0400823#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200824 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400825#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200826
Antoine Pitroub52ec782009-01-25 16:34:23 +0000827/* Computed GOTOs, or
828 the-optimization-commonly-but-improperly-known-as-"threaded code"
829 using gcc's labels-as-values extension
830 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
831
832 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000834 combined with a lookup table of jump addresses. However, since the
835 indirect jump instruction is shared by all opcodes, the CPU will have a
836 hard time making the right prediction for where to jump next (actually,
837 it will be always wrong except in the uncommon case of a sequence of
838 several identical opcodes).
839
840 "Threaded code" in contrast, uses an explicit jump table and an explicit
841 indirect jump instruction at the end of each opcode. Since the jump
842 instruction is at a different address for each opcode, the CPU will make a
843 separate prediction for each of these instructions, which is equivalent to
844 predicting the second opcode of each opcode pair. These predictions have
845 a much better chance to turn out valid, especially in small bytecode loops.
846
847 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000849 and potentially many more instructions (depending on the pipeline width).
850 A correctly predicted branch, however, is nearly free.
851
852 At the time of this writing, the "threaded code" version is up to 15-20%
853 faster than the normal "switch" version, depending on the compiler and the
854 CPU architecture.
855
856 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
857 because it would render the measurements invalid.
858
859
860 NOTE: care must be taken that the compiler doesn't try to "optimize" the
861 indirect jumps by sharing them between all opcodes. Such optimizations
862 can be disabled on gcc by using the -fno-gcse flag (or possibly
863 -fno-crossjumping).
864*/
865
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000868#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000869#endif
870
Antoine Pitrou042b1282010-08-13 21:15:58 +0000871#ifdef HAVE_COMPUTED_GOTOS
872 #ifndef USE_COMPUTED_GOTOS
873 #define USE_COMPUTED_GOTOS 1
874 #endif
875#else
876 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
877 #error "Computed gotos are not supported on this compiler."
878 #endif
879 #undef USE_COMPUTED_GOTOS
880 #define USE_COMPUTED_GOTOS 0
881#endif
882
883#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000884/* Import the static jump table */
885#include "opcode_targets.h"
886
887/* This macro is used when several opcodes defer to the same implementation
888 (e.g. SETUP_LOOP, SETUP_FINALLY) */
889#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 TARGET_##op: \
891 opcode = op; \
892 if (HAS_ARG(op)) \
893 oparg = NEXTARG(); \
894 case op: \
895 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000896
897#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 TARGET_##op: \
899 opcode = op; \
900 if (HAS_ARG(op)) \
901 oparg = NEXTARG(); \
902 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000903
904
905#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 { \
907 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
908 FAST_DISPATCH(); \
909 } \
910 continue; \
911 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912
913#ifdef LLTRACE
914#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 { \
916 if (!lltrace && !_Py_TracingPossible) { \
917 f->f_lasti = INSTR_OFFSET(); \
918 goto *opcode_targets[*next_instr++]; \
919 } \
920 goto fast_next_opcode; \
921 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922#else
923#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 { \
925 if (!_Py_TracingPossible) { \
926 f->f_lasti = INSTR_OFFSET(); \
927 goto *opcode_targets[*next_instr++]; \
928 } \
929 goto fast_next_opcode; \
930 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000931#endif
932
933#else
934#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000936#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 /* silence compiler warnings about `impl` unused */ \
938 if (0) goto impl; \
939 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000940#define DISPATCH() continue
941#define FAST_DISPATCH() goto fast_next_opcode
942#endif
943
944
Neal Norwitza81d2202002-07-14 00:27:26 +0000945/* Tuple access macros */
946
947#ifndef Py_DEBUG
948#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
949#else
950#define GETITEM(v, i) PyTuple_GetItem((v), (i))
951#endif
952
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000953#ifdef WITH_TSC
954/* Use Pentium timestamp counter to mark certain events:
955 inst0 -- beginning of switch statement for opcode dispatch
956 inst1 -- end of switch statement (may be skipped)
957 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000958 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000959 (may be skipped)
960 intr1 -- beginning of long interruption
961 intr2 -- end of long interruption
962
963 Many opcodes call out to helper C functions. In some cases, the
964 time in those functions should be counted towards the time for the
965 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
966 calls another Python function; there's no point in charge all the
967 bytecode executed by the called function to the caller.
968
969 It's hard to make a useful judgement statically. In the presence
970 of operator overloading, it's impossible to tell if a call will
971 execute new Python code or not.
972
973 It's a case-by-case judgement. I'll use intr1 for the following
974 cases:
975
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000976 IMPORT_STAR
977 IMPORT_FROM
978 CALL_FUNCTION (and friends)
979
980 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
982 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 READ_TIMESTAMP(inst0);
985 READ_TIMESTAMP(inst1);
986 READ_TIMESTAMP(loop0);
987 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989 /* shut up the compiler */
990 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000991#endif
992
Guido van Rossum374a9221991-04-04 10:40:29 +0000993/* Code access macros */
994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995#define INSTR_OFFSET() ((int)(next_instr - first_instr))
996#define NEXTOP() (*next_instr++)
997#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
998#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
999#define JUMPTO(x) (next_instr = first_instr + (x))
1000#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +00001001
Raymond Hettingerf606f872003-03-16 03:11:04 +00001002/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Some opcodes tend to come in pairs thus making it possible to
1004 predict the second code when the first is run. For example,
1005 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1006 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 Verifying the prediction costs a single high-speed test of a register
1009 variable against a constant. If the pairing was good, then the
1010 processor's own internal branch predication has a high likelihood of
1011 success, resulting in a nearly zero-overhead transition to the
1012 next opcode. A successful prediction saves a trip through the eval-loop
1013 including its two unpredictable branches, the HAS_ARG test and the
1014 switch-case. Combined with the processor's internal branch prediction,
1015 a successful PREDICT has the effect of making the two opcodes run as if
1016 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001017
Georg Brandl86b2fb92008-07-16 03:43:04 +00001018 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 predictions turned-on and interpret the results as if some opcodes
1020 had been combined or turn-off predictions so that the opcode frequency
1021 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001022
1023 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 the CPU to record separate branch prediction information for each
1025 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001026
Raymond Hettingerf606f872003-03-16 03:11:04 +00001027*/
1028
Antoine Pitrou042b1282010-08-13 21:15:58 +00001029#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030#define PREDICT(op) if (0) goto PRED_##op
1031#define PREDICTED(op) PRED_##op:
1032#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001033#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1035#define PREDICTED(op) PRED_##op: next_instr++
1036#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001037#endif
1038
Raymond Hettingerf606f872003-03-16 03:11:04 +00001039
Guido van Rossum374a9221991-04-04 10:40:29 +00001040/* Stack manipulation macros */
1041
Martin v. Löwis18e16552006-02-15 17:27:45 +00001042/* The stack can grow at most MAXINT deep, as co_nlocals and
1043 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001044#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1045#define EMPTY() (STACK_LEVEL() == 0)
1046#define TOP() (stack_pointer[-1])
1047#define SECOND() (stack_pointer[-2])
1048#define THIRD() (stack_pointer[-3])
1049#define FOURTH() (stack_pointer[-4])
1050#define PEEK(n) (stack_pointer[-(n)])
1051#define SET_TOP(v) (stack_pointer[-1] = (v))
1052#define SET_SECOND(v) (stack_pointer[-2] = (v))
1053#define SET_THIRD(v) (stack_pointer[-3] = (v))
1054#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1055#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1056#define BASIC_STACKADJ(n) (stack_pointer += n)
1057#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1058#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001059
Guido van Rossum96a42c81992-01-12 02:29:51 +00001060#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001062 lltrace && prtrace(TOP(), "push")); \
1063 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001067 lltrace && prtrace(TOP(), "stackadj")); \
1068 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001069#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001070 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1071 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001072#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001073#define PUSH(v) BASIC_PUSH(v)
1074#define POP() BASIC_POP()
1075#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001076#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001077#endif
1078
Guido van Rossum681d79a1995-07-18 14:51:37 +00001079/* Local variable macros */
1080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001082
1083/* The SETLOCAL() macro must not DECREF the local variable in-place and
1084 then store the new value; it must copy the old value to a temporary
1085 value, then store the new value, and then DECREF the temporary value.
1086 This is because it is possible that during the DECREF the frame is
1087 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1088 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001090 GETLOCAL(i) = value; \
1091 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001092
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001093
1094#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 while (STACK_LEVEL() > (b)->b_level) { \
1096 PyObject *v = POP(); \
1097 Py_XDECREF(v); \
1098 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001099
1100#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001101 { \
1102 PyObject *type, *value, *traceback; \
1103 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1104 while (STACK_LEVEL() > (b)->b_level + 3) { \
1105 value = POP(); \
1106 Py_XDECREF(value); \
1107 } \
1108 type = tstate->exc_type; \
1109 value = tstate->exc_value; \
1110 traceback = tstate->exc_traceback; \
1111 tstate->exc_type = POP(); \
1112 tstate->exc_value = POP(); \
1113 tstate->exc_traceback = POP(); \
1114 Py_XDECREF(type); \
1115 Py_XDECREF(value); \
1116 Py_XDECREF(traceback); \
1117 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001118
Guido van Rossuma027efa1997-05-05 20:56:21 +00001119/* Start of code */
1120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 /* push frame */
1122 if (Py_EnterRecursiveCall(""))
1123 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (tstate->use_tracing) {
1128 if (tstate->c_tracefunc != NULL) {
1129 /* tstate->c_tracefunc, if defined, is a
1130 function that will be called on *every* entry
1131 to a code block. Its return value, if not
1132 None, is a function that will be called at
1133 the start of each executed line of code.
1134 (Actually, the function must return itself
1135 in order to continue tracing.) The trace
1136 functions are called with three arguments:
1137 a pointer to the current frame, a string
1138 indicating why the function is called, and
1139 an argument which depends on the situation.
1140 The global trace function is also called
1141 whenever an exception is detected. */
1142 if (call_trace_protected(tstate->c_tracefunc,
1143 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001144 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 /* Trace function raised an error */
1146 goto exit_eval_frame;
1147 }
1148 }
1149 if (tstate->c_profilefunc != NULL) {
1150 /* Similar for c_profilefunc, except it needn't
1151 return itself and isn't called for "line" events */
1152 if (call_trace_protected(tstate->c_profilefunc,
1153 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001154 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 /* Profile function raised an error */
1156 goto exit_eval_frame;
1157 }
1158 }
1159 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 co = f->f_code;
1162 names = co->co_names;
1163 consts = co->co_consts;
1164 fastlocals = f->f_localsplus;
1165 freevars = f->f_localsplus + co->co_nlocals;
1166 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1167 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001168
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001169 f->f_lasti now refers to the index of the last instruction
1170 executed. You might think this was obvious from the name, but
1171 this wasn't always true before 2.3! PyFrame_New now sets
1172 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1173 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1174 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001175 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1176 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 When the PREDICT() macros are enabled, some opcode pairs follow in
1179 direct succession without updating f->f_lasti. A successful
1180 prediction effectively links the two codes together as if they
1181 were a single new opcode; accordingly,f->f_lasti will point to
1182 the first code in the pair (for instance, GET_ITER followed by
1183 FOR_ITER is effectively a single opcode and f->f_lasti will point
1184 at to the beginning of the combined pair.)
1185 */
1186 next_instr = first_instr + f->f_lasti + 1;
1187 stack_pointer = f->f_stacktop;
1188 assert(stack_pointer != NULL);
1189 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +02001190 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (co->co_flags & CO_GENERATOR && !throwflag) {
1193 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1194 /* We were in an except handler when we left,
1195 restore the exception state which was put aside
1196 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001197 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001199 else
1200 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001202
Tim Peters5ca576e2001-06-18 22:08:13 +00001203#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001204 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001205#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001209 if (throwflag) /* support for generator.throw() */
1210 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001211
Victor Stinnerace47d72013-07-18 01:41:08 +02001212#ifdef Py_DEBUG
1213 /* PyEval_EvalFrameEx() must not be called with an exception set,
1214 because it may clear it (directly or indirectly) and so the
1215 caller looses its exception */
1216 assert(!PyErr_Occurred());
1217#endif
1218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001220#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (inst1 == 0) {
1222 /* Almost surely, the opcode executed a break
1223 or a continue, preventing inst1 from being set
1224 on the way out of the loop.
1225 */
1226 READ_TIMESTAMP(inst1);
1227 loop1 = inst1;
1228 }
1229 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1230 intr0, intr1);
1231 ticked = 0;
1232 inst1 = 0;
1233 intr0 = 0;
1234 intr1 = 0;
1235 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001236#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1238 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +02001239 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 /* Do periodic things. Doing this every time through
1242 the loop would add too much overhead, so we do it
1243 only every Nth instruction. We also do it if
1244 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1245 event needs attention (e.g. a signal handler or
1246 async I/O handler); see Py_AddPendingCall() and
1247 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1250 if (*next_instr == SETUP_FINALLY) {
1251 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001252 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 goto fast_next_opcode;
1254 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001255#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001257#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001259 if (Py_MakePendingCalls() < 0)
1260 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001262#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001263 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* Give another thread a chance */
1265 if (PyThreadState_Swap(NULL) != tstate)
1266 Py_FatalError("ceval: tstate mix-up");
1267 drop_gil(tstate);
1268
1269 /* Other threads may run now */
1270
1271 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -07001272
1273 /* Check if we should make a quick exit. */
1274 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1275 drop_gil(tstate);
1276 PyThread_exit_thread();
1277 }
1278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (PyThreadState_Swap(tstate) != NULL)
1280 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001282#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 /* Check for asynchronous exceptions. */
1284 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 tstate->async_exc = NULL;
1287 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001288 PyErr_SetNone(exc);
1289 Py_DECREF(exc);
1290 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 }
1292 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 fast_next_opcode:
1295 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001300 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001301 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 /* see maybe_call_line_trace
1303 for expository comments */
1304 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 err = maybe_call_line_trace(tstate->c_tracefunc,
1307 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001308 tstate, f,
1309 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* Reload possibly changed frame fields */
1311 JUMPTO(f->f_lasti);
1312 if (f->f_stacktop != NULL) {
1313 stack_pointer = f->f_stacktop;
1314 f->f_stacktop = NULL;
1315 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 opcode = NEXTOP();
1324 oparg = 0; /* allows oparg to be stored in a register because
1325 it doesn't have to be remembered across a full loop */
1326 if (HAS_ARG(opcode))
1327 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001328 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001329#ifdef DYNAMIC_EXECUTION_PROFILE
1330#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 dxpairs[lastopcode][opcode]++;
1332 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001333#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001335#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001336
Guido van Rossum96a42c81992-01-12 02:29:51 +00001337#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (lltrace) {
1341 if (HAS_ARG(opcode)) {
1342 printf("%d: %d, %d\n",
1343 f->f_lasti, opcode, oparg);
1344 }
1345 else {
1346 printf("%d: %d\n",
1347 f->f_lasti, opcode);
1348 }
1349 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001350#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 /* Main switch on opcode */
1353 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 /* BEWARE!
1358 It is essential that any operation that fails sets either
1359 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1360 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 TARGET(NOP)
1363 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001364
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001365 TARGET(LOAD_FAST) {
1366 PyObject *value = GETLOCAL(oparg);
1367 if (value == NULL) {
1368 format_exc_check_arg(PyExc_UnboundLocalError,
1369 UNBOUNDLOCAL_ERROR_MSG,
1370 PyTuple_GetItem(co->co_varnames, oparg));
1371 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001373 Py_INCREF(value);
1374 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 }
1377
1378 TARGET(LOAD_CONST) {
1379 PyObject *value = GETITEM(consts, oparg);
1380 Py_INCREF(value);
1381 PUSH(value);
1382 FAST_DISPATCH();
1383 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 TARGET(STORE_FAST) {
1387 PyObject *value = POP();
1388 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001390 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001391
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001392 TARGET(POP_TOP) {
1393 PyObject *value = POP();
1394 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001398 TARGET(ROT_TWO) {
1399 PyObject *top = TOP();
1400 PyObject *second = SECOND();
1401 SET_TOP(second);
1402 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001404 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001406 TARGET(ROT_THREE) {
1407 PyObject *top = TOP();
1408 PyObject *second = SECOND();
1409 PyObject *third = THIRD();
1410 SET_TOP(second);
1411 SET_SECOND(third);
1412 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001416 TARGET(DUP_TOP) {
1417 PyObject *top = TOP();
1418 Py_INCREF(top);
1419 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001423 TARGET(DUP_TOP_TWO) {
1424 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001425 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001426 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001427 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001428 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001429 SET_TOP(top);
1430 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001431 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001432 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001433
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 TARGET(UNARY_POSITIVE) {
1435 PyObject *value = TOP();
1436 PyObject *res = PyNumber_Positive(value);
1437 Py_DECREF(value);
1438 SET_TOP(res);
1439 if (res == NULL)
1440 goto error;
1441 DISPATCH();
1442 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001444 TARGET(UNARY_NEGATIVE) {
1445 PyObject *value = TOP();
1446 PyObject *res = PyNumber_Negative(value);
1447 Py_DECREF(value);
1448 SET_TOP(res);
1449 if (res == NULL)
1450 goto error;
1451 DISPATCH();
1452 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001454 TARGET(UNARY_NOT) {
1455 PyObject *value = TOP();
1456 int err = PyObject_IsTrue(value);
1457 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001458 if (err == 0) {
1459 Py_INCREF(Py_True);
1460 SET_TOP(Py_True);
1461 DISPATCH();
1462 }
1463 else if (err > 0) {
1464 Py_INCREF(Py_False);
1465 SET_TOP(Py_False);
1466 err = 0;
1467 DISPATCH();
1468 }
1469 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001470 goto error;
1471 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001473 TARGET(UNARY_INVERT) {
1474 PyObject *value = TOP();
1475 PyObject *res = PyNumber_Invert(value);
1476 Py_DECREF(value);
1477 SET_TOP(res);
1478 if (res == NULL)
1479 goto error;
1480 DISPATCH();
1481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001483 TARGET(BINARY_POWER) {
1484 PyObject *exp = POP();
1485 PyObject *base = TOP();
1486 PyObject *res = PyNumber_Power(base, exp, Py_None);
1487 Py_DECREF(base);
1488 Py_DECREF(exp);
1489 SET_TOP(res);
1490 if (res == NULL)
1491 goto error;
1492 DISPATCH();
1493 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001494
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 TARGET(BINARY_MULTIPLY) {
1496 PyObject *right = POP();
1497 PyObject *left = TOP();
1498 PyObject *res = PyNumber_Multiply(left, right);
1499 Py_DECREF(left);
1500 Py_DECREF(right);
1501 SET_TOP(res);
1502 if (res == NULL)
1503 goto error;
1504 DISPATCH();
1505 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001507 TARGET(BINARY_TRUE_DIVIDE) {
1508 PyObject *divisor = POP();
1509 PyObject *dividend = TOP();
1510 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1511 Py_DECREF(dividend);
1512 Py_DECREF(divisor);
1513 SET_TOP(quotient);
1514 if (quotient == NULL)
1515 goto error;
1516 DISPATCH();
1517 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001519 TARGET(BINARY_FLOOR_DIVIDE) {
1520 PyObject *divisor = POP();
1521 PyObject *dividend = TOP();
1522 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1523 Py_DECREF(dividend);
1524 Py_DECREF(divisor);
1525 SET_TOP(quotient);
1526 if (quotient == NULL)
1527 goto error;
1528 DISPATCH();
1529 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001530
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001531 TARGET(BINARY_MODULO) {
1532 PyObject *divisor = POP();
1533 PyObject *dividend = TOP();
1534 PyObject *res = PyUnicode_CheckExact(dividend) ?
1535 PyUnicode_Format(dividend, divisor) :
1536 PyNumber_Remainder(dividend, divisor);
1537 Py_DECREF(divisor);
1538 Py_DECREF(dividend);
1539 SET_TOP(res);
1540 if (res == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001545 TARGET(BINARY_ADD) {
1546 PyObject *right = POP();
1547 PyObject *left = TOP();
1548 PyObject *sum;
1549 if (PyUnicode_CheckExact(left) &&
1550 PyUnicode_CheckExact(right)) {
1551 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001552 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001553 }
1554 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001555 sum = PyNumber_Add(left, right);
1556 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001557 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001558 Py_DECREF(right);
1559 SET_TOP(sum);
1560 if (sum == NULL)
1561 goto error;
1562 DISPATCH();
1563 }
1564
1565 TARGET(BINARY_SUBTRACT) {
1566 PyObject *right = POP();
1567 PyObject *left = TOP();
1568 PyObject *diff = PyNumber_Subtract(left, right);
1569 Py_DECREF(right);
1570 Py_DECREF(left);
1571 SET_TOP(diff);
1572 if (diff == NULL)
1573 goto error;
1574 DISPATCH();
1575 }
1576
1577 TARGET(BINARY_SUBSCR) {
1578 PyObject *sub = POP();
1579 PyObject *container = TOP();
1580 PyObject *res = PyObject_GetItem(container, sub);
1581 Py_DECREF(container);
1582 Py_DECREF(sub);
1583 SET_TOP(res);
1584 if (res == NULL)
1585 goto error;
1586 DISPATCH();
1587 }
1588
1589 TARGET(BINARY_LSHIFT) {
1590 PyObject *right = POP();
1591 PyObject *left = TOP();
1592 PyObject *res = PyNumber_Lshift(left, right);
1593 Py_DECREF(left);
1594 Py_DECREF(right);
1595 SET_TOP(res);
1596 if (res == NULL)
1597 goto error;
1598 DISPATCH();
1599 }
1600
1601 TARGET(BINARY_RSHIFT) {
1602 PyObject *right = POP();
1603 PyObject *left = TOP();
1604 PyObject *res = PyNumber_Rshift(left, right);
1605 Py_DECREF(left);
1606 Py_DECREF(right);
1607 SET_TOP(res);
1608 if (res == NULL)
1609 goto error;
1610 DISPATCH();
1611 }
1612
1613 TARGET(BINARY_AND) {
1614 PyObject *right = POP();
1615 PyObject *left = TOP();
1616 PyObject *res = PyNumber_And(left, right);
1617 Py_DECREF(left);
1618 Py_DECREF(right);
1619 SET_TOP(res);
1620 if (res == NULL)
1621 goto error;
1622 DISPATCH();
1623 }
1624
1625 TARGET(BINARY_XOR) {
1626 PyObject *right = POP();
1627 PyObject *left = TOP();
1628 PyObject *res = PyNumber_Xor(left, right);
1629 Py_DECREF(left);
1630 Py_DECREF(right);
1631 SET_TOP(res);
1632 if (res == NULL)
1633 goto error;
1634 DISPATCH();
1635 }
1636
1637 TARGET(BINARY_OR) {
1638 PyObject *right = POP();
1639 PyObject *left = TOP();
1640 PyObject *res = PyNumber_Or(left, right);
1641 Py_DECREF(left);
1642 Py_DECREF(right);
1643 SET_TOP(res);
1644 if (res == NULL)
1645 goto error;
1646 DISPATCH();
1647 }
1648
1649 TARGET(LIST_APPEND) {
1650 PyObject *v = POP();
1651 PyObject *list = PEEK(oparg);
1652 int err;
1653 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001655 if (err != 0)
1656 goto error;
1657 PREDICT(JUMP_ABSOLUTE);
1658 DISPATCH();
1659 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 TARGET(SET_ADD) {
1662 PyObject *v = POP();
1663 PyObject *set = stack_pointer[-oparg];
1664 int err;
1665 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001667 if (err != 0)
1668 goto error;
1669 PREDICT(JUMP_ABSOLUTE);
1670 DISPATCH();
1671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001673 TARGET(INPLACE_POWER) {
1674 PyObject *exp = POP();
1675 PyObject *base = TOP();
1676 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1677 Py_DECREF(base);
1678 Py_DECREF(exp);
1679 SET_TOP(res);
1680 if (res == NULL)
1681 goto error;
1682 DISPATCH();
1683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001684
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001685 TARGET(INPLACE_MULTIPLY) {
1686 PyObject *right = POP();
1687 PyObject *left = TOP();
1688 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1689 Py_DECREF(left);
1690 Py_DECREF(right);
1691 SET_TOP(res);
1692 if (res == NULL)
1693 goto error;
1694 DISPATCH();
1695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001697 TARGET(INPLACE_TRUE_DIVIDE) {
1698 PyObject *divisor = POP();
1699 PyObject *dividend = TOP();
1700 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1701 Py_DECREF(dividend);
1702 Py_DECREF(divisor);
1703 SET_TOP(quotient);
1704 if (quotient == NULL)
1705 goto error;
1706 DISPATCH();
1707 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001709 TARGET(INPLACE_FLOOR_DIVIDE) {
1710 PyObject *divisor = POP();
1711 PyObject *dividend = TOP();
1712 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1713 Py_DECREF(dividend);
1714 Py_DECREF(divisor);
1715 SET_TOP(quotient);
1716 if (quotient == NULL)
1717 goto error;
1718 DISPATCH();
1719 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001720
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001721 TARGET(INPLACE_MODULO) {
1722 PyObject *right = POP();
1723 PyObject *left = TOP();
1724 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1725 Py_DECREF(left);
1726 Py_DECREF(right);
1727 SET_TOP(mod);
1728 if (mod == NULL)
1729 goto error;
1730 DISPATCH();
1731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001733 TARGET(INPLACE_ADD) {
1734 PyObject *right = POP();
1735 PyObject *left = TOP();
1736 PyObject *sum;
1737 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1738 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001739 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001740 }
1741 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 sum = PyNumber_InPlaceAdd(left, right);
1743 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001744 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001745 Py_DECREF(right);
1746 SET_TOP(sum);
1747 if (sum == NULL)
1748 goto error;
1749 DISPATCH();
1750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001752 TARGET(INPLACE_SUBTRACT) {
1753 PyObject *right = POP();
1754 PyObject *left = TOP();
1755 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1756 Py_DECREF(left);
1757 Py_DECREF(right);
1758 SET_TOP(diff);
1759 if (diff == NULL)
1760 goto error;
1761 DISPATCH();
1762 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001763
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001764 TARGET(INPLACE_LSHIFT) {
1765 PyObject *right = POP();
1766 PyObject *left = TOP();
1767 PyObject *res = PyNumber_InPlaceLshift(left, right);
1768 Py_DECREF(left);
1769 Py_DECREF(right);
1770 SET_TOP(res);
1771 if (res == NULL)
1772 goto error;
1773 DISPATCH();
1774 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001775
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001776 TARGET(INPLACE_RSHIFT) {
1777 PyObject *right = POP();
1778 PyObject *left = TOP();
1779 PyObject *res = PyNumber_InPlaceRshift(left, right);
1780 Py_DECREF(left);
1781 Py_DECREF(right);
1782 SET_TOP(res);
1783 if (res == NULL)
1784 goto error;
1785 DISPATCH();
1786 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001788 TARGET(INPLACE_AND) {
1789 PyObject *right = POP();
1790 PyObject *left = TOP();
1791 PyObject *res = PyNumber_InPlaceAnd(left, right);
1792 Py_DECREF(left);
1793 Py_DECREF(right);
1794 SET_TOP(res);
1795 if (res == NULL)
1796 goto error;
1797 DISPATCH();
1798 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001799
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001800 TARGET(INPLACE_XOR) {
1801 PyObject *right = POP();
1802 PyObject *left = TOP();
1803 PyObject *res = PyNumber_InPlaceXor(left, right);
1804 Py_DECREF(left);
1805 Py_DECREF(right);
1806 SET_TOP(res);
1807 if (res == NULL)
1808 goto error;
1809 DISPATCH();
1810 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001812 TARGET(INPLACE_OR) {
1813 PyObject *right = POP();
1814 PyObject *left = TOP();
1815 PyObject *res = PyNumber_InPlaceOr(left, right);
1816 Py_DECREF(left);
1817 Py_DECREF(right);
1818 SET_TOP(res);
1819 if (res == NULL)
1820 goto error;
1821 DISPATCH();
1822 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001823
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001824 TARGET(STORE_SUBSCR) {
1825 PyObject *sub = TOP();
1826 PyObject *container = SECOND();
1827 PyObject *v = THIRD();
1828 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 STACKADJ(-3);
1830 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001833 Py_DECREF(container);
1834 Py_DECREF(sub);
1835 if (err != 0)
1836 goto error;
1837 DISPATCH();
1838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001839
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 TARGET(DELETE_SUBSCR) {
1841 PyObject *sub = TOP();
1842 PyObject *container = SECOND();
1843 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 STACKADJ(-2);
1845 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 err = PyObject_DelItem(container, sub);
1847 Py_DECREF(container);
1848 Py_DECREF(sub);
1849 if (err != 0)
1850 goto error;
1851 DISPATCH();
1852 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001853
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001854 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001855 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001856 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001857 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001858 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 PyErr_SetString(PyExc_RuntimeError,
1861 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001862 Py_DECREF(value);
1863 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001865 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001866 Py_DECREF(value);
1867 if (res == NULL)
1868 goto error;
1869 Py_DECREF(res);
1870 DISPATCH();
1871 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001872
Thomas Wouters434d0822000-08-24 20:11:32 +00001873#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001875#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 TARGET(RAISE_VARARGS) {
1877 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 switch (oparg) {
1879 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001880 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 if (do_raise(exc, cause)) {
1885 why = WHY_EXCEPTION;
1886 goto fast_block_end;
1887 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 break;
1889 default:
1890 PyErr_SetString(PyExc_SystemError,
1891 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 break;
1893 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001894 goto error;
1895 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001896
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001897 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 retval = POP();
1899 why = WHY_RETURN;
1900 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001902
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 TARGET(YIELD_FROM) {
1904 PyObject *v = POP();
1905 PyObject *reciever = TOP();
1906 int err;
1907 if (PyGen_CheckExact(reciever)) {
1908 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001909 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001910 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 if (v == Py_None)
1912 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001913 else
Benjamin Petersonf6e50b42014-04-13 23:52:01 -04001914 retval = _PyObject_CallMethodIdObjArgs(reciever, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001915 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001916 Py_DECREF(v);
1917 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001918 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001919 if (tstate->c_tracefunc != NULL
1920 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001921 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001922 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001923 if (err < 0)
1924 goto error;
1925 Py_DECREF(reciever);
1926 SET_TOP(val);
1927 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001928 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001929 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001930 f->f_stacktop = stack_pointer;
1931 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001932 /* and repeat... */
1933 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001934 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001936
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 retval = POP();
1939 f->f_stacktop = stack_pointer;
1940 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001942 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001943
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 TARGET(POP_EXCEPT) {
1945 PyTryBlock *b = PyFrame_BlockPop(f);
1946 if (b->b_type != EXCEPT_HANDLER) {
1947 PyErr_SetString(PyExc_SystemError,
1948 "popped block is not an except handler");
1949 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001951 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001954
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001955 TARGET(POP_BLOCK) {
1956 PyTryBlock *b = PyFrame_BlockPop(f);
1957 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001962 TARGET(END_FINALLY) {
1963 PyObject *status = POP();
1964 if (PyLong_Check(status)) {
1965 why = (enum why_code) PyLong_AS_LONG(status);
1966 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 if (why == WHY_RETURN ||
1968 why == WHY_CONTINUE)
1969 retval = POP();
1970 if (why == WHY_SILENCED) {
1971 /* An exception was silenced by 'with', we must
1972 manually unwind the EXCEPT_HANDLER block which was
1973 created when the exception was caught, otherwise
1974 the stack will be in an inconsistent state. */
1975 PyTryBlock *b = PyFrame_BlockPop(f);
1976 assert(b->b_type == EXCEPT_HANDLER);
1977 UNWIND_EXCEPT_HANDLER(b);
1978 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 Py_DECREF(status);
1980 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001982 Py_DECREF(status);
1983 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001984 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001985 else if (PyExceptionClass_Check(status)) {
1986 PyObject *exc = POP();
1987 PyObject *tb = POP();
1988 PyErr_Restore(status, exc, tb);
1989 why = WHY_EXCEPTION;
1990 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001992 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001993 PyErr_SetString(PyExc_SystemError,
1994 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 Py_DECREF(status);
1996 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 Py_DECREF(status);
1999 DISPATCH();
2000 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002001
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002002 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002003 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002004
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002006 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002007 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
2008 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002009 PyErr_SetString(PyExc_NameError,
2010 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002014 }
2015 else {
2016 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2017 if (build_class_str == NULL)
2018 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002019 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2020 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002021 if (PyErr_ExceptionMatches(PyExc_KeyError))
2022 PyErr_SetString(PyExc_NameError,
2023 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002025 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002028 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002029 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002031 TARGET(STORE_NAME) {
2032 PyObject *name = GETITEM(names, oparg);
2033 PyObject *v = POP();
2034 PyObject *ns = f->f_locals;
2035 int err;
2036 if (ns == NULL) {
2037 PyErr_Format(PyExc_SystemError,
2038 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002040 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002042 if (PyDict_CheckExact(ns))
2043 err = PyDict_SetItem(ns, name, v);
2044 else
2045 err = PyObject_SetItem(ns, name, v);
2046 Py_DECREF(v);
2047 if (err != 0)
2048 goto error;
2049 DISPATCH();
2050 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002052 TARGET(DELETE_NAME) {
2053 PyObject *name = GETITEM(names, oparg);
2054 PyObject *ns = f->f_locals;
2055 int err;
2056 if (ns == NULL) {
2057 PyErr_Format(PyExc_SystemError,
2058 "no locals when deleting %R", name);
2059 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002060 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002061 err = PyObject_DelItem(ns, name);
2062 if (err != 0) {
2063 format_exc_check_arg(PyExc_NameError,
2064 NAME_ERROR_MSG,
2065 name);
2066 goto error;
2067 }
2068 DISPATCH();
2069 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 TARGET(UNPACK_SEQUENCE) {
2073 PyObject *seq = POP(), *item, **items;
2074 if (PyTuple_CheckExact(seq) &&
2075 PyTuple_GET_SIZE(seq) == oparg) {
2076 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002078 item = items[oparg];
2079 Py_INCREF(item);
2080 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 } else if (PyList_CheckExact(seq) &&
2083 PyList_GET_SIZE(seq) == oparg) {
2084 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 item = items[oparg];
2087 Py_INCREF(item);
2088 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002090 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 stack_pointer + oparg)) {
2092 STACKADJ(oparg);
2093 } else {
2094 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 Py_DECREF(seq);
2096 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002098 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002099 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002100 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002101
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002102 TARGET(UNPACK_EX) {
2103 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2104 PyObject *seq = POP();
2105
2106 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2107 stack_pointer + totalargs)) {
2108 stack_pointer += totalargs;
2109 } else {
2110 Py_DECREF(seq);
2111 goto error;
2112 }
2113 Py_DECREF(seq);
2114 DISPATCH();
2115 }
2116
2117 TARGET(STORE_ATTR) {
2118 PyObject *name = GETITEM(names, oparg);
2119 PyObject *owner = TOP();
2120 PyObject *v = SECOND();
2121 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002122 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002123 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002124 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 Py_DECREF(owner);
2126 if (err != 0)
2127 goto error;
2128 DISPATCH();
2129 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002130
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002131 TARGET(DELETE_ATTR) {
2132 PyObject *name = GETITEM(names, oparg);
2133 PyObject *owner = POP();
2134 int err;
2135 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2136 Py_DECREF(owner);
2137 if (err != 0)
2138 goto error;
2139 DISPATCH();
2140 }
2141
2142 TARGET(STORE_GLOBAL) {
2143 PyObject *name = GETITEM(names, oparg);
2144 PyObject *v = POP();
2145 int err;
2146 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002147 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 if (err != 0)
2149 goto error;
2150 DISPATCH();
2151 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 TARGET(DELETE_GLOBAL) {
2154 PyObject *name = GETITEM(names, oparg);
2155 int err;
2156 err = PyDict_DelItem(f->f_globals, name);
2157 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002159 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002161 }
2162 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002164
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 TARGET(LOAD_NAME) {
2166 PyObject *name = GETITEM(names, oparg);
2167 PyObject *locals = f->f_locals;
2168 PyObject *v;
2169 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 "no locals when loading %R", name);
2172 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 if (PyDict_CheckExact(locals)) {
2175 v = PyDict_GetItem(locals, name);
2176 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 }
2178 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 v = PyObject_GetItem(locals, name);
Antoine Pitrou1cfa0ba2013-10-07 20:40:59 +02002180 if (v == NULL && _PyErr_OCCURRED()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002181 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2182 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 PyErr_Clear();
2184 }
2185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 if (v == NULL) {
2187 v = PyDict_GetItem(f->f_globals, name);
2188 Py_XINCREF(v);
2189 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002190 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 v = PyDict_GetItem(f->f_builtins, name);
2192 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002193 format_exc_check_arg(
2194 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 NAME_ERROR_MSG, name);
2196 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002197 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002199 }
2200 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 v = PyObject_GetItem(f->f_builtins, name);
2202 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002203 if (PyErr_ExceptionMatches(PyExc_KeyError))
2204 format_exc_check_arg(
2205 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002206 NAME_ERROR_MSG, name);
2207 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002208 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002209 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002210 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002212 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002216 TARGET(LOAD_GLOBAL) {
2217 PyObject *name = GETITEM(names, oparg);
2218 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002219 if (PyDict_CheckExact(f->f_globals)
2220 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002221 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002222 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 name);
2224 if (v == NULL) {
Antoine Pitrou59c900d2013-10-07 20:38:51 +02002225 if (!_PyErr_OCCURRED())
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002226 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002227 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002228 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002229 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002232 else {
2233 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 v = PyObject_GetItem(f->f_globals, name);
2235 if (v == NULL) {
2236 v = PyObject_GetItem(f->f_builtins, name);
2237 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002238 if (PyErr_ExceptionMatches(PyExc_KeyError))
2239 format_exc_check_arg(
2240 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002241 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002243 }
2244 }
2245 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002246 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002249
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002250 TARGET(DELETE_FAST) {
2251 PyObject *v = GETLOCAL(oparg);
2252 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 SETLOCAL(oparg, NULL);
2254 DISPATCH();
2255 }
2256 format_exc_check_arg(
2257 PyExc_UnboundLocalError,
2258 UNBOUNDLOCAL_ERROR_MSG,
2259 PyTuple_GetItem(co->co_varnames, oparg)
2260 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 goto error;
2262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002263
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002264 TARGET(DELETE_DEREF) {
2265 PyObject *cell = freevars[oparg];
2266 if (PyCell_GET(cell) != NULL) {
2267 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002268 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002269 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002270 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002271 goto error;
2272 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002273
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002274 TARGET(LOAD_CLOSURE) {
2275 PyObject *cell = freevars[oparg];
2276 Py_INCREF(cell);
2277 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002279 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002280
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002281 TARGET(LOAD_CLASSDEREF) {
2282 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002283 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002284 assert(locals);
2285 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2286 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2287 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2288 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2289 if (PyDict_CheckExact(locals)) {
2290 value = PyDict_GetItem(locals, name);
2291 Py_XINCREF(value);
2292 }
2293 else {
2294 value = PyObject_GetItem(locals, name);
2295 if (value == NULL && PyErr_Occurred()) {
2296 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2297 goto error;
2298 PyErr_Clear();
2299 }
2300 }
2301 if (!value) {
2302 PyObject *cell = freevars[oparg];
2303 value = PyCell_GET(cell);
2304 if (value == NULL) {
2305 format_exc_unbound(co, oparg);
2306 goto error;
2307 }
2308 Py_INCREF(value);
2309 }
2310 PUSH(value);
2311 DISPATCH();
2312 }
2313
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 TARGET(LOAD_DEREF) {
2315 PyObject *cell = freevars[oparg];
2316 PyObject *value = PyCell_GET(cell);
2317 if (value == NULL) {
2318 format_exc_unbound(co, oparg);
2319 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002320 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002321 Py_INCREF(value);
2322 PUSH(value);
2323 DISPATCH();
2324 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002325
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002326 TARGET(STORE_DEREF) {
2327 PyObject *v = POP();
2328 PyObject *cell = freevars[oparg];
2329 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 DISPATCH();
2332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002334 TARGET(BUILD_TUPLE) {
2335 PyObject *tup = PyTuple_New(oparg);
2336 if (tup == NULL)
2337 goto error;
2338 while (--oparg >= 0) {
2339 PyObject *item = POP();
2340 PyTuple_SET_ITEM(tup, oparg, item);
2341 }
2342 PUSH(tup);
2343 DISPATCH();
2344 }
2345
2346 TARGET(BUILD_LIST) {
2347 PyObject *list = PyList_New(oparg);
2348 if (list == NULL)
2349 goto error;
2350 while (--oparg >= 0) {
2351 PyObject *item = POP();
2352 PyList_SET_ITEM(list, oparg, item);
2353 }
2354 PUSH(list);
2355 DISPATCH();
2356 }
2357
2358 TARGET(BUILD_SET) {
2359 PyObject *set = PySet_New(NULL);
2360 int err = 0;
2361 if (set == NULL)
2362 goto error;
2363 while (--oparg >= 0) {
2364 PyObject *item = POP();
2365 if (err == 0)
2366 err = PySet_Add(set, item);
2367 Py_DECREF(item);
2368 }
2369 if (err != 0) {
2370 Py_DECREF(set);
2371 goto error;
2372 }
2373 PUSH(set);
2374 DISPATCH();
2375 }
2376
2377 TARGET(BUILD_MAP) {
2378 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2379 if (map == NULL)
2380 goto error;
2381 PUSH(map);
2382 DISPATCH();
2383 }
2384
2385 TARGET(STORE_MAP) {
2386 PyObject *key = TOP();
2387 PyObject *value = SECOND();
2388 PyObject *map = THIRD();
2389 int err;
2390 STACKADJ(-2);
2391 assert(PyDict_CheckExact(map));
2392 err = PyDict_SetItem(map, key, value);
2393 Py_DECREF(value);
2394 Py_DECREF(key);
2395 if (err != 0)
2396 goto error;
2397 DISPATCH();
2398 }
2399
2400 TARGET(MAP_ADD) {
2401 PyObject *key = TOP();
2402 PyObject *value = SECOND();
2403 PyObject *map;
2404 int err;
2405 STACKADJ(-2);
2406 map = stack_pointer[-oparg]; /* dict */
2407 assert(PyDict_CheckExact(map));
2408 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2409 Py_DECREF(value);
2410 Py_DECREF(key);
2411 if (err != 0)
2412 goto error;
2413 PREDICT(JUMP_ABSOLUTE);
2414 DISPATCH();
2415 }
2416
2417 TARGET(LOAD_ATTR) {
2418 PyObject *name = GETITEM(names, oparg);
2419 PyObject *owner = TOP();
2420 PyObject *res = PyObject_GetAttr(owner, name);
2421 Py_DECREF(owner);
2422 SET_TOP(res);
2423 if (res == NULL)
2424 goto error;
2425 DISPATCH();
2426 }
2427
2428 TARGET(COMPARE_OP) {
2429 PyObject *right = POP();
2430 PyObject *left = TOP();
2431 PyObject *res = cmp_outcome(oparg, left, right);
2432 Py_DECREF(left);
2433 Py_DECREF(right);
2434 SET_TOP(res);
2435 if (res == NULL)
2436 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002437 PREDICT(POP_JUMP_IF_FALSE);
2438 PREDICT(POP_JUMP_IF_TRUE);
2439 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002440 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002441
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002442 TARGET(IMPORT_NAME) {
2443 _Py_IDENTIFIER(__import__);
2444 PyObject *name = GETITEM(names, oparg);
2445 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002446 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002447 if (func == NULL) {
2448 PyErr_SetString(PyExc_ImportError,
2449 "__import__ not found");
2450 goto error;
2451 }
2452 Py_INCREF(func);
2453 from = POP();
2454 level = TOP();
2455 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2456 args = PyTuple_Pack(5,
2457 name,
2458 f->f_globals,
2459 f->f_locals == NULL ?
2460 Py_None : f->f_locals,
2461 from,
2462 level);
2463 else
2464 args = PyTuple_Pack(4,
2465 name,
2466 f->f_globals,
2467 f->f_locals == NULL ?
2468 Py_None : f->f_locals,
2469 from);
2470 Py_DECREF(level);
2471 Py_DECREF(from);
2472 if (args == NULL) {
2473 Py_DECREF(func);
2474 STACKADJ(-1);
2475 goto error;
2476 }
2477 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002478 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 READ_TIMESTAMP(intr1);
2480 Py_DECREF(args);
2481 Py_DECREF(func);
2482 SET_TOP(res);
2483 if (res == NULL)
2484 goto error;
2485 DISPATCH();
2486 }
2487
2488 TARGET(IMPORT_STAR) {
2489 PyObject *from = POP(), *locals;
2490 int err;
Victor Stinner41bb43a2013-10-29 01:19:37 +01002491 if (PyFrame_FastToLocalsWithError(f) < 0)
2492 goto error;
2493
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002494 locals = f->f_locals;
2495 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002496 PyErr_SetString(PyExc_SystemError,
2497 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002498 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002499 }
2500 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002501 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002502 READ_TIMESTAMP(intr1);
2503 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 Py_DECREF(from);
2505 if (err != 0)
2506 goto error;
2507 DISPATCH();
2508 }
Guido van Rossum25831651993-05-19 14:50:45 +00002509
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002510 TARGET(IMPORT_FROM) {
2511 PyObject *name = GETITEM(names, oparg);
2512 PyObject *from = TOP();
2513 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002515 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002517 PUSH(res);
2518 if (res == NULL)
2519 goto error;
2520 DISPATCH();
2521 }
Thomas Wouters52152252000-08-17 22:55:00 +00002522
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 JUMPBY(oparg);
2525 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002526 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002529 TARGET(POP_JUMP_IF_FALSE) {
2530 PyObject *cond = POP();
2531 int err;
2532 if (cond == Py_True) {
2533 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 FAST_DISPATCH();
2535 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 if (cond == Py_False) {
2537 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002538 JUMPTO(oparg);
2539 FAST_DISPATCH();
2540 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002541 err = PyObject_IsTrue(cond);
2542 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 if (err > 0)
2544 err = 0;
2545 else if (err == 0)
2546 JUMPTO(oparg);
2547 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002552 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002553 TARGET(POP_JUMP_IF_TRUE) {
2554 PyObject *cond = POP();
2555 int err;
2556 if (cond == Py_False) {
2557 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 FAST_DISPATCH();
2559 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 if (cond == Py_True) {
2561 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002562 JUMPTO(oparg);
2563 FAST_DISPATCH();
2564 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002565 err = PyObject_IsTrue(cond);
2566 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 if (err > 0) {
2568 err = 0;
2569 JUMPTO(oparg);
2570 }
2571 else if (err == 0)
2572 ;
2573 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002577
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002578 TARGET(JUMP_IF_FALSE_OR_POP) {
2579 PyObject *cond = TOP();
2580 int err;
2581 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002583 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 FAST_DISPATCH();
2585 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002586 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 JUMPTO(oparg);
2588 FAST_DISPATCH();
2589 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 if (err > 0) {
2592 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 err = 0;
2595 }
2596 else if (err == 0)
2597 JUMPTO(oparg);
2598 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002599 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002601 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002602
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002603 TARGET(JUMP_IF_TRUE_OR_POP) {
2604 PyObject *cond = TOP();
2605 int err;
2606 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 FAST_DISPATCH();
2610 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002611 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002612 JUMPTO(oparg);
2613 FAST_DISPATCH();
2614 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002616 if (err > 0) {
2617 err = 0;
2618 JUMPTO(oparg);
2619 }
2620 else if (err == 0) {
2621 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002622 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 }
2624 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002625 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002627 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002632#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002633 /* Enabling this path speeds-up all while and for-loops by bypassing
2634 the per-loop checks for signals. By default, this should be turned-off
2635 because it prevents detection of a control-break in tight loops like
2636 "while 1: pass". Compile with this option turned-on when you need
2637 the speed-up and do not need break checking inside tight loops (ones
2638 that contain only instructions ending with FAST_DISPATCH).
2639 */
2640 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002641#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002643#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002644 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002645
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 PyObject *iterable = TOP();
2649 PyObject *iter = PyObject_GetIter(iterable);
2650 Py_DECREF(iterable);
2651 SET_TOP(iter);
2652 if (iter == NULL)
2653 goto error;
2654 PREDICT(FOR_ITER);
2655 DISPATCH();
2656 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002658 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002661 PyObject *iter = TOP();
2662 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2663 if (next != NULL) {
2664 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 PREDICT(STORE_FAST);
2666 PREDICT(UNPACK_SEQUENCE);
2667 DISPATCH();
2668 }
2669 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002670 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2671 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002672 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002673 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 PyErr_Clear();
2675 }
2676 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002677 STACKADJ(-1);
2678 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 JUMPBY(oparg);
2680 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002681 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002682
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002683 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 why = WHY_BREAK;
2685 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002686 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002687
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 if (retval == NULL)
2691 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 why = WHY_CONTINUE;
2693 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002696 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2697 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2698 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 /* NOTE: If you add any new block-setup opcodes that
2701 are not try/except/finally handlers, you may need
2702 to update the PyGen_NeedsFinalizing() function.
2703 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2706 STACK_LEVEL());
2707 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002708 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002709
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002711 _Py_IDENTIFIER(__exit__);
2712 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002713 PyObject *mgr = TOP();
2714 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2715 PyObject *res;
2716 if (exit == NULL)
2717 goto error;
2718 SET_TOP(exit);
2719 enter = special_lookup(mgr, &PyId___enter__);
2720 Py_DECREF(mgr);
2721 if (enter == NULL)
2722 goto error;
2723 res = PyObject_CallFunctionObjArgs(enter, NULL);
2724 Py_DECREF(enter);
2725 if (res == NULL)
2726 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 /* Setup the finally block before pushing the result
2728 of __enter__ on the stack. */
2729 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2730 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002731
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 DISPATCH();
2734 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002735
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 TARGET(WITH_CLEANUP) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002737 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 how/why we entered the finally clause:
2739 - TOP = None
2740 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2741 - TOP = WHY_*; no retval below it
2742 - (TOP, SECOND, THIRD) = exc_info()
2743 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2744 Below them is EXIT, the context.__exit__ bound method.
2745 In the last case, we must call
2746 EXIT(TOP, SECOND, THIRD)
2747 otherwise we must call
2748 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002749
Benjamin Peterson8f169482013-10-29 22:25:06 -04002750 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002752 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002755 In addition, if the stack represents an exception,
2756 *and* the function call returns a 'true' value, we
2757 push WHY_SILENCED onto the stack. END_FINALLY will
2758 then not re-raise the exception. (But non-local
2759 gotos should still be resumed.)
2760 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2764 int err;
2765 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 (void)POP();
2767 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002770 else if (PyLong_Check(exc)) {
2771 STACKADJ(-1);
2772 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 case WHY_RETURN:
2774 case WHY_CONTINUE:
2775 /* Retval in TOP. */
2776 exit_func = SECOND();
2777 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002779 break;
2780 default:
2781 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 break;
2784 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 }
2787 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002789 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 val = SECOND();
2791 tb = THIRD();
2792 tp2 = FOURTH();
2793 exc2 = PEEK(5);
2794 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 SET_VALUE(7, tb2);
2797 SET_VALUE(6, exc2);
2798 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2800 SET_FOURTH(NULL);
2801 /* We just shifted the stack down, so we have
2802 to tell the except handler block that the
2803 values are lower than it expects. */
2804 block = &f->f_blockstack[f->f_iblock - 1];
2805 assert(block->b_type == EXCEPT_HANDLER);
2806 block->b_level--;
2807 }
2808 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002809 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 if (res == NULL)
2812 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002813
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002814 if (exc != Py_None)
2815 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 else
2817 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002818 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002822 else if (err > 0) {
2823 err = 0;
2824 /* There was an exception and a True return */
2825 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2826 }
2827 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002830
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 TARGET(CALL_FUNCTION) {
2832 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 PCALL(PCALL_ALL);
2834 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002835#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002837#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002839#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 PUSH(res);
2842 if (res == NULL)
2843 goto error;
2844 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002846
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2848 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2849 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002850 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 int na = oparg & 0xff;
2852 int nk = (oparg>>8) & 0xff;
2853 int flags = (opcode - CALL_FUNCTION) & 3;
2854 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 PCALL(PCALL_ALL);
2857 if (flags & CALL_FLAG_VAR)
2858 n++;
2859 if (flags & CALL_FLAG_KW)
2860 n++;
2861 pfunc = stack_pointer - n - 1;
2862 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002865 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 PyObject *self = PyMethod_GET_SELF(func);
2867 Py_INCREF(self);
2868 func = PyMethod_GET_FUNCTION(func);
2869 Py_INCREF(func);
2870 Py_DECREF(*pfunc);
2871 *pfunc = self;
2872 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002873 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 } else
2875 Py_INCREF(func);
2876 sp = stack_pointer;
2877 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 READ_TIMESTAMP(intr1);
2880 stack_pointer = sp;
2881 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002884 PyObject *o = POP();
2885 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 PUSH(res);
2888 if (res == NULL)
2889 goto error;
2890 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2894 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 int posdefaults = oparg & 0xff;
2897 int kwdefaults = (oparg>>8) & 0xff;
2898 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002899
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 PyObject *qualname = POP(); /* qualname */
2901 PyObject *code = POP(); /* code object */
2902 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2903 Py_DECREF(code);
2904 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002905
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002906 if (func == NULL)
2907 goto error;
2908
2909 if (opcode == MAKE_CLOSURE) {
2910 PyObject *closure = POP();
2911 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 Py_DECREF(func);
2914 Py_DECREF(closure);
2915 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002917 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002919
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002920 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 PyObject *names = POP(); /* names of args with annotations */
2923 PyObject *anns = PyDict_New();
2924 if (anns == NULL) {
2925 Py_DECREF(func);
2926 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 assert(num_annotations == name_ix+1);
2930 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 PyObject *name, *value;
2932 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002934 name = PyTuple_GET_ITEM(names, name_ix);
2935 value = POP();
2936 err = PyDict_SetItem(anns, name, value);
2937 Py_DECREF(value);
2938 if (err != 0) {
2939 Py_DECREF(anns);
2940 Py_DECREF(func);
2941 goto error;
2942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002944
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002945 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 /* Can't happen unless
2947 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 Py_DECREF(anns);
2949 Py_DECREF(func);
2950 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 Py_DECREF(anns);
2953 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002955
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 if (kwdefaults > 0) {
2958 PyObject *defs = PyDict_New();
2959 if (defs == NULL) {
2960 Py_DECREF(func);
2961 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962 }
2963 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002964 PyObject *v = POP(); /* default value */
2965 PyObject *key = POP(); /* kw only arg name */
2966 int err = PyDict_SetItem(defs, key, v);
2967 Py_DECREF(v);
2968 Py_DECREF(key);
2969 if (err != 0) {
2970 Py_DECREF(defs);
2971 Py_DECREF(func);
2972 goto error;
2973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002974 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976 /* Can't happen unless
2977 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002978 Py_DECREF(func);
2979 Py_DECREF(defs);
2980 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 }
Benjamin Peterson1ef876c2013-02-10 09:29:59 -05002984 if (posdefaults > 0) {
2985 PyObject *defs = PyTuple_New(posdefaults);
2986 if (defs == NULL) {
2987 Py_DECREF(func);
2988 goto error;
2989 }
2990 while (--posdefaults >= 0)
2991 PyTuple_SET_ITEM(defs, posdefaults, POP());
2992 if (PyFunction_SetDefaults(func, defs) != 0) {
2993 /* Can't happen unless
2994 PyFunction_SetDefaults changes. */
2995 Py_DECREF(defs);
2996 Py_DECREF(func);
2997 goto error;
2998 }
2999 Py_DECREF(defs);
3000 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 PUSH(func);
3002 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003004
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003005 TARGET(BUILD_SLICE) {
3006 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 step = NULL;
3011 stop = POP();
3012 start = TOP();
3013 slice = PySlice_New(start, stop, step);
3014 Py_DECREF(start);
3015 Py_DECREF(stop);
3016 Py_XDECREF(step);
3017 SET_TOP(slice);
3018 if (slice == NULL)
3019 goto error;
3020 DISPATCH();
3021 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003022
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 opcode = NEXTOP();
3025 oparg = oparg<<16 | NEXTARG();
3026 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003028
Antoine Pitrou042b1282010-08-13 21:15:58 +00003029#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003031#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 default:
3033 fprintf(stderr,
3034 "XXX lineno: %d, opcode: %d\n",
3035 PyFrame_GetLineNumber(f),
3036 opcode);
3037 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003039
3040#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003042#endif
3043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003045
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003046 /* This should never be reached. Every opcode should end with DISPATCH()
3047 or goto error. */
3048 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00003049
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003052
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 assert(why == WHY_NOT);
3054 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003055
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003057#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 if (!PyErr_Occurred())
3059 PyErr_SetString(PyExc_SystemError,
3060 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003061#else
3062 assert(PyErr_Occurred());
3063#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003064
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 /* Log traceback info. */
3066 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003067
Benjamin Peterson51f46162013-01-23 08:38:47 -05003068 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003069 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3070 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003071
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003072fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003073 assert(why != WHY_NOT);
3074
3075 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003076 while (why != WHY_NOT && f->f_iblock > 0) {
3077 /* Peek at the current block. */
3078 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 assert(why != WHY_YIELD);
3081 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3082 why = WHY_NOT;
3083 JUMPTO(PyLong_AS_LONG(retval));
3084 Py_DECREF(retval);
3085 break;
3086 }
3087 /* Now we have to pop the block. */
3088 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 if (b->b_type == EXCEPT_HANDLER) {
3091 UNWIND_EXCEPT_HANDLER(b);
3092 continue;
3093 }
3094 UNWIND_BLOCK(b);
3095 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3096 why = WHY_NOT;
3097 JUMPTO(b->b_handler);
3098 break;
3099 }
3100 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3101 || b->b_type == SETUP_FINALLY)) {
3102 PyObject *exc, *val, *tb;
3103 int handler = b->b_handler;
3104 /* Beware, this invalidates all b->b_* fields */
3105 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3106 PUSH(tstate->exc_traceback);
3107 PUSH(tstate->exc_value);
3108 if (tstate->exc_type != NULL) {
3109 PUSH(tstate->exc_type);
3110 }
3111 else {
3112 Py_INCREF(Py_None);
3113 PUSH(Py_None);
3114 }
3115 PyErr_Fetch(&exc, &val, &tb);
3116 /* Make the raw exception data
3117 available to the handler,
3118 so a program can emulate the
3119 Python main loop. */
3120 PyErr_NormalizeException(
3121 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003122 if (tb != NULL)
3123 PyException_SetTraceback(val, tb);
3124 else
3125 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 Py_INCREF(exc);
3127 tstate->exc_type = exc;
3128 Py_INCREF(val);
3129 tstate->exc_value = val;
3130 tstate->exc_traceback = tb;
3131 if (tb == NULL)
3132 tb = Py_None;
3133 Py_INCREF(tb);
3134 PUSH(tb);
3135 PUSH(val);
3136 PUSH(exc);
3137 why = WHY_NOT;
3138 JUMPTO(handler);
3139 break;
3140 }
3141 if (b->b_type == SETUP_FINALLY) {
3142 if (why & (WHY_RETURN | WHY_CONTINUE))
3143 PUSH(retval);
3144 PUSH(PyLong_FromLong((long)why));
3145 why = WHY_NOT;
3146 JUMPTO(b->b_handler);
3147 break;
3148 }
3149 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003151 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 if (why != WHY_NOT)
3154 break;
3155 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003156
Victor Stinnerace47d72013-07-18 01:41:08 +02003157 assert(!PyErr_Occurred());
3158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 assert(why != WHY_YIELD);
3162 /* Pop remaining stack entries. */
3163 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003164 PyObject *o = POP();
3165 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003168 if (why != WHY_RETURN)
3169 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003170
Victor Stinnerace47d72013-07-18 01:41:08 +02003171 assert((retval != NULL && !PyErr_Occurred())
3172 || (retval == NULL && PyErr_Occurred()));
3173
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003174fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003175 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3176 /* The purpose of this block is to put aside the generator's exception
3177 state and restore that of the calling frame. If the current
3178 exception state is from the caller, we clear the exception values
3179 on the generator frame, so they are not swapped back in latter. The
3180 origin of the current exception state is determined by checking for
3181 except handler blocks, which we must be in iff a new exception
3182 state came into existence in this frame. (An uncaught exception
3183 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3184 int i;
3185 for (i = 0; i < f->f_iblock; i++)
3186 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3187 break;
3188 if (i == f->f_iblock)
3189 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003190 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003191 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003192 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003193 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003196 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003197 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003198 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3199 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003201 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 why = WHY_EXCEPTION;
3203 }
3204 }
3205 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003206 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3207 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 PyTrace_RETURN, NULL);
3209 }
3210 }
3211 if (tstate->c_profilefunc) {
3212 if (why == WHY_EXCEPTION)
3213 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003214 tstate->c_profileobj,
3215 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003217 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3218 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003220 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003221 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 }
3223 }
3224 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003226 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003227exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003229 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003233}
3234
Benjamin Petersonb204a422011-06-05 22:04:07 -05003235static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003236format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3237{
3238 int err;
3239 Py_ssize_t len = PyList_GET_SIZE(names);
3240 PyObject *name_str, *comma, *tail, *tmp;
3241
3242 assert(PyList_CheckExact(names));
3243 assert(len >= 1);
3244 /* Deal with the joys of natural language. */
3245 switch (len) {
3246 case 1:
3247 name_str = PyList_GET_ITEM(names, 0);
3248 Py_INCREF(name_str);
3249 break;
3250 case 2:
3251 name_str = PyUnicode_FromFormat("%U and %U",
3252 PyList_GET_ITEM(names, len - 2),
3253 PyList_GET_ITEM(names, len - 1));
3254 break;
3255 default:
3256 tail = PyUnicode_FromFormat(", %U, and %U",
3257 PyList_GET_ITEM(names, len - 2),
3258 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003259 if (tail == NULL)
3260 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003261 /* Chop off the last two objects in the list. This shouldn't actually
3262 fail, but we can't be too careful. */
3263 err = PyList_SetSlice(names, len - 2, len, NULL);
3264 if (err == -1) {
3265 Py_DECREF(tail);
3266 return;
3267 }
3268 /* Stitch everything up into a nice comma-separated list. */
3269 comma = PyUnicode_FromString(", ");
3270 if (comma == NULL) {
3271 Py_DECREF(tail);
3272 return;
3273 }
3274 tmp = PyUnicode_Join(comma, names);
3275 Py_DECREF(comma);
3276 if (tmp == NULL) {
3277 Py_DECREF(tail);
3278 return;
3279 }
3280 name_str = PyUnicode_Concat(tmp, tail);
3281 Py_DECREF(tmp);
3282 Py_DECREF(tail);
3283 break;
3284 }
3285 if (name_str == NULL)
3286 return;
3287 PyErr_Format(PyExc_TypeError,
3288 "%U() missing %i required %s argument%s: %U",
3289 co->co_name,
3290 len,
3291 kind,
3292 len == 1 ? "" : "s",
3293 name_str);
3294 Py_DECREF(name_str);
3295}
3296
3297static void
3298missing_arguments(PyCodeObject *co, int missing, int defcount,
3299 PyObject **fastlocals)
3300{
3301 int i, j = 0;
3302 int start, end;
3303 int positional = defcount != -1;
3304 const char *kind = positional ? "positional" : "keyword-only";
3305 PyObject *missing_names;
3306
3307 /* Compute the names of the arguments that are missing. */
3308 missing_names = PyList_New(missing);
3309 if (missing_names == NULL)
3310 return;
3311 if (positional) {
3312 start = 0;
3313 end = co->co_argcount - defcount;
3314 }
3315 else {
3316 start = co->co_argcount;
3317 end = start + co->co_kwonlyargcount;
3318 }
3319 for (i = start; i < end; i++) {
3320 if (GETLOCAL(i) == NULL) {
3321 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3322 PyObject *name = PyObject_Repr(raw);
3323 if (name == NULL) {
3324 Py_DECREF(missing_names);
3325 return;
3326 }
3327 PyList_SET_ITEM(missing_names, j++, name);
3328 }
3329 }
3330 assert(j == missing);
3331 format_missing(kind, co, missing_names);
3332 Py_DECREF(missing_names);
3333}
3334
3335static void
3336too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003337{
3338 int plural;
3339 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003340 int i;
3341 PyObject *sig, *kwonly_sig;
3342
Benjamin Petersone109c702011-06-24 09:37:26 -05003343 assert((co->co_flags & CO_VARARGS) == 0);
3344 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003345 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003346 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003347 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003348 if (defcount) {
3349 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003350 plural = 1;
3351 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3352 }
3353 else {
3354 plural = co->co_argcount != 1;
3355 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3356 }
3357 if (sig == NULL)
3358 return;
3359 if (kwonly_given) {
3360 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3361 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3362 kwonly_given != 1 ? "s" : "");
3363 if (kwonly_sig == NULL) {
3364 Py_DECREF(sig);
3365 return;
3366 }
3367 }
3368 else {
3369 /* This will not fail. */
3370 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003371 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003372 }
3373 PyErr_Format(PyExc_TypeError,
3374 "%U() takes %U positional argument%s but %d%U %s given",
3375 co->co_name,
3376 sig,
3377 plural ? "s" : "",
3378 given,
3379 kwonly_sig,
3380 given == 1 && !kwonly_given ? "was" : "were");
3381 Py_DECREF(sig);
3382 Py_DECREF(kwonly_sig);
3383}
3384
Guido van Rossumc2e20742006-02-27 22:32:47 +00003385/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003386 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003387 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003388
Tim Peters6d6c1a32001-08-02 04:15:00 +00003389PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003390PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 PyObject **args, int argcount, PyObject **kws, int kwcount,
3392 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003393{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003394 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003395 PyFrameObject *f;
3396 PyObject *retval = NULL;
3397 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 PyThreadState *tstate = PyThreadState_GET();
3399 PyObject *x, *u;
3400 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003401 int i;
3402 int n = argcount;
3403 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 if (globals == NULL) {
3406 PyErr_SetString(PyExc_SystemError,
3407 "PyEval_EvalCodeEx: NULL globals");
3408 return NULL;
3409 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 assert(tstate != NULL);
3412 assert(globals != NULL);
3413 f = PyFrame_New(tstate, co, globals, locals);
3414 if (f == NULL)
3415 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 fastlocals = f->f_localsplus;
3418 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003419
Benjamin Petersonb204a422011-06-05 22:04:07 -05003420 /* Parse arguments. */
3421 if (co->co_flags & CO_VARKEYWORDS) {
3422 kwdict = PyDict_New();
3423 if (kwdict == NULL)
3424 goto fail;
3425 i = total_args;
3426 if (co->co_flags & CO_VARARGS)
3427 i++;
3428 SETLOCAL(i, kwdict);
3429 }
3430 if (argcount > co->co_argcount)
3431 n = co->co_argcount;
3432 for (i = 0; i < n; i++) {
3433 x = args[i];
3434 Py_INCREF(x);
3435 SETLOCAL(i, x);
3436 }
3437 if (co->co_flags & CO_VARARGS) {
3438 u = PyTuple_New(argcount - n);
3439 if (u == NULL)
3440 goto fail;
3441 SETLOCAL(total_args, u);
3442 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 x = args[i];
3444 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003445 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003447 }
3448 for (i = 0; i < kwcount; i++) {
3449 PyObject **co_varnames;
3450 PyObject *keyword = kws[2*i];
3451 PyObject *value = kws[2*i + 1];
3452 int j;
3453 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3454 PyErr_Format(PyExc_TypeError,
3455 "%U() keywords must be strings",
3456 co->co_name);
3457 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003458 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003459 /* Speed hack: do raw pointer compares. As names are
3460 normally interned this should almost always hit. */
3461 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3462 for (j = 0; j < total_args; j++) {
3463 PyObject *nm = co_varnames[j];
3464 if (nm == keyword)
3465 goto kw_found;
3466 }
3467 /* Slow fallback, just in case */
3468 for (j = 0; j < total_args; j++) {
3469 PyObject *nm = co_varnames[j];
3470 int cmp = PyObject_RichCompareBool(
3471 keyword, nm, Py_EQ);
3472 if (cmp > 0)
3473 goto kw_found;
3474 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003476 }
3477 if (j >= total_args && kwdict == NULL) {
3478 PyErr_Format(PyExc_TypeError,
3479 "%U() got an unexpected "
3480 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 co->co_name,
3482 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003483 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003485 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3486 goto fail;
3487 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003488 continue;
3489 kw_found:
3490 if (GETLOCAL(j) != NULL) {
3491 PyErr_Format(PyExc_TypeError,
3492 "%U() got multiple "
3493 "values for argument '%S'",
3494 co->co_name,
3495 keyword);
3496 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003498 Py_INCREF(value);
3499 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003501 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003502 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 goto fail;
3504 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003505 if (argcount < co->co_argcount) {
3506 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003507 int missing = 0;
3508 for (i = argcount; i < m; i++)
3509 if (GETLOCAL(i) == NULL)
3510 missing++;
3511 if (missing) {
3512 missing_arguments(co, missing, defcount, fastlocals);
3513 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003514 }
3515 if (n > m)
3516 i = n - m;
3517 else
3518 i = 0;
3519 for (; i < defcount; i++) {
3520 if (GETLOCAL(m+i) == NULL) {
3521 PyObject *def = defs[i];
3522 Py_INCREF(def);
3523 SETLOCAL(m+i, def);
3524 }
3525 }
3526 }
3527 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003528 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003529 for (i = co->co_argcount; i < total_args; i++) {
3530 PyObject *name;
3531 if (GETLOCAL(i) != NULL)
3532 continue;
3533 name = PyTuple_GET_ITEM(co->co_varnames, i);
3534 if (kwdefs != NULL) {
3535 PyObject *def = PyDict_GetItem(kwdefs, name);
3536 if (def) {
3537 Py_INCREF(def);
3538 SETLOCAL(i, def);
3539 continue;
3540 }
3541 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003542 missing++;
3543 }
3544 if (missing) {
3545 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003546 goto fail;
3547 }
3548 }
3549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003551 vars into frame. */
3552 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003554 int arg;
3555 /* Possibly account for the cell variable being an argument. */
3556 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003557 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003558 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003559 /* Clear the local copy. */
3560 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003561 }
3562 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003563 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003564 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003565 if (c == NULL)
3566 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003567 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003569 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3570 PyObject *o = PyTuple_GET_ITEM(closure, i);
3571 Py_INCREF(o);
3572 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 if (co->co_flags & CO_GENERATOR) {
3576 /* Don't need to keep the reference to f_back, it will be set
3577 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003578 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 /* Create a new generator that owns the ready to run frame
3583 * and return that as the value. */
3584 return PyGen_New(f);
3585 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003588
Thomas Woutersce272b62007-09-19 21:19:28 +00003589fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 /* decref'ing the frame can cause __del__ methods to get invoked,
3592 which can call back into Python. While we're done with the
3593 current Python frame (f), the associated C stack is still in use,
3594 so recursion_depth must be boosted for the duration.
3595 */
3596 assert(tstate != NULL);
3597 ++tstate->recursion_depth;
3598 Py_DECREF(f);
3599 --tstate->recursion_depth;
3600 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003601}
3602
3603
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003604static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003605special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003608 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003610 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 return NULL;
3612 }
3613 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003614}
3615
3616
Benjamin Peterson87880242011-07-03 16:48:31 -05003617/* These 3 functions deal with the exception state of generators. */
3618
3619static void
3620save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3621{
3622 PyObject *type, *value, *traceback;
3623 Py_XINCREF(tstate->exc_type);
3624 Py_XINCREF(tstate->exc_value);
3625 Py_XINCREF(tstate->exc_traceback);
3626 type = f->f_exc_type;
3627 value = f->f_exc_value;
3628 traceback = f->f_exc_traceback;
3629 f->f_exc_type = tstate->exc_type;
3630 f->f_exc_value = tstate->exc_value;
3631 f->f_exc_traceback = tstate->exc_traceback;
3632 Py_XDECREF(type);
3633 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003634 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003635}
3636
3637static void
3638swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3639{
3640 PyObject *tmp;
3641 tmp = tstate->exc_type;
3642 tstate->exc_type = f->f_exc_type;
3643 f->f_exc_type = tmp;
3644 tmp = tstate->exc_value;
3645 tstate->exc_value = f->f_exc_value;
3646 f->f_exc_value = tmp;
3647 tmp = tstate->exc_traceback;
3648 tstate->exc_traceback = f->f_exc_traceback;
3649 f->f_exc_traceback = tmp;
3650}
3651
3652static void
3653restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3654{
3655 PyObject *type, *value, *tb;
3656 type = tstate->exc_type;
3657 value = tstate->exc_value;
3658 tb = tstate->exc_traceback;
3659 tstate->exc_type = f->f_exc_type;
3660 tstate->exc_value = f->f_exc_value;
3661 tstate->exc_traceback = f->f_exc_traceback;
3662 f->f_exc_type = NULL;
3663 f->f_exc_value = NULL;
3664 f->f_exc_traceback = NULL;
3665 Py_XDECREF(type);
3666 Py_XDECREF(value);
3667 Py_XDECREF(tb);
3668}
3669
3670
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003671/* Logic for the raise statement (too complicated for inlining).
3672 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003673static int
Collin Winter828f04a2007-08-31 00:04:24 +00003674do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 if (exc == NULL) {
3679 /* Reraise */
3680 PyThreadState *tstate = PyThreadState_GET();
3681 PyObject *tb;
3682 type = tstate->exc_type;
3683 value = tstate->exc_value;
3684 tb = tstate->exc_traceback;
3685 if (type == Py_None) {
3686 PyErr_SetString(PyExc_RuntimeError,
3687 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003688 return 0;
3689 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 Py_XINCREF(type);
3691 Py_XINCREF(value);
3692 Py_XINCREF(tb);
3693 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003694 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 /* We support the following forms of raise:
3698 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003699 raise <instance>
3700 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003702 if (PyExceptionClass_Check(exc)) {
3703 type = exc;
3704 value = PyObject_CallObject(exc, NULL);
3705 if (value == NULL)
3706 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003707 if (!PyExceptionInstance_Check(value)) {
3708 PyErr_Format(PyExc_TypeError,
3709 "calling %R should have returned an instance of "
3710 "BaseException, not %R",
3711 type, Py_TYPE(value));
3712 goto raise_error;
3713 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 }
3715 else if (PyExceptionInstance_Check(exc)) {
3716 value = exc;
3717 type = PyExceptionInstance_Class(exc);
3718 Py_INCREF(type);
3719 }
3720 else {
3721 /* Not something you can raise. You get an exception
3722 anyway, just not what you specified :-) */
3723 Py_DECREF(exc);
3724 PyErr_SetString(PyExc_TypeError,
3725 "exceptions must derive from BaseException");
3726 goto raise_error;
3727 }
Collin Winter828f04a2007-08-31 00:04:24 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 if (cause) {
3730 PyObject *fixed_cause;
3731 if (PyExceptionClass_Check(cause)) {
3732 fixed_cause = PyObject_CallObject(cause, NULL);
3733 if (fixed_cause == NULL)
3734 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003735 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003737 else if (PyExceptionInstance_Check(cause)) {
3738 fixed_cause = cause;
3739 }
3740 else if (cause == Py_None) {
3741 Py_DECREF(cause);
3742 fixed_cause = NULL;
3743 }
3744 else {
3745 PyErr_SetString(PyExc_TypeError,
3746 "exception causes must derive from "
3747 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 goto raise_error;
3749 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003750 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003751 }
Collin Winter828f04a2007-08-31 00:04:24 +00003752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 PyErr_SetObject(type, value);
3754 /* PyErr_SetObject incref's its arguments */
3755 Py_XDECREF(value);
3756 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003757 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003758
3759raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 Py_XDECREF(value);
3761 Py_XDECREF(type);
3762 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003763 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003764}
3765
Tim Petersd6d010b2001-06-21 02:49:55 +00003766/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003767 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003768
Guido van Rossum0368b722007-05-11 16:50:42 +00003769 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3770 with a variable target.
3771*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003772
Barry Warsawe42b18f1997-08-25 22:13:04 +00003773static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003774unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003775{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 int i = 0, j = 0;
3777 Py_ssize_t ll = 0;
3778 PyObject *it; /* iter(v) */
3779 PyObject *w;
3780 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 it = PyObject_GetIter(v);
3785 if (it == NULL)
3786 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 for (; i < argcnt; i++) {
3789 w = PyIter_Next(it);
3790 if (w == NULL) {
3791 /* Iterator done, via error or exhaustion. */
3792 if (!PyErr_Occurred()) {
3793 PyErr_Format(PyExc_ValueError,
3794 "need more than %d value%s to unpack",
3795 i, i == 1 ? "" : "s");
3796 }
3797 goto Error;
3798 }
3799 *--sp = w;
3800 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 if (argcntafter == -1) {
3803 /* We better have exhausted the iterator now. */
3804 w = PyIter_Next(it);
3805 if (w == NULL) {
3806 if (PyErr_Occurred())
3807 goto Error;
3808 Py_DECREF(it);
3809 return 1;
3810 }
3811 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003812 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3813 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 goto Error;
3815 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 l = PySequence_List(it);
3818 if (l == NULL)
3819 goto Error;
3820 *--sp = l;
3821 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 ll = PyList_GET_SIZE(l);
3824 if (ll < argcntafter) {
3825 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3826 argcnt + ll);
3827 goto Error;
3828 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003830 /* Pop the "after-variable" args off the list. */
3831 for (j = argcntafter; j > 0; j--, i++) {
3832 *--sp = PyList_GET_ITEM(l, ll - j);
3833 }
3834 /* Resize the list. */
3835 Py_SIZE(l) = ll - argcntafter;
3836 Py_DECREF(it);
3837 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003838
Tim Petersd6d010b2001-06-21 02:49:55 +00003839Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003840 for (; i > 0; i--, sp++)
3841 Py_DECREF(*sp);
3842 Py_XDECREF(it);
3843 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003844}
3845
3846
Guido van Rossum96a42c81992-01-12 02:29:51 +00003847#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003848static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003849prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003850{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003851 printf("%s ", str);
3852 if (PyObject_Print(v, stdout, 0) != 0)
3853 PyErr_Clear(); /* Don't know what else to do */
3854 printf("\n");
3855 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003856}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003857#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003858
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003859static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003860call_exc_trace(Py_tracefunc func, PyObject *self,
3861 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003862{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003863 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01003865 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003866 if (value == NULL) {
3867 value = Py_None;
3868 Py_INCREF(value);
3869 }
Antoine Pitrou89335212013-11-23 14:05:23 +01003870 PyErr_NormalizeException(&type, &value, &orig_traceback);
3871 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 arg = PyTuple_Pack(3, type, value, traceback);
3873 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01003874 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 return;
3876 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003877 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 Py_DECREF(arg);
3879 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003880 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 else {
3882 Py_XDECREF(type);
3883 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003884 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003886}
3887
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003888static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003889call_trace_protected(Py_tracefunc func, PyObject *obj,
3890 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 PyObject *type, *value, *traceback;
3894 int err;
3895 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003896 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 if (err == 0)
3898 {
3899 PyErr_Restore(type, value, traceback);
3900 return 0;
3901 }
3902 else {
3903 Py_XDECREF(type);
3904 Py_XDECREF(value);
3905 Py_XDECREF(traceback);
3906 return -1;
3907 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003908}
3909
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003910static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003911call_trace(Py_tracefunc func, PyObject *obj,
3912 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003914{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 int result;
3916 if (tstate->tracing)
3917 return 0;
3918 tstate->tracing++;
3919 tstate->use_tracing = 0;
3920 result = func(obj, frame, what, arg);
3921 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3922 || (tstate->c_profilefunc != NULL));
3923 tstate->tracing--;
3924 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003925}
3926
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003927PyObject *
3928_PyEval_CallTracing(PyObject *func, PyObject *args)
3929{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003930 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 int save_tracing = tstate->tracing;
3932 int save_use_tracing = tstate->use_tracing;
3933 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003934
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003935 tstate->tracing = 0;
3936 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3937 || (tstate->c_profilefunc != NULL));
3938 result = PyObject_Call(func, args, NULL);
3939 tstate->tracing = save_tracing;
3940 tstate->use_tracing = save_use_tracing;
3941 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003942}
3943
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003944/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003945static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003946maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003947 PyThreadState *tstate, PyFrameObject *frame,
3948 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 int result = 0;
3951 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 /* If the last instruction executed isn't in the current
3954 instruction window, reset the window.
3955 */
3956 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3957 PyAddrPair bounds;
3958 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3959 &bounds);
3960 *instr_lb = bounds.ap_lower;
3961 *instr_ub = bounds.ap_upper;
3962 }
3963 /* If the last instruction falls at the start of a line or if
3964 it represents a jump backwards, update the frame's line
3965 number and call the trace function. */
3966 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3967 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003968 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 }
3970 *instr_prev = frame->f_lasti;
3971 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003972}
3973
Fred Drake5755ce62001-06-27 19:19:46 +00003974void
3975PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003977 PyThreadState *tstate = PyThreadState_GET();
3978 PyObject *temp = tstate->c_profileobj;
3979 Py_XINCREF(arg);
3980 tstate->c_profilefunc = NULL;
3981 tstate->c_profileobj = NULL;
3982 /* Must make sure that tracing is not ignored if 'temp' is freed */
3983 tstate->use_tracing = tstate->c_tracefunc != NULL;
3984 Py_XDECREF(temp);
3985 tstate->c_profilefunc = func;
3986 tstate->c_profileobj = arg;
3987 /* Flag that tracing or profiling is turned on */
3988 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003989}
3990
3991void
3992PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 PyThreadState *tstate = PyThreadState_GET();
3995 PyObject *temp = tstate->c_traceobj;
3996 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3997 Py_XINCREF(arg);
3998 tstate->c_tracefunc = NULL;
3999 tstate->c_traceobj = NULL;
4000 /* Must make sure that profiling is not ignored if 'temp' is freed */
4001 tstate->use_tracing = tstate->c_profilefunc != NULL;
4002 Py_XDECREF(temp);
4003 tstate->c_tracefunc = func;
4004 tstate->c_traceobj = arg;
4005 /* Flag that tracing or profiling is turned on */
4006 tstate->use_tracing = ((func != NULL)
4007 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004008}
4009
Guido van Rossumb209a111997-04-29 18:18:01 +00004010PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004011PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 PyFrameObject *current_frame = PyEval_GetFrame();
4014 if (current_frame == NULL)
4015 return PyThreadState_GET()->interp->builtins;
4016 else
4017 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004018}
4019
Guido van Rossumb209a111997-04-29 18:18:01 +00004020PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004021PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004022{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004024 if (current_frame == NULL) {
4025 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004027 }
4028
4029 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4030 return NULL;
4031
4032 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004034}
4035
Guido van Rossumb209a111997-04-29 18:18:01 +00004036PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004037PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004038{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004039 PyFrameObject *current_frame = PyEval_GetFrame();
4040 if (current_frame == NULL)
4041 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004042
4043 assert(current_frame->f_globals != NULL);
4044 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004045}
4046
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004047PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004048PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 PyThreadState *tstate = PyThreadState_GET();
4051 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004052}
4053
Guido van Rossum6135a871995-01-09 17:53:26 +00004054int
Tim Peters5ba58662001-07-16 02:29:45 +00004055PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004056{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 PyFrameObject *current_frame = PyEval_GetFrame();
4058 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 if (current_frame != NULL) {
4061 const int codeflags = current_frame->f_code->co_flags;
4062 const int compilerflags = codeflags & PyCF_MASK;
4063 if (compilerflags) {
4064 result = 1;
4065 cf->cf_flags |= compilerflags;
4066 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004067#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 if (codeflags & CO_GENERATOR_ALLOWED) {
4069 result = 1;
4070 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4071 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004072#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 }
4074 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004075}
4076
Guido van Rossum3f5da241990-12-20 15:06:42 +00004077
Guido van Rossum681d79a1995-07-18 14:51:37 +00004078/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004079 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004080
Guido van Rossumb209a111997-04-29 18:18:01 +00004081PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004082PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004083{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004085
Victor Stinnerace47d72013-07-18 01:41:08 +02004086#ifdef Py_DEBUG
4087 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4088 set, because it may clear it (directly or indirectly)
4089 and so the caller looses its exception */
4090 assert(!PyErr_Occurred());
4091#endif
4092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004093 if (arg == NULL) {
4094 arg = PyTuple_New(0);
4095 if (arg == NULL)
4096 return NULL;
4097 }
4098 else if (!PyTuple_Check(arg)) {
4099 PyErr_SetString(PyExc_TypeError,
4100 "argument list must be a tuple");
4101 return NULL;
4102 }
4103 else
4104 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 if (kw != NULL && !PyDict_Check(kw)) {
4107 PyErr_SetString(PyExc_TypeError,
4108 "keyword list must be a dictionary");
4109 Py_DECREF(arg);
4110 return NULL;
4111 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004112
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 result = PyObject_Call(func, arg, kw);
4114 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004115
4116 assert((result != NULL && !PyErr_Occurred())
4117 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004119}
4120
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004121const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004122PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 if (PyMethod_Check(func))
4125 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4126 else if (PyFunction_Check(func))
4127 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4128 else if (PyCFunction_Check(func))
4129 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4130 else
4131 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004132}
4133
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004134const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004135PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 if (PyMethod_Check(func))
4138 return "()";
4139 else if (PyFunction_Check(func))
4140 return "()";
4141 else if (PyCFunction_Check(func))
4142 return "()";
4143 else
4144 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004145}
4146
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004147static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004148err_args(PyObject *func, int flags, int nargs)
4149{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 if (flags & METH_NOARGS)
4151 PyErr_Format(PyExc_TypeError,
4152 "%.200s() takes no arguments (%d given)",
4153 ((PyCFunctionObject *)func)->m_ml->ml_name,
4154 nargs);
4155 else
4156 PyErr_Format(PyExc_TypeError,
4157 "%.200s() takes exactly one argument (%d given)",
4158 ((PyCFunctionObject *)func)->m_ml->ml_name,
4159 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004160}
4161
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004162#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004163if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004164 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4165 tstate, tstate->frame, \
4166 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 x = NULL; \
4168 } \
4169 else { \
4170 x = call; \
4171 if (tstate->c_profilefunc != NULL) { \
4172 if (x == NULL) { \
4173 call_trace_protected(tstate->c_profilefunc, \
4174 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004175 tstate, tstate->frame, \
4176 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 /* XXX should pass (type, value, tb) */ \
4178 } else { \
4179 if (call_trace(tstate->c_profilefunc, \
4180 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004181 tstate, tstate->frame, \
4182 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 Py_DECREF(x); \
4184 x = NULL; \
4185 } \
4186 } \
4187 } \
4188 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004189} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 x = call; \
4191 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004192
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004193static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004194call_function(PyObject ***pp_stack, int oparg
4195#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004197#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 int na = oparg & 0xff;
4201 int nk = (oparg>>8) & 0xff;
4202 int n = na + 2 * nk;
4203 PyObject **pfunc = (*pp_stack) - n - 1;
4204 PyObject *func = *pfunc;
4205 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004207 /* Always dispatch PyCFunction first, because these are
4208 presumed to be the most frequent callable object.
4209 */
4210 if (PyCFunction_Check(func) && nk == 0) {
4211 int flags = PyCFunction_GET_FLAGS(func);
4212 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 PCALL(PCALL_CFUNCTION);
4215 if (flags & (METH_NOARGS | METH_O)) {
4216 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4217 PyObject *self = PyCFunction_GET_SELF(func);
4218 if (flags & METH_NOARGS && na == 0) {
4219 C_TRACE(x, (*meth)(self,NULL));
4220 }
4221 else if (flags & METH_O && na == 1) {
4222 PyObject *arg = EXT_POP(*pp_stack);
4223 C_TRACE(x, (*meth)(self,arg));
4224 Py_DECREF(arg);
4225 }
4226 else {
4227 err_args(func, flags, na);
4228 x = NULL;
4229 }
4230 }
4231 else {
4232 PyObject *callargs;
4233 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004234 if (callargs != NULL) {
4235 READ_TIMESTAMP(*pintr0);
4236 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4237 READ_TIMESTAMP(*pintr1);
4238 Py_XDECREF(callargs);
4239 }
4240 else {
4241 x = NULL;
4242 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 }
4244 } else {
4245 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4246 /* optimize access to bound methods */
4247 PyObject *self = PyMethod_GET_SELF(func);
4248 PCALL(PCALL_METHOD);
4249 PCALL(PCALL_BOUND_METHOD);
4250 Py_INCREF(self);
4251 func = PyMethod_GET_FUNCTION(func);
4252 Py_INCREF(func);
4253 Py_DECREF(*pfunc);
4254 *pfunc = self;
4255 na++;
4256 n++;
4257 } else
4258 Py_INCREF(func);
4259 READ_TIMESTAMP(*pintr0);
4260 if (PyFunction_Check(func))
4261 x = fast_function(func, pp_stack, n, na, nk);
4262 else
4263 x = do_call(func, pp_stack, na, nk);
4264 READ_TIMESTAMP(*pintr1);
4265 Py_DECREF(func);
4266 }
Victor Stinnerf243ee42013-07-16 01:02:12 +02004267 assert((x != NULL && !PyErr_Occurred())
4268 || (x == NULL && PyErr_Occurred()));
Tim Peters8a5c3c72004-04-05 19:36:21 +00004269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 /* Clear the stack of the function object. Also removes
4271 the arguments in case they weren't consumed already
4272 (fast_function() and err_args() leave them on the stack).
4273 */
4274 while ((*pp_stack) > pfunc) {
4275 w = EXT_POP(*pp_stack);
4276 Py_DECREF(w);
4277 PCALL(PCALL_POP);
4278 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004279
4280 assert((x != NULL && !PyErr_Occurred())
4281 || (x == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004282 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004283}
4284
Jeremy Hylton192690e2002-08-16 18:36:11 +00004285/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004286 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004287 For the simplest case -- a function that takes only positional
4288 arguments and is called with only positional arguments -- it
4289 inlines the most primitive frame setup code from
4290 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4291 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004292*/
4293
4294static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004295fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4298 PyObject *globals = PyFunction_GET_GLOBALS(func);
4299 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4300 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4301 PyObject **d = NULL;
4302 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 PCALL(PCALL_FUNCTION);
4305 PCALL(PCALL_FAST_FUNCTION);
4306 if (argdefs == NULL && co->co_argcount == n &&
4307 co->co_kwonlyargcount == 0 && nk==0 &&
4308 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4309 PyFrameObject *f;
4310 PyObject *retval = NULL;
4311 PyThreadState *tstate = PyThreadState_GET();
4312 PyObject **fastlocals, **stack;
4313 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004314
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 PCALL(PCALL_FASTER_FUNCTION);
4316 assert(globals != NULL);
4317 /* XXX Perhaps we should create a specialized
4318 PyFrame_New() that doesn't take locals, but does
4319 take builtins without sanity checking them.
4320 */
4321 assert(tstate != NULL);
4322 f = PyFrame_New(tstate, co, globals, NULL);
4323 if (f == NULL)
4324 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 fastlocals = f->f_localsplus;
4327 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 for (i = 0; i < n; i++) {
4330 Py_INCREF(*stack);
4331 fastlocals[i] = *stack++;
4332 }
4333 retval = PyEval_EvalFrameEx(f,0);
4334 ++tstate->recursion_depth;
4335 Py_DECREF(f);
4336 --tstate->recursion_depth;
4337 return retval;
4338 }
4339 if (argdefs != NULL) {
4340 d = &PyTuple_GET_ITEM(argdefs, 0);
4341 nd = Py_SIZE(argdefs);
4342 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004343 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 (PyObject *)NULL, (*pp_stack)-n, na,
4345 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4346 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004347}
4348
4349static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004350update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4351 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 PyObject *kwdict = NULL;
4354 if (orig_kwdict == NULL)
4355 kwdict = PyDict_New();
4356 else {
4357 kwdict = PyDict_Copy(orig_kwdict);
4358 Py_DECREF(orig_kwdict);
4359 }
4360 if (kwdict == NULL)
4361 return NULL;
4362 while (--nk >= 0) {
4363 int err;
4364 PyObject *value = EXT_POP(*pp_stack);
4365 PyObject *key = EXT_POP(*pp_stack);
4366 if (PyDict_GetItem(kwdict, key) != NULL) {
4367 PyErr_Format(PyExc_TypeError,
4368 "%.200s%s got multiple values "
4369 "for keyword argument '%U'",
4370 PyEval_GetFuncName(func),
4371 PyEval_GetFuncDesc(func),
4372 key);
4373 Py_DECREF(key);
4374 Py_DECREF(value);
4375 Py_DECREF(kwdict);
4376 return NULL;
4377 }
4378 err = PyDict_SetItem(kwdict, key, value);
4379 Py_DECREF(key);
4380 Py_DECREF(value);
4381 if (err) {
4382 Py_DECREF(kwdict);
4383 return NULL;
4384 }
4385 }
4386 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004387}
4388
4389static PyObject *
4390update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004392{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 callargs = PyTuple_New(nstack + nstar);
4396 if (callargs == NULL) {
4397 return NULL;
4398 }
4399 if (nstar) {
4400 int i;
4401 for (i = 0; i < nstar; i++) {
4402 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4403 Py_INCREF(a);
4404 PyTuple_SET_ITEM(callargs, nstack + i, a);
4405 }
4406 }
4407 while (--nstack >= 0) {
4408 w = EXT_POP(*pp_stack);
4409 PyTuple_SET_ITEM(callargs, nstack, w);
4410 }
4411 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004412}
4413
4414static PyObject *
4415load_args(PyObject ***pp_stack, int na)
4416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004417 PyObject *args = PyTuple_New(na);
4418 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 if (args == NULL)
4421 return NULL;
4422 while (--na >= 0) {
4423 w = EXT_POP(*pp_stack);
4424 PyTuple_SET_ITEM(args, na, w);
4425 }
4426 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004427}
4428
4429static PyObject *
4430do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 PyObject *callargs = NULL;
4433 PyObject *kwdict = NULL;
4434 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 if (nk > 0) {
4437 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4438 if (kwdict == NULL)
4439 goto call_fail;
4440 }
4441 callargs = load_args(pp_stack, na);
4442 if (callargs == NULL)
4443 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004444#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004445 /* At this point, we have to look at the type of func to
4446 update the call stats properly. Do it here so as to avoid
4447 exposing the call stats machinery outside ceval.c
4448 */
4449 if (PyFunction_Check(func))
4450 PCALL(PCALL_FUNCTION);
4451 else if (PyMethod_Check(func))
4452 PCALL(PCALL_METHOD);
4453 else if (PyType_Check(func))
4454 PCALL(PCALL_TYPE);
4455 else if (PyCFunction_Check(func))
4456 PCALL(PCALL_CFUNCTION);
4457 else
4458 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004459#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 if (PyCFunction_Check(func)) {
4461 PyThreadState *tstate = PyThreadState_GET();
4462 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4463 }
4464 else
4465 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004466call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 Py_XDECREF(callargs);
4468 Py_XDECREF(kwdict);
4469 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004470}
4471
4472static PyObject *
4473ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4474{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 int nstar = 0;
4476 PyObject *callargs = NULL;
4477 PyObject *stararg = NULL;
4478 PyObject *kwdict = NULL;
4479 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 if (flags & CALL_FLAG_KW) {
4482 kwdict = EXT_POP(*pp_stack);
4483 if (!PyDict_Check(kwdict)) {
4484 PyObject *d;
4485 d = PyDict_New();
4486 if (d == NULL)
4487 goto ext_call_fail;
4488 if (PyDict_Update(d, kwdict) != 0) {
4489 Py_DECREF(d);
4490 /* PyDict_Update raises attribute
4491 * error (percolated from an attempt
4492 * to get 'keys' attribute) instead of
4493 * a type error if its second argument
4494 * is not a mapping.
4495 */
4496 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4497 PyErr_Format(PyExc_TypeError,
4498 "%.200s%.200s argument after ** "
4499 "must be a mapping, not %.200s",
4500 PyEval_GetFuncName(func),
4501 PyEval_GetFuncDesc(func),
4502 kwdict->ob_type->tp_name);
4503 }
4504 goto ext_call_fail;
4505 }
4506 Py_DECREF(kwdict);
4507 kwdict = d;
4508 }
4509 }
4510 if (flags & CALL_FLAG_VAR) {
4511 stararg = EXT_POP(*pp_stack);
4512 if (!PyTuple_Check(stararg)) {
4513 PyObject *t = NULL;
4514 t = PySequence_Tuple(stararg);
4515 if (t == NULL) {
4516 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4517 PyErr_Format(PyExc_TypeError,
4518 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004519 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 PyEval_GetFuncName(func),
4521 PyEval_GetFuncDesc(func),
4522 stararg->ob_type->tp_name);
4523 }
4524 goto ext_call_fail;
4525 }
4526 Py_DECREF(stararg);
4527 stararg = t;
4528 }
4529 nstar = PyTuple_GET_SIZE(stararg);
4530 }
4531 if (nk > 0) {
4532 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4533 if (kwdict == NULL)
4534 goto ext_call_fail;
4535 }
4536 callargs = update_star_args(na, nstar, stararg, pp_stack);
4537 if (callargs == NULL)
4538 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004539#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 /* At this point, we have to look at the type of func to
4541 update the call stats properly. Do it here so as to avoid
4542 exposing the call stats machinery outside ceval.c
4543 */
4544 if (PyFunction_Check(func))
4545 PCALL(PCALL_FUNCTION);
4546 else if (PyMethod_Check(func))
4547 PCALL(PCALL_METHOD);
4548 else if (PyType_Check(func))
4549 PCALL(PCALL_TYPE);
4550 else if (PyCFunction_Check(func))
4551 PCALL(PCALL_CFUNCTION);
4552 else
4553 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004554#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004555 if (PyCFunction_Check(func)) {
4556 PyThreadState *tstate = PyThreadState_GET();
4557 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4558 }
4559 else
4560 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004561ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 Py_XDECREF(callargs);
4563 Py_XDECREF(kwdict);
4564 Py_XDECREF(stararg);
Victor Stinnerf243ee42013-07-16 01:02:12 +02004565 assert((result != NULL && !PyErr_Occurred())
4566 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004567 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004568}
4569
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004570/* Extract a slice index from a PyInt or PyLong or an object with the
4571 nb_index slot defined, and store in *pi.
4572 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4573 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 +00004574 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004575*/
Tim Petersb5196382001-12-16 19:44:20 +00004576/* Note: If v is NULL, return success without storing into *pi. This
4577 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4578 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004579*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004580int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004581_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004583 if (v != NULL) {
4584 Py_ssize_t x;
4585 if (PyIndex_Check(v)) {
4586 x = PyNumber_AsSsize_t(v, NULL);
4587 if (x == -1 && PyErr_Occurred())
4588 return 0;
4589 }
4590 else {
4591 PyErr_SetString(PyExc_TypeError,
4592 "slice indices must be integers or "
4593 "None or have an __index__ method");
4594 return 0;
4595 }
4596 *pi = x;
4597 }
4598 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004599}
4600
Guido van Rossum486364b2007-06-30 05:01:58 +00004601#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004603
Guido van Rossumb209a111997-04-29 18:18:01 +00004604static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004605cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 int res = 0;
4608 switch (op) {
4609 case PyCmp_IS:
4610 res = (v == w);
4611 break;
4612 case PyCmp_IS_NOT:
4613 res = (v != w);
4614 break;
4615 case PyCmp_IN:
4616 res = PySequence_Contains(w, v);
4617 if (res < 0)
4618 return NULL;
4619 break;
4620 case PyCmp_NOT_IN:
4621 res = PySequence_Contains(w, v);
4622 if (res < 0)
4623 return NULL;
4624 res = !res;
4625 break;
4626 case PyCmp_EXC_MATCH:
4627 if (PyTuple_Check(w)) {
4628 Py_ssize_t i, length;
4629 length = PyTuple_Size(w);
4630 for (i = 0; i < length; i += 1) {
4631 PyObject *exc = PyTuple_GET_ITEM(w, i);
4632 if (!PyExceptionClass_Check(exc)) {
4633 PyErr_SetString(PyExc_TypeError,
4634 CANNOT_CATCH_MSG);
4635 return NULL;
4636 }
4637 }
4638 }
4639 else {
4640 if (!PyExceptionClass_Check(w)) {
4641 PyErr_SetString(PyExc_TypeError,
4642 CANNOT_CATCH_MSG);
4643 return NULL;
4644 }
4645 }
4646 res = PyErr_GivenExceptionMatches(v, w);
4647 break;
4648 default:
4649 return PyObject_RichCompare(v, w, op);
4650 }
4651 v = res ? Py_True : Py_False;
4652 Py_INCREF(v);
4653 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004654}
4655
Thomas Wouters52152252000-08-17 22:55:00 +00004656static PyObject *
4657import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004659 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 x = PyObject_GetAttr(v, name);
4662 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Brett Cannona79e4fb2013-07-12 11:22:26 -04004663 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004664 }
4665 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004666}
Guido van Rossumac7be682001-01-17 15:42:30 +00004667
Thomas Wouters52152252000-08-17 22:55:00 +00004668static int
4669import_all_from(PyObject *locals, PyObject *v)
4670{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004671 _Py_IDENTIFIER(__all__);
4672 _Py_IDENTIFIER(__dict__);
4673 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 PyObject *dict, *name, *value;
4675 int skip_leading_underscores = 0;
4676 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004677
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004678 if (all == NULL) {
4679 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4680 return -1; /* Unexpected error */
4681 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004682 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 if (dict == NULL) {
4684 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4685 return -1;
4686 PyErr_SetString(PyExc_ImportError,
4687 "from-import-* object has no __dict__ and no __all__");
4688 return -1;
4689 }
4690 all = PyMapping_Keys(dict);
4691 Py_DECREF(dict);
4692 if (all == NULL)
4693 return -1;
4694 skip_leading_underscores = 1;
4695 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 for (pos = 0, err = 0; ; pos++) {
4698 name = PySequence_GetItem(all, pos);
4699 if (name == NULL) {
4700 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4701 err = -1;
4702 else
4703 PyErr_Clear();
4704 break;
4705 }
4706 if (skip_leading_underscores &&
4707 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004708 PyUnicode_READY(name) != -1 &&
4709 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004710 {
4711 Py_DECREF(name);
4712 continue;
4713 }
4714 value = PyObject_GetAttr(v, name);
4715 if (value == NULL)
4716 err = -1;
4717 else if (PyDict_CheckExact(locals))
4718 err = PyDict_SetItem(locals, name, value);
4719 else
4720 err = PyObject_SetItem(locals, name, value);
4721 Py_DECREF(name);
4722 Py_XDECREF(value);
4723 if (err != 0)
4724 break;
4725 }
4726 Py_DECREF(all);
4727 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004728}
4729
Guido van Rossumac7be682001-01-17 15:42:30 +00004730static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004731format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004735 if (!obj)
4736 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004738 obj_str = _PyUnicode_AsString(obj);
4739 if (!obj_str)
4740 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004743}
Guido van Rossum950361c1997-01-24 13:49:28 +00004744
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004745static void
4746format_exc_unbound(PyCodeObject *co, int oparg)
4747{
4748 PyObject *name;
4749 /* Don't stomp existing exception */
4750 if (PyErr_Occurred())
4751 return;
4752 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4753 name = PyTuple_GET_ITEM(co->co_cellvars,
4754 oparg);
4755 format_exc_check_arg(
4756 PyExc_UnboundLocalError,
4757 UNBOUNDLOCAL_ERROR_MSG,
4758 name);
4759 } else {
4760 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4761 PyTuple_GET_SIZE(co->co_cellvars));
4762 format_exc_check_arg(PyExc_NameError,
4763 UNBOUNDFREE_ERROR_MSG, name);
4764 }
4765}
4766
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004767static PyObject *
4768unicode_concatenate(PyObject *v, PyObject *w,
4769 PyFrameObject *f, unsigned char *next_instr)
4770{
4771 PyObject *res;
4772 if (Py_REFCNT(v) == 2) {
4773 /* In the common case, there are 2 references to the value
4774 * stored in 'variable' when the += is performed: one on the
4775 * value stack (in 'v') and one still stored in the
4776 * 'variable'. We try to delete the variable now to reduce
4777 * the refcnt to 1.
4778 */
4779 switch (*next_instr) {
4780 case STORE_FAST:
4781 {
4782 int oparg = PEEKARG();
4783 PyObject **fastlocals = f->f_localsplus;
4784 if (GETLOCAL(oparg) == v)
4785 SETLOCAL(oparg, NULL);
4786 break;
4787 }
4788 case STORE_DEREF:
4789 {
4790 PyObject **freevars = (f->f_localsplus +
4791 f->f_code->co_nlocals);
4792 PyObject *c = freevars[PEEKARG()];
4793 if (PyCell_GET(c) == v)
4794 PyCell_Set(c, NULL);
4795 break;
4796 }
4797 case STORE_NAME:
4798 {
4799 PyObject *names = f->f_code->co_names;
4800 PyObject *name = GETITEM(names, PEEKARG());
4801 PyObject *locals = f->f_locals;
4802 if (PyDict_CheckExact(locals) &&
4803 PyDict_GetItem(locals, name) == v) {
4804 if (PyDict_DelItem(locals, name) != 0) {
4805 PyErr_Clear();
4806 }
4807 }
4808 break;
4809 }
4810 }
4811 }
4812 res = v;
4813 PyUnicode_Append(&res, w);
4814 return res;
4815}
4816
Guido van Rossum950361c1997-01-24 13:49:28 +00004817#ifdef DYNAMIC_EXECUTION_PROFILE
4818
Skip Montanarof118cb12001-10-15 20:51:38 +00004819static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004820getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004822 int i;
4823 PyObject *l = PyList_New(256);
4824 if (l == NULL) return NULL;
4825 for (i = 0; i < 256; i++) {
4826 PyObject *x = PyLong_FromLong(a[i]);
4827 if (x == NULL) {
4828 Py_DECREF(l);
4829 return NULL;
4830 }
4831 PyList_SetItem(l, i, x);
4832 }
4833 for (i = 0; i < 256; i++)
4834 a[i] = 0;
4835 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004836}
4837
4838PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004839_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004840{
4841#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004843#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004844 int i;
4845 PyObject *l = PyList_New(257);
4846 if (l == NULL) return NULL;
4847 for (i = 0; i < 257; i++) {
4848 PyObject *x = getarray(dxpairs[i]);
4849 if (x == NULL) {
4850 Py_DECREF(l);
4851 return NULL;
4852 }
4853 PyList_SetItem(l, i, x);
4854 }
4855 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004856#endif
4857}
4858
4859#endif