blob: c0f2874a8f2398a26df2785ed8ee3dd834257f72 [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;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 TARGET(POP_EXCEPT)
1887 {
1888 PyTryBlock *b = PyFrame_BlockPop(f);
1889 if (b->b_type != EXCEPT_HANDLER) {
1890 PyErr_SetString(PyExc_SystemError,
1891 "popped block is not an except handler");
1892 why = WHY_EXCEPTION;
1893 break;
1894 }
1895 UNWIND_EXCEPT_HANDLER(b);
1896 }
1897 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 TARGET(POP_BLOCK)
1900 {
1901 PyTryBlock *b = PyFrame_BlockPop(f);
1902 UNWIND_BLOCK(b);
1903 }
1904 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 PREDICTED(END_FINALLY);
1907 TARGET(END_FINALLY)
1908 v = POP();
1909 if (PyLong_Check(v)) {
1910 why = (enum why_code) PyLong_AS_LONG(v);
1911 assert(why != WHY_YIELD);
1912 if (why == WHY_RETURN ||
1913 why == WHY_CONTINUE)
1914 retval = POP();
1915 if (why == WHY_SILENCED) {
1916 /* An exception was silenced by 'with', we must
1917 manually unwind the EXCEPT_HANDLER block which was
1918 created when the exception was caught, otherwise
1919 the stack will be in an inconsistent state. */
1920 PyTryBlock *b = PyFrame_BlockPop(f);
1921 assert(b->b_type == EXCEPT_HANDLER);
1922 UNWIND_EXCEPT_HANDLER(b);
1923 why = WHY_NOT;
1924 }
1925 }
1926 else if (PyExceptionClass_Check(v)) {
1927 w = POP();
1928 u = POP();
1929 PyErr_Restore(v, w, u);
1930 why = WHY_RERAISE;
1931 break;
1932 }
1933 else if (v != Py_None) {
1934 PyErr_SetString(PyExc_SystemError,
1935 "'finally' pops bad exception");
1936 why = WHY_EXCEPTION;
1937 }
1938 Py_DECREF(v);
1939 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 TARGET(LOAD_BUILD_CLASS)
1942 x = PyDict_GetItemString(f->f_builtins,
1943 "__build_class__");
1944 if (x == NULL) {
1945 PyErr_SetString(PyExc_ImportError,
1946 "__build_class__ not found");
1947 break;
1948 }
1949 Py_INCREF(x);
1950 PUSH(x);
1951 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001952
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 TARGET(STORE_NAME)
1954 w = GETITEM(names, oparg);
1955 v = POP();
1956 if ((x = f->f_locals) != NULL) {
1957 if (PyDict_CheckExact(x))
1958 err = PyDict_SetItem(x, w, v);
1959 else
1960 err = PyObject_SetItem(x, w, v);
1961 Py_DECREF(v);
1962 if (err == 0) DISPATCH();
1963 break;
1964 }
1965 PyErr_Format(PyExc_SystemError,
1966 "no locals found when storing %R", w);
1967 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 TARGET(DELETE_NAME)
1970 w = GETITEM(names, oparg);
1971 if ((x = f->f_locals) != NULL) {
1972 if ((err = PyObject_DelItem(x, w)) != 0)
1973 format_exc_check_arg(PyExc_NameError,
1974 NAME_ERROR_MSG,
1975 w);
1976 break;
1977 }
1978 PyErr_Format(PyExc_SystemError,
1979 "no locals when deleting %R", w);
1980 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001981
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1983 TARGET(UNPACK_SEQUENCE)
1984 v = POP();
1985 if (PyTuple_CheckExact(v) &&
1986 PyTuple_GET_SIZE(v) == oparg) {
1987 PyObject **items = \
1988 ((PyTupleObject *)v)->ob_item;
1989 while (oparg--) {
1990 w = items[oparg];
1991 Py_INCREF(w);
1992 PUSH(w);
1993 }
1994 Py_DECREF(v);
1995 DISPATCH();
1996 } else if (PyList_CheckExact(v) &&
1997 PyList_GET_SIZE(v) == oparg) {
1998 PyObject **items = \
1999 ((PyListObject *)v)->ob_item;
2000 while (oparg--) {
2001 w = items[oparg];
2002 Py_INCREF(w);
2003 PUSH(w);
2004 }
2005 } else if (unpack_iterable(v, oparg, -1,
2006 stack_pointer + oparg)) {
2007 STACKADJ(oparg);
2008 } else {
2009 /* unpack_iterable() raised an exception */
2010 why = WHY_EXCEPTION;
2011 }
2012 Py_DECREF(v);
2013 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002015 TARGET(UNPACK_EX)
2016 {
2017 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2018 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002020 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2021 stack_pointer + totalargs)) {
2022 stack_pointer += totalargs;
2023 } else {
2024 why = WHY_EXCEPTION;
2025 }
2026 Py_DECREF(v);
2027 break;
2028 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 TARGET(STORE_ATTR)
2031 w = GETITEM(names, oparg);
2032 v = TOP();
2033 u = SECOND();
2034 STACKADJ(-2);
2035 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2036 Py_DECREF(v);
2037 Py_DECREF(u);
2038 if (err == 0) DISPATCH();
2039 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002041 TARGET(DELETE_ATTR)
2042 w = GETITEM(names, oparg);
2043 v = POP();
2044 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2045 /* del v.w */
2046 Py_DECREF(v);
2047 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002049 TARGET(STORE_GLOBAL)
2050 w = GETITEM(names, oparg);
2051 v = POP();
2052 err = PyDict_SetItem(f->f_globals, w, v);
2053 Py_DECREF(v);
2054 if (err == 0) DISPATCH();
2055 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 TARGET(DELETE_GLOBAL)
2058 w = GETITEM(names, oparg);
2059 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2060 format_exc_check_arg(
2061 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2062 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002064 TARGET(LOAD_NAME)
2065 w = GETITEM(names, oparg);
2066 if ((v = f->f_locals) == NULL) {
2067 PyErr_Format(PyExc_SystemError,
2068 "no locals when loading %R", w);
2069 why = WHY_EXCEPTION;
2070 break;
2071 }
2072 if (PyDict_CheckExact(v)) {
2073 x = PyDict_GetItem(v, w);
2074 Py_XINCREF(x);
2075 }
2076 else {
2077 x = PyObject_GetItem(v, w);
2078 if (x == NULL && PyErr_Occurred()) {
2079 if (!PyErr_ExceptionMatches(
2080 PyExc_KeyError))
2081 break;
2082 PyErr_Clear();
2083 }
2084 }
2085 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002086 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002087 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002088 x = PyDict_GetItem(f->f_builtins, w);
2089 if (x == NULL) {
2090 format_exc_check_arg(
2091 PyExc_NameError,
2092 NAME_ERROR_MSG, w);
2093 break;
2094 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 }
2096 Py_INCREF(x);
2097 }
2098 PUSH(x);
2099 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002100
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002101 TARGET(LOAD_GLOBAL)
2102 w = GETITEM(names, oparg);
2103 if (PyUnicode_CheckExact(w)) {
2104 /* Inline the PyDict_GetItem() calls.
2105 WARNING: this is an extreme speed hack.
2106 Do not try this at home. */
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002107 Py_hash_t hash = ((PyUnicodeObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 if (hash != -1) {
2109 PyDictObject *d;
2110 PyDictEntry *e;
2111 d = (PyDictObject *)(f->f_globals);
2112 e = d->ma_lookup(d, w, hash);
2113 if (e == NULL) {
2114 x = NULL;
2115 break;
2116 }
2117 x = e->me_value;
2118 if (x != NULL) {
2119 Py_INCREF(x);
2120 PUSH(x);
2121 DISPATCH();
2122 }
2123 d = (PyDictObject *)(f->f_builtins);
2124 e = d->ma_lookup(d, w, hash);
2125 if (e == NULL) {
2126 x = NULL;
2127 break;
2128 }
2129 x = e->me_value;
2130 if (x != NULL) {
2131 Py_INCREF(x);
2132 PUSH(x);
2133 DISPATCH();
2134 }
2135 goto load_global_error;
2136 }
2137 }
2138 /* This is the un-inlined version of the code above */
2139 x = PyDict_GetItem(f->f_globals, w);
2140 if (x == NULL) {
2141 x = PyDict_GetItem(f->f_builtins, w);
2142 if (x == NULL) {
2143 load_global_error:
2144 format_exc_check_arg(
2145 PyExc_NameError,
2146 GLOBAL_NAME_ERROR_MSG, w);
2147 break;
2148 }
2149 }
2150 Py_INCREF(x);
2151 PUSH(x);
2152 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002154 TARGET(DELETE_FAST)
2155 x = GETLOCAL(oparg);
2156 if (x != NULL) {
2157 SETLOCAL(oparg, NULL);
2158 DISPATCH();
2159 }
2160 format_exc_check_arg(
2161 PyExc_UnboundLocalError,
2162 UNBOUNDLOCAL_ERROR_MSG,
2163 PyTuple_GetItem(co->co_varnames, oparg)
2164 );
2165 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002166
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002167 TARGET(DELETE_DEREF)
2168 x = freevars[oparg];
2169 if (PyCell_GET(x) != NULL) {
2170 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002171 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002172 }
2173 err = -1;
2174 format_exc_unbound(co, oparg);
2175 break;
2176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002177 TARGET(LOAD_CLOSURE)
2178 x = freevars[oparg];
2179 Py_INCREF(x);
2180 PUSH(x);
2181 if (x != NULL) DISPATCH();
2182 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 TARGET(LOAD_DEREF)
2185 x = freevars[oparg];
2186 w = PyCell_Get(x);
2187 if (w != NULL) {
2188 PUSH(w);
2189 DISPATCH();
2190 }
2191 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002192 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002193 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002194
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 TARGET(STORE_DEREF)
2196 w = POP();
2197 x = freevars[oparg];
2198 PyCell_Set(x, w);
2199 Py_DECREF(w);
2200 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002201
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002202 TARGET(BUILD_TUPLE)
2203 x = PyTuple_New(oparg);
2204 if (x != NULL) {
2205 for (; --oparg >= 0;) {
2206 w = POP();
2207 PyTuple_SET_ITEM(x, oparg, w);
2208 }
2209 PUSH(x);
2210 DISPATCH();
2211 }
2212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002214 TARGET(BUILD_LIST)
2215 x = PyList_New(oparg);
2216 if (x != NULL) {
2217 for (; --oparg >= 0;) {
2218 w = POP();
2219 PyList_SET_ITEM(x, oparg, w);
2220 }
2221 PUSH(x);
2222 DISPATCH();
2223 }
2224 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002226 TARGET(BUILD_SET)
2227 x = PySet_New(NULL);
2228 if (x != NULL) {
2229 for (; --oparg >= 0;) {
2230 w = POP();
2231 if (err == 0)
2232 err = PySet_Add(x, w);
2233 Py_DECREF(w);
2234 }
2235 if (err != 0) {
2236 Py_DECREF(x);
2237 break;
2238 }
2239 PUSH(x);
2240 DISPATCH();
2241 }
2242 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 TARGET(BUILD_MAP)
2245 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2246 PUSH(x);
2247 if (x != NULL) DISPATCH();
2248 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 TARGET(STORE_MAP)
2251 w = TOP(); /* key */
2252 u = SECOND(); /* value */
2253 v = THIRD(); /* dict */
2254 STACKADJ(-2);
2255 assert (PyDict_CheckExact(v));
2256 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2257 Py_DECREF(u);
2258 Py_DECREF(w);
2259 if (err == 0) DISPATCH();
2260 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002261
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002262 TARGET(MAP_ADD)
2263 w = TOP(); /* key */
2264 u = SECOND(); /* value */
2265 STACKADJ(-2);
2266 v = stack_pointer[-oparg]; /* dict */
2267 assert (PyDict_CheckExact(v));
2268 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2269 Py_DECREF(u);
2270 Py_DECREF(w);
2271 if (err == 0) {
2272 PREDICT(JUMP_ABSOLUTE);
2273 DISPATCH();
2274 }
2275 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 TARGET(LOAD_ATTR)
2278 w = GETITEM(names, oparg);
2279 v = TOP();
2280 x = PyObject_GetAttr(v, w);
2281 Py_DECREF(v);
2282 SET_TOP(x);
2283 if (x != NULL) DISPATCH();
2284 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002286 TARGET(COMPARE_OP)
2287 w = POP();
2288 v = TOP();
2289 x = cmp_outcome(oparg, v, w);
2290 Py_DECREF(v);
2291 Py_DECREF(w);
2292 SET_TOP(x);
2293 if (x == NULL) break;
2294 PREDICT(POP_JUMP_IF_FALSE);
2295 PREDICT(POP_JUMP_IF_TRUE);
2296 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002297
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002298 TARGET(IMPORT_NAME)
2299 w = GETITEM(names, oparg);
2300 x = PyDict_GetItemString(f->f_builtins, "__import__");
2301 if (x == NULL) {
2302 PyErr_SetString(PyExc_ImportError,
2303 "__import__ not found");
2304 break;
2305 }
2306 Py_INCREF(x);
2307 v = POP();
2308 u = TOP();
2309 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2310 w = PyTuple_Pack(5,
2311 w,
2312 f->f_globals,
2313 f->f_locals == NULL ?
2314 Py_None : f->f_locals,
2315 v,
2316 u);
2317 else
2318 w = PyTuple_Pack(4,
2319 w,
2320 f->f_globals,
2321 f->f_locals == NULL ?
2322 Py_None : f->f_locals,
2323 v);
2324 Py_DECREF(v);
2325 Py_DECREF(u);
2326 if (w == NULL) {
2327 u = POP();
2328 Py_DECREF(x);
2329 x = NULL;
2330 break;
2331 }
2332 READ_TIMESTAMP(intr0);
2333 v = x;
2334 x = PyEval_CallObject(v, w);
2335 Py_DECREF(v);
2336 READ_TIMESTAMP(intr1);
2337 Py_DECREF(w);
2338 SET_TOP(x);
2339 if (x != NULL) DISPATCH();
2340 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002341
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002342 TARGET(IMPORT_STAR)
2343 v = POP();
2344 PyFrame_FastToLocals(f);
2345 if ((x = f->f_locals) == NULL) {
2346 PyErr_SetString(PyExc_SystemError,
2347 "no locals found during 'import *'");
2348 break;
2349 }
2350 READ_TIMESTAMP(intr0);
2351 err = import_all_from(x, v);
2352 READ_TIMESTAMP(intr1);
2353 PyFrame_LocalsToFast(f, 0);
2354 Py_DECREF(v);
2355 if (err == 0) DISPATCH();
2356 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002358 TARGET(IMPORT_FROM)
2359 w = GETITEM(names, oparg);
2360 v = TOP();
2361 READ_TIMESTAMP(intr0);
2362 x = import_from(v, w);
2363 READ_TIMESTAMP(intr1);
2364 PUSH(x);
2365 if (x != NULL) DISPATCH();
2366 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002368 TARGET(JUMP_FORWARD)
2369 JUMPBY(oparg);
2370 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002372 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2373 TARGET(POP_JUMP_IF_FALSE)
2374 w = POP();
2375 if (w == Py_True) {
2376 Py_DECREF(w);
2377 FAST_DISPATCH();
2378 }
2379 if (w == Py_False) {
2380 Py_DECREF(w);
2381 JUMPTO(oparg);
2382 FAST_DISPATCH();
2383 }
2384 err = PyObject_IsTrue(w);
2385 Py_DECREF(w);
2386 if (err > 0)
2387 err = 0;
2388 else if (err == 0)
2389 JUMPTO(oparg);
2390 else
2391 break;
2392 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2395 TARGET(POP_JUMP_IF_TRUE)
2396 w = POP();
2397 if (w == Py_False) {
2398 Py_DECREF(w);
2399 FAST_DISPATCH();
2400 }
2401 if (w == Py_True) {
2402 Py_DECREF(w);
2403 JUMPTO(oparg);
2404 FAST_DISPATCH();
2405 }
2406 err = PyObject_IsTrue(w);
2407 Py_DECREF(w);
2408 if (err > 0) {
2409 err = 0;
2410 JUMPTO(oparg);
2411 }
2412 else if (err == 0)
2413 ;
2414 else
2415 break;
2416 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002417
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 TARGET(JUMP_IF_FALSE_OR_POP)
2419 w = TOP();
2420 if (w == Py_True) {
2421 STACKADJ(-1);
2422 Py_DECREF(w);
2423 FAST_DISPATCH();
2424 }
2425 if (w == Py_False) {
2426 JUMPTO(oparg);
2427 FAST_DISPATCH();
2428 }
2429 err = PyObject_IsTrue(w);
2430 if (err > 0) {
2431 STACKADJ(-1);
2432 Py_DECREF(w);
2433 err = 0;
2434 }
2435 else if (err == 0)
2436 JUMPTO(oparg);
2437 else
2438 break;
2439 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002441 TARGET(JUMP_IF_TRUE_OR_POP)
2442 w = TOP();
2443 if (w == Py_False) {
2444 STACKADJ(-1);
2445 Py_DECREF(w);
2446 FAST_DISPATCH();
2447 }
2448 if (w == Py_True) {
2449 JUMPTO(oparg);
2450 FAST_DISPATCH();
2451 }
2452 err = PyObject_IsTrue(w);
2453 if (err > 0) {
2454 err = 0;
2455 JUMPTO(oparg);
2456 }
2457 else if (err == 0) {
2458 STACKADJ(-1);
2459 Py_DECREF(w);
2460 }
2461 else
2462 break;
2463 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002465 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2466 TARGET(JUMP_ABSOLUTE)
2467 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002468#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002469 /* Enabling this path speeds-up all while and for-loops by bypassing
2470 the per-loop checks for signals. By default, this should be turned-off
2471 because it prevents detection of a control-break in tight loops like
2472 "while 1: pass". Compile with this option turned-on when you need
2473 the speed-up and do not need break checking inside tight loops (ones
2474 that contain only instructions ending with FAST_DISPATCH).
2475 */
2476 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002477#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002479#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002481 TARGET(GET_ITER)
2482 /* before: [obj]; after [getiter(obj)] */
2483 v = TOP();
2484 x = PyObject_GetIter(v);
2485 Py_DECREF(v);
2486 if (x != NULL) {
2487 SET_TOP(x);
2488 PREDICT(FOR_ITER);
2489 DISPATCH();
2490 }
2491 STACKADJ(-1);
2492 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002494 PREDICTED_WITH_ARG(FOR_ITER);
2495 TARGET(FOR_ITER)
2496 /* before: [iter]; after: [iter, iter()] *or* [] */
2497 v = TOP();
2498 x = (*v->ob_type->tp_iternext)(v);
2499 if (x != NULL) {
2500 PUSH(x);
2501 PREDICT(STORE_FAST);
2502 PREDICT(UNPACK_SEQUENCE);
2503 DISPATCH();
2504 }
2505 if (PyErr_Occurred()) {
2506 if (!PyErr_ExceptionMatches(
2507 PyExc_StopIteration))
2508 break;
2509 PyErr_Clear();
2510 }
2511 /* iterator ended normally */
2512 x = v = POP();
2513 Py_DECREF(v);
2514 JUMPBY(oparg);
2515 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002517 TARGET(BREAK_LOOP)
2518 why = WHY_BREAK;
2519 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002521 TARGET(CONTINUE_LOOP)
2522 retval = PyLong_FromLong(oparg);
2523 if (!retval) {
2524 x = NULL;
2525 break;
2526 }
2527 why = WHY_CONTINUE;
2528 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002530 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2531 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2532 TARGET(SETUP_FINALLY)
2533 _setup_finally:
2534 /* NOTE: If you add any new block-setup opcodes that
2535 are not try/except/finally handlers, you may need
2536 to update the PyGen_NeedsFinalizing() function.
2537 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002539 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2540 STACK_LEVEL());
2541 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002543 TARGET(SETUP_WITH)
2544 {
2545 static PyObject *exit, *enter;
2546 w = TOP();
2547 x = special_lookup(w, "__exit__", &exit);
2548 if (!x)
2549 break;
2550 SET_TOP(x);
2551 u = special_lookup(w, "__enter__", &enter);
2552 Py_DECREF(w);
2553 if (!u) {
2554 x = NULL;
2555 break;
2556 }
2557 x = PyObject_CallFunctionObjArgs(u, NULL);
2558 Py_DECREF(u);
2559 if (!x)
2560 break;
2561 /* Setup the finally block before pushing the result
2562 of __enter__ on the stack. */
2563 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2564 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002566 PUSH(x);
2567 DISPATCH();
2568 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 TARGET(WITH_CLEANUP)
2571 {
2572 /* At the top of the stack are 1-3 values indicating
2573 how/why we entered the finally clause:
2574 - TOP = None
2575 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2576 - TOP = WHY_*; no retval below it
2577 - (TOP, SECOND, THIRD) = exc_info()
2578 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2579 Below them is EXIT, the context.__exit__ bound method.
2580 In the last case, we must call
2581 EXIT(TOP, SECOND, THIRD)
2582 otherwise we must call
2583 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002584
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002585 In the first two cases, we remove EXIT from the
2586 stack, leaving the rest in the same order. In the
2587 third case, we shift the bottom 3 values of the
2588 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002590 In addition, if the stack represents an exception,
2591 *and* the function call returns a 'true' value, we
2592 push WHY_SILENCED onto the stack. END_FINALLY will
2593 then not re-raise the exception. (But non-local
2594 gotos should still be resumed.)
2595 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002597 PyObject *exit_func;
2598 u = TOP();
2599 if (u == Py_None) {
2600 (void)POP();
2601 exit_func = TOP();
2602 SET_TOP(u);
2603 v = w = Py_None;
2604 }
2605 else if (PyLong_Check(u)) {
2606 (void)POP();
2607 switch(PyLong_AsLong(u)) {
2608 case WHY_RETURN:
2609 case WHY_CONTINUE:
2610 /* Retval in TOP. */
2611 exit_func = SECOND();
2612 SET_SECOND(TOP());
2613 SET_TOP(u);
2614 break;
2615 default:
2616 exit_func = TOP();
2617 SET_TOP(u);
2618 break;
2619 }
2620 u = v = w = Py_None;
2621 }
2622 else {
2623 PyObject *tp, *exc, *tb;
2624 PyTryBlock *block;
2625 v = SECOND();
2626 w = THIRD();
2627 tp = FOURTH();
2628 exc = PEEK(5);
2629 tb = PEEK(6);
2630 exit_func = PEEK(7);
2631 SET_VALUE(7, tb);
2632 SET_VALUE(6, exc);
2633 SET_VALUE(5, tp);
2634 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2635 SET_FOURTH(NULL);
2636 /* We just shifted the stack down, so we have
2637 to tell the except handler block that the
2638 values are lower than it expects. */
2639 block = &f->f_blockstack[f->f_iblock - 1];
2640 assert(block->b_type == EXCEPT_HANDLER);
2641 block->b_level--;
2642 }
2643 /* XXX Not the fastest way to call it... */
2644 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2645 NULL);
2646 Py_DECREF(exit_func);
2647 if (x == NULL)
2648 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 if (u != Py_None)
2651 err = PyObject_IsTrue(x);
2652 else
2653 err = 0;
2654 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 if (err < 0)
2657 break; /* Go to error exit */
2658 else if (err > 0) {
2659 err = 0;
2660 /* There was an exception and a True return */
2661 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2662 }
2663 PREDICT(END_FINALLY);
2664 break;
2665 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002667 TARGET(CALL_FUNCTION)
2668 {
2669 PyObject **sp;
2670 PCALL(PCALL_ALL);
2671 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002672#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002674#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002676#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002677 stack_pointer = sp;
2678 PUSH(x);
2679 if (x != NULL)
2680 DISPATCH();
2681 break;
2682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2685 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2686 TARGET(CALL_FUNCTION_VAR_KW)
2687 _call_function_var_kw:
2688 {
2689 int na = oparg & 0xff;
2690 int nk = (oparg>>8) & 0xff;
2691 int flags = (opcode - CALL_FUNCTION) & 3;
2692 int n = na + 2 * nk;
2693 PyObject **pfunc, *func, **sp;
2694 PCALL(PCALL_ALL);
2695 if (flags & CALL_FLAG_VAR)
2696 n++;
2697 if (flags & CALL_FLAG_KW)
2698 n++;
2699 pfunc = stack_pointer - n - 1;
2700 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002702 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002703 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002704 PyObject *self = PyMethod_GET_SELF(func);
2705 Py_INCREF(self);
2706 func = PyMethod_GET_FUNCTION(func);
2707 Py_INCREF(func);
2708 Py_DECREF(*pfunc);
2709 *pfunc = self;
2710 na++;
2711 n++;
2712 } else
2713 Py_INCREF(func);
2714 sp = stack_pointer;
2715 READ_TIMESTAMP(intr0);
2716 x = ext_do_call(func, &sp, flags, na, nk);
2717 READ_TIMESTAMP(intr1);
2718 stack_pointer = sp;
2719 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 while (stack_pointer > pfunc) {
2722 w = POP();
2723 Py_DECREF(w);
2724 }
2725 PUSH(x);
2726 if (x != NULL)
2727 DISPATCH();
2728 break;
2729 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002730
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002731 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2732 TARGET(MAKE_FUNCTION)
2733 _make_function:
2734 {
2735 int posdefaults = oparg & 0xff;
2736 int kwdefaults = (oparg>>8) & 0xff;
2737 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 v = POP(); /* code object */
2740 x = PyFunction_New(v, f->f_globals);
2741 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 if (x != NULL && opcode == MAKE_CLOSURE) {
2744 v = POP();
2745 if (PyFunction_SetClosure(x, v) != 0) {
2746 /* Can't happen unless bytecode is corrupt. */
2747 why = WHY_EXCEPTION;
2748 }
2749 Py_DECREF(v);
2750 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (x != NULL && num_annotations > 0) {
2753 Py_ssize_t name_ix;
2754 u = POP(); /* names of args with annotations */
2755 v = PyDict_New();
2756 if (v == NULL) {
2757 Py_DECREF(x);
2758 x = NULL;
2759 break;
2760 }
2761 name_ix = PyTuple_Size(u);
2762 assert(num_annotations == name_ix+1);
2763 while (name_ix > 0) {
2764 --name_ix;
2765 t = PyTuple_GET_ITEM(u, name_ix);
2766 w = POP();
2767 /* XXX(nnorwitz): check for errors */
2768 PyDict_SetItem(v, t, w);
2769 Py_DECREF(w);
2770 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 if (PyFunction_SetAnnotations(x, v) != 0) {
2773 /* Can't happen unless
2774 PyFunction_SetAnnotations changes. */
2775 why = WHY_EXCEPTION;
2776 }
2777 Py_DECREF(v);
2778 Py_DECREF(u);
2779 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 /* XXX Maybe this should be a separate opcode? */
2782 if (x != NULL && posdefaults > 0) {
2783 v = PyTuple_New(posdefaults);
2784 if (v == NULL) {
2785 Py_DECREF(x);
2786 x = NULL;
2787 break;
2788 }
2789 while (--posdefaults >= 0) {
2790 w = POP();
2791 PyTuple_SET_ITEM(v, posdefaults, w);
2792 }
2793 if (PyFunction_SetDefaults(x, v) != 0) {
2794 /* Can't happen unless
2795 PyFunction_SetDefaults changes. */
2796 why = WHY_EXCEPTION;
2797 }
2798 Py_DECREF(v);
2799 }
2800 if (x != NULL && kwdefaults > 0) {
2801 v = PyDict_New();
2802 if (v == NULL) {
2803 Py_DECREF(x);
2804 x = NULL;
2805 break;
2806 }
2807 while (--kwdefaults >= 0) {
2808 w = POP(); /* default value */
2809 u = POP(); /* kw only arg name */
2810 /* XXX(nnorwitz): check for errors */
2811 PyDict_SetItem(v, u, w);
2812 Py_DECREF(w);
2813 Py_DECREF(u);
2814 }
2815 if (PyFunction_SetKwDefaults(x, v) != 0) {
2816 /* Can't happen unless
2817 PyFunction_SetKwDefaults changes. */
2818 why = WHY_EXCEPTION;
2819 }
2820 Py_DECREF(v);
2821 }
2822 PUSH(x);
2823 break;
2824 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002826 TARGET(BUILD_SLICE)
2827 if (oparg == 3)
2828 w = POP();
2829 else
2830 w = NULL;
2831 v = POP();
2832 u = TOP();
2833 x = PySlice_New(u, v, w);
2834 Py_DECREF(u);
2835 Py_DECREF(v);
2836 Py_XDECREF(w);
2837 SET_TOP(x);
2838 if (x != NULL) DISPATCH();
2839 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002840
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 TARGET(EXTENDED_ARG)
2842 opcode = NEXTOP();
2843 oparg = oparg<<16 | NEXTARG();
2844 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002845
Antoine Pitrou042b1282010-08-13 21:15:58 +00002846#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002848#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002849 default:
2850 fprintf(stderr,
2851 "XXX lineno: %d, opcode: %d\n",
2852 PyFrame_GetLineNumber(f),
2853 opcode);
2854 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2855 why = WHY_EXCEPTION;
2856 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002857
2858#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002860#endif
2861
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 if (why == WHY_NOT) {
2871 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002872#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002873 /* This check is expensive! */
2874 if (PyErr_Occurred())
2875 fprintf(stderr,
2876 "XXX undetected error\n");
2877 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002878#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 READ_TIMESTAMP(loop1);
2880 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002881#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002883#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 }
2885 why = WHY_EXCEPTION;
2886 x = Py_None;
2887 err = 0;
2888 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002890 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002892 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2893 if (!PyErr_Occurred()) {
2894 PyErr_SetString(PyExc_SystemError,
2895 "error return without exception set");
2896 why = WHY_EXCEPTION;
2897 }
2898 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002899#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 else {
2901 /* This check is expensive! */
2902 if (PyErr_Occurred()) {
2903 char buf[128];
2904 sprintf(buf, "Stack unwind with exception "
2905 "set and why=%d", why);
2906 Py_FatalError(buf);
2907 }
2908 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002909#endif
2910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002911 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (why == WHY_EXCEPTION) {
2914 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002916 if (tstate->c_tracefunc != NULL)
2917 call_exc_trace(tstate->c_tracefunc,
2918 tstate->c_traceobj, f);
2919 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 if (why == WHY_RERAISE)
2924 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002927
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002928fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002929 while (why != WHY_NOT && f->f_iblock > 0) {
2930 /* Peek at the current block. */
2931 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002933 assert(why != WHY_YIELD);
2934 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2935 why = WHY_NOT;
2936 JUMPTO(PyLong_AS_LONG(retval));
2937 Py_DECREF(retval);
2938 break;
2939 }
2940 /* Now we have to pop the block. */
2941 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002943 if (b->b_type == EXCEPT_HANDLER) {
2944 UNWIND_EXCEPT_HANDLER(b);
2945 continue;
2946 }
2947 UNWIND_BLOCK(b);
2948 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2949 why = WHY_NOT;
2950 JUMPTO(b->b_handler);
2951 break;
2952 }
2953 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2954 || b->b_type == SETUP_FINALLY)) {
2955 PyObject *exc, *val, *tb;
2956 int handler = b->b_handler;
2957 /* Beware, this invalidates all b->b_* fields */
2958 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2959 PUSH(tstate->exc_traceback);
2960 PUSH(tstate->exc_value);
2961 if (tstate->exc_type != NULL) {
2962 PUSH(tstate->exc_type);
2963 }
2964 else {
2965 Py_INCREF(Py_None);
2966 PUSH(Py_None);
2967 }
2968 PyErr_Fetch(&exc, &val, &tb);
2969 /* Make the raw exception data
2970 available to the handler,
2971 so a program can emulate the
2972 Python main loop. */
2973 PyErr_NormalizeException(
2974 &exc, &val, &tb);
2975 PyException_SetTraceback(val, tb);
2976 Py_INCREF(exc);
2977 tstate->exc_type = exc;
2978 Py_INCREF(val);
2979 tstate->exc_value = val;
2980 tstate->exc_traceback = tb;
2981 if (tb == NULL)
2982 tb = Py_None;
2983 Py_INCREF(tb);
2984 PUSH(tb);
2985 PUSH(val);
2986 PUSH(exc);
2987 why = WHY_NOT;
2988 JUMPTO(handler);
2989 break;
2990 }
2991 if (b->b_type == SETUP_FINALLY) {
2992 if (why & (WHY_RETURN | WHY_CONTINUE))
2993 PUSH(retval);
2994 PUSH(PyLong_FromLong((long)why));
2995 why = WHY_NOT;
2996 JUMPTO(b->b_handler);
2997 break;
2998 }
2999 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003003 if (why != WHY_NOT)
3004 break;
3005 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003008
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 assert(why != WHY_YIELD);
3010 /* Pop remaining stack entries. */
3011 while (!EMPTY()) {
3012 v = POP();
3013 Py_XDECREF(v);
3014 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 if (why != WHY_RETURN)
3017 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003018
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003019fast_yield:
Benjamin Peterson83195c32011-07-03 13:44:00 -05003020 if (co->co_flags & CO_GENERATOR && (why == WHY_YIELD || why == WHY_RETURN))
3021 /* Put aside the current exception state and restore that of the
3022 calling frame. */
3023 SWAP_EXC_STATE();
3024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 if (tstate->use_tracing) {
3026 if (tstate->c_tracefunc) {
3027 if (why == WHY_RETURN || why == WHY_YIELD) {
3028 if (call_trace(tstate->c_tracefunc,
3029 tstate->c_traceobj, f,
3030 PyTrace_RETURN, retval)) {
3031 Py_XDECREF(retval);
3032 retval = NULL;
3033 why = WHY_EXCEPTION;
3034 }
3035 }
3036 else if (why == WHY_EXCEPTION) {
3037 call_trace_protected(tstate->c_tracefunc,
3038 tstate->c_traceobj, f,
3039 PyTrace_RETURN, NULL);
3040 }
3041 }
3042 if (tstate->c_profilefunc) {
3043 if (why == WHY_EXCEPTION)
3044 call_trace_protected(tstate->c_profilefunc,
3045 tstate->c_profileobj, f,
3046 PyTrace_RETURN, NULL);
3047 else if (call_trace(tstate->c_profilefunc,
3048 tstate->c_profileobj, f,
3049 PyTrace_RETURN, retval)) {
3050 Py_XDECREF(retval);
3051 retval = NULL;
3052 why = WHY_EXCEPTION;
3053 }
3054 }
3055 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003057 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003058exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 Py_LeaveRecursiveCall();
3060 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003063}
3064
Guido van Rossumc2e20742006-02-27 22:32:47 +00003065/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003066 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003067 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003068
Tim Peters6d6c1a32001-08-02 04:15:00 +00003069PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003070PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 PyObject **args, int argcount, PyObject **kws, int kwcount,
3072 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003073{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003074 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 register PyFrameObject *f;
3076 register PyObject *retval = NULL;
3077 register PyObject **fastlocals, **freevars;
3078 PyThreadState *tstate = PyThreadState_GET();
3079 PyObject *x, *u;
3080 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 if (globals == NULL) {
3083 PyErr_SetString(PyExc_SystemError,
3084 "PyEval_EvalCodeEx: NULL globals");
3085 return NULL;
3086 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 assert(tstate != NULL);
3089 assert(globals != NULL);
3090 f = PyFrame_New(tstate, co, globals, locals);
3091 if (f == NULL)
3092 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003094 fastlocals = f->f_localsplus;
3095 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3098 int i;
3099 int n = argcount;
3100 PyObject *kwdict = NULL;
3101 if (co->co_flags & CO_VARKEYWORDS) {
3102 kwdict = PyDict_New();
3103 if (kwdict == NULL)
3104 goto fail;
3105 i = total_args;
3106 if (co->co_flags & CO_VARARGS)
3107 i++;
3108 SETLOCAL(i, kwdict);
3109 }
3110 if (argcount > co->co_argcount) {
3111 if (!(co->co_flags & CO_VARARGS)) {
3112 PyErr_Format(PyExc_TypeError,
3113 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003114 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003115 co->co_name,
3116 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003117 co->co_argcount,
3118 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonaa7fbd92010-09-25 03:25:42 +00003119 argcount + kwcount);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 goto fail;
3121 }
3122 n = co->co_argcount;
3123 }
3124 for (i = 0; i < n; i++) {
3125 x = args[i];
3126 Py_INCREF(x);
3127 SETLOCAL(i, x);
3128 }
3129 if (co->co_flags & CO_VARARGS) {
3130 u = PyTuple_New(argcount - n);
3131 if (u == NULL)
3132 goto fail;
3133 SETLOCAL(total_args, u);
3134 for (i = n; i < argcount; i++) {
3135 x = args[i];
3136 Py_INCREF(x);
3137 PyTuple_SET_ITEM(u, i-n, x);
3138 }
3139 }
3140 for (i = 0; i < kwcount; i++) {
3141 PyObject **co_varnames;
3142 PyObject *keyword = kws[2*i];
3143 PyObject *value = kws[2*i + 1];
3144 int j;
3145 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3146 PyErr_Format(PyExc_TypeError,
3147 "%U() keywords must be strings",
3148 co->co_name);
3149 goto fail;
3150 }
3151 /* Speed hack: do raw pointer compares. As names are
3152 normally interned this should almost always hit. */
3153 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3154 for (j = 0; j < total_args; j++) {
3155 PyObject *nm = co_varnames[j];
3156 if (nm == keyword)
3157 goto kw_found;
3158 }
3159 /* Slow fallback, just in case */
3160 for (j = 0; j < total_args; j++) {
3161 PyObject *nm = co_varnames[j];
3162 int cmp = PyObject_RichCompareBool(
3163 keyword, nm, Py_EQ);
3164 if (cmp > 0)
3165 goto kw_found;
3166 else if (cmp < 0)
3167 goto fail;
3168 }
3169 if (j >= total_args && kwdict == NULL) {
3170 PyErr_Format(PyExc_TypeError,
3171 "%U() got an unexpected "
3172 "keyword argument '%S'",
3173 co->co_name,
3174 keyword);
3175 goto fail;
3176 }
3177 PyDict_SetItem(kwdict, keyword, value);
3178 continue;
3179 kw_found:
3180 if (GETLOCAL(j) != NULL) {
3181 PyErr_Format(PyExc_TypeError,
3182 "%U() got multiple "
3183 "values for keyword "
3184 "argument '%S'",
3185 co->co_name,
3186 keyword);
3187 goto fail;
3188 }
3189 Py_INCREF(value);
3190 SETLOCAL(j, value);
3191 }
3192 if (co->co_kwonlyargcount > 0) {
3193 for (i = co->co_argcount; i < total_args; i++) {
3194 PyObject *name;
3195 if (GETLOCAL(i) != NULL)
3196 continue;
3197 name = PyTuple_GET_ITEM(co->co_varnames, i);
3198 if (kwdefs != NULL) {
3199 PyObject *def = PyDict_GetItem(kwdefs, name);
3200 if (def) {
3201 Py_INCREF(def);
3202 SETLOCAL(i, def);
3203 continue;
3204 }
3205 }
3206 PyErr_Format(PyExc_TypeError,
3207 "%U() needs keyword-only argument %S",
3208 co->co_name, name);
3209 goto fail;
3210 }
3211 }
3212 if (argcount < co->co_argcount) {
3213 int m = co->co_argcount - defcount;
3214 for (i = argcount; i < m; i++) {
3215 if (GETLOCAL(i) == NULL) {
3216 int j, given = 0;
3217 for (j = 0; j < co->co_argcount; j++)
3218 if (GETLOCAL(j))
3219 given++;
3220 PyErr_Format(PyExc_TypeError,
3221 "%U() takes %s %d "
3222 "argument%s "
3223 "(%d given)",
3224 co->co_name,
3225 ((co->co_flags & CO_VARARGS) ||
3226 defcount) ? "at least"
3227 : "exactly",
3228 m, m == 1 ? "" : "s", given);
3229 goto fail;
3230 }
3231 }
3232 if (n > m)
3233 i = n - m;
3234 else
3235 i = 0;
3236 for (; i < defcount; i++) {
3237 if (GETLOCAL(m+i) == NULL) {
3238 PyObject *def = defs[i];
3239 Py_INCREF(def);
3240 SETLOCAL(m+i, def);
3241 }
3242 }
3243 }
3244 }
3245 else if (argcount > 0 || kwcount > 0) {
3246 PyErr_Format(PyExc_TypeError,
3247 "%U() takes no arguments (%d given)",
3248 co->co_name,
3249 argcount + kwcount);
3250 goto fail;
3251 }
3252 /* Allocate and initialize storage for cell vars, and copy free
3253 vars into frame. This isn't too efficient right now. */
3254 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3255 int i, j, nargs, found;
3256 Py_UNICODE *cellname, *argname;
3257 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 nargs = total_args;
3260 if (co->co_flags & CO_VARARGS)
3261 nargs++;
3262 if (co->co_flags & CO_VARKEYWORDS)
3263 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003264
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003265 /* Initialize each cell var, taking into account
3266 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 Should arrange for the compiler to put cellvars
3269 that are arguments at the beginning of the cellvars
3270 list so that we can march over it more efficiently?
3271 */
3272 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3273 cellname = PyUnicode_AS_UNICODE(
3274 PyTuple_GET_ITEM(co->co_cellvars, i));
3275 found = 0;
3276 for (j = 0; j < nargs; j++) {
3277 argname = PyUnicode_AS_UNICODE(
3278 PyTuple_GET_ITEM(co->co_varnames, j));
3279 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3280 c = PyCell_New(GETLOCAL(j));
3281 if (c == NULL)
3282 goto fail;
3283 GETLOCAL(co->co_nlocals + i) = c;
3284 found = 1;
3285 break;
3286 }
3287 }
3288 if (found == 0) {
3289 c = PyCell_New(NULL);
3290 if (c == NULL)
3291 goto fail;
3292 SETLOCAL(co->co_nlocals + i, c);
3293 }
3294 }
3295 }
3296 if (PyTuple_GET_SIZE(co->co_freevars)) {
3297 int i;
3298 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3299 PyObject *o = PyTuple_GET_ITEM(closure, i);
3300 Py_INCREF(o);
3301 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3302 }
3303 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 if (co->co_flags & CO_GENERATOR) {
3306 /* Don't need to keep the reference to f_back, it will be set
3307 * when the generator is resumed. */
3308 Py_XDECREF(f->f_back);
3309 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 /* Create a new generator that owns the ready to run frame
3314 * and return that as the value. */
3315 return PyGen_New(f);
3316 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003318 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003319
Thomas Woutersce272b62007-09-19 21:19:28 +00003320fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003322 /* decref'ing the frame can cause __del__ methods to get invoked,
3323 which can call back into Python. While we're done with the
3324 current Python frame (f), the associated C stack is still in use,
3325 so recursion_depth must be boosted for the duration.
3326 */
3327 assert(tstate != NULL);
3328 ++tstate->recursion_depth;
3329 Py_DECREF(f);
3330 --tstate->recursion_depth;
3331 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003332}
3333
3334
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003335static PyObject *
3336special_lookup(PyObject *o, char *meth, PyObject **cache)
3337{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003338 PyObject *res;
3339 res = _PyObject_LookupSpecial(o, meth, cache);
3340 if (res == NULL && !PyErr_Occurred()) {
3341 PyErr_SetObject(PyExc_AttributeError, *cache);
3342 return NULL;
3343 }
3344 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003345}
3346
3347
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003348/* Logic for the raise statement (too complicated for inlining).
3349 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003350static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003351do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003352{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003355 if (exc == NULL) {
3356 /* Reraise */
3357 PyThreadState *tstate = PyThreadState_GET();
3358 PyObject *tb;
3359 type = tstate->exc_type;
3360 value = tstate->exc_value;
3361 tb = tstate->exc_traceback;
3362 if (type == Py_None) {
3363 PyErr_SetString(PyExc_RuntimeError,
3364 "No active exception to reraise");
3365 return WHY_EXCEPTION;
3366 }
3367 Py_XINCREF(type);
3368 Py_XINCREF(value);
3369 Py_XINCREF(tb);
3370 PyErr_Restore(type, value, tb);
3371 return WHY_RERAISE;
3372 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003374 /* We support the following forms of raise:
3375 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003376 raise <instance>
3377 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 if (PyExceptionClass_Check(exc)) {
3380 type = exc;
3381 value = PyObject_CallObject(exc, NULL);
3382 if (value == NULL)
3383 goto raise_error;
3384 }
3385 else if (PyExceptionInstance_Check(exc)) {
3386 value = exc;
3387 type = PyExceptionInstance_Class(exc);
3388 Py_INCREF(type);
3389 }
3390 else {
3391 /* Not something you can raise. You get an exception
3392 anyway, just not what you specified :-) */
3393 Py_DECREF(exc);
3394 PyErr_SetString(PyExc_TypeError,
3395 "exceptions must derive from BaseException");
3396 goto raise_error;
3397 }
Collin Winter828f04a2007-08-31 00:04:24 +00003398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003399 if (cause) {
3400 PyObject *fixed_cause;
3401 if (PyExceptionClass_Check(cause)) {
3402 fixed_cause = PyObject_CallObject(cause, NULL);
3403 if (fixed_cause == NULL)
3404 goto raise_error;
3405 Py_DECREF(cause);
3406 }
3407 else if (PyExceptionInstance_Check(cause)) {
3408 fixed_cause = cause;
3409 }
3410 else {
3411 PyErr_SetString(PyExc_TypeError,
3412 "exception causes must derive from "
3413 "BaseException");
3414 goto raise_error;
3415 }
3416 PyException_SetCause(value, fixed_cause);
3417 }
Collin Winter828f04a2007-08-31 00:04:24 +00003418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 PyErr_SetObject(type, value);
3420 /* PyErr_SetObject incref's its arguments */
3421 Py_XDECREF(value);
3422 Py_XDECREF(type);
3423 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003424
3425raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 Py_XDECREF(value);
3427 Py_XDECREF(type);
3428 Py_XDECREF(cause);
3429 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003430}
3431
Tim Petersd6d010b2001-06-21 02:49:55 +00003432/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003433 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003434
Guido van Rossum0368b722007-05-11 16:50:42 +00003435 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3436 with a variable target.
3437*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003438
Barry Warsawe42b18f1997-08-25 22:13:04 +00003439static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003440unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003441{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 int i = 0, j = 0;
3443 Py_ssize_t ll = 0;
3444 PyObject *it; /* iter(v) */
3445 PyObject *w;
3446 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003449
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003450 it = PyObject_GetIter(v);
3451 if (it == NULL)
3452 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003453
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 for (; i < argcnt; i++) {
3455 w = PyIter_Next(it);
3456 if (w == NULL) {
3457 /* Iterator done, via error or exhaustion. */
3458 if (!PyErr_Occurred()) {
3459 PyErr_Format(PyExc_ValueError,
3460 "need more than %d value%s to unpack",
3461 i, i == 1 ? "" : "s");
3462 }
3463 goto Error;
3464 }
3465 *--sp = w;
3466 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003467
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003468 if (argcntafter == -1) {
3469 /* We better have exhausted the iterator now. */
3470 w = PyIter_Next(it);
3471 if (w == NULL) {
3472 if (PyErr_Occurred())
3473 goto Error;
3474 Py_DECREF(it);
3475 return 1;
3476 }
3477 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003478 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3479 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 goto Error;
3481 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003482
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 l = PySequence_List(it);
3484 if (l == NULL)
3485 goto Error;
3486 *--sp = l;
3487 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003488
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 ll = PyList_GET_SIZE(l);
3490 if (ll < argcntafter) {
3491 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3492 argcnt + ll);
3493 goto Error;
3494 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 /* Pop the "after-variable" args off the list. */
3497 for (j = argcntafter; j > 0; j--, i++) {
3498 *--sp = PyList_GET_ITEM(l, ll - j);
3499 }
3500 /* Resize the list. */
3501 Py_SIZE(l) = ll - argcntafter;
3502 Py_DECREF(it);
3503 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003504
Tim Petersd6d010b2001-06-21 02:49:55 +00003505Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 for (; i > 0; i--, sp++)
3507 Py_DECREF(*sp);
3508 Py_XDECREF(it);
3509 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003510}
3511
3512
Guido van Rossum96a42c81992-01-12 02:29:51 +00003513#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003514static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003515prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003516{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003517 printf("%s ", str);
3518 if (PyObject_Print(v, stdout, 0) != 0)
3519 PyErr_Clear(); /* Don't know what else to do */
3520 printf("\n");
3521 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003522}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003523#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003524
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003525static void
Fred Drake5755ce62001-06-27 19:19:46 +00003526call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 PyObject *type, *value, *traceback, *arg;
3529 int err;
3530 PyErr_Fetch(&type, &value, &traceback);
3531 if (value == NULL) {
3532 value = Py_None;
3533 Py_INCREF(value);
3534 }
3535 arg = PyTuple_Pack(3, type, value, traceback);
3536 if (arg == NULL) {
3537 PyErr_Restore(type, value, traceback);
3538 return;
3539 }
3540 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3541 Py_DECREF(arg);
3542 if (err == 0)
3543 PyErr_Restore(type, value, traceback);
3544 else {
3545 Py_XDECREF(type);
3546 Py_XDECREF(value);
3547 Py_XDECREF(traceback);
3548 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003549}
3550
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003551static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003552call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003553 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003554{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 PyObject *type, *value, *traceback;
3556 int err;
3557 PyErr_Fetch(&type, &value, &traceback);
3558 err = call_trace(func, obj, frame, what, arg);
3559 if (err == 0)
3560 {
3561 PyErr_Restore(type, value, traceback);
3562 return 0;
3563 }
3564 else {
3565 Py_XDECREF(type);
3566 Py_XDECREF(value);
3567 Py_XDECREF(traceback);
3568 return -1;
3569 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003570}
3571
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003572static int
Fred Drake5755ce62001-06-27 19:19:46 +00003573call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003574 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003576 register PyThreadState *tstate = frame->f_tstate;
3577 int result;
3578 if (tstate->tracing)
3579 return 0;
3580 tstate->tracing++;
3581 tstate->use_tracing = 0;
3582 result = func(obj, frame, what, arg);
3583 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3584 || (tstate->c_profilefunc != NULL));
3585 tstate->tracing--;
3586 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003587}
3588
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003589PyObject *
3590_PyEval_CallTracing(PyObject *func, PyObject *args)
3591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003592 PyFrameObject *frame = PyEval_GetFrame();
3593 PyThreadState *tstate = frame->f_tstate;
3594 int save_tracing = tstate->tracing;
3595 int save_use_tracing = tstate->use_tracing;
3596 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003598 tstate->tracing = 0;
3599 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3600 || (tstate->c_profilefunc != NULL));
3601 result = PyObject_Call(func, args, NULL);
3602 tstate->tracing = save_tracing;
3603 tstate->use_tracing = save_use_tracing;
3604 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003605}
3606
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003607/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003608static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003609maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003610 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3611 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003612{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003613 int result = 0;
3614 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003616 /* If the last instruction executed isn't in the current
3617 instruction window, reset the window.
3618 */
3619 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3620 PyAddrPair bounds;
3621 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3622 &bounds);
3623 *instr_lb = bounds.ap_lower;
3624 *instr_ub = bounds.ap_upper;
3625 }
3626 /* If the last instruction falls at the start of a line or if
3627 it represents a jump backwards, update the frame's line
3628 number and call the trace function. */
3629 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3630 frame->f_lineno = line;
3631 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3632 }
3633 *instr_prev = frame->f_lasti;
3634 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003635}
3636
Fred Drake5755ce62001-06-27 19:19:46 +00003637void
3638PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 PyThreadState *tstate = PyThreadState_GET();
3641 PyObject *temp = tstate->c_profileobj;
3642 Py_XINCREF(arg);
3643 tstate->c_profilefunc = NULL;
3644 tstate->c_profileobj = NULL;
3645 /* Must make sure that tracing is not ignored if 'temp' is freed */
3646 tstate->use_tracing = tstate->c_tracefunc != NULL;
3647 Py_XDECREF(temp);
3648 tstate->c_profilefunc = func;
3649 tstate->c_profileobj = arg;
3650 /* Flag that tracing or profiling is turned on */
3651 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003652}
3653
3654void
3655PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003657 PyThreadState *tstate = PyThreadState_GET();
3658 PyObject *temp = tstate->c_traceobj;
3659 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3660 Py_XINCREF(arg);
3661 tstate->c_tracefunc = NULL;
3662 tstate->c_traceobj = NULL;
3663 /* Must make sure that profiling is not ignored if 'temp' is freed */
3664 tstate->use_tracing = tstate->c_profilefunc != NULL;
3665 Py_XDECREF(temp);
3666 tstate->c_tracefunc = func;
3667 tstate->c_traceobj = arg;
3668 /* Flag that tracing or profiling is turned on */
3669 tstate->use_tracing = ((func != NULL)
3670 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003671}
3672
Guido van Rossumb209a111997-04-29 18:18:01 +00003673PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003674PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003676 PyFrameObject *current_frame = PyEval_GetFrame();
3677 if (current_frame == NULL)
3678 return PyThreadState_GET()->interp->builtins;
3679 else
3680 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003681}
3682
Guido van Rossumb209a111997-04-29 18:18:01 +00003683PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003684PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003686 PyFrameObject *current_frame = PyEval_GetFrame();
3687 if (current_frame == NULL)
3688 return NULL;
3689 PyFrame_FastToLocals(current_frame);
3690 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003691}
3692
Guido van Rossumb209a111997-04-29 18:18:01 +00003693PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003694PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003695{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003696 PyFrameObject *current_frame = PyEval_GetFrame();
3697 if (current_frame == NULL)
3698 return NULL;
3699 else
3700 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003701}
3702
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003703PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003704PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 PyThreadState *tstate = PyThreadState_GET();
3707 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003708}
3709
Guido van Rossum6135a871995-01-09 17:53:26 +00003710int
Tim Peters5ba58662001-07-16 02:29:45 +00003711PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003712{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 PyFrameObject *current_frame = PyEval_GetFrame();
3714 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 if (current_frame != NULL) {
3717 const int codeflags = current_frame->f_code->co_flags;
3718 const int compilerflags = codeflags & PyCF_MASK;
3719 if (compilerflags) {
3720 result = 1;
3721 cf->cf_flags |= compilerflags;
3722 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003723#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003724 if (codeflags & CO_GENERATOR_ALLOWED) {
3725 result = 1;
3726 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3727 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003728#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 }
3730 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003731}
3732
Guido van Rossum3f5da241990-12-20 15:06:42 +00003733
Guido van Rossum681d79a1995-07-18 14:51:37 +00003734/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003735 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003736
Guido van Rossumb209a111997-04-29 18:18:01 +00003737PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003738PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003740 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003742 if (arg == NULL) {
3743 arg = PyTuple_New(0);
3744 if (arg == NULL)
3745 return NULL;
3746 }
3747 else if (!PyTuple_Check(arg)) {
3748 PyErr_SetString(PyExc_TypeError,
3749 "argument list must be a tuple");
3750 return NULL;
3751 }
3752 else
3753 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 if (kw != NULL && !PyDict_Check(kw)) {
3756 PyErr_SetString(PyExc_TypeError,
3757 "keyword list must be a dictionary");
3758 Py_DECREF(arg);
3759 return NULL;
3760 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 result = PyObject_Call(func, arg, kw);
3763 Py_DECREF(arg);
3764 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003765}
3766
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003767const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003768PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 if (PyMethod_Check(func))
3771 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3772 else if (PyFunction_Check(func))
3773 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3774 else if (PyCFunction_Check(func))
3775 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3776 else
3777 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003778}
3779
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003780const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003781PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 if (PyMethod_Check(func))
3784 return "()";
3785 else if (PyFunction_Check(func))
3786 return "()";
3787 else if (PyCFunction_Check(func))
3788 return "()";
3789 else
3790 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003791}
3792
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003793static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003794err_args(PyObject *func, int flags, int nargs)
3795{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 if (flags & METH_NOARGS)
3797 PyErr_Format(PyExc_TypeError,
3798 "%.200s() takes no arguments (%d given)",
3799 ((PyCFunctionObject *)func)->m_ml->ml_name,
3800 nargs);
3801 else
3802 PyErr_Format(PyExc_TypeError,
3803 "%.200s() takes exactly one argument (%d given)",
3804 ((PyCFunctionObject *)func)->m_ml->ml_name,
3805 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003806}
3807
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003808#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003809if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003810 if (call_trace(tstate->c_profilefunc, \
3811 tstate->c_profileobj, \
3812 tstate->frame, PyTrace_C_CALL, \
3813 func)) { \
3814 x = NULL; \
3815 } \
3816 else { \
3817 x = call; \
3818 if (tstate->c_profilefunc != NULL) { \
3819 if (x == NULL) { \
3820 call_trace_protected(tstate->c_profilefunc, \
3821 tstate->c_profileobj, \
3822 tstate->frame, PyTrace_C_EXCEPTION, \
3823 func); \
3824 /* XXX should pass (type, value, tb) */ \
3825 } else { \
3826 if (call_trace(tstate->c_profilefunc, \
3827 tstate->c_profileobj, \
3828 tstate->frame, PyTrace_C_RETURN, \
3829 func)) { \
3830 Py_DECREF(x); \
3831 x = NULL; \
3832 } \
3833 } \
3834 } \
3835 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003836} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 x = call; \
3838 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003839
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003840static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003841call_function(PyObject ***pp_stack, int oparg
3842#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003843 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003844#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003847 int na = oparg & 0xff;
3848 int nk = (oparg>>8) & 0xff;
3849 int n = na + 2 * nk;
3850 PyObject **pfunc = (*pp_stack) - n - 1;
3851 PyObject *func = *pfunc;
3852 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 /* Always dispatch PyCFunction first, because these are
3855 presumed to be the most frequent callable object.
3856 */
3857 if (PyCFunction_Check(func) && nk == 0) {
3858 int flags = PyCFunction_GET_FLAGS(func);
3859 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003861 PCALL(PCALL_CFUNCTION);
3862 if (flags & (METH_NOARGS | METH_O)) {
3863 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3864 PyObject *self = PyCFunction_GET_SELF(func);
3865 if (flags & METH_NOARGS && na == 0) {
3866 C_TRACE(x, (*meth)(self,NULL));
3867 }
3868 else if (flags & METH_O && na == 1) {
3869 PyObject *arg = EXT_POP(*pp_stack);
3870 C_TRACE(x, (*meth)(self,arg));
3871 Py_DECREF(arg);
3872 }
3873 else {
3874 err_args(func, flags, na);
3875 x = NULL;
3876 }
3877 }
3878 else {
3879 PyObject *callargs;
3880 callargs = load_args(pp_stack, na);
3881 READ_TIMESTAMP(*pintr0);
3882 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3883 READ_TIMESTAMP(*pintr1);
3884 Py_XDECREF(callargs);
3885 }
3886 } else {
3887 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3888 /* optimize access to bound methods */
3889 PyObject *self = PyMethod_GET_SELF(func);
3890 PCALL(PCALL_METHOD);
3891 PCALL(PCALL_BOUND_METHOD);
3892 Py_INCREF(self);
3893 func = PyMethod_GET_FUNCTION(func);
3894 Py_INCREF(func);
3895 Py_DECREF(*pfunc);
3896 *pfunc = self;
3897 na++;
3898 n++;
3899 } else
3900 Py_INCREF(func);
3901 READ_TIMESTAMP(*pintr0);
3902 if (PyFunction_Check(func))
3903 x = fast_function(func, pp_stack, n, na, nk);
3904 else
3905 x = do_call(func, pp_stack, na, nk);
3906 READ_TIMESTAMP(*pintr1);
3907 Py_DECREF(func);
3908 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003910 /* Clear the stack of the function object. Also removes
3911 the arguments in case they weren't consumed already
3912 (fast_function() and err_args() leave them on the stack).
3913 */
3914 while ((*pp_stack) > pfunc) {
3915 w = EXT_POP(*pp_stack);
3916 Py_DECREF(w);
3917 PCALL(PCALL_POP);
3918 }
3919 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003920}
3921
Jeremy Hylton192690e2002-08-16 18:36:11 +00003922/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003923 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003924 For the simplest case -- a function that takes only positional
3925 arguments and is called with only positional arguments -- it
3926 inlines the most primitive frame setup code from
3927 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3928 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003929*/
3930
3931static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003932fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003934 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3935 PyObject *globals = PyFunction_GET_GLOBALS(func);
3936 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3937 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3938 PyObject **d = NULL;
3939 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 PCALL(PCALL_FUNCTION);
3942 PCALL(PCALL_FAST_FUNCTION);
3943 if (argdefs == NULL && co->co_argcount == n &&
3944 co->co_kwonlyargcount == 0 && nk==0 &&
3945 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3946 PyFrameObject *f;
3947 PyObject *retval = NULL;
3948 PyThreadState *tstate = PyThreadState_GET();
3949 PyObject **fastlocals, **stack;
3950 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 PCALL(PCALL_FASTER_FUNCTION);
3953 assert(globals != NULL);
3954 /* XXX Perhaps we should create a specialized
3955 PyFrame_New() that doesn't take locals, but does
3956 take builtins without sanity checking them.
3957 */
3958 assert(tstate != NULL);
3959 f = PyFrame_New(tstate, co, globals, NULL);
3960 if (f == NULL)
3961 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 fastlocals = f->f_localsplus;
3964 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003966 for (i = 0; i < n; i++) {
3967 Py_INCREF(*stack);
3968 fastlocals[i] = *stack++;
3969 }
3970 retval = PyEval_EvalFrameEx(f,0);
3971 ++tstate->recursion_depth;
3972 Py_DECREF(f);
3973 --tstate->recursion_depth;
3974 return retval;
3975 }
3976 if (argdefs != NULL) {
3977 d = &PyTuple_GET_ITEM(argdefs, 0);
3978 nd = Py_SIZE(argdefs);
3979 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003980 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 (PyObject *)NULL, (*pp_stack)-n, na,
3982 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3983 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003984}
3985
3986static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003987update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3988 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003989{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003990 PyObject *kwdict = NULL;
3991 if (orig_kwdict == NULL)
3992 kwdict = PyDict_New();
3993 else {
3994 kwdict = PyDict_Copy(orig_kwdict);
3995 Py_DECREF(orig_kwdict);
3996 }
3997 if (kwdict == NULL)
3998 return NULL;
3999 while (--nk >= 0) {
4000 int err;
4001 PyObject *value = EXT_POP(*pp_stack);
4002 PyObject *key = EXT_POP(*pp_stack);
4003 if (PyDict_GetItem(kwdict, key) != NULL) {
4004 PyErr_Format(PyExc_TypeError,
4005 "%.200s%s got multiple values "
4006 "for keyword argument '%U'",
4007 PyEval_GetFuncName(func),
4008 PyEval_GetFuncDesc(func),
4009 key);
4010 Py_DECREF(key);
4011 Py_DECREF(value);
4012 Py_DECREF(kwdict);
4013 return NULL;
4014 }
4015 err = PyDict_SetItem(kwdict, key, value);
4016 Py_DECREF(key);
4017 Py_DECREF(value);
4018 if (err) {
4019 Py_DECREF(kwdict);
4020 return NULL;
4021 }
4022 }
4023 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004024}
4025
4026static PyObject *
4027update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004032 callargs = PyTuple_New(nstack + nstar);
4033 if (callargs == NULL) {
4034 return NULL;
4035 }
4036 if (nstar) {
4037 int i;
4038 for (i = 0; i < nstar; i++) {
4039 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4040 Py_INCREF(a);
4041 PyTuple_SET_ITEM(callargs, nstack + i, a);
4042 }
4043 }
4044 while (--nstack >= 0) {
4045 w = EXT_POP(*pp_stack);
4046 PyTuple_SET_ITEM(callargs, nstack, w);
4047 }
4048 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004049}
4050
4051static PyObject *
4052load_args(PyObject ***pp_stack, int na)
4053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 PyObject *args = PyTuple_New(na);
4055 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 if (args == NULL)
4058 return NULL;
4059 while (--na >= 0) {
4060 w = EXT_POP(*pp_stack);
4061 PyTuple_SET_ITEM(args, na, w);
4062 }
4063 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004064}
4065
4066static PyObject *
4067do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004069 PyObject *callargs = NULL;
4070 PyObject *kwdict = NULL;
4071 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 if (nk > 0) {
4074 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4075 if (kwdict == NULL)
4076 goto call_fail;
4077 }
4078 callargs = load_args(pp_stack, na);
4079 if (callargs == NULL)
4080 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004081#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004082 /* At this point, we have to look at the type of func to
4083 update the call stats properly. Do it here so as to avoid
4084 exposing the call stats machinery outside ceval.c
4085 */
4086 if (PyFunction_Check(func))
4087 PCALL(PCALL_FUNCTION);
4088 else if (PyMethod_Check(func))
4089 PCALL(PCALL_METHOD);
4090 else if (PyType_Check(func))
4091 PCALL(PCALL_TYPE);
4092 else if (PyCFunction_Check(func))
4093 PCALL(PCALL_CFUNCTION);
4094 else
4095 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004096#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004097 if (PyCFunction_Check(func)) {
4098 PyThreadState *tstate = PyThreadState_GET();
4099 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4100 }
4101 else
4102 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004103call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 Py_XDECREF(callargs);
4105 Py_XDECREF(kwdict);
4106 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004107}
4108
4109static PyObject *
4110ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4111{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004112 int nstar = 0;
4113 PyObject *callargs = NULL;
4114 PyObject *stararg = NULL;
4115 PyObject *kwdict = NULL;
4116 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004118 if (flags & CALL_FLAG_KW) {
4119 kwdict = EXT_POP(*pp_stack);
4120 if (!PyDict_Check(kwdict)) {
4121 PyObject *d;
4122 d = PyDict_New();
4123 if (d == NULL)
4124 goto ext_call_fail;
4125 if (PyDict_Update(d, kwdict) != 0) {
4126 Py_DECREF(d);
4127 /* PyDict_Update raises attribute
4128 * error (percolated from an attempt
4129 * to get 'keys' attribute) instead of
4130 * a type error if its second argument
4131 * is not a mapping.
4132 */
4133 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4134 PyErr_Format(PyExc_TypeError,
4135 "%.200s%.200s argument after ** "
4136 "must be a mapping, not %.200s",
4137 PyEval_GetFuncName(func),
4138 PyEval_GetFuncDesc(func),
4139 kwdict->ob_type->tp_name);
4140 }
4141 goto ext_call_fail;
4142 }
4143 Py_DECREF(kwdict);
4144 kwdict = d;
4145 }
4146 }
4147 if (flags & CALL_FLAG_VAR) {
4148 stararg = EXT_POP(*pp_stack);
4149 if (!PyTuple_Check(stararg)) {
4150 PyObject *t = NULL;
4151 t = PySequence_Tuple(stararg);
4152 if (t == NULL) {
4153 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4154 PyErr_Format(PyExc_TypeError,
4155 "%.200s%.200s argument after * "
4156 "must be a sequence, not %200s",
4157 PyEval_GetFuncName(func),
4158 PyEval_GetFuncDesc(func),
4159 stararg->ob_type->tp_name);
4160 }
4161 goto ext_call_fail;
4162 }
4163 Py_DECREF(stararg);
4164 stararg = t;
4165 }
4166 nstar = PyTuple_GET_SIZE(stararg);
4167 }
4168 if (nk > 0) {
4169 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4170 if (kwdict == NULL)
4171 goto ext_call_fail;
4172 }
4173 callargs = update_star_args(na, nstar, stararg, pp_stack);
4174 if (callargs == NULL)
4175 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004176#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 /* At this point, we have to look at the type of func to
4178 update the call stats properly. Do it here so as to avoid
4179 exposing the call stats machinery outside ceval.c
4180 */
4181 if (PyFunction_Check(func))
4182 PCALL(PCALL_FUNCTION);
4183 else if (PyMethod_Check(func))
4184 PCALL(PCALL_METHOD);
4185 else if (PyType_Check(func))
4186 PCALL(PCALL_TYPE);
4187 else if (PyCFunction_Check(func))
4188 PCALL(PCALL_CFUNCTION);
4189 else
4190 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004191#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 if (PyCFunction_Check(func)) {
4193 PyThreadState *tstate = PyThreadState_GET();
4194 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4195 }
4196 else
4197 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004198ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 Py_XDECREF(callargs);
4200 Py_XDECREF(kwdict);
4201 Py_XDECREF(stararg);
4202 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004203}
4204
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004205/* Extract a slice index from a PyInt or PyLong or an object with the
4206 nb_index slot defined, and store in *pi.
4207 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4208 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 +00004209 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004210*/
Tim Petersb5196382001-12-16 19:44:20 +00004211/* Note: If v is NULL, return success without storing into *pi. This
4212 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4213 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004214*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004215int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004216_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004217{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004218 if (v != NULL) {
4219 Py_ssize_t x;
4220 if (PyIndex_Check(v)) {
4221 x = PyNumber_AsSsize_t(v, NULL);
4222 if (x == -1 && PyErr_Occurred())
4223 return 0;
4224 }
4225 else {
4226 PyErr_SetString(PyExc_TypeError,
4227 "slice indices must be integers or "
4228 "None or have an __index__ method");
4229 return 0;
4230 }
4231 *pi = x;
4232 }
4233 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004234}
4235
Guido van Rossum486364b2007-06-30 05:01:58 +00004236#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004238
Guido van Rossumb209a111997-04-29 18:18:01 +00004239static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004240cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004241{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004242 int res = 0;
4243 switch (op) {
4244 case PyCmp_IS:
4245 res = (v == w);
4246 break;
4247 case PyCmp_IS_NOT:
4248 res = (v != w);
4249 break;
4250 case PyCmp_IN:
4251 res = PySequence_Contains(w, v);
4252 if (res < 0)
4253 return NULL;
4254 break;
4255 case PyCmp_NOT_IN:
4256 res = PySequence_Contains(w, v);
4257 if (res < 0)
4258 return NULL;
4259 res = !res;
4260 break;
4261 case PyCmp_EXC_MATCH:
4262 if (PyTuple_Check(w)) {
4263 Py_ssize_t i, length;
4264 length = PyTuple_Size(w);
4265 for (i = 0; i < length; i += 1) {
4266 PyObject *exc = PyTuple_GET_ITEM(w, i);
4267 if (!PyExceptionClass_Check(exc)) {
4268 PyErr_SetString(PyExc_TypeError,
4269 CANNOT_CATCH_MSG);
4270 return NULL;
4271 }
4272 }
4273 }
4274 else {
4275 if (!PyExceptionClass_Check(w)) {
4276 PyErr_SetString(PyExc_TypeError,
4277 CANNOT_CATCH_MSG);
4278 return NULL;
4279 }
4280 }
4281 res = PyErr_GivenExceptionMatches(v, w);
4282 break;
4283 default:
4284 return PyObject_RichCompare(v, w, op);
4285 }
4286 v = res ? Py_True : Py_False;
4287 Py_INCREF(v);
4288 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004289}
4290
Thomas Wouters52152252000-08-17 22:55:00 +00004291static PyObject *
4292import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004293{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 x = PyObject_GetAttr(v, name);
4297 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4298 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4299 }
4300 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004301}
Guido van Rossumac7be682001-01-17 15:42:30 +00004302
Thomas Wouters52152252000-08-17 22:55:00 +00004303static int
4304import_all_from(PyObject *locals, PyObject *v)
4305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004306 PyObject *all = PyObject_GetAttrString(v, "__all__");
4307 PyObject *dict, *name, *value;
4308 int skip_leading_underscores = 0;
4309 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004311 if (all == NULL) {
4312 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4313 return -1; /* Unexpected error */
4314 PyErr_Clear();
4315 dict = PyObject_GetAttrString(v, "__dict__");
4316 if (dict == NULL) {
4317 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4318 return -1;
4319 PyErr_SetString(PyExc_ImportError,
4320 "from-import-* object has no __dict__ and no __all__");
4321 return -1;
4322 }
4323 all = PyMapping_Keys(dict);
4324 Py_DECREF(dict);
4325 if (all == NULL)
4326 return -1;
4327 skip_leading_underscores = 1;
4328 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004330 for (pos = 0, err = 0; ; pos++) {
4331 name = PySequence_GetItem(all, pos);
4332 if (name == NULL) {
4333 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4334 err = -1;
4335 else
4336 PyErr_Clear();
4337 break;
4338 }
4339 if (skip_leading_underscores &&
4340 PyUnicode_Check(name) &&
4341 PyUnicode_AS_UNICODE(name)[0] == '_')
4342 {
4343 Py_DECREF(name);
4344 continue;
4345 }
4346 value = PyObject_GetAttr(v, name);
4347 if (value == NULL)
4348 err = -1;
4349 else if (PyDict_CheckExact(locals))
4350 err = PyDict_SetItem(locals, name, value);
4351 else
4352 err = PyObject_SetItem(locals, name, value);
4353 Py_DECREF(name);
4354 Py_XDECREF(value);
4355 if (err != 0)
4356 break;
4357 }
4358 Py_DECREF(all);
4359 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004360}
4361
Guido van Rossumac7be682001-01-17 15:42:30 +00004362static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004363format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004367 if (!obj)
4368 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 obj_str = _PyUnicode_AsString(obj);
4371 if (!obj_str)
4372 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004375}
Guido van Rossum950361c1997-01-24 13:49:28 +00004376
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004377static void
4378format_exc_unbound(PyCodeObject *co, int oparg)
4379{
4380 PyObject *name;
4381 /* Don't stomp existing exception */
4382 if (PyErr_Occurred())
4383 return;
4384 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4385 name = PyTuple_GET_ITEM(co->co_cellvars,
4386 oparg);
4387 format_exc_check_arg(
4388 PyExc_UnboundLocalError,
4389 UNBOUNDLOCAL_ERROR_MSG,
4390 name);
4391 } else {
4392 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4393 PyTuple_GET_SIZE(co->co_cellvars));
4394 format_exc_check_arg(PyExc_NameError,
4395 UNBOUNDFREE_ERROR_MSG, name);
4396 }
4397}
4398
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004399static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004400unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004402{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 /* This function implements 'variable += expr' when both arguments
4404 are (Unicode) strings. */
4405 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4406 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4407 Py_ssize_t new_len = v_len + w_len;
4408 if (new_len < 0) {
4409 PyErr_SetString(PyExc_OverflowError,
4410 "strings are too large to concat");
4411 return NULL;
4412 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004413
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004414 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004415 /* In the common case, there are 2 references to the value
4416 * stored in 'variable' when the += is performed: one on the
4417 * value stack (in 'v') and one still stored in the
4418 * 'variable'. We try to delete the variable now to reduce
4419 * the refcnt to 1.
4420 */
4421 switch (*next_instr) {
4422 case STORE_FAST:
4423 {
4424 int oparg = PEEKARG();
4425 PyObject **fastlocals = f->f_localsplus;
4426 if (GETLOCAL(oparg) == v)
4427 SETLOCAL(oparg, NULL);
4428 break;
4429 }
4430 case STORE_DEREF:
4431 {
4432 PyObject **freevars = (f->f_localsplus +
4433 f->f_code->co_nlocals);
4434 PyObject *c = freevars[PEEKARG()];
4435 if (PyCell_GET(c) == v)
4436 PyCell_Set(c, NULL);
4437 break;
4438 }
4439 case STORE_NAME:
4440 {
4441 PyObject *names = f->f_code->co_names;
4442 PyObject *name = GETITEM(names, PEEKARG());
4443 PyObject *locals = f->f_locals;
4444 if (PyDict_CheckExact(locals) &&
4445 PyDict_GetItem(locals, name) == v) {
4446 if (PyDict_DelItem(locals, name) != 0) {
4447 PyErr_Clear();
4448 }
4449 }
4450 break;
4451 }
4452 }
4453 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004454
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004455 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004456 /* Now we own the last reference to 'v', so we can resize it
4457 * in-place.
4458 */
4459 if (PyUnicode_Resize(&v, new_len) != 0) {
4460 /* XXX if PyUnicode_Resize() fails, 'v' has been
4461 * deallocated so it cannot be put back into
4462 * 'variable'. The MemoryError is raised when there
4463 * is no value in 'variable', which might (very
4464 * remotely) be a cause of incompatibilities.
4465 */
4466 return NULL;
4467 }
4468 /* copy 'w' into the newly allocated area of 'v' */
4469 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4470 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4471 return v;
4472 }
4473 else {
4474 /* When in-place resizing is not an option. */
4475 w = PyUnicode_Concat(v, w);
4476 Py_DECREF(v);
4477 return w;
4478 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004479}
4480
Guido van Rossum950361c1997-01-24 13:49:28 +00004481#ifdef DYNAMIC_EXECUTION_PROFILE
4482
Skip Montanarof118cb12001-10-15 20:51:38 +00004483static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004484getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004485{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 int i;
4487 PyObject *l = PyList_New(256);
4488 if (l == NULL) return NULL;
4489 for (i = 0; i < 256; i++) {
4490 PyObject *x = PyLong_FromLong(a[i]);
4491 if (x == NULL) {
4492 Py_DECREF(l);
4493 return NULL;
4494 }
4495 PyList_SetItem(l, i, x);
4496 }
4497 for (i = 0; i < 256; i++)
4498 a[i] = 0;
4499 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004500}
4501
4502PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004503_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004504{
4505#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004506 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004507#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 int i;
4509 PyObject *l = PyList_New(257);
4510 if (l == NULL) return NULL;
4511 for (i = 0; i < 257; i++) {
4512 PyObject *x = getarray(dxpairs[i]);
4513 if (x == NULL) {
4514 Py_DECREF(l);
4515 return NULL;
4516 }
4517 PyList_SetItem(l, i, x);
4518 }
4519 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004520#endif
4521}
4522
4523#endif