blob: 140112fc1395925d47dd0cb7e12ab3523aed1470 [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 *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000758PyEval_EvalCode(PyCodeObject *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 *
3062PyEval_EvalCodeEx(PyCodeObject *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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 register PyFrameObject *f;
3067 register PyObject *retval = NULL;
3068 register PyObject **fastlocals, **freevars;
3069 PyThreadState *tstate = PyThreadState_GET();
3070 PyObject *x, *u;
3071 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003072
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 if (globals == NULL) {
3074 PyErr_SetString(PyExc_SystemError,
3075 "PyEval_EvalCodeEx: NULL globals");
3076 return NULL;
3077 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 assert(tstate != NULL);
3080 assert(globals != NULL);
3081 f = PyFrame_New(tstate, co, globals, locals);
3082 if (f == NULL)
3083 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003085 fastlocals = f->f_localsplus;
3086 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3089 int i;
3090 int n = argcount;
3091 PyObject *kwdict = NULL;
3092 if (co->co_flags & CO_VARKEYWORDS) {
3093 kwdict = PyDict_New();
3094 if (kwdict == NULL)
3095 goto fail;
3096 i = total_args;
3097 if (co->co_flags & CO_VARARGS)
3098 i++;
3099 SETLOCAL(i, kwdict);
3100 }
3101 if (argcount > co->co_argcount) {
3102 if (!(co->co_flags & CO_VARARGS)) {
3103 PyErr_Format(PyExc_TypeError,
3104 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003105 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003106 co->co_name,
3107 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003108 co->co_argcount,
3109 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonaa7fbd92010-09-25 03:25:42 +00003110 argcount + kwcount);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003111 goto fail;
3112 }
3113 n = co->co_argcount;
3114 }
3115 for (i = 0; i < n; i++) {
3116 x = args[i];
3117 Py_INCREF(x);
3118 SETLOCAL(i, x);
3119 }
3120 if (co->co_flags & CO_VARARGS) {
3121 u = PyTuple_New(argcount - n);
3122 if (u == NULL)
3123 goto fail;
3124 SETLOCAL(total_args, u);
3125 for (i = n; i < argcount; i++) {
3126 x = args[i];
3127 Py_INCREF(x);
3128 PyTuple_SET_ITEM(u, i-n, x);
3129 }
3130 }
3131 for (i = 0; i < kwcount; i++) {
3132 PyObject **co_varnames;
3133 PyObject *keyword = kws[2*i];
3134 PyObject *value = kws[2*i + 1];
3135 int j;
3136 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3137 PyErr_Format(PyExc_TypeError,
3138 "%U() keywords must be strings",
3139 co->co_name);
3140 goto fail;
3141 }
3142 /* Speed hack: do raw pointer compares. As names are
3143 normally interned this should almost always hit. */
3144 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3145 for (j = 0; j < total_args; j++) {
3146 PyObject *nm = co_varnames[j];
3147 if (nm == keyword)
3148 goto kw_found;
3149 }
3150 /* Slow fallback, just in case */
3151 for (j = 0; j < total_args; j++) {
3152 PyObject *nm = co_varnames[j];
3153 int cmp = PyObject_RichCompareBool(
3154 keyword, nm, Py_EQ);
3155 if (cmp > 0)
3156 goto kw_found;
3157 else if (cmp < 0)
3158 goto fail;
3159 }
3160 if (j >= total_args && kwdict == NULL) {
3161 PyErr_Format(PyExc_TypeError,
3162 "%U() got an unexpected "
3163 "keyword argument '%S'",
3164 co->co_name,
3165 keyword);
3166 goto fail;
3167 }
3168 PyDict_SetItem(kwdict, keyword, value);
3169 continue;
3170 kw_found:
3171 if (GETLOCAL(j) != NULL) {
3172 PyErr_Format(PyExc_TypeError,
3173 "%U() got multiple "
3174 "values for keyword "
3175 "argument '%S'",
3176 co->co_name,
3177 keyword);
3178 goto fail;
3179 }
3180 Py_INCREF(value);
3181 SETLOCAL(j, value);
3182 }
3183 if (co->co_kwonlyargcount > 0) {
3184 for (i = co->co_argcount; i < total_args; i++) {
3185 PyObject *name;
3186 if (GETLOCAL(i) != NULL)
3187 continue;
3188 name = PyTuple_GET_ITEM(co->co_varnames, i);
3189 if (kwdefs != NULL) {
3190 PyObject *def = PyDict_GetItem(kwdefs, name);
3191 if (def) {
3192 Py_INCREF(def);
3193 SETLOCAL(i, def);
3194 continue;
3195 }
3196 }
3197 PyErr_Format(PyExc_TypeError,
3198 "%U() needs keyword-only argument %S",
3199 co->co_name, name);
3200 goto fail;
3201 }
3202 }
3203 if (argcount < co->co_argcount) {
3204 int m = co->co_argcount - defcount;
3205 for (i = argcount; i < m; i++) {
3206 if (GETLOCAL(i) == NULL) {
3207 int j, given = 0;
3208 for (j = 0; j < co->co_argcount; j++)
3209 if (GETLOCAL(j))
3210 given++;
3211 PyErr_Format(PyExc_TypeError,
3212 "%U() takes %s %d "
3213 "argument%s "
3214 "(%d given)",
3215 co->co_name,
3216 ((co->co_flags & CO_VARARGS) ||
3217 defcount) ? "at least"
3218 : "exactly",
3219 m, m == 1 ? "" : "s", given);
3220 goto fail;
3221 }
3222 }
3223 if (n > m)
3224 i = n - m;
3225 else
3226 i = 0;
3227 for (; i < defcount; i++) {
3228 if (GETLOCAL(m+i) == NULL) {
3229 PyObject *def = defs[i];
3230 Py_INCREF(def);
3231 SETLOCAL(m+i, def);
3232 }
3233 }
3234 }
3235 }
3236 else if (argcount > 0 || kwcount > 0) {
3237 PyErr_Format(PyExc_TypeError,
3238 "%U() takes no arguments (%d given)",
3239 co->co_name,
3240 argcount + kwcount);
3241 goto fail;
3242 }
3243 /* Allocate and initialize storage for cell vars, and copy free
3244 vars into frame. This isn't too efficient right now. */
3245 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3246 int i, j, nargs, found;
3247 Py_UNICODE *cellname, *argname;
3248 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003250 nargs = total_args;
3251 if (co->co_flags & CO_VARARGS)
3252 nargs++;
3253 if (co->co_flags & CO_VARKEYWORDS)
3254 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003256 /* Initialize each cell var, taking into account
3257 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003259 Should arrange for the compiler to put cellvars
3260 that are arguments at the beginning of the cellvars
3261 list so that we can march over it more efficiently?
3262 */
3263 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3264 cellname = PyUnicode_AS_UNICODE(
3265 PyTuple_GET_ITEM(co->co_cellvars, i));
3266 found = 0;
3267 for (j = 0; j < nargs; j++) {
3268 argname = PyUnicode_AS_UNICODE(
3269 PyTuple_GET_ITEM(co->co_varnames, j));
3270 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3271 c = PyCell_New(GETLOCAL(j));
3272 if (c == NULL)
3273 goto fail;
3274 GETLOCAL(co->co_nlocals + i) = c;
3275 found = 1;
3276 break;
3277 }
3278 }
3279 if (found == 0) {
3280 c = PyCell_New(NULL);
3281 if (c == NULL)
3282 goto fail;
3283 SETLOCAL(co->co_nlocals + i, c);
3284 }
3285 }
3286 }
3287 if (PyTuple_GET_SIZE(co->co_freevars)) {
3288 int i;
3289 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3290 PyObject *o = PyTuple_GET_ITEM(closure, i);
3291 Py_INCREF(o);
3292 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3293 }
3294 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 if (co->co_flags & CO_GENERATOR) {
3297 /* Don't need to keep the reference to f_back, it will be set
3298 * when the generator is resumed. */
3299 Py_XDECREF(f->f_back);
3300 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003302 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003304 /* Create a new generator that owns the ready to run frame
3305 * and return that as the value. */
3306 return PyGen_New(f);
3307 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003309 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003310
Thomas Woutersce272b62007-09-19 21:19:28 +00003311fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003312
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 /* decref'ing the frame can cause __del__ methods to get invoked,
3314 which can call back into Python. While we're done with the
3315 current Python frame (f), the associated C stack is still in use,
3316 so recursion_depth must be boosted for the duration.
3317 */
3318 assert(tstate != NULL);
3319 ++tstate->recursion_depth;
3320 Py_DECREF(f);
3321 --tstate->recursion_depth;
3322 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003323}
3324
3325
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003326static PyObject *
3327special_lookup(PyObject *o, char *meth, PyObject **cache)
3328{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 PyObject *res;
3330 res = _PyObject_LookupSpecial(o, meth, cache);
3331 if (res == NULL && !PyErr_Occurred()) {
3332 PyErr_SetObject(PyExc_AttributeError, *cache);
3333 return NULL;
3334 }
3335 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003336}
3337
3338
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003339/* Logic for the raise statement (too complicated for inlining).
3340 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003341static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003342do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003343{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003344 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003345
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003346 if (exc == NULL) {
3347 /* Reraise */
3348 PyThreadState *tstate = PyThreadState_GET();
3349 PyObject *tb;
3350 type = tstate->exc_type;
3351 value = tstate->exc_value;
3352 tb = tstate->exc_traceback;
3353 if (type == Py_None) {
3354 PyErr_SetString(PyExc_RuntimeError,
3355 "No active exception to reraise");
3356 return WHY_EXCEPTION;
3357 }
3358 Py_XINCREF(type);
3359 Py_XINCREF(value);
3360 Py_XINCREF(tb);
3361 PyErr_Restore(type, value, tb);
3362 return WHY_RERAISE;
3363 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003365 /* We support the following forms of raise:
3366 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003367 raise <instance>
3368 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003370 if (PyExceptionClass_Check(exc)) {
3371 type = exc;
3372 value = PyObject_CallObject(exc, NULL);
3373 if (value == NULL)
3374 goto raise_error;
3375 }
3376 else if (PyExceptionInstance_Check(exc)) {
3377 value = exc;
3378 type = PyExceptionInstance_Class(exc);
3379 Py_INCREF(type);
3380 }
3381 else {
3382 /* Not something you can raise. You get an exception
3383 anyway, just not what you specified :-) */
3384 Py_DECREF(exc);
3385 PyErr_SetString(PyExc_TypeError,
3386 "exceptions must derive from BaseException");
3387 goto raise_error;
3388 }
Collin Winter828f04a2007-08-31 00:04:24 +00003389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003390 if (cause) {
3391 PyObject *fixed_cause;
3392 if (PyExceptionClass_Check(cause)) {
3393 fixed_cause = PyObject_CallObject(cause, NULL);
3394 if (fixed_cause == NULL)
3395 goto raise_error;
3396 Py_DECREF(cause);
3397 }
3398 else if (PyExceptionInstance_Check(cause)) {
3399 fixed_cause = cause;
3400 }
3401 else {
3402 PyErr_SetString(PyExc_TypeError,
3403 "exception causes must derive from "
3404 "BaseException");
3405 goto raise_error;
3406 }
3407 PyException_SetCause(value, fixed_cause);
3408 }
Collin Winter828f04a2007-08-31 00:04:24 +00003409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 PyErr_SetObject(type, value);
3411 /* PyErr_SetObject incref's its arguments */
3412 Py_XDECREF(value);
3413 Py_XDECREF(type);
3414 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003415
3416raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 Py_XDECREF(value);
3418 Py_XDECREF(type);
3419 Py_XDECREF(cause);
3420 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003421}
3422
Tim Petersd6d010b2001-06-21 02:49:55 +00003423/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003424 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003425
Guido van Rossum0368b722007-05-11 16:50:42 +00003426 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3427 with a variable target.
3428*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003429
Barry Warsawe42b18f1997-08-25 22:13:04 +00003430static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003431unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 int i = 0, j = 0;
3434 Py_ssize_t ll = 0;
3435 PyObject *it; /* iter(v) */
3436 PyObject *w;
3437 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003438
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003440
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 it = PyObject_GetIter(v);
3442 if (it == NULL)
3443 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003445 for (; i < argcnt; i++) {
3446 w = PyIter_Next(it);
3447 if (w == NULL) {
3448 /* Iterator done, via error or exhaustion. */
3449 if (!PyErr_Occurred()) {
3450 PyErr_Format(PyExc_ValueError,
3451 "need more than %d value%s to unpack",
3452 i, i == 1 ? "" : "s");
3453 }
3454 goto Error;
3455 }
3456 *--sp = w;
3457 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003458
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003459 if (argcntafter == -1) {
3460 /* We better have exhausted the iterator now. */
3461 w = PyIter_Next(it);
3462 if (w == NULL) {
3463 if (PyErr_Occurred())
3464 goto Error;
3465 Py_DECREF(it);
3466 return 1;
3467 }
3468 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003469 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3470 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003471 goto Error;
3472 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 l = PySequence_List(it);
3475 if (l == NULL)
3476 goto Error;
3477 *--sp = l;
3478 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003479
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 ll = PyList_GET_SIZE(l);
3481 if (ll < argcntafter) {
3482 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3483 argcnt + ll);
3484 goto Error;
3485 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003487 /* Pop the "after-variable" args off the list. */
3488 for (j = argcntafter; j > 0; j--, i++) {
3489 *--sp = PyList_GET_ITEM(l, ll - j);
3490 }
3491 /* Resize the list. */
3492 Py_SIZE(l) = ll - argcntafter;
3493 Py_DECREF(it);
3494 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003495
Tim Petersd6d010b2001-06-21 02:49:55 +00003496Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 for (; i > 0; i--, sp++)
3498 Py_DECREF(*sp);
3499 Py_XDECREF(it);
3500 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003501}
3502
3503
Guido van Rossum96a42c81992-01-12 02:29:51 +00003504#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003505static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003506prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003507{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 printf("%s ", str);
3509 if (PyObject_Print(v, stdout, 0) != 0)
3510 PyErr_Clear(); /* Don't know what else to do */
3511 printf("\n");
3512 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003513}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003514#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003515
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003516static void
Fred Drake5755ce62001-06-27 19:19:46 +00003517call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003519 PyObject *type, *value, *traceback, *arg;
3520 int err;
3521 PyErr_Fetch(&type, &value, &traceback);
3522 if (value == NULL) {
3523 value = Py_None;
3524 Py_INCREF(value);
3525 }
3526 arg = PyTuple_Pack(3, type, value, traceback);
3527 if (arg == NULL) {
3528 PyErr_Restore(type, value, traceback);
3529 return;
3530 }
3531 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3532 Py_DECREF(arg);
3533 if (err == 0)
3534 PyErr_Restore(type, value, traceback);
3535 else {
3536 Py_XDECREF(type);
3537 Py_XDECREF(value);
3538 Py_XDECREF(traceback);
3539 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003540}
3541
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003542static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003543call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003545{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003546 PyObject *type, *value, *traceback;
3547 int err;
3548 PyErr_Fetch(&type, &value, &traceback);
3549 err = call_trace(func, obj, frame, what, arg);
3550 if (err == 0)
3551 {
3552 PyErr_Restore(type, value, traceback);
3553 return 0;
3554 }
3555 else {
3556 Py_XDECREF(type);
3557 Py_XDECREF(value);
3558 Py_XDECREF(traceback);
3559 return -1;
3560 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003561}
3562
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003563static int
Fred Drake5755ce62001-06-27 19:19:46 +00003564call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003565 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003567 register PyThreadState *tstate = frame->f_tstate;
3568 int result;
3569 if (tstate->tracing)
3570 return 0;
3571 tstate->tracing++;
3572 tstate->use_tracing = 0;
3573 result = func(obj, frame, what, arg);
3574 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3575 || (tstate->c_profilefunc != NULL));
3576 tstate->tracing--;
3577 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003578}
3579
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003580PyObject *
3581_PyEval_CallTracing(PyObject *func, PyObject *args)
3582{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003583 PyFrameObject *frame = PyEval_GetFrame();
3584 PyThreadState *tstate = frame->f_tstate;
3585 int save_tracing = tstate->tracing;
3586 int save_use_tracing = tstate->use_tracing;
3587 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003589 tstate->tracing = 0;
3590 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3591 || (tstate->c_profilefunc != NULL));
3592 result = PyObject_Call(func, args, NULL);
3593 tstate->tracing = save_tracing;
3594 tstate->use_tracing = save_use_tracing;
3595 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003596}
3597
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003598/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003599static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003600maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3602 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003603{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 int result = 0;
3605 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003606
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 /* If the last instruction executed isn't in the current
3608 instruction window, reset the window.
3609 */
3610 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3611 PyAddrPair bounds;
3612 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3613 &bounds);
3614 *instr_lb = bounds.ap_lower;
3615 *instr_ub = bounds.ap_upper;
3616 }
3617 /* If the last instruction falls at the start of a line or if
3618 it represents a jump backwards, update the frame's line
3619 number and call the trace function. */
3620 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3621 frame->f_lineno = line;
3622 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3623 }
3624 *instr_prev = frame->f_lasti;
3625 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003626}
3627
Fred Drake5755ce62001-06-27 19:19:46 +00003628void
3629PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 PyThreadState *tstate = PyThreadState_GET();
3632 PyObject *temp = tstate->c_profileobj;
3633 Py_XINCREF(arg);
3634 tstate->c_profilefunc = NULL;
3635 tstate->c_profileobj = NULL;
3636 /* Must make sure that tracing is not ignored if 'temp' is freed */
3637 tstate->use_tracing = tstate->c_tracefunc != NULL;
3638 Py_XDECREF(temp);
3639 tstate->c_profilefunc = func;
3640 tstate->c_profileobj = arg;
3641 /* Flag that tracing or profiling is turned on */
3642 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003643}
3644
3645void
3646PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3647{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003648 PyThreadState *tstate = PyThreadState_GET();
3649 PyObject *temp = tstate->c_traceobj;
3650 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3651 Py_XINCREF(arg);
3652 tstate->c_tracefunc = NULL;
3653 tstate->c_traceobj = NULL;
3654 /* Must make sure that profiling is not ignored if 'temp' is freed */
3655 tstate->use_tracing = tstate->c_profilefunc != NULL;
3656 Py_XDECREF(temp);
3657 tstate->c_tracefunc = func;
3658 tstate->c_traceobj = arg;
3659 /* Flag that tracing or profiling is turned on */
3660 tstate->use_tracing = ((func != NULL)
3661 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003662}
3663
Guido van Rossumb209a111997-04-29 18:18:01 +00003664PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003665PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003666{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003667 PyFrameObject *current_frame = PyEval_GetFrame();
3668 if (current_frame == NULL)
3669 return PyThreadState_GET()->interp->builtins;
3670 else
3671 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003672}
3673
Guido van Rossumb209a111997-04-29 18:18:01 +00003674PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003675PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 PyFrameObject *current_frame = PyEval_GetFrame();
3678 if (current_frame == NULL)
3679 return NULL;
3680 PyFrame_FastToLocals(current_frame);
3681 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003682}
3683
Guido van Rossumb209a111997-04-29 18:18:01 +00003684PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003685PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 PyFrameObject *current_frame = PyEval_GetFrame();
3688 if (current_frame == NULL)
3689 return NULL;
3690 else
3691 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003692}
3693
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003694PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003695PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003697 PyThreadState *tstate = PyThreadState_GET();
3698 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003699}
3700
Guido van Rossum6135a871995-01-09 17:53:26 +00003701int
Tim Peters5ba58662001-07-16 02:29:45 +00003702PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003704 PyFrameObject *current_frame = PyEval_GetFrame();
3705 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003706
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003707 if (current_frame != NULL) {
3708 const int codeflags = current_frame->f_code->co_flags;
3709 const int compilerflags = codeflags & PyCF_MASK;
3710 if (compilerflags) {
3711 result = 1;
3712 cf->cf_flags |= compilerflags;
3713 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003714#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003715 if (codeflags & CO_GENERATOR_ALLOWED) {
3716 result = 1;
3717 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3718 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003719#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003720 }
3721 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003722}
3723
Guido van Rossum3f5da241990-12-20 15:06:42 +00003724
Guido van Rossum681d79a1995-07-18 14:51:37 +00003725/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003726 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003727
Guido van Rossumb209a111997-04-29 18:18:01 +00003728PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003729PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003731 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 if (arg == NULL) {
3734 arg = PyTuple_New(0);
3735 if (arg == NULL)
3736 return NULL;
3737 }
3738 else if (!PyTuple_Check(arg)) {
3739 PyErr_SetString(PyExc_TypeError,
3740 "argument list must be a tuple");
3741 return NULL;
3742 }
3743 else
3744 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003746 if (kw != NULL && !PyDict_Check(kw)) {
3747 PyErr_SetString(PyExc_TypeError,
3748 "keyword list must be a dictionary");
3749 Py_DECREF(arg);
3750 return NULL;
3751 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003753 result = PyObject_Call(func, arg, kw);
3754 Py_DECREF(arg);
3755 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003756}
3757
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003758const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003759PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003760{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003761 if (PyMethod_Check(func))
3762 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3763 else if (PyFunction_Check(func))
3764 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3765 else if (PyCFunction_Check(func))
3766 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3767 else
3768 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003769}
3770
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003771const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003772PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003773{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 if (PyMethod_Check(func))
3775 return "()";
3776 else if (PyFunction_Check(func))
3777 return "()";
3778 else if (PyCFunction_Check(func))
3779 return "()";
3780 else
3781 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003782}
3783
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003784static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003785err_args(PyObject *func, int flags, int nargs)
3786{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 if (flags & METH_NOARGS)
3788 PyErr_Format(PyExc_TypeError,
3789 "%.200s() takes no arguments (%d given)",
3790 ((PyCFunctionObject *)func)->m_ml->ml_name,
3791 nargs);
3792 else
3793 PyErr_Format(PyExc_TypeError,
3794 "%.200s() takes exactly one argument (%d given)",
3795 ((PyCFunctionObject *)func)->m_ml->ml_name,
3796 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003797}
3798
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003799#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003800if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 if (call_trace(tstate->c_profilefunc, \
3802 tstate->c_profileobj, \
3803 tstate->frame, PyTrace_C_CALL, \
3804 func)) { \
3805 x = NULL; \
3806 } \
3807 else { \
3808 x = call; \
3809 if (tstate->c_profilefunc != NULL) { \
3810 if (x == NULL) { \
3811 call_trace_protected(tstate->c_profilefunc, \
3812 tstate->c_profileobj, \
3813 tstate->frame, PyTrace_C_EXCEPTION, \
3814 func); \
3815 /* XXX should pass (type, value, tb) */ \
3816 } else { \
3817 if (call_trace(tstate->c_profilefunc, \
3818 tstate->c_profileobj, \
3819 tstate->frame, PyTrace_C_RETURN, \
3820 func)) { \
3821 Py_DECREF(x); \
3822 x = NULL; \
3823 } \
3824 } \
3825 } \
3826 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003827} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 x = call; \
3829 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003830
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003831static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003832call_function(PyObject ***pp_stack, int oparg
3833#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003835#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003836 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003837{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003838 int na = oparg & 0xff;
3839 int nk = (oparg>>8) & 0xff;
3840 int n = na + 2 * nk;
3841 PyObject **pfunc = (*pp_stack) - n - 1;
3842 PyObject *func = *pfunc;
3843 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003845 /* Always dispatch PyCFunction first, because these are
3846 presumed to be the most frequent callable object.
3847 */
3848 if (PyCFunction_Check(func) && nk == 0) {
3849 int flags = PyCFunction_GET_FLAGS(func);
3850 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003852 PCALL(PCALL_CFUNCTION);
3853 if (flags & (METH_NOARGS | METH_O)) {
3854 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3855 PyObject *self = PyCFunction_GET_SELF(func);
3856 if (flags & METH_NOARGS && na == 0) {
3857 C_TRACE(x, (*meth)(self,NULL));
3858 }
3859 else if (flags & METH_O && na == 1) {
3860 PyObject *arg = EXT_POP(*pp_stack);
3861 C_TRACE(x, (*meth)(self,arg));
3862 Py_DECREF(arg);
3863 }
3864 else {
3865 err_args(func, flags, na);
3866 x = NULL;
3867 }
3868 }
3869 else {
3870 PyObject *callargs;
3871 callargs = load_args(pp_stack, na);
3872 READ_TIMESTAMP(*pintr0);
3873 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3874 READ_TIMESTAMP(*pintr1);
3875 Py_XDECREF(callargs);
3876 }
3877 } else {
3878 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3879 /* optimize access to bound methods */
3880 PyObject *self = PyMethod_GET_SELF(func);
3881 PCALL(PCALL_METHOD);
3882 PCALL(PCALL_BOUND_METHOD);
3883 Py_INCREF(self);
3884 func = PyMethod_GET_FUNCTION(func);
3885 Py_INCREF(func);
3886 Py_DECREF(*pfunc);
3887 *pfunc = self;
3888 na++;
3889 n++;
3890 } else
3891 Py_INCREF(func);
3892 READ_TIMESTAMP(*pintr0);
3893 if (PyFunction_Check(func))
3894 x = fast_function(func, pp_stack, n, na, nk);
3895 else
3896 x = do_call(func, pp_stack, na, nk);
3897 READ_TIMESTAMP(*pintr1);
3898 Py_DECREF(func);
3899 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003901 /* Clear the stack of the function object. Also removes
3902 the arguments in case they weren't consumed already
3903 (fast_function() and err_args() leave them on the stack).
3904 */
3905 while ((*pp_stack) > pfunc) {
3906 w = EXT_POP(*pp_stack);
3907 Py_DECREF(w);
3908 PCALL(PCALL_POP);
3909 }
3910 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003911}
3912
Jeremy Hylton192690e2002-08-16 18:36:11 +00003913/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003914 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003915 For the simplest case -- a function that takes only positional
3916 arguments and is called with only positional arguments -- it
3917 inlines the most primitive frame setup code from
3918 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3919 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003920*/
3921
3922static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003923fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003924{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003925 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3926 PyObject *globals = PyFunction_GET_GLOBALS(func);
3927 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3928 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3929 PyObject **d = NULL;
3930 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003932 PCALL(PCALL_FUNCTION);
3933 PCALL(PCALL_FAST_FUNCTION);
3934 if (argdefs == NULL && co->co_argcount == n &&
3935 co->co_kwonlyargcount == 0 && nk==0 &&
3936 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3937 PyFrameObject *f;
3938 PyObject *retval = NULL;
3939 PyThreadState *tstate = PyThreadState_GET();
3940 PyObject **fastlocals, **stack;
3941 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003942
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 PCALL(PCALL_FASTER_FUNCTION);
3944 assert(globals != NULL);
3945 /* XXX Perhaps we should create a specialized
3946 PyFrame_New() that doesn't take locals, but does
3947 take builtins without sanity checking them.
3948 */
3949 assert(tstate != NULL);
3950 f = PyFrame_New(tstate, co, globals, NULL);
3951 if (f == NULL)
3952 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003954 fastlocals = f->f_localsplus;
3955 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 for (i = 0; i < n; i++) {
3958 Py_INCREF(*stack);
3959 fastlocals[i] = *stack++;
3960 }
3961 retval = PyEval_EvalFrameEx(f,0);
3962 ++tstate->recursion_depth;
3963 Py_DECREF(f);
3964 --tstate->recursion_depth;
3965 return retval;
3966 }
3967 if (argdefs != NULL) {
3968 d = &PyTuple_GET_ITEM(argdefs, 0);
3969 nd = Py_SIZE(argdefs);
3970 }
3971 return PyEval_EvalCodeEx(co, globals,
3972 (PyObject *)NULL, (*pp_stack)-n, na,
3973 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3974 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003975}
3976
3977static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003978update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3979 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003980{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 PyObject *kwdict = NULL;
3982 if (orig_kwdict == NULL)
3983 kwdict = PyDict_New();
3984 else {
3985 kwdict = PyDict_Copy(orig_kwdict);
3986 Py_DECREF(orig_kwdict);
3987 }
3988 if (kwdict == NULL)
3989 return NULL;
3990 while (--nk >= 0) {
3991 int err;
3992 PyObject *value = EXT_POP(*pp_stack);
3993 PyObject *key = EXT_POP(*pp_stack);
3994 if (PyDict_GetItem(kwdict, key) != NULL) {
3995 PyErr_Format(PyExc_TypeError,
3996 "%.200s%s got multiple values "
3997 "for keyword argument '%U'",
3998 PyEval_GetFuncName(func),
3999 PyEval_GetFuncDesc(func),
4000 key);
4001 Py_DECREF(key);
4002 Py_DECREF(value);
4003 Py_DECREF(kwdict);
4004 return NULL;
4005 }
4006 err = PyDict_SetItem(kwdict, key, value);
4007 Py_DECREF(key);
4008 Py_DECREF(value);
4009 if (err) {
4010 Py_DECREF(kwdict);
4011 return NULL;
4012 }
4013 }
4014 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004015}
4016
4017static PyObject *
4018update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004021 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 callargs = PyTuple_New(nstack + nstar);
4024 if (callargs == NULL) {
4025 return NULL;
4026 }
4027 if (nstar) {
4028 int i;
4029 for (i = 0; i < nstar; i++) {
4030 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4031 Py_INCREF(a);
4032 PyTuple_SET_ITEM(callargs, nstack + i, a);
4033 }
4034 }
4035 while (--nstack >= 0) {
4036 w = EXT_POP(*pp_stack);
4037 PyTuple_SET_ITEM(callargs, nstack, w);
4038 }
4039 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004040}
4041
4042static PyObject *
4043load_args(PyObject ***pp_stack, int na)
4044{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 PyObject *args = PyTuple_New(na);
4046 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004048 if (args == NULL)
4049 return NULL;
4050 while (--na >= 0) {
4051 w = EXT_POP(*pp_stack);
4052 PyTuple_SET_ITEM(args, na, w);
4053 }
4054 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004055}
4056
4057static PyObject *
4058do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4059{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004060 PyObject *callargs = NULL;
4061 PyObject *kwdict = NULL;
4062 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004064 if (nk > 0) {
4065 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4066 if (kwdict == NULL)
4067 goto call_fail;
4068 }
4069 callargs = load_args(pp_stack, na);
4070 if (callargs == NULL)
4071 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004072#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004073 /* At this point, we have to look at the type of func to
4074 update the call stats properly. Do it here so as to avoid
4075 exposing the call stats machinery outside ceval.c
4076 */
4077 if (PyFunction_Check(func))
4078 PCALL(PCALL_FUNCTION);
4079 else if (PyMethod_Check(func))
4080 PCALL(PCALL_METHOD);
4081 else if (PyType_Check(func))
4082 PCALL(PCALL_TYPE);
4083 else if (PyCFunction_Check(func))
4084 PCALL(PCALL_CFUNCTION);
4085 else
4086 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004087#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 if (PyCFunction_Check(func)) {
4089 PyThreadState *tstate = PyThreadState_GET();
4090 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4091 }
4092 else
4093 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004094call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004095 Py_XDECREF(callargs);
4096 Py_XDECREF(kwdict);
4097 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004098}
4099
4100static PyObject *
4101ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004103 int nstar = 0;
4104 PyObject *callargs = NULL;
4105 PyObject *stararg = NULL;
4106 PyObject *kwdict = NULL;
4107 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004109 if (flags & CALL_FLAG_KW) {
4110 kwdict = EXT_POP(*pp_stack);
4111 if (!PyDict_Check(kwdict)) {
4112 PyObject *d;
4113 d = PyDict_New();
4114 if (d == NULL)
4115 goto ext_call_fail;
4116 if (PyDict_Update(d, kwdict) != 0) {
4117 Py_DECREF(d);
4118 /* PyDict_Update raises attribute
4119 * error (percolated from an attempt
4120 * to get 'keys' attribute) instead of
4121 * a type error if its second argument
4122 * is not a mapping.
4123 */
4124 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4125 PyErr_Format(PyExc_TypeError,
4126 "%.200s%.200s argument after ** "
4127 "must be a mapping, not %.200s",
4128 PyEval_GetFuncName(func),
4129 PyEval_GetFuncDesc(func),
4130 kwdict->ob_type->tp_name);
4131 }
4132 goto ext_call_fail;
4133 }
4134 Py_DECREF(kwdict);
4135 kwdict = d;
4136 }
4137 }
4138 if (flags & CALL_FLAG_VAR) {
4139 stararg = EXT_POP(*pp_stack);
4140 if (!PyTuple_Check(stararg)) {
4141 PyObject *t = NULL;
4142 t = PySequence_Tuple(stararg);
4143 if (t == NULL) {
4144 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4145 PyErr_Format(PyExc_TypeError,
4146 "%.200s%.200s argument after * "
4147 "must be a sequence, not %200s",
4148 PyEval_GetFuncName(func),
4149 PyEval_GetFuncDesc(func),
4150 stararg->ob_type->tp_name);
4151 }
4152 goto ext_call_fail;
4153 }
4154 Py_DECREF(stararg);
4155 stararg = t;
4156 }
4157 nstar = PyTuple_GET_SIZE(stararg);
4158 }
4159 if (nk > 0) {
4160 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4161 if (kwdict == NULL)
4162 goto ext_call_fail;
4163 }
4164 callargs = update_star_args(na, nstar, stararg, pp_stack);
4165 if (callargs == NULL)
4166 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004167#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004168 /* At this point, we have to look at the type of func to
4169 update the call stats properly. Do it here so as to avoid
4170 exposing the call stats machinery outside ceval.c
4171 */
4172 if (PyFunction_Check(func))
4173 PCALL(PCALL_FUNCTION);
4174 else if (PyMethod_Check(func))
4175 PCALL(PCALL_METHOD);
4176 else if (PyType_Check(func))
4177 PCALL(PCALL_TYPE);
4178 else if (PyCFunction_Check(func))
4179 PCALL(PCALL_CFUNCTION);
4180 else
4181 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004182#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 if (PyCFunction_Check(func)) {
4184 PyThreadState *tstate = PyThreadState_GET();
4185 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4186 }
4187 else
4188 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004189ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 Py_XDECREF(callargs);
4191 Py_XDECREF(kwdict);
4192 Py_XDECREF(stararg);
4193 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004194}
4195
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004196/* Extract a slice index from a PyInt or PyLong or an object with the
4197 nb_index slot defined, and store in *pi.
4198 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4199 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 +00004200 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004201*/
Tim Petersb5196382001-12-16 19:44:20 +00004202/* Note: If v is NULL, return success without storing into *pi. This
4203 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4204 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004205*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004206int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004207_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004208{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004209 if (v != NULL) {
4210 Py_ssize_t x;
4211 if (PyIndex_Check(v)) {
4212 x = PyNumber_AsSsize_t(v, NULL);
4213 if (x == -1 && PyErr_Occurred())
4214 return 0;
4215 }
4216 else {
4217 PyErr_SetString(PyExc_TypeError,
4218 "slice indices must be integers or "
4219 "None or have an __index__ method");
4220 return 0;
4221 }
4222 *pi = x;
4223 }
4224 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004225}
4226
Guido van Rossum486364b2007-06-30 05:01:58 +00004227#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004229
Guido van Rossumb209a111997-04-29 18:18:01 +00004230static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004231cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004233 int res = 0;
4234 switch (op) {
4235 case PyCmp_IS:
4236 res = (v == w);
4237 break;
4238 case PyCmp_IS_NOT:
4239 res = (v != w);
4240 break;
4241 case PyCmp_IN:
4242 res = PySequence_Contains(w, v);
4243 if (res < 0)
4244 return NULL;
4245 break;
4246 case PyCmp_NOT_IN:
4247 res = PySequence_Contains(w, v);
4248 if (res < 0)
4249 return NULL;
4250 res = !res;
4251 break;
4252 case PyCmp_EXC_MATCH:
4253 if (PyTuple_Check(w)) {
4254 Py_ssize_t i, length;
4255 length = PyTuple_Size(w);
4256 for (i = 0; i < length; i += 1) {
4257 PyObject *exc = PyTuple_GET_ITEM(w, i);
4258 if (!PyExceptionClass_Check(exc)) {
4259 PyErr_SetString(PyExc_TypeError,
4260 CANNOT_CATCH_MSG);
4261 return NULL;
4262 }
4263 }
4264 }
4265 else {
4266 if (!PyExceptionClass_Check(w)) {
4267 PyErr_SetString(PyExc_TypeError,
4268 CANNOT_CATCH_MSG);
4269 return NULL;
4270 }
4271 }
4272 res = PyErr_GivenExceptionMatches(v, w);
4273 break;
4274 default:
4275 return PyObject_RichCompare(v, w, op);
4276 }
4277 v = res ? Py_True : Py_False;
4278 Py_INCREF(v);
4279 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004280}
4281
Thomas Wouters52152252000-08-17 22:55:00 +00004282static PyObject *
4283import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004284{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004287 x = PyObject_GetAttr(v, name);
4288 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4289 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4290 }
4291 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004292}
Guido van Rossumac7be682001-01-17 15:42:30 +00004293
Thomas Wouters52152252000-08-17 22:55:00 +00004294static int
4295import_all_from(PyObject *locals, PyObject *v)
4296{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004297 PyObject *all = PyObject_GetAttrString(v, "__all__");
4298 PyObject *dict, *name, *value;
4299 int skip_leading_underscores = 0;
4300 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004302 if (all == NULL) {
4303 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4304 return -1; /* Unexpected error */
4305 PyErr_Clear();
4306 dict = PyObject_GetAttrString(v, "__dict__");
4307 if (dict == NULL) {
4308 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4309 return -1;
4310 PyErr_SetString(PyExc_ImportError,
4311 "from-import-* object has no __dict__ and no __all__");
4312 return -1;
4313 }
4314 all = PyMapping_Keys(dict);
4315 Py_DECREF(dict);
4316 if (all == NULL)
4317 return -1;
4318 skip_leading_underscores = 1;
4319 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004320
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 for (pos = 0, err = 0; ; pos++) {
4322 name = PySequence_GetItem(all, pos);
4323 if (name == NULL) {
4324 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4325 err = -1;
4326 else
4327 PyErr_Clear();
4328 break;
4329 }
4330 if (skip_leading_underscores &&
4331 PyUnicode_Check(name) &&
4332 PyUnicode_AS_UNICODE(name)[0] == '_')
4333 {
4334 Py_DECREF(name);
4335 continue;
4336 }
4337 value = PyObject_GetAttr(v, name);
4338 if (value == NULL)
4339 err = -1;
4340 else if (PyDict_CheckExact(locals))
4341 err = PyDict_SetItem(locals, name, value);
4342 else
4343 err = PyObject_SetItem(locals, name, value);
4344 Py_DECREF(name);
4345 Py_XDECREF(value);
4346 if (err != 0)
4347 break;
4348 }
4349 Py_DECREF(all);
4350 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004351}
4352
Guido van Rossumac7be682001-01-17 15:42:30 +00004353static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004354format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004356 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004357
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 if (!obj)
4359 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004360
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004361 obj_str = _PyUnicode_AsString(obj);
4362 if (!obj_str)
4363 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004365 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004366}
Guido van Rossum950361c1997-01-24 13:49:28 +00004367
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004368static void
4369format_exc_unbound(PyCodeObject *co, int oparg)
4370{
4371 PyObject *name;
4372 /* Don't stomp existing exception */
4373 if (PyErr_Occurred())
4374 return;
4375 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4376 name = PyTuple_GET_ITEM(co->co_cellvars,
4377 oparg);
4378 format_exc_check_arg(
4379 PyExc_UnboundLocalError,
4380 UNBOUNDLOCAL_ERROR_MSG,
4381 name);
4382 } else {
4383 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4384 PyTuple_GET_SIZE(co->co_cellvars));
4385 format_exc_check_arg(PyExc_NameError,
4386 UNBOUNDFREE_ERROR_MSG, name);
4387 }
4388}
4389
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004390static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004391unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004392 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004393{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004394 /* This function implements 'variable += expr' when both arguments
4395 are (Unicode) strings. */
4396 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4397 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4398 Py_ssize_t new_len = v_len + w_len;
4399 if (new_len < 0) {
4400 PyErr_SetString(PyExc_OverflowError,
4401 "strings are too large to concat");
4402 return NULL;
4403 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004404
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004405 if (Py_REFCNT(v) == 2) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 /* In the common case, there are 2 references to the value
4407 * stored in 'variable' when the += is performed: one on the
4408 * value stack (in 'v') and one still stored in the
4409 * 'variable'. We try to delete the variable now to reduce
4410 * the refcnt to 1.
4411 */
4412 switch (*next_instr) {
4413 case STORE_FAST:
4414 {
4415 int oparg = PEEKARG();
4416 PyObject **fastlocals = f->f_localsplus;
4417 if (GETLOCAL(oparg) == v)
4418 SETLOCAL(oparg, NULL);
4419 break;
4420 }
4421 case STORE_DEREF:
4422 {
4423 PyObject **freevars = (f->f_localsplus +
4424 f->f_code->co_nlocals);
4425 PyObject *c = freevars[PEEKARG()];
4426 if (PyCell_GET(c) == v)
4427 PyCell_Set(c, NULL);
4428 break;
4429 }
4430 case STORE_NAME:
4431 {
4432 PyObject *names = f->f_code->co_names;
4433 PyObject *name = GETITEM(names, PEEKARG());
4434 PyObject *locals = f->f_locals;
4435 if (PyDict_CheckExact(locals) &&
4436 PyDict_GetItem(locals, name) == v) {
4437 if (PyDict_DelItem(locals, name) != 0) {
4438 PyErr_Clear();
4439 }
4440 }
4441 break;
4442 }
4443 }
4444 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004445
Benjamin Petersone208b7c2010-09-10 23:53:14 +00004446 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 /* Now we own the last reference to 'v', so we can resize it
4448 * in-place.
4449 */
4450 if (PyUnicode_Resize(&v, new_len) != 0) {
4451 /* XXX if PyUnicode_Resize() fails, 'v' has been
4452 * deallocated so it cannot be put back into
4453 * 'variable'. The MemoryError is raised when there
4454 * is no value in 'variable', which might (very
4455 * remotely) be a cause of incompatibilities.
4456 */
4457 return NULL;
4458 }
4459 /* copy 'w' into the newly allocated area of 'v' */
4460 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4461 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4462 return v;
4463 }
4464 else {
4465 /* When in-place resizing is not an option. */
4466 w = PyUnicode_Concat(v, w);
4467 Py_DECREF(v);
4468 return w;
4469 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004470}
4471
Guido van Rossum950361c1997-01-24 13:49:28 +00004472#ifdef DYNAMIC_EXECUTION_PROFILE
4473
Skip Montanarof118cb12001-10-15 20:51:38 +00004474static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004475getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004476{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004477 int i;
4478 PyObject *l = PyList_New(256);
4479 if (l == NULL) return NULL;
4480 for (i = 0; i < 256; i++) {
4481 PyObject *x = PyLong_FromLong(a[i]);
4482 if (x == NULL) {
4483 Py_DECREF(l);
4484 return NULL;
4485 }
4486 PyList_SetItem(l, i, x);
4487 }
4488 for (i = 0; i < 256; i++)
4489 a[i] = 0;
4490 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004491}
4492
4493PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004494_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004495{
4496#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004497 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004498#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 int i;
4500 PyObject *l = PyList_New(257);
4501 if (l == NULL) return NULL;
4502 for (i = 0; i < 257; i++) {
4503 PyObject *x = getarray(dxpairs[i]);
4504 if (x == NULL) {
4505 Py_DECREF(l);
4506 return NULL;
4507 }
4508 PyList_SetItem(l, i, x);
4509 }
4510 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004511#endif
4512}
4513
4514#endif