blob: 684c6c28f34d4d339493629ca058e98099d2aeb6 [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
Michael W. Hudson800ba232004-08-12 18:19:17 +000029#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 section should work for GCC on any PowerPC
31 platform, irrespective of OS.
32 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000033
Michael W. Hudson75eabd22005-01-18 15:56:11 +000034#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
36static void
37ppc_getcounter(uint64 *v)
38{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000040
41 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 asm volatile ("mftbu %0" : "=r" (tbu) );
43 asm volatile ("mftb %0" : "=r" (tb) );
44 asm volatile ("mftbu %0" : "=r" (tbu2));
45 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 /* The slightly peculiar way of writing the next lines is
48 compiled better by GCC than any other way I tried. */
49 ((long*)(v))[0] = tbu;
50 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000051}
52
Mark Dickinsona25b1312009-10-31 10:18:44 +000053#elif defined(__i386__)
54
55/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000056
Michael W. Hudson75eabd22005-01-18 15:56:11 +000057#define READ_TIMESTAMP(val) \
58 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000059
Mark Dickinsona25b1312009-10-31 10:18:44 +000060#elif defined(__x86_64__)
61
62/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
63 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
64 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
65 32-bit pieces of the result. */
66
67#define READ_TIMESTAMP(val) \
68 __asm__ __volatile__("rdtsc" : \
69 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
70
71
72#else
73
74#error "Don't know how to implement timestamp counter for this architecture"
75
Michael W. Hudson800ba232004-08-12 18:19:17 +000076#endif
77
Thomas Wouters477c8d52006-05-27 19:21:47 +000078void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 uint64 intr, inst, loop;
82 PyThreadState *tstate = PyThreadState_Get();
83 if (!tstate->interp->tscdump)
84 return;
85 intr = intr1 - intr0;
86 inst = inst1 - inst0 - intr;
87 loop = loop1 - loop0 - intr;
88 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000089 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000090}
Michael W. Hudson800ba232004-08-12 18:19:17 +000091
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092#endif
93
Guido van Rossum04691fc1992-08-12 15:35:34 +000094/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000095/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000096
Guido van Rossum408027e1996-12-30 16:17:54 +000097#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000098/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000099#define LLTRACE 1 /* Low-level trace feature */
100#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000101#endif
102
Jeremy Hylton52820442001-01-03 23:52:36 +0000103typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000104
Guido van Rossum374a9221991-04-04 10:40:29 +0000105/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000106#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000107static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000108#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000110#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000111static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
112static PyObject * do_call(PyObject *, PyObject ***, int, int);
113static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000114static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
117static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000118#define CALL_FLAG_VAR 1
119#define CALL_FLAG_KW 2
120
Guido van Rossum0a066c01992-03-27 17:29:15 +0000121#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000122static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000123static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000124#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000125static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000127static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000128 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000129static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000130static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000131 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000132
Thomas Wouters477c8d52006-05-27 19:21:47 +0000133static PyObject * cmp_outcome(int, PyObject *, PyObject *);
134static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000135static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000136static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000137static void format_exc_unbound(PyCodeObject *co, int oparg);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000138static PyObject * unicode_concatenate(PyObject *, PyObject *,
139 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000140static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000141
Paul Prescode68140d2000-08-30 20:25:01 +0000142#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000144#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000145 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000148#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 "free variable '%.200s' referenced before assignment" \
150 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Guido van Rossum950361c1997-01-24 13:49:28 +0000152/* Dynamic execution profile */
153#ifdef DYNAMIC_EXECUTION_PROFILE
154#ifdef DXPAIRS
155static long dxpairs[257][256];
156#define dxp dxpairs[256]
157#else
158static long dxp[256];
159#endif
160#endif
161
Jeremy Hylton985eba52003-02-05 23:13:00 +0000162/* Function call profile */
163#ifdef CALL_PROFILE
164#define PCALL_NUM 11
165static int pcall[PCALL_NUM];
166
167#define PCALL_ALL 0
168#define PCALL_FUNCTION 1
169#define PCALL_FAST_FUNCTION 2
170#define PCALL_FASTER_FUNCTION 3
171#define PCALL_METHOD 4
172#define PCALL_BOUND_METHOD 5
173#define PCALL_CFUNCTION 6
174#define PCALL_TYPE 7
175#define PCALL_GENERATOR 8
176#define PCALL_OTHER 9
177#define PCALL_POP 10
178
179/* Notes about the statistics
180
181 PCALL_FAST stats
182
183 FAST_FUNCTION means no argument tuple needs to be created.
184 FASTER_FUNCTION means that the fast-path frame setup code is used.
185
186 If there is a method call where the call can be optimized by changing
187 the argument tuple and calling the function directly, it gets recorded
188 twice.
189
190 As a result, the relationship among the statistics appears to be
191 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
192 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
193 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
194 PCALL_METHOD > PCALL_BOUND_METHOD
195*/
196
197#define PCALL(POS) pcall[POS]++
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000202 return Py_BuildValue("iiiiiiiiiii",
203 pcall[0], pcall[1], pcall[2], pcall[3],
204 pcall[4], pcall[5], pcall[6], pcall[7],
205 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000206}
207#else
208#define PCALL(O)
209
210PyObject *
211PyEval_GetCallStats(PyObject *self)
212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000215}
216#endif
217
Tim Peters5ca576e2001-06-18 22:08:13 +0000218
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000219#ifdef WITH_THREAD
220#define GIL_REQUEST _Py_atomic_load_relaxed(&gil_drop_request)
221#else
222#define GIL_REQUEST 0
223#endif
224
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000225/* This can set eval_breaker to 0 even though gil_drop_request became
226 1. We believe this is all right because the eval loop will release
227 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000228#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 _Py_atomic_store_relaxed( \
230 &eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000231 GIL_REQUEST | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
233 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000234
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000235#ifdef WITH_THREAD
236
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000237#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 do { \
239 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
240 _Py_atomic_store_relaxed(&eval_breaker, 1); \
241 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242
243#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 do { \
245 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
246 COMPUTE_EVAL_BREAKER(); \
247 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000249#endif
250
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000251/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000252#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 do { \
254 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
255 _Py_atomic_store_relaxed(&eval_breaker, 1); \
256 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000257
258#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 do { \
260 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
261 COMPUTE_EVAL_BREAKER(); \
262 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000263
264#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 do { \
266 pending_async_exc = 1; \
267 _Py_atomic_store_relaxed(&eval_breaker, 1); \
268 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000269
270#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000271 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000272
273
Guido van Rossume59214e1994-08-30 08:01:59 +0000274#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000275
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000277#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000278#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000279#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000280
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000281static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000282static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000283/* This single variable consolidates all requests to break out of the fast path
284 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000285static _Py_atomic_int eval_breaker = {0};
286/* Request for dropping the GIL */
287static _Py_atomic_int gil_drop_request = {0};
288/* Request for running pending calls. */
289static _Py_atomic_int pendingcalls_to_do = {0};
290/* Request for looking at the `async_exc` field of the current thread state.
291 Guarded by the GIL. */
292static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000293
294#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295
Tim Peters7f468f22004-10-11 02:40:51 +0000296int
297PyEval_ThreadsInitialized(void)
298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000300}
301
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000303PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 if (gil_created())
306 return;
307 create_gil();
308 take_gil(PyThreadState_GET());
309 main_thread = PyThread_get_thread_ident();
310 if (!pending_lock)
311 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000312}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000313
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000314void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000315_PyEval_FiniThreads(void)
316{
317 if (!gil_created())
318 return;
319 destroy_gil();
320 assert(!gil_created());
321}
322
323void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000324PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000325{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PyThreadState *tstate = PyThreadState_GET();
327 if (tstate == NULL)
328 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
329 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000330}
331
332void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000333PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000334{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 /* This function must succeed when the current thread state is NULL.
336 We therefore avoid PyThreadState_GET() which dumps a fatal error
337 in debug mode.
338 */
339 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
340 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000341}
342
343void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000344PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000345{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 if (tstate == NULL)
347 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
348 /* Check someone has called PyEval_InitThreads() to create the lock */
349 assert(gil_created());
350 take_gil(tstate);
351 if (PyThreadState_Swap(tstate) != NULL)
352 Py_FatalError(
353 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000354}
355
356void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000357PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000358{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (tstate == NULL)
360 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
361 if (PyThreadState_Swap(NULL) != tstate)
362 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
363 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000364}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000365
366/* This function is called from PyOS_AfterFork to ensure that newly
367 created child processes don't hold locks referring to threads which
368 are not running in the child process. (This could also be done using
369 pthread_atfork mechanism, at least for the pthreads implementation.) */
370
371void
372PyEval_ReInitThreads(void)
373{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyObject *threading, *result;
375 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 if (!gil_created())
378 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 recreate_gil();
380 pending_lock = PyThread_allocate_lock();
381 take_gil(tstate);
382 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 /* Update the threading module with the new state.
385 */
386 tstate = PyThreadState_GET();
387 threading = PyMapping_GetItemString(tstate->interp->modules,
388 "threading");
389 if (threading == NULL) {
390 /* threading not imported */
391 PyErr_Clear();
392 return;
393 }
394 result = PyObject_CallMethod(threading, "_after_fork", NULL);
395 if (result == NULL)
396 PyErr_WriteUnraisable(threading);
397 else
398 Py_DECREF(result);
399 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000400}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000401
402#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000403static _Py_atomic_int eval_breaker = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000404static int pending_async_exc = 0;
405#endif /* WITH_THREAD */
406
407/* This function is used to signal that async exceptions are waiting to be
408 raised, therefore it is also useful in non-threaded builds. */
409
410void
411_PyEval_SignalAsyncExc(void)
412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000414}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000415
Guido van Rossumff4949e1992-08-05 19:58:53 +0000416/* Functions save_thread and restore_thread are always defined so
417 dynamically loaded modules needn't be compiled separately for use
418 with and without threads: */
419
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000420PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 PyThreadState *tstate = PyThreadState_Swap(NULL);
424 if (tstate == NULL)
425 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000426#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 if (gil_created())
428 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000429#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431}
432
433void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000434PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000435{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 if (tstate == NULL)
437 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000438#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 if (gil_created()) {
440 int err = errno;
441 take_gil(tstate);
442 errno = err;
443 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000444#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000446}
447
448
Guido van Rossuma9672091994-09-14 13:31:22 +0000449/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
450 signal handlers or Mac I/O completion routines) can schedule calls
451 to a function to be called synchronously.
452 The synchronous function is called with one void* argument.
453 It should return 0 for success or -1 for failure -- failure should
454 be accompanied by an exception.
455
456 If registry succeeds, the registry function returns 0; if it fails
457 (e.g. due to too many pending calls) it returns -1 (without setting
458 an exception condition).
459
460 Note that because registry may occur from within signal handlers,
461 or other asynchronous events, calling malloc() is unsafe!
462
463#ifdef WITH_THREAD
464 Any thread can schedule pending calls, but only the main thread
465 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000466 There is no facility to schedule calls to a particular thread, but
467 that should be easy to change, should that ever be required. In
468 that case, the static variables here should go into the python
469 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000470#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000472
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000473#ifdef WITH_THREAD
474
475/* The WITH_THREAD implementation is thread-safe. It allows
476 scheduling to be made from any thread, and even from an executing
477 callback.
478 */
479
480#define NPENDINGCALLS 32
481static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 int (*func)(void *);
483 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000484} pendingcalls[NPENDINGCALLS];
485static int pendingfirst = 0;
486static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487static char pendingbusy = 0;
488
489int
490Py_AddPendingCall(int (*func)(void *), void *arg)
491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 int i, j, result=0;
493 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000495 /* try a few times for the lock. Since this mechanism is used
496 * for signal handling (on the main thread), there is a (slim)
497 * chance that a signal is delivered on the same thread while we
498 * hold the lock during the Py_MakePendingCalls() function.
499 * This avoids a deadlock in that case.
500 * Note that signals can be delivered on any thread. In particular,
501 * on Windows, a SIGINT is delivered on a system-created worker
502 * thread.
503 * We also check for lock being NULL, in the unlikely case that
504 * this function is called before any bytecode evaluation takes place.
505 */
506 if (lock != NULL) {
507 for (i = 0; i<100; i++) {
508 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
509 break;
510 }
511 if (i == 100)
512 return -1;
513 }
514
515 i = pendinglast;
516 j = (i + 1) % NPENDINGCALLS;
517 if (j == pendingfirst) {
518 result = -1; /* Queue full */
519 } else {
520 pendingcalls[i].func = func;
521 pendingcalls[i].arg = arg;
522 pendinglast = j;
523 }
524 /* signal main loop */
525 SIGNAL_PENDING_CALLS();
526 if (lock != NULL)
527 PyThread_release_lock(lock);
528 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000529}
530
531int
532Py_MakePendingCalls(void)
533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 int i;
535 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (!pending_lock) {
538 /* initial allocation of the lock */
539 pending_lock = PyThread_allocate_lock();
540 if (pending_lock == NULL)
541 return -1;
542 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 /* only service pending calls on main thread */
545 if (main_thread && PyThread_get_thread_ident() != main_thread)
546 return 0;
547 /* don't perform recursive pending calls */
548 if (pendingbusy)
549 return 0;
550 pendingbusy = 1;
551 /* perform a bounded number of calls, in case of recursion */
552 for (i=0; i<NPENDINGCALLS; i++) {
553 int j;
554 int (*func)(void *);
555 void *arg = NULL;
556
557 /* pop one item off the queue while holding the lock */
558 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
559 j = pendingfirst;
560 if (j == pendinglast) {
561 func = NULL; /* Queue empty */
562 } else {
563 func = pendingcalls[j].func;
564 arg = pendingcalls[j].arg;
565 pendingfirst = (j + 1) % NPENDINGCALLS;
566 }
567 if (pendingfirst != pendinglast)
568 SIGNAL_PENDING_CALLS();
569 else
570 UNSIGNAL_PENDING_CALLS();
571 PyThread_release_lock(pending_lock);
572 /* having released the lock, perform the callback */
573 if (func == NULL)
574 break;
575 r = func(arg);
576 if (r)
577 break;
578 }
579 pendingbusy = 0;
580 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000581}
582
583#else /* if ! defined WITH_THREAD */
584
585/*
586 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
587 This code is used for signal handling in python that isn't built
588 with WITH_THREAD.
589 Don't use this implementation when Py_AddPendingCalls() can happen
590 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591
Guido van Rossuma9672091994-09-14 13:31:22 +0000592 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000593 (1) nested asynchronous calls to Py_AddPendingCall()
594 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000595
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000596 (1) is very unlikely because typically signal delivery
597 is blocked during signal handling. So it should be impossible.
598 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000599 The current code is safe against (2), but not against (1).
600 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601 thread is present, interrupted by signals, and that the critical
602 section is protected with the "busy" variable. On Windows, which
603 delivers SIGINT on a system thread, this does not hold and therefore
604 Windows really shouldn't use this version.
605 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000606*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000607
Guido van Rossuma9672091994-09-14 13:31:22 +0000608#define NPENDINGCALLS 32
609static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 int (*func)(void *);
611 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000612} pendingcalls[NPENDINGCALLS];
613static volatile int pendingfirst = 0;
614static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000615static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000616
617int
Thomas Wouters334fb892000-07-25 12:56:38 +0000618Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 static volatile int busy = 0;
621 int i, j;
622 /* XXX Begin critical section */
623 if (busy)
624 return -1;
625 busy = 1;
626 i = pendinglast;
627 j = (i + 1) % NPENDINGCALLS;
628 if (j == pendingfirst) {
629 busy = 0;
630 return -1; /* Queue full */
631 }
632 pendingcalls[i].func = func;
633 pendingcalls[i].arg = arg;
634 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 SIGNAL_PENDING_CALLS();
637 busy = 0;
638 /* XXX End critical section */
639 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000640}
641
Guido van Rossum180d7b41994-09-29 09:45:57 +0000642int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000643Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000644{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000645 static int busy = 0;
646 if (busy)
647 return 0;
648 busy = 1;
649 UNSIGNAL_PENDING_CALLS();
650 for (;;) {
651 int i;
652 int (*func)(void *);
653 void *arg;
654 i = pendingfirst;
655 if (i == pendinglast)
656 break; /* Queue empty */
657 func = pendingcalls[i].func;
658 arg = pendingcalls[i].arg;
659 pendingfirst = (i + 1) % NPENDINGCALLS;
660 if (func(arg) < 0) {
661 busy = 0;
662 SIGNAL_PENDING_CALLS(); /* We're not done yet */
663 return -1;
664 }
665 }
666 busy = 0;
667 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000668}
669
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000670#endif /* WITH_THREAD */
671
Guido van Rossuma9672091994-09-14 13:31:22 +0000672
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000673/* The interpreter's recursion limit */
674
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000675#ifndef Py_DEFAULT_RECURSION_LIMIT
676#define Py_DEFAULT_RECURSION_LIMIT 1000
677#endif
678static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
679int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000680
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000681int
682Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000685}
686
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000687void
688Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000689{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000690 recursion_limit = new_limit;
691 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000692}
693
Armin Rigo2b3eb402003-10-28 12:05:48 +0000694/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
695 if the recursion_depth reaches _Py_CheckRecursionLimit.
696 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
697 to guarantee that _Py_CheckRecursiveCall() is regularly called.
698 Without USE_STACKCHECK, there is no need for this. */
699int
700_Py_CheckRecursiveCall(char *where)
701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000702 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000703
704#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705 if (PyOS_CheckStack()) {
706 --tstate->recursion_depth;
707 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
708 return -1;
709 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000710#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 _Py_CheckRecursionLimit = recursion_limit;
712 if (tstate->recursion_critical)
713 /* Somebody asked that we don't check for recursion. */
714 return 0;
715 if (tstate->overflowed) {
716 if (tstate->recursion_depth > recursion_limit + 50) {
717 /* Overflowing while handling an overflow. Give up. */
718 Py_FatalError("Cannot recover from stack overflow.");
719 }
720 return 0;
721 }
722 if (tstate->recursion_depth > recursion_limit) {
723 --tstate->recursion_depth;
724 tstate->overflowed = 1;
725 PyErr_Format(PyExc_RuntimeError,
726 "maximum recursion depth exceeded%s",
727 where);
728 return -1;
729 }
730 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000731}
732
Guido van Rossum374a9221991-04-04 10:40:29 +0000733/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000734enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000735 WHY_NOT = 0x0001, /* No error */
736 WHY_EXCEPTION = 0x0002, /* Exception occurred */
737 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
738 WHY_RETURN = 0x0008, /* 'return' statement */
739 WHY_BREAK = 0x0010, /* 'break' statement */
740 WHY_CONTINUE = 0x0020, /* 'continue' statement */
741 WHY_YIELD = 0x0040, /* 'yield' operator */
742 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000743};
Guido van Rossum374a9221991-04-04 10:40:29 +0000744
Collin Winter828f04a2007-08-31 00:04:24 +0000745static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000746static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000747
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000748/* Records whether tracing is on for any thread. Counts the number of
749 threads for which tstate->c_tracefunc is non-NULL, so if the value
750 is 0, we know we don't have to check this thread's c_tracefunc.
751 This speeds up the if statement in PyEval_EvalFrameEx() after
752 fast_next_opcode*/
753static int _Py_TracingPossible = 0;
754
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000755
Guido van Rossum374a9221991-04-04 10:40:29 +0000756
Guido van Rossumb209a111997-04-29 18:18:01 +0000757PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000758PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000759{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 return PyEval_EvalCodeEx(co,
761 globals, locals,
762 (PyObject **)NULL, 0,
763 (PyObject **)NULL, 0,
764 (PyObject **)NULL, 0,
765 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000766}
767
768
769/* Interpreter main loop */
770
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000771PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000772PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 /* This is for backward compatibility with extension modules that
774 used this API; core interpreter code should call
775 PyEval_EvalFrameEx() */
776 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000777}
778
779PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000781{
Guido van Rossum950361c1997-01-24 13:49:28 +0000782#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000783 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000784#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 register PyObject **stack_pointer; /* Next free slot in value stack */
786 register unsigned char *next_instr;
787 register int opcode; /* Current opcode */
788 register int oparg; /* Current opcode argument, if any */
789 register enum why_code why; /* Reason for block stack unwind */
790 register int err; /* Error status -- nonzero if error */
791 register PyObject *x; /* Result object -- NULL if error */
792 register PyObject *v; /* Temporary objects popped off stack */
793 register PyObject *w;
794 register PyObject *u;
795 register PyObject *t;
796 register PyObject **fastlocals, **freevars;
797 PyObject *retval = NULL; /* Return value */
798 PyThreadState *tstate = PyThreadState_GET();
799 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 is true when the line being executed has changed. The
806 initial values are such as to make this false the first
807 time it is tested. */
808 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 unsigned char *first_instr;
811 PyObject *names;
812 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000813#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000814 /* Make it easier to find out where we are with a debugger */
815 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000816#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000817
Antoine Pitroub52ec782009-01-25 16:34:23 +0000818/* Computed GOTOs, or
819 the-optimization-commonly-but-improperly-known-as-"threaded code"
820 using gcc's labels-as-values extension
821 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
822
823 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825 combined with a lookup table of jump addresses. However, since the
826 indirect jump instruction is shared by all opcodes, the CPU will have a
827 hard time making the right prediction for where to jump next (actually,
828 it will be always wrong except in the uncommon case of a sequence of
829 several identical opcodes).
830
831 "Threaded code" in contrast, uses an explicit jump table and an explicit
832 indirect jump instruction at the end of each opcode. Since the jump
833 instruction is at a different address for each opcode, the CPU will make a
834 separate prediction for each of these instructions, which is equivalent to
835 predicting the second opcode of each opcode pair. These predictions have
836 a much better chance to turn out valid, especially in small bytecode loops.
837
838 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000840 and potentially many more instructions (depending on the pipeline width).
841 A correctly predicted branch, however, is nearly free.
842
843 At the time of this writing, the "threaded code" version is up to 15-20%
844 faster than the normal "switch" version, depending on the compiler and the
845 CPU architecture.
846
847 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
848 because it would render the measurements invalid.
849
850
851 NOTE: care must be taken that the compiler doesn't try to "optimize" the
852 indirect jumps by sharing them between all opcodes. Such optimizations
853 can be disabled on gcc by using the -fno-gcse flag (or possibly
854 -fno-crossjumping).
855*/
856
Antoine Pitrou042b1282010-08-13 21:15:58 +0000857#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000858#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000859#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000860#endif
861
Antoine Pitrou042b1282010-08-13 21:15:58 +0000862#ifdef HAVE_COMPUTED_GOTOS
863 #ifndef USE_COMPUTED_GOTOS
864 #define USE_COMPUTED_GOTOS 1
865 #endif
866#else
867 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
868 #error "Computed gotos are not supported on this compiler."
869 #endif
870 #undef USE_COMPUTED_GOTOS
871 #define USE_COMPUTED_GOTOS 0
872#endif
873
874#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000875/* Import the static jump table */
876#include "opcode_targets.h"
877
878/* This macro is used when several opcodes defer to the same implementation
879 (e.g. SETUP_LOOP, SETUP_FINALLY) */
880#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 TARGET_##op: \
882 opcode = op; \
883 if (HAS_ARG(op)) \
884 oparg = NEXTARG(); \
885 case op: \
886 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000887
888#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 TARGET_##op: \
890 opcode = op; \
891 if (HAS_ARG(op)) \
892 oparg = NEXTARG(); \
893 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000894
895
896#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 { \
898 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
899 FAST_DISPATCH(); \
900 } \
901 continue; \
902 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000903
904#ifdef LLTRACE
905#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 { \
907 if (!lltrace && !_Py_TracingPossible) { \
908 f->f_lasti = INSTR_OFFSET(); \
909 goto *opcode_targets[*next_instr++]; \
910 } \
911 goto fast_next_opcode; \
912 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000913#else
914#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 { \
916 if (!_Py_TracingPossible) { \
917 f->f_lasti = INSTR_OFFSET(); \
918 goto *opcode_targets[*next_instr++]; \
919 } \
920 goto fast_next_opcode; \
921 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000922#endif
923
924#else
925#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000927#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* silence compiler warnings about `impl` unused */ \
929 if (0) goto impl; \
930 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000931#define DISPATCH() continue
932#define FAST_DISPATCH() goto fast_next_opcode
933#endif
934
935
Neal Norwitza81d2202002-07-14 00:27:26 +0000936/* Tuple access macros */
937
938#ifndef Py_DEBUG
939#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
940#else
941#define GETITEM(v, i) PyTuple_GetItem((v), (i))
942#endif
943
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000944#ifdef WITH_TSC
945/* Use Pentium timestamp counter to mark certain events:
946 inst0 -- beginning of switch statement for opcode dispatch
947 inst1 -- end of switch statement (may be skipped)
948 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000949 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000950 (may be skipped)
951 intr1 -- beginning of long interruption
952 intr2 -- end of long interruption
953
954 Many opcodes call out to helper C functions. In some cases, the
955 time in those functions should be counted towards the time for the
956 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
957 calls another Python function; there's no point in charge all the
958 bytecode executed by the called function to the caller.
959
960 It's hard to make a useful judgement statically. In the presence
961 of operator overloading, it's impossible to tell if a call will
962 execute new Python code or not.
963
964 It's a case-by-case judgement. I'll use intr1 for the following
965 cases:
966
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000967 IMPORT_STAR
968 IMPORT_FROM
969 CALL_FUNCTION (and friends)
970
971 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
973 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000974
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 READ_TIMESTAMP(inst0);
976 READ_TIMESTAMP(inst1);
977 READ_TIMESTAMP(loop0);
978 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000980 /* shut up the compiler */
981 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000982#endif
983
Guido van Rossum374a9221991-04-04 10:40:29 +0000984/* Code access macros */
985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986#define INSTR_OFFSET() ((int)(next_instr - first_instr))
987#define NEXTOP() (*next_instr++)
988#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
989#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
990#define JUMPTO(x) (next_instr = first_instr + (x))
991#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000992
Raymond Hettingerf606f872003-03-16 03:11:04 +0000993/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 Some opcodes tend to come in pairs thus making it possible to
995 predict the second code when the first is run. For example,
996 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
997 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000998
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 Verifying the prediction costs a single high-speed test of a register
1000 variable against a constant. If the pairing was good, then the
1001 processor's own internal branch predication has a high likelihood of
1002 success, resulting in a nearly zero-overhead transition to the
1003 next opcode. A successful prediction saves a trip through the eval-loop
1004 including its two unpredictable branches, the HAS_ARG test and the
1005 switch-case. Combined with the processor's internal branch prediction,
1006 a successful PREDICT has the effect of making the two opcodes run as if
1007 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001008
Georg Brandl86b2fb92008-07-16 03:43:04 +00001009 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 predictions turned-on and interpret the results as if some opcodes
1011 had been combined or turn-off predictions so that the opcode frequency
1012 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001013
1014 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 the CPU to record separate branch prediction information for each
1016 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001017
Raymond Hettingerf606f872003-03-16 03:11:04 +00001018*/
1019
Antoine Pitrou042b1282010-08-13 21:15:58 +00001020#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021#define PREDICT(op) if (0) goto PRED_##op
1022#define PREDICTED(op) PRED_##op:
1023#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001024#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1026#define PREDICTED(op) PRED_##op: next_instr++
1027#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001028#endif
1029
Raymond Hettingerf606f872003-03-16 03:11:04 +00001030
Guido van Rossum374a9221991-04-04 10:40:29 +00001031/* Stack manipulation macros */
1032
Martin v. Löwis18e16552006-02-15 17:27:45 +00001033/* The stack can grow at most MAXINT deep, as co_nlocals and
1034 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001035#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1036#define EMPTY() (STACK_LEVEL() == 0)
1037#define TOP() (stack_pointer[-1])
1038#define SECOND() (stack_pointer[-2])
1039#define THIRD() (stack_pointer[-3])
1040#define FOURTH() (stack_pointer[-4])
1041#define PEEK(n) (stack_pointer[-(n)])
1042#define SET_TOP(v) (stack_pointer[-1] = (v))
1043#define SET_SECOND(v) (stack_pointer[-2] = (v))
1044#define SET_THIRD(v) (stack_pointer[-3] = (v))
1045#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1046#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1047#define BASIC_STACKADJ(n) (stack_pointer += n)
1048#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1049#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001050
Guido van Rossum96a42c81992-01-12 02:29:51 +00001051#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001053 lltrace && prtrace(TOP(), "push")); \
1054 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001056 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001058 lltrace && prtrace(TOP(), "stackadj")); \
1059 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001060#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001061 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1062 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001063#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001064#define PUSH(v) BASIC_PUSH(v)
1065#define POP() BASIC_POP()
1066#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001067#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001068#endif
1069
Guido van Rossum681d79a1995-07-18 14:51:37 +00001070/* Local variable macros */
1071
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001073
1074/* The SETLOCAL() macro must not DECREF the local variable in-place and
1075 then store the new value; it must copy the old value to a temporary
1076 value, then store the new value, and then DECREF the temporary value.
1077 This is because it is possible that during the DECREF the frame is
1078 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1079 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001081 GETLOCAL(i) = value; \
1082 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001083
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001084
1085#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001086 while (STACK_LEVEL() > (b)->b_level) { \
1087 PyObject *v = POP(); \
1088 Py_XDECREF(v); \
1089 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001090
1091#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 { \
1093 PyObject *type, *value, *traceback; \
1094 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1095 while (STACK_LEVEL() > (b)->b_level + 3) { \
1096 value = POP(); \
1097 Py_XDECREF(value); \
1098 } \
1099 type = tstate->exc_type; \
1100 value = tstate->exc_value; \
1101 traceback = tstate->exc_traceback; \
1102 tstate->exc_type = POP(); \
1103 tstate->exc_value = POP(); \
1104 tstate->exc_traceback = POP(); \
1105 Py_XDECREF(type); \
1106 Py_XDECREF(value); \
1107 Py_XDECREF(traceback); \
1108 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001109
1110#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 { \
1112 PyObject *type, *value, *traceback; \
1113 Py_XINCREF(tstate->exc_type); \
1114 Py_XINCREF(tstate->exc_value); \
1115 Py_XINCREF(tstate->exc_traceback); \
1116 type = f->f_exc_type; \
1117 value = f->f_exc_value; \
1118 traceback = f->f_exc_traceback; \
1119 f->f_exc_type = tstate->exc_type; \
1120 f->f_exc_value = tstate->exc_value; \
1121 f->f_exc_traceback = tstate->exc_traceback; \
1122 Py_XDECREF(type); \
1123 Py_XDECREF(value); \
1124 Py_XDECREF(traceback); \
1125 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001126
1127#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 { \
1129 PyObject *tmp; \
1130 tmp = tstate->exc_type; \
1131 tstate->exc_type = f->f_exc_type; \
1132 f->f_exc_type = tmp; \
1133 tmp = tstate->exc_value; \
1134 tstate->exc_value = f->f_exc_value; \
1135 f->f_exc_value = tmp; \
1136 tmp = tstate->exc_traceback; \
1137 tstate->exc_traceback = f->f_exc_traceback; \
1138 f->f_exc_traceback = tmp; \
1139 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001140
Guido van Rossuma027efa1997-05-05 20:56:21 +00001141/* Start of code */
1142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 if (f == NULL)
1144 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 /* push frame */
1147 if (Py_EnterRecursiveCall(""))
1148 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (tstate->use_tracing) {
1153 if (tstate->c_tracefunc != NULL) {
1154 /* tstate->c_tracefunc, if defined, is a
1155 function that will be called on *every* entry
1156 to a code block. Its return value, if not
1157 None, is a function that will be called at
1158 the start of each executed line of code.
1159 (Actually, the function must return itself
1160 in order to continue tracing.) The trace
1161 functions are called with three arguments:
1162 a pointer to the current frame, a string
1163 indicating why the function is called, and
1164 an argument which depends on the situation.
1165 The global trace function is also called
1166 whenever an exception is detected. */
1167 if (call_trace_protected(tstate->c_tracefunc,
1168 tstate->c_traceobj,
1169 f, PyTrace_CALL, Py_None)) {
1170 /* Trace function raised an error */
1171 goto exit_eval_frame;
1172 }
1173 }
1174 if (tstate->c_profilefunc != NULL) {
1175 /* Similar for c_profilefunc, except it needn't
1176 return itself and isn't called for "line" events */
1177 if (call_trace_protected(tstate->c_profilefunc,
1178 tstate->c_profileobj,
1179 f, PyTrace_CALL, Py_None)) {
1180 /* Profile function raised an error */
1181 goto exit_eval_frame;
1182 }
1183 }
1184 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001185
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 co = f->f_code;
1187 names = co->co_names;
1188 consts = co->co_consts;
1189 fastlocals = f->f_localsplus;
1190 freevars = f->f_localsplus + co->co_nlocals;
1191 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1192 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 f->f_lasti now refers to the index of the last instruction
1195 executed. You might think this was obvious from the name, but
1196 this wasn't always true before 2.3! PyFrame_New now sets
1197 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1198 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1199 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 When the PREDICT() macros are enabled, some opcode pairs follow in
1202 direct succession without updating f->f_lasti. A successful
1203 prediction effectively links the two codes together as if they
1204 were a single new opcode; accordingly,f->f_lasti will point to
1205 the first code in the pair (for instance, GET_ITER followed by
1206 FOR_ITER is effectively a single opcode and f->f_lasti will point
1207 at to the beginning of the combined pair.)
1208 */
1209 next_instr = first_instr + f->f_lasti + 1;
1210 stack_pointer = f->f_stacktop;
1211 assert(stack_pointer != NULL);
1212 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001213
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (co->co_flags & CO_GENERATOR && !throwflag) {
1215 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1216 /* We were in an except handler when we left,
1217 restore the exception state which was put aside
1218 (see YIELD_VALUE). */
1219 SWAP_EXC_STATE();
1220 }
1221 else {
1222 SAVE_EXC_STATE();
1223 }
1224 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001225
Tim Peters5ca576e2001-06-18 22:08:13 +00001226#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001227 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001228#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001229#if defined(Py_DEBUG) || defined(LLTRACE)
Victor Stinner4a3733d2010-08-17 00:39:57 +00001230 {
1231 PyObject *error_type, *error_value, *error_traceback;
1232 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1233 filename = _PyUnicode_AsString(co->co_filename);
Victor Stinnera0006452010-10-13 10:48:55 +00001234 if (filename == NULL && tstate->overflowed) {
1235 /* maximum recursion depth exceeded */
1236 goto exit_eval_frame;
1237 }
Victor Stinner4a3733d2010-08-17 00:39:57 +00001238 PyErr_Restore(error_type, error_value, error_traceback);
1239 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001240#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 why = WHY_NOT;
1243 err = 0;
1244 x = Py_None; /* Not a reference, just anything non-NULL */
1245 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 if (throwflag) { /* support for generator.throw() */
1248 why = WHY_EXCEPTION;
1249 goto on_error;
1250 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001253#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 if (inst1 == 0) {
1255 /* Almost surely, the opcode executed a break
1256 or a continue, preventing inst1 from being set
1257 on the way out of the loop.
1258 */
1259 READ_TIMESTAMP(inst1);
1260 loop1 = inst1;
1261 }
1262 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1263 intr0, intr1);
1264 ticked = 0;
1265 inst1 = 0;
1266 intr0 = 0;
1267 intr1 = 0;
1268 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001269#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001270 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1271 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 /* Do periodic things. Doing this every time through
1274 the loop would add too much overhead, so we do it
1275 only every Nth instruction. We also do it if
1276 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1277 event needs attention (e.g. a signal handler or
1278 async I/O handler); see Py_AddPendingCall() and
1279 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1282 if (*next_instr == SETUP_FINALLY) {
1283 /* Make the last opcode before
1284 a try: finally: block uninterruptable. */
1285 goto fast_next_opcode;
1286 }
1287 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001288#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001289 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001290#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1292 if (Py_MakePendingCalls() < 0) {
1293 why = WHY_EXCEPTION;
1294 goto on_error;
1295 }
1296 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001297#ifdef WITH_THREAD
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001298 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 /* Give another thread a chance */
1300 if (PyThreadState_Swap(NULL) != tstate)
1301 Py_FatalError("ceval: tstate mix-up");
1302 drop_gil(tstate);
1303
1304 /* Other threads may run now */
1305
1306 take_gil(tstate);
1307 if (PyThreadState_Swap(tstate) != NULL)
1308 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 }
Benjamin Petersond2be5b42010-09-10 22:47:02 +00001310#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 /* Check for asynchronous exceptions. */
1312 if (tstate->async_exc != NULL) {
1313 x = tstate->async_exc;
1314 tstate->async_exc = NULL;
1315 UNSIGNAL_ASYNC_EXC();
1316 PyErr_SetNone(x);
1317 Py_DECREF(x);
1318 why = WHY_EXCEPTION;
1319 goto on_error;
1320 }
1321 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 fast_next_opcode:
1324 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001327
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 if (_Py_TracingPossible &&
1329 tstate->c_tracefunc != NULL && !tstate->tracing) {
1330 /* see maybe_call_line_trace
1331 for expository comments */
1332 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001334 err = maybe_call_line_trace(tstate->c_tracefunc,
1335 tstate->c_traceobj,
1336 f, &instr_lb, &instr_ub,
1337 &instr_prev);
1338 /* Reload possibly changed frame fields */
1339 JUMPTO(f->f_lasti);
1340 if (f->f_stacktop != NULL) {
1341 stack_pointer = f->f_stacktop;
1342 f->f_stacktop = NULL;
1343 }
1344 if (err) {
1345 /* trace function raised an exception */
1346 goto on_error;
1347 }
1348 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 opcode = NEXTOP();
1353 oparg = 0; /* allows oparg to be stored in a register because
1354 it doesn't have to be remembered across a full loop */
1355 if (HAS_ARG(opcode))
1356 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001357 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001358#ifdef DYNAMIC_EXECUTION_PROFILE
1359#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 dxpairs[lastopcode][opcode]++;
1361 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001362#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001363 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001364#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001365
Guido van Rossum96a42c81992-01-12 02:29:51 +00001366#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 if (lltrace) {
1370 if (HAS_ARG(opcode)) {
1371 printf("%d: %d, %d\n",
1372 f->f_lasti, opcode, oparg);
1373 }
1374 else {
1375 printf("%d: %d\n",
1376 f->f_lasti, opcode);
1377 }
1378 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001379#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 /* Main switch on opcode */
1382 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 /* BEWARE!
1387 It is essential that any operation that fails sets either
1388 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1389 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 TARGET(NOP)
1394 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001395
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 TARGET(LOAD_FAST)
1397 x = GETLOCAL(oparg);
1398 if (x != NULL) {
1399 Py_INCREF(x);
1400 PUSH(x);
1401 FAST_DISPATCH();
1402 }
1403 format_exc_check_arg(PyExc_UnboundLocalError,
1404 UNBOUNDLOCAL_ERROR_MSG,
1405 PyTuple_GetItem(co->co_varnames, oparg));
1406 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 TARGET(LOAD_CONST)
1409 x = GETITEM(consts, oparg);
1410 Py_INCREF(x);
1411 PUSH(x);
1412 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 PREDICTED_WITH_ARG(STORE_FAST);
1415 TARGET(STORE_FAST)
1416 v = POP();
1417 SETLOCAL(oparg, v);
1418 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001419
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 TARGET(POP_TOP)
1421 v = POP();
1422 Py_DECREF(v);
1423 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 TARGET(ROT_TWO)
1426 v = TOP();
1427 w = SECOND();
1428 SET_TOP(w);
1429 SET_SECOND(v);
1430 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 TARGET(ROT_THREE)
1433 v = TOP();
1434 w = SECOND();
1435 x = THIRD();
1436 SET_TOP(w);
1437 SET_SECOND(x);
1438 SET_THIRD(v);
1439 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001441 TARGET(DUP_TOP)
1442 v = TOP();
1443 Py_INCREF(v);
1444 PUSH(v);
1445 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001446
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001447 TARGET(DUP_TOP_TWO)
1448 x = TOP();
1449 Py_INCREF(x);
1450 w = SECOND();
1451 Py_INCREF(w);
1452 STACKADJ(2);
1453 SET_TOP(x);
1454 SET_SECOND(w);
1455 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 TARGET(UNARY_POSITIVE)
1458 v = TOP();
1459 x = PyNumber_Positive(v);
1460 Py_DECREF(v);
1461 SET_TOP(x);
1462 if (x != NULL) DISPATCH();
1463 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 TARGET(UNARY_NEGATIVE)
1466 v = TOP();
1467 x = PyNumber_Negative(v);
1468 Py_DECREF(v);
1469 SET_TOP(x);
1470 if (x != NULL) DISPATCH();
1471 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 TARGET(UNARY_NOT)
1474 v = TOP();
1475 err = PyObject_IsTrue(v);
1476 Py_DECREF(v);
1477 if (err == 0) {
1478 Py_INCREF(Py_True);
1479 SET_TOP(Py_True);
1480 DISPATCH();
1481 }
1482 else if (err > 0) {
1483 Py_INCREF(Py_False);
1484 SET_TOP(Py_False);
1485 err = 0;
1486 DISPATCH();
1487 }
1488 STACKADJ(-1);
1489 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 TARGET(UNARY_INVERT)
1492 v = TOP();
1493 x = PyNumber_Invert(v);
1494 Py_DECREF(v);
1495 SET_TOP(x);
1496 if (x != NULL) DISPATCH();
1497 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001499 TARGET(BINARY_POWER)
1500 w = POP();
1501 v = TOP();
1502 x = PyNumber_Power(v, w, Py_None);
1503 Py_DECREF(v);
1504 Py_DECREF(w);
1505 SET_TOP(x);
1506 if (x != NULL) DISPATCH();
1507 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001508
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 TARGET(BINARY_MULTIPLY)
1510 w = POP();
1511 v = TOP();
1512 x = PyNumber_Multiply(v, w);
1513 Py_DECREF(v);
1514 Py_DECREF(w);
1515 SET_TOP(x);
1516 if (x != NULL) DISPATCH();
1517 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 TARGET(BINARY_TRUE_DIVIDE)
1520 w = POP();
1521 v = TOP();
1522 x = PyNumber_TrueDivide(v, w);
1523 Py_DECREF(v);
1524 Py_DECREF(w);
1525 SET_TOP(x);
1526 if (x != NULL) DISPATCH();
1527 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001528
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 TARGET(BINARY_FLOOR_DIVIDE)
1530 w = POP();
1531 v = TOP();
1532 x = PyNumber_FloorDivide(v, w);
1533 Py_DECREF(v);
1534 Py_DECREF(w);
1535 SET_TOP(x);
1536 if (x != NULL) DISPATCH();
1537 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 TARGET(BINARY_MODULO)
1540 w = POP();
1541 v = TOP();
1542 if (PyUnicode_CheckExact(v))
1543 x = PyUnicode_Format(v, w);
1544 else
1545 x = PyNumber_Remainder(v, w);
1546 Py_DECREF(v);
1547 Py_DECREF(w);
1548 SET_TOP(x);
1549 if (x != NULL) DISPATCH();
1550 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 TARGET(BINARY_ADD)
1553 w = POP();
1554 v = TOP();
1555 if (PyUnicode_CheckExact(v) &&
1556 PyUnicode_CheckExact(w)) {
1557 x = unicode_concatenate(v, w, f, next_instr);
1558 /* unicode_concatenate consumed the ref to v */
1559 goto skip_decref_vx;
1560 }
1561 else {
1562 x = PyNumber_Add(v, w);
1563 }
1564 Py_DECREF(v);
1565 skip_decref_vx:
1566 Py_DECREF(w);
1567 SET_TOP(x);
1568 if (x != NULL) DISPATCH();
1569 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 TARGET(BINARY_SUBTRACT)
1572 w = POP();
1573 v = TOP();
1574 x = PyNumber_Subtract(v, w);
1575 Py_DECREF(v);
1576 Py_DECREF(w);
1577 SET_TOP(x);
1578 if (x != NULL) DISPATCH();
1579 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 TARGET(BINARY_SUBSCR)
1582 w = POP();
1583 v = TOP();
1584 x = PyObject_GetItem(v, w);
1585 Py_DECREF(v);
1586 Py_DECREF(w);
1587 SET_TOP(x);
1588 if (x != NULL) DISPATCH();
1589 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 TARGET(BINARY_LSHIFT)
1592 w = POP();
1593 v = TOP();
1594 x = PyNumber_Lshift(v, w);
1595 Py_DECREF(v);
1596 Py_DECREF(w);
1597 SET_TOP(x);
1598 if (x != NULL) DISPATCH();
1599 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 TARGET(BINARY_RSHIFT)
1602 w = POP();
1603 v = TOP();
1604 x = PyNumber_Rshift(v, w);
1605 Py_DECREF(v);
1606 Py_DECREF(w);
1607 SET_TOP(x);
1608 if (x != NULL) DISPATCH();
1609 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 TARGET(BINARY_AND)
1612 w = POP();
1613 v = TOP();
1614 x = PyNumber_And(v, w);
1615 Py_DECREF(v);
1616 Py_DECREF(w);
1617 SET_TOP(x);
1618 if (x != NULL) DISPATCH();
1619 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001620
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 TARGET(BINARY_XOR)
1622 w = POP();
1623 v = TOP();
1624 x = PyNumber_Xor(v, w);
1625 Py_DECREF(v);
1626 Py_DECREF(w);
1627 SET_TOP(x);
1628 if (x != NULL) DISPATCH();
1629 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 TARGET(BINARY_OR)
1632 w = POP();
1633 v = TOP();
1634 x = PyNumber_Or(v, w);
1635 Py_DECREF(v);
1636 Py_DECREF(w);
1637 SET_TOP(x);
1638 if (x != NULL) DISPATCH();
1639 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001640
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 TARGET(LIST_APPEND)
1642 w = POP();
1643 v = PEEK(oparg);
1644 err = PyList_Append(v, w);
1645 Py_DECREF(w);
1646 if (err == 0) {
1647 PREDICT(JUMP_ABSOLUTE);
1648 DISPATCH();
1649 }
1650 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 TARGET(SET_ADD)
1653 w = POP();
1654 v = stack_pointer[-oparg];
1655 err = PySet_Add(v, w);
1656 Py_DECREF(w);
1657 if (err == 0) {
1658 PREDICT(JUMP_ABSOLUTE);
1659 DISPATCH();
1660 }
1661 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001662
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 TARGET(INPLACE_POWER)
1664 w = POP();
1665 v = TOP();
1666 x = PyNumber_InPlacePower(v, w, Py_None);
1667 Py_DECREF(v);
1668 Py_DECREF(w);
1669 SET_TOP(x);
1670 if (x != NULL) DISPATCH();
1671 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673 TARGET(INPLACE_MULTIPLY)
1674 w = POP();
1675 v = TOP();
1676 x = PyNumber_InPlaceMultiply(v, w);
1677 Py_DECREF(v);
1678 Py_DECREF(w);
1679 SET_TOP(x);
1680 if (x != NULL) DISPATCH();
1681 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683 TARGET(INPLACE_TRUE_DIVIDE)
1684 w = POP();
1685 v = TOP();
1686 x = PyNumber_InPlaceTrueDivide(v, w);
1687 Py_DECREF(v);
1688 Py_DECREF(w);
1689 SET_TOP(x);
1690 if (x != NULL) DISPATCH();
1691 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 TARGET(INPLACE_FLOOR_DIVIDE)
1694 w = POP();
1695 v = TOP();
1696 x = PyNumber_InPlaceFloorDivide(v, w);
1697 Py_DECREF(v);
1698 Py_DECREF(w);
1699 SET_TOP(x);
1700 if (x != NULL) DISPATCH();
1701 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 TARGET(INPLACE_MODULO)
1704 w = POP();
1705 v = TOP();
1706 x = PyNumber_InPlaceRemainder(v, w);
1707 Py_DECREF(v);
1708 Py_DECREF(w);
1709 SET_TOP(x);
1710 if (x != NULL) DISPATCH();
1711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 TARGET(INPLACE_ADD)
1714 w = POP();
1715 v = TOP();
1716 if (PyUnicode_CheckExact(v) &&
1717 PyUnicode_CheckExact(w)) {
1718 x = unicode_concatenate(v, w, f, next_instr);
1719 /* unicode_concatenate consumed the ref to v */
1720 goto skip_decref_v;
1721 }
1722 else {
1723 x = PyNumber_InPlaceAdd(v, w);
1724 }
1725 Py_DECREF(v);
1726 skip_decref_v:
1727 Py_DECREF(w);
1728 SET_TOP(x);
1729 if (x != NULL) DISPATCH();
1730 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 TARGET(INPLACE_SUBTRACT)
1733 w = POP();
1734 v = TOP();
1735 x = PyNumber_InPlaceSubtract(v, w);
1736 Py_DECREF(v);
1737 Py_DECREF(w);
1738 SET_TOP(x);
1739 if (x != NULL) DISPATCH();
1740 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 TARGET(INPLACE_LSHIFT)
1743 w = POP();
1744 v = TOP();
1745 x = PyNumber_InPlaceLshift(v, w);
1746 Py_DECREF(v);
1747 Py_DECREF(w);
1748 SET_TOP(x);
1749 if (x != NULL) DISPATCH();
1750 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 TARGET(INPLACE_RSHIFT)
1753 w = POP();
1754 v = TOP();
1755 x = PyNumber_InPlaceRshift(v, w);
1756 Py_DECREF(v);
1757 Py_DECREF(w);
1758 SET_TOP(x);
1759 if (x != NULL) DISPATCH();
1760 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 TARGET(INPLACE_AND)
1763 w = POP();
1764 v = TOP();
1765 x = PyNumber_InPlaceAnd(v, w);
1766 Py_DECREF(v);
1767 Py_DECREF(w);
1768 SET_TOP(x);
1769 if (x != NULL) DISPATCH();
1770 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 TARGET(INPLACE_XOR)
1773 w = POP();
1774 v = TOP();
1775 x = PyNumber_InPlaceXor(v, w);
1776 Py_DECREF(v);
1777 Py_DECREF(w);
1778 SET_TOP(x);
1779 if (x != NULL) DISPATCH();
1780 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001781
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001782 TARGET(INPLACE_OR)
1783 w = POP();
1784 v = TOP();
1785 x = PyNumber_InPlaceOr(v, w);
1786 Py_DECREF(v);
1787 Py_DECREF(w);
1788 SET_TOP(x);
1789 if (x != NULL) DISPATCH();
1790 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 TARGET(STORE_SUBSCR)
1793 w = TOP();
1794 v = SECOND();
1795 u = THIRD();
1796 STACKADJ(-3);
1797 /* v[w] = u */
1798 err = PyObject_SetItem(v, w, u);
1799 Py_DECREF(u);
1800 Py_DECREF(v);
1801 Py_DECREF(w);
1802 if (err == 0) DISPATCH();
1803 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 TARGET(DELETE_SUBSCR)
1806 w = TOP();
1807 v = SECOND();
1808 STACKADJ(-2);
1809 /* del v[w] */
1810 err = PyObject_DelItem(v, w);
1811 Py_DECREF(v);
1812 Py_DECREF(w);
1813 if (err == 0) DISPATCH();
1814 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001815
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 TARGET(PRINT_EXPR)
1817 v = POP();
1818 w = PySys_GetObject("displayhook");
1819 if (w == NULL) {
1820 PyErr_SetString(PyExc_RuntimeError,
1821 "lost sys.displayhook");
1822 err = -1;
1823 x = NULL;
1824 }
1825 if (err == 0) {
1826 x = PyTuple_Pack(1, v);
1827 if (x == NULL)
1828 err = -1;
1829 }
1830 if (err == 0) {
1831 w = PyEval_CallObject(w, x);
1832 Py_XDECREF(w);
1833 if (w == NULL)
1834 err = -1;
1835 }
1836 Py_DECREF(v);
1837 Py_XDECREF(x);
1838 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001839
Thomas Wouters434d0822000-08-24 20:11:32 +00001840#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001841 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001842#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 TARGET(RAISE_VARARGS)
1844 v = w = NULL;
1845 switch (oparg) {
1846 case 2:
1847 v = POP(); /* cause */
1848 case 1:
1849 w = POP(); /* exc */
1850 case 0: /* Fallthrough */
1851 why = do_raise(w, v);
1852 break;
1853 default:
1854 PyErr_SetString(PyExc_SystemError,
1855 "bad RAISE_VARARGS oparg");
1856 why = WHY_EXCEPTION;
1857 break;
1858 }
1859 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 TARGET(STORE_LOCALS)
1862 x = POP();
1863 v = f->f_locals;
1864 Py_XDECREF(v);
1865 f->f_locals = x;
1866 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868 TARGET(RETURN_VALUE)
1869 retval = POP();
1870 why = WHY_RETURN;
1871 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001872
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 TARGET(YIELD_VALUE)
1874 retval = POP();
1875 f->f_stacktop = stack_pointer;
1876 why = WHY_YIELD;
1877 /* Put aside the current exception state and restore
1878 that of the calling frame. This only serves when
1879 "yield" is used inside an except handler. */
1880 SWAP_EXC_STATE();
1881 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 TARGET(POP_EXCEPT)
1884 {
1885 PyTryBlock *b = PyFrame_BlockPop(f);
1886 if (b->b_type != EXCEPT_HANDLER) {
1887 PyErr_SetString(PyExc_SystemError,
1888 "popped block is not an except handler");
1889 why = WHY_EXCEPTION;
1890 break;
1891 }
1892 UNWIND_EXCEPT_HANDLER(b);
1893 }
1894 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001896 TARGET(POP_BLOCK)
1897 {
1898 PyTryBlock *b = PyFrame_BlockPop(f);
1899 UNWIND_BLOCK(b);
1900 }
1901 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 PREDICTED(END_FINALLY);
1904 TARGET(END_FINALLY)
1905 v = POP();
1906 if (PyLong_Check(v)) {
1907 why = (enum why_code) PyLong_AS_LONG(v);
1908 assert(why != WHY_YIELD);
1909 if (why == WHY_RETURN ||
1910 why == WHY_CONTINUE)
1911 retval = POP();
1912 if (why == WHY_SILENCED) {
1913 /* An exception was silenced by 'with', we must
1914 manually unwind the EXCEPT_HANDLER block which was
1915 created when the exception was caught, otherwise
1916 the stack will be in an inconsistent state. */
1917 PyTryBlock *b = PyFrame_BlockPop(f);
1918 assert(b->b_type == EXCEPT_HANDLER);
1919 UNWIND_EXCEPT_HANDLER(b);
1920 why = WHY_NOT;
1921 }
1922 }
1923 else if (PyExceptionClass_Check(v)) {
1924 w = POP();
1925 u = POP();
1926 PyErr_Restore(v, w, u);
1927 why = WHY_RERAISE;
1928 break;
1929 }
1930 else if (v != Py_None) {
1931 PyErr_SetString(PyExc_SystemError,
1932 "'finally' pops bad exception");
1933 why = WHY_EXCEPTION;
1934 }
1935 Py_DECREF(v);
1936 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938 TARGET(LOAD_BUILD_CLASS)
1939 x = PyDict_GetItemString(f->f_builtins,
1940 "__build_class__");
1941 if (x == NULL) {
1942 PyErr_SetString(PyExc_ImportError,
1943 "__build_class__ not found");
1944 break;
1945 }
1946 Py_INCREF(x);
1947 PUSH(x);
1948 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 TARGET(STORE_NAME)
1951 w = GETITEM(names, oparg);
1952 v = POP();
1953 if ((x = f->f_locals) != NULL) {
1954 if (PyDict_CheckExact(x))
1955 err = PyDict_SetItem(x, w, v);
1956 else
1957 err = PyObject_SetItem(x, w, v);
1958 Py_DECREF(v);
1959 if (err == 0) DISPATCH();
1960 break;
1961 }
1962 PyErr_Format(PyExc_SystemError,
1963 "no locals found when storing %R", w);
1964 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001965
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 TARGET(DELETE_NAME)
1967 w = GETITEM(names, oparg);
1968 if ((x = f->f_locals) != NULL) {
1969 if ((err = PyObject_DelItem(x, w)) != 0)
1970 format_exc_check_arg(PyExc_NameError,
1971 NAME_ERROR_MSG,
1972 w);
1973 break;
1974 }
1975 PyErr_Format(PyExc_SystemError,
1976 "no locals when deleting %R", w);
1977 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001978
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1980 TARGET(UNPACK_SEQUENCE)
1981 v = POP();
1982 if (PyTuple_CheckExact(v) &&
1983 PyTuple_GET_SIZE(v) == oparg) {
1984 PyObject **items = \
1985 ((PyTupleObject *)v)->ob_item;
1986 while (oparg--) {
1987 w = items[oparg];
1988 Py_INCREF(w);
1989 PUSH(w);
1990 }
1991 Py_DECREF(v);
1992 DISPATCH();
1993 } else if (PyList_CheckExact(v) &&
1994 PyList_GET_SIZE(v) == oparg) {
1995 PyObject **items = \
1996 ((PyListObject *)v)->ob_item;
1997 while (oparg--) {
1998 w = items[oparg];
1999 Py_INCREF(w);
2000 PUSH(w);
2001 }
2002 } else if (unpack_iterable(v, oparg, -1,
2003 stack_pointer + oparg)) {
2004 STACKADJ(oparg);
2005 } else {
2006 /* unpack_iterable() raised an exception */
2007 why = WHY_EXCEPTION;
2008 }
2009 Py_DECREF(v);
2010 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 TARGET(UNPACK_EX)
2013 {
2014 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2015 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002016
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002017 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2018 stack_pointer + totalargs)) {
2019 stack_pointer += totalargs;
2020 } else {
2021 why = WHY_EXCEPTION;
2022 }
2023 Py_DECREF(v);
2024 break;
2025 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 TARGET(STORE_ATTR)
2028 w = GETITEM(names, oparg);
2029 v = TOP();
2030 u = SECOND();
2031 STACKADJ(-2);
2032 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2033 Py_DECREF(v);
2034 Py_DECREF(u);
2035 if (err == 0) DISPATCH();
2036 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002038 TARGET(DELETE_ATTR)
2039 w = GETITEM(names, oparg);
2040 v = POP();
2041 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2042 /* del v.w */
2043 Py_DECREF(v);
2044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 TARGET(STORE_GLOBAL)
2047 w = GETITEM(names, oparg);
2048 v = POP();
2049 err = PyDict_SetItem(f->f_globals, w, v);
2050 Py_DECREF(v);
2051 if (err == 0) DISPATCH();
2052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 TARGET(DELETE_GLOBAL)
2055 w = GETITEM(names, oparg);
2056 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2057 format_exc_check_arg(
2058 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2059 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 TARGET(LOAD_NAME)
2062 w = GETITEM(names, oparg);
2063 if ((v = f->f_locals) == NULL) {
2064 PyErr_Format(PyExc_SystemError,
2065 "no locals when loading %R", w);
2066 why = WHY_EXCEPTION;
2067 break;
2068 }
2069 if (PyDict_CheckExact(v)) {
2070 x = PyDict_GetItem(v, w);
2071 Py_XINCREF(x);
2072 }
2073 else {
2074 x = PyObject_GetItem(v, w);
2075 if (x == NULL && PyErr_Occurred()) {
2076 if (!PyErr_ExceptionMatches(
2077 PyExc_KeyError))
2078 break;
2079 PyErr_Clear();
2080 }
2081 }
2082 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002083 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002084 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002085 x = PyDict_GetItem(f->f_builtins, w);
2086 if (x == NULL) {
2087 format_exc_check_arg(
2088 PyExc_NameError,
2089 NAME_ERROR_MSG, w);
2090 break;
2091 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002092 }
2093 Py_INCREF(x);
2094 }
2095 PUSH(x);
2096 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002097
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002098 TARGET(LOAD_GLOBAL)
2099 w = GETITEM(names, oparg);
2100 if (PyUnicode_CheckExact(w)) {
2101 /* Inline the PyDict_GetItem() calls.
2102 WARNING: this is an extreme speed hack.
2103 Do not try this at home. */
Benjamin Peterson8f67d082010-10-17 20:54:53 +00002104 Py_hash_t hash = ((PyUnicodeObject *)w)->hash;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002105 if (hash != -1) {
2106 PyDictObject *d;
2107 PyDictEntry *e;
2108 d = (PyDictObject *)(f->f_globals);
2109 e = d->ma_lookup(d, w, hash);
2110 if (e == NULL) {
2111 x = NULL;
2112 break;
2113 }
2114 x = e->me_value;
2115 if (x != NULL) {
2116 Py_INCREF(x);
2117 PUSH(x);
2118 DISPATCH();
2119 }
2120 d = (PyDictObject *)(f->f_builtins);
2121 e = d->ma_lookup(d, w, hash);
2122 if (e == NULL) {
2123 x = NULL;
2124 break;
2125 }
2126 x = e->me_value;
2127 if (x != NULL) {
2128 Py_INCREF(x);
2129 PUSH(x);
2130 DISPATCH();
2131 }
2132 goto load_global_error;
2133 }
2134 }
2135 /* This is the un-inlined version of the code above */
2136 x = PyDict_GetItem(f->f_globals, w);
2137 if (x == NULL) {
2138 x = PyDict_GetItem(f->f_builtins, w);
2139 if (x == NULL) {
2140 load_global_error:
2141 format_exc_check_arg(
2142 PyExc_NameError,
2143 GLOBAL_NAME_ERROR_MSG, w);
2144 break;
2145 }
2146 }
2147 Py_INCREF(x);
2148 PUSH(x);
2149 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002150
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002151 TARGET(DELETE_FAST)
2152 x = GETLOCAL(oparg);
2153 if (x != NULL) {
2154 SETLOCAL(oparg, NULL);
2155 DISPATCH();
2156 }
2157 format_exc_check_arg(
2158 PyExc_UnboundLocalError,
2159 UNBOUNDLOCAL_ERROR_MSG,
2160 PyTuple_GetItem(co->co_varnames, oparg)
2161 );
2162 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002163
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002164 TARGET(DELETE_DEREF)
2165 x = freevars[oparg];
2166 if (PyCell_GET(x) != NULL) {
2167 PyCell_Set(x, NULL);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002168 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002169 }
2170 err = -1;
2171 format_exc_unbound(co, oparg);
2172 break;
2173
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002174 TARGET(LOAD_CLOSURE)
2175 x = freevars[oparg];
2176 Py_INCREF(x);
2177 PUSH(x);
2178 if (x != NULL) DISPATCH();
2179 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 TARGET(LOAD_DEREF)
2182 x = freevars[oparg];
2183 w = PyCell_Get(x);
2184 if (w != NULL) {
2185 PUSH(w);
2186 DISPATCH();
2187 }
2188 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002189 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002191
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002192 TARGET(STORE_DEREF)
2193 w = POP();
2194 x = freevars[oparg];
2195 PyCell_Set(x, w);
2196 Py_DECREF(w);
2197 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 TARGET(BUILD_TUPLE)
2200 x = PyTuple_New(oparg);
2201 if (x != NULL) {
2202 for (; --oparg >= 0;) {
2203 w = POP();
2204 PyTuple_SET_ITEM(x, oparg, w);
2205 }
2206 PUSH(x);
2207 DISPATCH();
2208 }
2209 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002211 TARGET(BUILD_LIST)
2212 x = PyList_New(oparg);
2213 if (x != NULL) {
2214 for (; --oparg >= 0;) {
2215 w = POP();
2216 PyList_SET_ITEM(x, oparg, w);
2217 }
2218 PUSH(x);
2219 DISPATCH();
2220 }
2221 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002223 TARGET(BUILD_SET)
2224 x = PySet_New(NULL);
2225 if (x != NULL) {
2226 for (; --oparg >= 0;) {
2227 w = POP();
2228 if (err == 0)
2229 err = PySet_Add(x, w);
2230 Py_DECREF(w);
2231 }
2232 if (err != 0) {
2233 Py_DECREF(x);
2234 break;
2235 }
2236 PUSH(x);
2237 DISPATCH();
2238 }
2239 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002241 TARGET(BUILD_MAP)
2242 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2243 PUSH(x);
2244 if (x != NULL) DISPATCH();
2245 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002246
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002247 TARGET(STORE_MAP)
2248 w = TOP(); /* key */
2249 u = SECOND(); /* value */
2250 v = THIRD(); /* dict */
2251 STACKADJ(-2);
2252 assert (PyDict_CheckExact(v));
2253 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2254 Py_DECREF(u);
2255 Py_DECREF(w);
2256 if (err == 0) DISPATCH();
2257 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002259 TARGET(MAP_ADD)
2260 w = TOP(); /* key */
2261 u = SECOND(); /* value */
2262 STACKADJ(-2);
2263 v = stack_pointer[-oparg]; /* dict */
2264 assert (PyDict_CheckExact(v));
2265 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2266 Py_DECREF(u);
2267 Py_DECREF(w);
2268 if (err == 0) {
2269 PREDICT(JUMP_ABSOLUTE);
2270 DISPATCH();
2271 }
2272 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002274 TARGET(LOAD_ATTR)
2275 w = GETITEM(names, oparg);
2276 v = TOP();
2277 x = PyObject_GetAttr(v, w);
2278 Py_DECREF(v);
2279 SET_TOP(x);
2280 if (x != NULL) DISPATCH();
2281 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 TARGET(COMPARE_OP)
2284 w = POP();
2285 v = TOP();
2286 x = cmp_outcome(oparg, v, w);
2287 Py_DECREF(v);
2288 Py_DECREF(w);
2289 SET_TOP(x);
2290 if (x == NULL) break;
2291 PREDICT(POP_JUMP_IF_FALSE);
2292 PREDICT(POP_JUMP_IF_TRUE);
2293 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 TARGET(IMPORT_NAME)
2296 w = GETITEM(names, oparg);
2297 x = PyDict_GetItemString(f->f_builtins, "__import__");
2298 if (x == NULL) {
2299 PyErr_SetString(PyExc_ImportError,
2300 "__import__ not found");
2301 break;
2302 }
2303 Py_INCREF(x);
2304 v = POP();
2305 u = TOP();
2306 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2307 w = PyTuple_Pack(5,
2308 w,
2309 f->f_globals,
2310 f->f_locals == NULL ?
2311 Py_None : f->f_locals,
2312 v,
2313 u);
2314 else
2315 w = PyTuple_Pack(4,
2316 w,
2317 f->f_globals,
2318 f->f_locals == NULL ?
2319 Py_None : f->f_locals,
2320 v);
2321 Py_DECREF(v);
2322 Py_DECREF(u);
2323 if (w == NULL) {
2324 u = POP();
2325 Py_DECREF(x);
2326 x = NULL;
2327 break;
2328 }
2329 READ_TIMESTAMP(intr0);
2330 v = x;
2331 x = PyEval_CallObject(v, w);
2332 Py_DECREF(v);
2333 READ_TIMESTAMP(intr1);
2334 Py_DECREF(w);
2335 SET_TOP(x);
2336 if (x != NULL) DISPATCH();
2337 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002338
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 TARGET(IMPORT_STAR)
2340 v = POP();
2341 PyFrame_FastToLocals(f);
2342 if ((x = f->f_locals) == NULL) {
2343 PyErr_SetString(PyExc_SystemError,
2344 "no locals found during 'import *'");
2345 break;
2346 }
2347 READ_TIMESTAMP(intr0);
2348 err = import_all_from(x, v);
2349 READ_TIMESTAMP(intr1);
2350 PyFrame_LocalsToFast(f, 0);
2351 Py_DECREF(v);
2352 if (err == 0) DISPATCH();
2353 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002354
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002355 TARGET(IMPORT_FROM)
2356 w = GETITEM(names, oparg);
2357 v = TOP();
2358 READ_TIMESTAMP(intr0);
2359 x = import_from(v, w);
2360 READ_TIMESTAMP(intr1);
2361 PUSH(x);
2362 if (x != NULL) DISPATCH();
2363 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 TARGET(JUMP_FORWARD)
2366 JUMPBY(oparg);
2367 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002369 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2370 TARGET(POP_JUMP_IF_FALSE)
2371 w = POP();
2372 if (w == Py_True) {
2373 Py_DECREF(w);
2374 FAST_DISPATCH();
2375 }
2376 if (w == Py_False) {
2377 Py_DECREF(w);
2378 JUMPTO(oparg);
2379 FAST_DISPATCH();
2380 }
2381 err = PyObject_IsTrue(w);
2382 Py_DECREF(w);
2383 if (err > 0)
2384 err = 0;
2385 else if (err == 0)
2386 JUMPTO(oparg);
2387 else
2388 break;
2389 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002391 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2392 TARGET(POP_JUMP_IF_TRUE)
2393 w = POP();
2394 if (w == Py_False) {
2395 Py_DECREF(w);
2396 FAST_DISPATCH();
2397 }
2398 if (w == Py_True) {
2399 Py_DECREF(w);
2400 JUMPTO(oparg);
2401 FAST_DISPATCH();
2402 }
2403 err = PyObject_IsTrue(w);
2404 Py_DECREF(w);
2405 if (err > 0) {
2406 err = 0;
2407 JUMPTO(oparg);
2408 }
2409 else if (err == 0)
2410 ;
2411 else
2412 break;
2413 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 TARGET(JUMP_IF_FALSE_OR_POP)
2416 w = TOP();
2417 if (w == Py_True) {
2418 STACKADJ(-1);
2419 Py_DECREF(w);
2420 FAST_DISPATCH();
2421 }
2422 if (w == Py_False) {
2423 JUMPTO(oparg);
2424 FAST_DISPATCH();
2425 }
2426 err = PyObject_IsTrue(w);
2427 if (err > 0) {
2428 STACKADJ(-1);
2429 Py_DECREF(w);
2430 err = 0;
2431 }
2432 else if (err == 0)
2433 JUMPTO(oparg);
2434 else
2435 break;
2436 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002438 TARGET(JUMP_IF_TRUE_OR_POP)
2439 w = TOP();
2440 if (w == Py_False) {
2441 STACKADJ(-1);
2442 Py_DECREF(w);
2443 FAST_DISPATCH();
2444 }
2445 if (w == Py_True) {
2446 JUMPTO(oparg);
2447 FAST_DISPATCH();
2448 }
2449 err = PyObject_IsTrue(w);
2450 if (err > 0) {
2451 err = 0;
2452 JUMPTO(oparg);
2453 }
2454 else if (err == 0) {
2455 STACKADJ(-1);
2456 Py_DECREF(w);
2457 }
2458 else
2459 break;
2460 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002461
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002462 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2463 TARGET(JUMP_ABSOLUTE)
2464 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002465#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002466 /* Enabling this path speeds-up all while and for-loops by bypassing
2467 the per-loop checks for signals. By default, this should be turned-off
2468 because it prevents detection of a control-break in tight loops like
2469 "while 1: pass". Compile with this option turned-on when you need
2470 the speed-up and do not need break checking inside tight loops (ones
2471 that contain only instructions ending with FAST_DISPATCH).
2472 */
2473 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002474#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002475 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002476#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002478 TARGET(GET_ITER)
2479 /* before: [obj]; after [getiter(obj)] */
2480 v = TOP();
2481 x = PyObject_GetIter(v);
2482 Py_DECREF(v);
2483 if (x != NULL) {
2484 SET_TOP(x);
2485 PREDICT(FOR_ITER);
2486 DISPATCH();
2487 }
2488 STACKADJ(-1);
2489 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002491 PREDICTED_WITH_ARG(FOR_ITER);
2492 TARGET(FOR_ITER)
2493 /* before: [iter]; after: [iter, iter()] *or* [] */
2494 v = TOP();
2495 x = (*v->ob_type->tp_iternext)(v);
2496 if (x != NULL) {
2497 PUSH(x);
2498 PREDICT(STORE_FAST);
2499 PREDICT(UNPACK_SEQUENCE);
2500 DISPATCH();
2501 }
2502 if (PyErr_Occurred()) {
2503 if (!PyErr_ExceptionMatches(
2504 PyExc_StopIteration))
2505 break;
2506 PyErr_Clear();
2507 }
2508 /* iterator ended normally */
2509 x = v = POP();
2510 Py_DECREF(v);
2511 JUMPBY(oparg);
2512 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002513
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002514 TARGET(BREAK_LOOP)
2515 why = WHY_BREAK;
2516 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002518 TARGET(CONTINUE_LOOP)
2519 retval = PyLong_FromLong(oparg);
2520 if (!retval) {
2521 x = NULL;
2522 break;
2523 }
2524 why = WHY_CONTINUE;
2525 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002527 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2528 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2529 TARGET(SETUP_FINALLY)
2530 _setup_finally:
2531 /* NOTE: If you add any new block-setup opcodes that
2532 are not try/except/finally handlers, you may need
2533 to update the PyGen_NeedsFinalizing() function.
2534 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002536 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2537 STACK_LEVEL());
2538 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002540 TARGET(SETUP_WITH)
2541 {
2542 static PyObject *exit, *enter;
2543 w = TOP();
2544 x = special_lookup(w, "__exit__", &exit);
2545 if (!x)
2546 break;
2547 SET_TOP(x);
2548 u = special_lookup(w, "__enter__", &enter);
2549 Py_DECREF(w);
2550 if (!u) {
2551 x = NULL;
2552 break;
2553 }
2554 x = PyObject_CallFunctionObjArgs(u, NULL);
2555 Py_DECREF(u);
2556 if (!x)
2557 break;
2558 /* Setup the finally block before pushing the result
2559 of __enter__ on the stack. */
2560 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2561 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002562
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 PUSH(x);
2564 DISPATCH();
2565 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002567 TARGET(WITH_CLEANUP)
2568 {
2569 /* At the top of the stack are 1-3 values indicating
2570 how/why we entered the finally clause:
2571 - TOP = None
2572 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2573 - TOP = WHY_*; no retval below it
2574 - (TOP, SECOND, THIRD) = exc_info()
2575 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2576 Below them is EXIT, the context.__exit__ bound method.
2577 In the last case, we must call
2578 EXIT(TOP, SECOND, THIRD)
2579 otherwise we must call
2580 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002582 In the first two cases, we remove EXIT from the
2583 stack, leaving the rest in the same order. In the
2584 third case, we shift the bottom 3 values of the
2585 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002586
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002587 In addition, if the stack represents an exception,
2588 *and* the function call returns a 'true' value, we
2589 push WHY_SILENCED onto the stack. END_FINALLY will
2590 then not re-raise the exception. (But non-local
2591 gotos should still be resumed.)
2592 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002594 PyObject *exit_func;
2595 u = TOP();
2596 if (u == Py_None) {
2597 (void)POP();
2598 exit_func = TOP();
2599 SET_TOP(u);
2600 v = w = Py_None;
2601 }
2602 else if (PyLong_Check(u)) {
2603 (void)POP();
2604 switch(PyLong_AsLong(u)) {
2605 case WHY_RETURN:
2606 case WHY_CONTINUE:
2607 /* Retval in TOP. */
2608 exit_func = SECOND();
2609 SET_SECOND(TOP());
2610 SET_TOP(u);
2611 break;
2612 default:
2613 exit_func = TOP();
2614 SET_TOP(u);
2615 break;
2616 }
2617 u = v = w = Py_None;
2618 }
2619 else {
2620 PyObject *tp, *exc, *tb;
2621 PyTryBlock *block;
2622 v = SECOND();
2623 w = THIRD();
2624 tp = FOURTH();
2625 exc = PEEK(5);
2626 tb = PEEK(6);
2627 exit_func = PEEK(7);
2628 SET_VALUE(7, tb);
2629 SET_VALUE(6, exc);
2630 SET_VALUE(5, tp);
2631 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2632 SET_FOURTH(NULL);
2633 /* We just shifted the stack down, so we have
2634 to tell the except handler block that the
2635 values are lower than it expects. */
2636 block = &f->f_blockstack[f->f_iblock - 1];
2637 assert(block->b_type == EXCEPT_HANDLER);
2638 block->b_level--;
2639 }
2640 /* XXX Not the fastest way to call it... */
2641 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2642 NULL);
2643 Py_DECREF(exit_func);
2644 if (x == NULL)
2645 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 if (u != Py_None)
2648 err = PyObject_IsTrue(x);
2649 else
2650 err = 0;
2651 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (err < 0)
2654 break; /* Go to error exit */
2655 else if (err > 0) {
2656 err = 0;
2657 /* There was an exception and a True return */
2658 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2659 }
2660 PREDICT(END_FINALLY);
2661 break;
2662 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 TARGET(CALL_FUNCTION)
2665 {
2666 PyObject **sp;
2667 PCALL(PCALL_ALL);
2668 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002669#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002671#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002673#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002674 stack_pointer = sp;
2675 PUSH(x);
2676 if (x != NULL)
2677 DISPATCH();
2678 break;
2679 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2682 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2683 TARGET(CALL_FUNCTION_VAR_KW)
2684 _call_function_var_kw:
2685 {
2686 int na = oparg & 0xff;
2687 int nk = (oparg>>8) & 0xff;
2688 int flags = (opcode - CALL_FUNCTION) & 3;
2689 int n = na + 2 * nk;
2690 PyObject **pfunc, *func, **sp;
2691 PCALL(PCALL_ALL);
2692 if (flags & CALL_FLAG_VAR)
2693 n++;
2694 if (flags & CALL_FLAG_KW)
2695 n++;
2696 pfunc = stack_pointer - n - 1;
2697 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002699 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002700 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 PyObject *self = PyMethod_GET_SELF(func);
2702 Py_INCREF(self);
2703 func = PyMethod_GET_FUNCTION(func);
2704 Py_INCREF(func);
2705 Py_DECREF(*pfunc);
2706 *pfunc = self;
2707 na++;
2708 n++;
2709 } else
2710 Py_INCREF(func);
2711 sp = stack_pointer;
2712 READ_TIMESTAMP(intr0);
2713 x = ext_do_call(func, &sp, flags, na, nk);
2714 READ_TIMESTAMP(intr1);
2715 stack_pointer = sp;
2716 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 while (stack_pointer > pfunc) {
2719 w = POP();
2720 Py_DECREF(w);
2721 }
2722 PUSH(x);
2723 if (x != NULL)
2724 DISPATCH();
2725 break;
2726 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002727
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002728 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2729 TARGET(MAKE_FUNCTION)
2730 _make_function:
2731 {
2732 int posdefaults = oparg & 0xff;
2733 int kwdefaults = (oparg>>8) & 0xff;
2734 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002736 v = POP(); /* code object */
2737 x = PyFunction_New(v, f->f_globals);
2738 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 if (x != NULL && opcode == MAKE_CLOSURE) {
2741 v = POP();
2742 if (PyFunction_SetClosure(x, v) != 0) {
2743 /* Can't happen unless bytecode is corrupt. */
2744 why = WHY_EXCEPTION;
2745 }
2746 Py_DECREF(v);
2747 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002749 if (x != NULL && num_annotations > 0) {
2750 Py_ssize_t name_ix;
2751 u = POP(); /* names of args with annotations */
2752 v = PyDict_New();
2753 if (v == NULL) {
2754 Py_DECREF(x);
2755 x = NULL;
2756 break;
2757 }
2758 name_ix = PyTuple_Size(u);
2759 assert(num_annotations == name_ix+1);
2760 while (name_ix > 0) {
2761 --name_ix;
2762 t = PyTuple_GET_ITEM(u, name_ix);
2763 w = POP();
2764 /* XXX(nnorwitz): check for errors */
2765 PyDict_SetItem(v, t, w);
2766 Py_DECREF(w);
2767 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 if (PyFunction_SetAnnotations(x, v) != 0) {
2770 /* Can't happen unless
2771 PyFunction_SetAnnotations changes. */
2772 why = WHY_EXCEPTION;
2773 }
2774 Py_DECREF(v);
2775 Py_DECREF(u);
2776 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002778 /* XXX Maybe this should be a separate opcode? */
2779 if (x != NULL && posdefaults > 0) {
2780 v = PyTuple_New(posdefaults);
2781 if (v == NULL) {
2782 Py_DECREF(x);
2783 x = NULL;
2784 break;
2785 }
2786 while (--posdefaults >= 0) {
2787 w = POP();
2788 PyTuple_SET_ITEM(v, posdefaults, w);
2789 }
2790 if (PyFunction_SetDefaults(x, v) != 0) {
2791 /* Can't happen unless
2792 PyFunction_SetDefaults changes. */
2793 why = WHY_EXCEPTION;
2794 }
2795 Py_DECREF(v);
2796 }
2797 if (x != NULL && kwdefaults > 0) {
2798 v = PyDict_New();
2799 if (v == NULL) {
2800 Py_DECREF(x);
2801 x = NULL;
2802 break;
2803 }
2804 while (--kwdefaults >= 0) {
2805 w = POP(); /* default value */
2806 u = POP(); /* kw only arg name */
2807 /* XXX(nnorwitz): check for errors */
2808 PyDict_SetItem(v, u, w);
2809 Py_DECREF(w);
2810 Py_DECREF(u);
2811 }
2812 if (PyFunction_SetKwDefaults(x, v) != 0) {
2813 /* Can't happen unless
2814 PyFunction_SetKwDefaults changes. */
2815 why = WHY_EXCEPTION;
2816 }
2817 Py_DECREF(v);
2818 }
2819 PUSH(x);
2820 break;
2821 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 TARGET(BUILD_SLICE)
2824 if (oparg == 3)
2825 w = POP();
2826 else
2827 w = NULL;
2828 v = POP();
2829 u = TOP();
2830 x = PySlice_New(u, v, w);
2831 Py_DECREF(u);
2832 Py_DECREF(v);
2833 Py_XDECREF(w);
2834 SET_TOP(x);
2835 if (x != NULL) DISPATCH();
2836 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002838 TARGET(EXTENDED_ARG)
2839 opcode = NEXTOP();
2840 oparg = oparg<<16 | NEXTARG();
2841 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002842
Antoine Pitrou042b1282010-08-13 21:15:58 +00002843#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002845#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 default:
2847 fprintf(stderr,
2848 "XXX lineno: %d, opcode: %d\n",
2849 PyFrame_GetLineNumber(f),
2850 opcode);
2851 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2852 why = WHY_EXCEPTION;
2853 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002854
2855#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002856 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002857#endif
2858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002860
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002861 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002864
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002865 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002866
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 if (why == WHY_NOT) {
2868 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002869#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 /* This check is expensive! */
2871 if (PyErr_Occurred())
2872 fprintf(stderr,
2873 "XXX undetected error\n");
2874 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002875#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 READ_TIMESTAMP(loop1);
2877 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002878#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002880#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 }
2882 why = WHY_EXCEPTION;
2883 x = Py_None;
2884 err = 0;
2885 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002889 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2890 if (!PyErr_Occurred()) {
2891 PyErr_SetString(PyExc_SystemError,
2892 "error return without exception set");
2893 why = WHY_EXCEPTION;
2894 }
2895 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002896#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002897 else {
2898 /* This check is expensive! */
2899 if (PyErr_Occurred()) {
2900 char buf[128];
2901 sprintf(buf, "Stack unwind with exception "
2902 "set and why=%d", why);
2903 Py_FatalError(buf);
2904 }
2905 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002906#endif
2907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002909
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002910 if (why == WHY_EXCEPTION) {
2911 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 if (tstate->c_tracefunc != NULL)
2914 call_exc_trace(tstate->c_tracefunc,
2915 tstate->c_traceobj, f);
2916 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002918 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 if (why == WHY_RERAISE)
2921 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002924
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002925fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 while (why != WHY_NOT && f->f_iblock > 0) {
2927 /* Peek at the current block. */
2928 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002930 assert(why != WHY_YIELD);
2931 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2932 why = WHY_NOT;
2933 JUMPTO(PyLong_AS_LONG(retval));
2934 Py_DECREF(retval);
2935 break;
2936 }
2937 /* Now we have to pop the block. */
2938 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002940 if (b->b_type == EXCEPT_HANDLER) {
2941 UNWIND_EXCEPT_HANDLER(b);
2942 continue;
2943 }
2944 UNWIND_BLOCK(b);
2945 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2946 why = WHY_NOT;
2947 JUMPTO(b->b_handler);
2948 break;
2949 }
2950 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2951 || b->b_type == SETUP_FINALLY)) {
2952 PyObject *exc, *val, *tb;
2953 int handler = b->b_handler;
2954 /* Beware, this invalidates all b->b_* fields */
2955 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2956 PUSH(tstate->exc_traceback);
2957 PUSH(tstate->exc_value);
2958 if (tstate->exc_type != NULL) {
2959 PUSH(tstate->exc_type);
2960 }
2961 else {
2962 Py_INCREF(Py_None);
2963 PUSH(Py_None);
2964 }
2965 PyErr_Fetch(&exc, &val, &tb);
2966 /* Make the raw exception data
2967 available to the handler,
2968 so a program can emulate the
2969 Python main loop. */
2970 PyErr_NormalizeException(
2971 &exc, &val, &tb);
2972 PyException_SetTraceback(val, tb);
2973 Py_INCREF(exc);
2974 tstate->exc_type = exc;
2975 Py_INCREF(val);
2976 tstate->exc_value = val;
2977 tstate->exc_traceback = tb;
2978 if (tb == NULL)
2979 tb = Py_None;
2980 Py_INCREF(tb);
2981 PUSH(tb);
2982 PUSH(val);
2983 PUSH(exc);
2984 why = WHY_NOT;
2985 JUMPTO(handler);
2986 break;
2987 }
2988 if (b->b_type == SETUP_FINALLY) {
2989 if (why & (WHY_RETURN | WHY_CONTINUE))
2990 PUSH(retval);
2991 PUSH(PyLong_FromLong((long)why));
2992 why = WHY_NOT;
2993 JUMPTO(b->b_handler);
2994 break;
2995 }
2996 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002998 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 if (why != WHY_NOT)
3001 break;
3002 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 assert(why != WHY_YIELD);
3007 /* Pop remaining stack entries. */
3008 while (!EMPTY()) {
3009 v = POP();
3010 Py_XDECREF(v);
3011 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003013 if (why != WHY_RETURN)
3014 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003015
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003016fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 if (tstate->use_tracing) {
3018 if (tstate->c_tracefunc) {
3019 if (why == WHY_RETURN || why == WHY_YIELD) {
3020 if (call_trace(tstate->c_tracefunc,
3021 tstate->c_traceobj, f,
3022 PyTrace_RETURN, retval)) {
3023 Py_XDECREF(retval);
3024 retval = NULL;
3025 why = WHY_EXCEPTION;
3026 }
3027 }
3028 else if (why == WHY_EXCEPTION) {
3029 call_trace_protected(tstate->c_tracefunc,
3030 tstate->c_traceobj, f,
3031 PyTrace_RETURN, NULL);
3032 }
3033 }
3034 if (tstate->c_profilefunc) {
3035 if (why == WHY_EXCEPTION)
3036 call_trace_protected(tstate->c_profilefunc,
3037 tstate->c_profileobj, f,
3038 PyTrace_RETURN, NULL);
3039 else if (call_trace(tstate->c_profilefunc,
3040 tstate->c_profileobj, f,
3041 PyTrace_RETURN, retval)) {
3042 Py_XDECREF(retval);
3043 retval = NULL;
3044 why = WHY_EXCEPTION;
3045 }
3046 }
3047 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003050exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003051 Py_LeaveRecursiveCall();
3052 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003054 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003055}
3056
Guido van Rossumc2e20742006-02-27 22:32:47 +00003057/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003058 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003059 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003060
Tim Peters6d6c1a32001-08-02 04:15:00 +00003061PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003062PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 PyObject **args, int argcount, PyObject **kws, int kwcount,
3064 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003065{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003066 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 register PyFrameObject *f;
3068 register PyObject *retval = NULL;
3069 register PyObject **fastlocals, **freevars;
3070 PyThreadState *tstate = PyThreadState_GET();
3071 PyObject *x, *u;
3072 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 if (globals == NULL) {
3075 PyErr_SetString(PyExc_SystemError,
3076 "PyEval_EvalCodeEx: NULL globals");
3077 return NULL;
3078 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 assert(tstate != NULL);
3081 assert(globals != NULL);
3082 f = PyFrame_New(tstate, co, globals, locals);
3083 if (f == NULL)
3084 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003085
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003086 fastlocals = f->f_localsplus;
3087 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003088
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3090 int i;
3091 int n = argcount;
3092 PyObject *kwdict = NULL;
3093 if (co->co_flags & CO_VARKEYWORDS) {
3094 kwdict = PyDict_New();
3095 if (kwdict == NULL)
3096 goto fail;
3097 i = total_args;
3098 if (co->co_flags & CO_VARARGS)
3099 i++;
3100 SETLOCAL(i, kwdict);
3101 }
3102 if (argcount > co->co_argcount) {
3103 if (!(co->co_flags & CO_VARARGS)) {
3104 PyErr_Format(PyExc_TypeError,
3105 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003106 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003107 co->co_name,
3108 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003109 co->co_argcount,
3110 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonaa7fbd92010-09-25 03:25:42 +00003111 argcount + kwcount);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 goto fail;
3113 }
3114 n = co->co_argcount;
3115 }
3116 for (i = 0; i < n; i++) {
3117 x = args[i];
3118 Py_INCREF(x);
3119 SETLOCAL(i, x);
3120 }
3121 if (co->co_flags & CO_VARARGS) {
3122 u = PyTuple_New(argcount - n);
3123 if (u == NULL)
3124 goto fail;
3125 SETLOCAL(total_args, u);
3126 for (i = n; i < argcount; i++) {
3127 x = args[i];
3128 Py_INCREF(x);
3129 PyTuple_SET_ITEM(u, i-n, x);
3130 }
3131 }
3132 for (i = 0; i < kwcount; i++) {
3133 PyObject **co_varnames;
3134 PyObject *keyword = kws[2*i];
3135 PyObject *value = kws[2*i + 1];
3136 int j;
3137 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3138 PyErr_Format(PyExc_TypeError,
3139 "%U() keywords must be strings",
3140 co->co_name);
3141 goto fail;
3142 }
3143 /* Speed hack: do raw pointer compares. As names are
3144 normally interned this should almost always hit. */
3145 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3146 for (j = 0; j < total_args; j++) {
3147 PyObject *nm = co_varnames[j];
3148 if (nm == keyword)
3149 goto kw_found;
3150 }
3151 /* Slow fallback, just in case */
3152 for (j = 0; j < total_args; j++) {
3153 PyObject *nm = co_varnames[j];
3154 int cmp = PyObject_RichCompareBool(
3155 keyword, nm, Py_EQ);
3156 if (cmp > 0)
3157 goto kw_found;
3158 else if (cmp < 0)
3159 goto fail;
3160 }
3161 if (j >= total_args && kwdict == NULL) {
3162 PyErr_Format(PyExc_TypeError,
3163 "%U() got an unexpected "
3164 "keyword argument '%S'",
3165 co->co_name,
3166 keyword);
3167 goto fail;
3168 }
3169 PyDict_SetItem(kwdict, keyword, value);
3170 continue;
3171 kw_found:
3172 if (GETLOCAL(j) != NULL) {
3173 PyErr_Format(PyExc_TypeError,
3174 "%U() got multiple "
3175 "values for keyword "
3176 "argument '%S'",
3177 co->co_name,
3178 keyword);
3179 goto fail;
3180 }
3181 Py_INCREF(value);
3182 SETLOCAL(j, value);
3183 }
3184 if (co->co_kwonlyargcount > 0) {
3185 for (i = co->co_argcount; i < total_args; i++) {
3186 PyObject *name;
3187 if (GETLOCAL(i) != NULL)
3188 continue;
3189 name = PyTuple_GET_ITEM(co->co_varnames, i);
3190 if (kwdefs != NULL) {
3191 PyObject *def = PyDict_GetItem(kwdefs, name);
3192 if (def) {
3193 Py_INCREF(def);
3194 SETLOCAL(i, def);
3195 continue;
3196 }
3197 }
3198 PyErr_Format(PyExc_TypeError,
3199 "%U() needs keyword-only argument %S",
3200 co->co_name, name);
3201 goto fail;
3202 }
3203 }
3204 if (argcount < co->co_argcount) {
3205 int m = co->co_argcount - defcount;
3206 for (i = argcount; i < m; i++) {
3207 if (GETLOCAL(i) == NULL) {
3208 int j, given = 0;
3209 for (j = 0; j < co->co_argcount; j++)
3210 if (GETLOCAL(j))
3211 given++;
3212 PyErr_Format(PyExc_TypeError,
3213 "%U() takes %s %d "
3214 "argument%s "
3215 "(%d given)",
3216 co->co_name,
3217 ((co->co_flags & CO_VARARGS) ||
3218 defcount) ? "at least"
3219 : "exactly",
3220 m, m == 1 ? "" : "s", given);
3221 goto fail;
3222 }
3223 }
3224 if (n > m)
3225 i = n - m;
3226 else
3227 i = 0;
3228 for (; i < defcount; i++) {
3229 if (GETLOCAL(m+i) == NULL) {
3230 PyObject *def = defs[i];
3231 Py_INCREF(def);
3232 SETLOCAL(m+i, def);
3233 }
3234 }
3235 }
3236 }
3237 else if (argcount > 0 || kwcount > 0) {
3238 PyErr_Format(PyExc_TypeError,
3239 "%U() takes no arguments (%d given)",
3240 co->co_name,
3241 argcount + kwcount);
3242 goto fail;
3243 }
3244 /* Allocate and initialize storage for cell vars, and copy free
3245 vars into frame. This isn't too efficient right now. */
3246 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3247 int i, j, nargs, found;
3248 Py_UNICODE *cellname, *argname;
3249 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003250
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003251 nargs = total_args;
3252 if (co->co_flags & CO_VARARGS)
3253 nargs++;
3254 if (co->co_flags & CO_VARKEYWORDS)
3255 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 /* Initialize each cell var, taking into account
3258 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003259
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003260 Should arrange for the compiler to put cellvars
3261 that are arguments at the beginning of the cellvars
3262 list so that we can march over it more efficiently?
3263 */
3264 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3265 cellname = PyUnicode_AS_UNICODE(
3266 PyTuple_GET_ITEM(co->co_cellvars, i));
3267 found = 0;
3268 for (j = 0; j < nargs; j++) {
3269 argname = PyUnicode_AS_UNICODE(
3270 PyTuple_GET_ITEM(co->co_varnames, j));
3271 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3272 c = PyCell_New(GETLOCAL(j));
3273 if (c == NULL)
3274 goto fail;
3275 GETLOCAL(co->co_nlocals + i) = c;
3276 found = 1;
3277 break;
3278 }
3279 }
3280 if (found == 0) {
3281 c = PyCell_New(NULL);
3282 if (c == NULL)
3283 goto fail;
3284 SETLOCAL(co->co_nlocals + i, c);
3285 }
3286 }
3287 }
3288 if (PyTuple_GET_SIZE(co->co_freevars)) {
3289 int i;
3290 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3291 PyObject *o = PyTuple_GET_ITEM(closure, i);
3292 Py_INCREF(o);
3293 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3294 }
3295 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 if (co->co_flags & CO_GENERATOR) {
3298 /* Don't need to keep the reference to f_back, it will be set
3299 * when the generator is resumed. */
3300 Py_XDECREF(f->f_back);
3301 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003304
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003305 /* Create a new generator that owns the ready to run frame
3306 * and return that as the value. */
3307 return PyGen_New(f);
3308 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003310 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003311
Thomas Woutersce272b62007-09-19 21:19:28 +00003312fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003314 /* decref'ing the frame can cause __del__ methods to get invoked,
3315 which can call back into Python. While we're done with the
3316 current Python frame (f), the associated C stack is still in use,
3317 so recursion_depth must be boosted for the duration.
3318 */
3319 assert(tstate != NULL);
3320 ++tstate->recursion_depth;
3321 Py_DECREF(f);
3322 --tstate->recursion_depth;
3323 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003324}
3325
3326
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003327static PyObject *
3328special_lookup(PyObject *o, char *meth, PyObject **cache)
3329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 PyObject *res;
3331 res = _PyObject_LookupSpecial(o, meth, cache);
3332 if (res == NULL && !PyErr_Occurred()) {
3333 PyErr_SetObject(PyExc_AttributeError, *cache);
3334 return NULL;
3335 }
3336 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003337}
3338
3339
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003340/* Logic for the raise statement (too complicated for inlining).
3341 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003342static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003343do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003345 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003347 if (exc == NULL) {
3348 /* Reraise */
3349 PyThreadState *tstate = PyThreadState_GET();
3350 PyObject *tb;
3351 type = tstate->exc_type;
3352 value = tstate->exc_value;
3353 tb = tstate->exc_traceback;
3354 if (type == Py_None) {
3355 PyErr_SetString(PyExc_RuntimeError,
3356 "No active exception to reraise");
3357 return WHY_EXCEPTION;
3358 }
3359 Py_XINCREF(type);
3360 Py_XINCREF(value);
3361 Py_XINCREF(tb);
3362 PyErr_Restore(type, value, tb);
3363 return WHY_RERAISE;
3364 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003366 /* We support the following forms of raise:
3367 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003368 raise <instance>
3369 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003371 if (PyExceptionClass_Check(exc)) {
3372 type = exc;
3373 value = PyObject_CallObject(exc, NULL);
3374 if (value == NULL)
3375 goto raise_error;
3376 }
3377 else if (PyExceptionInstance_Check(exc)) {
3378 value = exc;
3379 type = PyExceptionInstance_Class(exc);
3380 Py_INCREF(type);
3381 }
3382 else {
3383 /* Not something you can raise. You get an exception
3384 anyway, just not what you specified :-) */
3385 Py_DECREF(exc);
3386 PyErr_SetString(PyExc_TypeError,
3387 "exceptions must derive from BaseException");
3388 goto raise_error;
3389 }
Collin Winter828f04a2007-08-31 00:04:24 +00003390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 if (cause) {
3392 PyObject *fixed_cause;
3393 if (PyExceptionClass_Check(cause)) {
3394 fixed_cause = PyObject_CallObject(cause, NULL);
3395 if (fixed_cause == NULL)
3396 goto raise_error;
3397 Py_DECREF(cause);
3398 }
3399 else if (PyExceptionInstance_Check(cause)) {
3400 fixed_cause = cause;
3401 }
3402 else {
3403 PyErr_SetString(PyExc_TypeError,
3404 "exception causes must derive from "
3405 "BaseException");
3406 goto raise_error;
3407 }
3408 PyException_SetCause(value, fixed_cause);
3409 }
Collin Winter828f04a2007-08-31 00:04:24 +00003410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003411 PyErr_SetObject(type, value);
3412 /* PyErr_SetObject incref's its arguments */
3413 Py_XDECREF(value);
3414 Py_XDECREF(type);
3415 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003416
3417raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003418 Py_XDECREF(value);
3419 Py_XDECREF(type);
3420 Py_XDECREF(cause);
3421 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003422}
3423
Tim Petersd6d010b2001-06-21 02:49:55 +00003424/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003425 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003426
Guido van Rossum0368b722007-05-11 16:50:42 +00003427 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3428 with a variable target.
3429*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003430
Barry Warsawe42b18f1997-08-25 22:13:04 +00003431static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003432unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003433{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003434 int i = 0, j = 0;
3435 Py_ssize_t ll = 0;
3436 PyObject *it; /* iter(v) */
3437 PyObject *w;
3438 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 it = PyObject_GetIter(v);
3443 if (it == NULL)
3444 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003445
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003446 for (; i < argcnt; i++) {
3447 w = PyIter_Next(it);
3448 if (w == NULL) {
3449 /* Iterator done, via error or exhaustion. */
3450 if (!PyErr_Occurred()) {
3451 PyErr_Format(PyExc_ValueError,
3452 "need more than %d value%s to unpack",
3453 i, i == 1 ? "" : "s");
3454 }
3455 goto Error;
3456 }
3457 *--sp = w;
3458 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003459
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 if (argcntafter == -1) {
3461 /* We better have exhausted the iterator now. */
3462 w = PyIter_Next(it);
3463 if (w == NULL) {
3464 if (PyErr_Occurred())
3465 goto Error;
3466 Py_DECREF(it);
3467 return 1;
3468 }
3469 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003470 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3471 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 goto Error;
3473 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003474
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 l = PySequence_List(it);
3476 if (l == NULL)
3477 goto Error;
3478 *--sp = l;
3479 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 ll = PyList_GET_SIZE(l);
3482 if (ll < argcntafter) {
3483 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3484 argcnt + ll);
3485 goto Error;
3486 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 /* Pop the "after-variable" args off the list. */
3489 for (j = argcntafter; j > 0; j--, i++) {
3490 *--sp = PyList_GET_ITEM(l, ll - j);
3491 }
3492 /* Resize the list. */
3493 Py_SIZE(l) = ll - argcntafter;
3494 Py_DECREF(it);
3495 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003496
Tim Petersd6d010b2001-06-21 02:49:55 +00003497Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003498 for (; i > 0; i--, sp++)
3499 Py_DECREF(*sp);
3500 Py_XDECREF(it);
3501 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003502}
3503
3504
Guido van Rossum96a42c81992-01-12 02:29:51 +00003505#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003506static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003507prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003509 printf("%s ", str);
3510 if (PyObject_Print(v, stdout, 0) != 0)
3511 PyErr_Clear(); /* Don't know what else to do */
3512 printf("\n");
3513 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003514}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003515#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003516
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003517static void
Fred Drake5755ce62001-06-27 19:19:46 +00003518call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003519{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 PyObject *type, *value, *traceback, *arg;
3521 int err;
3522 PyErr_Fetch(&type, &value, &traceback);
3523 if (value == NULL) {
3524 value = Py_None;
3525 Py_INCREF(value);
3526 }
3527 arg = PyTuple_Pack(3, type, value, traceback);
3528 if (arg == NULL) {
3529 PyErr_Restore(type, value, traceback);
3530 return;
3531 }
3532 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3533 Py_DECREF(arg);
3534 if (err == 0)
3535 PyErr_Restore(type, value, traceback);
3536 else {
3537 Py_XDECREF(type);
3538 Py_XDECREF(value);
3539 Py_XDECREF(traceback);
3540 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003541}
3542
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003543static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003544call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003545 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 PyObject *type, *value, *traceback;
3548 int err;
3549 PyErr_Fetch(&type, &value, &traceback);
3550 err = call_trace(func, obj, frame, what, arg);
3551 if (err == 0)
3552 {
3553 PyErr_Restore(type, value, traceback);
3554 return 0;
3555 }
3556 else {
3557 Py_XDECREF(type);
3558 Py_XDECREF(value);
3559 Py_XDECREF(traceback);
3560 return -1;
3561 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003562}
3563
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003564static int
Fred Drake5755ce62001-06-27 19:19:46 +00003565call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003568 register PyThreadState *tstate = frame->f_tstate;
3569 int result;
3570 if (tstate->tracing)
3571 return 0;
3572 tstate->tracing++;
3573 tstate->use_tracing = 0;
3574 result = func(obj, frame, what, arg);
3575 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3576 || (tstate->c_profilefunc != NULL));
3577 tstate->tracing--;
3578 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003579}
3580
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003581PyObject *
3582_PyEval_CallTracing(PyObject *func, PyObject *args)
3583{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 PyFrameObject *frame = PyEval_GetFrame();
3585 PyThreadState *tstate = frame->f_tstate;
3586 int save_tracing = tstate->tracing;
3587 int save_use_tracing = tstate->use_tracing;
3588 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 tstate->tracing = 0;
3591 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3592 || (tstate->c_profilefunc != NULL));
3593 result = PyObject_Call(func, args, NULL);
3594 tstate->tracing = save_tracing;
3595 tstate->use_tracing = save_use_tracing;
3596 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003597}
3598
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003599/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003600static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003601maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3603 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 int result = 0;
3606 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003607
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003608 /* If the last instruction executed isn't in the current
3609 instruction window, reset the window.
3610 */
3611 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3612 PyAddrPair bounds;
3613 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3614 &bounds);
3615 *instr_lb = bounds.ap_lower;
3616 *instr_ub = bounds.ap_upper;
3617 }
3618 /* If the last instruction falls at the start of a line or if
3619 it represents a jump backwards, update the frame's line
3620 number and call the trace function. */
3621 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3622 frame->f_lineno = line;
3623 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3624 }
3625 *instr_prev = frame->f_lasti;
3626 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003627}
3628
Fred Drake5755ce62001-06-27 19:19:46 +00003629void
3630PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003632 PyThreadState *tstate = PyThreadState_GET();
3633 PyObject *temp = tstate->c_profileobj;
3634 Py_XINCREF(arg);
3635 tstate->c_profilefunc = NULL;
3636 tstate->c_profileobj = NULL;
3637 /* Must make sure that tracing is not ignored if 'temp' is freed */
3638 tstate->use_tracing = tstate->c_tracefunc != NULL;
3639 Py_XDECREF(temp);
3640 tstate->c_profilefunc = func;
3641 tstate->c_profileobj = arg;
3642 /* Flag that tracing or profiling is turned on */
3643 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003644}
3645
3646void
3647PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 PyThreadState *tstate = PyThreadState_GET();
3650 PyObject *temp = tstate->c_traceobj;
3651 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3652 Py_XINCREF(arg);
3653 tstate->c_tracefunc = NULL;
3654 tstate->c_traceobj = NULL;
3655 /* Must make sure that profiling is not ignored if 'temp' is freed */
3656 tstate->use_tracing = tstate->c_profilefunc != NULL;
3657 Py_XDECREF(temp);
3658 tstate->c_tracefunc = func;
3659 tstate->c_traceobj = arg;
3660 /* Flag that tracing or profiling is turned on */
3661 tstate->use_tracing = ((func != NULL)
3662 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003663}
3664
Guido van Rossumb209a111997-04-29 18:18:01 +00003665PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003666PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003667{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003668 PyFrameObject *current_frame = PyEval_GetFrame();
3669 if (current_frame == NULL)
3670 return PyThreadState_GET()->interp->builtins;
3671 else
3672 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003673}
3674
Guido van Rossumb209a111997-04-29 18:18:01 +00003675PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003676PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003678 PyFrameObject *current_frame = PyEval_GetFrame();
3679 if (current_frame == NULL)
3680 return NULL;
3681 PyFrame_FastToLocals(current_frame);
3682 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003683}
3684
Guido van Rossumb209a111997-04-29 18:18:01 +00003685PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003686PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003687{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 PyFrameObject *current_frame = PyEval_GetFrame();
3689 if (current_frame == NULL)
3690 return NULL;
3691 else
3692 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003693}
3694
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003695PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003696PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003697{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 PyThreadState *tstate = PyThreadState_GET();
3699 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003700}
3701
Guido van Rossum6135a871995-01-09 17:53:26 +00003702int
Tim Peters5ba58662001-07-16 02:29:45 +00003703PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003704{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003705 PyFrameObject *current_frame = PyEval_GetFrame();
3706 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 if (current_frame != NULL) {
3709 const int codeflags = current_frame->f_code->co_flags;
3710 const int compilerflags = codeflags & PyCF_MASK;
3711 if (compilerflags) {
3712 result = 1;
3713 cf->cf_flags |= compilerflags;
3714 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003715#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 if (codeflags & CO_GENERATOR_ALLOWED) {
3717 result = 1;
3718 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3719 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003720#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003721 }
3722 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003723}
3724
Guido van Rossum3f5da241990-12-20 15:06:42 +00003725
Guido van Rossum681d79a1995-07-18 14:51:37 +00003726/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003727 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003728
Guido van Rossumb209a111997-04-29 18:18:01 +00003729PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003730PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003732 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003733
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003734 if (arg == NULL) {
3735 arg = PyTuple_New(0);
3736 if (arg == NULL)
3737 return NULL;
3738 }
3739 else if (!PyTuple_Check(arg)) {
3740 PyErr_SetString(PyExc_TypeError,
3741 "argument list must be a tuple");
3742 return NULL;
3743 }
3744 else
3745 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003746
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 if (kw != NULL && !PyDict_Check(kw)) {
3748 PyErr_SetString(PyExc_TypeError,
3749 "keyword list must be a dictionary");
3750 Py_DECREF(arg);
3751 return NULL;
3752 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003753
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 result = PyObject_Call(func, arg, kw);
3755 Py_DECREF(arg);
3756 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003757}
3758
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003759const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003760PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003762 if (PyMethod_Check(func))
3763 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3764 else if (PyFunction_Check(func))
3765 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3766 else if (PyCFunction_Check(func))
3767 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3768 else
3769 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003770}
3771
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003772const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003773PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003775 if (PyMethod_Check(func))
3776 return "()";
3777 else if (PyFunction_Check(func))
3778 return "()";
3779 else if (PyCFunction_Check(func))
3780 return "()";
3781 else
3782 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003783}
3784
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003785static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003786err_args(PyObject *func, int flags, int nargs)
3787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003788 if (flags & METH_NOARGS)
3789 PyErr_Format(PyExc_TypeError,
3790 "%.200s() takes no arguments (%d given)",
3791 ((PyCFunctionObject *)func)->m_ml->ml_name,
3792 nargs);
3793 else
3794 PyErr_Format(PyExc_TypeError,
3795 "%.200s() takes exactly one argument (%d given)",
3796 ((PyCFunctionObject *)func)->m_ml->ml_name,
3797 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003798}
3799
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003800#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003801if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 if (call_trace(tstate->c_profilefunc, \
3803 tstate->c_profileobj, \
3804 tstate->frame, PyTrace_C_CALL, \
3805 func)) { \
3806 x = NULL; \
3807 } \
3808 else { \
3809 x = call; \
3810 if (tstate->c_profilefunc != NULL) { \
3811 if (x == NULL) { \
3812 call_trace_protected(tstate->c_profilefunc, \
3813 tstate->c_profileobj, \
3814 tstate->frame, PyTrace_C_EXCEPTION, \
3815 func); \
3816 /* XXX should pass (type, value, tb) */ \
3817 } else { \
3818 if (call_trace(tstate->c_profilefunc, \
3819 tstate->c_profileobj, \
3820 tstate->frame, PyTrace_C_RETURN, \
3821 func)) { \
3822 Py_DECREF(x); \
3823 x = NULL; \
3824 } \
3825 } \
3826 } \
3827 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003828} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 x = call; \
3830 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003831
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003832static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003833call_function(PyObject ***pp_stack, int oparg
3834#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003836#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003838{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 int na = oparg & 0xff;
3840 int nk = (oparg>>8) & 0xff;
3841 int n = na + 2 * nk;
3842 PyObject **pfunc = (*pp_stack) - n - 1;
3843 PyObject *func = *pfunc;
3844 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003846 /* Always dispatch PyCFunction first, because these are
3847 presumed to be the most frequent callable object.
3848 */
3849 if (PyCFunction_Check(func) && nk == 0) {
3850 int flags = PyCFunction_GET_FLAGS(func);
3851 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 PCALL(PCALL_CFUNCTION);
3854 if (flags & (METH_NOARGS | METH_O)) {
3855 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3856 PyObject *self = PyCFunction_GET_SELF(func);
3857 if (flags & METH_NOARGS && na == 0) {
3858 C_TRACE(x, (*meth)(self,NULL));
3859 }
3860 else if (flags & METH_O && na == 1) {
3861 PyObject *arg = EXT_POP(*pp_stack);
3862 C_TRACE(x, (*meth)(self,arg));
3863 Py_DECREF(arg);
3864 }
3865 else {
3866 err_args(func, flags, na);
3867 x = NULL;
3868 }
3869 }
3870 else {
3871 PyObject *callargs;
3872 callargs = load_args(pp_stack, na);
3873 READ_TIMESTAMP(*pintr0);
3874 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3875 READ_TIMESTAMP(*pintr1);
3876 Py_XDECREF(callargs);
3877 }
3878 } else {
3879 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3880 /* optimize access to bound methods */
3881 PyObject *self = PyMethod_GET_SELF(func);
3882 PCALL(PCALL_METHOD);
3883 PCALL(PCALL_BOUND_METHOD);
3884 Py_INCREF(self);
3885 func = PyMethod_GET_FUNCTION(func);
3886 Py_INCREF(func);
3887 Py_DECREF(*pfunc);
3888 *pfunc = self;
3889 na++;
3890 n++;
3891 } else
3892 Py_INCREF(func);
3893 READ_TIMESTAMP(*pintr0);
3894 if (PyFunction_Check(func))
3895 x = fast_function(func, pp_stack, n, na, nk);
3896 else
3897 x = do_call(func, pp_stack, na, nk);
3898 READ_TIMESTAMP(*pintr1);
3899 Py_DECREF(func);
3900 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003901
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 /* Clear the stack of the function object. Also removes
3903 the arguments in case they weren't consumed already
3904 (fast_function() and err_args() leave them on the stack).
3905 */
3906 while ((*pp_stack) > pfunc) {
3907 w = EXT_POP(*pp_stack);
3908 Py_DECREF(w);
3909 PCALL(PCALL_POP);
3910 }
3911 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003912}
3913
Jeremy Hylton192690e2002-08-16 18:36:11 +00003914/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003915 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003916 For the simplest case -- a function that takes only positional
3917 arguments and is called with only positional arguments -- it
3918 inlines the most primitive frame setup code from
3919 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3920 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003921*/
3922
3923static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003924fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3927 PyObject *globals = PyFunction_GET_GLOBALS(func);
3928 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3929 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3930 PyObject **d = NULL;
3931 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 PCALL(PCALL_FUNCTION);
3934 PCALL(PCALL_FAST_FUNCTION);
3935 if (argdefs == NULL && co->co_argcount == n &&
3936 co->co_kwonlyargcount == 0 && nk==0 &&
3937 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3938 PyFrameObject *f;
3939 PyObject *retval = NULL;
3940 PyThreadState *tstate = PyThreadState_GET();
3941 PyObject **fastlocals, **stack;
3942 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003943
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003944 PCALL(PCALL_FASTER_FUNCTION);
3945 assert(globals != NULL);
3946 /* XXX Perhaps we should create a specialized
3947 PyFrame_New() that doesn't take locals, but does
3948 take builtins without sanity checking them.
3949 */
3950 assert(tstate != NULL);
3951 f = PyFrame_New(tstate, co, globals, NULL);
3952 if (f == NULL)
3953 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 fastlocals = f->f_localsplus;
3956 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003957
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003958 for (i = 0; i < n; i++) {
3959 Py_INCREF(*stack);
3960 fastlocals[i] = *stack++;
3961 }
3962 retval = PyEval_EvalFrameEx(f,0);
3963 ++tstate->recursion_depth;
3964 Py_DECREF(f);
3965 --tstate->recursion_depth;
3966 return retval;
3967 }
3968 if (argdefs != NULL) {
3969 d = &PyTuple_GET_ITEM(argdefs, 0);
3970 nd = Py_SIZE(argdefs);
3971 }
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003972 return PyEval_EvalCodeEx((PyObject*)co, globals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003973 (PyObject *)NULL, (*pp_stack)-n, na,
3974 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3975 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003976}
3977
3978static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003979update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3980 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003981{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003982 PyObject *kwdict = NULL;
3983 if (orig_kwdict == NULL)
3984 kwdict = PyDict_New();
3985 else {
3986 kwdict = PyDict_Copy(orig_kwdict);
3987 Py_DECREF(orig_kwdict);
3988 }
3989 if (kwdict == NULL)
3990 return NULL;
3991 while (--nk >= 0) {
3992 int err;
3993 PyObject *value = EXT_POP(*pp_stack);
3994 PyObject *key = EXT_POP(*pp_stack);
3995 if (PyDict_GetItem(kwdict, key) != NULL) {
3996 PyErr_Format(PyExc_TypeError,
3997 "%.200s%s got multiple values "
3998 "for keyword argument '%U'",
3999 PyEval_GetFuncName(func),
4000 PyEval_GetFuncDesc(func),
4001 key);
4002 Py_DECREF(key);
4003 Py_DECREF(value);
4004 Py_DECREF(kwdict);
4005 return NULL;
4006 }
4007 err = PyDict_SetItem(kwdict, key, value);
4008 Py_DECREF(key);
4009 Py_DECREF(value);
4010 if (err) {
4011 Py_DECREF(kwdict);
4012 return NULL;
4013 }
4014 }
4015 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004016}
4017
4018static PyObject *
4019update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004020 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004022 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004023
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 callargs = PyTuple_New(nstack + nstar);
4025 if (callargs == NULL) {
4026 return NULL;
4027 }
4028 if (nstar) {
4029 int i;
4030 for (i = 0; i < nstar; i++) {
4031 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4032 Py_INCREF(a);
4033 PyTuple_SET_ITEM(callargs, nstack + i, a);
4034 }
4035 }
4036 while (--nstack >= 0) {
4037 w = EXT_POP(*pp_stack);
4038 PyTuple_SET_ITEM(callargs, nstack, w);
4039 }
4040 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004041}
4042
4043static PyObject *
4044load_args(PyObject ***pp_stack, int na)
4045{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004046 PyObject *args = PyTuple_New(na);
4047 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 if (args == NULL)
4050 return NULL;
4051 while (--na >= 0) {
4052 w = EXT_POP(*pp_stack);
4053 PyTuple_SET_ITEM(args, na, w);
4054 }
4055 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004056}
4057
4058static PyObject *
4059do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4060{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 PyObject *callargs = NULL;
4062 PyObject *kwdict = NULL;
4063 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 if (nk > 0) {
4066 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4067 if (kwdict == NULL)
4068 goto call_fail;
4069 }
4070 callargs = load_args(pp_stack, na);
4071 if (callargs == NULL)
4072 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004073#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004074 /* At this point, we have to look at the type of func to
4075 update the call stats properly. Do it here so as to avoid
4076 exposing the call stats machinery outside ceval.c
4077 */
4078 if (PyFunction_Check(func))
4079 PCALL(PCALL_FUNCTION);
4080 else if (PyMethod_Check(func))
4081 PCALL(PCALL_METHOD);
4082 else if (PyType_Check(func))
4083 PCALL(PCALL_TYPE);
4084 else if (PyCFunction_Check(func))
4085 PCALL(PCALL_CFUNCTION);
4086 else
4087 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004088#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 if (PyCFunction_Check(func)) {
4090 PyThreadState *tstate = PyThreadState_GET();
4091 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4092 }
4093 else
4094 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004095call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004096 Py_XDECREF(callargs);
4097 Py_XDECREF(kwdict);
4098 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004099}
4100
4101static PyObject *
4102ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4103{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004104 int nstar = 0;
4105 PyObject *callargs = NULL;
4106 PyObject *stararg = NULL;
4107 PyObject *kwdict = NULL;
4108 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004109
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004110 if (flags & CALL_FLAG_KW) {
4111 kwdict = EXT_POP(*pp_stack);
4112 if (!PyDict_Check(kwdict)) {
4113 PyObject *d;
4114 d = PyDict_New();
4115 if (d == NULL)
4116 goto ext_call_fail;
4117 if (PyDict_Update(d, kwdict) != 0) {
4118 Py_DECREF(d);
4119 /* PyDict_Update raises attribute
4120 * error (percolated from an attempt
4121 * to get 'keys' attribute) instead of
4122 * a type error if its second argument
4123 * is not a mapping.
4124 */
4125 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4126 PyErr_Format(PyExc_TypeError,
4127 "%.200s%.200s argument after ** "
4128 "must be a mapping, not %.200s",
4129 PyEval_GetFuncName(func),
4130 PyEval_GetFuncDesc(func),
4131 kwdict->ob_type->tp_name);
4132 }
4133 goto ext_call_fail;
4134 }
4135 Py_DECREF(kwdict);
4136 kwdict = d;
4137 }
4138 }
4139 if (flags & CALL_FLAG_VAR) {
4140 stararg = EXT_POP(*pp_stack);
4141 if (!PyTuple_Check(stararg)) {
4142 PyObject *t = NULL;
4143 t = PySequence_Tuple(stararg);
4144 if (t == NULL) {
4145 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4146 PyErr_Format(PyExc_TypeError,
4147 "%.200s%.200s argument after * "
4148 "must be a sequence, not %200s",
4149 PyEval_GetFuncName(func),
4150 PyEval_GetFuncDesc(func),
4151 stararg->ob_type->tp_name);
4152 }
4153 goto ext_call_fail;
4154 }
4155 Py_DECREF(stararg);
4156 stararg = t;
4157 }
4158 nstar = PyTuple_GET_SIZE(stararg);
4159 }
4160 if (nk > 0) {
4161 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4162 if (kwdict == NULL)
4163 goto ext_call_fail;
4164 }
4165 callargs = update_star_args(na, nstar, stararg, pp_stack);
4166 if (callargs == NULL)
4167 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004168#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004169 /* At this point, we have to look at the type of func to
4170 update the call stats properly. Do it here so as to avoid
4171 exposing the call stats machinery outside ceval.c
4172 */
4173 if (PyFunction_Check(func))
4174 PCALL(PCALL_FUNCTION);
4175 else if (PyMethod_Check(func))
4176 PCALL(PCALL_METHOD);
4177 else if (PyType_Check(func))
4178 PCALL(PCALL_TYPE);
4179 else if (PyCFunction_Check(func))
4180 PCALL(PCALL_CFUNCTION);
4181 else
4182 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004183#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004184 if (PyCFunction_Check(func)) {
4185 PyThreadState *tstate = PyThreadState_GET();
4186 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4187 }
4188 else
4189 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004190ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004191 Py_XDECREF(callargs);
4192 Py_XDECREF(kwdict);
4193 Py_XDECREF(stararg);
4194 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004195}
4196
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004197/* Extract a slice index from a PyInt or PyLong or an object with the
4198 nb_index slot defined, and store in *pi.
4199 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4200 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 +00004201 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004202*/
Tim Petersb5196382001-12-16 19:44:20 +00004203/* Note: If v is NULL, return success without storing into *pi. This
4204 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4205 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004206*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004207int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004208_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004209{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004210 if (v != NULL) {
4211 Py_ssize_t x;
4212 if (PyIndex_Check(v)) {
4213 x = PyNumber_AsSsize_t(v, NULL);
4214 if (x == -1 && PyErr_Occurred())
4215 return 0;
4216 }
4217 else {
4218 PyErr_SetString(PyExc_TypeError,
4219 "slice indices must be integers or "
4220 "None or have an __index__ method");
4221 return 0;
4222 }
4223 *pi = x;
4224 }
4225 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004226}
4227
Guido van Rossum486364b2007-06-30 05:01:58 +00004228#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004229 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004230
Guido van Rossumb209a111997-04-29 18:18:01 +00004231static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004232cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004233{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 int res = 0;
4235 switch (op) {
4236 case PyCmp_IS:
4237 res = (v == w);
4238 break;
4239 case PyCmp_IS_NOT:
4240 res = (v != w);
4241 break;
4242 case PyCmp_IN:
4243 res = PySequence_Contains(w, v);
4244 if (res < 0)
4245 return NULL;
4246 break;
4247 case PyCmp_NOT_IN:
4248 res = PySequence_Contains(w, v);
4249 if (res < 0)
4250 return NULL;
4251 res = !res;
4252 break;
4253 case PyCmp_EXC_MATCH:
4254 if (PyTuple_Check(w)) {
4255 Py_ssize_t i, length;
4256 length = PyTuple_Size(w);
4257 for (i = 0; i < length; i += 1) {
4258 PyObject *exc = PyTuple_GET_ITEM(w, i);
4259 if (!PyExceptionClass_Check(exc)) {
4260 PyErr_SetString(PyExc_TypeError,
4261 CANNOT_CATCH_MSG);
4262 return NULL;
4263 }
4264 }
4265 }
4266 else {
4267 if (!PyExceptionClass_Check(w)) {
4268 PyErr_SetString(PyExc_TypeError,
4269 CANNOT_CATCH_MSG);
4270 return NULL;
4271 }
4272 }
4273 res = PyErr_GivenExceptionMatches(v, w);
4274 break;
4275 default:
4276 return PyObject_RichCompare(v, w, op);
4277 }
4278 v = res ? Py_True : Py_False;
4279 Py_INCREF(v);
4280 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004281}
4282
Thomas Wouters52152252000-08-17 22:55:00 +00004283static PyObject *
4284import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004285{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 x = PyObject_GetAttr(v, name);
4289 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4290 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4291 }
4292 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004293}
Guido van Rossumac7be682001-01-17 15:42:30 +00004294
Thomas Wouters52152252000-08-17 22:55:00 +00004295static int
4296import_all_from(PyObject *locals, PyObject *v)
4297{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004298 PyObject *all = PyObject_GetAttrString(v, "__all__");
4299 PyObject *dict, *name, *value;
4300 int skip_leading_underscores = 0;
4301 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 if (all == NULL) {
4304 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4305 return -1; /* Unexpected error */
4306 PyErr_Clear();
4307 dict = PyObject_GetAttrString(v, "__dict__");
4308 if (dict == NULL) {
4309 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4310 return -1;
4311 PyErr_SetString(PyExc_ImportError,
4312 "from-import-* object has no __dict__ and no __all__");
4313 return -1;
4314 }
4315 all = PyMapping_Keys(dict);
4316 Py_DECREF(dict);
4317 if (all == NULL)
4318 return -1;
4319 skip_leading_underscores = 1;
4320 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 for (pos = 0, err = 0; ; pos++) {
4323 name = PySequence_GetItem(all, pos);
4324 if (name == NULL) {
4325 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4326 err = -1;
4327 else
4328 PyErr_Clear();
4329 break;
4330 }
4331 if (skip_leading_underscores &&
4332 PyUnicode_Check(name) &&
4333 PyUnicode_AS_UNICODE(name)[0] == '_')
4334 {
4335 Py_DECREF(name);
4336 continue;
4337 }
4338 value = PyObject_GetAttr(v, name);
4339 if (value == NULL)
4340 err = -1;
4341 else if (PyDict_CheckExact(locals))
4342 err = PyDict_SetItem(locals, name, value);
4343 else
4344 err = PyObject_SetItem(locals, name, value);
4345 Py_DECREF(name);
4346 Py_XDECREF(value);
4347 if (err != 0)
4348 break;
4349 }
4350 Py_DECREF(all);
4351 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004352}
4353
Guido van Rossumac7be682001-01-17 15:42:30 +00004354static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004355format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004357 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004359 if (!obj)
4360 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004361
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 obj_str = _PyUnicode_AsString(obj);
4363 if (!obj_str)
4364 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004367}
Guido van Rossum950361c1997-01-24 13:49:28 +00004368
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004369static void
4370format_exc_unbound(PyCodeObject *co, int oparg)
4371{
4372 PyObject *name;
4373 /* Don't stomp existing exception */
4374 if (PyErr_Occurred())
4375 return;
4376 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4377 name = PyTuple_GET_ITEM(co->co_cellvars,
4378 oparg);
4379 format_exc_check_arg(
4380 PyExc_UnboundLocalError,
4381 UNBOUNDLOCAL_ERROR_MSG,
4382 name);
4383 } else {
4384 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4385 PyTuple_GET_SIZE(co->co_cellvars));
4386 format_exc_check_arg(PyExc_NameError,
4387 UNBOUNDFREE_ERROR_MSG, name);
4388 }
4389}
4390
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004391static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004392unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004393 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004394{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004395 /* This function implements 'variable += expr' when both arguments
4396 are (Unicode) strings. */
4397 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4398 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4399 Py_ssize_t new_len = v_len + w_len;
4400 if (new_len < 0) {
4401 PyErr_SetString(PyExc_OverflowError,
4402 "strings are too large to concat");
4403 return NULL;
4404 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004405
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004406 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 /* In the common case, there are 2 references to the value
4408 * stored in 'variable' when the += is performed: one on the
4409 * value stack (in 'v') and one still stored in the
4410 * 'variable'. We try to delete the variable now to reduce
4411 * the refcnt to 1.
4412 */
4413 switch (*next_instr) {
4414 case STORE_FAST:
4415 {
4416 int oparg = PEEKARG();
4417 PyObject **fastlocals = f->f_localsplus;
4418 if (GETLOCAL(oparg) == v)
4419 SETLOCAL(oparg, NULL);
4420 break;
4421 }
4422 case STORE_DEREF:
4423 {
4424 PyObject **freevars = (f->f_localsplus +
4425 f->f_code->co_nlocals);
4426 PyObject *c = freevars[PEEKARG()];
4427 if (PyCell_GET(c) == v)
4428 PyCell_Set(c, NULL);
4429 break;
4430 }
4431 case STORE_NAME:
4432 {
4433 PyObject *names = f->f_code->co_names;
4434 PyObject *name = GETITEM(names, PEEKARG());
4435 PyObject *locals = f->f_locals;
4436 if (PyDict_CheckExact(locals) &&
4437 PyDict_GetItem(locals, name) == v) {
4438 if (PyDict_DelItem(locals, name) != 0) {
4439 PyErr_Clear();
4440 }
4441 }
4442 break;
4443 }
4444 }
4445 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004446
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004447 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 /* Now we own the last reference to 'v', so we can resize it
4449 * in-place.
4450 */
4451 if (PyUnicode_Resize(&v, new_len) != 0) {
4452 /* XXX if PyUnicode_Resize() fails, 'v' has been
4453 * deallocated so it cannot be put back into
4454 * 'variable'. The MemoryError is raised when there
4455 * is no value in 'variable', which might (very
4456 * remotely) be a cause of incompatibilities.
4457 */
4458 return NULL;
4459 }
4460 /* copy 'w' into the newly allocated area of 'v' */
4461 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4462 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4463 return v;
4464 }
4465 else {
4466 /* When in-place resizing is not an option. */
4467 w = PyUnicode_Concat(v, w);
4468 Py_DECREF(v);
4469 return w;
4470 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004471}
4472
Guido van Rossum950361c1997-01-24 13:49:28 +00004473#ifdef DYNAMIC_EXECUTION_PROFILE
4474
Skip Montanarof118cb12001-10-15 20:51:38 +00004475static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004476getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 int i;
4479 PyObject *l = PyList_New(256);
4480 if (l == NULL) return NULL;
4481 for (i = 0; i < 256; i++) {
4482 PyObject *x = PyLong_FromLong(a[i]);
4483 if (x == NULL) {
4484 Py_DECREF(l);
4485 return NULL;
4486 }
4487 PyList_SetItem(l, i, x);
4488 }
4489 for (i = 0; i < 256; i++)
4490 a[i] = 0;
4491 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004492}
4493
4494PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004495_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004496{
4497#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004499#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 int i;
4501 PyObject *l = PyList_New(257);
4502 if (l == NULL) return NULL;
4503 for (i = 0; i < 257; i++) {
4504 PyObject *x = getarray(dxpairs[i]);
4505 if (x == NULL) {
4506 Py_DECREF(l);
4507 return NULL;
4508 }
4509 PyList_SetItem(l, i, x);
4510 }
4511 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004512#endif
4513}
4514
4515#endif