blob: ac0707046a6a80853ad79da48ffdd05fb742ada4 [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
Guido van Rossuma027efa1997-05-05 20:56:21 +00001144/* Start of code */
1145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (f == NULL)
1147 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001148
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 /* push frame */
1150 if (Py_EnterRecursiveCall(""))
1151 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001154
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (tstate->use_tracing) {
1156 if (tstate->c_tracefunc != NULL) {
1157 /* tstate->c_tracefunc, if defined, is a
1158 function that will be called on *every* entry
1159 to a code block. Its return value, if not
1160 None, is a function that will be called at
1161 the start of each executed line of code.
1162 (Actually, the function must return itself
1163 in order to continue tracing.) The trace
1164 functions are called with three arguments:
1165 a pointer to the current frame, a string
1166 indicating why the function is called, and
1167 an argument which depends on the situation.
1168 The global trace function is also called
1169 whenever an exception is detected. */
1170 if (call_trace_protected(tstate->c_tracefunc,
1171 tstate->c_traceobj,
1172 f, PyTrace_CALL, Py_None)) {
1173 /* Trace function raised an error */
1174 goto exit_eval_frame;
1175 }
1176 }
1177 if (tstate->c_profilefunc != NULL) {
1178 /* Similar for c_profilefunc, except it needn't
1179 return itself and isn't called for "line" events */
1180 if (call_trace_protected(tstate->c_profilefunc,
1181 tstate->c_profileobj,
1182 f, PyTrace_CALL, Py_None)) {
1183 /* Profile function raised an error */
1184 goto exit_eval_frame;
1185 }
1186 }
1187 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 co = f->f_code;
1190 names = co->co_names;
1191 consts = co->co_consts;
1192 fastlocals = f->f_localsplus;
1193 freevars = f->f_localsplus + co->co_nlocals;
1194 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1195 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001196
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 f->f_lasti now refers to the index of the last instruction
1198 executed. You might think this was obvious from the name, but
1199 this wasn't always true before 2.3! PyFrame_New now sets
1200 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1201 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1202 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 When the PREDICT() macros are enabled, some opcode pairs follow in
1205 direct succession without updating f->f_lasti. A successful
1206 prediction effectively links the two codes together as if they
1207 were a single new opcode; accordingly,f->f_lasti will point to
1208 the first code in the pair (for instance, GET_ITER followed by
1209 FOR_ITER is effectively a single opcode and f->f_lasti will point
1210 at to the beginning of the combined pair.)
1211 */
1212 next_instr = first_instr + f->f_lasti + 1;
1213 stack_pointer = f->f_stacktop;
1214 assert(stack_pointer != NULL);
1215 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 if (co->co_flags & CO_GENERATOR && !throwflag) {
1218 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1219 /* We were in an except handler when we left,
1220 restore the exception state which was put aside
1221 (see YIELD_VALUE). */
1222 SWAP_EXC_STATE();
1223 }
1224 else {
1225 SAVE_EXC_STATE();
1226 }
1227 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001228
Tim Peters5ca576e2001-06-18 22:08:13 +00001229#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001231#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 why = WHY_NOT;
1234 err = 0;
1235 x = Py_None; /* Not a reference, just anything non-NULL */
1236 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 if (throwflag) { /* support for generator.throw() */
1239 why = WHY_EXCEPTION;
1240 goto on_error;
1241 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001244#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (inst1 == 0) {
1246 /* Almost surely, the opcode executed a break
1247 or a continue, preventing inst1 from being set
1248 on the way out of the loop.
1249 */
1250 READ_TIMESTAMP(inst1);
1251 loop1 = inst1;
1252 }
1253 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1254 intr0, intr1);
1255 ticked = 0;
1256 inst1 = 0;
1257 intr0 = 0;
1258 intr1 = 0;
1259 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001260#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1262 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 /* Do periodic things. Doing this every time through
1265 the loop would add too much overhead, so we do it
1266 only every Nth instruction. We also do it if
1267 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1268 event needs attention (e.g. a signal handler or
1269 async I/O handler); see Py_AddPendingCall() and
1270 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1273 if (*next_instr == SETUP_FINALLY) {
1274 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001275 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001276 goto fast_next_opcode;
1277 }
1278 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001279#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001281#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1283 if (Py_MakePendingCalls() < 0) {
1284 why = WHY_EXCEPTION;
1285 goto on_error;
1286 }
1287 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001288#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001289 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 /* Give another thread a chance */
1291 if (PyThreadState_Swap(NULL) != tstate)
1292 Py_FatalError("ceval: tstate mix-up");
1293 drop_gil(tstate);
1294
1295 /* Other threads may run now */
1296
1297 take_gil(tstate);
1298 if (PyThreadState_Swap(tstate) != NULL)
1299 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001301#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 /* Check for asynchronous exceptions. */
1303 if (tstate->async_exc != NULL) {
1304 x = tstate->async_exc;
1305 tstate->async_exc = NULL;
1306 UNSIGNAL_ASYNC_EXC();
1307 PyErr_SetNone(x);
1308 Py_DECREF(x);
1309 why = WHY_EXCEPTION;
1310 goto on_error;
1311 }
1312 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 fast_next_opcode:
1315 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 if (_Py_TracingPossible &&
1320 tstate->c_tracefunc != NULL && !tstate->tracing) {
1321 /* see maybe_call_line_trace
1322 for expository comments */
1323 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001324
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 err = maybe_call_line_trace(tstate->c_tracefunc,
1326 tstate->c_traceobj,
1327 f, &instr_lb, &instr_ub,
1328 &instr_prev);
1329 /* Reload possibly changed frame fields */
1330 JUMPTO(f->f_lasti);
1331 if (f->f_stacktop != NULL) {
1332 stack_pointer = f->f_stacktop;
1333 f->f_stacktop = NULL;
1334 }
1335 if (err) {
1336 /* trace function raised an exception */
1337 goto on_error;
1338 }
1339 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 opcode = NEXTOP();
1344 oparg = 0; /* allows oparg to be stored in a register because
1345 it doesn't have to be remembered across a full loop */
1346 if (HAS_ARG(opcode))
1347 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001348 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001349#ifdef DYNAMIC_EXECUTION_PROFILE
1350#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 dxpairs[lastopcode][opcode]++;
1352 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001353#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001355#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001356
Guido van Rossum96a42c81992-01-12 02:29:51 +00001357#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 if (lltrace) {
1361 if (HAS_ARG(opcode)) {
1362 printf("%d: %d, %d\n",
1363 f->f_lasti, opcode, oparg);
1364 }
1365 else {
1366 printf("%d: %d\n",
1367 f->f_lasti, opcode);
1368 }
1369 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001370#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 /* Main switch on opcode */
1373 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 /* BEWARE!
1378 It is essential that any operation that fails sets either
1379 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1380 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 TARGET(NOP)
1385 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001386
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 TARGET(LOAD_FAST)
1388 x = GETLOCAL(oparg);
1389 if (x != NULL) {
1390 Py_INCREF(x);
1391 PUSH(x);
1392 FAST_DISPATCH();
1393 }
1394 format_exc_check_arg(PyExc_UnboundLocalError,
1395 UNBOUNDLOCAL_ERROR_MSG,
1396 PyTuple_GetItem(co->co_varnames, oparg));
1397 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 TARGET(LOAD_CONST)
1400 x = GETITEM(consts, oparg);
1401 Py_INCREF(x);
1402 PUSH(x);
1403 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 PREDICTED_WITH_ARG(STORE_FAST);
1406 TARGET(STORE_FAST)
1407 v = POP();
1408 SETLOCAL(oparg, v);
1409 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 TARGET(POP_TOP)
1412 v = POP();
1413 Py_DECREF(v);
1414 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001416 TARGET(ROT_TWO)
1417 v = TOP();
1418 w = SECOND();
1419 SET_TOP(w);
1420 SET_SECOND(v);
1421 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 TARGET(ROT_THREE)
1424 v = TOP();
1425 w = SECOND();
1426 x = THIRD();
1427 SET_TOP(w);
1428 SET_SECOND(x);
1429 SET_THIRD(v);
1430 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 TARGET(DUP_TOP)
1433 v = TOP();
1434 Py_INCREF(v);
1435 PUSH(v);
1436 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001438 TARGET(DUP_TOP_TWO)
1439 x = TOP();
1440 Py_INCREF(x);
1441 w = SECOND();
1442 Py_INCREF(w);
1443 STACKADJ(2);
1444 SET_TOP(x);
1445 SET_SECOND(w);
1446 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 TARGET(UNARY_POSITIVE)
1449 v = TOP();
1450 x = PyNumber_Positive(v);
1451 Py_DECREF(v);
1452 SET_TOP(x);
1453 if (x != NULL) DISPATCH();
1454 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 TARGET(UNARY_NEGATIVE)
1457 v = TOP();
1458 x = PyNumber_Negative(v);
1459 Py_DECREF(v);
1460 SET_TOP(x);
1461 if (x != NULL) DISPATCH();
1462 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 TARGET(UNARY_NOT)
1465 v = TOP();
1466 err = PyObject_IsTrue(v);
1467 Py_DECREF(v);
1468 if (err == 0) {
1469 Py_INCREF(Py_True);
1470 SET_TOP(Py_True);
1471 DISPATCH();
1472 }
1473 else if (err > 0) {
1474 Py_INCREF(Py_False);
1475 SET_TOP(Py_False);
1476 err = 0;
1477 DISPATCH();
1478 }
1479 STACKADJ(-1);
1480 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 TARGET(UNARY_INVERT)
1483 v = TOP();
1484 x = PyNumber_Invert(v);
1485 Py_DECREF(v);
1486 SET_TOP(x);
1487 if (x != NULL) DISPATCH();
1488 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 TARGET(BINARY_POWER)
1491 w = POP();
1492 v = TOP();
1493 x = PyNumber_Power(v, w, Py_None);
1494 Py_DECREF(v);
1495 Py_DECREF(w);
1496 SET_TOP(x);
1497 if (x != NULL) DISPATCH();
1498 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 TARGET(BINARY_MULTIPLY)
1501 w = POP();
1502 v = TOP();
1503 x = PyNumber_Multiply(v, w);
1504 Py_DECREF(v);
1505 Py_DECREF(w);
1506 SET_TOP(x);
1507 if (x != NULL) DISPATCH();
1508 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 TARGET(BINARY_TRUE_DIVIDE)
1511 w = POP();
1512 v = TOP();
1513 x = PyNumber_TrueDivide(v, w);
1514 Py_DECREF(v);
1515 Py_DECREF(w);
1516 SET_TOP(x);
1517 if (x != NULL) DISPATCH();
1518 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520 TARGET(BINARY_FLOOR_DIVIDE)
1521 w = POP();
1522 v = TOP();
1523 x = PyNumber_FloorDivide(v, w);
1524 Py_DECREF(v);
1525 Py_DECREF(w);
1526 SET_TOP(x);
1527 if (x != NULL) DISPATCH();
1528 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 TARGET(BINARY_MODULO)
1531 w = POP();
1532 v = TOP();
1533 if (PyUnicode_CheckExact(v))
1534 x = PyUnicode_Format(v, w);
1535 else
1536 x = PyNumber_Remainder(v, w);
1537 Py_DECREF(v);
1538 Py_DECREF(w);
1539 SET_TOP(x);
1540 if (x != NULL) DISPATCH();
1541 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 TARGET(BINARY_ADD)
1544 w = POP();
1545 v = TOP();
1546 if (PyUnicode_CheckExact(v) &&
1547 PyUnicode_CheckExact(w)) {
1548 x = unicode_concatenate(v, w, f, next_instr);
1549 /* unicode_concatenate consumed the ref to v */
1550 goto skip_decref_vx;
1551 }
1552 else {
1553 x = PyNumber_Add(v, w);
1554 }
1555 Py_DECREF(v);
1556 skip_decref_vx:
1557 Py_DECREF(w);
1558 SET_TOP(x);
1559 if (x != NULL) DISPATCH();
1560 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 TARGET(BINARY_SUBTRACT)
1563 w = POP();
1564 v = TOP();
1565 x = PyNumber_Subtract(v, w);
1566 Py_DECREF(v);
1567 Py_DECREF(w);
1568 SET_TOP(x);
1569 if (x != NULL) DISPATCH();
1570 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 TARGET(BINARY_SUBSCR)
1573 w = POP();
1574 v = TOP();
1575 x = PyObject_GetItem(v, w);
1576 Py_DECREF(v);
1577 Py_DECREF(w);
1578 SET_TOP(x);
1579 if (x != NULL) DISPATCH();
1580 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 TARGET(BINARY_LSHIFT)
1583 w = POP();
1584 v = TOP();
1585 x = PyNumber_Lshift(v, w);
1586 Py_DECREF(v);
1587 Py_DECREF(w);
1588 SET_TOP(x);
1589 if (x != NULL) DISPATCH();
1590 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 TARGET(BINARY_RSHIFT)
1593 w = POP();
1594 v = TOP();
1595 x = PyNumber_Rshift(v, w);
1596 Py_DECREF(v);
1597 Py_DECREF(w);
1598 SET_TOP(x);
1599 if (x != NULL) DISPATCH();
1600 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 TARGET(BINARY_AND)
1603 w = POP();
1604 v = TOP();
1605 x = PyNumber_And(v, w);
1606 Py_DECREF(v);
1607 Py_DECREF(w);
1608 SET_TOP(x);
1609 if (x != NULL) DISPATCH();
1610 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 TARGET(BINARY_XOR)
1613 w = POP();
1614 v = TOP();
1615 x = PyNumber_Xor(v, w);
1616 Py_DECREF(v);
1617 Py_DECREF(w);
1618 SET_TOP(x);
1619 if (x != NULL) DISPATCH();
1620 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001622 TARGET(BINARY_OR)
1623 w = POP();
1624 v = TOP();
1625 x = PyNumber_Or(v, w);
1626 Py_DECREF(v);
1627 Py_DECREF(w);
1628 SET_TOP(x);
1629 if (x != NULL) DISPATCH();
1630 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 TARGET(LIST_APPEND)
1633 w = POP();
1634 v = PEEK(oparg);
1635 err = PyList_Append(v, w);
1636 Py_DECREF(w);
1637 if (err == 0) {
1638 PREDICT(JUMP_ABSOLUTE);
1639 DISPATCH();
1640 }
1641 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 TARGET(SET_ADD)
1644 w = POP();
1645 v = stack_pointer[-oparg];
1646 err = PySet_Add(v, w);
1647 Py_DECREF(w);
1648 if (err == 0) {
1649 PREDICT(JUMP_ABSOLUTE);
1650 DISPATCH();
1651 }
1652 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 TARGET(INPLACE_POWER)
1655 w = POP();
1656 v = TOP();
1657 x = PyNumber_InPlacePower(v, w, Py_None);
1658 Py_DECREF(v);
1659 Py_DECREF(w);
1660 SET_TOP(x);
1661 if (x != NULL) DISPATCH();
1662 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 TARGET(INPLACE_MULTIPLY)
1665 w = POP();
1666 v = TOP();
1667 x = PyNumber_InPlaceMultiply(v, w);
1668 Py_DECREF(v);
1669 Py_DECREF(w);
1670 SET_TOP(x);
1671 if (x != NULL) DISPATCH();
1672 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 TARGET(INPLACE_TRUE_DIVIDE)
1675 w = POP();
1676 v = TOP();
1677 x = PyNumber_InPlaceTrueDivide(v, w);
1678 Py_DECREF(v);
1679 Py_DECREF(w);
1680 SET_TOP(x);
1681 if (x != NULL) DISPATCH();
1682 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 TARGET(INPLACE_FLOOR_DIVIDE)
1685 w = POP();
1686 v = TOP();
1687 x = PyNumber_InPlaceFloorDivide(v, w);
1688 Py_DECREF(v);
1689 Py_DECREF(w);
1690 SET_TOP(x);
1691 if (x != NULL) DISPATCH();
1692 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001693
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001694 TARGET(INPLACE_MODULO)
1695 w = POP();
1696 v = TOP();
1697 x = PyNumber_InPlaceRemainder(v, w);
1698 Py_DECREF(v);
1699 Py_DECREF(w);
1700 SET_TOP(x);
1701 if (x != NULL) DISPATCH();
1702 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 TARGET(INPLACE_ADD)
1705 w = POP();
1706 v = TOP();
1707 if (PyUnicode_CheckExact(v) &&
1708 PyUnicode_CheckExact(w)) {
1709 x = unicode_concatenate(v, w, f, next_instr);
1710 /* unicode_concatenate consumed the ref to v */
1711 goto skip_decref_v;
1712 }
1713 else {
1714 x = PyNumber_InPlaceAdd(v, w);
1715 }
1716 Py_DECREF(v);
1717 skip_decref_v:
1718 Py_DECREF(w);
1719 SET_TOP(x);
1720 if (x != NULL) DISPATCH();
1721 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 TARGET(INPLACE_SUBTRACT)
1724 w = POP();
1725 v = TOP();
1726 x = PyNumber_InPlaceSubtract(v, w);
1727 Py_DECREF(v);
1728 Py_DECREF(w);
1729 SET_TOP(x);
1730 if (x != NULL) DISPATCH();
1731 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 TARGET(INPLACE_LSHIFT)
1734 w = POP();
1735 v = TOP();
1736 x = PyNumber_InPlaceLshift(v, w);
1737 Py_DECREF(v);
1738 Py_DECREF(w);
1739 SET_TOP(x);
1740 if (x != NULL) DISPATCH();
1741 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 TARGET(INPLACE_RSHIFT)
1744 w = POP();
1745 v = TOP();
1746 x = PyNumber_InPlaceRshift(v, w);
1747 Py_DECREF(v);
1748 Py_DECREF(w);
1749 SET_TOP(x);
1750 if (x != NULL) DISPATCH();
1751 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 TARGET(INPLACE_AND)
1754 w = POP();
1755 v = TOP();
1756 x = PyNumber_InPlaceAnd(v, w);
1757 Py_DECREF(v);
1758 Py_DECREF(w);
1759 SET_TOP(x);
1760 if (x != NULL) DISPATCH();
1761 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 TARGET(INPLACE_XOR)
1764 w = POP();
1765 v = TOP();
1766 x = PyNumber_InPlaceXor(v, w);
1767 Py_DECREF(v);
1768 Py_DECREF(w);
1769 SET_TOP(x);
1770 if (x != NULL) DISPATCH();
1771 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773 TARGET(INPLACE_OR)
1774 w = POP();
1775 v = TOP();
1776 x = PyNumber_InPlaceOr(v, w);
1777 Py_DECREF(v);
1778 Py_DECREF(w);
1779 SET_TOP(x);
1780 if (x != NULL) DISPATCH();
1781 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 TARGET(STORE_SUBSCR)
1784 w = TOP();
1785 v = SECOND();
1786 u = THIRD();
1787 STACKADJ(-3);
1788 /* v[w] = u */
1789 err = PyObject_SetItem(v, w, u);
1790 Py_DECREF(u);
1791 Py_DECREF(v);
1792 Py_DECREF(w);
1793 if (err == 0) DISPATCH();
1794 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 TARGET(DELETE_SUBSCR)
1797 w = TOP();
1798 v = SECOND();
1799 STACKADJ(-2);
1800 /* del v[w] */
1801 err = PyObject_DelItem(v, w);
1802 Py_DECREF(v);
1803 Py_DECREF(w);
1804 if (err == 0) DISPATCH();
1805 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 TARGET(PRINT_EXPR)
1808 v = POP();
1809 w = PySys_GetObject("displayhook");
1810 if (w == NULL) {
1811 PyErr_SetString(PyExc_RuntimeError,
1812 "lost sys.displayhook");
1813 err = -1;
1814 x = NULL;
1815 }
1816 if (err == 0) {
1817 x = PyTuple_Pack(1, v);
1818 if (x == NULL)
1819 err = -1;
1820 }
1821 if (err == 0) {
1822 w = PyEval_CallObject(w, x);
1823 Py_XDECREF(w);
1824 if (w == NULL)
1825 err = -1;
1826 }
1827 Py_DECREF(v);
1828 Py_XDECREF(x);
1829 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001830
Thomas Wouters434d0822000-08-24 20:11:32 +00001831#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001833#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 TARGET(RAISE_VARARGS)
1835 v = w = NULL;
1836 switch (oparg) {
1837 case 2:
1838 v = POP(); /* cause */
1839 case 1:
1840 w = POP(); /* exc */
1841 case 0: /* Fallthrough */
1842 why = do_raise(w, v);
1843 break;
1844 default:
1845 PyErr_SetString(PyExc_SystemError,
1846 "bad RAISE_VARARGS oparg");
1847 why = WHY_EXCEPTION;
1848 break;
1849 }
1850 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 TARGET(STORE_LOCALS)
1853 x = POP();
1854 v = f->f_locals;
1855 Py_XDECREF(v);
1856 f->f_locals = x;
1857 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 TARGET(RETURN_VALUE)
1860 retval = POP();
1861 why = WHY_RETURN;
1862 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 TARGET(YIELD_VALUE)
1865 retval = POP();
1866 f->f_stacktop = stack_pointer;
1867 why = WHY_YIELD;
1868 /* Put aside the current exception state and restore
1869 that of the calling frame. This only serves when
1870 "yield" is used inside an except handler. */
1871 SWAP_EXC_STATE();
1872 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 TARGET(POP_EXCEPT)
1875 {
1876 PyTryBlock *b = PyFrame_BlockPop(f);
1877 if (b->b_type != EXCEPT_HANDLER) {
1878 PyErr_SetString(PyExc_SystemError,
1879 "popped block is not an except handler");
1880 why = WHY_EXCEPTION;
1881 break;
1882 }
1883 UNWIND_EXCEPT_HANDLER(b);
1884 }
1885 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 TARGET(POP_BLOCK)
1888 {
1889 PyTryBlock *b = PyFrame_BlockPop(f);
1890 UNWIND_BLOCK(b);
1891 }
1892 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 PREDICTED(END_FINALLY);
1895 TARGET(END_FINALLY)
1896 v = POP();
1897 if (PyLong_Check(v)) {
1898 why = (enum why_code) PyLong_AS_LONG(v);
1899 assert(why != WHY_YIELD);
1900 if (why == WHY_RETURN ||
1901 why == WHY_CONTINUE)
1902 retval = POP();
1903 if (why == WHY_SILENCED) {
1904 /* An exception was silenced by 'with', we must
1905 manually unwind the EXCEPT_HANDLER block which was
1906 created when the exception was caught, otherwise
1907 the stack will be in an inconsistent state. */
1908 PyTryBlock *b = PyFrame_BlockPop(f);
1909 assert(b->b_type == EXCEPT_HANDLER);
1910 UNWIND_EXCEPT_HANDLER(b);
1911 why = WHY_NOT;
1912 }
1913 }
1914 else if (PyExceptionClass_Check(v)) {
1915 w = POP();
1916 u = POP();
1917 PyErr_Restore(v, w, u);
1918 why = WHY_RERAISE;
1919 break;
1920 }
1921 else if (v != Py_None) {
1922 PyErr_SetString(PyExc_SystemError,
1923 "'finally' pops bad exception");
1924 why = WHY_EXCEPTION;
1925 }
1926 Py_DECREF(v);
1927 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001928
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 TARGET(LOAD_BUILD_CLASS)
1930 x = PyDict_GetItemString(f->f_builtins,
1931 "__build_class__");
1932 if (x == NULL) {
1933 PyErr_SetString(PyExc_ImportError,
1934 "__build_class__ not found");
1935 break;
1936 }
1937 Py_INCREF(x);
1938 PUSH(x);
1939 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 TARGET(STORE_NAME)
1942 w = GETITEM(names, oparg);
1943 v = POP();
1944 if ((x = f->f_locals) != NULL) {
1945 if (PyDict_CheckExact(x))
1946 err = PyDict_SetItem(x, w, v);
1947 else
1948 err = PyObject_SetItem(x, w, v);
1949 Py_DECREF(v);
1950 if (err == 0) DISPATCH();
1951 break;
1952 }
1953 PyErr_Format(PyExc_SystemError,
1954 "no locals found when storing %R", w);
1955 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 TARGET(DELETE_NAME)
1958 w = GETITEM(names, oparg);
1959 if ((x = f->f_locals) != NULL) {
1960 if ((err = PyObject_DelItem(x, w)) != 0)
1961 format_exc_check_arg(PyExc_NameError,
1962 NAME_ERROR_MSG,
1963 w);
1964 break;
1965 }
1966 PyErr_Format(PyExc_SystemError,
1967 "no locals when deleting %R", w);
1968 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1971 TARGET(UNPACK_SEQUENCE)
1972 v = POP();
1973 if (PyTuple_CheckExact(v) &&
1974 PyTuple_GET_SIZE(v) == oparg) {
1975 PyObject **items = \
1976 ((PyTupleObject *)v)->ob_item;
1977 while (oparg--) {
1978 w = items[oparg];
1979 Py_INCREF(w);
1980 PUSH(w);
1981 }
1982 Py_DECREF(v);
1983 DISPATCH();
1984 } else if (PyList_CheckExact(v) &&
1985 PyList_GET_SIZE(v) == oparg) {
1986 PyObject **items = \
1987 ((PyListObject *)v)->ob_item;
1988 while (oparg--) {
1989 w = items[oparg];
1990 Py_INCREF(w);
1991 PUSH(w);
1992 }
1993 } else if (unpack_iterable(v, oparg, -1,
1994 stack_pointer + oparg)) {
1995 STACKADJ(oparg);
1996 } else {
1997 /* unpack_iterable() raised an exception */
1998 why = WHY_EXCEPTION;
1999 }
2000 Py_DECREF(v);
2001 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003 TARGET(UNPACK_EX)
2004 {
2005 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2006 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2009 stack_pointer + totalargs)) {
2010 stack_pointer += totalargs;
2011 } else {
2012 why = WHY_EXCEPTION;
2013 }
2014 Py_DECREF(v);
2015 break;
2016 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 TARGET(STORE_ATTR)
2019 w = GETITEM(names, oparg);
2020 v = TOP();
2021 u = SECOND();
2022 STACKADJ(-2);
2023 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2024 Py_DECREF(v);
2025 Py_DECREF(u);
2026 if (err == 0) DISPATCH();
2027 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 TARGET(DELETE_ATTR)
2030 w = GETITEM(names, oparg);
2031 v = POP();
2032 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2033 /* del v.w */
2034 Py_DECREF(v);
2035 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 TARGET(STORE_GLOBAL)
2038 w = GETITEM(names, oparg);
2039 v = POP();
2040 err = PyDict_SetItem(f->f_globals, w, v);
2041 Py_DECREF(v);
2042 if (err == 0) DISPATCH();
2043 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 TARGET(DELETE_GLOBAL)
2046 w = GETITEM(names, oparg);
2047 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2048 format_exc_check_arg(
2049 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2050 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002052 TARGET(LOAD_NAME)
2053 w = GETITEM(names, oparg);
2054 if ((v = f->f_locals) == NULL) {
2055 PyErr_Format(PyExc_SystemError,
2056 "no locals when loading %R", w);
2057 why = WHY_EXCEPTION;
2058 break;
2059 }
2060 if (PyDict_CheckExact(v)) {
2061 x = PyDict_GetItem(v, w);
2062 Py_XINCREF(x);
2063 }
2064 else {
2065 x = PyObject_GetItem(v, w);
2066 if (x == NULL && PyErr_Occurred()) {
2067 if (!PyErr_ExceptionMatches(
2068 PyExc_KeyError))
2069 break;
2070 PyErr_Clear();
2071 }
2072 }
2073 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002074 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002076 x = PyDict_GetItem(f->f_builtins, w);
2077 if (x == NULL) {
2078 format_exc_check_arg(
2079 PyExc_NameError,
2080 NAME_ERROR_MSG, w);
2081 break;
2082 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 }
2084 Py_INCREF(x);
2085 }
2086 PUSH(x);
2087 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 TARGET(LOAD_GLOBAL)
2090 w = GETITEM(names, oparg);
2091 if (PyUnicode_CheckExact(w)) {
2092 /* Inline the PyDict_GetItem() calls.
2093 WARNING: this is an extreme speed hack.
2094 Do not try this at home. */
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002095 Py_hash_t hash = ((PyUnicodeObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 if (hash != -1) {
2097 PyDictObject *d;
2098 PyDictEntry *e;
2099 d = (PyDictObject *)(f->f_globals);
2100 e = d->ma_lookup(d, w, hash);
2101 if (e == NULL) {
2102 x = NULL;
2103 break;
2104 }
2105 x = e->me_value;
2106 if (x != NULL) {
2107 Py_INCREF(x);
2108 PUSH(x);
2109 DISPATCH();
2110 }
2111 d = (PyDictObject *)(f->f_builtins);
2112 e = d->ma_lookup(d, w, hash);
2113 if (e == NULL) {
2114 x = NULL;
2115 break;
2116 }
2117 x = e->me_value;
2118 if (x != NULL) {
2119 Py_INCREF(x);
2120 PUSH(x);
2121 DISPATCH();
2122 }
2123 goto load_global_error;
2124 }
2125 }
2126 /* This is the un-inlined version of the code above */
2127 x = PyDict_GetItem(f->f_globals, w);
2128 if (x == NULL) {
2129 x = PyDict_GetItem(f->f_builtins, w);
2130 if (x == NULL) {
2131 load_global_error:
2132 format_exc_check_arg(
2133 PyExc_NameError,
2134 GLOBAL_NAME_ERROR_MSG, w);
2135 break;
2136 }
2137 }
2138 Py_INCREF(x);
2139 PUSH(x);
2140 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 TARGET(DELETE_FAST)
2143 x = GETLOCAL(oparg);
2144 if (x != NULL) {
2145 SETLOCAL(oparg, NULL);
2146 DISPATCH();
2147 }
2148 format_exc_check_arg(
2149 PyExc_UnboundLocalError,
2150 UNBOUNDLOCAL_ERROR_MSG,
2151 PyTuple_GetItem(co->co_varnames, oparg)
2152 );
2153 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002154
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002155 TARGET(DELETE_DEREF)
2156 x = freevars[oparg];
2157 if (PyCell_GET(x) != NULL) {
2158 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002159 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002160 }
2161 err = -1;
2162 format_exc_unbound(co, oparg);
2163 break;
2164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002165 TARGET(LOAD_CLOSURE)
2166 x = freevars[oparg];
2167 Py_INCREF(x);
2168 PUSH(x);
2169 if (x != NULL) DISPATCH();
2170 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002172 TARGET(LOAD_DEREF)
2173 x = freevars[oparg];
2174 w = PyCell_Get(x);
2175 if (w != NULL) {
2176 PUSH(w);
2177 DISPATCH();
2178 }
2179 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002180 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 TARGET(STORE_DEREF)
2184 w = POP();
2185 x = freevars[oparg];
2186 PyCell_Set(x, w);
2187 Py_DECREF(w);
2188 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 TARGET(BUILD_TUPLE)
2191 x = PyTuple_New(oparg);
2192 if (x != NULL) {
2193 for (; --oparg >= 0;) {
2194 w = POP();
2195 PyTuple_SET_ITEM(x, oparg, w);
2196 }
2197 PUSH(x);
2198 DISPATCH();
2199 }
2200 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 TARGET(BUILD_LIST)
2203 x = PyList_New(oparg);
2204 if (x != NULL) {
2205 for (; --oparg >= 0;) {
2206 w = POP();
2207 PyList_SET_ITEM(x, oparg, w);
2208 }
2209 PUSH(x);
2210 DISPATCH();
2211 }
2212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 TARGET(BUILD_SET)
2215 x = PySet_New(NULL);
2216 if (x != NULL) {
2217 for (; --oparg >= 0;) {
2218 w = POP();
2219 if (err == 0)
2220 err = PySet_Add(x, w);
2221 Py_DECREF(w);
2222 }
2223 if (err != 0) {
2224 Py_DECREF(x);
2225 break;
2226 }
2227 PUSH(x);
2228 DISPATCH();
2229 }
2230 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002231
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002232 TARGET(BUILD_MAP)
2233 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2234 PUSH(x);
2235 if (x != NULL) DISPATCH();
2236 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002238 TARGET(STORE_MAP)
2239 w = TOP(); /* key */
2240 u = SECOND(); /* value */
2241 v = THIRD(); /* dict */
2242 STACKADJ(-2);
2243 assert (PyDict_CheckExact(v));
2244 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2245 Py_DECREF(u);
2246 Py_DECREF(w);
2247 if (err == 0) DISPATCH();
2248 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 TARGET(MAP_ADD)
2251 w = TOP(); /* key */
2252 u = SECOND(); /* value */
2253 STACKADJ(-2);
2254 v = stack_pointer[-oparg]; /* dict */
2255 assert (PyDict_CheckExact(v));
2256 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2257 Py_DECREF(u);
2258 Py_DECREF(w);
2259 if (err == 0) {
2260 PREDICT(JUMP_ABSOLUTE);
2261 DISPATCH();
2262 }
2263 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002265 TARGET(LOAD_ATTR)
2266 w = GETITEM(names, oparg);
2267 v = TOP();
2268 x = PyObject_GetAttr(v, w);
2269 Py_DECREF(v);
2270 SET_TOP(x);
2271 if (x != NULL) DISPATCH();
2272 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 TARGET(COMPARE_OP)
2275 w = POP();
2276 v = TOP();
2277 x = cmp_outcome(oparg, v, w);
2278 Py_DECREF(v);
2279 Py_DECREF(w);
2280 SET_TOP(x);
2281 if (x == NULL) break;
2282 PREDICT(POP_JUMP_IF_FALSE);
2283 PREDICT(POP_JUMP_IF_TRUE);
2284 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 TARGET(IMPORT_NAME)
2287 w = GETITEM(names, oparg);
2288 x = PyDict_GetItemString(f->f_builtins, "__import__");
2289 if (x == NULL) {
2290 PyErr_SetString(PyExc_ImportError,
2291 "__import__ not found");
2292 break;
2293 }
2294 Py_INCREF(x);
2295 v = POP();
2296 u = TOP();
2297 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2298 w = PyTuple_Pack(5,
2299 w,
2300 f->f_globals,
2301 f->f_locals == NULL ?
2302 Py_None : f->f_locals,
2303 v,
2304 u);
2305 else
2306 w = PyTuple_Pack(4,
2307 w,
2308 f->f_globals,
2309 f->f_locals == NULL ?
2310 Py_None : f->f_locals,
2311 v);
2312 Py_DECREF(v);
2313 Py_DECREF(u);
2314 if (w == NULL) {
2315 u = POP();
2316 Py_DECREF(x);
2317 x = NULL;
2318 break;
2319 }
2320 READ_TIMESTAMP(intr0);
2321 v = x;
2322 x = PyEval_CallObject(v, w);
2323 Py_DECREF(v);
2324 READ_TIMESTAMP(intr1);
2325 Py_DECREF(w);
2326 SET_TOP(x);
2327 if (x != NULL) DISPATCH();
2328 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 TARGET(IMPORT_STAR)
2331 v = POP();
2332 PyFrame_FastToLocals(f);
2333 if ((x = f->f_locals) == NULL) {
2334 PyErr_SetString(PyExc_SystemError,
2335 "no locals found during 'import *'");
2336 break;
2337 }
2338 READ_TIMESTAMP(intr0);
2339 err = import_all_from(x, v);
2340 READ_TIMESTAMP(intr1);
2341 PyFrame_LocalsToFast(f, 0);
2342 Py_DECREF(v);
2343 if (err == 0) DISPATCH();
2344 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 TARGET(IMPORT_FROM)
2347 w = GETITEM(names, oparg);
2348 v = TOP();
2349 READ_TIMESTAMP(intr0);
2350 x = import_from(v, w);
2351 READ_TIMESTAMP(intr1);
2352 PUSH(x);
2353 if (x != NULL) DISPATCH();
2354 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 TARGET(JUMP_FORWARD)
2357 JUMPBY(oparg);
2358 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002359
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002360 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2361 TARGET(POP_JUMP_IF_FALSE)
2362 w = POP();
2363 if (w == Py_True) {
2364 Py_DECREF(w);
2365 FAST_DISPATCH();
2366 }
2367 if (w == Py_False) {
2368 Py_DECREF(w);
2369 JUMPTO(oparg);
2370 FAST_DISPATCH();
2371 }
2372 err = PyObject_IsTrue(w);
2373 Py_DECREF(w);
2374 if (err > 0)
2375 err = 0;
2376 else if (err == 0)
2377 JUMPTO(oparg);
2378 else
2379 break;
2380 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002382 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2383 TARGET(POP_JUMP_IF_TRUE)
2384 w = POP();
2385 if (w == Py_False) {
2386 Py_DECREF(w);
2387 FAST_DISPATCH();
2388 }
2389 if (w == Py_True) {
2390 Py_DECREF(w);
2391 JUMPTO(oparg);
2392 FAST_DISPATCH();
2393 }
2394 err = PyObject_IsTrue(w);
2395 Py_DECREF(w);
2396 if (err > 0) {
2397 err = 0;
2398 JUMPTO(oparg);
2399 }
2400 else if (err == 0)
2401 ;
2402 else
2403 break;
2404 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002405
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002406 TARGET(JUMP_IF_FALSE_OR_POP)
2407 w = TOP();
2408 if (w == Py_True) {
2409 STACKADJ(-1);
2410 Py_DECREF(w);
2411 FAST_DISPATCH();
2412 }
2413 if (w == Py_False) {
2414 JUMPTO(oparg);
2415 FAST_DISPATCH();
2416 }
2417 err = PyObject_IsTrue(w);
2418 if (err > 0) {
2419 STACKADJ(-1);
2420 Py_DECREF(w);
2421 err = 0;
2422 }
2423 else if (err == 0)
2424 JUMPTO(oparg);
2425 else
2426 break;
2427 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 TARGET(JUMP_IF_TRUE_OR_POP)
2430 w = TOP();
2431 if (w == Py_False) {
2432 STACKADJ(-1);
2433 Py_DECREF(w);
2434 FAST_DISPATCH();
2435 }
2436 if (w == Py_True) {
2437 JUMPTO(oparg);
2438 FAST_DISPATCH();
2439 }
2440 err = PyObject_IsTrue(w);
2441 if (err > 0) {
2442 err = 0;
2443 JUMPTO(oparg);
2444 }
2445 else if (err == 0) {
2446 STACKADJ(-1);
2447 Py_DECREF(w);
2448 }
2449 else
2450 break;
2451 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002453 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2454 TARGET(JUMP_ABSOLUTE)
2455 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002456#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002457 /* Enabling this path speeds-up all while and for-loops by bypassing
2458 the per-loop checks for signals. By default, this should be turned-off
2459 because it prevents detection of a control-break in tight loops like
2460 "while 1: pass". Compile with this option turned-on when you need
2461 the speed-up and do not need break checking inside tight loops (ones
2462 that contain only instructions ending with FAST_DISPATCH).
2463 */
2464 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002465#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002467#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 TARGET(GET_ITER)
2470 /* before: [obj]; after [getiter(obj)] */
2471 v = TOP();
2472 x = PyObject_GetIter(v);
2473 Py_DECREF(v);
2474 if (x != NULL) {
2475 SET_TOP(x);
2476 PREDICT(FOR_ITER);
2477 DISPATCH();
2478 }
2479 STACKADJ(-1);
2480 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 PREDICTED_WITH_ARG(FOR_ITER);
2483 TARGET(FOR_ITER)
2484 /* before: [iter]; after: [iter, iter()] *or* [] */
2485 v = TOP();
2486 x = (*v->ob_type->tp_iternext)(v);
2487 if (x != NULL) {
2488 PUSH(x);
2489 PREDICT(STORE_FAST);
2490 PREDICT(UNPACK_SEQUENCE);
2491 DISPATCH();
2492 }
2493 if (PyErr_Occurred()) {
2494 if (!PyErr_ExceptionMatches(
2495 PyExc_StopIteration))
2496 break;
2497 PyErr_Clear();
2498 }
2499 /* iterator ended normally */
2500 x = v = POP();
2501 Py_DECREF(v);
2502 JUMPBY(oparg);
2503 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002505 TARGET(BREAK_LOOP)
2506 why = WHY_BREAK;
2507 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002509 TARGET(CONTINUE_LOOP)
2510 retval = PyLong_FromLong(oparg);
2511 if (!retval) {
2512 x = NULL;
2513 break;
2514 }
2515 why = WHY_CONTINUE;
2516 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2519 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2520 TARGET(SETUP_FINALLY)
2521 _setup_finally:
2522 /* NOTE: If you add any new block-setup opcodes that
2523 are not try/except/finally handlers, you may need
2524 to update the PyGen_NeedsFinalizing() function.
2525 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2528 STACK_LEVEL());
2529 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 TARGET(SETUP_WITH)
2532 {
2533 static PyObject *exit, *enter;
2534 w = TOP();
2535 x = special_lookup(w, "__exit__", &exit);
2536 if (!x)
2537 break;
2538 SET_TOP(x);
2539 u = special_lookup(w, "__enter__", &enter);
2540 Py_DECREF(w);
2541 if (!u) {
2542 x = NULL;
2543 break;
2544 }
2545 x = PyObject_CallFunctionObjArgs(u, NULL);
2546 Py_DECREF(u);
2547 if (!x)
2548 break;
2549 /* Setup the finally block before pushing the result
2550 of __enter__ on the stack. */
2551 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2552 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002554 PUSH(x);
2555 DISPATCH();
2556 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002557
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 TARGET(WITH_CLEANUP)
2559 {
2560 /* At the top of the stack are 1-3 values indicating
2561 how/why we entered the finally clause:
2562 - TOP = None
2563 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2564 - TOP = WHY_*; no retval below it
2565 - (TOP, SECOND, THIRD) = exc_info()
2566 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2567 Below them is EXIT, the context.__exit__ bound method.
2568 In the last case, we must call
2569 EXIT(TOP, SECOND, THIRD)
2570 otherwise we must call
2571 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002573 In the first two cases, we remove EXIT from the
2574 stack, leaving the rest in the same order. In the
2575 third case, we shift the bottom 3 values of the
2576 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 In addition, if the stack represents an exception,
2579 *and* the function call returns a 'true' value, we
2580 push WHY_SILENCED onto the stack. END_FINALLY will
2581 then not re-raise the exception. (But non-local
2582 gotos should still be resumed.)
2583 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 PyObject *exit_func;
2586 u = TOP();
2587 if (u == Py_None) {
2588 (void)POP();
2589 exit_func = TOP();
2590 SET_TOP(u);
2591 v = w = Py_None;
2592 }
2593 else if (PyLong_Check(u)) {
2594 (void)POP();
2595 switch(PyLong_AsLong(u)) {
2596 case WHY_RETURN:
2597 case WHY_CONTINUE:
2598 /* Retval in TOP. */
2599 exit_func = SECOND();
2600 SET_SECOND(TOP());
2601 SET_TOP(u);
2602 break;
2603 default:
2604 exit_func = TOP();
2605 SET_TOP(u);
2606 break;
2607 }
2608 u = v = w = Py_None;
2609 }
2610 else {
2611 PyObject *tp, *exc, *tb;
2612 PyTryBlock *block;
2613 v = SECOND();
2614 w = THIRD();
2615 tp = FOURTH();
2616 exc = PEEK(5);
2617 tb = PEEK(6);
2618 exit_func = PEEK(7);
2619 SET_VALUE(7, tb);
2620 SET_VALUE(6, exc);
2621 SET_VALUE(5, tp);
2622 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2623 SET_FOURTH(NULL);
2624 /* We just shifted the stack down, so we have
2625 to tell the except handler block that the
2626 values are lower than it expects. */
2627 block = &f->f_blockstack[f->f_iblock - 1];
2628 assert(block->b_type == EXCEPT_HANDLER);
2629 block->b_level--;
2630 }
2631 /* XXX Not the fastest way to call it... */
2632 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2633 NULL);
2634 Py_DECREF(exit_func);
2635 if (x == NULL)
2636 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002638 if (u != Py_None)
2639 err = PyObject_IsTrue(x);
2640 else
2641 err = 0;
2642 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 if (err < 0)
2645 break; /* Go to error exit */
2646 else if (err > 0) {
2647 err = 0;
2648 /* There was an exception and a True return */
2649 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2650 }
2651 PREDICT(END_FINALLY);
2652 break;
2653 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 TARGET(CALL_FUNCTION)
2656 {
2657 PyObject **sp;
2658 PCALL(PCALL_ALL);
2659 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002660#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002661 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002662#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002664#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 stack_pointer = sp;
2666 PUSH(x);
2667 if (x != NULL)
2668 DISPATCH();
2669 break;
2670 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002671
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2673 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2674 TARGET(CALL_FUNCTION_VAR_KW)
2675 _call_function_var_kw:
2676 {
2677 int na = oparg & 0xff;
2678 int nk = (oparg>>8) & 0xff;
2679 int flags = (opcode - CALL_FUNCTION) & 3;
2680 int n = na + 2 * nk;
2681 PyObject **pfunc, *func, **sp;
2682 PCALL(PCALL_ALL);
2683 if (flags & CALL_FLAG_VAR)
2684 n++;
2685 if (flags & CALL_FLAG_KW)
2686 n++;
2687 pfunc = stack_pointer - n - 1;
2688 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002690 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002691 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 PyObject *self = PyMethod_GET_SELF(func);
2693 Py_INCREF(self);
2694 func = PyMethod_GET_FUNCTION(func);
2695 Py_INCREF(func);
2696 Py_DECREF(*pfunc);
2697 *pfunc = self;
2698 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002699 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 } else
2701 Py_INCREF(func);
2702 sp = stack_pointer;
2703 READ_TIMESTAMP(intr0);
2704 x = ext_do_call(func, &sp, flags, na, nk);
2705 READ_TIMESTAMP(intr1);
2706 stack_pointer = sp;
2707 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 while (stack_pointer > pfunc) {
2710 w = POP();
2711 Py_DECREF(w);
2712 }
2713 PUSH(x);
2714 if (x != NULL)
2715 DISPATCH();
2716 break;
2717 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2720 TARGET(MAKE_FUNCTION)
2721 _make_function:
2722 {
2723 int posdefaults = oparg & 0xff;
2724 int kwdefaults = (oparg>>8) & 0xff;
2725 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 v = POP(); /* code object */
2728 x = PyFunction_New(v, f->f_globals);
2729 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 if (x != NULL && opcode == MAKE_CLOSURE) {
2732 v = POP();
2733 if (PyFunction_SetClosure(x, v) != 0) {
2734 /* Can't happen unless bytecode is corrupt. */
2735 why = WHY_EXCEPTION;
2736 }
2737 Py_DECREF(v);
2738 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 if (x != NULL && num_annotations > 0) {
2741 Py_ssize_t name_ix;
2742 u = POP(); /* names of args with annotations */
2743 v = PyDict_New();
2744 if (v == NULL) {
2745 Py_DECREF(x);
2746 x = NULL;
2747 break;
2748 }
2749 name_ix = PyTuple_Size(u);
2750 assert(num_annotations == name_ix+1);
2751 while (name_ix > 0) {
2752 --name_ix;
2753 t = PyTuple_GET_ITEM(u, name_ix);
2754 w = POP();
2755 /* XXX(nnorwitz): check for errors */
2756 PyDict_SetItem(v, t, w);
2757 Py_DECREF(w);
2758 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 if (PyFunction_SetAnnotations(x, v) != 0) {
2761 /* Can't happen unless
2762 PyFunction_SetAnnotations changes. */
2763 why = WHY_EXCEPTION;
2764 }
2765 Py_DECREF(v);
2766 Py_DECREF(u);
2767 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 /* XXX Maybe this should be a separate opcode? */
2770 if (x != NULL && posdefaults > 0) {
2771 v = PyTuple_New(posdefaults);
2772 if (v == NULL) {
2773 Py_DECREF(x);
2774 x = NULL;
2775 break;
2776 }
2777 while (--posdefaults >= 0) {
2778 w = POP();
2779 PyTuple_SET_ITEM(v, posdefaults, w);
2780 }
2781 if (PyFunction_SetDefaults(x, v) != 0) {
2782 /* Can't happen unless
2783 PyFunction_SetDefaults changes. */
2784 why = WHY_EXCEPTION;
2785 }
2786 Py_DECREF(v);
2787 }
2788 if (x != NULL && kwdefaults > 0) {
2789 v = PyDict_New();
2790 if (v == NULL) {
2791 Py_DECREF(x);
2792 x = NULL;
2793 break;
2794 }
2795 while (--kwdefaults >= 0) {
2796 w = POP(); /* default value */
2797 u = POP(); /* kw only arg name */
2798 /* XXX(nnorwitz): check for errors */
2799 PyDict_SetItem(v, u, w);
2800 Py_DECREF(w);
2801 Py_DECREF(u);
2802 }
2803 if (PyFunction_SetKwDefaults(x, v) != 0) {
2804 /* Can't happen unless
2805 PyFunction_SetKwDefaults changes. */
2806 why = WHY_EXCEPTION;
2807 }
2808 Py_DECREF(v);
2809 }
2810 PUSH(x);
2811 break;
2812 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 TARGET(BUILD_SLICE)
2815 if (oparg == 3)
2816 w = POP();
2817 else
2818 w = NULL;
2819 v = POP();
2820 u = TOP();
2821 x = PySlice_New(u, v, w);
2822 Py_DECREF(u);
2823 Py_DECREF(v);
2824 Py_XDECREF(w);
2825 SET_TOP(x);
2826 if (x != NULL) DISPATCH();
2827 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 TARGET(EXTENDED_ARG)
2830 opcode = NEXTOP();
2831 oparg = oparg<<16 | NEXTARG();
2832 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002833
Antoine Pitrou042b1282010-08-13 21:15:58 +00002834#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002836#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 default:
2838 fprintf(stderr,
2839 "XXX lineno: %d, opcode: %d\n",
2840 PyFrame_GetLineNumber(f),
2841 opcode);
2842 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2843 why = WHY_EXCEPTION;
2844 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002845
2846#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002848#endif
2849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002854 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 if (why == WHY_NOT) {
2859 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002860#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 /* This check is expensive! */
2862 if (PyErr_Occurred())
2863 fprintf(stderr,
2864 "XXX undetected error\n");
2865 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002866#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 READ_TIMESTAMP(loop1);
2868 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002869#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002871#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 }
2873 why = WHY_EXCEPTION;
2874 x = Py_None;
2875 err = 0;
2876 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002878 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2881 if (!PyErr_Occurred()) {
2882 PyErr_SetString(PyExc_SystemError,
2883 "error return without exception set");
2884 why = WHY_EXCEPTION;
2885 }
2886 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002887#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 else {
2889 /* This check is expensive! */
2890 if (PyErr_Occurred()) {
2891 char buf[128];
2892 sprintf(buf, "Stack unwind with exception "
2893 "set and why=%d", why);
2894 Py_FatalError(buf);
2895 }
2896 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002897#endif
2898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002899 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 if (why == WHY_EXCEPTION) {
2902 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 if (tstate->c_tracefunc != NULL)
2905 call_exc_trace(tstate->c_tracefunc,
2906 tstate->c_traceobj, f);
2907 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 if (why == WHY_RERAISE)
2912 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002913
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002915
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002916fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 while (why != WHY_NOT && f->f_iblock > 0) {
2918 /* Peek at the current block. */
2919 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 assert(why != WHY_YIELD);
2922 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2923 why = WHY_NOT;
2924 JUMPTO(PyLong_AS_LONG(retval));
2925 Py_DECREF(retval);
2926 break;
2927 }
2928 /* Now we have to pop the block. */
2929 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 if (b->b_type == EXCEPT_HANDLER) {
2932 UNWIND_EXCEPT_HANDLER(b);
2933 continue;
2934 }
2935 UNWIND_BLOCK(b);
2936 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2937 why = WHY_NOT;
2938 JUMPTO(b->b_handler);
2939 break;
2940 }
2941 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2942 || b->b_type == SETUP_FINALLY)) {
2943 PyObject *exc, *val, *tb;
2944 int handler = b->b_handler;
2945 /* Beware, this invalidates all b->b_* fields */
2946 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2947 PUSH(tstate->exc_traceback);
2948 PUSH(tstate->exc_value);
2949 if (tstate->exc_type != NULL) {
2950 PUSH(tstate->exc_type);
2951 }
2952 else {
2953 Py_INCREF(Py_None);
2954 PUSH(Py_None);
2955 }
2956 PyErr_Fetch(&exc, &val, &tb);
2957 /* Make the raw exception data
2958 available to the handler,
2959 so a program can emulate the
2960 Python main loop. */
2961 PyErr_NormalizeException(
2962 &exc, &val, &tb);
2963 PyException_SetTraceback(val, tb);
2964 Py_INCREF(exc);
2965 tstate->exc_type = exc;
2966 Py_INCREF(val);
2967 tstate->exc_value = val;
2968 tstate->exc_traceback = tb;
2969 if (tb == NULL)
2970 tb = Py_None;
2971 Py_INCREF(tb);
2972 PUSH(tb);
2973 PUSH(val);
2974 PUSH(exc);
2975 why = WHY_NOT;
2976 JUMPTO(handler);
2977 break;
2978 }
2979 if (b->b_type == SETUP_FINALLY) {
2980 if (why & (WHY_RETURN | WHY_CONTINUE))
2981 PUSH(retval);
2982 PUSH(PyLong_FromLong((long)why));
2983 why = WHY_NOT;
2984 JUMPTO(b->b_handler);
2985 break;
2986 }
2987 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 if (why != WHY_NOT)
2992 break;
2993 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002995 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002996
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002997 assert(why != WHY_YIELD);
2998 /* Pop remaining stack entries. */
2999 while (!EMPTY()) {
3000 v = POP();
3001 Py_XDECREF(v);
3002 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 if (why != WHY_RETURN)
3005 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003007fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 if (tstate->use_tracing) {
3009 if (tstate->c_tracefunc) {
3010 if (why == WHY_RETURN || why == WHY_YIELD) {
3011 if (call_trace(tstate->c_tracefunc,
3012 tstate->c_traceobj, f,
3013 PyTrace_RETURN, retval)) {
3014 Py_XDECREF(retval);
3015 retval = NULL;
3016 why = WHY_EXCEPTION;
3017 }
3018 }
3019 else if (why == WHY_EXCEPTION) {
3020 call_trace_protected(tstate->c_tracefunc,
3021 tstate->c_traceobj, f,
3022 PyTrace_RETURN, NULL);
3023 }
3024 }
3025 if (tstate->c_profilefunc) {
3026 if (why == WHY_EXCEPTION)
3027 call_trace_protected(tstate->c_profilefunc,
3028 tstate->c_profileobj, f,
3029 PyTrace_RETURN, NULL);
3030 else if (call_trace(tstate->c_profilefunc,
3031 tstate->c_profileobj, f,
3032 PyTrace_RETURN, retval)) {
3033 Py_XDECREF(retval);
3034 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003035 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003036 }
3037 }
3038 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003041exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003042 Py_LeaveRecursiveCall();
3043 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003045 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003046}
3047
Benjamin Petersonb204a422011-06-05 22:04:07 -05003048static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003049format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3050{
3051 int err;
3052 Py_ssize_t len = PyList_GET_SIZE(names);
3053 PyObject *name_str, *comma, *tail, *tmp;
3054
3055 assert(PyList_CheckExact(names));
3056 assert(len >= 1);
3057 /* Deal with the joys of natural language. */
3058 switch (len) {
3059 case 1:
3060 name_str = PyList_GET_ITEM(names, 0);
3061 Py_INCREF(name_str);
3062 break;
3063 case 2:
3064 name_str = PyUnicode_FromFormat("%U and %U",
3065 PyList_GET_ITEM(names, len - 2),
3066 PyList_GET_ITEM(names, len - 1));
3067 break;
3068 default:
3069 tail = PyUnicode_FromFormat(", %U, and %U",
3070 PyList_GET_ITEM(names, len - 2),
3071 PyList_GET_ITEM(names, len - 1));
3072 /* Chop off the last two objects in the list. This shouldn't actually
3073 fail, but we can't be too careful. */
3074 err = PyList_SetSlice(names, len - 2, len, NULL);
3075 if (err == -1) {
3076 Py_DECREF(tail);
3077 return;
3078 }
3079 /* Stitch everything up into a nice comma-separated list. */
3080 comma = PyUnicode_FromString(", ");
3081 if (comma == NULL) {
3082 Py_DECREF(tail);
3083 return;
3084 }
3085 tmp = PyUnicode_Join(comma, names);
3086 Py_DECREF(comma);
3087 if (tmp == NULL) {
3088 Py_DECREF(tail);
3089 return;
3090 }
3091 name_str = PyUnicode_Concat(tmp, tail);
3092 Py_DECREF(tmp);
3093 Py_DECREF(tail);
3094 break;
3095 }
3096 if (name_str == NULL)
3097 return;
3098 PyErr_Format(PyExc_TypeError,
3099 "%U() missing %i required %s argument%s: %U",
3100 co->co_name,
3101 len,
3102 kind,
3103 len == 1 ? "" : "s",
3104 name_str);
3105 Py_DECREF(name_str);
3106}
3107
3108static void
3109missing_arguments(PyCodeObject *co, int missing, int defcount,
3110 PyObject **fastlocals)
3111{
3112 int i, j = 0;
3113 int start, end;
3114 int positional = defcount != -1;
3115 const char *kind = positional ? "positional" : "keyword-only";
3116 PyObject *missing_names;
3117
3118 /* Compute the names of the arguments that are missing. */
3119 missing_names = PyList_New(missing);
3120 if (missing_names == NULL)
3121 return;
3122 if (positional) {
3123 start = 0;
3124 end = co->co_argcount - defcount;
3125 }
3126 else {
3127 start = co->co_argcount;
3128 end = start + co->co_kwonlyargcount;
3129 }
3130 for (i = start; i < end; i++) {
3131 if (GETLOCAL(i) == NULL) {
3132 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3133 PyObject *name = PyObject_Repr(raw);
3134 if (name == NULL) {
3135 Py_DECREF(missing_names);
3136 return;
3137 }
3138 PyList_SET_ITEM(missing_names, j++, name);
3139 }
3140 }
3141 assert(j == missing);
3142 format_missing(kind, co, missing_names);
3143 Py_DECREF(missing_names);
3144}
3145
3146static void
3147too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003148{
3149 int plural;
3150 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003151 int i;
3152 PyObject *sig, *kwonly_sig;
3153
Benjamin Petersone109c702011-06-24 09:37:26 -05003154 assert((co->co_flags & CO_VARARGS) == 0);
3155 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003156 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003157 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003158 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003159 if (defcount) {
3160 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003161 plural = 1;
3162 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3163 }
3164 else {
3165 plural = co->co_argcount != 1;
3166 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3167 }
3168 if (sig == NULL)
3169 return;
3170 if (kwonly_given) {
3171 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3172 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3173 kwonly_given != 1 ? "s" : "");
3174 if (kwonly_sig == NULL) {
3175 Py_DECREF(sig);
3176 return;
3177 }
3178 }
3179 else {
3180 /* This will not fail. */
3181 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003182 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003183 }
3184 PyErr_Format(PyExc_TypeError,
3185 "%U() takes %U positional argument%s but %d%U %s given",
3186 co->co_name,
3187 sig,
3188 plural ? "s" : "",
3189 given,
3190 kwonly_sig,
3191 given == 1 && !kwonly_given ? "was" : "were");
3192 Py_DECREF(sig);
3193 Py_DECREF(kwonly_sig);
3194}
3195
Guido van Rossumc2e20742006-02-27 22:32:47 +00003196/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003197 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003198 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003199
Tim Peters6d6c1a32001-08-02 04:15:00 +00003200PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003201PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003202 PyObject **args, int argcount, PyObject **kws, int kwcount,
3203 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003204{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003205 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003206 register PyFrameObject *f;
3207 register PyObject *retval = NULL;
3208 register PyObject **fastlocals, **freevars;
3209 PyThreadState *tstate = PyThreadState_GET();
3210 PyObject *x, *u;
3211 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003212 int i;
3213 int n = argcount;
3214 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003215
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003216 if (globals == NULL) {
3217 PyErr_SetString(PyExc_SystemError,
3218 "PyEval_EvalCodeEx: NULL globals");
3219 return NULL;
3220 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003222 assert(tstate != NULL);
3223 assert(globals != NULL);
3224 f = PyFrame_New(tstate, co, globals, locals);
3225 if (f == NULL)
3226 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003228 fastlocals = f->f_localsplus;
3229 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003230
Benjamin Petersonb204a422011-06-05 22:04:07 -05003231 /* Parse arguments. */
3232 if (co->co_flags & CO_VARKEYWORDS) {
3233 kwdict = PyDict_New();
3234 if (kwdict == NULL)
3235 goto fail;
3236 i = total_args;
3237 if (co->co_flags & CO_VARARGS)
3238 i++;
3239 SETLOCAL(i, kwdict);
3240 }
3241 if (argcount > co->co_argcount)
3242 n = co->co_argcount;
3243 for (i = 0; i < n; i++) {
3244 x = args[i];
3245 Py_INCREF(x);
3246 SETLOCAL(i, x);
3247 }
3248 if (co->co_flags & CO_VARARGS) {
3249 u = PyTuple_New(argcount - n);
3250 if (u == NULL)
3251 goto fail;
3252 SETLOCAL(total_args, u);
3253 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 x = args[i];
3255 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003256 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003258 }
3259 for (i = 0; i < kwcount; i++) {
3260 PyObject **co_varnames;
3261 PyObject *keyword = kws[2*i];
3262 PyObject *value = kws[2*i + 1];
3263 int j;
3264 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3265 PyErr_Format(PyExc_TypeError,
3266 "%U() keywords must be strings",
3267 co->co_name);
3268 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003270 /* Speed hack: do raw pointer compares. As names are
3271 normally interned this should almost always hit. */
3272 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3273 for (j = 0; j < total_args; j++) {
3274 PyObject *nm = co_varnames[j];
3275 if (nm == keyword)
3276 goto kw_found;
3277 }
3278 /* Slow fallback, just in case */
3279 for (j = 0; j < total_args; j++) {
3280 PyObject *nm = co_varnames[j];
3281 int cmp = PyObject_RichCompareBool(
3282 keyword, nm, Py_EQ);
3283 if (cmp > 0)
3284 goto kw_found;
3285 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003287 }
3288 if (j >= total_args && kwdict == NULL) {
3289 PyErr_Format(PyExc_TypeError,
3290 "%U() got an unexpected "
3291 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 co->co_name,
3293 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003294 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003296 PyDict_SetItem(kwdict, keyword, value);
3297 continue;
3298 kw_found:
3299 if (GETLOCAL(j) != NULL) {
3300 PyErr_Format(PyExc_TypeError,
3301 "%U() got multiple "
3302 "values for argument '%S'",
3303 co->co_name,
3304 keyword);
3305 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003306 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003307 Py_INCREF(value);
3308 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003310 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003311 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 goto fail;
3313 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003314 if (argcount < co->co_argcount) {
3315 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003316 int missing = 0;
3317 for (i = argcount; i < m; i++)
3318 if (GETLOCAL(i) == NULL)
3319 missing++;
3320 if (missing) {
3321 missing_arguments(co, missing, defcount, fastlocals);
3322 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003323 }
3324 if (n > m)
3325 i = n - m;
3326 else
3327 i = 0;
3328 for (; i < defcount; i++) {
3329 if (GETLOCAL(m+i) == NULL) {
3330 PyObject *def = defs[i];
3331 Py_INCREF(def);
3332 SETLOCAL(m+i, def);
3333 }
3334 }
3335 }
3336 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003337 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003338 for (i = co->co_argcount; i < total_args; i++) {
3339 PyObject *name;
3340 if (GETLOCAL(i) != NULL)
3341 continue;
3342 name = PyTuple_GET_ITEM(co->co_varnames, i);
3343 if (kwdefs != NULL) {
3344 PyObject *def = PyDict_GetItem(kwdefs, name);
3345 if (def) {
3346 Py_INCREF(def);
3347 SETLOCAL(i, def);
3348 continue;
3349 }
3350 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003351 missing++;
3352 }
3353 if (missing) {
3354 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003355 goto fail;
3356 }
3357 }
3358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003359 /* Allocate and initialize storage for cell vars, and copy free
3360 vars into frame. This isn't too efficient right now. */
3361 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3362 int i, j, nargs, found;
3363 Py_UNICODE *cellname, *argname;
3364 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 nargs = total_args;
3367 if (co->co_flags & CO_VARARGS)
3368 nargs++;
3369 if (co->co_flags & CO_VARKEYWORDS)
3370 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 /* Initialize each cell var, taking into account
3373 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 Should arrange for the compiler to put cellvars
3376 that are arguments at the beginning of the cellvars
3377 list so that we can march over it more efficiently?
3378 */
3379 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3380 cellname = PyUnicode_AS_UNICODE(
3381 PyTuple_GET_ITEM(co->co_cellvars, i));
3382 found = 0;
3383 for (j = 0; j < nargs; j++) {
3384 argname = PyUnicode_AS_UNICODE(
3385 PyTuple_GET_ITEM(co->co_varnames, j));
3386 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3387 c = PyCell_New(GETLOCAL(j));
3388 if (c == NULL)
3389 goto fail;
3390 GETLOCAL(co->co_nlocals + i) = c;
3391 found = 1;
3392 break;
3393 }
3394 }
3395 if (found == 0) {
3396 c = PyCell_New(NULL);
3397 if (c == NULL)
3398 goto fail;
3399 SETLOCAL(co->co_nlocals + i, c);
3400 }
3401 }
3402 }
3403 if (PyTuple_GET_SIZE(co->co_freevars)) {
3404 int i;
3405 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3406 PyObject *o = PyTuple_GET_ITEM(closure, i);
3407 Py_INCREF(o);
3408 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3409 }
3410 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003412 if (co->co_flags & CO_GENERATOR) {
3413 /* Don't need to keep the reference to f_back, it will be set
3414 * when the generator is resumed. */
3415 Py_XDECREF(f->f_back);
3416 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003420 /* Create a new generator that owns the ready to run frame
3421 * and return that as the value. */
3422 return PyGen_New(f);
3423 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003426
Thomas Woutersce272b62007-09-19 21:19:28 +00003427fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 /* decref'ing the frame can cause __del__ methods to get invoked,
3430 which can call back into Python. While we're done with the
3431 current Python frame (f), the associated C stack is still in use,
3432 so recursion_depth must be boosted for the duration.
3433 */
3434 assert(tstate != NULL);
3435 ++tstate->recursion_depth;
3436 Py_DECREF(f);
3437 --tstate->recursion_depth;
3438 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003439}
3440
3441
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003442static PyObject *
3443special_lookup(PyObject *o, char *meth, PyObject **cache)
3444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 PyObject *res;
3446 res = _PyObject_LookupSpecial(o, meth, cache);
3447 if (res == NULL && !PyErr_Occurred()) {
3448 PyErr_SetObject(PyExc_AttributeError, *cache);
3449 return NULL;
3450 }
3451 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003452}
3453
3454
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003455/* Logic for the raise statement (too complicated for inlining).
3456 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003457static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003458do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 if (exc == NULL) {
3463 /* Reraise */
3464 PyThreadState *tstate = PyThreadState_GET();
3465 PyObject *tb;
3466 type = tstate->exc_type;
3467 value = tstate->exc_value;
3468 tb = tstate->exc_traceback;
3469 if (type == Py_None) {
3470 PyErr_SetString(PyExc_RuntimeError,
3471 "No active exception to reraise");
3472 return WHY_EXCEPTION;
3473 }
3474 Py_XINCREF(type);
3475 Py_XINCREF(value);
3476 Py_XINCREF(tb);
3477 PyErr_Restore(type, value, tb);
3478 return WHY_RERAISE;
3479 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 /* We support the following forms of raise:
3482 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003483 raise <instance>
3484 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003486 if (PyExceptionClass_Check(exc)) {
3487 type = exc;
3488 value = PyObject_CallObject(exc, NULL);
3489 if (value == NULL)
3490 goto raise_error;
3491 }
3492 else if (PyExceptionInstance_Check(exc)) {
3493 value = exc;
3494 type = PyExceptionInstance_Class(exc);
3495 Py_INCREF(type);
3496 }
3497 else {
3498 /* Not something you can raise. You get an exception
3499 anyway, just not what you specified :-) */
3500 Py_DECREF(exc);
3501 PyErr_SetString(PyExc_TypeError,
3502 "exceptions must derive from BaseException");
3503 goto raise_error;
3504 }
Collin Winter828f04a2007-08-31 00:04:24 +00003505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 if (cause) {
3507 PyObject *fixed_cause;
3508 if (PyExceptionClass_Check(cause)) {
3509 fixed_cause = PyObject_CallObject(cause, NULL);
3510 if (fixed_cause == NULL)
3511 goto raise_error;
3512 Py_DECREF(cause);
3513 }
3514 else if (PyExceptionInstance_Check(cause)) {
3515 fixed_cause = cause;
3516 }
3517 else {
3518 PyErr_SetString(PyExc_TypeError,
3519 "exception causes must derive from "
3520 "BaseException");
3521 goto raise_error;
3522 }
3523 PyException_SetCause(value, fixed_cause);
3524 }
Collin Winter828f04a2007-08-31 00:04:24 +00003525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 PyErr_SetObject(type, value);
3527 /* PyErr_SetObject incref's its arguments */
3528 Py_XDECREF(value);
3529 Py_XDECREF(type);
3530 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003531
3532raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003533 Py_XDECREF(value);
3534 Py_XDECREF(type);
3535 Py_XDECREF(cause);
3536 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003537}
3538
Tim Petersd6d010b2001-06-21 02:49:55 +00003539/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003540 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003541
Guido van Rossum0368b722007-05-11 16:50:42 +00003542 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3543 with a variable target.
3544*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003545
Barry Warsawe42b18f1997-08-25 22:13:04 +00003546static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003547unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003548{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 int i = 0, j = 0;
3550 Py_ssize_t ll = 0;
3551 PyObject *it; /* iter(v) */
3552 PyObject *w;
3553 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 it = PyObject_GetIter(v);
3558 if (it == NULL)
3559 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003561 for (; i < argcnt; i++) {
3562 w = PyIter_Next(it);
3563 if (w == NULL) {
3564 /* Iterator done, via error or exhaustion. */
3565 if (!PyErr_Occurred()) {
3566 PyErr_Format(PyExc_ValueError,
3567 "need more than %d value%s to unpack",
3568 i, i == 1 ? "" : "s");
3569 }
3570 goto Error;
3571 }
3572 *--sp = w;
3573 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 if (argcntafter == -1) {
3576 /* We better have exhausted the iterator now. */
3577 w = PyIter_Next(it);
3578 if (w == NULL) {
3579 if (PyErr_Occurred())
3580 goto Error;
3581 Py_DECREF(it);
3582 return 1;
3583 }
3584 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003585 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3586 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 goto Error;
3588 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 l = PySequence_List(it);
3591 if (l == NULL)
3592 goto Error;
3593 *--sp = l;
3594 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003595
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003596 ll = PyList_GET_SIZE(l);
3597 if (ll < argcntafter) {
3598 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3599 argcnt + ll);
3600 goto Error;
3601 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003603 /* Pop the "after-variable" args off the list. */
3604 for (j = argcntafter; j > 0; j--, i++) {
3605 *--sp = PyList_GET_ITEM(l, ll - j);
3606 }
3607 /* Resize the list. */
3608 Py_SIZE(l) = ll - argcntafter;
3609 Py_DECREF(it);
3610 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003611
Tim Petersd6d010b2001-06-21 02:49:55 +00003612Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 for (; i > 0; i--, sp++)
3614 Py_DECREF(*sp);
3615 Py_XDECREF(it);
3616 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003617}
3618
3619
Guido van Rossum96a42c81992-01-12 02:29:51 +00003620#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003621static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003622prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003623{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 printf("%s ", str);
3625 if (PyObject_Print(v, stdout, 0) != 0)
3626 PyErr_Clear(); /* Don't know what else to do */
3627 printf("\n");
3628 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003629}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003630#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003631
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003632static void
Fred Drake5755ce62001-06-27 19:19:46 +00003633call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003634{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003635 PyObject *type, *value, *traceback, *arg;
3636 int err;
3637 PyErr_Fetch(&type, &value, &traceback);
3638 if (value == NULL) {
3639 value = Py_None;
3640 Py_INCREF(value);
3641 }
3642 arg = PyTuple_Pack(3, type, value, traceback);
3643 if (arg == NULL) {
3644 PyErr_Restore(type, value, traceback);
3645 return;
3646 }
3647 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3648 Py_DECREF(arg);
3649 if (err == 0)
3650 PyErr_Restore(type, value, traceback);
3651 else {
3652 Py_XDECREF(type);
3653 Py_XDECREF(value);
3654 Py_XDECREF(traceback);
3655 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003656}
3657
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003658static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003659call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003661{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003662 PyObject *type, *value, *traceback;
3663 int err;
3664 PyErr_Fetch(&type, &value, &traceback);
3665 err = call_trace(func, obj, frame, what, arg);
3666 if (err == 0)
3667 {
3668 PyErr_Restore(type, value, traceback);
3669 return 0;
3670 }
3671 else {
3672 Py_XDECREF(type);
3673 Py_XDECREF(value);
3674 Py_XDECREF(traceback);
3675 return -1;
3676 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003677}
3678
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003679static int
Fred Drake5755ce62001-06-27 19:19:46 +00003680call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003681 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003682{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003683 register PyThreadState *tstate = frame->f_tstate;
3684 int result;
3685 if (tstate->tracing)
3686 return 0;
3687 tstate->tracing++;
3688 tstate->use_tracing = 0;
3689 result = func(obj, frame, what, arg);
3690 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3691 || (tstate->c_profilefunc != NULL));
3692 tstate->tracing--;
3693 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003694}
3695
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003696PyObject *
3697_PyEval_CallTracing(PyObject *func, PyObject *args)
3698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003699 PyFrameObject *frame = PyEval_GetFrame();
3700 PyThreadState *tstate = frame->f_tstate;
3701 int save_tracing = tstate->tracing;
3702 int save_use_tracing = tstate->use_tracing;
3703 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 tstate->tracing = 0;
3706 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3707 || (tstate->c_profilefunc != NULL));
3708 result = PyObject_Call(func, args, NULL);
3709 tstate->tracing = save_tracing;
3710 tstate->use_tracing = save_use_tracing;
3711 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003712}
3713
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003714/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003715static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003716maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3718 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 int result = 0;
3721 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 /* If the last instruction executed isn't in the current
3724 instruction window, reset the window.
3725 */
3726 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3727 PyAddrPair bounds;
3728 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3729 &bounds);
3730 *instr_lb = bounds.ap_lower;
3731 *instr_ub = bounds.ap_upper;
3732 }
3733 /* If the last instruction falls at the start of a line or if
3734 it represents a jump backwards, update the frame's line
3735 number and call the trace function. */
3736 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3737 frame->f_lineno = line;
3738 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3739 }
3740 *instr_prev = frame->f_lasti;
3741 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003742}
3743
Fred Drake5755ce62001-06-27 19:19:46 +00003744void
3745PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 PyThreadState *tstate = PyThreadState_GET();
3748 PyObject *temp = tstate->c_profileobj;
3749 Py_XINCREF(arg);
3750 tstate->c_profilefunc = NULL;
3751 tstate->c_profileobj = NULL;
3752 /* Must make sure that tracing is not ignored if 'temp' is freed */
3753 tstate->use_tracing = tstate->c_tracefunc != NULL;
3754 Py_XDECREF(temp);
3755 tstate->c_profilefunc = func;
3756 tstate->c_profileobj = arg;
3757 /* Flag that tracing or profiling is turned on */
3758 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003759}
3760
3761void
3762PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003764 PyThreadState *tstate = PyThreadState_GET();
3765 PyObject *temp = tstate->c_traceobj;
3766 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3767 Py_XINCREF(arg);
3768 tstate->c_tracefunc = NULL;
3769 tstate->c_traceobj = NULL;
3770 /* Must make sure that profiling is not ignored if 'temp' is freed */
3771 tstate->use_tracing = tstate->c_profilefunc != NULL;
3772 Py_XDECREF(temp);
3773 tstate->c_tracefunc = func;
3774 tstate->c_traceobj = arg;
3775 /* Flag that tracing or profiling is turned on */
3776 tstate->use_tracing = ((func != NULL)
3777 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003778}
3779
Guido van Rossumb209a111997-04-29 18:18:01 +00003780PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003781PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 PyFrameObject *current_frame = PyEval_GetFrame();
3784 if (current_frame == NULL)
3785 return PyThreadState_GET()->interp->builtins;
3786 else
3787 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003788}
3789
Guido van Rossumb209a111997-04-29 18:18:01 +00003790PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003791PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 PyFrameObject *current_frame = PyEval_GetFrame();
3794 if (current_frame == NULL)
3795 return NULL;
3796 PyFrame_FastToLocals(current_frame);
3797 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003798}
3799
Guido van Rossumb209a111997-04-29 18:18:01 +00003800PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003801PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003802{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003803 PyFrameObject *current_frame = PyEval_GetFrame();
3804 if (current_frame == NULL)
3805 return NULL;
3806 else
3807 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003808}
3809
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003810PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003811PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003812{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003813 PyThreadState *tstate = PyThreadState_GET();
3814 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003815}
3816
Guido van Rossum6135a871995-01-09 17:53:26 +00003817int
Tim Peters5ba58662001-07-16 02:29:45 +00003818PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003819{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003820 PyFrameObject *current_frame = PyEval_GetFrame();
3821 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 if (current_frame != NULL) {
3824 const int codeflags = current_frame->f_code->co_flags;
3825 const int compilerflags = codeflags & PyCF_MASK;
3826 if (compilerflags) {
3827 result = 1;
3828 cf->cf_flags |= compilerflags;
3829 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003830#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003831 if (codeflags & CO_GENERATOR_ALLOWED) {
3832 result = 1;
3833 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3834 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003835#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 }
3837 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003838}
3839
Guido van Rossum3f5da241990-12-20 15:06:42 +00003840
Guido van Rossum681d79a1995-07-18 14:51:37 +00003841/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003842 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003843
Guido van Rossumb209a111997-04-29 18:18:01 +00003844PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003845PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003849 if (arg == NULL) {
3850 arg = PyTuple_New(0);
3851 if (arg == NULL)
3852 return NULL;
3853 }
3854 else if (!PyTuple_Check(arg)) {
3855 PyErr_SetString(PyExc_TypeError,
3856 "argument list must be a tuple");
3857 return NULL;
3858 }
3859 else
3860 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003862 if (kw != NULL && !PyDict_Check(kw)) {
3863 PyErr_SetString(PyExc_TypeError,
3864 "keyword list must be a dictionary");
3865 Py_DECREF(arg);
3866 return NULL;
3867 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003869 result = PyObject_Call(func, arg, kw);
3870 Py_DECREF(arg);
3871 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003872}
3873
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003874const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003875PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003876{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 if (PyMethod_Check(func))
3878 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3879 else if (PyFunction_Check(func))
3880 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3881 else if (PyCFunction_Check(func))
3882 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3883 else
3884 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003885}
3886
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003887const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003888PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003889{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003890 if (PyMethod_Check(func))
3891 return "()";
3892 else if (PyFunction_Check(func))
3893 return "()";
3894 else if (PyCFunction_Check(func))
3895 return "()";
3896 else
3897 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003898}
3899
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003900static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003901err_args(PyObject *func, int flags, int nargs)
3902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 if (flags & METH_NOARGS)
3904 PyErr_Format(PyExc_TypeError,
3905 "%.200s() takes no arguments (%d given)",
3906 ((PyCFunctionObject *)func)->m_ml->ml_name,
3907 nargs);
3908 else
3909 PyErr_Format(PyExc_TypeError,
3910 "%.200s() takes exactly one argument (%d given)",
3911 ((PyCFunctionObject *)func)->m_ml->ml_name,
3912 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003913}
3914
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003915#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003916if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003917 if (call_trace(tstate->c_profilefunc, \
3918 tstate->c_profileobj, \
3919 tstate->frame, PyTrace_C_CALL, \
3920 func)) { \
3921 x = NULL; \
3922 } \
3923 else { \
3924 x = call; \
3925 if (tstate->c_profilefunc != NULL) { \
3926 if (x == NULL) { \
3927 call_trace_protected(tstate->c_profilefunc, \
3928 tstate->c_profileobj, \
3929 tstate->frame, PyTrace_C_EXCEPTION, \
3930 func); \
3931 /* XXX should pass (type, value, tb) */ \
3932 } else { \
3933 if (call_trace(tstate->c_profilefunc, \
3934 tstate->c_profileobj, \
3935 tstate->frame, PyTrace_C_RETURN, \
3936 func)) { \
3937 Py_DECREF(x); \
3938 x = NULL; \
3939 } \
3940 } \
3941 } \
3942 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003943} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 x = call; \
3945 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003946
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003947static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003948call_function(PyObject ***pp_stack, int oparg
3949#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003950 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003951#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 int na = oparg & 0xff;
3955 int nk = (oparg>>8) & 0xff;
3956 int n = na + 2 * nk;
3957 PyObject **pfunc = (*pp_stack) - n - 1;
3958 PyObject *func = *pfunc;
3959 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003961 /* Always dispatch PyCFunction first, because these are
3962 presumed to be the most frequent callable object.
3963 */
3964 if (PyCFunction_Check(func) && nk == 0) {
3965 int flags = PyCFunction_GET_FLAGS(func);
3966 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 PCALL(PCALL_CFUNCTION);
3969 if (flags & (METH_NOARGS | METH_O)) {
3970 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3971 PyObject *self = PyCFunction_GET_SELF(func);
3972 if (flags & METH_NOARGS && na == 0) {
3973 C_TRACE(x, (*meth)(self,NULL));
3974 }
3975 else if (flags & METH_O && na == 1) {
3976 PyObject *arg = EXT_POP(*pp_stack);
3977 C_TRACE(x, (*meth)(self,arg));
3978 Py_DECREF(arg);
3979 }
3980 else {
3981 err_args(func, flags, na);
3982 x = NULL;
3983 }
3984 }
3985 else {
3986 PyObject *callargs;
3987 callargs = load_args(pp_stack, na);
3988 READ_TIMESTAMP(*pintr0);
3989 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3990 READ_TIMESTAMP(*pintr1);
3991 Py_XDECREF(callargs);
3992 }
3993 } else {
3994 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3995 /* optimize access to bound methods */
3996 PyObject *self = PyMethod_GET_SELF(func);
3997 PCALL(PCALL_METHOD);
3998 PCALL(PCALL_BOUND_METHOD);
3999 Py_INCREF(self);
4000 func = PyMethod_GET_FUNCTION(func);
4001 Py_INCREF(func);
4002 Py_DECREF(*pfunc);
4003 *pfunc = self;
4004 na++;
4005 n++;
4006 } else
4007 Py_INCREF(func);
4008 READ_TIMESTAMP(*pintr0);
4009 if (PyFunction_Check(func))
4010 x = fast_function(func, pp_stack, n, na, nk);
4011 else
4012 x = do_call(func, pp_stack, na, nk);
4013 READ_TIMESTAMP(*pintr1);
4014 Py_DECREF(func);
4015 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004017 /* Clear the stack of the function object. Also removes
4018 the arguments in case they weren't consumed already
4019 (fast_function() and err_args() leave them on the stack).
4020 */
4021 while ((*pp_stack) > pfunc) {
4022 w = EXT_POP(*pp_stack);
4023 Py_DECREF(w);
4024 PCALL(PCALL_POP);
4025 }
4026 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004027}
4028
Jeremy Hylton192690e2002-08-16 18:36:11 +00004029/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004030 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004031 For the simplest case -- a function that takes only positional
4032 arguments and is called with only positional arguments -- it
4033 inlines the most primitive frame setup code from
4034 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4035 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004036*/
4037
4038static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004039fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004041 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4042 PyObject *globals = PyFunction_GET_GLOBALS(func);
4043 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4044 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4045 PyObject **d = NULL;
4046 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 PCALL(PCALL_FUNCTION);
4049 PCALL(PCALL_FAST_FUNCTION);
4050 if (argdefs == NULL && co->co_argcount == n &&
4051 co->co_kwonlyargcount == 0 && nk==0 &&
4052 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4053 PyFrameObject *f;
4054 PyObject *retval = NULL;
4055 PyThreadState *tstate = PyThreadState_GET();
4056 PyObject **fastlocals, **stack;
4057 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 PCALL(PCALL_FASTER_FUNCTION);
4060 assert(globals != NULL);
4061 /* XXX Perhaps we should create a specialized
4062 PyFrame_New() that doesn't take locals, but does
4063 take builtins without sanity checking them.
4064 */
4065 assert(tstate != NULL);
4066 f = PyFrame_New(tstate, co, globals, NULL);
4067 if (f == NULL)
4068 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004070 fastlocals = f->f_localsplus;
4071 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 for (i = 0; i < n; i++) {
4074 Py_INCREF(*stack);
4075 fastlocals[i] = *stack++;
4076 }
4077 retval = PyEval_EvalFrameEx(f,0);
4078 ++tstate->recursion_depth;
4079 Py_DECREF(f);
4080 --tstate->recursion_depth;
4081 return retval;
4082 }
4083 if (argdefs != NULL) {
4084 d = &PyTuple_GET_ITEM(argdefs, 0);
4085 nd = Py_SIZE(argdefs);
4086 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004087 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 (PyObject *)NULL, (*pp_stack)-n, na,
4089 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4090 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004091}
4092
4093static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004094update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4095 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004096{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 PyObject *kwdict = NULL;
4098 if (orig_kwdict == NULL)
4099 kwdict = PyDict_New();
4100 else {
4101 kwdict = PyDict_Copy(orig_kwdict);
4102 Py_DECREF(orig_kwdict);
4103 }
4104 if (kwdict == NULL)
4105 return NULL;
4106 while (--nk >= 0) {
4107 int err;
4108 PyObject *value = EXT_POP(*pp_stack);
4109 PyObject *key = EXT_POP(*pp_stack);
4110 if (PyDict_GetItem(kwdict, key) != NULL) {
4111 PyErr_Format(PyExc_TypeError,
4112 "%.200s%s got multiple values "
4113 "for keyword argument '%U'",
4114 PyEval_GetFuncName(func),
4115 PyEval_GetFuncDesc(func),
4116 key);
4117 Py_DECREF(key);
4118 Py_DECREF(value);
4119 Py_DECREF(kwdict);
4120 return NULL;
4121 }
4122 err = PyDict_SetItem(kwdict, key, value);
4123 Py_DECREF(key);
4124 Py_DECREF(value);
4125 if (err) {
4126 Py_DECREF(kwdict);
4127 return NULL;
4128 }
4129 }
4130 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004131}
4132
4133static PyObject *
4134update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004135 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004136{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004137 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 callargs = PyTuple_New(nstack + nstar);
4140 if (callargs == NULL) {
4141 return NULL;
4142 }
4143 if (nstar) {
4144 int i;
4145 for (i = 0; i < nstar; i++) {
4146 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4147 Py_INCREF(a);
4148 PyTuple_SET_ITEM(callargs, nstack + i, a);
4149 }
4150 }
4151 while (--nstack >= 0) {
4152 w = EXT_POP(*pp_stack);
4153 PyTuple_SET_ITEM(callargs, nstack, w);
4154 }
4155 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004156}
4157
4158static PyObject *
4159load_args(PyObject ***pp_stack, int na)
4160{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004161 PyObject *args = PyTuple_New(na);
4162 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004164 if (args == NULL)
4165 return NULL;
4166 while (--na >= 0) {
4167 w = EXT_POP(*pp_stack);
4168 PyTuple_SET_ITEM(args, na, w);
4169 }
4170 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004171}
4172
4173static PyObject *
4174do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4175{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 PyObject *callargs = NULL;
4177 PyObject *kwdict = NULL;
4178 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 if (nk > 0) {
4181 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4182 if (kwdict == NULL)
4183 goto call_fail;
4184 }
4185 callargs = load_args(pp_stack, na);
4186 if (callargs == NULL)
4187 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004188#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004189 /* At this point, we have to look at the type of func to
4190 update the call stats properly. Do it here so as to avoid
4191 exposing the call stats machinery outside ceval.c
4192 */
4193 if (PyFunction_Check(func))
4194 PCALL(PCALL_FUNCTION);
4195 else if (PyMethod_Check(func))
4196 PCALL(PCALL_METHOD);
4197 else if (PyType_Check(func))
4198 PCALL(PCALL_TYPE);
4199 else if (PyCFunction_Check(func))
4200 PCALL(PCALL_CFUNCTION);
4201 else
4202 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004203#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 if (PyCFunction_Check(func)) {
4205 PyThreadState *tstate = PyThreadState_GET();
4206 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4207 }
4208 else
4209 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004210call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 Py_XDECREF(callargs);
4212 Py_XDECREF(kwdict);
4213 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004214}
4215
4216static PyObject *
4217ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4218{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004219 int nstar = 0;
4220 PyObject *callargs = NULL;
4221 PyObject *stararg = NULL;
4222 PyObject *kwdict = NULL;
4223 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 if (flags & CALL_FLAG_KW) {
4226 kwdict = EXT_POP(*pp_stack);
4227 if (!PyDict_Check(kwdict)) {
4228 PyObject *d;
4229 d = PyDict_New();
4230 if (d == NULL)
4231 goto ext_call_fail;
4232 if (PyDict_Update(d, kwdict) != 0) {
4233 Py_DECREF(d);
4234 /* PyDict_Update raises attribute
4235 * error (percolated from an attempt
4236 * to get 'keys' attribute) instead of
4237 * a type error if its second argument
4238 * is not a mapping.
4239 */
4240 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4241 PyErr_Format(PyExc_TypeError,
4242 "%.200s%.200s argument after ** "
4243 "must be a mapping, not %.200s",
4244 PyEval_GetFuncName(func),
4245 PyEval_GetFuncDesc(func),
4246 kwdict->ob_type->tp_name);
4247 }
4248 goto ext_call_fail;
4249 }
4250 Py_DECREF(kwdict);
4251 kwdict = d;
4252 }
4253 }
4254 if (flags & CALL_FLAG_VAR) {
4255 stararg = EXT_POP(*pp_stack);
4256 if (!PyTuple_Check(stararg)) {
4257 PyObject *t = NULL;
4258 t = PySequence_Tuple(stararg);
4259 if (t == NULL) {
4260 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4261 PyErr_Format(PyExc_TypeError,
4262 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004263 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004264 PyEval_GetFuncName(func),
4265 PyEval_GetFuncDesc(func),
4266 stararg->ob_type->tp_name);
4267 }
4268 goto ext_call_fail;
4269 }
4270 Py_DECREF(stararg);
4271 stararg = t;
4272 }
4273 nstar = PyTuple_GET_SIZE(stararg);
4274 }
4275 if (nk > 0) {
4276 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4277 if (kwdict == NULL)
4278 goto ext_call_fail;
4279 }
4280 callargs = update_star_args(na, nstar, stararg, pp_stack);
4281 if (callargs == NULL)
4282 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004283#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004284 /* At this point, we have to look at the type of func to
4285 update the call stats properly. Do it here so as to avoid
4286 exposing the call stats machinery outside ceval.c
4287 */
4288 if (PyFunction_Check(func))
4289 PCALL(PCALL_FUNCTION);
4290 else if (PyMethod_Check(func))
4291 PCALL(PCALL_METHOD);
4292 else if (PyType_Check(func))
4293 PCALL(PCALL_TYPE);
4294 else if (PyCFunction_Check(func))
4295 PCALL(PCALL_CFUNCTION);
4296 else
4297 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004298#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004299 if (PyCFunction_Check(func)) {
4300 PyThreadState *tstate = PyThreadState_GET();
4301 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4302 }
4303 else
4304 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004305ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 Py_XDECREF(callargs);
4307 Py_XDECREF(kwdict);
4308 Py_XDECREF(stararg);
4309 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004310}
4311
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004312/* Extract a slice index from a PyInt or PyLong or an object with the
4313 nb_index slot defined, and store in *pi.
4314 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4315 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 +00004316 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004317*/
Tim Petersb5196382001-12-16 19:44:20 +00004318/* Note: If v is NULL, return success without storing into *pi. This
4319 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4320 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004321*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004322int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004323_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 if (v != NULL) {
4326 Py_ssize_t x;
4327 if (PyIndex_Check(v)) {
4328 x = PyNumber_AsSsize_t(v, NULL);
4329 if (x == -1 && PyErr_Occurred())
4330 return 0;
4331 }
4332 else {
4333 PyErr_SetString(PyExc_TypeError,
4334 "slice indices must be integers or "
4335 "None or have an __index__ method");
4336 return 0;
4337 }
4338 *pi = x;
4339 }
4340 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004341}
4342
Guido van Rossum486364b2007-06-30 05:01:58 +00004343#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004345
Guido van Rossumb209a111997-04-29 18:18:01 +00004346static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004347cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 int res = 0;
4350 switch (op) {
4351 case PyCmp_IS:
4352 res = (v == w);
4353 break;
4354 case PyCmp_IS_NOT:
4355 res = (v != w);
4356 break;
4357 case PyCmp_IN:
4358 res = PySequence_Contains(w, v);
4359 if (res < 0)
4360 return NULL;
4361 break;
4362 case PyCmp_NOT_IN:
4363 res = PySequence_Contains(w, v);
4364 if (res < 0)
4365 return NULL;
4366 res = !res;
4367 break;
4368 case PyCmp_EXC_MATCH:
4369 if (PyTuple_Check(w)) {
4370 Py_ssize_t i, length;
4371 length = PyTuple_Size(w);
4372 for (i = 0; i < length; i += 1) {
4373 PyObject *exc = PyTuple_GET_ITEM(w, i);
4374 if (!PyExceptionClass_Check(exc)) {
4375 PyErr_SetString(PyExc_TypeError,
4376 CANNOT_CATCH_MSG);
4377 return NULL;
4378 }
4379 }
4380 }
4381 else {
4382 if (!PyExceptionClass_Check(w)) {
4383 PyErr_SetString(PyExc_TypeError,
4384 CANNOT_CATCH_MSG);
4385 return NULL;
4386 }
4387 }
4388 res = PyErr_GivenExceptionMatches(v, w);
4389 break;
4390 default:
4391 return PyObject_RichCompare(v, w, op);
4392 }
4393 v = res ? Py_True : Py_False;
4394 Py_INCREF(v);
4395 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004396}
4397
Thomas Wouters52152252000-08-17 22:55:00 +00004398static PyObject *
4399import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004400{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 x = PyObject_GetAttr(v, name);
4404 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4405 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4406 }
4407 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004408}
Guido van Rossumac7be682001-01-17 15:42:30 +00004409
Thomas Wouters52152252000-08-17 22:55:00 +00004410static int
4411import_all_from(PyObject *locals, PyObject *v)
4412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004413 PyObject *all = PyObject_GetAttrString(v, "__all__");
4414 PyObject *dict, *name, *value;
4415 int skip_leading_underscores = 0;
4416 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004418 if (all == NULL) {
4419 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4420 return -1; /* Unexpected error */
4421 PyErr_Clear();
4422 dict = PyObject_GetAttrString(v, "__dict__");
4423 if (dict == NULL) {
4424 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4425 return -1;
4426 PyErr_SetString(PyExc_ImportError,
4427 "from-import-* object has no __dict__ and no __all__");
4428 return -1;
4429 }
4430 all = PyMapping_Keys(dict);
4431 Py_DECREF(dict);
4432 if (all == NULL)
4433 return -1;
4434 skip_leading_underscores = 1;
4435 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004437 for (pos = 0, err = 0; ; pos++) {
4438 name = PySequence_GetItem(all, pos);
4439 if (name == NULL) {
4440 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4441 err = -1;
4442 else
4443 PyErr_Clear();
4444 break;
4445 }
4446 if (skip_leading_underscores &&
4447 PyUnicode_Check(name) &&
4448 PyUnicode_AS_UNICODE(name)[0] == '_')
4449 {
4450 Py_DECREF(name);
4451 continue;
4452 }
4453 value = PyObject_GetAttr(v, name);
4454 if (value == NULL)
4455 err = -1;
4456 else if (PyDict_CheckExact(locals))
4457 err = PyDict_SetItem(locals, name, value);
4458 else
4459 err = PyObject_SetItem(locals, name, value);
4460 Py_DECREF(name);
4461 Py_XDECREF(value);
4462 if (err != 0)
4463 break;
4464 }
4465 Py_DECREF(all);
4466 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004467}
4468
Guido van Rossumac7be682001-01-17 15:42:30 +00004469static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004470format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004474 if (!obj)
4475 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 obj_str = _PyUnicode_AsString(obj);
4478 if (!obj_str)
4479 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004482}
Guido van Rossum950361c1997-01-24 13:49:28 +00004483
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004484static void
4485format_exc_unbound(PyCodeObject *co, int oparg)
4486{
4487 PyObject *name;
4488 /* Don't stomp existing exception */
4489 if (PyErr_Occurred())
4490 return;
4491 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4492 name = PyTuple_GET_ITEM(co->co_cellvars,
4493 oparg);
4494 format_exc_check_arg(
4495 PyExc_UnboundLocalError,
4496 UNBOUNDLOCAL_ERROR_MSG,
4497 name);
4498 } else {
4499 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4500 PyTuple_GET_SIZE(co->co_cellvars));
4501 format_exc_check_arg(PyExc_NameError,
4502 UNBOUNDFREE_ERROR_MSG, name);
4503 }
4504}
4505
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004506static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004507unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004510 /* This function implements 'variable += expr' when both arguments
4511 are (Unicode) strings. */
4512 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4513 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4514 Py_ssize_t new_len = v_len + w_len;
4515 if (new_len < 0) {
4516 PyErr_SetString(PyExc_OverflowError,
4517 "strings are too large to concat");
4518 return NULL;
4519 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004520
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004521 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004522 /* In the common case, there are 2 references to the value
4523 * stored in 'variable' when the += is performed: one on the
4524 * value stack (in 'v') and one still stored in the
4525 * 'variable'. We try to delete the variable now to reduce
4526 * the refcnt to 1.
4527 */
4528 switch (*next_instr) {
4529 case STORE_FAST:
4530 {
4531 int oparg = PEEKARG();
4532 PyObject **fastlocals = f->f_localsplus;
4533 if (GETLOCAL(oparg) == v)
4534 SETLOCAL(oparg, NULL);
4535 break;
4536 }
4537 case STORE_DEREF:
4538 {
4539 PyObject **freevars = (f->f_localsplus +
4540 f->f_code->co_nlocals);
4541 PyObject *c = freevars[PEEKARG()];
4542 if (PyCell_GET(c) == v)
4543 PyCell_Set(c, NULL);
4544 break;
4545 }
4546 case STORE_NAME:
4547 {
4548 PyObject *names = f->f_code->co_names;
4549 PyObject *name = GETITEM(names, PEEKARG());
4550 PyObject *locals = f->f_locals;
4551 if (PyDict_CheckExact(locals) &&
4552 PyDict_GetItem(locals, name) == v) {
4553 if (PyDict_DelItem(locals, name) != 0) {
4554 PyErr_Clear();
4555 }
4556 }
4557 break;
4558 }
4559 }
4560 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004561
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004562 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004563 /* Now we own the last reference to 'v', so we can resize it
4564 * in-place.
4565 */
4566 if (PyUnicode_Resize(&v, new_len) != 0) {
4567 /* XXX if PyUnicode_Resize() fails, 'v' has been
4568 * deallocated so it cannot be put back into
4569 * 'variable'. The MemoryError is raised when there
4570 * is no value in 'variable', which might (very
4571 * remotely) be a cause of incompatibilities.
4572 */
4573 return NULL;
4574 }
4575 /* copy 'w' into the newly allocated area of 'v' */
4576 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4577 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4578 return v;
4579 }
4580 else {
4581 /* When in-place resizing is not an option. */
4582 w = PyUnicode_Concat(v, w);
4583 Py_DECREF(v);
4584 return w;
4585 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004586}
4587
Guido van Rossum950361c1997-01-24 13:49:28 +00004588#ifdef DYNAMIC_EXECUTION_PROFILE
4589
Skip Montanarof118cb12001-10-15 20:51:38 +00004590static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004591getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004592{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004593 int i;
4594 PyObject *l = PyList_New(256);
4595 if (l == NULL) return NULL;
4596 for (i = 0; i < 256; i++) {
4597 PyObject *x = PyLong_FromLong(a[i]);
4598 if (x == NULL) {
4599 Py_DECREF(l);
4600 return NULL;
4601 }
4602 PyList_SetItem(l, i, x);
4603 }
4604 for (i = 0; i < 256; i++)
4605 a[i] = 0;
4606 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004607}
4608
4609PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004610_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004611{
4612#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004613 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004614#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 int i;
4616 PyObject *l = PyList_New(257);
4617 if (l == NULL) return NULL;
4618 for (i = 0; i < 257; i++) {
4619 PyObject *x = getarray(dxpairs[i]);
4620 if (x == NULL) {
4621 Py_DECREF(l);
4622 return NULL;
4623 }
4624 PyList_SetItem(l, i, x);
4625 }
4626 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004627#endif
4628}
4629
4630#endif