blob: 705ed415a9ce36421ed9a77e4d732bd8a10d3d48 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000017#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018
Guido van Rossumc6004111993-11-05 10:22:19 +000019#include <ctype.h>
20
Thomas Wouters477c8d52006-05-27 19:21:47 +000021#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000022
23#define READ_TIMESTAMP(var)
24
25#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000026
27typedef unsigned long long uint64;
28
Ezio Melotti13925002011-03-16 11:05:33 +020029/* PowerPC support.
David Malcolmf1397ad2011-01-06 17:01:36 +000030 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
31 "__powerpc__" appears to be the correct one for Linux with GCC
32*/
33#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000141static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000147#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000149#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "free variable '%.200s' referenced before assignment" \
151 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000152
Guido van Rossum950361c1997-01-24 13:49:28 +0000153/* Dynamic execution profile */
154#ifdef DYNAMIC_EXECUTION_PROFILE
155#ifdef DXPAIRS
156static long dxpairs[257][256];
157#define dxp dxpairs[256]
158#else
159static long dxp[256];
160#endif
161#endif
162
Jeremy Hylton985eba52003-02-05 23:13:00 +0000163/* Function call profile */
164#ifdef CALL_PROFILE
165#define PCALL_NUM 11
166static int pcall[PCALL_NUM];
167
168#define PCALL_ALL 0
169#define PCALL_FUNCTION 1
170#define PCALL_FAST_FUNCTION 2
171#define PCALL_FASTER_FUNCTION 3
172#define PCALL_METHOD 4
173#define PCALL_BOUND_METHOD 5
174#define PCALL_CFUNCTION 6
175#define PCALL_TYPE 7
176#define PCALL_GENERATOR 8
177#define PCALL_OTHER 9
178#define PCALL_POP 10
179
180/* Notes about the statistics
181
182 PCALL_FAST stats
183
184 FAST_FUNCTION means no argument tuple needs to be created.
185 FASTER_FUNCTION means that the fast-path frame setup code is used.
186
187 If there is a method call where the call can be optimized by changing
188 the argument tuple and calling the function directly, it gets recorded
189 twice.
190
191 As a result, the relationship among the statistics appears to be
192 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
193 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
194 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
195 PCALL_METHOD > PCALL_BOUND_METHOD
196*/
197
198#define PCALL(POS) pcall[POS]++
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return Py_BuildValue("iiiiiiiiiii",
204 pcall[0], pcall[1], pcall[2], pcall[3],
205 pcall[4], pcall[5], pcall[6], pcall[7],
206 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000207}
208#else
209#define PCALL(O)
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#endif
218
Tim Peters5ca576e2001-06-18 22:08:13 +0000219
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000220#ifdef WITH_THREAD
221#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
222#else
223#define GIL_REQUEST 0
224#endif
225
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000226/* This can set eval_breaker to 0 even though gil_drop_request became
227 1. We believe this is all right because the eval loop will release
228 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000229#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 _Py_atomic_store_relaxed( \
231 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000232 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
234 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000236#ifdef WITH_THREAD
237
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000238#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 do { \
240 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
241 _Py_atomic_store_relaxed(&eval_breaker, 1); \
242 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243
244#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 do { \
246 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
247 COMPUTE_EVAL_BREAKER(); \
248 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000249
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000250#endif
251
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000252/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 do { \
255 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
256 _Py_atomic_store_relaxed(&eval_breaker, 1); \
257 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000258
259#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 do { \
261 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
262 COMPUTE_EVAL_BREAKER(); \
263 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000264
265#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 do { \
267 pending_async_exc = 1; \
268 _Py_atomic_store_relaxed(&eval_breaker, 1); \
269 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000270
271#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273
274
Guido van Rossume59214e1994-08-30 08:01:59 +0000275#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000276
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000277#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000279#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000280#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000281
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000282static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000283static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284/* This single variable consolidates all requests to break out of the fast path
285 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000286static _Py_atomic_int eval_breaker = {0};
287/* Request for dropping the GIL */
288static _Py_atomic_int gil_drop_request = {0};
289/* Request for running pending calls. */
290static _Py_atomic_int pendingcalls_to_do = {0};
291/* Request for looking at the `async_exc` field of the current thread state.
292 Guarded by the GIL. */
293static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000294
295#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000296
Tim Peters7f468f22004-10-11 02:40:51 +0000297int
298PyEval_ThreadsInitialized(void)
299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000301}
302
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000304PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000306 if (gil_created())
307 return;
308 create_gil();
309 take_gil(PyThreadState_GET());
310 main_thread = PyThread_get_thread_ident();
311 if (!pending_lock)
312 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000313}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000314
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000315void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000316_PyEval_FiniThreads(void)
317{
318 if (!gil_created())
319 return;
320 destroy_gil();
321 assert(!gil_created());
322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyThreadState *tstate = PyThreadState_GET();
328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
330 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000331}
332
333void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000334PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000335{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 /* This function must succeed when the current thread state is NULL.
337 We therefore avoid PyThreadState_GET() which dumps a fatal error
338 in debug mode.
339 */
340 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
341 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000342}
343
344void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000345PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 if (tstate == NULL)
348 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
349 /* Check someone has called PyEval_InitThreads() to create the lock */
350 assert(gil_created());
351 take_gil(tstate);
352 if (PyThreadState_Swap(tstate) != NULL)
353 Py_FatalError(
354 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000355}
356
357void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000358PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000359{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (tstate == NULL)
361 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
362 if (PyThreadState_Swap(NULL) != tstate)
363 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
364 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000365}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000366
367/* This function is called from PyOS_AfterFork to ensure that newly
368 created child processes don't hold locks referring to threads which
369 are not running in the child process. (This could also be done using
370 pthread_atfork mechanism, at least for the pthreads implementation.) */
371
372void
373PyEval_ReInitThreads(void)
374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *threading, *result;
376 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 if (!gil_created())
379 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 recreate_gil();
381 pending_lock = PyThread_allocate_lock();
382 take_gil(tstate);
383 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* Update the threading module with the new state.
386 */
387 tstate = PyThreadState_GET();
388 threading = PyMapping_GetItemString(tstate->interp->modules,
389 "threading");
390 if (threading == NULL) {
391 /* threading not imported */
392 PyErr_Clear();
393 return;
394 }
395 result = PyObject_CallMethod(threading, "_after_fork", NULL);
396 if (result == NULL)
397 PyErr_WriteUnraisable(threading);
398 else
399 Py_DECREF(result);
400 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000401}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000402
403#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000404static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000405static int pending_async_exc = 0;
406#endif /* WITH_THREAD */
407
408/* This function is used to signal that async exceptions are waiting to be
409 raised, therefore it is also useful in non-threaded builds. */
410
411void
412_PyEval_SignalAsyncExc(void)
413{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000415}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000416
Guido van Rossumff4949e1992-08-05 19:58:53 +0000417/* Functions save_thread and restore_thread are always defined so
418 dynamically loaded modules needn't be compiled separately for use
419 with and without threads: */
420
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000421PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000422PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000423{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 PyThreadState *tstate = PyThreadState_Swap(NULL);
425 if (tstate == NULL)
426 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000427#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 if (gil_created())
429 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000430#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432}
433
434void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000435PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000436{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 if (tstate == NULL)
438 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000439#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (gil_created()) {
441 int err = errno;
442 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200443 /* _Py_Finalizing is protected by the GIL */
444 if (_Py_Finalizing && tstate != _Py_Finalizing) {
445 drop_gil(tstate);
446 PyThread_exit_thread();
447 assert(0); /* unreachable */
448 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449 errno = err;
450 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000451#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000452 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000453}
454
455
Guido van Rossuma9672091994-09-14 13:31:22 +0000456/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
457 signal handlers or Mac I/O completion routines) can schedule calls
458 to a function to be called synchronously.
459 The synchronous function is called with one void* argument.
460 It should return 0 for success or -1 for failure -- failure should
461 be accompanied by an exception.
462
463 If registry succeeds, the registry function returns 0; if it fails
464 (e.g. due to too many pending calls) it returns -1 (without setting
465 an exception condition).
466
467 Note that because registry may occur from within signal handlers,
468 or other asynchronous events, calling malloc() is unsafe!
469
470#ifdef WITH_THREAD
471 Any thread can schedule pending calls, but only the main thread
472 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000473 There is no facility to schedule calls to a particular thread, but
474 that should be easy to change, should that ever be required. In
475 that case, the static variables here should go into the python
476 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000477#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000478*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000479
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000480#ifdef WITH_THREAD
481
482/* The WITH_THREAD implementation is thread-safe. It allows
483 scheduling to be made from any thread, and even from an executing
484 callback.
485 */
486
487#define NPENDINGCALLS 32
488static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 int (*func)(void *);
490 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000491} pendingcalls[NPENDINGCALLS];
492static int pendingfirst = 0;
493static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000494static char pendingbusy = 0;
495
496int
497Py_AddPendingCall(int (*func)(void *), void *arg)
498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 int i, j, result=0;
500 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 /* try a few times for the lock. Since this mechanism is used
503 * for signal handling (on the main thread), there is a (slim)
504 * chance that a signal is delivered on the same thread while we
505 * hold the lock during the Py_MakePendingCalls() function.
506 * This avoids a deadlock in that case.
507 * Note that signals can be delivered on any thread. In particular,
508 * on Windows, a SIGINT is delivered on a system-created worker
509 * thread.
510 * We also check for lock being NULL, in the unlikely case that
511 * this function is called before any bytecode evaluation takes place.
512 */
513 if (lock != NULL) {
514 for (i = 0; i<100; i++) {
515 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
516 break;
517 }
518 if (i == 100)
519 return -1;
520 }
521
522 i = pendinglast;
523 j = (i + 1) % NPENDINGCALLS;
524 if (j == pendingfirst) {
525 result = -1; /* Queue full */
526 } else {
527 pendingcalls[i].func = func;
528 pendingcalls[i].arg = arg;
529 pendinglast = j;
530 }
531 /* signal main loop */
532 SIGNAL_PENDING_CALLS();
533 if (lock != NULL)
534 PyThread_release_lock(lock);
535 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536}
537
538int
539Py_MakePendingCalls(void)
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 int i;
542 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 if (!pending_lock) {
545 /* initial allocation of the lock */
546 pending_lock = PyThread_allocate_lock();
547 if (pending_lock == NULL)
548 return -1;
549 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* only service pending calls on main thread */
552 if (main_thread && PyThread_get_thread_ident() != main_thread)
553 return 0;
554 /* don't perform recursive pending calls */
555 if (pendingbusy)
556 return 0;
557 pendingbusy = 1;
558 /* perform a bounded number of calls, in case of recursion */
559 for (i=0; i<NPENDINGCALLS; i++) {
560 int j;
561 int (*func)(void *);
562 void *arg = NULL;
563
564 /* pop one item off the queue while holding the lock */
565 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
566 j = pendingfirst;
567 if (j == pendinglast) {
568 func = NULL; /* Queue empty */
569 } else {
570 func = pendingcalls[j].func;
571 arg = pendingcalls[j].arg;
572 pendingfirst = (j + 1) % NPENDINGCALLS;
573 }
574 if (pendingfirst != pendinglast)
575 SIGNAL_PENDING_CALLS();
576 else
577 UNSIGNAL_PENDING_CALLS();
578 PyThread_release_lock(pending_lock);
579 /* having released the lock, perform the callback */
580 if (func == NULL)
581 break;
582 r = func(arg);
583 if (r)
584 break;
585 }
586 pendingbusy = 0;
587 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000588}
589
590#else /* if ! defined WITH_THREAD */
591
592/*
593 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
594 This code is used for signal handling in python that isn't built
595 with WITH_THREAD.
596 Don't use this implementation when Py_AddPendingCalls() can happen
597 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000598
Guido van Rossuma9672091994-09-14 13:31:22 +0000599 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000600 (1) nested asynchronous calls to Py_AddPendingCall()
601 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000603 (1) is very unlikely because typically signal delivery
604 is blocked during signal handling. So it should be impossible.
605 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000606 The current code is safe against (2), but not against (1).
607 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000608 thread is present, interrupted by signals, and that the critical
609 section is protected with the "busy" variable. On Windows, which
610 delivers SIGINT on a system thread, this does not hold and therefore
611 Windows really shouldn't use this version.
612 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000613*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000614
Guido van Rossuma9672091994-09-14 13:31:22 +0000615#define NPENDINGCALLS 32
616static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 int (*func)(void *);
618 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000619} pendingcalls[NPENDINGCALLS];
620static volatile int pendingfirst = 0;
621static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000622static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000623
624int
Thomas Wouters334fb892000-07-25 12:56:38 +0000625Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000626{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 static volatile int busy = 0;
628 int i, j;
629 /* XXX Begin critical section */
630 if (busy)
631 return -1;
632 busy = 1;
633 i = pendinglast;
634 j = (i + 1) % NPENDINGCALLS;
635 if (j == pendingfirst) {
636 busy = 0;
637 return -1; /* Queue full */
638 }
639 pendingcalls[i].func = func;
640 pendingcalls[i].arg = arg;
641 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 SIGNAL_PENDING_CALLS();
644 busy = 0;
645 /* XXX End critical section */
646 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000647}
648
Guido van Rossum180d7b41994-09-29 09:45:57 +0000649int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000650Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000651{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 static int busy = 0;
653 if (busy)
654 return 0;
655 busy = 1;
656 UNSIGNAL_PENDING_CALLS();
657 for (;;) {
658 int i;
659 int (*func)(void *);
660 void *arg;
661 i = pendingfirst;
662 if (i == pendinglast)
663 break; /* Queue empty */
664 func = pendingcalls[i].func;
665 arg = pendingcalls[i].arg;
666 pendingfirst = (i + 1) % NPENDINGCALLS;
667 if (func(arg) < 0) {
668 busy = 0;
669 SIGNAL_PENDING_CALLS(); /* We're not done yet */
670 return -1;
671 }
672 }
673 busy = 0;
674 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000675}
676
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000677#endif /* WITH_THREAD */
678
Guido van Rossuma9672091994-09-14 13:31:22 +0000679
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000680/* The interpreter's recursion limit */
681
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000682#ifndef Py_DEFAULT_RECURSION_LIMIT
683#define Py_DEFAULT_RECURSION_LIMIT 1000
684#endif
685static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
686int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000687
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000688int
689Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000692}
693
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000694void
695Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 recursion_limit = new_limit;
698 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000699}
700
Armin Rigo2b3eb402003-10-28 12:05:48 +0000701/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
702 if the recursion_depth reaches _Py_CheckRecursionLimit.
703 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
704 to guarantee that _Py_CheckRecursiveCall() is regularly called.
705 Without USE_STACKCHECK, there is no need for this. */
706int
707_Py_CheckRecursiveCall(char *where)
708{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000710
711#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 if (PyOS_CheckStack()) {
713 --tstate->recursion_depth;
714 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
715 return -1;
716 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000717#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000718 _Py_CheckRecursionLimit = recursion_limit;
719 if (tstate->recursion_critical)
720 /* Somebody asked that we don't check for recursion. */
721 return 0;
722 if (tstate->overflowed) {
723 if (tstate->recursion_depth > recursion_limit + 50) {
724 /* Overflowing while handling an overflow. Give up. */
725 Py_FatalError("Cannot recover from stack overflow.");
726 }
727 return 0;
728 }
729 if (tstate->recursion_depth > recursion_limit) {
730 --tstate->recursion_depth;
731 tstate->overflowed = 1;
732 PyErr_Format(PyExc_RuntimeError,
733 "maximum recursion depth exceeded%s",
734 where);
735 return -1;
736 }
737 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000738}
739
Guido van Rossum374a9221991-04-04 10:40:29 +0000740/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000741enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000742 WHY_NOT = 0x0001, /* No error */
743 WHY_EXCEPTION = 0x0002, /* Exception occurred */
744 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
745 WHY_RETURN = 0x0008, /* 'return' statement */
746 WHY_BREAK = 0x0010, /* 'break' statement */
747 WHY_CONTINUE = 0x0020, /* 'continue' statement */
748 WHY_YIELD = 0x0040, /* 'yield' operator */
749 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000750};
Guido van Rossum374a9221991-04-04 10:40:29 +0000751
Collin Winter828f04a2007-08-31 00:04:24 +0000752static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000753static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000754
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000755/* Records whether tracing is on for any thread. Counts the number of
756 threads for which tstate->c_tracefunc is non-NULL, so if the value
757 is 0, we know we don't have to check this thread's c_tracefunc.
758 This speeds up the if statement in PyEval_EvalFrameEx() after
759 fast_next_opcode*/
760static int _Py_TracingPossible = 0;
761
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000762
Guido van Rossum374a9221991-04-04 10:40:29 +0000763
Guido van Rossumb209a111997-04-29 18:18:01 +0000764PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000765PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 return PyEval_EvalCodeEx(co,
768 globals, locals,
769 (PyObject **)NULL, 0,
770 (PyObject **)NULL, 0,
771 (PyObject **)NULL, 0,
772 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000773}
774
775
776/* Interpreter main loop */
777
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000778PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000779PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 /* This is for backward compatibility with extension modules that
781 used this API; core interpreter code should call
782 PyEval_EvalFrameEx() */
783 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000784}
785
786PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000788{
Guido van Rossum950361c1997-01-24 13:49:28 +0000789#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000791#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 register PyObject **stack_pointer; /* Next free slot in value stack */
793 register unsigned char *next_instr;
794 register int opcode; /* Current opcode */
795 register int oparg; /* Current opcode argument, if any */
796 register enum why_code why; /* Reason for block stack unwind */
797 register int err; /* Error status -- nonzero if error */
798 register PyObject *x; /* Result object -- NULL if error */
799 register PyObject *v; /* Temporary objects popped off stack */
800 register PyObject *w;
801 register PyObject *u;
802 register PyObject *t;
803 register PyObject **fastlocals, **freevars;
804 PyObject *retval = NULL; /* Return value */
805 PyThreadState *tstate = PyThreadState_GET();
806 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000807
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000812 is true when the line being executed has changed. The
813 initial values are such as to make this false the first
814 time it is tested. */
815 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000816
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 unsigned char *first_instr;
818 PyObject *names;
819 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000820#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 /* Make it easier to find out where we are with a debugger */
822 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000823#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000824
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825/* Computed GOTOs, or
826 the-optimization-commonly-but-improperly-known-as-"threaded code"
827 using gcc's labels-as-values extension
828 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
829
830 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000832 combined with a lookup table of jump addresses. However, since the
833 indirect jump instruction is shared by all opcodes, the CPU will have a
834 hard time making the right prediction for where to jump next (actually,
835 it will be always wrong except in the uncommon case of a sequence of
836 several identical opcodes).
837
838 "Threaded code" in contrast, uses an explicit jump table and an explicit
839 indirect jump instruction at the end of each opcode. Since the jump
840 instruction is at a different address for each opcode, the CPU will make a
841 separate prediction for each of these instructions, which is equivalent to
842 predicting the second opcode of each opcode pair. These predictions have
843 a much better chance to turn out valid, especially in small bytecode loops.
844
845 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847 and potentially many more instructions (depending on the pipeline width).
848 A correctly predicted branch, however, is nearly free.
849
850 At the time of this writing, the "threaded code" version is up to 15-20%
851 faster than the normal "switch" version, depending on the compiler and the
852 CPU architecture.
853
854 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
855 because it would render the measurements invalid.
856
857
858 NOTE: care must be taken that the compiler doesn't try to "optimize" the
859 indirect jumps by sharing them between all opcodes. Such optimizations
860 can be disabled on gcc by using the -fno-gcse flag (or possibly
861 -fno-crossjumping).
862*/
863
Antoine Pitrou042b1282010-08-13 21:15:58 +0000864#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000865#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000866#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000867#endif
868
Antoine Pitrou042b1282010-08-13 21:15:58 +0000869#ifdef HAVE_COMPUTED_GOTOS
870 #ifndef USE_COMPUTED_GOTOS
871 #define USE_COMPUTED_GOTOS 1
872 #endif
873#else
874 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
875 #error "Computed gotos are not supported on this compiler."
876 #endif
877 #undef USE_COMPUTED_GOTOS
878 #define USE_COMPUTED_GOTOS 0
879#endif
880
881#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000882/* Import the static jump table */
883#include "opcode_targets.h"
884
885/* This macro is used when several opcodes defer to the same implementation
886 (e.g. SETUP_LOOP, SETUP_FINALLY) */
887#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 TARGET_##op: \
889 opcode = op; \
890 if (HAS_ARG(op)) \
891 oparg = NEXTARG(); \
892 case op: \
893 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894
895#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 TARGET_##op: \
897 opcode = op; \
898 if (HAS_ARG(op)) \
899 oparg = NEXTARG(); \
900 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000901
902
903#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 { \
905 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
906 FAST_DISPATCH(); \
907 } \
908 continue; \
909 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000910
911#ifdef LLTRACE
912#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 { \
914 if (!lltrace && !_Py_TracingPossible) { \
915 f->f_lasti = INSTR_OFFSET(); \
916 goto *opcode_targets[*next_instr++]; \
917 } \
918 goto fast_next_opcode; \
919 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000920#else
921#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 { \
923 if (!_Py_TracingPossible) { \
924 f->f_lasti = INSTR_OFFSET(); \
925 goto *opcode_targets[*next_instr++]; \
926 } \
927 goto fast_next_opcode; \
928 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000929#endif
930
931#else
932#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000933 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000934#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000935 /* silence compiler warnings about `impl` unused */ \
936 if (0) goto impl; \
937 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000938#define DISPATCH() continue
939#define FAST_DISPATCH() goto fast_next_opcode
940#endif
941
942
Neal Norwitza81d2202002-07-14 00:27:26 +0000943/* Tuple access macros */
944
945#ifndef Py_DEBUG
946#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
947#else
948#define GETITEM(v, i) PyTuple_GetItem((v), (i))
949#endif
950
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000951#ifdef WITH_TSC
952/* Use Pentium timestamp counter to mark certain events:
953 inst0 -- beginning of switch statement for opcode dispatch
954 inst1 -- end of switch statement (may be skipped)
955 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000956 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000957 (may be skipped)
958 intr1 -- beginning of long interruption
959 intr2 -- end of long interruption
960
961 Many opcodes call out to helper C functions. In some cases, the
962 time in those functions should be counted towards the time for the
963 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
964 calls another Python function; there's no point in charge all the
965 bytecode executed by the called function to the caller.
966
967 It's hard to make a useful judgement statically. In the presence
968 of operator overloading, it's impossible to tell if a call will
969 execute new Python code or not.
970
971 It's a case-by-case judgement. I'll use intr1 for the following
972 cases:
973
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000974 IMPORT_STAR
975 IMPORT_FROM
976 CALL_FUNCTION (and friends)
977
978 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
980 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 READ_TIMESTAMP(inst0);
983 READ_TIMESTAMP(inst1);
984 READ_TIMESTAMP(loop0);
985 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 /* shut up the compiler */
988 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000989#endif
990
Guido van Rossum374a9221991-04-04 10:40:29 +0000991/* Code access macros */
992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993#define INSTR_OFFSET() ((int)(next_instr - first_instr))
994#define NEXTOP() (*next_instr++)
995#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
996#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
997#define JUMPTO(x) (next_instr = first_instr + (x))
998#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000999
Raymond Hettingerf606f872003-03-16 03:11:04 +00001000/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 Some opcodes tend to come in pairs thus making it possible to
1002 predict the second code when the first is run. For example,
1003 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
1004 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 Verifying the prediction costs a single high-speed test of a register
1007 variable against a constant. If the pairing was good, then the
1008 processor's own internal branch predication has a high likelihood of
1009 success, resulting in a nearly zero-overhead transition to the
1010 next opcode. A successful prediction saves a trip through the eval-loop
1011 including its two unpredictable branches, the HAS_ARG test and the
1012 switch-case. Combined with the processor's internal branch prediction,
1013 a successful PREDICT has the effect of making the two opcodes run as if
1014 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001015
Georg Brandl86b2fb92008-07-16 03:43:04 +00001016 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 predictions turned-on and interpret the results as if some opcodes
1018 had been combined or turn-off predictions so that the opcode frequency
1019 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001020
1021 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 the CPU to record separate branch prediction information for each
1023 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001024
Raymond Hettingerf606f872003-03-16 03:11:04 +00001025*/
1026
Antoine Pitrou042b1282010-08-13 21:15:58 +00001027#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028#define PREDICT(op) if (0) goto PRED_##op
1029#define PREDICTED(op) PRED_##op:
1030#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001031#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1033#define PREDICTED(op) PRED_##op: next_instr++
1034#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001035#endif
1036
Raymond Hettingerf606f872003-03-16 03:11:04 +00001037
Guido van Rossum374a9221991-04-04 10:40:29 +00001038/* Stack manipulation macros */
1039
Martin v. Löwis18e16552006-02-15 17:27:45 +00001040/* The stack can grow at most MAXINT deep, as co_nlocals and
1041 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001042#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1043#define EMPTY() (STACK_LEVEL() == 0)
1044#define TOP() (stack_pointer[-1])
1045#define SECOND() (stack_pointer[-2])
1046#define THIRD() (stack_pointer[-3])
1047#define FOURTH() (stack_pointer[-4])
1048#define PEEK(n) (stack_pointer[-(n)])
1049#define SET_TOP(v) (stack_pointer[-1] = (v))
1050#define SET_SECOND(v) (stack_pointer[-2] = (v))
1051#define SET_THIRD(v) (stack_pointer[-3] = (v))
1052#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1053#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1054#define BASIC_STACKADJ(n) (stack_pointer += n)
1055#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1056#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001057
Guido van Rossum96a42c81992-01-12 02:29:51 +00001058#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001060 lltrace && prtrace(TOP(), "push")); \
1061 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001063 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001065 lltrace && prtrace(TOP(), "stackadj")); \
1066 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001067#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001068 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1069 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001070#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001071#define PUSH(v) BASIC_PUSH(v)
1072#define POP() BASIC_POP()
1073#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001074#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001075#endif
1076
Guido van Rossum681d79a1995-07-18 14:51:37 +00001077/* Local variable macros */
1078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001080
1081/* The SETLOCAL() macro must not DECREF the local variable in-place and
1082 then store the new value; it must copy the old value to a temporary
1083 value, then store the new value, and then DECREF the temporary value.
1084 This is because it is possible that during the DECREF the frame is
1085 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1086 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001088 GETLOCAL(i) = value; \
1089 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001090
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001091
1092#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 while (STACK_LEVEL() > (b)->b_level) { \
1094 PyObject *v = POP(); \
1095 Py_XDECREF(v); \
1096 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001097
1098#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 { \
1100 PyObject *type, *value, *traceback; \
1101 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1102 while (STACK_LEVEL() > (b)->b_level + 3) { \
1103 value = POP(); \
1104 Py_XDECREF(value); \
1105 } \
1106 type = tstate->exc_type; \
1107 value = tstate->exc_value; \
1108 traceback = tstate->exc_traceback; \
1109 tstate->exc_type = POP(); \
1110 tstate->exc_value = POP(); \
1111 tstate->exc_traceback = POP(); \
1112 Py_XDECREF(type); \
1113 Py_XDECREF(value); \
1114 Py_XDECREF(traceback); \
1115 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001116
1117#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 { \
1119 PyObject *type, *value, *traceback; \
1120 Py_XINCREF(tstate->exc_type); \
1121 Py_XINCREF(tstate->exc_value); \
1122 Py_XINCREF(tstate->exc_traceback); \
1123 type = f->f_exc_type; \
1124 value = f->f_exc_value; \
1125 traceback = f->f_exc_traceback; \
1126 f->f_exc_type = tstate->exc_type; \
1127 f->f_exc_value = tstate->exc_value; \
1128 f->f_exc_traceback = tstate->exc_traceback; \
1129 Py_XDECREF(type); \
1130 Py_XDECREF(value); \
1131 Py_XDECREF(traceback); \
1132 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001133
1134#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 { \
1136 PyObject *tmp; \
1137 tmp = tstate->exc_type; \
1138 tstate->exc_type = f->f_exc_type; \
1139 f->f_exc_type = tmp; \
1140 tmp = tstate->exc_value; \
1141 tstate->exc_value = f->f_exc_value; \
1142 f->f_exc_value = tmp; \
1143 tmp = tstate->exc_traceback; \
1144 tstate->exc_traceback = f->f_exc_traceback; \
1145 f->f_exc_traceback = tmp; \
1146 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001147
Guido van Rossuma027efa1997-05-05 20:56:21 +00001148/* Start of code */
1149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 if (f == NULL)
1151 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001152
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 /* push frame */
1154 if (Py_EnterRecursiveCall(""))
1155 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001157 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 if (tstate->use_tracing) {
1160 if (tstate->c_tracefunc != NULL) {
1161 /* tstate->c_tracefunc, if defined, is a
1162 function that will be called on *every* entry
1163 to a code block. Its return value, if not
1164 None, is a function that will be called at
1165 the start of each executed line of code.
1166 (Actually, the function must return itself
1167 in order to continue tracing.) The trace
1168 functions are called with three arguments:
1169 a pointer to the current frame, a string
1170 indicating why the function is called, and
1171 an argument which depends on the situation.
1172 The global trace function is also called
1173 whenever an exception is detected. */
1174 if (call_trace_protected(tstate->c_tracefunc,
1175 tstate->c_traceobj,
1176 f, PyTrace_CALL, Py_None)) {
1177 /* Trace function raised an error */
1178 goto exit_eval_frame;
1179 }
1180 }
1181 if (tstate->c_profilefunc != NULL) {
1182 /* Similar for c_profilefunc, except it needn't
1183 return itself and isn't called for "line" events */
1184 if (call_trace_protected(tstate->c_profilefunc,
1185 tstate->c_profileobj,
1186 f, PyTrace_CALL, Py_None)) {
1187 /* Profile function raised an error */
1188 goto exit_eval_frame;
1189 }
1190 }
1191 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001192
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 co = f->f_code;
1194 names = co->co_names;
1195 consts = co->co_consts;
1196 fastlocals = f->f_localsplus;
1197 freevars = f->f_localsplus + co->co_nlocals;
1198 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1199 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 f->f_lasti now refers to the index of the last instruction
1202 executed. You might think this was obvious from the name, but
1203 this wasn't always true before 2.3! PyFrame_New now sets
1204 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1205 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1206 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001207
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 When the PREDICT() macros are enabled, some opcode pairs follow in
1209 direct succession without updating f->f_lasti. A successful
1210 prediction effectively links the two codes together as if they
1211 were a single new opcode; accordingly,f->f_lasti will point to
1212 the first code in the pair (for instance, GET_ITER followed by
1213 FOR_ITER is effectively a single opcode and f->f_lasti will point
1214 at to the beginning of the combined pair.)
1215 */
1216 next_instr = first_instr + f->f_lasti + 1;
1217 stack_pointer = f->f_stacktop;
1218 assert(stack_pointer != NULL);
1219 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001221 if (co->co_flags & CO_GENERATOR && !throwflag) {
1222 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1223 /* We were in an except handler when we left,
1224 restore the exception state which was put aside
1225 (see YIELD_VALUE). */
1226 SWAP_EXC_STATE();
1227 }
1228 else {
1229 SAVE_EXC_STATE();
1230 }
1231 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001232
Tim Peters5ca576e2001-06-18 22:08:13 +00001233#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001234 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001235#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001236#if defined(Py_DEBUG) || defined(LLTRACE)
Victor Stinner4a3733d2010-08-17 00:39:57 +00001237 {
1238 PyObject *error_type, *error_value, *error_traceback;
1239 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1240 filename = _PyUnicode_AsString(co->co_filename);
Victor Stinnera0006452010-10-13 10:48:55 +00001241 if (filename == NULL && tstate->overflowed) {
1242 /* maximum recursion depth exceeded */
1243 goto exit_eval_frame;
1244 }
Victor Stinner4a3733d2010-08-17 00:39:57 +00001245 PyErr_Restore(error_type, error_value, error_traceback);
1246 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001247#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 why = WHY_NOT;
1250 err = 0;
1251 x = Py_None; /* Not a reference, just anything non-NULL */
1252 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 if (throwflag) { /* support for generator.throw() */
1255 why = WHY_EXCEPTION;
1256 goto on_error;
1257 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001260#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 if (inst1 == 0) {
1262 /* Almost surely, the opcode executed a break
1263 or a continue, preventing inst1 from being set
1264 on the way out of the loop.
1265 */
1266 READ_TIMESTAMP(inst1);
1267 loop1 = inst1;
1268 }
1269 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1270 intr0, intr1);
1271 ticked = 0;
1272 inst1 = 0;
1273 intr0 = 0;
1274 intr1 = 0;
1275 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001276#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001277 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1278 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001279
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 /* Do periodic things. Doing this every time through
1281 the loop would add too much overhead, so we do it
1282 only every Nth instruction. We also do it if
1283 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1284 event needs attention (e.g. a signal handler or
1285 async I/O handler); see Py_AddPendingCall() and
1286 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001288 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1289 if (*next_instr == SETUP_FINALLY) {
1290 /* Make the last opcode before
Ezio Melotti13925002011-03-16 11:05:33 +02001291 a try: finally: block uninterruptible. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 goto fast_next_opcode;
1293 }
1294 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001295#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001297#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1299 if (Py_MakePendingCalls() < 0) {
1300 why = WHY_EXCEPTION;
1301 goto on_error;
1302 }
1303 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001304#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001305 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 /* Give another thread a chance */
1307 if (PyThreadState_Swap(NULL) != tstate)
1308 Py_FatalError("ceval: tstate mix-up");
1309 drop_gil(tstate);
1310
1311 /* Other threads may run now */
1312
1313 take_gil(tstate);
1314 if (PyThreadState_Swap(tstate) != NULL)
1315 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001317#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 /* Check for asynchronous exceptions. */
1319 if (tstate->async_exc != NULL) {
1320 x = tstate->async_exc;
1321 tstate->async_exc = NULL;
1322 UNSIGNAL_ASYNC_EXC();
1323 PyErr_SetNone(x);
1324 Py_DECREF(x);
1325 why = WHY_EXCEPTION;
1326 goto on_error;
1327 }
1328 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 fast_next_opcode:
1331 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 if (_Py_TracingPossible &&
1336 tstate->c_tracefunc != NULL && !tstate->tracing) {
1337 /* see maybe_call_line_trace
1338 for expository comments */
1339 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001341 err = maybe_call_line_trace(tstate->c_tracefunc,
1342 tstate->c_traceobj,
1343 f, &instr_lb, &instr_ub,
1344 &instr_prev);
1345 /* Reload possibly changed frame fields */
1346 JUMPTO(f->f_lasti);
1347 if (f->f_stacktop != NULL) {
1348 stack_pointer = f->f_stacktop;
1349 f->f_stacktop = NULL;
1350 }
1351 if (err) {
1352 /* trace function raised an exception */
1353 goto on_error;
1354 }
1355 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001356
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001357 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001359 opcode = NEXTOP();
1360 oparg = 0; /* allows oparg to be stored in a register because
1361 it doesn't have to be remembered across a full loop */
1362 if (HAS_ARG(opcode))
1363 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001364 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001365#ifdef DYNAMIC_EXECUTION_PROFILE
1366#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 dxpairs[lastopcode][opcode]++;
1368 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001369#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001371#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001372
Guido van Rossum96a42c81992-01-12 02:29:51 +00001373#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (lltrace) {
1377 if (HAS_ARG(opcode)) {
1378 printf("%d: %d, %d\n",
1379 f->f_lasti, opcode, oparg);
1380 }
1381 else {
1382 printf("%d: %d\n",
1383 f->f_lasti, opcode);
1384 }
1385 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001386#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001388 /* Main switch on opcode */
1389 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 /* BEWARE!
1394 It is essential that any operation that fails sets either
1395 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1396 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001400 TARGET(NOP)
1401 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 TARGET(LOAD_FAST)
1404 x = GETLOCAL(oparg);
1405 if (x != NULL) {
1406 Py_INCREF(x);
1407 PUSH(x);
1408 FAST_DISPATCH();
1409 }
1410 format_exc_check_arg(PyExc_UnboundLocalError,
1411 UNBOUNDLOCAL_ERROR_MSG,
1412 PyTuple_GetItem(co->co_varnames, oparg));
1413 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 TARGET(LOAD_CONST)
1416 x = GETITEM(consts, oparg);
1417 Py_INCREF(x);
1418 PUSH(x);
1419 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 PREDICTED_WITH_ARG(STORE_FAST);
1422 TARGET(STORE_FAST)
1423 v = POP();
1424 SETLOCAL(oparg, v);
1425 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 TARGET(POP_TOP)
1428 v = POP();
1429 Py_DECREF(v);
1430 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 TARGET(ROT_TWO)
1433 v = TOP();
1434 w = SECOND();
1435 SET_TOP(w);
1436 SET_SECOND(v);
1437 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 TARGET(ROT_THREE)
1440 v = TOP();
1441 w = SECOND();
1442 x = THIRD();
1443 SET_TOP(w);
1444 SET_SECOND(x);
1445 SET_THIRD(v);
1446 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 TARGET(DUP_TOP)
1449 v = TOP();
1450 Py_INCREF(v);
1451 PUSH(v);
1452 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001454 TARGET(DUP_TOP_TWO)
1455 x = TOP();
1456 Py_INCREF(x);
1457 w = SECOND();
1458 Py_INCREF(w);
1459 STACKADJ(2);
1460 SET_TOP(x);
1461 SET_SECOND(w);
1462 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 TARGET(UNARY_POSITIVE)
1465 v = TOP();
1466 x = PyNumber_Positive(v);
1467 Py_DECREF(v);
1468 SET_TOP(x);
1469 if (x != NULL) DISPATCH();
1470 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 TARGET(UNARY_NEGATIVE)
1473 v = TOP();
1474 x = PyNumber_Negative(v);
1475 Py_DECREF(v);
1476 SET_TOP(x);
1477 if (x != NULL) DISPATCH();
1478 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001480 TARGET(UNARY_NOT)
1481 v = TOP();
1482 err = PyObject_IsTrue(v);
1483 Py_DECREF(v);
1484 if (err == 0) {
1485 Py_INCREF(Py_True);
1486 SET_TOP(Py_True);
1487 DISPATCH();
1488 }
1489 else if (err > 0) {
1490 Py_INCREF(Py_False);
1491 SET_TOP(Py_False);
1492 err = 0;
1493 DISPATCH();
1494 }
1495 STACKADJ(-1);
1496 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 TARGET(UNARY_INVERT)
1499 v = TOP();
1500 x = PyNumber_Invert(v);
1501 Py_DECREF(v);
1502 SET_TOP(x);
1503 if (x != NULL) DISPATCH();
1504 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001506 TARGET(BINARY_POWER)
1507 w = POP();
1508 v = TOP();
1509 x = PyNumber_Power(v, w, Py_None);
1510 Py_DECREF(v);
1511 Py_DECREF(w);
1512 SET_TOP(x);
1513 if (x != NULL) DISPATCH();
1514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 TARGET(BINARY_MULTIPLY)
1517 w = POP();
1518 v = TOP();
1519 x = PyNumber_Multiply(v, w);
1520 Py_DECREF(v);
1521 Py_DECREF(w);
1522 SET_TOP(x);
1523 if (x != NULL) DISPATCH();
1524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 TARGET(BINARY_TRUE_DIVIDE)
1527 w = POP();
1528 v = TOP();
1529 x = PyNumber_TrueDivide(v, w);
1530 Py_DECREF(v);
1531 Py_DECREF(w);
1532 SET_TOP(x);
1533 if (x != NULL) DISPATCH();
1534 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 TARGET(BINARY_FLOOR_DIVIDE)
1537 w = POP();
1538 v = TOP();
1539 x = PyNumber_FloorDivide(v, w);
1540 Py_DECREF(v);
1541 Py_DECREF(w);
1542 SET_TOP(x);
1543 if (x != NULL) DISPATCH();
1544 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 TARGET(BINARY_MODULO)
1547 w = POP();
1548 v = TOP();
1549 if (PyUnicode_CheckExact(v))
1550 x = PyUnicode_Format(v, w);
1551 else
1552 x = PyNumber_Remainder(v, w);
1553 Py_DECREF(v);
1554 Py_DECREF(w);
1555 SET_TOP(x);
1556 if (x != NULL) DISPATCH();
1557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 TARGET(BINARY_ADD)
1560 w = POP();
1561 v = TOP();
1562 if (PyUnicode_CheckExact(v) &&
1563 PyUnicode_CheckExact(w)) {
1564 x = unicode_concatenate(v, w, f, next_instr);
1565 /* unicode_concatenate consumed the ref to v */
1566 goto skip_decref_vx;
1567 }
1568 else {
1569 x = PyNumber_Add(v, w);
1570 }
1571 Py_DECREF(v);
1572 skip_decref_vx:
1573 Py_DECREF(w);
1574 SET_TOP(x);
1575 if (x != NULL) DISPATCH();
1576 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001578 TARGET(BINARY_SUBTRACT)
1579 w = POP();
1580 v = TOP();
1581 x = PyNumber_Subtract(v, w);
1582 Py_DECREF(v);
1583 Py_DECREF(w);
1584 SET_TOP(x);
1585 if (x != NULL) DISPATCH();
1586 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 TARGET(BINARY_SUBSCR)
1589 w = POP();
1590 v = TOP();
1591 x = PyObject_GetItem(v, w);
1592 Py_DECREF(v);
1593 Py_DECREF(w);
1594 SET_TOP(x);
1595 if (x != NULL) DISPATCH();
1596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 TARGET(BINARY_LSHIFT)
1599 w = POP();
1600 v = TOP();
1601 x = PyNumber_Lshift(v, w);
1602 Py_DECREF(v);
1603 Py_DECREF(w);
1604 SET_TOP(x);
1605 if (x != NULL) DISPATCH();
1606 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 TARGET(BINARY_RSHIFT)
1609 w = POP();
1610 v = TOP();
1611 x = PyNumber_Rshift(v, w);
1612 Py_DECREF(v);
1613 Py_DECREF(w);
1614 SET_TOP(x);
1615 if (x != NULL) DISPATCH();
1616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 TARGET(BINARY_AND)
1619 w = POP();
1620 v = TOP();
1621 x = PyNumber_And(v, w);
1622 Py_DECREF(v);
1623 Py_DECREF(w);
1624 SET_TOP(x);
1625 if (x != NULL) DISPATCH();
1626 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001627
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 TARGET(BINARY_XOR)
1629 w = POP();
1630 v = TOP();
1631 x = PyNumber_Xor(v, w);
1632 Py_DECREF(v);
1633 Py_DECREF(w);
1634 SET_TOP(x);
1635 if (x != NULL) DISPATCH();
1636 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 TARGET(BINARY_OR)
1639 w = POP();
1640 v = TOP();
1641 x = PyNumber_Or(v, w);
1642 Py_DECREF(v);
1643 Py_DECREF(w);
1644 SET_TOP(x);
1645 if (x != NULL) DISPATCH();
1646 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001647
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 TARGET(LIST_APPEND)
1649 w = POP();
1650 v = PEEK(oparg);
1651 err = PyList_Append(v, w);
1652 Py_DECREF(w);
1653 if (err == 0) {
1654 PREDICT(JUMP_ABSOLUTE);
1655 DISPATCH();
1656 }
1657 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001658
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 TARGET(SET_ADD)
1660 w = POP();
1661 v = stack_pointer[-oparg];
1662 err = PySet_Add(v, w);
1663 Py_DECREF(w);
1664 if (err == 0) {
1665 PREDICT(JUMP_ABSOLUTE);
1666 DISPATCH();
1667 }
1668 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 TARGET(INPLACE_POWER)
1671 w = POP();
1672 v = TOP();
1673 x = PyNumber_InPlacePower(v, w, Py_None);
1674 Py_DECREF(v);
1675 Py_DECREF(w);
1676 SET_TOP(x);
1677 if (x != NULL) DISPATCH();
1678 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001679
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 TARGET(INPLACE_MULTIPLY)
1681 w = POP();
1682 v = TOP();
1683 x = PyNumber_InPlaceMultiply(v, w);
1684 Py_DECREF(v);
1685 Py_DECREF(w);
1686 SET_TOP(x);
1687 if (x != NULL) DISPATCH();
1688 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 TARGET(INPLACE_TRUE_DIVIDE)
1691 w = POP();
1692 v = TOP();
1693 x = PyNumber_InPlaceTrueDivide(v, w);
1694 Py_DECREF(v);
1695 Py_DECREF(w);
1696 SET_TOP(x);
1697 if (x != NULL) DISPATCH();
1698 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 TARGET(INPLACE_FLOOR_DIVIDE)
1701 w = POP();
1702 v = TOP();
1703 x = PyNumber_InPlaceFloorDivide(v, w);
1704 Py_DECREF(v);
1705 Py_DECREF(w);
1706 SET_TOP(x);
1707 if (x != NULL) DISPATCH();
1708 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 TARGET(INPLACE_MODULO)
1711 w = POP();
1712 v = TOP();
1713 x = PyNumber_InPlaceRemainder(v, w);
1714 Py_DECREF(v);
1715 Py_DECREF(w);
1716 SET_TOP(x);
1717 if (x != NULL) DISPATCH();
1718 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 TARGET(INPLACE_ADD)
1721 w = POP();
1722 v = TOP();
1723 if (PyUnicode_CheckExact(v) &&
1724 PyUnicode_CheckExact(w)) {
1725 x = unicode_concatenate(v, w, f, next_instr);
1726 /* unicode_concatenate consumed the ref to v */
1727 goto skip_decref_v;
1728 }
1729 else {
1730 x = PyNumber_InPlaceAdd(v, w);
1731 }
1732 Py_DECREF(v);
1733 skip_decref_v:
1734 Py_DECREF(w);
1735 SET_TOP(x);
1736 if (x != NULL) DISPATCH();
1737 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 TARGET(INPLACE_SUBTRACT)
1740 w = POP();
1741 v = TOP();
1742 x = PyNumber_InPlaceSubtract(v, w);
1743 Py_DECREF(v);
1744 Py_DECREF(w);
1745 SET_TOP(x);
1746 if (x != NULL) DISPATCH();
1747 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749 TARGET(INPLACE_LSHIFT)
1750 w = POP();
1751 v = TOP();
1752 x = PyNumber_InPlaceLshift(v, w);
1753 Py_DECREF(v);
1754 Py_DECREF(w);
1755 SET_TOP(x);
1756 if (x != NULL) DISPATCH();
1757 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 TARGET(INPLACE_RSHIFT)
1760 w = POP();
1761 v = TOP();
1762 x = PyNumber_InPlaceRshift(v, w);
1763 Py_DECREF(v);
1764 Py_DECREF(w);
1765 SET_TOP(x);
1766 if (x != NULL) DISPATCH();
1767 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 TARGET(INPLACE_AND)
1770 w = POP();
1771 v = TOP();
1772 x = PyNumber_InPlaceAnd(v, w);
1773 Py_DECREF(v);
1774 Py_DECREF(w);
1775 SET_TOP(x);
1776 if (x != NULL) DISPATCH();
1777 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001778
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 TARGET(INPLACE_XOR)
1780 w = POP();
1781 v = TOP();
1782 x = PyNumber_InPlaceXor(v, w);
1783 Py_DECREF(v);
1784 Py_DECREF(w);
1785 SET_TOP(x);
1786 if (x != NULL) DISPATCH();
1787 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 TARGET(INPLACE_OR)
1790 w = POP();
1791 v = TOP();
1792 x = PyNumber_InPlaceOr(v, w);
1793 Py_DECREF(v);
1794 Py_DECREF(w);
1795 SET_TOP(x);
1796 if (x != NULL) DISPATCH();
1797 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 TARGET(STORE_SUBSCR)
1800 w = TOP();
1801 v = SECOND();
1802 u = THIRD();
1803 STACKADJ(-3);
1804 /* v[w] = u */
1805 err = PyObject_SetItem(v, w, u);
1806 Py_DECREF(u);
1807 Py_DECREF(v);
1808 Py_DECREF(w);
1809 if (err == 0) DISPATCH();
1810 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 TARGET(DELETE_SUBSCR)
1813 w = TOP();
1814 v = SECOND();
1815 STACKADJ(-2);
1816 /* del v[w] */
1817 err = PyObject_DelItem(v, w);
1818 Py_DECREF(v);
1819 Py_DECREF(w);
1820 if (err == 0) DISPATCH();
1821 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 TARGET(PRINT_EXPR)
1824 v = POP();
1825 w = PySys_GetObject("displayhook");
1826 if (w == NULL) {
1827 PyErr_SetString(PyExc_RuntimeError,
1828 "lost sys.displayhook");
1829 err = -1;
1830 x = NULL;
1831 }
1832 if (err == 0) {
1833 x = PyTuple_Pack(1, v);
1834 if (x == NULL)
1835 err = -1;
1836 }
1837 if (err == 0) {
1838 w = PyEval_CallObject(w, x);
1839 Py_XDECREF(w);
1840 if (w == NULL)
1841 err = -1;
1842 }
1843 Py_DECREF(v);
1844 Py_XDECREF(x);
1845 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001846
Thomas Wouters434d0822000-08-24 20:11:32 +00001847#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001849#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 TARGET(RAISE_VARARGS)
1851 v = w = NULL;
1852 switch (oparg) {
1853 case 2:
1854 v = POP(); /* cause */
1855 case 1:
1856 w = POP(); /* exc */
1857 case 0: /* Fallthrough */
1858 why = do_raise(w, v);
1859 break;
1860 default:
1861 PyErr_SetString(PyExc_SystemError,
1862 "bad RAISE_VARARGS oparg");
1863 why = WHY_EXCEPTION;
1864 break;
1865 }
1866 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 TARGET(STORE_LOCALS)
1869 x = POP();
1870 v = f->f_locals;
1871 Py_XDECREF(v);
1872 f->f_locals = x;
1873 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 TARGET(RETURN_VALUE)
1876 retval = POP();
1877 why = WHY_RETURN;
1878 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 TARGET(YIELD_VALUE)
1881 retval = POP();
1882 f->f_stacktop = stack_pointer;
1883 why = WHY_YIELD;
1884 /* Put aside the current exception state and restore
1885 that of the calling frame. This only serves when
1886 "yield" is used inside an except handler. */
1887 SWAP_EXC_STATE();
1888 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 TARGET(POP_EXCEPT)
1891 {
1892 PyTryBlock *b = PyFrame_BlockPop(f);
1893 if (b->b_type != EXCEPT_HANDLER) {
1894 PyErr_SetString(PyExc_SystemError,
1895 "popped block is not an except handler");
1896 why = WHY_EXCEPTION;
1897 break;
1898 }
1899 UNWIND_EXCEPT_HANDLER(b);
1900 }
1901 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 TARGET(POP_BLOCK)
1904 {
1905 PyTryBlock *b = PyFrame_BlockPop(f);
1906 UNWIND_BLOCK(b);
1907 }
1908 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 PREDICTED(END_FINALLY);
1911 TARGET(END_FINALLY)
1912 v = POP();
1913 if (PyLong_Check(v)) {
1914 why = (enum why_code) PyLong_AS_LONG(v);
1915 assert(why != WHY_YIELD);
1916 if (why == WHY_RETURN ||
1917 why == WHY_CONTINUE)
1918 retval = POP();
1919 if (why == WHY_SILENCED) {
1920 /* An exception was silenced by 'with', we must
1921 manually unwind the EXCEPT_HANDLER block which was
1922 created when the exception was caught, otherwise
1923 the stack will be in an inconsistent state. */
1924 PyTryBlock *b = PyFrame_BlockPop(f);
1925 assert(b->b_type == EXCEPT_HANDLER);
1926 UNWIND_EXCEPT_HANDLER(b);
1927 why = WHY_NOT;
1928 }
1929 }
1930 else if (PyExceptionClass_Check(v)) {
1931 w = POP();
1932 u = POP();
1933 PyErr_Restore(v, w, u);
1934 why = WHY_RERAISE;
1935 break;
1936 }
1937 else if (v != Py_None) {
1938 PyErr_SetString(PyExc_SystemError,
1939 "'finally' pops bad exception");
1940 why = WHY_EXCEPTION;
1941 }
1942 Py_DECREF(v);
1943 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 TARGET(LOAD_BUILD_CLASS)
1946 x = PyDict_GetItemString(f->f_builtins,
1947 "__build_class__");
1948 if (x == NULL) {
1949 PyErr_SetString(PyExc_ImportError,
1950 "__build_class__ not found");
1951 break;
1952 }
1953 Py_INCREF(x);
1954 PUSH(x);
1955 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 TARGET(STORE_NAME)
1958 w = GETITEM(names, oparg);
1959 v = POP();
1960 if ((x = f->f_locals) != NULL) {
1961 if (PyDict_CheckExact(x))
1962 err = PyDict_SetItem(x, w, v);
1963 else
1964 err = PyObject_SetItem(x, w, v);
1965 Py_DECREF(v);
1966 if (err == 0) DISPATCH();
1967 break;
1968 }
1969 PyErr_Format(PyExc_SystemError,
1970 "no locals found when storing %R", w);
1971 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 TARGET(DELETE_NAME)
1974 w = GETITEM(names, oparg);
1975 if ((x = f->f_locals) != NULL) {
1976 if ((err = PyObject_DelItem(x, w)) != 0)
1977 format_exc_check_arg(PyExc_NameError,
1978 NAME_ERROR_MSG,
1979 w);
1980 break;
1981 }
1982 PyErr_Format(PyExc_SystemError,
1983 "no locals when deleting %R", w);
1984 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001986 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1987 TARGET(UNPACK_SEQUENCE)
1988 v = POP();
1989 if (PyTuple_CheckExact(v) &&
1990 PyTuple_GET_SIZE(v) == oparg) {
1991 PyObject **items = \
1992 ((PyTupleObject *)v)->ob_item;
1993 while (oparg--) {
1994 w = items[oparg];
1995 Py_INCREF(w);
1996 PUSH(w);
1997 }
1998 Py_DECREF(v);
1999 DISPATCH();
2000 } else if (PyList_CheckExact(v) &&
2001 PyList_GET_SIZE(v) == oparg) {
2002 PyObject **items = \
2003 ((PyListObject *)v)->ob_item;
2004 while (oparg--) {
2005 w = items[oparg];
2006 Py_INCREF(w);
2007 PUSH(w);
2008 }
2009 } else if (unpack_iterable(v, oparg, -1,
2010 stack_pointer + oparg)) {
2011 STACKADJ(oparg);
2012 } else {
2013 /* unpack_iterable() raised an exception */
2014 why = WHY_EXCEPTION;
2015 }
2016 Py_DECREF(v);
2017 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002019 TARGET(UNPACK_EX)
2020 {
2021 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2022 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2025 stack_pointer + totalargs)) {
2026 stack_pointer += totalargs;
2027 } else {
2028 why = WHY_EXCEPTION;
2029 }
2030 Py_DECREF(v);
2031 break;
2032 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002033
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002034 TARGET(STORE_ATTR)
2035 w = GETITEM(names, oparg);
2036 v = TOP();
2037 u = SECOND();
2038 STACKADJ(-2);
2039 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2040 Py_DECREF(v);
2041 Py_DECREF(u);
2042 if (err == 0) DISPATCH();
2043 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 TARGET(DELETE_ATTR)
2046 w = GETITEM(names, oparg);
2047 v = POP();
2048 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2049 /* del v.w */
2050 Py_DECREF(v);
2051 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 TARGET(STORE_GLOBAL)
2054 w = GETITEM(names, oparg);
2055 v = POP();
2056 err = PyDict_SetItem(f->f_globals, w, v);
2057 Py_DECREF(v);
2058 if (err == 0) DISPATCH();
2059 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 TARGET(DELETE_GLOBAL)
2062 w = GETITEM(names, oparg);
2063 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2064 format_exc_check_arg(
2065 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2066 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002068 TARGET(LOAD_NAME)
2069 w = GETITEM(names, oparg);
2070 if ((v = f->f_locals) == NULL) {
2071 PyErr_Format(PyExc_SystemError,
2072 "no locals when loading %R", w);
2073 why = WHY_EXCEPTION;
2074 break;
2075 }
2076 if (PyDict_CheckExact(v)) {
2077 x = PyDict_GetItem(v, w);
2078 Py_XINCREF(x);
2079 }
2080 else {
2081 x = PyObject_GetItem(v, w);
2082 if (x == NULL && PyErr_Occurred()) {
2083 if (!PyErr_ExceptionMatches(
2084 PyExc_KeyError))
2085 break;
2086 PyErr_Clear();
2087 }
2088 }
2089 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002090 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002092 x = PyDict_GetItem(f->f_builtins, w);
2093 if (x == NULL) {
2094 format_exc_check_arg(
2095 PyExc_NameError,
2096 NAME_ERROR_MSG, w);
2097 break;
2098 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002099 }
2100 Py_INCREF(x);
2101 }
2102 PUSH(x);
2103 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 TARGET(LOAD_GLOBAL)
2106 w = GETITEM(names, oparg);
2107 if (PyUnicode_CheckExact(w)) {
2108 /* Inline the PyDict_GetItem() calls.
2109 WARNING: this is an extreme speed hack.
2110 Do not try this at home. */
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002111 Py_hash_t hash = ((PyUnicodeObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 if (hash != -1) {
2113 PyDictObject *d;
2114 PyDictEntry *e;
2115 d = (PyDictObject *)(f->f_globals);
2116 e = d->ma_lookup(d, w, hash);
2117 if (e == NULL) {
2118 x = NULL;
2119 break;
2120 }
2121 x = e->me_value;
2122 if (x != NULL) {
2123 Py_INCREF(x);
2124 PUSH(x);
2125 DISPATCH();
2126 }
2127 d = (PyDictObject *)(f->f_builtins);
2128 e = d->ma_lookup(d, w, hash);
2129 if (e == NULL) {
2130 x = NULL;
2131 break;
2132 }
2133 x = e->me_value;
2134 if (x != NULL) {
2135 Py_INCREF(x);
2136 PUSH(x);
2137 DISPATCH();
2138 }
2139 goto load_global_error;
2140 }
2141 }
2142 /* This is the un-inlined version of the code above */
2143 x = PyDict_GetItem(f->f_globals, w);
2144 if (x == NULL) {
2145 x = PyDict_GetItem(f->f_builtins, w);
2146 if (x == NULL) {
2147 load_global_error:
2148 format_exc_check_arg(
2149 PyExc_NameError,
2150 GLOBAL_NAME_ERROR_MSG, w);
2151 break;
2152 }
2153 }
2154 Py_INCREF(x);
2155 PUSH(x);
2156 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002157
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 TARGET(DELETE_FAST)
2159 x = GETLOCAL(oparg);
2160 if (x != NULL) {
2161 SETLOCAL(oparg, NULL);
2162 DISPATCH();
2163 }
2164 format_exc_check_arg(
2165 PyExc_UnboundLocalError,
2166 UNBOUNDLOCAL_ERROR_MSG,
2167 PyTuple_GetItem(co->co_varnames, oparg)
2168 );
2169 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002170
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002171 TARGET(DELETE_DEREF)
2172 x = freevars[oparg];
2173 if (PyCell_GET(x) != NULL) {
2174 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002175 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002176 }
2177 err = -1;
2178 format_exc_unbound(co, oparg);
2179 break;
2180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 TARGET(LOAD_CLOSURE)
2182 x = freevars[oparg];
2183 Py_INCREF(x);
2184 PUSH(x);
2185 if (x != NULL) DISPATCH();
2186 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002188 TARGET(LOAD_DEREF)
2189 x = freevars[oparg];
2190 w = PyCell_Get(x);
2191 if (w != NULL) {
2192 PUSH(w);
2193 DISPATCH();
2194 }
2195 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002196 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 TARGET(STORE_DEREF)
2200 w = POP();
2201 x = freevars[oparg];
2202 PyCell_Set(x, w);
2203 Py_DECREF(w);
2204 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 TARGET(BUILD_TUPLE)
2207 x = PyTuple_New(oparg);
2208 if (x != NULL) {
2209 for (; --oparg >= 0;) {
2210 w = POP();
2211 PyTuple_SET_ITEM(x, oparg, w);
2212 }
2213 PUSH(x);
2214 DISPATCH();
2215 }
2216 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002217
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 TARGET(BUILD_LIST)
2219 x = PyList_New(oparg);
2220 if (x != NULL) {
2221 for (; --oparg >= 0;) {
2222 w = POP();
2223 PyList_SET_ITEM(x, oparg, w);
2224 }
2225 PUSH(x);
2226 DISPATCH();
2227 }
2228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 TARGET(BUILD_SET)
2231 x = PySet_New(NULL);
2232 if (x != NULL) {
2233 for (; --oparg >= 0;) {
2234 w = POP();
2235 if (err == 0)
2236 err = PySet_Add(x, w);
2237 Py_DECREF(w);
2238 }
2239 if (err != 0) {
2240 Py_DECREF(x);
2241 break;
2242 }
2243 PUSH(x);
2244 DISPATCH();
2245 }
2246 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002248 TARGET(BUILD_MAP)
2249 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2250 PUSH(x);
2251 if (x != NULL) DISPATCH();
2252 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002254 TARGET(STORE_MAP)
2255 w = TOP(); /* key */
2256 u = SECOND(); /* value */
2257 v = THIRD(); /* dict */
2258 STACKADJ(-2);
2259 assert (PyDict_CheckExact(v));
2260 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2261 Py_DECREF(u);
2262 Py_DECREF(w);
2263 if (err == 0) DISPATCH();
2264 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 TARGET(MAP_ADD)
2267 w = TOP(); /* key */
2268 u = SECOND(); /* value */
2269 STACKADJ(-2);
2270 v = stack_pointer[-oparg]; /* dict */
2271 assert (PyDict_CheckExact(v));
2272 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2273 Py_DECREF(u);
2274 Py_DECREF(w);
2275 if (err == 0) {
2276 PREDICT(JUMP_ABSOLUTE);
2277 DISPATCH();
2278 }
2279 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 TARGET(LOAD_ATTR)
2282 w = GETITEM(names, oparg);
2283 v = TOP();
2284 x = PyObject_GetAttr(v, w);
2285 Py_DECREF(v);
2286 SET_TOP(x);
2287 if (x != NULL) DISPATCH();
2288 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002290 TARGET(COMPARE_OP)
2291 w = POP();
2292 v = TOP();
2293 x = cmp_outcome(oparg, v, w);
2294 Py_DECREF(v);
2295 Py_DECREF(w);
2296 SET_TOP(x);
2297 if (x == NULL) break;
2298 PREDICT(POP_JUMP_IF_FALSE);
2299 PREDICT(POP_JUMP_IF_TRUE);
2300 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002302 TARGET(IMPORT_NAME)
2303 w = GETITEM(names, oparg);
2304 x = PyDict_GetItemString(f->f_builtins, "__import__");
2305 if (x == NULL) {
2306 PyErr_SetString(PyExc_ImportError,
2307 "__import__ not found");
2308 break;
2309 }
2310 Py_INCREF(x);
2311 v = POP();
2312 u = TOP();
2313 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2314 w = PyTuple_Pack(5,
2315 w,
2316 f->f_globals,
2317 f->f_locals == NULL ?
2318 Py_None : f->f_locals,
2319 v,
2320 u);
2321 else
2322 w = PyTuple_Pack(4,
2323 w,
2324 f->f_globals,
2325 f->f_locals == NULL ?
2326 Py_None : f->f_locals,
2327 v);
2328 Py_DECREF(v);
2329 Py_DECREF(u);
2330 if (w == NULL) {
2331 u = POP();
2332 Py_DECREF(x);
2333 x = NULL;
2334 break;
2335 }
2336 READ_TIMESTAMP(intr0);
2337 v = x;
2338 x = PyEval_CallObject(v, w);
2339 Py_DECREF(v);
2340 READ_TIMESTAMP(intr1);
2341 Py_DECREF(w);
2342 SET_TOP(x);
2343 if (x != NULL) DISPATCH();
2344 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 TARGET(IMPORT_STAR)
2347 v = POP();
2348 PyFrame_FastToLocals(f);
2349 if ((x = f->f_locals) == NULL) {
2350 PyErr_SetString(PyExc_SystemError,
2351 "no locals found during 'import *'");
2352 break;
2353 }
2354 READ_TIMESTAMP(intr0);
2355 err = import_all_from(x, v);
2356 READ_TIMESTAMP(intr1);
2357 PyFrame_LocalsToFast(f, 0);
2358 Py_DECREF(v);
2359 if (err == 0) DISPATCH();
2360 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002362 TARGET(IMPORT_FROM)
2363 w = GETITEM(names, oparg);
2364 v = TOP();
2365 READ_TIMESTAMP(intr0);
2366 x = import_from(v, w);
2367 READ_TIMESTAMP(intr1);
2368 PUSH(x);
2369 if (x != NULL) DISPATCH();
2370 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 TARGET(JUMP_FORWARD)
2373 JUMPBY(oparg);
2374 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002376 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2377 TARGET(POP_JUMP_IF_FALSE)
2378 w = POP();
2379 if (w == Py_True) {
2380 Py_DECREF(w);
2381 FAST_DISPATCH();
2382 }
2383 if (w == Py_False) {
2384 Py_DECREF(w);
2385 JUMPTO(oparg);
2386 FAST_DISPATCH();
2387 }
2388 err = PyObject_IsTrue(w);
2389 Py_DECREF(w);
2390 if (err > 0)
2391 err = 0;
2392 else if (err == 0)
2393 JUMPTO(oparg);
2394 else
2395 break;
2396 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2399 TARGET(POP_JUMP_IF_TRUE)
2400 w = POP();
2401 if (w == Py_False) {
2402 Py_DECREF(w);
2403 FAST_DISPATCH();
2404 }
2405 if (w == Py_True) {
2406 Py_DECREF(w);
2407 JUMPTO(oparg);
2408 FAST_DISPATCH();
2409 }
2410 err = PyObject_IsTrue(w);
2411 Py_DECREF(w);
2412 if (err > 0) {
2413 err = 0;
2414 JUMPTO(oparg);
2415 }
2416 else if (err == 0)
2417 ;
2418 else
2419 break;
2420 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 TARGET(JUMP_IF_FALSE_OR_POP)
2423 w = TOP();
2424 if (w == Py_True) {
2425 STACKADJ(-1);
2426 Py_DECREF(w);
2427 FAST_DISPATCH();
2428 }
2429 if (w == Py_False) {
2430 JUMPTO(oparg);
2431 FAST_DISPATCH();
2432 }
2433 err = PyObject_IsTrue(w);
2434 if (err > 0) {
2435 STACKADJ(-1);
2436 Py_DECREF(w);
2437 err = 0;
2438 }
2439 else if (err == 0)
2440 JUMPTO(oparg);
2441 else
2442 break;
2443 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 TARGET(JUMP_IF_TRUE_OR_POP)
2446 w = TOP();
2447 if (w == Py_False) {
2448 STACKADJ(-1);
2449 Py_DECREF(w);
2450 FAST_DISPATCH();
2451 }
2452 if (w == Py_True) {
2453 JUMPTO(oparg);
2454 FAST_DISPATCH();
2455 }
2456 err = PyObject_IsTrue(w);
2457 if (err > 0) {
2458 err = 0;
2459 JUMPTO(oparg);
2460 }
2461 else if (err == 0) {
2462 STACKADJ(-1);
2463 Py_DECREF(w);
2464 }
2465 else
2466 break;
2467 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002468
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2470 TARGET(JUMP_ABSOLUTE)
2471 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002472#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002473 /* Enabling this path speeds-up all while and for-loops by bypassing
2474 the per-loop checks for signals. By default, this should be turned-off
2475 because it prevents detection of a control-break in tight loops like
2476 "while 1: pass". Compile with this option turned-on when you need
2477 the speed-up and do not need break checking inside tight loops (ones
2478 that contain only instructions ending with FAST_DISPATCH).
2479 */
2480 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002481#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002482 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002483#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002485 TARGET(GET_ITER)
2486 /* before: [obj]; after [getiter(obj)] */
2487 v = TOP();
2488 x = PyObject_GetIter(v);
2489 Py_DECREF(v);
2490 if (x != NULL) {
2491 SET_TOP(x);
2492 PREDICT(FOR_ITER);
2493 DISPATCH();
2494 }
2495 STACKADJ(-1);
2496 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 PREDICTED_WITH_ARG(FOR_ITER);
2499 TARGET(FOR_ITER)
2500 /* before: [iter]; after: [iter, iter()] *or* [] */
2501 v = TOP();
2502 x = (*v->ob_type->tp_iternext)(v);
2503 if (x != NULL) {
2504 PUSH(x);
2505 PREDICT(STORE_FAST);
2506 PREDICT(UNPACK_SEQUENCE);
2507 DISPATCH();
2508 }
2509 if (PyErr_Occurred()) {
2510 if (!PyErr_ExceptionMatches(
2511 PyExc_StopIteration))
2512 break;
2513 PyErr_Clear();
2514 }
2515 /* iterator ended normally */
2516 x = v = POP();
2517 Py_DECREF(v);
2518 JUMPBY(oparg);
2519 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 TARGET(BREAK_LOOP)
2522 why = WHY_BREAK;
2523 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002525 TARGET(CONTINUE_LOOP)
2526 retval = PyLong_FromLong(oparg);
2527 if (!retval) {
2528 x = NULL;
2529 break;
2530 }
2531 why = WHY_CONTINUE;
2532 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002534 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2535 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2536 TARGET(SETUP_FINALLY)
2537 _setup_finally:
2538 /* NOTE: If you add any new block-setup opcodes that
2539 are not try/except/finally handlers, you may need
2540 to update the PyGen_NeedsFinalizing() function.
2541 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2544 STACK_LEVEL());
2545 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002547 TARGET(SETUP_WITH)
2548 {
2549 static PyObject *exit, *enter;
2550 w = TOP();
2551 x = special_lookup(w, "__exit__", &exit);
2552 if (!x)
2553 break;
2554 SET_TOP(x);
2555 u = special_lookup(w, "__enter__", &enter);
2556 Py_DECREF(w);
2557 if (!u) {
2558 x = NULL;
2559 break;
2560 }
2561 x = PyObject_CallFunctionObjArgs(u, NULL);
2562 Py_DECREF(u);
2563 if (!x)
2564 break;
2565 /* Setup the finally block before pushing the result
2566 of __enter__ on the stack. */
2567 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2568 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 PUSH(x);
2571 DISPATCH();
2572 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002574 TARGET(WITH_CLEANUP)
2575 {
2576 /* At the top of the stack are 1-3 values indicating
2577 how/why we entered the finally clause:
2578 - TOP = None
2579 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2580 - TOP = WHY_*; no retval below it
2581 - (TOP, SECOND, THIRD) = exc_info()
2582 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2583 Below them is EXIT, the context.__exit__ bound method.
2584 In the last case, we must call
2585 EXIT(TOP, SECOND, THIRD)
2586 otherwise we must call
2587 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002589 In the first two cases, we remove EXIT from the
2590 stack, leaving the rest in the same order. In the
2591 third case, we shift the bottom 3 values of the
2592 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 In addition, if the stack represents an exception,
2595 *and* the function call returns a 'true' value, we
2596 push WHY_SILENCED onto the stack. END_FINALLY will
2597 then not re-raise the exception. (But non-local
2598 gotos should still be resumed.)
2599 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002601 PyObject *exit_func;
2602 u = TOP();
2603 if (u == Py_None) {
2604 (void)POP();
2605 exit_func = TOP();
2606 SET_TOP(u);
2607 v = w = Py_None;
2608 }
2609 else if (PyLong_Check(u)) {
2610 (void)POP();
2611 switch(PyLong_AsLong(u)) {
2612 case WHY_RETURN:
2613 case WHY_CONTINUE:
2614 /* Retval in TOP. */
2615 exit_func = SECOND();
2616 SET_SECOND(TOP());
2617 SET_TOP(u);
2618 break;
2619 default:
2620 exit_func = TOP();
2621 SET_TOP(u);
2622 break;
2623 }
2624 u = v = w = Py_None;
2625 }
2626 else {
2627 PyObject *tp, *exc, *tb;
2628 PyTryBlock *block;
2629 v = SECOND();
2630 w = THIRD();
2631 tp = FOURTH();
2632 exc = PEEK(5);
2633 tb = PEEK(6);
2634 exit_func = PEEK(7);
2635 SET_VALUE(7, tb);
2636 SET_VALUE(6, exc);
2637 SET_VALUE(5, tp);
2638 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2639 SET_FOURTH(NULL);
2640 /* We just shifted the stack down, so we have
2641 to tell the except handler block that the
2642 values are lower than it expects. */
2643 block = &f->f_blockstack[f->f_iblock - 1];
2644 assert(block->b_type == EXCEPT_HANDLER);
2645 block->b_level--;
2646 }
2647 /* XXX Not the fastest way to call it... */
2648 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2649 NULL);
2650 Py_DECREF(exit_func);
2651 if (x == NULL)
2652 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002654 if (u != Py_None)
2655 err = PyObject_IsTrue(x);
2656 else
2657 err = 0;
2658 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 if (err < 0)
2661 break; /* Go to error exit */
2662 else if (err > 0) {
2663 err = 0;
2664 /* There was an exception and a True return */
2665 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2666 }
2667 PREDICT(END_FINALLY);
2668 break;
2669 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 TARGET(CALL_FUNCTION)
2672 {
2673 PyObject **sp;
2674 PCALL(PCALL_ALL);
2675 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002676#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002678#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002680#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 stack_pointer = sp;
2682 PUSH(x);
2683 if (x != NULL)
2684 DISPATCH();
2685 break;
2686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2689 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2690 TARGET(CALL_FUNCTION_VAR_KW)
2691 _call_function_var_kw:
2692 {
2693 int na = oparg & 0xff;
2694 int nk = (oparg>>8) & 0xff;
2695 int flags = (opcode - CALL_FUNCTION) & 3;
2696 int n = na + 2 * nk;
2697 PyObject **pfunc, *func, **sp;
2698 PCALL(PCALL_ALL);
2699 if (flags & CALL_FLAG_VAR)
2700 n++;
2701 if (flags & CALL_FLAG_KW)
2702 n++;
2703 pfunc = stack_pointer - n - 1;
2704 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002707 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002708 PyObject *self = PyMethod_GET_SELF(func);
2709 Py_INCREF(self);
2710 func = PyMethod_GET_FUNCTION(func);
2711 Py_INCREF(func);
2712 Py_DECREF(*pfunc);
2713 *pfunc = self;
2714 na++;
2715 n++;
2716 } else
2717 Py_INCREF(func);
2718 sp = stack_pointer;
2719 READ_TIMESTAMP(intr0);
2720 x = ext_do_call(func, &sp, flags, na, nk);
2721 READ_TIMESTAMP(intr1);
2722 stack_pointer = sp;
2723 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 while (stack_pointer > pfunc) {
2726 w = POP();
2727 Py_DECREF(w);
2728 }
2729 PUSH(x);
2730 if (x != NULL)
2731 DISPATCH();
2732 break;
2733 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002735 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2736 TARGET(MAKE_FUNCTION)
2737 _make_function:
2738 {
2739 int posdefaults = oparg & 0xff;
2740 int kwdefaults = (oparg>>8) & 0xff;
2741 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 v = POP(); /* code object */
2744 x = PyFunction_New(v, f->f_globals);
2745 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 if (x != NULL && opcode == MAKE_CLOSURE) {
2748 v = POP();
2749 if (PyFunction_SetClosure(x, v) != 0) {
2750 /* Can't happen unless bytecode is corrupt. */
2751 why = WHY_EXCEPTION;
2752 }
2753 Py_DECREF(v);
2754 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 if (x != NULL && num_annotations > 0) {
2757 Py_ssize_t name_ix;
2758 u = POP(); /* names of args with annotations */
2759 v = PyDict_New();
2760 if (v == NULL) {
2761 Py_DECREF(x);
2762 x = NULL;
2763 break;
2764 }
2765 name_ix = PyTuple_Size(u);
2766 assert(num_annotations == name_ix+1);
2767 while (name_ix > 0) {
2768 --name_ix;
2769 t = PyTuple_GET_ITEM(u, name_ix);
2770 w = POP();
2771 /* XXX(nnorwitz): check for errors */
2772 PyDict_SetItem(v, t, w);
2773 Py_DECREF(w);
2774 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 if (PyFunction_SetAnnotations(x, v) != 0) {
2777 /* Can't happen unless
2778 PyFunction_SetAnnotations changes. */
2779 why = WHY_EXCEPTION;
2780 }
2781 Py_DECREF(v);
2782 Py_DECREF(u);
2783 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002785 /* XXX Maybe this should be a separate opcode? */
2786 if (x != NULL && posdefaults > 0) {
2787 v = PyTuple_New(posdefaults);
2788 if (v == NULL) {
2789 Py_DECREF(x);
2790 x = NULL;
2791 break;
2792 }
2793 while (--posdefaults >= 0) {
2794 w = POP();
2795 PyTuple_SET_ITEM(v, posdefaults, w);
2796 }
2797 if (PyFunction_SetDefaults(x, v) != 0) {
2798 /* Can't happen unless
2799 PyFunction_SetDefaults changes. */
2800 why = WHY_EXCEPTION;
2801 }
2802 Py_DECREF(v);
2803 }
2804 if (x != NULL && kwdefaults > 0) {
2805 v = PyDict_New();
2806 if (v == NULL) {
2807 Py_DECREF(x);
2808 x = NULL;
2809 break;
2810 }
2811 while (--kwdefaults >= 0) {
2812 w = POP(); /* default value */
2813 u = POP(); /* kw only arg name */
2814 /* XXX(nnorwitz): check for errors */
2815 PyDict_SetItem(v, u, w);
2816 Py_DECREF(w);
2817 Py_DECREF(u);
2818 }
2819 if (PyFunction_SetKwDefaults(x, v) != 0) {
2820 /* Can't happen unless
2821 PyFunction_SetKwDefaults changes. */
2822 why = WHY_EXCEPTION;
2823 }
2824 Py_DECREF(v);
2825 }
2826 PUSH(x);
2827 break;
2828 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002830 TARGET(BUILD_SLICE)
2831 if (oparg == 3)
2832 w = POP();
2833 else
2834 w = NULL;
2835 v = POP();
2836 u = TOP();
2837 x = PySlice_New(u, v, w);
2838 Py_DECREF(u);
2839 Py_DECREF(v);
2840 Py_XDECREF(w);
2841 SET_TOP(x);
2842 if (x != NULL) DISPATCH();
2843 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 TARGET(EXTENDED_ARG)
2846 opcode = NEXTOP();
2847 oparg = oparg<<16 | NEXTARG();
2848 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002849
Antoine Pitrou042b1282010-08-13 21:15:58 +00002850#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002851 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002852#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 default:
2854 fprintf(stderr,
2855 "XXX lineno: %d, opcode: %d\n",
2856 PyFrame_GetLineNumber(f),
2857 opcode);
2858 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2859 why = WHY_EXCEPTION;
2860 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002861
2862#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002864#endif
2865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002874 if (why == WHY_NOT) {
2875 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002876#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 /* This check is expensive! */
2878 if (PyErr_Occurred())
2879 fprintf(stderr,
2880 "XXX undetected error\n");
2881 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002882#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002883 READ_TIMESTAMP(loop1);
2884 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002885#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002886 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002887#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 }
2889 why = WHY_EXCEPTION;
2890 x = Py_None;
2891 err = 0;
2892 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002894 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2897 if (!PyErr_Occurred()) {
2898 PyErr_SetString(PyExc_SystemError,
2899 "error return without exception set");
2900 why = WHY_EXCEPTION;
2901 }
2902 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002903#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002904 else {
2905 /* This check is expensive! */
2906 if (PyErr_Occurred()) {
2907 char buf[128];
2908 sprintf(buf, "Stack unwind with exception "
2909 "set and why=%d", why);
2910 Py_FatalError(buf);
2911 }
2912 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002913#endif
2914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 if (why == WHY_EXCEPTION) {
2918 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 if (tstate->c_tracefunc != NULL)
2921 call_exc_trace(tstate->c_tracefunc,
2922 tstate->c_traceobj, f);
2923 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002924
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002927 if (why == WHY_RERAISE)
2928 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002931
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002932fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 while (why != WHY_NOT && f->f_iblock > 0) {
2934 /* Peek at the current block. */
2935 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002937 assert(why != WHY_YIELD);
2938 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2939 why = WHY_NOT;
2940 JUMPTO(PyLong_AS_LONG(retval));
2941 Py_DECREF(retval);
2942 break;
2943 }
2944 /* Now we have to pop the block. */
2945 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 if (b->b_type == EXCEPT_HANDLER) {
2948 UNWIND_EXCEPT_HANDLER(b);
2949 continue;
2950 }
2951 UNWIND_BLOCK(b);
2952 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2953 why = WHY_NOT;
2954 JUMPTO(b->b_handler);
2955 break;
2956 }
2957 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2958 || b->b_type == SETUP_FINALLY)) {
2959 PyObject *exc, *val, *tb;
2960 int handler = b->b_handler;
2961 /* Beware, this invalidates all b->b_* fields */
2962 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2963 PUSH(tstate->exc_traceback);
2964 PUSH(tstate->exc_value);
2965 if (tstate->exc_type != NULL) {
2966 PUSH(tstate->exc_type);
2967 }
2968 else {
2969 Py_INCREF(Py_None);
2970 PUSH(Py_None);
2971 }
2972 PyErr_Fetch(&exc, &val, &tb);
2973 /* Make the raw exception data
2974 available to the handler,
2975 so a program can emulate the
2976 Python main loop. */
2977 PyErr_NormalizeException(
2978 &exc, &val, &tb);
2979 PyException_SetTraceback(val, tb);
2980 Py_INCREF(exc);
2981 tstate->exc_type = exc;
2982 Py_INCREF(val);
2983 tstate->exc_value = val;
2984 tstate->exc_traceback = tb;
2985 if (tb == NULL)
2986 tb = Py_None;
2987 Py_INCREF(tb);
2988 PUSH(tb);
2989 PUSH(val);
2990 PUSH(exc);
2991 why = WHY_NOT;
2992 JUMPTO(handler);
2993 break;
2994 }
2995 if (b->b_type == SETUP_FINALLY) {
2996 if (why & (WHY_RETURN | WHY_CONTINUE))
2997 PUSH(retval);
2998 PUSH(PyLong_FromLong((long)why));
2999 why = WHY_NOT;
3000 JUMPTO(b->b_handler);
3001 break;
3002 }
3003 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003004
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003005 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 if (why != WHY_NOT)
3008 break;
3009 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 assert(why != WHY_YIELD);
3014 /* Pop remaining stack entries. */
3015 while (!EMPTY()) {
3016 v = POP();
3017 Py_XDECREF(v);
3018 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003020 if (why != WHY_RETURN)
3021 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003022
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003023fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024 if (tstate->use_tracing) {
3025 if (tstate->c_tracefunc) {
3026 if (why == WHY_RETURN || why == WHY_YIELD) {
3027 if (call_trace(tstate->c_tracefunc,
3028 tstate->c_traceobj, f,
3029 PyTrace_RETURN, retval)) {
3030 Py_XDECREF(retval);
3031 retval = NULL;
3032 why = WHY_EXCEPTION;
3033 }
3034 }
3035 else if (why == WHY_EXCEPTION) {
3036 call_trace_protected(tstate->c_tracefunc,
3037 tstate->c_traceobj, f,
3038 PyTrace_RETURN, NULL);
3039 }
3040 }
3041 if (tstate->c_profilefunc) {
3042 if (why == WHY_EXCEPTION)
3043 call_trace_protected(tstate->c_profilefunc,
3044 tstate->c_profileobj, f,
3045 PyTrace_RETURN, NULL);
3046 else if (call_trace(tstate->c_profilefunc,
3047 tstate->c_profileobj, f,
3048 PyTrace_RETURN, retval)) {
3049 Py_XDECREF(retval);
3050 retval = NULL;
3051 why = WHY_EXCEPTION;
3052 }
3053 }
3054 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003057exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 Py_LeaveRecursiveCall();
3059 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003061 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003062}
3063
Guido van Rossumc2e20742006-02-27 22:32:47 +00003064/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003065 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003066 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003067
Tim Peters6d6c1a32001-08-02 04:15:00 +00003068PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003069PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 PyObject **args, int argcount, PyObject **kws, int kwcount,
3071 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003072{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003073 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 register PyFrameObject *f;
3075 register PyObject *retval = NULL;
3076 register PyObject **fastlocals, **freevars;
3077 PyThreadState *tstate = PyThreadState_GET();
3078 PyObject *x, *u;
3079 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003081 if (globals == NULL) {
3082 PyErr_SetString(PyExc_SystemError,
3083 "PyEval_EvalCodeEx: NULL globals");
3084 return NULL;
3085 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003087 assert(tstate != NULL);
3088 assert(globals != NULL);
3089 f = PyFrame_New(tstate, co, globals, locals);
3090 if (f == NULL)
3091 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003092
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 fastlocals = f->f_localsplus;
3094 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003096 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3097 int i;
3098 int n = argcount;
3099 PyObject *kwdict = NULL;
3100 if (co->co_flags & CO_VARKEYWORDS) {
3101 kwdict = PyDict_New();
3102 if (kwdict == NULL)
3103 goto fail;
3104 i = total_args;
3105 if (co->co_flags & CO_VARARGS)
3106 i++;
3107 SETLOCAL(i, kwdict);
3108 }
3109 if (argcount > co->co_argcount) {
3110 if (!(co->co_flags & CO_VARARGS)) {
3111 PyErr_Format(PyExc_TypeError,
3112 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003113 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114 co->co_name,
3115 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003116 co->co_argcount,
3117 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonaa7fbd92010-09-25 03:25:42 +00003118 argcount + kwcount);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003119 goto fail;
3120 }
3121 n = co->co_argcount;
3122 }
3123 for (i = 0; i < n; i++) {
3124 x = args[i];
3125 Py_INCREF(x);
3126 SETLOCAL(i, x);
3127 }
3128 if (co->co_flags & CO_VARARGS) {
3129 u = PyTuple_New(argcount - n);
3130 if (u == NULL)
3131 goto fail;
3132 SETLOCAL(total_args, u);
3133 for (i = n; i < argcount; i++) {
3134 x = args[i];
3135 Py_INCREF(x);
3136 PyTuple_SET_ITEM(u, i-n, x);
3137 }
3138 }
3139 for (i = 0; i < kwcount; i++) {
3140 PyObject **co_varnames;
3141 PyObject *keyword = kws[2*i];
3142 PyObject *value = kws[2*i + 1];
3143 int j;
3144 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3145 PyErr_Format(PyExc_TypeError,
3146 "%U() keywords must be strings",
3147 co->co_name);
3148 goto fail;
3149 }
3150 /* Speed hack: do raw pointer compares. As names are
3151 normally interned this should almost always hit. */
3152 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3153 for (j = 0; j < total_args; j++) {
3154 PyObject *nm = co_varnames[j];
3155 if (nm == keyword)
3156 goto kw_found;
3157 }
3158 /* Slow fallback, just in case */
3159 for (j = 0; j < total_args; j++) {
3160 PyObject *nm = co_varnames[j];
3161 int cmp = PyObject_RichCompareBool(
3162 keyword, nm, Py_EQ);
3163 if (cmp > 0)
3164 goto kw_found;
3165 else if (cmp < 0)
3166 goto fail;
3167 }
3168 if (j >= total_args && kwdict == NULL) {
3169 PyErr_Format(PyExc_TypeError,
3170 "%U() got an unexpected "
3171 "keyword argument '%S'",
3172 co->co_name,
3173 keyword);
3174 goto fail;
3175 }
3176 PyDict_SetItem(kwdict, keyword, value);
3177 continue;
3178 kw_found:
3179 if (GETLOCAL(j) != NULL) {
3180 PyErr_Format(PyExc_TypeError,
3181 "%U() got multiple "
3182 "values for keyword "
3183 "argument '%S'",
3184 co->co_name,
3185 keyword);
3186 goto fail;
3187 }
3188 Py_INCREF(value);
3189 SETLOCAL(j, value);
3190 }
3191 if (co->co_kwonlyargcount > 0) {
3192 for (i = co->co_argcount; i < total_args; i++) {
3193 PyObject *name;
3194 if (GETLOCAL(i) != NULL)
3195 continue;
3196 name = PyTuple_GET_ITEM(co->co_varnames, i);
3197 if (kwdefs != NULL) {
3198 PyObject *def = PyDict_GetItem(kwdefs, name);
3199 if (def) {
3200 Py_INCREF(def);
3201 SETLOCAL(i, def);
3202 continue;
3203 }
3204 }
3205 PyErr_Format(PyExc_TypeError,
3206 "%U() needs keyword-only argument %S",
3207 co->co_name, name);
3208 goto fail;
3209 }
3210 }
3211 if (argcount < co->co_argcount) {
3212 int m = co->co_argcount - defcount;
3213 for (i = argcount; i < m; i++) {
3214 if (GETLOCAL(i) == NULL) {
3215 int j, given = 0;
3216 for (j = 0; j < co->co_argcount; j++)
3217 if (GETLOCAL(j))
3218 given++;
3219 PyErr_Format(PyExc_TypeError,
3220 "%U() takes %s %d "
3221 "argument%s "
3222 "(%d given)",
3223 co->co_name,
3224 ((co->co_flags & CO_VARARGS) ||
3225 defcount) ? "at least"
3226 : "exactly",
3227 m, m == 1 ? "" : "s", given);
3228 goto fail;
3229 }
3230 }
3231 if (n > m)
3232 i = n - m;
3233 else
3234 i = 0;
3235 for (; i < defcount; i++) {
3236 if (GETLOCAL(m+i) == NULL) {
3237 PyObject *def = defs[i];
3238 Py_INCREF(def);
3239 SETLOCAL(m+i, def);
3240 }
3241 }
3242 }
3243 }
3244 else if (argcount > 0 || kwcount > 0) {
3245 PyErr_Format(PyExc_TypeError,
3246 "%U() takes no arguments (%d given)",
3247 co->co_name,
3248 argcount + kwcount);
3249 goto fail;
3250 }
3251 /* Allocate and initialize storage for cell vars, and copy free
3252 vars into frame. This isn't too efficient right now. */
3253 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3254 int i, j, nargs, found;
3255 Py_UNICODE *cellname, *argname;
3256 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003257
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003258 nargs = total_args;
3259 if (co->co_flags & CO_VARARGS)
3260 nargs++;
3261 if (co->co_flags & CO_VARKEYWORDS)
3262 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003264 /* Initialize each cell var, taking into account
3265 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003266
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003267 Should arrange for the compiler to put cellvars
3268 that are arguments at the beginning of the cellvars
3269 list so that we can march over it more efficiently?
3270 */
3271 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3272 cellname = PyUnicode_AS_UNICODE(
3273 PyTuple_GET_ITEM(co->co_cellvars, i));
3274 found = 0;
3275 for (j = 0; j < nargs; j++) {
3276 argname = PyUnicode_AS_UNICODE(
3277 PyTuple_GET_ITEM(co->co_varnames, j));
3278 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3279 c = PyCell_New(GETLOCAL(j));
3280 if (c == NULL)
3281 goto fail;
3282 GETLOCAL(co->co_nlocals + i) = c;
3283 found = 1;
3284 break;
3285 }
3286 }
3287 if (found == 0) {
3288 c = PyCell_New(NULL);
3289 if (c == NULL)
3290 goto fail;
3291 SETLOCAL(co->co_nlocals + i, c);
3292 }
3293 }
3294 }
3295 if (PyTuple_GET_SIZE(co->co_freevars)) {
3296 int i;
3297 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3298 PyObject *o = PyTuple_GET_ITEM(closure, i);
3299 Py_INCREF(o);
3300 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3301 }
3302 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 if (co->co_flags & CO_GENERATOR) {
3305 /* Don't need to keep the reference to f_back, it will be set
3306 * when the generator is resumed. */
3307 Py_XDECREF(f->f_back);
3308 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003311
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 /* Create a new generator that owns the ready to run frame
3313 * and return that as the value. */
3314 return PyGen_New(f);
3315 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003317 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003318
Thomas Woutersce272b62007-09-19 21:19:28 +00003319fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003321 /* decref'ing the frame can cause __del__ methods to get invoked,
3322 which can call back into Python. While we're done with the
3323 current Python frame (f), the associated C stack is still in use,
3324 so recursion_depth must be boosted for the duration.
3325 */
3326 assert(tstate != NULL);
3327 ++tstate->recursion_depth;
3328 Py_DECREF(f);
3329 --tstate->recursion_depth;
3330 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003331}
3332
3333
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003334static PyObject *
3335special_lookup(PyObject *o, char *meth, PyObject **cache)
3336{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003337 PyObject *res;
3338 res = _PyObject_LookupSpecial(o, meth, cache);
3339 if (res == NULL && !PyErr_Occurred()) {
3340 PyErr_SetObject(PyExc_AttributeError, *cache);
3341 return NULL;
3342 }
3343 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003344}
3345
3346
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003347/* Logic for the raise statement (too complicated for inlining).
3348 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003349static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003350do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003351{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003352 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003353
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003354 if (exc == NULL) {
3355 /* Reraise */
3356 PyThreadState *tstate = PyThreadState_GET();
3357 PyObject *tb;
3358 type = tstate->exc_type;
3359 value = tstate->exc_value;
3360 tb = tstate->exc_traceback;
3361 if (type == Py_None) {
3362 PyErr_SetString(PyExc_RuntimeError,
3363 "No active exception to reraise");
3364 return WHY_EXCEPTION;
3365 }
3366 Py_XINCREF(type);
3367 Py_XINCREF(value);
3368 Py_XINCREF(tb);
3369 PyErr_Restore(type, value, tb);
3370 return WHY_RERAISE;
3371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 /* We support the following forms of raise:
3374 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003375 raise <instance>
3376 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 if (PyExceptionClass_Check(exc)) {
3379 type = exc;
3380 value = PyObject_CallObject(exc, NULL);
3381 if (value == NULL)
3382 goto raise_error;
3383 }
3384 else if (PyExceptionInstance_Check(exc)) {
3385 value = exc;
3386 type = PyExceptionInstance_Class(exc);
3387 Py_INCREF(type);
3388 }
3389 else {
3390 /* Not something you can raise. You get an exception
3391 anyway, just not what you specified :-) */
3392 Py_DECREF(exc);
3393 PyErr_SetString(PyExc_TypeError,
3394 "exceptions must derive from BaseException");
3395 goto raise_error;
3396 }
Collin Winter828f04a2007-08-31 00:04:24 +00003397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003398 if (cause) {
3399 PyObject *fixed_cause;
3400 if (PyExceptionClass_Check(cause)) {
3401 fixed_cause = PyObject_CallObject(cause, NULL);
3402 if (fixed_cause == NULL)
3403 goto raise_error;
3404 Py_DECREF(cause);
3405 }
3406 else if (PyExceptionInstance_Check(cause)) {
3407 fixed_cause = cause;
3408 }
3409 else {
3410 PyErr_SetString(PyExc_TypeError,
3411 "exception causes must derive from "
3412 "BaseException");
3413 goto raise_error;
3414 }
3415 PyException_SetCause(value, fixed_cause);
3416 }
Collin Winter828f04a2007-08-31 00:04:24 +00003417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 PyErr_SetObject(type, value);
3419 /* PyErr_SetObject incref's its arguments */
3420 Py_XDECREF(value);
3421 Py_XDECREF(type);
3422 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003423
3424raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003425 Py_XDECREF(value);
3426 Py_XDECREF(type);
3427 Py_XDECREF(cause);
3428 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003429}
3430
Tim Petersd6d010b2001-06-21 02:49:55 +00003431/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003432 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003433
Guido van Rossum0368b722007-05-11 16:50:42 +00003434 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3435 with a variable target.
3436*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003437
Barry Warsawe42b18f1997-08-25 22:13:04 +00003438static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003439unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003440{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 int i = 0, j = 0;
3442 Py_ssize_t ll = 0;
3443 PyObject *it; /* iter(v) */
3444 PyObject *w;
3445 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 it = PyObject_GetIter(v);
3450 if (it == NULL)
3451 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003452
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003453 for (; i < argcnt; i++) {
3454 w = PyIter_Next(it);
3455 if (w == NULL) {
3456 /* Iterator done, via error or exhaustion. */
3457 if (!PyErr_Occurred()) {
3458 PyErr_Format(PyExc_ValueError,
3459 "need more than %d value%s to unpack",
3460 i, i == 1 ? "" : "s");
3461 }
3462 goto Error;
3463 }
3464 *--sp = w;
3465 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003467 if (argcntafter == -1) {
3468 /* We better have exhausted the iterator now. */
3469 w = PyIter_Next(it);
3470 if (w == NULL) {
3471 if (PyErr_Occurred())
3472 goto Error;
3473 Py_DECREF(it);
3474 return 1;
3475 }
3476 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003477 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3478 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003479 goto Error;
3480 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003482 l = PySequence_List(it);
3483 if (l == NULL)
3484 goto Error;
3485 *--sp = l;
3486 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 ll = PyList_GET_SIZE(l);
3489 if (ll < argcntafter) {
3490 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3491 argcnt + ll);
3492 goto Error;
3493 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 /* Pop the "after-variable" args off the list. */
3496 for (j = argcntafter; j > 0; j--, i++) {
3497 *--sp = PyList_GET_ITEM(l, ll - j);
3498 }
3499 /* Resize the list. */
3500 Py_SIZE(l) = ll - argcntafter;
3501 Py_DECREF(it);
3502 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003503
Tim Petersd6d010b2001-06-21 02:49:55 +00003504Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 for (; i > 0; i--, sp++)
3506 Py_DECREF(*sp);
3507 Py_XDECREF(it);
3508 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003509}
3510
3511
Guido van Rossum96a42c81992-01-12 02:29:51 +00003512#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003513static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003514prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003515{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 printf("%s ", str);
3517 if (PyObject_Print(v, stdout, 0) != 0)
3518 PyErr_Clear(); /* Don't know what else to do */
3519 printf("\n");
3520 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003521}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003522#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003523
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003524static void
Fred Drake5755ce62001-06-27 19:19:46 +00003525call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003526{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 PyObject *type, *value, *traceback, *arg;
3528 int err;
3529 PyErr_Fetch(&type, &value, &traceback);
3530 if (value == NULL) {
3531 value = Py_None;
3532 Py_INCREF(value);
3533 }
3534 arg = PyTuple_Pack(3, type, value, traceback);
3535 if (arg == NULL) {
3536 PyErr_Restore(type, value, traceback);
3537 return;
3538 }
3539 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3540 Py_DECREF(arg);
3541 if (err == 0)
3542 PyErr_Restore(type, value, traceback);
3543 else {
3544 Py_XDECREF(type);
3545 Py_XDECREF(value);
3546 Py_XDECREF(traceback);
3547 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003548}
3549
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003550static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003551call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003554 PyObject *type, *value, *traceback;
3555 int err;
3556 PyErr_Fetch(&type, &value, &traceback);
3557 err = call_trace(func, obj, frame, what, arg);
3558 if (err == 0)
3559 {
3560 PyErr_Restore(type, value, traceback);
3561 return 0;
3562 }
3563 else {
3564 Py_XDECREF(type);
3565 Py_XDECREF(value);
3566 Py_XDECREF(traceback);
3567 return -1;
3568 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003569}
3570
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003571static int
Fred Drake5755ce62001-06-27 19:19:46 +00003572call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003574{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003575 register PyThreadState *tstate = frame->f_tstate;
3576 int result;
3577 if (tstate->tracing)
3578 return 0;
3579 tstate->tracing++;
3580 tstate->use_tracing = 0;
3581 result = func(obj, frame, what, arg);
3582 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3583 || (tstate->c_profilefunc != NULL));
3584 tstate->tracing--;
3585 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003586}
3587
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003588PyObject *
3589_PyEval_CallTracing(PyObject *func, PyObject *args)
3590{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 PyFrameObject *frame = PyEval_GetFrame();
3592 PyThreadState *tstate = frame->f_tstate;
3593 int save_tracing = tstate->tracing;
3594 int save_use_tracing = tstate->use_tracing;
3595 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 tstate->tracing = 0;
3598 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3599 || (tstate->c_profilefunc != NULL));
3600 result = PyObject_Call(func, args, NULL);
3601 tstate->tracing = save_tracing;
3602 tstate->use_tracing = save_use_tracing;
3603 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003604}
3605
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003606/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003607static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003608maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003609 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3610 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 int result = 0;
3613 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 /* If the last instruction executed isn't in the current
3616 instruction window, reset the window.
3617 */
3618 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3619 PyAddrPair bounds;
3620 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3621 &bounds);
3622 *instr_lb = bounds.ap_lower;
3623 *instr_ub = bounds.ap_upper;
3624 }
3625 /* If the last instruction falls at the start of a line or if
3626 it represents a jump backwards, update the frame's line
3627 number and call the trace function. */
3628 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3629 frame->f_lineno = line;
3630 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3631 }
3632 *instr_prev = frame->f_lasti;
3633 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003634}
3635
Fred Drake5755ce62001-06-27 19:19:46 +00003636void
3637PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003638{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003639 PyThreadState *tstate = PyThreadState_GET();
3640 PyObject *temp = tstate->c_profileobj;
3641 Py_XINCREF(arg);
3642 tstate->c_profilefunc = NULL;
3643 tstate->c_profileobj = NULL;
3644 /* Must make sure that tracing is not ignored if 'temp' is freed */
3645 tstate->use_tracing = tstate->c_tracefunc != NULL;
3646 Py_XDECREF(temp);
3647 tstate->c_profilefunc = func;
3648 tstate->c_profileobj = arg;
3649 /* Flag that tracing or profiling is turned on */
3650 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003651}
3652
3653void
3654PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3655{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003656 PyThreadState *tstate = PyThreadState_GET();
3657 PyObject *temp = tstate->c_traceobj;
3658 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3659 Py_XINCREF(arg);
3660 tstate->c_tracefunc = NULL;
3661 tstate->c_traceobj = NULL;
3662 /* Must make sure that profiling is not ignored if 'temp' is freed */
3663 tstate->use_tracing = tstate->c_profilefunc != NULL;
3664 Py_XDECREF(temp);
3665 tstate->c_tracefunc = func;
3666 tstate->c_traceobj = arg;
3667 /* Flag that tracing or profiling is turned on */
3668 tstate->use_tracing = ((func != NULL)
3669 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003670}
3671
Guido van Rossumb209a111997-04-29 18:18:01 +00003672PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003673PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003674{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003675 PyFrameObject *current_frame = PyEval_GetFrame();
3676 if (current_frame == NULL)
3677 return PyThreadState_GET()->interp->builtins;
3678 else
3679 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003680}
3681
Guido van Rossumb209a111997-04-29 18:18:01 +00003682PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003683PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003685 PyFrameObject *current_frame = PyEval_GetFrame();
3686 if (current_frame == NULL)
3687 return NULL;
3688 PyFrame_FastToLocals(current_frame);
3689 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003690}
3691
Guido van Rossumb209a111997-04-29 18:18:01 +00003692PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003693PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 PyFrameObject *current_frame = PyEval_GetFrame();
3696 if (current_frame == NULL)
3697 return NULL;
3698 else
3699 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003700}
3701
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003702PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003703PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 PyThreadState *tstate = PyThreadState_GET();
3706 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003707}
3708
Guido van Rossum6135a871995-01-09 17:53:26 +00003709int
Tim Peters5ba58662001-07-16 02:29:45 +00003710PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003711{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 PyFrameObject *current_frame = PyEval_GetFrame();
3713 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 if (current_frame != NULL) {
3716 const int codeflags = current_frame->f_code->co_flags;
3717 const int compilerflags = codeflags & PyCF_MASK;
3718 if (compilerflags) {
3719 result = 1;
3720 cf->cf_flags |= compilerflags;
3721 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003722#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003723 if (codeflags & CO_GENERATOR_ALLOWED) {
3724 result = 1;
3725 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3726 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003727#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003728 }
3729 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003730}
3731
Guido van Rossum3f5da241990-12-20 15:06:42 +00003732
Guido van Rossum681d79a1995-07-18 14:51:37 +00003733/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003734 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003735
Guido van Rossumb209a111997-04-29 18:18:01 +00003736PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003737PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003738{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003739 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003741 if (arg == NULL) {
3742 arg = PyTuple_New(0);
3743 if (arg == NULL)
3744 return NULL;
3745 }
3746 else if (!PyTuple_Check(arg)) {
3747 PyErr_SetString(PyExc_TypeError,
3748 "argument list must be a tuple");
3749 return NULL;
3750 }
3751 else
3752 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 if (kw != NULL && !PyDict_Check(kw)) {
3755 PyErr_SetString(PyExc_TypeError,
3756 "keyword list must be a dictionary");
3757 Py_DECREF(arg);
3758 return NULL;
3759 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 result = PyObject_Call(func, arg, kw);
3762 Py_DECREF(arg);
3763 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003764}
3765
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003766const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003767PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003769 if (PyMethod_Check(func))
3770 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3771 else if (PyFunction_Check(func))
3772 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3773 else if (PyCFunction_Check(func))
3774 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3775 else
3776 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003777}
3778
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003779const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003780PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003781{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003782 if (PyMethod_Check(func))
3783 return "()";
3784 else if (PyFunction_Check(func))
3785 return "()";
3786 else if (PyCFunction_Check(func))
3787 return "()";
3788 else
3789 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003790}
3791
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003792static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003793err_args(PyObject *func, int flags, int nargs)
3794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 if (flags & METH_NOARGS)
3796 PyErr_Format(PyExc_TypeError,
3797 "%.200s() takes no arguments (%d given)",
3798 ((PyCFunctionObject *)func)->m_ml->ml_name,
3799 nargs);
3800 else
3801 PyErr_Format(PyExc_TypeError,
3802 "%.200s() takes exactly one argument (%d given)",
3803 ((PyCFunctionObject *)func)->m_ml->ml_name,
3804 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003805}
3806
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003807#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003808if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 if (call_trace(tstate->c_profilefunc, \
3810 tstate->c_profileobj, \
3811 tstate->frame, PyTrace_C_CALL, \
3812 func)) { \
3813 x = NULL; \
3814 } \
3815 else { \
3816 x = call; \
3817 if (tstate->c_profilefunc != NULL) { \
3818 if (x == NULL) { \
3819 call_trace_protected(tstate->c_profilefunc, \
3820 tstate->c_profileobj, \
3821 tstate->frame, PyTrace_C_EXCEPTION, \
3822 func); \
3823 /* XXX should pass (type, value, tb) */ \
3824 } else { \
3825 if (call_trace(tstate->c_profilefunc, \
3826 tstate->c_profileobj, \
3827 tstate->frame, PyTrace_C_RETURN, \
3828 func)) { \
3829 Py_DECREF(x); \
3830 x = NULL; \
3831 } \
3832 } \
3833 } \
3834 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003835} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 x = call; \
3837 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003838
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003839static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003840call_function(PyObject ***pp_stack, int oparg
3841#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003842 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003843#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003844 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003845{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 int na = oparg & 0xff;
3847 int nk = (oparg>>8) & 0xff;
3848 int n = na + 2 * nk;
3849 PyObject **pfunc = (*pp_stack) - n - 1;
3850 PyObject *func = *pfunc;
3851 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 /* Always dispatch PyCFunction first, because these are
3854 presumed to be the most frequent callable object.
3855 */
3856 if (PyCFunction_Check(func) && nk == 0) {
3857 int flags = PyCFunction_GET_FLAGS(func);
3858 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003860 PCALL(PCALL_CFUNCTION);
3861 if (flags & (METH_NOARGS | METH_O)) {
3862 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3863 PyObject *self = PyCFunction_GET_SELF(func);
3864 if (flags & METH_NOARGS && na == 0) {
3865 C_TRACE(x, (*meth)(self,NULL));
3866 }
3867 else if (flags & METH_O && na == 1) {
3868 PyObject *arg = EXT_POP(*pp_stack);
3869 C_TRACE(x, (*meth)(self,arg));
3870 Py_DECREF(arg);
3871 }
3872 else {
3873 err_args(func, flags, na);
3874 x = NULL;
3875 }
3876 }
3877 else {
3878 PyObject *callargs;
3879 callargs = load_args(pp_stack, na);
3880 READ_TIMESTAMP(*pintr0);
3881 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3882 READ_TIMESTAMP(*pintr1);
3883 Py_XDECREF(callargs);
3884 }
3885 } else {
3886 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3887 /* optimize access to bound methods */
3888 PyObject *self = PyMethod_GET_SELF(func);
3889 PCALL(PCALL_METHOD);
3890 PCALL(PCALL_BOUND_METHOD);
3891 Py_INCREF(self);
3892 func = PyMethod_GET_FUNCTION(func);
3893 Py_INCREF(func);
3894 Py_DECREF(*pfunc);
3895 *pfunc = self;
3896 na++;
3897 n++;
3898 } else
3899 Py_INCREF(func);
3900 READ_TIMESTAMP(*pintr0);
3901 if (PyFunction_Check(func))
3902 x = fast_function(func, pp_stack, n, na, nk);
3903 else
3904 x = do_call(func, pp_stack, na, nk);
3905 READ_TIMESTAMP(*pintr1);
3906 Py_DECREF(func);
3907 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 /* Clear the stack of the function object. Also removes
3910 the arguments in case they weren't consumed already
3911 (fast_function() and err_args() leave them on the stack).
3912 */
3913 while ((*pp_stack) > pfunc) {
3914 w = EXT_POP(*pp_stack);
3915 Py_DECREF(w);
3916 PCALL(PCALL_POP);
3917 }
3918 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003919}
3920
Jeremy Hylton192690e2002-08-16 18:36:11 +00003921/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003922 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003923 For the simplest case -- a function that takes only positional
3924 arguments and is called with only positional arguments -- it
3925 inlines the most primitive frame setup code from
3926 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3927 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003928*/
3929
3930static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003931fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3934 PyObject *globals = PyFunction_GET_GLOBALS(func);
3935 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3936 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3937 PyObject **d = NULL;
3938 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 PCALL(PCALL_FUNCTION);
3941 PCALL(PCALL_FAST_FUNCTION);
3942 if (argdefs == NULL && co->co_argcount == n &&
3943 co->co_kwonlyargcount == 0 && nk==0 &&
3944 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3945 PyFrameObject *f;
3946 PyObject *retval = NULL;
3947 PyThreadState *tstate = PyThreadState_GET();
3948 PyObject **fastlocals, **stack;
3949 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003951 PCALL(PCALL_FASTER_FUNCTION);
3952 assert(globals != NULL);
3953 /* XXX Perhaps we should create a specialized
3954 PyFrame_New() that doesn't take locals, but does
3955 take builtins without sanity checking them.
3956 */
3957 assert(tstate != NULL);
3958 f = PyFrame_New(tstate, co, globals, NULL);
3959 if (f == NULL)
3960 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003962 fastlocals = f->f_localsplus;
3963 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003965 for (i = 0; i < n; i++) {
3966 Py_INCREF(*stack);
3967 fastlocals[i] = *stack++;
3968 }
3969 retval = PyEval_EvalFrameEx(f,0);
3970 ++tstate->recursion_depth;
3971 Py_DECREF(f);
3972 --tstate->recursion_depth;
3973 return retval;
3974 }
3975 if (argdefs != NULL) {
3976 d = &PyTuple_GET_ITEM(argdefs, 0);
3977 nd = Py_SIZE(argdefs);
3978 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003979 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003980 (PyObject *)NULL, (*pp_stack)-n, na,
3981 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3982 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003983}
3984
3985static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003986update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3987 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 PyObject *kwdict = NULL;
3990 if (orig_kwdict == NULL)
3991 kwdict = PyDict_New();
3992 else {
3993 kwdict = PyDict_Copy(orig_kwdict);
3994 Py_DECREF(orig_kwdict);
3995 }
3996 if (kwdict == NULL)
3997 return NULL;
3998 while (--nk >= 0) {
3999 int err;
4000 PyObject *value = EXT_POP(*pp_stack);
4001 PyObject *key = EXT_POP(*pp_stack);
4002 if (PyDict_GetItem(kwdict, key) != NULL) {
4003 PyErr_Format(PyExc_TypeError,
4004 "%.200s%s got multiple values "
4005 "for keyword argument '%U'",
4006 PyEval_GetFuncName(func),
4007 PyEval_GetFuncDesc(func),
4008 key);
4009 Py_DECREF(key);
4010 Py_DECREF(value);
4011 Py_DECREF(kwdict);
4012 return NULL;
4013 }
4014 err = PyDict_SetItem(kwdict, key, value);
4015 Py_DECREF(key);
4016 Py_DECREF(value);
4017 if (err) {
4018 Py_DECREF(kwdict);
4019 return NULL;
4020 }
4021 }
4022 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004023}
4024
4025static PyObject *
4026update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004027 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004029 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 callargs = PyTuple_New(nstack + nstar);
4032 if (callargs == NULL) {
4033 return NULL;
4034 }
4035 if (nstar) {
4036 int i;
4037 for (i = 0; i < nstar; i++) {
4038 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4039 Py_INCREF(a);
4040 PyTuple_SET_ITEM(callargs, nstack + i, a);
4041 }
4042 }
4043 while (--nstack >= 0) {
4044 w = EXT_POP(*pp_stack);
4045 PyTuple_SET_ITEM(callargs, nstack, w);
4046 }
4047 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004048}
4049
4050static PyObject *
4051load_args(PyObject ***pp_stack, int na)
4052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 PyObject *args = PyTuple_New(na);
4054 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 if (args == NULL)
4057 return NULL;
4058 while (--na >= 0) {
4059 w = EXT_POP(*pp_stack);
4060 PyTuple_SET_ITEM(args, na, w);
4061 }
4062 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004063}
4064
4065static PyObject *
4066do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4067{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 PyObject *callargs = NULL;
4069 PyObject *kwdict = NULL;
4070 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004072 if (nk > 0) {
4073 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4074 if (kwdict == NULL)
4075 goto call_fail;
4076 }
4077 callargs = load_args(pp_stack, na);
4078 if (callargs == NULL)
4079 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004080#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004081 /* At this point, we have to look at the type of func to
4082 update the call stats properly. Do it here so as to avoid
4083 exposing the call stats machinery outside ceval.c
4084 */
4085 if (PyFunction_Check(func))
4086 PCALL(PCALL_FUNCTION);
4087 else if (PyMethod_Check(func))
4088 PCALL(PCALL_METHOD);
4089 else if (PyType_Check(func))
4090 PCALL(PCALL_TYPE);
4091 else if (PyCFunction_Check(func))
4092 PCALL(PCALL_CFUNCTION);
4093 else
4094 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004095#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 if (PyCFunction_Check(func)) {
4097 PyThreadState *tstate = PyThreadState_GET();
4098 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4099 }
4100 else
4101 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004102call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 Py_XDECREF(callargs);
4104 Py_XDECREF(kwdict);
4105 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004106}
4107
4108static PyObject *
4109ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4110{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004111 int nstar = 0;
4112 PyObject *callargs = NULL;
4113 PyObject *stararg = NULL;
4114 PyObject *kwdict = NULL;
4115 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 if (flags & CALL_FLAG_KW) {
4118 kwdict = EXT_POP(*pp_stack);
4119 if (!PyDict_Check(kwdict)) {
4120 PyObject *d;
4121 d = PyDict_New();
4122 if (d == NULL)
4123 goto ext_call_fail;
4124 if (PyDict_Update(d, kwdict) != 0) {
4125 Py_DECREF(d);
4126 /* PyDict_Update raises attribute
4127 * error (percolated from an attempt
4128 * to get 'keys' attribute) instead of
4129 * a type error if its second argument
4130 * is not a mapping.
4131 */
4132 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4133 PyErr_Format(PyExc_TypeError,
4134 "%.200s%.200s argument after ** "
4135 "must be a mapping, not %.200s",
4136 PyEval_GetFuncName(func),
4137 PyEval_GetFuncDesc(func),
4138 kwdict->ob_type->tp_name);
4139 }
4140 goto ext_call_fail;
4141 }
4142 Py_DECREF(kwdict);
4143 kwdict = d;
4144 }
4145 }
4146 if (flags & CALL_FLAG_VAR) {
4147 stararg = EXT_POP(*pp_stack);
4148 if (!PyTuple_Check(stararg)) {
4149 PyObject *t = NULL;
4150 t = PySequence_Tuple(stararg);
4151 if (t == NULL) {
4152 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4153 PyErr_Format(PyExc_TypeError,
4154 "%.200s%.200s argument after * "
4155 "must be a sequence, not %200s",
4156 PyEval_GetFuncName(func),
4157 PyEval_GetFuncDesc(func),
4158 stararg->ob_type->tp_name);
4159 }
4160 goto ext_call_fail;
4161 }
4162 Py_DECREF(stararg);
4163 stararg = t;
4164 }
4165 nstar = PyTuple_GET_SIZE(stararg);
4166 }
4167 if (nk > 0) {
4168 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4169 if (kwdict == NULL)
4170 goto ext_call_fail;
4171 }
4172 callargs = update_star_args(na, nstar, stararg, pp_stack);
4173 if (callargs == NULL)
4174 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004175#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 /* At this point, we have to look at the type of func to
4177 update the call stats properly. Do it here so as to avoid
4178 exposing the call stats machinery outside ceval.c
4179 */
4180 if (PyFunction_Check(func))
4181 PCALL(PCALL_FUNCTION);
4182 else if (PyMethod_Check(func))
4183 PCALL(PCALL_METHOD);
4184 else if (PyType_Check(func))
4185 PCALL(PCALL_TYPE);
4186 else if (PyCFunction_Check(func))
4187 PCALL(PCALL_CFUNCTION);
4188 else
4189 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004190#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 if (PyCFunction_Check(func)) {
4192 PyThreadState *tstate = PyThreadState_GET();
4193 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4194 }
4195 else
4196 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004197ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004198 Py_XDECREF(callargs);
4199 Py_XDECREF(kwdict);
4200 Py_XDECREF(stararg);
4201 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004202}
4203
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004204/* Extract a slice index from a PyInt or PyLong or an object with the
4205 nb_index slot defined, and store in *pi.
4206 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4207 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 +00004208 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004209*/
Tim Petersb5196382001-12-16 19:44:20 +00004210/* Note: If v is NULL, return success without storing into *pi. This
4211 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4212 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004213*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004214int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004215_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004216{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 if (v != NULL) {
4218 Py_ssize_t x;
4219 if (PyIndex_Check(v)) {
4220 x = PyNumber_AsSsize_t(v, NULL);
4221 if (x == -1 && PyErr_Occurred())
4222 return 0;
4223 }
4224 else {
4225 PyErr_SetString(PyExc_TypeError,
4226 "slice indices must be integers or "
4227 "None or have an __index__ method");
4228 return 0;
4229 }
4230 *pi = x;
4231 }
4232 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004233}
4234
Guido van Rossum486364b2007-06-30 05:01:58 +00004235#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004236 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004237
Guido van Rossumb209a111997-04-29 18:18:01 +00004238static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004239cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004240{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 int res = 0;
4242 switch (op) {
4243 case PyCmp_IS:
4244 res = (v == w);
4245 break;
4246 case PyCmp_IS_NOT:
4247 res = (v != w);
4248 break;
4249 case PyCmp_IN:
4250 res = PySequence_Contains(w, v);
4251 if (res < 0)
4252 return NULL;
4253 break;
4254 case PyCmp_NOT_IN:
4255 res = PySequence_Contains(w, v);
4256 if (res < 0)
4257 return NULL;
4258 res = !res;
4259 break;
4260 case PyCmp_EXC_MATCH:
4261 if (PyTuple_Check(w)) {
4262 Py_ssize_t i, length;
4263 length = PyTuple_Size(w);
4264 for (i = 0; i < length; i += 1) {
4265 PyObject *exc = PyTuple_GET_ITEM(w, i);
4266 if (!PyExceptionClass_Check(exc)) {
4267 PyErr_SetString(PyExc_TypeError,
4268 CANNOT_CATCH_MSG);
4269 return NULL;
4270 }
4271 }
4272 }
4273 else {
4274 if (!PyExceptionClass_Check(w)) {
4275 PyErr_SetString(PyExc_TypeError,
4276 CANNOT_CATCH_MSG);
4277 return NULL;
4278 }
4279 }
4280 res = PyErr_GivenExceptionMatches(v, w);
4281 break;
4282 default:
4283 return PyObject_RichCompare(v, w, op);
4284 }
4285 v = res ? Py_True : Py_False;
4286 Py_INCREF(v);
4287 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004288}
4289
Thomas Wouters52152252000-08-17 22:55:00 +00004290static PyObject *
4291import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004292{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004293 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004295 x = PyObject_GetAttr(v, name);
4296 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4297 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4298 }
4299 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004300}
Guido van Rossumac7be682001-01-17 15:42:30 +00004301
Thomas Wouters52152252000-08-17 22:55:00 +00004302static int
4303import_all_from(PyObject *locals, PyObject *v)
4304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004305 PyObject *all = PyObject_GetAttrString(v, "__all__");
4306 PyObject *dict, *name, *value;
4307 int skip_leading_underscores = 0;
4308 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 if (all == NULL) {
4311 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4312 return -1; /* Unexpected error */
4313 PyErr_Clear();
4314 dict = PyObject_GetAttrString(v, "__dict__");
4315 if (dict == NULL) {
4316 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4317 return -1;
4318 PyErr_SetString(PyExc_ImportError,
4319 "from-import-* object has no __dict__ and no __all__");
4320 return -1;
4321 }
4322 all = PyMapping_Keys(dict);
4323 Py_DECREF(dict);
4324 if (all == NULL)
4325 return -1;
4326 skip_leading_underscores = 1;
4327 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004329 for (pos = 0, err = 0; ; pos++) {
4330 name = PySequence_GetItem(all, pos);
4331 if (name == NULL) {
4332 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4333 err = -1;
4334 else
4335 PyErr_Clear();
4336 break;
4337 }
4338 if (skip_leading_underscores &&
4339 PyUnicode_Check(name) &&
4340 PyUnicode_AS_UNICODE(name)[0] == '_')
4341 {
4342 Py_DECREF(name);
4343 continue;
4344 }
4345 value = PyObject_GetAttr(v, name);
4346 if (value == NULL)
4347 err = -1;
4348 else if (PyDict_CheckExact(locals))
4349 err = PyDict_SetItem(locals, name, value);
4350 else
4351 err = PyObject_SetItem(locals, name, value);
4352 Py_DECREF(name);
4353 Py_XDECREF(value);
4354 if (err != 0)
4355 break;
4356 }
4357 Py_DECREF(all);
4358 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004359}
4360
Guido van Rossumac7be682001-01-17 15:42:30 +00004361static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004362format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004364 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 if (!obj)
4367 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 obj_str = _PyUnicode_AsString(obj);
4370 if (!obj_str)
4371 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004373 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004374}
Guido van Rossum950361c1997-01-24 13:49:28 +00004375
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004376static void
4377format_exc_unbound(PyCodeObject *co, int oparg)
4378{
4379 PyObject *name;
4380 /* Don't stomp existing exception */
4381 if (PyErr_Occurred())
4382 return;
4383 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4384 name = PyTuple_GET_ITEM(co->co_cellvars,
4385 oparg);
4386 format_exc_check_arg(
4387 PyExc_UnboundLocalError,
4388 UNBOUNDLOCAL_ERROR_MSG,
4389 name);
4390 } else {
4391 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4392 PyTuple_GET_SIZE(co->co_cellvars));
4393 format_exc_check_arg(PyExc_NameError,
4394 UNBOUNDFREE_ERROR_MSG, name);
4395 }
4396}
4397
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004398static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004399unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004400 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004402 /* This function implements 'variable += expr' when both arguments
4403 are (Unicode) strings. */
4404 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4405 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4406 Py_ssize_t new_len = v_len + w_len;
4407 if (new_len < 0) {
4408 PyErr_SetString(PyExc_OverflowError,
4409 "strings are too large to concat");
4410 return NULL;
4411 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004412
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004413 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 /* In the common case, there are 2 references to the value
4415 * stored in 'variable' when the += is performed: one on the
4416 * value stack (in 'v') and one still stored in the
4417 * 'variable'. We try to delete the variable now to reduce
4418 * the refcnt to 1.
4419 */
4420 switch (*next_instr) {
4421 case STORE_FAST:
4422 {
4423 int oparg = PEEKARG();
4424 PyObject **fastlocals = f->f_localsplus;
4425 if (GETLOCAL(oparg) == v)
4426 SETLOCAL(oparg, NULL);
4427 break;
4428 }
4429 case STORE_DEREF:
4430 {
4431 PyObject **freevars = (f->f_localsplus +
4432 f->f_code->co_nlocals);
4433 PyObject *c = freevars[PEEKARG()];
4434 if (PyCell_GET(c) == v)
4435 PyCell_Set(c, NULL);
4436 break;
4437 }
4438 case STORE_NAME:
4439 {
4440 PyObject *names = f->f_code->co_names;
4441 PyObject *name = GETITEM(names, PEEKARG());
4442 PyObject *locals = f->f_locals;
4443 if (PyDict_CheckExact(locals) &&
4444 PyDict_GetItem(locals, name) == v) {
4445 if (PyDict_DelItem(locals, name) != 0) {
4446 PyErr_Clear();
4447 }
4448 }
4449 break;
4450 }
4451 }
4452 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004453
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004454 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 /* Now we own the last reference to 'v', so we can resize it
4456 * in-place.
4457 */
4458 if (PyUnicode_Resize(&v, new_len) != 0) {
4459 /* XXX if PyUnicode_Resize() fails, 'v' has been
4460 * deallocated so it cannot be put back into
4461 * 'variable'. The MemoryError is raised when there
4462 * is no value in 'variable', which might (very
4463 * remotely) be a cause of incompatibilities.
4464 */
4465 return NULL;
4466 }
4467 /* copy 'w' into the newly allocated area of 'v' */
4468 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4469 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4470 return v;
4471 }
4472 else {
4473 /* When in-place resizing is not an option. */
4474 w = PyUnicode_Concat(v, w);
4475 Py_DECREF(v);
4476 return w;
4477 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004478}
4479
Guido van Rossum950361c1997-01-24 13:49:28 +00004480#ifdef DYNAMIC_EXECUTION_PROFILE
4481
Skip Montanarof118cb12001-10-15 20:51:38 +00004482static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004483getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004484{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 int i;
4486 PyObject *l = PyList_New(256);
4487 if (l == NULL) return NULL;
4488 for (i = 0; i < 256; i++) {
4489 PyObject *x = PyLong_FromLong(a[i]);
4490 if (x == NULL) {
4491 Py_DECREF(l);
4492 return NULL;
4493 }
4494 PyList_SetItem(l, i, x);
4495 }
4496 for (i = 0; i < 256; i++)
4497 a[i] = 0;
4498 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004499}
4500
4501PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004502_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004503{
4504#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004506#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 int i;
4508 PyObject *l = PyList_New(257);
4509 if (l == NULL) return NULL;
4510 for (i = 0; i < 257; i++) {
4511 PyObject *x = getarray(dxpairs[i]);
4512 if (x == NULL) {
4513 Py_DECREF(l);
4514 return NULL;
4515 }
4516 PyList_SetItem(l, i, x);
4517 }
4518 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004519#endif
4520}
4521
4522#endif