blob: f0d278c5648fc47349b47b7a7367a10c23b5f61b [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 Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsona25b1312009-10-31 10:18:44 +000054#elif defined(__i386__)
55
56/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000057
Michael W. Hudson75eabd22005-01-18 15:56:11 +000058#define READ_TIMESTAMP(val) \
59 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000060
Mark Dickinsona25b1312009-10-31 10:18:44 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahb7e10102010-06-23 18:42:39 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahb7e10102010-06-23 18:42:39 +0000132 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000133
Thomas Wouters477c8d52006-05-27 19:21:47 +0000134static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000136static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000137static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +0000138static void format_exc_unbound(PyCodeObject *co, int oparg);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000141static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000142
Paul Prescode68140d2000-08-30 20:25:01 +0000143#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000145#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000147#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000149#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 "free variable '%.200s' referenced before assignment" \
151 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000152
Guido van Rossum950361c1997-01-24 13:49:28 +0000153/* Dynamic execution profile */
154#ifdef DYNAMIC_EXECUTION_PROFILE
155#ifdef DXPAIRS
156static long dxpairs[257][256];
157#define dxp dxpairs[256]
158#else
159static long dxp[256];
160#endif
161#endif
162
Jeremy Hylton985eba52003-02-05 23:13:00 +0000163/* Function call profile */
164#ifdef CALL_PROFILE
165#define PCALL_NUM 11
166static int pcall[PCALL_NUM];
167
168#define PCALL_ALL 0
169#define PCALL_FUNCTION 1
170#define PCALL_FAST_FUNCTION 2
171#define PCALL_FASTER_FUNCTION 3
172#define PCALL_METHOD 4
173#define PCALL_BOUND_METHOD 5
174#define PCALL_CFUNCTION 6
175#define PCALL_TYPE 7
176#define PCALL_GENERATOR 8
177#define PCALL_OTHER 9
178#define PCALL_POP 10
179
180/* Notes about the statistics
181
182 PCALL_FAST stats
183
184 FAST_FUNCTION means no argument tuple needs to be created.
185 FASTER_FUNCTION means that the fast-path frame setup code is used.
186
187 If there is a method call where the call can be optimized by changing
188 the argument tuple and calling the function directly, it gets recorded
189 twice.
190
191 As a result, the relationship among the statistics appears to be
192 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
193 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
194 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
195 PCALL_METHOD > PCALL_BOUND_METHOD
196*/
197
198#define PCALL(POS) pcall[POS]++
199
200PyObject *
201PyEval_GetCallStats(PyObject *self)
202{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 return Py_BuildValue("iiiiiiiiiii",
204 pcall[0], pcall[1], pcall[2], pcall[3],
205 pcall[4], pcall[5], pcall[6], pcall[7],
206 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000207}
208#else
209#define PCALL(O)
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 Py_INCREF(Py_None);
215 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#endif
218
Tim Peters5ca576e2001-06-18 22:08:13 +0000219
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000220/* This can set eval_breaker to 0 even though gil_drop_request became
221 1. We believe this is all right because the eval loop will release
222 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000223#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 _Py_atomic_store_relaxed( \
225 &eval_breaker, \
226 _Py_atomic_load_relaxed(&gil_drop_request) | \
227 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
228 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000229
230#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 do { \
232 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
233 _Py_atomic_store_relaxed(&eval_breaker, 1); \
234 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000235
236#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 do { \
238 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
239 COMPUTE_EVAL_BREAKER(); \
240 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000242/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000243#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 do { \
245 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
246 _Py_atomic_store_relaxed(&eval_breaker, 1); \
247 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248
249#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 do { \
251 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
252 COMPUTE_EVAL_BREAKER(); \
253 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000254
255#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000256 do { \
257 pending_async_exc = 1; \
258 _Py_atomic_store_relaxed(&eval_breaker, 1); \
259 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000260
261#define UNSIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000263
264
Guido van Rossume59214e1994-08-30 08:01:59 +0000265#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000266
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000267#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000268#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000269#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000270#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000271
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000272static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000273static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000274/* This single variable consolidates all requests to break out of the fast path
275 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000276static _Py_atomic_int eval_breaker = {0};
277/* Request for dropping the GIL */
278static _Py_atomic_int gil_drop_request = {0};
279/* Request for running pending calls. */
280static _Py_atomic_int pendingcalls_to_do = {0};
281/* Request for looking at the `async_exc` field of the current thread state.
282 Guarded by the GIL. */
283static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000284
285#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000286
Tim Peters7f468f22004-10-11 02:40:51 +0000287int
288PyEval_ThreadsInitialized(void)
289{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000291}
292
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000293void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000294PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 if (gil_created())
297 return;
298 create_gil();
299 take_gil(PyThreadState_GET());
300 main_thread = PyThread_get_thread_ident();
301 if (!pending_lock)
302 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000303}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000304
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000305void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000306PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000307{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 PyThreadState *tstate = PyThreadState_GET();
309 if (tstate == NULL)
310 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
311 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000312}
313
314void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000315PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000316{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000317 /* This function must succeed when the current thread state is NULL.
318 We therefore avoid PyThreadState_GET() which dumps a fatal error
319 in debug mode.
320 */
321 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
322 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000323}
324
325void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000327{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 if (tstate == NULL)
329 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
330 /* Check someone has called PyEval_InitThreads() to create the lock */
331 assert(gil_created());
332 take_gil(tstate);
333 if (PyThreadState_Swap(tstate) != NULL)
334 Py_FatalError(
335 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000336}
337
338void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000340{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 if (tstate == NULL)
342 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
343 if (PyThreadState_Swap(NULL) != tstate)
344 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
345 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000346}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000347
348/* This function is called from PyOS_AfterFork to ensure that newly
349 created child processes don't hold locks referring to threads which
350 are not running in the child process. (This could also be done using
351 pthread_atfork mechanism, at least for the pthreads implementation.) */
352
353void
354PyEval_ReInitThreads(void)
355{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 PyObject *threading, *result;
357 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 if (!gil_created())
360 return;
361 /*XXX Can't use PyThread_free_lock here because it does too
362 much error-checking. Doing this cleanly would require
363 adding a new function to each thread_*.h. Instead, just
364 create a new lock and waste a little bit of memory */
365 recreate_gil();
366 pending_lock = PyThread_allocate_lock();
367 take_gil(tstate);
368 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 /* Update the threading module with the new state.
371 */
372 tstate = PyThreadState_GET();
373 threading = PyMapping_GetItemString(tstate->interp->modules,
374 "threading");
375 if (threading == NULL) {
376 /* threading not imported */
377 PyErr_Clear();
378 return;
379 }
380 result = PyObject_CallMethod(threading, "_after_fork", NULL);
381 if (result == NULL)
382 PyErr_WriteUnraisable(threading);
383 else
384 Py_DECREF(result);
385 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000386}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000387
388#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000389static _Py_atomic_int eval_breaker = {0};
390static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000391static int pending_async_exc = 0;
392#endif /* WITH_THREAD */
393
394/* This function is used to signal that async exceptions are waiting to be
395 raised, therefore it is also useful in non-threaded builds. */
396
397void
398_PyEval_SignalAsyncExc(void)
399{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000400 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000401}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000402
Guido van Rossumff4949e1992-08-05 19:58:53 +0000403/* Functions save_thread and restore_thread are always defined so
404 dynamically loaded modules needn't be compiled separately for use
405 with and without threads: */
406
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000407PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000408PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000409{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 PyThreadState *tstate = PyThreadState_Swap(NULL);
411 if (tstate == NULL)
412 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000413#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 if (gil_created())
415 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000416#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000418}
419
420void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000421PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 if (tstate == NULL)
424 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000425#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 if (gil_created()) {
427 int err = errno;
428 take_gil(tstate);
429 errno = err;
430 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000431#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000433}
434
435
Guido van Rossuma9672091994-09-14 13:31:22 +0000436/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
437 signal handlers or Mac I/O completion routines) can schedule calls
438 to a function to be called synchronously.
439 The synchronous function is called with one void* argument.
440 It should return 0 for success or -1 for failure -- failure should
441 be accompanied by an exception.
442
443 If registry succeeds, the registry function returns 0; if it fails
444 (e.g. due to too many pending calls) it returns -1 (without setting
445 an exception condition).
446
447 Note that because registry may occur from within signal handlers,
448 or other asynchronous events, calling malloc() is unsafe!
449
450#ifdef WITH_THREAD
451 Any thread can schedule pending calls, but only the main thread
452 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000453 There is no facility to schedule calls to a particular thread, but
454 that should be easy to change, should that ever be required. In
455 that case, the static variables here should go into the python
456 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000457#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000458*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000459
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000460#ifdef WITH_THREAD
461
462/* The WITH_THREAD implementation is thread-safe. It allows
463 scheduling to be made from any thread, and even from an executing
464 callback.
465 */
466
467#define NPENDINGCALLS 32
468static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 int (*func)(void *);
470 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000471} pendingcalls[NPENDINGCALLS];
472static int pendingfirst = 0;
473static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000474static char pendingbusy = 0;
475
476int
477Py_AddPendingCall(int (*func)(void *), void *arg)
478{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 int i, j, result=0;
480 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 /* try a few times for the lock. Since this mechanism is used
483 * for signal handling (on the main thread), there is a (slim)
484 * chance that a signal is delivered on the same thread while we
485 * hold the lock during the Py_MakePendingCalls() function.
486 * This avoids a deadlock in that case.
487 * Note that signals can be delivered on any thread. In particular,
488 * on Windows, a SIGINT is delivered on a system-created worker
489 * thread.
490 * We also check for lock being NULL, in the unlikely case that
491 * this function is called before any bytecode evaluation takes place.
492 */
493 if (lock != NULL) {
494 for (i = 0; i<100; i++) {
495 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
496 break;
497 }
498 if (i == 100)
499 return -1;
500 }
501
502 i = pendinglast;
503 j = (i + 1) % NPENDINGCALLS;
504 if (j == pendingfirst) {
505 result = -1; /* Queue full */
506 } else {
507 pendingcalls[i].func = func;
508 pendingcalls[i].arg = arg;
509 pendinglast = j;
510 }
511 /* signal main loop */
512 SIGNAL_PENDING_CALLS();
513 if (lock != NULL)
514 PyThread_release_lock(lock);
515 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000516}
517
518int
519Py_MakePendingCalls(void)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 int i;
522 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (!pending_lock) {
525 /* initial allocation of the lock */
526 pending_lock = PyThread_allocate_lock();
527 if (pending_lock == NULL)
528 return -1;
529 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000530
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000531 /* only service pending calls on main thread */
532 if (main_thread && PyThread_get_thread_ident() != main_thread)
533 return 0;
534 /* don't perform recursive pending calls */
535 if (pendingbusy)
536 return 0;
537 pendingbusy = 1;
538 /* perform a bounded number of calls, in case of recursion */
539 for (i=0; i<NPENDINGCALLS; i++) {
540 int j;
541 int (*func)(void *);
542 void *arg = NULL;
543
544 /* pop one item off the queue while holding the lock */
545 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
546 j = pendingfirst;
547 if (j == pendinglast) {
548 func = NULL; /* Queue empty */
549 } else {
550 func = pendingcalls[j].func;
551 arg = pendingcalls[j].arg;
552 pendingfirst = (j + 1) % NPENDINGCALLS;
553 }
554 if (pendingfirst != pendinglast)
555 SIGNAL_PENDING_CALLS();
556 else
557 UNSIGNAL_PENDING_CALLS();
558 PyThread_release_lock(pending_lock);
559 /* having released the lock, perform the callback */
560 if (func == NULL)
561 break;
562 r = func(arg);
563 if (r)
564 break;
565 }
566 pendingbusy = 0;
567 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000568}
569
570#else /* if ! defined WITH_THREAD */
571
572/*
573 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
574 This code is used for signal handling in python that isn't built
575 with WITH_THREAD.
576 Don't use this implementation when Py_AddPendingCalls() can happen
577 on a different thread!
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578
Guido van Rossuma9672091994-09-14 13:31:22 +0000579 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000580 (1) nested asynchronous calls to Py_AddPendingCall()
581 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000583 (1) is very unlikely because typically signal delivery
584 is blocked during signal handling. So it should be impossible.
585 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000586 The current code is safe against (2), but not against (1).
587 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000588 thread is present, interrupted by signals, and that the critical
589 section is protected with the "busy" variable. On Windows, which
590 delivers SIGINT on a system thread, this does not hold and therefore
591 Windows really shouldn't use this version.
592 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000593*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000594
Guido van Rossuma9672091994-09-14 13:31:22 +0000595#define NPENDINGCALLS 32
596static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 int (*func)(void *);
598 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000599} pendingcalls[NPENDINGCALLS];
600static volatile int pendingfirst = 0;
601static volatile int pendinglast = 0;
Benjamin Peterson08ec84c2010-05-30 14:49:32 +0000602static _Py_atomic_int pendingcalls_to_do = {0};
Guido van Rossuma9672091994-09-14 13:31:22 +0000603
604int
Thomas Wouters334fb892000-07-25 12:56:38 +0000605Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000607 static volatile int busy = 0;
608 int i, j;
609 /* XXX Begin critical section */
610 if (busy)
611 return -1;
612 busy = 1;
613 i = pendinglast;
614 j = (i + 1) % NPENDINGCALLS;
615 if (j == pendingfirst) {
616 busy = 0;
617 return -1; /* Queue full */
618 }
619 pendingcalls[i].func = func;
620 pendingcalls[i].arg = arg;
621 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 SIGNAL_PENDING_CALLS();
624 busy = 0;
625 /* XXX End critical section */
626 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000627}
628
Guido van Rossum180d7b41994-09-29 09:45:57 +0000629int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000630Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000631{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 static int busy = 0;
633 if (busy)
634 return 0;
635 busy = 1;
636 UNSIGNAL_PENDING_CALLS();
637 for (;;) {
638 int i;
639 int (*func)(void *);
640 void *arg;
641 i = pendingfirst;
642 if (i == pendinglast)
643 break; /* Queue empty */
644 func = pendingcalls[i].func;
645 arg = pendingcalls[i].arg;
646 pendingfirst = (i + 1) % NPENDINGCALLS;
647 if (func(arg) < 0) {
648 busy = 0;
649 SIGNAL_PENDING_CALLS(); /* We're not done yet */
650 return -1;
651 }
652 }
653 busy = 0;
654 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000655}
656
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000657#endif /* WITH_THREAD */
658
Guido van Rossuma9672091994-09-14 13:31:22 +0000659
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000660/* The interpreter's recursion limit */
661
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000662#ifndef Py_DEFAULT_RECURSION_LIMIT
663#define Py_DEFAULT_RECURSION_LIMIT 1000
664#endif
665static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
666int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000667
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000668int
669Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000670{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000672}
673
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000674void
675Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000677 recursion_limit = new_limit;
678 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000679}
680
Armin Rigo2b3eb402003-10-28 12:05:48 +0000681/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
682 if the recursion_depth reaches _Py_CheckRecursionLimit.
683 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
684 to guarantee that _Py_CheckRecursiveCall() is regularly called.
685 Without USE_STACKCHECK, there is no need for this. */
686int
687_Py_CheckRecursiveCall(char *where)
688{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000690
691#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (PyOS_CheckStack()) {
693 --tstate->recursion_depth;
694 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
695 return -1;
696 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000697#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 _Py_CheckRecursionLimit = recursion_limit;
699 if (tstate->recursion_critical)
700 /* Somebody asked that we don't check for recursion. */
701 return 0;
702 if (tstate->overflowed) {
703 if (tstate->recursion_depth > recursion_limit + 50) {
704 /* Overflowing while handling an overflow. Give up. */
705 Py_FatalError("Cannot recover from stack overflow.");
706 }
707 return 0;
708 }
709 if (tstate->recursion_depth > recursion_limit) {
710 --tstate->recursion_depth;
711 tstate->overflowed = 1;
712 PyErr_Format(PyExc_RuntimeError,
713 "maximum recursion depth exceeded%s",
714 where);
715 return -1;
716 }
717 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000718}
719
Guido van Rossum374a9221991-04-04 10:40:29 +0000720/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000721enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000722 WHY_NOT = 0x0001, /* No error */
723 WHY_EXCEPTION = 0x0002, /* Exception occurred */
724 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
725 WHY_RETURN = 0x0008, /* 'return' statement */
726 WHY_BREAK = 0x0010, /* 'break' statement */
727 WHY_CONTINUE = 0x0020, /* 'continue' statement */
728 WHY_YIELD = 0x0040, /* 'yield' operator */
729 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000730};
Guido van Rossum374a9221991-04-04 10:40:29 +0000731
Collin Winter828f04a2007-08-31 00:04:24 +0000732static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000733static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000734
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000735/* Records whether tracing is on for any thread. Counts the number of
736 threads for which tstate->c_tracefunc is non-NULL, so if the value
737 is 0, we know we don't have to check this thread's c_tracefunc.
738 This speeds up the if statement in PyEval_EvalFrameEx() after
739 fast_next_opcode*/
740static int _Py_TracingPossible = 0;
741
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000742
Guido van Rossum374a9221991-04-04 10:40:29 +0000743
Guido van Rossumb209a111997-04-29 18:18:01 +0000744PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000745PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000747 return PyEval_EvalCodeEx(co,
748 globals, locals,
749 (PyObject **)NULL, 0,
750 (PyObject **)NULL, 0,
751 (PyObject **)NULL, 0,
752 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000753}
754
755
756/* Interpreter main loop */
757
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000758PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000759PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 /* This is for backward compatibility with extension modules that
761 used this API; core interpreter code should call
762 PyEval_EvalFrameEx() */
763 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000764}
765
766PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000767PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000768{
Guido van Rossum950361c1997-01-24 13:49:28 +0000769#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000771#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 register PyObject **stack_pointer; /* Next free slot in value stack */
773 register unsigned char *next_instr;
774 register int opcode; /* Current opcode */
775 register int oparg; /* Current opcode argument, if any */
776 register enum why_code why; /* Reason for block stack unwind */
777 register int err; /* Error status -- nonzero if error */
778 register PyObject *x; /* Result object -- NULL if error */
779 register PyObject *v; /* Temporary objects popped off stack */
780 register PyObject *w;
781 register PyObject *u;
782 register PyObject *t;
783 register PyObject **fastlocals, **freevars;
784 PyObject *retval = NULL; /* Return value */
785 PyThreadState *tstate = PyThreadState_GET();
786 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 is true when the line being executed has changed. The
793 initial values are such as to make this false the first
794 time it is tested. */
795 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000796
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000797 unsigned char *first_instr;
798 PyObject *names;
799 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000800#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* Make it easier to find out where we are with a debugger */
802 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000803#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000804
Antoine Pitroub52ec782009-01-25 16:34:23 +0000805/* Computed GOTOs, or
806 the-optimization-commonly-but-improperly-known-as-"threaded code"
807 using gcc's labels-as-values extension
808 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
809
810 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000812 combined with a lookup table of jump addresses. However, since the
813 indirect jump instruction is shared by all opcodes, the CPU will have a
814 hard time making the right prediction for where to jump next (actually,
815 it will be always wrong except in the uncommon case of a sequence of
816 several identical opcodes).
817
818 "Threaded code" in contrast, uses an explicit jump table and an explicit
819 indirect jump instruction at the end of each opcode. Since the jump
820 instruction is at a different address for each opcode, the CPU will make a
821 separate prediction for each of these instructions, which is equivalent to
822 predicting the second opcode of each opcode pair. These predictions have
823 a much better chance to turn out valid, especially in small bytecode loops.
824
825 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000827 and potentially many more instructions (depending on the pipeline width).
828 A correctly predicted branch, however, is nearly free.
829
830 At the time of this writing, the "threaded code" version is up to 15-20%
831 faster than the normal "switch" version, depending on the compiler and the
832 CPU architecture.
833
834 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
835 because it would render the measurements invalid.
836
837
838 NOTE: care must be taken that the compiler doesn't try to "optimize" the
839 indirect jumps by sharing them between all opcodes. Such optimizations
840 can be disabled on gcc by using the -fno-gcse flag (or possibly
841 -fno-crossjumping).
842*/
843
Antoine Pitrou042b1282010-08-13 21:15:58 +0000844#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000845#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000846#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847#endif
848
Antoine Pitrou042b1282010-08-13 21:15:58 +0000849#ifdef HAVE_COMPUTED_GOTOS
850 #ifndef USE_COMPUTED_GOTOS
851 #define USE_COMPUTED_GOTOS 1
852 #endif
853#else
854 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
855 #error "Computed gotos are not supported on this compiler."
856 #endif
857 #undef USE_COMPUTED_GOTOS
858 #define USE_COMPUTED_GOTOS 0
859#endif
860
861#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000862/* Import the static jump table */
863#include "opcode_targets.h"
864
865/* This macro is used when several opcodes defer to the same implementation
866 (e.g. SETUP_LOOP, SETUP_FINALLY) */
867#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 TARGET_##op: \
869 opcode = op; \
870 if (HAS_ARG(op)) \
871 oparg = NEXTARG(); \
872 case op: \
873 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000874
875#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 TARGET_##op: \
877 opcode = op; \
878 if (HAS_ARG(op)) \
879 oparg = NEXTARG(); \
880 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000881
882
883#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 { \
885 if (!_Py_atomic_load_relaxed(&eval_breaker)) { \
886 FAST_DISPATCH(); \
887 } \
888 continue; \
889 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000890
891#ifdef LLTRACE
892#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 { \
894 if (!lltrace && !_Py_TracingPossible) { \
895 f->f_lasti = INSTR_OFFSET(); \
896 goto *opcode_targets[*next_instr++]; \
897 } \
898 goto fast_next_opcode; \
899 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000900#else
901#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 { \
903 if (!_Py_TracingPossible) { \
904 f->f_lasti = INSTR_OFFSET(); \
905 goto *opcode_targets[*next_instr++]; \
906 } \
907 goto fast_next_opcode; \
908 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000909#endif
910
911#else
912#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000914#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 /* silence compiler warnings about `impl` unused */ \
916 if (0) goto impl; \
917 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000918#define DISPATCH() continue
919#define FAST_DISPATCH() goto fast_next_opcode
920#endif
921
922
Neal Norwitza81d2202002-07-14 00:27:26 +0000923/* Tuple access macros */
924
925#ifndef Py_DEBUG
926#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
927#else
928#define GETITEM(v, i) PyTuple_GetItem((v), (i))
929#endif
930
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000931#ifdef WITH_TSC
932/* Use Pentium timestamp counter to mark certain events:
933 inst0 -- beginning of switch statement for opcode dispatch
934 inst1 -- end of switch statement (may be skipped)
935 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000936 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000937 (may be skipped)
938 intr1 -- beginning of long interruption
939 intr2 -- end of long interruption
940
941 Many opcodes call out to helper C functions. In some cases, the
942 time in those functions should be counted towards the time for the
943 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
944 calls another Python function; there's no point in charge all the
945 bytecode executed by the called function to the caller.
946
947 It's hard to make a useful judgement statically. In the presence
948 of operator overloading, it's impossible to tell if a call will
949 execute new Python code or not.
950
951 It's a case-by-case judgement. I'll use intr1 for the following
952 cases:
953
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000954 IMPORT_STAR
955 IMPORT_FROM
956 CALL_FUNCTION (and friends)
957
958 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
960 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 READ_TIMESTAMP(inst0);
963 READ_TIMESTAMP(inst1);
964 READ_TIMESTAMP(loop0);
965 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000966
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 /* shut up the compiler */
968 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000969#endif
970
Guido van Rossum374a9221991-04-04 10:40:29 +0000971/* Code access macros */
972
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000973#define INSTR_OFFSET() ((int)(next_instr - first_instr))
974#define NEXTOP() (*next_instr++)
975#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
976#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
977#define JUMPTO(x) (next_instr = first_instr + (x))
978#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000979
Raymond Hettingerf606f872003-03-16 03:11:04 +0000980/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 Some opcodes tend to come in pairs thus making it possible to
982 predict the second code when the first is run. For example,
983 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
984 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 Verifying the prediction costs a single high-speed test of a register
987 variable against a constant. If the pairing was good, then the
988 processor's own internal branch predication has a high likelihood of
989 success, resulting in a nearly zero-overhead transition to the
990 next opcode. A successful prediction saves a trip through the eval-loop
991 including its two unpredictable branches, the HAS_ARG test and the
992 switch-case. Combined with the processor's internal branch prediction,
993 a successful PREDICT has the effect of making the two opcodes run as if
994 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000995
Georg Brandl86b2fb92008-07-16 03:43:04 +0000996 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 predictions turned-on and interpret the results as if some opcodes
998 had been combined or turn-off predictions so that the opcode frequency
999 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001000
1001 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 the CPU to record separate branch prediction information for each
1003 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001004
Raymond Hettingerf606f872003-03-16 03:11:04 +00001005*/
1006
Antoine Pitrou042b1282010-08-13 21:15:58 +00001007#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008#define PREDICT(op) if (0) goto PRED_##op
1009#define PREDICTED(op) PRED_##op:
1010#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +00001011#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012#define PREDICT(op) if (*next_instr == op) goto PRED_##op
1013#define PREDICTED(op) PRED_##op: next_instr++
1014#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001015#endif
1016
Raymond Hettingerf606f872003-03-16 03:11:04 +00001017
Guido van Rossum374a9221991-04-04 10:40:29 +00001018/* Stack manipulation macros */
1019
Martin v. Löwis18e16552006-02-15 17:27:45 +00001020/* The stack can grow at most MAXINT deep, as co_nlocals and
1021 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001022#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1023#define EMPTY() (STACK_LEVEL() == 0)
1024#define TOP() (stack_pointer[-1])
1025#define SECOND() (stack_pointer[-2])
1026#define THIRD() (stack_pointer[-3])
1027#define FOURTH() (stack_pointer[-4])
1028#define PEEK(n) (stack_pointer[-(n)])
1029#define SET_TOP(v) (stack_pointer[-1] = (v))
1030#define SET_SECOND(v) (stack_pointer[-2] = (v))
1031#define SET_THIRD(v) (stack_pointer[-3] = (v))
1032#define SET_FOURTH(v) (stack_pointer[-4] = (v))
1033#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
1034#define BASIC_STACKADJ(n) (stack_pointer += n)
1035#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1036#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001037
Guido van Rossum96a42c81992-01-12 02:29:51 +00001038#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001040 lltrace && prtrace(TOP(), "push")); \
1041 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001043 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001045 lltrace && prtrace(TOP(), "stackadj")); \
1046 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001047#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +00001048 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1049 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001050#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001051#define PUSH(v) BASIC_PUSH(v)
1052#define POP() BASIC_POP()
1053#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001054#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001055#endif
1056
Guido van Rossum681d79a1995-07-18 14:51:37 +00001057/* Local variable macros */
1058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001060
1061/* The SETLOCAL() macro must not DECREF the local variable in-place and
1062 then store the new value; it must copy the old value to a temporary
1063 value, then store the new value, and then DECREF the temporary value.
1064 This is because it is possible that during the DECREF the frame is
1065 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1066 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001068 GETLOCAL(i) = value; \
1069 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001070
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001071
1072#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 while (STACK_LEVEL() > (b)->b_level) { \
1074 PyObject *v = POP(); \
1075 Py_XDECREF(v); \
1076 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001077
1078#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 { \
1080 PyObject *type, *value, *traceback; \
1081 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1082 while (STACK_LEVEL() > (b)->b_level + 3) { \
1083 value = POP(); \
1084 Py_XDECREF(value); \
1085 } \
1086 type = tstate->exc_type; \
1087 value = tstate->exc_value; \
1088 traceback = tstate->exc_traceback; \
1089 tstate->exc_type = POP(); \
1090 tstate->exc_value = POP(); \
1091 tstate->exc_traceback = POP(); \
1092 Py_XDECREF(type); \
1093 Py_XDECREF(value); \
1094 Py_XDECREF(traceback); \
1095 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001096
1097#define SAVE_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 { \
1099 PyObject *type, *value, *traceback; \
1100 Py_XINCREF(tstate->exc_type); \
1101 Py_XINCREF(tstate->exc_value); \
1102 Py_XINCREF(tstate->exc_traceback); \
1103 type = f->f_exc_type; \
1104 value = f->f_exc_value; \
1105 traceback = f->f_exc_traceback; \
1106 f->f_exc_type = tstate->exc_type; \
1107 f->f_exc_value = tstate->exc_value; \
1108 f->f_exc_traceback = tstate->exc_traceback; \
1109 Py_XDECREF(type); \
1110 Py_XDECREF(value); \
1111 Py_XDECREF(traceback); \
1112 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001113
1114#define SWAP_EXC_STATE() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 { \
1116 PyObject *tmp; \
1117 tmp = tstate->exc_type; \
1118 tstate->exc_type = f->f_exc_type; \
1119 f->f_exc_type = tmp; \
1120 tmp = tstate->exc_value; \
1121 tstate->exc_value = f->f_exc_value; \
1122 f->f_exc_value = tmp; \
1123 tmp = tstate->exc_traceback; \
1124 tstate->exc_traceback = f->f_exc_traceback; \
1125 f->f_exc_traceback = tmp; \
1126 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001127
Guido van Rossuma027efa1997-05-05 20:56:21 +00001128/* Start of code */
1129
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 if (f == NULL)
1131 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001132
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 /* push frame */
1134 if (Py_EnterRecursiveCall(""))
1135 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001136
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001138
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 if (tstate->use_tracing) {
1140 if (tstate->c_tracefunc != NULL) {
1141 /* tstate->c_tracefunc, if defined, is a
1142 function that will be called on *every* entry
1143 to a code block. Its return value, if not
1144 None, is a function that will be called at
1145 the start of each executed line of code.
1146 (Actually, the function must return itself
1147 in order to continue tracing.) The trace
1148 functions are called with three arguments:
1149 a pointer to the current frame, a string
1150 indicating why the function is called, and
1151 an argument which depends on the situation.
1152 The global trace function is also called
1153 whenever an exception is detected. */
1154 if (call_trace_protected(tstate->c_tracefunc,
1155 tstate->c_traceobj,
1156 f, PyTrace_CALL, Py_None)) {
1157 /* Trace function raised an error */
1158 goto exit_eval_frame;
1159 }
1160 }
1161 if (tstate->c_profilefunc != NULL) {
1162 /* Similar for c_profilefunc, except it needn't
1163 return itself and isn't called for "line" events */
1164 if (call_trace_protected(tstate->c_profilefunc,
1165 tstate->c_profileobj,
1166 f, PyTrace_CALL, Py_None)) {
1167 /* Profile function raised an error */
1168 goto exit_eval_frame;
1169 }
1170 }
1171 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001172
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 co = f->f_code;
1174 names = co->co_names;
1175 consts = co->co_consts;
1176 fastlocals = f->f_localsplus;
1177 freevars = f->f_localsplus + co->co_nlocals;
1178 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1179 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001180
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 f->f_lasti now refers to the index of the last instruction
1182 executed. You might think this was obvious from the name, but
1183 this wasn't always true before 2.3! PyFrame_New now sets
1184 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1185 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1186 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001187
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001188 When the PREDICT() macros are enabled, some opcode pairs follow in
1189 direct succession without updating f->f_lasti. A successful
1190 prediction effectively links the two codes together as if they
1191 were a single new opcode; accordingly,f->f_lasti will point to
1192 the first code in the pair (for instance, GET_ITER followed by
1193 FOR_ITER is effectively a single opcode and f->f_lasti will point
1194 at to the beginning of the combined pair.)
1195 */
1196 next_instr = first_instr + f->f_lasti + 1;
1197 stack_pointer = f->f_stacktop;
1198 assert(stack_pointer != NULL);
1199 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001200
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 if (co->co_flags & CO_GENERATOR && !throwflag) {
1202 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1203 /* We were in an except handler when we left,
1204 restore the exception state which was put aside
1205 (see YIELD_VALUE). */
1206 SWAP_EXC_STATE();
1207 }
1208 else {
1209 SAVE_EXC_STATE();
1210 }
1211 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001212
Tim Peters5ca576e2001-06-18 22:08:13 +00001213#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001215#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001216#if defined(Py_DEBUG) || defined(LLTRACE)
Victor Stinner4a3733d2010-08-17 00:39:57 +00001217 {
1218 PyObject *error_type, *error_value, *error_traceback;
1219 PyErr_Fetch(&error_type, &error_value, &error_traceback);
1220 filename = _PyUnicode_AsString(co->co_filename);
1221 PyErr_Restore(error_type, error_value, error_traceback);
1222 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001223#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001225 why = WHY_NOT;
1226 err = 0;
1227 x = Py_None; /* Not a reference, just anything non-NULL */
1228 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001230 if (throwflag) { /* support for generator.throw() */
1231 why = WHY_EXCEPTION;
1232 goto on_error;
1233 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001236#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 if (inst1 == 0) {
1238 /* Almost surely, the opcode executed a break
1239 or a continue, preventing inst1 from being set
1240 on the way out of the loop.
1241 */
1242 READ_TIMESTAMP(inst1);
1243 loop1 = inst1;
1244 }
1245 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1246 intr0, intr1);
1247 ticked = 0;
1248 inst1 = 0;
1249 intr0 = 0;
1250 intr1 = 0;
1251 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1254 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 /* Do periodic things. Doing this every time through
1257 the loop would add too much overhead, so we do it
1258 only every Nth instruction. We also do it if
1259 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1260 event needs attention (e.g. a signal handler or
1261 async I/O handler); see Py_AddPendingCall() and
1262 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 if (_Py_atomic_load_relaxed(&eval_breaker)) {
1265 if (*next_instr == SETUP_FINALLY) {
1266 /* Make the last opcode before
1267 a try: finally: block uninterruptable. */
1268 goto fast_next_opcode;
1269 }
1270 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001271#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001273#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001274 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
1275 if (Py_MakePendingCalls() < 0) {
1276 why = WHY_EXCEPTION;
1277 goto on_error;
1278 }
1279 }
1280 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001281#ifdef WITH_THREAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 /* Give another thread a chance */
1283 if (PyThreadState_Swap(NULL) != tstate)
1284 Py_FatalError("ceval: tstate mix-up");
1285 drop_gil(tstate);
1286
1287 /* Other threads may run now */
1288
1289 take_gil(tstate);
1290 if (PyThreadState_Swap(tstate) != NULL)
1291 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001292#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
1294 /* Check for asynchronous exceptions. */
1295 if (tstate->async_exc != NULL) {
1296 x = tstate->async_exc;
1297 tstate->async_exc = NULL;
1298 UNSIGNAL_ASYNC_EXC();
1299 PyErr_SetNone(x);
1300 Py_DECREF(x);
1301 why = WHY_EXCEPTION;
1302 goto on_error;
1303 }
1304 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001305
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 fast_next_opcode:
1307 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001308
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 if (_Py_TracingPossible &&
1312 tstate->c_tracefunc != NULL && !tstate->tracing) {
1313 /* see maybe_call_line_trace
1314 for expository comments */
1315 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 err = maybe_call_line_trace(tstate->c_tracefunc,
1318 tstate->c_traceobj,
1319 f, &instr_lb, &instr_ub,
1320 &instr_prev);
1321 /* Reload possibly changed frame fields */
1322 JUMPTO(f->f_lasti);
1323 if (f->f_stacktop != NULL) {
1324 stack_pointer = f->f_stacktop;
1325 f->f_stacktop = NULL;
1326 }
1327 if (err) {
1328 /* trace function raised an exception */
1329 goto on_error;
1330 }
1331 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 opcode = NEXTOP();
1336 oparg = 0; /* allows oparg to be stored in a register because
1337 it doesn't have to be remembered across a full loop */
1338 if (HAS_ARG(opcode))
1339 oparg = NEXTARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001340 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001341#ifdef DYNAMIC_EXECUTION_PROFILE
1342#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 dxpairs[lastopcode][opcode]++;
1344 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001345#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001347#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001348
Guido van Rossum96a42c81992-01-12 02:29:51 +00001349#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 if (lltrace) {
1353 if (HAS_ARG(opcode)) {
1354 printf("%d: %d, %d\n",
1355 f->f_lasti, opcode, oparg);
1356 }
1357 else {
1358 printf("%d: %d\n",
1359 f->f_lasti, opcode);
1360 }
1361 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001362#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 /* Main switch on opcode */
1365 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369 /* BEWARE!
1370 It is essential that any operation that fails sets either
1371 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1372 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 TARGET(NOP)
1377 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 TARGET(LOAD_FAST)
1380 x = GETLOCAL(oparg);
1381 if (x != NULL) {
1382 Py_INCREF(x);
1383 PUSH(x);
1384 FAST_DISPATCH();
1385 }
1386 format_exc_check_arg(PyExc_UnboundLocalError,
1387 UNBOUNDLOCAL_ERROR_MSG,
1388 PyTuple_GetItem(co->co_varnames, oparg));
1389 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 TARGET(LOAD_CONST)
1392 x = GETITEM(consts, oparg);
1393 Py_INCREF(x);
1394 PUSH(x);
1395 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001396
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 PREDICTED_WITH_ARG(STORE_FAST);
1398 TARGET(STORE_FAST)
1399 v = POP();
1400 SETLOCAL(oparg, v);
1401 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 TARGET(POP_TOP)
1404 v = POP();
1405 Py_DECREF(v);
1406 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001408 TARGET(ROT_TWO)
1409 v = TOP();
1410 w = SECOND();
1411 SET_TOP(w);
1412 SET_SECOND(v);
1413 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001414
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 TARGET(ROT_THREE)
1416 v = TOP();
1417 w = SECOND();
1418 x = THIRD();
1419 SET_TOP(w);
1420 SET_SECOND(x);
1421 SET_THIRD(v);
1422 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 TARGET(DUP_TOP)
1425 v = TOP();
1426 Py_INCREF(v);
1427 PUSH(v);
1428 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001429
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001430 TARGET(DUP_TOP_TWO)
1431 x = TOP();
1432 Py_INCREF(x);
1433 w = SECOND();
1434 Py_INCREF(w);
1435 STACKADJ(2);
1436 SET_TOP(x);
1437 SET_SECOND(w);
1438 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 TARGET(UNARY_POSITIVE)
1441 v = TOP();
1442 x = PyNumber_Positive(v);
1443 Py_DECREF(v);
1444 SET_TOP(x);
1445 if (x != NULL) DISPATCH();
1446 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001447
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 TARGET(UNARY_NEGATIVE)
1449 v = TOP();
1450 x = PyNumber_Negative(v);
1451 Py_DECREF(v);
1452 SET_TOP(x);
1453 if (x != NULL) DISPATCH();
1454 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 TARGET(UNARY_NOT)
1457 v = TOP();
1458 err = PyObject_IsTrue(v);
1459 Py_DECREF(v);
1460 if (err == 0) {
1461 Py_INCREF(Py_True);
1462 SET_TOP(Py_True);
1463 DISPATCH();
1464 }
1465 else if (err > 0) {
1466 Py_INCREF(Py_False);
1467 SET_TOP(Py_False);
1468 err = 0;
1469 DISPATCH();
1470 }
1471 STACKADJ(-1);
1472 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001474 TARGET(UNARY_INVERT)
1475 v = TOP();
1476 x = PyNumber_Invert(v);
1477 Py_DECREF(v);
1478 SET_TOP(x);
1479 if (x != NULL) DISPATCH();
1480 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001481
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 TARGET(BINARY_POWER)
1483 w = POP();
1484 v = TOP();
1485 x = PyNumber_Power(v, w, Py_None);
1486 Py_DECREF(v);
1487 Py_DECREF(w);
1488 SET_TOP(x);
1489 if (x != NULL) DISPATCH();
1490 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001491
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 TARGET(BINARY_MULTIPLY)
1493 w = POP();
1494 v = TOP();
1495 x = PyNumber_Multiply(v, w);
1496 Py_DECREF(v);
1497 Py_DECREF(w);
1498 SET_TOP(x);
1499 if (x != NULL) DISPATCH();
1500 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001502 TARGET(BINARY_TRUE_DIVIDE)
1503 w = POP();
1504 v = TOP();
1505 x = PyNumber_TrueDivide(v, w);
1506 Py_DECREF(v);
1507 Py_DECREF(w);
1508 SET_TOP(x);
1509 if (x != NULL) DISPATCH();
1510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 TARGET(BINARY_FLOOR_DIVIDE)
1513 w = POP();
1514 v = TOP();
1515 x = PyNumber_FloorDivide(v, w);
1516 Py_DECREF(v);
1517 Py_DECREF(w);
1518 SET_TOP(x);
1519 if (x != NULL) DISPATCH();
1520 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 TARGET(BINARY_MODULO)
1523 w = POP();
1524 v = TOP();
1525 if (PyUnicode_CheckExact(v))
1526 x = PyUnicode_Format(v, w);
1527 else
1528 x = PyNumber_Remainder(v, w);
1529 Py_DECREF(v);
1530 Py_DECREF(w);
1531 SET_TOP(x);
1532 if (x != NULL) DISPATCH();
1533 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535 TARGET(BINARY_ADD)
1536 w = POP();
1537 v = TOP();
1538 if (PyUnicode_CheckExact(v) &&
1539 PyUnicode_CheckExact(w)) {
1540 x = unicode_concatenate(v, w, f, next_instr);
1541 /* unicode_concatenate consumed the ref to v */
1542 goto skip_decref_vx;
1543 }
1544 else {
1545 x = PyNumber_Add(v, w);
1546 }
1547 Py_DECREF(v);
1548 skip_decref_vx:
1549 Py_DECREF(w);
1550 SET_TOP(x);
1551 if (x != NULL) DISPATCH();
1552 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001553
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 TARGET(BINARY_SUBTRACT)
1555 w = POP();
1556 v = TOP();
1557 x = PyNumber_Subtract(v, w);
1558 Py_DECREF(v);
1559 Py_DECREF(w);
1560 SET_TOP(x);
1561 if (x != NULL) DISPATCH();
1562 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 TARGET(BINARY_SUBSCR)
1565 w = POP();
1566 v = TOP();
1567 x = PyObject_GetItem(v, w);
1568 Py_DECREF(v);
1569 Py_DECREF(w);
1570 SET_TOP(x);
1571 if (x != NULL) DISPATCH();
1572 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 TARGET(BINARY_LSHIFT)
1575 w = POP();
1576 v = TOP();
1577 x = PyNumber_Lshift(v, w);
1578 Py_DECREF(v);
1579 Py_DECREF(w);
1580 SET_TOP(x);
1581 if (x != NULL) DISPATCH();
1582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 TARGET(BINARY_RSHIFT)
1585 w = POP();
1586 v = TOP();
1587 x = PyNumber_Rshift(v, w);
1588 Py_DECREF(v);
1589 Py_DECREF(w);
1590 SET_TOP(x);
1591 if (x != NULL) DISPATCH();
1592 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 TARGET(BINARY_AND)
1595 w = POP();
1596 v = TOP();
1597 x = PyNumber_And(v, w);
1598 Py_DECREF(v);
1599 Py_DECREF(w);
1600 SET_TOP(x);
1601 if (x != NULL) DISPATCH();
1602 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001604 TARGET(BINARY_XOR)
1605 w = POP();
1606 v = TOP();
1607 x = PyNumber_Xor(v, w);
1608 Py_DECREF(v);
1609 Py_DECREF(w);
1610 SET_TOP(x);
1611 if (x != NULL) DISPATCH();
1612 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 TARGET(BINARY_OR)
1615 w = POP();
1616 v = TOP();
1617 x = PyNumber_Or(v, w);
1618 Py_DECREF(v);
1619 Py_DECREF(w);
1620 SET_TOP(x);
1621 if (x != NULL) DISPATCH();
1622 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 TARGET(LIST_APPEND)
1625 w = POP();
1626 v = PEEK(oparg);
1627 err = PyList_Append(v, w);
1628 Py_DECREF(w);
1629 if (err == 0) {
1630 PREDICT(JUMP_ABSOLUTE);
1631 DISPATCH();
1632 }
1633 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 TARGET(SET_ADD)
1636 w = POP();
1637 v = stack_pointer[-oparg];
1638 err = PySet_Add(v, w);
1639 Py_DECREF(w);
1640 if (err == 0) {
1641 PREDICT(JUMP_ABSOLUTE);
1642 DISPATCH();
1643 }
1644 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001645
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 TARGET(INPLACE_POWER)
1647 w = POP();
1648 v = TOP();
1649 x = PyNumber_InPlacePower(v, w, Py_None);
1650 Py_DECREF(v);
1651 Py_DECREF(w);
1652 SET_TOP(x);
1653 if (x != NULL) DISPATCH();
1654 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 TARGET(INPLACE_MULTIPLY)
1657 w = POP();
1658 v = TOP();
1659 x = PyNumber_InPlaceMultiply(v, w);
1660 Py_DECREF(v);
1661 Py_DECREF(w);
1662 SET_TOP(x);
1663 if (x != NULL) DISPATCH();
1664 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 TARGET(INPLACE_TRUE_DIVIDE)
1667 w = POP();
1668 v = TOP();
1669 x = PyNumber_InPlaceTrueDivide(v, w);
1670 Py_DECREF(v);
1671 Py_DECREF(w);
1672 SET_TOP(x);
1673 if (x != NULL) DISPATCH();
1674 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 TARGET(INPLACE_FLOOR_DIVIDE)
1677 w = POP();
1678 v = TOP();
1679 x = PyNumber_InPlaceFloorDivide(v, w);
1680 Py_DECREF(v);
1681 Py_DECREF(w);
1682 SET_TOP(x);
1683 if (x != NULL) DISPATCH();
1684 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001685
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 TARGET(INPLACE_MODULO)
1687 w = POP();
1688 v = TOP();
1689 x = PyNumber_InPlaceRemainder(v, w);
1690 Py_DECREF(v);
1691 Py_DECREF(w);
1692 SET_TOP(x);
1693 if (x != NULL) DISPATCH();
1694 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 TARGET(INPLACE_ADD)
1697 w = POP();
1698 v = TOP();
1699 if (PyUnicode_CheckExact(v) &&
1700 PyUnicode_CheckExact(w)) {
1701 x = unicode_concatenate(v, w, f, next_instr);
1702 /* unicode_concatenate consumed the ref to v */
1703 goto skip_decref_v;
1704 }
1705 else {
1706 x = PyNumber_InPlaceAdd(v, w);
1707 }
1708 Py_DECREF(v);
1709 skip_decref_v:
1710 Py_DECREF(w);
1711 SET_TOP(x);
1712 if (x != NULL) DISPATCH();
1713 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 TARGET(INPLACE_SUBTRACT)
1716 w = POP();
1717 v = TOP();
1718 x = PyNumber_InPlaceSubtract(v, w);
1719 Py_DECREF(v);
1720 Py_DECREF(w);
1721 SET_TOP(x);
1722 if (x != NULL) DISPATCH();
1723 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 TARGET(INPLACE_LSHIFT)
1726 w = POP();
1727 v = TOP();
1728 x = PyNumber_InPlaceLshift(v, w);
1729 Py_DECREF(v);
1730 Py_DECREF(w);
1731 SET_TOP(x);
1732 if (x != NULL) DISPATCH();
1733 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 TARGET(INPLACE_RSHIFT)
1736 w = POP();
1737 v = TOP();
1738 x = PyNumber_InPlaceRshift(v, w);
1739 Py_DECREF(v);
1740 Py_DECREF(w);
1741 SET_TOP(x);
1742 if (x != NULL) DISPATCH();
1743 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 TARGET(INPLACE_AND)
1746 w = POP();
1747 v = TOP();
1748 x = PyNumber_InPlaceAnd(v, w);
1749 Py_DECREF(v);
1750 Py_DECREF(w);
1751 SET_TOP(x);
1752 if (x != NULL) DISPATCH();
1753 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 TARGET(INPLACE_XOR)
1756 w = POP();
1757 v = TOP();
1758 x = PyNumber_InPlaceXor(v, w);
1759 Py_DECREF(v);
1760 Py_DECREF(w);
1761 SET_TOP(x);
1762 if (x != NULL) DISPATCH();
1763 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 TARGET(INPLACE_OR)
1766 w = POP();
1767 v = TOP();
1768 x = PyNumber_InPlaceOr(v, w);
1769 Py_DECREF(v);
1770 Py_DECREF(w);
1771 SET_TOP(x);
1772 if (x != NULL) DISPATCH();
1773 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 TARGET(STORE_SUBSCR)
1776 w = TOP();
1777 v = SECOND();
1778 u = THIRD();
1779 STACKADJ(-3);
1780 /* v[w] = u */
1781 err = PyObject_SetItem(v, w, u);
1782 Py_DECREF(u);
1783 Py_DECREF(v);
1784 Py_DECREF(w);
1785 if (err == 0) DISPATCH();
1786 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 TARGET(DELETE_SUBSCR)
1789 w = TOP();
1790 v = SECOND();
1791 STACKADJ(-2);
1792 /* del v[w] */
1793 err = PyObject_DelItem(v, w);
1794 Py_DECREF(v);
1795 Py_DECREF(w);
1796 if (err == 0) DISPATCH();
1797 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 TARGET(PRINT_EXPR)
1800 v = POP();
1801 w = PySys_GetObject("displayhook");
1802 if (w == NULL) {
1803 PyErr_SetString(PyExc_RuntimeError,
1804 "lost sys.displayhook");
1805 err = -1;
1806 x = NULL;
1807 }
1808 if (err == 0) {
1809 x = PyTuple_Pack(1, v);
1810 if (x == NULL)
1811 err = -1;
1812 }
1813 if (err == 0) {
1814 w = PyEval_CallObject(w, x);
1815 Py_XDECREF(w);
1816 if (w == NULL)
1817 err = -1;
1818 }
1819 Py_DECREF(v);
1820 Py_XDECREF(x);
1821 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001822
Thomas Wouters434d0822000-08-24 20:11:32 +00001823#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001825#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 TARGET(RAISE_VARARGS)
1827 v = w = NULL;
1828 switch (oparg) {
1829 case 2:
1830 v = POP(); /* cause */
1831 case 1:
1832 w = POP(); /* exc */
1833 case 0: /* Fallthrough */
1834 why = do_raise(w, v);
1835 break;
1836 default:
1837 PyErr_SetString(PyExc_SystemError,
1838 "bad RAISE_VARARGS oparg");
1839 why = WHY_EXCEPTION;
1840 break;
1841 }
1842 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 TARGET(STORE_LOCALS)
1845 x = POP();
1846 v = f->f_locals;
1847 Py_XDECREF(v);
1848 f->f_locals = x;
1849 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 TARGET(RETURN_VALUE)
1852 retval = POP();
1853 why = WHY_RETURN;
1854 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 TARGET(YIELD_VALUE)
1857 retval = POP();
1858 f->f_stacktop = stack_pointer;
1859 why = WHY_YIELD;
1860 /* Put aside the current exception state and restore
1861 that of the calling frame. This only serves when
1862 "yield" is used inside an except handler. */
1863 SWAP_EXC_STATE();
1864 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 TARGET(POP_EXCEPT)
1867 {
1868 PyTryBlock *b = PyFrame_BlockPop(f);
1869 if (b->b_type != EXCEPT_HANDLER) {
1870 PyErr_SetString(PyExc_SystemError,
1871 "popped block is not an except handler");
1872 why = WHY_EXCEPTION;
1873 break;
1874 }
1875 UNWIND_EXCEPT_HANDLER(b);
1876 }
1877 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001879 TARGET(POP_BLOCK)
1880 {
1881 PyTryBlock *b = PyFrame_BlockPop(f);
1882 UNWIND_BLOCK(b);
1883 }
1884 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 PREDICTED(END_FINALLY);
1887 TARGET(END_FINALLY)
1888 v = POP();
1889 if (PyLong_Check(v)) {
1890 why = (enum why_code) PyLong_AS_LONG(v);
1891 assert(why != WHY_YIELD);
1892 if (why == WHY_RETURN ||
1893 why == WHY_CONTINUE)
1894 retval = POP();
1895 if (why == WHY_SILENCED) {
1896 /* An exception was silenced by 'with', we must
1897 manually unwind the EXCEPT_HANDLER block which was
1898 created when the exception was caught, otherwise
1899 the stack will be in an inconsistent state. */
1900 PyTryBlock *b = PyFrame_BlockPop(f);
1901 assert(b->b_type == EXCEPT_HANDLER);
1902 UNWIND_EXCEPT_HANDLER(b);
1903 why = WHY_NOT;
1904 }
1905 }
1906 else if (PyExceptionClass_Check(v)) {
1907 w = POP();
1908 u = POP();
1909 PyErr_Restore(v, w, u);
1910 why = WHY_RERAISE;
1911 break;
1912 }
1913 else if (v != Py_None) {
1914 PyErr_SetString(PyExc_SystemError,
1915 "'finally' pops bad exception");
1916 why = WHY_EXCEPTION;
1917 }
1918 Py_DECREF(v);
1919 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921 TARGET(LOAD_BUILD_CLASS)
1922 x = PyDict_GetItemString(f->f_builtins,
1923 "__build_class__");
1924 if (x == NULL) {
1925 PyErr_SetString(PyExc_ImportError,
1926 "__build_class__ not found");
1927 break;
1928 }
1929 Py_INCREF(x);
1930 PUSH(x);
1931 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001932
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 TARGET(STORE_NAME)
1934 w = GETITEM(names, oparg);
1935 v = POP();
1936 if ((x = f->f_locals) != NULL) {
1937 if (PyDict_CheckExact(x))
1938 err = PyDict_SetItem(x, w, v);
1939 else
1940 err = PyObject_SetItem(x, w, v);
1941 Py_DECREF(v);
1942 if (err == 0) DISPATCH();
1943 break;
1944 }
1945 PyErr_Format(PyExc_SystemError,
1946 "no locals found when storing %R", w);
1947 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001948
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 TARGET(DELETE_NAME)
1950 w = GETITEM(names, oparg);
1951 if ((x = f->f_locals) != NULL) {
1952 if ((err = PyObject_DelItem(x, w)) != 0)
1953 format_exc_check_arg(PyExc_NameError,
1954 NAME_ERROR_MSG,
1955 w);
1956 break;
1957 }
1958 PyErr_Format(PyExc_SystemError,
1959 "no locals when deleting %R", w);
1960 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001961
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001962 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1963 TARGET(UNPACK_SEQUENCE)
1964 v = POP();
1965 if (PyTuple_CheckExact(v) &&
1966 PyTuple_GET_SIZE(v) == oparg) {
1967 PyObject **items = \
1968 ((PyTupleObject *)v)->ob_item;
1969 while (oparg--) {
1970 w = items[oparg];
1971 Py_INCREF(w);
1972 PUSH(w);
1973 }
1974 Py_DECREF(v);
1975 DISPATCH();
1976 } else if (PyList_CheckExact(v) &&
1977 PyList_GET_SIZE(v) == oparg) {
1978 PyObject **items = \
1979 ((PyListObject *)v)->ob_item;
1980 while (oparg--) {
1981 w = items[oparg];
1982 Py_INCREF(w);
1983 PUSH(w);
1984 }
1985 } else if (unpack_iterable(v, oparg, -1,
1986 stack_pointer + oparg)) {
1987 STACKADJ(oparg);
1988 } else {
1989 /* unpack_iterable() raised an exception */
1990 why = WHY_EXCEPTION;
1991 }
1992 Py_DECREF(v);
1993 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001994
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001995 TARGET(UNPACK_EX)
1996 {
1997 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1998 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002000 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2001 stack_pointer + totalargs)) {
2002 stack_pointer += totalargs;
2003 } else {
2004 why = WHY_EXCEPTION;
2005 }
2006 Py_DECREF(v);
2007 break;
2008 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002010 TARGET(STORE_ATTR)
2011 w = GETITEM(names, oparg);
2012 v = TOP();
2013 u = SECOND();
2014 STACKADJ(-2);
2015 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2016 Py_DECREF(v);
2017 Py_DECREF(u);
2018 if (err == 0) DISPATCH();
2019 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002021 TARGET(DELETE_ATTR)
2022 w = GETITEM(names, oparg);
2023 v = POP();
2024 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2025 /* del v.w */
2026 Py_DECREF(v);
2027 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002028
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 TARGET(STORE_GLOBAL)
2030 w = GETITEM(names, oparg);
2031 v = POP();
2032 err = PyDict_SetItem(f->f_globals, w, v);
2033 Py_DECREF(v);
2034 if (err == 0) DISPATCH();
2035 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 TARGET(DELETE_GLOBAL)
2038 w = GETITEM(names, oparg);
2039 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2040 format_exc_check_arg(
2041 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2042 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 TARGET(LOAD_NAME)
2045 w = GETITEM(names, oparg);
2046 if ((v = f->f_locals) == NULL) {
2047 PyErr_Format(PyExc_SystemError,
2048 "no locals when loading %R", w);
2049 why = WHY_EXCEPTION;
2050 break;
2051 }
2052 if (PyDict_CheckExact(v)) {
2053 x = PyDict_GetItem(v, w);
2054 Py_XINCREF(x);
2055 }
2056 else {
2057 x = PyObject_GetItem(v, w);
2058 if (x == NULL && PyErr_Occurred()) {
2059 if (!PyErr_ExceptionMatches(
2060 PyExc_KeyError))
2061 break;
2062 PyErr_Clear();
2063 }
2064 }
2065 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002066 x = PyDict_GetItem(f->f_globals, w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 if (x == NULL) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002068 x = PyDict_GetItem(f->f_builtins, w);
2069 if (x == NULL) {
2070 format_exc_check_arg(
2071 PyExc_NameError,
2072 NAME_ERROR_MSG, w);
2073 break;
2074 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 }
2076 Py_INCREF(x);
2077 }
2078 PUSH(x);
2079 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002080
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002081 TARGET(LOAD_GLOBAL)
2082 w = GETITEM(names, oparg);
2083 if (PyUnicode_CheckExact(w)) {
2084 /* Inline the PyDict_GetItem() calls.
2085 WARNING: this is an extreme speed hack.
2086 Do not try this at home. */
2087 long hash = ((PyUnicodeObject *)w)->hash;
2088 if (hash != -1) {
2089 PyDictObject *d;
2090 PyDictEntry *e;
2091 d = (PyDictObject *)(f->f_globals);
2092 e = d->ma_lookup(d, w, hash);
2093 if (e == NULL) {
2094 x = NULL;
2095 break;
2096 }
2097 x = e->me_value;
2098 if (x != NULL) {
2099 Py_INCREF(x);
2100 PUSH(x);
2101 DISPATCH();
2102 }
2103 d = (PyDictObject *)(f->f_builtins);
2104 e = d->ma_lookup(d, w, hash);
2105 if (e == NULL) {
2106 x = NULL;
2107 break;
2108 }
2109 x = e->me_value;
2110 if (x != NULL) {
2111 Py_INCREF(x);
2112 PUSH(x);
2113 DISPATCH();
2114 }
2115 goto load_global_error;
2116 }
2117 }
2118 /* This is the un-inlined version of the code above */
2119 x = PyDict_GetItem(f->f_globals, w);
2120 if (x == NULL) {
2121 x = PyDict_GetItem(f->f_builtins, w);
2122 if (x == NULL) {
2123 load_global_error:
2124 format_exc_check_arg(
2125 PyExc_NameError,
2126 GLOBAL_NAME_ERROR_MSG, w);
2127 break;
2128 }
2129 }
2130 Py_INCREF(x);
2131 PUSH(x);
2132 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002133
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002134 TARGET(DELETE_FAST)
2135 x = GETLOCAL(oparg);
2136 if (x != NULL) {
2137 SETLOCAL(oparg, NULL);
2138 DISPATCH();
2139 }
2140 format_exc_check_arg(
2141 PyExc_UnboundLocalError,
2142 UNBOUNDLOCAL_ERROR_MSG,
2143 PyTuple_GetItem(co->co_varnames, oparg)
2144 );
2145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002146
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002147 TARGET(DELETE_DEREF)
2148 x = freevars[oparg];
2149 if (PyCell_GET(x) != NULL) {
2150 PyCell_Set(x, NULL);
2151 continue;
2152 }
2153 err = -1;
2154 format_exc_unbound(co, oparg);
2155 break;
2156
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002157 TARGET(LOAD_CLOSURE)
2158 x = freevars[oparg];
2159 Py_INCREF(x);
2160 PUSH(x);
2161 if (x != NULL) DISPATCH();
2162 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002163
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002164 TARGET(LOAD_DEREF)
2165 x = freevars[oparg];
2166 w = PyCell_Get(x);
2167 if (w != NULL) {
2168 PUSH(w);
2169 DISPATCH();
2170 }
2171 err = -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002172 format_exc_unbound(co, oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002175 TARGET(STORE_DEREF)
2176 w = POP();
2177 x = freevars[oparg];
2178 PyCell_Set(x, w);
2179 Py_DECREF(w);
2180 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002181
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002182 TARGET(BUILD_TUPLE)
2183 x = PyTuple_New(oparg);
2184 if (x != NULL) {
2185 for (; --oparg >= 0;) {
2186 w = POP();
2187 PyTuple_SET_ITEM(x, oparg, w);
2188 }
2189 PUSH(x);
2190 DISPATCH();
2191 }
2192 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002193
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002194 TARGET(BUILD_LIST)
2195 x = PyList_New(oparg);
2196 if (x != NULL) {
2197 for (; --oparg >= 0;) {
2198 w = POP();
2199 PyList_SET_ITEM(x, oparg, w);
2200 }
2201 PUSH(x);
2202 DISPATCH();
2203 }
2204 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002205
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 TARGET(BUILD_SET)
2207 x = PySet_New(NULL);
2208 if (x != NULL) {
2209 for (; --oparg >= 0;) {
2210 w = POP();
2211 if (err == 0)
2212 err = PySet_Add(x, w);
2213 Py_DECREF(w);
2214 }
2215 if (err != 0) {
2216 Py_DECREF(x);
2217 break;
2218 }
2219 PUSH(x);
2220 DISPATCH();
2221 }
2222 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002224 TARGET(BUILD_MAP)
2225 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2226 PUSH(x);
2227 if (x != NULL) DISPATCH();
2228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002230 TARGET(STORE_MAP)
2231 w = TOP(); /* key */
2232 u = SECOND(); /* value */
2233 v = THIRD(); /* dict */
2234 STACKADJ(-2);
2235 assert (PyDict_CheckExact(v));
2236 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2237 Py_DECREF(u);
2238 Py_DECREF(w);
2239 if (err == 0) DISPATCH();
2240 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 TARGET(MAP_ADD)
2243 w = TOP(); /* key */
2244 u = SECOND(); /* value */
2245 STACKADJ(-2);
2246 v = stack_pointer[-oparg]; /* dict */
2247 assert (PyDict_CheckExact(v));
2248 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2249 Py_DECREF(u);
2250 Py_DECREF(w);
2251 if (err == 0) {
2252 PREDICT(JUMP_ABSOLUTE);
2253 DISPATCH();
2254 }
2255 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002257 TARGET(LOAD_ATTR)
2258 w = GETITEM(names, oparg);
2259 v = TOP();
2260 x = PyObject_GetAttr(v, w);
2261 Py_DECREF(v);
2262 SET_TOP(x);
2263 if (x != NULL) DISPATCH();
2264 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002266 TARGET(COMPARE_OP)
2267 w = POP();
2268 v = TOP();
2269 x = cmp_outcome(oparg, v, w);
2270 Py_DECREF(v);
2271 Py_DECREF(w);
2272 SET_TOP(x);
2273 if (x == NULL) break;
2274 PREDICT(POP_JUMP_IF_FALSE);
2275 PREDICT(POP_JUMP_IF_TRUE);
2276 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 TARGET(IMPORT_NAME)
2279 w = GETITEM(names, oparg);
2280 x = PyDict_GetItemString(f->f_builtins, "__import__");
2281 if (x == NULL) {
2282 PyErr_SetString(PyExc_ImportError,
2283 "__import__ not found");
2284 break;
2285 }
2286 Py_INCREF(x);
2287 v = POP();
2288 u = TOP();
2289 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2290 w = PyTuple_Pack(5,
2291 w,
2292 f->f_globals,
2293 f->f_locals == NULL ?
2294 Py_None : f->f_locals,
2295 v,
2296 u);
2297 else
2298 w = PyTuple_Pack(4,
2299 w,
2300 f->f_globals,
2301 f->f_locals == NULL ?
2302 Py_None : f->f_locals,
2303 v);
2304 Py_DECREF(v);
2305 Py_DECREF(u);
2306 if (w == NULL) {
2307 u = POP();
2308 Py_DECREF(x);
2309 x = NULL;
2310 break;
2311 }
2312 READ_TIMESTAMP(intr0);
2313 v = x;
2314 x = PyEval_CallObject(v, w);
2315 Py_DECREF(v);
2316 READ_TIMESTAMP(intr1);
2317 Py_DECREF(w);
2318 SET_TOP(x);
2319 if (x != NULL) DISPATCH();
2320 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002322 TARGET(IMPORT_STAR)
2323 v = POP();
2324 PyFrame_FastToLocals(f);
2325 if ((x = f->f_locals) == NULL) {
2326 PyErr_SetString(PyExc_SystemError,
2327 "no locals found during 'import *'");
2328 break;
2329 }
2330 READ_TIMESTAMP(intr0);
2331 err = import_all_from(x, v);
2332 READ_TIMESTAMP(intr1);
2333 PyFrame_LocalsToFast(f, 0);
2334 Py_DECREF(v);
2335 if (err == 0) DISPATCH();
2336 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002338 TARGET(IMPORT_FROM)
2339 w = GETITEM(names, oparg);
2340 v = TOP();
2341 READ_TIMESTAMP(intr0);
2342 x = import_from(v, w);
2343 READ_TIMESTAMP(intr1);
2344 PUSH(x);
2345 if (x != NULL) DISPATCH();
2346 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002348 TARGET(JUMP_FORWARD)
2349 JUMPBY(oparg);
2350 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002351
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002352 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2353 TARGET(POP_JUMP_IF_FALSE)
2354 w = POP();
2355 if (w == Py_True) {
2356 Py_DECREF(w);
2357 FAST_DISPATCH();
2358 }
2359 if (w == Py_False) {
2360 Py_DECREF(w);
2361 JUMPTO(oparg);
2362 FAST_DISPATCH();
2363 }
2364 err = PyObject_IsTrue(w);
2365 Py_DECREF(w);
2366 if (err > 0)
2367 err = 0;
2368 else if (err == 0)
2369 JUMPTO(oparg);
2370 else
2371 break;
2372 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002373
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002374 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2375 TARGET(POP_JUMP_IF_TRUE)
2376 w = POP();
2377 if (w == Py_False) {
2378 Py_DECREF(w);
2379 FAST_DISPATCH();
2380 }
2381 if (w == Py_True) {
2382 Py_DECREF(w);
2383 JUMPTO(oparg);
2384 FAST_DISPATCH();
2385 }
2386 err = PyObject_IsTrue(w);
2387 Py_DECREF(w);
2388 if (err > 0) {
2389 err = 0;
2390 JUMPTO(oparg);
2391 }
2392 else if (err == 0)
2393 ;
2394 else
2395 break;
2396 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002398 TARGET(JUMP_IF_FALSE_OR_POP)
2399 w = TOP();
2400 if (w == Py_True) {
2401 STACKADJ(-1);
2402 Py_DECREF(w);
2403 FAST_DISPATCH();
2404 }
2405 if (w == Py_False) {
2406 JUMPTO(oparg);
2407 FAST_DISPATCH();
2408 }
2409 err = PyObject_IsTrue(w);
2410 if (err > 0) {
2411 STACKADJ(-1);
2412 Py_DECREF(w);
2413 err = 0;
2414 }
2415 else if (err == 0)
2416 JUMPTO(oparg);
2417 else
2418 break;
2419 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 TARGET(JUMP_IF_TRUE_OR_POP)
2422 w = TOP();
2423 if (w == Py_False) {
2424 STACKADJ(-1);
2425 Py_DECREF(w);
2426 FAST_DISPATCH();
2427 }
2428 if (w == Py_True) {
2429 JUMPTO(oparg);
2430 FAST_DISPATCH();
2431 }
2432 err = PyObject_IsTrue(w);
2433 if (err > 0) {
2434 err = 0;
2435 JUMPTO(oparg);
2436 }
2437 else if (err == 0) {
2438 STACKADJ(-1);
2439 Py_DECREF(w);
2440 }
2441 else
2442 break;
2443 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002444
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002445 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2446 TARGET(JUMP_ABSOLUTE)
2447 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002448#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002449 /* Enabling this path speeds-up all while and for-loops by bypassing
2450 the per-loop checks for signals. By default, this should be turned-off
2451 because it prevents detection of a control-break in tight loops like
2452 "while 1: pass". Compile with this option turned-on when you need
2453 the speed-up and do not need break checking inside tight loops (ones
2454 that contain only instructions ending with FAST_DISPATCH).
2455 */
2456 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002457#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002458 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002459#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002461 TARGET(GET_ITER)
2462 /* before: [obj]; after [getiter(obj)] */
2463 v = TOP();
2464 x = PyObject_GetIter(v);
2465 Py_DECREF(v);
2466 if (x != NULL) {
2467 SET_TOP(x);
2468 PREDICT(FOR_ITER);
2469 DISPATCH();
2470 }
2471 STACKADJ(-1);
2472 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002474 PREDICTED_WITH_ARG(FOR_ITER);
2475 TARGET(FOR_ITER)
2476 /* before: [iter]; after: [iter, iter()] *or* [] */
2477 v = TOP();
2478 x = (*v->ob_type->tp_iternext)(v);
2479 if (x != NULL) {
2480 PUSH(x);
2481 PREDICT(STORE_FAST);
2482 PREDICT(UNPACK_SEQUENCE);
2483 DISPATCH();
2484 }
2485 if (PyErr_Occurred()) {
2486 if (!PyErr_ExceptionMatches(
2487 PyExc_StopIteration))
2488 break;
2489 PyErr_Clear();
2490 }
2491 /* iterator ended normally */
2492 x = v = POP();
2493 Py_DECREF(v);
2494 JUMPBY(oparg);
2495 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002497 TARGET(BREAK_LOOP)
2498 why = WHY_BREAK;
2499 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002500
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002501 TARGET(CONTINUE_LOOP)
2502 retval = PyLong_FromLong(oparg);
2503 if (!retval) {
2504 x = NULL;
2505 break;
2506 }
2507 why = WHY_CONTINUE;
2508 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002510 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2511 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2512 TARGET(SETUP_FINALLY)
2513 _setup_finally:
2514 /* NOTE: If you add any new block-setup opcodes that
2515 are not try/except/finally handlers, you may need
2516 to update the PyGen_NeedsFinalizing() function.
2517 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002518
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002519 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2520 STACK_LEVEL());
2521 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002523 TARGET(SETUP_WITH)
2524 {
2525 static PyObject *exit, *enter;
2526 w = TOP();
2527 x = special_lookup(w, "__exit__", &exit);
2528 if (!x)
2529 break;
2530 SET_TOP(x);
2531 u = special_lookup(w, "__enter__", &enter);
2532 Py_DECREF(w);
2533 if (!u) {
2534 x = NULL;
2535 break;
2536 }
2537 x = PyObject_CallFunctionObjArgs(u, NULL);
2538 Py_DECREF(u);
2539 if (!x)
2540 break;
2541 /* Setup the finally block before pushing the result
2542 of __enter__ on the stack. */
2543 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2544 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002546 PUSH(x);
2547 DISPATCH();
2548 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002549
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002550 TARGET(WITH_CLEANUP)
2551 {
2552 /* At the top of the stack are 1-3 values indicating
2553 how/why we entered the finally clause:
2554 - TOP = None
2555 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2556 - TOP = WHY_*; no retval below it
2557 - (TOP, SECOND, THIRD) = exc_info()
2558 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2559 Below them is EXIT, the context.__exit__ bound method.
2560 In the last case, we must call
2561 EXIT(TOP, SECOND, THIRD)
2562 otherwise we must call
2563 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002564
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 In the first two cases, we remove EXIT from the
2566 stack, leaving the rest in the same order. In the
2567 third case, we shift the bottom 3 values of the
2568 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002570 In addition, if the stack represents an exception,
2571 *and* the function call returns a 'true' value, we
2572 push WHY_SILENCED onto the stack. END_FINALLY will
2573 then not re-raise the exception. (But non-local
2574 gotos should still be resumed.)
2575 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002576
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002577 PyObject *exit_func;
2578 u = TOP();
2579 if (u == Py_None) {
2580 (void)POP();
2581 exit_func = TOP();
2582 SET_TOP(u);
2583 v = w = Py_None;
2584 }
2585 else if (PyLong_Check(u)) {
2586 (void)POP();
2587 switch(PyLong_AsLong(u)) {
2588 case WHY_RETURN:
2589 case WHY_CONTINUE:
2590 /* Retval in TOP. */
2591 exit_func = SECOND();
2592 SET_SECOND(TOP());
2593 SET_TOP(u);
2594 break;
2595 default:
2596 exit_func = TOP();
2597 SET_TOP(u);
2598 break;
2599 }
2600 u = v = w = Py_None;
2601 }
2602 else {
2603 PyObject *tp, *exc, *tb;
2604 PyTryBlock *block;
2605 v = SECOND();
2606 w = THIRD();
2607 tp = FOURTH();
2608 exc = PEEK(5);
2609 tb = PEEK(6);
2610 exit_func = PEEK(7);
2611 SET_VALUE(7, tb);
2612 SET_VALUE(6, exc);
2613 SET_VALUE(5, tp);
2614 /* UNWIND_EXCEPT_HANDLER will pop this off. */
2615 SET_FOURTH(NULL);
2616 /* We just shifted the stack down, so we have
2617 to tell the except handler block that the
2618 values are lower than it expects. */
2619 block = &f->f_blockstack[f->f_iblock - 1];
2620 assert(block->b_type == EXCEPT_HANDLER);
2621 block->b_level--;
2622 }
2623 /* XXX Not the fastest way to call it... */
2624 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2625 NULL);
2626 Py_DECREF(exit_func);
2627 if (x == NULL)
2628 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002630 if (u != Py_None)
2631 err = PyObject_IsTrue(x);
2632 else
2633 err = 0;
2634 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 if (err < 0)
2637 break; /* Go to error exit */
2638 else if (err > 0) {
2639 err = 0;
2640 /* There was an exception and a True return */
2641 PUSH(PyLong_FromLong((long) WHY_SILENCED));
2642 }
2643 PREDICT(END_FINALLY);
2644 break;
2645 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 TARGET(CALL_FUNCTION)
2648 {
2649 PyObject **sp;
2650 PCALL(PCALL_ALL);
2651 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002652#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002654#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002655 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002656#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002657 stack_pointer = sp;
2658 PUSH(x);
2659 if (x != NULL)
2660 DISPATCH();
2661 break;
2662 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002664 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2665 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2666 TARGET(CALL_FUNCTION_VAR_KW)
2667 _call_function_var_kw:
2668 {
2669 int na = oparg & 0xff;
2670 int nk = (oparg>>8) & 0xff;
2671 int flags = (opcode - CALL_FUNCTION) & 3;
2672 int n = na + 2 * nk;
2673 PyObject **pfunc, *func, **sp;
2674 PCALL(PCALL_ALL);
2675 if (flags & CALL_FLAG_VAR)
2676 n++;
2677 if (flags & CALL_FLAG_KW)
2678 n++;
2679 pfunc = stack_pointer - n - 1;
2680 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002681
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002682 if (PyMethod_Check(func)
Stefan Krahb7e10102010-06-23 18:42:39 +00002683 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 PyObject *self = PyMethod_GET_SELF(func);
2685 Py_INCREF(self);
2686 func = PyMethod_GET_FUNCTION(func);
2687 Py_INCREF(func);
2688 Py_DECREF(*pfunc);
2689 *pfunc = self;
2690 na++;
2691 n++;
2692 } else
2693 Py_INCREF(func);
2694 sp = stack_pointer;
2695 READ_TIMESTAMP(intr0);
2696 x = ext_do_call(func, &sp, flags, na, nk);
2697 READ_TIMESTAMP(intr1);
2698 stack_pointer = sp;
2699 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002701 while (stack_pointer > pfunc) {
2702 w = POP();
2703 Py_DECREF(w);
2704 }
2705 PUSH(x);
2706 if (x != NULL)
2707 DISPATCH();
2708 break;
2709 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002710
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002711 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2712 TARGET(MAKE_FUNCTION)
2713 _make_function:
2714 {
2715 int posdefaults = oparg & 0xff;
2716 int kwdefaults = (oparg>>8) & 0xff;
2717 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 v = POP(); /* code object */
2720 x = PyFunction_New(v, f->f_globals);
2721 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002723 if (x != NULL && opcode == MAKE_CLOSURE) {
2724 v = POP();
2725 if (PyFunction_SetClosure(x, v) != 0) {
2726 /* Can't happen unless bytecode is corrupt. */
2727 why = WHY_EXCEPTION;
2728 }
2729 Py_DECREF(v);
2730 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002731
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 if (x != NULL && num_annotations > 0) {
2733 Py_ssize_t name_ix;
2734 u = POP(); /* names of args with annotations */
2735 v = PyDict_New();
2736 if (v == NULL) {
2737 Py_DECREF(x);
2738 x = NULL;
2739 break;
2740 }
2741 name_ix = PyTuple_Size(u);
2742 assert(num_annotations == name_ix+1);
2743 while (name_ix > 0) {
2744 --name_ix;
2745 t = PyTuple_GET_ITEM(u, name_ix);
2746 w = POP();
2747 /* XXX(nnorwitz): check for errors */
2748 PyDict_SetItem(v, t, w);
2749 Py_DECREF(w);
2750 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 if (PyFunction_SetAnnotations(x, v) != 0) {
2753 /* Can't happen unless
2754 PyFunction_SetAnnotations changes. */
2755 why = WHY_EXCEPTION;
2756 }
2757 Py_DECREF(v);
2758 Py_DECREF(u);
2759 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002761 /* XXX Maybe this should be a separate opcode? */
2762 if (x != NULL && posdefaults > 0) {
2763 v = PyTuple_New(posdefaults);
2764 if (v == NULL) {
2765 Py_DECREF(x);
2766 x = NULL;
2767 break;
2768 }
2769 while (--posdefaults >= 0) {
2770 w = POP();
2771 PyTuple_SET_ITEM(v, posdefaults, w);
2772 }
2773 if (PyFunction_SetDefaults(x, v) != 0) {
2774 /* Can't happen unless
2775 PyFunction_SetDefaults changes. */
2776 why = WHY_EXCEPTION;
2777 }
2778 Py_DECREF(v);
2779 }
2780 if (x != NULL && kwdefaults > 0) {
2781 v = PyDict_New();
2782 if (v == NULL) {
2783 Py_DECREF(x);
2784 x = NULL;
2785 break;
2786 }
2787 while (--kwdefaults >= 0) {
2788 w = POP(); /* default value */
2789 u = POP(); /* kw only arg name */
2790 /* XXX(nnorwitz): check for errors */
2791 PyDict_SetItem(v, u, w);
2792 Py_DECREF(w);
2793 Py_DECREF(u);
2794 }
2795 if (PyFunction_SetKwDefaults(x, v) != 0) {
2796 /* Can't happen unless
2797 PyFunction_SetKwDefaults changes. */
2798 why = WHY_EXCEPTION;
2799 }
2800 Py_DECREF(v);
2801 }
2802 PUSH(x);
2803 break;
2804 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 TARGET(BUILD_SLICE)
2807 if (oparg == 3)
2808 w = POP();
2809 else
2810 w = NULL;
2811 v = POP();
2812 u = TOP();
2813 x = PySlice_New(u, v, w);
2814 Py_DECREF(u);
2815 Py_DECREF(v);
2816 Py_XDECREF(w);
2817 SET_TOP(x);
2818 if (x != NULL) DISPATCH();
2819 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 TARGET(EXTENDED_ARG)
2822 opcode = NEXTOP();
2823 oparg = oparg<<16 | NEXTARG();
2824 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002825
Antoine Pitrou042b1282010-08-13 21:15:58 +00002826#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002828#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 default:
2830 fprintf(stderr,
2831 "XXX lineno: %d, opcode: %d\n",
2832 PyFrame_GetLineNumber(f),
2833 opcode);
2834 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2835 why = WHY_EXCEPTION;
2836 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002837
2838#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002839 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002840#endif
2841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002846 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 if (why == WHY_NOT) {
2851 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002852#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 /* This check is expensive! */
2854 if (PyErr_Occurred())
2855 fprintf(stderr,
2856 "XXX undetected error\n");
2857 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002858#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002859 READ_TIMESTAMP(loop1);
2860 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002861#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002862 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002863#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002864 }
2865 why = WHY_EXCEPTION;
2866 x = Py_None;
2867 err = 0;
2868 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002870 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2873 if (!PyErr_Occurred()) {
2874 PyErr_SetString(PyExc_SystemError,
2875 "error return without exception set");
2876 why = WHY_EXCEPTION;
2877 }
2878 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002879#ifdef CHECKEXC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002880 else {
2881 /* This check is expensive! */
2882 if (PyErr_Occurred()) {
2883 char buf[128];
2884 sprintf(buf, "Stack unwind with exception "
2885 "set and why=%d", why);
2886 Py_FatalError(buf);
2887 }
2888 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002889#endif
2890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 if (why == WHY_EXCEPTION) {
2894 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 if (tstate->c_tracefunc != NULL)
2897 call_exc_trace(tstate->c_tracefunc,
2898 tstate->c_traceobj, f);
2899 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 if (why == WHY_RERAISE)
2904 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002907
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002908fast_block_end:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002909 while (why != WHY_NOT && f->f_iblock > 0) {
2910 /* Peek at the current block. */
2911 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002912
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002913 assert(why != WHY_YIELD);
2914 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2915 why = WHY_NOT;
2916 JUMPTO(PyLong_AS_LONG(retval));
2917 Py_DECREF(retval);
2918 break;
2919 }
2920 /* Now we have to pop the block. */
2921 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002923 if (b->b_type == EXCEPT_HANDLER) {
2924 UNWIND_EXCEPT_HANDLER(b);
2925 continue;
2926 }
2927 UNWIND_BLOCK(b);
2928 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2929 why = WHY_NOT;
2930 JUMPTO(b->b_handler);
2931 break;
2932 }
2933 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2934 || b->b_type == SETUP_FINALLY)) {
2935 PyObject *exc, *val, *tb;
2936 int handler = b->b_handler;
2937 /* Beware, this invalidates all b->b_* fields */
2938 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2939 PUSH(tstate->exc_traceback);
2940 PUSH(tstate->exc_value);
2941 if (tstate->exc_type != NULL) {
2942 PUSH(tstate->exc_type);
2943 }
2944 else {
2945 Py_INCREF(Py_None);
2946 PUSH(Py_None);
2947 }
2948 PyErr_Fetch(&exc, &val, &tb);
2949 /* Make the raw exception data
2950 available to the handler,
2951 so a program can emulate the
2952 Python main loop. */
2953 PyErr_NormalizeException(
2954 &exc, &val, &tb);
2955 PyException_SetTraceback(val, tb);
2956 Py_INCREF(exc);
2957 tstate->exc_type = exc;
2958 Py_INCREF(val);
2959 tstate->exc_value = val;
2960 tstate->exc_traceback = tb;
2961 if (tb == NULL)
2962 tb = Py_None;
2963 Py_INCREF(tb);
2964 PUSH(tb);
2965 PUSH(val);
2966 PUSH(exc);
2967 why = WHY_NOT;
2968 JUMPTO(handler);
2969 break;
2970 }
2971 if (b->b_type == SETUP_FINALLY) {
2972 if (why & (WHY_RETURN | WHY_CONTINUE))
2973 PUSH(retval);
2974 PUSH(PyLong_FromLong((long)why));
2975 why = WHY_NOT;
2976 JUMPTO(b->b_handler);
2977 break;
2978 }
2979 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002981 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002983 if (why != WHY_NOT)
2984 break;
2985 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002987 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 assert(why != WHY_YIELD);
2990 /* Pop remaining stack entries. */
2991 while (!EMPTY()) {
2992 v = POP();
2993 Py_XDECREF(v);
2994 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 if (why != WHY_RETURN)
2997 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002998
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002999fast_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003000 if (tstate->use_tracing) {
3001 if (tstate->c_tracefunc) {
3002 if (why == WHY_RETURN || why == WHY_YIELD) {
3003 if (call_trace(tstate->c_tracefunc,
3004 tstate->c_traceobj, f,
3005 PyTrace_RETURN, retval)) {
3006 Py_XDECREF(retval);
3007 retval = NULL;
3008 why = WHY_EXCEPTION;
3009 }
3010 }
3011 else if (why == WHY_EXCEPTION) {
3012 call_trace_protected(tstate->c_tracefunc,
3013 tstate->c_traceobj, f,
3014 PyTrace_RETURN, NULL);
3015 }
3016 }
3017 if (tstate->c_profilefunc) {
3018 if (why == WHY_EXCEPTION)
3019 call_trace_protected(tstate->c_profilefunc,
3020 tstate->c_profileobj, f,
3021 PyTrace_RETURN, NULL);
3022 else if (call_trace(tstate->c_profilefunc,
3023 tstate->c_profileobj, f,
3024 PyTrace_RETURN, retval)) {
3025 Py_XDECREF(retval);
3026 retval = NULL;
3027 why = WHY_EXCEPTION;
3028 }
3029 }
3030 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003033exit_eval_frame:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 Py_LeaveRecursiveCall();
3035 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003036
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003038}
3039
Guido van Rossumc2e20742006-02-27 22:32:47 +00003040/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003041 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003042 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003043
Tim Peters6d6c1a32001-08-02 04:15:00 +00003044PyObject *
3045PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 PyObject **args, int argcount, PyObject **kws, int kwcount,
3047 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003048{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 register PyFrameObject *f;
3050 register PyObject *retval = NULL;
3051 register PyObject **fastlocals, **freevars;
3052 PyThreadState *tstate = PyThreadState_GET();
3053 PyObject *x, *u;
3054 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003056 if (globals == NULL) {
3057 PyErr_SetString(PyExc_SystemError,
3058 "PyEval_EvalCodeEx: NULL globals");
3059 return NULL;
3060 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 assert(tstate != NULL);
3063 assert(globals != NULL);
3064 f = PyFrame_New(tstate, co, globals, locals);
3065 if (f == NULL)
3066 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 fastlocals = f->f_localsplus;
3069 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3072 int i;
3073 int n = argcount;
3074 PyObject *kwdict = NULL;
3075 if (co->co_flags & CO_VARKEYWORDS) {
3076 kwdict = PyDict_New();
3077 if (kwdict == NULL)
3078 goto fail;
3079 i = total_args;
3080 if (co->co_flags & CO_VARARGS)
3081 i++;
3082 SETLOCAL(i, kwdict);
3083 }
3084 if (argcount > co->co_argcount) {
3085 if (!(co->co_flags & CO_VARARGS)) {
3086 PyErr_Format(PyExc_TypeError,
3087 "%U() takes %s %d "
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003088 "positional argument%s (%d given)",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003089 co->co_name,
3090 defcount ? "at most" : "exactly",
Benjamin Peterson88968ad2010-06-25 19:30:21 +00003091 co->co_argcount,
3092 co->co_argcount == 1 ? "" : "s",
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 argcount + kwcount);
3094 goto fail;
3095 }
3096 n = co->co_argcount;
3097 }
3098 for (i = 0; i < n; i++) {
3099 x = args[i];
3100 Py_INCREF(x);
3101 SETLOCAL(i, x);
3102 }
3103 if (co->co_flags & CO_VARARGS) {
3104 u = PyTuple_New(argcount - n);
3105 if (u == NULL)
3106 goto fail;
3107 SETLOCAL(total_args, u);
3108 for (i = n; i < argcount; i++) {
3109 x = args[i];
3110 Py_INCREF(x);
3111 PyTuple_SET_ITEM(u, i-n, x);
3112 }
3113 }
3114 for (i = 0; i < kwcount; i++) {
3115 PyObject **co_varnames;
3116 PyObject *keyword = kws[2*i];
3117 PyObject *value = kws[2*i + 1];
3118 int j;
3119 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3120 PyErr_Format(PyExc_TypeError,
3121 "%U() keywords must be strings",
3122 co->co_name);
3123 goto fail;
3124 }
3125 /* Speed hack: do raw pointer compares. As names are
3126 normally interned this should almost always hit. */
3127 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3128 for (j = 0; j < total_args; j++) {
3129 PyObject *nm = co_varnames[j];
3130 if (nm == keyword)
3131 goto kw_found;
3132 }
3133 /* Slow fallback, just in case */
3134 for (j = 0; j < total_args; j++) {
3135 PyObject *nm = co_varnames[j];
3136 int cmp = PyObject_RichCompareBool(
3137 keyword, nm, Py_EQ);
3138 if (cmp > 0)
3139 goto kw_found;
3140 else if (cmp < 0)
3141 goto fail;
3142 }
3143 if (j >= total_args && kwdict == NULL) {
3144 PyErr_Format(PyExc_TypeError,
3145 "%U() got an unexpected "
3146 "keyword argument '%S'",
3147 co->co_name,
3148 keyword);
3149 goto fail;
3150 }
3151 PyDict_SetItem(kwdict, keyword, value);
3152 continue;
3153 kw_found:
3154 if (GETLOCAL(j) != NULL) {
3155 PyErr_Format(PyExc_TypeError,
3156 "%U() got multiple "
3157 "values for keyword "
3158 "argument '%S'",
3159 co->co_name,
3160 keyword);
3161 goto fail;
3162 }
3163 Py_INCREF(value);
3164 SETLOCAL(j, value);
3165 }
3166 if (co->co_kwonlyargcount > 0) {
3167 for (i = co->co_argcount; i < total_args; i++) {
3168 PyObject *name;
3169 if (GETLOCAL(i) != NULL)
3170 continue;
3171 name = PyTuple_GET_ITEM(co->co_varnames, i);
3172 if (kwdefs != NULL) {
3173 PyObject *def = PyDict_GetItem(kwdefs, name);
3174 if (def) {
3175 Py_INCREF(def);
3176 SETLOCAL(i, def);
3177 continue;
3178 }
3179 }
3180 PyErr_Format(PyExc_TypeError,
3181 "%U() needs keyword-only argument %S",
3182 co->co_name, name);
3183 goto fail;
3184 }
3185 }
3186 if (argcount < co->co_argcount) {
3187 int m = co->co_argcount - defcount;
3188 for (i = argcount; i < m; i++) {
3189 if (GETLOCAL(i) == NULL) {
3190 int j, given = 0;
3191 for (j = 0; j < co->co_argcount; j++)
3192 if (GETLOCAL(j))
3193 given++;
3194 PyErr_Format(PyExc_TypeError,
3195 "%U() takes %s %d "
3196 "argument%s "
3197 "(%d given)",
3198 co->co_name,
3199 ((co->co_flags & CO_VARARGS) ||
3200 defcount) ? "at least"
3201 : "exactly",
3202 m, m == 1 ? "" : "s", given);
3203 goto fail;
3204 }
3205 }
3206 if (n > m)
3207 i = n - m;
3208 else
3209 i = 0;
3210 for (; i < defcount; i++) {
3211 if (GETLOCAL(m+i) == NULL) {
3212 PyObject *def = defs[i];
3213 Py_INCREF(def);
3214 SETLOCAL(m+i, def);
3215 }
3216 }
3217 }
3218 }
3219 else if (argcount > 0 || kwcount > 0) {
3220 PyErr_Format(PyExc_TypeError,
3221 "%U() takes no arguments (%d given)",
3222 co->co_name,
3223 argcount + kwcount);
3224 goto fail;
3225 }
3226 /* Allocate and initialize storage for cell vars, and copy free
3227 vars into frame. This isn't too efficient right now. */
3228 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3229 int i, j, nargs, found;
3230 Py_UNICODE *cellname, *argname;
3231 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003233 nargs = total_args;
3234 if (co->co_flags & CO_VARARGS)
3235 nargs++;
3236 if (co->co_flags & CO_VARKEYWORDS)
3237 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003239 /* Initialize each cell var, taking into account
3240 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003242 Should arrange for the compiler to put cellvars
3243 that are arguments at the beginning of the cellvars
3244 list so that we can march over it more efficiently?
3245 */
3246 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3247 cellname = PyUnicode_AS_UNICODE(
3248 PyTuple_GET_ITEM(co->co_cellvars, i));
3249 found = 0;
3250 for (j = 0; j < nargs; j++) {
3251 argname = PyUnicode_AS_UNICODE(
3252 PyTuple_GET_ITEM(co->co_varnames, j));
3253 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3254 c = PyCell_New(GETLOCAL(j));
3255 if (c == NULL)
3256 goto fail;
3257 GETLOCAL(co->co_nlocals + i) = c;
3258 found = 1;
3259 break;
3260 }
3261 }
3262 if (found == 0) {
3263 c = PyCell_New(NULL);
3264 if (c == NULL)
3265 goto fail;
3266 SETLOCAL(co->co_nlocals + i, c);
3267 }
3268 }
3269 }
3270 if (PyTuple_GET_SIZE(co->co_freevars)) {
3271 int i;
3272 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3273 PyObject *o = PyTuple_GET_ITEM(closure, i);
3274 Py_INCREF(o);
3275 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3276 }
3277 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003279 if (co->co_flags & CO_GENERATOR) {
3280 /* Don't need to keep the reference to f_back, it will be set
3281 * when the generator is resumed. */
3282 Py_XDECREF(f->f_back);
3283 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003285 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003287 /* Create a new generator that owns the ready to run frame
3288 * and return that as the value. */
3289 return PyGen_New(f);
3290 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003292 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003293
Thomas Woutersce272b62007-09-19 21:19:28 +00003294fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 /* decref'ing the frame can cause __del__ methods to get invoked,
3297 which can call back into Python. While we're done with the
3298 current Python frame (f), the associated C stack is still in use,
3299 so recursion_depth must be boosted for the duration.
3300 */
3301 assert(tstate != NULL);
3302 ++tstate->recursion_depth;
3303 Py_DECREF(f);
3304 --tstate->recursion_depth;
3305 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003306}
3307
3308
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003309static PyObject *
3310special_lookup(PyObject *o, char *meth, PyObject **cache)
3311{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 PyObject *res;
3313 res = _PyObject_LookupSpecial(o, meth, cache);
3314 if (res == NULL && !PyErr_Occurred()) {
3315 PyErr_SetObject(PyExc_AttributeError, *cache);
3316 return NULL;
3317 }
3318 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003319}
3320
3321
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003322/* Logic for the raise statement (too complicated for inlining).
3323 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003324static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003325do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003328
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003329 if (exc == NULL) {
3330 /* Reraise */
3331 PyThreadState *tstate = PyThreadState_GET();
3332 PyObject *tb;
3333 type = tstate->exc_type;
3334 value = tstate->exc_value;
3335 tb = tstate->exc_traceback;
3336 if (type == Py_None) {
3337 PyErr_SetString(PyExc_RuntimeError,
3338 "No active exception to reraise");
3339 return WHY_EXCEPTION;
3340 }
3341 Py_XINCREF(type);
3342 Py_XINCREF(value);
3343 Py_XINCREF(tb);
3344 PyErr_Restore(type, value, tb);
3345 return WHY_RERAISE;
3346 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003348 /* We support the following forms of raise:
3349 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003350 raise <instance>
3351 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003353 if (PyExceptionClass_Check(exc)) {
3354 type = exc;
3355 value = PyObject_CallObject(exc, NULL);
3356 if (value == NULL)
3357 goto raise_error;
3358 }
3359 else if (PyExceptionInstance_Check(exc)) {
3360 value = exc;
3361 type = PyExceptionInstance_Class(exc);
3362 Py_INCREF(type);
3363 }
3364 else {
3365 /* Not something you can raise. You get an exception
3366 anyway, just not what you specified :-) */
3367 Py_DECREF(exc);
3368 PyErr_SetString(PyExc_TypeError,
3369 "exceptions must derive from BaseException");
3370 goto raise_error;
3371 }
Collin Winter828f04a2007-08-31 00:04:24 +00003372
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003373 if (cause) {
3374 PyObject *fixed_cause;
3375 if (PyExceptionClass_Check(cause)) {
3376 fixed_cause = PyObject_CallObject(cause, NULL);
3377 if (fixed_cause == NULL)
3378 goto raise_error;
3379 Py_DECREF(cause);
3380 }
3381 else if (PyExceptionInstance_Check(cause)) {
3382 fixed_cause = cause;
3383 }
3384 else {
3385 PyErr_SetString(PyExc_TypeError,
3386 "exception causes must derive from "
3387 "BaseException");
3388 goto raise_error;
3389 }
3390 PyException_SetCause(value, fixed_cause);
3391 }
Collin Winter828f04a2007-08-31 00:04:24 +00003392
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003393 PyErr_SetObject(type, value);
3394 /* PyErr_SetObject incref's its arguments */
3395 Py_XDECREF(value);
3396 Py_XDECREF(type);
3397 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003398
3399raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003400 Py_XDECREF(value);
3401 Py_XDECREF(type);
3402 Py_XDECREF(cause);
3403 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003404}
3405
Tim Petersd6d010b2001-06-21 02:49:55 +00003406/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003407 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003408
Guido van Rossum0368b722007-05-11 16:50:42 +00003409 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3410 with a variable target.
3411*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003412
Barry Warsawe42b18f1997-08-25 22:13:04 +00003413static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003414unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003415{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003416 int i = 0, j = 0;
3417 Py_ssize_t ll = 0;
3418 PyObject *it; /* iter(v) */
3419 PyObject *w;
3420 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003421
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003423
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 it = PyObject_GetIter(v);
3425 if (it == NULL)
3426 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003428 for (; i < argcnt; i++) {
3429 w = PyIter_Next(it);
3430 if (w == NULL) {
3431 /* Iterator done, via error or exhaustion. */
3432 if (!PyErr_Occurred()) {
3433 PyErr_Format(PyExc_ValueError,
3434 "need more than %d value%s to unpack",
3435 i, i == 1 ? "" : "s");
3436 }
3437 goto Error;
3438 }
3439 *--sp = w;
3440 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 if (argcntafter == -1) {
3443 /* We better have exhausted the iterator now. */
3444 w = PyIter_Next(it);
3445 if (w == NULL) {
3446 if (PyErr_Occurred())
3447 goto Error;
3448 Py_DECREF(it);
3449 return 1;
3450 }
3451 Py_DECREF(w);
Georg Brandl0310a832010-07-10 10:32:36 +00003452 PyErr_Format(PyExc_ValueError, "too many values to unpack "
3453 "(expected %d)", argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 goto Error;
3455 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003457 l = PySequence_List(it);
3458 if (l == NULL)
3459 goto Error;
3460 *--sp = l;
3461 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003462
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003463 ll = PyList_GET_SIZE(l);
3464 if (ll < argcntafter) {
3465 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3466 argcnt + ll);
3467 goto Error;
3468 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003470 /* Pop the "after-variable" args off the list. */
3471 for (j = argcntafter; j > 0; j--, i++) {
3472 *--sp = PyList_GET_ITEM(l, ll - j);
3473 }
3474 /* Resize the list. */
3475 Py_SIZE(l) = ll - argcntafter;
3476 Py_DECREF(it);
3477 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003478
Tim Petersd6d010b2001-06-21 02:49:55 +00003479Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003480 for (; i > 0; i--, sp++)
3481 Py_DECREF(*sp);
3482 Py_XDECREF(it);
3483 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003484}
3485
3486
Guido van Rossum96a42c81992-01-12 02:29:51 +00003487#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003488static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003489prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 printf("%s ", str);
3492 if (PyObject_Print(v, stdout, 0) != 0)
3493 PyErr_Clear(); /* Don't know what else to do */
3494 printf("\n");
3495 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003496}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003497#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003498
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003499static void
Fred Drake5755ce62001-06-27 19:19:46 +00003500call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003502 PyObject *type, *value, *traceback, *arg;
3503 int err;
3504 PyErr_Fetch(&type, &value, &traceback);
3505 if (value == NULL) {
3506 value = Py_None;
3507 Py_INCREF(value);
3508 }
3509 arg = PyTuple_Pack(3, type, value, traceback);
3510 if (arg == NULL) {
3511 PyErr_Restore(type, value, traceback);
3512 return;
3513 }
3514 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3515 Py_DECREF(arg);
3516 if (err == 0)
3517 PyErr_Restore(type, value, traceback);
3518 else {
3519 Py_XDECREF(type);
3520 Py_XDECREF(value);
3521 Py_XDECREF(traceback);
3522 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003523}
3524
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003525static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003526call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003528{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003529 PyObject *type, *value, *traceback;
3530 int err;
3531 PyErr_Fetch(&type, &value, &traceback);
3532 err = call_trace(func, obj, frame, what, arg);
3533 if (err == 0)
3534 {
3535 PyErr_Restore(type, value, traceback);
3536 return 0;
3537 }
3538 else {
3539 Py_XDECREF(type);
3540 Py_XDECREF(value);
3541 Py_XDECREF(traceback);
3542 return -1;
3543 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003544}
3545
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003546static int
Fred Drake5755ce62001-06-27 19:19:46 +00003547call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003548 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 register PyThreadState *tstate = frame->f_tstate;
3551 int result;
3552 if (tstate->tracing)
3553 return 0;
3554 tstate->tracing++;
3555 tstate->use_tracing = 0;
3556 result = func(obj, frame, what, arg);
3557 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3558 || (tstate->c_profilefunc != NULL));
3559 tstate->tracing--;
3560 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003561}
3562
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003563PyObject *
3564_PyEval_CallTracing(PyObject *func, PyObject *args)
3565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 PyFrameObject *frame = PyEval_GetFrame();
3567 PyThreadState *tstate = frame->f_tstate;
3568 int save_tracing = tstate->tracing;
3569 int save_use_tracing = tstate->use_tracing;
3570 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003572 tstate->tracing = 0;
3573 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3574 || (tstate->c_profilefunc != NULL));
3575 result = PyObject_Call(func, args, NULL);
3576 tstate->tracing = save_tracing;
3577 tstate->use_tracing = save_use_tracing;
3578 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003579}
3580
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003581/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003582static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003583maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3585 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 int result = 0;
3588 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 /* If the last instruction executed isn't in the current
3591 instruction window, reset the window.
3592 */
3593 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3594 PyAddrPair bounds;
3595 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3596 &bounds);
3597 *instr_lb = bounds.ap_lower;
3598 *instr_ub = bounds.ap_upper;
3599 }
3600 /* If the last instruction falls at the start of a line or if
3601 it represents a jump backwards, update the frame's line
3602 number and call the trace function. */
3603 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3604 frame->f_lineno = line;
3605 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3606 }
3607 *instr_prev = frame->f_lasti;
3608 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003609}
3610
Fred Drake5755ce62001-06-27 19:19:46 +00003611void
3612PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003613{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 PyThreadState *tstate = PyThreadState_GET();
3615 PyObject *temp = tstate->c_profileobj;
3616 Py_XINCREF(arg);
3617 tstate->c_profilefunc = NULL;
3618 tstate->c_profileobj = NULL;
3619 /* Must make sure that tracing is not ignored if 'temp' is freed */
3620 tstate->use_tracing = tstate->c_tracefunc != NULL;
3621 Py_XDECREF(temp);
3622 tstate->c_profilefunc = func;
3623 tstate->c_profileobj = arg;
3624 /* Flag that tracing or profiling is turned on */
3625 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003626}
3627
3628void
3629PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3630{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 PyThreadState *tstate = PyThreadState_GET();
3632 PyObject *temp = tstate->c_traceobj;
3633 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3634 Py_XINCREF(arg);
3635 tstate->c_tracefunc = NULL;
3636 tstate->c_traceobj = NULL;
3637 /* Must make sure that profiling is not ignored if 'temp' is freed */
3638 tstate->use_tracing = tstate->c_profilefunc != NULL;
3639 Py_XDECREF(temp);
3640 tstate->c_tracefunc = func;
3641 tstate->c_traceobj = arg;
3642 /* Flag that tracing or profiling is turned on */
3643 tstate->use_tracing = ((func != NULL)
3644 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003645}
3646
Guido van Rossumb209a111997-04-29 18:18:01 +00003647PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003648PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003649{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003650 PyFrameObject *current_frame = PyEval_GetFrame();
3651 if (current_frame == NULL)
3652 return PyThreadState_GET()->interp->builtins;
3653 else
3654 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003655}
3656
Guido van Rossumb209a111997-04-29 18:18:01 +00003657PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003658PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003659{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003660 PyFrameObject *current_frame = PyEval_GetFrame();
3661 if (current_frame == NULL)
3662 return NULL;
3663 PyFrame_FastToLocals(current_frame);
3664 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003665}
3666
Guido van Rossumb209a111997-04-29 18:18:01 +00003667PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003668PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003670 PyFrameObject *current_frame = PyEval_GetFrame();
3671 if (current_frame == NULL)
3672 return NULL;
3673 else
3674 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003675}
3676
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003677PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003678PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003680 PyThreadState *tstate = PyThreadState_GET();
3681 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003682}
3683
Guido van Rossum6135a871995-01-09 17:53:26 +00003684int
Tim Peters5ba58662001-07-16 02:29:45 +00003685PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003687 PyFrameObject *current_frame = PyEval_GetFrame();
3688 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003690 if (current_frame != NULL) {
3691 const int codeflags = current_frame->f_code->co_flags;
3692 const int compilerflags = codeflags & PyCF_MASK;
3693 if (compilerflags) {
3694 result = 1;
3695 cf->cf_flags |= compilerflags;
3696 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003697#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003698 if (codeflags & CO_GENERATOR_ALLOWED) {
3699 result = 1;
3700 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3701 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003702#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003703 }
3704 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003705}
3706
Guido van Rossum3f5da241990-12-20 15:06:42 +00003707
Guido van Rossum681d79a1995-07-18 14:51:37 +00003708/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003709 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003710
Guido van Rossumb209a111997-04-29 18:18:01 +00003711PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003712PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003716 if (arg == NULL) {
3717 arg = PyTuple_New(0);
3718 if (arg == NULL)
3719 return NULL;
3720 }
3721 else if (!PyTuple_Check(arg)) {
3722 PyErr_SetString(PyExc_TypeError,
3723 "argument list must be a tuple");
3724 return NULL;
3725 }
3726 else
3727 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003729 if (kw != NULL && !PyDict_Check(kw)) {
3730 PyErr_SetString(PyExc_TypeError,
3731 "keyword list must be a dictionary");
3732 Py_DECREF(arg);
3733 return NULL;
3734 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003736 result = PyObject_Call(func, arg, kw);
3737 Py_DECREF(arg);
3738 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003739}
3740
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003741const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003742PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 if (PyMethod_Check(func))
3745 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3746 else if (PyFunction_Check(func))
3747 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3748 else if (PyCFunction_Check(func))
3749 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3750 else
3751 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003752}
3753
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003754const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003755PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003756{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003757 if (PyMethod_Check(func))
3758 return "()";
3759 else if (PyFunction_Check(func))
3760 return "()";
3761 else if (PyCFunction_Check(func))
3762 return "()";
3763 else
3764 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003765}
3766
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003767static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003768err_args(PyObject *func, int flags, int nargs)
3769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003770 if (flags & METH_NOARGS)
3771 PyErr_Format(PyExc_TypeError,
3772 "%.200s() takes no arguments (%d given)",
3773 ((PyCFunctionObject *)func)->m_ml->ml_name,
3774 nargs);
3775 else
3776 PyErr_Format(PyExc_TypeError,
3777 "%.200s() takes exactly one argument (%d given)",
3778 ((PyCFunctionObject *)func)->m_ml->ml_name,
3779 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003780}
3781
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003782#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003783if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003784 if (call_trace(tstate->c_profilefunc, \
3785 tstate->c_profileobj, \
3786 tstate->frame, PyTrace_C_CALL, \
3787 func)) { \
3788 x = NULL; \
3789 } \
3790 else { \
3791 x = call; \
3792 if (tstate->c_profilefunc != NULL) { \
3793 if (x == NULL) { \
3794 call_trace_protected(tstate->c_profilefunc, \
3795 tstate->c_profileobj, \
3796 tstate->frame, PyTrace_C_EXCEPTION, \
3797 func); \
3798 /* XXX should pass (type, value, tb) */ \
3799 } else { \
3800 if (call_trace(tstate->c_profilefunc, \
3801 tstate->c_profileobj, \
3802 tstate->frame, PyTrace_C_RETURN, \
3803 func)) { \
3804 Py_DECREF(x); \
3805 x = NULL; \
3806 } \
3807 } \
3808 } \
3809 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003810} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003811 x = call; \
3812 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003813
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003814static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003815call_function(PyObject ***pp_stack, int oparg
3816#ifdef WITH_TSC
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003818#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 int na = oparg & 0xff;
3822 int nk = (oparg>>8) & 0xff;
3823 int n = na + 2 * nk;
3824 PyObject **pfunc = (*pp_stack) - n - 1;
3825 PyObject *func = *pfunc;
3826 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003828 /* Always dispatch PyCFunction first, because these are
3829 presumed to be the most frequent callable object.
3830 */
3831 if (PyCFunction_Check(func) && nk == 0) {
3832 int flags = PyCFunction_GET_FLAGS(func);
3833 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003835 PCALL(PCALL_CFUNCTION);
3836 if (flags & (METH_NOARGS | METH_O)) {
3837 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3838 PyObject *self = PyCFunction_GET_SELF(func);
3839 if (flags & METH_NOARGS && na == 0) {
3840 C_TRACE(x, (*meth)(self,NULL));
3841 }
3842 else if (flags & METH_O && na == 1) {
3843 PyObject *arg = EXT_POP(*pp_stack);
3844 C_TRACE(x, (*meth)(self,arg));
3845 Py_DECREF(arg);
3846 }
3847 else {
3848 err_args(func, flags, na);
3849 x = NULL;
3850 }
3851 }
3852 else {
3853 PyObject *callargs;
3854 callargs = load_args(pp_stack, na);
3855 READ_TIMESTAMP(*pintr0);
3856 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3857 READ_TIMESTAMP(*pintr1);
3858 Py_XDECREF(callargs);
3859 }
3860 } else {
3861 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3862 /* optimize access to bound methods */
3863 PyObject *self = PyMethod_GET_SELF(func);
3864 PCALL(PCALL_METHOD);
3865 PCALL(PCALL_BOUND_METHOD);
3866 Py_INCREF(self);
3867 func = PyMethod_GET_FUNCTION(func);
3868 Py_INCREF(func);
3869 Py_DECREF(*pfunc);
3870 *pfunc = self;
3871 na++;
3872 n++;
3873 } else
3874 Py_INCREF(func);
3875 READ_TIMESTAMP(*pintr0);
3876 if (PyFunction_Check(func))
3877 x = fast_function(func, pp_stack, n, na, nk);
3878 else
3879 x = do_call(func, pp_stack, na, nk);
3880 READ_TIMESTAMP(*pintr1);
3881 Py_DECREF(func);
3882 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003884 /* Clear the stack of the function object. Also removes
3885 the arguments in case they weren't consumed already
3886 (fast_function() and err_args() leave them on the stack).
3887 */
3888 while ((*pp_stack) > pfunc) {
3889 w = EXT_POP(*pp_stack);
3890 Py_DECREF(w);
3891 PCALL(PCALL_POP);
3892 }
3893 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003894}
3895
Jeremy Hylton192690e2002-08-16 18:36:11 +00003896/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003897 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003898 For the simplest case -- a function that takes only positional
3899 arguments and is called with only positional arguments -- it
3900 inlines the most primitive frame setup code from
3901 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3902 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003903*/
3904
3905static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003906fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003907{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3909 PyObject *globals = PyFunction_GET_GLOBALS(func);
3910 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3911 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3912 PyObject **d = NULL;
3913 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 PCALL(PCALL_FUNCTION);
3916 PCALL(PCALL_FAST_FUNCTION);
3917 if (argdefs == NULL && co->co_argcount == n &&
3918 co->co_kwonlyargcount == 0 && nk==0 &&
3919 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3920 PyFrameObject *f;
3921 PyObject *retval = NULL;
3922 PyThreadState *tstate = PyThreadState_GET();
3923 PyObject **fastlocals, **stack;
3924 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003925
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003926 PCALL(PCALL_FASTER_FUNCTION);
3927 assert(globals != NULL);
3928 /* XXX Perhaps we should create a specialized
3929 PyFrame_New() that doesn't take locals, but does
3930 take builtins without sanity checking them.
3931 */
3932 assert(tstate != NULL);
3933 f = PyFrame_New(tstate, co, globals, NULL);
3934 if (f == NULL)
3935 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003937 fastlocals = f->f_localsplus;
3938 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 for (i = 0; i < n; i++) {
3941 Py_INCREF(*stack);
3942 fastlocals[i] = *stack++;
3943 }
3944 retval = PyEval_EvalFrameEx(f,0);
3945 ++tstate->recursion_depth;
3946 Py_DECREF(f);
3947 --tstate->recursion_depth;
3948 return retval;
3949 }
3950 if (argdefs != NULL) {
3951 d = &PyTuple_GET_ITEM(argdefs, 0);
3952 nd = Py_SIZE(argdefs);
3953 }
3954 return PyEval_EvalCodeEx(co, globals,
3955 (PyObject *)NULL, (*pp_stack)-n, na,
3956 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3957 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003958}
3959
3960static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003961update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3962 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003963{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003964 PyObject *kwdict = NULL;
3965 if (orig_kwdict == NULL)
3966 kwdict = PyDict_New();
3967 else {
3968 kwdict = PyDict_Copy(orig_kwdict);
3969 Py_DECREF(orig_kwdict);
3970 }
3971 if (kwdict == NULL)
3972 return NULL;
3973 while (--nk >= 0) {
3974 int err;
3975 PyObject *value = EXT_POP(*pp_stack);
3976 PyObject *key = EXT_POP(*pp_stack);
3977 if (PyDict_GetItem(kwdict, key) != NULL) {
3978 PyErr_Format(PyExc_TypeError,
3979 "%.200s%s got multiple values "
3980 "for keyword argument '%U'",
3981 PyEval_GetFuncName(func),
3982 PyEval_GetFuncDesc(func),
3983 key);
3984 Py_DECREF(key);
3985 Py_DECREF(value);
3986 Py_DECREF(kwdict);
3987 return NULL;
3988 }
3989 err = PyDict_SetItem(kwdict, key, value);
3990 Py_DECREF(key);
3991 Py_DECREF(value);
3992 if (err) {
3993 Py_DECREF(kwdict);
3994 return NULL;
3995 }
3996 }
3997 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00003998}
3999
4000static PyObject *
4001update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 callargs = PyTuple_New(nstack + nstar);
4007 if (callargs == NULL) {
4008 return NULL;
4009 }
4010 if (nstar) {
4011 int i;
4012 for (i = 0; i < nstar; i++) {
4013 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4014 Py_INCREF(a);
4015 PyTuple_SET_ITEM(callargs, nstack + i, a);
4016 }
4017 }
4018 while (--nstack >= 0) {
4019 w = EXT_POP(*pp_stack);
4020 PyTuple_SET_ITEM(callargs, nstack, w);
4021 }
4022 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004023}
4024
4025static PyObject *
4026load_args(PyObject ***pp_stack, int na)
4027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004028 PyObject *args = PyTuple_New(na);
4029 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 if (args == NULL)
4032 return NULL;
4033 while (--na >= 0) {
4034 w = EXT_POP(*pp_stack);
4035 PyTuple_SET_ITEM(args, na, w);
4036 }
4037 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004038}
4039
4040static PyObject *
4041do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4042{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 PyObject *callargs = NULL;
4044 PyObject *kwdict = NULL;
4045 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004046
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 if (nk > 0) {
4048 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4049 if (kwdict == NULL)
4050 goto call_fail;
4051 }
4052 callargs = load_args(pp_stack, na);
4053 if (callargs == NULL)
4054 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004055#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004056 /* At this point, we have to look at the type of func to
4057 update the call stats properly. Do it here so as to avoid
4058 exposing the call stats machinery outside ceval.c
4059 */
4060 if (PyFunction_Check(func))
4061 PCALL(PCALL_FUNCTION);
4062 else if (PyMethod_Check(func))
4063 PCALL(PCALL_METHOD);
4064 else if (PyType_Check(func))
4065 PCALL(PCALL_TYPE);
4066 else if (PyCFunction_Check(func))
4067 PCALL(PCALL_CFUNCTION);
4068 else
4069 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004070#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 if (PyCFunction_Check(func)) {
4072 PyThreadState *tstate = PyThreadState_GET();
4073 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4074 }
4075 else
4076 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004077call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004078 Py_XDECREF(callargs);
4079 Py_XDECREF(kwdict);
4080 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004081}
4082
4083static PyObject *
4084ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4085{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004086 int nstar = 0;
4087 PyObject *callargs = NULL;
4088 PyObject *stararg = NULL;
4089 PyObject *kwdict = NULL;
4090 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004091
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 if (flags & CALL_FLAG_KW) {
4093 kwdict = EXT_POP(*pp_stack);
4094 if (!PyDict_Check(kwdict)) {
4095 PyObject *d;
4096 d = PyDict_New();
4097 if (d == NULL)
4098 goto ext_call_fail;
4099 if (PyDict_Update(d, kwdict) != 0) {
4100 Py_DECREF(d);
4101 /* PyDict_Update raises attribute
4102 * error (percolated from an attempt
4103 * to get 'keys' attribute) instead of
4104 * a type error if its second argument
4105 * is not a mapping.
4106 */
4107 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4108 PyErr_Format(PyExc_TypeError,
4109 "%.200s%.200s argument after ** "
4110 "must be a mapping, not %.200s",
4111 PyEval_GetFuncName(func),
4112 PyEval_GetFuncDesc(func),
4113 kwdict->ob_type->tp_name);
4114 }
4115 goto ext_call_fail;
4116 }
4117 Py_DECREF(kwdict);
4118 kwdict = d;
4119 }
4120 }
4121 if (flags & CALL_FLAG_VAR) {
4122 stararg = EXT_POP(*pp_stack);
4123 if (!PyTuple_Check(stararg)) {
4124 PyObject *t = NULL;
4125 t = PySequence_Tuple(stararg);
4126 if (t == NULL) {
4127 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4128 PyErr_Format(PyExc_TypeError,
4129 "%.200s%.200s argument after * "
4130 "must be a sequence, not %200s",
4131 PyEval_GetFuncName(func),
4132 PyEval_GetFuncDesc(func),
4133 stararg->ob_type->tp_name);
4134 }
4135 goto ext_call_fail;
4136 }
4137 Py_DECREF(stararg);
4138 stararg = t;
4139 }
4140 nstar = PyTuple_GET_SIZE(stararg);
4141 }
4142 if (nk > 0) {
4143 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4144 if (kwdict == NULL)
4145 goto ext_call_fail;
4146 }
4147 callargs = update_star_args(na, nstar, stararg, pp_stack);
4148 if (callargs == NULL)
4149 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004150#ifdef CALL_PROFILE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004151 /* At this point, we have to look at the type of func to
4152 update the call stats properly. Do it here so as to avoid
4153 exposing the call stats machinery outside ceval.c
4154 */
4155 if (PyFunction_Check(func))
4156 PCALL(PCALL_FUNCTION);
4157 else if (PyMethod_Check(func))
4158 PCALL(PCALL_METHOD);
4159 else if (PyType_Check(func))
4160 PCALL(PCALL_TYPE);
4161 else if (PyCFunction_Check(func))
4162 PCALL(PCALL_CFUNCTION);
4163 else
4164 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004165#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 if (PyCFunction_Check(func)) {
4167 PyThreadState *tstate = PyThreadState_GET();
4168 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4169 }
4170 else
4171 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004172ext_call_fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004173 Py_XDECREF(callargs);
4174 Py_XDECREF(kwdict);
4175 Py_XDECREF(stararg);
4176 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004177}
4178
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004179/* Extract a slice index from a PyInt or PyLong or an object with the
4180 nb_index slot defined, and store in *pi.
4181 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4182 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 +00004183 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004184*/
Tim Petersb5196382001-12-16 19:44:20 +00004185/* Note: If v is NULL, return success without storing into *pi. This
4186 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4187 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004188*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004189int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004190_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004191{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004192 if (v != NULL) {
4193 Py_ssize_t x;
4194 if (PyIndex_Check(v)) {
4195 x = PyNumber_AsSsize_t(v, NULL);
4196 if (x == -1 && PyErr_Occurred())
4197 return 0;
4198 }
4199 else {
4200 PyErr_SetString(PyExc_TypeError,
4201 "slice indices must be integers or "
4202 "None or have an __index__ method");
4203 return 0;
4204 }
4205 *pi = x;
4206 }
4207 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004208}
4209
Guido van Rossum486364b2007-06-30 05:01:58 +00004210#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004211 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004212
Guido van Rossumb209a111997-04-29 18:18:01 +00004213static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004214cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004215{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 int res = 0;
4217 switch (op) {
4218 case PyCmp_IS:
4219 res = (v == w);
4220 break;
4221 case PyCmp_IS_NOT:
4222 res = (v != w);
4223 break;
4224 case PyCmp_IN:
4225 res = PySequence_Contains(w, v);
4226 if (res < 0)
4227 return NULL;
4228 break;
4229 case PyCmp_NOT_IN:
4230 res = PySequence_Contains(w, v);
4231 if (res < 0)
4232 return NULL;
4233 res = !res;
4234 break;
4235 case PyCmp_EXC_MATCH:
4236 if (PyTuple_Check(w)) {
4237 Py_ssize_t i, length;
4238 length = PyTuple_Size(w);
4239 for (i = 0; i < length; i += 1) {
4240 PyObject *exc = PyTuple_GET_ITEM(w, i);
4241 if (!PyExceptionClass_Check(exc)) {
4242 PyErr_SetString(PyExc_TypeError,
4243 CANNOT_CATCH_MSG);
4244 return NULL;
4245 }
4246 }
4247 }
4248 else {
4249 if (!PyExceptionClass_Check(w)) {
4250 PyErr_SetString(PyExc_TypeError,
4251 CANNOT_CATCH_MSG);
4252 return NULL;
4253 }
4254 }
4255 res = PyErr_GivenExceptionMatches(v, w);
4256 break;
4257 default:
4258 return PyObject_RichCompare(v, w, op);
4259 }
4260 v = res ? Py_True : Py_False;
4261 Py_INCREF(v);
4262 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004263}
4264
Thomas Wouters52152252000-08-17 22:55:00 +00004265static PyObject *
4266import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004270 x = PyObject_GetAttr(v, name);
4271 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4272 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4273 }
4274 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004275}
Guido van Rossumac7be682001-01-17 15:42:30 +00004276
Thomas Wouters52152252000-08-17 22:55:00 +00004277static int
4278import_all_from(PyObject *locals, PyObject *v)
4279{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004280 PyObject *all = PyObject_GetAttrString(v, "__all__");
4281 PyObject *dict, *name, *value;
4282 int skip_leading_underscores = 0;
4283 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 if (all == NULL) {
4286 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4287 return -1; /* Unexpected error */
4288 PyErr_Clear();
4289 dict = PyObject_GetAttrString(v, "__dict__");
4290 if (dict == NULL) {
4291 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4292 return -1;
4293 PyErr_SetString(PyExc_ImportError,
4294 "from-import-* object has no __dict__ and no __all__");
4295 return -1;
4296 }
4297 all = PyMapping_Keys(dict);
4298 Py_DECREF(dict);
4299 if (all == NULL)
4300 return -1;
4301 skip_leading_underscores = 1;
4302 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004304 for (pos = 0, err = 0; ; pos++) {
4305 name = PySequence_GetItem(all, pos);
4306 if (name == NULL) {
4307 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4308 err = -1;
4309 else
4310 PyErr_Clear();
4311 break;
4312 }
4313 if (skip_leading_underscores &&
4314 PyUnicode_Check(name) &&
4315 PyUnicode_AS_UNICODE(name)[0] == '_')
4316 {
4317 Py_DECREF(name);
4318 continue;
4319 }
4320 value = PyObject_GetAttr(v, name);
4321 if (value == NULL)
4322 err = -1;
4323 else if (PyDict_CheckExact(locals))
4324 err = PyDict_SetItem(locals, name, value);
4325 else
4326 err = PyObject_SetItem(locals, name, value);
4327 Py_DECREF(name);
4328 Py_XDECREF(value);
4329 if (err != 0)
4330 break;
4331 }
4332 Py_DECREF(all);
4333 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004334}
4335
Guido van Rossumac7be682001-01-17 15:42:30 +00004336static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004337format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004338{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004339 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004340
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 if (!obj)
4342 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004343
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004344 obj_str = _PyUnicode_AsString(obj);
4345 if (!obj_str)
4346 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004347
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004348 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004349}
Guido van Rossum950361c1997-01-24 13:49:28 +00004350
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00004351static void
4352format_exc_unbound(PyCodeObject *co, int oparg)
4353{
4354 PyObject *name;
4355 /* Don't stomp existing exception */
4356 if (PyErr_Occurred())
4357 return;
4358 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
4359 name = PyTuple_GET_ITEM(co->co_cellvars,
4360 oparg);
4361 format_exc_check_arg(
4362 PyExc_UnboundLocalError,
4363 UNBOUNDLOCAL_ERROR_MSG,
4364 name);
4365 } else {
4366 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
4367 PyTuple_GET_SIZE(co->co_cellvars));
4368 format_exc_check_arg(PyExc_NameError,
4369 UNBOUNDFREE_ERROR_MSG, name);
4370 }
4371}
4372
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004373static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004374unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004376{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 /* This function implements 'variable += expr' when both arguments
4378 are (Unicode) strings. */
4379 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4380 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4381 Py_ssize_t new_len = v_len + w_len;
4382 if (new_len < 0) {
4383 PyErr_SetString(PyExc_OverflowError,
4384 "strings are too large to concat");
4385 return NULL;
4386 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004388 if (v->ob_refcnt == 2) {
4389 /* In the common case, there are 2 references to the value
4390 * stored in 'variable' when the += is performed: one on the
4391 * value stack (in 'v') and one still stored in the
4392 * 'variable'. We try to delete the variable now to reduce
4393 * the refcnt to 1.
4394 */
4395 switch (*next_instr) {
4396 case STORE_FAST:
4397 {
4398 int oparg = PEEKARG();
4399 PyObject **fastlocals = f->f_localsplus;
4400 if (GETLOCAL(oparg) == v)
4401 SETLOCAL(oparg, NULL);
4402 break;
4403 }
4404 case STORE_DEREF:
4405 {
4406 PyObject **freevars = (f->f_localsplus +
4407 f->f_code->co_nlocals);
4408 PyObject *c = freevars[PEEKARG()];
4409 if (PyCell_GET(c) == v)
4410 PyCell_Set(c, NULL);
4411 break;
4412 }
4413 case STORE_NAME:
4414 {
4415 PyObject *names = f->f_code->co_names;
4416 PyObject *name = GETITEM(names, PEEKARG());
4417 PyObject *locals = f->f_locals;
4418 if (PyDict_CheckExact(locals) &&
4419 PyDict_GetItem(locals, name) == v) {
4420 if (PyDict_DelItem(locals, name) != 0) {
4421 PyErr_Clear();
4422 }
4423 }
4424 break;
4425 }
4426 }
4427 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4430 /* Now we own the last reference to 'v', so we can resize it
4431 * in-place.
4432 */
4433 if (PyUnicode_Resize(&v, new_len) != 0) {
4434 /* XXX if PyUnicode_Resize() fails, 'v' has been
4435 * deallocated so it cannot be put back into
4436 * 'variable'. The MemoryError is raised when there
4437 * is no value in 'variable', which might (very
4438 * remotely) be a cause of incompatibilities.
4439 */
4440 return NULL;
4441 }
4442 /* copy 'w' into the newly allocated area of 'v' */
4443 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4444 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4445 return v;
4446 }
4447 else {
4448 /* When in-place resizing is not an option. */
4449 w = PyUnicode_Concat(v, w);
4450 Py_DECREF(v);
4451 return w;
4452 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004453}
4454
Guido van Rossum950361c1997-01-24 13:49:28 +00004455#ifdef DYNAMIC_EXECUTION_PROFILE
4456
Skip Montanarof118cb12001-10-15 20:51:38 +00004457static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004458getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004459{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 int i;
4461 PyObject *l = PyList_New(256);
4462 if (l == NULL) return NULL;
4463 for (i = 0; i < 256; i++) {
4464 PyObject *x = PyLong_FromLong(a[i]);
4465 if (x == NULL) {
4466 Py_DECREF(l);
4467 return NULL;
4468 }
4469 PyList_SetItem(l, i, x);
4470 }
4471 for (i = 0; i < 256; i++)
4472 a[i] = 0;
4473 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004474}
4475
4476PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004477_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004478{
4479#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004480 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004481#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 int i;
4483 PyObject *l = PyList_New(257);
4484 if (l == NULL) return NULL;
4485 for (i = 0; i < 257; i++) {
4486 PyObject *x = getarray(dxpairs[i]);
4487 if (x == NULL) {
4488 Py_DECREF(l);
4489 return NULL;
4490 }
4491 PyList_SetItem(l, i, x);
4492 }
4493 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004494#endif
4495}
4496
4497#endif