blob: 09f939ea8e57b8ba22f4f09fc31a8db04b45797b [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
Thomas Wouters8ce81f72007-09-20 18:22:40 +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{
40 register unsigned long tbu, tb, tbu2;
41
42 loop:
43 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;
47
Thomas Wouters477c8d52006-05-27 19:21:47 +000048 /* The slightly peculiar way of writing the next lines is
Michael W. Hudson800ba232004-08-12 18:19:17 +000049 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
52}
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,
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
81{
82 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",
90 opcode, ticked, inst, loop);
91}
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: */
100#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 ***,
116 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 *,
127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Armin Rigo1c2d7e52005-09-20 18:34:01 +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 *,
Armin Rigobf57a142004-03-22 19:24:58 +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 *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000138static PyObject * unicode_concatenate(PyObject *, PyObject *,
139 PyFrameObject *, unsigned char *);
Benjamin Peterson876b2f22009-06-28 03:18:59 +0000140static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000141
Paul Prescode68140d2000-08-30 20:25:01 +0000142#define NAME_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000143 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000144#define GLOBAL_NAME_ERROR_MSG \
145 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define UNBOUNDLOCAL_ERROR_MSG \
Fred Drake661ea262000-10-24 19:57:45 +0000147 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000148#define UNBOUNDFREE_ERROR_MSG \
149 "free variable '%.200s' referenced before assignment" \
150 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Guido van Rossum950361c1997-01-24 13:49:28 +0000152/* Dynamic execution profile */
153#ifdef DYNAMIC_EXECUTION_PROFILE
154#ifdef DXPAIRS
155static long dxpairs[257][256];
156#define dxp dxpairs[256]
157#else
158static long dxp[256];
159#endif
160#endif
161
Jeremy Hylton985eba52003-02-05 23:13:00 +0000162/* Function call profile */
163#ifdef CALL_PROFILE
164#define PCALL_NUM 11
165static int pcall[PCALL_NUM];
166
167#define PCALL_ALL 0
168#define PCALL_FUNCTION 1
169#define PCALL_FAST_FUNCTION 2
170#define PCALL_FASTER_FUNCTION 3
171#define PCALL_METHOD 4
172#define PCALL_BOUND_METHOD 5
173#define PCALL_CFUNCTION 6
174#define PCALL_TYPE 7
175#define PCALL_GENERATOR 8
176#define PCALL_OTHER 9
177#define PCALL_POP 10
178
179/* Notes about the statistics
180
181 PCALL_FAST stats
182
183 FAST_FUNCTION means no argument tuple needs to be created.
184 FASTER_FUNCTION means that the fast-path frame setup code is used.
185
186 If there is a method call where the call can be optimized by changing
187 the argument tuple and calling the function directly, it gets recorded
188 twice.
189
190 As a result, the relationship among the statistics appears to be
191 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
192 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
193 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
194 PCALL_METHOD > PCALL_BOUND_METHOD
195*/
196
197#define PCALL(POS) pcall[POS]++
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
Thomas Wouters89f507f2006-12-13 04:49:30 +0000202 return Py_BuildValue("iiiiiiiiiii",
Jeremy Hylton985eba52003-02-05 23:13:00 +0000203 pcall[0], pcall[1], pcall[2], pcall[3],
204 pcall[4], pcall[5], pcall[6], pcall[7],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000205 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000206}
207#else
208#define PCALL(O)
209
210PyObject *
211PyEval_GetCallStats(PyObject *self)
212{
213 Py_INCREF(Py_None);
214 return Py_None;
215}
216#endif
217
Tim Peters5ca576e2001-06-18 22:08:13 +0000218
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000219/* This can set eval_breaker to 0 even though gil_drop_request became
220 1. We believe this is all right because the eval loop will release
221 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000222#define COMPUTE_EVAL_BREAKER() \
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000223 _Py_atomic_store_relaxed( \
224 &eval_breaker, \
225 _Py_atomic_load_relaxed(&gil_drop_request) | \
226 _Py_atomic_load_relaxed(&pendingcalls_to_do) | \
227 pending_async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000228
229#define SET_GIL_DROP_REQUEST() \
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000230 do { \
231 _Py_atomic_store_relaxed(&gil_drop_request, 1); \
232 _Py_atomic_store_relaxed(&eval_breaker, 1); \
233 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000234
235#define RESET_GIL_DROP_REQUEST() \
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000236 do { \
237 _Py_atomic_store_relaxed(&gil_drop_request, 0); \
238 COMPUTE_EVAL_BREAKER(); \
239 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000240
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000241/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242#define SIGNAL_PENDING_CALLS() \
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000243 do { \
244 _Py_atomic_store_relaxed(&pendingcalls_to_do, 1); \
245 _Py_atomic_store_relaxed(&eval_breaker, 1); \
246 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000247
248#define UNSIGNAL_PENDING_CALLS() \
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000249 do { \
250 _Py_atomic_store_relaxed(&pendingcalls_to_do, 0); \
251 COMPUTE_EVAL_BREAKER(); \
252 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253
254#define SIGNAL_ASYNC_EXC() \
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000255 do { \
256 pending_async_exc = 1; \
257 _Py_atomic_store_relaxed(&eval_breaker, 1); \
258 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000259
260#define UNSIGNAL_ASYNC_EXC() \
261 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
262
263
Guido van Rossume59214e1994-08-30 08:01:59 +0000264#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000265
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000266#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000268#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000269#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000270
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000271static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000272static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273/* This single variable consolidates all requests to break out of the fast path
274 in the eval loop. */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000275static _Py_atomic_int eval_breaker = {0};
276/* Request for dropping the GIL */
277static _Py_atomic_int gil_drop_request = {0};
278/* Request for running pending calls. */
279static _Py_atomic_int pendingcalls_to_do = {0};
280/* Request for looking at the `async_exc` field of the current thread state.
281 Guarded by the GIL. */
282static int pending_async_exc = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000283
284#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000285
Tim Peters7f468f22004-10-11 02:40:51 +0000286int
287PyEval_ThreadsInitialized(void)
288{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000289 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000290}
291
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000292void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000293PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000294{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000295 if (gil_created())
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000296 return;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000297 create_gil();
298 take_gil(PyThreadState_GET());
Guido van Rossum65d5b571998-12-21 19:32:43 +0000299 main_thread = PyThread_get_thread_ident();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000300 if (!pending_lock)
301 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000302}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000303
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000304void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000305PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000306{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000307 PyThreadState *tstate = PyThreadState_GET();
308 if (tstate == NULL)
309 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
310 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000311}
312
313void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000314PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000315{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000316 /* This function must succeed when the current thread state is NULL.
317 We therefore avoid PyThreadState_GET() which dumps a fatal error
318 in debug mode.
319 */
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000320 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
321 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000322}
323
324void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000325PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000326{
327 if (tstate == NULL)
328 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000329 /* Check someone has called PyEval_InitThreads() to create the lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000330 assert(gil_created());
331 take_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000332 if (PyThreadState_Swap(tstate) != NULL)
333 Py_FatalError(
334 "PyEval_AcquireThread: non-NULL old thread state");
335}
336
337void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000338PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000339{
340 if (tstate == NULL)
341 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
342 if (PyThreadState_Swap(NULL) != tstate)
343 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000344 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000345}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000346
347/* This function is called from PyOS_AfterFork to ensure that newly
348 created child processes don't hold locks referring to threads which
349 are not running in the child process. (This could also be done using
350 pthread_atfork mechanism, at least for the pthreads implementation.) */
351
352void
353PyEval_ReInitThreads(void)
354{
Jesse Nollera8513972008-07-17 16:49:17 +0000355 PyObject *threading, *result;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000356 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000357
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000358 if (!gil_created())
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000359 return;
360 /*XXX Can't use PyThread_free_lock here because it does too
361 much error-checking. Doing this cleanly would require
362 adding a new function to each thread_*.h. Instead, just
363 create a new lock and waste a little bit of memory */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000364 recreate_gil();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000365 pending_lock = PyThread_allocate_lock();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000366 take_gil(tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000367 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000368
369 /* Update the threading module with the new state.
370 */
371 tstate = PyThreadState_GET();
372 threading = PyMapping_GetItemString(tstate->interp->modules,
373 "threading");
374 if (threading == NULL) {
375 /* threading not imported */
376 PyErr_Clear();
377 return;
378 }
379 result = PyObject_CallMethod(threading, "_after_fork", NULL);
380 if (result == NULL)
381 PyErr_WriteUnraisable(threading);
382 else
383 Py_DECREF(result);
384 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000385}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000386
387#else
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000388static _Py_atomic_int eval_breaker = {0};
389static _Py_atomic_int gil_drop_request = {0};
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000390static int pending_async_exc = 0;
391#endif /* WITH_THREAD */
392
393/* This function is used to signal that async exceptions are waiting to be
394 raised, therefore it is also useful in non-threaded builds. */
395
396void
397_PyEval_SignalAsyncExc(void)
398{
399 SIGNAL_ASYNC_EXC();
400}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000401
Guido van Rossumff4949e1992-08-05 19:58:53 +0000402/* Functions save_thread and restore_thread are always defined so
403 dynamically loaded modules needn't be compiled separately for use
404 with and without threads: */
405
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000406PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000407PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000408{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000409 PyThreadState *tstate = PyThreadState_Swap(NULL);
410 if (tstate == NULL)
411 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000412#ifdef WITH_THREAD
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000413 if (gil_created())
414 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000415#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000416 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000417}
418
419void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000420PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000421{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000422 if (tstate == NULL)
423 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000424#ifdef WITH_THREAD
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000425 if (gil_created()) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000426 int err = errno;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000427 take_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000428 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000429 }
430#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000431 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000432}
433
434
Guido van Rossuma9672091994-09-14 13:31:22 +0000435/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
436 signal handlers or Mac I/O completion routines) can schedule calls
437 to a function to be called synchronously.
438 The synchronous function is called with one void* argument.
439 It should return 0 for success or -1 for failure -- failure should
440 be accompanied by an exception.
441
442 If registry succeeds, the registry function returns 0; if it fails
443 (e.g. due to too many pending calls) it returns -1 (without setting
444 an exception condition).
445
446 Note that because registry may occur from within signal handlers,
447 or other asynchronous events, calling malloc() is unsafe!
448
449#ifdef WITH_THREAD
450 Any thread can schedule pending calls, but only the main thread
451 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000452 There is no facility to schedule calls to a particular thread, but
453 that should be easy to change, should that ever be required. In
454 that case, the static variables here should go into the python
455 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000456#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000457*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000458
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000459#ifdef WITH_THREAD
460
461/* The WITH_THREAD implementation is thread-safe. It allows
462 scheduling to be made from any thread, and even from an executing
463 callback.
464 */
465
466#define NPENDINGCALLS 32
467static struct {
468 int (*func)(void *);
469 void *arg;
470} pendingcalls[NPENDINGCALLS];
471static int pendingfirst = 0;
472static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000473static char pendingbusy = 0;
474
475int
476Py_AddPendingCall(int (*func)(void *), void *arg)
477{
478 int i, j, result=0;
479 PyThread_type_lock lock = pending_lock;
480
481 /* try a few times for the lock. Since this mechanism is used
482 * for signal handling (on the main thread), there is a (slim)
483 * chance that a signal is delivered on the same thread while we
484 * hold the lock during the Py_MakePendingCalls() function.
485 * This avoids a deadlock in that case.
486 * Note that signals can be delivered on any thread. In particular,
487 * on Windows, a SIGINT is delivered on a system-created worker
488 * thread.
489 * We also check for lock being NULL, in the unlikely case that
490 * this function is called before any bytecode evaluation takes place.
491 */
492 if (lock != NULL) {
493 for (i = 0; i<100; i++) {
494 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
495 break;
496 }
497 if (i == 100)
498 return -1;
499 }
500
501 i = pendinglast;
502 j = (i + 1) % NPENDINGCALLS;
503 if (j == pendingfirst) {
504 result = -1; /* Queue full */
505 } else {
506 pendingcalls[i].func = func;
507 pendingcalls[i].arg = arg;
508 pendinglast = j;
509 }
510 /* signal main loop */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000511 SIGNAL_PENDING_CALLS();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000512 if (lock != NULL)
513 PyThread_release_lock(lock);
514 return result;
515}
516
517int
518Py_MakePendingCalls(void)
519{
520 int i;
521 int r = 0;
522
523 if (!pending_lock) {
524 /* initial allocation of the lock */
525 pending_lock = PyThread_allocate_lock();
526 if (pending_lock == NULL)
527 return -1;
528 }
529
530 /* only service pending calls on main thread */
531 if (main_thread && PyThread_get_thread_ident() != main_thread)
532 return 0;
533 /* don't perform recursive pending calls */
534 if (pendingbusy)
535 return 0;
536 pendingbusy = 1;
537 /* perform a bounded number of calls, in case of recursion */
538 for (i=0; i<NPENDINGCALLS; i++) {
539 int j;
540 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000541 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000542
543 /* pop one item off the queue while holding the lock */
544 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
545 j = pendingfirst;
546 if (j == pendinglast) {
547 func = NULL; /* Queue empty */
548 } else {
549 func = pendingcalls[j].func;
550 arg = pendingcalls[j].arg;
551 pendingfirst = (j + 1) % NPENDINGCALLS;
552 }
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000553 if (pendingfirst != pendinglast)
554 SIGNAL_PENDING_CALLS();
555 else
556 UNSIGNAL_PENDING_CALLS();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000557 PyThread_release_lock(pending_lock);
558 /* having released the lock, perform the callback */
559 if (func == NULL)
560 break;
561 r = func(arg);
562 if (r)
563 break;
564 }
565 pendingbusy = 0;
566 return r;
567}
568
569#else /* if ! defined WITH_THREAD */
570
571/*
572 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
573 This code is used for signal handling in python that isn't built
574 with WITH_THREAD.
575 Don't use this implementation when Py_AddPendingCalls() can happen
576 on a different thread!
577
Guido van Rossuma9672091994-09-14 13:31:22 +0000578 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000579 (1) nested asynchronous calls to Py_AddPendingCall()
580 (2) AddPendingCall() calls made while pending calls are being processed.
581
582 (1) is very unlikely because typically signal delivery
583 is blocked during signal handling. So it should be impossible.
584 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000585 The current code is safe against (2), but not against (1).
586 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000587 thread is present, interrupted by signals, and that the critical
588 section is protected with the "busy" variable. On Windows, which
589 delivers SIGINT on a system thread, this does not hold and therefore
590 Windows really shouldn't use this version.
591 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000592*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000593
Guido van Rossuma9672091994-09-14 13:31:22 +0000594#define NPENDINGCALLS 32
595static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000596 int (*func)(void *);
597 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000598} pendingcalls[NPENDINGCALLS];
599static volatile int pendingfirst = 0;
600static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000601static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000602
603int
Thomas Wouters334fb892000-07-25 12:56:38 +0000604Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000605{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000606 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000607 int i, j;
608 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000609 if (busy)
610 return -1;
611 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000612 i = pendinglast;
613 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000614 if (j == pendingfirst) {
615 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000616 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000617 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000618 pendingcalls[i].func = func;
619 pendingcalls[i].arg = arg;
620 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000621
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000622 SIGNAL_PENDING_CALLS();
Guido van Rossum180d7b41994-09-29 09:45:57 +0000623 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000624 /* XXX End critical section */
625 return 0;
626}
627
Guido van Rossum180d7b41994-09-29 09:45:57 +0000628int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000629Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000630{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000631 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000632 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000633 return 0;
634 busy = 1;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000635 UNSIGNAL_PENDING_CALLS();
Guido van Rossuma9672091994-09-14 13:31:22 +0000636 for (;;) {
637 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000638 int (*func)(void *);
639 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000640 i = pendingfirst;
641 if (i == pendinglast)
642 break; /* Queue empty */
643 func = pendingcalls[i].func;
644 arg = pendingcalls[i].arg;
645 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000646 if (func(arg) < 0) {
647 busy = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000648 SIGNAL_PENDING_CALLS(); /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000649 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000650 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000651 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000652 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000653 return 0;
654}
655
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000656#endif /* WITH_THREAD */
657
Guido van Rossuma9672091994-09-14 13:31:22 +0000658
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000659/* The interpreter's recursion limit */
660
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000661#ifndef Py_DEFAULT_RECURSION_LIMIT
662#define Py_DEFAULT_RECURSION_LIMIT 1000
663#endif
664static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
665int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000666
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000667int
668Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000669{
670 return recursion_limit;
671}
672
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000673void
674Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000675{
676 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000677 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000678}
679
Armin Rigo2b3eb402003-10-28 12:05:48 +0000680/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
681 if the recursion_depth reaches _Py_CheckRecursionLimit.
682 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
683 to guarantee that _Py_CheckRecursiveCall() is regularly called.
684 Without USE_STACKCHECK, there is no need for this. */
685int
686_Py_CheckRecursiveCall(char *where)
687{
688 PyThreadState *tstate = PyThreadState_GET();
689
690#ifdef USE_STACKCHECK
691 if (PyOS_CheckStack()) {
692 --tstate->recursion_depth;
693 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
694 return -1;
695 }
696#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000697 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000698 if (tstate->recursion_critical)
699 /* Somebody asked that we don't check for recursion. */
700 return 0;
701 if (tstate->overflowed) {
702 if (tstate->recursion_depth > recursion_limit + 50) {
703 /* Overflowing while handling an overflow. Give up. */
704 Py_FatalError("Cannot recover from stack overflow.");
705 }
706 return 0;
707 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000708 if (tstate->recursion_depth > recursion_limit) {
709 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000710 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000711 PyErr_Format(PyExc_RuntimeError,
712 "maximum recursion depth exceeded%s",
713 where);
714 return -1;
715 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000716 return 0;
717}
718
Guido van Rossum374a9221991-04-04 10:40:29 +0000719/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000720enum why_code {
721 WHY_NOT = 0x0001, /* No error */
722 WHY_EXCEPTION = 0x0002, /* Exception occurred */
723 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
724 WHY_RETURN = 0x0008, /* 'return' statement */
725 WHY_BREAK = 0x0010, /* 'break' statement */
726 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000727 WHY_YIELD = 0x0040, /* 'yield' operator */
728 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000729};
Guido van Rossum374a9221991-04-04 10:40:29 +0000730
Collin Winter828f04a2007-08-31 00:04:24 +0000731static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000732static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000733
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000734/* Records whether tracing is on for any thread. Counts the number of
735 threads for which tstate->c_tracefunc is non-NULL, so if the value
736 is 0, we know we don't have to check this thread's c_tracefunc.
737 This speeds up the if statement in PyEval_EvalFrameEx() after
738 fast_next_opcode*/
739static int _Py_TracingPossible = 0;
740
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000741
Guido van Rossum374a9221991-04-04 10:40:29 +0000742
Guido van Rossumb209a111997-04-29 18:18:01 +0000743PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000744PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000745{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000747 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000748 (PyObject **)NULL, 0,
749 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000750 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000751 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000752}
753
754
755/* Interpreter main loop */
756
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000757PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000758PyEval_EvalFrame(PyFrameObject *f) {
759 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000760 used this API; core interpreter code should call
761 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000762 return PyEval_EvalFrameEx(f, 0);
763}
764
765PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000766PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000767{
Guido van Rossum950361c1997-01-24 13:49:28 +0000768#ifdef DXPAIRS
769 int lastopcode = 0;
770#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000771 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000772 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000773 register int opcode; /* Current opcode */
774 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000775 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000776 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000777 register PyObject *x; /* Result object -- NULL if error */
778 register PyObject *v; /* Temporary objects popped off stack */
779 register PyObject *w;
780 register PyObject *u;
781 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000782 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000783 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000784 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000785 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000786
Tim Peters8a5c3c72004-04-05 19:36:21 +0000787 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000788
789 not (instr_lb <= current_bytecode_offset < instr_ub)
790
Tim Peters8a5c3c72004-04-05 19:36:21 +0000791 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000792 initial values are such as to make this false the first
793 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000794 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000795
Guido van Rossumd076c731998-10-07 19:42:25 +0000796 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000797 PyObject *names;
798 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000799#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000800 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000801 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000802#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000803
Antoine Pitroub52ec782009-01-25 16:34:23 +0000804/* Computed GOTOs, or
805 the-optimization-commonly-but-improperly-known-as-"threaded code"
806 using gcc's labels-as-values extension
807 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
808
809 The traditional bytecode evaluation loop uses a "switch" statement, which
810 decent compilers will optimize as a single indirect branch instruction
811 combined with a lookup table of jump addresses. However, since the
812 indirect jump instruction is shared by all opcodes, the CPU will have a
813 hard time making the right prediction for where to jump next (actually,
814 it will be always wrong except in the uncommon case of a sequence of
815 several identical opcodes).
816
817 "Threaded code" in contrast, uses an explicit jump table and an explicit
818 indirect jump instruction at the end of each opcode. Since the jump
819 instruction is at a different address for each opcode, the CPU will make a
820 separate prediction for each of these instructions, which is equivalent to
821 predicting the second opcode of each opcode pair. These predictions have
822 a much better chance to turn out valid, especially in small bytecode loops.
823
824 A mispredicted branch on a modern CPU flushes the whole pipeline and
825 can cost several CPU cycles (depending on the pipeline depth),
826 and potentially many more instructions (depending on the pipeline width).
827 A correctly predicted branch, however, is nearly free.
828
829 At the time of this writing, the "threaded code" version is up to 15-20%
830 faster than the normal "switch" version, depending on the compiler and the
831 CPU architecture.
832
833 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
834 because it would render the measurements invalid.
835
836
837 NOTE: care must be taken that the compiler doesn't try to "optimize" the
838 indirect jumps by sharing them between all opcodes. Such optimizations
839 can be disabled on gcc by using the -fno-gcse flag (or possibly
840 -fno-crossjumping).
841*/
842
843#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
844#undef USE_COMPUTED_GOTOS
845#endif
846
847#ifdef USE_COMPUTED_GOTOS
848/* Import the static jump table */
849#include "opcode_targets.h"
850
851/* This macro is used when several opcodes defer to the same implementation
852 (e.g. SETUP_LOOP, SETUP_FINALLY) */
853#define TARGET_WITH_IMPL(op, impl) \
854 TARGET_##op: \
855 opcode = op; \
856 if (HAS_ARG(op)) \
857 oparg = NEXTARG(); \
858 case op: \
859 goto impl; \
860
861#define TARGET(op) \
862 TARGET_##op: \
863 opcode = op; \
864 if (HAS_ARG(op)) \
865 oparg = NEXTARG(); \
866 case op:
867
868
869#define DISPATCH() \
870 { \
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000871 if (!eval_breaker) { \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000872 FAST_DISPATCH(); \
873 } \
874 continue; \
875 }
876
877#ifdef LLTRACE
878#define FAST_DISPATCH() \
879 { \
880 if (!lltrace && !_Py_TracingPossible) { \
881 f->f_lasti = INSTR_OFFSET(); \
882 goto *opcode_targets[*next_instr++]; \
883 } \
884 goto fast_next_opcode; \
885 }
886#else
887#define FAST_DISPATCH() \
888 { \
889 if (!_Py_TracingPossible) { \
890 f->f_lasti = INSTR_OFFSET(); \
891 goto *opcode_targets[*next_instr++]; \
892 } \
893 goto fast_next_opcode; \
894 }
895#endif
896
897#else
898#define TARGET(op) \
899 case op:
900#define TARGET_WITH_IMPL(op, impl) \
901 /* silence compiler warnings about `impl` unused */ \
902 if (0) goto impl; \
903 case op:
904#define DISPATCH() continue
905#define FAST_DISPATCH() goto fast_next_opcode
906#endif
907
908
Neal Norwitza81d2202002-07-14 00:27:26 +0000909/* Tuple access macros */
910
911#ifndef Py_DEBUG
912#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
913#else
914#define GETITEM(v, i) PyTuple_GetItem((v), (i))
915#endif
916
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000917#ifdef WITH_TSC
918/* Use Pentium timestamp counter to mark certain events:
919 inst0 -- beginning of switch statement for opcode dispatch
920 inst1 -- end of switch statement (may be skipped)
921 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000922 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000923 (may be skipped)
924 intr1 -- beginning of long interruption
925 intr2 -- end of long interruption
926
927 Many opcodes call out to helper C functions. In some cases, the
928 time in those functions should be counted towards the time for the
929 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
930 calls another Python function; there's no point in charge all the
931 bytecode executed by the called function to the caller.
932
933 It's hard to make a useful judgement statically. In the presence
934 of operator overloading, it's impossible to tell if a call will
935 execute new Python code or not.
936
937 It's a case-by-case judgement. I'll use intr1 for the following
938 cases:
939
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000940 IMPORT_STAR
941 IMPORT_FROM
942 CALL_FUNCTION (and friends)
943
944 */
945 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
946 int ticked = 0;
947
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000948 READ_TIMESTAMP(inst0);
949 READ_TIMESTAMP(inst1);
950 READ_TIMESTAMP(loop0);
951 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000952
953 /* shut up the compiler */
954 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000955#endif
956
Guido van Rossum374a9221991-04-04 10:40:29 +0000957/* Code access macros */
958
Martin v. Löwis18e16552006-02-15 17:27:45 +0000959#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000960#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000961#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000962#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000963#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000964#define JUMPBY(x) (next_instr += (x))
965
Raymond Hettingerf606f872003-03-16 03:11:04 +0000966/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000967 Some opcodes tend to come in pairs thus making it possible to
968 predict the second code when the first is run. For example,
969 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
970 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000971
Georg Brandl86b2fb92008-07-16 03:43:04 +0000972 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000973 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000974 processor's own internal branch predication has a high likelihood of
975 success, resulting in a nearly zero-overhead transition to the
976 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000977 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000978 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000979 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000980 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000981
Georg Brandl86b2fb92008-07-16 03:43:04 +0000982 If collecting opcode statistics, your choices are to either keep the
983 predictions turned-on and interpret the results as if some opcodes
984 had been combined or turn-off predictions so that the opcode frequency
985 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000986
987 Opcode prediction is disabled with threaded code, since the latter allows
988 the CPU to record separate branch prediction information for each
989 opcode.
990
Raymond Hettingerf606f872003-03-16 03:11:04 +0000991*/
992
Antoine Pitroub52ec782009-01-25 16:34:23 +0000993#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000994#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000995#define PREDICTED(op) PRED_##op:
996#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000997#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000998#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000999#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001000#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +00001001#endif
1002
Raymond Hettingerf606f872003-03-16 03:11:04 +00001003
Guido van Rossum374a9221991-04-04 10:40:29 +00001004/* Stack manipulation macros */
1005
Martin v. Löwis18e16552006-02-15 17:27:45 +00001006/* The stack can grow at most MAXINT deep, as co_nlocals and
1007 co_stacksize are ints. */
1008#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +00001009#define EMPTY() (STACK_LEVEL() == 0)
1010#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +00001011#define SECOND() (stack_pointer[-2])
1012#define THIRD() (stack_pointer[-3])
1013#define FOURTH() (stack_pointer[-4])
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001014#define PEEK(n) (stack_pointer[-(n)])
Raymond Hettinger663004b2003-01-09 15:24:30 +00001015#define SET_TOP(v) (stack_pointer[-1] = (v))
1016#define SET_SECOND(v) (stack_pointer[-2] = (v))
1017#define SET_THIRD(v) (stack_pointer[-3] = (v))
1018#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001019#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +00001020#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +00001021#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1022#define BASIC_POP() (*--stack_pointer)
1023
Guido van Rossum96a42c81992-01-12 02:29:51 +00001024#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +00001025#define PUSH(v) { (void)(BASIC_PUSH(v), \
1026 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001027 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001028#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
1029 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +00001030#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
1031 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001032 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001033#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
1034 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1035 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001036#else
1037#define PUSH(v) BASIC_PUSH(v)
1038#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +00001039#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001040#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001041#endif
1042
Guido van Rossum681d79a1995-07-18 14:51:37 +00001043/* Local variable macros */
1044
1045#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001046
1047/* The SETLOCAL() macro must not DECREF the local variable in-place and
1048 then store the new value; it must copy the old value to a temporary
1049 value, then store the new value, and then DECREF the temporary value.
1050 This is because it is possible that during the DECREF the frame is
1051 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1052 variable would be pointing to already-freed memory. */
1053#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
1054 GETLOCAL(i) = value; \
1055 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001056
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001057
1058#define UNWIND_BLOCK(b) \
1059 while (STACK_LEVEL() > (b)->b_level) { \
1060 PyObject *v = POP(); \
1061 Py_XDECREF(v); \
1062 }
1063
1064#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001065 { \
1066 PyObject *type, *value, *traceback; \
1067 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1068 while (STACK_LEVEL() > (b)->b_level + 3) { \
1069 value = POP(); \
1070 Py_XDECREF(value); \
1071 } \
1072 type = tstate->exc_type; \
1073 value = tstate->exc_value; \
1074 traceback = tstate->exc_traceback; \
1075 tstate->exc_type = POP(); \
1076 tstate->exc_value = POP(); \
1077 tstate->exc_traceback = POP(); \
1078 Py_XDECREF(type); \
1079 Py_XDECREF(value); \
1080 Py_XDECREF(traceback); \
1081 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001082
1083#define SAVE_EXC_STATE() \
1084 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001085 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001086 Py_XINCREF(tstate->exc_type); \
1087 Py_XINCREF(tstate->exc_value); \
1088 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001089 type = f->f_exc_type; \
1090 value = f->f_exc_value; \
1091 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001092 f->f_exc_type = tstate->exc_type; \
1093 f->f_exc_value = tstate->exc_value; \
1094 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001095 Py_XDECREF(type); \
1096 Py_XDECREF(value); \
1097 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001098 }
1099
1100#define SWAP_EXC_STATE() \
1101 { \
1102 PyObject *tmp; \
1103 tmp = tstate->exc_type; \
1104 tstate->exc_type = f->f_exc_type; \
1105 f->f_exc_type = tmp; \
1106 tmp = tstate->exc_value; \
1107 tstate->exc_value = f->f_exc_value; \
1108 f->f_exc_value = tmp; \
1109 tmp = tstate->exc_traceback; \
1110 tstate->exc_traceback = f->f_exc_traceback; \
1111 f->f_exc_traceback = tmp; \
1112 }
1113
Guido van Rossuma027efa1997-05-05 20:56:21 +00001114/* Start of code */
1115
Tim Peters5ca576e2001-06-18 22:08:13 +00001116 if (f == NULL)
1117 return NULL;
1118
Armin Rigo1d313ab2003-10-25 14:33:09 +00001119 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001120 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001121 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001122
Tim Peters5ca576e2001-06-18 22:08:13 +00001123 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001124
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001125 if (tstate->use_tracing) {
1126 if (tstate->c_tracefunc != NULL) {
1127 /* tstate->c_tracefunc, if defined, is a
1128 function that will be called on *every* entry
1129 to a code block. Its return value, if not
1130 None, is a function that will be called at
1131 the start of each executed line of code.
1132 (Actually, the function must return itself
1133 in order to continue tracing.) The trace
1134 functions are called with three arguments:
1135 a pointer to the current frame, a string
1136 indicating why the function is called, and
1137 an argument which depends on the situation.
1138 The global trace function is also called
1139 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001140 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001141 tstate->c_traceobj,
1142 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001143 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001144 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001145 }
1146 }
1147 if (tstate->c_profilefunc != NULL) {
1148 /* Similar for c_profilefunc, except it needn't
1149 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001150 if (call_trace_protected(tstate->c_profilefunc,
1151 tstate->c_profileobj,
1152 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001153 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001154 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001155 }
1156 }
1157 }
1158
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001159 co = f->f_code;
1160 names = co->co_names;
1161 consts = co->co_consts;
1162 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001163 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001164 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001165 /* An explanation is in order for the next line.
1166
1167 f->f_lasti now refers to the index of the last instruction
1168 executed. You might think this was obvious from the name, but
1169 this wasn't always true before 2.3! PyFrame_New now sets
1170 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1171 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001172 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001173
1174 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001175 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001176 prediction effectively links the two codes together as if they
1177 were a single new opcode; accordingly,f->f_lasti will point to
1178 the first code in the pair (for instance, GET_ITER followed by
1179 FOR_ITER is effectively a single opcode and f->f_lasti will point
1180 at to the beginning of the combined pair.)
1181 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001182 next_instr = first_instr + f->f_lasti + 1;
1183 stack_pointer = f->f_stacktop;
1184 assert(stack_pointer != NULL);
1185 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1186
Benjamin Petersonae5f2f42010-03-07 17:10:51 +00001187 if (co->co_flags & CO_GENERATOR && !throwflag) {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001188 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1189 /* We were in an except handler when we left,
1190 restore the exception state which was put aside
1191 (see YIELD_VALUE). */
1192 SWAP_EXC_STATE();
1193 }
1194 else {
1195 SAVE_EXC_STATE();
1196 }
1197 }
1198
Tim Peters5ca576e2001-06-18 22:08:13 +00001199#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001200 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001201#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001202#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001203 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001204#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Guido van Rossum374a9221991-04-04 10:40:29 +00001206 why = WHY_NOT;
1207 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001208 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001209 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001210
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001211 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001212 why = WHY_EXCEPTION;
1213 goto on_error;
1214 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001215
Guido van Rossum374a9221991-04-04 10:40:29 +00001216 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001217#ifdef WITH_TSC
1218 if (inst1 == 0) {
1219 /* Almost surely, the opcode executed a break
1220 or a continue, preventing inst1 from being set
1221 on the way out of the loop.
1222 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001223 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001224 loop1 = inst1;
1225 }
1226 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1227 intr0, intr1);
1228 ticked = 0;
1229 inst1 = 0;
1230 intr0 = 0;
1231 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001232 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001233#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001234 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001235 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001236
Guido van Rossuma027efa1997-05-05 20:56:21 +00001237 /* Do periodic things. Doing this every time through
1238 the loop would add too much overhead, so we do it
1239 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001240 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001241 event needs attention (e.g. a signal handler or
1242 async I/O handler); see Py_AddPendingCall() and
1243 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Jeffrey Yasskin39370832010-05-03 19:29:34 +00001245 if (_Py_atomic_load_relaxed(&eval_breaker)) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001246 if (*next_instr == SETUP_FINALLY) {
1247 /* Make the last opcode before
1248 a try: finally: block uninterruptable. */
1249 goto fast_next_opcode;
1250 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001251 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001252#ifdef WITH_TSC
1253 ticked = 1;
1254#endif
Jeffrey Yasskin39370832010-05-03 19:29:34 +00001255 if (_Py_atomic_load_relaxed(&pendingcalls_to_do)) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001256 if (Py_MakePendingCalls() < 0) {
1257 why = WHY_EXCEPTION;
1258 goto on_error;
1259 }
1260 }
Jeffrey Yasskin39370832010-05-03 19:29:34 +00001261 if (_Py_atomic_load_relaxed(&gil_drop_request)) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001262#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001263 /* Give another thread a chance */
Guido van Rossum25ce5661997-08-02 03:10:38 +00001264 if (PyThreadState_Swap(NULL) != tstate)
1265 Py_FatalError("ceval: tstate mix-up");
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001266 drop_gil(tstate);
1267
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001268 /* Other threads may run now */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001269
1270 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001271 if (PyThreadState_Swap(tstate) != NULL)
1272 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001273#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001274 }
1275 /* Check for asynchronous exceptions. */
1276 if (tstate->async_exc != NULL) {
1277 x = tstate->async_exc;
1278 tstate->async_exc = NULL;
1279 UNSIGNAL_ASYNC_EXC();
1280 PyErr_SetNone(x);
1281 Py_DECREF(x);
1282 why = WHY_EXCEPTION;
1283 goto on_error;
1284 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001285 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001286
Neil Schemenauer63543862002-02-17 19:10:14 +00001287 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001288 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001289
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001290 /* line-by-line tracing support */
1291
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001292 if (_Py_TracingPossible &&
1293 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001294 /* see maybe_call_line_trace
1295 for expository comments */
1296 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001297
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001298 err = maybe_call_line_trace(tstate->c_tracefunc,
1299 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001300 f, &instr_lb, &instr_ub,
1301 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001302 /* Reload possibly changed frame fields */
1303 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001304 if (f->f_stacktop != NULL) {
1305 stack_pointer = f->f_stacktop;
1306 f->f_stacktop = NULL;
1307 }
1308 if (err) {
1309 /* trace function raised an exception */
1310 goto on_error;
1311 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001312 }
1313
1314 /* Extract opcode and argument */
1315
Guido van Rossum374a9221991-04-04 10:40:29 +00001316 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001317 oparg = 0; /* allows oparg to be stored in a register because
1318 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001319 if (HAS_ARG(opcode))
1320 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001321 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001322#ifdef DYNAMIC_EXECUTION_PROFILE
1323#ifdef DXPAIRS
1324 dxpairs[lastopcode][opcode]++;
1325 lastopcode = opcode;
1326#endif
1327 dxp[opcode]++;
1328#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001329
Guido van Rossum96a42c81992-01-12 02:29:51 +00001330#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001331 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Guido van Rossum96a42c81992-01-12 02:29:51 +00001333 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001334 if (HAS_ARG(opcode)) {
1335 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001336 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001337 }
1338 else {
1339 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001340 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001341 }
1342 }
1343#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001344
Guido van Rossum374a9221991-04-04 10:40:29 +00001345 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001346 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001347
Guido van Rossum374a9221991-04-04 10:40:29 +00001348 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001349
Guido van Rossum374a9221991-04-04 10:40:29 +00001350 /* BEWARE!
1351 It is essential that any operation that fails sets either
1352 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1353 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Guido van Rossum374a9221991-04-04 10:40:29 +00001355 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001356
Antoine Pitroub52ec782009-01-25 16:34:23 +00001357 TARGET(NOP)
1358 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001359
Antoine Pitroub52ec782009-01-25 16:34:23 +00001360 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001361 x = GETLOCAL(oparg);
1362 if (x != NULL) {
1363 Py_INCREF(x);
1364 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001365 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001366 }
1367 format_exc_check_arg(PyExc_UnboundLocalError,
1368 UNBOUNDLOCAL_ERROR_MSG,
1369 PyTuple_GetItem(co->co_varnames, oparg));
1370 break;
1371
Antoine Pitroub52ec782009-01-25 16:34:23 +00001372 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001373 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001374 Py_INCREF(x);
1375 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001376 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001377
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001378 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001379 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001380 v = POP();
1381 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001382 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001383
Antoine Pitroub52ec782009-01-25 16:34:23 +00001384 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001385 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001386 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001387 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Antoine Pitroub52ec782009-01-25 16:34:23 +00001389 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001390 v = TOP();
1391 w = SECOND();
1392 SET_TOP(w);
1393 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001394 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001395
Antoine Pitroub52ec782009-01-25 16:34:23 +00001396 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001397 v = TOP();
1398 w = SECOND();
1399 x = THIRD();
1400 SET_TOP(w);
1401 SET_SECOND(x);
1402 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001403 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001404
Antoine Pitroub52ec782009-01-25 16:34:23 +00001405 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001406 u = TOP();
1407 v = SECOND();
1408 w = THIRD();
1409 x = FOURTH();
1410 SET_TOP(v);
1411 SET_SECOND(w);
1412 SET_THIRD(x);
1413 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001414 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Antoine Pitroub52ec782009-01-25 16:34:23 +00001416 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001417 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001418 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001419 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001420 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001421
Antoine Pitroub52ec782009-01-25 16:34:23 +00001422 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001423 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001424 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001425 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001426 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001427 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001428 STACKADJ(2);
1429 SET_TOP(x);
1430 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001431 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001432 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001433 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001434 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001435 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001436 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001437 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001438 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001439 STACKADJ(3);
1440 SET_TOP(x);
1441 SET_SECOND(w);
1442 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001443 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001444 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001445 Py_FatalError("invalid argument to DUP_TOPX"
1446 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001447 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001448 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001449
Antoine Pitroub52ec782009-01-25 16:34:23 +00001450 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001451 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001452 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001453 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001454 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001455 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001456 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001457
Antoine Pitroub52ec782009-01-25 16:34:23 +00001458 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001459 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001460 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001461 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001462 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001463 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001464 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Antoine Pitroub52ec782009-01-25 16:34:23 +00001466 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001467 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001468 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001469 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001470 if (err == 0) {
1471 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001472 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001473 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001474 }
1475 else if (err > 0) {
1476 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001477 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001478 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001479 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001480 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001481 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001482 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001483
Antoine Pitroub52ec782009-01-25 16:34:23 +00001484 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001485 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001486 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001487 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001488 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001489 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001490 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001491
Antoine Pitroub52ec782009-01-25 16:34:23 +00001492 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001493 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001494 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001495 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001496 Py_DECREF(v);
1497 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001498 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001499 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001500 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Antoine Pitroub52ec782009-01-25 16:34:23 +00001502 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001503 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001504 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001505 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001506 Py_DECREF(v);
1507 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001508 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001509 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001510 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Antoine Pitroub52ec782009-01-25 16:34:23 +00001512 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001513 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001514 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001515 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001516 Py_DECREF(v);
1517 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001518 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001519 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001520 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001521
Antoine Pitroub52ec782009-01-25 16:34:23 +00001522 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001523 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001524 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001525 x = PyNumber_FloorDivide(v, w);
1526 Py_DECREF(v);
1527 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001528 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001529 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001530 break;
1531
Antoine Pitroub52ec782009-01-25 16:34:23 +00001532 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001533 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001534 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001535 if (PyUnicode_CheckExact(v))
1536 x = PyUnicode_Format(v, w);
1537 else
1538 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001539 Py_DECREF(v);
1540 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001541 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001542 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001543 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001544
Antoine Pitroub52ec782009-01-25 16:34:23 +00001545 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001546 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001547 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001548 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001549 PyUnicode_CheckExact(w)) {
1550 x = unicode_concatenate(v, w, f, next_instr);
1551 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001552 goto skip_decref_vx;
1553 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001554 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001555 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001556 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001557 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001558 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001559 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001560 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001561 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001562 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001563
Antoine Pitroub52ec782009-01-25 16:34:23 +00001564 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001565 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001566 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001567 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001568 Py_DECREF(v);
1569 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001570 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001571 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001572 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001573
Antoine Pitroub52ec782009-01-25 16:34:23 +00001574 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001575 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001576 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001577 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001578 Py_DECREF(v);
1579 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001580 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001581 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001582 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001583
Antoine Pitroub52ec782009-01-25 16:34:23 +00001584 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001585 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001586 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001587 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001588 Py_DECREF(v);
1589 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001590 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001591 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001592 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001593
Antoine Pitroub52ec782009-01-25 16:34:23 +00001594 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001595 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001596 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001597 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001598 Py_DECREF(v);
1599 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001600 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001601 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001602 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001603
Antoine Pitroub52ec782009-01-25 16:34:23 +00001604 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001605 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001606 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001607 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001608 Py_DECREF(v);
1609 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001610 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001611 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001612 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001613
Antoine Pitroub52ec782009-01-25 16:34:23 +00001614 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001615 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001616 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001617 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001618 Py_DECREF(v);
1619 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001620 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001621 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001622 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001623
Antoine Pitroub52ec782009-01-25 16:34:23 +00001624 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001625 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001626 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001627 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001628 Py_DECREF(v);
1629 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001630 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001631 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001632 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001633
Antoine Pitroub52ec782009-01-25 16:34:23 +00001634 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001635 w = POP();
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001636 v = PEEK(oparg);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001637 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001638 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001639 if (err == 0) {
1640 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001641 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001642 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001643 break;
1644
Antoine Pitroub52ec782009-01-25 16:34:23 +00001645 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001646 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001647 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001648 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001649 Py_DECREF(w);
1650 if (err == 0) {
1651 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001652 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001653 }
1654 break;
1655
Antoine Pitroub52ec782009-01-25 16:34:23 +00001656 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001657 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001658 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001659 x = PyNumber_InPlacePower(v, w, Py_None);
1660 Py_DECREF(v);
1661 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001662 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001663 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001664 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001665
Antoine Pitroub52ec782009-01-25 16:34:23 +00001666 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001667 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001668 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001669 x = PyNumber_InPlaceMultiply(v, w);
1670 Py_DECREF(v);
1671 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001672 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001673 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001674 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001675
Antoine Pitroub52ec782009-01-25 16:34:23 +00001676 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001677 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001678 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001679 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001680 Py_DECREF(v);
1681 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001682 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001683 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001684 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001685
Antoine Pitroub52ec782009-01-25 16:34:23 +00001686 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001687 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001688 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001689 x = PyNumber_InPlaceFloorDivide(v, w);
1690 Py_DECREF(v);
1691 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001692 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001693 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001694 break;
1695
Antoine Pitroub52ec782009-01-25 16:34:23 +00001696 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001697 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001698 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001699 x = PyNumber_InPlaceRemainder(v, w);
1700 Py_DECREF(v);
1701 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001702 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001703 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001704 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001705
Antoine Pitroub52ec782009-01-25 16:34:23 +00001706 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001707 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001708 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001709 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001710 PyUnicode_CheckExact(w)) {
1711 x = unicode_concatenate(v, w, f, next_instr);
1712 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001713 goto skip_decref_v;
1714 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001715 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001716 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001717 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001718 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001719 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001720 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001721 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001722 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001723 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001724
Antoine Pitroub52ec782009-01-25 16:34:23 +00001725 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001726 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001727 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001728 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001729 Py_DECREF(v);
1730 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001731 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001732 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001733 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001734
Antoine Pitroub52ec782009-01-25 16:34:23 +00001735 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001736 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001737 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001738 x = PyNumber_InPlaceLshift(v, w);
1739 Py_DECREF(v);
1740 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001741 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001742 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001743 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001744
Antoine Pitroub52ec782009-01-25 16:34:23 +00001745 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001746 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001747 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001748 x = PyNumber_InPlaceRshift(v, w);
1749 Py_DECREF(v);
1750 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001751 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001752 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001753 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001754
Antoine Pitroub52ec782009-01-25 16:34:23 +00001755 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001756 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001757 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001758 x = PyNumber_InPlaceAnd(v, w);
1759 Py_DECREF(v);
1760 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001761 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001762 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001763 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Antoine Pitroub52ec782009-01-25 16:34:23 +00001765 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001766 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001767 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001768 x = PyNumber_InPlaceXor(v, w);
1769 Py_DECREF(v);
1770 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001771 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001772 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001773 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001774
Antoine Pitroub52ec782009-01-25 16:34:23 +00001775 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001776 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001777 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001778 x = PyNumber_InPlaceOr(v, w);
1779 Py_DECREF(v);
1780 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001781 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001782 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001783 break;
1784
Antoine Pitroub52ec782009-01-25 16:34:23 +00001785 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001786 w = TOP();
1787 v = SECOND();
1788 u = THIRD();
1789 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001790 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001791 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001792 Py_DECREF(u);
1793 Py_DECREF(v);
1794 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001795 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001796 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Antoine Pitroub52ec782009-01-25 16:34:23 +00001798 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001799 w = TOP();
1800 v = SECOND();
1801 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001802 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001803 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001804 Py_DECREF(v);
1805 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001806 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001807 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001808
Antoine Pitroub52ec782009-01-25 16:34:23 +00001809 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001810 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001811 w = PySys_GetObject("displayhook");
1812 if (w == NULL) {
1813 PyErr_SetString(PyExc_RuntimeError,
1814 "lost sys.displayhook");
1815 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001816 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001817 }
1818 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001819 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001820 if (x == NULL)
1821 err = -1;
1822 }
1823 if (err == 0) {
1824 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001825 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001826 if (w == NULL)
1827 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001828 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001829 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001830 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001831 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001832
Thomas Wouters434d0822000-08-24 20:11:32 +00001833#ifdef CASE_TOO_BIG
1834 default: switch (opcode) {
1835#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001836 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001837 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001838 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001839 case 2:
1840 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001841 case 1:
1842 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001843 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001844 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001845 break;
1846 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001847 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001848 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001849 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001850 break;
1851 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001852 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Antoine Pitroub52ec782009-01-25 16:34:23 +00001854 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001855 x = POP();
1856 v = f->f_locals;
1857 Py_XDECREF(v);
1858 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001859 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001860
Antoine Pitroub52ec782009-01-25 16:34:23 +00001861 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001862 retval = POP();
1863 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001864 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001865
Antoine Pitroub52ec782009-01-25 16:34:23 +00001866 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001867 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001868 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001869 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001870 /* Put aside the current exception state and restore
1871 that of the calling frame. This only serves when
1872 "yield" is used inside an except handler. */
1873 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001874 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001875
Antoine Pitroub52ec782009-01-25 16:34:23 +00001876 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001877 {
1878 PyTryBlock *b = PyFrame_BlockPop(f);
1879 if (b->b_type != EXCEPT_HANDLER) {
1880 PyErr_SetString(PyExc_SystemError,
1881 "popped block is not an except handler");
1882 why = WHY_EXCEPTION;
1883 break;
1884 }
1885 UNWIND_EXCEPT_HANDLER(b);
1886 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001887 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001888
Antoine Pitroub52ec782009-01-25 16:34:23 +00001889 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001891 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001892 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001893 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001894 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001895
Christian Heimes180510d2008-03-03 19:15:45 +00001896 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001897 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001898 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001899 if (PyLong_Check(v)) {
1900 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001901 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001902 if (why == WHY_RETURN ||
1903 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001904 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001905 if (why == WHY_SILENCED) {
1906 /* An exception was silenced by 'with', we must
1907 manually unwind the EXCEPT_HANDLER block which was
1908 created when the exception was caught, otherwise
1909 the stack will be in an inconsistent state. */
1910 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersonac8c7302009-06-28 16:03:15 +00001911 assert(b->b_type == EXCEPT_HANDLER);
1912 UNWIND_EXCEPT_HANDLER(b);
1913 why = WHY_NOT;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001914 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001915 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001916 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001917 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001918 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001919 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001921 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001922 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001923 else if (v != Py_None) {
1924 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001925 "'finally' pops bad exception");
1926 why = WHY_EXCEPTION;
1927 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001928 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001929 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Antoine Pitroub52ec782009-01-25 16:34:23 +00001931 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001932 x = PyDict_GetItemString(f->f_builtins,
1933 "__build_class__");
1934 if (x == NULL) {
1935 PyErr_SetString(PyExc_ImportError,
1936 "__build_class__ not found");
1937 break;
1938 }
1939 Py_INCREF(x);
1940 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001941 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Antoine Pitroub52ec782009-01-25 16:34:23 +00001943 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001944 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001946 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001947 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001948 err = PyDict_SetItem(x, w, v);
1949 else
1950 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001951 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001952 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001953 break;
1954 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001955 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001956 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Antoine Pitroub52ec782009-01-25 16:34:23 +00001959 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001960 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001961 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001962 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001963 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001964 NAME_ERROR_MSG,
1965 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001966 break;
1967 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001968 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001969 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001970 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001971
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001972 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001973 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001974 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001975 if (PyTuple_CheckExact(v) &&
1976 PyTuple_GET_SIZE(v) == oparg) {
1977 PyObject **items = \
1978 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001979 while (oparg--) {
1980 w = items[oparg];
1981 Py_INCREF(w);
1982 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001983 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001984 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001985 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001986 } else if (PyList_CheckExact(v) &&
1987 PyList_GET_SIZE(v) == oparg) {
1988 PyObject **items = \
1989 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001990 while (oparg--) {
1991 w = items[oparg];
1992 Py_INCREF(w);
1993 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001994 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001995 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001996 stack_pointer + oparg)) {
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001997 STACKADJ(oparg);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001998 } else {
1999 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00002000 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00002001 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002002 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002003 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002004
Antoine Pitroub52ec782009-01-25 16:34:23 +00002005 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00002006 {
2007 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2008 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002009
Guido van Rossum0368b722007-05-11 16:50:42 +00002010 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
2011 stack_pointer + totalargs)) {
2012 stack_pointer += totalargs;
2013 } else {
2014 why = WHY_EXCEPTION;
2015 }
2016 Py_DECREF(v);
2017 break;
2018 }
2019
Antoine Pitroub52ec782009-01-25 16:34:23 +00002020 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002021 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002022 v = TOP();
2023 u = SECOND();
2024 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00002025 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2026 Py_DECREF(v);
2027 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002028 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002029 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Antoine Pitroub52ec782009-01-25 16:34:23 +00002031 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002032 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002033 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002034 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2035 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00002036 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitroub52ec782009-01-25 16:34:23 +00002039 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002040 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002041 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002042 err = PyDict_SetItem(f->f_globals, w, v);
2043 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002044 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Antoine Pitroub52ec782009-01-25 16:34:23 +00002047 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002048 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002049 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00002050 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002051 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002053
Antoine Pitroub52ec782009-01-25 16:34:23 +00002054 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002055 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002056 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002057 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00002058 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002059 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002060 break;
2061 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002062 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002063 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002064 Py_XINCREF(x);
2065 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002066 else {
2067 x = PyObject_GetItem(v, w);
2068 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002069 if (!PyErr_ExceptionMatches(
2070 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002071 break;
2072 PyErr_Clear();
2073 }
2074 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002075 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002076 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002077 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002078 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002079 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002080 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002081 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002082 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002083 break;
2084 }
2085 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002086 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002087 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002088 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002089 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002090
Antoine Pitroub52ec782009-01-25 16:34:23 +00002091 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002092 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002093 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002094 /* Inline the PyDict_GetItem() calls.
2095 WARNING: this is an extreme speed hack.
2096 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002097 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002098 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002099 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002100 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002101 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002102 e = d->ma_lookup(d, w, hash);
2103 if (e == NULL) {
2104 x = NULL;
2105 break;
2106 }
2107 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002108 if (x != NULL) {
2109 Py_INCREF(x);
2110 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002111 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002112 }
2113 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002114 e = d->ma_lookup(d, w, hash);
2115 if (e == NULL) {
2116 x = NULL;
2117 break;
2118 }
2119 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002120 if (x != NULL) {
2121 Py_INCREF(x);
2122 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002123 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002124 }
2125 goto load_global_error;
2126 }
2127 }
2128 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002129 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002130 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002131 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002132 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002133 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002134 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002135 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002136 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002137 break;
2138 }
2139 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002140 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002141 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002142 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002143
Antoine Pitroub52ec782009-01-25 16:34:23 +00002144 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002145 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002146 if (x != NULL) {
2147 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002148 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002149 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002150 format_exc_check_arg(
2151 PyExc_UnboundLocalError,
2152 UNBOUNDLOCAL_ERROR_MSG,
2153 PyTuple_GetItem(co->co_varnames, oparg)
2154 );
2155 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002156
Antoine Pitroub52ec782009-01-25 16:34:23 +00002157 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002158 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002159 Py_INCREF(x);
2160 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002161 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002162 break;
2163
Antoine Pitroub52ec782009-01-25 16:34:23 +00002164 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002165 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002166 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002167 if (w != NULL) {
2168 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002169 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002170 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002171 err = -1;
2172 /* Don't stomp existing exception */
2173 if (PyErr_Occurred())
2174 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002175 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2176 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002177 oparg);
2178 format_exc_check_arg(
2179 PyExc_UnboundLocalError,
2180 UNBOUNDLOCAL_ERROR_MSG,
2181 v);
2182 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002183 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2184 PyTuple_GET_SIZE(co->co_cellvars));
2185 format_exc_check_arg(PyExc_NameError,
2186 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002187 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002188 break;
2189
Antoine Pitroub52ec782009-01-25 16:34:23 +00002190 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002191 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002192 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002193 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002194 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002195 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002196
Antoine Pitroub52ec782009-01-25 16:34:23 +00002197 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002198 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002199 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002200 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002201 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002202 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002203 }
2204 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002205 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002206 }
2207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002208
Antoine Pitroub52ec782009-01-25 16:34:23 +00002209 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002210 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002211 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002212 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002213 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002214 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002215 }
2216 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002217 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002218 }
2219 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002220
Antoine Pitroub52ec782009-01-25 16:34:23 +00002221 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002222 x = PySet_New(NULL);
2223 if (x != NULL) {
2224 for (; --oparg >= 0;) {
2225 w = POP();
2226 if (err == 0)
2227 err = PySet_Add(x, w);
2228 Py_DECREF(w);
2229 }
2230 if (err != 0) {
2231 Py_DECREF(x);
2232 break;
2233 }
2234 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002235 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002236 }
2237 break;
2238
Antoine Pitroub52ec782009-01-25 16:34:23 +00002239 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002240 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002241 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002242 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002243 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002244
Antoine Pitroub52ec782009-01-25 16:34:23 +00002245 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002246 w = TOP(); /* key */
2247 u = SECOND(); /* value */
2248 v = THIRD(); /* dict */
2249 STACKADJ(-2);
2250 assert (PyDict_CheckExact(v));
2251 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2252 Py_DECREF(u);
2253 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002254 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002255 break;
2256
Antoine Pitroub52ec782009-01-25 16:34:23 +00002257 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002258 w = TOP(); /* key */
2259 u = SECOND(); /* value */
2260 STACKADJ(-2);
2261 v = stack_pointer[-oparg]; /* dict */
2262 assert (PyDict_CheckExact(v));
2263 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2264 Py_DECREF(u);
2265 Py_DECREF(w);
2266 if (err == 0) {
2267 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002268 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002269 }
2270 break;
2271
Antoine Pitroub52ec782009-01-25 16:34:23 +00002272 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002273 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002274 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002275 x = PyObject_GetAttr(v, w);
2276 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002277 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002278 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002279 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002280
Antoine Pitroub52ec782009-01-25 16:34:23 +00002281 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002282 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002283 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002284 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002285 Py_DECREF(v);
2286 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002287 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002288 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002289 PREDICT(POP_JUMP_IF_FALSE);
2290 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002291 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Antoine Pitroub52ec782009-01-25 16:34:23 +00002293 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002294 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002295 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002296 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002297 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002298 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002299 break;
2300 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002301 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002302 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002303 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002304 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002305 w = PyTuple_Pack(5,
2306 w,
2307 f->f_globals,
2308 f->f_locals == NULL ?
2309 Py_None : f->f_locals,
2310 v,
2311 u);
2312 else
2313 w = PyTuple_Pack(4,
2314 w,
2315 f->f_globals,
2316 f->f_locals == NULL ?
2317 Py_None : f->f_locals,
2318 v);
2319 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002320 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002321 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002322 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002323 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002324 x = NULL;
2325 break;
2326 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002327 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002328 v = x;
2329 x = PyEval_CallObject(v, w);
2330 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002331 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002332 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002333 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002334 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002335 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002336
Antoine Pitroub52ec782009-01-25 16:34:23 +00002337 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002338 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002339 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002340 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002341 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002342 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002343 break;
2344 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002345 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002346 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002347 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002348 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002349 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002350 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002351 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002352
Antoine Pitroub52ec782009-01-25 16:34:23 +00002353 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002354 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002355 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002356 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002357 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002358 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002359 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002360 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002361 break;
2362
Antoine Pitroub52ec782009-01-25 16:34:23 +00002363 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002364 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002365 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002366
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002367 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2368 TARGET(POP_JUMP_IF_FALSE)
2369 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002370 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002371 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002372 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002373 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002374 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002375 Py_DECREF(w);
2376 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002377 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002378 }
2379 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002380 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002381 if (err > 0)
2382 err = 0;
2383 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002384 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002385 else
2386 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002387 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002388
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002389 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2390 TARGET(POP_JUMP_IF_TRUE)
2391 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002392 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002393 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002394 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002395 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002396 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002397 Py_DECREF(w);
2398 JUMPTO(oparg);
2399 FAST_DISPATCH();
2400 }
2401 err = PyObject_IsTrue(w);
2402 Py_DECREF(w);
2403 if (err > 0) {
2404 err = 0;
2405 JUMPTO(oparg);
2406 }
2407 else if (err == 0)
2408 ;
2409 else
2410 break;
2411 DISPATCH();
2412
2413 TARGET(JUMP_IF_FALSE_OR_POP)
2414 w = TOP();
2415 if (w == Py_True) {
2416 STACKADJ(-1);
2417 Py_DECREF(w);
2418 FAST_DISPATCH();
2419 }
2420 if (w == Py_False) {
2421 JUMPTO(oparg);
2422 FAST_DISPATCH();
2423 }
2424 err = PyObject_IsTrue(w);
2425 if (err > 0) {
2426 STACKADJ(-1);
2427 Py_DECREF(w);
2428 err = 0;
2429 }
2430 else if (err == 0)
2431 JUMPTO(oparg);
2432 else
2433 break;
2434 DISPATCH();
2435
2436 TARGET(JUMP_IF_TRUE_OR_POP)
2437 w = TOP();
2438 if (w == Py_False) {
2439 STACKADJ(-1);
2440 Py_DECREF(w);
2441 FAST_DISPATCH();
2442 }
2443 if (w == Py_True) {
2444 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002445 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002446 }
2447 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002448 if (err > 0) {
2449 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002450 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002451 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002452 else if (err == 0) {
2453 STACKADJ(-1);
2454 Py_DECREF(w);
2455 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002456 else
2457 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002458 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002459
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002460 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002461 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002462 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002463#if FAST_LOOPS
2464 /* Enabling this path speeds-up all while and for-loops by bypassing
2465 the per-loop checks for signals. By default, this should be turned-off
2466 because it prevents detection of a control-break in tight loops like
2467 "while 1: pass". Compile with this option turned-on when you need
2468 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002469 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002470 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002471 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002472#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002473 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002474#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002475
Antoine Pitroub52ec782009-01-25 16:34:23 +00002476 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002477 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002478 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002479 x = PyObject_GetIter(v);
2480 Py_DECREF(v);
2481 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002482 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002483 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002484 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002485 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002486 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002487 break;
2488
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002489 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002490 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002491 /* before: [iter]; after: [iter, iter()] *or* [] */
2492 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002493 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002494 if (x != NULL) {
2495 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002496 PREDICT(STORE_FAST);
2497 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002498 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002499 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002500 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002501 if (!PyErr_ExceptionMatches(
2502 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002503 break;
2504 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002505 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002506 /* iterator ended normally */
2507 x = v = POP();
2508 Py_DECREF(v);
2509 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002510 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002511
Antoine Pitroub52ec782009-01-25 16:34:23 +00002512 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002513 why = WHY_BREAK;
2514 goto fast_block_end;
2515
Antoine Pitroub52ec782009-01-25 16:34:23 +00002516 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002517 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002518 if (!retval) {
2519 x = NULL;
2520 break;
2521 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002522 why = WHY_CONTINUE;
2523 goto fast_block_end;
2524
Antoine Pitroub52ec782009-01-25 16:34:23 +00002525 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2526 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2527 TARGET(SETUP_FINALLY)
2528 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002529 /* NOTE: If you add any new block-setup opcodes that
2530 are not try/except/finally handlers, you may need
2531 to update the PyGen_NeedsFinalizing() function.
2532 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002533
Guido van Rossumb209a111997-04-29 18:18:01 +00002534 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002535 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002536 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002537
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002538 TARGET(SETUP_WITH)
2539 {
2540 static PyObject *exit, *enter;
2541 w = TOP();
2542 x = special_lookup(w, "__exit__", &exit);
2543 if (!x)
2544 break;
2545 SET_TOP(x);
2546 u = special_lookup(w, "__enter__", &enter);
2547 Py_DECREF(w);
2548 if (!u) {
2549 x = NULL;
2550 break;
2551 }
2552 x = PyObject_CallFunctionObjArgs(u, NULL);
2553 Py_DECREF(u);
2554 if (!x)
2555 break;
2556 /* Setup the finally block before pushing the result
2557 of __enter__ on the stack. */
2558 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2559 STACK_LEVEL());
2560
2561 PUSH(x);
2562 DISPATCH();
2563 }
2564
Antoine Pitroub52ec782009-01-25 16:34:23 +00002565 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002566 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002567 /* At the top of the stack are 1-3 values indicating
2568 how/why we entered the finally clause:
2569 - TOP = None
2570 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2571 - TOP = WHY_*; no retval below it
2572 - (TOP, SECOND, THIRD) = exc_info()
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002573 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002574 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002575 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002576 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002577 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002578 EXIT(None, None, None)
2579
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002580 In the first two cases, we remove EXIT from the
2581 stack, leaving the rest in the same order. In the
2582 third case, we shift the bottom 3 values of the
2583 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002584
2585 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002586 *and* the function call returns a 'true' value, we
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002587 push WHY_SILENCED onto the stack. END_FINALLY will
2588 then not re-raise the exception. (But non-local
2589 gotos should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002590 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002591
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002592 PyObject *exit_func;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002593 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002594 if (u == Py_None) {
Ezio Melottib4d286d2009-10-03 16:14:07 +00002595 (void)POP();
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002596 exit_func = TOP();
2597 SET_TOP(u);
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002598 v = w = Py_None;
2599 }
2600 else if (PyLong_Check(u)) {
Ezio Melottib4d286d2009-10-03 16:14:07 +00002601 (void)POP();
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002602 switch(PyLong_AsLong(u)) {
2603 case WHY_RETURN:
2604 case WHY_CONTINUE:
2605 /* Retval in TOP. */
2606 exit_func = SECOND();
2607 SET_SECOND(TOP());
2608 SET_TOP(u);
2609 break;
2610 default:
2611 exit_func = TOP();
2612 SET_TOP(u);
2613 break;
2614 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002615 u = v = w = Py_None;
2616 }
2617 else {
Benjamin Peterson176101d2009-06-28 15:40:50 +00002618 PyObject *tp, *exc, *tb;
2619 PyTryBlock *block;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002620 v = SECOND();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002621 w = THIRD();
Benjamin Peterson176101d2009-06-28 15:40:50 +00002622 tp = FOURTH();
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002623 exc = PEEK(5);
2624 tb = PEEK(6);
2625 exit_func = PEEK(7);
2626 SET_VALUE(7, tb);
2627 SET_VALUE(6, exc);
2628 SET_VALUE(5, tp);
Benjamin Petersonf8b60b22010-02-11 02:31:04 +00002629 /* UNWIND_EXCEPT_HANDLER will pop this off. */
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002630 SET_FOURTH(NULL);
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002631 /* We just shifted the stack down, so we have
2632 to tell the except handler block that the
2633 values are lower than it expects. */
Benjamin Peterson176101d2009-06-28 15:40:50 +00002634 block = &f->f_blockstack[f->f_iblock - 1];
2635 assert(block->b_type == EXCEPT_HANDLER);
2636 block->b_level--;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002637 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002638 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002639 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2640 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002641 Py_DECREF(exit_func);
2642 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002643 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002644
2645 if (u != Py_None)
2646 err = PyObject_IsTrue(x);
2647 else
2648 err = 0;
2649 Py_DECREF(x);
2650
2651 if (err < 0)
2652 break; /* Go to error exit */
2653 else if (err > 0) {
2654 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002655 /* There was an exception and a True return */
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002656 PUSH(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002657 }
Christian Heimes180510d2008-03-03 19:15:45 +00002658 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002659 break;
2660 }
2661
Antoine Pitroub52ec782009-01-25 16:34:23 +00002662 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002663 {
2664 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002665 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002666 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002667#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002668 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002669#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002670 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002671#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002672 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002673 PUSH(x);
2674 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002675 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002676 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002677 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002678
Antoine Pitroub52ec782009-01-25 16:34:23 +00002679 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2680 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2681 TARGET(CALL_FUNCTION_VAR_KW)
2682 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002683 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002684 int na = oparg & 0xff;
2685 int nk = (oparg>>8) & 0xff;
2686 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002687 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002688 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002689 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002690 if (flags & CALL_FLAG_VAR)
2691 n++;
2692 if (flags & CALL_FLAG_KW)
2693 n++;
2694 pfunc = stack_pointer - n - 1;
2695 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002696
Guido van Rossumac7be682001-01-17 15:42:30 +00002697 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002698 && PyMethod_GET_SELF(func) != NULL) {
2699 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002700 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002701 func = PyMethod_GET_FUNCTION(func);
2702 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002703 Py_DECREF(*pfunc);
2704 *pfunc = self;
2705 na++;
2706 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002707 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002708 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002709 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002710 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002711 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002712 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002713 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002714 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002715
Jeremy Hylton76901512000-03-28 23:49:17 +00002716 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002717 w = POP();
2718 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002719 }
2720 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002721 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002722 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002723 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002724 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002725
Antoine Pitroub52ec782009-01-25 16:34:23 +00002726 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2727 TARGET(MAKE_FUNCTION)
2728 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002729 {
2730 int posdefaults = oparg & 0xff;
2731 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002732 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002733
Guido van Rossum681d79a1995-07-18 14:51:37 +00002734 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002735 x = PyFunction_New(v, f->f_globals);
2736 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002737
Guido van Rossum0240b922007-02-26 21:23:50 +00002738 if (x != NULL && opcode == MAKE_CLOSURE) {
2739 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002740 if (PyFunction_SetClosure(x, v) != 0) {
2741 /* Can't happen unless bytecode is corrupt. */
2742 why = WHY_EXCEPTION;
2743 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002744 Py_DECREF(v);
2745 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002746
2747 if (x != NULL && num_annotations > 0) {
2748 Py_ssize_t name_ix;
2749 u = POP(); /* names of args with annotations */
2750 v = PyDict_New();
2751 if (v == NULL) {
2752 Py_DECREF(x);
2753 x = NULL;
2754 break;
2755 }
2756 name_ix = PyTuple_Size(u);
2757 assert(num_annotations == name_ix+1);
2758 while (name_ix > 0) {
2759 --name_ix;
2760 t = PyTuple_GET_ITEM(u, name_ix);
2761 w = POP();
2762 /* XXX(nnorwitz): check for errors */
2763 PyDict_SetItem(v, t, w);
2764 Py_DECREF(w);
2765 }
2766
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002767 if (PyFunction_SetAnnotations(x, v) != 0) {
2768 /* Can't happen unless
2769 PyFunction_SetAnnotations changes. */
2770 why = WHY_EXCEPTION;
2771 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002772 Py_DECREF(v);
2773 Py_DECREF(u);
2774 }
2775
Guido van Rossum681d79a1995-07-18 14:51:37 +00002776 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002777 if (x != NULL && posdefaults > 0) {
2778 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002779 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002780 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002781 x = NULL;
2782 break;
2783 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002784 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002785 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002786 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002787 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002788 if (PyFunction_SetDefaults(x, v) != 0) {
2789 /* Can't happen unless
2790 PyFunction_SetDefaults changes. */
2791 why = WHY_EXCEPTION;
2792 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002793 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002794 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002795 if (x != NULL && kwdefaults > 0) {
2796 v = PyDict_New();
2797 if (v == NULL) {
2798 Py_DECREF(x);
2799 x = NULL;
2800 break;
2801 }
2802 while (--kwdefaults >= 0) {
2803 w = POP(); /* default value */
2804 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002805 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002806 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002807 Py_DECREF(w);
2808 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002809 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002810 if (PyFunction_SetKwDefaults(x, v) != 0) {
2811 /* Can't happen unless
2812 PyFunction_SetKwDefaults changes. */
2813 why = WHY_EXCEPTION;
2814 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002815 Py_DECREF(v);
2816 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002817 PUSH(x);
2818 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002819 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002820
Antoine Pitroub52ec782009-01-25 16:34:23 +00002821 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002822 if (oparg == 3)
2823 w = POP();
2824 else
2825 w = NULL;
2826 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002827 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002828 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002829 Py_DECREF(u);
2830 Py_DECREF(v);
2831 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002832 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002833 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002834 break;
2835
Antoine Pitroub52ec782009-01-25 16:34:23 +00002836 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002837 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002838 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002839 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002840
Antoine Pitroub52ec782009-01-25 16:34:23 +00002841#ifdef USE_COMPUTED_GOTOS
2842 _unknown_opcode:
2843#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002844 default:
2845 fprintf(stderr,
2846 "XXX lineno: %d, opcode: %d\n",
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002847 PyFrame_GetLineNumber(f),
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002848 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002849 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002850 why = WHY_EXCEPTION;
2851 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002852
2853#ifdef CASE_TOO_BIG
2854 }
2855#endif
2856
Guido van Rossum374a9221991-04-04 10:40:29 +00002857 } /* switch */
2858
2859 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002860
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002861 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002862
Guido van Rossum374a9221991-04-04 10:40:29 +00002863 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002864
Guido van Rossum374a9221991-04-04 10:40:29 +00002865 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002866 if (err == 0 && x != NULL) {
2867#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002868 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002869 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002870 fprintf(stderr,
2871 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002872 else {
2873#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002874 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002875 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002876#ifdef CHECKEXC
2877 }
2878#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002879 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002880 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002881 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002882 err = 0;
2883 }
2884
Guido van Rossum374a9221991-04-04 10:40:29 +00002885 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002886
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002887 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002888 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002889 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002890 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002891 why = WHY_EXCEPTION;
2892 }
2893 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002894#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002895 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002896 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002897 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002898 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002899 sprintf(buf, "Stack unwind with exception "
2900 "set and why=%d", why);
2901 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002902 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002903 }
2904#endif
2905
2906 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002907
Guido van Rossum374a9221991-04-04 10:40:29 +00002908 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002909 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002910
Fred Drake8f51f542001-10-04 14:48:42 +00002911 if (tstate->c_tracefunc != NULL)
2912 call_exc_trace(tstate->c_tracefunc,
2913 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002914 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002915
Guido van Rossum374a9221991-04-04 10:40:29 +00002916 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002917
Guido van Rossum374a9221991-04-04 10:40:29 +00002918 if (why == WHY_RERAISE)
2919 why = WHY_EXCEPTION;
2920
2921 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002922
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002923fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002924 while (why != WHY_NOT && f->f_iblock > 0) {
Alexandre Vassalottibfc30992009-07-21 05:23:51 +00002925 /* Peek at the current block. */
2926 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002927
Tim Peters8a5c3c72004-04-05 19:36:21 +00002928 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002929 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002930 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002931 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002932 Py_DECREF(retval);
2933 break;
2934 }
Alexandre Vassalottibfc30992009-07-21 05:23:51 +00002935 /* Now we have to pop the block. */
2936 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002937
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002938 if (b->b_type == EXCEPT_HANDLER) {
2939 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002940 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002941 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002942 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002943 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2944 why = WHY_NOT;
2945 JUMPTO(b->b_handler);
2946 break;
2947 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002948 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2949 || b->b_type == SETUP_FINALLY)) {
2950 PyObject *exc, *val, *tb;
2951 int handler = b->b_handler;
2952 /* Beware, this invalidates all b->b_* fields */
2953 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2954 PUSH(tstate->exc_traceback);
2955 PUSH(tstate->exc_value);
2956 if (tstate->exc_type != NULL) {
2957 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002958 }
2959 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002960 Py_INCREF(Py_None);
2961 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002962 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002963 PyErr_Fetch(&exc, &val, &tb);
2964 /* Make the raw exception data
2965 available to the handler,
2966 so a program can emulate the
2967 Python main loop. */
2968 PyErr_NormalizeException(
2969 &exc, &val, &tb);
2970 PyException_SetTraceback(val, tb);
2971 Py_INCREF(exc);
2972 tstate->exc_type = exc;
2973 Py_INCREF(val);
2974 tstate->exc_value = val;
2975 tstate->exc_traceback = tb;
2976 if (tb == NULL)
2977 tb = Py_None;
2978 Py_INCREF(tb);
2979 PUSH(tb);
2980 PUSH(val);
2981 PUSH(exc);
2982 why = WHY_NOT;
2983 JUMPTO(handler);
2984 break;
2985 }
2986 if (b->b_type == SETUP_FINALLY) {
2987 if (why & (WHY_RETURN | WHY_CONTINUE))
2988 PUSH(retval);
2989 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002990 why = WHY_NOT;
2991 JUMPTO(b->b_handler);
2992 break;
2993 }
2994 } /* unwind stack */
2995
2996 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002997
Guido van Rossum374a9221991-04-04 10:40:29 +00002998 if (why != WHY_NOT)
2999 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003000 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003001
Guido van Rossum374a9221991-04-04 10:40:29 +00003002 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003003
Tim Peters8a5c3c72004-04-05 19:36:21 +00003004 assert(why != WHY_YIELD);
3005 /* Pop remaining stack entries. */
3006 while (!EMPTY()) {
3007 v = POP();
3008 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00003009 }
3010
Tim Peters8a5c3c72004-04-05 19:36:21 +00003011 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003012 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003013
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003014fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00003015 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00003016 if (tstate->c_tracefunc) {
3017 if (why == WHY_RETURN || why == WHY_YIELD) {
3018 if (call_trace(tstate->c_tracefunc,
3019 tstate->c_traceobj, f,
3020 PyTrace_RETURN, retval)) {
3021 Py_XDECREF(retval);
3022 retval = NULL;
3023 why = WHY_EXCEPTION;
3024 }
3025 }
3026 else if (why == WHY_EXCEPTION) {
3027 call_trace_protected(tstate->c_tracefunc,
3028 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003029 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00003030 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00003031 }
Fred Drake8f51f542001-10-04 14:48:42 +00003032 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00003033 if (why == WHY_EXCEPTION)
3034 call_trace_protected(tstate->c_profilefunc,
3035 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003036 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00003037 else if (call_trace(tstate->c_profilefunc,
3038 tstate->c_profileobj, f,
3039 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00003040 Py_XDECREF(retval);
3041 retval = NULL;
3042 why = WHY_EXCEPTION;
3043 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003044 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00003045 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003046
Tim Peters5ca576e2001-06-18 22:08:13 +00003047 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003048exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00003049 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00003050 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003051
Guido van Rossum96a42c81992-01-12 02:29:51 +00003052 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003053}
3054
Guido van Rossumc2e20742006-02-27 22:32:47 +00003055/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003056 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003057 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003058
Tim Peters6d6c1a32001-08-02 04:15:00 +00003059PyObject *
3060PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00003061 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003062 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003063{
3064 register PyFrameObject *f;
3065 register PyObject *retval = NULL;
3066 register PyObject **fastlocals, **freevars;
3067 PyThreadState *tstate = PyThreadState_GET();
3068 PyObject *x, *u;
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003069 int total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003070
3071 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00003072 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00003073 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00003074 return NULL;
3075 }
3076
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003077 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003078 assert(globals != NULL);
3079 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00003080 if (f == NULL)
3081 return NULL;
3082
3083 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003084 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003085
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003086 if (total_args || co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003087 int i;
3088 int n = argcount;
3089 PyObject *kwdict = NULL;
3090 if (co->co_flags & CO_VARKEYWORDS) {
3091 kwdict = PyDict_New();
3092 if (kwdict == NULL)
3093 goto fail;
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003094 i = total_args;
Tim Peters5ca576e2001-06-18 22:08:13 +00003095 if (co->co_flags & CO_VARARGS)
3096 i++;
3097 SETLOCAL(i, kwdict);
3098 }
3099 if (argcount > co->co_argcount) {
3100 if (!(co->co_flags & CO_VARARGS)) {
3101 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003102 "%U() takes %s %d "
Benjamin Petersona567a772010-03-21 21:00:50 +00003103 "argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003104 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003105 defcount ? "at most" : "exactly",
Benjamin Petersonafcee8b2010-03-21 21:16:24 +00003106 total_args,
3107 total_args == 1 ? "" : "s",
Benjamin Petersona567a772010-03-21 21:00:50 +00003108 argcount + kwcount);
Tim Peters5ca576e2001-06-18 22:08:13 +00003109 goto fail;
3110 }
3111 n = co->co_argcount;
3112 }
3113 for (i = 0; i < n; i++) {
3114 x = args[i];
3115 Py_INCREF(x);
3116 SETLOCAL(i, x);
3117 }
3118 if (co->co_flags & CO_VARARGS) {
3119 u = PyTuple_New(argcount - n);
3120 if (u == NULL)
3121 goto fail;
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003122 SETLOCAL(total_args, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003123 for (i = n; i < argcount; i++) {
3124 x = args[i];
3125 Py_INCREF(x);
3126 PyTuple_SET_ITEM(u, i-n, x);
3127 }
3128 }
3129 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003130 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003131 PyObject *keyword = kws[2*i];
3132 PyObject *value = kws[2*i + 1];
3133 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003134 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003135 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003136 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003137 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003138 goto fail;
3139 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003140 /* Speed hack: do raw pointer compares. As names are
3141 normally interned this should almost always hit. */
Benjamin Petersona567a772010-03-21 21:00:50 +00003142 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003143 for (j = 0; j < total_args; j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003144 PyObject *nm = co_varnames[j];
3145 if (nm == keyword)
3146 goto kw_found;
3147 }
3148 /* Slow fallback, just in case */
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003149 for (j = 0; j < total_args; j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003150 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003151 int cmp = PyObject_RichCompareBool(
3152 keyword, nm, Py_EQ);
3153 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003154 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003155 else if (cmp < 0)
3156 goto fail;
3157 }
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003158 if (j >= total_args && kwdict == NULL) {
3159 PyErr_Format(PyExc_TypeError,
3160 "%U() got an unexpected "
3161 "keyword argument '%S'",
3162 co->co_name,
3163 keyword);
3164 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003165 }
Benjamin Petersona567a772010-03-21 21:00:50 +00003166 PyDict_SetItem(kwdict, keyword, value);
3167 continue;
3168 kw_found:
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003169 if (GETLOCAL(j) != NULL) {
3170 PyErr_Format(PyExc_TypeError,
3171 "%U() got multiple "
3172 "values for keyword "
3173 "argument '%S'",
3174 co->co_name,
3175 keyword);
3176 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003177 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003178 Py_INCREF(value);
3179 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003180 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003181 if (co->co_kwonlyargcount > 0) {
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003182 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Peterson7588b8b2010-03-21 21:22:12 +00003183 PyObject *name;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003184 if (GETLOCAL(i) != NULL)
3185 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003186 name = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Peterson7588b8b2010-03-21 21:22:12 +00003187 if (kwdefs != NULL) {
3188 PyObject *def = PyDict_GetItem(kwdefs, name);
3189 if (def) {
3190 Py_INCREF(def);
3191 SETLOCAL(i, def);
3192 continue;
3193 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003194 }
3195 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003196 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003197 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003198 goto fail;
3199 }
3200 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003201 if (argcount < co->co_argcount) {
3202 int m = co->co_argcount - defcount;
3203 for (i = argcount; i < m; i++) {
3204 if (GETLOCAL(i) == NULL) {
Benjamin Petersona567a772010-03-21 21:00:50 +00003205 int j, given = 0;
3206 for (j = 0; j < co->co_argcount; j++)
3207 if (GETLOCAL(j))
3208 given++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003209 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003210 "%U() takes %s %d "
Benjamin Petersona567a772010-03-21 21:00:50 +00003211 "argument%s "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003212 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003213 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003214 ((co->co_flags & CO_VARARGS) ||
3215 defcount) ? "at least"
3216 : "exactly",
Benjamin Petersona567a772010-03-21 21:00:50 +00003217 m, m == 1 ? "" : "s", given);
Tim Peters5ca576e2001-06-18 22:08:13 +00003218 goto fail;
3219 }
3220 }
3221 if (n > m)
3222 i = n - m;
3223 else
3224 i = 0;
3225 for (; i < defcount; i++) {
3226 if (GETLOCAL(m+i) == NULL) {
3227 PyObject *def = defs[i];
3228 Py_INCREF(def);
3229 SETLOCAL(m+i, def);
3230 }
3231 }
3232 }
3233 }
Benjamin Petersona567a772010-03-21 21:00:50 +00003234 else if (argcount > 0 || kwcount > 0) {
3235 PyErr_Format(PyExc_TypeError,
3236 "%U() takes no arguments (%d given)",
3237 co->co_name,
3238 argcount + kwcount);
3239 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003240 }
3241 /* Allocate and initialize storage for cell vars, and copy free
3242 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003243 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003244 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003245 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003246 PyObject *c;
3247
Benjamin Peterson232ecb82010-03-21 21:08:54 +00003248 nargs = total_args;
Tim Peters5ca576e2001-06-18 22:08:13 +00003249 if (co->co_flags & CO_VARARGS)
3250 nargs++;
3251 if (co->co_flags & CO_VARKEYWORDS)
3252 nargs++;
3253
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254 /* Initialize each cell var, taking into account
3255 cell vars that are initialized from arguments.
3256
3257 Should arrange for the compiler to put cellvars
3258 that are arguments at the beginning of the cellvars
3259 list so that we can march over it more efficiently?
3260 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003261 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003262 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003263 PyTuple_GET_ITEM(co->co_cellvars, i));
3264 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003265 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003266 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003267 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003268 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003269 c = PyCell_New(GETLOCAL(j));
3270 if (c == NULL)
3271 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003272 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003273 found = 1;
3274 break;
3275 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003276 }
3277 if (found == 0) {
3278 c = PyCell_New(NULL);
3279 if (c == NULL)
3280 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003281 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003282 }
3283 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003284 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003285 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003286 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003287 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003288 PyObject *o = PyTuple_GET_ITEM(closure, i);
3289 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003290 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003291 }
3292 }
3293
Tim Peters5ca576e2001-06-18 22:08:13 +00003294 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003295 /* Don't need to keep the reference to f_back, it will be set
3296 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003297 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003298 f->f_back = NULL;
3299
Jeremy Hylton985eba52003-02-05 23:13:00 +00003300 PCALL(PCALL_GENERATOR);
3301
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003302 /* Create a new generator that owns the ready to run frame
3303 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003304 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003305 }
3306
Thomas Woutersce272b62007-09-19 21:19:28 +00003307 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003308
Thomas Woutersce272b62007-09-19 21:19:28 +00003309fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003310
Tim Petersb13680b2001-11-27 23:29:29 +00003311 /* decref'ing the frame can cause __del__ methods to get invoked,
3312 which can call back into Python. While we're done with the
3313 current Python frame (f), the associated C stack is still in use,
3314 so recursion_depth must be boosted for the duration.
3315 */
3316 assert(tstate != NULL);
3317 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003318 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003319 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003320 return retval;
3321}
3322
3323
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003324static PyObject *
3325special_lookup(PyObject *o, char *meth, PyObject **cache)
3326{
3327 PyObject *res;
3328 res = _PyObject_LookupSpecial(o, meth, cache);
3329 if (res == NULL && !PyErr_Occurred()) {
3330 PyErr_SetObject(PyExc_AttributeError, *cache);
3331 return NULL;
3332 }
3333 return res;
3334}
3335
3336
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003337/* Logic for the raise statement (too complicated for inlining).
3338 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003339static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003340do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003341{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003342 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003343
3344 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003345 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003346 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003347 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003348 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003349 value = tstate->exc_value;
3350 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003351 if (type == Py_None) {
3352 PyErr_SetString(PyExc_RuntimeError,
3353 "No active exception to reraise");
3354 return WHY_EXCEPTION;
3355 }
3356 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003357 Py_XINCREF(value);
3358 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003359 PyErr_Restore(type, value, tb);
3360 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003362
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003363 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003364 raise
3365 raise <instance>
3366 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003367
Collin Winter828f04a2007-08-31 00:04:24 +00003368 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003369 type = exc;
3370 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003371 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003372 goto raise_error;
3373 }
Collin Winter828f04a2007-08-31 00:04:24 +00003374 else if (PyExceptionInstance_Check(exc)) {
3375 value = exc;
3376 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003377 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003378 }
3379 else {
3380 /* Not something you can raise. You get an exception
3381 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003382 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003383 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003384 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003385 goto raise_error;
3386 }
Collin Winter828f04a2007-08-31 00:04:24 +00003387
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003388 if (cause) {
3389 PyObject *fixed_cause;
3390 if (PyExceptionClass_Check(cause)) {
3391 fixed_cause = PyObject_CallObject(cause, NULL);
3392 if (fixed_cause == NULL)
3393 goto raise_error;
3394 Py_DECREF(cause);
3395 }
3396 else if (PyExceptionInstance_Check(cause)) {
3397 fixed_cause = cause;
3398 }
3399 else {
3400 PyErr_SetString(PyExc_TypeError,
3401 "exception causes must derive from "
3402 "BaseException");
3403 goto raise_error;
3404 }
3405 PyException_SetCause(value, fixed_cause);
3406 }
Collin Winter828f04a2007-08-31 00:04:24 +00003407
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003408 PyErr_SetObject(type, value);
3409 /* PyErr_SetObject incref's its arguments */
3410 Py_XDECREF(value);
3411 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003412 return WHY_EXCEPTION;
3413
3414raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003415 Py_XDECREF(value);
3416 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003417 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003418 return WHY_EXCEPTION;
3419}
3420
Tim Petersd6d010b2001-06-21 02:49:55 +00003421/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003422 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003423
Guido van Rossum0368b722007-05-11 16:50:42 +00003424 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3425 with a variable target.
3426*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003427
Barry Warsawe42b18f1997-08-25 22:13:04 +00003428static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003429unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003430{
Guido van Rossum0368b722007-05-11 16:50:42 +00003431 int i = 0, j = 0;
3432 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003433 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003434 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003435 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003436
Tim Petersd6d010b2001-06-21 02:49:55 +00003437 assert(v != NULL);
3438
3439 it = PyObject_GetIter(v);
3440 if (it == NULL)
3441 goto Error;
3442
3443 for (; i < argcnt; i++) {
3444 w = PyIter_Next(it);
3445 if (w == NULL) {
3446 /* Iterator done, via error or exhaustion. */
3447 if (!PyErr_Occurred()) {
3448 PyErr_Format(PyExc_ValueError,
3449 "need more than %d value%s to unpack",
3450 i, i == 1 ? "" : "s");
3451 }
3452 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003453 }
3454 *--sp = w;
3455 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003456
Guido van Rossum0368b722007-05-11 16:50:42 +00003457 if (argcntafter == -1) {
3458 /* We better have exhausted the iterator now. */
3459 w = PyIter_Next(it);
3460 if (w == NULL) {
3461 if (PyErr_Occurred())
3462 goto Error;
3463 Py_DECREF(it);
3464 return 1;
3465 }
3466 Py_DECREF(w);
3467 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3468 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003469 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003470
3471 l = PySequence_List(it);
3472 if (l == NULL)
3473 goto Error;
3474 *--sp = l;
3475 i++;
3476
3477 ll = PyList_GET_SIZE(l);
3478 if (ll < argcntafter) {
3479 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3480 argcnt + ll);
3481 goto Error;
3482 }
3483
3484 /* Pop the "after-variable" args off the list. */
3485 for (j = argcntafter; j > 0; j--, i++) {
3486 *--sp = PyList_GET_ITEM(l, ll - j);
3487 }
3488 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003489 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003490 Py_DECREF(it);
3491 return 1;
3492
Tim Petersd6d010b2001-06-21 02:49:55 +00003493Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003494 for (; i > 0; i--, sp++)
3495 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003496 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003497 return 0;
3498}
3499
3500
Guido van Rossum96a42c81992-01-12 02:29:51 +00003501#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003502static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003503prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003504{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003505 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003506 if (PyObject_Print(v, stdout, 0) != 0)
3507 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003508 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003509 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003510}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003511#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003512
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003513static void
Fred Drake5755ce62001-06-27 19:19:46 +00003514call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003515{
Guido van Rossumb209a111997-04-29 18:18:01 +00003516 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003517 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003518 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003519 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003520 value = Py_None;
3521 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003522 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003523 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003524 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003525 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003526 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003527 }
Fred Drake5755ce62001-06-27 19:19:46 +00003528 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003529 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003530 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003531 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003532 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003533 Py_XDECREF(type);
3534 Py_XDECREF(value);
3535 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003536 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003537}
3538
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003539static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003540call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003541 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003542{
3543 PyObject *type, *value, *traceback;
3544 int err;
3545 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003546 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003547 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003548 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003549 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003550 return 0;
3551 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003552 else {
3553 Py_XDECREF(type);
3554 Py_XDECREF(value);
3555 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003556 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003557 }
3558}
3559
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003560static int
Fred Drake5755ce62001-06-27 19:19:46 +00003561call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3562 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003563{
Fred Drake5755ce62001-06-27 19:19:46 +00003564 register PyThreadState *tstate = frame->f_tstate;
3565 int result;
3566 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003567 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003568 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003569 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003570 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003571 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3572 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003573 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003574 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003575}
3576
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003577PyObject *
3578_PyEval_CallTracing(PyObject *func, PyObject *args)
3579{
3580 PyFrameObject *frame = PyEval_GetFrame();
3581 PyThreadState *tstate = frame->f_tstate;
3582 int save_tracing = tstate->tracing;
3583 int save_use_tracing = tstate->use_tracing;
3584 PyObject *result;
3585
3586 tstate->tracing = 0;
3587 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3588 || (tstate->c_profilefunc != NULL));
3589 result = PyObject_Call(func, args, NULL);
3590 tstate->tracing = save_tracing;
3591 tstate->use_tracing = save_use_tracing;
3592 return result;
3593}
3594
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003595/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003596static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003597maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003598 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3599 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003600{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003601 int result = 0;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003602 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003603
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003604 /* If the last instruction executed isn't in the current
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003605 instruction window, reset the window.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003606 */
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003607 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003608 PyAddrPair bounds;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003609 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3610 &bounds);
Thomas Woutersce272b62007-09-19 21:19:28 +00003611 *instr_lb = bounds.ap_lower;
3612 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003613 }
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003614 /* If the last instruction falls at the start of a line or if
3615 it represents a jump backwards, update the frame's line
3616 number and call the trace function. */
3617 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3618 frame->f_lineno = line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003619 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003620 }
3621 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003622 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003623}
3624
Fred Drake5755ce62001-06-27 19:19:46 +00003625void
3626PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003627{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003628 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003629 PyObject *temp = tstate->c_profileobj;
3630 Py_XINCREF(arg);
3631 tstate->c_profilefunc = NULL;
3632 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003633 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003634 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003635 Py_XDECREF(temp);
3636 tstate->c_profilefunc = func;
3637 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003638 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003639 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003640}
3641
3642void
3643PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3644{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003645 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003646 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003647 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003648 Py_XINCREF(arg);
3649 tstate->c_tracefunc = NULL;
3650 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003651 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003652 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003653 Py_XDECREF(temp);
3654 tstate->c_tracefunc = func;
3655 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003656 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003657 tstate->use_tracing = ((func != NULL)
3658 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003659}
3660
Guido van Rossumb209a111997-04-29 18:18:01 +00003661PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003662PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003663{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003664 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003665 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003666 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003667 else
3668 return current_frame->f_builtins;
3669}
3670
Guido van Rossumb209a111997-04-29 18:18:01 +00003671PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003672PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003673{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003674 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003675 if (current_frame == NULL)
3676 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003677 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003678 return current_frame->f_locals;
3679}
3680
Guido van Rossumb209a111997-04-29 18:18:01 +00003681PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003682PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003683{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003684 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003685 if (current_frame == NULL)
3686 return NULL;
3687 else
3688 return current_frame->f_globals;
3689}
3690
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003691PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003692PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003693{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003694 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003695 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003696}
3697
Guido van Rossum6135a871995-01-09 17:53:26 +00003698int
Tim Peters5ba58662001-07-16 02:29:45 +00003699PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003700{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003701 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003702 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003703
3704 if (current_frame != NULL) {
3705 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003706 const int compilerflags = codeflags & PyCF_MASK;
3707 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003708 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003709 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003710 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003711#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003712 if (codeflags & CO_GENERATOR_ALLOWED) {
3713 result = 1;
3714 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3715 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003716#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003717 }
3718 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003719}
3720
Guido van Rossum3f5da241990-12-20 15:06:42 +00003721
Guido van Rossum681d79a1995-07-18 14:51:37 +00003722/* External interface to call any callable object.
Antoine Pitrou8689a102010-04-01 16:53:15 +00003723 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003724
Guido van Rossumb209a111997-04-29 18:18:01 +00003725PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003726PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003727{
Jeremy Hylton52820442001-01-03 23:52:36 +00003728 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003729
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003730 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003731 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003732 if (arg == NULL)
3733 return NULL;
3734 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003735 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003736 PyErr_SetString(PyExc_TypeError,
3737 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003738 return NULL;
3739 }
3740 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003741 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003742
Guido van Rossumb209a111997-04-29 18:18:01 +00003743 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003744 PyErr_SetString(PyExc_TypeError,
3745 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003746 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003747 return NULL;
3748 }
3749
Tim Peters6d6c1a32001-08-02 04:15:00 +00003750 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003751 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003752 return result;
3753}
3754
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003755const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003756PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003757{
3758 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003759 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003760 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003761 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003762 else if (PyCFunction_Check(func))
3763 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003764 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003765 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003766}
3767
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003768const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003769PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003770{
3771 if (PyMethod_Check(func))
3772 return "()";
3773 else if (PyFunction_Check(func))
3774 return "()";
3775 else if (PyCFunction_Check(func))
3776 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003777 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003778 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003779}
3780
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003781static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003782err_args(PyObject *func, int flags, int nargs)
3783{
3784 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003785 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003786 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003787 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003788 nargs);
3789 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003790 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003791 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003792 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003793 nargs);
3794}
3795
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003796#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003797if (tstate->use_tracing && tstate->c_profilefunc) { \
3798 if (call_trace(tstate->c_profilefunc, \
3799 tstate->c_profileobj, \
3800 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003801 func)) { \
3802 x = NULL; \
3803 } \
3804 else { \
3805 x = call; \
3806 if (tstate->c_profilefunc != NULL) { \
3807 if (x == NULL) { \
3808 call_trace_protected(tstate->c_profilefunc, \
3809 tstate->c_profileobj, \
3810 tstate->frame, PyTrace_C_EXCEPTION, \
3811 func); \
3812 /* XXX should pass (type, value, tb) */ \
3813 } else { \
3814 if (call_trace(tstate->c_profilefunc, \
3815 tstate->c_profileobj, \
3816 tstate->frame, PyTrace_C_RETURN, \
3817 func)) { \
3818 Py_DECREF(x); \
3819 x = NULL; \
3820 } \
3821 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003822 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003823 } \
3824} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003825 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003826 }
3827
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003828static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003829call_function(PyObject ***pp_stack, int oparg
3830#ifdef WITH_TSC
3831 , uint64* pintr0, uint64* pintr1
3832#endif
3833 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003834{
3835 int na = oparg & 0xff;
3836 int nk = (oparg>>8) & 0xff;
3837 int n = na + 2 * nk;
3838 PyObject **pfunc = (*pp_stack) - n - 1;
3839 PyObject *func = *pfunc;
3840 PyObject *x, *w;
3841
Jeremy Hylton985eba52003-02-05 23:13:00 +00003842 /* Always dispatch PyCFunction first, because these are
3843 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003844 */
3845 if (PyCFunction_Check(func) && nk == 0) {
3846 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003847 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003848
3849 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003850 if (flags & (METH_NOARGS | METH_O)) {
3851 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3852 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003853 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003854 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003855 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003856 else if (flags & METH_O && na == 1) {
3857 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003858 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003859 Py_DECREF(arg);
3860 }
3861 else {
3862 err_args(func, flags, na);
3863 x = NULL;
3864 }
3865 }
3866 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003867 PyObject *callargs;
3868 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003869 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003870 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003871 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003872 Py_XDECREF(callargs);
3873 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003874 } else {
3875 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3876 /* optimize access to bound methods */
3877 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003878 PCALL(PCALL_METHOD);
3879 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003880 Py_INCREF(self);
3881 func = PyMethod_GET_FUNCTION(func);
3882 Py_INCREF(func);
3883 Py_DECREF(*pfunc);
3884 *pfunc = self;
3885 na++;
3886 n++;
3887 } else
3888 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003889 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003890 if (PyFunction_Check(func))
3891 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003892 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003893 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003894 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003895 Py_DECREF(func);
3896 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003897
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003898 /* Clear the stack of the function object. Also removes
3899 the arguments in case they weren't consumed already
3900 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003901 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003902 while ((*pp_stack) > pfunc) {
3903 w = EXT_POP(*pp_stack);
3904 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003905 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003906 }
3907 return x;
3908}
3909
Jeremy Hylton192690e2002-08-16 18:36:11 +00003910/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003911 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003912 For the simplest case -- a function that takes only positional
3913 arguments and is called with only positional arguments -- it
3914 inlines the most primitive frame setup code from
3915 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3916 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003917*/
3918
3919static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003920fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003921{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003922 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003923 PyObject *globals = PyFunction_GET_GLOBALS(func);
3924 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003925 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003926 PyObject **d = NULL;
3927 int nd = 0;
3928
Jeremy Hylton985eba52003-02-05 23:13:00 +00003929 PCALL(PCALL_FUNCTION);
3930 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003931 if (argdefs == NULL && co->co_argcount == n &&
3932 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003933 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3934 PyFrameObject *f;
3935 PyObject *retval = NULL;
3936 PyThreadState *tstate = PyThreadState_GET();
3937 PyObject **fastlocals, **stack;
3938 int i;
3939
3940 PCALL(PCALL_FASTER_FUNCTION);
3941 assert(globals != NULL);
3942 /* XXX Perhaps we should create a specialized
3943 PyFrame_New() that doesn't take locals, but does
3944 take builtins without sanity checking them.
3945 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003946 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003947 f = PyFrame_New(tstate, co, globals, NULL);
3948 if (f == NULL)
3949 return NULL;
3950
3951 fastlocals = f->f_localsplus;
3952 stack = (*pp_stack) - n;
3953
3954 for (i = 0; i < n; i++) {
3955 Py_INCREF(*stack);
3956 fastlocals[i] = *stack++;
3957 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003958 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003959 ++tstate->recursion_depth;
3960 Py_DECREF(f);
3961 --tstate->recursion_depth;
3962 return retval;
3963 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003964 if (argdefs != NULL) {
3965 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003966 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003967 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003968 return PyEval_EvalCodeEx(co, globals,
3969 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003970 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003971 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003972}
3973
3974static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003975update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3976 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003977{
3978 PyObject *kwdict = NULL;
3979 if (orig_kwdict == NULL)
3980 kwdict = PyDict_New();
3981 else {
3982 kwdict = PyDict_Copy(orig_kwdict);
3983 Py_DECREF(orig_kwdict);
3984 }
3985 if (kwdict == NULL)
3986 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003987 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003988 int err;
3989 PyObject *value = EXT_POP(*pp_stack);
3990 PyObject *key = EXT_POP(*pp_stack);
3991 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003992 PyErr_Format(PyExc_TypeError,
3993 "%.200s%s got multiple values "
Victor Stinner3f1af5c2010-03-12 17:00:41 +00003994 "for keyword argument '%U'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003995 PyEval_GetFuncName(func),
3996 PyEval_GetFuncDesc(func),
Victor Stinner3f1af5c2010-03-12 17:00:41 +00003997 key);
Jeremy Hylton52820442001-01-03 23:52:36 +00003998 Py_DECREF(key);
3999 Py_DECREF(value);
4000 Py_DECREF(kwdict);
4001 return NULL;
4002 }
4003 err = PyDict_SetItem(kwdict, key, value);
4004 Py_DECREF(key);
4005 Py_DECREF(value);
4006 if (err) {
4007 Py_DECREF(kwdict);
4008 return NULL;
4009 }
4010 }
4011 return kwdict;
4012}
4013
4014static PyObject *
4015update_star_args(int nstack, int nstar, PyObject *stararg,
4016 PyObject ***pp_stack)
4017{
4018 PyObject *callargs, *w;
4019
4020 callargs = PyTuple_New(nstack + nstar);
4021 if (callargs == NULL) {
4022 return NULL;
4023 }
4024 if (nstar) {
4025 int i;
4026 for (i = 0; i < nstar; i++) {
4027 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4028 Py_INCREF(a);
4029 PyTuple_SET_ITEM(callargs, nstack + i, a);
4030 }
4031 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004032 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004033 w = EXT_POP(*pp_stack);
4034 PyTuple_SET_ITEM(callargs, nstack, w);
4035 }
4036 return callargs;
4037}
4038
4039static PyObject *
4040load_args(PyObject ***pp_stack, int na)
4041{
4042 PyObject *args = PyTuple_New(na);
4043 PyObject *w;
4044
4045 if (args == NULL)
4046 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004047 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004048 w = EXT_POP(*pp_stack);
4049 PyTuple_SET_ITEM(args, na, w);
4050 }
4051 return args;
4052}
4053
4054static PyObject *
4055do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4056{
4057 PyObject *callargs = NULL;
4058 PyObject *kwdict = NULL;
4059 PyObject *result = NULL;
4060
4061 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004062 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004063 if (kwdict == NULL)
4064 goto call_fail;
4065 }
4066 callargs = load_args(pp_stack, na);
4067 if (callargs == NULL)
4068 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004069#ifdef CALL_PROFILE
4070 /* At this point, we have to look at the type of func to
4071 update the call stats properly. Do it here so as to avoid
4072 exposing the call stats machinery outside ceval.c
4073 */
4074 if (PyFunction_Check(func))
4075 PCALL(PCALL_FUNCTION);
4076 else if (PyMethod_Check(func))
4077 PCALL(PCALL_METHOD);
4078 else if (PyType_Check(func))
4079 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004080 else if (PyCFunction_Check(func))
4081 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004082 else
4083 PCALL(PCALL_OTHER);
4084#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004085 if (PyCFunction_Check(func)) {
4086 PyThreadState *tstate = PyThreadState_GET();
4087 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4088 }
4089 else
4090 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004091call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004092 Py_XDECREF(callargs);
4093 Py_XDECREF(kwdict);
4094 return result;
4095}
4096
4097static PyObject *
4098ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4099{
4100 int nstar = 0;
4101 PyObject *callargs = NULL;
4102 PyObject *stararg = NULL;
4103 PyObject *kwdict = NULL;
4104 PyObject *result = NULL;
4105
4106 if (flags & CALL_FLAG_KW) {
4107 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004108 if (!PyDict_Check(kwdict)) {
4109 PyObject *d;
4110 d = PyDict_New();
4111 if (d == NULL)
4112 goto ext_call_fail;
4113 if (PyDict_Update(d, kwdict) != 0) {
4114 Py_DECREF(d);
4115 /* PyDict_Update raises attribute
4116 * error (percolated from an attempt
4117 * to get 'keys' attribute) instead of
4118 * a type error if its second argument
4119 * is not a mapping.
4120 */
4121 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4122 PyErr_Format(PyExc_TypeError,
4123 "%.200s%.200s argument after ** "
4124 "must be a mapping, not %.200s",
4125 PyEval_GetFuncName(func),
4126 PyEval_GetFuncDesc(func),
4127 kwdict->ob_type->tp_name);
4128 }
4129 goto ext_call_fail;
4130 }
4131 Py_DECREF(kwdict);
4132 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004133 }
4134 }
4135 if (flags & CALL_FLAG_VAR) {
4136 stararg = EXT_POP(*pp_stack);
4137 if (!PyTuple_Check(stararg)) {
4138 PyObject *t = NULL;
4139 t = PySequence_Tuple(stararg);
4140 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004141 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4142 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004143 "%.200s%.200s argument after * "
4144 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004145 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004146 PyEval_GetFuncDesc(func),
4147 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004148 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004149 goto ext_call_fail;
4150 }
4151 Py_DECREF(stararg);
4152 stararg = t;
4153 }
4154 nstar = PyTuple_GET_SIZE(stararg);
4155 }
4156 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004157 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004158 if (kwdict == NULL)
4159 goto ext_call_fail;
4160 }
4161 callargs = update_star_args(na, nstar, stararg, pp_stack);
4162 if (callargs == NULL)
4163 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004164#ifdef CALL_PROFILE
4165 /* At this point, we have to look at the type of func to
4166 update the call stats properly. Do it here so as to avoid
4167 exposing the call stats machinery outside ceval.c
4168 */
4169 if (PyFunction_Check(func))
4170 PCALL(PCALL_FUNCTION);
4171 else if (PyMethod_Check(func))
4172 PCALL(PCALL_METHOD);
4173 else if (PyType_Check(func))
4174 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004175 else if (PyCFunction_Check(func))
4176 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004177 else
4178 PCALL(PCALL_OTHER);
4179#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004180 if (PyCFunction_Check(func)) {
4181 PyThreadState *tstate = PyThreadState_GET();
4182 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4183 }
4184 else
4185 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004186ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004187 Py_XDECREF(callargs);
4188 Py_XDECREF(kwdict);
4189 Py_XDECREF(stararg);
4190 return result;
4191}
4192
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004193/* Extract a slice index from a PyInt or PyLong or an object with the
4194 nb_index slot defined, and store in *pi.
4195 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4196 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 +00004197 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004198*/
Tim Petersb5196382001-12-16 19:44:20 +00004199/* Note: If v is NULL, return success without storing into *pi. This
4200 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4201 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004202*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004203int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004204_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004205{
Tim Petersb5196382001-12-16 19:44:20 +00004206 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004207 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004208 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004209 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004210 if (x == -1 && PyErr_Occurred())
4211 return 0;
4212 }
4213 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004214 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004215 "slice indices must be integers or "
4216 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004217 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004218 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004219 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004220 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004221 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004222}
4223
Guido van Rossum486364b2007-06-30 05:01:58 +00004224#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004225 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004226
Guido van Rossumb209a111997-04-29 18:18:01 +00004227static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004228cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004229{
Guido van Rossumac7be682001-01-17 15:42:30 +00004230 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004231 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004232 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004233 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004234 break;
4235 case PyCmp_IS_NOT:
4236 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004237 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004238 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004239 res = PySequence_Contains(w, v);
4240 if (res < 0)
4241 return NULL;
4242 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004243 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004244 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004245 if (res < 0)
4246 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004247 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004248 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004249 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004250 if (PyTuple_Check(w)) {
4251 Py_ssize_t i, length;
4252 length = PyTuple_Size(w);
4253 for (i = 0; i < length; i += 1) {
4254 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004255 if (!PyExceptionClass_Check(exc)) {
4256 PyErr_SetString(PyExc_TypeError,
4257 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004258 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004259 }
4260 }
4261 }
4262 else {
Brett Cannon39590462007-02-26 22:01:14 +00004263 if (!PyExceptionClass_Check(w)) {
4264 PyErr_SetString(PyExc_TypeError,
4265 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004266 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004267 }
4268 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004269 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004270 break;
4271 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004272 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004273 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004274 v = res ? Py_True : Py_False;
4275 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004276 return v;
4277}
4278
Thomas Wouters52152252000-08-17 22:55:00 +00004279static PyObject *
4280import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004281{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004282 PyObject *x;
4283
4284 x = PyObject_GetAttr(v, name);
4285 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004286 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004287 }
Thomas Wouters52152252000-08-17 22:55:00 +00004288 return x;
4289}
Guido van Rossumac7be682001-01-17 15:42:30 +00004290
Thomas Wouters52152252000-08-17 22:55:00 +00004291static int
4292import_all_from(PyObject *locals, PyObject *v)
4293{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004294 PyObject *all = PyObject_GetAttrString(v, "__all__");
4295 PyObject *dict, *name, *value;
4296 int skip_leading_underscores = 0;
4297 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004298
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004299 if (all == NULL) {
4300 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4301 return -1; /* Unexpected error */
4302 PyErr_Clear();
4303 dict = PyObject_GetAttrString(v, "__dict__");
4304 if (dict == NULL) {
4305 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4306 return -1;
4307 PyErr_SetString(PyExc_ImportError,
4308 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004309 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004310 }
4311 all = PyMapping_Keys(dict);
4312 Py_DECREF(dict);
4313 if (all == NULL)
4314 return -1;
4315 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004316 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004317
4318 for (pos = 0, err = 0; ; pos++) {
4319 name = PySequence_GetItem(all, pos);
4320 if (name == NULL) {
4321 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4322 err = -1;
4323 else
4324 PyErr_Clear();
4325 break;
4326 }
4327 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004328 PyUnicode_Check(name) &&
4329 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004330 {
4331 Py_DECREF(name);
4332 continue;
4333 }
4334 value = PyObject_GetAttr(v, name);
4335 if (value == NULL)
4336 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004337 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004338 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004339 else
4340 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004341 Py_DECREF(name);
4342 Py_XDECREF(value);
4343 if (err != 0)
4344 break;
4345 }
4346 Py_DECREF(all);
4347 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004348}
4349
Guido van Rossumac7be682001-01-17 15:42:30 +00004350static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004351format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004352{
Neal Norwitzda059e32007-08-26 05:33:45 +00004353 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004354
4355 if (!obj)
4356 return;
4357
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004358 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004359 if (!obj_str)
4360 return;
4361
4362 PyErr_Format(exc, format_str, obj_str);
4363}
Guido van Rossum950361c1997-01-24 13:49:28 +00004364
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004365static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004366unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004367 PyFrameObject *f, unsigned char *next_instr)
4368{
4369 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004370 are (Unicode) strings. */
4371 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4372 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004373 Py_ssize_t new_len = v_len + w_len;
4374 if (new_len < 0) {
4375 PyErr_SetString(PyExc_OverflowError,
4376 "strings are too large to concat");
4377 return NULL;
4378 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004379
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004380 if (v->ob_refcnt == 2) {
4381 /* In the common case, there are 2 references to the value
4382 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004383 * value stack (in 'v') and one still stored in the
4384 * 'variable'. We try to delete the variable now to reduce
4385 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004386 */
4387 switch (*next_instr) {
4388 case STORE_FAST:
4389 {
4390 int oparg = PEEKARG();
4391 PyObject **fastlocals = f->f_localsplus;
4392 if (GETLOCAL(oparg) == v)
4393 SETLOCAL(oparg, NULL);
4394 break;
4395 }
4396 case STORE_DEREF:
4397 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004398 PyObject **freevars = (f->f_localsplus +
4399 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004400 PyObject *c = freevars[PEEKARG()];
4401 if (PyCell_GET(c) == v)
4402 PyCell_Set(c, NULL);
4403 break;
4404 }
4405 case STORE_NAME:
4406 {
4407 PyObject *names = f->f_code->co_names;
4408 PyObject *name = GETITEM(names, PEEKARG());
4409 PyObject *locals = f->f_locals;
4410 if (PyDict_CheckExact(locals) &&
4411 PyDict_GetItem(locals, name) == v) {
4412 if (PyDict_DelItem(locals, name) != 0) {
4413 PyErr_Clear();
4414 }
4415 }
4416 break;
4417 }
4418 }
4419 }
4420
Guido van Rossum98297ee2007-11-06 21:34:58 +00004421 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004422 /* Now we own the last reference to 'v', so we can resize it
4423 * in-place.
4424 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004425 if (PyUnicode_Resize(&v, new_len) != 0) {
4426 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004427 * deallocated so it cannot be put back into
4428 * 'variable'. The MemoryError is raised when there
4429 * is no value in 'variable', which might (very
4430 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004431 */
4432 return NULL;
4433 }
4434 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004435 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4436 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004437 return v;
4438 }
4439 else {
4440 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004441 w = PyUnicode_Concat(v, w);
4442 Py_DECREF(v);
4443 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004444 }
4445}
4446
Guido van Rossum950361c1997-01-24 13:49:28 +00004447#ifdef DYNAMIC_EXECUTION_PROFILE
4448
Skip Montanarof118cb12001-10-15 20:51:38 +00004449static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004450getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004451{
4452 int i;
4453 PyObject *l = PyList_New(256);
4454 if (l == NULL) return NULL;
4455 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004456 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004457 if (x == NULL) {
4458 Py_DECREF(l);
4459 return NULL;
4460 }
4461 PyList_SetItem(l, i, x);
4462 }
4463 for (i = 0; i < 256; i++)
4464 a[i] = 0;
4465 return l;
4466}
4467
4468PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004469_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004470{
4471#ifndef DXPAIRS
4472 return getarray(dxp);
4473#else
4474 int i;
4475 PyObject *l = PyList_New(257);
4476 if (l == NULL) return NULL;
4477 for (i = 0; i < 257; i++) {
4478 PyObject *x = getarray(dxpairs[i]);
4479 if (x == NULL) {
4480 Py_DECREF(l);
4481 return NULL;
4482 }
4483 PyList_SetItem(l, i, x);
4484 }
4485 return l;
4486#endif
4487}
4488
4489#endif