blob: 54980565bf6ee22021eaf630d97ac35bd61b9b0b [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000017#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018
Guido van Rossumc6004111993-11-05 10:22:19 +000019#include <ctype.h>
20
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000022
23#define READ_TIMESTAMP(var)
24
25#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000026
27typedef unsigned long long uint64;
28
Ezio Melotti13925002011-03-16 11:05:33 +020029/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000030 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
31 "__powerpc__" appears to be the correct one for Linux with GCC
32*/
33#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +0200139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Petersonce798522012-01-22 11:24:29 -0500141static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000147#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000149#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "free variable '%.200s' referenced before assignment" \
151 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000152
Guido van Rossum950361c1997-01-24 13:49:28 +0000153/* Dynamic execution profile */
154#ifdef DYNAMIC_EXECUTION_PROFILE
155#ifdef DXPAIRS
156static long dxpairs[257][256];
157#define dxp dxpairs[256]
158#else
159static long dxp[256];
160#endif
161#endif
162
Jeremy Hylton985eba52003-02-05 23:13:00 +0000163/* Function call profile */
164#ifdef CALL_PROFILE
165#define PCALL_NUM 11
166static int pcall[PCALL_NUM];
167
168#define PCALL_ALL 0
169#define PCALL_FUNCTION 1
170#define PCALL_FAST_FUNCTION 2
171#define PCALL_FASTER_FUNCTION 3
172#define PCALL_METHOD 4
173#define PCALL_BOUND_METHOD 5
174#define PCALL_CFUNCTION 6
175#define PCALL_TYPE 7
176#define PCALL_GENERATOR 8
177#define PCALL_OTHER 9
178#define PCALL_POP 10
179
180/* Notes about the statistics
181
182 PCALL_FAST stats
183
184 FAST_FUNCTION means no argument tuple needs to be created.
185 FASTER_FUNCTION means that the fast-path frame setup code is used.
186
187 If there is a method call where the call can be optimized by changing
188 the argument tuple and calling the function directly, it gets recorded
189 twice.
190
191 As a result, the relationship among the statistics appears to be
192 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
193 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
194 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
195 PCALL_METHOD > PCALL_BOUND_METHOD
196*/
197
198#define PCALL(POS) pcall[POS]++
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return Py_BuildValue("iiiiiiiiiii",
204 pcall[0], pcall[1], pcall[2], pcall[3],
205 pcall[4], pcall[5], pcall[6], pcall[7],
206 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000207}
208#else
209#define PCALL(O)
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#endif
218
Tim Peters5ca576e2001-06-18 22:08:13 +0000219
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000220#ifdef WITH_THREAD
221#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
222#else
223#define GIL_REQUEST 0
224#endif
225
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000226/* This can set eval_breaker to 0 even though gil_drop_request became
227 1. We believe this is all right because the eval loop will release
228 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000229#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 _Py_atomic_store_relaxed( \
231 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000232 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
234 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000236#ifdef WITH_THREAD
237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 do { \
240 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
241 _Py_atomic_store_relaxed(&eval_breaker, 1); \
242 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243
244#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
247 COMPUTE_EVAL_BREAKER(); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000250#endif
251
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000252/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 do { \
255 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
256 _Py_atomic_store_relaxed(&eval_breaker, 1); \
257 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
259#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
262 COMPUTE_EVAL_BREAKER(); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 pending_async_exc = 1; \
268 _Py_atomic_store_relaxed(&eval_breaker, 1); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274
Guido van Rossume59214e1994-08-30 08:01:59 +0000275#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000276
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000279#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000280#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000282static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000283static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284/* This single variable consolidates all requests to break out of the fast path
285 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000286static _Py_atomic_int eval_breaker = {0};
287/* Request for dropping the GIL */
288static _Py_atomic_int gil_drop_request = {0};
289/* Request for running pending calls. */
290static _Py_atomic_int pendingcalls_to_do = {0};
291/* Request for looking at the `async_exc` field of the current thread state.
292 Guarded by the GIL. */
293static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000294
295#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296
Tim Peters7f468f22004-10-11 02:40:51 +0000297int
298PyEval_ThreadsInitialized(void)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000301}
302
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (gil_created())
307 return;
308 create_gil();
309 take_gil(PyThreadState_GET());
310 main_thread = PyThread_get_thread_ident();
311 if (!pending_lock)
312 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000316_PyEval_FiniThreads(void)
317{
318 if (!gil_created())
319 return;
320 destroy_gil();
321 assert(!gil_created());
322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
330 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000331}
332
333void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* This function must succeed when the current thread state is NULL.
337 We therefore avoid PyThreadState_GET() which dumps a fatal error
338 in debug mode.
339 */
340 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
341 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (tstate == NULL)
348 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
349 /* Check someone has called PyEval_InitThreads() to create the lock */
350 assert(gil_created());
351 take_gil(tstate);
352 if (PyThreadState_Swap(tstate) != NULL)
353 Py_FatalError(
354 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000355}
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (tstate == NULL)
361 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
362 if (PyThreadState_Swap(NULL) != tstate)
363 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
364 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000366
367/* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */
371
372void
373PyEval_ReInitThreads(void)
374{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200375 _Py_IDENTIFIER(_after_fork);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyObject *threading, *result;
377 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (!gil_created())
380 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 recreate_gil();
382 pending_lock = PyThread_allocate_lock();
383 take_gil(tstate);
384 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 /* Update the threading module with the new state.
387 */
388 tstate = PyThreadState_GET();
389 threading = PyMapping_GetItemString(tstate->interp->modules,
390 "threading");
391 if (threading == NULL) {
392 /* threading not imported */
393 PyErr_Clear();
394 return;
395 }
Martin v. Löwisafe55bb2011-10-09 10:38:36 +0200396 result = _PyObject_CallMethodId(threading, &PyId__after_fork, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 if (result == NULL)
398 PyErr_WriteUnraisable(threading);
399 else
400 Py_DECREF(result);
401 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000402}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000403
404#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000405static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000406static int pending_async_exc = 0;
407#endif /* WITH_THREAD */
408
409/* This function is used to signal that async exceptions are waiting to be
410 raised, therefore it is also useful in non-threaded builds. */
411
412void
413_PyEval_SignalAsyncExc(void)
414{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000416}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417
Guido van Rossumff4949e1992-08-05 19:58:53 +0000418/* Functions save_thread and restore_thread are always defined so
419 dynamically loaded modules needn't be compiled separately for use
420 with and without threads: */
421
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000422PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000423PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000424{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 PyThreadState *tstate = PyThreadState_Swap(NULL);
426 if (tstate == NULL)
427 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000428#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 if (gil_created())
430 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433}
434
435void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000436PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000437{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 if (tstate == NULL)
439 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000440#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 if (gil_created()) {
442 int err = errno;
443 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200444 /* _Py_Finalizing is protected by the GIL */
445 if (_Py_Finalizing && tstate != _Py_Finalizing) {
446 drop_gil(tstate);
447 PyThread_exit_thread();
448 assert(0); /* unreachable */
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 errno = err;
451 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000452#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000453 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000454}
455
456
Guido van Rossuma9672091994-09-14 13:31:22 +0000457/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
458 signal handlers or Mac I/O completion routines) can schedule calls
459 to a function to be called synchronously.
460 The synchronous function is called with one void* argument.
461 It should return 0 for success or -1 for failure -- failure should
462 be accompanied by an exception.
463
464 If registry succeeds, the registry function returns 0; if it fails
465 (e.g. due to too many pending calls) it returns -1 (without setting
466 an exception condition).
467
468 Note that because registry may occur from within signal handlers,
469 or other asynchronous events, calling malloc() is unsafe!
470
471#ifdef WITH_THREAD
472 Any thread can schedule pending calls, but only the main thread
473 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474 There is no facility to schedule calls to a particular thread, but
475 that should be easy to change, should that ever be required. In
476 that case, the static variables here should go into the python
477 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000478#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000479*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000480
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481#ifdef WITH_THREAD
482
483/* The WITH_THREAD implementation is thread-safe. It allows
484 scheduling to be made from any thread, and even from an executing
485 callback.
486 */
487
488#define NPENDINGCALLS 32
489static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 int (*func)(void *);
491 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000492} pendingcalls[NPENDINGCALLS];
493static int pendingfirst = 0;
494static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000495
496int
497Py_AddPendingCall(int (*func)(void *), void *arg)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we
505 * hold the lock during the Py_MakePendingCalls() function.
506 * This avoids a deadlock in that case.
507 * Note that signals can be delivered on any thread. In particular,
508 * on Windows, a SIGINT is delivered on a system-created worker
509 * thread.
510 * We also check for lock being NULL, in the unlikely case that
511 * this function is called before any bytecode evaluation takes place.
512 */
513 if (lock != NULL) {
514 for (i = 0; i<100; i++) {
515 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
516 break;
517 }
518 if (i == 100)
519 return -1;
520 }
521
522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
524 if (j == pendingfirst) {
525 result = -1; /* Queue full */
526 } else {
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
530 }
531 /* signal main loop */
532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL)
534 PyThread_release_lock(lock);
535 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538int
539Py_MakePendingCalls(void)
540{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200541 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000542 int i;
543 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 if (!pending_lock) {
546 /* initial allocation of the lock */
547 pending_lock = PyThread_allocate_lock();
548 if (pending_lock == NULL)
549 return -1;
550 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 /* only service pending calls on main thread */
553 if (main_thread && PyThread_get_thread_ident() != main_thread)
554 return 0;
555 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200556 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200558 busy = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 /* perform a bounded number of calls, in case of recursion */
560 for (i=0; i<NPENDINGCALLS; i++) {
561 int j;
562 int (*func)(void *);
563 void *arg = NULL;
564
565 /* pop one item off the queue while holding the lock */
566 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
567 j = pendingfirst;
568 if (j == pendinglast) {
569 func = NULL; /* Queue empty */
570 } else {
571 func = pendingcalls[j].func;
572 arg = pendingcalls[j].arg;
573 pendingfirst = (j + 1) % NPENDINGCALLS;
574 }
575 if (pendingfirst != pendinglast)
576 SIGNAL_PENDING_CALLS();
577 else
578 UNSIGNAL_PENDING_CALLS();
579 PyThread_release_lock(pending_lock);
580 /* having released the lock, perform the callback */
581 if (func == NULL)
582 break;
583 r = func(arg);
584 if (r)
585 break;
586 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200587 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000589}
590
591#else /* if ! defined WITH_THREAD */
592
593/*
594 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
595 This code is used for signal handling in python that isn't built
596 with WITH_THREAD.
597 Don't use this implementation when Py_AddPendingCalls() can happen
598 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599
Guido van Rossuma9672091994-09-14 13:31:22 +0000600 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601 (1) nested asynchronous calls to Py_AddPendingCall()
602 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000604 (1) is very unlikely because typically signal delivery
605 is blocked during signal handling. So it should be impossible.
606 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000607 The current code is safe against (2), but not against (1).
608 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000609 thread is present, interrupted by signals, and that the critical
610 section is protected with the "busy" variable. On Windows, which
611 delivers SIGINT on a system thread, this does not hold and therefore
612 Windows really shouldn't use this version.
613 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000614*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000615
Guido van Rossuma9672091994-09-14 13:31:22 +0000616#define NPENDINGCALLS 32
617static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 int (*func)(void *);
619 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000620} pendingcalls[NPENDINGCALLS];
621static volatile int pendingfirst = 0;
622static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000623static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000624
625int
Thomas Wouters334fb892000-07-25 12:56:38 +0000626Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 static volatile int busy = 0;
629 int i, j;
630 /* XXX Begin critical section */
631 if (busy)
632 return -1;
633 busy = 1;
634 i = pendinglast;
635 j = (i + 1) % NPENDINGCALLS;
636 if (j == pendingfirst) {
637 busy = 0;
638 return -1; /* Queue full */
639 }
640 pendingcalls[i].func = func;
641 pendingcalls[i].arg = arg;
642 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000643
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 SIGNAL_PENDING_CALLS();
645 busy = 0;
646 /* XXX End critical section */
647 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000648}
649
Guido van Rossum180d7b41994-09-29 09:45:57 +0000650int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000651Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000652{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 static int busy = 0;
654 if (busy)
655 return 0;
656 busy = 1;
657 UNSIGNAL_PENDING_CALLS();
658 for (;;) {
659 int i;
660 int (*func)(void *);
661 void *arg;
662 i = pendingfirst;
663 if (i == pendinglast)
664 break; /* Queue empty */
665 func = pendingcalls[i].func;
666 arg = pendingcalls[i].arg;
667 pendingfirst = (i + 1) % NPENDINGCALLS;
668 if (func(arg) < 0) {
669 busy = 0;
670 SIGNAL_PENDING_CALLS(); /* We're not done yet */
671 return -1;
672 }
673 }
674 busy = 0;
675 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000676}
677
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000678#endif /* WITH_THREAD */
679
Guido van Rossuma9672091994-09-14 13:31:22 +0000680
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000681/* The interpreter's recursion limit */
682
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000683#ifndef Py_DEFAULT_RECURSION_LIMIT
684#define Py_DEFAULT_RECURSION_LIMIT 1000
685#endif
686static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
687int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000688
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000689int
690Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000691{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000693}
694
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000695void
696Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 recursion_limit = new_limit;
699 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000700}
701
Armin Rigo2b3eb402003-10-28 12:05:48 +0000702/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
703 if the recursion_depth reaches _Py_CheckRecursionLimit.
704 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
705 to guarantee that _Py_CheckRecursiveCall() is regularly called.
706 Without USE_STACKCHECK, there is no need for this. */
707int
708_Py_CheckRecursiveCall(char *where)
709{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711
712#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 if (PyOS_CheckStack()) {
714 --tstate->recursion_depth;
715 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
716 return -1;
717 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000718#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 _Py_CheckRecursionLimit = recursion_limit;
720 if (tstate->recursion_critical)
721 /* Somebody asked that we don't check for recursion. */
722 return 0;
723 if (tstate->overflowed) {
724 if (tstate->recursion_depth > recursion_limit + 50) {
725 /* Overflowing while handling an overflow. Give up. */
726 Py_FatalError("Cannot recover from stack overflow.");
727 }
728 return 0;
729 }
730 if (tstate->recursion_depth > recursion_limit) {
731 --tstate->recursion_depth;
732 tstate->overflowed = 1;
733 PyErr_Format(PyExc_RuntimeError,
734 "maximum recursion depth exceeded%s",
735 where);
736 return -1;
737 }
738 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000739}
740
Guido van Rossum374a9221991-04-04 10:40:29 +0000741/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000742enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000743 WHY_NOT = 0x0001, /* No error */
744 WHY_EXCEPTION = 0x0002, /* Exception occurred */
745 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
746 WHY_RETURN = 0x0008, /* 'return' statement */
747 WHY_BREAK = 0x0010, /* 'break' statement */
748 WHY_CONTINUE = 0x0020, /* 'continue' statement */
749 WHY_YIELD = 0x0040, /* 'yield' operator */
750 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000751};
Guido van Rossum374a9221991-04-04 10:40:29 +0000752
Benjamin Peterson87880242011-07-03 16:48:31 -0500753static void save_exc_state(PyThreadState *, PyFrameObject *);
754static void swap_exc_state(PyThreadState *, PyFrameObject *);
755static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Collin Winter828f04a2007-08-31 00:04:24 +0000756static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000757static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000758
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000759/* Records whether tracing is on for any thread. Counts the number of
760 threads for which tstate->c_tracefunc is non-NULL, so if the value
761 is 0, we know we don't have to check this thread's c_tracefunc.
762 This speeds up the if statement in PyEval_EvalFrameEx() after
763 fast_next_opcode*/
764static int _Py_TracingPossible = 0;
765
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000766
Guido van Rossum374a9221991-04-04 10:40:29 +0000767
Guido van Rossumb209a111997-04-29 18:18:01 +0000768PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000769PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 return PyEval_EvalCodeEx(co,
772 globals, locals,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 (PyObject **)NULL, 0,
776 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000777}
778
779
780/* Interpreter main loop */
781
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000782PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000783PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* This is for backward compatibility with extension modules that
785 used this API; core interpreter code should call
786 PyEval_EvalFrameEx() */
787 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000788}
789
790PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000791PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000792{
Guido van Rossum950361c1997-01-24 13:49:28 +0000793#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000795#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 register PyObject **stack_pointer; /* Next free slot in value stack */
797 register unsigned char *next_instr;
798 register int opcode; /* Current opcode */
799 register int oparg; /* Current opcode argument, if any */
800 register enum why_code why; /* Reason for block stack unwind */
801 register int err; /* Error status -- nonzero if error */
802 register PyObject *x; /* Result object -- NULL if error */
803 register PyObject *v; /* Temporary objects popped off stack */
804 register PyObject *w;
805 register PyObject *u;
806 register PyObject *t;
807 register PyObject **fastlocals, **freevars;
808 PyObject *retval = NULL; /* Return value */
809 PyThreadState *tstate = PyThreadState_GET();
810 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000816 is true when the line being executed has changed. The
817 initial values are such as to make this false the first
818 time it is tested. */
819 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 unsigned char *first_instr;
822 PyObject *names;
823 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000824
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.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001173 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1174 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001175
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 When the PREDICT() macros are enabled, some opcode pairs follow in
1177 direct succession without updating f->f_lasti. A successful
1178 prediction effectively links the two codes together as if they
1179 were a single new opcode; accordingly,f->f_lasti will point to
1180 the first code in the pair (for instance, GET_ITER followed by
1181 FOR_ITER is effectively a single opcode and f->f_lasti will point
1182 at to the beginning of the combined pair.)
1183 */
1184 next_instr = first_instr + f->f_lasti + 1;
1185 stack_pointer = f->f_stacktop;
1186 assert(stack_pointer != NULL);
1187 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001188
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (co->co_flags & CO_GENERATOR && !throwflag) {
1190 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1191 /* We were in an except handler when we left,
1192 restore the exception state which was put aside
1193 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001194 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001196 else
1197 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001199
Tim Peters5ca576e2001-06-18 22:08:13 +00001200#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001202#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 why = WHY_NOT;
1205 err = 0;
1206 x = Py_None; /* Not a reference, just anything non-NULL */
1207 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (throwflag) { /* support for generator.throw() */
1210 why = WHY_EXCEPTION;
1211 goto on_error;
1212 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001215#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 if (inst1 == 0) {
1217 /* Almost surely, the opcode executed a break
1218 or a continue, preventing inst1 from being set
1219 on the way out of the loop.
1220 */
1221 READ_TIMESTAMP(inst1);
1222 loop1 = inst1;
1223 }
1224 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1225 intr0, intr1);
1226 ticked = 0;
1227 inst1 = 0;
1228 intr0 = 0;
1229 intr1 = 0;
1230 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001231#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1233 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 /* Do periodic things. Doing this every time through
1236 the loop would add too much overhead, so we do it
1237 only every Nth instruction. We also do it if
1238 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1239 event needs attention (e.g. a signal handler or
1240 async I/O handler); see Py_AddPendingCall() and
1241 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1244 if (*next_instr == SETUP_FINALLY) {
1245 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001246 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 goto fast_next_opcode;
1248 }
1249 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001250#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1254 if (Py_MakePendingCalls() < 0) {
1255 why = WHY_EXCEPTION;
1256 goto on_error;
1257 }
1258 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001259#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001260 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 /* Give another thread a chance */
1262 if (PyThreadState_Swap(NULL) != tstate)
1263 Py_FatalError("ceval: tstate mix-up");
1264 drop_gil(tstate);
1265
1266 /* Other threads may run now */
1267
1268 take_gil(tstate);
1269 if (PyThreadState_Swap(tstate) != NULL)
1270 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001272#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* Check for asynchronous exceptions. */
1274 if (tstate->async_exc != NULL) {
1275 x = tstate->async_exc;
1276 tstate->async_exc = NULL;
1277 UNSIGNAL_ASYNC_EXC();
1278 PyErr_SetNone(x);
1279 Py_DECREF(x);
1280 why = WHY_EXCEPTION;
1281 goto on_error;
1282 }
1283 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 fast_next_opcode:
1286 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 if (_Py_TracingPossible &&
1291 tstate->c_tracefunc != NULL && !tstate->tracing) {
1292 /* see maybe_call_line_trace
1293 for expository comments */
1294 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 err = maybe_call_line_trace(tstate->c_tracefunc,
1297 tstate->c_traceobj,
1298 f, &instr_lb, &instr_ub,
1299 &instr_prev);
1300 /* Reload possibly changed frame fields */
1301 JUMPTO(f->f_lasti);
1302 if (f->f_stacktop != NULL) {
1303 stack_pointer = f->f_stacktop;
1304 f->f_stacktop = NULL;
1305 }
1306 if (err) {
1307 /* trace function raised an exception */
1308 goto on_error;
1309 }
1310 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 opcode = NEXTOP();
1315 oparg = 0; /* allows oparg to be stored in a register because
1316 it doesn't have to be remembered across a full loop */
1317 if (HAS_ARG(opcode))
1318 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001319 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001320#ifdef DYNAMIC_EXECUTION_PROFILE
1321#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 dxpairs[lastopcode][opcode]++;
1323 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001324#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001326#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001327
Guido van Rossum96a42c81992-01-12 02:29:51 +00001328#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 if (lltrace) {
1332 if (HAS_ARG(opcode)) {
1333 printf("%d: %d, %d\n",
1334 f->f_lasti, opcode, oparg);
1335 }
1336 else {
1337 printf("%d: %d\n",
1338 f->f_lasti, opcode);
1339 }
1340 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001341#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001342
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 /* Main switch on opcode */
1344 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 /* BEWARE!
1349 It is essential that any operation that fails sets either
1350 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1351 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 TARGET(NOP)
1354 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001355
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 TARGET(LOAD_FAST)
1357 x = GETLOCAL(oparg);
1358 if (x != NULL) {
1359 Py_INCREF(x);
1360 PUSH(x);
1361 FAST_DISPATCH();
1362 }
1363 format_exc_check_arg(PyExc_UnboundLocalError,
1364 UNBOUNDLOCAL_ERROR_MSG,
1365 PyTuple_GetItem(co->co_varnames, oparg));
1366 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 TARGET(LOAD_CONST)
1369 x = GETITEM(consts, oparg);
1370 Py_INCREF(x);
1371 PUSH(x);
1372 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 PREDICTED_WITH_ARG(STORE_FAST);
1375 TARGET(STORE_FAST)
1376 v = POP();
1377 SETLOCAL(oparg, v);
1378 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 TARGET(POP_TOP)
1381 v = POP();
1382 Py_DECREF(v);
1383 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001385 TARGET(ROT_TWO)
1386 v = TOP();
1387 w = SECOND();
1388 SET_TOP(w);
1389 SET_SECOND(v);
1390 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 TARGET(ROT_THREE)
1393 v = TOP();
1394 w = SECOND();
1395 x = THIRD();
1396 SET_TOP(w);
1397 SET_SECOND(x);
1398 SET_THIRD(v);
1399 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 TARGET(DUP_TOP)
1402 v = TOP();
1403 Py_INCREF(v);
1404 PUSH(v);
1405 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001407 TARGET(DUP_TOP_TWO)
1408 x = TOP();
1409 Py_INCREF(x);
1410 w = SECOND();
1411 Py_INCREF(w);
1412 STACKADJ(2);
1413 SET_TOP(x);
1414 SET_SECOND(w);
1415 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 TARGET(UNARY_POSITIVE)
1418 v = TOP();
1419 x = PyNumber_Positive(v);
1420 Py_DECREF(v);
1421 SET_TOP(x);
1422 if (x != NULL) DISPATCH();
1423 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 TARGET(UNARY_NEGATIVE)
1426 v = TOP();
1427 x = PyNumber_Negative(v);
1428 Py_DECREF(v);
1429 SET_TOP(x);
1430 if (x != NULL) DISPATCH();
1431 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 TARGET(UNARY_NOT)
1434 v = TOP();
1435 err = PyObject_IsTrue(v);
1436 Py_DECREF(v);
1437 if (err == 0) {
1438 Py_INCREF(Py_True);
1439 SET_TOP(Py_True);
1440 DISPATCH();
1441 }
1442 else if (err > 0) {
1443 Py_INCREF(Py_False);
1444 SET_TOP(Py_False);
1445 err = 0;
1446 DISPATCH();
1447 }
1448 STACKADJ(-1);
1449 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001451 TARGET(UNARY_INVERT)
1452 v = TOP();
1453 x = PyNumber_Invert(v);
1454 Py_DECREF(v);
1455 SET_TOP(x);
1456 if (x != NULL) DISPATCH();
1457 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001459 TARGET(BINARY_POWER)
1460 w = POP();
1461 v = TOP();
1462 x = PyNumber_Power(v, w, Py_None);
1463 Py_DECREF(v);
1464 Py_DECREF(w);
1465 SET_TOP(x);
1466 if (x != NULL) DISPATCH();
1467 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 TARGET(BINARY_MULTIPLY)
1470 w = POP();
1471 v = TOP();
1472 x = PyNumber_Multiply(v, w);
1473 Py_DECREF(v);
1474 Py_DECREF(w);
1475 SET_TOP(x);
1476 if (x != NULL) DISPATCH();
1477 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001478
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001479 TARGET(BINARY_TRUE_DIVIDE)
1480 w = POP();
1481 v = TOP();
1482 x = PyNumber_TrueDivide(v, w);
1483 Py_DECREF(v);
1484 Py_DECREF(w);
1485 SET_TOP(x);
1486 if (x != NULL) DISPATCH();
1487 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 TARGET(BINARY_FLOOR_DIVIDE)
1490 w = POP();
1491 v = TOP();
1492 x = PyNumber_FloorDivide(v, w);
1493 Py_DECREF(v);
1494 Py_DECREF(w);
1495 SET_TOP(x);
1496 if (x != NULL) DISPATCH();
1497 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 TARGET(BINARY_MODULO)
1500 w = POP();
1501 v = TOP();
1502 if (PyUnicode_CheckExact(v))
1503 x = PyUnicode_Format(v, w);
1504 else
1505 x = PyNumber_Remainder(v, w);
1506 Py_DECREF(v);
1507 Py_DECREF(w);
1508 SET_TOP(x);
1509 if (x != NULL) DISPATCH();
1510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 TARGET(BINARY_ADD)
1513 w = POP();
1514 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001515 if (PyUnicode_CheckExact(v) &&
1516 PyUnicode_CheckExact(w)) {
1517 x = unicode_concatenate(v, w, f, next_instr);
1518 /* unicode_concatenate consumed the ref to v */
1519 goto skip_decref_vx;
1520 }
1521 else {
1522 x = PyNumber_Add(v, w);
1523 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001525 skip_decref_vx:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 Py_DECREF(w);
1527 SET_TOP(x);
1528 if (x != NULL) DISPATCH();
1529 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 TARGET(BINARY_SUBTRACT)
1532 w = POP();
1533 v = TOP();
1534 x = PyNumber_Subtract(v, w);
1535 Py_DECREF(v);
1536 Py_DECREF(w);
1537 SET_TOP(x);
1538 if (x != NULL) DISPATCH();
1539 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 TARGET(BINARY_SUBSCR)
1542 w = POP();
1543 v = TOP();
1544 x = PyObject_GetItem(v, w);
1545 Py_DECREF(v);
1546 Py_DECREF(w);
1547 SET_TOP(x);
1548 if (x != NULL) DISPATCH();
1549 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 TARGET(BINARY_LSHIFT)
1552 w = POP();
1553 v = TOP();
1554 x = PyNumber_Lshift(v, w);
1555 Py_DECREF(v);
1556 Py_DECREF(w);
1557 SET_TOP(x);
1558 if (x != NULL) DISPATCH();
1559 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 TARGET(BINARY_RSHIFT)
1562 w = POP();
1563 v = TOP();
1564 x = PyNumber_Rshift(v, w);
1565 Py_DECREF(v);
1566 Py_DECREF(w);
1567 SET_TOP(x);
1568 if (x != NULL) DISPATCH();
1569 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 TARGET(BINARY_AND)
1572 w = POP();
1573 v = TOP();
1574 x = PyNumber_And(v, w);
1575 Py_DECREF(v);
1576 Py_DECREF(w);
1577 SET_TOP(x);
1578 if (x != NULL) DISPATCH();
1579 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 TARGET(BINARY_XOR)
1582 w = POP();
1583 v = TOP();
1584 x = PyNumber_Xor(v, w);
1585 Py_DECREF(v);
1586 Py_DECREF(w);
1587 SET_TOP(x);
1588 if (x != NULL) DISPATCH();
1589 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 TARGET(BINARY_OR)
1592 w = POP();
1593 v = TOP();
1594 x = PyNumber_Or(v, w);
1595 Py_DECREF(v);
1596 Py_DECREF(w);
1597 SET_TOP(x);
1598 if (x != NULL) DISPATCH();
1599 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 TARGET(LIST_APPEND)
1602 w = POP();
1603 v = PEEK(oparg);
1604 err = PyList_Append(v, w);
1605 Py_DECREF(w);
1606 if (err == 0) {
1607 PREDICT(JUMP_ABSOLUTE);
1608 DISPATCH();
1609 }
1610 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 TARGET(SET_ADD)
1613 w = POP();
1614 v = stack_pointer[-oparg];
1615 err = PySet_Add(v, w);
1616 Py_DECREF(w);
1617 if (err == 0) {
1618 PREDICT(JUMP_ABSOLUTE);
1619 DISPATCH();
1620 }
1621 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 TARGET(INPLACE_POWER)
1624 w = POP();
1625 v = TOP();
1626 x = PyNumber_InPlacePower(v, w, Py_None);
1627 Py_DECREF(v);
1628 Py_DECREF(w);
1629 SET_TOP(x);
1630 if (x != NULL) DISPATCH();
1631 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 TARGET(INPLACE_MULTIPLY)
1634 w = POP();
1635 v = TOP();
1636 x = PyNumber_InPlaceMultiply(v, w);
1637 Py_DECREF(v);
1638 Py_DECREF(w);
1639 SET_TOP(x);
1640 if (x != NULL) DISPATCH();
1641 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643 TARGET(INPLACE_TRUE_DIVIDE)
1644 w = POP();
1645 v = TOP();
1646 x = PyNumber_InPlaceTrueDivide(v, w);
1647 Py_DECREF(v);
1648 Py_DECREF(w);
1649 SET_TOP(x);
1650 if (x != NULL) DISPATCH();
1651 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 TARGET(INPLACE_FLOOR_DIVIDE)
1654 w = POP();
1655 v = TOP();
1656 x = PyNumber_InPlaceFloorDivide(v, w);
1657 Py_DECREF(v);
1658 Py_DECREF(w);
1659 SET_TOP(x);
1660 if (x != NULL) DISPATCH();
1661 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 TARGET(INPLACE_MODULO)
1664 w = POP();
1665 v = TOP();
1666 x = PyNumber_InPlaceRemainder(v, w);
1667 Py_DECREF(v);
1668 Py_DECREF(w);
1669 SET_TOP(x);
1670 if (x != NULL) DISPATCH();
1671 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 TARGET(INPLACE_ADD)
1674 w = POP();
1675 v = TOP();
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001676 if (PyUnicode_CheckExact(v) &&
1677 PyUnicode_CheckExact(w)) {
1678 x = unicode_concatenate(v, w, f, next_instr);
1679 /* unicode_concatenate consumed the ref to v */
1680 goto skip_decref_v;
1681 }
1682 else {
1683 x = PyNumber_InPlaceAdd(v, w);
1684 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 Py_DECREF(v);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001686 skip_decref_v:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 Py_DECREF(w);
1688 SET_TOP(x);
1689 if (x != NULL) DISPATCH();
1690 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001692 TARGET(INPLACE_SUBTRACT)
1693 w = POP();
1694 v = TOP();
1695 x = PyNumber_InPlaceSubtract(v, w);
1696 Py_DECREF(v);
1697 Py_DECREF(w);
1698 SET_TOP(x);
1699 if (x != NULL) DISPATCH();
1700 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 TARGET(INPLACE_LSHIFT)
1703 w = POP();
1704 v = TOP();
1705 x = PyNumber_InPlaceLshift(v, w);
1706 Py_DECREF(v);
1707 Py_DECREF(w);
1708 SET_TOP(x);
1709 if (x != NULL) DISPATCH();
1710 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001712 TARGET(INPLACE_RSHIFT)
1713 w = POP();
1714 v = TOP();
1715 x = PyNumber_InPlaceRshift(v, w);
1716 Py_DECREF(v);
1717 Py_DECREF(w);
1718 SET_TOP(x);
1719 if (x != NULL) DISPATCH();
1720 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 TARGET(INPLACE_AND)
1723 w = POP();
1724 v = TOP();
1725 x = PyNumber_InPlaceAnd(v, w);
1726 Py_DECREF(v);
1727 Py_DECREF(w);
1728 SET_TOP(x);
1729 if (x != NULL) DISPATCH();
1730 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 TARGET(INPLACE_XOR)
1733 w = POP();
1734 v = TOP();
1735 x = PyNumber_InPlaceXor(v, w);
1736 Py_DECREF(v);
1737 Py_DECREF(w);
1738 SET_TOP(x);
1739 if (x != NULL) DISPATCH();
1740 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 TARGET(INPLACE_OR)
1743 w = POP();
1744 v = TOP();
1745 x = PyNumber_InPlaceOr(v, w);
1746 Py_DECREF(v);
1747 Py_DECREF(w);
1748 SET_TOP(x);
1749 if (x != NULL) DISPATCH();
1750 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 TARGET(STORE_SUBSCR)
1753 w = TOP();
1754 v = SECOND();
1755 u = THIRD();
1756 STACKADJ(-3);
1757 /* v[w] = u */
1758 err = PyObject_SetItem(v, w, u);
1759 Py_DECREF(u);
1760 Py_DECREF(v);
1761 Py_DECREF(w);
1762 if (err == 0) DISPATCH();
1763 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 TARGET(DELETE_SUBSCR)
1766 w = TOP();
1767 v = SECOND();
1768 STACKADJ(-2);
1769 /* del v[w] */
1770 err = PyObject_DelItem(v, w);
1771 Py_DECREF(v);
1772 Py_DECREF(w);
1773 if (err == 0) DISPATCH();
1774 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776 TARGET(PRINT_EXPR)
1777 v = POP();
1778 w = PySys_GetObject("displayhook");
1779 if (w == NULL) {
1780 PyErr_SetString(PyExc_RuntimeError,
1781 "lost sys.displayhook");
1782 err = -1;
1783 x = NULL;
1784 }
1785 if (err == 0) {
1786 x = PyTuple_Pack(1, v);
1787 if (x == NULL)
1788 err = -1;
1789 }
1790 if (err == 0) {
1791 w = PyEval_CallObject(w, x);
1792 Py_XDECREF(w);
1793 if (w == NULL)
1794 err = -1;
1795 }
1796 Py_DECREF(v);
1797 Py_XDECREF(x);
1798 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001799
Thomas Wouters434d0822000-08-24 20:11:32 +00001800#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001802#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001803 TARGET(RAISE_VARARGS)
1804 v = w = NULL;
1805 switch (oparg) {
1806 case 2:
1807 v = POP(); /* cause */
1808 case 1:
1809 w = POP(); /* exc */
1810 case 0: /* Fallthrough */
1811 why = do_raise(w, v);
1812 break;
1813 default:
1814 PyErr_SetString(PyExc_SystemError,
1815 "bad RAISE_VARARGS oparg");
1816 why = WHY_EXCEPTION;
1817 break;
1818 }
1819 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 TARGET(STORE_LOCALS)
1822 x = POP();
1823 v = f->f_locals;
1824 Py_XDECREF(v);
1825 f->f_locals = x;
1826 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 TARGET(RETURN_VALUE)
1829 retval = POP();
1830 why = WHY_RETURN;
1831 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001832
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001833 TARGET(YIELD_FROM)
1834 u = POP();
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001835 x = TOP();
1836 /* send u to x */
1837 if (PyGen_CheckExact(x)) {
1838 retval = _PyGen_Send((PyGenObject *)x, u);
1839 } else {
1840 if (u == Py_None)
1841 retval = PyIter_Next(x);
1842 else
1843 retval = PyObject_CallMethod(x, "send", "O", u);
1844 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001845 Py_DECREF(u);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001846 if (!retval) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001847 PyObject *val;
1848 x = POP(); /* Remove iter from stack */
1849 Py_DECREF(x);
1850 err = PyGen_FetchStopIterationValue(&val);
1851 if (err < 0) {
1852 x = NULL;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001853 break;
1854 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001855 x = val;
1856 PUSH(x);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001857 continue;
1858 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001859 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001860 f->f_stacktop = stack_pointer;
1861 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001862 /* and repeat... */
1863 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001864 goto fast_yield;
1865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 TARGET(YIELD_VALUE)
1867 retval = POP();
1868 f->f_stacktop = stack_pointer;
1869 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 TARGET(POP_EXCEPT)
1873 {
1874 PyTryBlock *b = PyFrame_BlockPop(f);
1875 if (b->b_type != EXCEPT_HANDLER) {
1876 PyErr_SetString(PyExc_SystemError,
1877 "popped block is not an except handler");
1878 why = WHY_EXCEPTION;
1879 break;
1880 }
1881 UNWIND_EXCEPT_HANDLER(b);
1882 }
1883 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001884
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 TARGET(POP_BLOCK)
1886 {
1887 PyTryBlock *b = PyFrame_BlockPop(f);
1888 UNWIND_BLOCK(b);
1889 }
1890 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 PREDICTED(END_FINALLY);
1893 TARGET(END_FINALLY)
1894 v = POP();
1895 if (PyLong_Check(v)) {
1896 why = (enum why_code) PyLong_AS_LONG(v);
1897 assert(why != WHY_YIELD);
1898 if (why == WHY_RETURN ||
1899 why == WHY_CONTINUE)
1900 retval = POP();
1901 if (why == WHY_SILENCED) {
1902 /* An exception was silenced by 'with', we must
1903 manually unwind the EXCEPT_HANDLER block which was
1904 created when the exception was caught, otherwise
1905 the stack will be in an inconsistent state. */
1906 PyTryBlock *b = PyFrame_BlockPop(f);
1907 assert(b->b_type == EXCEPT_HANDLER);
1908 UNWIND_EXCEPT_HANDLER(b);
1909 why = WHY_NOT;
1910 }
1911 }
1912 else if (PyExceptionClass_Check(v)) {
1913 w = POP();
1914 u = POP();
1915 PyErr_Restore(v, w, u);
1916 why = WHY_RERAISE;
1917 break;
1918 }
1919 else if (v != Py_None) {
1920 PyErr_SetString(PyExc_SystemError,
1921 "'finally' pops bad exception");
1922 why = WHY_EXCEPTION;
1923 }
1924 Py_DECREF(v);
1925 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 TARGET(LOAD_BUILD_CLASS)
1928 x = PyDict_GetItemString(f->f_builtins,
1929 "__build_class__");
1930 if (x == NULL) {
1931 PyErr_SetString(PyExc_ImportError,
1932 "__build_class__ not found");
1933 break;
1934 }
1935 Py_INCREF(x);
1936 PUSH(x);
1937 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 TARGET(STORE_NAME)
1940 w = GETITEM(names, oparg);
1941 v = POP();
1942 if ((x = f->f_locals) != NULL) {
1943 if (PyDict_CheckExact(x))
1944 err = PyDict_SetItem(x, w, v);
1945 else
1946 err = PyObject_SetItem(x, w, v);
1947 Py_DECREF(v);
1948 if (err == 0) DISPATCH();
1949 break;
1950 }
1951 PyErr_Format(PyExc_SystemError,
1952 "no locals found when storing %R", w);
1953 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 TARGET(DELETE_NAME)
1956 w = GETITEM(names, oparg);
1957 if ((x = f->f_locals) != NULL) {
1958 if ((err = PyObject_DelItem(x, w)) != 0)
1959 format_exc_check_arg(PyExc_NameError,
1960 NAME_ERROR_MSG,
1961 w);
1962 break;
1963 }
1964 PyErr_Format(PyExc_SystemError,
1965 "no locals when deleting %R", w);
1966 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001967
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1969 TARGET(UNPACK_SEQUENCE)
1970 v = POP();
1971 if (PyTuple_CheckExact(v) &&
1972 PyTuple_GET_SIZE(v) == oparg) {
1973 PyObject **items = \
1974 ((PyTupleObject *)v)->ob_item;
1975 while (oparg--) {
1976 w = items[oparg];
1977 Py_INCREF(w);
1978 PUSH(w);
1979 }
1980 Py_DECREF(v);
1981 DISPATCH();
1982 } else if (PyList_CheckExact(v) &&
1983 PyList_GET_SIZE(v) == oparg) {
1984 PyObject **items = \
1985 ((PyListObject *)v)->ob_item;
1986 while (oparg--) {
1987 w = items[oparg];
1988 Py_INCREF(w);
1989 PUSH(w);
1990 }
1991 } else if (unpack_iterable(v, oparg, -1,
1992 stack_pointer + oparg)) {
1993 STACKADJ(oparg);
1994 } else {
1995 /* unpack_iterable() raised an exception */
1996 why = WHY_EXCEPTION;
1997 }
1998 Py_DECREF(v);
1999 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002001 TARGET(UNPACK_EX)
2002 {
2003 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2004 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2007 stack_pointer + totalargs)) {
2008 stack_pointer += totalargs;
2009 } else {
2010 why = WHY_EXCEPTION;
2011 }
2012 Py_DECREF(v);
2013 break;
2014 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002016 TARGET(STORE_ATTR)
2017 w = GETITEM(names, oparg);
2018 v = TOP();
2019 u = SECOND();
2020 STACKADJ(-2);
2021 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2022 Py_DECREF(v);
2023 Py_DECREF(u);
2024 if (err == 0) DISPATCH();
2025 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 TARGET(DELETE_ATTR)
2028 w = GETITEM(names, oparg);
2029 v = POP();
2030 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2031 /* del v.w */
2032 Py_DECREF(v);
2033 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002034
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002035 TARGET(STORE_GLOBAL)
2036 w = GETITEM(names, oparg);
2037 v = POP();
2038 err = PyDict_SetItem(f->f_globals, w, v);
2039 Py_DECREF(v);
2040 if (err == 0) DISPATCH();
2041 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002043 TARGET(DELETE_GLOBAL)
2044 w = GETITEM(names, oparg);
2045 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2046 format_exc_check_arg(
2047 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2048 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002050 TARGET(LOAD_NAME)
2051 w = GETITEM(names, oparg);
2052 if ((v = f->f_locals) == NULL) {
2053 PyErr_Format(PyExc_SystemError,
2054 "no locals when loading %R", w);
2055 why = WHY_EXCEPTION;
2056 break;
2057 }
2058 if (PyDict_CheckExact(v)) {
2059 x = PyDict_GetItem(v, w);
2060 Py_XINCREF(x);
2061 }
2062 else {
2063 x = PyObject_GetItem(v, w);
2064 if (x == NULL && PyErr_Occurred()) {
2065 if (!PyErr_ExceptionMatches(
2066 PyExc_KeyError))
2067 break;
2068 PyErr_Clear();
2069 }
2070 }
2071 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002072 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002074 x = PyDict_GetItem(f->f_builtins, w);
2075 if (x == NULL) {
2076 format_exc_check_arg(
2077 PyExc_NameError,
2078 NAME_ERROR_MSG, w);
2079 break;
2080 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 }
2082 Py_INCREF(x);
2083 }
2084 PUSH(x);
2085 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 TARGET(LOAD_GLOBAL)
2088 w = GETITEM(names, oparg);
2089 if (PyUnicode_CheckExact(w)) {
2090 /* Inline the PyDict_GetItem() calls.
2091 WARNING: this is an extreme speed hack.
2092 Do not try this at home. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02002093 Py_hash_t hash = ((PyASCIIObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 if (hash != -1) {
2095 PyDictObject *d;
2096 PyDictEntry *e;
2097 d = (PyDictObject *)(f->f_globals);
2098 e = d->ma_lookup(d, w, hash);
2099 if (e == NULL) {
2100 x = NULL;
2101 break;
2102 }
2103 x = e->me_value;
2104 if (x != NULL) {
2105 Py_INCREF(x);
2106 PUSH(x);
2107 DISPATCH();
2108 }
2109 d = (PyDictObject *)(f->f_builtins);
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 goto load_global_error;
2122 }
2123 }
2124 /* This is the un-inlined version of the code above */
2125 x = PyDict_GetItem(f->f_globals, w);
2126 if (x == NULL) {
2127 x = PyDict_GetItem(f->f_builtins, w);
2128 if (x == NULL) {
2129 load_global_error:
2130 format_exc_check_arg(
2131 PyExc_NameError,
2132 GLOBAL_NAME_ERROR_MSG, w);
2133 break;
2134 }
2135 }
2136 Py_INCREF(x);
2137 PUSH(x);
2138 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002140 TARGET(DELETE_FAST)
2141 x = GETLOCAL(oparg);
2142 if (x != NULL) {
2143 SETLOCAL(oparg, NULL);
2144 DISPATCH();
2145 }
2146 format_exc_check_arg(
2147 PyExc_UnboundLocalError,
2148 UNBOUNDLOCAL_ERROR_MSG,
2149 PyTuple_GetItem(co->co_varnames, oparg)
2150 );
2151 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002152
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002153 TARGET(DELETE_DEREF)
2154 x = freevars[oparg];
2155 if (PyCell_GET(x) != NULL) {
2156 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002157 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002158 }
2159 err = -1;
2160 format_exc_unbound(co, oparg);
2161 break;
2162
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 TARGET(LOAD_CLOSURE)
2164 x = freevars[oparg];
2165 Py_INCREF(x);
2166 PUSH(x);
2167 if (x != NULL) DISPATCH();
2168 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002170 TARGET(LOAD_DEREF)
2171 x = freevars[oparg];
2172 w = PyCell_Get(x);
2173 if (w != NULL) {
2174 PUSH(w);
2175 DISPATCH();
2176 }
2177 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002178 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002179 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 TARGET(STORE_DEREF)
2182 w = POP();
2183 x = freevars[oparg];
2184 PyCell_Set(x, w);
2185 Py_DECREF(w);
2186 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 TARGET(BUILD_TUPLE)
2189 x = PyTuple_New(oparg);
2190 if (x != NULL) {
2191 for (; --oparg >= 0;) {
2192 w = POP();
2193 PyTuple_SET_ITEM(x, oparg, w);
2194 }
2195 PUSH(x);
2196 DISPATCH();
2197 }
2198 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002200 TARGET(BUILD_LIST)
2201 x = PyList_New(oparg);
2202 if (x != NULL) {
2203 for (; --oparg >= 0;) {
2204 w = POP();
2205 PyList_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_SET)
2213 x = PySet_New(NULL);
2214 if (x != NULL) {
2215 for (; --oparg >= 0;) {
2216 w = POP();
2217 if (err == 0)
2218 err = PySet_Add(x, w);
2219 Py_DECREF(w);
2220 }
2221 if (err != 0) {
2222 Py_DECREF(x);
2223 break;
2224 }
2225 PUSH(x);
2226 DISPATCH();
2227 }
2228 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 TARGET(BUILD_MAP)
2231 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2232 PUSH(x);
2233 if (x != NULL) DISPATCH();
2234 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002236 TARGET(STORE_MAP)
2237 w = TOP(); /* key */
2238 u = SECOND(); /* value */
2239 v = THIRD(); /* dict */
2240 STACKADJ(-2);
2241 assert (PyDict_CheckExact(v));
2242 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2243 Py_DECREF(u);
2244 Py_DECREF(w);
2245 if (err == 0) DISPATCH();
2246 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 TARGET(MAP_ADD)
2249 w = TOP(); /* key */
2250 u = SECOND(); /* value */
2251 STACKADJ(-2);
2252 v = stack_pointer[-oparg]; /* dict */
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) {
2258 PREDICT(JUMP_ABSOLUTE);
2259 DISPATCH();
2260 }
2261 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002263 TARGET(LOAD_ATTR)
2264 w = GETITEM(names, oparg);
2265 v = TOP();
2266 x = PyObject_GetAttr(v, w);
2267 Py_DECREF(v);
2268 SET_TOP(x);
2269 if (x != NULL) DISPATCH();
2270 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 TARGET(COMPARE_OP)
2273 w = POP();
2274 v = TOP();
2275 x = cmp_outcome(oparg, v, w);
2276 Py_DECREF(v);
2277 Py_DECREF(w);
2278 SET_TOP(x);
2279 if (x == NULL) break;
2280 PREDICT(POP_JUMP_IF_FALSE);
2281 PREDICT(POP_JUMP_IF_TRUE);
2282 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 TARGET(IMPORT_NAME)
2285 w = GETITEM(names, oparg);
2286 x = PyDict_GetItemString(f->f_builtins, "__import__");
2287 if (x == NULL) {
2288 PyErr_SetString(PyExc_ImportError,
2289 "__import__ not found");
2290 break;
2291 }
2292 Py_INCREF(x);
2293 v = POP();
2294 u = TOP();
2295 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2296 w = PyTuple_Pack(5,
2297 w,
2298 f->f_globals,
2299 f->f_locals == NULL ?
2300 Py_None : f->f_locals,
2301 v,
2302 u);
2303 else
2304 w = PyTuple_Pack(4,
2305 w,
2306 f->f_globals,
2307 f->f_locals == NULL ?
2308 Py_None : f->f_locals,
2309 v);
2310 Py_DECREF(v);
2311 Py_DECREF(u);
2312 if (w == NULL) {
2313 u = POP();
2314 Py_DECREF(x);
2315 x = NULL;
2316 break;
2317 }
2318 READ_TIMESTAMP(intr0);
2319 v = x;
2320 x = PyEval_CallObject(v, w);
2321 Py_DECREF(v);
2322 READ_TIMESTAMP(intr1);
2323 Py_DECREF(w);
2324 SET_TOP(x);
2325 if (x != NULL) DISPATCH();
2326 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002328 TARGET(IMPORT_STAR)
2329 v = POP();
2330 PyFrame_FastToLocals(f);
2331 if ((x = f->f_locals) == NULL) {
2332 PyErr_SetString(PyExc_SystemError,
2333 "no locals found during 'import *'");
2334 break;
2335 }
2336 READ_TIMESTAMP(intr0);
2337 err = import_all_from(x, v);
2338 READ_TIMESTAMP(intr1);
2339 PyFrame_LocalsToFast(f, 0);
2340 Py_DECREF(v);
2341 if (err == 0) DISPATCH();
2342 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002344 TARGET(IMPORT_FROM)
2345 w = GETITEM(names, oparg);
2346 v = TOP();
2347 READ_TIMESTAMP(intr0);
2348 x = import_from(v, w);
2349 READ_TIMESTAMP(intr1);
2350 PUSH(x);
2351 if (x != NULL) DISPATCH();
2352 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002354 TARGET(JUMP_FORWARD)
2355 JUMPBY(oparg);
2356 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2359 TARGET(POP_JUMP_IF_FALSE)
2360 w = POP();
2361 if (w == Py_True) {
2362 Py_DECREF(w);
2363 FAST_DISPATCH();
2364 }
2365 if (w == Py_False) {
2366 Py_DECREF(w);
2367 JUMPTO(oparg);
2368 FAST_DISPATCH();
2369 }
2370 err = PyObject_IsTrue(w);
2371 Py_DECREF(w);
2372 if (err > 0)
2373 err = 0;
2374 else if (err == 0)
2375 JUMPTO(oparg);
2376 else
2377 break;
2378 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002380 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2381 TARGET(POP_JUMP_IF_TRUE)
2382 w = POP();
2383 if (w == Py_False) {
2384 Py_DECREF(w);
2385 FAST_DISPATCH();
2386 }
2387 if (w == Py_True) {
2388 Py_DECREF(w);
2389 JUMPTO(oparg);
2390 FAST_DISPATCH();
2391 }
2392 err = PyObject_IsTrue(w);
2393 Py_DECREF(w);
2394 if (err > 0) {
2395 err = 0;
2396 JUMPTO(oparg);
2397 }
2398 else if (err == 0)
2399 ;
2400 else
2401 break;
2402 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002404 TARGET(JUMP_IF_FALSE_OR_POP)
2405 w = TOP();
2406 if (w == Py_True) {
2407 STACKADJ(-1);
2408 Py_DECREF(w);
2409 FAST_DISPATCH();
2410 }
2411 if (w == Py_False) {
2412 JUMPTO(oparg);
2413 FAST_DISPATCH();
2414 }
2415 err = PyObject_IsTrue(w);
2416 if (err > 0) {
2417 STACKADJ(-1);
2418 Py_DECREF(w);
2419 err = 0;
2420 }
2421 else if (err == 0)
2422 JUMPTO(oparg);
2423 else
2424 break;
2425 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002427 TARGET(JUMP_IF_TRUE_OR_POP)
2428 w = TOP();
2429 if (w == Py_False) {
2430 STACKADJ(-1);
2431 Py_DECREF(w);
2432 FAST_DISPATCH();
2433 }
2434 if (w == Py_True) {
2435 JUMPTO(oparg);
2436 FAST_DISPATCH();
2437 }
2438 err = PyObject_IsTrue(w);
2439 if (err > 0) {
2440 err = 0;
2441 JUMPTO(oparg);
2442 }
2443 else if (err == 0) {
2444 STACKADJ(-1);
2445 Py_DECREF(w);
2446 }
2447 else
2448 break;
2449 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002450
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2452 TARGET(JUMP_ABSOLUTE)
2453 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002454#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002455 /* Enabling this path speeds-up all while and for-loops by bypassing
2456 the per-loop checks for signals. By default, this should be turned-off
2457 because it prevents detection of a control-break in tight loops like
2458 "while 1: pass". Compile with this option turned-on when you need
2459 the speed-up and do not need break checking inside tight loops (ones
2460 that contain only instructions ending with FAST_DISPATCH).
2461 */
2462 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002463#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002464 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002465#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002467 TARGET(GET_ITER)
2468 /* before: [obj]; after [getiter(obj)] */
2469 v = TOP();
2470 x = PyObject_GetIter(v);
2471 Py_DECREF(v);
2472 if (x != NULL) {
2473 SET_TOP(x);
2474 PREDICT(FOR_ITER);
2475 DISPATCH();
2476 }
2477 STACKADJ(-1);
2478 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002480 PREDICTED_WITH_ARG(FOR_ITER);
2481 TARGET(FOR_ITER)
2482 /* before: [iter]; after: [iter, iter()] *or* [] */
2483 v = TOP();
2484 x = (*v->ob_type->tp_iternext)(v);
2485 if (x != NULL) {
2486 PUSH(x);
2487 PREDICT(STORE_FAST);
2488 PREDICT(UNPACK_SEQUENCE);
2489 DISPATCH();
2490 }
2491 if (PyErr_Occurred()) {
2492 if (!PyErr_ExceptionMatches(
2493 PyExc_StopIteration))
2494 break;
2495 PyErr_Clear();
2496 }
2497 /* iterator ended normally */
2498 x = v = POP();
2499 Py_DECREF(v);
2500 JUMPBY(oparg);
2501 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002503 TARGET(BREAK_LOOP)
2504 why = WHY_BREAK;
2505 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002506
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 TARGET(CONTINUE_LOOP)
2508 retval = PyLong_FromLong(oparg);
2509 if (!retval) {
2510 x = NULL;
2511 break;
2512 }
2513 why = WHY_CONTINUE;
2514 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2517 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2518 TARGET(SETUP_FINALLY)
2519 _setup_finally:
2520 /* NOTE: If you add any new block-setup opcodes that
2521 are not try/except/finally handlers, you may need
2522 to update the PyGen_NeedsFinalizing() function.
2523 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2526 STACK_LEVEL());
2527 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002529 TARGET(SETUP_WITH)
2530 {
Benjamin Petersonce798522012-01-22 11:24:29 -05002531 _Py_IDENTIFIER(__exit__);
2532 _Py_IDENTIFIER(__enter__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 w = TOP();
Benjamin Petersonce798522012-01-22 11:24:29 -05002534 x = special_lookup(w, &PyId___exit__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002535 if (!x)
2536 break;
2537 SET_TOP(x);
Benjamin Petersonce798522012-01-22 11:24:29 -05002538 u = special_lookup(w, &PyId___enter__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 Py_DECREF(w);
2540 if (!u) {
2541 x = NULL;
2542 break;
2543 }
2544 x = PyObject_CallFunctionObjArgs(u, NULL);
2545 Py_DECREF(u);
2546 if (!x)
2547 break;
2548 /* Setup the finally block before pushing the result
2549 of __enter__ on the stack. */
2550 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2551 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 PUSH(x);
2554 DISPATCH();
2555 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 TARGET(WITH_CLEANUP)
2558 {
2559 /* At the top of the stack are 1-3 values indicating
2560 how/why we entered the finally clause:
2561 - TOP = None
2562 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2563 - TOP = WHY_*; no retval below it
2564 - (TOP, SECOND, THIRD) = exc_info()
2565 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2566 Below them is EXIT, the context.__exit__ bound method.
2567 In the last case, we must call
2568 EXIT(TOP, SECOND, THIRD)
2569 otherwise we must call
2570 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 In the first two cases, we remove EXIT from the
2573 stack, leaving the rest in the same order. In the
2574 third case, we shift the bottom 3 values of the
2575 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 In addition, if the stack represents an exception,
2578 *and* the function call returns a 'true' value, we
2579 push WHY_SILENCED onto the stack. END_FINALLY will
2580 then not re-raise the exception. (But non-local
2581 gotos should still be resumed.)
2582 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002584 PyObject *exit_func;
2585 u = TOP();
2586 if (u == Py_None) {
2587 (void)POP();
2588 exit_func = TOP();
2589 SET_TOP(u);
2590 v = w = Py_None;
2591 }
2592 else if (PyLong_Check(u)) {
2593 (void)POP();
2594 switch(PyLong_AsLong(u)) {
2595 case WHY_RETURN:
2596 case WHY_CONTINUE:
2597 /* Retval in TOP. */
2598 exit_func = SECOND();
2599 SET_SECOND(TOP());
2600 SET_TOP(u);
2601 break;
2602 default:
2603 exit_func = TOP();
2604 SET_TOP(u);
2605 break;
2606 }
2607 u = v = w = Py_None;
2608 }
2609 else {
2610 PyObject *tp, *exc, *tb;
2611 PyTryBlock *block;
2612 v = SECOND();
2613 w = THIRD();
2614 tp = FOURTH();
2615 exc = PEEK(5);
2616 tb = PEEK(6);
2617 exit_func = PEEK(7);
2618 SET_VALUE(7, tb);
2619 SET_VALUE(6, exc);
2620 SET_VALUE(5, tp);
2621 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2622 SET_FOURTH(NULL);
2623 /* We just shifted the stack down, so we have
2624 to tell the except handler block that the
2625 values are lower than it expects. */
2626 block = &f->f_blockstack[f->f_iblock - 1];
2627 assert(block->b_type == EXCEPT_HANDLER);
2628 block->b_level--;
2629 }
2630 /* XXX Not the fastest way to call it... */
2631 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2632 NULL);
2633 Py_DECREF(exit_func);
2634 if (x == NULL)
2635 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002636
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002637 if (u != Py_None)
2638 err = PyObject_IsTrue(x);
2639 else
2640 err = 0;
2641 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 if (err < 0)
2644 break; /* Go to error exit */
2645 else if (err > 0) {
2646 err = 0;
2647 /* There was an exception and a True return */
2648 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2649 }
2650 PREDICT(END_FINALLY);
2651 break;
2652 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 TARGET(CALL_FUNCTION)
2655 {
2656 PyObject **sp;
2657 PCALL(PCALL_ALL);
2658 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002659#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002661#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002663#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 stack_pointer = sp;
2665 PUSH(x);
2666 if (x != NULL)
2667 DISPATCH();
2668 break;
2669 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2672 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2673 TARGET(CALL_FUNCTION_VAR_KW)
2674 _call_function_var_kw:
2675 {
2676 int na = oparg & 0xff;
2677 int nk = (oparg>>8) & 0xff;
2678 int flags = (opcode - CALL_FUNCTION) & 3;
2679 int n = na + 2 * nk;
2680 PyObject **pfunc, *func, **sp;
2681 PCALL(PCALL_ALL);
2682 if (flags & CALL_FLAG_VAR)
2683 n++;
2684 if (flags & CALL_FLAG_KW)
2685 n++;
2686 pfunc = stack_pointer - n - 1;
2687 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002690 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002691 PyObject *self = PyMethod_GET_SELF(func);
2692 Py_INCREF(self);
2693 func = PyMethod_GET_FUNCTION(func);
2694 Py_INCREF(func);
2695 Py_DECREF(*pfunc);
2696 *pfunc = self;
2697 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002698 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 } else
2700 Py_INCREF(func);
2701 sp = stack_pointer;
2702 READ_TIMESTAMP(intr0);
2703 x = ext_do_call(func, &sp, flags, na, nk);
2704 READ_TIMESTAMP(intr1);
2705 stack_pointer = sp;
2706 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 while (stack_pointer > pfunc) {
2709 w = POP();
2710 Py_DECREF(w);
2711 }
2712 PUSH(x);
2713 if (x != NULL)
2714 DISPATCH();
2715 break;
2716 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2719 TARGET(MAKE_FUNCTION)
2720 _make_function:
2721 {
2722 int posdefaults = oparg & 0xff;
2723 int kwdefaults = (oparg>>8) & 0xff;
2724 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002725
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002726 w = POP(); /* qualname */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 v = POP(); /* code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002728 x = PyFunction_NewWithQualName(v, f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 Py_DECREF(v);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002730 Py_DECREF(w);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 if (x != NULL && opcode == MAKE_CLOSURE) {
2733 v = POP();
2734 if (PyFunction_SetClosure(x, v) != 0) {
2735 /* Can't happen unless bytecode is corrupt. */
2736 why = WHY_EXCEPTION;
2737 }
2738 Py_DECREF(v);
2739 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002741 if (x != NULL && num_annotations > 0) {
2742 Py_ssize_t name_ix;
2743 u = POP(); /* names of args with annotations */
2744 v = PyDict_New();
2745 if (v == NULL) {
2746 Py_DECREF(x);
2747 x = NULL;
2748 break;
2749 }
2750 name_ix = PyTuple_Size(u);
2751 assert(num_annotations == name_ix+1);
2752 while (name_ix > 0) {
2753 --name_ix;
2754 t = PyTuple_GET_ITEM(u, name_ix);
2755 w = POP();
2756 /* XXX(nnorwitz): check for errors */
2757 PyDict_SetItem(v, t, w);
2758 Py_DECREF(w);
2759 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 if (PyFunction_SetAnnotations(x, v) != 0) {
2762 /* Can't happen unless
2763 PyFunction_SetAnnotations changes. */
2764 why = WHY_EXCEPTION;
2765 }
2766 Py_DECREF(v);
2767 Py_DECREF(u);
2768 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 /* XXX Maybe this should be a separate opcode? */
2771 if (x != NULL && posdefaults > 0) {
2772 v = PyTuple_New(posdefaults);
2773 if (v == NULL) {
2774 Py_DECREF(x);
2775 x = NULL;
2776 break;
2777 }
2778 while (--posdefaults >= 0) {
2779 w = POP();
2780 PyTuple_SET_ITEM(v, posdefaults, w);
2781 }
2782 if (PyFunction_SetDefaults(x, v) != 0) {
2783 /* Can't happen unless
2784 PyFunction_SetDefaults changes. */
2785 why = WHY_EXCEPTION;
2786 }
2787 Py_DECREF(v);
2788 }
2789 if (x != NULL && kwdefaults > 0) {
2790 v = PyDict_New();
2791 if (v == NULL) {
2792 Py_DECREF(x);
2793 x = NULL;
2794 break;
2795 }
2796 while (--kwdefaults >= 0) {
2797 w = POP(); /* default value */
2798 u = POP(); /* kw only arg name */
2799 /* XXX(nnorwitz): check for errors */
2800 PyDict_SetItem(v, u, w);
2801 Py_DECREF(w);
2802 Py_DECREF(u);
2803 }
2804 if (PyFunction_SetKwDefaults(x, v) != 0) {
2805 /* Can't happen unless
2806 PyFunction_SetKwDefaults changes. */
2807 why = WHY_EXCEPTION;
2808 }
2809 Py_DECREF(v);
2810 }
2811 PUSH(x);
2812 break;
2813 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002814
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 TARGET(BUILD_SLICE)
2816 if (oparg == 3)
2817 w = POP();
2818 else
2819 w = NULL;
2820 v = POP();
2821 u = TOP();
2822 x = PySlice_New(u, v, w);
2823 Py_DECREF(u);
2824 Py_DECREF(v);
2825 Py_XDECREF(w);
2826 SET_TOP(x);
2827 if (x != NULL) DISPATCH();
2828 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 TARGET(EXTENDED_ARG)
2831 opcode = NEXTOP();
2832 oparg = oparg<<16 | NEXTARG();
2833 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002834
Antoine Pitrou042b1282010-08-13 21:15:58 +00002835#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002837#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 default:
2839 fprintf(stderr,
2840 "XXX lineno: %d, opcode: %d\n",
2841 PyFrame_GetLineNumber(f),
2842 opcode);
2843 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2844 why = WHY_EXCEPTION;
2845 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002846
2847#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002849#endif
2850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002855 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002856
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 if (why == WHY_NOT) {
2860 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002861#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 /* This check is expensive! */
2863 if (PyErr_Occurred())
2864 fprintf(stderr,
2865 "XXX undetected error\n");
2866 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002867#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 READ_TIMESTAMP(loop1);
2869 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002870#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002872#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 }
2874 why = WHY_EXCEPTION;
2875 x = Py_None;
2876 err = 0;
2877 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002880
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2882 if (!PyErr_Occurred()) {
2883 PyErr_SetString(PyExc_SystemError,
2884 "error return without exception set");
2885 why = WHY_EXCEPTION;
2886 }
2887 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002888#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 else {
2890 /* This check is expensive! */
2891 if (PyErr_Occurred()) {
2892 char buf[128];
2893 sprintf(buf, "Stack unwind with exception "
2894 "set and why=%d", why);
2895 Py_FatalError(buf);
2896 }
2897 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002898#endif
2899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002902 if (why == WHY_EXCEPTION) {
2903 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002904
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 if (tstate->c_tracefunc != NULL)
2906 call_exc_trace(tstate->c_tracefunc,
2907 tstate->c_traceobj, f);
2908 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002911
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 if (why == WHY_RERAISE)
2913 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002916
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002917fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 while (why != WHY_NOT && f->f_iblock > 0) {
2919 /* Peek at the current block. */
2920 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 assert(why != WHY_YIELD);
2923 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2924 why = WHY_NOT;
2925 JUMPTO(PyLong_AS_LONG(retval));
2926 Py_DECREF(retval);
2927 break;
2928 }
2929 /* Now we have to pop the block. */
2930 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002932 if (b->b_type == EXCEPT_HANDLER) {
2933 UNWIND_EXCEPT_HANDLER(b);
2934 continue;
2935 }
2936 UNWIND_BLOCK(b);
2937 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2938 why = WHY_NOT;
2939 JUMPTO(b->b_handler);
2940 break;
2941 }
2942 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2943 || b->b_type == SETUP_FINALLY)) {
2944 PyObject *exc, *val, *tb;
2945 int handler = b->b_handler;
2946 /* Beware, this invalidates all b->b_* fields */
2947 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2948 PUSH(tstate->exc_traceback);
2949 PUSH(tstate->exc_value);
2950 if (tstate->exc_type != NULL) {
2951 PUSH(tstate->exc_type);
2952 }
2953 else {
2954 Py_INCREF(Py_None);
2955 PUSH(Py_None);
2956 }
2957 PyErr_Fetch(&exc, &val, &tb);
2958 /* Make the raw exception data
2959 available to the handler,
2960 so a program can emulate the
2961 Python main loop. */
2962 PyErr_NormalizeException(
2963 &exc, &val, &tb);
2964 PyException_SetTraceback(val, tb);
2965 Py_INCREF(exc);
2966 tstate->exc_type = exc;
2967 Py_INCREF(val);
2968 tstate->exc_value = val;
2969 tstate->exc_traceback = tb;
2970 if (tb == NULL)
2971 tb = Py_None;
2972 Py_INCREF(tb);
2973 PUSH(tb);
2974 PUSH(val);
2975 PUSH(exc);
2976 why = WHY_NOT;
2977 JUMPTO(handler);
2978 break;
2979 }
2980 if (b->b_type == SETUP_FINALLY) {
2981 if (why & (WHY_RETURN | WHY_CONTINUE))
2982 PUSH(retval);
2983 PUSH(PyLong_FromLong((long)why));
2984 why = WHY_NOT;
2985 JUMPTO(b->b_handler);
2986 break;
2987 }
2988 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002991
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 if (why != WHY_NOT)
2993 break;
2994 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 assert(why != WHY_YIELD);
2999 /* Pop remaining stack entries. */
3000 while (!EMPTY()) {
3001 v = POP();
3002 Py_XDECREF(v);
3003 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 if (why != WHY_RETURN)
3006 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003007
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003008fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003009 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3010 /* The purpose of this block is to put aside the generator's exception
3011 state and restore that of the calling frame. If the current
3012 exception state is from the caller, we clear the exception values
3013 on the generator frame, so they are not swapped back in latter. The
3014 origin of the current exception state is determined by checking for
3015 except handler blocks, which we must be in iff a new exception
3016 state came into existence in this frame. (An uncaught exception
3017 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3018 int i;
3019 for (i = 0; i < f->f_iblock; i++)
3020 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3021 break;
3022 if (i == f->f_iblock)
3023 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003024 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003025 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003026 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003027 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 if (tstate->use_tracing) {
3030 if (tstate->c_tracefunc) {
3031 if (why == WHY_RETURN || why == WHY_YIELD) {
3032 if (call_trace(tstate->c_tracefunc,
3033 tstate->c_traceobj, f,
3034 PyTrace_RETURN, retval)) {
3035 Py_XDECREF(retval);
3036 retval = NULL;
3037 why = WHY_EXCEPTION;
3038 }
3039 }
3040 else if (why == WHY_EXCEPTION) {
3041 call_trace_protected(tstate->c_tracefunc,
3042 tstate->c_traceobj, f,
3043 PyTrace_RETURN, NULL);
3044 }
3045 }
3046 if (tstate->c_profilefunc) {
3047 if (why == WHY_EXCEPTION)
3048 call_trace_protected(tstate->c_profilefunc,
3049 tstate->c_profileobj, f,
3050 PyTrace_RETURN, NULL);
3051 else if (call_trace(tstate->c_profilefunc,
3052 tstate->c_profileobj, f,
3053 PyTrace_RETURN, retval)) {
3054 Py_XDECREF(retval);
3055 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003056 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 }
3058 }
3059 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003062exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 Py_LeaveRecursiveCall();
3064 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003067}
3068
Benjamin Petersonb204a422011-06-05 22:04:07 -05003069static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003070format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3071{
3072 int err;
3073 Py_ssize_t len = PyList_GET_SIZE(names);
3074 PyObject *name_str, *comma, *tail, *tmp;
3075
3076 assert(PyList_CheckExact(names));
3077 assert(len >= 1);
3078 /* Deal with the joys of natural language. */
3079 switch (len) {
3080 case 1:
3081 name_str = PyList_GET_ITEM(names, 0);
3082 Py_INCREF(name_str);
3083 break;
3084 case 2:
3085 name_str = PyUnicode_FromFormat("%U and %U",
3086 PyList_GET_ITEM(names, len - 2),
3087 PyList_GET_ITEM(names, len - 1));
3088 break;
3089 default:
3090 tail = PyUnicode_FromFormat(", %U, and %U",
3091 PyList_GET_ITEM(names, len - 2),
3092 PyList_GET_ITEM(names, len - 1));
3093 /* Chop off the last two objects in the list. This shouldn't actually
3094 fail, but we can't be too careful. */
3095 err = PyList_SetSlice(names, len - 2, len, NULL);
3096 if (err == -1) {
3097 Py_DECREF(tail);
3098 return;
3099 }
3100 /* Stitch everything up into a nice comma-separated list. */
3101 comma = PyUnicode_FromString(", ");
3102 if (comma == NULL) {
3103 Py_DECREF(tail);
3104 return;
3105 }
3106 tmp = PyUnicode_Join(comma, names);
3107 Py_DECREF(comma);
3108 if (tmp == NULL) {
3109 Py_DECREF(tail);
3110 return;
3111 }
3112 name_str = PyUnicode_Concat(tmp, tail);
3113 Py_DECREF(tmp);
3114 Py_DECREF(tail);
3115 break;
3116 }
3117 if (name_str == NULL)
3118 return;
3119 PyErr_Format(PyExc_TypeError,
3120 "%U() missing %i required %s argument%s: %U",
3121 co->co_name,
3122 len,
3123 kind,
3124 len == 1 ? "" : "s",
3125 name_str);
3126 Py_DECREF(name_str);
3127}
3128
3129static void
3130missing_arguments(PyCodeObject *co, int missing, int defcount,
3131 PyObject **fastlocals)
3132{
3133 int i, j = 0;
3134 int start, end;
3135 int positional = defcount != -1;
3136 const char *kind = positional ? "positional" : "keyword-only";
3137 PyObject *missing_names;
3138
3139 /* Compute the names of the arguments that are missing. */
3140 missing_names = PyList_New(missing);
3141 if (missing_names == NULL)
3142 return;
3143 if (positional) {
3144 start = 0;
3145 end = co->co_argcount - defcount;
3146 }
3147 else {
3148 start = co->co_argcount;
3149 end = start + co->co_kwonlyargcount;
3150 }
3151 for (i = start; i < end; i++) {
3152 if (GETLOCAL(i) == NULL) {
3153 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3154 PyObject *name = PyObject_Repr(raw);
3155 if (name == NULL) {
3156 Py_DECREF(missing_names);
3157 return;
3158 }
3159 PyList_SET_ITEM(missing_names, j++, name);
3160 }
3161 }
3162 assert(j == missing);
3163 format_missing(kind, co, missing_names);
3164 Py_DECREF(missing_names);
3165}
3166
3167static void
3168too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003169{
3170 int plural;
3171 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003172 int i;
3173 PyObject *sig, *kwonly_sig;
3174
Benjamin Petersone109c702011-06-24 09:37:26 -05003175 assert((co->co_flags & CO_VARARGS) == 0);
3176 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003177 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003178 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003179 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003180 if (defcount) {
3181 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003182 plural = 1;
3183 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3184 }
3185 else {
3186 plural = co->co_argcount != 1;
3187 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3188 }
3189 if (sig == NULL)
3190 return;
3191 if (kwonly_given) {
3192 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3193 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3194 kwonly_given != 1 ? "s" : "");
3195 if (kwonly_sig == NULL) {
3196 Py_DECREF(sig);
3197 return;
3198 }
3199 }
3200 else {
3201 /* This will not fail. */
3202 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003203 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003204 }
3205 PyErr_Format(PyExc_TypeError,
3206 "%U() takes %U positional argument%s but %d%U %s given",
3207 co->co_name,
3208 sig,
3209 plural ? "s" : "",
3210 given,
3211 kwonly_sig,
3212 given == 1 && !kwonly_given ? "was" : "were");
3213 Py_DECREF(sig);
3214 Py_DECREF(kwonly_sig);
3215}
3216
Guido van Rossumc2e20742006-02-27 22:32:47 +00003217/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003218 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003219 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003220
Tim Peters6d6c1a32001-08-02 04:15:00 +00003221PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003222PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 PyObject **args, int argcount, PyObject **kws, int kwcount,
3224 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003225{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003226 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003227 register PyFrameObject *f;
3228 register PyObject *retval = NULL;
3229 register PyObject **fastlocals, **freevars;
3230 PyThreadState *tstate = PyThreadState_GET();
3231 PyObject *x, *u;
3232 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003233 int i;
3234 int n = argcount;
3235 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003237 if (globals == NULL) {
3238 PyErr_SetString(PyExc_SystemError,
3239 "PyEval_EvalCodeEx: NULL globals");
3240 return NULL;
3241 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003243 assert(tstate != NULL);
3244 assert(globals != NULL);
3245 f = PyFrame_New(tstate, co, globals, locals);
3246 if (f == NULL)
3247 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003249 fastlocals = f->f_localsplus;
3250 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003251
Benjamin Petersonb204a422011-06-05 22:04:07 -05003252 /* Parse arguments. */
3253 if (co->co_flags & CO_VARKEYWORDS) {
3254 kwdict = PyDict_New();
3255 if (kwdict == NULL)
3256 goto fail;
3257 i = total_args;
3258 if (co->co_flags & CO_VARARGS)
3259 i++;
3260 SETLOCAL(i, kwdict);
3261 }
3262 if (argcount > co->co_argcount)
3263 n = co->co_argcount;
3264 for (i = 0; i < n; i++) {
3265 x = args[i];
3266 Py_INCREF(x);
3267 SETLOCAL(i, x);
3268 }
3269 if (co->co_flags & CO_VARARGS) {
3270 u = PyTuple_New(argcount - n);
3271 if (u == NULL)
3272 goto fail;
3273 SETLOCAL(total_args, u);
3274 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 x = args[i];
3276 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003277 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003278 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003279 }
3280 for (i = 0; i < kwcount; i++) {
3281 PyObject **co_varnames;
3282 PyObject *keyword = kws[2*i];
3283 PyObject *value = kws[2*i + 1];
3284 int j;
3285 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3286 PyErr_Format(PyExc_TypeError,
3287 "%U() keywords must be strings",
3288 co->co_name);
3289 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003291 /* Speed hack: do raw pointer compares. As names are
3292 normally interned this should almost always hit. */
3293 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3294 for (j = 0; j < total_args; j++) {
3295 PyObject *nm = co_varnames[j];
3296 if (nm == keyword)
3297 goto kw_found;
3298 }
3299 /* Slow fallback, just in case */
3300 for (j = 0; j < total_args; j++) {
3301 PyObject *nm = co_varnames[j];
3302 int cmp = PyObject_RichCompareBool(
3303 keyword, nm, Py_EQ);
3304 if (cmp > 0)
3305 goto kw_found;
3306 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003308 }
3309 if (j >= total_args && kwdict == NULL) {
3310 PyErr_Format(PyExc_TypeError,
3311 "%U() got an unexpected "
3312 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 co->co_name,
3314 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003315 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003316 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003317 PyDict_SetItem(kwdict, keyword, value);
3318 continue;
3319 kw_found:
3320 if (GETLOCAL(j) != NULL) {
3321 PyErr_Format(PyExc_TypeError,
3322 "%U() got multiple "
3323 "values for argument '%S'",
3324 co->co_name,
3325 keyword);
3326 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003328 Py_INCREF(value);
3329 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003331 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003332 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 goto fail;
3334 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003335 if (argcount < co->co_argcount) {
3336 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003337 int missing = 0;
3338 for (i = argcount; i < m; i++)
3339 if (GETLOCAL(i) == NULL)
3340 missing++;
3341 if (missing) {
3342 missing_arguments(co, missing, defcount, fastlocals);
3343 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003344 }
3345 if (n > m)
3346 i = n - m;
3347 else
3348 i = 0;
3349 for (; i < defcount; i++) {
3350 if (GETLOCAL(m+i) == NULL) {
3351 PyObject *def = defs[i];
3352 Py_INCREF(def);
3353 SETLOCAL(m+i, def);
3354 }
3355 }
3356 }
3357 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003358 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003359 for (i = co->co_argcount; i < total_args; i++) {
3360 PyObject *name;
3361 if (GETLOCAL(i) != NULL)
3362 continue;
3363 name = PyTuple_GET_ITEM(co->co_varnames, i);
3364 if (kwdefs != NULL) {
3365 PyObject *def = PyDict_GetItem(kwdefs, name);
3366 if (def) {
3367 Py_INCREF(def);
3368 SETLOCAL(i, def);
3369 continue;
3370 }
3371 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003372 missing++;
3373 }
3374 if (missing) {
3375 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003376 goto fail;
3377 }
3378 }
3379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003381 vars into frame. */
3382 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003383 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003384 int arg;
3385 /* Possibly account for the cell variable being an argument. */
3386 if (co->co_cell2arg != NULL &&
3387 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3388 c = PyCell_New(GETLOCAL(arg));
3389 else
3390 c = PyCell_New(NULL);
3391 if (c == NULL)
3392 goto fail;
3393 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003395 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3396 PyObject *o = PyTuple_GET_ITEM(closure, i);
3397 Py_INCREF(o);
3398 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 if (co->co_flags & CO_GENERATOR) {
3402 /* Don't need to keep the reference to f_back, it will be set
3403 * when the generator is resumed. */
3404 Py_XDECREF(f->f_back);
3405 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003407 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 /* Create a new generator that owns the ready to run frame
3410 * and return that as the value. */
3411 return PyGen_New(f);
3412 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003414 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003415
Thomas Woutersce272b62007-09-19 21:19:28 +00003416fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 /* decref'ing the frame can cause __del__ methods to get invoked,
3419 which can call back into Python. While we're done with the
3420 current Python frame (f), the associated C stack is still in use,
3421 so recursion_depth must be boosted for the duration.
3422 */
3423 assert(tstate != NULL);
3424 ++tstate->recursion_depth;
3425 Py_DECREF(f);
3426 --tstate->recursion_depth;
3427 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003428}
3429
3430
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003431static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003432special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003435 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003437 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003438 return NULL;
3439 }
3440 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003441}
3442
3443
Benjamin Peterson87880242011-07-03 16:48:31 -05003444/* These 3 functions deal with the exception state of generators. */
3445
3446static void
3447save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3448{
3449 PyObject *type, *value, *traceback;
3450 Py_XINCREF(tstate->exc_type);
3451 Py_XINCREF(tstate->exc_value);
3452 Py_XINCREF(tstate->exc_traceback);
3453 type = f->f_exc_type;
3454 value = f->f_exc_value;
3455 traceback = f->f_exc_traceback;
3456 f->f_exc_type = tstate->exc_type;
3457 f->f_exc_value = tstate->exc_value;
3458 f->f_exc_traceback = tstate->exc_traceback;
3459 Py_XDECREF(type);
3460 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003461 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003462}
3463
3464static void
3465swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3466{
3467 PyObject *tmp;
3468 tmp = tstate->exc_type;
3469 tstate->exc_type = f->f_exc_type;
3470 f->f_exc_type = tmp;
3471 tmp = tstate->exc_value;
3472 tstate->exc_value = f->f_exc_value;
3473 f->f_exc_value = tmp;
3474 tmp = tstate->exc_traceback;
3475 tstate->exc_traceback = f->f_exc_traceback;
3476 f->f_exc_traceback = tmp;
3477}
3478
3479static void
3480restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3481{
3482 PyObject *type, *value, *tb;
3483 type = tstate->exc_type;
3484 value = tstate->exc_value;
3485 tb = tstate->exc_traceback;
3486 tstate->exc_type = f->f_exc_type;
3487 tstate->exc_value = f->f_exc_value;
3488 tstate->exc_traceback = f->f_exc_traceback;
3489 f->f_exc_type = NULL;
3490 f->f_exc_value = NULL;
3491 f->f_exc_traceback = NULL;
3492 Py_XDECREF(type);
3493 Py_XDECREF(value);
3494 Py_XDECREF(tb);
3495}
3496
3497
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003498/* Logic for the raise statement (too complicated for inlining).
3499 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003500static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003501do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 if (exc == NULL) {
3506 /* Reraise */
3507 PyThreadState *tstate = PyThreadState_GET();
3508 PyObject *tb;
3509 type = tstate->exc_type;
3510 value = tstate->exc_value;
3511 tb = tstate->exc_traceback;
3512 if (type == Py_None) {
3513 PyErr_SetString(PyExc_RuntimeError,
3514 "No active exception to reraise");
3515 return WHY_EXCEPTION;
3516 }
3517 Py_XINCREF(type);
3518 Py_XINCREF(value);
3519 Py_XINCREF(tb);
3520 PyErr_Restore(type, value, tb);
3521 return WHY_RERAISE;
3522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003524 /* We support the following forms of raise:
3525 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003526 raise <instance>
3527 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 if (PyExceptionClass_Check(exc)) {
3530 type = exc;
3531 value = PyObject_CallObject(exc, NULL);
3532 if (value == NULL)
3533 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003534 if (!PyExceptionInstance_Check(value)) {
3535 PyErr_Format(PyExc_TypeError,
3536 "calling %R should have returned an instance of "
3537 "BaseException, not %R",
3538 type, Py_TYPE(value));
3539 goto raise_error;
3540 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003541 }
3542 else if (PyExceptionInstance_Check(exc)) {
3543 value = exc;
3544 type = PyExceptionInstance_Class(exc);
3545 Py_INCREF(type);
3546 }
3547 else {
3548 /* Not something you can raise. You get an exception
3549 anyway, just not what you specified :-) */
3550 Py_DECREF(exc);
3551 PyErr_SetString(PyExc_TypeError,
3552 "exceptions must derive from BaseException");
3553 goto raise_error;
3554 }
Collin Winter828f04a2007-08-31 00:04:24 +00003555
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003556 if (cause) {
3557 PyObject *fixed_cause;
Nick Coghlanab7bf212012-02-26 17:49:52 +10003558 int result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 if (PyExceptionClass_Check(cause)) {
3560 fixed_cause = PyObject_CallObject(cause, NULL);
3561 if (fixed_cause == NULL)
3562 goto raise_error;
Nick Coghlanab7bf212012-02-26 17:49:52 +10003563 Py_CLEAR(cause);
3564 } else {
3565 /* Let "exc.__cause__ = cause" handle all further checks */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 fixed_cause = cause;
Nick Coghlanab7bf212012-02-26 17:49:52 +10003567 cause = NULL; /* Steal the reference */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 }
Nick Coghlanab7bf212012-02-26 17:49:52 +10003569 /* We retain ownership of the reference to fixed_cause */
3570 result = _PyException_SetCauseChecked(value, fixed_cause);
3571 Py_DECREF(fixed_cause);
3572 if (result < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 goto raise_error;
3574 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 }
Collin Winter828f04a2007-08-31 00:04:24 +00003576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 PyErr_SetObject(type, value);
3578 /* PyErr_SetObject incref's its arguments */
3579 Py_XDECREF(value);
3580 Py_XDECREF(type);
3581 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003582
3583raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 Py_XDECREF(value);
3585 Py_XDECREF(type);
3586 Py_XDECREF(cause);
3587 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003588}
3589
Tim Petersd6d010b2001-06-21 02:49:55 +00003590/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003591 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003592
Guido van Rossum0368b722007-05-11 16:50:42 +00003593 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3594 with a variable target.
3595*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003596
Barry Warsawe42b18f1997-08-25 22:13:04 +00003597static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003598unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003600 int i = 0, j = 0;
3601 Py_ssize_t ll = 0;
3602 PyObject *it; /* iter(v) */
3603 PyObject *w;
3604 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 it = PyObject_GetIter(v);
3609 if (it == NULL)
3610 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 for (; i < argcnt; i++) {
3613 w = PyIter_Next(it);
3614 if (w == NULL) {
3615 /* Iterator done, via error or exhaustion. */
3616 if (!PyErr_Occurred()) {
3617 PyErr_Format(PyExc_ValueError,
3618 "need more than %d value%s to unpack",
3619 i, i == 1 ? "" : "s");
3620 }
3621 goto Error;
3622 }
3623 *--sp = w;
3624 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 if (argcntafter == -1) {
3627 /* We better have exhausted the iterator now. */
3628 w = PyIter_Next(it);
3629 if (w == NULL) {
3630 if (PyErr_Occurred())
3631 goto Error;
3632 Py_DECREF(it);
3633 return 1;
3634 }
3635 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003636 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3637 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 goto Error;
3639 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003641 l = PySequence_List(it);
3642 if (l == NULL)
3643 goto Error;
3644 *--sp = l;
3645 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 ll = PyList_GET_SIZE(l);
3648 if (ll < argcntafter) {
3649 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3650 argcnt + ll);
3651 goto Error;
3652 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 /* Pop the "after-variable" args off the list. */
3655 for (j = argcntafter; j > 0; j--, i++) {
3656 *--sp = PyList_GET_ITEM(l, ll - j);
3657 }
3658 /* Resize the list. */
3659 Py_SIZE(l) = ll - argcntafter;
3660 Py_DECREF(it);
3661 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003662
Tim Petersd6d010b2001-06-21 02:49:55 +00003663Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003664 for (; i > 0; i--, sp++)
3665 Py_DECREF(*sp);
3666 Py_XDECREF(it);
3667 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003668}
3669
3670
Guido van Rossum96a42c81992-01-12 02:29:51 +00003671#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003672static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003673prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 printf("%s ", str);
3676 if (PyObject_Print(v, stdout, 0) != 0)
3677 PyErr_Clear(); /* Don't know what else to do */
3678 printf("\n");
3679 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003680}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003681#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003682
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003683static void
Fred Drake5755ce62001-06-27 19:19:46 +00003684call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 PyObject *type, *value, *traceback, *arg;
3687 int err;
3688 PyErr_Fetch(&type, &value, &traceback);
3689 if (value == NULL) {
3690 value = Py_None;
3691 Py_INCREF(value);
3692 }
3693 arg = PyTuple_Pack(3, type, value, traceback);
3694 if (arg == NULL) {
3695 PyErr_Restore(type, value, traceback);
3696 return;
3697 }
3698 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3699 Py_DECREF(arg);
3700 if (err == 0)
3701 PyErr_Restore(type, value, traceback);
3702 else {
3703 Py_XDECREF(type);
3704 Py_XDECREF(value);
3705 Py_XDECREF(traceback);
3706 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003707}
3708
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003709static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003710call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 PyObject *type, *value, *traceback;
3714 int err;
3715 PyErr_Fetch(&type, &value, &traceback);
3716 err = call_trace(func, obj, frame, what, arg);
3717 if (err == 0)
3718 {
3719 PyErr_Restore(type, value, traceback);
3720 return 0;
3721 }
3722 else {
3723 Py_XDECREF(type);
3724 Py_XDECREF(value);
3725 Py_XDECREF(traceback);
3726 return -1;
3727 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003728}
3729
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003730static int
Fred Drake5755ce62001-06-27 19:19:46 +00003731call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 register PyThreadState *tstate = frame->f_tstate;
3735 int result;
3736 if (tstate->tracing)
3737 return 0;
3738 tstate->tracing++;
3739 tstate->use_tracing = 0;
3740 result = func(obj, frame, what, arg);
3741 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3742 || (tstate->c_profilefunc != NULL));
3743 tstate->tracing--;
3744 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003745}
3746
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003747PyObject *
3748_PyEval_CallTracing(PyObject *func, PyObject *args)
3749{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003750 PyFrameObject *frame = PyEval_GetFrame();
3751 PyThreadState *tstate = frame->f_tstate;
3752 int save_tracing = tstate->tracing;
3753 int save_use_tracing = tstate->use_tracing;
3754 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003756 tstate->tracing = 0;
3757 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3758 || (tstate->c_profilefunc != NULL));
3759 result = PyObject_Call(func, args, NULL);
3760 tstate->tracing = save_tracing;
3761 tstate->use_tracing = save_use_tracing;
3762 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003763}
3764
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003765/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003766static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003767maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3769 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003770{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 int result = 0;
3772 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 /* If the last instruction executed isn't in the current
3775 instruction window, reset the window.
3776 */
3777 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3778 PyAddrPair bounds;
3779 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3780 &bounds);
3781 *instr_lb = bounds.ap_lower;
3782 *instr_ub = bounds.ap_upper;
3783 }
3784 /* If the last instruction falls at the start of a line or if
3785 it represents a jump backwards, update the frame's line
3786 number and call the trace function. */
3787 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3788 frame->f_lineno = line;
3789 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3790 }
3791 *instr_prev = frame->f_lasti;
3792 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003793}
3794
Fred Drake5755ce62001-06-27 19:19:46 +00003795void
3796PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003797{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003798 PyThreadState *tstate = PyThreadState_GET();
3799 PyObject *temp = tstate->c_profileobj;
3800 Py_XINCREF(arg);
3801 tstate->c_profilefunc = NULL;
3802 tstate->c_profileobj = NULL;
3803 /* Must make sure that tracing is not ignored if 'temp' is freed */
3804 tstate->use_tracing = tstate->c_tracefunc != NULL;
3805 Py_XDECREF(temp);
3806 tstate->c_profilefunc = func;
3807 tstate->c_profileobj = arg;
3808 /* Flag that tracing or profiling is turned on */
3809 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003810}
3811
3812void
3813PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 PyThreadState *tstate = PyThreadState_GET();
3816 PyObject *temp = tstate->c_traceobj;
3817 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3818 Py_XINCREF(arg);
3819 tstate->c_tracefunc = NULL;
3820 tstate->c_traceobj = NULL;
3821 /* Must make sure that profiling is not ignored if 'temp' is freed */
3822 tstate->use_tracing = tstate->c_profilefunc != NULL;
3823 Py_XDECREF(temp);
3824 tstate->c_tracefunc = func;
3825 tstate->c_traceobj = arg;
3826 /* Flag that tracing or profiling is turned on */
3827 tstate->use_tracing = ((func != NULL)
3828 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003829}
3830
Guido van Rossumb209a111997-04-29 18:18:01 +00003831PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003832PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 PyFrameObject *current_frame = PyEval_GetFrame();
3835 if (current_frame == NULL)
3836 return PyThreadState_GET()->interp->builtins;
3837 else
3838 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003839}
3840
Guido van Rossumb209a111997-04-29 18:18:01 +00003841PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003842PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003843{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 PyFrameObject *current_frame = PyEval_GetFrame();
3845 if (current_frame == NULL)
3846 return NULL;
3847 PyFrame_FastToLocals(current_frame);
3848 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003849}
3850
Guido van Rossumb209a111997-04-29 18:18:01 +00003851PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003852PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 PyFrameObject *current_frame = PyEval_GetFrame();
3855 if (current_frame == NULL)
3856 return NULL;
3857 else
3858 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003859}
3860
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003861PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003862PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003863{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 PyThreadState *tstate = PyThreadState_GET();
3865 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003866}
3867
Guido van Rossum6135a871995-01-09 17:53:26 +00003868int
Tim Peters5ba58662001-07-16 02:29:45 +00003869PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003871 PyFrameObject *current_frame = PyEval_GetFrame();
3872 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003874 if (current_frame != NULL) {
3875 const int codeflags = current_frame->f_code->co_flags;
3876 const int compilerflags = codeflags & PyCF_MASK;
3877 if (compilerflags) {
3878 result = 1;
3879 cf->cf_flags |= compilerflags;
3880 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003881#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 if (codeflags & CO_GENERATOR_ALLOWED) {
3883 result = 1;
3884 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3885 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003886#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003887 }
3888 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003889}
3890
Guido van Rossum3f5da241990-12-20 15:06:42 +00003891
Guido van Rossum681d79a1995-07-18 14:51:37 +00003892/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003893 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003894
Guido van Rossumb209a111997-04-29 18:18:01 +00003895PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003896PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003898 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 if (arg == NULL) {
3901 arg = PyTuple_New(0);
3902 if (arg == NULL)
3903 return NULL;
3904 }
3905 else if (!PyTuple_Check(arg)) {
3906 PyErr_SetString(PyExc_TypeError,
3907 "argument list must be a tuple");
3908 return NULL;
3909 }
3910 else
3911 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003913 if (kw != NULL && !PyDict_Check(kw)) {
3914 PyErr_SetString(PyExc_TypeError,
3915 "keyword list must be a dictionary");
3916 Py_DECREF(arg);
3917 return NULL;
3918 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 result = PyObject_Call(func, arg, kw);
3921 Py_DECREF(arg);
3922 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003923}
3924
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003925const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003926PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003927{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003928 if (PyMethod_Check(func))
3929 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3930 else if (PyFunction_Check(func))
3931 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3932 else if (PyCFunction_Check(func))
3933 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3934 else
3935 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003936}
3937
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003938const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003939PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 if (PyMethod_Check(func))
3942 return "()";
3943 else if (PyFunction_Check(func))
3944 return "()";
3945 else if (PyCFunction_Check(func))
3946 return "()";
3947 else
3948 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003949}
3950
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003951static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003952err_args(PyObject *func, int flags, int nargs)
3953{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 if (flags & METH_NOARGS)
3955 PyErr_Format(PyExc_TypeError,
3956 "%.200s() takes no arguments (%d given)",
3957 ((PyCFunctionObject *)func)->m_ml->ml_name,
3958 nargs);
3959 else
3960 PyErr_Format(PyExc_TypeError,
3961 "%.200s() takes exactly one argument (%d given)",
3962 ((PyCFunctionObject *)func)->m_ml->ml_name,
3963 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003964}
3965
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003966#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003967if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003968 if (call_trace(tstate->c_profilefunc, \
3969 tstate->c_profileobj, \
3970 tstate->frame, PyTrace_C_CALL, \
3971 func)) { \
3972 x = NULL; \
3973 } \
3974 else { \
3975 x = call; \
3976 if (tstate->c_profilefunc != NULL) { \
3977 if (x == NULL) { \
3978 call_trace_protected(tstate->c_profilefunc, \
3979 tstate->c_profileobj, \
3980 tstate->frame, PyTrace_C_EXCEPTION, \
3981 func); \
3982 /* XXX should pass (type, value, tb) */ \
3983 } else { \
3984 if (call_trace(tstate->c_profilefunc, \
3985 tstate->c_profileobj, \
3986 tstate->frame, PyTrace_C_RETURN, \
3987 func)) { \
3988 Py_DECREF(x); \
3989 x = NULL; \
3990 } \
3991 } \
3992 } \
3993 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003994} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003995 x = call; \
3996 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003997
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003998static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003999call_function(PyObject ***pp_stack, int oparg
4000#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004002#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 int na = oparg & 0xff;
4006 int nk = (oparg>>8) & 0xff;
4007 int n = na + 2 * nk;
4008 PyObject **pfunc = (*pp_stack) - n - 1;
4009 PyObject *func = *pfunc;
4010 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004012 /* Always dispatch PyCFunction first, because these are
4013 presumed to be the most frequent callable object.
4014 */
4015 if (PyCFunction_Check(func) && nk == 0) {
4016 int flags = PyCFunction_GET_FLAGS(func);
4017 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 PCALL(PCALL_CFUNCTION);
4020 if (flags & (METH_NOARGS | METH_O)) {
4021 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4022 PyObject *self = PyCFunction_GET_SELF(func);
4023 if (flags & METH_NOARGS && na == 0) {
4024 C_TRACE(x, (*meth)(self,NULL));
4025 }
4026 else if (flags & METH_O && na == 1) {
4027 PyObject *arg = EXT_POP(*pp_stack);
4028 C_TRACE(x, (*meth)(self,arg));
4029 Py_DECREF(arg);
4030 }
4031 else {
4032 err_args(func, flags, na);
4033 x = NULL;
4034 }
4035 }
4036 else {
4037 PyObject *callargs;
4038 callargs = load_args(pp_stack, na);
4039 READ_TIMESTAMP(*pintr0);
4040 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4041 READ_TIMESTAMP(*pintr1);
4042 Py_XDECREF(callargs);
4043 }
4044 } else {
4045 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4046 /* optimize access to bound methods */
4047 PyObject *self = PyMethod_GET_SELF(func);
4048 PCALL(PCALL_METHOD);
4049 PCALL(PCALL_BOUND_METHOD);
4050 Py_INCREF(self);
4051 func = PyMethod_GET_FUNCTION(func);
4052 Py_INCREF(func);
4053 Py_DECREF(*pfunc);
4054 *pfunc = self;
4055 na++;
4056 n++;
4057 } else
4058 Py_INCREF(func);
4059 READ_TIMESTAMP(*pintr0);
4060 if (PyFunction_Check(func))
4061 x = fast_function(func, pp_stack, n, na, nk);
4062 else
4063 x = do_call(func, pp_stack, na, nk);
4064 READ_TIMESTAMP(*pintr1);
4065 Py_DECREF(func);
4066 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 /* Clear the stack of the function object. Also removes
4069 the arguments in case they weren't consumed already
4070 (fast_function() and err_args() leave them on the stack).
4071 */
4072 while ((*pp_stack) > pfunc) {
4073 w = EXT_POP(*pp_stack);
4074 Py_DECREF(w);
4075 PCALL(PCALL_POP);
4076 }
4077 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004078}
4079
Jeremy Hylton192690e2002-08-16 18:36:11 +00004080/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004081 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004082 For the simplest case -- a function that takes only positional
4083 arguments and is called with only positional arguments -- it
4084 inlines the most primitive frame setup code from
4085 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4086 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004087*/
4088
4089static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004090fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004091{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4093 PyObject *globals = PyFunction_GET_GLOBALS(func);
4094 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4095 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4096 PyObject **d = NULL;
4097 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004099 PCALL(PCALL_FUNCTION);
4100 PCALL(PCALL_FAST_FUNCTION);
4101 if (argdefs == NULL && co->co_argcount == n &&
4102 co->co_kwonlyargcount == 0 && nk==0 &&
4103 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4104 PyFrameObject *f;
4105 PyObject *retval = NULL;
4106 PyThreadState *tstate = PyThreadState_GET();
4107 PyObject **fastlocals, **stack;
4108 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 PCALL(PCALL_FASTER_FUNCTION);
4111 assert(globals != NULL);
4112 /* XXX Perhaps we should create a specialized
4113 PyFrame_New() that doesn't take locals, but does
4114 take builtins without sanity checking them.
4115 */
4116 assert(tstate != NULL);
4117 f = PyFrame_New(tstate, co, globals, NULL);
4118 if (f == NULL)
4119 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 fastlocals = f->f_localsplus;
4122 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 for (i = 0; i < n; i++) {
4125 Py_INCREF(*stack);
4126 fastlocals[i] = *stack++;
4127 }
4128 retval = PyEval_EvalFrameEx(f,0);
4129 ++tstate->recursion_depth;
4130 Py_DECREF(f);
4131 --tstate->recursion_depth;
4132 return retval;
4133 }
4134 if (argdefs != NULL) {
4135 d = &PyTuple_GET_ITEM(argdefs, 0);
4136 nd = Py_SIZE(argdefs);
4137 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004138 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004139 (PyObject *)NULL, (*pp_stack)-n, na,
4140 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4141 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004142}
4143
4144static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004145update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4146 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004147{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 PyObject *kwdict = NULL;
4149 if (orig_kwdict == NULL)
4150 kwdict = PyDict_New();
4151 else {
4152 kwdict = PyDict_Copy(orig_kwdict);
4153 Py_DECREF(orig_kwdict);
4154 }
4155 if (kwdict == NULL)
4156 return NULL;
4157 while (--nk >= 0) {
4158 int err;
4159 PyObject *value = EXT_POP(*pp_stack);
4160 PyObject *key = EXT_POP(*pp_stack);
4161 if (PyDict_GetItem(kwdict, key) != NULL) {
4162 PyErr_Format(PyExc_TypeError,
4163 "%.200s%s got multiple values "
4164 "for keyword argument '%U'",
4165 PyEval_GetFuncName(func),
4166 PyEval_GetFuncDesc(func),
4167 key);
4168 Py_DECREF(key);
4169 Py_DECREF(value);
4170 Py_DECREF(kwdict);
4171 return NULL;
4172 }
4173 err = PyDict_SetItem(kwdict, key, value);
4174 Py_DECREF(key);
4175 Py_DECREF(value);
4176 if (err) {
4177 Py_DECREF(kwdict);
4178 return NULL;
4179 }
4180 }
4181 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004182}
4183
4184static PyObject *
4185update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004187{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004188 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004189
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 callargs = PyTuple_New(nstack + nstar);
4191 if (callargs == NULL) {
4192 return NULL;
4193 }
4194 if (nstar) {
4195 int i;
4196 for (i = 0; i < nstar; i++) {
4197 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4198 Py_INCREF(a);
4199 PyTuple_SET_ITEM(callargs, nstack + i, a);
4200 }
4201 }
4202 while (--nstack >= 0) {
4203 w = EXT_POP(*pp_stack);
4204 PyTuple_SET_ITEM(callargs, nstack, w);
4205 }
4206 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004207}
4208
4209static PyObject *
4210load_args(PyObject ***pp_stack, int na)
4211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004212 PyObject *args = PyTuple_New(na);
4213 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 if (args == NULL)
4216 return NULL;
4217 while (--na >= 0) {
4218 w = EXT_POP(*pp_stack);
4219 PyTuple_SET_ITEM(args, na, w);
4220 }
4221 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004222}
4223
4224static PyObject *
4225do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004227 PyObject *callargs = NULL;
4228 PyObject *kwdict = NULL;
4229 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 if (nk > 0) {
4232 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4233 if (kwdict == NULL)
4234 goto call_fail;
4235 }
4236 callargs = load_args(pp_stack, na);
4237 if (callargs == NULL)
4238 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004239#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004240 /* At this point, we have to look at the type of func to
4241 update the call stats properly. Do it here so as to avoid
4242 exposing the call stats machinery outside ceval.c
4243 */
4244 if (PyFunction_Check(func))
4245 PCALL(PCALL_FUNCTION);
4246 else if (PyMethod_Check(func))
4247 PCALL(PCALL_METHOD);
4248 else if (PyType_Check(func))
4249 PCALL(PCALL_TYPE);
4250 else if (PyCFunction_Check(func))
4251 PCALL(PCALL_CFUNCTION);
4252 else
4253 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004254#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004255 if (PyCFunction_Check(func)) {
4256 PyThreadState *tstate = PyThreadState_GET();
4257 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4258 }
4259 else
4260 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004261call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004262 Py_XDECREF(callargs);
4263 Py_XDECREF(kwdict);
4264 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004265}
4266
4267static PyObject *
4268ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 int nstar = 0;
4271 PyObject *callargs = NULL;
4272 PyObject *stararg = NULL;
4273 PyObject *kwdict = NULL;
4274 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004276 if (flags & CALL_FLAG_KW) {
4277 kwdict = EXT_POP(*pp_stack);
4278 if (!PyDict_Check(kwdict)) {
4279 PyObject *d;
4280 d = PyDict_New();
4281 if (d == NULL)
4282 goto ext_call_fail;
4283 if (PyDict_Update(d, kwdict) != 0) {
4284 Py_DECREF(d);
4285 /* PyDict_Update raises attribute
4286 * error (percolated from an attempt
4287 * to get 'keys' attribute) instead of
4288 * a type error if its second argument
4289 * is not a mapping.
4290 */
4291 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4292 PyErr_Format(PyExc_TypeError,
4293 "%.200s%.200s argument after ** "
4294 "must be a mapping, not %.200s",
4295 PyEval_GetFuncName(func),
4296 PyEval_GetFuncDesc(func),
4297 kwdict->ob_type->tp_name);
4298 }
4299 goto ext_call_fail;
4300 }
4301 Py_DECREF(kwdict);
4302 kwdict = d;
4303 }
4304 }
4305 if (flags & CALL_FLAG_VAR) {
4306 stararg = EXT_POP(*pp_stack);
4307 if (!PyTuple_Check(stararg)) {
4308 PyObject *t = NULL;
4309 t = PySequence_Tuple(stararg);
4310 if (t == NULL) {
4311 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4312 PyErr_Format(PyExc_TypeError,
4313 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004314 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004315 PyEval_GetFuncName(func),
4316 PyEval_GetFuncDesc(func),
4317 stararg->ob_type->tp_name);
4318 }
4319 goto ext_call_fail;
4320 }
4321 Py_DECREF(stararg);
4322 stararg = t;
4323 }
4324 nstar = PyTuple_GET_SIZE(stararg);
4325 }
4326 if (nk > 0) {
4327 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4328 if (kwdict == NULL)
4329 goto ext_call_fail;
4330 }
4331 callargs = update_star_args(na, nstar, stararg, pp_stack);
4332 if (callargs == NULL)
4333 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004334#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 /* At this point, we have to look at the type of func to
4336 update the call stats properly. Do it here so as to avoid
4337 exposing the call stats machinery outside ceval.c
4338 */
4339 if (PyFunction_Check(func))
4340 PCALL(PCALL_FUNCTION);
4341 else if (PyMethod_Check(func))
4342 PCALL(PCALL_METHOD);
4343 else if (PyType_Check(func))
4344 PCALL(PCALL_TYPE);
4345 else if (PyCFunction_Check(func))
4346 PCALL(PCALL_CFUNCTION);
4347 else
4348 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004349#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004350 if (PyCFunction_Check(func)) {
4351 PyThreadState *tstate = PyThreadState_GET();
4352 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4353 }
4354 else
4355 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004356ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 Py_XDECREF(callargs);
4358 Py_XDECREF(kwdict);
4359 Py_XDECREF(stararg);
4360 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004361}
4362
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004363/* Extract a slice index from a PyInt or PyLong or an object with the
4364 nb_index slot defined, and store in *pi.
4365 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4366 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 +00004367 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004368*/
Tim Petersb5196382001-12-16 19:44:20 +00004369/* Note: If v is NULL, return success without storing into *pi. This
4370 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4371 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004372*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004373int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004374_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004375{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376 if (v != NULL) {
4377 Py_ssize_t x;
4378 if (PyIndex_Check(v)) {
4379 x = PyNumber_AsSsize_t(v, NULL);
4380 if (x == -1 && PyErr_Occurred())
4381 return 0;
4382 }
4383 else {
4384 PyErr_SetString(PyExc_TypeError,
4385 "slice indices must be integers or "
4386 "None or have an __index__ method");
4387 return 0;
4388 }
4389 *pi = x;
4390 }
4391 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004392}
4393
Guido van Rossum486364b2007-06-30 05:01:58 +00004394#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004396
Guido van Rossumb209a111997-04-29 18:18:01 +00004397static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004398cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 int res = 0;
4401 switch (op) {
4402 case PyCmp_IS:
4403 res = (v == w);
4404 break;
4405 case PyCmp_IS_NOT:
4406 res = (v != w);
4407 break;
4408 case PyCmp_IN:
4409 res = PySequence_Contains(w, v);
4410 if (res < 0)
4411 return NULL;
4412 break;
4413 case PyCmp_NOT_IN:
4414 res = PySequence_Contains(w, v);
4415 if (res < 0)
4416 return NULL;
4417 res = !res;
4418 break;
4419 case PyCmp_EXC_MATCH:
4420 if (PyTuple_Check(w)) {
4421 Py_ssize_t i, length;
4422 length = PyTuple_Size(w);
4423 for (i = 0; i < length; i += 1) {
4424 PyObject *exc = PyTuple_GET_ITEM(w, i);
4425 if (!PyExceptionClass_Check(exc)) {
4426 PyErr_SetString(PyExc_TypeError,
4427 CANNOT_CATCH_MSG);
4428 return NULL;
4429 }
4430 }
4431 }
4432 else {
4433 if (!PyExceptionClass_Check(w)) {
4434 PyErr_SetString(PyExc_TypeError,
4435 CANNOT_CATCH_MSG);
4436 return NULL;
4437 }
4438 }
4439 res = PyErr_GivenExceptionMatches(v, w);
4440 break;
4441 default:
4442 return PyObject_RichCompare(v, w, op);
4443 }
4444 v = res ? Py_True : Py_False;
4445 Py_INCREF(v);
4446 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004447}
4448
Thomas Wouters52152252000-08-17 22:55:00 +00004449static PyObject *
4450import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004451{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004452 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004454 x = PyObject_GetAttr(v, name);
4455 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4456 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4457 }
4458 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004459}
Guido van Rossumac7be682001-01-17 15:42:30 +00004460
Thomas Wouters52152252000-08-17 22:55:00 +00004461static int
4462import_all_from(PyObject *locals, PyObject *v)
4463{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004464 _Py_IDENTIFIER(__all__);
4465 _Py_IDENTIFIER(__dict__);
4466 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 PyObject *dict, *name, *value;
4468 int skip_leading_underscores = 0;
4469 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004470
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004471 if (all == NULL) {
4472 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4473 return -1; /* Unexpected error */
4474 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004475 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004476 if (dict == NULL) {
4477 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4478 return -1;
4479 PyErr_SetString(PyExc_ImportError,
4480 "from-import-* object has no __dict__ and no __all__");
4481 return -1;
4482 }
4483 all = PyMapping_Keys(dict);
4484 Py_DECREF(dict);
4485 if (all == NULL)
4486 return -1;
4487 skip_leading_underscores = 1;
4488 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 for (pos = 0, err = 0; ; pos++) {
4491 name = PySequence_GetItem(all, pos);
4492 if (name == NULL) {
4493 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4494 err = -1;
4495 else
4496 PyErr_Clear();
4497 break;
4498 }
4499 if (skip_leading_underscores &&
4500 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004501 PyUnicode_READY(name) != -1 &&
4502 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 {
4504 Py_DECREF(name);
4505 continue;
4506 }
4507 value = PyObject_GetAttr(v, name);
4508 if (value == NULL)
4509 err = -1;
4510 else if (PyDict_CheckExact(locals))
4511 err = PyDict_SetItem(locals, name, value);
4512 else
4513 err = PyObject_SetItem(locals, name, value);
4514 Py_DECREF(name);
4515 Py_XDECREF(value);
4516 if (err != 0)
4517 break;
4518 }
4519 Py_DECREF(all);
4520 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004521}
4522
Guido van Rossumac7be682001-01-17 15:42:30 +00004523static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004524format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004526 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 if (!obj)
4529 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 obj_str = _PyUnicode_AsString(obj);
4532 if (!obj_str)
4533 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004535 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004536}
Guido van Rossum950361c1997-01-24 13:49:28 +00004537
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004538static void
4539format_exc_unbound(PyCodeObject *co, int oparg)
4540{
4541 PyObject *name;
4542 /* Don't stomp existing exception */
4543 if (PyErr_Occurred())
4544 return;
4545 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4546 name = PyTuple_GET_ITEM(co->co_cellvars,
4547 oparg);
4548 format_exc_check_arg(
4549 PyExc_UnboundLocalError,
4550 UNBOUNDLOCAL_ERROR_MSG,
4551 name);
4552 } else {
4553 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4554 PyTuple_GET_SIZE(co->co_cellvars));
4555 format_exc_check_arg(PyExc_NameError,
4556 UNBOUNDFREE_ERROR_MSG, name);
4557 }
4558}
4559
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004560static PyObject *
4561unicode_concatenate(PyObject *v, PyObject *w,
4562 PyFrameObject *f, unsigned char *next_instr)
4563{
4564 PyObject *res;
4565 if (Py_REFCNT(v) == 2) {
4566 /* In the common case, there are 2 references to the value
4567 * stored in 'variable' when the += is performed: one on the
4568 * value stack (in 'v') and one still stored in the
4569 * 'variable'. We try to delete the variable now to reduce
4570 * the refcnt to 1.
4571 */
4572 switch (*next_instr) {
4573 case STORE_FAST:
4574 {
4575 int oparg = PEEKARG();
4576 PyObject **fastlocals = f->f_localsplus;
4577 if (GETLOCAL(oparg) == v)
4578 SETLOCAL(oparg, NULL);
4579 break;
4580 }
4581 case STORE_DEREF:
4582 {
4583 PyObject **freevars = (f->f_localsplus +
4584 f->f_code->co_nlocals);
4585 PyObject *c = freevars[PEEKARG()];
4586 if (PyCell_GET(c) == v)
4587 PyCell_Set(c, NULL);
4588 break;
4589 }
4590 case STORE_NAME:
4591 {
4592 PyObject *names = f->f_code->co_names;
4593 PyObject *name = GETITEM(names, PEEKARG());
4594 PyObject *locals = f->f_locals;
4595 if (PyDict_CheckExact(locals) &&
4596 PyDict_GetItem(locals, name) == v) {
4597 if (PyDict_DelItem(locals, name) != 0) {
4598 PyErr_Clear();
4599 }
4600 }
4601 break;
4602 }
4603 }
4604 }
4605 res = v;
4606 PyUnicode_Append(&res, w);
4607 return res;
4608}
4609
Guido van Rossum950361c1997-01-24 13:49:28 +00004610#ifdef DYNAMIC_EXECUTION_PROFILE
4611
Skip Montanarof118cb12001-10-15 20:51:38 +00004612static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004613getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004614{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004615 int i;
4616 PyObject *l = PyList_New(256);
4617 if (l == NULL) return NULL;
4618 for (i = 0; i < 256; i++) {
4619 PyObject *x = PyLong_FromLong(a[i]);
4620 if (x == NULL) {
4621 Py_DECREF(l);
4622 return NULL;
4623 }
4624 PyList_SetItem(l, i, x);
4625 }
4626 for (i = 0; i < 256; i++)
4627 a[i] = 0;
4628 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004629}
4630
4631PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004632_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004633{
4634#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004635 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004636#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004637 int i;
4638 PyObject *l = PyList_New(257);
4639 if (l == NULL) return NULL;
4640 for (i = 0; i < 257; i++) {
4641 PyObject *x = getarray(dxpairs[i]);
4642 if (x == NULL) {
4643 Py_DECREF(l);
4644 return NULL;
4645 }
4646 PyList_SetItem(l, i, x);
4647 }
4648 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004649#endif
4650}
4651
4652#endif