blob: e2d96c5c4426217bc4b549b7bf35f3c77156c791 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000017#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018
Guido van Rossumc6004111993-11-05 10:22:19 +000019#include <ctype.h>
20
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000022
23#define READ_TIMESTAMP(var)
24
25#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000026
27typedef unsigned long long uint64;
28
Ezio Melotti13925002011-03-16 11:05:33 +020029/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000030 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
31 "__powerpc__" appears to be the correct one for Linux with GCC
32*/
33#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000141static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000147#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000149#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "free variable '%.200s' referenced before assignment" \
151 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000152
Guido van Rossum950361c1997-01-24 13:49:28 +0000153/* Dynamic execution profile */
154#ifdef DYNAMIC_EXECUTION_PROFILE
155#ifdef DXPAIRS
156static long dxpairs[257][256];
157#define dxp dxpairs[256]
158#else
159static long dxp[256];
160#endif
161#endif
162
Jeremy Hylton985eba52003-02-05 23:13:00 +0000163/* Function call profile */
164#ifdef CALL_PROFILE
165#define PCALL_NUM 11
166static int pcall[PCALL_NUM];
167
168#define PCALL_ALL 0
169#define PCALL_FUNCTION 1
170#define PCALL_FAST_FUNCTION 2
171#define PCALL_FASTER_FUNCTION 3
172#define PCALL_METHOD 4
173#define PCALL_BOUND_METHOD 5
174#define PCALL_CFUNCTION 6
175#define PCALL_TYPE 7
176#define PCALL_GENERATOR 8
177#define PCALL_OTHER 9
178#define PCALL_POP 10
179
180/* Notes about the statistics
181
182 PCALL_FAST stats
183
184 FAST_FUNCTION means no argument tuple needs to be created.
185 FASTER_FUNCTION means that the fast-path frame setup code is used.
186
187 If there is a method call where the call can be optimized by changing
188 the argument tuple and calling the function directly, it gets recorded
189 twice.
190
191 As a result, the relationship among the statistics appears to be
192 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
193 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
194 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
195 PCALL_METHOD > PCALL_BOUND_METHOD
196*/
197
198#define PCALL(POS) pcall[POS]++
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return Py_BuildValue("iiiiiiiiiii",
204 pcall[0], pcall[1], pcall[2], pcall[3],
205 pcall[4], pcall[5], pcall[6], pcall[7],
206 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000207}
208#else
209#define PCALL(O)
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#endif
218
Tim Peters5ca576e2001-06-18 22:08:13 +0000219
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000220#ifdef WITH_THREAD
221#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
222#else
223#define GIL_REQUEST 0
224#endif
225
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000226/* This can set eval_breaker to 0 even though gil_drop_request became
227 1. We believe this is all right because the eval loop will release
228 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000229#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 _Py_atomic_store_relaxed( \
231 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000232 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
234 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000236#ifdef WITH_THREAD
237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 do { \
240 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
241 _Py_atomic_store_relaxed(&eval_breaker, 1); \
242 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243
244#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
247 COMPUTE_EVAL_BREAKER(); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000250#endif
251
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000252/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 do { \
255 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
256 _Py_atomic_store_relaxed(&eval_breaker, 1); \
257 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
259#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
262 COMPUTE_EVAL_BREAKER(); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 pending_async_exc = 1; \
268 _Py_atomic_store_relaxed(&eval_breaker, 1); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274
Guido van Rossume59214e1994-08-30 08:01:59 +0000275#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000276
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000279#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000280#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000282static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000283static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284/* This single variable consolidates all requests to break out of the fast path
285 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000286static _Py_atomic_int eval_breaker = {0};
287/* Request for dropping the GIL */
288static _Py_atomic_int gil_drop_request = {0};
289/* Request for running pending calls. */
290static _Py_atomic_int pendingcalls_to_do = {0};
291/* Request for looking at the `async_exc` field of the current thread state.
292 Guarded by the GIL. */
293static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000294
295#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296
Tim Peters7f468f22004-10-11 02:40:51 +0000297int
298PyEval_ThreadsInitialized(void)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000301}
302
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (gil_created())
307 return;
308 create_gil();
309 take_gil(PyThreadState_GET());
310 main_thread = PyThread_get_thread_ident();
311 if (!pending_lock)
312 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000316_PyEval_FiniThreads(void)
317{
318 if (!gil_created())
319 return;
320 destroy_gil();
321 assert(!gil_created());
322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
330 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000331}
332
333void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* This function must succeed when the current thread state is NULL.
337 We therefore avoid PyThreadState_GET() which dumps a fatal error
338 in debug mode.
339 */
340 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
341 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (tstate == NULL)
348 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
349 /* Check someone has called PyEval_InitThreads() to create the lock */
350 assert(gil_created());
351 take_gil(tstate);
352 if (PyThreadState_Swap(tstate) != NULL)
353 Py_FatalError(
354 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000355}
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (tstate == NULL)
361 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
362 if (PyThreadState_Swap(NULL) != tstate)
363 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
364 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000366
367/* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */
371
372void
373PyEval_ReInitThreads(void)
374{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200375 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyObject *threading, *result;
377 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (!gil_created())
380 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 recreate_gil();
382 pending_lock = PyThread_allocate_lock();
383 take_gil(tstate);
384 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* Update the threading module with the new state.
387 */
388 tstate = PyThreadState_GET();
389 threading = PyMapping_GetItemString(tstate->interp->modules,
390 "threading");
391 if (threading == NULL) {
392 /* threading not imported */
393 PyErr_Clear();
394 return;
395 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200396 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (result == NULL)
398 PyErr_WriteUnraisable(threading);
399 else
400 Py_DECREF(result);
401 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000402}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000403
404#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000405static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000406static int pending_async_exc = 0;
407#endif /* WITH_THREAD */
408
409/* This function is used to signal that async exceptions are waiting to be
410 raised, therefore it is also useful in non-threaded builds. */
411
412void
413_PyEval_SignalAsyncExc(void)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000416}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417
Guido van Rossumff4949e1992-08-05 19:58:53 +0000418/* Functions save_thread and restore_thread are always defined so
419 dynamically loaded modules needn't be compiled separately for use
420 with and without threads: */
421
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000422PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 PyThreadState *tstate = PyThreadState_Swap(NULL);
426 if (tstate == NULL)
427 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000428#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (gil_created())
430 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433}
434
435void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (tstate == NULL)
439 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000440#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (gil_created()) {
442 int err = errno;
443 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200444 /* _Py_Finalizing is protected by the GIL */
445 if (_Py_Finalizing && tstate != _Py_Finalizing) {
446 drop_gil(tstate);
447 PyThread_exit_thread();
448 assert(0); /* unreachable */
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 errno = err;
451 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454}
455
456
Guido van Rossuma9672091994-09-14 13:31:22 +0000457/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
458 signal handlers or Mac I/O completion routines) can schedule calls
459 to a function to be called synchronously.
460 The synchronous function is called with one void* argument.
461 It should return 0 for success or -1 for failure -- failure should
462 be accompanied by an exception.
463
464 If registry succeeds, the registry function returns 0; if it fails
465 (e.g. due to too many pending calls) it returns -1 (without setting
466 an exception condition).
467
468 Note that because registry may occur from within signal handlers,
469 or other asynchronous events, calling malloc() is unsafe!
470
471#ifdef WITH_THREAD
472 Any thread can schedule pending calls, but only the main thread
473 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474 There is no facility to schedule calls to a particular thread, but
475 that should be easy to change, should that ever be required. In
476 that case, the static variables here should go into the python
477 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000478#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000480
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481#ifdef WITH_THREAD
482
483/* The WITH_THREAD implementation is thread-safe. It allows
484 scheduling to be made from any thread, and even from an executing
485 callback.
486 */
487
488#define NPENDINGCALLS 32
489static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 int (*func)(void *);
491 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492} pendingcalls[NPENDINGCALLS];
493static int pendingfirst = 0;
494static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
496int
497Py_AddPendingCall(int (*func)(void *), void *arg)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we
505 * hold the lock during the Py_MakePendingCalls() function.
506 * This avoids a deadlock in that case.
507 * Note that signals can be delivered on any thread. In particular,
508 * on Windows, a SIGINT is delivered on a system-created worker
509 * thread.
510 * We also check for lock being NULL, in the unlikely case that
511 * this function is called before any bytecode evaluation takes place.
512 */
513 if (lock != NULL) {
514 for (i = 0; i<100; i++) {
515 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
516 break;
517 }
518 if (i == 100)
519 return -1;
520 }
521
522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
524 if (j == pendingfirst) {
525 result = -1; /* Queue full */
526 } else {
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
530 }
531 /* signal main loop */
532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL)
534 PyThread_release_lock(lock);
535 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538int
539Py_MakePendingCalls(void)
540{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200541 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int i;
543 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (!pending_lock) {
546 /* initial allocation of the lock */
547 pending_lock = PyThread_allocate_lock();
548 if (pending_lock == NULL)
549 return -1;
550 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* only service pending calls on main thread */
553 if (main_thread && PyThread_get_thread_ident() != main_thread)
554 return 0;
555 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200556 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200558 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* perform a bounded number of calls, in case of recursion */
560 for (i=0; i<NPENDINGCALLS; i++) {
561 int j;
562 int (*func)(void *);
563 void *arg = NULL;
564
565 /* pop one item off the queue while holding the lock */
566 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
567 j = pendingfirst;
568 if (j == pendinglast) {
569 func = NULL; /* Queue empty */
570 } else {
571 func = pendingcalls[j].func;
572 arg = pendingcalls[j].arg;
573 pendingfirst = (j + 1) % NPENDINGCALLS;
574 }
575 if (pendingfirst != pendinglast)
576 SIGNAL_PENDING_CALLS();
577 else
578 UNSIGNAL_PENDING_CALLS();
579 PyThread_release_lock(pending_lock);
580 /* having released the lock, perform the callback */
581 if (func == NULL)
582 break;
583 r = func(arg);
584 if (r)
585 break;
586 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200587 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000589}
590
591#else /* if ! defined WITH_THREAD */
592
593/*
594 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
595 This code is used for signal handling in python that isn't built
596 with WITH_THREAD.
597 Don't use this implementation when Py_AddPendingCalls() can happen
598 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599
Guido van Rossuma9672091994-09-14 13:31:22 +0000600 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601 (1) nested asynchronous calls to Py_AddPendingCall()
602 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604 (1) is very unlikely because typically signal delivery
605 is blocked during signal handling. So it should be impossible.
606 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000607 The current code is safe against (2), but not against (1).
608 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 thread is present, interrupted by signals, and that the critical
610 section is protected with the "busy" variable. On Windows, which
611 delivers SIGINT on a system thread, this does not hold and therefore
612 Windows really shouldn't use this version.
613 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000614*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000615
Guido van Rossuma9672091994-09-14 13:31:22 +0000616#define NPENDINGCALLS 32
617static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 int (*func)(void *);
619 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000620} pendingcalls[NPENDINGCALLS];
621static volatile int pendingfirst = 0;
622static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000623static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000624
625int
Thomas Wouters334fb892000-07-25 12:56:38 +0000626Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 static volatile int busy = 0;
629 int i, j;
630 /* XXX Begin critical section */
631 if (busy)
632 return -1;
633 busy = 1;
634 i = pendinglast;
635 j = (i + 1) % NPENDINGCALLS;
636 if (j == pendingfirst) {
637 busy = 0;
638 return -1; /* Queue full */
639 }
640 pendingcalls[i].func = func;
641 pendingcalls[i].arg = arg;
642 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 SIGNAL_PENDING_CALLS();
645 busy = 0;
646 /* XXX End critical section */
647 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000648}
649
Guido van Rossum180d7b41994-09-29 09:45:57 +0000650int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 static int busy = 0;
654 if (busy)
655 return 0;
656 busy = 1;
657 UNSIGNAL_PENDING_CALLS();
658 for (;;) {
659 int i;
660 int (*func)(void *);
661 void *arg;
662 i = pendingfirst;
663 if (i == pendinglast)
664 break; /* Queue empty */
665 func = pendingcalls[i].func;
666 arg = pendingcalls[i].arg;
667 pendingfirst = (i + 1) % NPENDINGCALLS;
668 if (func(arg) < 0) {
669 busy = 0;
670 SIGNAL_PENDING_CALLS(); /* We're not done yet */
671 return -1;
672 }
673 }
674 busy = 0;
675 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000676}
677
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000678#endif /* WITH_THREAD */
679
Guido van Rossuma9672091994-09-14 13:31:22 +0000680
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681/* The interpreter's recursion limit */
682
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000683#ifndef Py_DEFAULT_RECURSION_LIMIT
684#define Py_DEFAULT_RECURSION_LIMIT 1000
685#endif
686static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
687int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000689int
690Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693}
694
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000695void
696Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 recursion_limit = new_limit;
699 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700}
701
Armin Rigo2b3eb402003-10-28 12:05:48 +0000702/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
703 if the recursion_depth reaches _Py_CheckRecursionLimit.
704 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
705 to guarantee that _Py_CheckRecursiveCall() is regularly called.
706 Without USE_STACKCHECK, there is no need for this. */
707int
708_Py_CheckRecursiveCall(char *where)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711
712#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (PyOS_CheckStack()) {
714 --tstate->recursion_depth;
715 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
716 return -1;
717 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 _Py_CheckRecursionLimit = recursion_limit;
720 if (tstate->recursion_critical)
721 /* Somebody asked that we don't check for recursion. */
722 return 0;
723 if (tstate->overflowed) {
724 if (tstate->recursion_depth > recursion_limit + 50) {
725 /* Overflowing while handling an overflow. Give up. */
726 Py_FatalError("Cannot recover from stack overflow.");
727 }
728 return 0;
729 }
730 if (tstate->recursion_depth > recursion_limit) {
731 --tstate->recursion_depth;
732 tstate->overflowed = 1;
733 PyErr_Format(PyExc_RuntimeError,
734 "maximum recursion depth exceeded%s",
735 where);
736 return -1;
737 }
738 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000739}
740
Guido van Rossum374a9221991-04-04 10:40:29 +0000741/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000742enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000743 WHY_NOT = 0x0001, /* No error */
744 WHY_EXCEPTION = 0x0002, /* Exception occurred */
745 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
746 WHY_RETURN = 0x0008, /* 'return' statement */
747 WHY_BREAK = 0x0010, /* 'break' statement */
748 WHY_CONTINUE = 0x0020, /* 'continue' statement */
749 WHY_YIELD = 0x0040, /* 'yield' operator */
750 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000751};
Guido van Rossum374a9221991-04-04 10:40:29 +0000752
Benjamin Peterson87880242011-07-03 16:48:31 -0500753static void save_exc_state(PyThreadState *, PyFrameObject *);
754static void swap_exc_state(PyThreadState *, PyFrameObject *);
755static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Collin Winter828f04a2007-08-31 00:04:24 +0000756static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000757static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000758
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000759/* Records whether tracing is on for any thread. Counts the number of
760 threads for which tstate->c_tracefunc is non-NULL, so if the value
761 is 0, we know we don't have to check this thread's c_tracefunc.
762 This speeds up the if statement in PyEval_EvalFrameEx() after
763 fast_next_opcode*/
764static int _Py_TracingPossible = 0;
765
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000766
Guido van Rossum374a9221991-04-04 10:40:29 +0000767
Guido van Rossumb209a111997-04-29 18:18:01 +0000768PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000769PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return PyEval_EvalCodeEx(co,
772 globals, locals,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 (PyObject **)NULL, 0,
776 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777}
778
779
780/* Interpreter main loop */
781
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000782PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000783PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* This is for backward compatibility with extension modules that
785 used this API; core interpreter code should call
786 PyEval_EvalFrameEx() */
787 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000788}
789
790PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000792{
Guido van Rossum950361c1997-01-24 13:49:28 +0000793#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000795#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 register PyObject **stack_pointer; /* Next free slot in value stack */
797 register unsigned char *next_instr;
798 register int opcode; /* Current opcode */
799 register int oparg; /* Current opcode argument, if any */
800 register enum why_code why; /* Reason for block stack unwind */
801 register int err; /* Error status -- nonzero if error */
802 register PyObject *x; /* Result object -- NULL if error */
803 register PyObject *v; /* Temporary objects popped off stack */
804 register PyObject *w;
805 register PyObject *u;
806 register PyObject *t;
807 register PyObject **fastlocals, **freevars;
808 PyObject *retval = NULL; /* Return value */
809 PyThreadState *tstate = PyThreadState_GET();
810 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 is true when the line being executed has changed. The
817 initial values are such as to make this false the first
818 time it is tested. */
819 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 unsigned char *first_instr;
822 PyObject *names;
823 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000824
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825/* Computed GOTOs, or
826 the-optimization-commonly-but-improperly-known-as-"threaded code"
827 using gcc's labels-as-values extension
828 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
829
830 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000832 combined with a lookup table of jump addresses. However, since the
833 indirect jump instruction is shared by all opcodes, the CPU will have a
834 hard time making the right prediction for where to jump next (actually,
835 it will be always wrong except in the uncommon case of a sequence of
836 several identical opcodes).
837
838 "Threaded code" in contrast, uses an explicit jump table and an explicit
839 indirect jump instruction at the end of each opcode. Since the jump
840 instruction is at a different address for each opcode, the CPU will make a
841 separate prediction for each of these instructions, which is equivalent to
842 predicting the second opcode of each opcode pair. These predictions have
843 a much better chance to turn out valid, especially in small bytecode loops.
844
845 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847 and potentially many more instructions (depending on the pipeline width).
848 A correctly predicted branch, however, is nearly free.
849
850 At the time of this writing, the "threaded code" version is up to 15-20%
851 faster than the normal "switch" version, depending on the compiler and the
852 CPU architecture.
853
854 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
855 because it would render the measurements invalid.
856
857
858 NOTE: care must be taken that the compiler doesn't try to "optimize" the
859 indirect jumps by sharing them between all opcodes. Such optimizations
860 can be disabled on gcc by using the -fno-gcse flag (or possibly
861 -fno-crossjumping).
862*/
863
Antoine Pitrou042b1282010-08-13 21:15:58 +0000864#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867#endif
868
Antoine Pitrou042b1282010-08-13 21:15:58 +0000869#ifdef HAVE_COMPUTED_GOTOS
870 #ifndef USE_COMPUTED_GOTOS
871 #define USE_COMPUTED_GOTOS 1
872 #endif
873#else
874 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
875 #error "Computed gotos are not supported on this compiler."
876 #endif
877 #undef USE_COMPUTED_GOTOS
878 #define USE_COMPUTED_GOTOS 0
879#endif
880
881#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000882/* Import the static jump table */
883#include "opcode_targets.h"
884
885/* This macro is used when several opcodes defer to the same implementation
886 (e.g. SETUP_LOOP, SETUP_FINALLY) */
887#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 TARGET_##op: \
889 opcode = op; \
890 if (HAS_ARG(op)) \
891 oparg = NEXTARG(); \
892 case op: \
893 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894
895#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 TARGET_##op: \
897 opcode = op; \
898 if (HAS_ARG(op)) \
899 oparg = NEXTARG(); \
900 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901
902
903#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 { \
905 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
906 FAST_DISPATCH(); \
907 } \
908 continue; \
909 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000910
911#ifdef LLTRACE
912#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 { \
914 if (!lltrace && !_Py_TracingPossible) { \
915 f->f_lasti = INSTR_OFFSET(); \
916 goto *opcode_targets[*next_instr++]; \
917 } \
918 goto fast_next_opcode; \
919 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000920#else
921#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 { \
923 if (!_Py_TracingPossible) { \
924 f->f_lasti = INSTR_OFFSET(); \
925 goto *opcode_targets[*next_instr++]; \
926 } \
927 goto fast_next_opcode; \
928 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000929#endif
930
931#else
932#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000934#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* silence compiler warnings about `impl` unused */ \
936 if (0) goto impl; \
937 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938#define DISPATCH() continue
939#define FAST_DISPATCH() goto fast_next_opcode
940#endif
941
942
Neal Norwitza81d2202002-07-14 00:27:26 +0000943/* Tuple access macros */
944
945#ifndef Py_DEBUG
946#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
947#else
948#define GETITEM(v, i) PyTuple_GetItem((v), (i))
949#endif
950
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000951#ifdef WITH_TSC
952/* Use Pentium timestamp counter to mark certain events:
953 inst0 -- beginning of switch statement for opcode dispatch
954 inst1 -- end of switch statement (may be skipped)
955 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000957 (may be skipped)
958 intr1 -- beginning of long interruption
959 intr2 -- end of long interruption
960
961 Many opcodes call out to helper C functions. In some cases, the
962 time in those functions should be counted towards the time for the
963 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
964 calls another Python function; there's no point in charge all the
965 bytecode executed by the called function to the caller.
966
967 It's hard to make a useful judgement statically. In the presence
968 of operator overloading, it's impossible to tell if a call will
969 execute new Python code or not.
970
971 It's a case-by-case judgement. I'll use intr1 for the following
972 cases:
973
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000974 IMPORT_STAR
975 IMPORT_FROM
976 CALL_FUNCTION (and friends)
977
978 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
980 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 READ_TIMESTAMP(inst0);
983 READ_TIMESTAMP(inst1);
984 READ_TIMESTAMP(loop0);
985 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 /* shut up the compiler */
988 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000989#endif
990
Guido van Rossum374a9221991-04-04 10:40:29 +0000991/* Code access macros */
992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993#define INSTR_OFFSET() ((int)(next_instr - first_instr))
994#define NEXTOP() (*next_instr++)
995#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
996#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
997#define JUMPTO(x) (next_instr = first_instr + (x))
998#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000999
Raymond Hettingerf606f872003-03-16 03:11:04 +00001000/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Some opcodes tend to come in pairs thus making it possible to
1002 predict the second code when the first is run. For example,
1003 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1004 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Verifying the prediction costs a single high-speed test of a register
1007 variable against a constant. If the pairing was good, then the
1008 processor's own internal branch predication has a high likelihood of
1009 success, resulting in a nearly zero-overhead transition to the
1010 next opcode. A successful prediction saves a trip through the eval-loop
1011 including its two unpredictable branches, the HAS_ARG test and the
1012 switch-case. Combined with the processor's internal branch prediction,
1013 a successful PREDICT has the effect of making the two opcodes run as if
1014 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001015
Georg Brandl86b2fb92008-07-16 03:43:04 +00001016 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 predictions turned-on and interpret the results as if some opcodes
1018 had been combined or turn-off predictions so that the opcode frequency
1019 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020
1021 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 the CPU to record separate branch prediction information for each
1023 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024
Raymond Hettingerf606f872003-03-16 03:11:04 +00001025*/
1026
Antoine Pitrou042b1282010-08-13 21:15:58 +00001027#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028#define PREDICT(op) if (0) goto PRED_##op
1029#define PREDICTED(op) PRED_##op:
1030#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001031#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1033#define PREDICTED(op) PRED_##op: next_instr++
1034#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001035#endif
1036
Raymond Hettingerf606f872003-03-16 03:11:04 +00001037
Guido van Rossum374a9221991-04-04 10:40:29 +00001038/* Stack manipulation macros */
1039
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040/* The stack can grow at most MAXINT deep, as co_nlocals and
1041 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001042#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1043#define EMPTY() (STACK_LEVEL() == 0)
1044#define TOP() (stack_pointer[-1])
1045#define SECOND() (stack_pointer[-2])
1046#define THIRD() (stack_pointer[-3])
1047#define FOURTH() (stack_pointer[-4])
1048#define PEEK(n) (stack_pointer[-(n)])
1049#define SET_TOP(v) (stack_pointer[-1] = (v))
1050#define SET_SECOND(v) (stack_pointer[-2] = (v))
1051#define SET_THIRD(v) (stack_pointer[-3] = (v))
1052#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1053#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1054#define BASIC_STACKADJ(n) (stack_pointer += n)
1055#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1056#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001057
Guido van Rossum96a42c81992-01-12 02:29:51 +00001058#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001060 lltrace && prtrace(TOP(), "push")); \
1061 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001063 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 lltrace && prtrace(TOP(), "stackadj")); \
1066 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001067#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001068 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1069 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001070#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001071#define PUSH(v) BASIC_PUSH(v)
1072#define POP() BASIC_POP()
1073#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001074#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001075#endif
1076
Guido van Rossum681d79a1995-07-18 14:51:37 +00001077/* Local variable macros */
1078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001080
1081/* The SETLOCAL() macro must not DECREF the local variable in-place and
1082 then store the new value; it must copy the old value to a temporary
1083 value, then store the new value, and then DECREF the temporary value.
1084 This is because it is possible that during the DECREF the frame is
1085 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1086 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001088 GETLOCAL(i) = value; \
1089 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001090
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001091
1092#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 while (STACK_LEVEL() > (b)->b_level) { \
1094 PyObject *v = POP(); \
1095 Py_XDECREF(v); \
1096 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001097
1098#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 { \
1100 PyObject *type, *value, *traceback; \
1101 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1102 while (STACK_LEVEL() > (b)->b_level + 3) { \
1103 value = POP(); \
1104 Py_XDECREF(value); \
1105 } \
1106 type = tstate->exc_type; \
1107 value = tstate->exc_value; \
1108 traceback = tstate->exc_traceback; \
1109 tstate->exc_type = POP(); \
1110 tstate->exc_value = POP(); \
1111 tstate->exc_traceback = POP(); \
1112 Py_XDECREF(type); \
1113 Py_XDECREF(value); \
1114 Py_XDECREF(traceback); \
1115 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001116
Guido van Rossuma027efa1997-05-05 20:56:21 +00001117/* Start of code */
1118
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 /* push frame */
1120 if (Py_EnterRecursiveCall(""))
1121 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001122
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001124
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (tstate->use_tracing) {
1126 if (tstate->c_tracefunc != NULL) {
1127 /* tstate->c_tracefunc, if defined, is a
1128 function that will be called on *every* entry
1129 to a code block. Its return value, if not
1130 None, is a function that will be called at
1131 the start of each executed line of code.
1132 (Actually, the function must return itself
1133 in order to continue tracing.) The trace
1134 functions are called with three arguments:
1135 a pointer to the current frame, a string
1136 indicating why the function is called, and
1137 an argument which depends on the situation.
1138 The global trace function is also called
1139 whenever an exception is detected. */
1140 if (call_trace_protected(tstate->c_tracefunc,
1141 tstate->c_traceobj,
1142 f, PyTrace_CALL, Py_None)) {
1143 /* Trace function raised an error */
1144 goto exit_eval_frame;
1145 }
1146 }
1147 if (tstate->c_profilefunc != NULL) {
1148 /* Similar for c_profilefunc, except it needn't
1149 return itself and isn't called for "line" events */
1150 if (call_trace_protected(tstate->c_profilefunc,
1151 tstate->c_profileobj,
1152 f, PyTrace_CALL, Py_None)) {
1153 /* Profile function raised an error */
1154 goto exit_eval_frame;
1155 }
1156 }
1157 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 co = f->f_code;
1160 names = co->co_names;
1161 consts = co->co_consts;
1162 fastlocals = f->f_localsplus;
1163 freevars = f->f_localsplus + co->co_nlocals;
1164 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1165 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001166
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 f->f_lasti now refers to the index of the last instruction
1168 executed. You might think this was obvious from the name, but
1169 this wasn't always true before 2.3! PyFrame_New now sets
1170 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1171 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1172 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001174 When the PREDICT() macros are enabled, some opcode pairs follow in
1175 direct succession without updating f->f_lasti. A successful
1176 prediction effectively links the two codes together as if they
1177 were a single new opcode; accordingly,f->f_lasti will point to
1178 the first code in the pair (for instance, GET_ITER followed by
1179 FOR_ITER is effectively a single opcode and f->f_lasti will point
1180 at to the beginning of the combined pair.)
1181 */
1182 next_instr = first_instr + f->f_lasti + 1;
1183 stack_pointer = f->f_stacktop;
1184 assert(stack_pointer != NULL);
1185 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001186
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (co->co_flags & CO_GENERATOR && !throwflag) {
1188 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1189 /* We were in an except handler when we left,
1190 restore the exception state which was put aside
1191 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001192 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001194 else
1195 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001197
Tim Peters5ca576e2001-06-18 22:08:13 +00001198#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001200#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 why = WHY_NOT;
1203 err = 0;
1204 x = Py_None; /* Not a reference, just anything non-NULL */
1205 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 if (throwflag) { /* support for generator.throw() */
1208 why = WHY_EXCEPTION;
1209 goto on_error;
1210 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001213#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (inst1 == 0) {
1215 /* Almost surely, the opcode executed a break
1216 or a continue, preventing inst1 from being set
1217 on the way out of the loop.
1218 */
1219 READ_TIMESTAMP(inst1);
1220 loop1 = inst1;
1221 }
1222 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1223 intr0, intr1);
1224 ticked = 0;
1225 inst1 = 0;
1226 intr0 = 0;
1227 intr1 = 0;
1228 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001229#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1231 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001233 /* Do periodic things. Doing this every time through
1234 the loop would add too much overhead, so we do it
1235 only every Nth instruction. We also do it if
1236 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1237 event needs attention (e.g. a signal handler or
1238 async I/O handler); see Py_AddPendingCall() and
1239 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001241 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1242 if (*next_instr == SETUP_FINALLY) {
1243 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001244 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 goto fast_next_opcode;
1246 }
1247 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001248#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001250#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1252 if (Py_MakePendingCalls() < 0) {
1253 why = WHY_EXCEPTION;
1254 goto on_error;
1255 }
1256 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001257#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001258 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 /* Give another thread a chance */
1260 if (PyThreadState_Swap(NULL) != tstate)
1261 Py_FatalError("ceval: tstate mix-up");
1262 drop_gil(tstate);
1263
1264 /* Other threads may run now */
1265
1266 take_gil(tstate);
1267 if (PyThreadState_Swap(tstate) != NULL)
1268 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001269 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001270#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 /* Check for asynchronous exceptions. */
1272 if (tstate->async_exc != NULL) {
1273 x = tstate->async_exc;
1274 tstate->async_exc = NULL;
1275 UNSIGNAL_ASYNC_EXC();
1276 PyErr_SetNone(x);
1277 Py_DECREF(x);
1278 why = WHY_EXCEPTION;
1279 goto on_error;
1280 }
1281 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 fast_next_opcode:
1284 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (_Py_TracingPossible &&
1289 tstate->c_tracefunc != NULL && !tstate->tracing) {
1290 /* see maybe_call_line_trace
1291 for expository comments */
1292 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 err = maybe_call_line_trace(tstate->c_tracefunc,
1295 tstate->c_traceobj,
1296 f, &instr_lb, &instr_ub,
1297 &instr_prev);
1298 /* Reload possibly changed frame fields */
1299 JUMPTO(f->f_lasti);
1300 if (f->f_stacktop != NULL) {
1301 stack_pointer = f->f_stacktop;
1302 f->f_stacktop = NULL;
1303 }
1304 if (err) {
1305 /* trace function raised an exception */
1306 goto on_error;
1307 }
1308 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 opcode = NEXTOP();
1313 oparg = 0; /* allows oparg to be stored in a register because
1314 it doesn't have to be remembered across a full loop */
1315 if (HAS_ARG(opcode))
1316 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001317 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001318#ifdef DYNAMIC_EXECUTION_PROFILE
1319#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 dxpairs[lastopcode][opcode]++;
1321 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001322#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001324#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001325
Guido van Rossum96a42c81992-01-12 02:29:51 +00001326#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 if (lltrace) {
1330 if (HAS_ARG(opcode)) {
1331 printf("%d: %d, %d\n",
1332 f->f_lasti, opcode, oparg);
1333 }
1334 else {
1335 printf("%d: %d\n",
1336 f->f_lasti, opcode);
1337 }
1338 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001339#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 /* Main switch on opcode */
1342 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 /* BEWARE!
1347 It is essential that any operation that fails sets either
1348 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1349 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001351 TARGET(NOP)
1352 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 TARGET(LOAD_FAST)
1355 x = GETLOCAL(oparg);
1356 if (x != NULL) {
1357 Py_INCREF(x);
1358 PUSH(x);
1359 FAST_DISPATCH();
1360 }
1361 format_exc_check_arg(PyExc_UnboundLocalError,
1362 UNBOUNDLOCAL_ERROR_MSG,
1363 PyTuple_GetItem(co->co_varnames, oparg));
1364 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 TARGET(LOAD_CONST)
1367 x = GETITEM(consts, oparg);
1368 Py_INCREF(x);
1369 PUSH(x);
1370 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 PREDICTED_WITH_ARG(STORE_FAST);
1373 TARGET(STORE_FAST)
1374 v = POP();
1375 SETLOCAL(oparg, v);
1376 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 TARGET(POP_TOP)
1379 v = POP();
1380 Py_DECREF(v);
1381 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 TARGET(ROT_TWO)
1384 v = TOP();
1385 w = SECOND();
1386 SET_TOP(w);
1387 SET_SECOND(v);
1388 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 TARGET(ROT_THREE)
1391 v = TOP();
1392 w = SECOND();
1393 x = THIRD();
1394 SET_TOP(w);
1395 SET_SECOND(x);
1396 SET_THIRD(v);
1397 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 TARGET(DUP_TOP)
1400 v = TOP();
1401 Py_INCREF(v);
1402 PUSH(v);
1403 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001405 TARGET(DUP_TOP_TWO)
1406 x = TOP();
1407 Py_INCREF(x);
1408 w = SECOND();
1409 Py_INCREF(w);
1410 STACKADJ(2);
1411 SET_TOP(x);
1412 SET_SECOND(w);
1413 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 TARGET(UNARY_POSITIVE)
1416 v = TOP();
1417 x = PyNumber_Positive(v);
1418 Py_DECREF(v);
1419 SET_TOP(x);
1420 if (x != NULL) DISPATCH();
1421 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 TARGET(UNARY_NEGATIVE)
1424 v = TOP();
1425 x = PyNumber_Negative(v);
1426 Py_DECREF(v);
1427 SET_TOP(x);
1428 if (x != NULL) DISPATCH();
1429 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 TARGET(UNARY_NOT)
1432 v = TOP();
1433 err = PyObject_IsTrue(v);
1434 Py_DECREF(v);
1435 if (err == 0) {
1436 Py_INCREF(Py_True);
1437 SET_TOP(Py_True);
1438 DISPATCH();
1439 }
1440 else if (err > 0) {
1441 Py_INCREF(Py_False);
1442 SET_TOP(Py_False);
1443 err = 0;
1444 DISPATCH();
1445 }
1446 STACKADJ(-1);
1447 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 TARGET(UNARY_INVERT)
1450 v = TOP();
1451 x = PyNumber_Invert(v);
1452 Py_DECREF(v);
1453 SET_TOP(x);
1454 if (x != NULL) DISPATCH();
1455 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 TARGET(BINARY_POWER)
1458 w = POP();
1459 v = TOP();
1460 x = PyNumber_Power(v, w, Py_None);
1461 Py_DECREF(v);
1462 Py_DECREF(w);
1463 SET_TOP(x);
1464 if (x != NULL) DISPATCH();
1465 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 TARGET(BINARY_MULTIPLY)
1468 w = POP();
1469 v = TOP();
1470 x = PyNumber_Multiply(v, w);
1471 Py_DECREF(v);
1472 Py_DECREF(w);
1473 SET_TOP(x);
1474 if (x != NULL) DISPATCH();
1475 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001477 TARGET(BINARY_TRUE_DIVIDE)
1478 w = POP();
1479 v = TOP();
1480 x = PyNumber_TrueDivide(v, w);
1481 Py_DECREF(v);
1482 Py_DECREF(w);
1483 SET_TOP(x);
1484 if (x != NULL) DISPATCH();
1485 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 TARGET(BINARY_FLOOR_DIVIDE)
1488 w = POP();
1489 v = TOP();
1490 x = PyNumber_FloorDivide(v, w);
1491 Py_DECREF(v);
1492 Py_DECREF(w);
1493 SET_TOP(x);
1494 if (x != NULL) DISPATCH();
1495 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 TARGET(BINARY_MODULO)
1498 w = POP();
1499 v = TOP();
1500 if (PyUnicode_CheckExact(v))
1501 x = PyUnicode_Format(v, w);
1502 else
1503 x = PyNumber_Remainder(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_ADD)
1511 w = POP();
1512 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001513 if (PyUnicode_CheckExact(v) &&
1514 PyUnicode_CheckExact(w)) {
1515 x = unicode_concatenate(v, w, f, next_instr);
1516 /* unicode_concatenate consumed the ref to v */
1517 goto skip_decref_vx;
1518 }
1519 else {
1520 x = PyNumber_Add(v, w);
1521 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001523 skip_decref_vx:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 Py_DECREF(w);
1525 SET_TOP(x);
1526 if (x != NULL) DISPATCH();
1527 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 TARGET(BINARY_SUBTRACT)
1530 w = POP();
1531 v = TOP();
1532 x = PyNumber_Subtract(v, w);
1533 Py_DECREF(v);
1534 Py_DECREF(w);
1535 SET_TOP(x);
1536 if (x != NULL) DISPATCH();
1537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 TARGET(BINARY_SUBSCR)
1540 w = POP();
1541 v = TOP();
1542 x = PyObject_GetItem(v, w);
1543 Py_DECREF(v);
1544 Py_DECREF(w);
1545 SET_TOP(x);
1546 if (x != NULL) DISPATCH();
1547 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 TARGET(BINARY_LSHIFT)
1550 w = POP();
1551 v = TOP();
1552 x = PyNumber_Lshift(v, w);
1553 Py_DECREF(v);
1554 Py_DECREF(w);
1555 SET_TOP(x);
1556 if (x != NULL) DISPATCH();
1557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 TARGET(BINARY_RSHIFT)
1560 w = POP();
1561 v = TOP();
1562 x = PyNumber_Rshift(v, w);
1563 Py_DECREF(v);
1564 Py_DECREF(w);
1565 SET_TOP(x);
1566 if (x != NULL) DISPATCH();
1567 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 TARGET(BINARY_AND)
1570 w = POP();
1571 v = TOP();
1572 x = PyNumber_And(v, w);
1573 Py_DECREF(v);
1574 Py_DECREF(w);
1575 SET_TOP(x);
1576 if (x != NULL) DISPATCH();
1577 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001578
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 TARGET(BINARY_XOR)
1580 w = POP();
1581 v = TOP();
1582 x = PyNumber_Xor(v, w);
1583 Py_DECREF(v);
1584 Py_DECREF(w);
1585 SET_TOP(x);
1586 if (x != NULL) DISPATCH();
1587 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 TARGET(BINARY_OR)
1590 w = POP();
1591 v = TOP();
1592 x = PyNumber_Or(v, w);
1593 Py_DECREF(v);
1594 Py_DECREF(w);
1595 SET_TOP(x);
1596 if (x != NULL) DISPATCH();
1597 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001598
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 TARGET(LIST_APPEND)
1600 w = POP();
1601 v = PEEK(oparg);
1602 err = PyList_Append(v, w);
1603 Py_DECREF(w);
1604 if (err == 0) {
1605 PREDICT(JUMP_ABSOLUTE);
1606 DISPATCH();
1607 }
1608 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 TARGET(SET_ADD)
1611 w = POP();
1612 v = stack_pointer[-oparg];
1613 err = PySet_Add(v, w);
1614 Py_DECREF(w);
1615 if (err == 0) {
1616 PREDICT(JUMP_ABSOLUTE);
1617 DISPATCH();
1618 }
1619 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 TARGET(INPLACE_POWER)
1622 w = POP();
1623 v = TOP();
1624 x = PyNumber_InPlacePower(v, w, Py_None);
1625 Py_DECREF(v);
1626 Py_DECREF(w);
1627 SET_TOP(x);
1628 if (x != NULL) DISPATCH();
1629 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 TARGET(INPLACE_MULTIPLY)
1632 w = POP();
1633 v = TOP();
1634 x = PyNumber_InPlaceMultiply(v, w);
1635 Py_DECREF(v);
1636 Py_DECREF(w);
1637 SET_TOP(x);
1638 if (x != NULL) DISPATCH();
1639 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 TARGET(INPLACE_TRUE_DIVIDE)
1642 w = POP();
1643 v = TOP();
1644 x = PyNumber_InPlaceTrueDivide(v, w);
1645 Py_DECREF(v);
1646 Py_DECREF(w);
1647 SET_TOP(x);
1648 if (x != NULL) DISPATCH();
1649 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 TARGET(INPLACE_FLOOR_DIVIDE)
1652 w = POP();
1653 v = TOP();
1654 x = PyNumber_InPlaceFloorDivide(v, w);
1655 Py_DECREF(v);
1656 Py_DECREF(w);
1657 SET_TOP(x);
1658 if (x != NULL) DISPATCH();
1659 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001660
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 TARGET(INPLACE_MODULO)
1662 w = POP();
1663 v = TOP();
1664 x = PyNumber_InPlaceRemainder(v, w);
1665 Py_DECREF(v);
1666 Py_DECREF(w);
1667 SET_TOP(x);
1668 if (x != NULL) DISPATCH();
1669 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 TARGET(INPLACE_ADD)
1672 w = POP();
1673 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001674 if (PyUnicode_CheckExact(v) &&
1675 PyUnicode_CheckExact(w)) {
1676 x = unicode_concatenate(v, w, f, next_instr);
1677 /* unicode_concatenate consumed the ref to v */
1678 goto skip_decref_v;
1679 }
1680 else {
1681 x = PyNumber_InPlaceAdd(v, w);
1682 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001684 skip_decref_v:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 Py_DECREF(w);
1686 SET_TOP(x);
1687 if (x != NULL) DISPATCH();
1688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 TARGET(INPLACE_SUBTRACT)
1691 w = POP();
1692 v = TOP();
1693 x = PyNumber_InPlaceSubtract(v, w);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
1696 SET_TOP(x);
1697 if (x != NULL) DISPATCH();
1698 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 TARGET(INPLACE_LSHIFT)
1701 w = POP();
1702 v = TOP();
1703 x = PyNumber_InPlaceLshift(v, w);
1704 Py_DECREF(v);
1705 Py_DECREF(w);
1706 SET_TOP(x);
1707 if (x != NULL) DISPATCH();
1708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 TARGET(INPLACE_RSHIFT)
1711 w = POP();
1712 v = TOP();
1713 x = PyNumber_InPlaceRshift(v, w);
1714 Py_DECREF(v);
1715 Py_DECREF(w);
1716 SET_TOP(x);
1717 if (x != NULL) DISPATCH();
1718 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 TARGET(INPLACE_AND)
1721 w = POP();
1722 v = TOP();
1723 x = PyNumber_InPlaceAnd(v, w);
1724 Py_DECREF(v);
1725 Py_DECREF(w);
1726 SET_TOP(x);
1727 if (x != NULL) DISPATCH();
1728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 TARGET(INPLACE_XOR)
1731 w = POP();
1732 v = TOP();
1733 x = PyNumber_InPlaceXor(v, w);
1734 Py_DECREF(v);
1735 Py_DECREF(w);
1736 SET_TOP(x);
1737 if (x != NULL) DISPATCH();
1738 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 TARGET(INPLACE_OR)
1741 w = POP();
1742 v = TOP();
1743 x = PyNumber_InPlaceOr(v, w);
1744 Py_DECREF(v);
1745 Py_DECREF(w);
1746 SET_TOP(x);
1747 if (x != NULL) DISPATCH();
1748 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750 TARGET(STORE_SUBSCR)
1751 w = TOP();
1752 v = SECOND();
1753 u = THIRD();
1754 STACKADJ(-3);
1755 /* v[w] = u */
1756 err = PyObject_SetItem(v, w, u);
1757 Py_DECREF(u);
1758 Py_DECREF(v);
1759 Py_DECREF(w);
1760 if (err == 0) DISPATCH();
1761 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 TARGET(DELETE_SUBSCR)
1764 w = TOP();
1765 v = SECOND();
1766 STACKADJ(-2);
1767 /* del v[w] */
1768 err = PyObject_DelItem(v, w);
1769 Py_DECREF(v);
1770 Py_DECREF(w);
1771 if (err == 0) DISPATCH();
1772 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 TARGET(PRINT_EXPR)
1775 v = POP();
1776 w = PySys_GetObject("displayhook");
1777 if (w == NULL) {
1778 PyErr_SetString(PyExc_RuntimeError,
1779 "lost sys.displayhook");
1780 err = -1;
1781 x = NULL;
1782 }
1783 if (err == 0) {
1784 x = PyTuple_Pack(1, v);
1785 if (x == NULL)
1786 err = -1;
1787 }
1788 if (err == 0) {
1789 w = PyEval_CallObject(w, x);
1790 Py_XDECREF(w);
1791 if (w == NULL)
1792 err = -1;
1793 }
1794 Py_DECREF(v);
1795 Py_XDECREF(x);
1796 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001797
Thomas Wouters434d0822000-08-24 20:11:32 +00001798#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001800#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 TARGET(RAISE_VARARGS)
1802 v = w = NULL;
1803 switch (oparg) {
1804 case 2:
1805 v = POP(); /* cause */
1806 case 1:
1807 w = POP(); /* exc */
1808 case 0: /* Fallthrough */
1809 why = do_raise(w, v);
1810 break;
1811 default:
1812 PyErr_SetString(PyExc_SystemError,
1813 "bad RAISE_VARARGS oparg");
1814 why = WHY_EXCEPTION;
1815 break;
1816 }
1817 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 TARGET(STORE_LOCALS)
1820 x = POP();
1821 v = f->f_locals;
1822 Py_XDECREF(v);
1823 f->f_locals = x;
1824 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 TARGET(RETURN_VALUE)
1827 retval = POP();
1828 why = WHY_RETURN;
1829 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001830
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001831 TARGET(YIELD_FROM)
1832 u = POP();
1833 x = PyObject_GetIter(u);
1834 Py_DECREF(u);
1835 if (x == NULL)
1836 break;
1837 /* x is now the iterator, make the first next() call */
1838 retval = (*Py_TYPE(x)->tp_iternext)(x);
1839 if (!retval) {
Amaury Forgeot d'Arc0a239e92012-01-13 21:08:49 +01001840 PyObject *et, *ev, *tb;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001841 /* iter may be exhausted */
1842 Py_CLEAR(x);
Benjamin Peterson0296a562012-01-13 14:54:31 -05001843 if (PyErr_Occurred() &&
1844 !PyErr_ExceptionMatches(PyExc_StopIteration)) {
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001845 /* some other exception */
1846 break;
1847 }
1848 /* try to get return value from exception */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001849 PyErr_Fetch(&et, &ev, &tb);
1850 Py_XDECREF(et);
1851 Py_XDECREF(tb);
1852 /* u is return value */
1853 u = NULL;
1854 if (ev) {
1855 u = PyObject_GetAttrString(ev, "value");
1856 Py_DECREF(ev);
1857 if (u == NULL) {
1858 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) {
1859 /* some other exception */
1860 break;
1861 }
1862 PyErr_Clear();
1863 }
1864 }
1865 if (u == NULL) {
1866 u = Py_None;
1867 Py_INCREF(u);
1868 }
1869 PUSH(u);
1870 continue;
1871 }
1872 /* x is iterator, retval is value to be yielded */
1873 f->f_yieldfrom = x;
1874 f->f_stacktop = stack_pointer;
1875 why = WHY_YIELD;
1876 goto fast_yield;
1877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878 TARGET(YIELD_VALUE)
1879 retval = POP();
1880 f->f_stacktop = stack_pointer;
1881 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 TARGET(POP_EXCEPT)
1885 {
1886 PyTryBlock *b = PyFrame_BlockPop(f);
1887 if (b->b_type != EXCEPT_HANDLER) {
1888 PyErr_SetString(PyExc_SystemError,
1889 "popped block is not an except handler");
1890 why = WHY_EXCEPTION;
1891 break;
1892 }
1893 UNWIND_EXCEPT_HANDLER(b);
1894 }
1895 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001896
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 TARGET(POP_BLOCK)
1898 {
1899 PyTryBlock *b = PyFrame_BlockPop(f);
1900 UNWIND_BLOCK(b);
1901 }
1902 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 PREDICTED(END_FINALLY);
1905 TARGET(END_FINALLY)
1906 v = POP();
1907 if (PyLong_Check(v)) {
1908 why = (enum why_code) PyLong_AS_LONG(v);
1909 assert(why != WHY_YIELD);
1910 if (why == WHY_RETURN ||
1911 why == WHY_CONTINUE)
1912 retval = POP();
1913 if (why == WHY_SILENCED) {
1914 /* An exception was silenced by 'with', we must
1915 manually unwind the EXCEPT_HANDLER block which was
1916 created when the exception was caught, otherwise
1917 the stack will be in an inconsistent state. */
1918 PyTryBlock *b = PyFrame_BlockPop(f);
1919 assert(b->b_type == EXCEPT_HANDLER);
1920 UNWIND_EXCEPT_HANDLER(b);
1921 why = WHY_NOT;
1922 }
1923 }
1924 else if (PyExceptionClass_Check(v)) {
1925 w = POP();
1926 u = POP();
1927 PyErr_Restore(v, w, u);
1928 why = WHY_RERAISE;
1929 break;
1930 }
1931 else if (v != Py_None) {
1932 PyErr_SetString(PyExc_SystemError,
1933 "'finally' pops bad exception");
1934 why = WHY_EXCEPTION;
1935 }
1936 Py_DECREF(v);
1937 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 TARGET(LOAD_BUILD_CLASS)
1940 x = PyDict_GetItemString(f->f_builtins,
1941 "__build_class__");
1942 if (x == NULL) {
1943 PyErr_SetString(PyExc_ImportError,
1944 "__build_class__ not found");
1945 break;
1946 }
1947 Py_INCREF(x);
1948 PUSH(x);
1949 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 TARGET(STORE_NAME)
1952 w = GETITEM(names, oparg);
1953 v = POP();
1954 if ((x = f->f_locals) != NULL) {
1955 if (PyDict_CheckExact(x))
1956 err = PyDict_SetItem(x, w, v);
1957 else
1958 err = PyObject_SetItem(x, w, v);
1959 Py_DECREF(v);
1960 if (err == 0) DISPATCH();
1961 break;
1962 }
1963 PyErr_Format(PyExc_SystemError,
1964 "no locals found when storing %R", w);
1965 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 TARGET(DELETE_NAME)
1968 w = GETITEM(names, oparg);
1969 if ((x = f->f_locals) != NULL) {
1970 if ((err = PyObject_DelItem(x, w)) != 0)
1971 format_exc_check_arg(PyExc_NameError,
1972 NAME_ERROR_MSG,
1973 w);
1974 break;
1975 }
1976 PyErr_Format(PyExc_SystemError,
1977 "no locals when deleting %R", w);
1978 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001980 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1981 TARGET(UNPACK_SEQUENCE)
1982 v = POP();
1983 if (PyTuple_CheckExact(v) &&
1984 PyTuple_GET_SIZE(v) == oparg) {
1985 PyObject **items = \
1986 ((PyTupleObject *)v)->ob_item;
1987 while (oparg--) {
1988 w = items[oparg];
1989 Py_INCREF(w);
1990 PUSH(w);
1991 }
1992 Py_DECREF(v);
1993 DISPATCH();
1994 } else if (PyList_CheckExact(v) &&
1995 PyList_GET_SIZE(v) == oparg) {
1996 PyObject **items = \
1997 ((PyListObject *)v)->ob_item;
1998 while (oparg--) {
1999 w = items[oparg];
2000 Py_INCREF(w);
2001 PUSH(w);
2002 }
2003 } else if (unpack_iterable(v, oparg, -1,
2004 stack_pointer + oparg)) {
2005 STACKADJ(oparg);
2006 } else {
2007 /* unpack_iterable() raised an exception */
2008 why = WHY_EXCEPTION;
2009 }
2010 Py_DECREF(v);
2011 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002013 TARGET(UNPACK_EX)
2014 {
2015 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2016 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002018 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2019 stack_pointer + totalargs)) {
2020 stack_pointer += totalargs;
2021 } else {
2022 why = WHY_EXCEPTION;
2023 }
2024 Py_DECREF(v);
2025 break;
2026 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 TARGET(STORE_ATTR)
2029 w = GETITEM(names, oparg);
2030 v = TOP();
2031 u = SECOND();
2032 STACKADJ(-2);
2033 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2034 Py_DECREF(v);
2035 Py_DECREF(u);
2036 if (err == 0) DISPATCH();
2037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 TARGET(DELETE_ATTR)
2040 w = GETITEM(names, oparg);
2041 v = POP();
2042 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2043 /* del v.w */
2044 Py_DECREF(v);
2045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002047 TARGET(STORE_GLOBAL)
2048 w = GETITEM(names, oparg);
2049 v = POP();
2050 err = PyDict_SetItem(f->f_globals, w, v);
2051 Py_DECREF(v);
2052 if (err == 0) DISPATCH();
2053 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 TARGET(DELETE_GLOBAL)
2056 w = GETITEM(names, oparg);
2057 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2058 format_exc_check_arg(
2059 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2060 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 TARGET(LOAD_NAME)
2063 w = GETITEM(names, oparg);
2064 if ((v = f->f_locals) == NULL) {
2065 PyErr_Format(PyExc_SystemError,
2066 "no locals when loading %R", w);
2067 why = WHY_EXCEPTION;
2068 break;
2069 }
2070 if (PyDict_CheckExact(v)) {
2071 x = PyDict_GetItem(v, w);
2072 Py_XINCREF(x);
2073 }
2074 else {
2075 x = PyObject_GetItem(v, w);
2076 if (x == NULL && PyErr_Occurred()) {
2077 if (!PyErr_ExceptionMatches(
2078 PyExc_KeyError))
2079 break;
2080 PyErr_Clear();
2081 }
2082 }
2083 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002084 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002086 x = PyDict_GetItem(f->f_builtins, w);
2087 if (x == NULL) {
2088 format_exc_check_arg(
2089 PyExc_NameError,
2090 NAME_ERROR_MSG, w);
2091 break;
2092 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002093 }
2094 Py_INCREF(x);
2095 }
2096 PUSH(x);
2097 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 TARGET(LOAD_GLOBAL)
2100 w = GETITEM(names, oparg);
2101 if (PyUnicode_CheckExact(w)) {
2102 /* Inline the PyDict_GetItem() calls.
2103 WARNING: this is an extreme speed hack.
2104 Do not try this at home. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002105 Py_hash_t hash = ((PyASCIIObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 if (hash != -1) {
2107 PyDictObject *d;
2108 PyDictEntry *e;
2109 d = (PyDictObject *)(f->f_globals);
2110 e = d->ma_lookup(d, w, hash);
2111 if (e == NULL) {
2112 x = NULL;
2113 break;
2114 }
2115 x = e->me_value;
2116 if (x != NULL) {
2117 Py_INCREF(x);
2118 PUSH(x);
2119 DISPATCH();
2120 }
2121 d = (PyDictObject *)(f->f_builtins);
2122 e = d->ma_lookup(d, w, hash);
2123 if (e == NULL) {
2124 x = NULL;
2125 break;
2126 }
2127 x = e->me_value;
2128 if (x != NULL) {
2129 Py_INCREF(x);
2130 PUSH(x);
2131 DISPATCH();
2132 }
2133 goto load_global_error;
2134 }
2135 }
2136 /* This is the un-inlined version of the code above */
2137 x = PyDict_GetItem(f->f_globals, w);
2138 if (x == NULL) {
2139 x = PyDict_GetItem(f->f_builtins, w);
2140 if (x == NULL) {
2141 load_global_error:
2142 format_exc_check_arg(
2143 PyExc_NameError,
2144 GLOBAL_NAME_ERROR_MSG, w);
2145 break;
2146 }
2147 }
2148 Py_INCREF(x);
2149 PUSH(x);
2150 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002152 TARGET(DELETE_FAST)
2153 x = GETLOCAL(oparg);
2154 if (x != NULL) {
2155 SETLOCAL(oparg, NULL);
2156 DISPATCH();
2157 }
2158 format_exc_check_arg(
2159 PyExc_UnboundLocalError,
2160 UNBOUNDLOCAL_ERROR_MSG,
2161 PyTuple_GetItem(co->co_varnames, oparg)
2162 );
2163 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002164
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002165 TARGET(DELETE_DEREF)
2166 x = freevars[oparg];
2167 if (PyCell_GET(x) != NULL) {
2168 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002169 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002170 }
2171 err = -1;
2172 format_exc_unbound(co, oparg);
2173 break;
2174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 TARGET(LOAD_CLOSURE)
2176 x = freevars[oparg];
2177 Py_INCREF(x);
2178 PUSH(x);
2179 if (x != NULL) DISPATCH();
2180 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 TARGET(LOAD_DEREF)
2183 x = freevars[oparg];
2184 w = PyCell_Get(x);
2185 if (w != NULL) {
2186 PUSH(w);
2187 DISPATCH();
2188 }
2189 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002190 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002191 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 TARGET(STORE_DEREF)
2194 w = POP();
2195 x = freevars[oparg];
2196 PyCell_Set(x, w);
2197 Py_DECREF(w);
2198 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 TARGET(BUILD_TUPLE)
2201 x = PyTuple_New(oparg);
2202 if (x != NULL) {
2203 for (; --oparg >= 0;) {
2204 w = POP();
2205 PyTuple_SET_ITEM(x, oparg, w);
2206 }
2207 PUSH(x);
2208 DISPATCH();
2209 }
2210 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002211
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 TARGET(BUILD_LIST)
2213 x = PyList_New(oparg);
2214 if (x != NULL) {
2215 for (; --oparg >= 0;) {
2216 w = POP();
2217 PyList_SET_ITEM(x, oparg, w);
2218 }
2219 PUSH(x);
2220 DISPATCH();
2221 }
2222 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 TARGET(BUILD_SET)
2225 x = PySet_New(NULL);
2226 if (x != NULL) {
2227 for (; --oparg >= 0;) {
2228 w = POP();
2229 if (err == 0)
2230 err = PySet_Add(x, w);
2231 Py_DECREF(w);
2232 }
2233 if (err != 0) {
2234 Py_DECREF(x);
2235 break;
2236 }
2237 PUSH(x);
2238 DISPATCH();
2239 }
2240 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 TARGET(BUILD_MAP)
2243 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2244 PUSH(x);
2245 if (x != NULL) DISPATCH();
2246 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 TARGET(STORE_MAP)
2249 w = TOP(); /* key */
2250 u = SECOND(); /* value */
2251 v = THIRD(); /* dict */
2252 STACKADJ(-2);
2253 assert (PyDict_CheckExact(v));
2254 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2255 Py_DECREF(u);
2256 Py_DECREF(w);
2257 if (err == 0) DISPATCH();
2258 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002260 TARGET(MAP_ADD)
2261 w = TOP(); /* key */
2262 u = SECOND(); /* value */
2263 STACKADJ(-2);
2264 v = stack_pointer[-oparg]; /* dict */
2265 assert (PyDict_CheckExact(v));
2266 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2267 Py_DECREF(u);
2268 Py_DECREF(w);
2269 if (err == 0) {
2270 PREDICT(JUMP_ABSOLUTE);
2271 DISPATCH();
2272 }
2273 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002275 TARGET(LOAD_ATTR)
2276 w = GETITEM(names, oparg);
2277 v = TOP();
2278 x = PyObject_GetAttr(v, w);
2279 Py_DECREF(v);
2280 SET_TOP(x);
2281 if (x != NULL) DISPATCH();
2282 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 TARGET(COMPARE_OP)
2285 w = POP();
2286 v = TOP();
2287 x = cmp_outcome(oparg, v, w);
2288 Py_DECREF(v);
2289 Py_DECREF(w);
2290 SET_TOP(x);
2291 if (x == NULL) break;
2292 PREDICT(POP_JUMP_IF_FALSE);
2293 PREDICT(POP_JUMP_IF_TRUE);
2294 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002296 TARGET(IMPORT_NAME)
2297 w = GETITEM(names, oparg);
2298 x = PyDict_GetItemString(f->f_builtins, "__import__");
2299 if (x == NULL) {
2300 PyErr_SetString(PyExc_ImportError,
2301 "__import__ not found");
2302 break;
2303 }
2304 Py_INCREF(x);
2305 v = POP();
2306 u = TOP();
2307 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2308 w = PyTuple_Pack(5,
2309 w,
2310 f->f_globals,
2311 f->f_locals == NULL ?
2312 Py_None : f->f_locals,
2313 v,
2314 u);
2315 else
2316 w = PyTuple_Pack(4,
2317 w,
2318 f->f_globals,
2319 f->f_locals == NULL ?
2320 Py_None : f->f_locals,
2321 v);
2322 Py_DECREF(v);
2323 Py_DECREF(u);
2324 if (w == NULL) {
2325 u = POP();
2326 Py_DECREF(x);
2327 x = NULL;
2328 break;
2329 }
2330 READ_TIMESTAMP(intr0);
2331 v = x;
2332 x = PyEval_CallObject(v, w);
2333 Py_DECREF(v);
2334 READ_TIMESTAMP(intr1);
2335 Py_DECREF(w);
2336 SET_TOP(x);
2337 if (x != NULL) DISPATCH();
2338 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002339
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 TARGET(IMPORT_STAR)
2341 v = POP();
2342 PyFrame_FastToLocals(f);
2343 if ((x = f->f_locals) == NULL) {
2344 PyErr_SetString(PyExc_SystemError,
2345 "no locals found during 'import *'");
2346 break;
2347 }
2348 READ_TIMESTAMP(intr0);
2349 err = import_all_from(x, v);
2350 READ_TIMESTAMP(intr1);
2351 PyFrame_LocalsToFast(f, 0);
2352 Py_DECREF(v);
2353 if (err == 0) DISPATCH();
2354 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002356 TARGET(IMPORT_FROM)
2357 w = GETITEM(names, oparg);
2358 v = TOP();
2359 READ_TIMESTAMP(intr0);
2360 x = import_from(v, w);
2361 READ_TIMESTAMP(intr1);
2362 PUSH(x);
2363 if (x != NULL) DISPATCH();
2364 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 TARGET(JUMP_FORWARD)
2367 JUMPBY(oparg);
2368 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002370 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2371 TARGET(POP_JUMP_IF_FALSE)
2372 w = POP();
2373 if (w == Py_True) {
2374 Py_DECREF(w);
2375 FAST_DISPATCH();
2376 }
2377 if (w == Py_False) {
2378 Py_DECREF(w);
2379 JUMPTO(oparg);
2380 FAST_DISPATCH();
2381 }
2382 err = PyObject_IsTrue(w);
2383 Py_DECREF(w);
2384 if (err > 0)
2385 err = 0;
2386 else if (err == 0)
2387 JUMPTO(oparg);
2388 else
2389 break;
2390 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002392 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2393 TARGET(POP_JUMP_IF_TRUE)
2394 w = POP();
2395 if (w == Py_False) {
2396 Py_DECREF(w);
2397 FAST_DISPATCH();
2398 }
2399 if (w == Py_True) {
2400 Py_DECREF(w);
2401 JUMPTO(oparg);
2402 FAST_DISPATCH();
2403 }
2404 err = PyObject_IsTrue(w);
2405 Py_DECREF(w);
2406 if (err > 0) {
2407 err = 0;
2408 JUMPTO(oparg);
2409 }
2410 else if (err == 0)
2411 ;
2412 else
2413 break;
2414 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002416 TARGET(JUMP_IF_FALSE_OR_POP)
2417 w = TOP();
2418 if (w == Py_True) {
2419 STACKADJ(-1);
2420 Py_DECREF(w);
2421 FAST_DISPATCH();
2422 }
2423 if (w == Py_False) {
2424 JUMPTO(oparg);
2425 FAST_DISPATCH();
2426 }
2427 err = PyObject_IsTrue(w);
2428 if (err > 0) {
2429 STACKADJ(-1);
2430 Py_DECREF(w);
2431 err = 0;
2432 }
2433 else if (err == 0)
2434 JUMPTO(oparg);
2435 else
2436 break;
2437 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 TARGET(JUMP_IF_TRUE_OR_POP)
2440 w = TOP();
2441 if (w == Py_False) {
2442 STACKADJ(-1);
2443 Py_DECREF(w);
2444 FAST_DISPATCH();
2445 }
2446 if (w == Py_True) {
2447 JUMPTO(oparg);
2448 FAST_DISPATCH();
2449 }
2450 err = PyObject_IsTrue(w);
2451 if (err > 0) {
2452 err = 0;
2453 JUMPTO(oparg);
2454 }
2455 else if (err == 0) {
2456 STACKADJ(-1);
2457 Py_DECREF(w);
2458 }
2459 else
2460 break;
2461 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2464 TARGET(JUMP_ABSOLUTE)
2465 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002466#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 /* Enabling this path speeds-up all while and for-loops by bypassing
2468 the per-loop checks for signals. By default, this should be turned-off
2469 because it prevents detection of a control-break in tight loops like
2470 "while 1: pass". Compile with this option turned-on when you need
2471 the speed-up and do not need break checking inside tight loops (ones
2472 that contain only instructions ending with FAST_DISPATCH).
2473 */
2474 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002475#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002476 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002477#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002479 TARGET(GET_ITER)
2480 /* before: [obj]; after [getiter(obj)] */
2481 v = TOP();
2482 x = PyObject_GetIter(v);
2483 Py_DECREF(v);
2484 if (x != NULL) {
2485 SET_TOP(x);
2486 PREDICT(FOR_ITER);
2487 DISPATCH();
2488 }
2489 STACKADJ(-1);
2490 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 PREDICTED_WITH_ARG(FOR_ITER);
2493 TARGET(FOR_ITER)
2494 /* before: [iter]; after: [iter, iter()] *or* [] */
2495 v = TOP();
2496 x = (*v->ob_type->tp_iternext)(v);
2497 if (x != NULL) {
2498 PUSH(x);
2499 PREDICT(STORE_FAST);
2500 PREDICT(UNPACK_SEQUENCE);
2501 DISPATCH();
2502 }
2503 if (PyErr_Occurred()) {
2504 if (!PyErr_ExceptionMatches(
2505 PyExc_StopIteration))
2506 break;
2507 PyErr_Clear();
2508 }
2509 /* iterator ended normally */
2510 x = v = POP();
2511 Py_DECREF(v);
2512 JUMPBY(oparg);
2513 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002514
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002515 TARGET(BREAK_LOOP)
2516 why = WHY_BREAK;
2517 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 TARGET(CONTINUE_LOOP)
2520 retval = PyLong_FromLong(oparg);
2521 if (!retval) {
2522 x = NULL;
2523 break;
2524 }
2525 why = WHY_CONTINUE;
2526 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002528 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2529 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2530 TARGET(SETUP_FINALLY)
2531 _setup_finally:
2532 /* NOTE: If you add any new block-setup opcodes that
2533 are not try/except/finally handlers, you may need
2534 to update the PyGen_NeedsFinalizing() function.
2535 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002537 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2538 STACK_LEVEL());
2539 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002541 TARGET(SETUP_WITH)
2542 {
2543 static PyObject *exit, *enter;
2544 w = TOP();
2545 x = special_lookup(w, "__exit__", &exit);
2546 if (!x)
2547 break;
2548 SET_TOP(x);
2549 u = special_lookup(w, "__enter__", &enter);
2550 Py_DECREF(w);
2551 if (!u) {
2552 x = NULL;
2553 break;
2554 }
2555 x = PyObject_CallFunctionObjArgs(u, NULL);
2556 Py_DECREF(u);
2557 if (!x)
2558 break;
2559 /* Setup the finally block before pushing the result
2560 of __enter__ on the stack. */
2561 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2562 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002564 PUSH(x);
2565 DISPATCH();
2566 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002568 TARGET(WITH_CLEANUP)
2569 {
2570 /* At the top of the stack are 1-3 values indicating
2571 how/why we entered the finally clause:
2572 - TOP = None
2573 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2574 - TOP = WHY_*; no retval below it
2575 - (TOP, SECOND, THIRD) = exc_info()
2576 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2577 Below them is EXIT, the context.__exit__ bound method.
2578 In the last case, we must call
2579 EXIT(TOP, SECOND, THIRD)
2580 otherwise we must call
2581 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002582
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002583 In the first two cases, we remove EXIT from the
2584 stack, leaving the rest in the same order. In the
2585 third case, we shift the bottom 3 values of the
2586 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002588 In addition, if the stack represents an exception,
2589 *and* the function call returns a 'true' value, we
2590 push WHY_SILENCED onto the stack. END_FINALLY will
2591 then not re-raise the exception. (But non-local
2592 gotos should still be resumed.)
2593 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002595 PyObject *exit_func;
2596 u = TOP();
2597 if (u == Py_None) {
2598 (void)POP();
2599 exit_func = TOP();
2600 SET_TOP(u);
2601 v = w = Py_None;
2602 }
2603 else if (PyLong_Check(u)) {
2604 (void)POP();
2605 switch(PyLong_AsLong(u)) {
2606 case WHY_RETURN:
2607 case WHY_CONTINUE:
2608 /* Retval in TOP. */
2609 exit_func = SECOND();
2610 SET_SECOND(TOP());
2611 SET_TOP(u);
2612 break;
2613 default:
2614 exit_func = TOP();
2615 SET_TOP(u);
2616 break;
2617 }
2618 u = v = w = Py_None;
2619 }
2620 else {
2621 PyObject *tp, *exc, *tb;
2622 PyTryBlock *block;
2623 v = SECOND();
2624 w = THIRD();
2625 tp = FOURTH();
2626 exc = PEEK(5);
2627 tb = PEEK(6);
2628 exit_func = PEEK(7);
2629 SET_VALUE(7, tb);
2630 SET_VALUE(6, exc);
2631 SET_VALUE(5, tp);
2632 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2633 SET_FOURTH(NULL);
2634 /* We just shifted the stack down, so we have
2635 to tell the except handler block that the
2636 values are lower than it expects. */
2637 block = &f->f_blockstack[f->f_iblock - 1];
2638 assert(block->b_type == EXCEPT_HANDLER);
2639 block->b_level--;
2640 }
2641 /* XXX Not the fastest way to call it... */
2642 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2643 NULL);
2644 Py_DECREF(exit_func);
2645 if (x == NULL)
2646 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002648 if (u != Py_None)
2649 err = PyObject_IsTrue(x);
2650 else
2651 err = 0;
2652 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 if (err < 0)
2655 break; /* Go to error exit */
2656 else if (err > 0) {
2657 err = 0;
2658 /* There was an exception and a True return */
2659 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2660 }
2661 PREDICT(END_FINALLY);
2662 break;
2663 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 TARGET(CALL_FUNCTION)
2666 {
2667 PyObject **sp;
2668 PCALL(PCALL_ALL);
2669 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002670#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002672#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002674#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 stack_pointer = sp;
2676 PUSH(x);
2677 if (x != NULL)
2678 DISPATCH();
2679 break;
2680 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2683 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2684 TARGET(CALL_FUNCTION_VAR_KW)
2685 _call_function_var_kw:
2686 {
2687 int na = oparg & 0xff;
2688 int nk = (oparg>>8) & 0xff;
2689 int flags = (opcode - CALL_FUNCTION) & 3;
2690 int n = na + 2 * nk;
2691 PyObject **pfunc, *func, **sp;
2692 PCALL(PCALL_ALL);
2693 if (flags & CALL_FLAG_VAR)
2694 n++;
2695 if (flags & CALL_FLAG_KW)
2696 n++;
2697 pfunc = stack_pointer - n - 1;
2698 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002701 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 PyObject *self = PyMethod_GET_SELF(func);
2703 Py_INCREF(self);
2704 func = PyMethod_GET_FUNCTION(func);
2705 Py_INCREF(func);
2706 Py_DECREF(*pfunc);
2707 *pfunc = self;
2708 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002709 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 } else
2711 Py_INCREF(func);
2712 sp = stack_pointer;
2713 READ_TIMESTAMP(intr0);
2714 x = ext_do_call(func, &sp, flags, na, nk);
2715 READ_TIMESTAMP(intr1);
2716 stack_pointer = sp;
2717 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 while (stack_pointer > pfunc) {
2720 w = POP();
2721 Py_DECREF(w);
2722 }
2723 PUSH(x);
2724 if (x != NULL)
2725 DISPATCH();
2726 break;
2727 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2730 TARGET(MAKE_FUNCTION)
2731 _make_function:
2732 {
2733 int posdefaults = oparg & 0xff;
2734 int kwdefaults = (oparg>>8) & 0xff;
2735 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002736
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002737 w = POP(); /* qualname */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 v = POP(); /* code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002739 x = PyFunction_NewWithQualName(v, f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 Py_DECREF(v);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002741 Py_DECREF(w);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 if (x != NULL && opcode == MAKE_CLOSURE) {
2744 v = POP();
2745 if (PyFunction_SetClosure(x, v) != 0) {
2746 /* Can't happen unless bytecode is corrupt. */
2747 why = WHY_EXCEPTION;
2748 }
2749 Py_DECREF(v);
2750 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (x != NULL && num_annotations > 0) {
2753 Py_ssize_t name_ix;
2754 u = POP(); /* names of args with annotations */
2755 v = PyDict_New();
2756 if (v == NULL) {
2757 Py_DECREF(x);
2758 x = NULL;
2759 break;
2760 }
2761 name_ix = PyTuple_Size(u);
2762 assert(num_annotations == name_ix+1);
2763 while (name_ix > 0) {
2764 --name_ix;
2765 t = PyTuple_GET_ITEM(u, name_ix);
2766 w = POP();
2767 /* XXX(nnorwitz): check for errors */
2768 PyDict_SetItem(v, t, w);
2769 Py_DECREF(w);
2770 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 if (PyFunction_SetAnnotations(x, v) != 0) {
2773 /* Can't happen unless
2774 PyFunction_SetAnnotations changes. */
2775 why = WHY_EXCEPTION;
2776 }
2777 Py_DECREF(v);
2778 Py_DECREF(u);
2779 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 /* XXX Maybe this should be a separate opcode? */
2782 if (x != NULL && posdefaults > 0) {
2783 v = PyTuple_New(posdefaults);
2784 if (v == NULL) {
2785 Py_DECREF(x);
2786 x = NULL;
2787 break;
2788 }
2789 while (--posdefaults >= 0) {
2790 w = POP();
2791 PyTuple_SET_ITEM(v, posdefaults, w);
2792 }
2793 if (PyFunction_SetDefaults(x, v) != 0) {
2794 /* Can't happen unless
2795 PyFunction_SetDefaults changes. */
2796 why = WHY_EXCEPTION;
2797 }
2798 Py_DECREF(v);
2799 }
2800 if (x != NULL && kwdefaults > 0) {
2801 v = PyDict_New();
2802 if (v == NULL) {
2803 Py_DECREF(x);
2804 x = NULL;
2805 break;
2806 }
2807 while (--kwdefaults >= 0) {
2808 w = POP(); /* default value */
2809 u = POP(); /* kw only arg name */
2810 /* XXX(nnorwitz): check for errors */
2811 PyDict_SetItem(v, u, w);
2812 Py_DECREF(w);
2813 Py_DECREF(u);
2814 }
2815 if (PyFunction_SetKwDefaults(x, v) != 0) {
2816 /* Can't happen unless
2817 PyFunction_SetKwDefaults changes. */
2818 why = WHY_EXCEPTION;
2819 }
2820 Py_DECREF(v);
2821 }
2822 PUSH(x);
2823 break;
2824 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 TARGET(BUILD_SLICE)
2827 if (oparg == 3)
2828 w = POP();
2829 else
2830 w = NULL;
2831 v = POP();
2832 u = TOP();
2833 x = PySlice_New(u, v, w);
2834 Py_DECREF(u);
2835 Py_DECREF(v);
2836 Py_XDECREF(w);
2837 SET_TOP(x);
2838 if (x != NULL) DISPATCH();
2839 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 TARGET(EXTENDED_ARG)
2842 opcode = NEXTOP();
2843 oparg = oparg<<16 | NEXTARG();
2844 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002845
Antoine Pitrou042b1282010-08-13 21:15:58 +00002846#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002848#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 default:
2850 fprintf(stderr,
2851 "XXX lineno: %d, opcode: %d\n",
2852 PyFrame_GetLineNumber(f),
2853 opcode);
2854 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2855 why = WHY_EXCEPTION;
2856 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002857
2858#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002860#endif
2861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (why == WHY_NOT) {
2871 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002872#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 /* This check is expensive! */
2874 if (PyErr_Occurred())
2875 fprintf(stderr,
2876 "XXX undetected error\n");
2877 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002878#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 READ_TIMESTAMP(loop1);
2880 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002881#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002883#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 }
2885 why = WHY_EXCEPTION;
2886 x = Py_None;
2887 err = 0;
2888 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2893 if (!PyErr_Occurred()) {
2894 PyErr_SetString(PyExc_SystemError,
2895 "error return without exception set");
2896 why = WHY_EXCEPTION;
2897 }
2898 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002899#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 else {
2901 /* This check is expensive! */
2902 if (PyErr_Occurred()) {
2903 char buf[128];
2904 sprintf(buf, "Stack unwind with exception "
2905 "set and why=%d", why);
2906 Py_FatalError(buf);
2907 }
2908 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002909#endif
2910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (why == WHY_EXCEPTION) {
2914 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 if (tstate->c_tracefunc != NULL)
2917 call_exc_trace(tstate->c_tracefunc,
2918 tstate->c_traceobj, f);
2919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 if (why == WHY_RERAISE)
2924 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002927
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002928fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 while (why != WHY_NOT && f->f_iblock > 0) {
2930 /* Peek at the current block. */
2931 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 assert(why != WHY_YIELD);
2934 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2935 why = WHY_NOT;
2936 JUMPTO(PyLong_AS_LONG(retval));
2937 Py_DECREF(retval);
2938 break;
2939 }
2940 /* Now we have to pop the block. */
2941 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 if (b->b_type == EXCEPT_HANDLER) {
2944 UNWIND_EXCEPT_HANDLER(b);
2945 continue;
2946 }
2947 UNWIND_BLOCK(b);
2948 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2949 why = WHY_NOT;
2950 JUMPTO(b->b_handler);
2951 break;
2952 }
2953 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2954 || b->b_type == SETUP_FINALLY)) {
2955 PyObject *exc, *val, *tb;
2956 int handler = b->b_handler;
2957 /* Beware, this invalidates all b->b_* fields */
2958 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2959 PUSH(tstate->exc_traceback);
2960 PUSH(tstate->exc_value);
2961 if (tstate->exc_type != NULL) {
2962 PUSH(tstate->exc_type);
2963 }
2964 else {
2965 Py_INCREF(Py_None);
2966 PUSH(Py_None);
2967 }
2968 PyErr_Fetch(&exc, &val, &tb);
2969 /* Make the raw exception data
2970 available to the handler,
2971 so a program can emulate the
2972 Python main loop. */
2973 PyErr_NormalizeException(
2974 &exc, &val, &tb);
2975 PyException_SetTraceback(val, tb);
2976 Py_INCREF(exc);
2977 tstate->exc_type = exc;
2978 Py_INCREF(val);
2979 tstate->exc_value = val;
2980 tstate->exc_traceback = tb;
2981 if (tb == NULL)
2982 tb = Py_None;
2983 Py_INCREF(tb);
2984 PUSH(tb);
2985 PUSH(val);
2986 PUSH(exc);
2987 why = WHY_NOT;
2988 JUMPTO(handler);
2989 break;
2990 }
2991 if (b->b_type == SETUP_FINALLY) {
2992 if (why & (WHY_RETURN | WHY_CONTINUE))
2993 PUSH(retval);
2994 PUSH(PyLong_FromLong((long)why));
2995 why = WHY_NOT;
2996 JUMPTO(b->b_handler);
2997 break;
2998 }
2999 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 if (why != WHY_NOT)
3004 break;
3005 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 assert(why != WHY_YIELD);
3010 /* Pop remaining stack entries. */
3011 while (!EMPTY()) {
3012 v = POP();
3013 Py_XDECREF(v);
3014 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 if (why != WHY_RETURN)
3017 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003018
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003019fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003020 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3021 /* The purpose of this block is to put aside the generator's exception
3022 state and restore that of the calling frame. If the current
3023 exception state is from the caller, we clear the exception values
3024 on the generator frame, so they are not swapped back in latter. The
3025 origin of the current exception state is determined by checking for
3026 except handler blocks, which we must be in iff a new exception
3027 state came into existence in this frame. (An uncaught exception
3028 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3029 int i;
3030 for (i = 0; i < f->f_iblock; i++)
3031 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3032 break;
3033 if (i == f->f_iblock)
3034 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003035 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003036 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003037 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003038 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 if (tstate->use_tracing) {
3041 if (tstate->c_tracefunc) {
3042 if (why == WHY_RETURN || why == WHY_YIELD) {
3043 if (call_trace(tstate->c_tracefunc,
3044 tstate->c_traceobj, f,
3045 PyTrace_RETURN, retval)) {
3046 Py_XDECREF(retval);
3047 retval = NULL;
3048 why = WHY_EXCEPTION;
3049 }
3050 }
3051 else if (why == WHY_EXCEPTION) {
3052 call_trace_protected(tstate->c_tracefunc,
3053 tstate->c_traceobj, f,
3054 PyTrace_RETURN, NULL);
3055 }
3056 }
3057 if (tstate->c_profilefunc) {
3058 if (why == WHY_EXCEPTION)
3059 call_trace_protected(tstate->c_profilefunc,
3060 tstate->c_profileobj, f,
3061 PyTrace_RETURN, NULL);
3062 else if (call_trace(tstate->c_profilefunc,
3063 tstate->c_profileobj, f,
3064 PyTrace_RETURN, retval)) {
3065 Py_XDECREF(retval);
3066 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003067 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 }
3069 }
3070 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003072 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003073exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 Py_LeaveRecursiveCall();
3075 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003078}
3079
Benjamin Petersonb204a422011-06-05 22:04:07 -05003080static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003081format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3082{
3083 int err;
3084 Py_ssize_t len = PyList_GET_SIZE(names);
3085 PyObject *name_str, *comma, *tail, *tmp;
3086
3087 assert(PyList_CheckExact(names));
3088 assert(len >= 1);
3089 /* Deal with the joys of natural language. */
3090 switch (len) {
3091 case 1:
3092 name_str = PyList_GET_ITEM(names, 0);
3093 Py_INCREF(name_str);
3094 break;
3095 case 2:
3096 name_str = PyUnicode_FromFormat("%U and %U",
3097 PyList_GET_ITEM(names, len - 2),
3098 PyList_GET_ITEM(names, len - 1));
3099 break;
3100 default:
3101 tail = PyUnicode_FromFormat(", %U, and %U",
3102 PyList_GET_ITEM(names, len - 2),
3103 PyList_GET_ITEM(names, len - 1));
3104 /* Chop off the last two objects in the list. This shouldn't actually
3105 fail, but we can't be too careful. */
3106 err = PyList_SetSlice(names, len - 2, len, NULL);
3107 if (err == -1) {
3108 Py_DECREF(tail);
3109 return;
3110 }
3111 /* Stitch everything up into a nice comma-separated list. */
3112 comma = PyUnicode_FromString(", ");
3113 if (comma == NULL) {
3114 Py_DECREF(tail);
3115 return;
3116 }
3117 tmp = PyUnicode_Join(comma, names);
3118 Py_DECREF(comma);
3119 if (tmp == NULL) {
3120 Py_DECREF(tail);
3121 return;
3122 }
3123 name_str = PyUnicode_Concat(tmp, tail);
3124 Py_DECREF(tmp);
3125 Py_DECREF(tail);
3126 break;
3127 }
3128 if (name_str == NULL)
3129 return;
3130 PyErr_Format(PyExc_TypeError,
3131 "%U() missing %i required %s argument%s: %U",
3132 co->co_name,
3133 len,
3134 kind,
3135 len == 1 ? "" : "s",
3136 name_str);
3137 Py_DECREF(name_str);
3138}
3139
3140static void
3141missing_arguments(PyCodeObject *co, int missing, int defcount,
3142 PyObject **fastlocals)
3143{
3144 int i, j = 0;
3145 int start, end;
3146 int positional = defcount != -1;
3147 const char *kind = positional ? "positional" : "keyword-only";
3148 PyObject *missing_names;
3149
3150 /* Compute the names of the arguments that are missing. */
3151 missing_names = PyList_New(missing);
3152 if (missing_names == NULL)
3153 return;
3154 if (positional) {
3155 start = 0;
3156 end = co->co_argcount - defcount;
3157 }
3158 else {
3159 start = co->co_argcount;
3160 end = start + co->co_kwonlyargcount;
3161 }
3162 for (i = start; i < end; i++) {
3163 if (GETLOCAL(i) == NULL) {
3164 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3165 PyObject *name = PyObject_Repr(raw);
3166 if (name == NULL) {
3167 Py_DECREF(missing_names);
3168 return;
3169 }
3170 PyList_SET_ITEM(missing_names, j++, name);
3171 }
3172 }
3173 assert(j == missing);
3174 format_missing(kind, co, missing_names);
3175 Py_DECREF(missing_names);
3176}
3177
3178static void
3179too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003180{
3181 int plural;
3182 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003183 int i;
3184 PyObject *sig, *kwonly_sig;
3185
Benjamin Petersone109c702011-06-24 09:37:26 -05003186 assert((co->co_flags & CO_VARARGS) == 0);
3187 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003188 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003189 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003190 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003191 if (defcount) {
3192 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003193 plural = 1;
3194 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3195 }
3196 else {
3197 plural = co->co_argcount != 1;
3198 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3199 }
3200 if (sig == NULL)
3201 return;
3202 if (kwonly_given) {
3203 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3204 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3205 kwonly_given != 1 ? "s" : "");
3206 if (kwonly_sig == NULL) {
3207 Py_DECREF(sig);
3208 return;
3209 }
3210 }
3211 else {
3212 /* This will not fail. */
3213 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003214 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003215 }
3216 PyErr_Format(PyExc_TypeError,
3217 "%U() takes %U positional argument%s but %d%U %s given",
3218 co->co_name,
3219 sig,
3220 plural ? "s" : "",
3221 given,
3222 kwonly_sig,
3223 given == 1 && !kwonly_given ? "was" : "were");
3224 Py_DECREF(sig);
3225 Py_DECREF(kwonly_sig);
3226}
3227
Guido van Rossumc2e20742006-02-27 22:32:47 +00003228/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003229 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003230 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003231
Tim Peters6d6c1a32001-08-02 04:15:00 +00003232PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003233PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 PyObject **args, int argcount, PyObject **kws, int kwcount,
3235 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003236{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003237 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 register PyFrameObject *f;
3239 register PyObject *retval = NULL;
3240 register PyObject **fastlocals, **freevars;
3241 PyThreadState *tstate = PyThreadState_GET();
3242 PyObject *x, *u;
3243 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003244 int i;
3245 int n = argcount;
3246 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003248 if (globals == NULL) {
3249 PyErr_SetString(PyExc_SystemError,
3250 "PyEval_EvalCodeEx: NULL globals");
3251 return NULL;
3252 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 assert(tstate != NULL);
3255 assert(globals != NULL);
3256 f = PyFrame_New(tstate, co, globals, locals);
3257 if (f == NULL)
3258 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 fastlocals = f->f_localsplus;
3261 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003262
Benjamin Petersonb204a422011-06-05 22:04:07 -05003263 /* Parse arguments. */
3264 if (co->co_flags & CO_VARKEYWORDS) {
3265 kwdict = PyDict_New();
3266 if (kwdict == NULL)
3267 goto fail;
3268 i = total_args;
3269 if (co->co_flags & CO_VARARGS)
3270 i++;
3271 SETLOCAL(i, kwdict);
3272 }
3273 if (argcount > co->co_argcount)
3274 n = co->co_argcount;
3275 for (i = 0; i < n; i++) {
3276 x = args[i];
3277 Py_INCREF(x);
3278 SETLOCAL(i, x);
3279 }
3280 if (co->co_flags & CO_VARARGS) {
3281 u = PyTuple_New(argcount - n);
3282 if (u == NULL)
3283 goto fail;
3284 SETLOCAL(total_args, u);
3285 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 x = args[i];
3287 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003288 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003290 }
3291 for (i = 0; i < kwcount; i++) {
3292 PyObject **co_varnames;
3293 PyObject *keyword = kws[2*i];
3294 PyObject *value = kws[2*i + 1];
3295 int j;
3296 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3297 PyErr_Format(PyExc_TypeError,
3298 "%U() keywords must be strings",
3299 co->co_name);
3300 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003302 /* Speed hack: do raw pointer compares. As names are
3303 normally interned this should almost always hit. */
3304 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3305 for (j = 0; j < total_args; j++) {
3306 PyObject *nm = co_varnames[j];
3307 if (nm == keyword)
3308 goto kw_found;
3309 }
3310 /* Slow fallback, just in case */
3311 for (j = 0; j < total_args; j++) {
3312 PyObject *nm = co_varnames[j];
3313 int cmp = PyObject_RichCompareBool(
3314 keyword, nm, Py_EQ);
3315 if (cmp > 0)
3316 goto kw_found;
3317 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003319 }
3320 if (j >= total_args && kwdict == NULL) {
3321 PyErr_Format(PyExc_TypeError,
3322 "%U() got an unexpected "
3323 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 co->co_name,
3325 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003326 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003328 PyDict_SetItem(kwdict, keyword, value);
3329 continue;
3330 kw_found:
3331 if (GETLOCAL(j) != NULL) {
3332 PyErr_Format(PyExc_TypeError,
3333 "%U() got multiple "
3334 "values for argument '%S'",
3335 co->co_name,
3336 keyword);
3337 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003339 Py_INCREF(value);
3340 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003341 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003342 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003343 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 goto fail;
3345 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003346 if (argcount < co->co_argcount) {
3347 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003348 int missing = 0;
3349 for (i = argcount; i < m; i++)
3350 if (GETLOCAL(i) == NULL)
3351 missing++;
3352 if (missing) {
3353 missing_arguments(co, missing, defcount, fastlocals);
3354 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003355 }
3356 if (n > m)
3357 i = n - m;
3358 else
3359 i = 0;
3360 for (; i < defcount; i++) {
3361 if (GETLOCAL(m+i) == NULL) {
3362 PyObject *def = defs[i];
3363 Py_INCREF(def);
3364 SETLOCAL(m+i, def);
3365 }
3366 }
3367 }
3368 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003369 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003370 for (i = co->co_argcount; i < total_args; i++) {
3371 PyObject *name;
3372 if (GETLOCAL(i) != NULL)
3373 continue;
3374 name = PyTuple_GET_ITEM(co->co_varnames, i);
3375 if (kwdefs != NULL) {
3376 PyObject *def = PyDict_GetItem(kwdefs, name);
3377 if (def) {
3378 Py_INCREF(def);
3379 SETLOCAL(i, def);
3380 continue;
3381 }
3382 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003383 missing++;
3384 }
3385 if (missing) {
3386 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003387 goto fail;
3388 }
3389 }
3390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003392 vars into frame. */
3393 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003395 int arg;
3396 /* Possibly account for the cell variable being an argument. */
3397 if (co->co_cell2arg != NULL &&
3398 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3399 c = PyCell_New(GETLOCAL(arg));
3400 else
3401 c = PyCell_New(NULL);
3402 if (c == NULL)
3403 goto fail;
3404 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003405 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003406 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3407 PyObject *o = PyTuple_GET_ITEM(closure, i);
3408 Py_INCREF(o);
3409 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 }
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
Benjamin Peterson87880242011-07-03 16:48:31 -05003455/* These 3 functions deal with the exception state of generators. */
3456
3457static void
3458save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3459{
3460 PyObject *type, *value, *traceback;
3461 Py_XINCREF(tstate->exc_type);
3462 Py_XINCREF(tstate->exc_value);
3463 Py_XINCREF(tstate->exc_traceback);
3464 type = f->f_exc_type;
3465 value = f->f_exc_value;
3466 traceback = f->f_exc_traceback;
3467 f->f_exc_type = tstate->exc_type;
3468 f->f_exc_value = tstate->exc_value;
3469 f->f_exc_traceback = tstate->exc_traceback;
3470 Py_XDECREF(type);
3471 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003472 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003473}
3474
3475static void
3476swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3477{
3478 PyObject *tmp;
3479 tmp = tstate->exc_type;
3480 tstate->exc_type = f->f_exc_type;
3481 f->f_exc_type = tmp;
3482 tmp = tstate->exc_value;
3483 tstate->exc_value = f->f_exc_value;
3484 f->f_exc_value = tmp;
3485 tmp = tstate->exc_traceback;
3486 tstate->exc_traceback = f->f_exc_traceback;
3487 f->f_exc_traceback = tmp;
3488}
3489
3490static void
3491restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3492{
3493 PyObject *type, *value, *tb;
3494 type = tstate->exc_type;
3495 value = tstate->exc_value;
3496 tb = tstate->exc_traceback;
3497 tstate->exc_type = f->f_exc_type;
3498 tstate->exc_value = f->f_exc_value;
3499 tstate->exc_traceback = f->f_exc_traceback;
3500 f->f_exc_type = NULL;
3501 f->f_exc_value = NULL;
3502 f->f_exc_traceback = NULL;
3503 Py_XDECREF(type);
3504 Py_XDECREF(value);
3505 Py_XDECREF(tb);
3506}
3507
3508
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003509/* Logic for the raise statement (too complicated for inlining).
3510 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003511static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003512do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003513{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 if (exc == NULL) {
3517 /* Reraise */
3518 PyThreadState *tstate = PyThreadState_GET();
3519 PyObject *tb;
3520 type = tstate->exc_type;
3521 value = tstate->exc_value;
3522 tb = tstate->exc_traceback;
3523 if (type == Py_None) {
3524 PyErr_SetString(PyExc_RuntimeError,
3525 "No active exception to reraise");
3526 return WHY_EXCEPTION;
3527 }
3528 Py_XINCREF(type);
3529 Py_XINCREF(value);
3530 Py_XINCREF(tb);
3531 PyErr_Restore(type, value, tb);
3532 return WHY_RERAISE;
3533 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 /* We support the following forms of raise:
3536 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003537 raise <instance>
3538 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 if (PyExceptionClass_Check(exc)) {
3541 type = exc;
3542 value = PyObject_CallObject(exc, NULL);
3543 if (value == NULL)
3544 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003545 if (!PyExceptionInstance_Check(value)) {
3546 PyErr_Format(PyExc_TypeError,
3547 "calling %R should have returned an instance of "
3548 "BaseException, not %R",
3549 type, Py_TYPE(value));
3550 goto raise_error;
3551 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 }
3553 else if (PyExceptionInstance_Check(exc)) {
3554 value = exc;
3555 type = PyExceptionInstance_Class(exc);
3556 Py_INCREF(type);
3557 }
3558 else {
3559 /* Not something you can raise. You get an exception
3560 anyway, just not what you specified :-) */
3561 Py_DECREF(exc);
3562 PyErr_SetString(PyExc_TypeError,
3563 "exceptions must derive from BaseException");
3564 goto raise_error;
3565 }
Collin Winter828f04a2007-08-31 00:04:24 +00003566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 if (cause) {
3568 PyObject *fixed_cause;
3569 if (PyExceptionClass_Check(cause)) {
3570 fixed_cause = PyObject_CallObject(cause, NULL);
3571 if (fixed_cause == NULL)
3572 goto raise_error;
3573 Py_DECREF(cause);
3574 }
3575 else if (PyExceptionInstance_Check(cause)) {
3576 fixed_cause = cause;
3577 }
3578 else {
3579 PyErr_SetString(PyExc_TypeError,
3580 "exception causes must derive from "
3581 "BaseException");
3582 goto raise_error;
3583 }
3584 PyException_SetCause(value, fixed_cause);
3585 }
Collin Winter828f04a2007-08-31 00:04:24 +00003586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 PyErr_SetObject(type, value);
3588 /* PyErr_SetObject incref's its arguments */
3589 Py_XDECREF(value);
3590 Py_XDECREF(type);
3591 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003592
3593raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 Py_XDECREF(value);
3595 Py_XDECREF(type);
3596 Py_XDECREF(cause);
3597 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003598}
3599
Tim Petersd6d010b2001-06-21 02:49:55 +00003600/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003601 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003602
Guido van Rossum0368b722007-05-11 16:50:42 +00003603 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3604 with a variable target.
3605*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003606
Barry Warsawe42b18f1997-08-25 22:13:04 +00003607static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003608unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003609{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 int i = 0, j = 0;
3611 Py_ssize_t ll = 0;
3612 PyObject *it; /* iter(v) */
3613 PyObject *w;
3614 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 it = PyObject_GetIter(v);
3619 if (it == NULL)
3620 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003622 for (; i < argcnt; i++) {
3623 w = PyIter_Next(it);
3624 if (w == NULL) {
3625 /* Iterator done, via error or exhaustion. */
3626 if (!PyErr_Occurred()) {
3627 PyErr_Format(PyExc_ValueError,
3628 "need more than %d value%s to unpack",
3629 i, i == 1 ? "" : "s");
3630 }
3631 goto Error;
3632 }
3633 *--sp = w;
3634 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003636 if (argcntafter == -1) {
3637 /* We better have exhausted the iterator now. */
3638 w = PyIter_Next(it);
3639 if (w == NULL) {
3640 if (PyErr_Occurred())
3641 goto Error;
3642 Py_DECREF(it);
3643 return 1;
3644 }
3645 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003646 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3647 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 goto Error;
3649 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003650
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003651 l = PySequence_List(it);
3652 if (l == NULL)
3653 goto Error;
3654 *--sp = l;
3655 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003656
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 ll = PyList_GET_SIZE(l);
3658 if (ll < argcntafter) {
3659 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3660 argcnt + ll);
3661 goto Error;
3662 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 /* Pop the "after-variable" args off the list. */
3665 for (j = argcntafter; j > 0; j--, i++) {
3666 *--sp = PyList_GET_ITEM(l, ll - j);
3667 }
3668 /* Resize the list. */
3669 Py_SIZE(l) = ll - argcntafter;
3670 Py_DECREF(it);
3671 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003672
Tim Petersd6d010b2001-06-21 02:49:55 +00003673Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003674 for (; i > 0; i--, sp++)
3675 Py_DECREF(*sp);
3676 Py_XDECREF(it);
3677 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003678}
3679
3680
Guido van Rossum96a42c81992-01-12 02:29:51 +00003681#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003682static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003683prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 printf("%s ", str);
3686 if (PyObject_Print(v, stdout, 0) != 0)
3687 PyErr_Clear(); /* Don't know what else to do */
3688 printf("\n");
3689 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003690}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003691#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003692
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003693static void
Fred Drake5755ce62001-06-27 19:19:46 +00003694call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 PyObject *type, *value, *traceback, *arg;
3697 int err;
3698 PyErr_Fetch(&type, &value, &traceback);
3699 if (value == NULL) {
3700 value = Py_None;
3701 Py_INCREF(value);
3702 }
3703 arg = PyTuple_Pack(3, type, value, traceback);
3704 if (arg == NULL) {
3705 PyErr_Restore(type, value, traceback);
3706 return;
3707 }
3708 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3709 Py_DECREF(arg);
3710 if (err == 0)
3711 PyErr_Restore(type, value, traceback);
3712 else {
3713 Py_XDECREF(type);
3714 Py_XDECREF(value);
3715 Py_XDECREF(traceback);
3716 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003717}
3718
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003719static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003720call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003722{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 PyObject *type, *value, *traceback;
3724 int err;
3725 PyErr_Fetch(&type, &value, &traceback);
3726 err = call_trace(func, obj, frame, what, arg);
3727 if (err == 0)
3728 {
3729 PyErr_Restore(type, value, traceback);
3730 return 0;
3731 }
3732 else {
3733 Py_XDECREF(type);
3734 Py_XDECREF(value);
3735 Py_XDECREF(traceback);
3736 return -1;
3737 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003738}
3739
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003740static int
Fred Drake5755ce62001-06-27 19:19:46 +00003741call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 register PyThreadState *tstate = frame->f_tstate;
3745 int result;
3746 if (tstate->tracing)
3747 return 0;
3748 tstate->tracing++;
3749 tstate->use_tracing = 0;
3750 result = func(obj, frame, what, arg);
3751 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3752 || (tstate->c_profilefunc != NULL));
3753 tstate->tracing--;
3754 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003755}
3756
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003757PyObject *
3758_PyEval_CallTracing(PyObject *func, PyObject *args)
3759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003760 PyFrameObject *frame = PyEval_GetFrame();
3761 PyThreadState *tstate = frame->f_tstate;
3762 int save_tracing = tstate->tracing;
3763 int save_use_tracing = tstate->use_tracing;
3764 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003766 tstate->tracing = 0;
3767 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3768 || (tstate->c_profilefunc != NULL));
3769 result = PyObject_Call(func, args, NULL);
3770 tstate->tracing = save_tracing;
3771 tstate->use_tracing = save_use_tracing;
3772 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003773}
3774
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003775/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003776static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003777maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003778 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3779 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003780{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003781 int result = 0;
3782 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 /* If the last instruction executed isn't in the current
3785 instruction window, reset the window.
3786 */
3787 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3788 PyAddrPair bounds;
3789 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3790 &bounds);
3791 *instr_lb = bounds.ap_lower;
3792 *instr_ub = bounds.ap_upper;
3793 }
3794 /* If the last instruction falls at the start of a line or if
3795 it represents a jump backwards, update the frame's line
3796 number and call the trace function. */
3797 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3798 frame->f_lineno = line;
3799 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3800 }
3801 *instr_prev = frame->f_lasti;
3802 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003803}
3804
Fred Drake5755ce62001-06-27 19:19:46 +00003805void
3806PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003807{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003808 PyThreadState *tstate = PyThreadState_GET();
3809 PyObject *temp = tstate->c_profileobj;
3810 Py_XINCREF(arg);
3811 tstate->c_profilefunc = NULL;
3812 tstate->c_profileobj = NULL;
3813 /* Must make sure that tracing is not ignored if 'temp' is freed */
3814 tstate->use_tracing = tstate->c_tracefunc != NULL;
3815 Py_XDECREF(temp);
3816 tstate->c_profilefunc = func;
3817 tstate->c_profileobj = arg;
3818 /* Flag that tracing or profiling is turned on */
3819 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003820}
3821
3822void
3823PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3824{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003825 PyThreadState *tstate = PyThreadState_GET();
3826 PyObject *temp = tstate->c_traceobj;
3827 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3828 Py_XINCREF(arg);
3829 tstate->c_tracefunc = NULL;
3830 tstate->c_traceobj = NULL;
3831 /* Must make sure that profiling is not ignored if 'temp' is freed */
3832 tstate->use_tracing = tstate->c_profilefunc != NULL;
3833 Py_XDECREF(temp);
3834 tstate->c_tracefunc = func;
3835 tstate->c_traceobj = arg;
3836 /* Flag that tracing or profiling is turned on */
3837 tstate->use_tracing = ((func != NULL)
3838 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003839}
3840
Guido van Rossumb209a111997-04-29 18:18:01 +00003841PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003842PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 PyFrameObject *current_frame = PyEval_GetFrame();
3845 if (current_frame == NULL)
3846 return PyThreadState_GET()->interp->builtins;
3847 else
3848 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003849}
3850
Guido van Rossumb209a111997-04-29 18:18:01 +00003851PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003852PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 PyFrameObject *current_frame = PyEval_GetFrame();
3855 if (current_frame == NULL)
3856 return NULL;
3857 PyFrame_FastToLocals(current_frame);
3858 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003859}
3860
Guido van Rossumb209a111997-04-29 18:18:01 +00003861PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003862PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 PyFrameObject *current_frame = PyEval_GetFrame();
3865 if (current_frame == NULL)
3866 return NULL;
3867 else
3868 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003869}
3870
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003871PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003872PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 PyThreadState *tstate = PyThreadState_GET();
3875 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003876}
3877
Guido van Rossum6135a871995-01-09 17:53:26 +00003878int
Tim Peters5ba58662001-07-16 02:29:45 +00003879PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003881 PyFrameObject *current_frame = PyEval_GetFrame();
3882 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 if (current_frame != NULL) {
3885 const int codeflags = current_frame->f_code->co_flags;
3886 const int compilerflags = codeflags & PyCF_MASK;
3887 if (compilerflags) {
3888 result = 1;
3889 cf->cf_flags |= compilerflags;
3890 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003891#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 if (codeflags & CO_GENERATOR_ALLOWED) {
3893 result = 1;
3894 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3895 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003896#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003897 }
3898 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003899}
3900
Guido van Rossum3f5da241990-12-20 15:06:42 +00003901
Guido van Rossum681d79a1995-07-18 14:51:37 +00003902/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003903 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003904
Guido van Rossumb209a111997-04-29 18:18:01 +00003905PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003906PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 if (arg == NULL) {
3911 arg = PyTuple_New(0);
3912 if (arg == NULL)
3913 return NULL;
3914 }
3915 else if (!PyTuple_Check(arg)) {
3916 PyErr_SetString(PyExc_TypeError,
3917 "argument list must be a tuple");
3918 return NULL;
3919 }
3920 else
3921 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 if (kw != NULL && !PyDict_Check(kw)) {
3924 PyErr_SetString(PyExc_TypeError,
3925 "keyword list must be a dictionary");
3926 Py_DECREF(arg);
3927 return NULL;
3928 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003930 result = PyObject_Call(func, arg, kw);
3931 Py_DECREF(arg);
3932 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003933}
3934
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003935const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003936PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 if (PyMethod_Check(func))
3939 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3940 else if (PyFunction_Check(func))
3941 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3942 else if (PyCFunction_Check(func))
3943 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3944 else
3945 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003946}
3947
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003948const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003949PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 if (PyMethod_Check(func))
3952 return "()";
3953 else if (PyFunction_Check(func))
3954 return "()";
3955 else if (PyCFunction_Check(func))
3956 return "()";
3957 else
3958 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003959}
3960
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003961static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003962err_args(PyObject *func, int flags, int nargs)
3963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 if (flags & METH_NOARGS)
3965 PyErr_Format(PyExc_TypeError,
3966 "%.200s() takes no arguments (%d given)",
3967 ((PyCFunctionObject *)func)->m_ml->ml_name,
3968 nargs);
3969 else
3970 PyErr_Format(PyExc_TypeError,
3971 "%.200s() takes exactly one argument (%d given)",
3972 ((PyCFunctionObject *)func)->m_ml->ml_name,
3973 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003974}
3975
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003976#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003977if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003978 if (call_trace(tstate->c_profilefunc, \
3979 tstate->c_profileobj, \
3980 tstate->frame, PyTrace_C_CALL, \
3981 func)) { \
3982 x = NULL; \
3983 } \
3984 else { \
3985 x = call; \
3986 if (tstate->c_profilefunc != NULL) { \
3987 if (x == NULL) { \
3988 call_trace_protected(tstate->c_profilefunc, \
3989 tstate->c_profileobj, \
3990 tstate->frame, PyTrace_C_EXCEPTION, \
3991 func); \
3992 /* XXX should pass (type, value, tb) */ \
3993 } else { \
3994 if (call_trace(tstate->c_profilefunc, \
3995 tstate->c_profileobj, \
3996 tstate->frame, PyTrace_C_RETURN, \
3997 func)) { \
3998 Py_DECREF(x); \
3999 x = NULL; \
4000 } \
4001 } \
4002 } \
4003 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004004} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 x = call; \
4006 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004007
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004008static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004009call_function(PyObject ***pp_stack, int oparg
4010#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004011 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004012#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004013 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 int na = oparg & 0xff;
4016 int nk = (oparg>>8) & 0xff;
4017 int n = na + 2 * nk;
4018 PyObject **pfunc = (*pp_stack) - n - 1;
4019 PyObject *func = *pfunc;
4020 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 /* Always dispatch PyCFunction first, because these are
4023 presumed to be the most frequent callable object.
4024 */
4025 if (PyCFunction_Check(func) && nk == 0) {
4026 int flags = PyCFunction_GET_FLAGS(func);
4027 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 PCALL(PCALL_CFUNCTION);
4030 if (flags & (METH_NOARGS | METH_O)) {
4031 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4032 PyObject *self = PyCFunction_GET_SELF(func);
4033 if (flags & METH_NOARGS && na == 0) {
4034 C_TRACE(x, (*meth)(self,NULL));
4035 }
4036 else if (flags & METH_O && na == 1) {
4037 PyObject *arg = EXT_POP(*pp_stack);
4038 C_TRACE(x, (*meth)(self,arg));
4039 Py_DECREF(arg);
4040 }
4041 else {
4042 err_args(func, flags, na);
4043 x = NULL;
4044 }
4045 }
4046 else {
4047 PyObject *callargs;
4048 callargs = load_args(pp_stack, na);
4049 READ_TIMESTAMP(*pintr0);
4050 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4051 READ_TIMESTAMP(*pintr1);
4052 Py_XDECREF(callargs);
4053 }
4054 } else {
4055 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4056 /* optimize access to bound methods */
4057 PyObject *self = PyMethod_GET_SELF(func);
4058 PCALL(PCALL_METHOD);
4059 PCALL(PCALL_BOUND_METHOD);
4060 Py_INCREF(self);
4061 func = PyMethod_GET_FUNCTION(func);
4062 Py_INCREF(func);
4063 Py_DECREF(*pfunc);
4064 *pfunc = self;
4065 na++;
4066 n++;
4067 } else
4068 Py_INCREF(func);
4069 READ_TIMESTAMP(*pintr0);
4070 if (PyFunction_Check(func))
4071 x = fast_function(func, pp_stack, n, na, nk);
4072 else
4073 x = do_call(func, pp_stack, na, nk);
4074 READ_TIMESTAMP(*pintr1);
4075 Py_DECREF(func);
4076 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004077
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 /* Clear the stack of the function object. Also removes
4079 the arguments in case they weren't consumed already
4080 (fast_function() and err_args() leave them on the stack).
4081 */
4082 while ((*pp_stack) > pfunc) {
4083 w = EXT_POP(*pp_stack);
4084 Py_DECREF(w);
4085 PCALL(PCALL_POP);
4086 }
4087 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004088}
4089
Jeremy Hylton192690e2002-08-16 18:36:11 +00004090/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004091 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004092 For the simplest case -- a function that takes only positional
4093 arguments and is called with only positional arguments -- it
4094 inlines the most primitive frame setup code from
4095 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4096 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004097*/
4098
4099static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004100fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004101{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4103 PyObject *globals = PyFunction_GET_GLOBALS(func);
4104 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4105 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4106 PyObject **d = NULL;
4107 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 PCALL(PCALL_FUNCTION);
4110 PCALL(PCALL_FAST_FUNCTION);
4111 if (argdefs == NULL && co->co_argcount == n &&
4112 co->co_kwonlyargcount == 0 && nk==0 &&
4113 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4114 PyFrameObject *f;
4115 PyObject *retval = NULL;
4116 PyThreadState *tstate = PyThreadState_GET();
4117 PyObject **fastlocals, **stack;
4118 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004120 PCALL(PCALL_FASTER_FUNCTION);
4121 assert(globals != NULL);
4122 /* XXX Perhaps we should create a specialized
4123 PyFrame_New() that doesn't take locals, but does
4124 take builtins without sanity checking them.
4125 */
4126 assert(tstate != NULL);
4127 f = PyFrame_New(tstate, co, globals, NULL);
4128 if (f == NULL)
4129 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004130
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004131 fastlocals = f->f_localsplus;
4132 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004134 for (i = 0; i < n; i++) {
4135 Py_INCREF(*stack);
4136 fastlocals[i] = *stack++;
4137 }
4138 retval = PyEval_EvalFrameEx(f,0);
4139 ++tstate->recursion_depth;
4140 Py_DECREF(f);
4141 --tstate->recursion_depth;
4142 return retval;
4143 }
4144 if (argdefs != NULL) {
4145 d = &PyTuple_GET_ITEM(argdefs, 0);
4146 nd = Py_SIZE(argdefs);
4147 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004148 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004149 (PyObject *)NULL, (*pp_stack)-n, na,
4150 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4151 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004152}
4153
4154static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004155update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4156 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004157{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004158 PyObject *kwdict = NULL;
4159 if (orig_kwdict == NULL)
4160 kwdict = PyDict_New();
4161 else {
4162 kwdict = PyDict_Copy(orig_kwdict);
4163 Py_DECREF(orig_kwdict);
4164 }
4165 if (kwdict == NULL)
4166 return NULL;
4167 while (--nk >= 0) {
4168 int err;
4169 PyObject *value = EXT_POP(*pp_stack);
4170 PyObject *key = EXT_POP(*pp_stack);
4171 if (PyDict_GetItem(kwdict, key) != NULL) {
4172 PyErr_Format(PyExc_TypeError,
4173 "%.200s%s got multiple values "
4174 "for keyword argument '%U'",
4175 PyEval_GetFuncName(func),
4176 PyEval_GetFuncDesc(func),
4177 key);
4178 Py_DECREF(key);
4179 Py_DECREF(value);
4180 Py_DECREF(kwdict);
4181 return NULL;
4182 }
4183 err = PyDict_SetItem(kwdict, key, value);
4184 Py_DECREF(key);
4185 Py_DECREF(value);
4186 if (err) {
4187 Py_DECREF(kwdict);
4188 return NULL;
4189 }
4190 }
4191 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004192}
4193
4194static PyObject *
4195update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004196 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004197{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004200 callargs = PyTuple_New(nstack + nstar);
4201 if (callargs == NULL) {
4202 return NULL;
4203 }
4204 if (nstar) {
4205 int i;
4206 for (i = 0; i < nstar; i++) {
4207 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4208 Py_INCREF(a);
4209 PyTuple_SET_ITEM(callargs, nstack + i, a);
4210 }
4211 }
4212 while (--nstack >= 0) {
4213 w = EXT_POP(*pp_stack);
4214 PyTuple_SET_ITEM(callargs, nstack, w);
4215 }
4216 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004217}
4218
4219static PyObject *
4220load_args(PyObject ***pp_stack, int na)
4221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 PyObject *args = PyTuple_New(na);
4223 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 if (args == NULL)
4226 return NULL;
4227 while (--na >= 0) {
4228 w = EXT_POP(*pp_stack);
4229 PyTuple_SET_ITEM(args, na, w);
4230 }
4231 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004232}
4233
4234static PyObject *
4235do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4236{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 PyObject *callargs = NULL;
4238 PyObject *kwdict = NULL;
4239 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 if (nk > 0) {
4242 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4243 if (kwdict == NULL)
4244 goto call_fail;
4245 }
4246 callargs = load_args(pp_stack, na);
4247 if (callargs == NULL)
4248 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004249#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 /* At this point, we have to look at the type of func to
4251 update the call stats properly. Do it here so as to avoid
4252 exposing the call stats machinery outside ceval.c
4253 */
4254 if (PyFunction_Check(func))
4255 PCALL(PCALL_FUNCTION);
4256 else if (PyMethod_Check(func))
4257 PCALL(PCALL_METHOD);
4258 else if (PyType_Check(func))
4259 PCALL(PCALL_TYPE);
4260 else if (PyCFunction_Check(func))
4261 PCALL(PCALL_CFUNCTION);
4262 else
4263 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004264#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 if (PyCFunction_Check(func)) {
4266 PyThreadState *tstate = PyThreadState_GET();
4267 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4268 }
4269 else
4270 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004271call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 Py_XDECREF(callargs);
4273 Py_XDECREF(kwdict);
4274 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004275}
4276
4277static PyObject *
4278ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 int nstar = 0;
4281 PyObject *callargs = NULL;
4282 PyObject *stararg = NULL;
4283 PyObject *kwdict = NULL;
4284 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 if (flags & CALL_FLAG_KW) {
4287 kwdict = EXT_POP(*pp_stack);
4288 if (!PyDict_Check(kwdict)) {
4289 PyObject *d;
4290 d = PyDict_New();
4291 if (d == NULL)
4292 goto ext_call_fail;
4293 if (PyDict_Update(d, kwdict) != 0) {
4294 Py_DECREF(d);
4295 /* PyDict_Update raises attribute
4296 * error (percolated from an attempt
4297 * to get 'keys' attribute) instead of
4298 * a type error if its second argument
4299 * is not a mapping.
4300 */
4301 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4302 PyErr_Format(PyExc_TypeError,
4303 "%.200s%.200s argument after ** "
4304 "must be a mapping, not %.200s",
4305 PyEval_GetFuncName(func),
4306 PyEval_GetFuncDesc(func),
4307 kwdict->ob_type->tp_name);
4308 }
4309 goto ext_call_fail;
4310 }
4311 Py_DECREF(kwdict);
4312 kwdict = d;
4313 }
4314 }
4315 if (flags & CALL_FLAG_VAR) {
4316 stararg = EXT_POP(*pp_stack);
4317 if (!PyTuple_Check(stararg)) {
4318 PyObject *t = NULL;
4319 t = PySequence_Tuple(stararg);
4320 if (t == NULL) {
4321 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4322 PyErr_Format(PyExc_TypeError,
4323 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004324 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004325 PyEval_GetFuncName(func),
4326 PyEval_GetFuncDesc(func),
4327 stararg->ob_type->tp_name);
4328 }
4329 goto ext_call_fail;
4330 }
4331 Py_DECREF(stararg);
4332 stararg = t;
4333 }
4334 nstar = PyTuple_GET_SIZE(stararg);
4335 }
4336 if (nk > 0) {
4337 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4338 if (kwdict == NULL)
4339 goto ext_call_fail;
4340 }
4341 callargs = update_star_args(na, nstar, stararg, pp_stack);
4342 if (callargs == NULL)
4343 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004344#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 /* At this point, we have to look at the type of func to
4346 update the call stats properly. Do it here so as to avoid
4347 exposing the call stats machinery outside ceval.c
4348 */
4349 if (PyFunction_Check(func))
4350 PCALL(PCALL_FUNCTION);
4351 else if (PyMethod_Check(func))
4352 PCALL(PCALL_METHOD);
4353 else if (PyType_Check(func))
4354 PCALL(PCALL_TYPE);
4355 else if (PyCFunction_Check(func))
4356 PCALL(PCALL_CFUNCTION);
4357 else
4358 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004359#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 if (PyCFunction_Check(func)) {
4361 PyThreadState *tstate = PyThreadState_GET();
4362 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4363 }
4364 else
4365 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004366ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 Py_XDECREF(callargs);
4368 Py_XDECREF(kwdict);
4369 Py_XDECREF(stararg);
4370 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004371}
4372
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004373/* Extract a slice index from a PyInt or PyLong or an object with the
4374 nb_index slot defined, and store in *pi.
4375 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4376 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 +00004377 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004378*/
Tim Petersb5196382001-12-16 19:44:20 +00004379/* Note: If v is NULL, return success without storing into *pi. This
4380 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4381 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004382*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004383int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004384_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004385{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004386 if (v != NULL) {
4387 Py_ssize_t x;
4388 if (PyIndex_Check(v)) {
4389 x = PyNumber_AsSsize_t(v, NULL);
4390 if (x == -1 && PyErr_Occurred())
4391 return 0;
4392 }
4393 else {
4394 PyErr_SetString(PyExc_TypeError,
4395 "slice indices must be integers or "
4396 "None or have an __index__ method");
4397 return 0;
4398 }
4399 *pi = x;
4400 }
4401 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004402}
4403
Guido van Rossum486364b2007-06-30 05:01:58 +00004404#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004406
Guido van Rossumb209a111997-04-29 18:18:01 +00004407static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004408cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 int res = 0;
4411 switch (op) {
4412 case PyCmp_IS:
4413 res = (v == w);
4414 break;
4415 case PyCmp_IS_NOT:
4416 res = (v != w);
4417 break;
4418 case PyCmp_IN:
4419 res = PySequence_Contains(w, v);
4420 if (res < 0)
4421 return NULL;
4422 break;
4423 case PyCmp_NOT_IN:
4424 res = PySequence_Contains(w, v);
4425 if (res < 0)
4426 return NULL;
4427 res = !res;
4428 break;
4429 case PyCmp_EXC_MATCH:
4430 if (PyTuple_Check(w)) {
4431 Py_ssize_t i, length;
4432 length = PyTuple_Size(w);
4433 for (i = 0; i < length; i += 1) {
4434 PyObject *exc = PyTuple_GET_ITEM(w, i);
4435 if (!PyExceptionClass_Check(exc)) {
4436 PyErr_SetString(PyExc_TypeError,
4437 CANNOT_CATCH_MSG);
4438 return NULL;
4439 }
4440 }
4441 }
4442 else {
4443 if (!PyExceptionClass_Check(w)) {
4444 PyErr_SetString(PyExc_TypeError,
4445 CANNOT_CATCH_MSG);
4446 return NULL;
4447 }
4448 }
4449 res = PyErr_GivenExceptionMatches(v, w);
4450 break;
4451 default:
4452 return PyObject_RichCompare(v, w, op);
4453 }
4454 v = res ? Py_True : Py_False;
4455 Py_INCREF(v);
4456 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004457}
4458
Thomas Wouters52152252000-08-17 22:55:00 +00004459static PyObject *
4460import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 x = PyObject_GetAttr(v, name);
4465 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4466 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4467 }
4468 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004469}
Guido van Rossumac7be682001-01-17 15:42:30 +00004470
Thomas Wouters52152252000-08-17 22:55:00 +00004471static int
4472import_all_from(PyObject *locals, PyObject *v)
4473{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004474 _Py_IDENTIFIER(__all__);
4475 _Py_IDENTIFIER(__dict__);
4476 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 PyObject *dict, *name, *value;
4478 int skip_leading_underscores = 0;
4479 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 if (all == NULL) {
4482 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4483 return -1; /* Unexpected error */
4484 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004485 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 if (dict == NULL) {
4487 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4488 return -1;
4489 PyErr_SetString(PyExc_ImportError,
4490 "from-import-* object has no __dict__ and no __all__");
4491 return -1;
4492 }
4493 all = PyMapping_Keys(dict);
4494 Py_DECREF(dict);
4495 if (all == NULL)
4496 return -1;
4497 skip_leading_underscores = 1;
4498 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 for (pos = 0, err = 0; ; pos++) {
4501 name = PySequence_GetItem(all, pos);
4502 if (name == NULL) {
4503 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4504 err = -1;
4505 else
4506 PyErr_Clear();
4507 break;
4508 }
4509 if (skip_leading_underscores &&
4510 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004511 PyUnicode_READY(name) != -1 &&
4512 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004513 {
4514 Py_DECREF(name);
4515 continue;
4516 }
4517 value = PyObject_GetAttr(v, name);
4518 if (value == NULL)
4519 err = -1;
4520 else if (PyDict_CheckExact(locals))
4521 err = PyDict_SetItem(locals, name, value);
4522 else
4523 err = PyObject_SetItem(locals, name, value);
4524 Py_DECREF(name);
4525 Py_XDECREF(value);
4526 if (err != 0)
4527 break;
4528 }
4529 Py_DECREF(all);
4530 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004531}
4532
Guido van Rossumac7be682001-01-17 15:42:30 +00004533static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004534format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004536 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004538 if (!obj)
4539 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004541 obj_str = _PyUnicode_AsString(obj);
4542 if (!obj_str)
4543 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004545 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004546}
Guido van Rossum950361c1997-01-24 13:49:28 +00004547
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004548static void
4549format_exc_unbound(PyCodeObject *co, int oparg)
4550{
4551 PyObject *name;
4552 /* Don't stomp existing exception */
4553 if (PyErr_Occurred())
4554 return;
4555 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4556 name = PyTuple_GET_ITEM(co->co_cellvars,
4557 oparg);
4558 format_exc_check_arg(
4559 PyExc_UnboundLocalError,
4560 UNBOUNDLOCAL_ERROR_MSG,
4561 name);
4562 } else {
4563 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4564 PyTuple_GET_SIZE(co->co_cellvars));
4565 format_exc_check_arg(PyExc_NameError,
4566 UNBOUNDFREE_ERROR_MSG, name);
4567 }
4568}
4569
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004570static PyObject *
4571unicode_concatenate(PyObject *v, PyObject *w,
4572 PyFrameObject *f, unsigned char *next_instr)
4573{
4574 PyObject *res;
4575 if (Py_REFCNT(v) == 2) {
4576 /* In the common case, there are 2 references to the value
4577 * stored in 'variable' when the += is performed: one on the
4578 * value stack (in 'v') and one still stored in the
4579 * 'variable'. We try to delete the variable now to reduce
4580 * the refcnt to 1.
4581 */
4582 switch (*next_instr) {
4583 case STORE_FAST:
4584 {
4585 int oparg = PEEKARG();
4586 PyObject **fastlocals = f->f_localsplus;
4587 if (GETLOCAL(oparg) == v)
4588 SETLOCAL(oparg, NULL);
4589 break;
4590 }
4591 case STORE_DEREF:
4592 {
4593 PyObject **freevars = (f->f_localsplus +
4594 f->f_code->co_nlocals);
4595 PyObject *c = freevars[PEEKARG()];
4596 if (PyCell_GET(c) == v)
4597 PyCell_Set(c, NULL);
4598 break;
4599 }
4600 case STORE_NAME:
4601 {
4602 PyObject *names = f->f_code->co_names;
4603 PyObject *name = GETITEM(names, PEEKARG());
4604 PyObject *locals = f->f_locals;
4605 if (PyDict_CheckExact(locals) &&
4606 PyDict_GetItem(locals, name) == v) {
4607 if (PyDict_DelItem(locals, name) != 0) {
4608 PyErr_Clear();
4609 }
4610 }
4611 break;
4612 }
4613 }
4614 }
4615 res = v;
4616 PyUnicode_Append(&res, w);
4617 return res;
4618}
4619
Guido van Rossum950361c1997-01-24 13:49:28 +00004620#ifdef DYNAMIC_EXECUTION_PROFILE
4621
Skip Montanarof118cb12001-10-15 20:51:38 +00004622static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004623getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004624{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004625 int i;
4626 PyObject *l = PyList_New(256);
4627 if (l == NULL) return NULL;
4628 for (i = 0; i < 256; i++) {
4629 PyObject *x = PyLong_FromLong(a[i]);
4630 if (x == NULL) {
4631 Py_DECREF(l);
4632 return NULL;
4633 }
4634 PyList_SetItem(l, i, x);
4635 }
4636 for (i = 0; i < 256; i++)
4637 a[i] = 0;
4638 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004639}
4640
4641PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004642_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004643{
4644#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004646#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 int i;
4648 PyObject *l = PyList_New(257);
4649 if (l == NULL) return NULL;
4650 for (i = 0; i < 257; i++) {
4651 PyObject *x = getarray(dxpairs[i]);
4652 if (x == NULL) {
4653 Py_DECREF(l);
4654 return NULL;
4655 }
4656 PyList_SetItem(l, i, x);
4657 }
4658 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004659#endif
4660}
4661
4662#endif