blob: 686a54dafe2f8d51c281a3390fb1f6a004b40064 [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 Peterson876b2f22009-06-28 03:18:59 +0000141static PyObject * special_lookup(PyObject *, char *, PyObject **);
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *threading, *result;
376 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (!gil_created())
379 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 recreate_gil();
381 pending_lock = PyThread_allocate_lock();
382 take_gil(tstate);
383 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* Update the threading module with the new state.
386 */
387 tstate = PyThreadState_GET();
388 threading = PyMapping_GetItemString(tstate->interp->modules,
389 "threading");
390 if (threading == NULL) {
391 /* threading not imported */
392 PyErr_Clear();
393 return;
394 }
395 result = PyObject_CallMethod(threading, "_after_fork", NULL);
396 if (result == NULL)
397 PyErr_WriteUnraisable(threading);
398 else
399 Py_DECREF(result);
400 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000401}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000402
403#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000404static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000405static int pending_async_exc = 0;
406#endif /* WITH_THREAD */
407
408/* This function is used to signal that async exceptions are waiting to be
409 raised, therefore it is also useful in non-threaded builds. */
410
411void
412_PyEval_SignalAsyncExc(void)
413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000415}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000416
Guido van Rossumff4949e1992-08-05 19:58:53 +0000417/* Functions save_thread and restore_thread are always defined so
418 dynamically loaded modules needn't be compiled separately for use
419 with and without threads: */
420
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000421PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000422PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 PyThreadState *tstate = PyThreadState_Swap(NULL);
425 if (tstate == NULL)
426 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000427#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (gil_created())
429 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000430#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432}
433
434void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (tstate == NULL)
438 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000439#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (gil_created()) {
441 int err = errno;
442 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200443 /* _Py_Finalizing is protected by the GIL */
444 if (_Py_Finalizing && tstate != _Py_Finalizing) {
445 drop_gil(tstate);
446 PyThread_exit_thread();
447 assert(0); /* unreachable */
448 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 errno = err;
450 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000451#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000453}
454
455
Guido van Rossuma9672091994-09-14 13:31:22 +0000456/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
457 signal handlers or Mac I/O completion routines) can schedule calls
458 to a function to be called synchronously.
459 The synchronous function is called with one void* argument.
460 It should return 0 for success or -1 for failure -- failure should
461 be accompanied by an exception.
462
463 If registry succeeds, the registry function returns 0; if it fails
464 (e.g. due to too many pending calls) it returns -1 (without setting
465 an exception condition).
466
467 Note that because registry may occur from within signal handlers,
468 or other asynchronous events, calling malloc() is unsafe!
469
470#ifdef WITH_THREAD
471 Any thread can schedule pending calls, but only the main thread
472 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000473 There is no facility to schedule calls to a particular thread, but
474 that should be easy to change, should that ever be required. In
475 that case, the static variables here should go into the python
476 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000477#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000478*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000479
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480#ifdef WITH_THREAD
481
482/* The WITH_THREAD implementation is thread-safe. It allows
483 scheduling to be made from any thread, and even from an executing
484 callback.
485 */
486
487#define NPENDINGCALLS 32
488static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 int (*func)(void *);
490 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000491} pendingcalls[NPENDINGCALLS];
492static int pendingfirst = 0;
493static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000494
495int
496Py_AddPendingCall(int (*func)(void *), void *arg)
497{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 int i, j, result=0;
499 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 /* try a few times for the lock. Since this mechanism is used
502 * for signal handling (on the main thread), there is a (slim)
503 * chance that a signal is delivered on the same thread while we
504 * hold the lock during the Py_MakePendingCalls() function.
505 * This avoids a deadlock in that case.
506 * Note that signals can be delivered on any thread. In particular,
507 * on Windows, a SIGINT is delivered on a system-created worker
508 * thread.
509 * We also check for lock being NULL, in the unlikely case that
510 * this function is called before any bytecode evaluation takes place.
511 */
512 if (lock != NULL) {
513 for (i = 0; i<100; i++) {
514 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
515 break;
516 }
517 if (i == 100)
518 return -1;
519 }
520
521 i = pendinglast;
522 j = (i + 1) % NPENDINGCALLS;
523 if (j == pendingfirst) {
524 result = -1; /* Queue full */
525 } else {
526 pendingcalls[i].func = func;
527 pendingcalls[i].arg = arg;
528 pendinglast = j;
529 }
530 /* signal main loop */
531 SIGNAL_PENDING_CALLS();
532 if (lock != NULL)
533 PyThread_release_lock(lock);
534 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000535}
536
537int
538Py_MakePendingCalls(void)
539{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200540 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 int i;
542 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (!pending_lock) {
545 /* initial allocation of the lock */
546 pending_lock = PyThread_allocate_lock();
547 if (pending_lock == NULL)
548 return -1;
549 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* only service pending calls on main thread */
552 if (main_thread && PyThread_get_thread_ident() != main_thread)
553 return 0;
554 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200555 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200557 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000558 /* perform a bounded number of calls, in case of recursion */
559 for (i=0; i<NPENDINGCALLS; i++) {
560 int j;
561 int (*func)(void *);
562 void *arg = NULL;
563
564 /* pop one item off the queue while holding the lock */
565 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
566 j = pendingfirst;
567 if (j == pendinglast) {
568 func = NULL; /* Queue empty */
569 } else {
570 func = pendingcalls[j].func;
571 arg = pendingcalls[j].arg;
572 pendingfirst = (j + 1) % NPENDINGCALLS;
573 }
574 if (pendingfirst != pendinglast)
575 SIGNAL_PENDING_CALLS();
576 else
577 UNSIGNAL_PENDING_CALLS();
578 PyThread_release_lock(pending_lock);
579 /* having released the lock, perform the callback */
580 if (func == NULL)
581 break;
582 r = func(arg);
583 if (r)
584 break;
585 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200586 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000588}
589
590#else /* if ! defined WITH_THREAD */
591
592/*
593 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
594 This code is used for signal handling in python that isn't built
595 with WITH_THREAD.
596 Don't use this implementation when Py_AddPendingCalls() can happen
597 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598
Guido van Rossuma9672091994-09-14 13:31:22 +0000599 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000600 (1) nested asynchronous calls to Py_AddPendingCall()
601 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000603 (1) is very unlikely because typically signal delivery
604 is blocked during signal handling. So it should be impossible.
605 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000606 The current code is safe against (2), but not against (1).
607 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000608 thread is present, interrupted by signals, and that the critical
609 section is protected with the "busy" variable. On Windows, which
610 delivers SIGINT on a system thread, this does not hold and therefore
611 Windows really shouldn't use this version.
612 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000613*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000614
Guido van Rossuma9672091994-09-14 13:31:22 +0000615#define NPENDINGCALLS 32
616static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 int (*func)(void *);
618 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000619} pendingcalls[NPENDINGCALLS];
620static volatile int pendingfirst = 0;
621static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000622static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000623
624int
Thomas Wouters334fb892000-07-25 12:56:38 +0000625Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 static volatile int busy = 0;
628 int i, j;
629 /* XXX Begin critical section */
630 if (busy)
631 return -1;
632 busy = 1;
633 i = pendinglast;
634 j = (i + 1) % NPENDINGCALLS;
635 if (j == pendingfirst) {
636 busy = 0;
637 return -1; /* Queue full */
638 }
639 pendingcalls[i].func = func;
640 pendingcalls[i].arg = arg;
641 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 SIGNAL_PENDING_CALLS();
644 busy = 0;
645 /* XXX End critical section */
646 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000647}
648
Guido van Rossum180d7b41994-09-29 09:45:57 +0000649int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000650Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 static int busy = 0;
653 if (busy)
654 return 0;
655 busy = 1;
656 UNSIGNAL_PENDING_CALLS();
657 for (;;) {
658 int i;
659 int (*func)(void *);
660 void *arg;
661 i = pendingfirst;
662 if (i == pendinglast)
663 break; /* Queue empty */
664 func = pendingcalls[i].func;
665 arg = pendingcalls[i].arg;
666 pendingfirst = (i + 1) % NPENDINGCALLS;
667 if (func(arg) < 0) {
668 busy = 0;
669 SIGNAL_PENDING_CALLS(); /* We're not done yet */
670 return -1;
671 }
672 }
673 busy = 0;
674 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000675}
676
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000677#endif /* WITH_THREAD */
678
Guido van Rossuma9672091994-09-14 13:31:22 +0000679
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000680/* The interpreter's recursion limit */
681
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000682#ifndef Py_DEFAULT_RECURSION_LIMIT
683#define Py_DEFAULT_RECURSION_LIMIT 1000
684#endif
685static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
686int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000687
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000688int
689Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000692}
693
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000694void
695Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 recursion_limit = new_limit;
698 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000699}
700
Armin Rigo2b3eb402003-10-28 12:05:48 +0000701/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
702 if the recursion_depth reaches _Py_CheckRecursionLimit.
703 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
704 to guarantee that _Py_CheckRecursiveCall() is regularly called.
705 Without USE_STACKCHECK, there is no need for this. */
706int
707_Py_CheckRecursiveCall(char *where)
708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000710
711#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 if (PyOS_CheckStack()) {
713 --tstate->recursion_depth;
714 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
715 return -1;
716 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000717#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 _Py_CheckRecursionLimit = recursion_limit;
719 if (tstate->recursion_critical)
720 /* Somebody asked that we don't check for recursion. */
721 return 0;
722 if (tstate->overflowed) {
723 if (tstate->recursion_depth > recursion_limit + 50) {
724 /* Overflowing while handling an overflow. Give up. */
725 Py_FatalError("Cannot recover from stack overflow.");
726 }
727 return 0;
728 }
729 if (tstate->recursion_depth > recursion_limit) {
730 --tstate->recursion_depth;
731 tstate->overflowed = 1;
732 PyErr_Format(PyExc_RuntimeError,
733 "maximum recursion depth exceeded%s",
734 where);
735 return -1;
736 }
737 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000738}
739
Guido van Rossum374a9221991-04-04 10:40:29 +0000740/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000741enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000742 WHY_NOT = 0x0001, /* No error */
743 WHY_EXCEPTION = 0x0002, /* Exception occurred */
744 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
745 WHY_RETURN = 0x0008, /* 'return' statement */
746 WHY_BREAK = 0x0010, /* 'break' statement */
747 WHY_CONTINUE = 0x0020, /* 'continue' statement */
748 WHY_YIELD = 0x0040, /* 'yield' operator */
749 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000750};
Guido van Rossum374a9221991-04-04 10:40:29 +0000751
Benjamin Peterson87880242011-07-03 16:48:31 -0500752static void save_exc_state(PyThreadState *, PyFrameObject *);
753static void swap_exc_state(PyThreadState *, PyFrameObject *);
754static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Collin Winter828f04a2007-08-31 00:04:24 +0000755static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000756static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000757
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000758/* Records whether tracing is on for any thread. Counts the number of
759 threads for which tstate->c_tracefunc is non-NULL, so if the value
760 is 0, we know we don't have to check this thread's c_tracefunc.
761 This speeds up the if statement in PyEval_EvalFrameEx() after
762 fast_next_opcode*/
763static int _Py_TracingPossible = 0;
764
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000765
Guido van Rossum374a9221991-04-04 10:40:29 +0000766
Guido van Rossumb209a111997-04-29 18:18:01 +0000767PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000768PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 return PyEval_EvalCodeEx(co,
771 globals, locals,
772 (PyObject **)NULL, 0,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000776}
777
778
779/* Interpreter main loop */
780
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000781PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000782PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /* This is for backward compatibility with extension modules that
784 used this API; core interpreter code should call
785 PyEval_EvalFrameEx() */
786 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000787}
788
789PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000791{
Guido van Rossum950361c1997-01-24 13:49:28 +0000792#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000794#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 register PyObject **stack_pointer; /* Next free slot in value stack */
796 register unsigned char *next_instr;
797 register int opcode; /* Current opcode */
798 register int oparg; /* Current opcode argument, if any */
799 register enum why_code why; /* Reason for block stack unwind */
800 register int err; /* Error status -- nonzero if error */
801 register PyObject *x; /* Result object -- NULL if error */
802 register PyObject *v; /* Temporary objects popped off stack */
803 register PyObject *w;
804 register PyObject *u;
805 register PyObject *t;
806 register PyObject **fastlocals, **freevars;
807 PyObject *retval = NULL; /* Return value */
808 PyThreadState *tstate = PyThreadState_GET();
809 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 is true when the line being executed has changed. The
816 initial values are such as to make this false the first
817 time it is tested. */
818 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000820 unsigned char *first_instr;
821 PyObject *names;
822 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000823
Antoine Pitroub52ec782009-01-25 16:34:23 +0000824/* Computed GOTOs, or
825 the-optimization-commonly-but-improperly-known-as-"threaded code"
826 using gcc's labels-as-values extension
827 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
828
829 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000831 combined with a lookup table of jump addresses. However, since the
832 indirect jump instruction is shared by all opcodes, the CPU will have a
833 hard time making the right prediction for where to jump next (actually,
834 it will be always wrong except in the uncommon case of a sequence of
835 several identical opcodes).
836
837 "Threaded code" in contrast, uses an explicit jump table and an explicit
838 indirect jump instruction at the end of each opcode. Since the jump
839 instruction is at a different address for each opcode, the CPU will make a
840 separate prediction for each of these instructions, which is equivalent to
841 predicting the second opcode of each opcode pair. These predictions have
842 a much better chance to turn out valid, especially in small bytecode loops.
843
844 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000846 and potentially many more instructions (depending on the pipeline width).
847 A correctly predicted branch, however, is nearly free.
848
849 At the time of this writing, the "threaded code" version is up to 15-20%
850 faster than the normal "switch" version, depending on the compiler and the
851 CPU architecture.
852
853 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
854 because it would render the measurements invalid.
855
856
857 NOTE: care must be taken that the compiler doesn't try to "optimize" the
858 indirect jumps by sharing them between all opcodes. Such optimizations
859 can be disabled on gcc by using the -fno-gcse flag (or possibly
860 -fno-crossjumping).
861*/
862
Antoine Pitrou042b1282010-08-13 21:15:58 +0000863#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000864#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000865#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000866#endif
867
Antoine Pitrou042b1282010-08-13 21:15:58 +0000868#ifdef HAVE_COMPUTED_GOTOS
869 #ifndef USE_COMPUTED_GOTOS
870 #define USE_COMPUTED_GOTOS 1
871 #endif
872#else
873 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
874 #error "Computed gotos are not supported on this compiler."
875 #endif
876 #undef USE_COMPUTED_GOTOS
877 #define USE_COMPUTED_GOTOS 0
878#endif
879
880#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000881/* Import the static jump table */
882#include "opcode_targets.h"
883
884/* This macro is used when several opcodes defer to the same implementation
885 (e.g. SETUP_LOOP, SETUP_FINALLY) */
886#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 TARGET_##op: \
888 opcode = op; \
889 if (HAS_ARG(op)) \
890 oparg = NEXTARG(); \
891 case op: \
892 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000893
894#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 TARGET_##op: \
896 opcode = op; \
897 if (HAS_ARG(op)) \
898 oparg = NEXTARG(); \
899 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900
901
902#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 { \
904 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
905 FAST_DISPATCH(); \
906 } \
907 continue; \
908 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000909
910#ifdef LLTRACE
911#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 { \
913 if (!lltrace && !_Py_TracingPossible) { \
914 f->f_lasti = INSTR_OFFSET(); \
915 goto *opcode_targets[*next_instr++]; \
916 } \
917 goto fast_next_opcode; \
918 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919#else
920#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 { \
922 if (!_Py_TracingPossible) { \
923 f->f_lasti = INSTR_OFFSET(); \
924 goto *opcode_targets[*next_instr++]; \
925 } \
926 goto fast_next_opcode; \
927 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000928#endif
929
930#else
931#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000933#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 /* silence compiler warnings about `impl` unused */ \
935 if (0) goto impl; \
936 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000937#define DISPATCH() continue
938#define FAST_DISPATCH() goto fast_next_opcode
939#endif
940
941
Neal Norwitza81d2202002-07-14 00:27:26 +0000942/* Tuple access macros */
943
944#ifndef Py_DEBUG
945#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
946#else
947#define GETITEM(v, i) PyTuple_GetItem((v), (i))
948#endif
949
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000950#ifdef WITH_TSC
951/* Use Pentium timestamp counter to mark certain events:
952 inst0 -- beginning of switch statement for opcode dispatch
953 inst1 -- end of switch statement (may be skipped)
954 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000955 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000956 (may be skipped)
957 intr1 -- beginning of long interruption
958 intr2 -- end of long interruption
959
960 Many opcodes call out to helper C functions. In some cases, the
961 time in those functions should be counted towards the time for the
962 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
963 calls another Python function; there's no point in charge all the
964 bytecode executed by the called function to the caller.
965
966 It's hard to make a useful judgement statically. In the presence
967 of operator overloading, it's impossible to tell if a call will
968 execute new Python code or not.
969
970 It's a case-by-case judgement. I'll use intr1 for the following
971 cases:
972
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000973 IMPORT_STAR
974 IMPORT_FROM
975 CALL_FUNCTION (and friends)
976
977 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
979 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 READ_TIMESTAMP(inst0);
982 READ_TIMESTAMP(inst1);
983 READ_TIMESTAMP(loop0);
984 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 /* shut up the compiler */
987 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000988#endif
989
Guido van Rossum374a9221991-04-04 10:40:29 +0000990/* Code access macros */
991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992#define INSTR_OFFSET() ((int)(next_instr - first_instr))
993#define NEXTOP() (*next_instr++)
994#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
995#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
996#define JUMPTO(x) (next_instr = first_instr + (x))
997#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000998
Raymond Hettingerf606f872003-03-16 03:11:04 +0000999/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 Some opcodes tend to come in pairs thus making it possible to
1001 predict the second code when the first is run. For example,
1002 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1003 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 Verifying the prediction costs a single high-speed test of a register
1006 variable against a constant. If the pairing was good, then the
1007 processor's own internal branch predication has a high likelihood of
1008 success, resulting in a nearly zero-overhead transition to the
1009 next opcode. A successful prediction saves a trip through the eval-loop
1010 including its two unpredictable branches, the HAS_ARG test and the
1011 switch-case. Combined with the processor's internal branch prediction,
1012 a successful PREDICT has the effect of making the two opcodes run as if
1013 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001014
Georg Brandl86b2fb92008-07-16 03:43:04 +00001015 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 predictions turned-on and interpret the results as if some opcodes
1017 had been combined or turn-off predictions so that the opcode frequency
1018 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001019
1020 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 the CPU to record separate branch prediction information for each
1022 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001023
Raymond Hettingerf606f872003-03-16 03:11:04 +00001024*/
1025
Antoine Pitrou042b1282010-08-13 21:15:58 +00001026#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027#define PREDICT(op) if (0) goto PRED_##op
1028#define PREDICTED(op) PRED_##op:
1029#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001030#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1032#define PREDICTED(op) PRED_##op: next_instr++
1033#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001034#endif
1035
Raymond Hettingerf606f872003-03-16 03:11:04 +00001036
Guido van Rossum374a9221991-04-04 10:40:29 +00001037/* Stack manipulation macros */
1038
Martin v. Löwis18e16552006-02-15 17:27:45 +00001039/* The stack can grow at most MAXINT deep, as co_nlocals and
1040 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001041#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1042#define EMPTY() (STACK_LEVEL() == 0)
1043#define TOP() (stack_pointer[-1])
1044#define SECOND() (stack_pointer[-2])
1045#define THIRD() (stack_pointer[-3])
1046#define FOURTH() (stack_pointer[-4])
1047#define PEEK(n) (stack_pointer[-(n)])
1048#define SET_TOP(v) (stack_pointer[-1] = (v))
1049#define SET_SECOND(v) (stack_pointer[-2] = (v))
1050#define SET_THIRD(v) (stack_pointer[-3] = (v))
1051#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1052#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1053#define BASIC_STACKADJ(n) (stack_pointer += n)
1054#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1055#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001056
Guido van Rossum96a42c81992-01-12 02:29:51 +00001057#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001059 lltrace && prtrace(TOP(), "push")); \
1060 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001062 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001064 lltrace && prtrace(TOP(), "stackadj")); \
1065 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001066#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001067 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1068 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001069#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001070#define PUSH(v) BASIC_PUSH(v)
1071#define POP() BASIC_POP()
1072#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001073#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001074#endif
1075
Guido van Rossum681d79a1995-07-18 14:51:37 +00001076/* Local variable macros */
1077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001079
1080/* The SETLOCAL() macro must not DECREF the local variable in-place and
1081 then store the new value; it must copy the old value to a temporary
1082 value, then store the new value, and then DECREF the temporary value.
1083 This is because it is possible that during the DECREF the frame is
1084 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1085 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001087 GETLOCAL(i) = value; \
1088 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001089
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001090
1091#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 while (STACK_LEVEL() > (b)->b_level) { \
1093 PyObject *v = POP(); \
1094 Py_XDECREF(v); \
1095 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001096
1097#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 { \
1099 PyObject *type, *value, *traceback; \
1100 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1101 while (STACK_LEVEL() > (b)->b_level + 3) { \
1102 value = POP(); \
1103 Py_XDECREF(value); \
1104 } \
1105 type = tstate->exc_type; \
1106 value = tstate->exc_value; \
1107 traceback = tstate->exc_traceback; \
1108 tstate->exc_type = POP(); \
1109 tstate->exc_value = POP(); \
1110 tstate->exc_traceback = POP(); \
1111 Py_XDECREF(type); \
1112 Py_XDECREF(value); \
1113 Py_XDECREF(traceback); \
1114 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001115
Guido van Rossuma027efa1997-05-05 20:56:21 +00001116/* Start of code */
1117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 /* push frame */
1119 if (Py_EnterRecursiveCall(""))
1120 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 if (tstate->use_tracing) {
1125 if (tstate->c_tracefunc != NULL) {
1126 /* tstate->c_tracefunc, if defined, is a
1127 function that will be called on *every* entry
1128 to a code block. Its return value, if not
1129 None, is a function that will be called at
1130 the start of each executed line of code.
1131 (Actually, the function must return itself
1132 in order to continue tracing.) The trace
1133 functions are called with three arguments:
1134 a pointer to the current frame, a string
1135 indicating why the function is called, and
1136 an argument which depends on the situation.
1137 The global trace function is also called
1138 whenever an exception is detected. */
1139 if (call_trace_protected(tstate->c_tracefunc,
1140 tstate->c_traceobj,
1141 f, PyTrace_CALL, Py_None)) {
1142 /* Trace function raised an error */
1143 goto exit_eval_frame;
1144 }
1145 }
1146 if (tstate->c_profilefunc != NULL) {
1147 /* Similar for c_profilefunc, except it needn't
1148 return itself and isn't called for "line" events */
1149 if (call_trace_protected(tstate->c_profilefunc,
1150 tstate->c_profileobj,
1151 f, PyTrace_CALL, Py_None)) {
1152 /* Profile function raised an error */
1153 goto exit_eval_frame;
1154 }
1155 }
1156 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 co = f->f_code;
1159 names = co->co_names;
1160 consts = co->co_consts;
1161 fastlocals = f->f_localsplus;
1162 freevars = f->f_localsplus + co->co_nlocals;
1163 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1164 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 f->f_lasti now refers to the index of the last instruction
1167 executed. You might think this was obvious from the name, but
1168 this wasn't always true before 2.3! PyFrame_New now sets
1169 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1170 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1171 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 When the PREDICT() macros are enabled, some opcode pairs follow in
1174 direct succession without updating f->f_lasti. A successful
1175 prediction effectively links the two codes together as if they
1176 were a single new opcode; accordingly,f->f_lasti will point to
1177 the first code in the pair (for instance, GET_ITER followed by
1178 FOR_ITER is effectively a single opcode and f->f_lasti will point
1179 at to the beginning of the combined pair.)
1180 */
1181 next_instr = first_instr + f->f_lasti + 1;
1182 stack_pointer = f->f_stacktop;
1183 assert(stack_pointer != NULL);
1184 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (co->co_flags & CO_GENERATOR && !throwflag) {
1187 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1188 /* We were in an except handler when we left,
1189 restore the exception state which was put aside
1190 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001191 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001193 else
1194 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001196
Tim Peters5ca576e2001-06-18 22:08:13 +00001197#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001199#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 why = WHY_NOT;
1202 err = 0;
1203 x = Py_None; /* Not a reference, just anything non-NULL */
1204 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 if (throwflag) { /* support for generator.throw() */
1207 why = WHY_EXCEPTION;
1208 goto on_error;
1209 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001212#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001213 if (inst1 == 0) {
1214 /* Almost surely, the opcode executed a break
1215 or a continue, preventing inst1 from being set
1216 on the way out of the loop.
1217 */
1218 READ_TIMESTAMP(inst1);
1219 loop1 = inst1;
1220 }
1221 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1222 intr0, intr1);
1223 ticked = 0;
1224 inst1 = 0;
1225 intr0 = 0;
1226 intr1 = 0;
1227 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001228#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001229 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1230 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 /* Do periodic things. Doing this every time through
1233 the loop would add too much overhead, so we do it
1234 only every Nth instruction. We also do it if
1235 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1236 event needs attention (e.g. a signal handler or
1237 async I/O handler); see Py_AddPendingCall() and
1238 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001239
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1241 if (*next_instr == SETUP_FINALLY) {
1242 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001243 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 goto fast_next_opcode;
1245 }
1246 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001247#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001249#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1251 if (Py_MakePendingCalls() < 0) {
1252 why = WHY_EXCEPTION;
1253 goto on_error;
1254 }
1255 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001256#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001257 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 /* Give another thread a chance */
1259 if (PyThreadState_Swap(NULL) != tstate)
1260 Py_FatalError("ceval: tstate mix-up");
1261 drop_gil(tstate);
1262
1263 /* Other threads may run now */
1264
1265 take_gil(tstate);
1266 if (PyThreadState_Swap(tstate) != NULL)
1267 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001268 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 /* Check for asynchronous exceptions. */
1271 if (tstate->async_exc != NULL) {
1272 x = tstate->async_exc;
1273 tstate->async_exc = NULL;
1274 UNSIGNAL_ASYNC_EXC();
1275 PyErr_SetNone(x);
1276 Py_DECREF(x);
1277 why = WHY_EXCEPTION;
1278 goto on_error;
1279 }
1280 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 fast_next_opcode:
1283 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 if (_Py_TracingPossible &&
1288 tstate->c_tracefunc != NULL && !tstate->tracing) {
1289 /* see maybe_call_line_trace
1290 for expository comments */
1291 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 err = maybe_call_line_trace(tstate->c_tracefunc,
1294 tstate->c_traceobj,
1295 f, &instr_lb, &instr_ub,
1296 &instr_prev);
1297 /* Reload possibly changed frame fields */
1298 JUMPTO(f->f_lasti);
1299 if (f->f_stacktop != NULL) {
1300 stack_pointer = f->f_stacktop;
1301 f->f_stacktop = NULL;
1302 }
1303 if (err) {
1304 /* trace function raised an exception */
1305 goto on_error;
1306 }
1307 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 opcode = NEXTOP();
1312 oparg = 0; /* allows oparg to be stored in a register because
1313 it doesn't have to be remembered across a full loop */
1314 if (HAS_ARG(opcode))
1315 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001316 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001317#ifdef DYNAMIC_EXECUTION_PROFILE
1318#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 dxpairs[lastopcode][opcode]++;
1320 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001321#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001323#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001324
Guido van Rossum96a42c81992-01-12 02:29:51 +00001325#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 if (lltrace) {
1329 if (HAS_ARG(opcode)) {
1330 printf("%d: %d, %d\n",
1331 f->f_lasti, opcode, oparg);
1332 }
1333 else {
1334 printf("%d: %d\n",
1335 f->f_lasti, opcode);
1336 }
1337 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001338#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 /* Main switch on opcode */
1341 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 /* BEWARE!
1346 It is essential that any operation that fails sets either
1347 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1348 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 TARGET(NOP)
1351 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 TARGET(LOAD_FAST)
1354 x = GETLOCAL(oparg);
1355 if (x != NULL) {
1356 Py_INCREF(x);
1357 PUSH(x);
1358 FAST_DISPATCH();
1359 }
1360 format_exc_check_arg(PyExc_UnboundLocalError,
1361 UNBOUNDLOCAL_ERROR_MSG,
1362 PyTuple_GetItem(co->co_varnames, oparg));
1363 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 TARGET(LOAD_CONST)
1366 x = GETITEM(consts, oparg);
1367 Py_INCREF(x);
1368 PUSH(x);
1369 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 PREDICTED_WITH_ARG(STORE_FAST);
1372 TARGET(STORE_FAST)
1373 v = POP();
1374 SETLOCAL(oparg, v);
1375 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 TARGET(POP_TOP)
1378 v = POP();
1379 Py_DECREF(v);
1380 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 TARGET(ROT_TWO)
1383 v = TOP();
1384 w = SECOND();
1385 SET_TOP(w);
1386 SET_SECOND(v);
1387 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 TARGET(ROT_THREE)
1390 v = TOP();
1391 w = SECOND();
1392 x = THIRD();
1393 SET_TOP(w);
1394 SET_SECOND(x);
1395 SET_THIRD(v);
1396 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 TARGET(DUP_TOP)
1399 v = TOP();
1400 Py_INCREF(v);
1401 PUSH(v);
1402 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001403
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001404 TARGET(DUP_TOP_TWO)
1405 x = TOP();
1406 Py_INCREF(x);
1407 w = SECOND();
1408 Py_INCREF(w);
1409 STACKADJ(2);
1410 SET_TOP(x);
1411 SET_SECOND(w);
1412 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 TARGET(UNARY_POSITIVE)
1415 v = TOP();
1416 x = PyNumber_Positive(v);
1417 Py_DECREF(v);
1418 SET_TOP(x);
1419 if (x != NULL) DISPATCH();
1420 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 TARGET(UNARY_NEGATIVE)
1423 v = TOP();
1424 x = PyNumber_Negative(v);
1425 Py_DECREF(v);
1426 SET_TOP(x);
1427 if (x != NULL) DISPATCH();
1428 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 TARGET(UNARY_NOT)
1431 v = TOP();
1432 err = PyObject_IsTrue(v);
1433 Py_DECREF(v);
1434 if (err == 0) {
1435 Py_INCREF(Py_True);
1436 SET_TOP(Py_True);
1437 DISPATCH();
1438 }
1439 else if (err > 0) {
1440 Py_INCREF(Py_False);
1441 SET_TOP(Py_False);
1442 err = 0;
1443 DISPATCH();
1444 }
1445 STACKADJ(-1);
1446 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 TARGET(UNARY_INVERT)
1449 v = TOP();
1450 x = PyNumber_Invert(v);
1451 Py_DECREF(v);
1452 SET_TOP(x);
1453 if (x != NULL) DISPATCH();
1454 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 TARGET(BINARY_POWER)
1457 w = POP();
1458 v = TOP();
1459 x = PyNumber_Power(v, w, Py_None);
1460 Py_DECREF(v);
1461 Py_DECREF(w);
1462 SET_TOP(x);
1463 if (x != NULL) DISPATCH();
1464 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 TARGET(BINARY_MULTIPLY)
1467 w = POP();
1468 v = TOP();
1469 x = PyNumber_Multiply(v, w);
1470 Py_DECREF(v);
1471 Py_DECREF(w);
1472 SET_TOP(x);
1473 if (x != NULL) DISPATCH();
1474 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 TARGET(BINARY_TRUE_DIVIDE)
1477 w = POP();
1478 v = TOP();
1479 x = PyNumber_TrueDivide(v, w);
1480 Py_DECREF(v);
1481 Py_DECREF(w);
1482 SET_TOP(x);
1483 if (x != NULL) DISPATCH();
1484 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 TARGET(BINARY_FLOOR_DIVIDE)
1487 w = POP();
1488 v = TOP();
1489 x = PyNumber_FloorDivide(v, w);
1490 Py_DECREF(v);
1491 Py_DECREF(w);
1492 SET_TOP(x);
1493 if (x != NULL) DISPATCH();
1494 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 TARGET(BINARY_MODULO)
1497 w = POP();
1498 v = TOP();
1499 if (PyUnicode_CheckExact(v))
1500 x = PyUnicode_Format(v, w);
1501 else
1502 x = PyNumber_Remainder(v, w);
1503 Py_DECREF(v);
1504 Py_DECREF(w);
1505 SET_TOP(x);
1506 if (x != NULL) DISPATCH();
1507 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 TARGET(BINARY_ADD)
1510 w = POP();
1511 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001512 if (PyUnicode_CheckExact(v) &&
1513 PyUnicode_CheckExact(w)) {
1514 x = unicode_concatenate(v, w, f, next_instr);
1515 /* unicode_concatenate consumed the ref to v */
1516 goto skip_decref_vx;
1517 }
1518 else {
1519 x = PyNumber_Add(v, w);
1520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001522 skip_decref_vx:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 Py_DECREF(w);
1524 SET_TOP(x);
1525 if (x != NULL) DISPATCH();
1526 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 TARGET(BINARY_SUBTRACT)
1529 w = POP();
1530 v = TOP();
1531 x = PyNumber_Subtract(v, w);
1532 Py_DECREF(v);
1533 Py_DECREF(w);
1534 SET_TOP(x);
1535 if (x != NULL) DISPATCH();
1536 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 TARGET(BINARY_SUBSCR)
1539 w = POP();
1540 v = TOP();
1541 x = PyObject_GetItem(v, w);
1542 Py_DECREF(v);
1543 Py_DECREF(w);
1544 SET_TOP(x);
1545 if (x != NULL) DISPATCH();
1546 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 TARGET(BINARY_LSHIFT)
1549 w = POP();
1550 v = TOP();
1551 x = PyNumber_Lshift(v, w);
1552 Py_DECREF(v);
1553 Py_DECREF(w);
1554 SET_TOP(x);
1555 if (x != NULL) DISPATCH();
1556 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 TARGET(BINARY_RSHIFT)
1559 w = POP();
1560 v = TOP();
1561 x = PyNumber_Rshift(v, w);
1562 Py_DECREF(v);
1563 Py_DECREF(w);
1564 SET_TOP(x);
1565 if (x != NULL) DISPATCH();
1566 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 TARGET(BINARY_AND)
1569 w = POP();
1570 v = TOP();
1571 x = PyNumber_And(v, w);
1572 Py_DECREF(v);
1573 Py_DECREF(w);
1574 SET_TOP(x);
1575 if (x != NULL) DISPATCH();
1576 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 TARGET(BINARY_XOR)
1579 w = POP();
1580 v = TOP();
1581 x = PyNumber_Xor(v, w);
1582 Py_DECREF(v);
1583 Py_DECREF(w);
1584 SET_TOP(x);
1585 if (x != NULL) DISPATCH();
1586 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 TARGET(BINARY_OR)
1589 w = POP();
1590 v = TOP();
1591 x = PyNumber_Or(v, w);
1592 Py_DECREF(v);
1593 Py_DECREF(w);
1594 SET_TOP(x);
1595 if (x != NULL) DISPATCH();
1596 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 TARGET(LIST_APPEND)
1599 w = POP();
1600 v = PEEK(oparg);
1601 err = PyList_Append(v, w);
1602 Py_DECREF(w);
1603 if (err == 0) {
1604 PREDICT(JUMP_ABSOLUTE);
1605 DISPATCH();
1606 }
1607 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 TARGET(SET_ADD)
1610 w = POP();
1611 v = stack_pointer[-oparg];
1612 err = PySet_Add(v, w);
1613 Py_DECREF(w);
1614 if (err == 0) {
1615 PREDICT(JUMP_ABSOLUTE);
1616 DISPATCH();
1617 }
1618 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620 TARGET(INPLACE_POWER)
1621 w = POP();
1622 v = TOP();
1623 x = PyNumber_InPlacePower(v, w, Py_None);
1624 Py_DECREF(v);
1625 Py_DECREF(w);
1626 SET_TOP(x);
1627 if (x != NULL) DISPATCH();
1628 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 TARGET(INPLACE_MULTIPLY)
1631 w = POP();
1632 v = TOP();
1633 x = PyNumber_InPlaceMultiply(v, w);
1634 Py_DECREF(v);
1635 Py_DECREF(w);
1636 SET_TOP(x);
1637 if (x != NULL) DISPATCH();
1638 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 TARGET(INPLACE_TRUE_DIVIDE)
1641 w = POP();
1642 v = TOP();
1643 x = PyNumber_InPlaceTrueDivide(v, w);
1644 Py_DECREF(v);
1645 Py_DECREF(w);
1646 SET_TOP(x);
1647 if (x != NULL) DISPATCH();
1648 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 TARGET(INPLACE_FLOOR_DIVIDE)
1651 w = POP();
1652 v = TOP();
1653 x = PyNumber_InPlaceFloorDivide(v, w);
1654 Py_DECREF(v);
1655 Py_DECREF(w);
1656 SET_TOP(x);
1657 if (x != NULL) DISPATCH();
1658 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 TARGET(INPLACE_MODULO)
1661 w = POP();
1662 v = TOP();
1663 x = PyNumber_InPlaceRemainder(v, w);
1664 Py_DECREF(v);
1665 Py_DECREF(w);
1666 SET_TOP(x);
1667 if (x != NULL) DISPATCH();
1668 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 TARGET(INPLACE_ADD)
1671 w = POP();
1672 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001673 if (PyUnicode_CheckExact(v) &&
1674 PyUnicode_CheckExact(w)) {
1675 x = unicode_concatenate(v, w, f, next_instr);
1676 /* unicode_concatenate consumed the ref to v */
1677 goto skip_decref_v;
1678 }
1679 else {
1680 x = PyNumber_InPlaceAdd(v, w);
1681 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001683 skip_decref_v:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 Py_DECREF(w);
1685 SET_TOP(x);
1686 if (x != NULL) DISPATCH();
1687 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 TARGET(INPLACE_SUBTRACT)
1690 w = POP();
1691 v = TOP();
1692 x = PyNumber_InPlaceSubtract(v, w);
1693 Py_DECREF(v);
1694 Py_DECREF(w);
1695 SET_TOP(x);
1696 if (x != NULL) DISPATCH();
1697 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 TARGET(INPLACE_LSHIFT)
1700 w = POP();
1701 v = TOP();
1702 x = PyNumber_InPlaceLshift(v, w);
1703 Py_DECREF(v);
1704 Py_DECREF(w);
1705 SET_TOP(x);
1706 if (x != NULL) DISPATCH();
1707 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 TARGET(INPLACE_RSHIFT)
1710 w = POP();
1711 v = TOP();
1712 x = PyNumber_InPlaceRshift(v, w);
1713 Py_DECREF(v);
1714 Py_DECREF(w);
1715 SET_TOP(x);
1716 if (x != NULL) DISPATCH();
1717 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 TARGET(INPLACE_AND)
1720 w = POP();
1721 v = TOP();
1722 x = PyNumber_InPlaceAnd(v, w);
1723 Py_DECREF(v);
1724 Py_DECREF(w);
1725 SET_TOP(x);
1726 if (x != NULL) DISPATCH();
1727 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001729 TARGET(INPLACE_XOR)
1730 w = POP();
1731 v = TOP();
1732 x = PyNumber_InPlaceXor(v, w);
1733 Py_DECREF(v);
1734 Py_DECREF(w);
1735 SET_TOP(x);
1736 if (x != NULL) DISPATCH();
1737 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 TARGET(INPLACE_OR)
1740 w = POP();
1741 v = TOP();
1742 x = PyNumber_InPlaceOr(v, w);
1743 Py_DECREF(v);
1744 Py_DECREF(w);
1745 SET_TOP(x);
1746 if (x != NULL) DISPATCH();
1747 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 TARGET(STORE_SUBSCR)
1750 w = TOP();
1751 v = SECOND();
1752 u = THIRD();
1753 STACKADJ(-3);
1754 /* v[w] = u */
1755 err = PyObject_SetItem(v, w, u);
1756 Py_DECREF(u);
1757 Py_DECREF(v);
1758 Py_DECREF(w);
1759 if (err == 0) DISPATCH();
1760 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 TARGET(DELETE_SUBSCR)
1763 w = TOP();
1764 v = SECOND();
1765 STACKADJ(-2);
1766 /* del v[w] */
1767 err = PyObject_DelItem(v, w);
1768 Py_DECREF(v);
1769 Py_DECREF(w);
1770 if (err == 0) DISPATCH();
1771 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 TARGET(PRINT_EXPR)
1774 v = POP();
1775 w = PySys_GetObject("displayhook");
1776 if (w == NULL) {
1777 PyErr_SetString(PyExc_RuntimeError,
1778 "lost sys.displayhook");
1779 err = -1;
1780 x = NULL;
1781 }
1782 if (err == 0) {
1783 x = PyTuple_Pack(1, v);
1784 if (x == NULL)
1785 err = -1;
1786 }
1787 if (err == 0) {
1788 w = PyEval_CallObject(w, x);
1789 Py_XDECREF(w);
1790 if (w == NULL)
1791 err = -1;
1792 }
1793 Py_DECREF(v);
1794 Py_XDECREF(x);
1795 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001796
Thomas Wouters434d0822000-08-24 20:11:32 +00001797#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001798 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001799#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 TARGET(RAISE_VARARGS)
1801 v = w = NULL;
1802 switch (oparg) {
1803 case 2:
1804 v = POP(); /* cause */
1805 case 1:
1806 w = POP(); /* exc */
1807 case 0: /* Fallthrough */
1808 why = do_raise(w, v);
1809 break;
1810 default:
1811 PyErr_SetString(PyExc_SystemError,
1812 "bad RAISE_VARARGS oparg");
1813 why = WHY_EXCEPTION;
1814 break;
1815 }
1816 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 TARGET(STORE_LOCALS)
1819 x = POP();
1820 v = f->f_locals;
1821 Py_XDECREF(v);
1822 f->f_locals = x;
1823 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 TARGET(RETURN_VALUE)
1826 retval = POP();
1827 why = WHY_RETURN;
1828 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 TARGET(YIELD_VALUE)
1831 retval = POP();
1832 f->f_stacktop = stack_pointer;
1833 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 TARGET(POP_EXCEPT)
1837 {
1838 PyTryBlock *b = PyFrame_BlockPop(f);
1839 if (b->b_type != EXCEPT_HANDLER) {
1840 PyErr_SetString(PyExc_SystemError,
1841 "popped block is not an except handler");
1842 why = WHY_EXCEPTION;
1843 break;
1844 }
1845 UNWIND_EXCEPT_HANDLER(b);
1846 }
1847 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 TARGET(POP_BLOCK)
1850 {
1851 PyTryBlock *b = PyFrame_BlockPop(f);
1852 UNWIND_BLOCK(b);
1853 }
1854 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 PREDICTED(END_FINALLY);
1857 TARGET(END_FINALLY)
1858 v = POP();
1859 if (PyLong_Check(v)) {
1860 why = (enum why_code) PyLong_AS_LONG(v);
1861 assert(why != WHY_YIELD);
1862 if (why == WHY_RETURN ||
1863 why == WHY_CONTINUE)
1864 retval = POP();
1865 if (why == WHY_SILENCED) {
1866 /* An exception was silenced by 'with', we must
1867 manually unwind the EXCEPT_HANDLER block which was
1868 created when the exception was caught, otherwise
1869 the stack will be in an inconsistent state. */
1870 PyTryBlock *b = PyFrame_BlockPop(f);
1871 assert(b->b_type == EXCEPT_HANDLER);
1872 UNWIND_EXCEPT_HANDLER(b);
1873 why = WHY_NOT;
1874 }
1875 }
1876 else if (PyExceptionClass_Check(v)) {
1877 w = POP();
1878 u = POP();
1879 PyErr_Restore(v, w, u);
1880 why = WHY_RERAISE;
1881 break;
1882 }
1883 else if (v != Py_None) {
1884 PyErr_SetString(PyExc_SystemError,
1885 "'finally' pops bad exception");
1886 why = WHY_EXCEPTION;
1887 }
1888 Py_DECREF(v);
1889 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001891 TARGET(LOAD_BUILD_CLASS)
1892 x = PyDict_GetItemString(f->f_builtins,
1893 "__build_class__");
1894 if (x == NULL) {
1895 PyErr_SetString(PyExc_ImportError,
1896 "__build_class__ not found");
1897 break;
1898 }
1899 Py_INCREF(x);
1900 PUSH(x);
1901 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 TARGET(STORE_NAME)
1904 w = GETITEM(names, oparg);
1905 v = POP();
1906 if ((x = f->f_locals) != NULL) {
1907 if (PyDict_CheckExact(x))
1908 err = PyDict_SetItem(x, w, v);
1909 else
1910 err = PyObject_SetItem(x, w, v);
1911 Py_DECREF(v);
1912 if (err == 0) DISPATCH();
1913 break;
1914 }
1915 PyErr_Format(PyExc_SystemError,
1916 "no locals found when storing %R", w);
1917 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 TARGET(DELETE_NAME)
1920 w = GETITEM(names, oparg);
1921 if ((x = f->f_locals) != NULL) {
1922 if ((err = PyObject_DelItem(x, w)) != 0)
1923 format_exc_check_arg(PyExc_NameError,
1924 NAME_ERROR_MSG,
1925 w);
1926 break;
1927 }
1928 PyErr_Format(PyExc_SystemError,
1929 "no locals when deleting %R", w);
1930 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1933 TARGET(UNPACK_SEQUENCE)
1934 v = POP();
1935 if (PyTuple_CheckExact(v) &&
1936 PyTuple_GET_SIZE(v) == oparg) {
1937 PyObject **items = \
1938 ((PyTupleObject *)v)->ob_item;
1939 while (oparg--) {
1940 w = items[oparg];
1941 Py_INCREF(w);
1942 PUSH(w);
1943 }
1944 Py_DECREF(v);
1945 DISPATCH();
1946 } else if (PyList_CheckExact(v) &&
1947 PyList_GET_SIZE(v) == oparg) {
1948 PyObject **items = \
1949 ((PyListObject *)v)->ob_item;
1950 while (oparg--) {
1951 w = items[oparg];
1952 Py_INCREF(w);
1953 PUSH(w);
1954 }
1955 } else if (unpack_iterable(v, oparg, -1,
1956 stack_pointer + oparg)) {
1957 STACKADJ(oparg);
1958 } else {
1959 /* unpack_iterable() raised an exception */
1960 why = WHY_EXCEPTION;
1961 }
1962 Py_DECREF(v);
1963 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 TARGET(UNPACK_EX)
1966 {
1967 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1968 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1971 stack_pointer + totalargs)) {
1972 stack_pointer += totalargs;
1973 } else {
1974 why = WHY_EXCEPTION;
1975 }
1976 Py_DECREF(v);
1977 break;
1978 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 TARGET(STORE_ATTR)
1981 w = GETITEM(names, oparg);
1982 v = TOP();
1983 u = SECOND();
1984 STACKADJ(-2);
1985 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1986 Py_DECREF(v);
1987 Py_DECREF(u);
1988 if (err == 0) DISPATCH();
1989 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001991 TARGET(DELETE_ATTR)
1992 w = GETITEM(names, oparg);
1993 v = POP();
1994 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1995 /* del v.w */
1996 Py_DECREF(v);
1997 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001999 TARGET(STORE_GLOBAL)
2000 w = GETITEM(names, oparg);
2001 v = POP();
2002 err = PyDict_SetItem(f->f_globals, w, v);
2003 Py_DECREF(v);
2004 if (err == 0) DISPATCH();
2005 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002007 TARGET(DELETE_GLOBAL)
2008 w = GETITEM(names, oparg);
2009 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2010 format_exc_check_arg(
2011 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 TARGET(LOAD_NAME)
2015 w = GETITEM(names, oparg);
2016 if ((v = f->f_locals) == NULL) {
2017 PyErr_Format(PyExc_SystemError,
2018 "no locals when loading %R", w);
2019 why = WHY_EXCEPTION;
2020 break;
2021 }
2022 if (PyDict_CheckExact(v)) {
2023 x = PyDict_GetItem(v, w);
2024 Py_XINCREF(x);
2025 }
2026 else {
2027 x = PyObject_GetItem(v, w);
2028 if (x == NULL && PyErr_Occurred()) {
2029 if (!PyErr_ExceptionMatches(
2030 PyExc_KeyError))
2031 break;
2032 PyErr_Clear();
2033 }
2034 }
2035 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002036 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002038 x = PyDict_GetItem(f->f_builtins, w);
2039 if (x == NULL) {
2040 format_exc_check_arg(
2041 PyExc_NameError,
2042 NAME_ERROR_MSG, w);
2043 break;
2044 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 }
2046 Py_INCREF(x);
2047 }
2048 PUSH(x);
2049 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002050
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002051 TARGET(LOAD_GLOBAL)
2052 w = GETITEM(names, oparg);
2053 if (PyUnicode_CheckExact(w)) {
2054 /* Inline the PyDict_GetItem() calls.
2055 WARNING: this is an extreme speed hack.
2056 Do not try this at home. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002057 Py_hash_t hash = ((PyASCIIObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 if (hash != -1) {
2059 PyDictObject *d;
2060 PyDictEntry *e;
2061 d = (PyDictObject *)(f->f_globals);
2062 e = d->ma_lookup(d, w, hash);
2063 if (e == NULL) {
2064 x = NULL;
2065 break;
2066 }
2067 x = e->me_value;
2068 if (x != NULL) {
2069 Py_INCREF(x);
2070 PUSH(x);
2071 DISPATCH();
2072 }
2073 d = (PyDictObject *)(f->f_builtins);
2074 e = d->ma_lookup(d, w, hash);
2075 if (e == NULL) {
2076 x = NULL;
2077 break;
2078 }
2079 x = e->me_value;
2080 if (x != NULL) {
2081 Py_INCREF(x);
2082 PUSH(x);
2083 DISPATCH();
2084 }
2085 goto load_global_error;
2086 }
2087 }
2088 /* This is the un-inlined version of the code above */
2089 x = PyDict_GetItem(f->f_globals, w);
2090 if (x == NULL) {
2091 x = PyDict_GetItem(f->f_builtins, w);
2092 if (x == NULL) {
2093 load_global_error:
2094 format_exc_check_arg(
2095 PyExc_NameError,
2096 GLOBAL_NAME_ERROR_MSG, w);
2097 break;
2098 }
2099 }
2100 Py_INCREF(x);
2101 PUSH(x);
2102 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002104 TARGET(DELETE_FAST)
2105 x = GETLOCAL(oparg);
2106 if (x != NULL) {
2107 SETLOCAL(oparg, NULL);
2108 DISPATCH();
2109 }
2110 format_exc_check_arg(
2111 PyExc_UnboundLocalError,
2112 UNBOUNDLOCAL_ERROR_MSG,
2113 PyTuple_GetItem(co->co_varnames, oparg)
2114 );
2115 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002117 TARGET(DELETE_DEREF)
2118 x = freevars[oparg];
2119 if (PyCell_GET(x) != NULL) {
2120 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002121 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002122 }
2123 err = -1;
2124 format_exc_unbound(co, oparg);
2125 break;
2126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002127 TARGET(LOAD_CLOSURE)
2128 x = freevars[oparg];
2129 Py_INCREF(x);
2130 PUSH(x);
2131 if (x != NULL) DISPATCH();
2132 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 TARGET(LOAD_DEREF)
2135 x = freevars[oparg];
2136 w = PyCell_Get(x);
2137 if (w != NULL) {
2138 PUSH(w);
2139 DISPATCH();
2140 }
2141 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002142 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002145 TARGET(STORE_DEREF)
2146 w = POP();
2147 x = freevars[oparg];
2148 PyCell_Set(x, w);
2149 Py_DECREF(w);
2150 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 TARGET(BUILD_TUPLE)
2153 x = PyTuple_New(oparg);
2154 if (x != NULL) {
2155 for (; --oparg >= 0;) {
2156 w = POP();
2157 PyTuple_SET_ITEM(x, oparg, w);
2158 }
2159 PUSH(x);
2160 DISPATCH();
2161 }
2162 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 TARGET(BUILD_LIST)
2165 x = PyList_New(oparg);
2166 if (x != NULL) {
2167 for (; --oparg >= 0;) {
2168 w = POP();
2169 PyList_SET_ITEM(x, oparg, w);
2170 }
2171 PUSH(x);
2172 DISPATCH();
2173 }
2174 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002176 TARGET(BUILD_SET)
2177 x = PySet_New(NULL);
2178 if (x != NULL) {
2179 for (; --oparg >= 0;) {
2180 w = POP();
2181 if (err == 0)
2182 err = PySet_Add(x, w);
2183 Py_DECREF(w);
2184 }
2185 if (err != 0) {
2186 Py_DECREF(x);
2187 break;
2188 }
2189 PUSH(x);
2190 DISPATCH();
2191 }
2192 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 TARGET(BUILD_MAP)
2195 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2196 PUSH(x);
2197 if (x != NULL) DISPATCH();
2198 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 TARGET(STORE_MAP)
2201 w = TOP(); /* key */
2202 u = SECOND(); /* value */
2203 v = THIRD(); /* dict */
2204 STACKADJ(-2);
2205 assert (PyDict_CheckExact(v));
2206 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2207 Py_DECREF(u);
2208 Py_DECREF(w);
2209 if (err == 0) DISPATCH();
2210 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 TARGET(MAP_ADD)
2213 w = TOP(); /* key */
2214 u = SECOND(); /* value */
2215 STACKADJ(-2);
2216 v = stack_pointer[-oparg]; /* dict */
2217 assert (PyDict_CheckExact(v));
2218 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2219 Py_DECREF(u);
2220 Py_DECREF(w);
2221 if (err == 0) {
2222 PREDICT(JUMP_ABSOLUTE);
2223 DISPATCH();
2224 }
2225 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 TARGET(LOAD_ATTR)
2228 w = GETITEM(names, oparg);
2229 v = TOP();
2230 x = PyObject_GetAttr(v, w);
2231 Py_DECREF(v);
2232 SET_TOP(x);
2233 if (x != NULL) DISPATCH();
2234 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 TARGET(COMPARE_OP)
2237 w = POP();
2238 v = TOP();
2239 x = cmp_outcome(oparg, v, w);
2240 Py_DECREF(v);
2241 Py_DECREF(w);
2242 SET_TOP(x);
2243 if (x == NULL) break;
2244 PREDICT(POP_JUMP_IF_FALSE);
2245 PREDICT(POP_JUMP_IF_TRUE);
2246 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 TARGET(IMPORT_NAME)
2249 w = GETITEM(names, oparg);
2250 x = PyDict_GetItemString(f->f_builtins, "__import__");
2251 if (x == NULL) {
2252 PyErr_SetString(PyExc_ImportError,
2253 "__import__ not found");
2254 break;
2255 }
2256 Py_INCREF(x);
2257 v = POP();
2258 u = TOP();
2259 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2260 w = PyTuple_Pack(5,
2261 w,
2262 f->f_globals,
2263 f->f_locals == NULL ?
2264 Py_None : f->f_locals,
2265 v,
2266 u);
2267 else
2268 w = PyTuple_Pack(4,
2269 w,
2270 f->f_globals,
2271 f->f_locals == NULL ?
2272 Py_None : f->f_locals,
2273 v);
2274 Py_DECREF(v);
2275 Py_DECREF(u);
2276 if (w == NULL) {
2277 u = POP();
2278 Py_DECREF(x);
2279 x = NULL;
2280 break;
2281 }
2282 READ_TIMESTAMP(intr0);
2283 v = x;
2284 x = PyEval_CallObject(v, w);
2285 Py_DECREF(v);
2286 READ_TIMESTAMP(intr1);
2287 Py_DECREF(w);
2288 SET_TOP(x);
2289 if (x != NULL) DISPATCH();
2290 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 TARGET(IMPORT_STAR)
2293 v = POP();
2294 PyFrame_FastToLocals(f);
2295 if ((x = f->f_locals) == NULL) {
2296 PyErr_SetString(PyExc_SystemError,
2297 "no locals found during 'import *'");
2298 break;
2299 }
2300 READ_TIMESTAMP(intr0);
2301 err = import_all_from(x, v);
2302 READ_TIMESTAMP(intr1);
2303 PyFrame_LocalsToFast(f, 0);
2304 Py_DECREF(v);
2305 if (err == 0) DISPATCH();
2306 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002308 TARGET(IMPORT_FROM)
2309 w = GETITEM(names, oparg);
2310 v = TOP();
2311 READ_TIMESTAMP(intr0);
2312 x = import_from(v, w);
2313 READ_TIMESTAMP(intr1);
2314 PUSH(x);
2315 if (x != NULL) DISPATCH();
2316 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 TARGET(JUMP_FORWARD)
2319 JUMPBY(oparg);
2320 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2323 TARGET(POP_JUMP_IF_FALSE)
2324 w = POP();
2325 if (w == Py_True) {
2326 Py_DECREF(w);
2327 FAST_DISPATCH();
2328 }
2329 if (w == Py_False) {
2330 Py_DECREF(w);
2331 JUMPTO(oparg);
2332 FAST_DISPATCH();
2333 }
2334 err = PyObject_IsTrue(w);
2335 Py_DECREF(w);
2336 if (err > 0)
2337 err = 0;
2338 else if (err == 0)
2339 JUMPTO(oparg);
2340 else
2341 break;
2342 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2345 TARGET(POP_JUMP_IF_TRUE)
2346 w = POP();
2347 if (w == Py_False) {
2348 Py_DECREF(w);
2349 FAST_DISPATCH();
2350 }
2351 if (w == Py_True) {
2352 Py_DECREF(w);
2353 JUMPTO(oparg);
2354 FAST_DISPATCH();
2355 }
2356 err = PyObject_IsTrue(w);
2357 Py_DECREF(w);
2358 if (err > 0) {
2359 err = 0;
2360 JUMPTO(oparg);
2361 }
2362 else if (err == 0)
2363 ;
2364 else
2365 break;
2366 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 TARGET(JUMP_IF_FALSE_OR_POP)
2369 w = TOP();
2370 if (w == Py_True) {
2371 STACKADJ(-1);
2372 Py_DECREF(w);
2373 FAST_DISPATCH();
2374 }
2375 if (w == Py_False) {
2376 JUMPTO(oparg);
2377 FAST_DISPATCH();
2378 }
2379 err = PyObject_IsTrue(w);
2380 if (err > 0) {
2381 STACKADJ(-1);
2382 Py_DECREF(w);
2383 err = 0;
2384 }
2385 else if (err == 0)
2386 JUMPTO(oparg);
2387 else
2388 break;
2389 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 TARGET(JUMP_IF_TRUE_OR_POP)
2392 w = TOP();
2393 if (w == Py_False) {
2394 STACKADJ(-1);
2395 Py_DECREF(w);
2396 FAST_DISPATCH();
2397 }
2398 if (w == Py_True) {
2399 JUMPTO(oparg);
2400 FAST_DISPATCH();
2401 }
2402 err = PyObject_IsTrue(w);
2403 if (err > 0) {
2404 err = 0;
2405 JUMPTO(oparg);
2406 }
2407 else if (err == 0) {
2408 STACKADJ(-1);
2409 Py_DECREF(w);
2410 }
2411 else
2412 break;
2413 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2416 TARGET(JUMP_ABSOLUTE)
2417 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002418#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 /* Enabling this path speeds-up all while and for-loops by bypassing
2420 the per-loop checks for signals. By default, this should be turned-off
2421 because it prevents detection of a control-break in tight loops like
2422 "while 1: pass". Compile with this option turned-on when you need
2423 the speed-up and do not need break checking inside tight loops (ones
2424 that contain only instructions ending with FAST_DISPATCH).
2425 */
2426 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002427#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002428 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002429#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002431 TARGET(GET_ITER)
2432 /* before: [obj]; after [getiter(obj)] */
2433 v = TOP();
2434 x = PyObject_GetIter(v);
2435 Py_DECREF(v);
2436 if (x != NULL) {
2437 SET_TOP(x);
2438 PREDICT(FOR_ITER);
2439 DISPATCH();
2440 }
2441 STACKADJ(-1);
2442 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002443
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002444 PREDICTED_WITH_ARG(FOR_ITER);
2445 TARGET(FOR_ITER)
2446 /* before: [iter]; after: [iter, iter()] *or* [] */
2447 v = TOP();
2448 x = (*v->ob_type->tp_iternext)(v);
2449 if (x != NULL) {
2450 PUSH(x);
2451 PREDICT(STORE_FAST);
2452 PREDICT(UNPACK_SEQUENCE);
2453 DISPATCH();
2454 }
2455 if (PyErr_Occurred()) {
2456 if (!PyErr_ExceptionMatches(
2457 PyExc_StopIteration))
2458 break;
2459 PyErr_Clear();
2460 }
2461 /* iterator ended normally */
2462 x = v = POP();
2463 Py_DECREF(v);
2464 JUMPBY(oparg);
2465 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 TARGET(BREAK_LOOP)
2468 why = WHY_BREAK;
2469 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002471 TARGET(CONTINUE_LOOP)
2472 retval = PyLong_FromLong(oparg);
2473 if (!retval) {
2474 x = NULL;
2475 break;
2476 }
2477 why = WHY_CONTINUE;
2478 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2481 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2482 TARGET(SETUP_FINALLY)
2483 _setup_finally:
2484 /* NOTE: If you add any new block-setup opcodes that
2485 are not try/except/finally handlers, you may need
2486 to update the PyGen_NeedsFinalizing() function.
2487 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002489 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2490 STACK_LEVEL());
2491 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002493 TARGET(SETUP_WITH)
2494 {
2495 static PyObject *exit, *enter;
2496 w = TOP();
2497 x = special_lookup(w, "__exit__", &exit);
2498 if (!x)
2499 break;
2500 SET_TOP(x);
2501 u = special_lookup(w, "__enter__", &enter);
2502 Py_DECREF(w);
2503 if (!u) {
2504 x = NULL;
2505 break;
2506 }
2507 x = PyObject_CallFunctionObjArgs(u, NULL);
2508 Py_DECREF(u);
2509 if (!x)
2510 break;
2511 /* Setup the finally block before pushing the result
2512 of __enter__ on the stack. */
2513 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2514 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 PUSH(x);
2517 DISPATCH();
2518 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002520 TARGET(WITH_CLEANUP)
2521 {
2522 /* At the top of the stack are 1-3 values indicating
2523 how/why we entered the finally clause:
2524 - TOP = None
2525 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2526 - TOP = WHY_*; no retval below it
2527 - (TOP, SECOND, THIRD) = exc_info()
2528 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2529 Below them is EXIT, the context.__exit__ bound method.
2530 In the last case, we must call
2531 EXIT(TOP, SECOND, THIRD)
2532 otherwise we must call
2533 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 In the first two cases, we remove EXIT from the
2536 stack, leaving the rest in the same order. In the
2537 third case, we shift the bottom 3 values of the
2538 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 In addition, if the stack represents an exception,
2541 *and* the function call returns a 'true' value, we
2542 push WHY_SILENCED onto the stack. END_FINALLY will
2543 then not re-raise the exception. (But non-local
2544 gotos should still be resumed.)
2545 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 PyObject *exit_func;
2548 u = TOP();
2549 if (u == Py_None) {
2550 (void)POP();
2551 exit_func = TOP();
2552 SET_TOP(u);
2553 v = w = Py_None;
2554 }
2555 else if (PyLong_Check(u)) {
2556 (void)POP();
2557 switch(PyLong_AsLong(u)) {
2558 case WHY_RETURN:
2559 case WHY_CONTINUE:
2560 /* Retval in TOP. */
2561 exit_func = SECOND();
2562 SET_SECOND(TOP());
2563 SET_TOP(u);
2564 break;
2565 default:
2566 exit_func = TOP();
2567 SET_TOP(u);
2568 break;
2569 }
2570 u = v = w = Py_None;
2571 }
2572 else {
2573 PyObject *tp, *exc, *tb;
2574 PyTryBlock *block;
2575 v = SECOND();
2576 w = THIRD();
2577 tp = FOURTH();
2578 exc = PEEK(5);
2579 tb = PEEK(6);
2580 exit_func = PEEK(7);
2581 SET_VALUE(7, tb);
2582 SET_VALUE(6, exc);
2583 SET_VALUE(5, tp);
2584 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2585 SET_FOURTH(NULL);
2586 /* We just shifted the stack down, so we have
2587 to tell the except handler block that the
2588 values are lower than it expects. */
2589 block = &f->f_blockstack[f->f_iblock - 1];
2590 assert(block->b_type == EXCEPT_HANDLER);
2591 block->b_level--;
2592 }
2593 /* XXX Not the fastest way to call it... */
2594 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2595 NULL);
2596 Py_DECREF(exit_func);
2597 if (x == NULL)
2598 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002600 if (u != Py_None)
2601 err = PyObject_IsTrue(x);
2602 else
2603 err = 0;
2604 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002606 if (err < 0)
2607 break; /* Go to error exit */
2608 else if (err > 0) {
2609 err = 0;
2610 /* There was an exception and a True return */
2611 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2612 }
2613 PREDICT(END_FINALLY);
2614 break;
2615 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002617 TARGET(CALL_FUNCTION)
2618 {
2619 PyObject **sp;
2620 PCALL(PCALL_ALL);
2621 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002622#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002623 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002624#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002625 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002626#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002627 stack_pointer = sp;
2628 PUSH(x);
2629 if (x != NULL)
2630 DISPATCH();
2631 break;
2632 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002634 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2635 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2636 TARGET(CALL_FUNCTION_VAR_KW)
2637 _call_function_var_kw:
2638 {
2639 int na = oparg & 0xff;
2640 int nk = (oparg>>8) & 0xff;
2641 int flags = (opcode - CALL_FUNCTION) & 3;
2642 int n = na + 2 * nk;
2643 PyObject **pfunc, *func, **sp;
2644 PCALL(PCALL_ALL);
2645 if (flags & CALL_FLAG_VAR)
2646 n++;
2647 if (flags & CALL_FLAG_KW)
2648 n++;
2649 pfunc = stack_pointer - n - 1;
2650 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002653 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 PyObject *self = PyMethod_GET_SELF(func);
2655 Py_INCREF(self);
2656 func = PyMethod_GET_FUNCTION(func);
2657 Py_INCREF(func);
2658 Py_DECREF(*pfunc);
2659 *pfunc = self;
2660 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002661 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 } else
2663 Py_INCREF(func);
2664 sp = stack_pointer;
2665 READ_TIMESTAMP(intr0);
2666 x = ext_do_call(func, &sp, flags, na, nk);
2667 READ_TIMESTAMP(intr1);
2668 stack_pointer = sp;
2669 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 while (stack_pointer > pfunc) {
2672 w = POP();
2673 Py_DECREF(w);
2674 }
2675 PUSH(x);
2676 if (x != NULL)
2677 DISPATCH();
2678 break;
2679 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2682 TARGET(MAKE_FUNCTION)
2683 _make_function:
2684 {
2685 int posdefaults = oparg & 0xff;
2686 int kwdefaults = (oparg>>8) & 0xff;
2687 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 v = POP(); /* code object */
2690 x = PyFunction_New(v, f->f_globals);
2691 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002693 if (x != NULL && opcode == MAKE_CLOSURE) {
2694 v = POP();
2695 if (PyFunction_SetClosure(x, v) != 0) {
2696 /* Can't happen unless bytecode is corrupt. */
2697 why = WHY_EXCEPTION;
2698 }
2699 Py_DECREF(v);
2700 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 if (x != NULL && num_annotations > 0) {
2703 Py_ssize_t name_ix;
2704 u = POP(); /* names of args with annotations */
2705 v = PyDict_New();
2706 if (v == NULL) {
2707 Py_DECREF(x);
2708 x = NULL;
2709 break;
2710 }
2711 name_ix = PyTuple_Size(u);
2712 assert(num_annotations == name_ix+1);
2713 while (name_ix > 0) {
2714 --name_ix;
2715 t = PyTuple_GET_ITEM(u, name_ix);
2716 w = POP();
2717 /* XXX(nnorwitz): check for errors */
2718 PyDict_SetItem(v, t, w);
2719 Py_DECREF(w);
2720 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 if (PyFunction_SetAnnotations(x, v) != 0) {
2723 /* Can't happen unless
2724 PyFunction_SetAnnotations changes. */
2725 why = WHY_EXCEPTION;
2726 }
2727 Py_DECREF(v);
2728 Py_DECREF(u);
2729 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 /* XXX Maybe this should be a separate opcode? */
2732 if (x != NULL && posdefaults > 0) {
2733 v = PyTuple_New(posdefaults);
2734 if (v == NULL) {
2735 Py_DECREF(x);
2736 x = NULL;
2737 break;
2738 }
2739 while (--posdefaults >= 0) {
2740 w = POP();
2741 PyTuple_SET_ITEM(v, posdefaults, w);
2742 }
2743 if (PyFunction_SetDefaults(x, v) != 0) {
2744 /* Can't happen unless
2745 PyFunction_SetDefaults changes. */
2746 why = WHY_EXCEPTION;
2747 }
2748 Py_DECREF(v);
2749 }
2750 if (x != NULL && kwdefaults > 0) {
2751 v = PyDict_New();
2752 if (v == NULL) {
2753 Py_DECREF(x);
2754 x = NULL;
2755 break;
2756 }
2757 while (--kwdefaults >= 0) {
2758 w = POP(); /* default value */
2759 u = POP(); /* kw only arg name */
2760 /* XXX(nnorwitz): check for errors */
2761 PyDict_SetItem(v, u, w);
2762 Py_DECREF(w);
2763 Py_DECREF(u);
2764 }
2765 if (PyFunction_SetKwDefaults(x, v) != 0) {
2766 /* Can't happen unless
2767 PyFunction_SetKwDefaults changes. */
2768 why = WHY_EXCEPTION;
2769 }
2770 Py_DECREF(v);
2771 }
2772 PUSH(x);
2773 break;
2774 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 TARGET(BUILD_SLICE)
2777 if (oparg == 3)
2778 w = POP();
2779 else
2780 w = NULL;
2781 v = POP();
2782 u = TOP();
2783 x = PySlice_New(u, v, w);
2784 Py_DECREF(u);
2785 Py_DECREF(v);
2786 Py_XDECREF(w);
2787 SET_TOP(x);
2788 if (x != NULL) DISPATCH();
2789 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 TARGET(EXTENDED_ARG)
2792 opcode = NEXTOP();
2793 oparg = oparg<<16 | NEXTARG();
2794 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002795
Antoine Pitrou042b1282010-08-13 21:15:58 +00002796#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002798#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 default:
2800 fprintf(stderr,
2801 "XXX lineno: %d, opcode: %d\n",
2802 PyFrame_GetLineNumber(f),
2803 opcode);
2804 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2805 why = WHY_EXCEPTION;
2806 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002807
2808#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002809 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002810#endif
2811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002816 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002819
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 if (why == WHY_NOT) {
2821 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002822#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 /* This check is expensive! */
2824 if (PyErr_Occurred())
2825 fprintf(stderr,
2826 "XXX undetected error\n");
2827 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002828#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 READ_TIMESTAMP(loop1);
2830 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002831#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002833#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 }
2835 why = WHY_EXCEPTION;
2836 x = Py_None;
2837 err = 0;
2838 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2843 if (!PyErr_Occurred()) {
2844 PyErr_SetString(PyExc_SystemError,
2845 "error return without exception set");
2846 why = WHY_EXCEPTION;
2847 }
2848 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002849#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 else {
2851 /* This check is expensive! */
2852 if (PyErr_Occurred()) {
2853 char buf[128];
2854 sprintf(buf, "Stack unwind with exception "
2855 "set and why=%d", why);
2856 Py_FatalError(buf);
2857 }
2858 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002859#endif
2860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 if (why == WHY_EXCEPTION) {
2864 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 if (tstate->c_tracefunc != NULL)
2867 call_exc_trace(tstate->c_tracefunc,
2868 tstate->c_traceobj, f);
2869 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 if (why == WHY_RERAISE)
2874 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002877
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002878fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 while (why != WHY_NOT && f->f_iblock > 0) {
2880 /* Peek at the current block. */
2881 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 assert(why != WHY_YIELD);
2884 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2885 why = WHY_NOT;
2886 JUMPTO(PyLong_AS_LONG(retval));
2887 Py_DECREF(retval);
2888 break;
2889 }
2890 /* Now we have to pop the block. */
2891 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 if (b->b_type == EXCEPT_HANDLER) {
2894 UNWIND_EXCEPT_HANDLER(b);
2895 continue;
2896 }
2897 UNWIND_BLOCK(b);
2898 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2899 why = WHY_NOT;
2900 JUMPTO(b->b_handler);
2901 break;
2902 }
2903 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2904 || b->b_type == SETUP_FINALLY)) {
2905 PyObject *exc, *val, *tb;
2906 int handler = b->b_handler;
2907 /* Beware, this invalidates all b->b_* fields */
2908 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2909 PUSH(tstate->exc_traceback);
2910 PUSH(tstate->exc_value);
2911 if (tstate->exc_type != NULL) {
2912 PUSH(tstate->exc_type);
2913 }
2914 else {
2915 Py_INCREF(Py_None);
2916 PUSH(Py_None);
2917 }
2918 PyErr_Fetch(&exc, &val, &tb);
2919 /* Make the raw exception data
2920 available to the handler,
2921 so a program can emulate the
2922 Python main loop. */
2923 PyErr_NormalizeException(
2924 &exc, &val, &tb);
2925 PyException_SetTraceback(val, tb);
2926 Py_INCREF(exc);
2927 tstate->exc_type = exc;
2928 Py_INCREF(val);
2929 tstate->exc_value = val;
2930 tstate->exc_traceback = tb;
2931 if (tb == NULL)
2932 tb = Py_None;
2933 Py_INCREF(tb);
2934 PUSH(tb);
2935 PUSH(val);
2936 PUSH(exc);
2937 why = WHY_NOT;
2938 JUMPTO(handler);
2939 break;
2940 }
2941 if (b->b_type == SETUP_FINALLY) {
2942 if (why & (WHY_RETURN | WHY_CONTINUE))
2943 PUSH(retval);
2944 PUSH(PyLong_FromLong((long)why));
2945 why = WHY_NOT;
2946 JUMPTO(b->b_handler);
2947 break;
2948 }
2949 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002951 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953 if (why != WHY_NOT)
2954 break;
2955 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002957 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002958
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002959 assert(why != WHY_YIELD);
2960 /* Pop remaining stack entries. */
2961 while (!EMPTY()) {
2962 v = POP();
2963 Py_XDECREF(v);
2964 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 if (why != WHY_RETURN)
2967 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002968
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002969fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05002970 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
2971 /* The purpose of this block is to put aside the generator's exception
2972 state and restore that of the calling frame. If the current
2973 exception state is from the caller, we clear the exception values
2974 on the generator frame, so they are not swapped back in latter. The
2975 origin of the current exception state is determined by checking for
2976 except handler blocks, which we must be in iff a new exception
2977 state came into existence in this frame. (An uncaught exception
2978 would have why == WHY_EXCEPTION, and we wouldn't be here). */
2979 int i;
2980 for (i = 0; i < f->f_iblock; i++)
2981 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
2982 break;
2983 if (i == f->f_iblock)
2984 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05002985 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05002986 else
Benjamin Peterson87880242011-07-03 16:48:31 -05002987 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05002988 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 if (tstate->use_tracing) {
2991 if (tstate->c_tracefunc) {
2992 if (why == WHY_RETURN || why == WHY_YIELD) {
2993 if (call_trace(tstate->c_tracefunc,
2994 tstate->c_traceobj, f,
2995 PyTrace_RETURN, retval)) {
2996 Py_XDECREF(retval);
2997 retval = NULL;
2998 why = WHY_EXCEPTION;
2999 }
3000 }
3001 else if (why == WHY_EXCEPTION) {
3002 call_trace_protected(tstate->c_tracefunc,
3003 tstate->c_traceobj, f,
3004 PyTrace_RETURN, NULL);
3005 }
3006 }
3007 if (tstate->c_profilefunc) {
3008 if (why == WHY_EXCEPTION)
3009 call_trace_protected(tstate->c_profilefunc,
3010 tstate->c_profileobj, f,
3011 PyTrace_RETURN, NULL);
3012 else if (call_trace(tstate->c_profilefunc,
3013 tstate->c_profileobj, f,
3014 PyTrace_RETURN, retval)) {
3015 Py_XDECREF(retval);
3016 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003017 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 }
3019 }
3020 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003023exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 Py_LeaveRecursiveCall();
3025 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003028}
3029
Benjamin Petersonb204a422011-06-05 22:04:07 -05003030static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003031format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3032{
3033 int err;
3034 Py_ssize_t len = PyList_GET_SIZE(names);
3035 PyObject *name_str, *comma, *tail, *tmp;
3036
3037 assert(PyList_CheckExact(names));
3038 assert(len >= 1);
3039 /* Deal with the joys of natural language. */
3040 switch (len) {
3041 case 1:
3042 name_str = PyList_GET_ITEM(names, 0);
3043 Py_INCREF(name_str);
3044 break;
3045 case 2:
3046 name_str = PyUnicode_FromFormat("%U and %U",
3047 PyList_GET_ITEM(names, len - 2),
3048 PyList_GET_ITEM(names, len - 1));
3049 break;
3050 default:
3051 tail = PyUnicode_FromFormat(", %U, and %U",
3052 PyList_GET_ITEM(names, len - 2),
3053 PyList_GET_ITEM(names, len - 1));
3054 /* Chop off the last two objects in the list. This shouldn't actually
3055 fail, but we can't be too careful. */
3056 err = PyList_SetSlice(names, len - 2, len, NULL);
3057 if (err == -1) {
3058 Py_DECREF(tail);
3059 return;
3060 }
3061 /* Stitch everything up into a nice comma-separated list. */
3062 comma = PyUnicode_FromString(", ");
3063 if (comma == NULL) {
3064 Py_DECREF(tail);
3065 return;
3066 }
3067 tmp = PyUnicode_Join(comma, names);
3068 Py_DECREF(comma);
3069 if (tmp == NULL) {
3070 Py_DECREF(tail);
3071 return;
3072 }
3073 name_str = PyUnicode_Concat(tmp, tail);
3074 Py_DECREF(tmp);
3075 Py_DECREF(tail);
3076 break;
3077 }
3078 if (name_str == NULL)
3079 return;
3080 PyErr_Format(PyExc_TypeError,
3081 "%U() missing %i required %s argument%s: %U",
3082 co->co_name,
3083 len,
3084 kind,
3085 len == 1 ? "" : "s",
3086 name_str);
3087 Py_DECREF(name_str);
3088}
3089
3090static void
3091missing_arguments(PyCodeObject *co, int missing, int defcount,
3092 PyObject **fastlocals)
3093{
3094 int i, j = 0;
3095 int start, end;
3096 int positional = defcount != -1;
3097 const char *kind = positional ? "positional" : "keyword-only";
3098 PyObject *missing_names;
3099
3100 /* Compute the names of the arguments that are missing. */
3101 missing_names = PyList_New(missing);
3102 if (missing_names == NULL)
3103 return;
3104 if (positional) {
3105 start = 0;
3106 end = co->co_argcount - defcount;
3107 }
3108 else {
3109 start = co->co_argcount;
3110 end = start + co->co_kwonlyargcount;
3111 }
3112 for (i = start; i < end; i++) {
3113 if (GETLOCAL(i) == NULL) {
3114 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3115 PyObject *name = PyObject_Repr(raw);
3116 if (name == NULL) {
3117 Py_DECREF(missing_names);
3118 return;
3119 }
3120 PyList_SET_ITEM(missing_names, j++, name);
3121 }
3122 }
3123 assert(j == missing);
3124 format_missing(kind, co, missing_names);
3125 Py_DECREF(missing_names);
3126}
3127
3128static void
3129too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003130{
3131 int plural;
3132 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003133 int i;
3134 PyObject *sig, *kwonly_sig;
3135
Benjamin Petersone109c702011-06-24 09:37:26 -05003136 assert((co->co_flags & CO_VARARGS) == 0);
3137 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003138 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003139 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003140 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003141 if (defcount) {
3142 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003143 plural = 1;
3144 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3145 }
3146 else {
3147 plural = co->co_argcount != 1;
3148 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3149 }
3150 if (sig == NULL)
3151 return;
3152 if (kwonly_given) {
3153 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3154 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3155 kwonly_given != 1 ? "s" : "");
3156 if (kwonly_sig == NULL) {
3157 Py_DECREF(sig);
3158 return;
3159 }
3160 }
3161 else {
3162 /* This will not fail. */
3163 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003164 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003165 }
3166 PyErr_Format(PyExc_TypeError,
3167 "%U() takes %U positional argument%s but %d%U %s given",
3168 co->co_name,
3169 sig,
3170 plural ? "s" : "",
3171 given,
3172 kwonly_sig,
3173 given == 1 && !kwonly_given ? "was" : "were");
3174 Py_DECREF(sig);
3175 Py_DECREF(kwonly_sig);
3176}
3177
Guido van Rossumc2e20742006-02-27 22:32:47 +00003178/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003179 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003180 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003181
Tim Peters6d6c1a32001-08-02 04:15:00 +00003182PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003183PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 PyObject **args, int argcount, PyObject **kws, int kwcount,
3185 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003186{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003187 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 register PyFrameObject *f;
3189 register PyObject *retval = NULL;
3190 register PyObject **fastlocals, **freevars;
3191 PyThreadState *tstate = PyThreadState_GET();
3192 PyObject *x, *u;
3193 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003194 int i;
3195 int n = argcount;
3196 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003197
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 if (globals == NULL) {
3199 PyErr_SetString(PyExc_SystemError,
3200 "PyEval_EvalCodeEx: NULL globals");
3201 return NULL;
3202 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 assert(tstate != NULL);
3205 assert(globals != NULL);
3206 f = PyFrame_New(tstate, co, globals, locals);
3207 if (f == NULL)
3208 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003209
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003210 fastlocals = f->f_localsplus;
3211 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003212
Benjamin Petersonb204a422011-06-05 22:04:07 -05003213 /* Parse arguments. */
3214 if (co->co_flags & CO_VARKEYWORDS) {
3215 kwdict = PyDict_New();
3216 if (kwdict == NULL)
3217 goto fail;
3218 i = total_args;
3219 if (co->co_flags & CO_VARARGS)
3220 i++;
3221 SETLOCAL(i, kwdict);
3222 }
3223 if (argcount > co->co_argcount)
3224 n = co->co_argcount;
3225 for (i = 0; i < n; i++) {
3226 x = args[i];
3227 Py_INCREF(x);
3228 SETLOCAL(i, x);
3229 }
3230 if (co->co_flags & CO_VARARGS) {
3231 u = PyTuple_New(argcount - n);
3232 if (u == NULL)
3233 goto fail;
3234 SETLOCAL(total_args, u);
3235 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 x = args[i];
3237 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003238 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003240 }
3241 for (i = 0; i < kwcount; i++) {
3242 PyObject **co_varnames;
3243 PyObject *keyword = kws[2*i];
3244 PyObject *value = kws[2*i + 1];
3245 int j;
3246 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3247 PyErr_Format(PyExc_TypeError,
3248 "%U() keywords must be strings",
3249 co->co_name);
3250 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003252 /* Speed hack: do raw pointer compares. As names are
3253 normally interned this should almost always hit. */
3254 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3255 for (j = 0; j < total_args; j++) {
3256 PyObject *nm = co_varnames[j];
3257 if (nm == keyword)
3258 goto kw_found;
3259 }
3260 /* Slow fallback, just in case */
3261 for (j = 0; j < total_args; j++) {
3262 PyObject *nm = co_varnames[j];
3263 int cmp = PyObject_RichCompareBool(
3264 keyword, nm, Py_EQ);
3265 if (cmp > 0)
3266 goto kw_found;
3267 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003269 }
3270 if (j >= total_args && kwdict == NULL) {
3271 PyErr_Format(PyExc_TypeError,
3272 "%U() got an unexpected "
3273 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 co->co_name,
3275 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003276 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003277 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003278 PyDict_SetItem(kwdict, keyword, value);
3279 continue;
3280 kw_found:
3281 if (GETLOCAL(j) != NULL) {
3282 PyErr_Format(PyExc_TypeError,
3283 "%U() got multiple "
3284 "values for argument '%S'",
3285 co->co_name,
3286 keyword);
3287 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003288 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003289 Py_INCREF(value);
3290 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003292 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003293 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 goto fail;
3295 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003296 if (argcount < co->co_argcount) {
3297 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003298 int missing = 0;
3299 for (i = argcount; i < m; i++)
3300 if (GETLOCAL(i) == NULL)
3301 missing++;
3302 if (missing) {
3303 missing_arguments(co, missing, defcount, fastlocals);
3304 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003305 }
3306 if (n > m)
3307 i = n - m;
3308 else
3309 i = 0;
3310 for (; i < defcount; i++) {
3311 if (GETLOCAL(m+i) == NULL) {
3312 PyObject *def = defs[i];
3313 Py_INCREF(def);
3314 SETLOCAL(m+i, def);
3315 }
3316 }
3317 }
3318 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003319 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003320 for (i = co->co_argcount; i < total_args; i++) {
3321 PyObject *name;
3322 if (GETLOCAL(i) != NULL)
3323 continue;
3324 name = PyTuple_GET_ITEM(co->co_varnames, i);
3325 if (kwdefs != NULL) {
3326 PyObject *def = PyDict_GetItem(kwdefs, name);
3327 if (def) {
3328 Py_INCREF(def);
3329 SETLOCAL(i, def);
3330 continue;
3331 }
3332 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003333 missing++;
3334 }
3335 if (missing) {
3336 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003337 goto fail;
3338 }
3339 }
3340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003342 vars into frame. */
3343 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003345 int arg;
3346 /* Possibly account for the cell variable being an argument. */
3347 if (co->co_cell2arg != NULL &&
3348 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3349 c = PyCell_New(GETLOCAL(arg));
3350 else
3351 c = PyCell_New(NULL);
3352 if (c == NULL)
3353 goto fail;
3354 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003356 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3357 PyObject *o = PyTuple_GET_ITEM(closure, i);
3358 Py_INCREF(o);
3359 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003360 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003362 if (co->co_flags & CO_GENERATOR) {
3363 /* Don't need to keep the reference to f_back, it will be set
3364 * when the generator is resumed. */
3365 Py_XDECREF(f->f_back);
3366 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003368 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 /* Create a new generator that owns the ready to run frame
3371 * and return that as the value. */
3372 return PyGen_New(f);
3373 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003376
Thomas Woutersce272b62007-09-19 21:19:28 +00003377fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 /* decref'ing the frame can cause __del__ methods to get invoked,
3380 which can call back into Python. While we're done with the
3381 current Python frame (f), the associated C stack is still in use,
3382 so recursion_depth must be boosted for the duration.
3383 */
3384 assert(tstate != NULL);
3385 ++tstate->recursion_depth;
3386 Py_DECREF(f);
3387 --tstate->recursion_depth;
3388 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003389}
3390
3391
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003392static PyObject *
3393special_lookup(PyObject *o, char *meth, PyObject **cache)
3394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 PyObject *res;
3396 res = _PyObject_LookupSpecial(o, meth, cache);
3397 if (res == NULL && !PyErr_Occurred()) {
3398 PyErr_SetObject(PyExc_AttributeError, *cache);
3399 return NULL;
3400 }
3401 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003402}
3403
3404
Benjamin Peterson87880242011-07-03 16:48:31 -05003405/* These 3 functions deal with the exception state of generators. */
3406
3407static void
3408save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3409{
3410 PyObject *type, *value, *traceback;
3411 Py_XINCREF(tstate->exc_type);
3412 Py_XINCREF(tstate->exc_value);
3413 Py_XINCREF(tstate->exc_traceback);
3414 type = f->f_exc_type;
3415 value = f->f_exc_value;
3416 traceback = f->f_exc_traceback;
3417 f->f_exc_type = tstate->exc_type;
3418 f->f_exc_value = tstate->exc_value;
3419 f->f_exc_traceback = tstate->exc_traceback;
3420 Py_XDECREF(type);
3421 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003422 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003423}
3424
3425static void
3426swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3427{
3428 PyObject *tmp;
3429 tmp = tstate->exc_type;
3430 tstate->exc_type = f->f_exc_type;
3431 f->f_exc_type = tmp;
3432 tmp = tstate->exc_value;
3433 tstate->exc_value = f->f_exc_value;
3434 f->f_exc_value = tmp;
3435 tmp = tstate->exc_traceback;
3436 tstate->exc_traceback = f->f_exc_traceback;
3437 f->f_exc_traceback = tmp;
3438}
3439
3440static void
3441restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3442{
3443 PyObject *type, *value, *tb;
3444 type = tstate->exc_type;
3445 value = tstate->exc_value;
3446 tb = tstate->exc_traceback;
3447 tstate->exc_type = f->f_exc_type;
3448 tstate->exc_value = f->f_exc_value;
3449 tstate->exc_traceback = f->f_exc_traceback;
3450 f->f_exc_type = NULL;
3451 f->f_exc_value = NULL;
3452 f->f_exc_traceback = NULL;
3453 Py_XDECREF(type);
3454 Py_XDECREF(value);
3455 Py_XDECREF(tb);
3456}
3457
3458
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003459/* Logic for the raise statement (too complicated for inlining).
3460 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003461static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003462do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003463{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 if (exc == NULL) {
3467 /* Reraise */
3468 PyThreadState *tstate = PyThreadState_GET();
3469 PyObject *tb;
3470 type = tstate->exc_type;
3471 value = tstate->exc_value;
3472 tb = tstate->exc_traceback;
3473 if (type == Py_None) {
3474 PyErr_SetString(PyExc_RuntimeError,
3475 "No active exception to reraise");
3476 return WHY_EXCEPTION;
3477 }
3478 Py_XINCREF(type);
3479 Py_XINCREF(value);
3480 Py_XINCREF(tb);
3481 PyErr_Restore(type, value, tb);
3482 return WHY_RERAISE;
3483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 /* We support the following forms of raise:
3486 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003487 raise <instance>
3488 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 if (PyExceptionClass_Check(exc)) {
3491 type = exc;
3492 value = PyObject_CallObject(exc, NULL);
3493 if (value == NULL)
3494 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003495 if (!PyExceptionInstance_Check(value)) {
3496 PyErr_Format(PyExc_TypeError,
3497 "calling %R should have returned an instance of "
3498 "BaseException, not %R",
3499 type, Py_TYPE(value));
3500 goto raise_error;
3501 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 }
3503 else if (PyExceptionInstance_Check(exc)) {
3504 value = exc;
3505 type = PyExceptionInstance_Class(exc);
3506 Py_INCREF(type);
3507 }
3508 else {
3509 /* Not something you can raise. You get an exception
3510 anyway, just not what you specified :-) */
3511 Py_DECREF(exc);
3512 PyErr_SetString(PyExc_TypeError,
3513 "exceptions must derive from BaseException");
3514 goto raise_error;
3515 }
Collin Winter828f04a2007-08-31 00:04:24 +00003516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 if (cause) {
3518 PyObject *fixed_cause;
3519 if (PyExceptionClass_Check(cause)) {
3520 fixed_cause = PyObject_CallObject(cause, NULL);
3521 if (fixed_cause == NULL)
3522 goto raise_error;
3523 Py_DECREF(cause);
3524 }
3525 else if (PyExceptionInstance_Check(cause)) {
3526 fixed_cause = cause;
3527 }
3528 else {
3529 PyErr_SetString(PyExc_TypeError,
3530 "exception causes must derive from "
3531 "BaseException");
3532 goto raise_error;
3533 }
3534 PyException_SetCause(value, fixed_cause);
3535 }
Collin Winter828f04a2007-08-31 00:04:24 +00003536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 PyErr_SetObject(type, value);
3538 /* PyErr_SetObject incref's its arguments */
3539 Py_XDECREF(value);
3540 Py_XDECREF(type);
3541 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003542
3543raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 Py_XDECREF(value);
3545 Py_XDECREF(type);
3546 Py_XDECREF(cause);
3547 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003548}
3549
Tim Petersd6d010b2001-06-21 02:49:55 +00003550/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003551 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003552
Guido van Rossum0368b722007-05-11 16:50:42 +00003553 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3554 with a variable target.
3555*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003556
Barry Warsawe42b18f1997-08-25 22:13:04 +00003557static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003558unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003560 int i = 0, j = 0;
3561 Py_ssize_t ll = 0;
3562 PyObject *it; /* iter(v) */
3563 PyObject *w;
3564 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 it = PyObject_GetIter(v);
3569 if (it == NULL)
3570 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 for (; i < argcnt; i++) {
3573 w = PyIter_Next(it);
3574 if (w == NULL) {
3575 /* Iterator done, via error or exhaustion. */
3576 if (!PyErr_Occurred()) {
3577 PyErr_Format(PyExc_ValueError,
3578 "need more than %d value%s to unpack",
3579 i, i == 1 ? "" : "s");
3580 }
3581 goto Error;
3582 }
3583 *--sp = w;
3584 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003586 if (argcntafter == -1) {
3587 /* We better have exhausted the iterator now. */
3588 w = PyIter_Next(it);
3589 if (w == NULL) {
3590 if (PyErr_Occurred())
3591 goto Error;
3592 Py_DECREF(it);
3593 return 1;
3594 }
3595 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003596 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3597 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 goto Error;
3599 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 l = PySequence_List(it);
3602 if (l == NULL)
3603 goto Error;
3604 *--sp = l;
3605 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 ll = PyList_GET_SIZE(l);
3608 if (ll < argcntafter) {
3609 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3610 argcnt + ll);
3611 goto Error;
3612 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 /* Pop the "after-variable" args off the list. */
3615 for (j = argcntafter; j > 0; j--, i++) {
3616 *--sp = PyList_GET_ITEM(l, ll - j);
3617 }
3618 /* Resize the list. */
3619 Py_SIZE(l) = ll - argcntafter;
3620 Py_DECREF(it);
3621 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003622
Tim Petersd6d010b2001-06-21 02:49:55 +00003623Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 for (; i > 0; i--, sp++)
3625 Py_DECREF(*sp);
3626 Py_XDECREF(it);
3627 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003628}
3629
3630
Guido van Rossum96a42c81992-01-12 02:29:51 +00003631#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003632static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003633prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 printf("%s ", str);
3636 if (PyObject_Print(v, stdout, 0) != 0)
3637 PyErr_Clear(); /* Don't know what else to do */
3638 printf("\n");
3639 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003640}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003641#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003642
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003643static void
Fred Drake5755ce62001-06-27 19:19:46 +00003644call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003646 PyObject *type, *value, *traceback, *arg;
3647 int err;
3648 PyErr_Fetch(&type, &value, &traceback);
3649 if (value == NULL) {
3650 value = Py_None;
3651 Py_INCREF(value);
3652 }
3653 arg = PyTuple_Pack(3, type, value, traceback);
3654 if (arg == NULL) {
3655 PyErr_Restore(type, value, traceback);
3656 return;
3657 }
3658 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3659 Py_DECREF(arg);
3660 if (err == 0)
3661 PyErr_Restore(type, value, traceback);
3662 else {
3663 Py_XDECREF(type);
3664 Py_XDECREF(value);
3665 Py_XDECREF(traceback);
3666 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003667}
3668
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003669static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003670call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003672{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003673 PyObject *type, *value, *traceback;
3674 int err;
3675 PyErr_Fetch(&type, &value, &traceback);
3676 err = call_trace(func, obj, frame, what, arg);
3677 if (err == 0)
3678 {
3679 PyErr_Restore(type, value, traceback);
3680 return 0;
3681 }
3682 else {
3683 Py_XDECREF(type);
3684 Py_XDECREF(value);
3685 Py_XDECREF(traceback);
3686 return -1;
3687 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003688}
3689
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003690static int
Fred Drake5755ce62001-06-27 19:19:46 +00003691call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003692 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003694 register PyThreadState *tstate = frame->f_tstate;
3695 int result;
3696 if (tstate->tracing)
3697 return 0;
3698 tstate->tracing++;
3699 tstate->use_tracing = 0;
3700 result = func(obj, frame, what, arg);
3701 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3702 || (tstate->c_profilefunc != NULL));
3703 tstate->tracing--;
3704 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003705}
3706
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003707PyObject *
3708_PyEval_CallTracing(PyObject *func, PyObject *args)
3709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003710 PyFrameObject *frame = PyEval_GetFrame();
3711 PyThreadState *tstate = frame->f_tstate;
3712 int save_tracing = tstate->tracing;
3713 int save_use_tracing = tstate->use_tracing;
3714 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 tstate->tracing = 0;
3717 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3718 || (tstate->c_profilefunc != NULL));
3719 result = PyObject_Call(func, args, NULL);
3720 tstate->tracing = save_tracing;
3721 tstate->use_tracing = save_use_tracing;
3722 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003723}
3724
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003725/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003726static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003727maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3729 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 int result = 0;
3732 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 /* If the last instruction executed isn't in the current
3735 instruction window, reset the window.
3736 */
3737 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3738 PyAddrPair bounds;
3739 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3740 &bounds);
3741 *instr_lb = bounds.ap_lower;
3742 *instr_ub = bounds.ap_upper;
3743 }
3744 /* If the last instruction falls at the start of a line or if
3745 it represents a jump backwards, update the frame's line
3746 number and call the trace function. */
3747 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3748 frame->f_lineno = line;
3749 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3750 }
3751 *instr_prev = frame->f_lasti;
3752 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003753}
3754
Fred Drake5755ce62001-06-27 19:19:46 +00003755void
3756PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003757{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 PyThreadState *tstate = PyThreadState_GET();
3759 PyObject *temp = tstate->c_profileobj;
3760 Py_XINCREF(arg);
3761 tstate->c_profilefunc = NULL;
3762 tstate->c_profileobj = NULL;
3763 /* Must make sure that tracing is not ignored if 'temp' is freed */
3764 tstate->use_tracing = tstate->c_tracefunc != NULL;
3765 Py_XDECREF(temp);
3766 tstate->c_profilefunc = func;
3767 tstate->c_profileobj = arg;
3768 /* Flag that tracing or profiling is turned on */
3769 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003770}
3771
3772void
3773PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 PyThreadState *tstate = PyThreadState_GET();
3776 PyObject *temp = tstate->c_traceobj;
3777 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3778 Py_XINCREF(arg);
3779 tstate->c_tracefunc = NULL;
3780 tstate->c_traceobj = NULL;
3781 /* Must make sure that profiling is not ignored if 'temp' is freed */
3782 tstate->use_tracing = tstate->c_profilefunc != NULL;
3783 Py_XDECREF(temp);
3784 tstate->c_tracefunc = func;
3785 tstate->c_traceobj = arg;
3786 /* Flag that tracing or profiling is turned on */
3787 tstate->use_tracing = ((func != NULL)
3788 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003789}
3790
Guido van Rossumb209a111997-04-29 18:18:01 +00003791PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003792PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 PyFrameObject *current_frame = PyEval_GetFrame();
3795 if (current_frame == NULL)
3796 return PyThreadState_GET()->interp->builtins;
3797 else
3798 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003799}
3800
Guido van Rossumb209a111997-04-29 18:18:01 +00003801PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003802PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 PyFrameObject *current_frame = PyEval_GetFrame();
3805 if (current_frame == NULL)
3806 return NULL;
3807 PyFrame_FastToLocals(current_frame);
3808 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003809}
3810
Guido van Rossumb209a111997-04-29 18:18:01 +00003811PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003812PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 PyFrameObject *current_frame = PyEval_GetFrame();
3815 if (current_frame == NULL)
3816 return NULL;
3817 else
3818 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003819}
3820
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003821PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003822PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 PyThreadState *tstate = PyThreadState_GET();
3825 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003826}
3827
Guido van Rossum6135a871995-01-09 17:53:26 +00003828int
Tim Peters5ba58662001-07-16 02:29:45 +00003829PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003830{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 PyFrameObject *current_frame = PyEval_GetFrame();
3832 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 if (current_frame != NULL) {
3835 const int codeflags = current_frame->f_code->co_flags;
3836 const int compilerflags = codeflags & PyCF_MASK;
3837 if (compilerflags) {
3838 result = 1;
3839 cf->cf_flags |= compilerflags;
3840 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003841#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 if (codeflags & CO_GENERATOR_ALLOWED) {
3843 result = 1;
3844 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3845 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003846#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 }
3848 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003849}
3850
Guido van Rossum3f5da241990-12-20 15:06:42 +00003851
Guido van Rossum681d79a1995-07-18 14:51:37 +00003852/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003853 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003854
Guido van Rossumb209a111997-04-29 18:18:01 +00003855PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003856PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003858 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 if (arg == NULL) {
3861 arg = PyTuple_New(0);
3862 if (arg == NULL)
3863 return NULL;
3864 }
3865 else if (!PyTuple_Check(arg)) {
3866 PyErr_SetString(PyExc_TypeError,
3867 "argument list must be a tuple");
3868 return NULL;
3869 }
3870 else
3871 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 if (kw != NULL && !PyDict_Check(kw)) {
3874 PyErr_SetString(PyExc_TypeError,
3875 "keyword list must be a dictionary");
3876 Py_DECREF(arg);
3877 return NULL;
3878 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 result = PyObject_Call(func, arg, kw);
3881 Py_DECREF(arg);
3882 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003883}
3884
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003885const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003886PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003887{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003888 if (PyMethod_Check(func))
3889 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3890 else if (PyFunction_Check(func))
3891 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3892 else if (PyCFunction_Check(func))
3893 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3894 else
3895 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003896}
3897
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003898const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003899PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003900{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 if (PyMethod_Check(func))
3902 return "()";
3903 else if (PyFunction_Check(func))
3904 return "()";
3905 else if (PyCFunction_Check(func))
3906 return "()";
3907 else
3908 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003909}
3910
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003911static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003912err_args(PyObject *func, int flags, int nargs)
3913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003914 if (flags & METH_NOARGS)
3915 PyErr_Format(PyExc_TypeError,
3916 "%.200s() takes no arguments (%d given)",
3917 ((PyCFunctionObject *)func)->m_ml->ml_name,
3918 nargs);
3919 else
3920 PyErr_Format(PyExc_TypeError,
3921 "%.200s() takes exactly one argument (%d given)",
3922 ((PyCFunctionObject *)func)->m_ml->ml_name,
3923 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003924}
3925
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003926#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003927if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 if (call_trace(tstate->c_profilefunc, \
3929 tstate->c_profileobj, \
3930 tstate->frame, PyTrace_C_CALL, \
3931 func)) { \
3932 x = NULL; \
3933 } \
3934 else { \
3935 x = call; \
3936 if (tstate->c_profilefunc != NULL) { \
3937 if (x == NULL) { \
3938 call_trace_protected(tstate->c_profilefunc, \
3939 tstate->c_profileobj, \
3940 tstate->frame, PyTrace_C_EXCEPTION, \
3941 func); \
3942 /* XXX should pass (type, value, tb) */ \
3943 } else { \
3944 if (call_trace(tstate->c_profilefunc, \
3945 tstate->c_profileobj, \
3946 tstate->frame, PyTrace_C_RETURN, \
3947 func)) { \
3948 Py_DECREF(x); \
3949 x = NULL; \
3950 } \
3951 } \
3952 } \
3953 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003954} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 x = call; \
3956 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003957
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003958static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003959call_function(PyObject ***pp_stack, int oparg
3960#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003962#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 int na = oparg & 0xff;
3966 int nk = (oparg>>8) & 0xff;
3967 int n = na + 2 * nk;
3968 PyObject **pfunc = (*pp_stack) - n - 1;
3969 PyObject *func = *pfunc;
3970 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003972 /* Always dispatch PyCFunction first, because these are
3973 presumed to be the most frequent callable object.
3974 */
3975 if (PyCFunction_Check(func) && nk == 0) {
3976 int flags = PyCFunction_GET_FLAGS(func);
3977 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 PCALL(PCALL_CFUNCTION);
3980 if (flags & (METH_NOARGS | METH_O)) {
3981 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3982 PyObject *self = PyCFunction_GET_SELF(func);
3983 if (flags & METH_NOARGS && na == 0) {
3984 C_TRACE(x, (*meth)(self,NULL));
3985 }
3986 else if (flags & METH_O && na == 1) {
3987 PyObject *arg = EXT_POP(*pp_stack);
3988 C_TRACE(x, (*meth)(self,arg));
3989 Py_DECREF(arg);
3990 }
3991 else {
3992 err_args(func, flags, na);
3993 x = NULL;
3994 }
3995 }
3996 else {
3997 PyObject *callargs;
3998 callargs = load_args(pp_stack, na);
3999 READ_TIMESTAMP(*pintr0);
4000 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4001 READ_TIMESTAMP(*pintr1);
4002 Py_XDECREF(callargs);
4003 }
4004 } else {
4005 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4006 /* optimize access to bound methods */
4007 PyObject *self = PyMethod_GET_SELF(func);
4008 PCALL(PCALL_METHOD);
4009 PCALL(PCALL_BOUND_METHOD);
4010 Py_INCREF(self);
4011 func = PyMethod_GET_FUNCTION(func);
4012 Py_INCREF(func);
4013 Py_DECREF(*pfunc);
4014 *pfunc = self;
4015 na++;
4016 n++;
4017 } else
4018 Py_INCREF(func);
4019 READ_TIMESTAMP(*pintr0);
4020 if (PyFunction_Check(func))
4021 x = fast_function(func, pp_stack, n, na, nk);
4022 else
4023 x = do_call(func, pp_stack, na, nk);
4024 READ_TIMESTAMP(*pintr1);
4025 Py_DECREF(func);
4026 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 /* Clear the stack of the function object. Also removes
4029 the arguments in case they weren't consumed already
4030 (fast_function() and err_args() leave them on the stack).
4031 */
4032 while ((*pp_stack) > pfunc) {
4033 w = EXT_POP(*pp_stack);
4034 Py_DECREF(w);
4035 PCALL(PCALL_POP);
4036 }
4037 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004038}
4039
Jeremy Hylton192690e2002-08-16 18:36:11 +00004040/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004041 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004042 For the simplest case -- a function that takes only positional
4043 arguments and is called with only positional arguments -- it
4044 inlines the most primitive frame setup code from
4045 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4046 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004047*/
4048
4049static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004050fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4053 PyObject *globals = PyFunction_GET_GLOBALS(func);
4054 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4055 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4056 PyObject **d = NULL;
4057 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 PCALL(PCALL_FUNCTION);
4060 PCALL(PCALL_FAST_FUNCTION);
4061 if (argdefs == NULL && co->co_argcount == n &&
4062 co->co_kwonlyargcount == 0 && nk==0 &&
4063 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4064 PyFrameObject *f;
4065 PyObject *retval = NULL;
4066 PyThreadState *tstate = PyThreadState_GET();
4067 PyObject **fastlocals, **stack;
4068 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 PCALL(PCALL_FASTER_FUNCTION);
4071 assert(globals != NULL);
4072 /* XXX Perhaps we should create a specialized
4073 PyFrame_New() that doesn't take locals, but does
4074 take builtins without sanity checking them.
4075 */
4076 assert(tstate != NULL);
4077 f = PyFrame_New(tstate, co, globals, NULL);
4078 if (f == NULL)
4079 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 fastlocals = f->f_localsplus;
4082 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 for (i = 0; i < n; i++) {
4085 Py_INCREF(*stack);
4086 fastlocals[i] = *stack++;
4087 }
4088 retval = PyEval_EvalFrameEx(f,0);
4089 ++tstate->recursion_depth;
4090 Py_DECREF(f);
4091 --tstate->recursion_depth;
4092 return retval;
4093 }
4094 if (argdefs != NULL) {
4095 d = &PyTuple_GET_ITEM(argdefs, 0);
4096 nd = Py_SIZE(argdefs);
4097 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004098 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 (PyObject *)NULL, (*pp_stack)-n, na,
4100 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4101 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004102}
4103
4104static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004105update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4106 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 PyObject *kwdict = NULL;
4109 if (orig_kwdict == NULL)
4110 kwdict = PyDict_New();
4111 else {
4112 kwdict = PyDict_Copy(orig_kwdict);
4113 Py_DECREF(orig_kwdict);
4114 }
4115 if (kwdict == NULL)
4116 return NULL;
4117 while (--nk >= 0) {
4118 int err;
4119 PyObject *value = EXT_POP(*pp_stack);
4120 PyObject *key = EXT_POP(*pp_stack);
4121 if (PyDict_GetItem(kwdict, key) != NULL) {
4122 PyErr_Format(PyExc_TypeError,
4123 "%.200s%s got multiple values "
4124 "for keyword argument '%U'",
4125 PyEval_GetFuncName(func),
4126 PyEval_GetFuncDesc(func),
4127 key);
4128 Py_DECREF(key);
4129 Py_DECREF(value);
4130 Py_DECREF(kwdict);
4131 return NULL;
4132 }
4133 err = PyDict_SetItem(kwdict, key, value);
4134 Py_DECREF(key);
4135 Py_DECREF(value);
4136 if (err) {
4137 Py_DECREF(kwdict);
4138 return NULL;
4139 }
4140 }
4141 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004142}
4143
4144static PyObject *
4145update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004146 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 callargs = PyTuple_New(nstack + nstar);
4151 if (callargs == NULL) {
4152 return NULL;
4153 }
4154 if (nstar) {
4155 int i;
4156 for (i = 0; i < nstar; i++) {
4157 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4158 Py_INCREF(a);
4159 PyTuple_SET_ITEM(callargs, nstack + i, a);
4160 }
4161 }
4162 while (--nstack >= 0) {
4163 w = EXT_POP(*pp_stack);
4164 PyTuple_SET_ITEM(callargs, nstack, w);
4165 }
4166 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004167}
4168
4169static PyObject *
4170load_args(PyObject ***pp_stack, int na)
4171{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 PyObject *args = PyTuple_New(na);
4173 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 if (args == NULL)
4176 return NULL;
4177 while (--na >= 0) {
4178 w = EXT_POP(*pp_stack);
4179 PyTuple_SET_ITEM(args, na, w);
4180 }
4181 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004182}
4183
4184static PyObject *
4185do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4186{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004187 PyObject *callargs = NULL;
4188 PyObject *kwdict = NULL;
4189 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004190
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 if (nk > 0) {
4192 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4193 if (kwdict == NULL)
4194 goto call_fail;
4195 }
4196 callargs = load_args(pp_stack, na);
4197 if (callargs == NULL)
4198 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004199#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 /* At this point, we have to look at the type of func to
4201 update the call stats properly. Do it here so as to avoid
4202 exposing the call stats machinery outside ceval.c
4203 */
4204 if (PyFunction_Check(func))
4205 PCALL(PCALL_FUNCTION);
4206 else if (PyMethod_Check(func))
4207 PCALL(PCALL_METHOD);
4208 else if (PyType_Check(func))
4209 PCALL(PCALL_TYPE);
4210 else if (PyCFunction_Check(func))
4211 PCALL(PCALL_CFUNCTION);
4212 else
4213 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004214#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 if (PyCFunction_Check(func)) {
4216 PyThreadState *tstate = PyThreadState_GET();
4217 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4218 }
4219 else
4220 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004221call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 Py_XDECREF(callargs);
4223 Py_XDECREF(kwdict);
4224 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004225}
4226
4227static PyObject *
4228ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4229{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 int nstar = 0;
4231 PyObject *callargs = NULL;
4232 PyObject *stararg = NULL;
4233 PyObject *kwdict = NULL;
4234 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 if (flags & CALL_FLAG_KW) {
4237 kwdict = EXT_POP(*pp_stack);
4238 if (!PyDict_Check(kwdict)) {
4239 PyObject *d;
4240 d = PyDict_New();
4241 if (d == NULL)
4242 goto ext_call_fail;
4243 if (PyDict_Update(d, kwdict) != 0) {
4244 Py_DECREF(d);
4245 /* PyDict_Update raises attribute
4246 * error (percolated from an attempt
4247 * to get 'keys' attribute) instead of
4248 * a type error if its second argument
4249 * is not a mapping.
4250 */
4251 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4252 PyErr_Format(PyExc_TypeError,
4253 "%.200s%.200s argument after ** "
4254 "must be a mapping, not %.200s",
4255 PyEval_GetFuncName(func),
4256 PyEval_GetFuncDesc(func),
4257 kwdict->ob_type->tp_name);
4258 }
4259 goto ext_call_fail;
4260 }
4261 Py_DECREF(kwdict);
4262 kwdict = d;
4263 }
4264 }
4265 if (flags & CALL_FLAG_VAR) {
4266 stararg = EXT_POP(*pp_stack);
4267 if (!PyTuple_Check(stararg)) {
4268 PyObject *t = NULL;
4269 t = PySequence_Tuple(stararg);
4270 if (t == NULL) {
4271 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4272 PyErr_Format(PyExc_TypeError,
4273 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004274 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 PyEval_GetFuncName(func),
4276 PyEval_GetFuncDesc(func),
4277 stararg->ob_type->tp_name);
4278 }
4279 goto ext_call_fail;
4280 }
4281 Py_DECREF(stararg);
4282 stararg = t;
4283 }
4284 nstar = PyTuple_GET_SIZE(stararg);
4285 }
4286 if (nk > 0) {
4287 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4288 if (kwdict == NULL)
4289 goto ext_call_fail;
4290 }
4291 callargs = update_star_args(na, nstar, stararg, pp_stack);
4292 if (callargs == NULL)
4293 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004294#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 /* At this point, we have to look at the type of func to
4296 update the call stats properly. Do it here so as to avoid
4297 exposing the call stats machinery outside ceval.c
4298 */
4299 if (PyFunction_Check(func))
4300 PCALL(PCALL_FUNCTION);
4301 else if (PyMethod_Check(func))
4302 PCALL(PCALL_METHOD);
4303 else if (PyType_Check(func))
4304 PCALL(PCALL_TYPE);
4305 else if (PyCFunction_Check(func))
4306 PCALL(PCALL_CFUNCTION);
4307 else
4308 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004309#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 if (PyCFunction_Check(func)) {
4311 PyThreadState *tstate = PyThreadState_GET();
4312 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4313 }
4314 else
4315 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004316ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 Py_XDECREF(callargs);
4318 Py_XDECREF(kwdict);
4319 Py_XDECREF(stararg);
4320 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004321}
4322
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004323/* Extract a slice index from a PyInt or PyLong or an object with the
4324 nb_index slot defined, and store in *pi.
4325 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4326 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 +00004327 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004328*/
Tim Petersb5196382001-12-16 19:44:20 +00004329/* Note: If v is NULL, return success without storing into *pi. This
4330 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4331 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004332*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004333int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004334_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 if (v != NULL) {
4337 Py_ssize_t x;
4338 if (PyIndex_Check(v)) {
4339 x = PyNumber_AsSsize_t(v, NULL);
4340 if (x == -1 && PyErr_Occurred())
4341 return 0;
4342 }
4343 else {
4344 PyErr_SetString(PyExc_TypeError,
4345 "slice indices must be integers or "
4346 "None or have an __index__ method");
4347 return 0;
4348 }
4349 *pi = x;
4350 }
4351 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004352}
4353
Guido van Rossum486364b2007-06-30 05:01:58 +00004354#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004356
Guido van Rossumb209a111997-04-29 18:18:01 +00004357static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004358cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 int res = 0;
4361 switch (op) {
4362 case PyCmp_IS:
4363 res = (v == w);
4364 break;
4365 case PyCmp_IS_NOT:
4366 res = (v != w);
4367 break;
4368 case PyCmp_IN:
4369 res = PySequence_Contains(w, v);
4370 if (res < 0)
4371 return NULL;
4372 break;
4373 case PyCmp_NOT_IN:
4374 res = PySequence_Contains(w, v);
4375 if (res < 0)
4376 return NULL;
4377 res = !res;
4378 break;
4379 case PyCmp_EXC_MATCH:
4380 if (PyTuple_Check(w)) {
4381 Py_ssize_t i, length;
4382 length = PyTuple_Size(w);
4383 for (i = 0; i < length; i += 1) {
4384 PyObject *exc = PyTuple_GET_ITEM(w, i);
4385 if (!PyExceptionClass_Check(exc)) {
4386 PyErr_SetString(PyExc_TypeError,
4387 CANNOT_CATCH_MSG);
4388 return NULL;
4389 }
4390 }
4391 }
4392 else {
4393 if (!PyExceptionClass_Check(w)) {
4394 PyErr_SetString(PyExc_TypeError,
4395 CANNOT_CATCH_MSG);
4396 return NULL;
4397 }
4398 }
4399 res = PyErr_GivenExceptionMatches(v, w);
4400 break;
4401 default:
4402 return PyObject_RichCompare(v, w, op);
4403 }
4404 v = res ? Py_True : Py_False;
4405 Py_INCREF(v);
4406 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004407}
4408
Thomas Wouters52152252000-08-17 22:55:00 +00004409static PyObject *
4410import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004411{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 x = PyObject_GetAttr(v, name);
4415 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4416 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4417 }
4418 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004419}
Guido van Rossumac7be682001-01-17 15:42:30 +00004420
Thomas Wouters52152252000-08-17 22:55:00 +00004421static int
4422import_all_from(PyObject *locals, PyObject *v)
4423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004424 PyObject *all = PyObject_GetAttrString(v, "__all__");
4425 PyObject *dict, *name, *value;
4426 int skip_leading_underscores = 0;
4427 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 if (all == NULL) {
4430 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4431 return -1; /* Unexpected error */
4432 PyErr_Clear();
4433 dict = PyObject_GetAttrString(v, "__dict__");
4434 if (dict == NULL) {
4435 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4436 return -1;
4437 PyErr_SetString(PyExc_ImportError,
4438 "from-import-* object has no __dict__ and no __all__");
4439 return -1;
4440 }
4441 all = PyMapping_Keys(dict);
4442 Py_DECREF(dict);
4443 if (all == NULL)
4444 return -1;
4445 skip_leading_underscores = 1;
4446 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 for (pos = 0, err = 0; ; pos++) {
4449 name = PySequence_GetItem(all, pos);
4450 if (name == NULL) {
4451 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4452 err = -1;
4453 else
4454 PyErr_Clear();
4455 break;
4456 }
4457 if (skip_leading_underscores &&
4458 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004459 PyUnicode_READY(name) != -1 &&
4460 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 {
4462 Py_DECREF(name);
4463 continue;
4464 }
4465 value = PyObject_GetAttr(v, name);
4466 if (value == NULL)
4467 err = -1;
4468 else if (PyDict_CheckExact(locals))
4469 err = PyDict_SetItem(locals, name, value);
4470 else
4471 err = PyObject_SetItem(locals, name, value);
4472 Py_DECREF(name);
4473 Py_XDECREF(value);
4474 if (err != 0)
4475 break;
4476 }
4477 Py_DECREF(all);
4478 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004479}
4480
Guido van Rossumac7be682001-01-17 15:42:30 +00004481static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004482format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004483{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 if (!obj)
4487 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 obj_str = _PyUnicode_AsString(obj);
4490 if (!obj_str)
4491 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004492
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004494}
Guido van Rossum950361c1997-01-24 13:49:28 +00004495
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004496static void
4497format_exc_unbound(PyCodeObject *co, int oparg)
4498{
4499 PyObject *name;
4500 /* Don't stomp existing exception */
4501 if (PyErr_Occurred())
4502 return;
4503 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4504 name = PyTuple_GET_ITEM(co->co_cellvars,
4505 oparg);
4506 format_exc_check_arg(
4507 PyExc_UnboundLocalError,
4508 UNBOUNDLOCAL_ERROR_MSG,
4509 name);
4510 } else {
4511 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4512 PyTuple_GET_SIZE(co->co_cellvars));
4513 format_exc_check_arg(PyExc_NameError,
4514 UNBOUNDFREE_ERROR_MSG, name);
4515 }
4516}
4517
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004518static PyObject *
4519unicode_concatenate(PyObject *v, PyObject *w,
4520 PyFrameObject *f, unsigned char *next_instr)
4521{
4522 PyObject *res;
4523 if (Py_REFCNT(v) == 2) {
4524 /* In the common case, there are 2 references to the value
4525 * stored in 'variable' when the += is performed: one on the
4526 * value stack (in 'v') and one still stored in the
4527 * 'variable'. We try to delete the variable now to reduce
4528 * the refcnt to 1.
4529 */
4530 switch (*next_instr) {
4531 case STORE_FAST:
4532 {
4533 int oparg = PEEKARG();
4534 PyObject **fastlocals = f->f_localsplus;
4535 if (GETLOCAL(oparg) == v)
4536 SETLOCAL(oparg, NULL);
4537 break;
4538 }
4539 case STORE_DEREF:
4540 {
4541 PyObject **freevars = (f->f_localsplus +
4542 f->f_code->co_nlocals);
4543 PyObject *c = freevars[PEEKARG()];
4544 if (PyCell_GET(c) == v)
4545 PyCell_Set(c, NULL);
4546 break;
4547 }
4548 case STORE_NAME:
4549 {
4550 PyObject *names = f->f_code->co_names;
4551 PyObject *name = GETITEM(names, PEEKARG());
4552 PyObject *locals = f->f_locals;
4553 if (PyDict_CheckExact(locals) &&
4554 PyDict_GetItem(locals, name) == v) {
4555 if (PyDict_DelItem(locals, name) != 0) {
4556 PyErr_Clear();
4557 }
4558 }
4559 break;
4560 }
4561 }
4562 }
4563 res = v;
4564 PyUnicode_Append(&res, w);
4565 return res;
4566}
4567
Guido van Rossum950361c1997-01-24 13:49:28 +00004568#ifdef DYNAMIC_EXECUTION_PROFILE
4569
Skip Montanarof118cb12001-10-15 20:51:38 +00004570static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004571getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004573 int i;
4574 PyObject *l = PyList_New(256);
4575 if (l == NULL) return NULL;
4576 for (i = 0; i < 256; i++) {
4577 PyObject *x = PyLong_FromLong(a[i]);
4578 if (x == NULL) {
4579 Py_DECREF(l);
4580 return NULL;
4581 }
4582 PyList_SetItem(l, i, x);
4583 }
4584 for (i = 0; i < 256; i++)
4585 a[i] = 0;
4586 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004587}
4588
4589PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004590_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004591{
4592#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004594#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 int i;
4596 PyObject *l = PyList_New(257);
4597 if (l == NULL) return NULL;
4598 for (i = 0; i < 257; i++) {
4599 PyObject *x = getarray(dxpairs[i]);
4600 if (x == NULL) {
4601 Py_DECREF(l);
4602 return NULL;
4603 }
4604 PyList_SetItem(l, i, x);
4605 }
4606 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004607#endif
4608}
4609
4610#endif