blob: ae0bd242a0173e4a998acda5be7a4b12c6df3b1c [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 Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500141static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000147#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000149#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "free variable '%.200s' referenced before assignment" \
151 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000152
Guido van Rossum950361c1997-01-24 13:49:28 +0000153/* Dynamic execution profile */
154#ifdef DYNAMIC_EXECUTION_PROFILE
155#ifdef DXPAIRS
156static long dxpairs[257][256];
157#define dxp dxpairs[256]
158#else
159static long dxp[256];
160#endif
161#endif
162
Jeremy Hylton985eba52003-02-05 23:13:00 +0000163/* Function call profile */
164#ifdef CALL_PROFILE
165#define PCALL_NUM 11
166static int pcall[PCALL_NUM];
167
168#define PCALL_ALL 0
169#define PCALL_FUNCTION 1
170#define PCALL_FAST_FUNCTION 2
171#define PCALL_FASTER_FUNCTION 3
172#define PCALL_METHOD 4
173#define PCALL_BOUND_METHOD 5
174#define PCALL_CFUNCTION 6
175#define PCALL_TYPE 7
176#define PCALL_GENERATOR 8
177#define PCALL_OTHER 9
178#define PCALL_POP 10
179
180/* Notes about the statistics
181
182 PCALL_FAST stats
183
184 FAST_FUNCTION means no argument tuple needs to be created.
185 FASTER_FUNCTION means that the fast-path frame setup code is used.
186
187 If there is a method call where the call can be optimized by changing
188 the argument tuple and calling the function directly, it gets recorded
189 twice.
190
191 As a result, the relationship among the statistics appears to be
192 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
193 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
194 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
195 PCALL_METHOD > PCALL_BOUND_METHOD
196*/
197
198#define PCALL(POS) pcall[POS]++
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return Py_BuildValue("iiiiiiiiiii",
204 pcall[0], pcall[1], pcall[2], pcall[3],
205 pcall[4], pcall[5], pcall[6], pcall[7],
206 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000207}
208#else
209#define PCALL(O)
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#endif
218
Tim Peters5ca576e2001-06-18 22:08:13 +0000219
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000220#ifdef WITH_THREAD
221#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
222#else
223#define GIL_REQUEST 0
224#endif
225
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000226/* This can set eval_breaker to 0 even though gil_drop_request became
227 1. We believe this is all right because the eval loop will release
228 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000229#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 _Py_atomic_store_relaxed( \
231 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000232 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
234 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000236#ifdef WITH_THREAD
237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 do { \
240 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
241 _Py_atomic_store_relaxed(&eval_breaker, 1); \
242 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243
244#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
247 COMPUTE_EVAL_BREAKER(); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000250#endif
251
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000252/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 do { \
255 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
256 _Py_atomic_store_relaxed(&eval_breaker, 1); \
257 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
259#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
262 COMPUTE_EVAL_BREAKER(); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 pending_async_exc = 1; \
268 _Py_atomic_store_relaxed(&eval_breaker, 1); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274
Guido van Rossume59214e1994-08-30 08:01:59 +0000275#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000276
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000279#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000280#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000282static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000283static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284/* This single variable consolidates all requests to break out of the fast path
285 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000286static _Py_atomic_int eval_breaker = {0};
287/* Request for dropping the GIL */
288static _Py_atomic_int gil_drop_request = {0};
289/* Request for running pending calls. */
290static _Py_atomic_int pendingcalls_to_do = {0};
291/* Request for looking at the `async_exc` field of the current thread state.
292 Guarded by the GIL. */
293static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000294
295#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296
Tim Peters7f468f22004-10-11 02:40:51 +0000297int
298PyEval_ThreadsInitialized(void)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000301}
302
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (gil_created())
307 return;
308 create_gil();
309 take_gil(PyThreadState_GET());
310 main_thread = PyThread_get_thread_ident();
311 if (!pending_lock)
312 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000316_PyEval_FiniThreads(void)
317{
318 if (!gil_created())
319 return;
320 destroy_gil();
321 assert(!gil_created());
322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
330 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000331}
332
333void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* This function must succeed when the current thread state is NULL.
337 We therefore avoid PyThreadState_GET() which dumps a fatal error
338 in debug mode.
339 */
340 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
341 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (tstate == NULL)
348 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
349 /* Check someone has called PyEval_InitThreads() to create the lock */
350 assert(gil_created());
351 take_gil(tstate);
352 if (PyThreadState_Swap(tstate) != NULL)
353 Py_FatalError(
354 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000355}
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (tstate == NULL)
361 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
362 if (PyThreadState_Swap(NULL) != tstate)
363 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
364 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000366
367/* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */
371
372void
373PyEval_ReInitThreads(void)
374{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200375 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyObject *threading, *result;
377 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (!gil_created())
380 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 recreate_gil();
382 pending_lock = PyThread_allocate_lock();
383 take_gil(tstate);
384 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* Update the threading module with the new state.
387 */
388 tstate = PyThreadState_GET();
389 threading = PyMapping_GetItemString(tstate->interp->modules,
390 "threading");
391 if (threading == NULL) {
392 /* threading not imported */
393 PyErr_Clear();
394 return;
395 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200396 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (result == NULL)
398 PyErr_WriteUnraisable(threading);
399 else
400 Py_DECREF(result);
401 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000402}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000403
404#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000405static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000406static int pending_async_exc = 0;
407#endif /* WITH_THREAD */
408
409/* This function is used to signal that async exceptions are waiting to be
410 raised, therefore it is also useful in non-threaded builds. */
411
412void
413_PyEval_SignalAsyncExc(void)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000416}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417
Guido van Rossumff4949e1992-08-05 19:58:53 +0000418/* Functions save_thread and restore_thread are always defined so
419 dynamically loaded modules needn't be compiled separately for use
420 with and without threads: */
421
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000422PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 PyThreadState *tstate = PyThreadState_Swap(NULL);
426 if (tstate == NULL)
427 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000428#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (gil_created())
430 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433}
434
435void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (tstate == NULL)
439 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000440#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (gil_created()) {
442 int err = errno;
443 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200444 /* _Py_Finalizing is protected by the GIL */
445 if (_Py_Finalizing && tstate != _Py_Finalizing) {
446 drop_gil(tstate);
447 PyThread_exit_thread();
448 assert(0); /* unreachable */
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 errno = err;
451 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454}
455
456
Guido van Rossuma9672091994-09-14 13:31:22 +0000457/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
458 signal handlers or Mac I/O completion routines) can schedule calls
459 to a function to be called synchronously.
460 The synchronous function is called with one void* argument.
461 It should return 0 for success or -1 for failure -- failure should
462 be accompanied by an exception.
463
464 If registry succeeds, the registry function returns 0; if it fails
465 (e.g. due to too many pending calls) it returns -1 (without setting
466 an exception condition).
467
468 Note that because registry may occur from within signal handlers,
469 or other asynchronous events, calling malloc() is unsafe!
470
471#ifdef WITH_THREAD
472 Any thread can schedule pending calls, but only the main thread
473 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474 There is no facility to schedule calls to a particular thread, but
475 that should be easy to change, should that ever be required. In
476 that case, the static variables here should go into the python
477 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000478#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000480
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481#ifdef WITH_THREAD
482
483/* The WITH_THREAD implementation is thread-safe. It allows
484 scheduling to be made from any thread, and even from an executing
485 callback.
486 */
487
488#define NPENDINGCALLS 32
489static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 int (*func)(void *);
491 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492} pendingcalls[NPENDINGCALLS];
493static int pendingfirst = 0;
494static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
496int
497Py_AddPendingCall(int (*func)(void *), void *arg)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we
505 * hold the lock during the Py_MakePendingCalls() function.
506 * This avoids a deadlock in that case.
507 * Note that signals can be delivered on any thread. In particular,
508 * on Windows, a SIGINT is delivered on a system-created worker
509 * thread.
510 * We also check for lock being NULL, in the unlikely case that
511 * this function is called before any bytecode evaluation takes place.
512 */
513 if (lock != NULL) {
514 for (i = 0; i<100; i++) {
515 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
516 break;
517 }
518 if (i == 100)
519 return -1;
520 }
521
522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
524 if (j == pendingfirst) {
525 result = -1; /* Queue full */
526 } else {
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
530 }
531 /* signal main loop */
532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL)
534 PyThread_release_lock(lock);
535 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538int
539Py_MakePendingCalls(void)
540{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200541 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int i;
543 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (!pending_lock) {
546 /* initial allocation of the lock */
547 pending_lock = PyThread_allocate_lock();
548 if (pending_lock == NULL)
549 return -1;
550 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* only service pending calls on main thread */
553 if (main_thread && PyThread_get_thread_ident() != main_thread)
554 return 0;
555 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200556 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200558 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* perform a bounded number of calls, in case of recursion */
560 for (i=0; i<NPENDINGCALLS; i++) {
561 int j;
562 int (*func)(void *);
563 void *arg = NULL;
564
565 /* pop one item off the queue while holding the lock */
566 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
567 j = pendingfirst;
568 if (j == pendinglast) {
569 func = NULL; /* Queue empty */
570 } else {
571 func = pendingcalls[j].func;
572 arg = pendingcalls[j].arg;
573 pendingfirst = (j + 1) % NPENDINGCALLS;
574 }
575 if (pendingfirst != pendinglast)
576 SIGNAL_PENDING_CALLS();
577 else
578 UNSIGNAL_PENDING_CALLS();
579 PyThread_release_lock(pending_lock);
580 /* having released the lock, perform the callback */
581 if (func == NULL)
582 break;
583 r = func(arg);
584 if (r)
585 break;
586 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200587 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000589}
590
591#else /* if ! defined WITH_THREAD */
592
593/*
594 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
595 This code is used for signal handling in python that isn't built
596 with WITH_THREAD.
597 Don't use this implementation when Py_AddPendingCalls() can happen
598 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599
Guido van Rossuma9672091994-09-14 13:31:22 +0000600 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601 (1) nested asynchronous calls to Py_AddPendingCall()
602 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604 (1) is very unlikely because typically signal delivery
605 is blocked during signal handling. So it should be impossible.
606 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000607 The current code is safe against (2), but not against (1).
608 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 thread is present, interrupted by signals, and that the critical
610 section is protected with the "busy" variable. On Windows, which
611 delivers SIGINT on a system thread, this does not hold and therefore
612 Windows really shouldn't use this version.
613 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000614*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000615
Guido van Rossuma9672091994-09-14 13:31:22 +0000616#define NPENDINGCALLS 32
617static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 int (*func)(void *);
619 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000620} pendingcalls[NPENDINGCALLS];
621static volatile int pendingfirst = 0;
622static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000623static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000624
625int
Thomas Wouters334fb892000-07-25 12:56:38 +0000626Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 static volatile int busy = 0;
629 int i, j;
630 /* XXX Begin critical section */
631 if (busy)
632 return -1;
633 busy = 1;
634 i = pendinglast;
635 j = (i + 1) % NPENDINGCALLS;
636 if (j == pendingfirst) {
637 busy = 0;
638 return -1; /* Queue full */
639 }
640 pendingcalls[i].func = func;
641 pendingcalls[i].arg = arg;
642 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 SIGNAL_PENDING_CALLS();
645 busy = 0;
646 /* XXX End critical section */
647 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000648}
649
Guido van Rossum180d7b41994-09-29 09:45:57 +0000650int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 static int busy = 0;
654 if (busy)
655 return 0;
656 busy = 1;
657 UNSIGNAL_PENDING_CALLS();
658 for (;;) {
659 int i;
660 int (*func)(void *);
661 void *arg;
662 i = pendingfirst;
663 if (i == pendinglast)
664 break; /* Queue empty */
665 func = pendingcalls[i].func;
666 arg = pendingcalls[i].arg;
667 pendingfirst = (i + 1) % NPENDINGCALLS;
668 if (func(arg) < 0) {
669 busy = 0;
670 SIGNAL_PENDING_CALLS(); /* We're not done yet */
671 return -1;
672 }
673 }
674 busy = 0;
675 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000676}
677
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000678#endif /* WITH_THREAD */
679
Guido van Rossuma9672091994-09-14 13:31:22 +0000680
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681/* The interpreter's recursion limit */
682
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000683#ifndef Py_DEFAULT_RECURSION_LIMIT
684#define Py_DEFAULT_RECURSION_LIMIT 1000
685#endif
686static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
687int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000689int
690Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693}
694
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000695void
696Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 recursion_limit = new_limit;
699 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700}
701
Armin Rigo2b3eb402003-10-28 12:05:48 +0000702/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
703 if the recursion_depth reaches _Py_CheckRecursionLimit.
704 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
705 to guarantee that _Py_CheckRecursiveCall() is regularly called.
706 Without USE_STACKCHECK, there is no need for this. */
707int
708_Py_CheckRecursiveCall(char *where)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711
712#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (PyOS_CheckStack()) {
714 --tstate->recursion_depth;
715 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
716 return -1;
717 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 _Py_CheckRecursionLimit = recursion_limit;
720 if (tstate->recursion_critical)
721 /* Somebody asked that we don't check for recursion. */
722 return 0;
723 if (tstate->overflowed) {
724 if (tstate->recursion_depth > recursion_limit + 50) {
725 /* Overflowing while handling an overflow. Give up. */
726 Py_FatalError("Cannot recover from stack overflow.");
727 }
728 return 0;
729 }
730 if (tstate->recursion_depth > recursion_limit) {
731 --tstate->recursion_depth;
732 tstate->overflowed = 1;
733 PyErr_Format(PyExc_RuntimeError,
734 "maximum recursion depth exceeded%s",
735 where);
736 return -1;
737 }
738 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000739}
740
Guido van Rossum374a9221991-04-04 10:40:29 +0000741/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000742enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000743 WHY_NOT = 0x0001, /* No error */
744 WHY_EXCEPTION = 0x0002, /* Exception occurred */
745 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
746 WHY_RETURN = 0x0008, /* 'return' statement */
747 WHY_BREAK = 0x0010, /* 'break' statement */
748 WHY_CONTINUE = 0x0020, /* 'continue' statement */
749 WHY_YIELD = 0x0040, /* 'yield' operator */
750 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000751};
Guido van Rossum374a9221991-04-04 10:40:29 +0000752
Benjamin Peterson87880242011-07-03 16:48:31 -0500753static void save_exc_state(PyThreadState *, PyFrameObject *);
754static void swap_exc_state(PyThreadState *, PyFrameObject *);
755static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Collin Winter828f04a2007-08-31 00:04:24 +0000756static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000757static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000758
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000759/* Records whether tracing is on for any thread. Counts the number of
760 threads for which tstate->c_tracefunc is non-NULL, so if the value
761 is 0, we know we don't have to check this thread's c_tracefunc.
762 This speeds up the if statement in PyEval_EvalFrameEx() after
763 fast_next_opcode*/
764static int _Py_TracingPossible = 0;
765
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000766
Guido van Rossum374a9221991-04-04 10:40:29 +0000767
Guido van Rossumb209a111997-04-29 18:18:01 +0000768PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000769PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return PyEval_EvalCodeEx(co,
772 globals, locals,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 (PyObject **)NULL, 0,
776 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777}
778
779
780/* Interpreter main loop */
781
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000782PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000783PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* This is for backward compatibility with extension modules that
785 used this API; core interpreter code should call
786 PyEval_EvalFrameEx() */
787 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000788}
789
790PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000792{
Guido van Rossum950361c1997-01-24 13:49:28 +0000793#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000795#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 register PyObject **stack_pointer; /* Next free slot in value stack */
797 register unsigned char *next_instr;
798 register int opcode; /* Current opcode */
799 register int oparg; /* Current opcode argument, if any */
800 register enum why_code why; /* Reason for block stack unwind */
801 register int err; /* Error status -- nonzero if error */
802 register PyObject *x; /* Result object -- NULL if error */
803 register PyObject *v; /* Temporary objects popped off stack */
804 register PyObject *w;
805 register PyObject *u;
806 register PyObject *t;
807 register PyObject **fastlocals, **freevars;
808 PyObject *retval = NULL; /* Return value */
809 PyThreadState *tstate = PyThreadState_GET();
810 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 is true when the line being executed has changed. The
817 initial values are such as to make this false the first
818 time it is tested. */
819 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 unsigned char *first_instr;
822 PyObject *names;
823 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000824
Victor Stinner3c1e4812012-03-26 22:10:51 +0200825 _Py_IDENTIFIER(__ltrace__);
826
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,
1144 f, PyTrace_CALL, Py_None)) {
1145 /* 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,
1154 f, PyTrace_CALL, Py_None)) {
1155 /* 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 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001191 if (co->co_flags & CO_GENERATOR && !throwflag) {
1192 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1193 /* We were in an except handler when we left,
1194 restore the exception state which was put aside
1195 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001196 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001198 else
1199 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001201
Tim Peters5ca576e2001-06-18 22:08:13 +00001202#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001203 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001204#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 why = WHY_NOT;
1207 err = 0;
1208 x = Py_None; /* Not a reference, just anything non-NULL */
1209 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (throwflag) { /* support for generator.throw() */
1212 why = WHY_EXCEPTION;
1213 goto on_error;
1214 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001217#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 if (inst1 == 0) {
1219 /* Almost surely, the opcode executed a break
1220 or a continue, preventing inst1 from being set
1221 on the way out of the loop.
1222 */
1223 READ_TIMESTAMP(inst1);
1224 loop1 = inst1;
1225 }
1226 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1227 intr0, intr1);
1228 ticked = 0;
1229 inst1 = 0;
1230 intr0 = 0;
1231 intr1 = 0;
1232 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001233#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1235 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 /* Do periodic things. Doing this every time through
1238 the loop would add too much overhead, so we do it
1239 only every Nth instruction. We also do it if
1240 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1241 event needs attention (e.g. a signal handler or
1242 async I/O handler); see Py_AddPendingCall() and
1243 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1246 if (*next_instr == SETUP_FINALLY) {
1247 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001248 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 goto fast_next_opcode;
1250 }
1251 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001252#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1256 if (Py_MakePendingCalls() < 0) {
1257 why = WHY_EXCEPTION;
1258 goto on_error;
1259 }
1260 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001261#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001262 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 /* Give another thread a chance */
1264 if (PyThreadState_Swap(NULL) != tstate)
1265 Py_FatalError("ceval: tstate mix-up");
1266 drop_gil(tstate);
1267
1268 /* Other threads may run now */
1269
1270 take_gil(tstate);
1271 if (PyThreadState_Swap(tstate) != NULL)
1272 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001274#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 /* Check for asynchronous exceptions. */
1276 if (tstate->async_exc != NULL) {
1277 x = tstate->async_exc;
1278 tstate->async_exc = NULL;
1279 UNSIGNAL_ASYNC_EXC();
1280 PyErr_SetNone(x);
1281 Py_DECREF(x);
1282 why = WHY_EXCEPTION;
1283 goto on_error;
1284 }
1285 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 fast_next_opcode:
1288 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 if (_Py_TracingPossible &&
1293 tstate->c_tracefunc != NULL && !tstate->tracing) {
1294 /* see maybe_call_line_trace
1295 for expository comments */
1296 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 err = maybe_call_line_trace(tstate->c_tracefunc,
1299 tstate->c_traceobj,
1300 f, &instr_lb, &instr_ub,
1301 &instr_prev);
1302 /* Reload possibly changed frame fields */
1303 JUMPTO(f->f_lasti);
1304 if (f->f_stacktop != NULL) {
1305 stack_pointer = f->f_stacktop;
1306 f->f_stacktop = NULL;
1307 }
1308 if (err) {
1309 /* trace function raised an exception */
1310 goto on_error;
1311 }
1312 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 opcode = NEXTOP();
1317 oparg = 0; /* allows oparg to be stored in a register because
1318 it doesn't have to be remembered across a full loop */
1319 if (HAS_ARG(opcode))
1320 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001321 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001322#ifdef DYNAMIC_EXECUTION_PROFILE
1323#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 dxpairs[lastopcode][opcode]++;
1325 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001326#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001328#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001329
Guido van Rossum96a42c81992-01-12 02:29:51 +00001330#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 if (lltrace) {
1334 if (HAS_ARG(opcode)) {
1335 printf("%d: %d, %d\n",
1336 f->f_lasti, opcode, oparg);
1337 }
1338 else {
1339 printf("%d: %d\n",
1340 f->f_lasti, opcode);
1341 }
1342 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001343#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 /* Main switch on opcode */
1346 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 /* BEWARE!
1351 It is essential that any operation that fails sets either
1352 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1353 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 TARGET(NOP)
1356 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 TARGET(LOAD_FAST)
1359 x = GETLOCAL(oparg);
1360 if (x != NULL) {
1361 Py_INCREF(x);
1362 PUSH(x);
1363 FAST_DISPATCH();
1364 }
1365 format_exc_check_arg(PyExc_UnboundLocalError,
1366 UNBOUNDLOCAL_ERROR_MSG,
1367 PyTuple_GetItem(co->co_varnames, oparg));
1368 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 TARGET(LOAD_CONST)
1371 x = GETITEM(consts, oparg);
1372 Py_INCREF(x);
1373 PUSH(x);
1374 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 PREDICTED_WITH_ARG(STORE_FAST);
1377 TARGET(STORE_FAST)
1378 v = POP();
1379 SETLOCAL(oparg, v);
1380 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 TARGET(POP_TOP)
1383 v = POP();
1384 Py_DECREF(v);
1385 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 TARGET(ROT_TWO)
1388 v = TOP();
1389 w = SECOND();
1390 SET_TOP(w);
1391 SET_SECOND(v);
1392 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 TARGET(ROT_THREE)
1395 v = TOP();
1396 w = SECOND();
1397 x = THIRD();
1398 SET_TOP(w);
1399 SET_SECOND(x);
1400 SET_THIRD(v);
1401 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 TARGET(DUP_TOP)
1404 v = TOP();
1405 Py_INCREF(v);
1406 PUSH(v);
1407 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001409 TARGET(DUP_TOP_TWO)
1410 x = TOP();
1411 Py_INCREF(x);
1412 w = SECOND();
1413 Py_INCREF(w);
1414 STACKADJ(2);
1415 SET_TOP(x);
1416 SET_SECOND(w);
1417 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 TARGET(UNARY_POSITIVE)
1420 v = TOP();
1421 x = PyNumber_Positive(v);
1422 Py_DECREF(v);
1423 SET_TOP(x);
1424 if (x != NULL) DISPATCH();
1425 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 TARGET(UNARY_NEGATIVE)
1428 v = TOP();
1429 x = PyNumber_Negative(v);
1430 Py_DECREF(v);
1431 SET_TOP(x);
1432 if (x != NULL) DISPATCH();
1433 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 TARGET(UNARY_NOT)
1436 v = TOP();
1437 err = PyObject_IsTrue(v);
1438 Py_DECREF(v);
1439 if (err == 0) {
1440 Py_INCREF(Py_True);
1441 SET_TOP(Py_True);
1442 DISPATCH();
1443 }
1444 else if (err > 0) {
1445 Py_INCREF(Py_False);
1446 SET_TOP(Py_False);
1447 err = 0;
1448 DISPATCH();
1449 }
1450 STACKADJ(-1);
1451 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001453 TARGET(UNARY_INVERT)
1454 v = TOP();
1455 x = PyNumber_Invert(v);
1456 Py_DECREF(v);
1457 SET_TOP(x);
1458 if (x != NULL) DISPATCH();
1459 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001461 TARGET(BINARY_POWER)
1462 w = POP();
1463 v = TOP();
1464 x = PyNumber_Power(v, w, Py_None);
1465 Py_DECREF(v);
1466 Py_DECREF(w);
1467 SET_TOP(x);
1468 if (x != NULL) DISPATCH();
1469 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 TARGET(BINARY_MULTIPLY)
1472 w = POP();
1473 v = TOP();
1474 x = PyNumber_Multiply(v, w);
1475 Py_DECREF(v);
1476 Py_DECREF(w);
1477 SET_TOP(x);
1478 if (x != NULL) DISPATCH();
1479 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 TARGET(BINARY_TRUE_DIVIDE)
1482 w = POP();
1483 v = TOP();
1484 x = PyNumber_TrueDivide(v, w);
1485 Py_DECREF(v);
1486 Py_DECREF(w);
1487 SET_TOP(x);
1488 if (x != NULL) DISPATCH();
1489 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 TARGET(BINARY_FLOOR_DIVIDE)
1492 w = POP();
1493 v = TOP();
1494 x = PyNumber_FloorDivide(v, w);
1495 Py_DECREF(v);
1496 Py_DECREF(w);
1497 SET_TOP(x);
1498 if (x != NULL) DISPATCH();
1499 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001501 TARGET(BINARY_MODULO)
1502 w = POP();
1503 v = TOP();
1504 if (PyUnicode_CheckExact(v))
1505 x = PyUnicode_Format(v, w);
1506 else
1507 x = PyNumber_Remainder(v, w);
1508 Py_DECREF(v);
1509 Py_DECREF(w);
1510 SET_TOP(x);
1511 if (x != NULL) DISPATCH();
1512 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001514 TARGET(BINARY_ADD)
1515 w = POP();
1516 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001517 if (PyUnicode_CheckExact(v) &&
1518 PyUnicode_CheckExact(w)) {
1519 x = unicode_concatenate(v, w, f, next_instr);
1520 /* unicode_concatenate consumed the ref to v */
1521 goto skip_decref_vx;
1522 }
1523 else {
1524 x = PyNumber_Add(v, w);
1525 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001527 skip_decref_vx:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 Py_DECREF(w);
1529 SET_TOP(x);
1530 if (x != NULL) DISPATCH();
1531 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 TARGET(BINARY_SUBTRACT)
1534 w = POP();
1535 v = TOP();
1536 x = PyNumber_Subtract(v, w);
1537 Py_DECREF(v);
1538 Py_DECREF(w);
1539 SET_TOP(x);
1540 if (x != NULL) DISPATCH();
1541 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 TARGET(BINARY_SUBSCR)
1544 w = POP();
1545 v = TOP();
1546 x = PyObject_GetItem(v, w);
1547 Py_DECREF(v);
1548 Py_DECREF(w);
1549 SET_TOP(x);
1550 if (x != NULL) DISPATCH();
1551 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 TARGET(BINARY_LSHIFT)
1554 w = POP();
1555 v = TOP();
1556 x = PyNumber_Lshift(v, w);
1557 Py_DECREF(v);
1558 Py_DECREF(w);
1559 SET_TOP(x);
1560 if (x != NULL) DISPATCH();
1561 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 TARGET(BINARY_RSHIFT)
1564 w = POP();
1565 v = TOP();
1566 x = PyNumber_Rshift(v, w);
1567 Py_DECREF(v);
1568 Py_DECREF(w);
1569 SET_TOP(x);
1570 if (x != NULL) DISPATCH();
1571 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 TARGET(BINARY_AND)
1574 w = POP();
1575 v = TOP();
1576 x = PyNumber_And(v, w);
1577 Py_DECREF(v);
1578 Py_DECREF(w);
1579 SET_TOP(x);
1580 if (x != NULL) DISPATCH();
1581 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 TARGET(BINARY_XOR)
1584 w = POP();
1585 v = TOP();
1586 x = PyNumber_Xor(v, w);
1587 Py_DECREF(v);
1588 Py_DECREF(w);
1589 SET_TOP(x);
1590 if (x != NULL) DISPATCH();
1591 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 TARGET(BINARY_OR)
1594 w = POP();
1595 v = TOP();
1596 x = PyNumber_Or(v, w);
1597 Py_DECREF(v);
1598 Py_DECREF(w);
1599 SET_TOP(x);
1600 if (x != NULL) DISPATCH();
1601 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 TARGET(LIST_APPEND)
1604 w = POP();
1605 v = PEEK(oparg);
1606 err = PyList_Append(v, w);
1607 Py_DECREF(w);
1608 if (err == 0) {
1609 PREDICT(JUMP_ABSOLUTE);
1610 DISPATCH();
1611 }
1612 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 TARGET(SET_ADD)
1615 w = POP();
1616 v = stack_pointer[-oparg];
1617 err = PySet_Add(v, w);
1618 Py_DECREF(w);
1619 if (err == 0) {
1620 PREDICT(JUMP_ABSOLUTE);
1621 DISPATCH();
1622 }
1623 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001624
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 TARGET(INPLACE_POWER)
1626 w = POP();
1627 v = TOP();
1628 x = PyNumber_InPlacePower(v, w, Py_None);
1629 Py_DECREF(v);
1630 Py_DECREF(w);
1631 SET_TOP(x);
1632 if (x != NULL) DISPATCH();
1633 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 TARGET(INPLACE_MULTIPLY)
1636 w = POP();
1637 v = TOP();
1638 x = PyNumber_InPlaceMultiply(v, w);
1639 Py_DECREF(v);
1640 Py_DECREF(w);
1641 SET_TOP(x);
1642 if (x != NULL) DISPATCH();
1643 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 TARGET(INPLACE_TRUE_DIVIDE)
1646 w = POP();
1647 v = TOP();
1648 x = PyNumber_InPlaceTrueDivide(v, w);
1649 Py_DECREF(v);
1650 Py_DECREF(w);
1651 SET_TOP(x);
1652 if (x != NULL) DISPATCH();
1653 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 TARGET(INPLACE_FLOOR_DIVIDE)
1656 w = POP();
1657 v = TOP();
1658 x = PyNumber_InPlaceFloorDivide(v, w);
1659 Py_DECREF(v);
1660 Py_DECREF(w);
1661 SET_TOP(x);
1662 if (x != NULL) DISPATCH();
1663 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 TARGET(INPLACE_MODULO)
1666 w = POP();
1667 v = TOP();
1668 x = PyNumber_InPlaceRemainder(v, w);
1669 Py_DECREF(v);
1670 Py_DECREF(w);
1671 SET_TOP(x);
1672 if (x != NULL) DISPATCH();
1673 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 TARGET(INPLACE_ADD)
1676 w = POP();
1677 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001678 if (PyUnicode_CheckExact(v) &&
1679 PyUnicode_CheckExact(w)) {
1680 x = unicode_concatenate(v, w, f, next_instr);
1681 /* unicode_concatenate consumed the ref to v */
1682 goto skip_decref_v;
1683 }
1684 else {
1685 x = PyNumber_InPlaceAdd(v, w);
1686 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001688 skip_decref_v:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 Py_DECREF(w);
1690 SET_TOP(x);
1691 if (x != NULL) DISPATCH();
1692 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 TARGET(INPLACE_SUBTRACT)
1695 w = POP();
1696 v = TOP();
1697 x = PyNumber_InPlaceSubtract(v, w);
1698 Py_DECREF(v);
1699 Py_DECREF(w);
1700 SET_TOP(x);
1701 if (x != NULL) DISPATCH();
1702 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 TARGET(INPLACE_LSHIFT)
1705 w = POP();
1706 v = TOP();
1707 x = PyNumber_InPlaceLshift(v, w);
1708 Py_DECREF(v);
1709 Py_DECREF(w);
1710 SET_TOP(x);
1711 if (x != NULL) DISPATCH();
1712 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 TARGET(INPLACE_RSHIFT)
1715 w = POP();
1716 v = TOP();
1717 x = PyNumber_InPlaceRshift(v, w);
1718 Py_DECREF(v);
1719 Py_DECREF(w);
1720 SET_TOP(x);
1721 if (x != NULL) DISPATCH();
1722 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 TARGET(INPLACE_AND)
1725 w = POP();
1726 v = TOP();
1727 x = PyNumber_InPlaceAnd(v, w);
1728 Py_DECREF(v);
1729 Py_DECREF(w);
1730 SET_TOP(x);
1731 if (x != NULL) DISPATCH();
1732 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001734 TARGET(INPLACE_XOR)
1735 w = POP();
1736 v = TOP();
1737 x = PyNumber_InPlaceXor(v, w);
1738 Py_DECREF(v);
1739 Py_DECREF(w);
1740 SET_TOP(x);
1741 if (x != NULL) DISPATCH();
1742 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001744 TARGET(INPLACE_OR)
1745 w = POP();
1746 v = TOP();
1747 x = PyNumber_InPlaceOr(v, w);
1748 Py_DECREF(v);
1749 Py_DECREF(w);
1750 SET_TOP(x);
1751 if (x != NULL) DISPATCH();
1752 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 TARGET(STORE_SUBSCR)
1755 w = TOP();
1756 v = SECOND();
1757 u = THIRD();
1758 STACKADJ(-3);
1759 /* v[w] = u */
1760 err = PyObject_SetItem(v, w, u);
1761 Py_DECREF(u);
1762 Py_DECREF(v);
1763 Py_DECREF(w);
1764 if (err == 0) DISPATCH();
1765 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 TARGET(DELETE_SUBSCR)
1768 w = TOP();
1769 v = SECOND();
1770 STACKADJ(-2);
1771 /* del v[w] */
1772 err = PyObject_DelItem(v, w);
1773 Py_DECREF(v);
1774 Py_DECREF(w);
1775 if (err == 0) DISPATCH();
1776 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 TARGET(PRINT_EXPR)
1779 v = POP();
1780 w = PySys_GetObject("displayhook");
1781 if (w == NULL) {
1782 PyErr_SetString(PyExc_RuntimeError,
1783 "lost sys.displayhook");
1784 err = -1;
1785 x = NULL;
1786 }
1787 if (err == 0) {
1788 x = PyTuple_Pack(1, v);
1789 if (x == NULL)
1790 err = -1;
1791 }
1792 if (err == 0) {
1793 w = PyEval_CallObject(w, x);
1794 Py_XDECREF(w);
1795 if (w == NULL)
1796 err = -1;
1797 }
1798 Py_DECREF(v);
1799 Py_XDECREF(x);
1800 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001801
Thomas Wouters434d0822000-08-24 20:11:32 +00001802#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001804#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 TARGET(RAISE_VARARGS)
1806 v = w = NULL;
1807 switch (oparg) {
1808 case 2:
1809 v = POP(); /* cause */
1810 case 1:
1811 w = POP(); /* exc */
1812 case 0: /* Fallthrough */
1813 why = do_raise(w, v);
1814 break;
1815 default:
1816 PyErr_SetString(PyExc_SystemError,
1817 "bad RAISE_VARARGS oparg");
1818 why = WHY_EXCEPTION;
1819 break;
1820 }
1821 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 TARGET(STORE_LOCALS)
1824 x = POP();
1825 v = f->f_locals;
1826 Py_XDECREF(v);
1827 f->f_locals = x;
1828 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 TARGET(RETURN_VALUE)
1831 retval = POP();
1832 why = WHY_RETURN;
1833 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001834
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001835 TARGET(YIELD_FROM)
1836 u = POP();
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001837 x = TOP();
1838 /* send u to x */
1839 if (PyGen_CheckExact(x)) {
1840 retval = _PyGen_Send((PyGenObject *)x, u);
1841 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001842 _Py_IDENTIFIER(send);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001843 if (u == Py_None)
1844 retval = PyIter_Next(x);
1845 else
Benjamin Peterson302e7902012-03-20 23:17:04 -04001846 retval = _PyObject_CallMethodId(x, &PyId_send, "O", u);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001847 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001848 Py_DECREF(u);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001849 if (!retval) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001850 PyObject *val;
1851 x = POP(); /* Remove iter from stack */
1852 Py_DECREF(x);
1853 err = PyGen_FetchStopIterationValue(&val);
1854 if (err < 0) {
1855 x = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001856 break;
1857 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001858 x = val;
1859 PUSH(x);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001860 continue;
1861 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001862 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001863 f->f_stacktop = stack_pointer;
1864 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001865 /* and repeat... */
1866 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001867 goto fast_yield;
1868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 TARGET(YIELD_VALUE)
1870 retval = POP();
1871 f->f_stacktop = stack_pointer;
1872 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 TARGET(POP_EXCEPT)
1876 {
1877 PyTryBlock *b = PyFrame_BlockPop(f);
1878 if (b->b_type != EXCEPT_HANDLER) {
1879 PyErr_SetString(PyExc_SystemError,
1880 "popped block is not an except handler");
1881 why = WHY_EXCEPTION;
1882 break;
1883 }
1884 UNWIND_EXCEPT_HANDLER(b);
1885 }
1886 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 TARGET(POP_BLOCK)
1889 {
1890 PyTryBlock *b = PyFrame_BlockPop(f);
1891 UNWIND_BLOCK(b);
1892 }
1893 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001895 PREDICTED(END_FINALLY);
1896 TARGET(END_FINALLY)
1897 v = POP();
1898 if (PyLong_Check(v)) {
1899 why = (enum why_code) PyLong_AS_LONG(v);
1900 assert(why != WHY_YIELD);
1901 if (why == WHY_RETURN ||
1902 why == WHY_CONTINUE)
1903 retval = POP();
1904 if (why == WHY_SILENCED) {
1905 /* An exception was silenced by 'with', we must
1906 manually unwind the EXCEPT_HANDLER block which was
1907 created when the exception was caught, otherwise
1908 the stack will be in an inconsistent state. */
1909 PyTryBlock *b = PyFrame_BlockPop(f);
1910 assert(b->b_type == EXCEPT_HANDLER);
1911 UNWIND_EXCEPT_HANDLER(b);
1912 why = WHY_NOT;
1913 }
1914 }
1915 else if (PyExceptionClass_Check(v)) {
1916 w = POP();
1917 u = POP();
1918 PyErr_Restore(v, w, u);
1919 why = WHY_RERAISE;
1920 break;
1921 }
1922 else if (v != Py_None) {
1923 PyErr_SetString(PyExc_SystemError,
1924 "'finally' pops bad exception");
1925 why = WHY_EXCEPTION;
1926 }
1927 Py_DECREF(v);
1928 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 TARGET(LOAD_BUILD_CLASS)
Victor Stinner3c1e4812012-03-26 22:10:51 +02001931 {
1932 _Py_IDENTIFIER(__build_class__);
1933 x = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 if (x == NULL) {
1935 PyErr_SetString(PyExc_ImportError,
1936 "__build_class__ not found");
1937 break;
1938 }
1939 Py_INCREF(x);
1940 PUSH(x);
1941 break;
Victor Stinner3c1e4812012-03-26 22:10:51 +02001942 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 TARGET(STORE_NAME)
1945 w = GETITEM(names, oparg);
1946 v = POP();
1947 if ((x = f->f_locals) != NULL) {
1948 if (PyDict_CheckExact(x))
1949 err = PyDict_SetItem(x, w, v);
1950 else
1951 err = PyObject_SetItem(x, w, v);
1952 Py_DECREF(v);
1953 if (err == 0) DISPATCH();
1954 break;
1955 }
1956 PyErr_Format(PyExc_SystemError,
1957 "no locals found when storing %R", w);
1958 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001959
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001960 TARGET(DELETE_NAME)
1961 w = GETITEM(names, oparg);
1962 if ((x = f->f_locals) != NULL) {
1963 if ((err = PyObject_DelItem(x, w)) != 0)
1964 format_exc_check_arg(PyExc_NameError,
1965 NAME_ERROR_MSG,
1966 w);
1967 break;
1968 }
1969 PyErr_Format(PyExc_SystemError,
1970 "no locals when deleting %R", w);
1971 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1974 TARGET(UNPACK_SEQUENCE)
1975 v = POP();
1976 if (PyTuple_CheckExact(v) &&
1977 PyTuple_GET_SIZE(v) == oparg) {
1978 PyObject **items = \
1979 ((PyTupleObject *)v)->ob_item;
1980 while (oparg--) {
1981 w = items[oparg];
1982 Py_INCREF(w);
1983 PUSH(w);
1984 }
1985 Py_DECREF(v);
1986 DISPATCH();
1987 } else if (PyList_CheckExact(v) &&
1988 PyList_GET_SIZE(v) == oparg) {
1989 PyObject **items = \
1990 ((PyListObject *)v)->ob_item;
1991 while (oparg--) {
1992 w = items[oparg];
1993 Py_INCREF(w);
1994 PUSH(w);
1995 }
1996 } else if (unpack_iterable(v, oparg, -1,
1997 stack_pointer + oparg)) {
1998 STACKADJ(oparg);
1999 } else {
2000 /* unpack_iterable() raised an exception */
2001 why = WHY_EXCEPTION;
2002 }
2003 Py_DECREF(v);
2004 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 TARGET(UNPACK_EX)
2007 {
2008 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2009 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2012 stack_pointer + totalargs)) {
2013 stack_pointer += totalargs;
2014 } else {
2015 why = WHY_EXCEPTION;
2016 }
2017 Py_DECREF(v);
2018 break;
2019 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 TARGET(STORE_ATTR)
2022 w = GETITEM(names, oparg);
2023 v = TOP();
2024 u = SECOND();
2025 STACKADJ(-2);
2026 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2027 Py_DECREF(v);
2028 Py_DECREF(u);
2029 if (err == 0) DISPATCH();
2030 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002032 TARGET(DELETE_ATTR)
2033 w = GETITEM(names, oparg);
2034 v = POP();
2035 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2036 /* del v.w */
2037 Py_DECREF(v);
2038 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002040 TARGET(STORE_GLOBAL)
2041 w = GETITEM(names, oparg);
2042 v = POP();
2043 err = PyDict_SetItem(f->f_globals, w, v);
2044 Py_DECREF(v);
2045 if (err == 0) DISPATCH();
2046 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 TARGET(DELETE_GLOBAL)
2049 w = GETITEM(names, oparg);
2050 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2051 format_exc_check_arg(
2052 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2053 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 TARGET(LOAD_NAME)
2056 w = GETITEM(names, oparg);
2057 if ((v = f->f_locals) == NULL) {
2058 PyErr_Format(PyExc_SystemError,
2059 "no locals when loading %R", w);
2060 why = WHY_EXCEPTION;
2061 break;
2062 }
2063 if (PyDict_CheckExact(v)) {
2064 x = PyDict_GetItem(v, w);
2065 Py_XINCREF(x);
2066 }
2067 else {
2068 x = PyObject_GetItem(v, w);
2069 if (x == NULL && PyErr_Occurred()) {
2070 if (!PyErr_ExceptionMatches(
2071 PyExc_KeyError))
2072 break;
2073 PyErr_Clear();
2074 }
2075 }
2076 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002077 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002079 x = PyDict_GetItem(f->f_builtins, w);
2080 if (x == NULL) {
2081 format_exc_check_arg(
2082 PyExc_NameError,
2083 NAME_ERROR_MSG, w);
2084 break;
2085 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 }
2087 Py_INCREF(x);
2088 }
2089 PUSH(x);
2090 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 TARGET(LOAD_GLOBAL)
2093 w = GETITEM(names, oparg);
2094 if (PyUnicode_CheckExact(w)) {
2095 /* Inline the PyDict_GetItem() calls.
2096 WARNING: this is an extreme speed hack.
2097 Do not try this at home. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002098 Py_hash_t hash = ((PyASCIIObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 if (hash != -1) {
2100 PyDictObject *d;
2101 PyDictEntry *e;
2102 d = (PyDictObject *)(f->f_globals);
2103 e = d->ma_lookup(d, w, hash);
2104 if (e == NULL) {
2105 x = NULL;
2106 break;
2107 }
2108 x = e->me_value;
2109 if (x != NULL) {
2110 Py_INCREF(x);
2111 PUSH(x);
2112 DISPATCH();
2113 }
2114 d = (PyDictObject *)(f->f_builtins);
2115 e = d->ma_lookup(d, w, hash);
2116 if (e == NULL) {
2117 x = NULL;
2118 break;
2119 }
2120 x = e->me_value;
2121 if (x != NULL) {
2122 Py_INCREF(x);
2123 PUSH(x);
2124 DISPATCH();
2125 }
2126 goto load_global_error;
2127 }
2128 }
2129 /* This is the un-inlined version of the code above */
2130 x = PyDict_GetItem(f->f_globals, w);
2131 if (x == NULL) {
2132 x = PyDict_GetItem(f->f_builtins, w);
2133 if (x == NULL) {
2134 load_global_error:
2135 format_exc_check_arg(
2136 PyExc_NameError,
2137 GLOBAL_NAME_ERROR_MSG, w);
2138 break;
2139 }
2140 }
2141 Py_INCREF(x);
2142 PUSH(x);
2143 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 TARGET(DELETE_FAST)
2146 x = GETLOCAL(oparg);
2147 if (x != NULL) {
2148 SETLOCAL(oparg, NULL);
2149 DISPATCH();
2150 }
2151 format_exc_check_arg(
2152 PyExc_UnboundLocalError,
2153 UNBOUNDLOCAL_ERROR_MSG,
2154 PyTuple_GetItem(co->co_varnames, oparg)
2155 );
2156 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002157
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002158 TARGET(DELETE_DEREF)
2159 x = freevars[oparg];
2160 if (PyCell_GET(x) != NULL) {
2161 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002162 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002163 }
2164 err = -1;
2165 format_exc_unbound(co, oparg);
2166 break;
2167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 TARGET(LOAD_CLOSURE)
2169 x = freevars[oparg];
2170 Py_INCREF(x);
2171 PUSH(x);
2172 if (x != NULL) DISPATCH();
2173 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 TARGET(LOAD_DEREF)
2176 x = freevars[oparg];
2177 w = PyCell_Get(x);
2178 if (w != NULL) {
2179 PUSH(w);
2180 DISPATCH();
2181 }
2182 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002183 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002186 TARGET(STORE_DEREF)
2187 w = POP();
2188 x = freevars[oparg];
2189 PyCell_Set(x, w);
2190 Py_DECREF(w);
2191 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 TARGET(BUILD_TUPLE)
2194 x = PyTuple_New(oparg);
2195 if (x != NULL) {
2196 for (; --oparg >= 0;) {
2197 w = POP();
2198 PyTuple_SET_ITEM(x, oparg, w);
2199 }
2200 PUSH(x);
2201 DISPATCH();
2202 }
2203 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002204
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 TARGET(BUILD_LIST)
2206 x = PyList_New(oparg);
2207 if (x != NULL) {
2208 for (; --oparg >= 0;) {
2209 w = POP();
2210 PyList_SET_ITEM(x, oparg, w);
2211 }
2212 PUSH(x);
2213 DISPATCH();
2214 }
2215 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002217 TARGET(BUILD_SET)
2218 x = PySet_New(NULL);
2219 if (x != NULL) {
2220 for (; --oparg >= 0;) {
2221 w = POP();
2222 if (err == 0)
2223 err = PySet_Add(x, w);
2224 Py_DECREF(w);
2225 }
2226 if (err != 0) {
2227 Py_DECREF(x);
2228 break;
2229 }
2230 PUSH(x);
2231 DISPATCH();
2232 }
2233 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002235 TARGET(BUILD_MAP)
2236 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2237 PUSH(x);
2238 if (x != NULL) DISPATCH();
2239 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 TARGET(STORE_MAP)
2242 w = TOP(); /* key */
2243 u = SECOND(); /* value */
2244 v = THIRD(); /* dict */
2245 STACKADJ(-2);
2246 assert (PyDict_CheckExact(v));
2247 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2248 Py_DECREF(u);
2249 Py_DECREF(w);
2250 if (err == 0) DISPATCH();
2251 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002252
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 TARGET(MAP_ADD)
2254 w = TOP(); /* key */
2255 u = SECOND(); /* value */
2256 STACKADJ(-2);
2257 v = stack_pointer[-oparg]; /* dict */
2258 assert (PyDict_CheckExact(v));
2259 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2260 Py_DECREF(u);
2261 Py_DECREF(w);
2262 if (err == 0) {
2263 PREDICT(JUMP_ABSOLUTE);
2264 DISPATCH();
2265 }
2266 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002268 TARGET(LOAD_ATTR)
2269 w = GETITEM(names, oparg);
2270 v = TOP();
2271 x = PyObject_GetAttr(v, w);
2272 Py_DECREF(v);
2273 SET_TOP(x);
2274 if (x != NULL) DISPATCH();
2275 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 TARGET(COMPARE_OP)
2278 w = POP();
2279 v = TOP();
2280 x = cmp_outcome(oparg, v, w);
2281 Py_DECREF(v);
2282 Py_DECREF(w);
2283 SET_TOP(x);
2284 if (x == NULL) break;
2285 PREDICT(POP_JUMP_IF_FALSE);
2286 PREDICT(POP_JUMP_IF_TRUE);
2287 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002289 TARGET(IMPORT_NAME)
Victor Stinner3c1e4812012-03-26 22:10:51 +02002290 {
2291 _Py_IDENTIFIER(__import__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 w = GETITEM(names, oparg);
Victor Stinner3c1e4812012-03-26 22:10:51 +02002293 x = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 if (x == NULL) {
2295 PyErr_SetString(PyExc_ImportError,
2296 "__import__ not found");
2297 break;
2298 }
2299 Py_INCREF(x);
2300 v = POP();
2301 u = TOP();
2302 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2303 w = PyTuple_Pack(5,
2304 w,
2305 f->f_globals,
2306 f->f_locals == NULL ?
2307 Py_None : f->f_locals,
2308 v,
2309 u);
2310 else
2311 w = PyTuple_Pack(4,
2312 w,
2313 f->f_globals,
2314 f->f_locals == NULL ?
2315 Py_None : f->f_locals,
2316 v);
2317 Py_DECREF(v);
2318 Py_DECREF(u);
2319 if (w == NULL) {
2320 u = POP();
2321 Py_DECREF(x);
2322 x = NULL;
2323 break;
2324 }
2325 READ_TIMESTAMP(intr0);
2326 v = x;
2327 x = PyEval_CallObject(v, w);
2328 Py_DECREF(v);
2329 READ_TIMESTAMP(intr1);
2330 Py_DECREF(w);
2331 SET_TOP(x);
2332 if (x != NULL) DISPATCH();
2333 break;
Victor Stinner3c1e4812012-03-26 22:10:51 +02002334 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002336 TARGET(IMPORT_STAR)
2337 v = POP();
2338 PyFrame_FastToLocals(f);
2339 if ((x = f->f_locals) == NULL) {
2340 PyErr_SetString(PyExc_SystemError,
2341 "no locals found during 'import *'");
2342 break;
2343 }
2344 READ_TIMESTAMP(intr0);
2345 err = import_all_from(x, v);
2346 READ_TIMESTAMP(intr1);
2347 PyFrame_LocalsToFast(f, 0);
2348 Py_DECREF(v);
2349 if (err == 0) DISPATCH();
2350 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 TARGET(IMPORT_FROM)
2353 w = GETITEM(names, oparg);
2354 v = TOP();
2355 READ_TIMESTAMP(intr0);
2356 x = import_from(v, w);
2357 READ_TIMESTAMP(intr1);
2358 PUSH(x);
2359 if (x != NULL) DISPATCH();
2360 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 TARGET(JUMP_FORWARD)
2363 JUMPBY(oparg);
2364 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2367 TARGET(POP_JUMP_IF_FALSE)
2368 w = POP();
2369 if (w == Py_True) {
2370 Py_DECREF(w);
2371 FAST_DISPATCH();
2372 }
2373 if (w == Py_False) {
2374 Py_DECREF(w);
2375 JUMPTO(oparg);
2376 FAST_DISPATCH();
2377 }
2378 err = PyObject_IsTrue(w);
2379 Py_DECREF(w);
2380 if (err > 0)
2381 err = 0;
2382 else if (err == 0)
2383 JUMPTO(oparg);
2384 else
2385 break;
2386 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2389 TARGET(POP_JUMP_IF_TRUE)
2390 w = POP();
2391 if (w == Py_False) {
2392 Py_DECREF(w);
2393 FAST_DISPATCH();
2394 }
2395 if (w == Py_True) {
2396 Py_DECREF(w);
2397 JUMPTO(oparg);
2398 FAST_DISPATCH();
2399 }
2400 err = PyObject_IsTrue(w);
2401 Py_DECREF(w);
2402 if (err > 0) {
2403 err = 0;
2404 JUMPTO(oparg);
2405 }
2406 else if (err == 0)
2407 ;
2408 else
2409 break;
2410 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002412 TARGET(JUMP_IF_FALSE_OR_POP)
2413 w = TOP();
2414 if (w == Py_True) {
2415 STACKADJ(-1);
2416 Py_DECREF(w);
2417 FAST_DISPATCH();
2418 }
2419 if (w == Py_False) {
2420 JUMPTO(oparg);
2421 FAST_DISPATCH();
2422 }
2423 err = PyObject_IsTrue(w);
2424 if (err > 0) {
2425 STACKADJ(-1);
2426 Py_DECREF(w);
2427 err = 0;
2428 }
2429 else if (err == 0)
2430 JUMPTO(oparg);
2431 else
2432 break;
2433 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002435 TARGET(JUMP_IF_TRUE_OR_POP)
2436 w = TOP();
2437 if (w == Py_False) {
2438 STACKADJ(-1);
2439 Py_DECREF(w);
2440 FAST_DISPATCH();
2441 }
2442 if (w == Py_True) {
2443 JUMPTO(oparg);
2444 FAST_DISPATCH();
2445 }
2446 err = PyObject_IsTrue(w);
2447 if (err > 0) {
2448 err = 0;
2449 JUMPTO(oparg);
2450 }
2451 else if (err == 0) {
2452 STACKADJ(-1);
2453 Py_DECREF(w);
2454 }
2455 else
2456 break;
2457 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002459 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2460 TARGET(JUMP_ABSOLUTE)
2461 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002462#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 /* Enabling this path speeds-up all while and for-loops by bypassing
2464 the per-loop checks for signals. By default, this should be turned-off
2465 because it prevents detection of a control-break in tight loops like
2466 "while 1: pass". Compile with this option turned-on when you need
2467 the speed-up and do not need break checking inside tight loops (ones
2468 that contain only instructions ending with FAST_DISPATCH).
2469 */
2470 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002471#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002472 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002473#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 TARGET(GET_ITER)
2476 /* before: [obj]; after [getiter(obj)] */
2477 v = TOP();
2478 x = PyObject_GetIter(v);
2479 Py_DECREF(v);
2480 if (x != NULL) {
2481 SET_TOP(x);
2482 PREDICT(FOR_ITER);
2483 DISPATCH();
2484 }
2485 STACKADJ(-1);
2486 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002488 PREDICTED_WITH_ARG(FOR_ITER);
2489 TARGET(FOR_ITER)
2490 /* before: [iter]; after: [iter, iter()] *or* [] */
2491 v = TOP();
2492 x = (*v->ob_type->tp_iternext)(v);
2493 if (x != NULL) {
2494 PUSH(x);
2495 PREDICT(STORE_FAST);
2496 PREDICT(UNPACK_SEQUENCE);
2497 DISPATCH();
2498 }
2499 if (PyErr_Occurred()) {
2500 if (!PyErr_ExceptionMatches(
2501 PyExc_StopIteration))
2502 break;
2503 PyErr_Clear();
2504 }
2505 /* iterator ended normally */
2506 x = v = POP();
2507 Py_DECREF(v);
2508 JUMPBY(oparg);
2509 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 TARGET(BREAK_LOOP)
2512 why = WHY_BREAK;
2513 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 TARGET(CONTINUE_LOOP)
2516 retval = PyLong_FromLong(oparg);
2517 if (!retval) {
2518 x = NULL;
2519 break;
2520 }
2521 why = WHY_CONTINUE;
2522 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2525 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2526 TARGET(SETUP_FINALLY)
2527 _setup_finally:
2528 /* NOTE: If you add any new block-setup opcodes that
2529 are not try/except/finally handlers, you may need
2530 to update the PyGen_NeedsFinalizing() function.
2531 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2534 STACK_LEVEL());
2535 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 TARGET(SETUP_WITH)
2538 {
Benjamin Petersonce798522012-01-22 11:24:29 -05002539 _Py_IDENTIFIER(__exit__);
2540 _Py_IDENTIFIER(__enter__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 w = TOP();
Benjamin Petersonce798522012-01-22 11:24:29 -05002542 x = special_lookup(w, &PyId___exit__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 if (!x)
2544 break;
2545 SET_TOP(x);
Benjamin Petersonce798522012-01-22 11:24:29 -05002546 u = special_lookup(w, &PyId___enter__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 Py_DECREF(w);
2548 if (!u) {
2549 x = NULL;
2550 break;
2551 }
2552 x = PyObject_CallFunctionObjArgs(u, NULL);
2553 Py_DECREF(u);
2554 if (!x)
2555 break;
2556 /* Setup the finally block before pushing the result
2557 of __enter__ on the stack. */
2558 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2559 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 PUSH(x);
2562 DISPATCH();
2563 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 TARGET(WITH_CLEANUP)
2566 {
2567 /* At the top of the stack are 1-3 values indicating
2568 how/why we entered the finally clause:
2569 - TOP = None
2570 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2571 - TOP = WHY_*; no retval below it
2572 - (TOP, SECOND, THIRD) = exc_info()
2573 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2574 Below them is EXIT, the context.__exit__ bound method.
2575 In the last case, we must call
2576 EXIT(TOP, SECOND, THIRD)
2577 otherwise we must call
2578 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 In the first two cases, we remove EXIT from the
2581 stack, leaving the rest in the same order. In the
2582 third case, we shift the bottom 3 values of the
2583 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 In addition, if the stack represents an exception,
2586 *and* the function call returns a 'true' value, we
2587 push WHY_SILENCED onto the stack. END_FINALLY will
2588 then not re-raise the exception. (But non-local
2589 gotos should still be resumed.)
2590 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002592 PyObject *exit_func;
2593 u = TOP();
2594 if (u == Py_None) {
2595 (void)POP();
2596 exit_func = TOP();
2597 SET_TOP(u);
2598 v = w = Py_None;
2599 }
2600 else if (PyLong_Check(u)) {
2601 (void)POP();
2602 switch(PyLong_AsLong(u)) {
2603 case WHY_RETURN:
2604 case WHY_CONTINUE:
2605 /* Retval in TOP. */
2606 exit_func = SECOND();
2607 SET_SECOND(TOP());
2608 SET_TOP(u);
2609 break;
2610 default:
2611 exit_func = TOP();
2612 SET_TOP(u);
2613 break;
2614 }
2615 u = v = w = Py_None;
2616 }
2617 else {
2618 PyObject *tp, *exc, *tb;
2619 PyTryBlock *block;
2620 v = SECOND();
2621 w = THIRD();
2622 tp = FOURTH();
2623 exc = PEEK(5);
2624 tb = PEEK(6);
2625 exit_func = PEEK(7);
2626 SET_VALUE(7, tb);
2627 SET_VALUE(6, exc);
2628 SET_VALUE(5, tp);
2629 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2630 SET_FOURTH(NULL);
2631 /* We just shifted the stack down, so we have
2632 to tell the except handler block that the
2633 values are lower than it expects. */
2634 block = &f->f_blockstack[f->f_iblock - 1];
2635 assert(block->b_type == EXCEPT_HANDLER);
2636 block->b_level--;
2637 }
2638 /* XXX Not the fastest way to call it... */
2639 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2640 NULL);
2641 Py_DECREF(exit_func);
2642 if (x == NULL)
2643 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002644
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 if (u != Py_None)
2646 err = PyObject_IsTrue(x);
2647 else
2648 err = 0;
2649 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 if (err < 0)
2652 break; /* Go to error exit */
2653 else if (err > 0) {
2654 err = 0;
2655 /* There was an exception and a True return */
2656 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2657 }
2658 PREDICT(END_FINALLY);
2659 break;
2660 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 TARGET(CALL_FUNCTION)
2663 {
2664 PyObject **sp;
2665 PCALL(PCALL_ALL);
2666 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002667#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002669#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002671#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 stack_pointer = sp;
2673 PUSH(x);
2674 if (x != NULL)
2675 DISPATCH();
2676 break;
2677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2680 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2681 TARGET(CALL_FUNCTION_VAR_KW)
2682 _call_function_var_kw:
2683 {
2684 int na = oparg & 0xff;
2685 int nk = (oparg>>8) & 0xff;
2686 int flags = (opcode - CALL_FUNCTION) & 3;
2687 int n = na + 2 * nk;
2688 PyObject **pfunc, *func, **sp;
2689 PCALL(PCALL_ALL);
2690 if (flags & CALL_FLAG_VAR)
2691 n++;
2692 if (flags & CALL_FLAG_KW)
2693 n++;
2694 pfunc = stack_pointer - n - 1;
2695 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002698 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 PyObject *self = PyMethod_GET_SELF(func);
2700 Py_INCREF(self);
2701 func = PyMethod_GET_FUNCTION(func);
2702 Py_INCREF(func);
2703 Py_DECREF(*pfunc);
2704 *pfunc = self;
2705 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002706 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002707 } else
2708 Py_INCREF(func);
2709 sp = stack_pointer;
2710 READ_TIMESTAMP(intr0);
2711 x = ext_do_call(func, &sp, flags, na, nk);
2712 READ_TIMESTAMP(intr1);
2713 stack_pointer = sp;
2714 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 while (stack_pointer > pfunc) {
2717 w = POP();
2718 Py_DECREF(w);
2719 }
2720 PUSH(x);
2721 if (x != NULL)
2722 DISPATCH();
2723 break;
2724 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002725
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2727 TARGET(MAKE_FUNCTION)
2728 _make_function:
2729 {
2730 int posdefaults = oparg & 0xff;
2731 int kwdefaults = (oparg>>8) & 0xff;
2732 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002733
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002734 w = POP(); /* qualname */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 v = POP(); /* code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002736 x = PyFunction_NewWithQualName(v, f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 Py_DECREF(v);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002738 Py_DECREF(w);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 if (x != NULL && opcode == MAKE_CLOSURE) {
2741 v = POP();
2742 if (PyFunction_SetClosure(x, v) != 0) {
2743 /* Can't happen unless bytecode is corrupt. */
2744 why = WHY_EXCEPTION;
2745 }
2746 Py_DECREF(v);
2747 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 if (x != NULL && num_annotations > 0) {
2750 Py_ssize_t name_ix;
2751 u = POP(); /* names of args with annotations */
2752 v = PyDict_New();
2753 if (v == NULL) {
2754 Py_DECREF(x);
2755 x = NULL;
2756 break;
2757 }
2758 name_ix = PyTuple_Size(u);
2759 assert(num_annotations == name_ix+1);
2760 while (name_ix > 0) {
2761 --name_ix;
2762 t = PyTuple_GET_ITEM(u, name_ix);
2763 w = POP();
2764 /* XXX(nnorwitz): check for errors */
2765 PyDict_SetItem(v, t, w);
2766 Py_DECREF(w);
2767 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 if (PyFunction_SetAnnotations(x, v) != 0) {
2770 /* Can't happen unless
2771 PyFunction_SetAnnotations changes. */
2772 why = WHY_EXCEPTION;
2773 }
2774 Py_DECREF(v);
2775 Py_DECREF(u);
2776 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 /* XXX Maybe this should be a separate opcode? */
2779 if (x != NULL && posdefaults > 0) {
2780 v = PyTuple_New(posdefaults);
2781 if (v == NULL) {
2782 Py_DECREF(x);
2783 x = NULL;
2784 break;
2785 }
2786 while (--posdefaults >= 0) {
2787 w = POP();
2788 PyTuple_SET_ITEM(v, posdefaults, w);
2789 }
2790 if (PyFunction_SetDefaults(x, v) != 0) {
2791 /* Can't happen unless
2792 PyFunction_SetDefaults changes. */
2793 why = WHY_EXCEPTION;
2794 }
2795 Py_DECREF(v);
2796 }
2797 if (x != NULL && kwdefaults > 0) {
2798 v = PyDict_New();
2799 if (v == NULL) {
2800 Py_DECREF(x);
2801 x = NULL;
2802 break;
2803 }
2804 while (--kwdefaults >= 0) {
2805 w = POP(); /* default value */
2806 u = POP(); /* kw only arg name */
2807 /* XXX(nnorwitz): check for errors */
2808 PyDict_SetItem(v, u, w);
2809 Py_DECREF(w);
2810 Py_DECREF(u);
2811 }
2812 if (PyFunction_SetKwDefaults(x, v) != 0) {
2813 /* Can't happen unless
2814 PyFunction_SetKwDefaults changes. */
2815 why = WHY_EXCEPTION;
2816 }
2817 Py_DECREF(v);
2818 }
2819 PUSH(x);
2820 break;
2821 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 TARGET(BUILD_SLICE)
2824 if (oparg == 3)
2825 w = POP();
2826 else
2827 w = NULL;
2828 v = POP();
2829 u = TOP();
2830 x = PySlice_New(u, v, w);
2831 Py_DECREF(u);
2832 Py_DECREF(v);
2833 Py_XDECREF(w);
2834 SET_TOP(x);
2835 if (x != NULL) DISPATCH();
2836 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 TARGET(EXTENDED_ARG)
2839 opcode = NEXTOP();
2840 oparg = oparg<<16 | NEXTARG();
2841 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002842
Antoine Pitrou042b1282010-08-13 21:15:58 +00002843#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002845#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 default:
2847 fprintf(stderr,
2848 "XXX lineno: %d, opcode: %d\n",
2849 PyFrame_GetLineNumber(f),
2850 opcode);
2851 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2852 why = WHY_EXCEPTION;
2853 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002854
2855#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002857#endif
2858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 if (why == WHY_NOT) {
2868 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002869#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 /* This check is expensive! */
2871 if (PyErr_Occurred())
2872 fprintf(stderr,
2873 "XXX undetected error\n");
2874 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002875#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 READ_TIMESTAMP(loop1);
2877 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002878#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002880#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 }
2882 why = WHY_EXCEPTION;
2883 x = Py_None;
2884 err = 0;
2885 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2890 if (!PyErr_Occurred()) {
2891 PyErr_SetString(PyExc_SystemError,
2892 "error return without exception set");
2893 why = WHY_EXCEPTION;
2894 }
2895 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002896#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 else {
2898 /* This check is expensive! */
2899 if (PyErr_Occurred()) {
2900 char buf[128];
2901 sprintf(buf, "Stack unwind with exception "
2902 "set and why=%d", why);
2903 Py_FatalError(buf);
2904 }
2905 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002906#endif
2907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (why == WHY_EXCEPTION) {
2911 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (tstate->c_tracefunc != NULL)
2914 call_exc_trace(tstate->c_tracefunc,
2915 tstate->c_traceobj, f);
2916 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 if (why == WHY_RERAISE)
2921 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002924
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002925fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 while (why != WHY_NOT && f->f_iblock > 0) {
2927 /* Peek at the current block. */
2928 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 assert(why != WHY_YIELD);
2931 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2932 why = WHY_NOT;
2933 JUMPTO(PyLong_AS_LONG(retval));
2934 Py_DECREF(retval);
2935 break;
2936 }
2937 /* Now we have to pop the block. */
2938 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 if (b->b_type == EXCEPT_HANDLER) {
2941 UNWIND_EXCEPT_HANDLER(b);
2942 continue;
2943 }
2944 UNWIND_BLOCK(b);
2945 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2946 why = WHY_NOT;
2947 JUMPTO(b->b_handler);
2948 break;
2949 }
2950 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2951 || b->b_type == SETUP_FINALLY)) {
2952 PyObject *exc, *val, *tb;
2953 int handler = b->b_handler;
2954 /* Beware, this invalidates all b->b_* fields */
2955 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2956 PUSH(tstate->exc_traceback);
2957 PUSH(tstate->exc_value);
2958 if (tstate->exc_type != NULL) {
2959 PUSH(tstate->exc_type);
2960 }
2961 else {
2962 Py_INCREF(Py_None);
2963 PUSH(Py_None);
2964 }
2965 PyErr_Fetch(&exc, &val, &tb);
2966 /* Make the raw exception data
2967 available to the handler,
2968 so a program can emulate the
2969 Python main loop. */
2970 PyErr_NormalizeException(
2971 &exc, &val, &tb);
2972 PyException_SetTraceback(val, tb);
2973 Py_INCREF(exc);
2974 tstate->exc_type = exc;
2975 Py_INCREF(val);
2976 tstate->exc_value = val;
2977 tstate->exc_traceback = tb;
2978 if (tb == NULL)
2979 tb = Py_None;
2980 Py_INCREF(tb);
2981 PUSH(tb);
2982 PUSH(val);
2983 PUSH(exc);
2984 why = WHY_NOT;
2985 JUMPTO(handler);
2986 break;
2987 }
2988 if (b->b_type == SETUP_FINALLY) {
2989 if (why & (WHY_RETURN | WHY_CONTINUE))
2990 PUSH(retval);
2991 PUSH(PyLong_FromLong((long)why));
2992 why = WHY_NOT;
2993 JUMPTO(b->b_handler);
2994 break;
2995 }
2996 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 if (why != WHY_NOT)
3001 break;
3002 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 assert(why != WHY_YIELD);
3007 /* Pop remaining stack entries. */
3008 while (!EMPTY()) {
3009 v = POP();
3010 Py_XDECREF(v);
3011 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 if (why != WHY_RETURN)
3014 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003015
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003016fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003017 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3018 /* The purpose of this block is to put aside the generator's exception
3019 state and restore that of the calling frame. If the current
3020 exception state is from the caller, we clear the exception values
3021 on the generator frame, so they are not swapped back in latter. The
3022 origin of the current exception state is determined by checking for
3023 except handler blocks, which we must be in iff a new exception
3024 state came into existence in this frame. (An uncaught exception
3025 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3026 int i;
3027 for (i = 0; i < f->f_iblock; i++)
3028 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3029 break;
3030 if (i == f->f_iblock)
3031 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003032 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003033 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003034 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003035 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 if (tstate->use_tracing) {
3038 if (tstate->c_tracefunc) {
3039 if (why == WHY_RETURN || why == WHY_YIELD) {
3040 if (call_trace(tstate->c_tracefunc,
3041 tstate->c_traceobj, f,
3042 PyTrace_RETURN, retval)) {
3043 Py_XDECREF(retval);
3044 retval = NULL;
3045 why = WHY_EXCEPTION;
3046 }
3047 }
3048 else if (why == WHY_EXCEPTION) {
3049 call_trace_protected(tstate->c_tracefunc,
3050 tstate->c_traceobj, f,
3051 PyTrace_RETURN, NULL);
3052 }
3053 }
3054 if (tstate->c_profilefunc) {
3055 if (why == WHY_EXCEPTION)
3056 call_trace_protected(tstate->c_profilefunc,
3057 tstate->c_profileobj, f,
3058 PyTrace_RETURN, NULL);
3059 else if (call_trace(tstate->c_profilefunc,
3060 tstate->c_profileobj, f,
3061 PyTrace_RETURN, retval)) {
3062 Py_XDECREF(retval);
3063 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003064 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 }
3066 }
3067 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003070exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 Py_LeaveRecursiveCall();
3072 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003075}
3076
Benjamin Petersonb204a422011-06-05 22:04:07 -05003077static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003078format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3079{
3080 int err;
3081 Py_ssize_t len = PyList_GET_SIZE(names);
3082 PyObject *name_str, *comma, *tail, *tmp;
3083
3084 assert(PyList_CheckExact(names));
3085 assert(len >= 1);
3086 /* Deal with the joys of natural language. */
3087 switch (len) {
3088 case 1:
3089 name_str = PyList_GET_ITEM(names, 0);
3090 Py_INCREF(name_str);
3091 break;
3092 case 2:
3093 name_str = PyUnicode_FromFormat("%U and %U",
3094 PyList_GET_ITEM(names, len - 2),
3095 PyList_GET_ITEM(names, len - 1));
3096 break;
3097 default:
3098 tail = PyUnicode_FromFormat(", %U, and %U",
3099 PyList_GET_ITEM(names, len - 2),
3100 PyList_GET_ITEM(names, len - 1));
3101 /* Chop off the last two objects in the list. This shouldn't actually
3102 fail, but we can't be too careful. */
3103 err = PyList_SetSlice(names, len - 2, len, NULL);
3104 if (err == -1) {
3105 Py_DECREF(tail);
3106 return;
3107 }
3108 /* Stitch everything up into a nice comma-separated list. */
3109 comma = PyUnicode_FromString(", ");
3110 if (comma == NULL) {
3111 Py_DECREF(tail);
3112 return;
3113 }
3114 tmp = PyUnicode_Join(comma, names);
3115 Py_DECREF(comma);
3116 if (tmp == NULL) {
3117 Py_DECREF(tail);
3118 return;
3119 }
3120 name_str = PyUnicode_Concat(tmp, tail);
3121 Py_DECREF(tmp);
3122 Py_DECREF(tail);
3123 break;
3124 }
3125 if (name_str == NULL)
3126 return;
3127 PyErr_Format(PyExc_TypeError,
3128 "%U() missing %i required %s argument%s: %U",
3129 co->co_name,
3130 len,
3131 kind,
3132 len == 1 ? "" : "s",
3133 name_str);
3134 Py_DECREF(name_str);
3135}
3136
3137static void
3138missing_arguments(PyCodeObject *co, int missing, int defcount,
3139 PyObject **fastlocals)
3140{
3141 int i, j = 0;
3142 int start, end;
3143 int positional = defcount != -1;
3144 const char *kind = positional ? "positional" : "keyword-only";
3145 PyObject *missing_names;
3146
3147 /* Compute the names of the arguments that are missing. */
3148 missing_names = PyList_New(missing);
3149 if (missing_names == NULL)
3150 return;
3151 if (positional) {
3152 start = 0;
3153 end = co->co_argcount - defcount;
3154 }
3155 else {
3156 start = co->co_argcount;
3157 end = start + co->co_kwonlyargcount;
3158 }
3159 for (i = start; i < end; i++) {
3160 if (GETLOCAL(i) == NULL) {
3161 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3162 PyObject *name = PyObject_Repr(raw);
3163 if (name == NULL) {
3164 Py_DECREF(missing_names);
3165 return;
3166 }
3167 PyList_SET_ITEM(missing_names, j++, name);
3168 }
3169 }
3170 assert(j == missing);
3171 format_missing(kind, co, missing_names);
3172 Py_DECREF(missing_names);
3173}
3174
3175static void
3176too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003177{
3178 int plural;
3179 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003180 int i;
3181 PyObject *sig, *kwonly_sig;
3182
Benjamin Petersone109c702011-06-24 09:37:26 -05003183 assert((co->co_flags & CO_VARARGS) == 0);
3184 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003185 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003186 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003187 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003188 if (defcount) {
3189 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003190 plural = 1;
3191 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3192 }
3193 else {
3194 plural = co->co_argcount != 1;
3195 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3196 }
3197 if (sig == NULL)
3198 return;
3199 if (kwonly_given) {
3200 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3201 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3202 kwonly_given != 1 ? "s" : "");
3203 if (kwonly_sig == NULL) {
3204 Py_DECREF(sig);
3205 return;
3206 }
3207 }
3208 else {
3209 /* This will not fail. */
3210 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003211 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003212 }
3213 PyErr_Format(PyExc_TypeError,
3214 "%U() takes %U positional argument%s but %d%U %s given",
3215 co->co_name,
3216 sig,
3217 plural ? "s" : "",
3218 given,
3219 kwonly_sig,
3220 given == 1 && !kwonly_given ? "was" : "were");
3221 Py_DECREF(sig);
3222 Py_DECREF(kwonly_sig);
3223}
3224
Guido van Rossumc2e20742006-02-27 22:32:47 +00003225/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003226 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003227 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003228
Tim Peters6d6c1a32001-08-02 04:15:00 +00003229PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003230PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 PyObject **args, int argcount, PyObject **kws, int kwcount,
3232 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003233{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003234 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 register PyFrameObject *f;
3236 register PyObject *retval = NULL;
3237 register PyObject **fastlocals, **freevars;
3238 PyThreadState *tstate = PyThreadState_GET();
3239 PyObject *x, *u;
3240 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003241 int i;
3242 int n = argcount;
3243 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 if (globals == NULL) {
3246 PyErr_SetString(PyExc_SystemError,
3247 "PyEval_EvalCodeEx: NULL globals");
3248 return NULL;
3249 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 assert(tstate != NULL);
3252 assert(globals != NULL);
3253 f = PyFrame_New(tstate, co, globals, locals);
3254 if (f == NULL)
3255 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 fastlocals = f->f_localsplus;
3258 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003259
Benjamin Petersonb204a422011-06-05 22:04:07 -05003260 /* Parse arguments. */
3261 if (co->co_flags & CO_VARKEYWORDS) {
3262 kwdict = PyDict_New();
3263 if (kwdict == NULL)
3264 goto fail;
3265 i = total_args;
3266 if (co->co_flags & CO_VARARGS)
3267 i++;
3268 SETLOCAL(i, kwdict);
3269 }
3270 if (argcount > co->co_argcount)
3271 n = co->co_argcount;
3272 for (i = 0; i < n; i++) {
3273 x = args[i];
3274 Py_INCREF(x);
3275 SETLOCAL(i, x);
3276 }
3277 if (co->co_flags & CO_VARARGS) {
3278 u = PyTuple_New(argcount - n);
3279 if (u == NULL)
3280 goto fail;
3281 SETLOCAL(total_args, u);
3282 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003283 x = args[i];
3284 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003285 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003287 }
3288 for (i = 0; i < kwcount; i++) {
3289 PyObject **co_varnames;
3290 PyObject *keyword = kws[2*i];
3291 PyObject *value = kws[2*i + 1];
3292 int j;
3293 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3294 PyErr_Format(PyExc_TypeError,
3295 "%U() keywords must be strings",
3296 co->co_name);
3297 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003298 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003299 /* Speed hack: do raw pointer compares. As names are
3300 normally interned this should almost always hit. */
3301 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3302 for (j = 0; j < total_args; j++) {
3303 PyObject *nm = co_varnames[j];
3304 if (nm == keyword)
3305 goto kw_found;
3306 }
3307 /* Slow fallback, just in case */
3308 for (j = 0; j < total_args; j++) {
3309 PyObject *nm = co_varnames[j];
3310 int cmp = PyObject_RichCompareBool(
3311 keyword, nm, Py_EQ);
3312 if (cmp > 0)
3313 goto kw_found;
3314 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003315 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003316 }
3317 if (j >= total_args && kwdict == NULL) {
3318 PyErr_Format(PyExc_TypeError,
3319 "%U() got an unexpected "
3320 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 co->co_name,
3322 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003323 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003325 PyDict_SetItem(kwdict, keyword, value);
3326 continue;
3327 kw_found:
3328 if (GETLOCAL(j) != NULL) {
3329 PyErr_Format(PyExc_TypeError,
3330 "%U() got multiple "
3331 "values for argument '%S'",
3332 co->co_name,
3333 keyword);
3334 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003336 Py_INCREF(value);
3337 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003339 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003340 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 goto fail;
3342 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003343 if (argcount < co->co_argcount) {
3344 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003345 int missing = 0;
3346 for (i = argcount; i < m; i++)
3347 if (GETLOCAL(i) == NULL)
3348 missing++;
3349 if (missing) {
3350 missing_arguments(co, missing, defcount, fastlocals);
3351 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003352 }
3353 if (n > m)
3354 i = n - m;
3355 else
3356 i = 0;
3357 for (; i < defcount; i++) {
3358 if (GETLOCAL(m+i) == NULL) {
3359 PyObject *def = defs[i];
3360 Py_INCREF(def);
3361 SETLOCAL(m+i, def);
3362 }
3363 }
3364 }
3365 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003366 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003367 for (i = co->co_argcount; i < total_args; i++) {
3368 PyObject *name;
3369 if (GETLOCAL(i) != NULL)
3370 continue;
3371 name = PyTuple_GET_ITEM(co->co_varnames, i);
3372 if (kwdefs != NULL) {
3373 PyObject *def = PyDict_GetItem(kwdefs, name);
3374 if (def) {
3375 Py_INCREF(def);
3376 SETLOCAL(i, def);
3377 continue;
3378 }
3379 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003380 missing++;
3381 }
3382 if (missing) {
3383 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003384 goto fail;
3385 }
3386 }
3387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003389 vars into frame. */
3390 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003392 int arg;
3393 /* Possibly account for the cell variable being an argument. */
3394 if (co->co_cell2arg != NULL &&
3395 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3396 c = PyCell_New(GETLOCAL(arg));
3397 else
3398 c = PyCell_New(NULL);
3399 if (c == NULL)
3400 goto fail;
3401 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003403 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3404 PyObject *o = PyTuple_GET_ITEM(closure, i);
3405 Py_INCREF(o);
3406 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 if (co->co_flags & CO_GENERATOR) {
3410 /* Don't need to keep the reference to f_back, it will be set
3411 * when the generator is resumed. */
3412 Py_XDECREF(f->f_back);
3413 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 /* Create a new generator that owns the ready to run frame
3418 * and return that as the value. */
3419 return PyGen_New(f);
3420 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003423
Thomas Woutersce272b62007-09-19 21:19:28 +00003424fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 /* decref'ing the frame can cause __del__ methods to get invoked,
3427 which can call back into Python. While we're done with the
3428 current Python frame (f), the associated C stack is still in use,
3429 so recursion_depth must be boosted for the duration.
3430 */
3431 assert(tstate != NULL);
3432 ++tstate->recursion_depth;
3433 Py_DECREF(f);
3434 --tstate->recursion_depth;
3435 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003436}
3437
3438
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003439static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003440special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003443 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003445 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 return NULL;
3447 }
3448 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003449}
3450
3451
Benjamin Peterson87880242011-07-03 16:48:31 -05003452/* These 3 functions deal with the exception state of generators. */
3453
3454static void
3455save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3456{
3457 PyObject *type, *value, *traceback;
3458 Py_XINCREF(tstate->exc_type);
3459 Py_XINCREF(tstate->exc_value);
3460 Py_XINCREF(tstate->exc_traceback);
3461 type = f->f_exc_type;
3462 value = f->f_exc_value;
3463 traceback = f->f_exc_traceback;
3464 f->f_exc_type = tstate->exc_type;
3465 f->f_exc_value = tstate->exc_value;
3466 f->f_exc_traceback = tstate->exc_traceback;
3467 Py_XDECREF(type);
3468 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003469 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003470}
3471
3472static void
3473swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3474{
3475 PyObject *tmp;
3476 tmp = tstate->exc_type;
3477 tstate->exc_type = f->f_exc_type;
3478 f->f_exc_type = tmp;
3479 tmp = tstate->exc_value;
3480 tstate->exc_value = f->f_exc_value;
3481 f->f_exc_value = tmp;
3482 tmp = tstate->exc_traceback;
3483 tstate->exc_traceback = f->f_exc_traceback;
3484 f->f_exc_traceback = tmp;
3485}
3486
3487static void
3488restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3489{
3490 PyObject *type, *value, *tb;
3491 type = tstate->exc_type;
3492 value = tstate->exc_value;
3493 tb = tstate->exc_traceback;
3494 tstate->exc_type = f->f_exc_type;
3495 tstate->exc_value = f->f_exc_value;
3496 tstate->exc_traceback = f->f_exc_traceback;
3497 f->f_exc_type = NULL;
3498 f->f_exc_value = NULL;
3499 f->f_exc_traceback = NULL;
3500 Py_XDECREF(type);
3501 Py_XDECREF(value);
3502 Py_XDECREF(tb);
3503}
3504
3505
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003506/* Logic for the raise statement (too complicated for inlining).
3507 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003508static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003509do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 if (exc == NULL) {
3514 /* Reraise */
3515 PyThreadState *tstate = PyThreadState_GET();
3516 PyObject *tb;
3517 type = tstate->exc_type;
3518 value = tstate->exc_value;
3519 tb = tstate->exc_traceback;
3520 if (type == Py_None) {
3521 PyErr_SetString(PyExc_RuntimeError,
3522 "No active exception to reraise");
3523 return WHY_EXCEPTION;
3524 }
3525 Py_XINCREF(type);
3526 Py_XINCREF(value);
3527 Py_XINCREF(tb);
3528 PyErr_Restore(type, value, tb);
3529 return WHY_RERAISE;
3530 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003531
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003532 /* We support the following forms of raise:
3533 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003534 raise <instance>
3535 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 if (PyExceptionClass_Check(exc)) {
3538 type = exc;
3539 value = PyObject_CallObject(exc, NULL);
3540 if (value == NULL)
3541 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003542 if (!PyExceptionInstance_Check(value)) {
3543 PyErr_Format(PyExc_TypeError,
3544 "calling %R should have returned an instance of "
3545 "BaseException, not %R",
3546 type, Py_TYPE(value));
3547 goto raise_error;
3548 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 }
3550 else if (PyExceptionInstance_Check(exc)) {
3551 value = exc;
3552 type = PyExceptionInstance_Class(exc);
3553 Py_INCREF(type);
3554 }
3555 else {
3556 /* Not something you can raise. You get an exception
3557 anyway, just not what you specified :-) */
3558 Py_DECREF(exc);
3559 PyErr_SetString(PyExc_TypeError,
3560 "exceptions must derive from BaseException");
3561 goto raise_error;
3562 }
Collin Winter828f04a2007-08-31 00:04:24 +00003563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003564 if (cause) {
3565 PyObject *fixed_cause;
Nick Coghlanab7bf212012-02-26 17:49:52 +10003566 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 if (PyExceptionClass_Check(cause)) {
3568 fixed_cause = PyObject_CallObject(cause, NULL);
3569 if (fixed_cause == NULL)
3570 goto raise_error;
Nick Coghlanab7bf212012-02-26 17:49:52 +10003571 Py_CLEAR(cause);
3572 } else {
3573 /* Let "exc.__cause__ = cause" handle all further checks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 fixed_cause = cause;
Nick Coghlanab7bf212012-02-26 17:49:52 +10003575 cause = NULL; /* Steal the reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 }
Nick Coghlanab7bf212012-02-26 17:49:52 +10003577 /* We retain ownership of the reference to fixed_cause */
3578 result = _PyException_SetCauseChecked(value, fixed_cause);
3579 Py_DECREF(fixed_cause);
3580 if (result < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003581 goto raise_error;
3582 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 }
Collin Winter828f04a2007-08-31 00:04:24 +00003584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 PyErr_SetObject(type, value);
3586 /* PyErr_SetObject incref's its arguments */
3587 Py_XDECREF(value);
3588 Py_XDECREF(type);
3589 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003590
3591raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 Py_XDECREF(value);
3593 Py_XDECREF(type);
3594 Py_XDECREF(cause);
3595 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003596}
3597
Tim Petersd6d010b2001-06-21 02:49:55 +00003598/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003599 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003600
Guido van Rossum0368b722007-05-11 16:50:42 +00003601 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3602 with a variable target.
3603*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003604
Barry Warsawe42b18f1997-08-25 22:13:04 +00003605static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003606unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003607{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 int i = 0, j = 0;
3609 Py_ssize_t ll = 0;
3610 PyObject *it; /* iter(v) */
3611 PyObject *w;
3612 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 it = PyObject_GetIter(v);
3617 if (it == NULL)
3618 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 for (; i < argcnt; i++) {
3621 w = PyIter_Next(it);
3622 if (w == NULL) {
3623 /* Iterator done, via error or exhaustion. */
3624 if (!PyErr_Occurred()) {
3625 PyErr_Format(PyExc_ValueError,
3626 "need more than %d value%s to unpack",
3627 i, i == 1 ? "" : "s");
3628 }
3629 goto Error;
3630 }
3631 *--sp = w;
3632 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 if (argcntafter == -1) {
3635 /* We better have exhausted the iterator now. */
3636 w = PyIter_Next(it);
3637 if (w == NULL) {
3638 if (PyErr_Occurred())
3639 goto Error;
3640 Py_DECREF(it);
3641 return 1;
3642 }
3643 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003644 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3645 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 goto Error;
3647 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 l = PySequence_List(it);
3650 if (l == NULL)
3651 goto Error;
3652 *--sp = l;
3653 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003655 ll = PyList_GET_SIZE(l);
3656 if (ll < argcntafter) {
3657 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3658 argcnt + ll);
3659 goto Error;
3660 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 /* Pop the "after-variable" args off the list. */
3663 for (j = argcntafter; j > 0; j--, i++) {
3664 *--sp = PyList_GET_ITEM(l, ll - j);
3665 }
3666 /* Resize the list. */
3667 Py_SIZE(l) = ll - argcntafter;
3668 Py_DECREF(it);
3669 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003670
Tim Petersd6d010b2001-06-21 02:49:55 +00003671Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003672 for (; i > 0; i--, sp++)
3673 Py_DECREF(*sp);
3674 Py_XDECREF(it);
3675 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003676}
3677
3678
Guido van Rossum96a42c81992-01-12 02:29:51 +00003679#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003680static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003681prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 printf("%s ", str);
3684 if (PyObject_Print(v, stdout, 0) != 0)
3685 PyErr_Clear(); /* Don't know what else to do */
3686 printf("\n");
3687 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003688}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003689#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003690
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003691static void
Fred Drake5755ce62001-06-27 19:19:46 +00003692call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 PyObject *type, *value, *traceback, *arg;
3695 int err;
3696 PyErr_Fetch(&type, &value, &traceback);
3697 if (value == NULL) {
3698 value = Py_None;
3699 Py_INCREF(value);
3700 }
3701 arg = PyTuple_Pack(3, type, value, traceback);
3702 if (arg == NULL) {
3703 PyErr_Restore(type, value, traceback);
3704 return;
3705 }
3706 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3707 Py_DECREF(arg);
3708 if (err == 0)
3709 PyErr_Restore(type, value, traceback);
3710 else {
3711 Py_XDECREF(type);
3712 Py_XDECREF(value);
3713 Py_XDECREF(traceback);
3714 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003715}
3716
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003717static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003718call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003719 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 PyObject *type, *value, *traceback;
3722 int err;
3723 PyErr_Fetch(&type, &value, &traceback);
3724 err = call_trace(func, obj, frame, what, arg);
3725 if (err == 0)
3726 {
3727 PyErr_Restore(type, value, traceback);
3728 return 0;
3729 }
3730 else {
3731 Py_XDECREF(type);
3732 Py_XDECREF(value);
3733 Py_XDECREF(traceback);
3734 return -1;
3735 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003736}
3737
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003738static int
Fred Drake5755ce62001-06-27 19:19:46 +00003739call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 register PyThreadState *tstate = frame->f_tstate;
3743 int result;
3744 if (tstate->tracing)
3745 return 0;
3746 tstate->tracing++;
3747 tstate->use_tracing = 0;
3748 result = func(obj, frame, what, arg);
3749 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3750 || (tstate->c_profilefunc != NULL));
3751 tstate->tracing--;
3752 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003753}
3754
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003755PyObject *
3756_PyEval_CallTracing(PyObject *func, PyObject *args)
3757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 PyFrameObject *frame = PyEval_GetFrame();
3759 PyThreadState *tstate = frame->f_tstate;
3760 int save_tracing = tstate->tracing;
3761 int save_use_tracing = tstate->use_tracing;
3762 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 tstate->tracing = 0;
3765 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3766 || (tstate->c_profilefunc != NULL));
3767 result = PyObject_Call(func, args, NULL);
3768 tstate->tracing = save_tracing;
3769 tstate->use_tracing = save_use_tracing;
3770 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003771}
3772
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003773/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003774static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003775maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3777 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003779 int result = 0;
3780 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 /* If the last instruction executed isn't in the current
3783 instruction window, reset the window.
3784 */
3785 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3786 PyAddrPair bounds;
3787 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3788 &bounds);
3789 *instr_lb = bounds.ap_lower;
3790 *instr_ub = bounds.ap_upper;
3791 }
3792 /* If the last instruction falls at the start of a line or if
3793 it represents a jump backwards, update the frame's line
3794 number and call the trace function. */
3795 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3796 frame->f_lineno = line;
3797 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3798 }
3799 *instr_prev = frame->f_lasti;
3800 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003801}
3802
Fred Drake5755ce62001-06-27 19:19:46 +00003803void
3804PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003805{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003806 PyThreadState *tstate = PyThreadState_GET();
3807 PyObject *temp = tstate->c_profileobj;
3808 Py_XINCREF(arg);
3809 tstate->c_profilefunc = NULL;
3810 tstate->c_profileobj = NULL;
3811 /* Must make sure that tracing is not ignored if 'temp' is freed */
3812 tstate->use_tracing = tstate->c_tracefunc != NULL;
3813 Py_XDECREF(temp);
3814 tstate->c_profilefunc = func;
3815 tstate->c_profileobj = arg;
3816 /* Flag that tracing or profiling is turned on */
3817 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003818}
3819
3820void
3821PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3822{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 PyThreadState *tstate = PyThreadState_GET();
3824 PyObject *temp = tstate->c_traceobj;
3825 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3826 Py_XINCREF(arg);
3827 tstate->c_tracefunc = NULL;
3828 tstate->c_traceobj = NULL;
3829 /* Must make sure that profiling is not ignored if 'temp' is freed */
3830 tstate->use_tracing = tstate->c_profilefunc != NULL;
3831 Py_XDECREF(temp);
3832 tstate->c_tracefunc = func;
3833 tstate->c_traceobj = arg;
3834 /* Flag that tracing or profiling is turned on */
3835 tstate->use_tracing = ((func != NULL)
3836 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003837}
3838
Guido van Rossumb209a111997-04-29 18:18:01 +00003839PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003840PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003841{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 PyFrameObject *current_frame = PyEval_GetFrame();
3843 if (current_frame == NULL)
3844 return PyThreadState_GET()->interp->builtins;
3845 else
3846 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003847}
3848
Guido van Rossumb209a111997-04-29 18:18:01 +00003849PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003850PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003851{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 PyFrameObject *current_frame = PyEval_GetFrame();
3853 if (current_frame == NULL)
3854 return NULL;
3855 PyFrame_FastToLocals(current_frame);
3856 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003857}
3858
Guido van Rossumb209a111997-04-29 18:18:01 +00003859PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003860PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003861{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 PyFrameObject *current_frame = PyEval_GetFrame();
3863 if (current_frame == NULL)
3864 return NULL;
3865 else
3866 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003867}
3868
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003869PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003870PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 PyThreadState *tstate = PyThreadState_GET();
3873 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003874}
3875
Guido van Rossum6135a871995-01-09 17:53:26 +00003876int
Tim Peters5ba58662001-07-16 02:29:45 +00003877PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 PyFrameObject *current_frame = PyEval_GetFrame();
3880 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 if (current_frame != NULL) {
3883 const int codeflags = current_frame->f_code->co_flags;
3884 const int compilerflags = codeflags & PyCF_MASK;
3885 if (compilerflags) {
3886 result = 1;
3887 cf->cf_flags |= compilerflags;
3888 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003889#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 if (codeflags & CO_GENERATOR_ALLOWED) {
3891 result = 1;
3892 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3893 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003894#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003895 }
3896 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003897}
3898
Guido van Rossum3f5da241990-12-20 15:06:42 +00003899
Guido van Rossum681d79a1995-07-18 14:51:37 +00003900/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003901 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003902
Guido van Rossumb209a111997-04-29 18:18:01 +00003903PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003904PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 if (arg == NULL) {
3909 arg = PyTuple_New(0);
3910 if (arg == NULL)
3911 return NULL;
3912 }
3913 else if (!PyTuple_Check(arg)) {
3914 PyErr_SetString(PyExc_TypeError,
3915 "argument list must be a tuple");
3916 return NULL;
3917 }
3918 else
3919 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 if (kw != NULL && !PyDict_Check(kw)) {
3922 PyErr_SetString(PyExc_TypeError,
3923 "keyword list must be a dictionary");
3924 Py_DECREF(arg);
3925 return NULL;
3926 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 result = PyObject_Call(func, arg, kw);
3929 Py_DECREF(arg);
3930 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003931}
3932
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003933const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003934PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003935{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003936 if (PyMethod_Check(func))
3937 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3938 else if (PyFunction_Check(func))
3939 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3940 else if (PyCFunction_Check(func))
3941 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3942 else
3943 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003944}
3945
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003946const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003947PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 if (PyMethod_Check(func))
3950 return "()";
3951 else if (PyFunction_Check(func))
3952 return "()";
3953 else if (PyCFunction_Check(func))
3954 return "()";
3955 else
3956 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003957}
3958
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003959static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003960err_args(PyObject *func, int flags, int nargs)
3961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 if (flags & METH_NOARGS)
3963 PyErr_Format(PyExc_TypeError,
3964 "%.200s() takes no arguments (%d given)",
3965 ((PyCFunctionObject *)func)->m_ml->ml_name,
3966 nargs);
3967 else
3968 PyErr_Format(PyExc_TypeError,
3969 "%.200s() takes exactly one argument (%d given)",
3970 ((PyCFunctionObject *)func)->m_ml->ml_name,
3971 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003972}
3973
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003974#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003975if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 if (call_trace(tstate->c_profilefunc, \
3977 tstate->c_profileobj, \
3978 tstate->frame, PyTrace_C_CALL, \
3979 func)) { \
3980 x = NULL; \
3981 } \
3982 else { \
3983 x = call; \
3984 if (tstate->c_profilefunc != NULL) { \
3985 if (x == NULL) { \
3986 call_trace_protected(tstate->c_profilefunc, \
3987 tstate->c_profileobj, \
3988 tstate->frame, PyTrace_C_EXCEPTION, \
3989 func); \
3990 /* XXX should pass (type, value, tb) */ \
3991 } else { \
3992 if (call_trace(tstate->c_profilefunc, \
3993 tstate->c_profileobj, \
3994 tstate->frame, PyTrace_C_RETURN, \
3995 func)) { \
3996 Py_DECREF(x); \
3997 x = NULL; \
3998 } \
3999 } \
4000 } \
4001 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004002} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 x = call; \
4004 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004005
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004006static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004007call_function(PyObject ***pp_stack, int oparg
4008#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004010#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004012{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 int na = oparg & 0xff;
4014 int nk = (oparg>>8) & 0xff;
4015 int n = na + 2 * nk;
4016 PyObject **pfunc = (*pp_stack) - n - 1;
4017 PyObject *func = *pfunc;
4018 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 /* Always dispatch PyCFunction first, because these are
4021 presumed to be the most frequent callable object.
4022 */
4023 if (PyCFunction_Check(func) && nk == 0) {
4024 int flags = PyCFunction_GET_FLAGS(func);
4025 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 PCALL(PCALL_CFUNCTION);
4028 if (flags & (METH_NOARGS | METH_O)) {
4029 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4030 PyObject *self = PyCFunction_GET_SELF(func);
4031 if (flags & METH_NOARGS && na == 0) {
4032 C_TRACE(x, (*meth)(self,NULL));
4033 }
4034 else if (flags & METH_O && na == 1) {
4035 PyObject *arg = EXT_POP(*pp_stack);
4036 C_TRACE(x, (*meth)(self,arg));
4037 Py_DECREF(arg);
4038 }
4039 else {
4040 err_args(func, flags, na);
4041 x = NULL;
4042 }
4043 }
4044 else {
4045 PyObject *callargs;
4046 callargs = load_args(pp_stack, na);
4047 READ_TIMESTAMP(*pintr0);
4048 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4049 READ_TIMESTAMP(*pintr1);
4050 Py_XDECREF(callargs);
4051 }
4052 } else {
4053 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4054 /* optimize access to bound methods */
4055 PyObject *self = PyMethod_GET_SELF(func);
4056 PCALL(PCALL_METHOD);
4057 PCALL(PCALL_BOUND_METHOD);
4058 Py_INCREF(self);
4059 func = PyMethod_GET_FUNCTION(func);
4060 Py_INCREF(func);
4061 Py_DECREF(*pfunc);
4062 *pfunc = self;
4063 na++;
4064 n++;
4065 } else
4066 Py_INCREF(func);
4067 READ_TIMESTAMP(*pintr0);
4068 if (PyFunction_Check(func))
4069 x = fast_function(func, pp_stack, n, na, nk);
4070 else
4071 x = do_call(func, pp_stack, na, nk);
4072 READ_TIMESTAMP(*pintr1);
4073 Py_DECREF(func);
4074 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004076 /* Clear the stack of the function object. Also removes
4077 the arguments in case they weren't consumed already
4078 (fast_function() and err_args() leave them on the stack).
4079 */
4080 while ((*pp_stack) > pfunc) {
4081 w = EXT_POP(*pp_stack);
4082 Py_DECREF(w);
4083 PCALL(PCALL_POP);
4084 }
4085 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004086}
4087
Jeremy Hylton192690e2002-08-16 18:36:11 +00004088/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004089 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004090 For the simplest case -- a function that takes only positional
4091 arguments and is called with only positional arguments -- it
4092 inlines the most primitive frame setup code from
4093 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4094 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004095*/
4096
4097static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004098fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004099{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4101 PyObject *globals = PyFunction_GET_GLOBALS(func);
4102 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4103 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4104 PyObject **d = NULL;
4105 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004106
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 PCALL(PCALL_FUNCTION);
4108 PCALL(PCALL_FAST_FUNCTION);
4109 if (argdefs == NULL && co->co_argcount == n &&
4110 co->co_kwonlyargcount == 0 && nk==0 &&
4111 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4112 PyFrameObject *f;
4113 PyObject *retval = NULL;
4114 PyThreadState *tstate = PyThreadState_GET();
4115 PyObject **fastlocals, **stack;
4116 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 PCALL(PCALL_FASTER_FUNCTION);
4119 assert(globals != NULL);
4120 /* XXX Perhaps we should create a specialized
4121 PyFrame_New() that doesn't take locals, but does
4122 take builtins without sanity checking them.
4123 */
4124 assert(tstate != NULL);
4125 f = PyFrame_New(tstate, co, globals, NULL);
4126 if (f == NULL)
4127 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 fastlocals = f->f_localsplus;
4130 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004131
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004132 for (i = 0; i < n; i++) {
4133 Py_INCREF(*stack);
4134 fastlocals[i] = *stack++;
4135 }
4136 retval = PyEval_EvalFrameEx(f,0);
4137 ++tstate->recursion_depth;
4138 Py_DECREF(f);
4139 --tstate->recursion_depth;
4140 return retval;
4141 }
4142 if (argdefs != NULL) {
4143 d = &PyTuple_GET_ITEM(argdefs, 0);
4144 nd = Py_SIZE(argdefs);
4145 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004146 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 (PyObject *)NULL, (*pp_stack)-n, na,
4148 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4149 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004150}
4151
4152static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004153update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4154 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004155{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004156 PyObject *kwdict = NULL;
4157 if (orig_kwdict == NULL)
4158 kwdict = PyDict_New();
4159 else {
4160 kwdict = PyDict_Copy(orig_kwdict);
4161 Py_DECREF(orig_kwdict);
4162 }
4163 if (kwdict == NULL)
4164 return NULL;
4165 while (--nk >= 0) {
4166 int err;
4167 PyObject *value = EXT_POP(*pp_stack);
4168 PyObject *key = EXT_POP(*pp_stack);
4169 if (PyDict_GetItem(kwdict, key) != NULL) {
4170 PyErr_Format(PyExc_TypeError,
4171 "%.200s%s got multiple values "
4172 "for keyword argument '%U'",
4173 PyEval_GetFuncName(func),
4174 PyEval_GetFuncDesc(func),
4175 key);
4176 Py_DECREF(key);
4177 Py_DECREF(value);
4178 Py_DECREF(kwdict);
4179 return NULL;
4180 }
4181 err = PyDict_SetItem(kwdict, key, value);
4182 Py_DECREF(key);
4183 Py_DECREF(value);
4184 if (err) {
4185 Py_DECREF(kwdict);
4186 return NULL;
4187 }
4188 }
4189 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004190}
4191
4192static PyObject *
4193update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004195{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 callargs = PyTuple_New(nstack + nstar);
4199 if (callargs == NULL) {
4200 return NULL;
4201 }
4202 if (nstar) {
4203 int i;
4204 for (i = 0; i < nstar; i++) {
4205 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4206 Py_INCREF(a);
4207 PyTuple_SET_ITEM(callargs, nstack + i, a);
4208 }
4209 }
4210 while (--nstack >= 0) {
4211 w = EXT_POP(*pp_stack);
4212 PyTuple_SET_ITEM(callargs, nstack, w);
4213 }
4214 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004215}
4216
4217static PyObject *
4218load_args(PyObject ***pp_stack, int na)
4219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 PyObject *args = PyTuple_New(na);
4221 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004223 if (args == NULL)
4224 return NULL;
4225 while (--na >= 0) {
4226 w = EXT_POP(*pp_stack);
4227 PyTuple_SET_ITEM(args, na, w);
4228 }
4229 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004230}
4231
4232static PyObject *
4233do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4234{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 PyObject *callargs = NULL;
4236 PyObject *kwdict = NULL;
4237 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004239 if (nk > 0) {
4240 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4241 if (kwdict == NULL)
4242 goto call_fail;
4243 }
4244 callargs = load_args(pp_stack, na);
4245 if (callargs == NULL)
4246 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004247#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004248 /* At this point, we have to look at the type of func to
4249 update the call stats properly. Do it here so as to avoid
4250 exposing the call stats machinery outside ceval.c
4251 */
4252 if (PyFunction_Check(func))
4253 PCALL(PCALL_FUNCTION);
4254 else if (PyMethod_Check(func))
4255 PCALL(PCALL_METHOD);
4256 else if (PyType_Check(func))
4257 PCALL(PCALL_TYPE);
4258 else if (PyCFunction_Check(func))
4259 PCALL(PCALL_CFUNCTION);
4260 else
4261 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004262#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 if (PyCFunction_Check(func)) {
4264 PyThreadState *tstate = PyThreadState_GET();
4265 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4266 }
4267 else
4268 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004269call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 Py_XDECREF(callargs);
4271 Py_XDECREF(kwdict);
4272 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004273}
4274
4275static PyObject *
4276ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4277{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 int nstar = 0;
4279 PyObject *callargs = NULL;
4280 PyObject *stararg = NULL;
4281 PyObject *kwdict = NULL;
4282 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 if (flags & CALL_FLAG_KW) {
4285 kwdict = EXT_POP(*pp_stack);
4286 if (!PyDict_Check(kwdict)) {
4287 PyObject *d;
4288 d = PyDict_New();
4289 if (d == NULL)
4290 goto ext_call_fail;
4291 if (PyDict_Update(d, kwdict) != 0) {
4292 Py_DECREF(d);
4293 /* PyDict_Update raises attribute
4294 * error (percolated from an attempt
4295 * to get 'keys' attribute) instead of
4296 * a type error if its second argument
4297 * is not a mapping.
4298 */
4299 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4300 PyErr_Format(PyExc_TypeError,
4301 "%.200s%.200s argument after ** "
4302 "must be a mapping, not %.200s",
4303 PyEval_GetFuncName(func),
4304 PyEval_GetFuncDesc(func),
4305 kwdict->ob_type->tp_name);
4306 }
4307 goto ext_call_fail;
4308 }
4309 Py_DECREF(kwdict);
4310 kwdict = d;
4311 }
4312 }
4313 if (flags & CALL_FLAG_VAR) {
4314 stararg = EXT_POP(*pp_stack);
4315 if (!PyTuple_Check(stararg)) {
4316 PyObject *t = NULL;
4317 t = PySequence_Tuple(stararg);
4318 if (t == NULL) {
4319 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4320 PyErr_Format(PyExc_TypeError,
4321 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004322 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004323 PyEval_GetFuncName(func),
4324 PyEval_GetFuncDesc(func),
4325 stararg->ob_type->tp_name);
4326 }
4327 goto ext_call_fail;
4328 }
4329 Py_DECREF(stararg);
4330 stararg = t;
4331 }
4332 nstar = PyTuple_GET_SIZE(stararg);
4333 }
4334 if (nk > 0) {
4335 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4336 if (kwdict == NULL)
4337 goto ext_call_fail;
4338 }
4339 callargs = update_star_args(na, nstar, stararg, pp_stack);
4340 if (callargs == NULL)
4341 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004342#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004343 /* At this point, we have to look at the type of func to
4344 update the call stats properly. Do it here so as to avoid
4345 exposing the call stats machinery outside ceval.c
4346 */
4347 if (PyFunction_Check(func))
4348 PCALL(PCALL_FUNCTION);
4349 else if (PyMethod_Check(func))
4350 PCALL(PCALL_METHOD);
4351 else if (PyType_Check(func))
4352 PCALL(PCALL_TYPE);
4353 else if (PyCFunction_Check(func))
4354 PCALL(PCALL_CFUNCTION);
4355 else
4356 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004357#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 if (PyCFunction_Check(func)) {
4359 PyThreadState *tstate = PyThreadState_GET();
4360 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4361 }
4362 else
4363 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004364ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 Py_XDECREF(callargs);
4366 Py_XDECREF(kwdict);
4367 Py_XDECREF(stararg);
4368 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004369}
4370
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004371/* Extract a slice index from a PyInt or PyLong or an object with the
4372 nb_index slot defined, and store in *pi.
4373 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4374 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 +00004375 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004376*/
Tim Petersb5196382001-12-16 19:44:20 +00004377/* Note: If v is NULL, return success without storing into *pi. This
4378 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4379 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004380*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004381int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004382_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004383{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 if (v != NULL) {
4385 Py_ssize_t x;
4386 if (PyIndex_Check(v)) {
4387 x = PyNumber_AsSsize_t(v, NULL);
4388 if (x == -1 && PyErr_Occurred())
4389 return 0;
4390 }
4391 else {
4392 PyErr_SetString(PyExc_TypeError,
4393 "slice indices must be integers or "
4394 "None or have an __index__ method");
4395 return 0;
4396 }
4397 *pi = x;
4398 }
4399 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004400}
4401
Guido van Rossum486364b2007-06-30 05:01:58 +00004402#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004404
Guido van Rossumb209a111997-04-29 18:18:01 +00004405static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004406cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004407{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 int res = 0;
4409 switch (op) {
4410 case PyCmp_IS:
4411 res = (v == w);
4412 break;
4413 case PyCmp_IS_NOT:
4414 res = (v != w);
4415 break;
4416 case PyCmp_IN:
4417 res = PySequence_Contains(w, v);
4418 if (res < 0)
4419 return NULL;
4420 break;
4421 case PyCmp_NOT_IN:
4422 res = PySequence_Contains(w, v);
4423 if (res < 0)
4424 return NULL;
4425 res = !res;
4426 break;
4427 case PyCmp_EXC_MATCH:
4428 if (PyTuple_Check(w)) {
4429 Py_ssize_t i, length;
4430 length = PyTuple_Size(w);
4431 for (i = 0; i < length; i += 1) {
4432 PyObject *exc = PyTuple_GET_ITEM(w, i);
4433 if (!PyExceptionClass_Check(exc)) {
4434 PyErr_SetString(PyExc_TypeError,
4435 CANNOT_CATCH_MSG);
4436 return NULL;
4437 }
4438 }
4439 }
4440 else {
4441 if (!PyExceptionClass_Check(w)) {
4442 PyErr_SetString(PyExc_TypeError,
4443 CANNOT_CATCH_MSG);
4444 return NULL;
4445 }
4446 }
4447 res = PyErr_GivenExceptionMatches(v, w);
4448 break;
4449 default:
4450 return PyObject_RichCompare(v, w, op);
4451 }
4452 v = res ? Py_True : Py_False;
4453 Py_INCREF(v);
4454 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004455}
4456
Thomas Wouters52152252000-08-17 22:55:00 +00004457static PyObject *
4458import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 x = PyObject_GetAttr(v, name);
4463 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4464 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4465 }
4466 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004467}
Guido van Rossumac7be682001-01-17 15:42:30 +00004468
Thomas Wouters52152252000-08-17 22:55:00 +00004469static int
4470import_all_from(PyObject *locals, PyObject *v)
4471{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004472 _Py_IDENTIFIER(__all__);
4473 _Py_IDENTIFIER(__dict__);
4474 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 PyObject *dict, *name, *value;
4476 int skip_leading_underscores = 0;
4477 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004479 if (all == NULL) {
4480 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4481 return -1; /* Unexpected error */
4482 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004483 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 if (dict == NULL) {
4485 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4486 return -1;
4487 PyErr_SetString(PyExc_ImportError,
4488 "from-import-* object has no __dict__ and no __all__");
4489 return -1;
4490 }
4491 all = PyMapping_Keys(dict);
4492 Py_DECREF(dict);
4493 if (all == NULL)
4494 return -1;
4495 skip_leading_underscores = 1;
4496 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 for (pos = 0, err = 0; ; pos++) {
4499 name = PySequence_GetItem(all, pos);
4500 if (name == NULL) {
4501 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4502 err = -1;
4503 else
4504 PyErr_Clear();
4505 break;
4506 }
4507 if (skip_leading_underscores &&
4508 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004509 PyUnicode_READY(name) != -1 &&
4510 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 {
4512 Py_DECREF(name);
4513 continue;
4514 }
4515 value = PyObject_GetAttr(v, name);
4516 if (value == NULL)
4517 err = -1;
4518 else if (PyDict_CheckExact(locals))
4519 err = PyDict_SetItem(locals, name, value);
4520 else
4521 err = PyObject_SetItem(locals, name, value);
4522 Py_DECREF(name);
4523 Py_XDECREF(value);
4524 if (err != 0)
4525 break;
4526 }
4527 Py_DECREF(all);
4528 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004529}
4530
Guido van Rossumac7be682001-01-17 15:42:30 +00004531static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004532format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 if (!obj)
4537 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004539 obj_str = _PyUnicode_AsString(obj);
4540 if (!obj_str)
4541 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004544}
Guido van Rossum950361c1997-01-24 13:49:28 +00004545
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004546static void
4547format_exc_unbound(PyCodeObject *co, int oparg)
4548{
4549 PyObject *name;
4550 /* Don't stomp existing exception */
4551 if (PyErr_Occurred())
4552 return;
4553 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4554 name = PyTuple_GET_ITEM(co->co_cellvars,
4555 oparg);
4556 format_exc_check_arg(
4557 PyExc_UnboundLocalError,
4558 UNBOUNDLOCAL_ERROR_MSG,
4559 name);
4560 } else {
4561 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4562 PyTuple_GET_SIZE(co->co_cellvars));
4563 format_exc_check_arg(PyExc_NameError,
4564 UNBOUNDFREE_ERROR_MSG, name);
4565 }
4566}
4567
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004568static PyObject *
4569unicode_concatenate(PyObject *v, PyObject *w,
4570 PyFrameObject *f, unsigned char *next_instr)
4571{
4572 PyObject *res;
4573 if (Py_REFCNT(v) == 2) {
4574 /* In the common case, there are 2 references to the value
4575 * stored in 'variable' when the += is performed: one on the
4576 * value stack (in 'v') and one still stored in the
4577 * 'variable'. We try to delete the variable now to reduce
4578 * the refcnt to 1.
4579 */
4580 switch (*next_instr) {
4581 case STORE_FAST:
4582 {
4583 int oparg = PEEKARG();
4584 PyObject **fastlocals = f->f_localsplus;
4585 if (GETLOCAL(oparg) == v)
4586 SETLOCAL(oparg, NULL);
4587 break;
4588 }
4589 case STORE_DEREF:
4590 {
4591 PyObject **freevars = (f->f_localsplus +
4592 f->f_code->co_nlocals);
4593 PyObject *c = freevars[PEEKARG()];
4594 if (PyCell_GET(c) == v)
4595 PyCell_Set(c, NULL);
4596 break;
4597 }
4598 case STORE_NAME:
4599 {
4600 PyObject *names = f->f_code->co_names;
4601 PyObject *name = GETITEM(names, PEEKARG());
4602 PyObject *locals = f->f_locals;
4603 if (PyDict_CheckExact(locals) &&
4604 PyDict_GetItem(locals, name) == v) {
4605 if (PyDict_DelItem(locals, name) != 0) {
4606 PyErr_Clear();
4607 }
4608 }
4609 break;
4610 }
4611 }
4612 }
4613 res = v;
4614 PyUnicode_Append(&res, w);
4615 return res;
4616}
4617
Guido van Rossum950361c1997-01-24 13:49:28 +00004618#ifdef DYNAMIC_EXECUTION_PROFILE
4619
Skip Montanarof118cb12001-10-15 20:51:38 +00004620static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004621getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004623 int i;
4624 PyObject *l = PyList_New(256);
4625 if (l == NULL) return NULL;
4626 for (i = 0; i < 256; i++) {
4627 PyObject *x = PyLong_FromLong(a[i]);
4628 if (x == NULL) {
4629 Py_DECREF(l);
4630 return NULL;
4631 }
4632 PyList_SetItem(l, i, x);
4633 }
4634 for (i = 0; i < 256; i++)
4635 a[i] = 0;
4636 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004637}
4638
4639PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004640_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004641{
4642#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004644#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 int i;
4646 PyObject *l = PyList_New(257);
4647 if (l == NULL) return NULL;
4648 for (i = 0; i < 257; i++) {
4649 PyObject *x = getarray(dxpairs[i]);
4650 if (x == NULL) {
4651 Py_DECREF(l);
4652 return NULL;
4653 }
4654 PyList_SetItem(l, i, x);
4655 }
4656 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004657#endif
4658}
4659
4660#endif