blob: 7a0cb7f04abdc301fc0afbf1924331df73705c1f [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
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300713_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000714{
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
Victor Stinner26f7b8a2015-01-31 10:29:47 +01001192 if (co->co_flags & CO_GENERATOR) {
1193 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 /* 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
Martin Panter9955a372015-10-07 10:26:23 +00001215 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02001216 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:
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003175 if (co->co_flags & CO_GENERATOR) {
3176
Benjamin Petersonac913412011-07-03 16:25:11 -05003177 /* The purpose of this block is to put aside the generator's exception
3178 state and restore that of the calling frame. If the current
3179 exception state is from the caller, we clear the exception values
3180 on the generator frame, so they are not swapped back in latter. The
3181 origin of the current exception state is determined by checking for
3182 except handler blocks, which we must be in iff a new exception
3183 state came into existence in this frame. (An uncaught exception
3184 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3185 int i;
3186 for (i = 0; i < f->f_iblock; i++)
3187 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3188 break;
3189 if (i == f->f_iblock)
3190 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003191 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003192 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003193 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003194 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003197 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003199 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3200 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003202 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003203 why = WHY_EXCEPTION;
3204 }
3205 }
3206 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003207 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3208 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003209 PyTrace_RETURN, NULL);
3210 }
3211 }
3212 if (tstate->c_profilefunc) {
3213 if (why == WHY_EXCEPTION)
3214 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003215 tstate->c_profileobj,
3216 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003218 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3219 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003220 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003221 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003222 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 }
3224 }
3225 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003228exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003230 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003234}
3235
Benjamin Petersonb204a422011-06-05 22:04:07 -05003236static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003237format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3238{
3239 int err;
3240 Py_ssize_t len = PyList_GET_SIZE(names);
3241 PyObject *name_str, *comma, *tail, *tmp;
3242
3243 assert(PyList_CheckExact(names));
3244 assert(len >= 1);
3245 /* Deal with the joys of natural language. */
3246 switch (len) {
3247 case 1:
3248 name_str = PyList_GET_ITEM(names, 0);
3249 Py_INCREF(name_str);
3250 break;
3251 case 2:
3252 name_str = PyUnicode_FromFormat("%U and %U",
3253 PyList_GET_ITEM(names, len - 2),
3254 PyList_GET_ITEM(names, len - 1));
3255 break;
3256 default:
3257 tail = PyUnicode_FromFormat(", %U, and %U",
3258 PyList_GET_ITEM(names, len - 2),
3259 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003260 if (tail == NULL)
3261 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003262 /* Chop off the last two objects in the list. This shouldn't actually
3263 fail, but we can't be too careful. */
3264 err = PyList_SetSlice(names, len - 2, len, NULL);
3265 if (err == -1) {
3266 Py_DECREF(tail);
3267 return;
3268 }
3269 /* Stitch everything up into a nice comma-separated list. */
3270 comma = PyUnicode_FromString(", ");
3271 if (comma == NULL) {
3272 Py_DECREF(tail);
3273 return;
3274 }
3275 tmp = PyUnicode_Join(comma, names);
3276 Py_DECREF(comma);
3277 if (tmp == NULL) {
3278 Py_DECREF(tail);
3279 return;
3280 }
3281 name_str = PyUnicode_Concat(tmp, tail);
3282 Py_DECREF(tmp);
3283 Py_DECREF(tail);
3284 break;
3285 }
3286 if (name_str == NULL)
3287 return;
3288 PyErr_Format(PyExc_TypeError,
3289 "%U() missing %i required %s argument%s: %U",
3290 co->co_name,
3291 len,
3292 kind,
3293 len == 1 ? "" : "s",
3294 name_str);
3295 Py_DECREF(name_str);
3296}
3297
3298static void
3299missing_arguments(PyCodeObject *co, int missing, int defcount,
3300 PyObject **fastlocals)
3301{
3302 int i, j = 0;
3303 int start, end;
3304 int positional = defcount != -1;
3305 const char *kind = positional ? "positional" : "keyword-only";
3306 PyObject *missing_names;
3307
3308 /* Compute the names of the arguments that are missing. */
3309 missing_names = PyList_New(missing);
3310 if (missing_names == NULL)
3311 return;
3312 if (positional) {
3313 start = 0;
3314 end = co->co_argcount - defcount;
3315 }
3316 else {
3317 start = co->co_argcount;
3318 end = start + co->co_kwonlyargcount;
3319 }
3320 for (i = start; i < end; i++) {
3321 if (GETLOCAL(i) == NULL) {
3322 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3323 PyObject *name = PyObject_Repr(raw);
3324 if (name == NULL) {
3325 Py_DECREF(missing_names);
3326 return;
3327 }
3328 PyList_SET_ITEM(missing_names, j++, name);
3329 }
3330 }
3331 assert(j == missing);
3332 format_missing(kind, co, missing_names);
3333 Py_DECREF(missing_names);
3334}
3335
3336static void
3337too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003338{
3339 int plural;
3340 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003341 int i;
3342 PyObject *sig, *kwonly_sig;
3343
Benjamin Petersone109c702011-06-24 09:37:26 -05003344 assert((co->co_flags & CO_VARARGS) == 0);
3345 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003346 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003347 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003348 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003349 if (defcount) {
3350 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003351 plural = 1;
3352 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3353 }
3354 else {
3355 plural = co->co_argcount != 1;
3356 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3357 }
3358 if (sig == NULL)
3359 return;
3360 if (kwonly_given) {
3361 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3362 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3363 kwonly_given != 1 ? "s" : "");
3364 if (kwonly_sig == NULL) {
3365 Py_DECREF(sig);
3366 return;
3367 }
3368 }
3369 else {
3370 /* This will not fail. */
3371 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003372 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003373 }
3374 PyErr_Format(PyExc_TypeError,
3375 "%U() takes %U positional argument%s but %d%U %s given",
3376 co->co_name,
3377 sig,
3378 plural ? "s" : "",
3379 given,
3380 kwonly_sig,
3381 given == 1 && !kwonly_given ? "was" : "were");
3382 Py_DECREF(sig);
3383 Py_DECREF(kwonly_sig);
3384}
3385
Guido van Rossumc2e20742006-02-27 22:32:47 +00003386/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003387 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003388 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003389
Tim Peters6d6c1a32001-08-02 04:15:00 +00003390PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003391PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 PyObject **args, int argcount, PyObject **kws, int kwcount,
3393 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003394{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003395 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003396 PyFrameObject *f;
3397 PyObject *retval = NULL;
3398 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 PyThreadState *tstate = PyThreadState_GET();
3400 PyObject *x, *u;
3401 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003402 int i;
3403 int n = argcount;
3404 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 if (globals == NULL) {
3407 PyErr_SetString(PyExc_SystemError,
3408 "PyEval_EvalCodeEx: NULL globals");
3409 return NULL;
3410 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 assert(tstate != NULL);
3413 assert(globals != NULL);
3414 f = PyFrame_New(tstate, co, globals, locals);
3415 if (f == NULL)
3416 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 fastlocals = f->f_localsplus;
3419 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003420
Benjamin Petersonb204a422011-06-05 22:04:07 -05003421 /* Parse arguments. */
3422 if (co->co_flags & CO_VARKEYWORDS) {
3423 kwdict = PyDict_New();
3424 if (kwdict == NULL)
3425 goto fail;
3426 i = total_args;
3427 if (co->co_flags & CO_VARARGS)
3428 i++;
3429 SETLOCAL(i, kwdict);
3430 }
3431 if (argcount > co->co_argcount)
3432 n = co->co_argcount;
3433 for (i = 0; i < n; i++) {
3434 x = args[i];
3435 Py_INCREF(x);
3436 SETLOCAL(i, x);
3437 }
3438 if (co->co_flags & CO_VARARGS) {
3439 u = PyTuple_New(argcount - n);
3440 if (u == NULL)
3441 goto fail;
3442 SETLOCAL(total_args, u);
3443 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 x = args[i];
3445 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003446 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003448 }
3449 for (i = 0; i < kwcount; i++) {
3450 PyObject **co_varnames;
3451 PyObject *keyword = kws[2*i];
3452 PyObject *value = kws[2*i + 1];
3453 int j;
3454 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3455 PyErr_Format(PyExc_TypeError,
3456 "%U() keywords must be strings",
3457 co->co_name);
3458 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003460 /* Speed hack: do raw pointer compares. As names are
3461 normally interned this should almost always hit. */
3462 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3463 for (j = 0; j < total_args; j++) {
3464 PyObject *nm = co_varnames[j];
3465 if (nm == keyword)
3466 goto kw_found;
3467 }
3468 /* Slow fallback, just in case */
3469 for (j = 0; j < total_args; j++) {
3470 PyObject *nm = co_varnames[j];
3471 int cmp = PyObject_RichCompareBool(
3472 keyword, nm, Py_EQ);
3473 if (cmp > 0)
3474 goto kw_found;
3475 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003476 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003477 }
3478 if (j >= total_args && kwdict == NULL) {
3479 PyErr_Format(PyExc_TypeError,
3480 "%U() got an unexpected "
3481 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 co->co_name,
3483 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003484 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 }
Christian Heimes0bd447f2013-07-20 14:48:10 +02003486 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3487 goto fail;
3488 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003489 continue;
3490 kw_found:
3491 if (GETLOCAL(j) != NULL) {
3492 PyErr_Format(PyExc_TypeError,
3493 "%U() got multiple "
3494 "values for argument '%S'",
3495 co->co_name,
3496 keyword);
3497 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003499 Py_INCREF(value);
3500 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003502 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003503 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 goto fail;
3505 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003506 if (argcount < co->co_argcount) {
3507 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003508 int missing = 0;
3509 for (i = argcount; i < m; i++)
3510 if (GETLOCAL(i) == NULL)
3511 missing++;
3512 if (missing) {
3513 missing_arguments(co, missing, defcount, fastlocals);
3514 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003515 }
3516 if (n > m)
3517 i = n - m;
3518 else
3519 i = 0;
3520 for (; i < defcount; i++) {
3521 if (GETLOCAL(m+i) == NULL) {
3522 PyObject *def = defs[i];
3523 Py_INCREF(def);
3524 SETLOCAL(m+i, def);
3525 }
3526 }
3527 }
3528 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003529 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003530 for (i = co->co_argcount; i < total_args; i++) {
3531 PyObject *name;
3532 if (GETLOCAL(i) != NULL)
3533 continue;
3534 name = PyTuple_GET_ITEM(co->co_varnames, i);
3535 if (kwdefs != NULL) {
3536 PyObject *def = PyDict_GetItem(kwdefs, name);
3537 if (def) {
3538 Py_INCREF(def);
3539 SETLOCAL(i, def);
3540 continue;
3541 }
3542 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003543 missing++;
3544 }
3545 if (missing) {
3546 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003547 goto fail;
3548 }
3549 }
3550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003552 vars into frame. */
3553 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003555 int arg;
3556 /* Possibly account for the cell variable being an argument. */
3557 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003558 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003559 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003560 /* Clear the local copy. */
3561 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003562 }
3563 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003564 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003565 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003566 if (c == NULL)
3567 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003568 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003570 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3571 PyObject *o = PyTuple_GET_ITEM(closure, i);
3572 Py_INCREF(o);
3573 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 if (co->co_flags & CO_GENERATOR) {
3577 /* Don't need to keep the reference to f_back, it will be set
3578 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003579 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 /* Create a new generator that owns the ready to run frame
3584 * and return that as the value. */
3585 return PyGen_New(f);
3586 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003589
Thomas Woutersce272b62007-09-19 21:19:28 +00003590fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 /* decref'ing the frame can cause __del__ methods to get invoked,
3593 which can call back into Python. While we're done with the
3594 current Python frame (f), the associated C stack is still in use,
3595 so recursion_depth must be boosted for the duration.
3596 */
3597 assert(tstate != NULL);
3598 ++tstate->recursion_depth;
3599 Py_DECREF(f);
3600 --tstate->recursion_depth;
3601 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003602}
3603
3604
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003605static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003606special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003609 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003611 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 return NULL;
3613 }
3614 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003615}
3616
3617
Benjamin Peterson87880242011-07-03 16:48:31 -05003618/* These 3 functions deal with the exception state of generators. */
3619
3620static void
3621save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3622{
3623 PyObject *type, *value, *traceback;
3624 Py_XINCREF(tstate->exc_type);
3625 Py_XINCREF(tstate->exc_value);
3626 Py_XINCREF(tstate->exc_traceback);
3627 type = f->f_exc_type;
3628 value = f->f_exc_value;
3629 traceback = f->f_exc_traceback;
3630 f->f_exc_type = tstate->exc_type;
3631 f->f_exc_value = tstate->exc_value;
3632 f->f_exc_traceback = tstate->exc_traceback;
3633 Py_XDECREF(type);
3634 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003635 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003636}
3637
3638static void
3639swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3640{
3641 PyObject *tmp;
3642 tmp = tstate->exc_type;
3643 tstate->exc_type = f->f_exc_type;
3644 f->f_exc_type = tmp;
3645 tmp = tstate->exc_value;
3646 tstate->exc_value = f->f_exc_value;
3647 f->f_exc_value = tmp;
3648 tmp = tstate->exc_traceback;
3649 tstate->exc_traceback = f->f_exc_traceback;
3650 f->f_exc_traceback = tmp;
3651}
3652
3653static void
3654restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3655{
3656 PyObject *type, *value, *tb;
3657 type = tstate->exc_type;
3658 value = tstate->exc_value;
3659 tb = tstate->exc_traceback;
3660 tstate->exc_type = f->f_exc_type;
3661 tstate->exc_value = f->f_exc_value;
3662 tstate->exc_traceback = f->f_exc_traceback;
3663 f->f_exc_type = NULL;
3664 f->f_exc_value = NULL;
3665 f->f_exc_traceback = NULL;
3666 Py_XDECREF(type);
3667 Py_XDECREF(value);
3668 Py_XDECREF(tb);
3669}
3670
3671
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003672/* Logic for the raise statement (too complicated for inlining).
3673 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003674static int
Collin Winter828f04a2007-08-31 00:04:24 +00003675do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003679 if (exc == NULL) {
3680 /* Reraise */
3681 PyThreadState *tstate = PyThreadState_GET();
3682 PyObject *tb;
3683 type = tstate->exc_type;
3684 value = tstate->exc_value;
3685 tb = tstate->exc_traceback;
3686 if (type == Py_None) {
3687 PyErr_SetString(PyExc_RuntimeError,
3688 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003689 return 0;
3690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003691 Py_XINCREF(type);
3692 Py_XINCREF(value);
3693 Py_XINCREF(tb);
3694 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003695 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 /* We support the following forms of raise:
3699 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003700 raise <instance>
3701 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 if (PyExceptionClass_Check(exc)) {
3704 type = exc;
3705 value = PyObject_CallObject(exc, NULL);
3706 if (value == NULL)
3707 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003708 if (!PyExceptionInstance_Check(value)) {
3709 PyErr_Format(PyExc_TypeError,
3710 "calling %R should have returned an instance of "
3711 "BaseException, not %R",
3712 type, Py_TYPE(value));
3713 goto raise_error;
3714 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 }
3716 else if (PyExceptionInstance_Check(exc)) {
3717 value = exc;
3718 type = PyExceptionInstance_Class(exc);
3719 Py_INCREF(type);
3720 }
3721 else {
3722 /* Not something you can raise. You get an exception
3723 anyway, just not what you specified :-) */
3724 Py_DECREF(exc);
3725 PyErr_SetString(PyExc_TypeError,
3726 "exceptions must derive from BaseException");
3727 goto raise_error;
3728 }
Collin Winter828f04a2007-08-31 00:04:24 +00003729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003730 if (cause) {
3731 PyObject *fixed_cause;
3732 if (PyExceptionClass_Check(cause)) {
3733 fixed_cause = PyObject_CallObject(cause, NULL);
3734 if (fixed_cause == NULL)
3735 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003736 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003737 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003738 else if (PyExceptionInstance_Check(cause)) {
3739 fixed_cause = cause;
3740 }
3741 else if (cause == Py_None) {
3742 Py_DECREF(cause);
3743 fixed_cause = NULL;
3744 }
3745 else {
3746 PyErr_SetString(PyExc_TypeError,
3747 "exception causes must derive from "
3748 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 goto raise_error;
3750 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003751 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 }
Collin Winter828f04a2007-08-31 00:04:24 +00003753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 PyErr_SetObject(type, value);
3755 /* PyErr_SetObject incref's its arguments */
3756 Py_XDECREF(value);
3757 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003758 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003759
3760raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 Py_XDECREF(value);
3762 Py_XDECREF(type);
3763 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003764 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003765}
3766
Tim Petersd6d010b2001-06-21 02:49:55 +00003767/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003768 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003769
Guido van Rossum0368b722007-05-11 16:50:42 +00003770 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3771 with a variable target.
3772*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003773
Barry Warsawe42b18f1997-08-25 22:13:04 +00003774static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003775unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 int i = 0, j = 0;
3778 Py_ssize_t ll = 0;
3779 PyObject *it; /* iter(v) */
3780 PyObject *w;
3781 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003785 it = PyObject_GetIter(v);
3786 if (it == NULL)
3787 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 for (; i < argcnt; i++) {
3790 w = PyIter_Next(it);
3791 if (w == NULL) {
3792 /* Iterator done, via error or exhaustion. */
3793 if (!PyErr_Occurred()) {
3794 PyErr_Format(PyExc_ValueError,
3795 "need more than %d value%s to unpack",
3796 i, i == 1 ? "" : "s");
3797 }
3798 goto Error;
3799 }
3800 *--sp = w;
3801 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 if (argcntafter == -1) {
3804 /* We better have exhausted the iterator now. */
3805 w = PyIter_Next(it);
3806 if (w == NULL) {
3807 if (PyErr_Occurred())
3808 goto Error;
3809 Py_DECREF(it);
3810 return 1;
3811 }
3812 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003813 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3814 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 goto Error;
3816 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 l = PySequence_List(it);
3819 if (l == NULL)
3820 goto Error;
3821 *--sp = l;
3822 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 ll = PyList_GET_SIZE(l);
3825 if (ll < argcntafter) {
3826 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3827 argcnt + ll);
3828 goto Error;
3829 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 /* Pop the "after-variable" args off the list. */
3832 for (j = argcntafter; j > 0; j--, i++) {
3833 *--sp = PyList_GET_ITEM(l, ll - j);
3834 }
3835 /* Resize the list. */
3836 Py_SIZE(l) = ll - argcntafter;
3837 Py_DECREF(it);
3838 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003839
Tim Petersd6d010b2001-06-21 02:49:55 +00003840Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003841 for (; i > 0; i--, sp++)
3842 Py_DECREF(*sp);
3843 Py_XDECREF(it);
3844 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003845}
3846
3847
Guido van Rossum96a42c81992-01-12 02:29:51 +00003848#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003849static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003850prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 printf("%s ", str);
3853 if (PyObject_Print(v, stdout, 0) != 0)
3854 PyErr_Clear(); /* Don't know what else to do */
3855 printf("\n");
3856 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003857}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003858#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003859
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003860static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003861call_exc_trace(Py_tracefunc func, PyObject *self,
3862 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003863{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003864 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003865 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01003866 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003867 if (value == NULL) {
3868 value = Py_None;
3869 Py_INCREF(value);
3870 }
Antoine Pitrou89335212013-11-23 14:05:23 +01003871 PyErr_NormalizeException(&type, &value, &orig_traceback);
3872 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 arg = PyTuple_Pack(3, type, value, traceback);
3874 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01003875 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 return;
3877 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003878 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 Py_DECREF(arg);
3880 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003881 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 else {
3883 Py_XDECREF(type);
3884 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02003885 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003886 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003887}
3888
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003889static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003890call_trace_protected(Py_tracefunc func, PyObject *obj,
3891 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003893{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003894 PyObject *type, *value, *traceback;
3895 int err;
3896 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003897 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 if (err == 0)
3899 {
3900 PyErr_Restore(type, value, traceback);
3901 return 0;
3902 }
3903 else {
3904 Py_XDECREF(type);
3905 Py_XDECREF(value);
3906 Py_XDECREF(traceback);
3907 return -1;
3908 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003909}
3910
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003911static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003912call_trace(Py_tracefunc func, PyObject *obj,
3913 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003915{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003916 int result;
3917 if (tstate->tracing)
3918 return 0;
3919 tstate->tracing++;
3920 tstate->use_tracing = 0;
3921 result = func(obj, frame, what, arg);
3922 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3923 || (tstate->c_profilefunc != NULL));
3924 tstate->tracing--;
3925 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003926}
3927
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003928PyObject *
3929_PyEval_CallTracing(PyObject *func, PyObject *args)
3930{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003931 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 int save_tracing = tstate->tracing;
3933 int save_use_tracing = tstate->use_tracing;
3934 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003935
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 tstate->tracing = 0;
3937 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3938 || (tstate->c_profilefunc != NULL));
3939 result = PyObject_Call(func, args, NULL);
3940 tstate->tracing = save_tracing;
3941 tstate->use_tracing = save_use_tracing;
3942 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003943}
3944
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003945/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003946static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003947maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003948 PyThreadState *tstate, PyFrameObject *frame,
3949 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 int result = 0;
3952 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 /* If the last instruction executed isn't in the current
3955 instruction window, reset the window.
3956 */
3957 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3958 PyAddrPair bounds;
3959 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3960 &bounds);
3961 *instr_lb = bounds.ap_lower;
3962 *instr_ub = bounds.ap_upper;
3963 }
3964 /* If the last instruction falls at the start of a line or if
3965 it represents a jump backwards, update the frame's line
3966 number and call the trace function. */
3967 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3968 frame->f_lineno = line;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003969 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003970 }
3971 *instr_prev = frame->f_lasti;
3972 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003973}
3974
Fred Drake5755ce62001-06-27 19:19:46 +00003975void
3976PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003977{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 PyThreadState *tstate = PyThreadState_GET();
3979 PyObject *temp = tstate->c_profileobj;
3980 Py_XINCREF(arg);
3981 tstate->c_profilefunc = NULL;
3982 tstate->c_profileobj = NULL;
3983 /* Must make sure that tracing is not ignored if 'temp' is freed */
3984 tstate->use_tracing = tstate->c_tracefunc != NULL;
3985 Py_XDECREF(temp);
3986 tstate->c_profilefunc = func;
3987 tstate->c_profileobj = arg;
3988 /* Flag that tracing or profiling is turned on */
3989 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003990}
3991
3992void
3993PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 PyThreadState *tstate = PyThreadState_GET();
3996 PyObject *temp = tstate->c_traceobj;
3997 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3998 Py_XINCREF(arg);
3999 tstate->c_tracefunc = NULL;
4000 tstate->c_traceobj = NULL;
4001 /* Must make sure that profiling is not ignored if 'temp' is freed */
4002 tstate->use_tracing = tstate->c_profilefunc != NULL;
4003 Py_XDECREF(temp);
4004 tstate->c_tracefunc = func;
4005 tstate->c_traceobj = arg;
4006 /* Flag that tracing or profiling is turned on */
4007 tstate->use_tracing = ((func != NULL)
4008 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004009}
4010
Guido van Rossumb209a111997-04-29 18:18:01 +00004011PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004012PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004014 PyFrameObject *current_frame = PyEval_GetFrame();
4015 if (current_frame == NULL)
4016 return PyThreadState_GET()->interp->builtins;
4017 else
4018 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004019}
4020
Guido van Rossumb209a111997-04-29 18:18:01 +00004021PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004022PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004023{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004025 if (current_frame == NULL) {
4026 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004028 }
4029
4030 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4031 return NULL;
4032
4033 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004035}
4036
Guido van Rossumb209a111997-04-29 18:18:01 +00004037PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004038PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004039{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004040 PyFrameObject *current_frame = PyEval_GetFrame();
4041 if (current_frame == NULL)
4042 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004043
4044 assert(current_frame->f_globals != NULL);
4045 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004046}
4047
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004048PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004049PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 PyThreadState *tstate = PyThreadState_GET();
4052 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004053}
4054
Guido van Rossum6135a871995-01-09 17:53:26 +00004055int
Tim Peters5ba58662001-07-16 02:29:45 +00004056PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004057{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004058 PyFrameObject *current_frame = PyEval_GetFrame();
4059 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 if (current_frame != NULL) {
4062 const int codeflags = current_frame->f_code->co_flags;
4063 const int compilerflags = codeflags & PyCF_MASK;
4064 if (compilerflags) {
4065 result = 1;
4066 cf->cf_flags |= compilerflags;
4067 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004068#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 if (codeflags & CO_GENERATOR_ALLOWED) {
4070 result = 1;
4071 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4072 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004073#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 }
4075 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004076}
4077
Guido van Rossum3f5da241990-12-20 15:06:42 +00004078
Guido van Rossum681d79a1995-07-18 14:51:37 +00004079/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00004080 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004081
Guido van Rossumb209a111997-04-29 18:18:01 +00004082PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004083PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004084{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004086
Victor Stinnerace47d72013-07-18 01:41:08 +02004087#ifdef Py_DEBUG
4088 /* PyEval_CallObjectWithKeywords() must not be called with an exception
4089 set, because it may clear it (directly or indirectly)
Martin Panter9955a372015-10-07 10:26:23 +00004090 and so the caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +02004091 assert(!PyErr_Occurred());
4092#endif
4093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 if (arg == NULL) {
4095 arg = PyTuple_New(0);
4096 if (arg == NULL)
4097 return NULL;
4098 }
4099 else if (!PyTuple_Check(arg)) {
4100 PyErr_SetString(PyExc_TypeError,
4101 "argument list must be a tuple");
4102 return NULL;
4103 }
4104 else
4105 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 if (kw != NULL && !PyDict_Check(kw)) {
4108 PyErr_SetString(PyExc_TypeError,
4109 "keyword list must be a dictionary");
4110 Py_DECREF(arg);
4111 return NULL;
4112 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 result = PyObject_Call(func, arg, kw);
4115 Py_DECREF(arg);
Victor Stinnerace47d72013-07-18 01:41:08 +02004116
4117 assert((result != NULL && !PyErr_Occurred())
4118 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004120}
4121
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004122const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004123PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004125 if (PyMethod_Check(func))
4126 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4127 else if (PyFunction_Check(func))
4128 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4129 else if (PyCFunction_Check(func))
4130 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4131 else
4132 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004133}
4134
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004135const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004136PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 if (PyMethod_Check(func))
4139 return "()";
4140 else if (PyFunction_Check(func))
4141 return "()";
4142 else if (PyCFunction_Check(func))
4143 return "()";
4144 else
4145 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004146}
4147
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004148static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004149err_args(PyObject *func, int flags, int nargs)
4150{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 if (flags & METH_NOARGS)
4152 PyErr_Format(PyExc_TypeError,
4153 "%.200s() takes no arguments (%d given)",
4154 ((PyCFunctionObject *)func)->m_ml->ml_name,
4155 nargs);
4156 else
4157 PyErr_Format(PyExc_TypeError,
4158 "%.200s() takes exactly one argument (%d given)",
4159 ((PyCFunctionObject *)func)->m_ml->ml_name,
4160 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004161}
4162
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004163#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004164if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004165 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4166 tstate, tstate->frame, \
4167 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 x = NULL; \
4169 } \
4170 else { \
4171 x = call; \
4172 if (tstate->c_profilefunc != NULL) { \
4173 if (x == NULL) { \
4174 call_trace_protected(tstate->c_profilefunc, \
4175 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004176 tstate, tstate->frame, \
4177 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 /* XXX should pass (type, value, tb) */ \
4179 } else { \
4180 if (call_trace(tstate->c_profilefunc, \
4181 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004182 tstate, tstate->frame, \
4183 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 Py_DECREF(x); \
4185 x = NULL; \
4186 } \
4187 } \
4188 } \
4189 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004190} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 x = call; \
4192 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004193
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004194static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004195call_function(PyObject ***pp_stack, int oparg
4196#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004198#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 int na = oparg & 0xff;
4202 int nk = (oparg>>8) & 0xff;
4203 int n = na + 2 * nk;
4204 PyObject **pfunc = (*pp_stack) - n - 1;
4205 PyObject *func = *pfunc;
4206 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004208 /* Always dispatch PyCFunction first, because these are
4209 presumed to be the most frequent callable object.
4210 */
4211 if (PyCFunction_Check(func) && nk == 0) {
4212 int flags = PyCFunction_GET_FLAGS(func);
4213 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 PCALL(PCALL_CFUNCTION);
4216 if (flags & (METH_NOARGS | METH_O)) {
4217 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4218 PyObject *self = PyCFunction_GET_SELF(func);
4219 if (flags & METH_NOARGS && na == 0) {
4220 C_TRACE(x, (*meth)(self,NULL));
4221 }
4222 else if (flags & METH_O && na == 1) {
4223 PyObject *arg = EXT_POP(*pp_stack);
4224 C_TRACE(x, (*meth)(self,arg));
4225 Py_DECREF(arg);
4226 }
4227 else {
4228 err_args(func, flags, na);
4229 x = NULL;
4230 }
4231 }
4232 else {
4233 PyObject *callargs;
4234 callargs = load_args(pp_stack, na);
Victor Stinner0ff0f542013-07-08 22:27:42 +02004235 if (callargs != NULL) {
4236 READ_TIMESTAMP(*pintr0);
4237 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4238 READ_TIMESTAMP(*pintr1);
4239 Py_XDECREF(callargs);
4240 }
4241 else {
4242 x = NULL;
4243 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 }
4245 } else {
4246 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4247 /* optimize access to bound methods */
4248 PyObject *self = PyMethod_GET_SELF(func);
4249 PCALL(PCALL_METHOD);
4250 PCALL(PCALL_BOUND_METHOD);
4251 Py_INCREF(self);
4252 func = PyMethod_GET_FUNCTION(func);
4253 Py_INCREF(func);
4254 Py_DECREF(*pfunc);
4255 *pfunc = self;
4256 na++;
4257 n++;
4258 } else
4259 Py_INCREF(func);
4260 READ_TIMESTAMP(*pintr0);
4261 if (PyFunction_Check(func))
4262 x = fast_function(func, pp_stack, n, na, nk);
4263 else
4264 x = do_call(func, pp_stack, na, nk);
4265 READ_TIMESTAMP(*pintr1);
4266 Py_DECREF(func);
4267 }
Victor Stinnerf243ee42013-07-16 01:02:12 +02004268 assert((x != NULL && !PyErr_Occurred())
4269 || (x == NULL && PyErr_Occurred()));
Tim Peters8a5c3c72004-04-05 19:36:21 +00004270
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004271 /* Clear the stack of the function object. Also removes
4272 the arguments in case they weren't consumed already
4273 (fast_function() and err_args() leave them on the stack).
4274 */
4275 while ((*pp_stack) > pfunc) {
4276 w = EXT_POP(*pp_stack);
4277 Py_DECREF(w);
4278 PCALL(PCALL_POP);
4279 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004280
4281 assert((x != NULL && !PyErr_Occurred())
4282 || (x == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004284}
4285
Jeremy Hylton192690e2002-08-16 18:36:11 +00004286/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004287 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004288 For the simplest case -- a function that takes only positional
4289 arguments and is called with only positional arguments -- it
4290 inlines the most primitive frame setup code from
4291 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4292 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004293*/
4294
4295static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004296fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4299 PyObject *globals = PyFunction_GET_GLOBALS(func);
4300 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4301 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4302 PyObject **d = NULL;
4303 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 PCALL(PCALL_FUNCTION);
4306 PCALL(PCALL_FAST_FUNCTION);
4307 if (argdefs == NULL && co->co_argcount == n &&
4308 co->co_kwonlyargcount == 0 && nk==0 &&
4309 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4310 PyFrameObject *f;
4311 PyObject *retval = NULL;
4312 PyThreadState *tstate = PyThreadState_GET();
4313 PyObject **fastlocals, **stack;
4314 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 PCALL(PCALL_FASTER_FUNCTION);
4317 assert(globals != NULL);
4318 /* XXX Perhaps we should create a specialized
4319 PyFrame_New() that doesn't take locals, but does
4320 take builtins without sanity checking them.
4321 */
4322 assert(tstate != NULL);
4323 f = PyFrame_New(tstate, co, globals, NULL);
4324 if (f == NULL)
4325 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 fastlocals = f->f_localsplus;
4328 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 for (i = 0; i < n; i++) {
4331 Py_INCREF(*stack);
4332 fastlocals[i] = *stack++;
4333 }
4334 retval = PyEval_EvalFrameEx(f,0);
4335 ++tstate->recursion_depth;
4336 Py_DECREF(f);
4337 --tstate->recursion_depth;
4338 return retval;
4339 }
4340 if (argdefs != NULL) {
4341 d = &PyTuple_GET_ITEM(argdefs, 0);
4342 nd = Py_SIZE(argdefs);
4343 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004344 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 (PyObject *)NULL, (*pp_stack)-n, na,
4346 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4347 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004348}
4349
4350static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004351update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4352 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004353{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004354 PyObject *kwdict = NULL;
4355 if (orig_kwdict == NULL)
4356 kwdict = PyDict_New();
4357 else {
4358 kwdict = PyDict_Copy(orig_kwdict);
4359 Py_DECREF(orig_kwdict);
4360 }
4361 if (kwdict == NULL)
4362 return NULL;
4363 while (--nk >= 0) {
4364 int err;
4365 PyObject *value = EXT_POP(*pp_stack);
4366 PyObject *key = EXT_POP(*pp_stack);
4367 if (PyDict_GetItem(kwdict, key) != NULL) {
4368 PyErr_Format(PyExc_TypeError,
4369 "%.200s%s got multiple values "
4370 "for keyword argument '%U'",
4371 PyEval_GetFuncName(func),
4372 PyEval_GetFuncDesc(func),
4373 key);
4374 Py_DECREF(key);
4375 Py_DECREF(value);
4376 Py_DECREF(kwdict);
4377 return NULL;
4378 }
4379 err = PyDict_SetItem(kwdict, key, value);
4380 Py_DECREF(key);
4381 Py_DECREF(value);
4382 if (err) {
4383 Py_DECREF(kwdict);
4384 return NULL;
4385 }
4386 }
4387 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004388}
4389
4390static PyObject *
4391update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 callargs = PyTuple_New(nstack + nstar);
4397 if (callargs == NULL) {
4398 return NULL;
4399 }
4400 if (nstar) {
4401 int i;
4402 for (i = 0; i < nstar; i++) {
4403 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4404 Py_INCREF(a);
4405 PyTuple_SET_ITEM(callargs, nstack + i, a);
4406 }
4407 }
4408 while (--nstack >= 0) {
4409 w = EXT_POP(*pp_stack);
4410 PyTuple_SET_ITEM(callargs, nstack, w);
4411 }
4412 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004413}
4414
4415static PyObject *
4416load_args(PyObject ***pp_stack, int na)
4417{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 PyObject *args = PyTuple_New(na);
4419 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004421 if (args == NULL)
4422 return NULL;
4423 while (--na >= 0) {
4424 w = EXT_POP(*pp_stack);
4425 PyTuple_SET_ITEM(args, na, w);
4426 }
4427 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004428}
4429
4430static PyObject *
4431do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 PyObject *callargs = NULL;
4434 PyObject *kwdict = NULL;
4435 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 if (nk > 0) {
4438 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4439 if (kwdict == NULL)
4440 goto call_fail;
4441 }
4442 callargs = load_args(pp_stack, na);
4443 if (callargs == NULL)
4444 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004445#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 /* At this point, we have to look at the type of func to
4447 update the call stats properly. Do it here so as to avoid
4448 exposing the call stats machinery outside ceval.c
4449 */
4450 if (PyFunction_Check(func))
4451 PCALL(PCALL_FUNCTION);
4452 else if (PyMethod_Check(func))
4453 PCALL(PCALL_METHOD);
4454 else if (PyType_Check(func))
4455 PCALL(PCALL_TYPE);
4456 else if (PyCFunction_Check(func))
4457 PCALL(PCALL_CFUNCTION);
4458 else
4459 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004460#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 if (PyCFunction_Check(func)) {
4462 PyThreadState *tstate = PyThreadState_GET();
4463 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4464 }
4465 else
4466 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004467call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 Py_XDECREF(callargs);
4469 Py_XDECREF(kwdict);
4470 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004471}
4472
4473static PyObject *
4474ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4475{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 int nstar = 0;
4477 PyObject *callargs = NULL;
4478 PyObject *stararg = NULL;
4479 PyObject *kwdict = NULL;
4480 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 if (flags & CALL_FLAG_KW) {
4483 kwdict = EXT_POP(*pp_stack);
4484 if (!PyDict_Check(kwdict)) {
4485 PyObject *d;
4486 d = PyDict_New();
4487 if (d == NULL)
4488 goto ext_call_fail;
4489 if (PyDict_Update(d, kwdict) != 0) {
4490 Py_DECREF(d);
4491 /* PyDict_Update raises attribute
4492 * error (percolated from an attempt
4493 * to get 'keys' attribute) instead of
4494 * a type error if its second argument
4495 * is not a mapping.
4496 */
4497 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4498 PyErr_Format(PyExc_TypeError,
4499 "%.200s%.200s argument after ** "
4500 "must be a mapping, not %.200s",
4501 PyEval_GetFuncName(func),
4502 PyEval_GetFuncDesc(func),
4503 kwdict->ob_type->tp_name);
4504 }
4505 goto ext_call_fail;
4506 }
4507 Py_DECREF(kwdict);
4508 kwdict = d;
4509 }
4510 }
4511 if (flags & CALL_FLAG_VAR) {
4512 stararg = EXT_POP(*pp_stack);
4513 if (!PyTuple_Check(stararg)) {
4514 PyObject *t = NULL;
4515 t = PySequence_Tuple(stararg);
4516 if (t == NULL) {
4517 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4518 PyErr_Format(PyExc_TypeError,
4519 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004520 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004521 PyEval_GetFuncName(func),
4522 PyEval_GetFuncDesc(func),
4523 stararg->ob_type->tp_name);
4524 }
4525 goto ext_call_fail;
4526 }
4527 Py_DECREF(stararg);
4528 stararg = t;
4529 }
4530 nstar = PyTuple_GET_SIZE(stararg);
4531 }
4532 if (nk > 0) {
4533 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4534 if (kwdict == NULL)
4535 goto ext_call_fail;
4536 }
4537 callargs = update_star_args(na, nstar, stararg, pp_stack);
4538 if (callargs == NULL)
4539 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004540#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 /* At this point, we have to look at the type of func to
4542 update the call stats properly. Do it here so as to avoid
4543 exposing the call stats machinery outside ceval.c
4544 */
4545 if (PyFunction_Check(func))
4546 PCALL(PCALL_FUNCTION);
4547 else if (PyMethod_Check(func))
4548 PCALL(PCALL_METHOD);
4549 else if (PyType_Check(func))
4550 PCALL(PCALL_TYPE);
4551 else if (PyCFunction_Check(func))
4552 PCALL(PCALL_CFUNCTION);
4553 else
4554 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004555#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004556 if (PyCFunction_Check(func)) {
4557 PyThreadState *tstate = PyThreadState_GET();
4558 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4559 }
4560 else
4561 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004562ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 Py_XDECREF(callargs);
4564 Py_XDECREF(kwdict);
4565 Py_XDECREF(stararg);
Victor Stinnerf243ee42013-07-16 01:02:12 +02004566 assert((result != NULL && !PyErr_Occurred())
4567 || (result == NULL && PyErr_Occurred()));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004569}
4570
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004571/* Extract a slice index from a PyInt or PyLong or an object with the
4572 nb_index slot defined, and store in *pi.
4573 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4574 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 +00004575 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004576*/
Tim Petersb5196382001-12-16 19:44:20 +00004577/* Note: If v is NULL, return success without storing into *pi. This
4578 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4579 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004580*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004581int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004582_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 if (v != NULL) {
4585 Py_ssize_t x;
4586 if (PyIndex_Check(v)) {
4587 x = PyNumber_AsSsize_t(v, NULL);
4588 if (x == -1 && PyErr_Occurred())
4589 return 0;
4590 }
4591 else {
4592 PyErr_SetString(PyExc_TypeError,
4593 "slice indices must be integers or "
4594 "None or have an __index__ method");
4595 return 0;
4596 }
4597 *pi = x;
4598 }
4599 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004600}
4601
Guido van Rossum486364b2007-06-30 05:01:58 +00004602#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004603 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004604
Guido van Rossumb209a111997-04-29 18:18:01 +00004605static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004606cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 int res = 0;
4609 switch (op) {
4610 case PyCmp_IS:
4611 res = (v == w);
4612 break;
4613 case PyCmp_IS_NOT:
4614 res = (v != w);
4615 break;
4616 case PyCmp_IN:
4617 res = PySequence_Contains(w, v);
4618 if (res < 0)
4619 return NULL;
4620 break;
4621 case PyCmp_NOT_IN:
4622 res = PySequence_Contains(w, v);
4623 if (res < 0)
4624 return NULL;
4625 res = !res;
4626 break;
4627 case PyCmp_EXC_MATCH:
4628 if (PyTuple_Check(w)) {
4629 Py_ssize_t i, length;
4630 length = PyTuple_Size(w);
4631 for (i = 0; i < length; i += 1) {
4632 PyObject *exc = PyTuple_GET_ITEM(w, i);
4633 if (!PyExceptionClass_Check(exc)) {
4634 PyErr_SetString(PyExc_TypeError,
4635 CANNOT_CATCH_MSG);
4636 return NULL;
4637 }
4638 }
4639 }
4640 else {
4641 if (!PyExceptionClass_Check(w)) {
4642 PyErr_SetString(PyExc_TypeError,
4643 CANNOT_CATCH_MSG);
4644 return NULL;
4645 }
4646 }
4647 res = PyErr_GivenExceptionMatches(v, w);
4648 break;
4649 default:
4650 return PyObject_RichCompare(v, w, op);
4651 }
4652 v = res ? Py_True : Py_False;
4653 Py_INCREF(v);
4654 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004655}
4656
Thomas Wouters52152252000-08-17 22:55:00 +00004657static PyObject *
4658import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 x = PyObject_GetAttr(v, name);
4663 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Brett Cannona79e4fb2013-07-12 11:22:26 -04004664 PyErr_Format(PyExc_ImportError, "cannot import name %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004665 }
4666 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004667}
Guido van Rossumac7be682001-01-17 15:42:30 +00004668
Thomas Wouters52152252000-08-17 22:55:00 +00004669static int
4670import_all_from(PyObject *locals, PyObject *v)
4671{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004672 _Py_IDENTIFIER(__all__);
4673 _Py_IDENTIFIER(__dict__);
4674 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004675 PyObject *dict, *name, *value;
4676 int skip_leading_underscores = 0;
4677 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004679 if (all == NULL) {
4680 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4681 return -1; /* Unexpected error */
4682 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004683 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 if (dict == NULL) {
4685 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4686 return -1;
4687 PyErr_SetString(PyExc_ImportError,
4688 "from-import-* object has no __dict__ and no __all__");
4689 return -1;
4690 }
4691 all = PyMapping_Keys(dict);
4692 Py_DECREF(dict);
4693 if (all == NULL)
4694 return -1;
4695 skip_leading_underscores = 1;
4696 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004698 for (pos = 0, err = 0; ; pos++) {
4699 name = PySequence_GetItem(all, pos);
4700 if (name == NULL) {
4701 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4702 err = -1;
4703 else
4704 PyErr_Clear();
4705 break;
4706 }
4707 if (skip_leading_underscores &&
4708 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004709 PyUnicode_READY(name) != -1 &&
4710 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004711 {
4712 Py_DECREF(name);
4713 continue;
4714 }
4715 value = PyObject_GetAttr(v, name);
4716 if (value == NULL)
4717 err = -1;
4718 else if (PyDict_CheckExact(locals))
4719 err = PyDict_SetItem(locals, name, value);
4720 else
4721 err = PyObject_SetItem(locals, name, value);
4722 Py_DECREF(name);
4723 Py_XDECREF(value);
4724 if (err != 0)
4725 break;
4726 }
4727 Py_DECREF(all);
4728 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004729}
4730
Guido van Rossumac7be682001-01-17 15:42:30 +00004731static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004732format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 if (!obj)
4737 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 obj_str = _PyUnicode_AsString(obj);
4740 if (!obj_str)
4741 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004743 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004744}
Guido van Rossum950361c1997-01-24 13:49:28 +00004745
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004746static void
4747format_exc_unbound(PyCodeObject *co, int oparg)
4748{
4749 PyObject *name;
4750 /* Don't stomp existing exception */
4751 if (PyErr_Occurred())
4752 return;
4753 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4754 name = PyTuple_GET_ITEM(co->co_cellvars,
4755 oparg);
4756 format_exc_check_arg(
4757 PyExc_UnboundLocalError,
4758 UNBOUNDLOCAL_ERROR_MSG,
4759 name);
4760 } else {
4761 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4762 PyTuple_GET_SIZE(co->co_cellvars));
4763 format_exc_check_arg(PyExc_NameError,
4764 UNBOUNDFREE_ERROR_MSG, name);
4765 }
4766}
4767
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004768static PyObject *
4769unicode_concatenate(PyObject *v, PyObject *w,
4770 PyFrameObject *f, unsigned char *next_instr)
4771{
4772 PyObject *res;
4773 if (Py_REFCNT(v) == 2) {
4774 /* In the common case, there are 2 references to the value
4775 * stored in 'variable' when the += is performed: one on the
4776 * value stack (in 'v') and one still stored in the
4777 * 'variable'. We try to delete the variable now to reduce
4778 * the refcnt to 1.
4779 */
4780 switch (*next_instr) {
4781 case STORE_FAST:
4782 {
4783 int oparg = PEEKARG();
4784 PyObject **fastlocals = f->f_localsplus;
4785 if (GETLOCAL(oparg) == v)
4786 SETLOCAL(oparg, NULL);
4787 break;
4788 }
4789 case STORE_DEREF:
4790 {
4791 PyObject **freevars = (f->f_localsplus +
4792 f->f_code->co_nlocals);
4793 PyObject *c = freevars[PEEKARG()];
4794 if (PyCell_GET(c) == v)
4795 PyCell_Set(c, NULL);
4796 break;
4797 }
4798 case STORE_NAME:
4799 {
4800 PyObject *names = f->f_code->co_names;
4801 PyObject *name = GETITEM(names, PEEKARG());
4802 PyObject *locals = f->f_locals;
4803 if (PyDict_CheckExact(locals) &&
4804 PyDict_GetItem(locals, name) == v) {
4805 if (PyDict_DelItem(locals, name) != 0) {
4806 PyErr_Clear();
4807 }
4808 }
4809 break;
4810 }
4811 }
4812 }
4813 res = v;
4814 PyUnicode_Append(&res, w);
4815 return res;
4816}
4817
Guido van Rossum950361c1997-01-24 13:49:28 +00004818#ifdef DYNAMIC_EXECUTION_PROFILE
4819
Skip Montanarof118cb12001-10-15 20:51:38 +00004820static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004821getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004823 int i;
4824 PyObject *l = PyList_New(256);
4825 if (l == NULL) return NULL;
4826 for (i = 0; i < 256; i++) {
4827 PyObject *x = PyLong_FromLong(a[i]);
4828 if (x == NULL) {
4829 Py_DECREF(l);
4830 return NULL;
4831 }
4832 PyList_SetItem(l, i, x);
4833 }
4834 for (i = 0; i < 256; i++)
4835 a[i] = 0;
4836 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004837}
4838
4839PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004840_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004841{
4842#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004843 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004844#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004845 int i;
4846 PyObject *l = PyList_New(257);
4847 if (l == NULL) return NULL;
4848 for (i = 0; i < 257; i++) {
4849 PyObject *x = getarray(dxpairs[i]);
4850 if (x == NULL) {
4851 Py_DECREF(l);
4852 return NULL;
4853 }
4854 PyList_SetItem(l, i, x);
4855 }
4856 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004857#endif
4858}
4859
4860#endif