blob: 1dd0d49118916adb876db04d2aa0be1ad7b41fb8 [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);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139static 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 +0000494static char pendingbusy = 0;
495
496int
497Py_AddPendingCall(int (*func)(void *), void *arg)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we
505 * hold the lock during the Py_MakePendingCalls() function.
506 * This avoids a deadlock in that case.
507 * Note that signals can be delivered on any thread. In particular,
508 * on Windows, a SIGINT is delivered on a system-created worker
509 * thread.
510 * We also check for lock being NULL, in the unlikely case that
511 * this function is called before any bytecode evaluation takes place.
512 */
513 if (lock != NULL) {
514 for (i = 0; i<100; i++) {
515 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
516 break;
517 }
518 if (i == 100)
519 return -1;
520 }
521
522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
524 if (j == pendingfirst) {
525 result = -1; /* Queue full */
526 } else {
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
530 }
531 /* signal main loop */
532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL)
534 PyThread_release_lock(lock);
535 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538int
539Py_MakePendingCalls(void)
540{
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 */
555 if (pendingbusy)
556 return 0;
557 pendingbusy = 1;
558 /* 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 }
586 pendingbusy = 0;
587 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
Collin Winter828f04a2007-08-31 00:04:24 +0000752static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000753static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000754
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000755/* Records whether tracing is on for any thread. Counts the number of
756 threads for which tstate->c_tracefunc is non-NULL, so if the value
757 is 0, we know we don't have to check this thread's c_tracefunc.
758 This speeds up the if statement in PyEval_EvalFrameEx() after
759 fast_next_opcode*/
760static int _Py_TracingPossible = 0;
761
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000762
Guido van Rossum374a9221991-04-04 10:40:29 +0000763
Guido van Rossumb209a111997-04-29 18:18:01 +0000764PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000765PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 return PyEval_EvalCodeEx(co,
768 globals, locals,
769 (PyObject **)NULL, 0,
770 (PyObject **)NULL, 0,
771 (PyObject **)NULL, 0,
772 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000773}
774
775
776/* Interpreter main loop */
777
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000778PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000779PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 /* This is for backward compatibility with extension modules that
781 used this API; core interpreter code should call
782 PyEval_EvalFrameEx() */
783 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000784}
785
786PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000788{
Guido van Rossum950361c1997-01-24 13:49:28 +0000789#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000791#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 register PyObject **stack_pointer; /* Next free slot in value stack */
793 register unsigned char *next_instr;
794 register int opcode; /* Current opcode */
795 register int oparg; /* Current opcode argument, if any */
796 register enum why_code why; /* Reason for block stack unwind */
797 register int err; /* Error status -- nonzero if error */
798 register PyObject *x; /* Result object -- NULL if error */
799 register PyObject *v; /* Temporary objects popped off stack */
800 register PyObject *w;
801 register PyObject *u;
802 register PyObject *t;
803 register PyObject **fastlocals, **freevars;
804 PyObject *retval = NULL; /* Return value */
805 PyThreadState *tstate = PyThreadState_GET();
806 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 is true when the line being executed has changed. The
813 initial values are such as to make this false the first
814 time it is tested. */
815 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 unsigned char *first_instr;
818 PyObject *names;
819 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000820
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821/* Computed GOTOs, or
822 the-optimization-commonly-but-improperly-known-as-"threaded code"
823 using gcc's labels-as-values extension
824 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
825
826 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000828 combined with a lookup table of jump addresses. However, since the
829 indirect jump instruction is shared by all opcodes, the CPU will have a
830 hard time making the right prediction for where to jump next (actually,
831 it will be always wrong except in the uncommon case of a sequence of
832 several identical opcodes).
833
834 "Threaded code" in contrast, uses an explicit jump table and an explicit
835 indirect jump instruction at the end of each opcode. Since the jump
836 instruction is at a different address for each opcode, the CPU will make a
837 separate prediction for each of these instructions, which is equivalent to
838 predicting the second opcode of each opcode pair. These predictions have
839 a much better chance to turn out valid, especially in small bytecode loops.
840
841 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000843 and potentially many more instructions (depending on the pipeline width).
844 A correctly predicted branch, however, is nearly free.
845
846 At the time of this writing, the "threaded code" version is up to 15-20%
847 faster than the normal "switch" version, depending on the compiler and the
848 CPU architecture.
849
850 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
851 because it would render the measurements invalid.
852
853
854 NOTE: care must be taken that the compiler doesn't try to "optimize" the
855 indirect jumps by sharing them between all opcodes. Such optimizations
856 can be disabled on gcc by using the -fno-gcse flag (or possibly
857 -fno-crossjumping).
858*/
859
Antoine Pitrou042b1282010-08-13 21:15:58 +0000860#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000861#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000862#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000863#endif
864
Antoine Pitrou042b1282010-08-13 21:15:58 +0000865#ifdef HAVE_COMPUTED_GOTOS
866 #ifndef USE_COMPUTED_GOTOS
867 #define USE_COMPUTED_GOTOS 1
868 #endif
869#else
870 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
871 #error "Computed gotos are not supported on this compiler."
872 #endif
873 #undef USE_COMPUTED_GOTOS
874 #define USE_COMPUTED_GOTOS 0
875#endif
876
877#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000878/* Import the static jump table */
879#include "opcode_targets.h"
880
881/* This macro is used when several opcodes defer to the same implementation
882 (e.g. SETUP_LOOP, SETUP_FINALLY) */
883#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 TARGET_##op: \
885 opcode = op; \
886 if (HAS_ARG(op)) \
887 oparg = NEXTARG(); \
888 case op: \
889 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000890
891#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 TARGET_##op: \
893 opcode = op; \
894 if (HAS_ARG(op)) \
895 oparg = NEXTARG(); \
896 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000897
898
899#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 { \
901 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
902 FAST_DISPATCH(); \
903 } \
904 continue; \
905 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000906
907#ifdef LLTRACE
908#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 { \
910 if (!lltrace && !_Py_TracingPossible) { \
911 f->f_lasti = INSTR_OFFSET(); \
912 goto *opcode_targets[*next_instr++]; \
913 } \
914 goto fast_next_opcode; \
915 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000916#else
917#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 { \
919 if (!_Py_TracingPossible) { \
920 f->f_lasti = INSTR_OFFSET(); \
921 goto *opcode_targets[*next_instr++]; \
922 } \
923 goto fast_next_opcode; \
924 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000925#endif
926
927#else
928#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000930#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 /* silence compiler warnings about `impl` unused */ \
932 if (0) goto impl; \
933 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000934#define DISPATCH() continue
935#define FAST_DISPATCH() goto fast_next_opcode
936#endif
937
938
Neal Norwitza81d2202002-07-14 00:27:26 +0000939/* Tuple access macros */
940
941#ifndef Py_DEBUG
942#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
943#else
944#define GETITEM(v, i) PyTuple_GetItem((v), (i))
945#endif
946
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000947#ifdef WITH_TSC
948/* Use Pentium timestamp counter to mark certain events:
949 inst0 -- beginning of switch statement for opcode dispatch
950 inst1 -- end of switch statement (may be skipped)
951 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000952 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000953 (may be skipped)
954 intr1 -- beginning of long interruption
955 intr2 -- end of long interruption
956
957 Many opcodes call out to helper C functions. In some cases, the
958 time in those functions should be counted towards the time for the
959 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
960 calls another Python function; there's no point in charge all the
961 bytecode executed by the called function to the caller.
962
963 It's hard to make a useful judgement statically. In the presence
964 of operator overloading, it's impossible to tell if a call will
965 execute new Python code or not.
966
967 It's a case-by-case judgement. I'll use intr1 for the following
968 cases:
969
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000970 IMPORT_STAR
971 IMPORT_FROM
972 CALL_FUNCTION (and friends)
973
974 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
976 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000977
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 READ_TIMESTAMP(inst0);
979 READ_TIMESTAMP(inst1);
980 READ_TIMESTAMP(loop0);
981 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 /* shut up the compiler */
984 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000985#endif
986
Guido van Rossum374a9221991-04-04 10:40:29 +0000987/* Code access macros */
988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000989#define INSTR_OFFSET() ((int)(next_instr - first_instr))
990#define NEXTOP() (*next_instr++)
991#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
992#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
993#define JUMPTO(x) (next_instr = first_instr + (x))
994#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000995
Raymond Hettingerf606f872003-03-16 03:11:04 +0000996/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 Some opcodes tend to come in pairs thus making it possible to
998 predict the second code when the first is run. For example,
999 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1000 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 Verifying the prediction costs a single high-speed test of a register
1003 variable against a constant. If the pairing was good, then the
1004 processor's own internal branch predication has a high likelihood of
1005 success, resulting in a nearly zero-overhead transition to the
1006 next opcode. A successful prediction saves a trip through the eval-loop
1007 including its two unpredictable branches, the HAS_ARG test and the
1008 switch-case. Combined with the processor's internal branch prediction,
1009 a successful PREDICT has the effect of making the two opcodes run as if
1010 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001011
Georg Brandl86b2fb92008-07-16 03:43:04 +00001012 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 predictions turned-on and interpret the results as if some opcodes
1014 had been combined or turn-off predictions so that the opcode frequency
1015 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001016
1017 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 the CPU to record separate branch prediction information for each
1019 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020
Raymond Hettingerf606f872003-03-16 03:11:04 +00001021*/
1022
Antoine Pitrou042b1282010-08-13 21:15:58 +00001023#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024#define PREDICT(op) if (0) goto PRED_##op
1025#define PREDICTED(op) PRED_##op:
1026#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001027#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1029#define PREDICTED(op) PRED_##op: next_instr++
1030#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001031#endif
1032
Raymond Hettingerf606f872003-03-16 03:11:04 +00001033
Guido van Rossum374a9221991-04-04 10:40:29 +00001034/* Stack manipulation macros */
1035
Martin v. Löwis18e16552006-02-15 17:27:45 +00001036/* The stack can grow at most MAXINT deep, as co_nlocals and
1037 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001038#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1039#define EMPTY() (STACK_LEVEL() == 0)
1040#define TOP() (stack_pointer[-1])
1041#define SECOND() (stack_pointer[-2])
1042#define THIRD() (stack_pointer[-3])
1043#define FOURTH() (stack_pointer[-4])
1044#define PEEK(n) (stack_pointer[-(n)])
1045#define SET_TOP(v) (stack_pointer[-1] = (v))
1046#define SET_SECOND(v) (stack_pointer[-2] = (v))
1047#define SET_THIRD(v) (stack_pointer[-3] = (v))
1048#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1049#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1050#define BASIC_STACKADJ(n) (stack_pointer += n)
1051#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1052#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001053
Guido van Rossum96a42c81992-01-12 02:29:51 +00001054#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001056 lltrace && prtrace(TOP(), "push")); \
1057 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001059 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001061 lltrace && prtrace(TOP(), "stackadj")); \
1062 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001063#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001064 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1065 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001066#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001067#define PUSH(v) BASIC_PUSH(v)
1068#define POP() BASIC_POP()
1069#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001070#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001071#endif
1072
Guido van Rossum681d79a1995-07-18 14:51:37 +00001073/* Local variable macros */
1074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001076
1077/* The SETLOCAL() macro must not DECREF the local variable in-place and
1078 then store the new value; it must copy the old value to a temporary
1079 value, then store the new value, and then DECREF the temporary value.
1080 This is because it is possible that during the DECREF the frame is
1081 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1082 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001084 GETLOCAL(i) = value; \
1085 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001086
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001087
1088#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 while (STACK_LEVEL() > (b)->b_level) { \
1090 PyObject *v = POP(); \
1091 Py_XDECREF(v); \
1092 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001093
1094#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 { \
1096 PyObject *type, *value, *traceback; \
1097 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1098 while (STACK_LEVEL() > (b)->b_level + 3) { \
1099 value = POP(); \
1100 Py_XDECREF(value); \
1101 } \
1102 type = tstate->exc_type; \
1103 value = tstate->exc_value; \
1104 traceback = tstate->exc_traceback; \
1105 tstate->exc_type = POP(); \
1106 tstate->exc_value = POP(); \
1107 tstate->exc_traceback = POP(); \
1108 Py_XDECREF(type); \
1109 Py_XDECREF(value); \
1110 Py_XDECREF(traceback); \
1111 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001112
1113#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 { \
1115 PyObject *type, *value, *traceback; \
1116 Py_XINCREF(tstate->exc_type); \
1117 Py_XINCREF(tstate->exc_value); \
1118 Py_XINCREF(tstate->exc_traceback); \
1119 type = f->f_exc_type; \
1120 value = f->f_exc_value; \
1121 traceback = f->f_exc_traceback; \
1122 f->f_exc_type = tstate->exc_type; \
1123 f->f_exc_value = tstate->exc_value; \
1124 f->f_exc_traceback = tstate->exc_traceback; \
1125 Py_XDECREF(type); \
1126 Py_XDECREF(value); \
1127 Py_XDECREF(traceback); \
1128 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001129
1130#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 { \
1132 PyObject *tmp; \
1133 tmp = tstate->exc_type; \
1134 tstate->exc_type = f->f_exc_type; \
1135 f->f_exc_type = tmp; \
1136 tmp = tstate->exc_value; \
1137 tstate->exc_value = f->f_exc_value; \
1138 f->f_exc_value = tmp; \
1139 tmp = tstate->exc_traceback; \
1140 tstate->exc_traceback = f->f_exc_traceback; \
1141 f->f_exc_traceback = tmp; \
1142 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001143
Benjamin Petersonac913412011-07-03 16:25:11 -05001144#define RESTORE_AND_CLEAR_EXC_STATE() \
1145 { \
1146 PyObject *type, *value, *tb; \
1147 type = tstate->exc_type; \
1148 value = tstate->exc_value; \
1149 tb = tstate->exc_traceback; \
1150 tstate->exc_type = f->f_exc_type; \
1151 tstate->exc_value = f->f_exc_value; \
1152 tstate->exc_traceback = f->f_exc_traceback; \
1153 f->f_exc_type = NULL; \
1154 f->f_exc_value = NULL; \
1155 f->f_exc_traceback = NULL; \
1156 Py_XDECREF(type); \
1157 Py_XDECREF(value); \
1158 Py_XDECREF(tb); \
1159 }
1160
Guido van Rossuma027efa1997-05-05 20:56:21 +00001161/* Start of code */
1162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 if (f == NULL)
1164 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 /* push frame */
1167 if (Py_EnterRecursiveCall(""))
1168 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 if (tstate->use_tracing) {
1173 if (tstate->c_tracefunc != NULL) {
1174 /* tstate->c_tracefunc, if defined, is a
1175 function that will be called on *every* entry
1176 to a code block. Its return value, if not
1177 None, is a function that will be called at
1178 the start of each executed line of code.
1179 (Actually, the function must return itself
1180 in order to continue tracing.) The trace
1181 functions are called with three arguments:
1182 a pointer to the current frame, a string
1183 indicating why the function is called, and
1184 an argument which depends on the situation.
1185 The global trace function is also called
1186 whenever an exception is detected. */
1187 if (call_trace_protected(tstate->c_tracefunc,
1188 tstate->c_traceobj,
1189 f, PyTrace_CALL, Py_None)) {
1190 /* Trace function raised an error */
1191 goto exit_eval_frame;
1192 }
1193 }
1194 if (tstate->c_profilefunc != NULL) {
1195 /* Similar for c_profilefunc, except it needn't
1196 return itself and isn't called for "line" events */
1197 if (call_trace_protected(tstate->c_profilefunc,
1198 tstate->c_profileobj,
1199 f, PyTrace_CALL, Py_None)) {
1200 /* Profile function raised an error */
1201 goto exit_eval_frame;
1202 }
1203 }
1204 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 co = f->f_code;
1207 names = co->co_names;
1208 consts = co->co_consts;
1209 fastlocals = f->f_localsplus;
1210 freevars = f->f_localsplus + co->co_nlocals;
1211 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1212 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 f->f_lasti now refers to the index of the last instruction
1215 executed. You might think this was obvious from the name, but
1216 this wasn't always true before 2.3! PyFrame_New now sets
1217 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1218 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1219 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 When the PREDICT() macros are enabled, some opcode pairs follow in
1222 direct succession without updating f->f_lasti. A successful
1223 prediction effectively links the two codes together as if they
1224 were a single new opcode; accordingly,f->f_lasti will point to
1225 the first code in the pair (for instance, GET_ITER followed by
1226 FOR_ITER is effectively a single opcode and f->f_lasti will point
1227 at to the beginning of the combined pair.)
1228 */
1229 next_instr = first_instr + f->f_lasti + 1;
1230 stack_pointer = f->f_stacktop;
1231 assert(stack_pointer != NULL);
1232 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 if (co->co_flags & CO_GENERATOR && !throwflag) {
1235 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1236 /* We were in an except handler when we left,
1237 restore the exception state which was put aside
1238 (see YIELD_VALUE). */
1239 SWAP_EXC_STATE();
1240 }
1241 else {
1242 SAVE_EXC_STATE();
1243 }
1244 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001245
Tim Peters5ca576e2001-06-18 22:08:13 +00001246#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001248#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 why = WHY_NOT;
1251 err = 0;
1252 x = Py_None; /* Not a reference, just anything non-NULL */
1253 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 if (throwflag) { /* support for generator.throw() */
1256 why = WHY_EXCEPTION;
1257 goto on_error;
1258 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001261#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 if (inst1 == 0) {
1263 /* Almost surely, the opcode executed a break
1264 or a continue, preventing inst1 from being set
1265 on the way out of the loop.
1266 */
1267 READ_TIMESTAMP(inst1);
1268 loop1 = inst1;
1269 }
1270 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1271 intr0, intr1);
1272 ticked = 0;
1273 inst1 = 0;
1274 intr0 = 0;
1275 intr1 = 0;
1276 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001277#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001278 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1279 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 /* Do periodic things. Doing this every time through
1282 the loop would add too much overhead, so we do it
1283 only every Nth instruction. We also do it if
1284 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1285 event needs attention (e.g. a signal handler or
1286 async I/O handler); see Py_AddPendingCall() and
1287 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1290 if (*next_instr == SETUP_FINALLY) {
1291 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001292 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 goto fast_next_opcode;
1294 }
1295 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001296#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001298#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1300 if (Py_MakePendingCalls() < 0) {
1301 why = WHY_EXCEPTION;
1302 goto on_error;
1303 }
1304 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001305#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001306 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001307 /* Give another thread a chance */
1308 if (PyThreadState_Swap(NULL) != tstate)
1309 Py_FatalError("ceval: tstate mix-up");
1310 drop_gil(tstate);
1311
1312 /* Other threads may run now */
1313
1314 take_gil(tstate);
1315 if (PyThreadState_Swap(tstate) != NULL)
1316 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001318#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 /* Check for asynchronous exceptions. */
1320 if (tstate->async_exc != NULL) {
1321 x = tstate->async_exc;
1322 tstate->async_exc = NULL;
1323 UNSIGNAL_ASYNC_EXC();
1324 PyErr_SetNone(x);
1325 Py_DECREF(x);
1326 why = WHY_EXCEPTION;
1327 goto on_error;
1328 }
1329 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 fast_next_opcode:
1332 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001336 if (_Py_TracingPossible &&
1337 tstate->c_tracefunc != NULL && !tstate->tracing) {
1338 /* see maybe_call_line_trace
1339 for expository comments */
1340 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 err = maybe_call_line_trace(tstate->c_tracefunc,
1343 tstate->c_traceobj,
1344 f, &instr_lb, &instr_ub,
1345 &instr_prev);
1346 /* Reload possibly changed frame fields */
1347 JUMPTO(f->f_lasti);
1348 if (f->f_stacktop != NULL) {
1349 stack_pointer = f->f_stacktop;
1350 f->f_stacktop = NULL;
1351 }
1352 if (err) {
1353 /* trace function raised an exception */
1354 goto on_error;
1355 }
1356 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 opcode = NEXTOP();
1361 oparg = 0; /* allows oparg to be stored in a register because
1362 it doesn't have to be remembered across a full loop */
1363 if (HAS_ARG(opcode))
1364 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001365 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001366#ifdef DYNAMIC_EXECUTION_PROFILE
1367#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 dxpairs[lastopcode][opcode]++;
1369 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001370#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001372#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001373
Guido van Rossum96a42c81992-01-12 02:29:51 +00001374#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 if (lltrace) {
1378 if (HAS_ARG(opcode)) {
1379 printf("%d: %d, %d\n",
1380 f->f_lasti, opcode, oparg);
1381 }
1382 else {
1383 printf("%d: %d\n",
1384 f->f_lasti, opcode);
1385 }
1386 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001387#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001388
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 /* Main switch on opcode */
1390 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 /* BEWARE!
1395 It is essential that any operation that fails sets either
1396 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1397 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 TARGET(NOP)
1402 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 TARGET(LOAD_FAST)
1405 x = GETLOCAL(oparg);
1406 if (x != NULL) {
1407 Py_INCREF(x);
1408 PUSH(x);
1409 FAST_DISPATCH();
1410 }
1411 format_exc_check_arg(PyExc_UnboundLocalError,
1412 UNBOUNDLOCAL_ERROR_MSG,
1413 PyTuple_GetItem(co->co_varnames, oparg));
1414 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 TARGET(LOAD_CONST)
1417 x = GETITEM(consts, oparg);
1418 Py_INCREF(x);
1419 PUSH(x);
1420 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 PREDICTED_WITH_ARG(STORE_FAST);
1423 TARGET(STORE_FAST)
1424 v = POP();
1425 SETLOCAL(oparg, v);
1426 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 TARGET(POP_TOP)
1429 v = POP();
1430 Py_DECREF(v);
1431 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 TARGET(ROT_TWO)
1434 v = TOP();
1435 w = SECOND();
1436 SET_TOP(w);
1437 SET_SECOND(v);
1438 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 TARGET(ROT_THREE)
1441 v = TOP();
1442 w = SECOND();
1443 x = THIRD();
1444 SET_TOP(w);
1445 SET_SECOND(x);
1446 SET_THIRD(v);
1447 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 TARGET(DUP_TOP)
1450 v = TOP();
1451 Py_INCREF(v);
1452 PUSH(v);
1453 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001455 TARGET(DUP_TOP_TWO)
1456 x = TOP();
1457 Py_INCREF(x);
1458 w = SECOND();
1459 Py_INCREF(w);
1460 STACKADJ(2);
1461 SET_TOP(x);
1462 SET_SECOND(w);
1463 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 TARGET(UNARY_POSITIVE)
1466 v = TOP();
1467 x = PyNumber_Positive(v);
1468 Py_DECREF(v);
1469 SET_TOP(x);
1470 if (x != NULL) DISPATCH();
1471 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 TARGET(UNARY_NEGATIVE)
1474 v = TOP();
1475 x = PyNumber_Negative(v);
1476 Py_DECREF(v);
1477 SET_TOP(x);
1478 if (x != NULL) DISPATCH();
1479 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 TARGET(UNARY_NOT)
1482 v = TOP();
1483 err = PyObject_IsTrue(v);
1484 Py_DECREF(v);
1485 if (err == 0) {
1486 Py_INCREF(Py_True);
1487 SET_TOP(Py_True);
1488 DISPATCH();
1489 }
1490 else if (err > 0) {
1491 Py_INCREF(Py_False);
1492 SET_TOP(Py_False);
1493 err = 0;
1494 DISPATCH();
1495 }
1496 STACKADJ(-1);
1497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 TARGET(UNARY_INVERT)
1500 v = TOP();
1501 x = PyNumber_Invert(v);
1502 Py_DECREF(v);
1503 SET_TOP(x);
1504 if (x != NULL) DISPATCH();
1505 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 TARGET(BINARY_POWER)
1508 w = POP();
1509 v = TOP();
1510 x = PyNumber_Power(v, w, Py_None);
1511 Py_DECREF(v);
1512 Py_DECREF(w);
1513 SET_TOP(x);
1514 if (x != NULL) DISPATCH();
1515 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001517 TARGET(BINARY_MULTIPLY)
1518 w = POP();
1519 v = TOP();
1520 x = PyNumber_Multiply(v, w);
1521 Py_DECREF(v);
1522 Py_DECREF(w);
1523 SET_TOP(x);
1524 if (x != NULL) DISPATCH();
1525 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 TARGET(BINARY_TRUE_DIVIDE)
1528 w = POP();
1529 v = TOP();
1530 x = PyNumber_TrueDivide(v, w);
1531 Py_DECREF(v);
1532 Py_DECREF(w);
1533 SET_TOP(x);
1534 if (x != NULL) DISPATCH();
1535 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 TARGET(BINARY_FLOOR_DIVIDE)
1538 w = POP();
1539 v = TOP();
1540 x = PyNumber_FloorDivide(v, w);
1541 Py_DECREF(v);
1542 Py_DECREF(w);
1543 SET_TOP(x);
1544 if (x != NULL) DISPATCH();
1545 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 TARGET(BINARY_MODULO)
1548 w = POP();
1549 v = TOP();
1550 if (PyUnicode_CheckExact(v))
1551 x = PyUnicode_Format(v, w);
1552 else
1553 x = PyNumber_Remainder(v, w);
1554 Py_DECREF(v);
1555 Py_DECREF(w);
1556 SET_TOP(x);
1557 if (x != NULL) DISPATCH();
1558 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 TARGET(BINARY_ADD)
1561 w = POP();
1562 v = TOP();
1563 if (PyUnicode_CheckExact(v) &&
1564 PyUnicode_CheckExact(w)) {
1565 x = unicode_concatenate(v, w, f, next_instr);
1566 /* unicode_concatenate consumed the ref to v */
1567 goto skip_decref_vx;
1568 }
1569 else {
1570 x = PyNumber_Add(v, w);
1571 }
1572 Py_DECREF(v);
1573 skip_decref_vx:
1574 Py_DECREF(w);
1575 SET_TOP(x);
1576 if (x != NULL) DISPATCH();
1577 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 TARGET(BINARY_SUBTRACT)
1580 w = POP();
1581 v = TOP();
1582 x = PyNumber_Subtract(v, w);
1583 Py_DECREF(v);
1584 Py_DECREF(w);
1585 SET_TOP(x);
1586 if (x != NULL) DISPATCH();
1587 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 TARGET(BINARY_SUBSCR)
1590 w = POP();
1591 v = TOP();
1592 x = PyObject_GetItem(v, w);
1593 Py_DECREF(v);
1594 Py_DECREF(w);
1595 SET_TOP(x);
1596 if (x != NULL) DISPATCH();
1597 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 TARGET(BINARY_LSHIFT)
1600 w = POP();
1601 v = TOP();
1602 x = PyNumber_Lshift(v, w);
1603 Py_DECREF(v);
1604 Py_DECREF(w);
1605 SET_TOP(x);
1606 if (x != NULL) DISPATCH();
1607 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 TARGET(BINARY_RSHIFT)
1610 w = POP();
1611 v = TOP();
1612 x = PyNumber_Rshift(v, w);
1613 Py_DECREF(v);
1614 Py_DECREF(w);
1615 SET_TOP(x);
1616 if (x != NULL) DISPATCH();
1617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 TARGET(BINARY_AND)
1620 w = POP();
1621 v = TOP();
1622 x = PyNumber_And(v, w);
1623 Py_DECREF(v);
1624 Py_DECREF(w);
1625 SET_TOP(x);
1626 if (x != NULL) DISPATCH();
1627 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 TARGET(BINARY_XOR)
1630 w = POP();
1631 v = TOP();
1632 x = PyNumber_Xor(v, w);
1633 Py_DECREF(v);
1634 Py_DECREF(w);
1635 SET_TOP(x);
1636 if (x != NULL) DISPATCH();
1637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 TARGET(BINARY_OR)
1640 w = POP();
1641 v = TOP();
1642 x = PyNumber_Or(v, w);
1643 Py_DECREF(v);
1644 Py_DECREF(w);
1645 SET_TOP(x);
1646 if (x != NULL) DISPATCH();
1647 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 TARGET(LIST_APPEND)
1650 w = POP();
1651 v = PEEK(oparg);
1652 err = PyList_Append(v, w);
1653 Py_DECREF(w);
1654 if (err == 0) {
1655 PREDICT(JUMP_ABSOLUTE);
1656 DISPATCH();
1657 }
1658 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 TARGET(SET_ADD)
1661 w = POP();
1662 v = stack_pointer[-oparg];
1663 err = PySet_Add(v, w);
1664 Py_DECREF(w);
1665 if (err == 0) {
1666 PREDICT(JUMP_ABSOLUTE);
1667 DISPATCH();
1668 }
1669 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 TARGET(INPLACE_POWER)
1672 w = POP();
1673 v = TOP();
1674 x = PyNumber_InPlacePower(v, w, Py_None);
1675 Py_DECREF(v);
1676 Py_DECREF(w);
1677 SET_TOP(x);
1678 if (x != NULL) DISPATCH();
1679 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 TARGET(INPLACE_MULTIPLY)
1682 w = POP();
1683 v = TOP();
1684 x = PyNumber_InPlaceMultiply(v, w);
1685 Py_DECREF(v);
1686 Py_DECREF(w);
1687 SET_TOP(x);
1688 if (x != NULL) DISPATCH();
1689 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 TARGET(INPLACE_TRUE_DIVIDE)
1692 w = POP();
1693 v = TOP();
1694 x = PyNumber_InPlaceTrueDivide(v, w);
1695 Py_DECREF(v);
1696 Py_DECREF(w);
1697 SET_TOP(x);
1698 if (x != NULL) DISPATCH();
1699 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 TARGET(INPLACE_FLOOR_DIVIDE)
1702 w = POP();
1703 v = TOP();
1704 x = PyNumber_InPlaceFloorDivide(v, w);
1705 Py_DECREF(v);
1706 Py_DECREF(w);
1707 SET_TOP(x);
1708 if (x != NULL) DISPATCH();
1709 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 TARGET(INPLACE_MODULO)
1712 w = POP();
1713 v = TOP();
1714 x = PyNumber_InPlaceRemainder(v, w);
1715 Py_DECREF(v);
1716 Py_DECREF(w);
1717 SET_TOP(x);
1718 if (x != NULL) DISPATCH();
1719 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 TARGET(INPLACE_ADD)
1722 w = POP();
1723 v = TOP();
1724 if (PyUnicode_CheckExact(v) &&
1725 PyUnicode_CheckExact(w)) {
1726 x = unicode_concatenate(v, w, f, next_instr);
1727 /* unicode_concatenate consumed the ref to v */
1728 goto skip_decref_v;
1729 }
1730 else {
1731 x = PyNumber_InPlaceAdd(v, w);
1732 }
1733 Py_DECREF(v);
1734 skip_decref_v:
1735 Py_DECREF(w);
1736 SET_TOP(x);
1737 if (x != NULL) DISPATCH();
1738 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 TARGET(INPLACE_SUBTRACT)
1741 w = POP();
1742 v = TOP();
1743 x = PyNumber_InPlaceSubtract(v, w);
1744 Py_DECREF(v);
1745 Py_DECREF(w);
1746 SET_TOP(x);
1747 if (x != NULL) DISPATCH();
1748 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 TARGET(INPLACE_LSHIFT)
1751 w = POP();
1752 v = TOP();
1753 x = PyNumber_InPlaceLshift(v, w);
1754 Py_DECREF(v);
1755 Py_DECREF(w);
1756 SET_TOP(x);
1757 if (x != NULL) DISPATCH();
1758 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760 TARGET(INPLACE_RSHIFT)
1761 w = POP();
1762 v = TOP();
1763 x = PyNumber_InPlaceRshift(v, w);
1764 Py_DECREF(v);
1765 Py_DECREF(w);
1766 SET_TOP(x);
1767 if (x != NULL) DISPATCH();
1768 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 TARGET(INPLACE_AND)
1771 w = POP();
1772 v = TOP();
1773 x = PyNumber_InPlaceAnd(v, w);
1774 Py_DECREF(v);
1775 Py_DECREF(w);
1776 SET_TOP(x);
1777 if (x != NULL) DISPATCH();
1778 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001780 TARGET(INPLACE_XOR)
1781 w = POP();
1782 v = TOP();
1783 x = PyNumber_InPlaceXor(v, w);
1784 Py_DECREF(v);
1785 Py_DECREF(w);
1786 SET_TOP(x);
1787 if (x != NULL) DISPATCH();
1788 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001790 TARGET(INPLACE_OR)
1791 w = POP();
1792 v = TOP();
1793 x = PyNumber_InPlaceOr(v, w);
1794 Py_DECREF(v);
1795 Py_DECREF(w);
1796 SET_TOP(x);
1797 if (x != NULL) DISPATCH();
1798 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001799
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 TARGET(STORE_SUBSCR)
1801 w = TOP();
1802 v = SECOND();
1803 u = THIRD();
1804 STACKADJ(-3);
1805 /* v[w] = u */
1806 err = PyObject_SetItem(v, w, u);
1807 Py_DECREF(u);
1808 Py_DECREF(v);
1809 Py_DECREF(w);
1810 if (err == 0) DISPATCH();
1811 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 TARGET(DELETE_SUBSCR)
1814 w = TOP();
1815 v = SECOND();
1816 STACKADJ(-2);
1817 /* del v[w] */
1818 err = PyObject_DelItem(v, w);
1819 Py_DECREF(v);
1820 Py_DECREF(w);
1821 if (err == 0) DISPATCH();
1822 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 TARGET(PRINT_EXPR)
1825 v = POP();
1826 w = PySys_GetObject("displayhook");
1827 if (w == NULL) {
1828 PyErr_SetString(PyExc_RuntimeError,
1829 "lost sys.displayhook");
1830 err = -1;
1831 x = NULL;
1832 }
1833 if (err == 0) {
1834 x = PyTuple_Pack(1, v);
1835 if (x == NULL)
1836 err = -1;
1837 }
1838 if (err == 0) {
1839 w = PyEval_CallObject(w, x);
1840 Py_XDECREF(w);
1841 if (w == NULL)
1842 err = -1;
1843 }
1844 Py_DECREF(v);
1845 Py_XDECREF(x);
1846 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001847
Thomas Wouters434d0822000-08-24 20:11:32 +00001848#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001850#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 TARGET(RAISE_VARARGS)
1852 v = w = NULL;
1853 switch (oparg) {
1854 case 2:
1855 v = POP(); /* cause */
1856 case 1:
1857 w = POP(); /* exc */
1858 case 0: /* Fallthrough */
1859 why = do_raise(w, v);
1860 break;
1861 default:
1862 PyErr_SetString(PyExc_SystemError,
1863 "bad RAISE_VARARGS oparg");
1864 why = WHY_EXCEPTION;
1865 break;
1866 }
1867 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 TARGET(STORE_LOCALS)
1870 x = POP();
1871 v = f->f_locals;
1872 Py_XDECREF(v);
1873 f->f_locals = x;
1874 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 TARGET(RETURN_VALUE)
1877 retval = POP();
1878 why = WHY_RETURN;
1879 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 TARGET(YIELD_VALUE)
1882 retval = POP();
1883 f->f_stacktop = stack_pointer;
1884 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 TARGET(POP_EXCEPT)
1888 {
1889 PyTryBlock *b = PyFrame_BlockPop(f);
1890 if (b->b_type != EXCEPT_HANDLER) {
1891 PyErr_SetString(PyExc_SystemError,
1892 "popped block is not an except handler");
1893 why = WHY_EXCEPTION;
1894 break;
1895 }
1896 UNWIND_EXCEPT_HANDLER(b);
1897 }
1898 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 TARGET(POP_BLOCK)
1901 {
1902 PyTryBlock *b = PyFrame_BlockPop(f);
1903 UNWIND_BLOCK(b);
1904 }
1905 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907 PREDICTED(END_FINALLY);
1908 TARGET(END_FINALLY)
1909 v = POP();
1910 if (PyLong_Check(v)) {
1911 why = (enum why_code) PyLong_AS_LONG(v);
1912 assert(why != WHY_YIELD);
1913 if (why == WHY_RETURN ||
1914 why == WHY_CONTINUE)
1915 retval = POP();
1916 if (why == WHY_SILENCED) {
1917 /* An exception was silenced by 'with', we must
1918 manually unwind the EXCEPT_HANDLER block which was
1919 created when the exception was caught, otherwise
1920 the stack will be in an inconsistent state. */
1921 PyTryBlock *b = PyFrame_BlockPop(f);
1922 assert(b->b_type == EXCEPT_HANDLER);
1923 UNWIND_EXCEPT_HANDLER(b);
1924 why = WHY_NOT;
1925 }
1926 }
1927 else if (PyExceptionClass_Check(v)) {
1928 w = POP();
1929 u = POP();
1930 PyErr_Restore(v, w, u);
1931 why = WHY_RERAISE;
1932 break;
1933 }
1934 else if (v != Py_None) {
1935 PyErr_SetString(PyExc_SystemError,
1936 "'finally' pops bad exception");
1937 why = WHY_EXCEPTION;
1938 }
1939 Py_DECREF(v);
1940 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 TARGET(LOAD_BUILD_CLASS)
1943 x = PyDict_GetItemString(f->f_builtins,
1944 "__build_class__");
1945 if (x == NULL) {
1946 PyErr_SetString(PyExc_ImportError,
1947 "__build_class__ not found");
1948 break;
1949 }
1950 Py_INCREF(x);
1951 PUSH(x);
1952 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 TARGET(STORE_NAME)
1955 w = GETITEM(names, oparg);
1956 v = POP();
1957 if ((x = f->f_locals) != NULL) {
1958 if (PyDict_CheckExact(x))
1959 err = PyDict_SetItem(x, w, v);
1960 else
1961 err = PyObject_SetItem(x, w, v);
1962 Py_DECREF(v);
1963 if (err == 0) DISPATCH();
1964 break;
1965 }
1966 PyErr_Format(PyExc_SystemError,
1967 "no locals found when storing %R", w);
1968 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 TARGET(DELETE_NAME)
1971 w = GETITEM(names, oparg);
1972 if ((x = f->f_locals) != NULL) {
1973 if ((err = PyObject_DelItem(x, w)) != 0)
1974 format_exc_check_arg(PyExc_NameError,
1975 NAME_ERROR_MSG,
1976 w);
1977 break;
1978 }
1979 PyErr_Format(PyExc_SystemError,
1980 "no locals when deleting %R", w);
1981 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1984 TARGET(UNPACK_SEQUENCE)
1985 v = POP();
1986 if (PyTuple_CheckExact(v) &&
1987 PyTuple_GET_SIZE(v) == oparg) {
1988 PyObject **items = \
1989 ((PyTupleObject *)v)->ob_item;
1990 while (oparg--) {
1991 w = items[oparg];
1992 Py_INCREF(w);
1993 PUSH(w);
1994 }
1995 Py_DECREF(v);
1996 DISPATCH();
1997 } else if (PyList_CheckExact(v) &&
1998 PyList_GET_SIZE(v) == oparg) {
1999 PyObject **items = \
2000 ((PyListObject *)v)->ob_item;
2001 while (oparg--) {
2002 w = items[oparg];
2003 Py_INCREF(w);
2004 PUSH(w);
2005 }
2006 } else if (unpack_iterable(v, oparg, -1,
2007 stack_pointer + oparg)) {
2008 STACKADJ(oparg);
2009 } else {
2010 /* unpack_iterable() raised an exception */
2011 why = WHY_EXCEPTION;
2012 }
2013 Py_DECREF(v);
2014 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 TARGET(UNPACK_EX)
2017 {
2018 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2019 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2022 stack_pointer + totalargs)) {
2023 stack_pointer += totalargs;
2024 } else {
2025 why = WHY_EXCEPTION;
2026 }
2027 Py_DECREF(v);
2028 break;
2029 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002031 TARGET(STORE_ATTR)
2032 w = GETITEM(names, oparg);
2033 v = TOP();
2034 u = SECOND();
2035 STACKADJ(-2);
2036 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2037 Py_DECREF(v);
2038 Py_DECREF(u);
2039 if (err == 0) DISPATCH();
2040 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002041
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002042 TARGET(DELETE_ATTR)
2043 w = GETITEM(names, oparg);
2044 v = POP();
2045 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2046 /* del v.w */
2047 Py_DECREF(v);
2048 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 TARGET(STORE_GLOBAL)
2051 w = GETITEM(names, oparg);
2052 v = POP();
2053 err = PyDict_SetItem(f->f_globals, w, v);
2054 Py_DECREF(v);
2055 if (err == 0) DISPATCH();
2056 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002058 TARGET(DELETE_GLOBAL)
2059 w = GETITEM(names, oparg);
2060 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2061 format_exc_check_arg(
2062 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2063 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 TARGET(LOAD_NAME)
2066 w = GETITEM(names, oparg);
2067 if ((v = f->f_locals) == NULL) {
2068 PyErr_Format(PyExc_SystemError,
2069 "no locals when loading %R", w);
2070 why = WHY_EXCEPTION;
2071 break;
2072 }
2073 if (PyDict_CheckExact(v)) {
2074 x = PyDict_GetItem(v, w);
2075 Py_XINCREF(x);
2076 }
2077 else {
2078 x = PyObject_GetItem(v, w);
2079 if (x == NULL && PyErr_Occurred()) {
2080 if (!PyErr_ExceptionMatches(
2081 PyExc_KeyError))
2082 break;
2083 PyErr_Clear();
2084 }
2085 }
2086 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002087 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002089 x = PyDict_GetItem(f->f_builtins, w);
2090 if (x == NULL) {
2091 format_exc_check_arg(
2092 PyExc_NameError,
2093 NAME_ERROR_MSG, w);
2094 break;
2095 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 }
2097 Py_INCREF(x);
2098 }
2099 PUSH(x);
2100 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002101
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 TARGET(LOAD_GLOBAL)
2103 w = GETITEM(names, oparg);
2104 if (PyUnicode_CheckExact(w)) {
2105 /* Inline the PyDict_GetItem() calls.
2106 WARNING: this is an extreme speed hack.
2107 Do not try this at home. */
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002108 Py_hash_t hash = ((PyUnicodeObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 if (hash != -1) {
2110 PyDictObject *d;
2111 PyDictEntry *e;
2112 d = (PyDictObject *)(f->f_globals);
2113 e = d->ma_lookup(d, w, hash);
2114 if (e == NULL) {
2115 x = NULL;
2116 break;
2117 }
2118 x = e->me_value;
2119 if (x != NULL) {
2120 Py_INCREF(x);
2121 PUSH(x);
2122 DISPATCH();
2123 }
2124 d = (PyDictObject *)(f->f_builtins);
2125 e = d->ma_lookup(d, w, hash);
2126 if (e == NULL) {
2127 x = NULL;
2128 break;
2129 }
2130 x = e->me_value;
2131 if (x != NULL) {
2132 Py_INCREF(x);
2133 PUSH(x);
2134 DISPATCH();
2135 }
2136 goto load_global_error;
2137 }
2138 }
2139 /* This is the un-inlined version of the code above */
2140 x = PyDict_GetItem(f->f_globals, w);
2141 if (x == NULL) {
2142 x = PyDict_GetItem(f->f_builtins, w);
2143 if (x == NULL) {
2144 load_global_error:
2145 format_exc_check_arg(
2146 PyExc_NameError,
2147 GLOBAL_NAME_ERROR_MSG, w);
2148 break;
2149 }
2150 }
2151 Py_INCREF(x);
2152 PUSH(x);
2153 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 TARGET(DELETE_FAST)
2156 x = GETLOCAL(oparg);
2157 if (x != NULL) {
2158 SETLOCAL(oparg, NULL);
2159 DISPATCH();
2160 }
2161 format_exc_check_arg(
2162 PyExc_UnboundLocalError,
2163 UNBOUNDLOCAL_ERROR_MSG,
2164 PyTuple_GetItem(co->co_varnames, oparg)
2165 );
2166 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002167
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002168 TARGET(DELETE_DEREF)
2169 x = freevars[oparg];
2170 if (PyCell_GET(x) != NULL) {
2171 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002172 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002173 }
2174 err = -1;
2175 format_exc_unbound(co, oparg);
2176 break;
2177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002178 TARGET(LOAD_CLOSURE)
2179 x = freevars[oparg];
2180 Py_INCREF(x);
2181 PUSH(x);
2182 if (x != NULL) DISPATCH();
2183 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002184
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 TARGET(LOAD_DEREF)
2186 x = freevars[oparg];
2187 w = PyCell_Get(x);
2188 if (w != NULL) {
2189 PUSH(w);
2190 DISPATCH();
2191 }
2192 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002193 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002195
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 TARGET(STORE_DEREF)
2197 w = POP();
2198 x = freevars[oparg];
2199 PyCell_Set(x, w);
2200 Py_DECREF(w);
2201 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002202
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002203 TARGET(BUILD_TUPLE)
2204 x = PyTuple_New(oparg);
2205 if (x != NULL) {
2206 for (; --oparg >= 0;) {
2207 w = POP();
2208 PyTuple_SET_ITEM(x, oparg, w);
2209 }
2210 PUSH(x);
2211 DISPATCH();
2212 }
2213 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 TARGET(BUILD_LIST)
2216 x = PyList_New(oparg);
2217 if (x != NULL) {
2218 for (; --oparg >= 0;) {
2219 w = POP();
2220 PyList_SET_ITEM(x, oparg, w);
2221 }
2222 PUSH(x);
2223 DISPATCH();
2224 }
2225 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002227 TARGET(BUILD_SET)
2228 x = PySet_New(NULL);
2229 if (x != NULL) {
2230 for (; --oparg >= 0;) {
2231 w = POP();
2232 if (err == 0)
2233 err = PySet_Add(x, w);
2234 Py_DECREF(w);
2235 }
2236 if (err != 0) {
2237 Py_DECREF(x);
2238 break;
2239 }
2240 PUSH(x);
2241 DISPATCH();
2242 }
2243 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002245 TARGET(BUILD_MAP)
2246 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2247 PUSH(x);
2248 if (x != NULL) DISPATCH();
2249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002251 TARGET(STORE_MAP)
2252 w = TOP(); /* key */
2253 u = SECOND(); /* value */
2254 v = THIRD(); /* dict */
2255 STACKADJ(-2);
2256 assert (PyDict_CheckExact(v));
2257 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2258 Py_DECREF(u);
2259 Py_DECREF(w);
2260 if (err == 0) DISPATCH();
2261 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 TARGET(MAP_ADD)
2264 w = TOP(); /* key */
2265 u = SECOND(); /* value */
2266 STACKADJ(-2);
2267 v = stack_pointer[-oparg]; /* dict */
2268 assert (PyDict_CheckExact(v));
2269 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2270 Py_DECREF(u);
2271 Py_DECREF(w);
2272 if (err == 0) {
2273 PREDICT(JUMP_ABSOLUTE);
2274 DISPATCH();
2275 }
2276 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 TARGET(LOAD_ATTR)
2279 w = GETITEM(names, oparg);
2280 v = TOP();
2281 x = PyObject_GetAttr(v, w);
2282 Py_DECREF(v);
2283 SET_TOP(x);
2284 if (x != NULL) DISPATCH();
2285 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 TARGET(COMPARE_OP)
2288 w = POP();
2289 v = TOP();
2290 x = cmp_outcome(oparg, v, w);
2291 Py_DECREF(v);
2292 Py_DECREF(w);
2293 SET_TOP(x);
2294 if (x == NULL) break;
2295 PREDICT(POP_JUMP_IF_FALSE);
2296 PREDICT(POP_JUMP_IF_TRUE);
2297 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002299 TARGET(IMPORT_NAME)
2300 w = GETITEM(names, oparg);
2301 x = PyDict_GetItemString(f->f_builtins, "__import__");
2302 if (x == NULL) {
2303 PyErr_SetString(PyExc_ImportError,
2304 "__import__ not found");
2305 break;
2306 }
2307 Py_INCREF(x);
2308 v = POP();
2309 u = TOP();
2310 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2311 w = PyTuple_Pack(5,
2312 w,
2313 f->f_globals,
2314 f->f_locals == NULL ?
2315 Py_None : f->f_locals,
2316 v,
2317 u);
2318 else
2319 w = PyTuple_Pack(4,
2320 w,
2321 f->f_globals,
2322 f->f_locals == NULL ?
2323 Py_None : f->f_locals,
2324 v);
2325 Py_DECREF(v);
2326 Py_DECREF(u);
2327 if (w == NULL) {
2328 u = POP();
2329 Py_DECREF(x);
2330 x = NULL;
2331 break;
2332 }
2333 READ_TIMESTAMP(intr0);
2334 v = x;
2335 x = PyEval_CallObject(v, w);
2336 Py_DECREF(v);
2337 READ_TIMESTAMP(intr1);
2338 Py_DECREF(w);
2339 SET_TOP(x);
2340 if (x != NULL) DISPATCH();
2341 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002343 TARGET(IMPORT_STAR)
2344 v = POP();
2345 PyFrame_FastToLocals(f);
2346 if ((x = f->f_locals) == NULL) {
2347 PyErr_SetString(PyExc_SystemError,
2348 "no locals found during 'import *'");
2349 break;
2350 }
2351 READ_TIMESTAMP(intr0);
2352 err = import_all_from(x, v);
2353 READ_TIMESTAMP(intr1);
2354 PyFrame_LocalsToFast(f, 0);
2355 Py_DECREF(v);
2356 if (err == 0) DISPATCH();
2357 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002359 TARGET(IMPORT_FROM)
2360 w = GETITEM(names, oparg);
2361 v = TOP();
2362 READ_TIMESTAMP(intr0);
2363 x = import_from(v, w);
2364 READ_TIMESTAMP(intr1);
2365 PUSH(x);
2366 if (x != NULL) DISPATCH();
2367 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 TARGET(JUMP_FORWARD)
2370 JUMPBY(oparg);
2371 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002373 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2374 TARGET(POP_JUMP_IF_FALSE)
2375 w = POP();
2376 if (w == Py_True) {
2377 Py_DECREF(w);
2378 FAST_DISPATCH();
2379 }
2380 if (w == Py_False) {
2381 Py_DECREF(w);
2382 JUMPTO(oparg);
2383 FAST_DISPATCH();
2384 }
2385 err = PyObject_IsTrue(w);
2386 Py_DECREF(w);
2387 if (err > 0)
2388 err = 0;
2389 else if (err == 0)
2390 JUMPTO(oparg);
2391 else
2392 break;
2393 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2396 TARGET(POP_JUMP_IF_TRUE)
2397 w = POP();
2398 if (w == Py_False) {
2399 Py_DECREF(w);
2400 FAST_DISPATCH();
2401 }
2402 if (w == Py_True) {
2403 Py_DECREF(w);
2404 JUMPTO(oparg);
2405 FAST_DISPATCH();
2406 }
2407 err = PyObject_IsTrue(w);
2408 Py_DECREF(w);
2409 if (err > 0) {
2410 err = 0;
2411 JUMPTO(oparg);
2412 }
2413 else if (err == 0)
2414 ;
2415 else
2416 break;
2417 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419 TARGET(JUMP_IF_FALSE_OR_POP)
2420 w = TOP();
2421 if (w == Py_True) {
2422 STACKADJ(-1);
2423 Py_DECREF(w);
2424 FAST_DISPATCH();
2425 }
2426 if (w == Py_False) {
2427 JUMPTO(oparg);
2428 FAST_DISPATCH();
2429 }
2430 err = PyObject_IsTrue(w);
2431 if (err > 0) {
2432 STACKADJ(-1);
2433 Py_DECREF(w);
2434 err = 0;
2435 }
2436 else if (err == 0)
2437 JUMPTO(oparg);
2438 else
2439 break;
2440 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 TARGET(JUMP_IF_TRUE_OR_POP)
2443 w = TOP();
2444 if (w == Py_False) {
2445 STACKADJ(-1);
2446 Py_DECREF(w);
2447 FAST_DISPATCH();
2448 }
2449 if (w == Py_True) {
2450 JUMPTO(oparg);
2451 FAST_DISPATCH();
2452 }
2453 err = PyObject_IsTrue(w);
2454 if (err > 0) {
2455 err = 0;
2456 JUMPTO(oparg);
2457 }
2458 else if (err == 0) {
2459 STACKADJ(-1);
2460 Py_DECREF(w);
2461 }
2462 else
2463 break;
2464 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2467 TARGET(JUMP_ABSOLUTE)
2468 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002469#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002470 /* Enabling this path speeds-up all while and for-loops by bypassing
2471 the per-loop checks for signals. By default, this should be turned-off
2472 because it prevents detection of a control-break in tight loops like
2473 "while 1: pass". Compile with this option turned-on when you need
2474 the speed-up and do not need break checking inside tight loops (ones
2475 that contain only instructions ending with FAST_DISPATCH).
2476 */
2477 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002478#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002480#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 TARGET(GET_ITER)
2483 /* before: [obj]; after [getiter(obj)] */
2484 v = TOP();
2485 x = PyObject_GetIter(v);
2486 Py_DECREF(v);
2487 if (x != NULL) {
2488 SET_TOP(x);
2489 PREDICT(FOR_ITER);
2490 DISPATCH();
2491 }
2492 STACKADJ(-1);
2493 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002495 PREDICTED_WITH_ARG(FOR_ITER);
2496 TARGET(FOR_ITER)
2497 /* before: [iter]; after: [iter, iter()] *or* [] */
2498 v = TOP();
2499 x = (*v->ob_type->tp_iternext)(v);
2500 if (x != NULL) {
2501 PUSH(x);
2502 PREDICT(STORE_FAST);
2503 PREDICT(UNPACK_SEQUENCE);
2504 DISPATCH();
2505 }
2506 if (PyErr_Occurred()) {
2507 if (!PyErr_ExceptionMatches(
2508 PyExc_StopIteration))
2509 break;
2510 PyErr_Clear();
2511 }
2512 /* iterator ended normally */
2513 x = v = POP();
2514 Py_DECREF(v);
2515 JUMPBY(oparg);
2516 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 TARGET(BREAK_LOOP)
2519 why = WHY_BREAK;
2520 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002522 TARGET(CONTINUE_LOOP)
2523 retval = PyLong_FromLong(oparg);
2524 if (!retval) {
2525 x = NULL;
2526 break;
2527 }
2528 why = WHY_CONTINUE;
2529 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2532 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2533 TARGET(SETUP_FINALLY)
2534 _setup_finally:
2535 /* NOTE: If you add any new block-setup opcodes that
2536 are not try/except/finally handlers, you may need
2537 to update the PyGen_NeedsFinalizing() function.
2538 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2541 STACK_LEVEL());
2542 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002544 TARGET(SETUP_WITH)
2545 {
2546 static PyObject *exit, *enter;
2547 w = TOP();
2548 x = special_lookup(w, "__exit__", &exit);
2549 if (!x)
2550 break;
2551 SET_TOP(x);
2552 u = special_lookup(w, "__enter__", &enter);
2553 Py_DECREF(w);
2554 if (!u) {
2555 x = NULL;
2556 break;
2557 }
2558 x = PyObject_CallFunctionObjArgs(u, NULL);
2559 Py_DECREF(u);
2560 if (!x)
2561 break;
2562 /* Setup the finally block before pushing the result
2563 of __enter__ on the stack. */
2564 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2565 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 PUSH(x);
2568 DISPATCH();
2569 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002571 TARGET(WITH_CLEANUP)
2572 {
2573 /* At the top of the stack are 1-3 values indicating
2574 how/why we entered the finally clause:
2575 - TOP = None
2576 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2577 - TOP = WHY_*; no retval below it
2578 - (TOP, SECOND, THIRD) = exc_info()
2579 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2580 Below them is EXIT, the context.__exit__ bound method.
2581 In the last case, we must call
2582 EXIT(TOP, SECOND, THIRD)
2583 otherwise we must call
2584 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002585
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002586 In the first two cases, we remove EXIT from the
2587 stack, leaving the rest in the same order. In the
2588 third case, we shift the bottom 3 values of the
2589 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 In addition, if the stack represents an exception,
2592 *and* the function call returns a 'true' value, we
2593 push WHY_SILENCED onto the stack. END_FINALLY will
2594 then not re-raise the exception. (But non-local
2595 gotos should still be resumed.)
2596 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002598 PyObject *exit_func;
2599 u = TOP();
2600 if (u == Py_None) {
2601 (void)POP();
2602 exit_func = TOP();
2603 SET_TOP(u);
2604 v = w = Py_None;
2605 }
2606 else if (PyLong_Check(u)) {
2607 (void)POP();
2608 switch(PyLong_AsLong(u)) {
2609 case WHY_RETURN:
2610 case WHY_CONTINUE:
2611 /* Retval in TOP. */
2612 exit_func = SECOND();
2613 SET_SECOND(TOP());
2614 SET_TOP(u);
2615 break;
2616 default:
2617 exit_func = TOP();
2618 SET_TOP(u);
2619 break;
2620 }
2621 u = v = w = Py_None;
2622 }
2623 else {
2624 PyObject *tp, *exc, *tb;
2625 PyTryBlock *block;
2626 v = SECOND();
2627 w = THIRD();
2628 tp = FOURTH();
2629 exc = PEEK(5);
2630 tb = PEEK(6);
2631 exit_func = PEEK(7);
2632 SET_VALUE(7, tb);
2633 SET_VALUE(6, exc);
2634 SET_VALUE(5, tp);
2635 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2636 SET_FOURTH(NULL);
2637 /* We just shifted the stack down, so we have
2638 to tell the except handler block that the
2639 values are lower than it expects. */
2640 block = &f->f_blockstack[f->f_iblock - 1];
2641 assert(block->b_type == EXCEPT_HANDLER);
2642 block->b_level--;
2643 }
2644 /* XXX Not the fastest way to call it... */
2645 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2646 NULL);
2647 Py_DECREF(exit_func);
2648 if (x == NULL)
2649 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002651 if (u != Py_None)
2652 err = PyObject_IsTrue(x);
2653 else
2654 err = 0;
2655 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 if (err < 0)
2658 break; /* Go to error exit */
2659 else if (err > 0) {
2660 err = 0;
2661 /* There was an exception and a True return */
2662 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2663 }
2664 PREDICT(END_FINALLY);
2665 break;
2666 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 TARGET(CALL_FUNCTION)
2669 {
2670 PyObject **sp;
2671 PCALL(PCALL_ALL);
2672 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002673#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002675#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002677#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 stack_pointer = sp;
2679 PUSH(x);
2680 if (x != NULL)
2681 DISPATCH();
2682 break;
2683 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002684
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2686 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2687 TARGET(CALL_FUNCTION_VAR_KW)
2688 _call_function_var_kw:
2689 {
2690 int na = oparg & 0xff;
2691 int nk = (oparg>>8) & 0xff;
2692 int flags = (opcode - CALL_FUNCTION) & 3;
2693 int n = na + 2 * nk;
2694 PyObject **pfunc, *func, **sp;
2695 PCALL(PCALL_ALL);
2696 if (flags & CALL_FLAG_VAR)
2697 n++;
2698 if (flags & CALL_FLAG_KW)
2699 n++;
2700 pfunc = stack_pointer - n - 1;
2701 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002703 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002704 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002705 PyObject *self = PyMethod_GET_SELF(func);
2706 Py_INCREF(self);
2707 func = PyMethod_GET_FUNCTION(func);
2708 Py_INCREF(func);
2709 Py_DECREF(*pfunc);
2710 *pfunc = self;
2711 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002712 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 } else
2714 Py_INCREF(func);
2715 sp = stack_pointer;
2716 READ_TIMESTAMP(intr0);
2717 x = ext_do_call(func, &sp, flags, na, nk);
2718 READ_TIMESTAMP(intr1);
2719 stack_pointer = sp;
2720 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 while (stack_pointer > pfunc) {
2723 w = POP();
2724 Py_DECREF(w);
2725 }
2726 PUSH(x);
2727 if (x != NULL)
2728 DISPATCH();
2729 break;
2730 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2733 TARGET(MAKE_FUNCTION)
2734 _make_function:
2735 {
2736 int posdefaults = oparg & 0xff;
2737 int kwdefaults = (oparg>>8) & 0xff;
2738 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 v = POP(); /* code object */
2741 x = PyFunction_New(v, f->f_globals);
2742 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 if (x != NULL && opcode == MAKE_CLOSURE) {
2745 v = POP();
2746 if (PyFunction_SetClosure(x, v) != 0) {
2747 /* Can't happen unless bytecode is corrupt. */
2748 why = WHY_EXCEPTION;
2749 }
2750 Py_DECREF(v);
2751 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 if (x != NULL && num_annotations > 0) {
2754 Py_ssize_t name_ix;
2755 u = POP(); /* names of args with annotations */
2756 v = PyDict_New();
2757 if (v == NULL) {
2758 Py_DECREF(x);
2759 x = NULL;
2760 break;
2761 }
2762 name_ix = PyTuple_Size(u);
2763 assert(num_annotations == name_ix+1);
2764 while (name_ix > 0) {
2765 --name_ix;
2766 t = PyTuple_GET_ITEM(u, name_ix);
2767 w = POP();
2768 /* XXX(nnorwitz): check for errors */
2769 PyDict_SetItem(v, t, w);
2770 Py_DECREF(w);
2771 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 if (PyFunction_SetAnnotations(x, v) != 0) {
2774 /* Can't happen unless
2775 PyFunction_SetAnnotations changes. */
2776 why = WHY_EXCEPTION;
2777 }
2778 Py_DECREF(v);
2779 Py_DECREF(u);
2780 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 /* XXX Maybe this should be a separate opcode? */
2783 if (x != NULL && posdefaults > 0) {
2784 v = PyTuple_New(posdefaults);
2785 if (v == NULL) {
2786 Py_DECREF(x);
2787 x = NULL;
2788 break;
2789 }
2790 while (--posdefaults >= 0) {
2791 w = POP();
2792 PyTuple_SET_ITEM(v, posdefaults, w);
2793 }
2794 if (PyFunction_SetDefaults(x, v) != 0) {
2795 /* Can't happen unless
2796 PyFunction_SetDefaults changes. */
2797 why = WHY_EXCEPTION;
2798 }
2799 Py_DECREF(v);
2800 }
2801 if (x != NULL && kwdefaults > 0) {
2802 v = PyDict_New();
2803 if (v == NULL) {
2804 Py_DECREF(x);
2805 x = NULL;
2806 break;
2807 }
2808 while (--kwdefaults >= 0) {
2809 w = POP(); /* default value */
2810 u = POP(); /* kw only arg name */
2811 /* XXX(nnorwitz): check for errors */
2812 PyDict_SetItem(v, u, w);
2813 Py_DECREF(w);
2814 Py_DECREF(u);
2815 }
2816 if (PyFunction_SetKwDefaults(x, v) != 0) {
2817 /* Can't happen unless
2818 PyFunction_SetKwDefaults changes. */
2819 why = WHY_EXCEPTION;
2820 }
2821 Py_DECREF(v);
2822 }
2823 PUSH(x);
2824 break;
2825 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 TARGET(BUILD_SLICE)
2828 if (oparg == 3)
2829 w = POP();
2830 else
2831 w = NULL;
2832 v = POP();
2833 u = TOP();
2834 x = PySlice_New(u, v, w);
2835 Py_DECREF(u);
2836 Py_DECREF(v);
2837 Py_XDECREF(w);
2838 SET_TOP(x);
2839 if (x != NULL) DISPATCH();
2840 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 TARGET(EXTENDED_ARG)
2843 opcode = NEXTOP();
2844 oparg = oparg<<16 | NEXTARG();
2845 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002846
Antoine Pitrou042b1282010-08-13 21:15:58 +00002847#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002849#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 default:
2851 fprintf(stderr,
2852 "XXX lineno: %d, opcode: %d\n",
2853 PyFrame_GetLineNumber(f),
2854 opcode);
2855 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2856 why = WHY_EXCEPTION;
2857 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002858
2859#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002860 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002861#endif
2862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 if (why == WHY_NOT) {
2872 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002873#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 /* This check is expensive! */
2875 if (PyErr_Occurred())
2876 fprintf(stderr,
2877 "XXX undetected error\n");
2878 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002879#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 READ_TIMESTAMP(loop1);
2881 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002882#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002884#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002885 }
2886 why = WHY_EXCEPTION;
2887 x = Py_None;
2888 err = 0;
2889 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2894 if (!PyErr_Occurred()) {
2895 PyErr_SetString(PyExc_SystemError,
2896 "error return without exception set");
2897 why = WHY_EXCEPTION;
2898 }
2899 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002900#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 else {
2902 /* This check is expensive! */
2903 if (PyErr_Occurred()) {
2904 char buf[128];
2905 sprintf(buf, "Stack unwind with exception "
2906 "set and why=%d", why);
2907 Py_FatalError(buf);
2908 }
2909 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002910#endif
2911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 if (why == WHY_EXCEPTION) {
2915 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 if (tstate->c_tracefunc != NULL)
2918 call_exc_trace(tstate->c_tracefunc,
2919 tstate->c_traceobj, f);
2920 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002924 if (why == WHY_RERAISE)
2925 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002928
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002929fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 while (why != WHY_NOT && f->f_iblock > 0) {
2931 /* Peek at the current block. */
2932 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 assert(why != WHY_YIELD);
2935 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2936 why = WHY_NOT;
2937 JUMPTO(PyLong_AS_LONG(retval));
2938 Py_DECREF(retval);
2939 break;
2940 }
2941 /* Now we have to pop the block. */
2942 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002944 if (b->b_type == EXCEPT_HANDLER) {
2945 UNWIND_EXCEPT_HANDLER(b);
2946 continue;
2947 }
2948 UNWIND_BLOCK(b);
2949 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2950 why = WHY_NOT;
2951 JUMPTO(b->b_handler);
2952 break;
2953 }
2954 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2955 || b->b_type == SETUP_FINALLY)) {
2956 PyObject *exc, *val, *tb;
2957 int handler = b->b_handler;
2958 /* Beware, this invalidates all b->b_* fields */
2959 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2960 PUSH(tstate->exc_traceback);
2961 PUSH(tstate->exc_value);
2962 if (tstate->exc_type != NULL) {
2963 PUSH(tstate->exc_type);
2964 }
2965 else {
2966 Py_INCREF(Py_None);
2967 PUSH(Py_None);
2968 }
2969 PyErr_Fetch(&exc, &val, &tb);
2970 /* Make the raw exception data
2971 available to the handler,
2972 so a program can emulate the
2973 Python main loop. */
2974 PyErr_NormalizeException(
2975 &exc, &val, &tb);
2976 PyException_SetTraceback(val, tb);
2977 Py_INCREF(exc);
2978 tstate->exc_type = exc;
2979 Py_INCREF(val);
2980 tstate->exc_value = val;
2981 tstate->exc_traceback = tb;
2982 if (tb == NULL)
2983 tb = Py_None;
2984 Py_INCREF(tb);
2985 PUSH(tb);
2986 PUSH(val);
2987 PUSH(exc);
2988 why = WHY_NOT;
2989 JUMPTO(handler);
2990 break;
2991 }
2992 if (b->b_type == SETUP_FINALLY) {
2993 if (why & (WHY_RETURN | WHY_CONTINUE))
2994 PUSH(retval);
2995 PUSH(PyLong_FromLong((long)why));
2996 why = WHY_NOT;
2997 JUMPTO(b->b_handler);
2998 break;
2999 }
3000 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 if (why != WHY_NOT)
3005 break;
3006 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 assert(why != WHY_YIELD);
3011 /* Pop remaining stack entries. */
3012 while (!EMPTY()) {
3013 v = POP();
3014 Py_XDECREF(v);
3015 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 if (why != WHY_RETURN)
3018 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003019
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003020fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003021 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3022 /* The purpose of this block is to put aside the generator's exception
3023 state and restore that of the calling frame. If the current
3024 exception state is from the caller, we clear the exception values
3025 on the generator frame, so they are not swapped back in latter. The
3026 origin of the current exception state is determined by checking for
3027 except handler blocks, which we must be in iff a new exception
3028 state came into existence in this frame. (An uncaught exception
3029 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3030 int i;
3031 for (i = 0; i < f->f_iblock; i++)
3032 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3033 break;
3034 if (i == f->f_iblock)
3035 /* We did not create this exception. */
3036 RESTORE_AND_CLEAR_EXC_STATE()
3037 else
3038 SWAP_EXC_STATE()
3039 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 if (tstate->use_tracing) {
3042 if (tstate->c_tracefunc) {
3043 if (why == WHY_RETURN || why == WHY_YIELD) {
3044 if (call_trace(tstate->c_tracefunc,
3045 tstate->c_traceobj, f,
3046 PyTrace_RETURN, retval)) {
3047 Py_XDECREF(retval);
3048 retval = NULL;
3049 why = WHY_EXCEPTION;
3050 }
3051 }
3052 else if (why == WHY_EXCEPTION) {
3053 call_trace_protected(tstate->c_tracefunc,
3054 tstate->c_traceobj, f,
3055 PyTrace_RETURN, NULL);
3056 }
3057 }
3058 if (tstate->c_profilefunc) {
3059 if (why == WHY_EXCEPTION)
3060 call_trace_protected(tstate->c_profilefunc,
3061 tstate->c_profileobj, f,
3062 PyTrace_RETURN, NULL);
3063 else if (call_trace(tstate->c_profilefunc,
3064 tstate->c_profileobj, f,
3065 PyTrace_RETURN, retval)) {
3066 Py_XDECREF(retval);
3067 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003068 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 }
3070 }
3071 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003074exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 Py_LeaveRecursiveCall();
3076 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003078 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003079}
3080
Benjamin Petersonb204a422011-06-05 22:04:07 -05003081static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003082format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3083{
3084 int err;
3085 Py_ssize_t len = PyList_GET_SIZE(names);
3086 PyObject *name_str, *comma, *tail, *tmp;
3087
3088 assert(PyList_CheckExact(names));
3089 assert(len >= 1);
3090 /* Deal with the joys of natural language. */
3091 switch (len) {
3092 case 1:
3093 name_str = PyList_GET_ITEM(names, 0);
3094 Py_INCREF(name_str);
3095 break;
3096 case 2:
3097 name_str = PyUnicode_FromFormat("%U and %U",
3098 PyList_GET_ITEM(names, len - 2),
3099 PyList_GET_ITEM(names, len - 1));
3100 break;
3101 default:
3102 tail = PyUnicode_FromFormat(", %U, and %U",
3103 PyList_GET_ITEM(names, len - 2),
3104 PyList_GET_ITEM(names, len - 1));
3105 /* Chop off the last two objects in the list. This shouldn't actually
3106 fail, but we can't be too careful. */
3107 err = PyList_SetSlice(names, len - 2, len, NULL);
3108 if (err == -1) {
3109 Py_DECREF(tail);
3110 return;
3111 }
3112 /* Stitch everything up into a nice comma-separated list. */
3113 comma = PyUnicode_FromString(", ");
3114 if (comma == NULL) {
3115 Py_DECREF(tail);
3116 return;
3117 }
3118 tmp = PyUnicode_Join(comma, names);
3119 Py_DECREF(comma);
3120 if (tmp == NULL) {
3121 Py_DECREF(tail);
3122 return;
3123 }
3124 name_str = PyUnicode_Concat(tmp, tail);
3125 Py_DECREF(tmp);
3126 Py_DECREF(tail);
3127 break;
3128 }
3129 if (name_str == NULL)
3130 return;
3131 PyErr_Format(PyExc_TypeError,
3132 "%U() missing %i required %s argument%s: %U",
3133 co->co_name,
3134 len,
3135 kind,
3136 len == 1 ? "" : "s",
3137 name_str);
3138 Py_DECREF(name_str);
3139}
3140
3141static void
3142missing_arguments(PyCodeObject *co, int missing, int defcount,
3143 PyObject **fastlocals)
3144{
3145 int i, j = 0;
3146 int start, end;
3147 int positional = defcount != -1;
3148 const char *kind = positional ? "positional" : "keyword-only";
3149 PyObject *missing_names;
3150
3151 /* Compute the names of the arguments that are missing. */
3152 missing_names = PyList_New(missing);
3153 if (missing_names == NULL)
3154 return;
3155 if (positional) {
3156 start = 0;
3157 end = co->co_argcount - defcount;
3158 }
3159 else {
3160 start = co->co_argcount;
3161 end = start + co->co_kwonlyargcount;
3162 }
3163 for (i = start; i < end; i++) {
3164 if (GETLOCAL(i) == NULL) {
3165 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3166 PyObject *name = PyObject_Repr(raw);
3167 if (name == NULL) {
3168 Py_DECREF(missing_names);
3169 return;
3170 }
3171 PyList_SET_ITEM(missing_names, j++, name);
3172 }
3173 }
3174 assert(j == missing);
3175 format_missing(kind, co, missing_names);
3176 Py_DECREF(missing_names);
3177}
3178
3179static void
3180too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003181{
3182 int plural;
3183 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003184 int i;
3185 PyObject *sig, *kwonly_sig;
3186
Benjamin Petersone109c702011-06-24 09:37:26 -05003187 assert((co->co_flags & CO_VARARGS) == 0);
3188 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003189 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003190 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003191 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003192 if (defcount) {
3193 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003194 plural = 1;
3195 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3196 }
3197 else {
3198 plural = co->co_argcount != 1;
3199 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3200 }
3201 if (sig == NULL)
3202 return;
3203 if (kwonly_given) {
3204 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3205 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3206 kwonly_given != 1 ? "s" : "");
3207 if (kwonly_sig == NULL) {
3208 Py_DECREF(sig);
3209 return;
3210 }
3211 }
3212 else {
3213 /* This will not fail. */
3214 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003215 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003216 }
3217 PyErr_Format(PyExc_TypeError,
3218 "%U() takes %U positional argument%s but %d%U %s given",
3219 co->co_name,
3220 sig,
3221 plural ? "s" : "",
3222 given,
3223 kwonly_sig,
3224 given == 1 && !kwonly_given ? "was" : "were");
3225 Py_DECREF(sig);
3226 Py_DECREF(kwonly_sig);
3227}
3228
Guido van Rossumc2e20742006-02-27 22:32:47 +00003229/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003230 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003231 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003232
Tim Peters6d6c1a32001-08-02 04:15:00 +00003233PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003234PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003235 PyObject **args, int argcount, PyObject **kws, int kwcount,
3236 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003237{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003238 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 register PyFrameObject *f;
3240 register PyObject *retval = NULL;
3241 register PyObject **fastlocals, **freevars;
3242 PyThreadState *tstate = PyThreadState_GET();
3243 PyObject *x, *u;
3244 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003245 int i;
3246 int n = argcount;
3247 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 if (globals == NULL) {
3250 PyErr_SetString(PyExc_SystemError,
3251 "PyEval_EvalCodeEx: NULL globals");
3252 return NULL;
3253 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003255 assert(tstate != NULL);
3256 assert(globals != NULL);
3257 f = PyFrame_New(tstate, co, globals, locals);
3258 if (f == NULL)
3259 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003260
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 fastlocals = f->f_localsplus;
3262 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003263
Benjamin Petersonb204a422011-06-05 22:04:07 -05003264 /* Parse arguments. */
3265 if (co->co_flags & CO_VARKEYWORDS) {
3266 kwdict = PyDict_New();
3267 if (kwdict == NULL)
3268 goto fail;
3269 i = total_args;
3270 if (co->co_flags & CO_VARARGS)
3271 i++;
3272 SETLOCAL(i, kwdict);
3273 }
3274 if (argcount > co->co_argcount)
3275 n = co->co_argcount;
3276 for (i = 0; i < n; i++) {
3277 x = args[i];
3278 Py_INCREF(x);
3279 SETLOCAL(i, x);
3280 }
3281 if (co->co_flags & CO_VARARGS) {
3282 u = PyTuple_New(argcount - n);
3283 if (u == NULL)
3284 goto fail;
3285 SETLOCAL(total_args, u);
3286 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 x = args[i];
3288 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003289 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003291 }
3292 for (i = 0; i < kwcount; i++) {
3293 PyObject **co_varnames;
3294 PyObject *keyword = kws[2*i];
3295 PyObject *value = kws[2*i + 1];
3296 int j;
3297 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3298 PyErr_Format(PyExc_TypeError,
3299 "%U() keywords must be strings",
3300 co->co_name);
3301 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003303 /* Speed hack: do raw pointer compares. As names are
3304 normally interned this should almost always hit. */
3305 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3306 for (j = 0; j < total_args; j++) {
3307 PyObject *nm = co_varnames[j];
3308 if (nm == keyword)
3309 goto kw_found;
3310 }
3311 /* Slow fallback, just in case */
3312 for (j = 0; j < total_args; j++) {
3313 PyObject *nm = co_varnames[j];
3314 int cmp = PyObject_RichCompareBool(
3315 keyword, nm, Py_EQ);
3316 if (cmp > 0)
3317 goto kw_found;
3318 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003320 }
3321 if (j >= total_args && kwdict == NULL) {
3322 PyErr_Format(PyExc_TypeError,
3323 "%U() got an unexpected "
3324 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 co->co_name,
3326 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003327 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003329 PyDict_SetItem(kwdict, keyword, value);
3330 continue;
3331 kw_found:
3332 if (GETLOCAL(j) != NULL) {
3333 PyErr_Format(PyExc_TypeError,
3334 "%U() got multiple "
3335 "values for argument '%S'",
3336 co->co_name,
3337 keyword);
3338 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003340 Py_INCREF(value);
3341 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003343 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003344 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 goto fail;
3346 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003347 if (argcount < co->co_argcount) {
3348 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003349 int missing = 0;
3350 for (i = argcount; i < m; i++)
3351 if (GETLOCAL(i) == NULL)
3352 missing++;
3353 if (missing) {
3354 missing_arguments(co, missing, defcount, fastlocals);
3355 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003356 }
3357 if (n > m)
3358 i = n - m;
3359 else
3360 i = 0;
3361 for (; i < defcount; i++) {
3362 if (GETLOCAL(m+i) == NULL) {
3363 PyObject *def = defs[i];
3364 Py_INCREF(def);
3365 SETLOCAL(m+i, def);
3366 }
3367 }
3368 }
3369 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003370 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003371 for (i = co->co_argcount; i < total_args; i++) {
3372 PyObject *name;
3373 if (GETLOCAL(i) != NULL)
3374 continue;
3375 name = PyTuple_GET_ITEM(co->co_varnames, i);
3376 if (kwdefs != NULL) {
3377 PyObject *def = PyDict_GetItem(kwdefs, name);
3378 if (def) {
3379 Py_INCREF(def);
3380 SETLOCAL(i, def);
3381 continue;
3382 }
3383 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003384 missing++;
3385 }
3386 if (missing) {
3387 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003388 goto fail;
3389 }
3390 }
3391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003393 vars into frame. */
3394 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003395 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003396 int arg;
3397 /* Possibly account for the cell variable being an argument. */
3398 if (co->co_cell2arg != NULL &&
3399 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3400 c = PyCell_New(GETLOCAL(arg));
3401 else
3402 c = PyCell_New(NULL);
3403 if (c == NULL)
3404 goto fail;
3405 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003406 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003407 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3408 PyObject *o = PyTuple_GET_ITEM(closure, i);
3409 Py_INCREF(o);
3410 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003412
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003413 if (co->co_flags & CO_GENERATOR) {
3414 /* Don't need to keep the reference to f_back, it will be set
3415 * when the generator is resumed. */
3416 Py_XDECREF(f->f_back);
3417 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003421 /* Create a new generator that owns the ready to run frame
3422 * and return that as the value. */
3423 return PyGen_New(f);
3424 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003427
Thomas Woutersce272b62007-09-19 21:19:28 +00003428fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003430 /* decref'ing the frame can cause __del__ methods to get invoked,
3431 which can call back into Python. While we're done with the
3432 current Python frame (f), the associated C stack is still in use,
3433 so recursion_depth must be boosted for the duration.
3434 */
3435 assert(tstate != NULL);
3436 ++tstate->recursion_depth;
3437 Py_DECREF(f);
3438 --tstate->recursion_depth;
3439 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003440}
3441
3442
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003443static PyObject *
3444special_lookup(PyObject *o, char *meth, PyObject **cache)
3445{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 PyObject *res;
3447 res = _PyObject_LookupSpecial(o, meth, cache);
3448 if (res == NULL && !PyErr_Occurred()) {
3449 PyErr_SetObject(PyExc_AttributeError, *cache);
3450 return NULL;
3451 }
3452 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003453}
3454
3455
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003456/* Logic for the raise statement (too complicated for inlining).
3457 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003458static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003459do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003460{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003461 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 if (exc == NULL) {
3464 /* Reraise */
3465 PyThreadState *tstate = PyThreadState_GET();
3466 PyObject *tb;
3467 type = tstate->exc_type;
3468 value = tstate->exc_value;
3469 tb = tstate->exc_traceback;
3470 if (type == Py_None) {
3471 PyErr_SetString(PyExc_RuntimeError,
3472 "No active exception to reraise");
3473 return WHY_EXCEPTION;
3474 }
3475 Py_XINCREF(type);
3476 Py_XINCREF(value);
3477 Py_XINCREF(tb);
3478 PyErr_Restore(type, value, tb);
3479 return WHY_RERAISE;
3480 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 /* We support the following forms of raise:
3483 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003484 raise <instance>
3485 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 if (PyExceptionClass_Check(exc)) {
3488 type = exc;
3489 value = PyObject_CallObject(exc, NULL);
3490 if (value == NULL)
3491 goto raise_error;
3492 }
3493 else if (PyExceptionInstance_Check(exc)) {
3494 value = exc;
3495 type = PyExceptionInstance_Class(exc);
3496 Py_INCREF(type);
3497 }
3498 else {
3499 /* Not something you can raise. You get an exception
3500 anyway, just not what you specified :-) */
3501 Py_DECREF(exc);
3502 PyErr_SetString(PyExc_TypeError,
3503 "exceptions must derive from BaseException");
3504 goto raise_error;
3505 }
Collin Winter828f04a2007-08-31 00:04:24 +00003506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003507 if (cause) {
3508 PyObject *fixed_cause;
3509 if (PyExceptionClass_Check(cause)) {
3510 fixed_cause = PyObject_CallObject(cause, NULL);
3511 if (fixed_cause == NULL)
3512 goto raise_error;
3513 Py_DECREF(cause);
3514 }
3515 else if (PyExceptionInstance_Check(cause)) {
3516 fixed_cause = cause;
3517 }
3518 else {
3519 PyErr_SetString(PyExc_TypeError,
3520 "exception causes must derive from "
3521 "BaseException");
3522 goto raise_error;
3523 }
3524 PyException_SetCause(value, fixed_cause);
3525 }
Collin Winter828f04a2007-08-31 00:04:24 +00003526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 PyErr_SetObject(type, value);
3528 /* PyErr_SetObject incref's its arguments */
3529 Py_XDECREF(value);
3530 Py_XDECREF(type);
3531 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003532
3533raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003534 Py_XDECREF(value);
3535 Py_XDECREF(type);
3536 Py_XDECREF(cause);
3537 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003538}
3539
Tim Petersd6d010b2001-06-21 02:49:55 +00003540/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003541 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003542
Guido van Rossum0368b722007-05-11 16:50:42 +00003543 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3544 with a variable target.
3545*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003546
Barry Warsawe42b18f1997-08-25 22:13:04 +00003547static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003548unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 int i = 0, j = 0;
3551 Py_ssize_t ll = 0;
3552 PyObject *it; /* iter(v) */
3553 PyObject *w;
3554 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003558 it = PyObject_GetIter(v);
3559 if (it == NULL)
3560 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003562 for (; i < argcnt; i++) {
3563 w = PyIter_Next(it);
3564 if (w == NULL) {
3565 /* Iterator done, via error or exhaustion. */
3566 if (!PyErr_Occurred()) {
3567 PyErr_Format(PyExc_ValueError,
3568 "need more than %d value%s to unpack",
3569 i, i == 1 ? "" : "s");
3570 }
3571 goto Error;
3572 }
3573 *--sp = w;
3574 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 if (argcntafter == -1) {
3577 /* We better have exhausted the iterator now. */
3578 w = PyIter_Next(it);
3579 if (w == NULL) {
3580 if (PyErr_Occurred())
3581 goto Error;
3582 Py_DECREF(it);
3583 return 1;
3584 }
3585 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003586 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3587 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003588 goto Error;
3589 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 l = PySequence_List(it);
3592 if (l == NULL)
3593 goto Error;
3594 *--sp = l;
3595 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 ll = PyList_GET_SIZE(l);
3598 if (ll < argcntafter) {
3599 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3600 argcnt + ll);
3601 goto Error;
3602 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 /* Pop the "after-variable" args off the list. */
3605 for (j = argcntafter; j > 0; j--, i++) {
3606 *--sp = PyList_GET_ITEM(l, ll - j);
3607 }
3608 /* Resize the list. */
3609 Py_SIZE(l) = ll - argcntafter;
3610 Py_DECREF(it);
3611 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003612
Tim Petersd6d010b2001-06-21 02:49:55 +00003613Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 for (; i > 0; i--, sp++)
3615 Py_DECREF(*sp);
3616 Py_XDECREF(it);
3617 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003618}
3619
3620
Guido van Rossum96a42c81992-01-12 02:29:51 +00003621#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003622static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003623prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003625 printf("%s ", str);
3626 if (PyObject_Print(v, stdout, 0) != 0)
3627 PyErr_Clear(); /* Don't know what else to do */
3628 printf("\n");
3629 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003630}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003631#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003632
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003633static void
Fred Drake5755ce62001-06-27 19:19:46 +00003634call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003635{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 PyObject *type, *value, *traceback, *arg;
3637 int err;
3638 PyErr_Fetch(&type, &value, &traceback);
3639 if (value == NULL) {
3640 value = Py_None;
3641 Py_INCREF(value);
3642 }
3643 arg = PyTuple_Pack(3, type, value, traceback);
3644 if (arg == NULL) {
3645 PyErr_Restore(type, value, traceback);
3646 return;
3647 }
3648 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3649 Py_DECREF(arg);
3650 if (err == 0)
3651 PyErr_Restore(type, value, traceback);
3652 else {
3653 Py_XDECREF(type);
3654 Py_XDECREF(value);
3655 Py_XDECREF(traceback);
3656 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003657}
3658
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003659static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003660call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003661 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003663 PyObject *type, *value, *traceback;
3664 int err;
3665 PyErr_Fetch(&type, &value, &traceback);
3666 err = call_trace(func, obj, frame, what, arg);
3667 if (err == 0)
3668 {
3669 PyErr_Restore(type, value, traceback);
3670 return 0;
3671 }
3672 else {
3673 Py_XDECREF(type);
3674 Py_XDECREF(value);
3675 Py_XDECREF(traceback);
3676 return -1;
3677 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003678}
3679
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003680static int
Fred Drake5755ce62001-06-27 19:19:46 +00003681call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003684 register PyThreadState *tstate = frame->f_tstate;
3685 int result;
3686 if (tstate->tracing)
3687 return 0;
3688 tstate->tracing++;
3689 tstate->use_tracing = 0;
3690 result = func(obj, frame, what, arg);
3691 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3692 || (tstate->c_profilefunc != NULL));
3693 tstate->tracing--;
3694 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003695}
3696
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003697PyObject *
3698_PyEval_CallTracing(PyObject *func, PyObject *args)
3699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 PyFrameObject *frame = PyEval_GetFrame();
3701 PyThreadState *tstate = frame->f_tstate;
3702 int save_tracing = tstate->tracing;
3703 int save_use_tracing = tstate->use_tracing;
3704 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 tstate->tracing = 0;
3707 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3708 || (tstate->c_profilefunc != NULL));
3709 result = PyObject_Call(func, args, NULL);
3710 tstate->tracing = save_tracing;
3711 tstate->use_tracing = save_use_tracing;
3712 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003713}
3714
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003715/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003716static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003717maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003718 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3719 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003720{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 int result = 0;
3722 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 /* If the last instruction executed isn't in the current
3725 instruction window, reset the window.
3726 */
3727 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3728 PyAddrPair bounds;
3729 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3730 &bounds);
3731 *instr_lb = bounds.ap_lower;
3732 *instr_ub = bounds.ap_upper;
3733 }
3734 /* If the last instruction falls at the start of a line or if
3735 it represents a jump backwards, update the frame's line
3736 number and call the trace function. */
3737 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3738 frame->f_lineno = line;
3739 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3740 }
3741 *instr_prev = frame->f_lasti;
3742 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003743}
3744
Fred Drake5755ce62001-06-27 19:19:46 +00003745void
3746PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003748 PyThreadState *tstate = PyThreadState_GET();
3749 PyObject *temp = tstate->c_profileobj;
3750 Py_XINCREF(arg);
3751 tstate->c_profilefunc = NULL;
3752 tstate->c_profileobj = NULL;
3753 /* Must make sure that tracing is not ignored if 'temp' is freed */
3754 tstate->use_tracing = tstate->c_tracefunc != NULL;
3755 Py_XDECREF(temp);
3756 tstate->c_profilefunc = func;
3757 tstate->c_profileobj = arg;
3758 /* Flag that tracing or profiling is turned on */
3759 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003760}
3761
3762void
3763PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003765 PyThreadState *tstate = PyThreadState_GET();
3766 PyObject *temp = tstate->c_traceobj;
3767 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3768 Py_XINCREF(arg);
3769 tstate->c_tracefunc = NULL;
3770 tstate->c_traceobj = NULL;
3771 /* Must make sure that profiling is not ignored if 'temp' is freed */
3772 tstate->use_tracing = tstate->c_profilefunc != NULL;
3773 Py_XDECREF(temp);
3774 tstate->c_tracefunc = func;
3775 tstate->c_traceobj = arg;
3776 /* Flag that tracing or profiling is turned on */
3777 tstate->use_tracing = ((func != NULL)
3778 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003779}
3780
Guido van Rossumb209a111997-04-29 18:18:01 +00003781PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003782PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 PyFrameObject *current_frame = PyEval_GetFrame();
3785 if (current_frame == NULL)
3786 return PyThreadState_GET()->interp->builtins;
3787 else
3788 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003789}
3790
Guido van Rossumb209a111997-04-29 18:18:01 +00003791PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003792PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003793{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 PyFrameObject *current_frame = PyEval_GetFrame();
3795 if (current_frame == NULL)
3796 return NULL;
3797 PyFrame_FastToLocals(current_frame);
3798 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003799}
3800
Guido van Rossumb209a111997-04-29 18:18:01 +00003801PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003802PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003804 PyFrameObject *current_frame = PyEval_GetFrame();
3805 if (current_frame == NULL)
3806 return NULL;
3807 else
3808 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003809}
3810
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003811PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003812PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 PyThreadState *tstate = PyThreadState_GET();
3815 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003816}
3817
Guido van Rossum6135a871995-01-09 17:53:26 +00003818int
Tim Peters5ba58662001-07-16 02:29:45 +00003819PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 PyFrameObject *current_frame = PyEval_GetFrame();
3822 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 if (current_frame != NULL) {
3825 const int codeflags = current_frame->f_code->co_flags;
3826 const int compilerflags = codeflags & PyCF_MASK;
3827 if (compilerflags) {
3828 result = 1;
3829 cf->cf_flags |= compilerflags;
3830 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003831#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 if (codeflags & CO_GENERATOR_ALLOWED) {
3833 result = 1;
3834 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3835 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003836#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 }
3838 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003839}
3840
Guido van Rossum3f5da241990-12-20 15:06:42 +00003841
Guido van Rossum681d79a1995-07-18 14:51:37 +00003842/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003843 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003844
Guido van Rossumb209a111997-04-29 18:18:01 +00003845PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003846PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003848 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003850 if (arg == NULL) {
3851 arg = PyTuple_New(0);
3852 if (arg == NULL)
3853 return NULL;
3854 }
3855 else if (!PyTuple_Check(arg)) {
3856 PyErr_SetString(PyExc_TypeError,
3857 "argument list must be a tuple");
3858 return NULL;
3859 }
3860 else
3861 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003863 if (kw != NULL && !PyDict_Check(kw)) {
3864 PyErr_SetString(PyExc_TypeError,
3865 "keyword list must be a dictionary");
3866 Py_DECREF(arg);
3867 return NULL;
3868 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 result = PyObject_Call(func, arg, kw);
3871 Py_DECREF(arg);
3872 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003873}
3874
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003875const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003876PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003877{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 if (PyMethod_Check(func))
3879 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3880 else if (PyFunction_Check(func))
3881 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3882 else if (PyCFunction_Check(func))
3883 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3884 else
3885 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003886}
3887
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003888const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003889PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003891 if (PyMethod_Check(func))
3892 return "()";
3893 else if (PyFunction_Check(func))
3894 return "()";
3895 else if (PyCFunction_Check(func))
3896 return "()";
3897 else
3898 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003899}
3900
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003901static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003902err_args(PyObject *func, int flags, int nargs)
3903{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003904 if (flags & METH_NOARGS)
3905 PyErr_Format(PyExc_TypeError,
3906 "%.200s() takes no arguments (%d given)",
3907 ((PyCFunctionObject *)func)->m_ml->ml_name,
3908 nargs);
3909 else
3910 PyErr_Format(PyExc_TypeError,
3911 "%.200s() takes exactly one argument (%d given)",
3912 ((PyCFunctionObject *)func)->m_ml->ml_name,
3913 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003914}
3915
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003916#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003917if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 if (call_trace(tstate->c_profilefunc, \
3919 tstate->c_profileobj, \
3920 tstate->frame, PyTrace_C_CALL, \
3921 func)) { \
3922 x = NULL; \
3923 } \
3924 else { \
3925 x = call; \
3926 if (tstate->c_profilefunc != NULL) { \
3927 if (x == NULL) { \
3928 call_trace_protected(tstate->c_profilefunc, \
3929 tstate->c_profileobj, \
3930 tstate->frame, PyTrace_C_EXCEPTION, \
3931 func); \
3932 /* XXX should pass (type, value, tb) */ \
3933 } else { \
3934 if (call_trace(tstate->c_profilefunc, \
3935 tstate->c_profileobj, \
3936 tstate->frame, PyTrace_C_RETURN, \
3937 func)) { \
3938 Py_DECREF(x); \
3939 x = NULL; \
3940 } \
3941 } \
3942 } \
3943 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003944} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 x = call; \
3946 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003947
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003948static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003949call_function(PyObject ***pp_stack, int oparg
3950#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003952#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003954{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 int na = oparg & 0xff;
3956 int nk = (oparg>>8) & 0xff;
3957 int n = na + 2 * nk;
3958 PyObject **pfunc = (*pp_stack) - n - 1;
3959 PyObject *func = *pfunc;
3960 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 /* Always dispatch PyCFunction first, because these are
3963 presumed to be the most frequent callable object.
3964 */
3965 if (PyCFunction_Check(func) && nk == 0) {
3966 int flags = PyCFunction_GET_FLAGS(func);
3967 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 PCALL(PCALL_CFUNCTION);
3970 if (flags & (METH_NOARGS | METH_O)) {
3971 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3972 PyObject *self = PyCFunction_GET_SELF(func);
3973 if (flags & METH_NOARGS && na == 0) {
3974 C_TRACE(x, (*meth)(self,NULL));
3975 }
3976 else if (flags & METH_O && na == 1) {
3977 PyObject *arg = EXT_POP(*pp_stack);
3978 C_TRACE(x, (*meth)(self,arg));
3979 Py_DECREF(arg);
3980 }
3981 else {
3982 err_args(func, flags, na);
3983 x = NULL;
3984 }
3985 }
3986 else {
3987 PyObject *callargs;
3988 callargs = load_args(pp_stack, na);
3989 READ_TIMESTAMP(*pintr0);
3990 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3991 READ_TIMESTAMP(*pintr1);
3992 Py_XDECREF(callargs);
3993 }
3994 } else {
3995 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3996 /* optimize access to bound methods */
3997 PyObject *self = PyMethod_GET_SELF(func);
3998 PCALL(PCALL_METHOD);
3999 PCALL(PCALL_BOUND_METHOD);
4000 Py_INCREF(self);
4001 func = PyMethod_GET_FUNCTION(func);
4002 Py_INCREF(func);
4003 Py_DECREF(*pfunc);
4004 *pfunc = self;
4005 na++;
4006 n++;
4007 } else
4008 Py_INCREF(func);
4009 READ_TIMESTAMP(*pintr0);
4010 if (PyFunction_Check(func))
4011 x = fast_function(func, pp_stack, n, na, nk);
4012 else
4013 x = do_call(func, pp_stack, na, nk);
4014 READ_TIMESTAMP(*pintr1);
4015 Py_DECREF(func);
4016 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 /* Clear the stack of the function object. Also removes
4019 the arguments in case they weren't consumed already
4020 (fast_function() and err_args() leave them on the stack).
4021 */
4022 while ((*pp_stack) > pfunc) {
4023 w = EXT_POP(*pp_stack);
4024 Py_DECREF(w);
4025 PCALL(PCALL_POP);
4026 }
4027 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004028}
4029
Jeremy Hylton192690e2002-08-16 18:36:11 +00004030/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004031 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004032 For the simplest case -- a function that takes only positional
4033 arguments and is called with only positional arguments -- it
4034 inlines the most primitive frame setup code from
4035 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4036 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004037*/
4038
4039static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004040fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4043 PyObject *globals = PyFunction_GET_GLOBALS(func);
4044 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4045 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4046 PyObject **d = NULL;
4047 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 PCALL(PCALL_FUNCTION);
4050 PCALL(PCALL_FAST_FUNCTION);
4051 if (argdefs == NULL && co->co_argcount == n &&
4052 co->co_kwonlyargcount == 0 && nk==0 &&
4053 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4054 PyFrameObject *f;
4055 PyObject *retval = NULL;
4056 PyThreadState *tstate = PyThreadState_GET();
4057 PyObject **fastlocals, **stack;
4058 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 PCALL(PCALL_FASTER_FUNCTION);
4061 assert(globals != NULL);
4062 /* XXX Perhaps we should create a specialized
4063 PyFrame_New() that doesn't take locals, but does
4064 take builtins without sanity checking them.
4065 */
4066 assert(tstate != NULL);
4067 f = PyFrame_New(tstate, co, globals, NULL);
4068 if (f == NULL)
4069 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 fastlocals = f->f_localsplus;
4072 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 for (i = 0; i < n; i++) {
4075 Py_INCREF(*stack);
4076 fastlocals[i] = *stack++;
4077 }
4078 retval = PyEval_EvalFrameEx(f,0);
4079 ++tstate->recursion_depth;
4080 Py_DECREF(f);
4081 --tstate->recursion_depth;
4082 return retval;
4083 }
4084 if (argdefs != NULL) {
4085 d = &PyTuple_GET_ITEM(argdefs, 0);
4086 nd = Py_SIZE(argdefs);
4087 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004088 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 (PyObject *)NULL, (*pp_stack)-n, na,
4090 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4091 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004092}
4093
4094static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004095update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4096 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004097{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 PyObject *kwdict = NULL;
4099 if (orig_kwdict == NULL)
4100 kwdict = PyDict_New();
4101 else {
4102 kwdict = PyDict_Copy(orig_kwdict);
4103 Py_DECREF(orig_kwdict);
4104 }
4105 if (kwdict == NULL)
4106 return NULL;
4107 while (--nk >= 0) {
4108 int err;
4109 PyObject *value = EXT_POP(*pp_stack);
4110 PyObject *key = EXT_POP(*pp_stack);
4111 if (PyDict_GetItem(kwdict, key) != NULL) {
4112 PyErr_Format(PyExc_TypeError,
4113 "%.200s%s got multiple values "
4114 "for keyword argument '%U'",
4115 PyEval_GetFuncName(func),
4116 PyEval_GetFuncDesc(func),
4117 key);
4118 Py_DECREF(key);
4119 Py_DECREF(value);
4120 Py_DECREF(kwdict);
4121 return NULL;
4122 }
4123 err = PyDict_SetItem(kwdict, key, value);
4124 Py_DECREF(key);
4125 Py_DECREF(value);
4126 if (err) {
4127 Py_DECREF(kwdict);
4128 return NULL;
4129 }
4130 }
4131 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004132}
4133
4134static PyObject *
4135update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004137{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 callargs = PyTuple_New(nstack + nstar);
4141 if (callargs == NULL) {
4142 return NULL;
4143 }
4144 if (nstar) {
4145 int i;
4146 for (i = 0; i < nstar; i++) {
4147 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4148 Py_INCREF(a);
4149 PyTuple_SET_ITEM(callargs, nstack + i, a);
4150 }
4151 }
4152 while (--nstack >= 0) {
4153 w = EXT_POP(*pp_stack);
4154 PyTuple_SET_ITEM(callargs, nstack, w);
4155 }
4156 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004157}
4158
4159static PyObject *
4160load_args(PyObject ***pp_stack, int na)
4161{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004162 PyObject *args = PyTuple_New(na);
4163 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 if (args == NULL)
4166 return NULL;
4167 while (--na >= 0) {
4168 w = EXT_POP(*pp_stack);
4169 PyTuple_SET_ITEM(args, na, w);
4170 }
4171 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004172}
4173
4174static PyObject *
4175do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4176{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 PyObject *callargs = NULL;
4178 PyObject *kwdict = NULL;
4179 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004181 if (nk > 0) {
4182 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4183 if (kwdict == NULL)
4184 goto call_fail;
4185 }
4186 callargs = load_args(pp_stack, na);
4187 if (callargs == NULL)
4188 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004189#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 /* At this point, we have to look at the type of func to
4191 update the call stats properly. Do it here so as to avoid
4192 exposing the call stats machinery outside ceval.c
4193 */
4194 if (PyFunction_Check(func))
4195 PCALL(PCALL_FUNCTION);
4196 else if (PyMethod_Check(func))
4197 PCALL(PCALL_METHOD);
4198 else if (PyType_Check(func))
4199 PCALL(PCALL_TYPE);
4200 else if (PyCFunction_Check(func))
4201 PCALL(PCALL_CFUNCTION);
4202 else
4203 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004204#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004205 if (PyCFunction_Check(func)) {
4206 PyThreadState *tstate = PyThreadState_GET();
4207 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4208 }
4209 else
4210 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004211call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 Py_XDECREF(callargs);
4213 Py_XDECREF(kwdict);
4214 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004215}
4216
4217static PyObject *
4218ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4219{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 int nstar = 0;
4221 PyObject *callargs = NULL;
4222 PyObject *stararg = NULL;
4223 PyObject *kwdict = NULL;
4224 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 if (flags & CALL_FLAG_KW) {
4227 kwdict = EXT_POP(*pp_stack);
4228 if (!PyDict_Check(kwdict)) {
4229 PyObject *d;
4230 d = PyDict_New();
4231 if (d == NULL)
4232 goto ext_call_fail;
4233 if (PyDict_Update(d, kwdict) != 0) {
4234 Py_DECREF(d);
4235 /* PyDict_Update raises attribute
4236 * error (percolated from an attempt
4237 * to get 'keys' attribute) instead of
4238 * a type error if its second argument
4239 * is not a mapping.
4240 */
4241 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4242 PyErr_Format(PyExc_TypeError,
4243 "%.200s%.200s argument after ** "
4244 "must be a mapping, not %.200s",
4245 PyEval_GetFuncName(func),
4246 PyEval_GetFuncDesc(func),
4247 kwdict->ob_type->tp_name);
4248 }
4249 goto ext_call_fail;
4250 }
4251 Py_DECREF(kwdict);
4252 kwdict = d;
4253 }
4254 }
4255 if (flags & CALL_FLAG_VAR) {
4256 stararg = EXT_POP(*pp_stack);
4257 if (!PyTuple_Check(stararg)) {
4258 PyObject *t = NULL;
4259 t = PySequence_Tuple(stararg);
4260 if (t == NULL) {
4261 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4262 PyErr_Format(PyExc_TypeError,
4263 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004264 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 PyEval_GetFuncName(func),
4266 PyEval_GetFuncDesc(func),
4267 stararg->ob_type->tp_name);
4268 }
4269 goto ext_call_fail;
4270 }
4271 Py_DECREF(stararg);
4272 stararg = t;
4273 }
4274 nstar = PyTuple_GET_SIZE(stararg);
4275 }
4276 if (nk > 0) {
4277 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4278 if (kwdict == NULL)
4279 goto ext_call_fail;
4280 }
4281 callargs = update_star_args(na, nstar, stararg, pp_stack);
4282 if (callargs == NULL)
4283 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004284#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 /* At this point, we have to look at the type of func to
4286 update the call stats properly. Do it here so as to avoid
4287 exposing the call stats machinery outside ceval.c
4288 */
4289 if (PyFunction_Check(func))
4290 PCALL(PCALL_FUNCTION);
4291 else if (PyMethod_Check(func))
4292 PCALL(PCALL_METHOD);
4293 else if (PyType_Check(func))
4294 PCALL(PCALL_TYPE);
4295 else if (PyCFunction_Check(func))
4296 PCALL(PCALL_CFUNCTION);
4297 else
4298 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004299#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 if (PyCFunction_Check(func)) {
4301 PyThreadState *tstate = PyThreadState_GET();
4302 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4303 }
4304 else
4305 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004306ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 Py_XDECREF(callargs);
4308 Py_XDECREF(kwdict);
4309 Py_XDECREF(stararg);
4310 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004311}
4312
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004313/* Extract a slice index from a PyInt or PyLong or an object with the
4314 nb_index slot defined, and store in *pi.
4315 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4316 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 +00004317 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004318*/
Tim Petersb5196382001-12-16 19:44:20 +00004319/* Note: If v is NULL, return success without storing into *pi. This
4320 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4321 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004322*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004323int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004324_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004326 if (v != NULL) {
4327 Py_ssize_t x;
4328 if (PyIndex_Check(v)) {
4329 x = PyNumber_AsSsize_t(v, NULL);
4330 if (x == -1 && PyErr_Occurred())
4331 return 0;
4332 }
4333 else {
4334 PyErr_SetString(PyExc_TypeError,
4335 "slice indices must be integers or "
4336 "None or have an __index__ method");
4337 return 0;
4338 }
4339 *pi = x;
4340 }
4341 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004342}
4343
Guido van Rossum486364b2007-06-30 05:01:58 +00004344#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004346
Guido van Rossumb209a111997-04-29 18:18:01 +00004347static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004348cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004349{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 int res = 0;
4351 switch (op) {
4352 case PyCmp_IS:
4353 res = (v == w);
4354 break;
4355 case PyCmp_IS_NOT:
4356 res = (v != w);
4357 break;
4358 case PyCmp_IN:
4359 res = PySequence_Contains(w, v);
4360 if (res < 0)
4361 return NULL;
4362 break;
4363 case PyCmp_NOT_IN:
4364 res = PySequence_Contains(w, v);
4365 if (res < 0)
4366 return NULL;
4367 res = !res;
4368 break;
4369 case PyCmp_EXC_MATCH:
4370 if (PyTuple_Check(w)) {
4371 Py_ssize_t i, length;
4372 length = PyTuple_Size(w);
4373 for (i = 0; i < length; i += 1) {
4374 PyObject *exc = PyTuple_GET_ITEM(w, i);
4375 if (!PyExceptionClass_Check(exc)) {
4376 PyErr_SetString(PyExc_TypeError,
4377 CANNOT_CATCH_MSG);
4378 return NULL;
4379 }
4380 }
4381 }
4382 else {
4383 if (!PyExceptionClass_Check(w)) {
4384 PyErr_SetString(PyExc_TypeError,
4385 CANNOT_CATCH_MSG);
4386 return NULL;
4387 }
4388 }
4389 res = PyErr_GivenExceptionMatches(v, w);
4390 break;
4391 default:
4392 return PyObject_RichCompare(v, w, op);
4393 }
4394 v = res ? Py_True : Py_False;
4395 Py_INCREF(v);
4396 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004397}
4398
Thomas Wouters52152252000-08-17 22:55:00 +00004399static PyObject *
4400import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004404 x = PyObject_GetAttr(v, name);
4405 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4406 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4407 }
4408 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004409}
Guido van Rossumac7be682001-01-17 15:42:30 +00004410
Thomas Wouters52152252000-08-17 22:55:00 +00004411static int
4412import_all_from(PyObject *locals, PyObject *v)
4413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 PyObject *all = PyObject_GetAttrString(v, "__all__");
4415 PyObject *dict, *name, *value;
4416 int skip_leading_underscores = 0;
4417 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004419 if (all == NULL) {
4420 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4421 return -1; /* Unexpected error */
4422 PyErr_Clear();
4423 dict = PyObject_GetAttrString(v, "__dict__");
4424 if (dict == NULL) {
4425 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4426 return -1;
4427 PyErr_SetString(PyExc_ImportError,
4428 "from-import-* object has no __dict__ and no __all__");
4429 return -1;
4430 }
4431 all = PyMapping_Keys(dict);
4432 Py_DECREF(dict);
4433 if (all == NULL)
4434 return -1;
4435 skip_leading_underscores = 1;
4436 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004438 for (pos = 0, err = 0; ; pos++) {
4439 name = PySequence_GetItem(all, pos);
4440 if (name == NULL) {
4441 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4442 err = -1;
4443 else
4444 PyErr_Clear();
4445 break;
4446 }
4447 if (skip_leading_underscores &&
4448 PyUnicode_Check(name) &&
4449 PyUnicode_AS_UNICODE(name)[0] == '_')
4450 {
4451 Py_DECREF(name);
4452 continue;
4453 }
4454 value = PyObject_GetAttr(v, name);
4455 if (value == NULL)
4456 err = -1;
4457 else if (PyDict_CheckExact(locals))
4458 err = PyDict_SetItem(locals, name, value);
4459 else
4460 err = PyObject_SetItem(locals, name, value);
4461 Py_DECREF(name);
4462 Py_XDECREF(value);
4463 if (err != 0)
4464 break;
4465 }
4466 Py_DECREF(all);
4467 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004468}
4469
Guido van Rossumac7be682001-01-17 15:42:30 +00004470static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004471format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004475 if (!obj)
4476 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 obj_str = _PyUnicode_AsString(obj);
4479 if (!obj_str)
4480 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004483}
Guido van Rossum950361c1997-01-24 13:49:28 +00004484
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004485static void
4486format_exc_unbound(PyCodeObject *co, int oparg)
4487{
4488 PyObject *name;
4489 /* Don't stomp existing exception */
4490 if (PyErr_Occurred())
4491 return;
4492 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4493 name = PyTuple_GET_ITEM(co->co_cellvars,
4494 oparg);
4495 format_exc_check_arg(
4496 PyExc_UnboundLocalError,
4497 UNBOUNDLOCAL_ERROR_MSG,
4498 name);
4499 } else {
4500 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4501 PyTuple_GET_SIZE(co->co_cellvars));
4502 format_exc_check_arg(PyExc_NameError,
4503 UNBOUNDFREE_ERROR_MSG, name);
4504 }
4505}
4506
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004507static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004508unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004511 /* This function implements 'variable += expr' when both arguments
4512 are (Unicode) strings. */
4513 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4514 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4515 Py_ssize_t new_len = v_len + w_len;
4516 if (new_len < 0) {
4517 PyErr_SetString(PyExc_OverflowError,
4518 "strings are too large to concat");
4519 return NULL;
4520 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004521
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004522 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004523 /* In the common case, there are 2 references to the value
4524 * stored in 'variable' when the += is performed: one on the
4525 * value stack (in 'v') and one still stored in the
4526 * 'variable'. We try to delete the variable now to reduce
4527 * the refcnt to 1.
4528 */
4529 switch (*next_instr) {
4530 case STORE_FAST:
4531 {
4532 int oparg = PEEKARG();
4533 PyObject **fastlocals = f->f_localsplus;
4534 if (GETLOCAL(oparg) == v)
4535 SETLOCAL(oparg, NULL);
4536 break;
4537 }
4538 case STORE_DEREF:
4539 {
4540 PyObject **freevars = (f->f_localsplus +
4541 f->f_code->co_nlocals);
4542 PyObject *c = freevars[PEEKARG()];
4543 if (PyCell_GET(c) == v)
4544 PyCell_Set(c, NULL);
4545 break;
4546 }
4547 case STORE_NAME:
4548 {
4549 PyObject *names = f->f_code->co_names;
4550 PyObject *name = GETITEM(names, PEEKARG());
4551 PyObject *locals = f->f_locals;
4552 if (PyDict_CheckExact(locals) &&
4553 PyDict_GetItem(locals, name) == v) {
4554 if (PyDict_DelItem(locals, name) != 0) {
4555 PyErr_Clear();
4556 }
4557 }
4558 break;
4559 }
4560 }
4561 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004562
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004563 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004564 /* Now we own the last reference to 'v', so we can resize it
4565 * in-place.
4566 */
4567 if (PyUnicode_Resize(&v, new_len) != 0) {
4568 /* XXX if PyUnicode_Resize() fails, 'v' has been
4569 * deallocated so it cannot be put back into
4570 * 'variable'. The MemoryError is raised when there
4571 * is no value in 'variable', which might (very
4572 * remotely) be a cause of incompatibilities.
4573 */
4574 return NULL;
4575 }
4576 /* copy 'w' into the newly allocated area of 'v' */
4577 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4578 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4579 return v;
4580 }
4581 else {
4582 /* When in-place resizing is not an option. */
4583 w = PyUnicode_Concat(v, w);
4584 Py_DECREF(v);
4585 return w;
4586 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004587}
4588
Guido van Rossum950361c1997-01-24 13:49:28 +00004589#ifdef DYNAMIC_EXECUTION_PROFILE
4590
Skip Montanarof118cb12001-10-15 20:51:38 +00004591static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004592getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004594 int i;
4595 PyObject *l = PyList_New(256);
4596 if (l == NULL) return NULL;
4597 for (i = 0; i < 256; i++) {
4598 PyObject *x = PyLong_FromLong(a[i]);
4599 if (x == NULL) {
4600 Py_DECREF(l);
4601 return NULL;
4602 }
4603 PyList_SetItem(l, i, x);
4604 }
4605 for (i = 0; i < 256; i++)
4606 a[i] = 0;
4607 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004608}
4609
4610PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004611_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004612{
4613#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004614 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004615#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004616 int i;
4617 PyObject *l = PyList_New(257);
4618 if (l == NULL) return NULL;
4619 for (i = 0; i < 257; i++) {
4620 PyObject *x = getarray(dxpairs[i]);
4621 if (x == NULL) {
4622 Py_DECREF(l);
4623 return NULL;
4624 }
4625 PyList_SetItem(l, i, x);
4626 }
4627 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004628#endif
4629}
4630
4631#endif