blob: 32c203ecbcf030aaa73d5dd16de4c6eafe668404 [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 */
Stefan Krahb7e10102010-06-23 18:42:39 +0000745 WHY_RETURN = 0x0008, /* 'return' statement */
746 WHY_BREAK = 0x0010, /* 'break' statement */
747 WHY_CONTINUE = 0x0020, /* 'continue' statement */
748 WHY_YIELD = 0x0040, /* 'yield' operator */
749 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000750};
Guido van Rossum374a9221991-04-04 10:40:29 +0000751
Benjamin Peterson87880242011-07-03 16:48:31 -0500752static void save_exc_state(PyThreadState *, PyFrameObject *);
753static void swap_exc_state(PyThreadState *, PyFrameObject *);
754static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400755static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000756static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000757
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000758/* Records whether tracing is on for any thread. Counts the number of
759 threads for which tstate->c_tracefunc is non-NULL, so if the value
760 is 0, we know we don't have to check this thread's c_tracefunc.
761 This speeds up the if statement in PyEval_EvalFrameEx() after
762 fast_next_opcode*/
763static int _Py_TracingPossible = 0;
764
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000765
Guido van Rossum374a9221991-04-04 10:40:29 +0000766
Guido van Rossumb209a111997-04-29 18:18:01 +0000767PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000768PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 return PyEval_EvalCodeEx(co,
771 globals, locals,
772 (PyObject **)NULL, 0,
773 (PyObject **)NULL, 0,
774 (PyObject **)NULL, 0,
775 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000776}
777
778
779/* Interpreter main loop */
780
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000781PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000782PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 /* This is for backward compatibility with extension modules that
784 used this API; core interpreter code should call
785 PyEval_EvalFrameEx() */
786 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000787}
788
789PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000790PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000791{
Guido van Rossum950361c1997-01-24 13:49:28 +0000792#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000794#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 register PyObject **stack_pointer; /* Next free slot in value stack */
796 register unsigned char *next_instr;
797 register int opcode; /* Current opcode */
798 register int oparg; /* Current opcode argument, if any */
799 register enum why_code why; /* Reason for block stack unwind */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 register PyObject **fastlocals, **freevars;
801 PyObject *retval = NULL; /* Return value */
802 PyThreadState *tstate = PyThreadState_GET();
803 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000808
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 is true when the line being executed has changed. The
810 initial values are such as to make this false the first
811 time it is tested. */
812 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 unsigned char *first_instr;
815 PyObject *names;
816 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000817
Brett Cannon368b4b72012-04-02 12:17:59 -0400818#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200819 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400820#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200821
Antoine Pitroub52ec782009-01-25 16:34:23 +0000822/* Computed GOTOs, or
823 the-optimization-commonly-but-improperly-known-as-"threaded code"
824 using gcc's labels-as-values extension
825 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
826
827 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829 combined with a lookup table of jump addresses. However, since the
830 indirect jump instruction is shared by all opcodes, the CPU will have a
831 hard time making the right prediction for where to jump next (actually,
832 it will be always wrong except in the uncommon case of a sequence of
833 several identical opcodes).
834
835 "Threaded code" in contrast, uses an explicit jump table and an explicit
836 indirect jump instruction at the end of each opcode. Since the jump
837 instruction is at a different address for each opcode, the CPU will make a
838 separate prediction for each of these instructions, which is equivalent to
839 predicting the second opcode of each opcode pair. These predictions have
840 a much better chance to turn out valid, especially in small bytecode loops.
841
842 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000844 and potentially many more instructions (depending on the pipeline width).
845 A correctly predicted branch, however, is nearly free.
846
847 At the time of this writing, the "threaded code" version is up to 15-20%
848 faster than the normal "switch" version, depending on the compiler and the
849 CPU architecture.
850
851 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
852 because it would render the measurements invalid.
853
854
855 NOTE: care must be taken that the compiler doesn't try to "optimize" the
856 indirect jumps by sharing them between all opcodes. Such optimizations
857 can be disabled on gcc by using the -fno-gcse flag (or possibly
858 -fno-crossjumping).
859*/
860
Antoine Pitrou042b1282010-08-13 21:15:58 +0000861#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000863#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000864#endif
865
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#ifdef HAVE_COMPUTED_GOTOS
867 #ifndef USE_COMPUTED_GOTOS
868 #define USE_COMPUTED_GOTOS 1
869 #endif
870#else
871 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
872 #error "Computed gotos are not supported on this compiler."
873 #endif
874 #undef USE_COMPUTED_GOTOS
875 #define USE_COMPUTED_GOTOS 0
876#endif
877
878#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000879/* Import the static jump table */
880#include "opcode_targets.h"
881
882/* This macro is used when several opcodes defer to the same implementation
883 (e.g. SETUP_LOOP, SETUP_FINALLY) */
884#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 TARGET_##op: \
886 opcode = op; \
887 if (HAS_ARG(op)) \
888 oparg = NEXTARG(); \
889 case op: \
890 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000891
892#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 TARGET_##op: \
894 opcode = op; \
895 if (HAS_ARG(op)) \
896 oparg = NEXTARG(); \
897 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000898
899
900#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 { \
902 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
903 FAST_DISPATCH(); \
904 } \
905 continue; \
906 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000907
908#ifdef LLTRACE
909#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 { \
911 if (!lltrace && !_Py_TracingPossible) { \
912 f->f_lasti = INSTR_OFFSET(); \
913 goto *opcode_targets[*next_instr++]; \
914 } \
915 goto fast_next_opcode; \
916 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000917#else
918#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 { \
920 if (!_Py_TracingPossible) { \
921 f->f_lasti = INSTR_OFFSET(); \
922 goto *opcode_targets[*next_instr++]; \
923 } \
924 goto fast_next_opcode; \
925 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#endif
927
928#else
929#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000931#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 /* silence compiler warnings about `impl` unused */ \
933 if (0) goto impl; \
934 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000935#define DISPATCH() continue
936#define FAST_DISPATCH() goto fast_next_opcode
937#endif
938
939
Neal Norwitza81d2202002-07-14 00:27:26 +0000940/* Tuple access macros */
941
942#ifndef Py_DEBUG
943#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
944#else
945#define GETITEM(v, i) PyTuple_GetItem((v), (i))
946#endif
947
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000948#ifdef WITH_TSC
949/* Use Pentium timestamp counter to mark certain events:
950 inst0 -- beginning of switch statement for opcode dispatch
951 inst1 -- end of switch statement (may be skipped)
952 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000953 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000954 (may be skipped)
955 intr1 -- beginning of long interruption
956 intr2 -- end of long interruption
957
958 Many opcodes call out to helper C functions. In some cases, the
959 time in those functions should be counted towards the time for the
960 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
961 calls another Python function; there's no point in charge all the
962 bytecode executed by the called function to the caller.
963
964 It's hard to make a useful judgement statically. In the presence
965 of operator overloading, it's impossible to tell if a call will
966 execute new Python code or not.
967
968 It's a case-by-case judgement. I'll use intr1 for the following
969 cases:
970
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000971 IMPORT_STAR
972 IMPORT_FROM
973 CALL_FUNCTION (and friends)
974
975 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
977 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 READ_TIMESTAMP(inst0);
980 READ_TIMESTAMP(inst1);
981 READ_TIMESTAMP(loop0);
982 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 /* shut up the compiler */
985 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000986#endif
987
Guido van Rossum374a9221991-04-04 10:40:29 +0000988/* Code access macros */
989
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990#define INSTR_OFFSET() ((int)(next_instr - first_instr))
991#define NEXTOP() (*next_instr++)
992#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
993#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
994#define JUMPTO(x) (next_instr = first_instr + (x))
995#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000996
Raymond Hettingerf606f872003-03-16 03:11:04 +0000997/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 Some opcodes tend to come in pairs thus making it possible to
999 predict the second code when the first is run. For example,
1000 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1001 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 Verifying the prediction costs a single high-speed test of a register
1004 variable against a constant. If the pairing was good, then the
1005 processor's own internal branch predication has a high likelihood of
1006 success, resulting in a nearly zero-overhead transition to the
1007 next opcode. A successful prediction saves a trip through the eval-loop
1008 including its two unpredictable branches, the HAS_ARG test and the
1009 switch-case. Combined with the processor's internal branch prediction,
1010 a successful PREDICT has the effect of making the two opcodes run as if
1011 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001012
Georg Brandl86b2fb92008-07-16 03:43:04 +00001013 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 predictions turned-on and interpret the results as if some opcodes
1015 had been combined or turn-off predictions so that the opcode frequency
1016 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017
1018 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 the CPU to record separate branch prediction information for each
1020 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001021
Raymond Hettingerf606f872003-03-16 03:11:04 +00001022*/
1023
Antoine Pitrou042b1282010-08-13 21:15:58 +00001024#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025#define PREDICT(op) if (0) goto PRED_##op
1026#define PREDICTED(op) PRED_##op:
1027#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001028#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1030#define PREDICTED(op) PRED_##op: next_instr++
1031#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001032#endif
1033
Raymond Hettingerf606f872003-03-16 03:11:04 +00001034
Guido van Rossum374a9221991-04-04 10:40:29 +00001035/* Stack manipulation macros */
1036
Martin v. Löwis18e16552006-02-15 17:27:45 +00001037/* The stack can grow at most MAXINT deep, as co_nlocals and
1038 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001039#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1040#define EMPTY() (STACK_LEVEL() == 0)
1041#define TOP() (stack_pointer[-1])
1042#define SECOND() (stack_pointer[-2])
1043#define THIRD() (stack_pointer[-3])
1044#define FOURTH() (stack_pointer[-4])
1045#define PEEK(n) (stack_pointer[-(n)])
1046#define SET_TOP(v) (stack_pointer[-1] = (v))
1047#define SET_SECOND(v) (stack_pointer[-2] = (v))
1048#define SET_THIRD(v) (stack_pointer[-3] = (v))
1049#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1050#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1051#define BASIC_STACKADJ(n) (stack_pointer += n)
1052#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1053#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001054
Guido van Rossum96a42c81992-01-12 02:29:51 +00001055#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001057 lltrace && prtrace(TOP(), "push")); \
1058 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001060 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001062 lltrace && prtrace(TOP(), "stackadj")); \
1063 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001064#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1066 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001067#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001068#define PUSH(v) BASIC_PUSH(v)
1069#define POP() BASIC_POP()
1070#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001071#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001072#endif
1073
Guido van Rossum681d79a1995-07-18 14:51:37 +00001074/* Local variable macros */
1075
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001077
1078/* The SETLOCAL() macro must not DECREF the local variable in-place and
1079 then store the new value; it must copy the old value to a temporary
1080 value, then store the new value, and then DECREF the temporary value.
1081 This is because it is possible that during the DECREF the frame is
1082 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1083 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001085 GETLOCAL(i) = value; \
1086 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001087
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001088
1089#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 while (STACK_LEVEL() > (b)->b_level) { \
1091 PyObject *v = POP(); \
1092 Py_XDECREF(v); \
1093 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001094
1095#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 { \
1097 PyObject *type, *value, *traceback; \
1098 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1099 while (STACK_LEVEL() > (b)->b_level + 3) { \
1100 value = POP(); \
1101 Py_XDECREF(value); \
1102 } \
1103 type = tstate->exc_type; \
1104 value = tstate->exc_value; \
1105 traceback = tstate->exc_traceback; \
1106 tstate->exc_type = POP(); \
1107 tstate->exc_value = POP(); \
1108 tstate->exc_traceback = POP(); \
1109 Py_XDECREF(type); \
1110 Py_XDECREF(value); \
1111 Py_XDECREF(traceback); \
1112 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001113
Guido van Rossuma027efa1997-05-05 20:56:21 +00001114/* Start of code */
1115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 /* push frame */
1117 if (Py_EnterRecursiveCall(""))
1118 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001121
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 if (tstate->use_tracing) {
1123 if (tstate->c_tracefunc != NULL) {
1124 /* tstate->c_tracefunc, if defined, is a
1125 function that will be called on *every* entry
1126 to a code block. Its return value, if not
1127 None, is a function that will be called at
1128 the start of each executed line of code.
1129 (Actually, the function must return itself
1130 in order to continue tracing.) The trace
1131 functions are called with three arguments:
1132 a pointer to the current frame, a string
1133 indicating why the function is called, and
1134 an argument which depends on the situation.
1135 The global trace function is also called
1136 whenever an exception is detected. */
1137 if (call_trace_protected(tstate->c_tracefunc,
1138 tstate->c_traceobj,
1139 f, PyTrace_CALL, Py_None)) {
1140 /* Trace function raised an error */
1141 goto exit_eval_frame;
1142 }
1143 }
1144 if (tstate->c_profilefunc != NULL) {
1145 /* Similar for c_profilefunc, except it needn't
1146 return itself and isn't called for "line" events */
1147 if (call_trace_protected(tstate->c_profilefunc,
1148 tstate->c_profileobj,
1149 f, PyTrace_CALL, Py_None)) {
1150 /* Profile function raised an error */
1151 goto exit_eval_frame;
1152 }
1153 }
1154 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001155
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001156 co = f->f_code;
1157 names = co->co_names;
1158 consts = co->co_consts;
1159 fastlocals = f->f_localsplus;
1160 freevars = f->f_localsplus + co->co_nlocals;
1161 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1162 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001164 f->f_lasti now refers to the index of the last instruction
1165 executed. You might think this was obvious from the name, but
1166 this wasn't always true before 2.3! PyFrame_New now sets
1167 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1168 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1169 does work. Promise.
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001170 YIELD_FROM sets f_lasti to itself, in order to repeated yield
1171 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 When the PREDICT() macros are enabled, some opcode pairs follow in
1174 direct succession without updating f->f_lasti. A successful
1175 prediction effectively links the two codes together as if they
1176 were a single new opcode; accordingly,f->f_lasti will point to
1177 the first code in the pair (for instance, GET_ITER followed by
1178 FOR_ITER is effectively a single opcode and f->f_lasti will point
1179 at to the beginning of the combined pair.)
1180 */
1181 next_instr = first_instr + f->f_lasti + 1;
1182 stack_pointer = f->f_stacktop;
1183 assert(stack_pointer != NULL);
1184 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (co->co_flags & CO_GENERATOR && !throwflag) {
1187 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1188 /* We were in an except handler when we left,
1189 restore the exception state which was put aside
1190 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -05001191 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 }
Benjamin Peterson87880242011-07-03 16:48:31 -05001193 else
1194 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001195 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001196
Tim Peters5ca576e2001-06-18 22:08:13 +00001197#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001198 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001199#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +00001202
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001203 if (throwflag) /* support for generator.throw() */
1204 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001207#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 if (inst1 == 0) {
1209 /* Almost surely, the opcode executed a break
1210 or a continue, preventing inst1 from being set
1211 on the way out of the loop.
1212 */
1213 READ_TIMESTAMP(inst1);
1214 loop1 = inst1;
1215 }
1216 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1217 intr0, intr1);
1218 ticked = 0;
1219 inst1 = 0;
1220 intr0 = 0;
1221 intr1 = 0;
1222 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001223#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1225 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 /* Do periodic things. Doing this every time through
1228 the loop would add too much overhead, so we do it
1229 only every Nth instruction. We also do it if
1230 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1231 event needs attention (e.g. a signal handler or
1232 async I/O handler); see Py_AddPendingCall() and
1233 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1236 if (*next_instr == SETUP_FINALLY) {
1237 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001238 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 goto fast_next_opcode;
1240 }
1241 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001242#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001244#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 if (Py_MakePendingCalls() < 0)
1247 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001248 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001249#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001250 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 /* Give another thread a chance */
1252 if (PyThreadState_Swap(NULL) != tstate)
1253 Py_FatalError("ceval: tstate mix-up");
1254 drop_gil(tstate);
1255
1256 /* Other threads may run now */
1257
1258 take_gil(tstate);
1259 if (PyThreadState_Swap(tstate) != NULL)
1260 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001262#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 /* Check for asynchronous exceptions. */
1264 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001265 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 tstate->async_exc = NULL;
1267 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001268 PyErr_SetNone(exc);
1269 Py_DECREF(exc);
1270 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 }
1272 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 fast_next_opcode:
1275 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001279 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001280 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001281 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 /* see maybe_call_line_trace
1283 for expository comments */
1284 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 err = maybe_call_line_trace(tstate->c_tracefunc,
1287 tstate->c_traceobj,
1288 f, &instr_lb, &instr_ub,
1289 &instr_prev);
1290 /* Reload possibly changed frame fields */
1291 JUMPTO(f->f_lasti);
1292 if (f->f_stacktop != NULL) {
1293 stack_pointer = f->f_stacktop;
1294 f->f_stacktop = NULL;
1295 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001298 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001301 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 opcode = NEXTOP();
1304 oparg = 0; /* allows oparg to be stored in a register because
1305 it doesn't have to be remembered across a full loop */
1306 if (HAS_ARG(opcode))
1307 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001308 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001309#ifdef DYNAMIC_EXECUTION_PROFILE
1310#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 dxpairs[lastopcode][opcode]++;
1312 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001313#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001315#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001316
Guido van Rossum96a42c81992-01-12 02:29:51 +00001317#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 if (lltrace) {
1321 if (HAS_ARG(opcode)) {
1322 printf("%d: %d, %d\n",
1323 f->f_lasti, opcode, oparg);
1324 }
1325 else {
1326 printf("%d: %d\n",
1327 f->f_lasti, opcode);
1328 }
1329 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001330#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001331
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 /* Main switch on opcode */
1333 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 /* BEWARE!
1338 It is essential that any operation that fails sets either
1339 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1340 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 TARGET(NOP)
1343 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001344
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001345 TARGET(LOAD_FAST) {
1346 PyObject *value = GETLOCAL(oparg);
1347 if (value == NULL) {
1348 format_exc_check_arg(PyExc_UnboundLocalError,
1349 UNBOUNDLOCAL_ERROR_MSG,
1350 PyTuple_GetItem(co->co_varnames, oparg));
1351 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001353 Py_INCREF(value);
1354 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001356 }
1357
1358 TARGET(LOAD_CONST) {
1359 PyObject *value = GETITEM(consts, oparg);
1360 Py_INCREF(value);
1361 PUSH(value);
1362 FAST_DISPATCH();
1363 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001366 TARGET(STORE_FAST) {
1367 PyObject *value = POP();
1368 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001370 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001371
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001372 TARGET(POP_TOP) {
1373 PyObject *value = POP();
1374 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001377
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001378 TARGET(ROT_TWO) {
1379 PyObject *top = TOP();
1380 PyObject *second = SECOND();
1381 SET_TOP(second);
1382 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001384 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001385
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001386 TARGET(ROT_THREE) {
1387 PyObject *top = TOP();
1388 PyObject *second = SECOND();
1389 PyObject *third = THIRD();
1390 SET_TOP(second);
1391 SET_SECOND(third);
1392 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001394 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001396 TARGET(DUP_TOP) {
1397 PyObject *top = TOP();
1398 Py_INCREF(top);
1399 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 TARGET(DUP_TOP_TWO) {
1404 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001406 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001407 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001408 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 SET_TOP(top);
1410 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001411 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001413
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001414 TARGET(UNARY_POSITIVE) {
1415 PyObject *value = TOP();
1416 PyObject *res = PyNumber_Positive(value);
1417 Py_DECREF(value);
1418 SET_TOP(res);
1419 if (res == NULL)
1420 goto error;
1421 DISPATCH();
1422 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001424 TARGET(UNARY_NEGATIVE) {
1425 PyObject *value = TOP();
1426 PyObject *res = PyNumber_Negative(value);
1427 Py_DECREF(value);
1428 SET_TOP(res);
1429 if (res == NULL)
1430 goto error;
1431 DISPATCH();
1432 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001433
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001434 TARGET(UNARY_NOT) {
1435 PyObject *value = TOP();
1436 int err = PyObject_IsTrue(value);
1437 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 if (err == 0) {
1439 Py_INCREF(Py_True);
1440 SET_TOP(Py_True);
1441 DISPATCH();
1442 }
1443 else if (err > 0) {
1444 Py_INCREF(Py_False);
1445 SET_TOP(Py_False);
1446 err = 0;
1447 DISPATCH();
1448 }
1449 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001450 goto error;
1451 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001452
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001453 TARGET(UNARY_INVERT) {
1454 PyObject *value = TOP();
1455 PyObject *res = PyNumber_Invert(value);
1456 Py_DECREF(value);
1457 SET_TOP(res);
1458 if (res == NULL)
1459 goto error;
1460 DISPATCH();
1461 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001462
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001463 TARGET(BINARY_POWER) {
1464 PyObject *exp = POP();
1465 PyObject *base = TOP();
1466 PyObject *res = PyNumber_Power(base, exp, Py_None);
1467 Py_DECREF(base);
1468 Py_DECREF(exp);
1469 SET_TOP(res);
1470 if (res == NULL)
1471 goto error;
1472 DISPATCH();
1473 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001474
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001475 TARGET(BINARY_MULTIPLY) {
1476 PyObject *right = POP();
1477 PyObject *left = TOP();
1478 PyObject *res = PyNumber_Multiply(left, right);
1479 Py_DECREF(left);
1480 Py_DECREF(right);
1481 SET_TOP(res);
1482 if (res == NULL)
1483 goto error;
1484 DISPATCH();
1485 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001487 TARGET(BINARY_TRUE_DIVIDE) {
1488 PyObject *divisor = POP();
1489 PyObject *dividend = TOP();
1490 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1491 Py_DECREF(dividend);
1492 Py_DECREF(divisor);
1493 SET_TOP(quotient);
1494 if (quotient == NULL)
1495 goto error;
1496 DISPATCH();
1497 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001499 TARGET(BINARY_FLOOR_DIVIDE) {
1500 PyObject *divisor = POP();
1501 PyObject *dividend = TOP();
1502 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1503 Py_DECREF(dividend);
1504 Py_DECREF(divisor);
1505 SET_TOP(quotient);
1506 if (quotient == NULL)
1507 goto error;
1508 DISPATCH();
1509 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001510
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001511 TARGET(BINARY_MODULO) {
1512 PyObject *divisor = POP();
1513 PyObject *dividend = TOP();
1514 PyObject *res = PyUnicode_CheckExact(dividend) ?
1515 PyUnicode_Format(dividend, divisor) :
1516 PyNumber_Remainder(dividend, divisor);
1517 Py_DECREF(divisor);
1518 Py_DECREF(dividend);
1519 SET_TOP(res);
1520 if (res == NULL)
1521 goto error;
1522 DISPATCH();
1523 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001524
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001525 TARGET(BINARY_ADD) {
1526 PyObject *right = POP();
1527 PyObject *left = TOP();
1528 PyObject *sum;
1529 if (PyUnicode_CheckExact(left) &&
1530 PyUnicode_CheckExact(right)) {
1531 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001532 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001533 }
1534 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001535 sum = PyNumber_Add(left, right);
1536 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001537 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001538 Py_DECREF(right);
1539 SET_TOP(sum);
1540 if (sum == NULL)
1541 goto error;
1542 DISPATCH();
1543 }
1544
1545 TARGET(BINARY_SUBTRACT) {
1546 PyObject *right = POP();
1547 PyObject *left = TOP();
1548 PyObject *diff = PyNumber_Subtract(left, right);
1549 Py_DECREF(right);
1550 Py_DECREF(left);
1551 SET_TOP(diff);
1552 if (diff == NULL)
1553 goto error;
1554 DISPATCH();
1555 }
1556
1557 TARGET(BINARY_SUBSCR) {
1558 PyObject *sub = POP();
1559 PyObject *container = TOP();
1560 PyObject *res = PyObject_GetItem(container, sub);
1561 Py_DECREF(container);
1562 Py_DECREF(sub);
1563 SET_TOP(res);
1564 if (res == NULL)
1565 goto error;
1566 DISPATCH();
1567 }
1568
1569 TARGET(BINARY_LSHIFT) {
1570 PyObject *right = POP();
1571 PyObject *left = TOP();
1572 PyObject *res = PyNumber_Lshift(left, right);
1573 Py_DECREF(left);
1574 Py_DECREF(right);
1575 SET_TOP(res);
1576 if (res == NULL)
1577 goto error;
1578 DISPATCH();
1579 }
1580
1581 TARGET(BINARY_RSHIFT) {
1582 PyObject *right = POP();
1583 PyObject *left = TOP();
1584 PyObject *res = PyNumber_Rshift(left, right);
1585 Py_DECREF(left);
1586 Py_DECREF(right);
1587 SET_TOP(res);
1588 if (res == NULL)
1589 goto error;
1590 DISPATCH();
1591 }
1592
1593 TARGET(BINARY_AND) {
1594 PyObject *right = POP();
1595 PyObject *left = TOP();
1596 PyObject *res = PyNumber_And(left, right);
1597 Py_DECREF(left);
1598 Py_DECREF(right);
1599 SET_TOP(res);
1600 if (res == NULL)
1601 goto error;
1602 DISPATCH();
1603 }
1604
1605 TARGET(BINARY_XOR) {
1606 PyObject *right = POP();
1607 PyObject *left = TOP();
1608 PyObject *res = PyNumber_Xor(left, right);
1609 Py_DECREF(left);
1610 Py_DECREF(right);
1611 SET_TOP(res);
1612 if (res == NULL)
1613 goto error;
1614 DISPATCH();
1615 }
1616
1617 TARGET(BINARY_OR) {
1618 PyObject *right = POP();
1619 PyObject *left = TOP();
1620 PyObject *res = PyNumber_Or(left, right);
1621 Py_DECREF(left);
1622 Py_DECREF(right);
1623 SET_TOP(res);
1624 if (res == NULL)
1625 goto error;
1626 DISPATCH();
1627 }
1628
1629 TARGET(LIST_APPEND) {
1630 PyObject *v = POP();
1631 PyObject *list = PEEK(oparg);
1632 int err;
1633 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 if (err != 0)
1636 goto error;
1637 PREDICT(JUMP_ABSOLUTE);
1638 DISPATCH();
1639 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001641 TARGET(SET_ADD) {
1642 PyObject *v = POP();
1643 PyObject *set = stack_pointer[-oparg];
1644 int err;
1645 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001647 if (err != 0)
1648 goto error;
1649 PREDICT(JUMP_ABSOLUTE);
1650 DISPATCH();
1651 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001653 TARGET(INPLACE_POWER) {
1654 PyObject *exp = POP();
1655 PyObject *base = TOP();
1656 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1657 Py_DECREF(base);
1658 Py_DECREF(exp);
1659 SET_TOP(res);
1660 if (res == NULL)
1661 goto error;
1662 DISPATCH();
1663 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001664
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 TARGET(INPLACE_MULTIPLY) {
1666 PyObject *right = POP();
1667 PyObject *left = TOP();
1668 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1669 Py_DECREF(left);
1670 Py_DECREF(right);
1671 SET_TOP(res);
1672 if (res == NULL)
1673 goto error;
1674 DISPATCH();
1675 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001677 TARGET(INPLACE_TRUE_DIVIDE) {
1678 PyObject *divisor = POP();
1679 PyObject *dividend = TOP();
1680 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1681 Py_DECREF(dividend);
1682 Py_DECREF(divisor);
1683 SET_TOP(quotient);
1684 if (quotient == NULL)
1685 goto error;
1686 DISPATCH();
1687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001688
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 TARGET(INPLACE_FLOOR_DIVIDE) {
1690 PyObject *divisor = POP();
1691 PyObject *dividend = TOP();
1692 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1693 Py_DECREF(dividend);
1694 Py_DECREF(divisor);
1695 SET_TOP(quotient);
1696 if (quotient == NULL)
1697 goto error;
1698 DISPATCH();
1699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001700
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001701 TARGET(INPLACE_MODULO) {
1702 PyObject *right = POP();
1703 PyObject *left = TOP();
1704 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1705 Py_DECREF(left);
1706 Py_DECREF(right);
1707 SET_TOP(mod);
1708 if (mod == NULL)
1709 goto error;
1710 DISPATCH();
1711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001713 TARGET(INPLACE_ADD) {
1714 PyObject *right = POP();
1715 PyObject *left = TOP();
1716 PyObject *sum;
1717 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1718 sum = unicode_concatenate(left, right, f, next_instr);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001719 /* unicode_concatenate consumed the ref to v */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001720 }
1721 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001722 sum = PyNumber_InPlaceAdd(left, right);
1723 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001724 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001725 Py_DECREF(right);
1726 SET_TOP(sum);
1727 if (sum == NULL)
1728 goto error;
1729 DISPATCH();
1730 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001732 TARGET(INPLACE_SUBTRACT) {
1733 PyObject *right = POP();
1734 PyObject *left = TOP();
1735 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1736 Py_DECREF(left);
1737 Py_DECREF(right);
1738 SET_TOP(diff);
1739 if (diff == NULL)
1740 goto error;
1741 DISPATCH();
1742 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001743
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001744 TARGET(INPLACE_LSHIFT) {
1745 PyObject *right = POP();
1746 PyObject *left = TOP();
1747 PyObject *res = PyNumber_InPlaceLshift(left, right);
1748 Py_DECREF(left);
1749 Py_DECREF(right);
1750 SET_TOP(res);
1751 if (res == NULL)
1752 goto error;
1753 DISPATCH();
1754 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001755
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001756 TARGET(INPLACE_RSHIFT) {
1757 PyObject *right = POP();
1758 PyObject *left = TOP();
1759 PyObject *res = PyNumber_InPlaceRshift(left, right);
1760 Py_DECREF(left);
1761 Py_DECREF(right);
1762 SET_TOP(res);
1763 if (res == NULL)
1764 goto error;
1765 DISPATCH();
1766 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001767
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001768 TARGET(INPLACE_AND) {
1769 PyObject *right = POP();
1770 PyObject *left = TOP();
1771 PyObject *res = PyNumber_InPlaceAnd(left, right);
1772 Py_DECREF(left);
1773 Py_DECREF(right);
1774 SET_TOP(res);
1775 if (res == NULL)
1776 goto error;
1777 DISPATCH();
1778 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001779
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001780 TARGET(INPLACE_XOR) {
1781 PyObject *right = POP();
1782 PyObject *left = TOP();
1783 PyObject *res = PyNumber_InPlaceXor(left, right);
1784 Py_DECREF(left);
1785 Py_DECREF(right);
1786 SET_TOP(res);
1787 if (res == NULL)
1788 goto error;
1789 DISPATCH();
1790 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001791
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001792 TARGET(INPLACE_OR) {
1793 PyObject *right = POP();
1794 PyObject *left = TOP();
1795 PyObject *res = PyNumber_InPlaceOr(left, right);
1796 Py_DECREF(left);
1797 Py_DECREF(right);
1798 SET_TOP(res);
1799 if (res == NULL)
1800 goto error;
1801 DISPATCH();
1802 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001803
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001804 TARGET(STORE_SUBSCR) {
1805 PyObject *sub = TOP();
1806 PyObject *container = SECOND();
1807 PyObject *v = THIRD();
1808 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 STACKADJ(-3);
1810 /* v[w] = u */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001811 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 Py_DECREF(container);
1814 Py_DECREF(sub);
1815 if (err != 0)
1816 goto error;
1817 DISPATCH();
1818 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001819
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 TARGET(DELETE_SUBSCR) {
1821 PyObject *sub = TOP();
1822 PyObject *container = SECOND();
1823 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 STACKADJ(-2);
1825 /* del v[w] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001826 err = PyObject_DelItem(container, sub);
1827 Py_DECREF(container);
1828 Py_DECREF(sub);
1829 if (err != 0)
1830 goto error;
1831 DISPATCH();
1832 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001833
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001834 TARGET(PRINT_EXPR) {
1835 PyObject *value = POP();
1836 PyObject *hook = PySys_GetObject("displayhook");
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001837 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001839 PyErr_SetString(PyExc_RuntimeError,
1840 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 Py_DECREF(value);
1842 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 }
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001844 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001845 Py_DECREF(value);
1846 if (res == NULL)
1847 goto error;
1848 Py_DECREF(res);
1849 DISPATCH();
1850 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001851
Thomas Wouters434d0822000-08-24 20:11:32 +00001852#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001854#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001855 TARGET(RAISE_VARARGS) {
1856 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 switch (oparg) {
1858 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 cause = POP(); /* cause */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 exc = POP(); /* exc */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 case 0: /* Fallthrough */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001863 if (do_raise(exc, cause)) {
1864 why = WHY_EXCEPTION;
1865 goto fast_block_end;
1866 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 break;
1868 default:
1869 PyErr_SetString(PyExc_SystemError,
1870 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 break;
1872 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 goto error;
1874 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 TARGET(STORE_LOCALS) {
1877 PyObject *locals = POP();
1878 PyObject *old = f->f_locals;
1879 Py_XDECREF(old);
1880 f->f_locals = locals;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001883
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 retval = POP();
1886 why = WHY_RETURN;
1887 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001888 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001889
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001890 TARGET(YIELD_FROM) {
1891 PyObject *v = POP();
1892 PyObject *reciever = TOP();
1893 int err;
1894 if (PyGen_CheckExact(reciever)) {
1895 retval = _PyGen_Send((PyGenObject *)reciever, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001896 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001897 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 if (v == Py_None)
1899 retval = Py_TYPE(reciever)->tp_iternext(reciever);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001900 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001901 retval = _PyObject_CallMethodId(reciever, &PyId_send, "O", v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001902 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 Py_DECREF(v);
1904 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001905 PyObject *val;
Nick Coghlanc40bc092012-06-17 15:15:49 +10001906 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 if (err < 0)
1908 goto error;
1909 Py_DECREF(reciever);
1910 SET_TOP(val);
1911 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001912 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001913 /* x remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001914 f->f_stacktop = stack_pointer;
1915 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001916 /* and repeat... */
1917 f->f_lasti--;
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001918 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001919 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001920
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001921 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 retval = POP();
1923 f->f_stacktop = stack_pointer;
1924 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001926 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001927
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001928 TARGET(POP_EXCEPT) {
1929 PyTryBlock *b = PyFrame_BlockPop(f);
1930 if (b->b_type != EXCEPT_HANDLER) {
1931 PyErr_SetString(PyExc_SystemError,
1932 "popped block is not an except handler");
1933 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001938
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001939 TARGET(POP_BLOCK) {
1940 PyTryBlock *b = PyFrame_BlockPop(f);
1941 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 TARGET(END_FINALLY) {
1947 PyObject *status = POP();
1948 if (PyLong_Check(status)) {
1949 why = (enum why_code) PyLong_AS_LONG(status);
1950 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 if (why == WHY_RETURN ||
1952 why == WHY_CONTINUE)
1953 retval = POP();
1954 if (why == WHY_SILENCED) {
1955 /* An exception was silenced by 'with', we must
1956 manually unwind the EXCEPT_HANDLER block which was
1957 created when the exception was caught, otherwise
1958 the stack will be in an inconsistent state. */
1959 PyTryBlock *b = PyFrame_BlockPop(f);
1960 assert(b->b_type == EXCEPT_HANDLER);
1961 UNWIND_EXCEPT_HANDLER(b);
1962 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001963 Py_DECREF(status);
1964 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001965 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001966 Py_DECREF(status);
1967 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001968 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001969 else if (PyExceptionClass_Check(status)) {
1970 PyObject *exc = POP();
1971 PyObject *tb = POP();
1972 PyErr_Restore(status, exc, tb);
1973 why = WHY_EXCEPTION;
1974 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001975 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001976 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 PyErr_SetString(PyExc_SystemError,
1978 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 Py_DECREF(status);
1980 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001982 Py_DECREF(status);
1983 DISPATCH();
1984 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001985
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001986 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001987 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001988
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001989 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001990 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1992 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001993 PyErr_SetString(PyExc_NameError,
1994 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001995 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001996 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001998 }
1999 else {
2000 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2001 if (build_class_str == NULL)
2002 break;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2004 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002005 if (PyErr_ExceptionMatches(PyExc_KeyError))
2006 PyErr_SetString(PyExc_NameError,
2007 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002008 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002009 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002011 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002012 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002013 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002014
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002015 TARGET(STORE_NAME) {
2016 PyObject *name = GETITEM(names, oparg);
2017 PyObject *v = POP();
2018 PyObject *ns = f->f_locals;
2019 int err;
2020 if (ns == NULL) {
2021 PyErr_Format(PyExc_SystemError,
2022 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002023 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002024 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 if (PyDict_CheckExact(ns))
2027 err = PyDict_SetItem(ns, name, v);
2028 else
2029 err = PyObject_SetItem(ns, name, v);
2030 Py_DECREF(v);
2031 if (err != 0)
2032 goto error;
2033 DISPATCH();
2034 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002035
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 TARGET(DELETE_NAME) {
2037 PyObject *name = GETITEM(names, oparg);
2038 PyObject *ns = f->f_locals;
2039 int err;
2040 if (ns == NULL) {
2041 PyErr_Format(PyExc_SystemError,
2042 "no locals when deleting %R", name);
2043 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002045 err = PyObject_DelItem(ns, name);
2046 if (err != 0) {
2047 format_exc_check_arg(PyExc_NameError,
2048 NAME_ERROR_MSG,
2049 name);
2050 goto error;
2051 }
2052 DISPATCH();
2053 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002055 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002056 TARGET(UNPACK_SEQUENCE) {
2057 PyObject *seq = POP(), *item, **items;
2058 if (PyTuple_CheckExact(seq) &&
2059 PyTuple_GET_SIZE(seq) == oparg) {
2060 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 item = items[oparg];
2063 Py_INCREF(item);
2064 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002065 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002066 } else if (PyList_CheckExact(seq) &&
2067 PyList_GET_SIZE(seq) == oparg) {
2068 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002069 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002070 item = items[oparg];
2071 Py_INCREF(item);
2072 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002074 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 stack_pointer + oparg)) {
2076 STACKADJ(oparg);
2077 } else {
2078 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 Py_DECREF(seq);
2080 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002082 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002083 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002085
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 TARGET(UNPACK_EX) {
2087 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2088 PyObject *seq = POP();
2089
2090 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2091 stack_pointer + totalargs)) {
2092 stack_pointer += totalargs;
2093 } else {
2094 Py_DECREF(seq);
2095 goto error;
2096 }
2097 Py_DECREF(seq);
2098 DISPATCH();
2099 }
2100
2101 TARGET(STORE_ATTR) {
2102 PyObject *name = GETITEM(names, oparg);
2103 PyObject *owner = TOP();
2104 PyObject *v = SECOND();
2105 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 Py_DECREF(owner);
2110 if (err != 0)
2111 goto error;
2112 DISPATCH();
2113 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002114
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002115 TARGET(DELETE_ATTR) {
2116 PyObject *name = GETITEM(names, oparg);
2117 PyObject *owner = POP();
2118 int err;
2119 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2120 Py_DECREF(owner);
2121 if (err != 0)
2122 goto error;
2123 DISPATCH();
2124 }
2125
2126 TARGET(STORE_GLOBAL) {
2127 PyObject *name = GETITEM(names, oparg);
2128 PyObject *v = POP();
2129 int err;
2130 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002132 if (err != 0)
2133 goto error;
2134 DISPATCH();
2135 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002136
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002137 TARGET(DELETE_GLOBAL) {
2138 PyObject *name = GETITEM(names, oparg);
2139 int err;
2140 err = PyDict_DelItem(f->f_globals, name);
2141 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002142 format_exc_check_arg(
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, name);
2144 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002145 }
2146 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002147 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002148
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 TARGET(LOAD_NAME) {
2150 PyObject *name = GETITEM(names, oparg);
2151 PyObject *locals = f->f_locals;
2152 PyObject *v;
2153 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002155 "no locals when loading %R", name);
2156 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002158 if (PyDict_CheckExact(locals)) {
2159 v = PyDict_GetItem(locals, name);
2160 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 }
2162 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002163 v = PyObject_GetItem(locals, name);
2164 if (v == NULL && PyErr_Occurred()) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002165 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2166 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002167 PyErr_Clear();
2168 }
2169 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 if (v == NULL) {
2171 v = PyDict_GetItem(f->f_globals, name);
2172 Py_XINCREF(v);
2173 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002174 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002175 v = PyDict_GetItem(f->f_builtins, name);
2176 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002177 format_exc_check_arg(
2178 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 NAME_ERROR_MSG, name);
2180 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002183 }
2184 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002185 v = PyObject_GetItem(f->f_builtins, name);
2186 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002187 if (PyErr_ExceptionMatches(PyExc_KeyError))
2188 format_exc_check_arg(
2189 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002190 NAME_ERROR_MSG, name);
2191 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002192 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002193 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002199
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 TARGET(LOAD_GLOBAL) {
2201 PyObject *name = GETITEM(names, oparg);
2202 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002203 if (PyDict_CheckExact(f->f_globals)
2204 && PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002206 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 name);
2208 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002209 if (!PyErr_Occurred())
2210 format_exc_check_arg(PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002211 GLOBAL_NAME_ERROR_MSG, name);
2212 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002213 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002214 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002215 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002216 else {
2217 /* Slow-path if globals or builtins is not a dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 v = PyObject_GetItem(f->f_globals, name);
2219 if (v == NULL) {
2220 v = PyObject_GetItem(f->f_builtins, name);
2221 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002222 if (PyErr_ExceptionMatches(PyExc_KeyError))
2223 format_exc_check_arg(
2224 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 GLOBAL_NAME_ERROR_MSG, name);
2226 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002227 }
2228 }
2229 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002230 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002231 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002233
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002234 TARGET(DELETE_FAST) {
2235 PyObject *v = GETLOCAL(oparg);
2236 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002237 SETLOCAL(oparg, NULL);
2238 DISPATCH();
2239 }
2240 format_exc_check_arg(
2241 PyExc_UnboundLocalError,
2242 UNBOUNDLOCAL_ERROR_MSG,
2243 PyTuple_GetItem(co->co_varnames, oparg)
2244 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 goto error;
2246 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002247
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002248 TARGET(DELETE_DEREF) {
2249 PyObject *cell = freevars[oparg];
2250 if (PyCell_GET(cell) != NULL) {
2251 PyCell_Set(cell, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002252 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002253 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002254 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002255 goto error;
2256 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002257
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 TARGET(LOAD_CLOSURE) {
2259 PyObject *cell = freevars[oparg];
2260 Py_INCREF(cell);
2261 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002263 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002264
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 TARGET(LOAD_DEREF) {
2266 PyObject *cell = freevars[oparg];
2267 PyObject *value = PyCell_GET(cell);
2268 if (value == NULL) {
2269 format_exc_unbound(co, oparg);
2270 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002271 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002272 Py_INCREF(value);
2273 PUSH(value);
2274 DISPATCH();
2275 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002276
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 TARGET(STORE_DEREF) {
2278 PyObject *v = POP();
2279 PyObject *cell = freevars[oparg];
2280 PyCell_Set(cell, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 DISPATCH();
2283 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002284
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 TARGET(BUILD_TUPLE) {
2286 PyObject *tup = PyTuple_New(oparg);
2287 if (tup == NULL)
2288 goto error;
2289 while (--oparg >= 0) {
2290 PyObject *item = POP();
2291 PyTuple_SET_ITEM(tup, oparg, item);
2292 }
2293 PUSH(tup);
2294 DISPATCH();
2295 }
2296
2297 TARGET(BUILD_LIST) {
2298 PyObject *list = PyList_New(oparg);
2299 if (list == NULL)
2300 goto error;
2301 while (--oparg >= 0) {
2302 PyObject *item = POP();
2303 PyList_SET_ITEM(list, oparg, item);
2304 }
2305 PUSH(list);
2306 DISPATCH();
2307 }
2308
2309 TARGET(BUILD_SET) {
2310 PyObject *set = PySet_New(NULL);
2311 int err = 0;
2312 if (set == NULL)
2313 goto error;
2314 while (--oparg >= 0) {
2315 PyObject *item = POP();
2316 if (err == 0)
2317 err = PySet_Add(set, item);
2318 Py_DECREF(item);
2319 }
2320 if (err != 0) {
2321 Py_DECREF(set);
2322 goto error;
2323 }
2324 PUSH(set);
2325 DISPATCH();
2326 }
2327
2328 TARGET(BUILD_MAP) {
2329 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2330 if (map == NULL)
2331 goto error;
2332 PUSH(map);
2333 DISPATCH();
2334 }
2335
2336 TARGET(STORE_MAP) {
2337 PyObject *key = TOP();
2338 PyObject *value = SECOND();
2339 PyObject *map = THIRD();
2340 int err;
2341 STACKADJ(-2);
2342 assert(PyDict_CheckExact(map));
2343 err = PyDict_SetItem(map, key, value);
2344 Py_DECREF(value);
2345 Py_DECREF(key);
2346 if (err != 0)
2347 goto error;
2348 DISPATCH();
2349 }
2350
2351 TARGET(MAP_ADD) {
2352 PyObject *key = TOP();
2353 PyObject *value = SECOND();
2354 PyObject *map;
2355 int err;
2356 STACKADJ(-2);
2357 map = stack_pointer[-oparg]; /* dict */
2358 assert(PyDict_CheckExact(map));
2359 err = PyDict_SetItem(map, key, value); /* v[w] = u */
2360 Py_DECREF(value);
2361 Py_DECREF(key);
2362 if (err != 0)
2363 goto error;
2364 PREDICT(JUMP_ABSOLUTE);
2365 DISPATCH();
2366 }
2367
2368 TARGET(LOAD_ATTR) {
2369 PyObject *name = GETITEM(names, oparg);
2370 PyObject *owner = TOP();
2371 PyObject *res = PyObject_GetAttr(owner, name);
2372 Py_DECREF(owner);
2373 SET_TOP(res);
2374 if (res == NULL)
2375 goto error;
2376 DISPATCH();
2377 }
2378
2379 TARGET(COMPARE_OP) {
2380 PyObject *right = POP();
2381 PyObject *left = TOP();
2382 PyObject *res = cmp_outcome(oparg, left, right);
2383 Py_DECREF(left);
2384 Py_DECREF(right);
2385 SET_TOP(res);
2386 if (res == NULL)
2387 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002388 PREDICT(POP_JUMP_IF_FALSE);
2389 PREDICT(POP_JUMP_IF_TRUE);
2390 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002391 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002392
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002393 TARGET(IMPORT_NAME) {
2394 _Py_IDENTIFIER(__import__);
2395 PyObject *name = GETITEM(names, oparg);
2396 PyObject *func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002397 PyObject *from, *level, *args, *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002398 if (func == NULL) {
2399 PyErr_SetString(PyExc_ImportError,
2400 "__import__ not found");
2401 goto error;
2402 }
2403 Py_INCREF(func);
2404 from = POP();
2405 level = TOP();
2406 if (PyLong_AsLong(level) != -1 || PyErr_Occurred())
2407 args = PyTuple_Pack(5,
2408 name,
2409 f->f_globals,
2410 f->f_locals == NULL ?
2411 Py_None : f->f_locals,
2412 from,
2413 level);
2414 else
2415 args = PyTuple_Pack(4,
2416 name,
2417 f->f_globals,
2418 f->f_locals == NULL ?
2419 Py_None : f->f_locals,
2420 from);
2421 Py_DECREF(level);
2422 Py_DECREF(from);
2423 if (args == NULL) {
2424 Py_DECREF(func);
2425 STACKADJ(-1);
2426 goto error;
2427 }
2428 READ_TIMESTAMP(intr0);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002429 res = PyEval_CallObject(func, args);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 READ_TIMESTAMP(intr1);
2431 Py_DECREF(args);
2432 Py_DECREF(func);
2433 SET_TOP(res);
2434 if (res == NULL)
2435 goto error;
2436 DISPATCH();
2437 }
2438
2439 TARGET(IMPORT_STAR) {
2440 PyObject *from = POP(), *locals;
2441 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002442 PyFrame_FastToLocals(f);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 locals = f->f_locals;
2444 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 PyErr_SetString(PyExc_SystemError,
2446 "no locals found during 'import *'");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002447 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002448 }
2449 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002450 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002451 READ_TIMESTAMP(intr1);
2452 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002453 Py_DECREF(from);
2454 if (err != 0)
2455 goto error;
2456 DISPATCH();
2457 }
Guido van Rossum25831651993-05-19 14:50:45 +00002458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 TARGET(IMPORT_FROM) {
2460 PyObject *name = GETITEM(names, oparg);
2461 PyObject *from = TOP();
2462 PyObject *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002463 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002464 res = import_from(from, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 READ_TIMESTAMP(intr1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002466 PUSH(res);
2467 if (res == NULL)
2468 goto error;
2469 DISPATCH();
2470 }
Thomas Wouters52152252000-08-17 22:55:00 +00002471
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002472 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 JUMPBY(oparg);
2474 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002476
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002477 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 TARGET(POP_JUMP_IF_FALSE) {
2479 PyObject *cond = POP();
2480 int err;
2481 if (cond == Py_True) {
2482 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002483 FAST_DISPATCH();
2484 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 if (cond == Py_False) {
2486 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002487 JUMPTO(oparg);
2488 FAST_DISPATCH();
2489 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002490 err = PyObject_IsTrue(cond);
2491 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002492 if (err > 0)
2493 err = 0;
2494 else if (err == 0)
2495 JUMPTO(oparg);
2496 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002497 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002502 TARGET(POP_JUMP_IF_TRUE) {
2503 PyObject *cond = POP();
2504 int err;
2505 if (cond == Py_False) {
2506 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002507 FAST_DISPATCH();
2508 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002509 if (cond == Py_True) {
2510 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 JUMPTO(oparg);
2512 FAST_DISPATCH();
2513 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 err = PyObject_IsTrue(cond);
2515 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002516 if (err > 0) {
2517 err = 0;
2518 JUMPTO(oparg);
2519 }
2520 else if (err == 0)
2521 ;
2522 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002523 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002524 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002526
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002527 TARGET(JUMP_IF_FALSE_OR_POP) {
2528 PyObject *cond = TOP();
2529 int err;
2530 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002531 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002532 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002533 FAST_DISPATCH();
2534 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002535 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 JUMPTO(oparg);
2537 FAST_DISPATCH();
2538 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002539 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 if (err > 0) {
2541 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002542 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 err = 0;
2544 }
2545 else if (err == 0)
2546 JUMPTO(oparg);
2547 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002548 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002551
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002552 TARGET(JUMP_IF_TRUE_OR_POP) {
2553 PyObject *cond = TOP();
2554 int err;
2555 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002556 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002557 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002558 FAST_DISPATCH();
2559 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002560 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 JUMPTO(oparg);
2562 FAST_DISPATCH();
2563 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002564 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 if (err > 0) {
2566 err = 0;
2567 JUMPTO(oparg);
2568 }
2569 else if (err == 0) {
2570 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002571 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 }
2573 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002574 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002575 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002576 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002578 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002580 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002581#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 /* Enabling this path speeds-up all while and for-loops by bypassing
2583 the per-loop checks for signals. By default, this should be turned-off
2584 because it prevents detection of a control-break in tight loops like
2585 "while 1: pass". Compile with this option turned-on when you need
2586 the speed-up and do not need break checking inside tight loops (ones
2587 that contain only instructions ending with FAST_DISPATCH).
2588 */
2589 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002590#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002591 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002592#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002594
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 PyObject *iterable = TOP();
2598 PyObject *iter = PyObject_GetIter(iterable);
2599 Py_DECREF(iterable);
2600 SET_TOP(iter);
2601 if (iter == NULL)
2602 goto error;
2603 PREDICT(FOR_ITER);
2604 DISPATCH();
2605 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002607 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002608 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002609 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002610 PyObject *iter = TOP();
2611 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2612 if (next != NULL) {
2613 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002614 PREDICT(STORE_FAST);
2615 PREDICT(UNPACK_SEQUENCE);
2616 DISPATCH();
2617 }
2618 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002619 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2620 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 PyErr_Clear();
2622 }
2623 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002624 STACKADJ(-1);
2625 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002626 JUMPBY(oparg);
2627 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002628 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002629
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002630 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 why = WHY_BREAK;
2632 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002633 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002634
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002635 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 if (retval == NULL)
2638 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002639 why = WHY_CONTINUE;
2640 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002641 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2644 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2645 TARGET(SETUP_FINALLY)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 _setup_finally: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 /* NOTE: If you add any new block-setup opcodes that
2648 are not try/except/finally handlers, you may need
2649 to update the PyGen_NeedsFinalizing() function.
2650 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002652 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2653 STACK_LEVEL());
2654 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002656
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002657 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002658 _Py_IDENTIFIER(__exit__);
2659 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002660 PyObject *mgr = TOP();
2661 PyObject *exit = special_lookup(mgr, &PyId___exit__), *enter;
2662 PyObject *res;
2663 if (exit == NULL)
2664 goto error;
2665 SET_TOP(exit);
2666 enter = special_lookup(mgr, &PyId___enter__);
2667 Py_DECREF(mgr);
2668 if (enter == NULL)
2669 goto error;
2670 res = PyObject_CallFunctionObjArgs(enter, NULL);
2671 Py_DECREF(enter);
2672 if (res == NULL)
2673 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 /* Setup the finally block before pushing the result
2675 of __enter__ on the stack. */
2676 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2677 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002678
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 DISPATCH();
2681 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002682
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002683 TARGET(WITH_CLEANUP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 /* At the top of the stack are 1-3 values indicating
2685 how/why we entered the finally clause:
2686 - TOP = None
2687 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2688 - TOP = WHY_*; no retval below it
2689 - (TOP, SECOND, THIRD) = exc_info()
2690 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2691 Below them is EXIT, the context.__exit__ bound method.
2692 In the last case, we must call
2693 EXIT(TOP, SECOND, THIRD)
2694 otherwise we must call
2695 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 In the first two cases, we remove EXIT from the
2698 stack, leaving the rest in the same order. In the
2699 third case, we shift the bottom 3 values of the
2700 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 In addition, if the stack represents an exception,
2703 *and* the function call returns a 'true' value, we
2704 push WHY_SILENCED onto the stack. END_FINALLY will
2705 then not re-raise the exception. (But non-local
2706 gotos should still be resumed.)
2707 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 PyObject *exit_func;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 PyObject *exc = TOP(), *val = Py_None, *tb = Py_None, *res;
2711 int err;
2712 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 (void)POP();
2714 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 else if (PyLong_Check(exc)) {
2718 STACKADJ(-1);
2719 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002720 case WHY_RETURN:
2721 case WHY_CONTINUE:
2722 /* Retval in TOP. */
2723 exit_func = SECOND();
2724 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002725 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002726 break;
2727 default:
2728 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002730 break;
2731 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002733 }
2734 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002735 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 val = SECOND();
2738 tb = THIRD();
2739 tp2 = FOURTH();
2740 exc2 = PEEK(5);
2741 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002742 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 SET_VALUE(7, tb2);
2744 SET_VALUE(6, exc2);
2745 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002746 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2747 SET_FOURTH(NULL);
2748 /* We just shifted the stack down, so we have
2749 to tell the except handler block that the
2750 values are lower than it expects. */
2751 block = &f->f_blockstack[f->f_iblock - 1];
2752 assert(block->b_type == EXCEPT_HANDLER);
2753 block->b_level--;
2754 }
2755 /* XXX Not the fastest way to call it... */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002756 res = PyObject_CallFunctionObjArgs(exit_func, exc, val, tb, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 if (res == NULL)
2759 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002760
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 if (exc != Py_None)
2762 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 else
2764 err = 0;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 Py_DECREF(res);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002767 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 else if (err > 0) {
2770 err = 0;
2771 /* There was an exception and a True return */
2772 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2773 }
2774 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002777
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 TARGET(CALL_FUNCTION) {
2779 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 PCALL(PCALL_ALL);
2781 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002782#ifdef WITH_TSC
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002783 res = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002784#else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 res = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002786#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 PUSH(res);
2789 if (res == NULL)
2790 goto error;
2791 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002794 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2795 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2796 TARGET(CALL_FUNCTION_VAR_KW)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002797 _call_function_var_kw: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 int na = oparg & 0xff;
2799 int nk = (oparg>>8) & 0xff;
2800 int flags = (opcode - CALL_FUNCTION) & 3;
2801 int n = na + 2 * nk;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 PyObject **pfunc, *func, **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 PCALL(PCALL_ALL);
2804 if (flags & CALL_FLAG_VAR)
2805 n++;
2806 if (flags & CALL_FLAG_KW)
2807 n++;
2808 pfunc = stack_pointer - n - 1;
2809 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002812 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002813 PyObject *self = PyMethod_GET_SELF(func);
2814 Py_INCREF(self);
2815 func = PyMethod_GET_FUNCTION(func);
2816 Py_INCREF(func);
2817 Py_DECREF(*pfunc);
2818 *pfunc = self;
2819 na++;
Brett Cannonb94767f2011-02-22 20:15:44 +00002820 /* n++; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 } else
2822 Py_INCREF(func);
2823 sp = stack_pointer;
2824 READ_TIMESTAMP(intr0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002825 res = ext_do_call(func, &sp, flags, na, nk);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 READ_TIMESTAMP(intr1);
2827 stack_pointer = sp;
2828 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 while (stack_pointer > pfunc) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002831 PyObject *o = POP();
2832 Py_DECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002834 PUSH(res);
2835 if (res == NULL)
2836 goto error;
2837 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002840 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2841 TARGET(MAKE_FUNCTION)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002842 _make_function: {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 int posdefaults = oparg & 0xff;
2844 int kwdefaults = (oparg>>8) & 0xff;
2845 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002846
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002847 PyObject *qualname = POP(); /* qualname */
2848 PyObject *code = POP(); /* code object */
2849 PyObject *func = PyFunction_NewWithQualName(code, f->f_globals, qualname);
2850 Py_DECREF(code);
2851 Py_DECREF(qualname);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002852
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 if (func == NULL)
2854 goto error;
2855
2856 if (opcode == MAKE_CLOSURE) {
2857 PyObject *closure = POP();
2858 if (PyFunction_SetClosure(func, closure) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 /* Can't happen unless bytecode is corrupt. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002860 Py_DECREF(func);
2861 Py_DECREF(closure);
2862 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 Py_DECREF(closure);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002866
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002867 if (num_annotations > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 Py_ssize_t name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 PyObject *names = POP(); /* names of args with annotations */
2870 PyObject *anns = PyDict_New();
2871 if (anns == NULL) {
2872 Py_DECREF(func);
2873 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 name_ix = PyTuple_Size(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 assert(num_annotations == name_ix+1);
2877 while (name_ix > 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002878 PyObject *name, *value;
2879 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 --name_ix;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002881 name = PyTuple_GET_ITEM(names, name_ix);
2882 value = POP();
2883 err = PyDict_SetItem(anns, name, value);
2884 Py_DECREF(value);
2885 if (err != 0) {
2886 Py_DECREF(anns);
2887 Py_DECREF(func);
2888 goto error;
2889 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002891
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 if (PyFunction_SetAnnotations(func, anns) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 /* Can't happen unless
2894 PyFunction_SetAnnotations changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 Py_DECREF(anns);
2896 Py_DECREF(func);
2897 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002898 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002899 Py_DECREF(anns);
2900 Py_DECREF(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* XXX Maybe this should be a separate opcode? */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002904 if (posdefaults > 0) {
2905 PyObject *defs = PyTuple_New(posdefaults);
2906 if (defs == NULL) {
2907 Py_DECREF(func);
2908 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 while (--posdefaults >= 0)
2911 PyTuple_SET_ITEM(defs, posdefaults, POP());
2912 if (PyFunction_SetDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 /* Can't happen unless
2914 PyFunction_SetDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002915 Py_DECREF(defs);
2916 Py_DECREF(func);
2917 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002919 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002921 if (kwdefaults > 0) {
2922 PyObject *defs = PyDict_New();
2923 if (defs == NULL) {
2924 Py_DECREF(func);
2925 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 }
2927 while (--kwdefaults >= 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 PyObject *v = POP(); /* default value */
2929 PyObject *key = POP(); /* kw only arg name */
2930 int err = PyDict_SetItem(defs, key, v);
2931 Py_DECREF(v);
2932 Py_DECREF(key);
2933 if (err != 0) {
2934 Py_DECREF(defs);
2935 Py_DECREF(func);
2936 goto error;
2937 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002938 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002939 if (PyFunction_SetKwDefaults(func, defs) != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 /* Can't happen unless
2941 PyFunction_SetKwDefaults changes. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002942 Py_DECREF(func);
2943 Py_DECREF(defs);
2944 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002945 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002946 Py_DECREF(defs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 PUSH(func);
2949 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002950 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002951
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 TARGET(BUILD_SLICE) {
2953 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002954 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002955 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002957 step = NULL;
2958 stop = POP();
2959 start = TOP();
2960 slice = PySlice_New(start, stop, step);
2961 Py_DECREF(start);
2962 Py_DECREF(stop);
2963 Py_XDECREF(step);
2964 SET_TOP(slice);
2965 if (slice == NULL)
2966 goto error;
2967 DISPATCH();
2968 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002969
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002970 TARGET(EXTENDED_ARG) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002971 opcode = NEXTOP();
2972 oparg = oparg<<16 | NEXTARG();
2973 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002975
Antoine Pitrou042b1282010-08-13 21:15:58 +00002976#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002978#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002979 default:
2980 fprintf(stderr,
2981 "XXX lineno: %d, opcode: %d\n",
2982 PyFrame_GetLineNumber(f),
2983 opcode);
2984 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002986
2987#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002989#endif
2990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002991 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002992
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 /* This should never be reached. Every opcode should end with DISPATCH()
2994 or goto error. */
2995 assert(0);
Guido van Rossumac7be682001-01-17 15:42:30 +00002996
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002997error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002999
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003000 assert(why == WHY_NOT);
3001 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003002
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003003 /* Double-check exception status. */
3004 if (!PyErr_Occurred())
3005 PyErr_SetString(PyExc_SystemError,
3006 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00003007
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 /* Log traceback info. */
3009 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003010
Benjamin Peterson51f46162013-01-23 08:38:47 -05003011 if (tstate->c_tracefunc != NULL)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003013
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003014fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 assert(why != WHY_NOT);
3016
3017 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003018 while (why != WHY_NOT && f->f_iblock > 0) {
3019 /* Peek at the current block. */
3020 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003021
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003022 assert(why != WHY_YIELD);
3023 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3024 why = WHY_NOT;
3025 JUMPTO(PyLong_AS_LONG(retval));
3026 Py_DECREF(retval);
3027 break;
3028 }
3029 /* Now we have to pop the block. */
3030 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 if (b->b_type == EXCEPT_HANDLER) {
3033 UNWIND_EXCEPT_HANDLER(b);
3034 continue;
3035 }
3036 UNWIND_BLOCK(b);
3037 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3038 why = WHY_NOT;
3039 JUMPTO(b->b_handler);
3040 break;
3041 }
3042 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3043 || b->b_type == SETUP_FINALLY)) {
3044 PyObject *exc, *val, *tb;
3045 int handler = b->b_handler;
3046 /* Beware, this invalidates all b->b_* fields */
3047 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3048 PUSH(tstate->exc_traceback);
3049 PUSH(tstate->exc_value);
3050 if (tstate->exc_type != NULL) {
3051 PUSH(tstate->exc_type);
3052 }
3053 else {
3054 Py_INCREF(Py_None);
3055 PUSH(Py_None);
3056 }
3057 PyErr_Fetch(&exc, &val, &tb);
3058 /* Make the raw exception data
3059 available to the handler,
3060 so a program can emulate the
3061 Python main loop. */
3062 PyErr_NormalizeException(
3063 &exc, &val, &tb);
3064 PyException_SetTraceback(val, tb);
3065 Py_INCREF(exc);
3066 tstate->exc_type = exc;
3067 Py_INCREF(val);
3068 tstate->exc_value = val;
3069 tstate->exc_traceback = tb;
3070 if (tb == NULL)
3071 tb = Py_None;
3072 Py_INCREF(tb);
3073 PUSH(tb);
3074 PUSH(val);
3075 PUSH(exc);
3076 why = WHY_NOT;
3077 JUMPTO(handler);
3078 break;
3079 }
3080 if (b->b_type == SETUP_FINALLY) {
3081 if (why & (WHY_RETURN | WHY_CONTINUE))
3082 PUSH(retval);
3083 PUSH(PyLong_FromLong((long)why));
3084 why = WHY_NOT;
3085 JUMPTO(b->b_handler);
3086 break;
3087 }
3088 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 if (why != WHY_NOT)
3093 break;
3094 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003098 assert(why != WHY_YIELD);
3099 /* Pop remaining stack entries. */
3100 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003101 PyObject *o = POP();
3102 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 if (why != WHY_RETURN)
3106 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003107
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003108fast_yield:
Benjamin Petersonac913412011-07-03 16:25:11 -05003109 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN)) {
3110 /* The purpose of this block is to put aside the generator's exception
3111 state and restore that of the calling frame. If the current
3112 exception state is from the caller, we clear the exception values
3113 on the generator frame, so they are not swapped back in latter. The
3114 origin of the current exception state is determined by checking for
3115 except handler blocks, which we must be in iff a new exception
3116 state came into existence in this frame. (An uncaught exception
3117 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3118 int i;
3119 for (i = 0; i < f->f_iblock; i++)
3120 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER)
3121 break;
3122 if (i == f->f_iblock)
3123 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003124 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003125 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003126 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003127 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003128
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003129 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003130 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131 if (why == WHY_RETURN || why == WHY_YIELD) {
3132 if (call_trace(tstate->c_tracefunc,
3133 tstate->c_traceobj, f,
3134 PyTrace_RETURN, retval)) {
3135 Py_XDECREF(retval);
3136 retval = NULL;
3137 why = WHY_EXCEPTION;
3138 }
3139 }
3140 else if (why == WHY_EXCEPTION) {
3141 call_trace_protected(tstate->c_tracefunc,
3142 tstate->c_traceobj, f,
3143 PyTrace_RETURN, NULL);
3144 }
3145 }
3146 if (tstate->c_profilefunc) {
3147 if (why == WHY_EXCEPTION)
3148 call_trace_protected(tstate->c_profilefunc,
3149 tstate->c_profileobj, f,
3150 PyTrace_RETURN, NULL);
3151 else if (call_trace(tstate->c_profilefunc,
3152 tstate->c_profileobj, f,
3153 PyTrace_RETURN, retval)) {
3154 Py_XDECREF(retval);
3155 retval = NULL;
Brett Cannonb94767f2011-02-22 20:15:44 +00003156 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003157 }
3158 }
3159 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003160
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003161 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003162exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 Py_LeaveRecursiveCall();
3164 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003166 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003167}
3168
Benjamin Petersonb204a422011-06-05 22:04:07 -05003169static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003170format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3171{
3172 int err;
3173 Py_ssize_t len = PyList_GET_SIZE(names);
3174 PyObject *name_str, *comma, *tail, *tmp;
3175
3176 assert(PyList_CheckExact(names));
3177 assert(len >= 1);
3178 /* Deal with the joys of natural language. */
3179 switch (len) {
3180 case 1:
3181 name_str = PyList_GET_ITEM(names, 0);
3182 Py_INCREF(name_str);
3183 break;
3184 case 2:
3185 name_str = PyUnicode_FromFormat("%U and %U",
3186 PyList_GET_ITEM(names, len - 2),
3187 PyList_GET_ITEM(names, len - 1));
3188 break;
3189 default:
3190 tail = PyUnicode_FromFormat(", %U, and %U",
3191 PyList_GET_ITEM(names, len - 2),
3192 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003193 if (tail == NULL)
3194 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003195 /* Chop off the last two objects in the list. This shouldn't actually
3196 fail, but we can't be too careful. */
3197 err = PyList_SetSlice(names, len - 2, len, NULL);
3198 if (err == -1) {
3199 Py_DECREF(tail);
3200 return;
3201 }
3202 /* Stitch everything up into a nice comma-separated list. */
3203 comma = PyUnicode_FromString(", ");
3204 if (comma == NULL) {
3205 Py_DECREF(tail);
3206 return;
3207 }
3208 tmp = PyUnicode_Join(comma, names);
3209 Py_DECREF(comma);
3210 if (tmp == NULL) {
3211 Py_DECREF(tail);
3212 return;
3213 }
3214 name_str = PyUnicode_Concat(tmp, tail);
3215 Py_DECREF(tmp);
3216 Py_DECREF(tail);
3217 break;
3218 }
3219 if (name_str == NULL)
3220 return;
3221 PyErr_Format(PyExc_TypeError,
3222 "%U() missing %i required %s argument%s: %U",
3223 co->co_name,
3224 len,
3225 kind,
3226 len == 1 ? "" : "s",
3227 name_str);
3228 Py_DECREF(name_str);
3229}
3230
3231static void
3232missing_arguments(PyCodeObject *co, int missing, int defcount,
3233 PyObject **fastlocals)
3234{
3235 int i, j = 0;
3236 int start, end;
3237 int positional = defcount != -1;
3238 const char *kind = positional ? "positional" : "keyword-only";
3239 PyObject *missing_names;
3240
3241 /* Compute the names of the arguments that are missing. */
3242 missing_names = PyList_New(missing);
3243 if (missing_names == NULL)
3244 return;
3245 if (positional) {
3246 start = 0;
3247 end = co->co_argcount - defcount;
3248 }
3249 else {
3250 start = co->co_argcount;
3251 end = start + co->co_kwonlyargcount;
3252 }
3253 for (i = start; i < end; i++) {
3254 if (GETLOCAL(i) == NULL) {
3255 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3256 PyObject *name = PyObject_Repr(raw);
3257 if (name == NULL) {
3258 Py_DECREF(missing_names);
3259 return;
3260 }
3261 PyList_SET_ITEM(missing_names, j++, name);
3262 }
3263 }
3264 assert(j == missing);
3265 format_missing(kind, co, missing_names);
3266 Py_DECREF(missing_names);
3267}
3268
3269static void
3270too_many_positional(PyCodeObject *co, int given, int defcount, PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003271{
3272 int plural;
3273 int kwonly_given = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003274 int i;
3275 PyObject *sig, *kwonly_sig;
3276
Benjamin Petersone109c702011-06-24 09:37:26 -05003277 assert((co->co_flags & CO_VARARGS) == 0);
3278 /* Count missing keyword-only args. */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003279 for (i = co->co_argcount; i < co->co_argcount + co->co_kwonlyargcount; i++)
Benjamin Petersone109c702011-06-24 09:37:26 -05003280 if (GETLOCAL(i) != NULL)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003281 kwonly_given++;
Benjamin Petersone109c702011-06-24 09:37:26 -05003282 if (defcount) {
3283 int atleast = co->co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003284 plural = 1;
3285 sig = PyUnicode_FromFormat("from %d to %d", atleast, co->co_argcount);
3286 }
3287 else {
3288 plural = co->co_argcount != 1;
3289 sig = PyUnicode_FromFormat("%d", co->co_argcount);
3290 }
3291 if (sig == NULL)
3292 return;
3293 if (kwonly_given) {
3294 const char *format = " positional argument%s (and %d keyword-only argument%s)";
3295 kwonly_sig = PyUnicode_FromFormat(format, given != 1 ? "s" : "", kwonly_given,
3296 kwonly_given != 1 ? "s" : "");
3297 if (kwonly_sig == NULL) {
3298 Py_DECREF(sig);
3299 return;
3300 }
3301 }
3302 else {
3303 /* This will not fail. */
3304 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003305 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003306 }
3307 PyErr_Format(PyExc_TypeError,
3308 "%U() takes %U positional argument%s but %d%U %s given",
3309 co->co_name,
3310 sig,
3311 plural ? "s" : "",
3312 given,
3313 kwonly_sig,
3314 given == 1 && !kwonly_given ? "was" : "were");
3315 Py_DECREF(sig);
3316 Py_DECREF(kwonly_sig);
3317}
3318
Guido van Rossumc2e20742006-02-27 22:32:47 +00003319/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003320 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003321 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003322
Tim Peters6d6c1a32001-08-02 04:15:00 +00003323PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003324PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003325 PyObject **args, int argcount, PyObject **kws, int kwcount,
3326 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003327{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003328 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 register PyFrameObject *f;
3330 register PyObject *retval = NULL;
3331 register PyObject **fastlocals, **freevars;
3332 PyThreadState *tstate = PyThreadState_GET();
3333 PyObject *x, *u;
3334 int total_args = co->co_argcount + co->co_kwonlyargcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003335 int i;
3336 int n = argcount;
3337 PyObject *kwdict = NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003339 if (globals == NULL) {
3340 PyErr_SetString(PyExc_SystemError,
3341 "PyEval_EvalCodeEx: NULL globals");
3342 return NULL;
3343 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003344
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 assert(tstate != NULL);
3346 assert(globals != NULL);
3347 f = PyFrame_New(tstate, co, globals, locals);
3348 if (f == NULL)
3349 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003351 fastlocals = f->f_localsplus;
3352 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003353
Benjamin Petersonb204a422011-06-05 22:04:07 -05003354 /* Parse arguments. */
3355 if (co->co_flags & CO_VARKEYWORDS) {
3356 kwdict = PyDict_New();
3357 if (kwdict == NULL)
3358 goto fail;
3359 i = total_args;
3360 if (co->co_flags & CO_VARARGS)
3361 i++;
3362 SETLOCAL(i, kwdict);
3363 }
3364 if (argcount > co->co_argcount)
3365 n = co->co_argcount;
3366 for (i = 0; i < n; i++) {
3367 x = args[i];
3368 Py_INCREF(x);
3369 SETLOCAL(i, x);
3370 }
3371 if (co->co_flags & CO_VARARGS) {
3372 u = PyTuple_New(argcount - n);
3373 if (u == NULL)
3374 goto fail;
3375 SETLOCAL(total_args, u);
3376 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 x = args[i];
3378 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003379 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003380 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003381 }
3382 for (i = 0; i < kwcount; i++) {
3383 PyObject **co_varnames;
3384 PyObject *keyword = kws[2*i];
3385 PyObject *value = kws[2*i + 1];
3386 int j;
3387 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3388 PyErr_Format(PyExc_TypeError,
3389 "%U() keywords must be strings",
3390 co->co_name);
3391 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003393 /* Speed hack: do raw pointer compares. As names are
3394 normally interned this should almost always hit. */
3395 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3396 for (j = 0; j < total_args; j++) {
3397 PyObject *nm = co_varnames[j];
3398 if (nm == keyword)
3399 goto kw_found;
3400 }
3401 /* Slow fallback, just in case */
3402 for (j = 0; j < total_args; j++) {
3403 PyObject *nm = co_varnames[j];
3404 int cmp = PyObject_RichCompareBool(
3405 keyword, nm, Py_EQ);
3406 if (cmp > 0)
3407 goto kw_found;
3408 else if (cmp < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003410 }
3411 if (j >= total_args && kwdict == NULL) {
3412 PyErr_Format(PyExc_TypeError,
3413 "%U() got an unexpected "
3414 "keyword argument '%S'",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 co->co_name,
3416 keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003417 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003419 PyDict_SetItem(kwdict, keyword, value);
3420 continue;
3421 kw_found:
3422 if (GETLOCAL(j) != NULL) {
3423 PyErr_Format(PyExc_TypeError,
3424 "%U() got multiple "
3425 "values for argument '%S'",
3426 co->co_name,
3427 keyword);
3428 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003430 Py_INCREF(value);
3431 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003433 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003434 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003435 goto fail;
3436 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003437 if (argcount < co->co_argcount) {
3438 int m = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05003439 int missing = 0;
3440 for (i = argcount; i < m; i++)
3441 if (GETLOCAL(i) == NULL)
3442 missing++;
3443 if (missing) {
3444 missing_arguments(co, missing, defcount, fastlocals);
3445 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003446 }
3447 if (n > m)
3448 i = n - m;
3449 else
3450 i = 0;
3451 for (; i < defcount; i++) {
3452 if (GETLOCAL(m+i) == NULL) {
3453 PyObject *def = defs[i];
3454 Py_INCREF(def);
3455 SETLOCAL(m+i, def);
3456 }
3457 }
3458 }
3459 if (co->co_kwonlyargcount > 0) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003460 int missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003461 for (i = co->co_argcount; i < total_args; i++) {
3462 PyObject *name;
3463 if (GETLOCAL(i) != NULL)
3464 continue;
3465 name = PyTuple_GET_ITEM(co->co_varnames, i);
3466 if (kwdefs != NULL) {
3467 PyObject *def = PyDict_GetItem(kwdefs, name);
3468 if (def) {
3469 Py_INCREF(def);
3470 SETLOCAL(i, def);
3471 continue;
3472 }
3473 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003474 missing++;
3475 }
3476 if (missing) {
3477 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003478 goto fail;
3479 }
3480 }
3481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003483 vars into frame. */
3484 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 PyObject *c;
Benjamin Peterson90037602011-06-25 22:54:45 -05003486 int arg;
3487 /* Possibly account for the cell variable being an argument. */
3488 if (co->co_cell2arg != NULL &&
3489 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG)
3490 c = PyCell_New(GETLOCAL(arg));
3491 else
3492 c = PyCell_New(NULL);
3493 if (c == NULL)
3494 goto fail;
3495 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 }
Benjamin Peterson90037602011-06-25 22:54:45 -05003497 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3498 PyObject *o = PyTuple_GET_ITEM(closure, i);
3499 Py_INCREF(o);
3500 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003501 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003503 if (co->co_flags & CO_GENERATOR) {
3504 /* Don't need to keep the reference to f_back, it will be set
3505 * when the generator is resumed. */
3506 Py_XDECREF(f->f_back);
3507 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003510
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 /* Create a new generator that owns the ready to run frame
3512 * and return that as the value. */
3513 return PyGen_New(f);
3514 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003517
Thomas Woutersce272b62007-09-19 21:19:28 +00003518fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 /* decref'ing the frame can cause __del__ methods to get invoked,
3521 which can call back into Python. While we're done with the
3522 current Python frame (f), the associated C stack is still in use,
3523 so recursion_depth must be boosted for the duration.
3524 */
3525 assert(tstate != NULL);
3526 ++tstate->recursion_depth;
3527 Py_DECREF(f);
3528 --tstate->recursion_depth;
3529 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003530}
3531
3532
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003533static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003534special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003536 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003537 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003538 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003539 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003540 return NULL;
3541 }
3542 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003543}
3544
3545
Benjamin Peterson87880242011-07-03 16:48:31 -05003546/* These 3 functions deal with the exception state of generators. */
3547
3548static void
3549save_exc_state(PyThreadState *tstate, PyFrameObject *f)
3550{
3551 PyObject *type, *value, *traceback;
3552 Py_XINCREF(tstate->exc_type);
3553 Py_XINCREF(tstate->exc_value);
3554 Py_XINCREF(tstate->exc_traceback);
3555 type = f->f_exc_type;
3556 value = f->f_exc_value;
3557 traceback = f->f_exc_traceback;
3558 f->f_exc_type = tstate->exc_type;
3559 f->f_exc_value = tstate->exc_value;
3560 f->f_exc_traceback = tstate->exc_traceback;
3561 Py_XDECREF(type);
3562 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02003563 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05003564}
3565
3566static void
3567swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
3568{
3569 PyObject *tmp;
3570 tmp = tstate->exc_type;
3571 tstate->exc_type = f->f_exc_type;
3572 f->f_exc_type = tmp;
3573 tmp = tstate->exc_value;
3574 tstate->exc_value = f->f_exc_value;
3575 f->f_exc_value = tmp;
3576 tmp = tstate->exc_traceback;
3577 tstate->exc_traceback = f->f_exc_traceback;
3578 f->f_exc_traceback = tmp;
3579}
3580
3581static void
3582restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
3583{
3584 PyObject *type, *value, *tb;
3585 type = tstate->exc_type;
3586 value = tstate->exc_value;
3587 tb = tstate->exc_traceback;
3588 tstate->exc_type = f->f_exc_type;
3589 tstate->exc_value = f->f_exc_value;
3590 tstate->exc_traceback = f->f_exc_traceback;
3591 f->f_exc_type = NULL;
3592 f->f_exc_value = NULL;
3593 f->f_exc_traceback = NULL;
3594 Py_XDECREF(type);
3595 Py_XDECREF(value);
3596 Py_XDECREF(tb);
3597}
3598
3599
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003600/* Logic for the raise statement (too complicated for inlining).
3601 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602static int
Collin Winter828f04a2007-08-31 00:04:24 +00003603do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 if (exc == NULL) {
3608 /* Reraise */
3609 PyThreadState *tstate = PyThreadState_GET();
3610 PyObject *tb;
3611 type = tstate->exc_type;
3612 value = tstate->exc_value;
3613 tb = tstate->exc_traceback;
3614 if (type == Py_None) {
3615 PyErr_SetString(PyExc_RuntimeError,
3616 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003617 return 0;
3618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 Py_XINCREF(type);
3620 Py_XINCREF(value);
3621 Py_XINCREF(tb);
3622 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003623 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003624 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003625
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 /* We support the following forms of raise:
3627 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003628 raise <instance>
3629 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 if (PyExceptionClass_Check(exc)) {
3632 type = exc;
3633 value = PyObject_CallObject(exc, NULL);
3634 if (value == NULL)
3635 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05003636 if (!PyExceptionInstance_Check(value)) {
3637 PyErr_Format(PyExc_TypeError,
3638 "calling %R should have returned an instance of "
3639 "BaseException, not %R",
3640 type, Py_TYPE(value));
3641 goto raise_error;
3642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003643 }
3644 else if (PyExceptionInstance_Check(exc)) {
3645 value = exc;
3646 type = PyExceptionInstance_Class(exc);
3647 Py_INCREF(type);
3648 }
3649 else {
3650 /* Not something you can raise. You get an exception
3651 anyway, just not what you specified :-) */
3652 Py_DECREF(exc);
3653 PyErr_SetString(PyExc_TypeError,
3654 "exceptions must derive from BaseException");
3655 goto raise_error;
3656 }
Collin Winter828f04a2007-08-31 00:04:24 +00003657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003658 if (cause) {
3659 PyObject *fixed_cause;
3660 if (PyExceptionClass_Check(cause)) {
3661 fixed_cause = PyObject_CallObject(cause, NULL);
3662 if (fixed_cause == NULL)
3663 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003664 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003665 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003666 else if (PyExceptionInstance_Check(cause)) {
3667 fixed_cause = cause;
3668 }
3669 else if (cause == Py_None) {
3670 Py_DECREF(cause);
3671 fixed_cause = NULL;
3672 }
3673 else {
3674 PyErr_SetString(PyExc_TypeError,
3675 "exception causes must derive from "
3676 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 goto raise_error;
3678 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07003679 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 }
Collin Winter828f04a2007-08-31 00:04:24 +00003681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 PyErr_SetObject(type, value);
3683 /* PyErr_SetObject incref's its arguments */
3684 Py_XDECREF(value);
3685 Py_XDECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003686 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00003687
3688raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 Py_XDECREF(value);
3690 Py_XDECREF(type);
3691 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003692 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003693}
3694
Tim Petersd6d010b2001-06-21 02:49:55 +00003695/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003696 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003697
Guido van Rossum0368b722007-05-11 16:50:42 +00003698 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3699 with a variable target.
3700*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003701
Barry Warsawe42b18f1997-08-25 22:13:04 +00003702static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003703unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 int i = 0, j = 0;
3706 Py_ssize_t ll = 0;
3707 PyObject *it; /* iter(v) */
3708 PyObject *w;
3709 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 it = PyObject_GetIter(v);
3714 if (it == NULL)
3715 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003717 for (; i < argcnt; i++) {
3718 w = PyIter_Next(it);
3719 if (w == NULL) {
3720 /* Iterator done, via error or exhaustion. */
3721 if (!PyErr_Occurred()) {
3722 PyErr_Format(PyExc_ValueError,
3723 "need more than %d value%s to unpack",
3724 i, i == 1 ? "" : "s");
3725 }
3726 goto Error;
3727 }
3728 *--sp = w;
3729 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 if (argcntafter == -1) {
3732 /* We better have exhausted the iterator now. */
3733 w = PyIter_Next(it);
3734 if (w == NULL) {
3735 if (PyErr_Occurred())
3736 goto Error;
3737 Py_DECREF(it);
3738 return 1;
3739 }
3740 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003741 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3742 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003743 goto Error;
3744 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 l = PySequence_List(it);
3747 if (l == NULL)
3748 goto Error;
3749 *--sp = l;
3750 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 ll = PyList_GET_SIZE(l);
3753 if (ll < argcntafter) {
3754 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3755 argcnt + ll);
3756 goto Error;
3757 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003759 /* Pop the "after-variable" args off the list. */
3760 for (j = argcntafter; j > 0; j--, i++) {
3761 *--sp = PyList_GET_ITEM(l, ll - j);
3762 }
3763 /* Resize the list. */
3764 Py_SIZE(l) = ll - argcntafter;
3765 Py_DECREF(it);
3766 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003767
Tim Petersd6d010b2001-06-21 02:49:55 +00003768Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 for (; i > 0; i--, sp++)
3770 Py_DECREF(*sp);
3771 Py_XDECREF(it);
3772 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003773}
3774
3775
Guido van Rossum96a42c81992-01-12 02:29:51 +00003776#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003777static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003778prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003779{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003780 printf("%s ", str);
3781 if (PyObject_Print(v, stdout, 0) != 0)
3782 PyErr_Clear(); /* Don't know what else to do */
3783 printf("\n");
3784 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003785}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003786#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003787
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003788static void
Fred Drake5755ce62001-06-27 19:19:46 +00003789call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 PyObject *type, *value, *traceback, *arg;
3792 int err;
3793 PyErr_Fetch(&type, &value, &traceback);
3794 if (value == NULL) {
3795 value = Py_None;
3796 Py_INCREF(value);
3797 }
3798 arg = PyTuple_Pack(3, type, value, traceback);
3799 if (arg == NULL) {
3800 PyErr_Restore(type, value, traceback);
3801 return;
3802 }
3803 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3804 Py_DECREF(arg);
3805 if (err == 0)
3806 PyErr_Restore(type, value, traceback);
3807 else {
3808 Py_XDECREF(type);
3809 Py_XDECREF(value);
3810 Py_XDECREF(traceback);
3811 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003812}
3813
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003814static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003815call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 PyObject *type, *value, *traceback;
3819 int err;
3820 PyErr_Fetch(&type, &value, &traceback);
3821 err = call_trace(func, obj, frame, what, arg);
3822 if (err == 0)
3823 {
3824 PyErr_Restore(type, value, traceback);
3825 return 0;
3826 }
3827 else {
3828 Py_XDECREF(type);
3829 Py_XDECREF(value);
3830 Py_XDECREF(traceback);
3831 return -1;
3832 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003833}
3834
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003835static int
Fred Drake5755ce62001-06-27 19:19:46 +00003836call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 register PyThreadState *tstate = frame->f_tstate;
3840 int result;
3841 if (tstate->tracing)
3842 return 0;
3843 tstate->tracing++;
3844 tstate->use_tracing = 0;
3845 result = func(obj, frame, what, arg);
3846 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3847 || (tstate->c_profilefunc != NULL));
3848 tstate->tracing--;
3849 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003850}
3851
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003852PyObject *
3853_PyEval_CallTracing(PyObject *func, PyObject *args)
3854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003855 PyFrameObject *frame = PyEval_GetFrame();
3856 PyThreadState *tstate = frame->f_tstate;
3857 int save_tracing = tstate->tracing;
3858 int save_use_tracing = tstate->use_tracing;
3859 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 tstate->tracing = 0;
3862 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3863 || (tstate->c_profilefunc != NULL));
3864 result = PyObject_Call(func, args, NULL);
3865 tstate->tracing = save_tracing;
3866 tstate->use_tracing = save_use_tracing;
3867 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003868}
3869
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003870/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003871static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003872maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003873 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3874 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003875{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003876 int result = 0;
3877 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003879 /* If the last instruction executed isn't in the current
3880 instruction window, reset the window.
3881 */
3882 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3883 PyAddrPair bounds;
3884 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3885 &bounds);
3886 *instr_lb = bounds.ap_lower;
3887 *instr_ub = bounds.ap_upper;
3888 }
3889 /* If the last instruction falls at the start of a line or if
3890 it represents a jump backwards, update the frame's line
3891 number and call the trace function. */
3892 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3893 frame->f_lineno = line;
3894 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3895 }
3896 *instr_prev = frame->f_lasti;
3897 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003898}
3899
Fred Drake5755ce62001-06-27 19:19:46 +00003900void
3901PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 PyThreadState *tstate = PyThreadState_GET();
3904 PyObject *temp = tstate->c_profileobj;
3905 Py_XINCREF(arg);
3906 tstate->c_profilefunc = NULL;
3907 tstate->c_profileobj = NULL;
3908 /* Must make sure that tracing is not ignored if 'temp' is freed */
3909 tstate->use_tracing = tstate->c_tracefunc != NULL;
3910 Py_XDECREF(temp);
3911 tstate->c_profilefunc = func;
3912 tstate->c_profileobj = arg;
3913 /* Flag that tracing or profiling is turned on */
3914 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003915}
3916
3917void
3918PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003920 PyThreadState *tstate = PyThreadState_GET();
3921 PyObject *temp = tstate->c_traceobj;
3922 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3923 Py_XINCREF(arg);
3924 tstate->c_tracefunc = NULL;
3925 tstate->c_traceobj = NULL;
3926 /* Must make sure that profiling is not ignored if 'temp' is freed */
3927 tstate->use_tracing = tstate->c_profilefunc != NULL;
3928 Py_XDECREF(temp);
3929 tstate->c_tracefunc = func;
3930 tstate->c_traceobj = arg;
3931 /* Flag that tracing or profiling is turned on */
3932 tstate->use_tracing = ((func != NULL)
3933 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003934}
3935
Guido van Rossumb209a111997-04-29 18:18:01 +00003936PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003937PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003938{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003939 PyFrameObject *current_frame = PyEval_GetFrame();
3940 if (current_frame == NULL)
3941 return PyThreadState_GET()->interp->builtins;
3942 else
3943 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003944}
3945
Guido van Rossumb209a111997-04-29 18:18:01 +00003946PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003947PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003949 PyFrameObject *current_frame = PyEval_GetFrame();
3950 if (current_frame == NULL)
3951 return NULL;
3952 PyFrame_FastToLocals(current_frame);
3953 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003954}
3955
Guido van Rossumb209a111997-04-29 18:18:01 +00003956PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003957PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003959 PyFrameObject *current_frame = PyEval_GetFrame();
3960 if (current_frame == NULL)
3961 return NULL;
3962 else
3963 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003964}
3965
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003966PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003967PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003969 PyThreadState *tstate = PyThreadState_GET();
3970 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003971}
3972
Guido van Rossum6135a871995-01-09 17:53:26 +00003973int
Tim Peters5ba58662001-07-16 02:29:45 +00003974PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 PyFrameObject *current_frame = PyEval_GetFrame();
3977 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 if (current_frame != NULL) {
3980 const int codeflags = current_frame->f_code->co_flags;
3981 const int compilerflags = codeflags & PyCF_MASK;
3982 if (compilerflags) {
3983 result = 1;
3984 cf->cf_flags |= compilerflags;
3985 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003986#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 if (codeflags & CO_GENERATOR_ALLOWED) {
3988 result = 1;
3989 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3990 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003991#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 }
3993 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003994}
3995
Guido van Rossum3f5da241990-12-20 15:06:42 +00003996
Guido van Rossum681d79a1995-07-18 14:51:37 +00003997/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003998 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003999
Guido van Rossumb209a111997-04-29 18:18:01 +00004000PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004001PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004003 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004005 if (arg == NULL) {
4006 arg = PyTuple_New(0);
4007 if (arg == NULL)
4008 return NULL;
4009 }
4010 else if (!PyTuple_Check(arg)) {
4011 PyErr_SetString(PyExc_TypeError,
4012 "argument list must be a tuple");
4013 return NULL;
4014 }
4015 else
4016 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004018 if (kw != NULL && !PyDict_Check(kw)) {
4019 PyErr_SetString(PyExc_TypeError,
4020 "keyword list must be a dictionary");
4021 Py_DECREF(arg);
4022 return NULL;
4023 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004025 result = PyObject_Call(func, arg, kw);
4026 Py_DECREF(arg);
4027 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004028}
4029
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004030const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004031PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004032{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004033 if (PyMethod_Check(func))
4034 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4035 else if (PyFunction_Check(func))
4036 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
4037 else if (PyCFunction_Check(func))
4038 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4039 else
4040 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004041}
4042
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004043const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004044PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 if (PyMethod_Check(func))
4047 return "()";
4048 else if (PyFunction_Check(func))
4049 return "()";
4050 else if (PyCFunction_Check(func))
4051 return "()";
4052 else
4053 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004054}
4055
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00004056static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004057err_args(PyObject *func, int flags, int nargs)
4058{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004059 if (flags & METH_NOARGS)
4060 PyErr_Format(PyExc_TypeError,
4061 "%.200s() takes no arguments (%d given)",
4062 ((PyCFunctionObject *)func)->m_ml->ml_name,
4063 nargs);
4064 else
4065 PyErr_Format(PyExc_TypeError,
4066 "%.200s() takes exactly one argument (%d given)",
4067 ((PyCFunctionObject *)func)->m_ml->ml_name,
4068 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004069}
4070
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004071#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004072if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 if (call_trace(tstate->c_profilefunc, \
4074 tstate->c_profileobj, \
4075 tstate->frame, PyTrace_C_CALL, \
4076 func)) { \
4077 x = NULL; \
4078 } \
4079 else { \
4080 x = call; \
4081 if (tstate->c_profilefunc != NULL) { \
4082 if (x == NULL) { \
4083 call_trace_protected(tstate->c_profilefunc, \
4084 tstate->c_profileobj, \
4085 tstate->frame, PyTrace_C_EXCEPTION, \
4086 func); \
4087 /* XXX should pass (type, value, tb) */ \
4088 } else { \
4089 if (call_trace(tstate->c_profilefunc, \
4090 tstate->c_profileobj, \
4091 tstate->frame, PyTrace_C_RETURN, \
4092 func)) { \
4093 Py_DECREF(x); \
4094 x = NULL; \
4095 } \
4096 } \
4097 } \
4098 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004099} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004100 x = call; \
4101 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004102
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004103static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004104call_function(PyObject ***pp_stack, int oparg
4105#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004106 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004107#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004109{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 int na = oparg & 0xff;
4111 int nk = (oparg>>8) & 0xff;
4112 int n = na + 2 * nk;
4113 PyObject **pfunc = (*pp_stack) - n - 1;
4114 PyObject *func = *pfunc;
4115 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 /* Always dispatch PyCFunction first, because these are
4118 presumed to be the most frequent callable object.
4119 */
4120 if (PyCFunction_Check(func) && nk == 0) {
4121 int flags = PyCFunction_GET_FLAGS(func);
4122 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004123
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004124 PCALL(PCALL_CFUNCTION);
4125 if (flags & (METH_NOARGS | METH_O)) {
4126 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4127 PyObject *self = PyCFunction_GET_SELF(func);
4128 if (flags & METH_NOARGS && na == 0) {
4129 C_TRACE(x, (*meth)(self,NULL));
4130 }
4131 else if (flags & METH_O && na == 1) {
4132 PyObject *arg = EXT_POP(*pp_stack);
4133 C_TRACE(x, (*meth)(self,arg));
4134 Py_DECREF(arg);
4135 }
4136 else {
4137 err_args(func, flags, na);
4138 x = NULL;
4139 }
4140 }
4141 else {
4142 PyObject *callargs;
4143 callargs = load_args(pp_stack, na);
4144 READ_TIMESTAMP(*pintr0);
4145 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4146 READ_TIMESTAMP(*pintr1);
4147 Py_XDECREF(callargs);
4148 }
4149 } else {
4150 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4151 /* optimize access to bound methods */
4152 PyObject *self = PyMethod_GET_SELF(func);
4153 PCALL(PCALL_METHOD);
4154 PCALL(PCALL_BOUND_METHOD);
4155 Py_INCREF(self);
4156 func = PyMethod_GET_FUNCTION(func);
4157 Py_INCREF(func);
4158 Py_DECREF(*pfunc);
4159 *pfunc = self;
4160 na++;
4161 n++;
4162 } else
4163 Py_INCREF(func);
4164 READ_TIMESTAMP(*pintr0);
4165 if (PyFunction_Check(func))
4166 x = fast_function(func, pp_stack, n, na, nk);
4167 else
4168 x = do_call(func, pp_stack, na, nk);
4169 READ_TIMESTAMP(*pintr1);
4170 Py_DECREF(func);
4171 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 /* Clear the stack of the function object. Also removes
4174 the arguments in case they weren't consumed already
4175 (fast_function() and err_args() leave them on the stack).
4176 */
4177 while ((*pp_stack) > pfunc) {
4178 w = EXT_POP(*pp_stack);
4179 Py_DECREF(w);
4180 PCALL(PCALL_POP);
4181 }
4182 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004183}
4184
Jeremy Hylton192690e2002-08-16 18:36:11 +00004185/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004186 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004187 For the simplest case -- a function that takes only positional
4188 arguments and is called with only positional arguments -- it
4189 inlines the most primitive frame setup code from
4190 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4191 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004192*/
4193
4194static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004195fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004196{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4198 PyObject *globals = PyFunction_GET_GLOBALS(func);
4199 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4200 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
4201 PyObject **d = NULL;
4202 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004203
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004204 PCALL(PCALL_FUNCTION);
4205 PCALL(PCALL_FAST_FUNCTION);
4206 if (argdefs == NULL && co->co_argcount == n &&
4207 co->co_kwonlyargcount == 0 && nk==0 &&
4208 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4209 PyFrameObject *f;
4210 PyObject *retval = NULL;
4211 PyThreadState *tstate = PyThreadState_GET();
4212 PyObject **fastlocals, **stack;
4213 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004215 PCALL(PCALL_FASTER_FUNCTION);
4216 assert(globals != NULL);
4217 /* XXX Perhaps we should create a specialized
4218 PyFrame_New() that doesn't take locals, but does
4219 take builtins without sanity checking them.
4220 */
4221 assert(tstate != NULL);
4222 f = PyFrame_New(tstate, co, globals, NULL);
4223 if (f == NULL)
4224 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004226 fastlocals = f->f_localsplus;
4227 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 for (i = 0; i < n; i++) {
4230 Py_INCREF(*stack);
4231 fastlocals[i] = *stack++;
4232 }
4233 retval = PyEval_EvalFrameEx(f,0);
4234 ++tstate->recursion_depth;
4235 Py_DECREF(f);
4236 --tstate->recursion_depth;
4237 return retval;
4238 }
4239 if (argdefs != NULL) {
4240 d = &PyTuple_GET_ITEM(argdefs, 0);
4241 nd = Py_SIZE(argdefs);
4242 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00004243 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 (PyObject *)NULL, (*pp_stack)-n, na,
4245 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
4246 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004247}
4248
4249static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004250update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4251 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004252{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004253 PyObject *kwdict = NULL;
4254 if (orig_kwdict == NULL)
4255 kwdict = PyDict_New();
4256 else {
4257 kwdict = PyDict_Copy(orig_kwdict);
4258 Py_DECREF(orig_kwdict);
4259 }
4260 if (kwdict == NULL)
4261 return NULL;
4262 while (--nk >= 0) {
4263 int err;
4264 PyObject *value = EXT_POP(*pp_stack);
4265 PyObject *key = EXT_POP(*pp_stack);
4266 if (PyDict_GetItem(kwdict, key) != NULL) {
4267 PyErr_Format(PyExc_TypeError,
4268 "%.200s%s got multiple values "
4269 "for keyword argument '%U'",
4270 PyEval_GetFuncName(func),
4271 PyEval_GetFuncDesc(func),
4272 key);
4273 Py_DECREF(key);
4274 Py_DECREF(value);
4275 Py_DECREF(kwdict);
4276 return NULL;
4277 }
4278 err = PyDict_SetItem(kwdict, key, value);
4279 Py_DECREF(key);
4280 Py_DECREF(value);
4281 if (err) {
4282 Py_DECREF(kwdict);
4283 return NULL;
4284 }
4285 }
4286 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004287}
4288
4289static PyObject *
4290update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004291 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 callargs = PyTuple_New(nstack + nstar);
4296 if (callargs == NULL) {
4297 return NULL;
4298 }
4299 if (nstar) {
4300 int i;
4301 for (i = 0; i < nstar; i++) {
4302 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4303 Py_INCREF(a);
4304 PyTuple_SET_ITEM(callargs, nstack + i, a);
4305 }
4306 }
4307 while (--nstack >= 0) {
4308 w = EXT_POP(*pp_stack);
4309 PyTuple_SET_ITEM(callargs, nstack, w);
4310 }
4311 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004312}
4313
4314static PyObject *
4315load_args(PyObject ***pp_stack, int na)
4316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004317 PyObject *args = PyTuple_New(na);
4318 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 if (args == NULL)
4321 return NULL;
4322 while (--na >= 0) {
4323 w = EXT_POP(*pp_stack);
4324 PyTuple_SET_ITEM(args, na, w);
4325 }
4326 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004327}
4328
4329static PyObject *
4330do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4331{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004332 PyObject *callargs = NULL;
4333 PyObject *kwdict = NULL;
4334 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004335
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004336 if (nk > 0) {
4337 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4338 if (kwdict == NULL)
4339 goto call_fail;
4340 }
4341 callargs = load_args(pp_stack, na);
4342 if (callargs == NULL)
4343 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004344#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 /* At this point, we have to look at the type of func to
4346 update the call stats properly. Do it here so as to avoid
4347 exposing the call stats machinery outside ceval.c
4348 */
4349 if (PyFunction_Check(func))
4350 PCALL(PCALL_FUNCTION);
4351 else if (PyMethod_Check(func))
4352 PCALL(PCALL_METHOD);
4353 else if (PyType_Check(func))
4354 PCALL(PCALL_TYPE);
4355 else if (PyCFunction_Check(func))
4356 PCALL(PCALL_CFUNCTION);
4357 else
4358 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004359#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 if (PyCFunction_Check(func)) {
4361 PyThreadState *tstate = PyThreadState_GET();
4362 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4363 }
4364 else
4365 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004366call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 Py_XDECREF(callargs);
4368 Py_XDECREF(kwdict);
4369 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004370}
4371
4372static PyObject *
4373ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 int nstar = 0;
4376 PyObject *callargs = NULL;
4377 PyObject *stararg = NULL;
4378 PyObject *kwdict = NULL;
4379 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 if (flags & CALL_FLAG_KW) {
4382 kwdict = EXT_POP(*pp_stack);
4383 if (!PyDict_Check(kwdict)) {
4384 PyObject *d;
4385 d = PyDict_New();
4386 if (d == NULL)
4387 goto ext_call_fail;
4388 if (PyDict_Update(d, kwdict) != 0) {
4389 Py_DECREF(d);
4390 /* PyDict_Update raises attribute
4391 * error (percolated from an attempt
4392 * to get 'keys' attribute) instead of
4393 * a type error if its second argument
4394 * is not a mapping.
4395 */
4396 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4397 PyErr_Format(PyExc_TypeError,
4398 "%.200s%.200s argument after ** "
4399 "must be a mapping, not %.200s",
4400 PyEval_GetFuncName(func),
4401 PyEval_GetFuncDesc(func),
4402 kwdict->ob_type->tp_name);
4403 }
4404 goto ext_call_fail;
4405 }
4406 Py_DECREF(kwdict);
4407 kwdict = d;
4408 }
4409 }
4410 if (flags & CALL_FLAG_VAR) {
4411 stararg = EXT_POP(*pp_stack);
4412 if (!PyTuple_Check(stararg)) {
4413 PyObject *t = NULL;
4414 t = PySequence_Tuple(stararg);
4415 if (t == NULL) {
4416 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4417 PyErr_Format(PyExc_TypeError,
4418 "%.200s%.200s argument after * "
Victor Stinner0a5f65a2011-03-22 01:09:21 +01004419 "must be a sequence, not %.200s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004420 PyEval_GetFuncName(func),
4421 PyEval_GetFuncDesc(func),
4422 stararg->ob_type->tp_name);
4423 }
4424 goto ext_call_fail;
4425 }
4426 Py_DECREF(stararg);
4427 stararg = t;
4428 }
4429 nstar = PyTuple_GET_SIZE(stararg);
4430 }
4431 if (nk > 0) {
4432 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4433 if (kwdict == NULL)
4434 goto ext_call_fail;
4435 }
4436 callargs = update_star_args(na, nstar, stararg, pp_stack);
4437 if (callargs == NULL)
4438 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004439#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 /* At this point, we have to look at the type of func to
4441 update the call stats properly. Do it here so as to avoid
4442 exposing the call stats machinery outside ceval.c
4443 */
4444 if (PyFunction_Check(func))
4445 PCALL(PCALL_FUNCTION);
4446 else if (PyMethod_Check(func))
4447 PCALL(PCALL_METHOD);
4448 else if (PyType_Check(func))
4449 PCALL(PCALL_TYPE);
4450 else if (PyCFunction_Check(func))
4451 PCALL(PCALL_CFUNCTION);
4452 else
4453 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004454#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 if (PyCFunction_Check(func)) {
4456 PyThreadState *tstate = PyThreadState_GET();
4457 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4458 }
4459 else
4460 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004461ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004462 Py_XDECREF(callargs);
4463 Py_XDECREF(kwdict);
4464 Py_XDECREF(stararg);
4465 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004466}
4467
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004468/* Extract a slice index from a PyInt or PyLong or an object with the
4469 nb_index slot defined, and store in *pi.
4470 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4471 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 +00004472 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004473*/
Tim Petersb5196382001-12-16 19:44:20 +00004474/* Note: If v is NULL, return success without storing into *pi. This
4475 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4476 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004477*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004478int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004479_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004481 if (v != NULL) {
4482 Py_ssize_t x;
4483 if (PyIndex_Check(v)) {
4484 x = PyNumber_AsSsize_t(v, NULL);
4485 if (x == -1 && PyErr_Occurred())
4486 return 0;
4487 }
4488 else {
4489 PyErr_SetString(PyExc_TypeError,
4490 "slice indices must be integers or "
4491 "None or have an __index__ method");
4492 return 0;
4493 }
4494 *pi = x;
4495 }
4496 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004497}
4498
Guido van Rossum486364b2007-06-30 05:01:58 +00004499#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004501
Guido van Rossumb209a111997-04-29 18:18:01 +00004502static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004503cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 int res = 0;
4506 switch (op) {
4507 case PyCmp_IS:
4508 res = (v == w);
4509 break;
4510 case PyCmp_IS_NOT:
4511 res = (v != w);
4512 break;
4513 case PyCmp_IN:
4514 res = PySequence_Contains(w, v);
4515 if (res < 0)
4516 return NULL;
4517 break;
4518 case PyCmp_NOT_IN:
4519 res = PySequence_Contains(w, v);
4520 if (res < 0)
4521 return NULL;
4522 res = !res;
4523 break;
4524 case PyCmp_EXC_MATCH:
4525 if (PyTuple_Check(w)) {
4526 Py_ssize_t i, length;
4527 length = PyTuple_Size(w);
4528 for (i = 0; i < length; i += 1) {
4529 PyObject *exc = PyTuple_GET_ITEM(w, i);
4530 if (!PyExceptionClass_Check(exc)) {
4531 PyErr_SetString(PyExc_TypeError,
4532 CANNOT_CATCH_MSG);
4533 return NULL;
4534 }
4535 }
4536 }
4537 else {
4538 if (!PyExceptionClass_Check(w)) {
4539 PyErr_SetString(PyExc_TypeError,
4540 CANNOT_CATCH_MSG);
4541 return NULL;
4542 }
4543 }
4544 res = PyErr_GivenExceptionMatches(v, w);
4545 break;
4546 default:
4547 return PyObject_RichCompare(v, w, op);
4548 }
4549 v = res ? Py_True : Py_False;
4550 Py_INCREF(v);
4551 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004552}
4553
Thomas Wouters52152252000-08-17 22:55:00 +00004554static PyObject *
4555import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004556{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 x = PyObject_GetAttr(v, name);
4560 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4561 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4562 }
4563 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004564}
Guido van Rossumac7be682001-01-17 15:42:30 +00004565
Thomas Wouters52152252000-08-17 22:55:00 +00004566static int
4567import_all_from(PyObject *locals, PyObject *v)
4568{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004569 _Py_IDENTIFIER(__all__);
4570 _Py_IDENTIFIER(__dict__);
4571 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 PyObject *dict, *name, *value;
4573 int skip_leading_underscores = 0;
4574 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 if (all == NULL) {
4577 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4578 return -1; /* Unexpected error */
4579 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004580 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 if (dict == NULL) {
4582 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4583 return -1;
4584 PyErr_SetString(PyExc_ImportError,
4585 "from-import-* object has no __dict__ and no __all__");
4586 return -1;
4587 }
4588 all = PyMapping_Keys(dict);
4589 Py_DECREF(dict);
4590 if (all == NULL)
4591 return -1;
4592 skip_leading_underscores = 1;
4593 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004595 for (pos = 0, err = 0; ; pos++) {
4596 name = PySequence_GetItem(all, pos);
4597 if (name == NULL) {
4598 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4599 err = -1;
4600 else
4601 PyErr_Clear();
4602 break;
4603 }
4604 if (skip_leading_underscores &&
4605 PyUnicode_Check(name) &&
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02004606 PyUnicode_READY(name) != -1 &&
4607 PyUnicode_READ_CHAR(name, 0) == '_')
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004608 {
4609 Py_DECREF(name);
4610 continue;
4611 }
4612 value = PyObject_GetAttr(v, name);
4613 if (value == NULL)
4614 err = -1;
4615 else if (PyDict_CheckExact(locals))
4616 err = PyDict_SetItem(locals, name, value);
4617 else
4618 err = PyObject_SetItem(locals, name, value);
4619 Py_DECREF(name);
4620 Py_XDECREF(value);
4621 if (err != 0)
4622 break;
4623 }
4624 Py_DECREF(all);
4625 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004626}
4627
Guido van Rossumac7be682001-01-17 15:42:30 +00004628static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004629format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004632
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004633 if (!obj)
4634 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 obj_str = _PyUnicode_AsString(obj);
4637 if (!obj_str)
4638 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004639
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004641}
Guido van Rossum950361c1997-01-24 13:49:28 +00004642
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004643static void
4644format_exc_unbound(PyCodeObject *co, int oparg)
4645{
4646 PyObject *name;
4647 /* Don't stomp existing exception */
4648 if (PyErr_Occurred())
4649 return;
4650 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4651 name = PyTuple_GET_ITEM(co->co_cellvars,
4652 oparg);
4653 format_exc_check_arg(
4654 PyExc_UnboundLocalError,
4655 UNBOUNDLOCAL_ERROR_MSG,
4656 name);
4657 } else {
4658 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4659 PyTuple_GET_SIZE(co->co_cellvars));
4660 format_exc_check_arg(PyExc_NameError,
4661 UNBOUNDFREE_ERROR_MSG, name);
4662 }
4663}
4664
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004665static PyObject *
4666unicode_concatenate(PyObject *v, PyObject *w,
4667 PyFrameObject *f, unsigned char *next_instr)
4668{
4669 PyObject *res;
4670 if (Py_REFCNT(v) == 2) {
4671 /* In the common case, there are 2 references to the value
4672 * stored in 'variable' when the += is performed: one on the
4673 * value stack (in 'v') and one still stored in the
4674 * 'variable'. We try to delete the variable now to reduce
4675 * the refcnt to 1.
4676 */
4677 switch (*next_instr) {
4678 case STORE_FAST:
4679 {
4680 int oparg = PEEKARG();
4681 PyObject **fastlocals = f->f_localsplus;
4682 if (GETLOCAL(oparg) == v)
4683 SETLOCAL(oparg, NULL);
4684 break;
4685 }
4686 case STORE_DEREF:
4687 {
4688 PyObject **freevars = (f->f_localsplus +
4689 f->f_code->co_nlocals);
4690 PyObject *c = freevars[PEEKARG()];
4691 if (PyCell_GET(c) == v)
4692 PyCell_Set(c, NULL);
4693 break;
4694 }
4695 case STORE_NAME:
4696 {
4697 PyObject *names = f->f_code->co_names;
4698 PyObject *name = GETITEM(names, PEEKARG());
4699 PyObject *locals = f->f_locals;
4700 if (PyDict_CheckExact(locals) &&
4701 PyDict_GetItem(locals, name) == v) {
4702 if (PyDict_DelItem(locals, name) != 0) {
4703 PyErr_Clear();
4704 }
4705 }
4706 break;
4707 }
4708 }
4709 }
4710 res = v;
4711 PyUnicode_Append(&res, w);
4712 return res;
4713}
4714
Guido van Rossum950361c1997-01-24 13:49:28 +00004715#ifdef DYNAMIC_EXECUTION_PROFILE
4716
Skip Montanarof118cb12001-10-15 20:51:38 +00004717static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004718getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004720 int i;
4721 PyObject *l = PyList_New(256);
4722 if (l == NULL) return NULL;
4723 for (i = 0; i < 256; i++) {
4724 PyObject *x = PyLong_FromLong(a[i]);
4725 if (x == NULL) {
4726 Py_DECREF(l);
4727 return NULL;
4728 }
4729 PyList_SetItem(l, i, x);
4730 }
4731 for (i = 0; i < 256; i++)
4732 a[i] = 0;
4733 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004734}
4735
4736PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004737_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004738{
4739#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004740 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004741#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004742 int i;
4743 PyObject *l = PyList_New(257);
4744 if (l == NULL) return NULL;
4745 for (i = 0; i < 257; i++) {
4746 PyObject *x = getarray(dxpairs[i]);
4747 if (x == NULL) {
4748 Py_DECREF(l);
4749 return NULL;
4750 }
4751 PyList_SetItem(l, i, x);
4752 }
4753 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004754#endif
4755}
4756
4757#endif