blob: 5d1fb285bebfaa853a517a5ad91684648d04a77f [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
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000219#define COMPUTE_EVAL_BREAKER() \
220 (eval_breaker = gil_drop_request | pendingcalls_to_do | pending_async_exc)
221
222#define SET_GIL_DROP_REQUEST() \
223 do { gil_drop_request = 1; eval_breaker = 1; } while (0)
224
225#define RESET_GIL_DROP_REQUEST() \
226 do { gil_drop_request = 0; COMPUTE_EVAL_BREAKER(); } while (0)
227
228#define SIGNAL_PENDING_CALLS() \
229 do { pendingcalls_to_do = 1; eval_breaker = 1; } while (0)
230
231#define UNSIGNAL_PENDING_CALLS() \
232 do { pendingcalls_to_do = 0; COMPUTE_EVAL_BREAKER(); } while (0)
233
234#define SIGNAL_ASYNC_EXC() \
235 do { pending_async_exc = 1; eval_breaker = 1; } while (0)
236
237#define UNSIGNAL_ASYNC_EXC() \
238 do { pending_async_exc = 0; COMPUTE_EVAL_BREAKER(); } while (0)
239
240
Guido van Rossume59214e1994-08-30 08:01:59 +0000241#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000242
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000243#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000245#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000246#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000247
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000248static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000249static long main_thread = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000250/* This single variable consolidates all requests to break out of the fast path
251 in the eval loop. */
252static volatile int eval_breaker = 0;
253/* Request for droppping the GIL */
254static volatile int gil_drop_request = 0;
255/* Request for running pending calls */
256static volatile int pendingcalls_to_do = 0;
257/* Request for looking at the `async_exc` field of the current thread state */
258static volatile int pending_async_exc = 0;
259
260#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000261
Tim Peters7f468f22004-10-11 02:40:51 +0000262int
263PyEval_ThreadsInitialized(void)
264{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000265 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000266}
267
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000268void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000270{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000271 if (gil_created())
Sjoerd Mullendered59d201993-01-06 13:36:38 +0000272 return;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000273 create_gil();
274 take_gil(PyThreadState_GET());
Guido van Rossum65d5b571998-12-21 19:32:43 +0000275 main_thread = PyThread_get_thread_ident();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000276 if (!pending_lock)
277 pending_lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000279
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000280void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000281PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000282{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000283 PyThreadState *tstate = PyThreadState_GET();
284 if (tstate == NULL)
285 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
286 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000287}
288
289void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000290PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000291{
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000292 /* This function must succeed when the current thread state is NULL.
293 We therefore avoid PyThreadState_GET() which dumps a fatal error
294 in debug mode.
295 */
296 drop_gil(_PyThreadState_Current);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000297}
298
299void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000300PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000301{
302 if (tstate == NULL)
303 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
Mark Hammond8d98d2c2003-04-19 15:41:53 +0000304 /* Check someone has called PyEval_InitThreads() to create the lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000305 assert(gil_created());
306 take_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000307 if (PyThreadState_Swap(tstate) != NULL)
308 Py_FatalError(
309 "PyEval_AcquireThread: non-NULL old thread state");
310}
311
312void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000313PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000314{
315 if (tstate == NULL)
316 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
317 if (PyThreadState_Swap(NULL) != tstate)
318 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000319 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000320}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000321
322/* This function is called from PyOS_AfterFork to ensure that newly
323 created child processes don't hold locks referring to threads which
324 are not running in the child process. (This could also be done using
325 pthread_atfork mechanism, at least for the pthreads implementation.) */
326
327void
328PyEval_ReInitThreads(void)
329{
Jesse Nollera8513972008-07-17 16:49:17 +0000330 PyObject *threading, *result;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000331 PyThreadState *tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000332
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000333 if (!gil_created())
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000334 return;
335 /*XXX Can't use PyThread_free_lock here because it does too
336 much error-checking. Doing this cleanly would require
337 adding a new function to each thread_*.h. Instead, just
338 create a new lock and waste a little bit of memory */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000339 recreate_gil();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000340 pending_lock = PyThread_allocate_lock();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000341 take_gil(tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000342 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000343
344 /* Update the threading module with the new state.
345 */
346 tstate = PyThreadState_GET();
347 threading = PyMapping_GetItemString(tstate->interp->modules,
348 "threading");
349 if (threading == NULL) {
350 /* threading not imported */
351 PyErr_Clear();
352 return;
353 }
354 result = PyObject_CallMethod(threading, "_after_fork", NULL);
355 if (result == NULL)
356 PyErr_WriteUnraisable(threading);
357 else
358 Py_DECREF(result);
359 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000360}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000361
362#else
363static int eval_breaker = 0;
364static int gil_drop_request = 0;
365static int pending_async_exc = 0;
366#endif /* WITH_THREAD */
367
368/* This function is used to signal that async exceptions are waiting to be
369 raised, therefore it is also useful in non-threaded builds. */
370
371void
372_PyEval_SignalAsyncExc(void)
373{
374 SIGNAL_ASYNC_EXC();
375}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000376
Guido van Rossumff4949e1992-08-05 19:58:53 +0000377/* Functions save_thread and restore_thread are always defined so
378 dynamically loaded modules needn't be compiled separately for use
379 with and without threads: */
380
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000381PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000382PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000383{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000384 PyThreadState *tstate = PyThreadState_Swap(NULL);
385 if (tstate == NULL)
386 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000387#ifdef WITH_THREAD
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000388 if (gil_created())
389 drop_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000390#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000391 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000392}
393
394void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000395PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000396{
Guido van Rossumb74eca91997-09-30 22:03:16 +0000397 if (tstate == NULL)
398 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000399#ifdef WITH_THREAD
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000400 if (gil_created()) {
Guido van Rossumb74eca91997-09-30 22:03:16 +0000401 int err = errno;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000402 take_gil(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000403 errno = err;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000404 }
405#endif
Guido van Rossumb74eca91997-09-30 22:03:16 +0000406 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000407}
408
409
Guido van Rossuma9672091994-09-14 13:31:22 +0000410/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
411 signal handlers or Mac I/O completion routines) can schedule calls
412 to a function to be called synchronously.
413 The synchronous function is called with one void* argument.
414 It should return 0 for success or -1 for failure -- failure should
415 be accompanied by an exception.
416
417 If registry succeeds, the registry function returns 0; if it fails
418 (e.g. due to too many pending calls) it returns -1 (without setting
419 an exception condition).
420
421 Note that because registry may occur from within signal handlers,
422 or other asynchronous events, calling malloc() is unsafe!
423
424#ifdef WITH_THREAD
425 Any thread can schedule pending calls, but only the main thread
426 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000427 There is no facility to schedule calls to a particular thread, but
428 that should be easy to change, should that ever be required. In
429 that case, the static variables here should go into the python
430 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000431#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000432*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000433
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000434#ifdef WITH_THREAD
435
436/* The WITH_THREAD implementation is thread-safe. It allows
437 scheduling to be made from any thread, and even from an executing
438 callback.
439 */
440
441#define NPENDINGCALLS 32
442static struct {
443 int (*func)(void *);
444 void *arg;
445} pendingcalls[NPENDINGCALLS];
446static int pendingfirst = 0;
447static int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000448static char pendingbusy = 0;
449
450int
451Py_AddPendingCall(int (*func)(void *), void *arg)
452{
453 int i, j, result=0;
454 PyThread_type_lock lock = pending_lock;
455
456 /* try a few times for the lock. Since this mechanism is used
457 * for signal handling (on the main thread), there is a (slim)
458 * chance that a signal is delivered on the same thread while we
459 * hold the lock during the Py_MakePendingCalls() function.
460 * This avoids a deadlock in that case.
461 * Note that signals can be delivered on any thread. In particular,
462 * on Windows, a SIGINT is delivered on a system-created worker
463 * thread.
464 * We also check for lock being NULL, in the unlikely case that
465 * this function is called before any bytecode evaluation takes place.
466 */
467 if (lock != NULL) {
468 for (i = 0; i<100; i++) {
469 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
470 break;
471 }
472 if (i == 100)
473 return -1;
474 }
475
476 i = pendinglast;
477 j = (i + 1) % NPENDINGCALLS;
478 if (j == pendingfirst) {
479 result = -1; /* Queue full */
480 } else {
481 pendingcalls[i].func = func;
482 pendingcalls[i].arg = arg;
483 pendinglast = j;
484 }
485 /* signal main loop */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000486 SIGNAL_PENDING_CALLS();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000487 if (lock != NULL)
488 PyThread_release_lock(lock);
489 return result;
490}
491
492int
493Py_MakePendingCalls(void)
494{
495 int i;
496 int r = 0;
497
498 if (!pending_lock) {
499 /* initial allocation of the lock */
500 pending_lock = PyThread_allocate_lock();
501 if (pending_lock == NULL)
502 return -1;
503 }
504
505 /* only service pending calls on main thread */
506 if (main_thread && PyThread_get_thread_ident() != main_thread)
507 return 0;
508 /* don't perform recursive pending calls */
509 if (pendingbusy)
510 return 0;
511 pendingbusy = 1;
512 /* perform a bounded number of calls, in case of recursion */
513 for (i=0; i<NPENDINGCALLS; i++) {
514 int j;
515 int (*func)(void *);
Benjamin Petersonb4ddfa42009-01-17 23:46:54 +0000516 void *arg = NULL;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000517
518 /* pop one item off the queue while holding the lock */
519 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
520 j = pendingfirst;
521 if (j == pendinglast) {
522 func = NULL; /* Queue empty */
523 } else {
524 func = pendingcalls[j].func;
525 arg = pendingcalls[j].arg;
526 pendingfirst = (j + 1) % NPENDINGCALLS;
527 }
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000528 if (pendingfirst != pendinglast)
529 SIGNAL_PENDING_CALLS();
530 else
531 UNSIGNAL_PENDING_CALLS();
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000532 PyThread_release_lock(pending_lock);
533 /* having released the lock, perform the callback */
534 if (func == NULL)
535 break;
536 r = func(arg);
537 if (r)
538 break;
539 }
540 pendingbusy = 0;
541 return r;
542}
543
544#else /* if ! defined WITH_THREAD */
545
546/*
547 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
548 This code is used for signal handling in python that isn't built
549 with WITH_THREAD.
550 Don't use this implementation when Py_AddPendingCalls() can happen
551 on a different thread!
552
Guido van Rossuma9672091994-09-14 13:31:22 +0000553 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000554 (1) nested asynchronous calls to Py_AddPendingCall()
555 (2) AddPendingCall() calls made while pending calls are being processed.
556
557 (1) is very unlikely because typically signal delivery
558 is blocked during signal handling. So it should be impossible.
559 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000560 The current code is safe against (2), but not against (1).
561 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000562 thread is present, interrupted by signals, and that the critical
563 section is protected with the "busy" variable. On Windows, which
564 delivers SIGINT on a system thread, this does not hold and therefore
565 Windows really shouldn't use this version.
566 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000567*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000568
Guido van Rossuma9672091994-09-14 13:31:22 +0000569#define NPENDINGCALLS 32
570static struct {
Thomas Wouters334fb892000-07-25 12:56:38 +0000571 int (*func)(void *);
572 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000573} pendingcalls[NPENDINGCALLS];
574static volatile int pendingfirst = 0;
575static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000576static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000577
578int
Thomas Wouters334fb892000-07-25 12:56:38 +0000579Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000580{
Michael W. Hudson30ea2f22004-07-07 17:44:12 +0000581 static volatile int busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000582 int i, j;
583 /* XXX Begin critical section */
Guido van Rossum180d7b41994-09-29 09:45:57 +0000584 if (busy)
585 return -1;
586 busy = 1;
Guido van Rossuma9672091994-09-14 13:31:22 +0000587 i = pendinglast;
588 j = (i + 1) % NPENDINGCALLS;
Guido van Rossum04e70322002-07-17 16:57:13 +0000589 if (j == pendingfirst) {
590 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000591 return -1; /* Queue full */
Guido van Rossum04e70322002-07-17 16:57:13 +0000592 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000593 pendingcalls[i].func = func;
594 pendingcalls[i].arg = arg;
595 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000596
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000597 SIGNAL_PENDING_CALLS();
Guido van Rossum180d7b41994-09-29 09:45:57 +0000598 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000599 /* XXX End critical section */
600 return 0;
601}
602
Guido van Rossum180d7b41994-09-29 09:45:57 +0000603int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000604Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000605{
Guido van Rossum180d7b41994-09-29 09:45:57 +0000606 static int busy = 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +0000607 if (busy)
Guido van Rossum180d7b41994-09-29 09:45:57 +0000608 return 0;
609 busy = 1;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000610 UNSIGNAL_PENDING_CALLS();
Guido van Rossuma9672091994-09-14 13:31:22 +0000611 for (;;) {
612 int i;
Thomas Wouters334fb892000-07-25 12:56:38 +0000613 int (*func)(void *);
614 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000615 i = pendingfirst;
616 if (i == pendinglast)
617 break; /* Queue empty */
618 func = pendingcalls[i].func;
619 arg = pendingcalls[i].arg;
620 pendingfirst = (i + 1) % NPENDINGCALLS;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000621 if (func(arg) < 0) {
622 busy = 0;
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000623 SIGNAL_PENDING_CALLS(); /* We're not done yet */
Guido van Rossuma9672091994-09-14 13:31:22 +0000624 return -1;
Guido van Rossum180d7b41994-09-29 09:45:57 +0000625 }
Guido van Rossuma9672091994-09-14 13:31:22 +0000626 }
Guido van Rossum180d7b41994-09-29 09:45:57 +0000627 busy = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000628 return 0;
629}
630
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000631#endif /* WITH_THREAD */
632
Guido van Rossuma9672091994-09-14 13:31:22 +0000633
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000634/* The interpreter's recursion limit */
635
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000636#ifndef Py_DEFAULT_RECURSION_LIMIT
637#define Py_DEFAULT_RECURSION_LIMIT 1000
638#endif
639static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
640int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000641
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000642int
643Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000644{
645 return recursion_limit;
646}
647
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000648void
649Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000650{
651 recursion_limit = new_limit;
Thomas Woutersce272b62007-09-19 21:19:28 +0000652 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000653}
654
Armin Rigo2b3eb402003-10-28 12:05:48 +0000655/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
656 if the recursion_depth reaches _Py_CheckRecursionLimit.
657 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
658 to guarantee that _Py_CheckRecursiveCall() is regularly called.
659 Without USE_STACKCHECK, there is no need for this. */
660int
661_Py_CheckRecursiveCall(char *where)
662{
663 PyThreadState *tstate = PyThreadState_GET();
664
665#ifdef USE_STACKCHECK
666 if (PyOS_CheckStack()) {
667 --tstate->recursion_depth;
668 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
669 return -1;
670 }
671#endif
Antoine Pitrou658fad82008-09-03 18:34:34 +0000672 _Py_CheckRecursionLimit = recursion_limit;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000673 if (tstate->recursion_critical)
674 /* Somebody asked that we don't check for recursion. */
675 return 0;
676 if (tstate->overflowed) {
677 if (tstate->recursion_depth > recursion_limit + 50) {
678 /* Overflowing while handling an overflow. Give up. */
679 Py_FatalError("Cannot recover from stack overflow.");
680 }
681 return 0;
682 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000683 if (tstate->recursion_depth > recursion_limit) {
684 --tstate->recursion_depth;
Martin v. Löwis5b222132007-06-10 09:51:05 +0000685 tstate->overflowed = 1;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000686 PyErr_Format(PyExc_RuntimeError,
687 "maximum recursion depth exceeded%s",
688 where);
689 return -1;
690 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000691 return 0;
692}
693
Guido van Rossum374a9221991-04-04 10:40:29 +0000694/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000695enum why_code {
696 WHY_NOT = 0x0001, /* No error */
697 WHY_EXCEPTION = 0x0002, /* Exception occurred */
698 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
699 WHY_RETURN = 0x0008, /* 'return' statement */
700 WHY_BREAK = 0x0010, /* 'break' statement */
701 WHY_CONTINUE = 0x0020, /* 'continue' statement */
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000702 WHY_YIELD = 0x0040, /* 'yield' operator */
703 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000704};
Guido van Rossum374a9221991-04-04 10:40:29 +0000705
Collin Winter828f04a2007-08-31 00:04:24 +0000706static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000707static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000708
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000709/* Records whether tracing is on for any thread. Counts the number of
710 threads for which tstate->c_tracefunc is non-NULL, so if the value
711 is 0, we know we don't have to check this thread's c_tracefunc.
712 This speeds up the if statement in PyEval_EvalFrameEx() after
713 fast_next_opcode*/
714static int _Py_TracingPossible = 0;
715
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000716
Guido van Rossum374a9221991-04-04 10:40:29 +0000717
Guido van Rossumb209a111997-04-29 18:18:01 +0000718PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000719PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000720{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721 return PyEval_EvalCodeEx(co,
Guido van Rossum681d79a1995-07-18 14:51:37 +0000722 globals, locals,
Guido van Rossumb209a111997-04-29 18:18:01 +0000723 (PyObject **)NULL, 0,
724 (PyObject **)NULL, 0,
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000725 (PyObject **)NULL, 0,
Guido van Rossum4f72a782006-10-27 23:31:49 +0000726 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000727}
728
729
730/* Interpreter main loop */
731
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000732PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000733PyEval_EvalFrame(PyFrameObject *f) {
734 /* This is for backward compatibility with extension modules that
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000735 used this API; core interpreter code should call
736 PyEval_EvalFrameEx() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000737 return PyEval_EvalFrameEx(f, 0);
738}
739
740PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000742{
Guido van Rossum950361c1997-01-24 13:49:28 +0000743#ifdef DXPAIRS
744 int lastopcode = 0;
745#endif
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000746 register PyObject **stack_pointer; /* Next free slot in value stack */
Guido van Rossum374a9221991-04-04 10:40:29 +0000747 register unsigned char *next_instr;
Armin Rigo8817fcd2004-06-17 10:22:40 +0000748 register int opcode; /* Current opcode */
749 register int oparg; /* Current opcode argument, if any */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000750 register enum why_code why; /* Reason for block stack unwind */
Guido van Rossum374a9221991-04-04 10:40:29 +0000751 register int err; /* Error status -- nonzero if error */
Guido van Rossumb209a111997-04-29 18:18:01 +0000752 register PyObject *x; /* Result object -- NULL if error */
753 register PyObject *v; /* Temporary objects popped off stack */
754 register PyObject *w;
755 register PyObject *u;
756 register PyObject *t;
Jeremy Hylton2b724da2001-01-29 22:51:52 +0000757 register PyObject **fastlocals, **freevars;
Guido van Rossum014518f1998-11-23 21:09:51 +0000758 PyObject *retval = NULL; /* Return value */
Guido van Rossum885553e1998-12-21 18:33:30 +0000759 PyThreadState *tstate = PyThreadState_GET();
Tim Peters5ca576e2001-06-18 22:08:13 +0000760 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000761
Tim Peters8a5c3c72004-04-05 19:36:21 +0000762 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000763
764 not (instr_lb <= current_bytecode_offset < instr_ub)
765
Tim Peters8a5c3c72004-04-05 19:36:21 +0000766 is true when the line being executed has changed. The
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000767 initial values are such as to make this false the first
768 time it is tested. */
Armin Rigobf57a142004-03-22 19:24:58 +0000769 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000770
Guido van Rossumd076c731998-10-07 19:42:25 +0000771 unsigned char *first_instr;
Skip Montanaro04d80f82002-08-04 21:03:35 +0000772 PyObject *names;
773 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000774#if defined(Py_DEBUG) || defined(LLTRACE)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000775 /* Make it easier to find out where we are with a debugger */
Tim Peters5ca576e2001-06-18 22:08:13 +0000776 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000777#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000778
Antoine Pitroub52ec782009-01-25 16:34:23 +0000779/* Computed GOTOs, or
780 the-optimization-commonly-but-improperly-known-as-"threaded code"
781 using gcc's labels-as-values extension
782 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
783
784 The traditional bytecode evaluation loop uses a "switch" statement, which
785 decent compilers will optimize as a single indirect branch instruction
786 combined with a lookup table of jump addresses. However, since the
787 indirect jump instruction is shared by all opcodes, the CPU will have a
788 hard time making the right prediction for where to jump next (actually,
789 it will be always wrong except in the uncommon case of a sequence of
790 several identical opcodes).
791
792 "Threaded code" in contrast, uses an explicit jump table and an explicit
793 indirect jump instruction at the end of each opcode. Since the jump
794 instruction is at a different address for each opcode, the CPU will make a
795 separate prediction for each of these instructions, which is equivalent to
796 predicting the second opcode of each opcode pair. These predictions have
797 a much better chance to turn out valid, especially in small bytecode loops.
798
799 A mispredicted branch on a modern CPU flushes the whole pipeline and
800 can cost several CPU cycles (depending on the pipeline depth),
801 and potentially many more instructions (depending on the pipeline width).
802 A correctly predicted branch, however, is nearly free.
803
804 At the time of this writing, the "threaded code" version is up to 15-20%
805 faster than the normal "switch" version, depending on the compiler and the
806 CPU architecture.
807
808 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
809 because it would render the measurements invalid.
810
811
812 NOTE: care must be taken that the compiler doesn't try to "optimize" the
813 indirect jumps by sharing them between all opcodes. Such optimizations
814 can be disabled on gcc by using the -fno-gcse flag (or possibly
815 -fno-crossjumping).
816*/
817
818#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
819#undef USE_COMPUTED_GOTOS
820#endif
821
822#ifdef USE_COMPUTED_GOTOS
823/* Import the static jump table */
824#include "opcode_targets.h"
825
826/* This macro is used when several opcodes defer to the same implementation
827 (e.g. SETUP_LOOP, SETUP_FINALLY) */
828#define TARGET_WITH_IMPL(op, impl) \
829 TARGET_##op: \
830 opcode = op; \
831 if (HAS_ARG(op)) \
832 oparg = NEXTARG(); \
833 case op: \
834 goto impl; \
835
836#define TARGET(op) \
837 TARGET_##op: \
838 opcode = op; \
839 if (HAS_ARG(op)) \
840 oparg = NEXTARG(); \
841 case op:
842
843
844#define DISPATCH() \
845 { \
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000846 if (!eval_breaker) { \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000847 FAST_DISPATCH(); \
848 } \
849 continue; \
850 }
851
852#ifdef LLTRACE
853#define FAST_DISPATCH() \
854 { \
855 if (!lltrace && !_Py_TracingPossible) { \
856 f->f_lasti = INSTR_OFFSET(); \
857 goto *opcode_targets[*next_instr++]; \
858 } \
859 goto fast_next_opcode; \
860 }
861#else
862#define FAST_DISPATCH() \
863 { \
864 if (!_Py_TracingPossible) { \
865 f->f_lasti = INSTR_OFFSET(); \
866 goto *opcode_targets[*next_instr++]; \
867 } \
868 goto fast_next_opcode; \
869 }
870#endif
871
872#else
873#define TARGET(op) \
874 case op:
875#define TARGET_WITH_IMPL(op, impl) \
876 /* silence compiler warnings about `impl` unused */ \
877 if (0) goto impl; \
878 case op:
879#define DISPATCH() continue
880#define FAST_DISPATCH() goto fast_next_opcode
881#endif
882
883
Neal Norwitza81d2202002-07-14 00:27:26 +0000884/* Tuple access macros */
885
886#ifndef Py_DEBUG
887#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
888#else
889#define GETITEM(v, i) PyTuple_GetItem((v), (i))
890#endif
891
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000892#ifdef WITH_TSC
893/* Use Pentium timestamp counter to mark certain events:
894 inst0 -- beginning of switch statement for opcode dispatch
895 inst1 -- end of switch statement (may be skipped)
896 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000897 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000898 (may be skipped)
899 intr1 -- beginning of long interruption
900 intr2 -- end of long interruption
901
902 Many opcodes call out to helper C functions. In some cases, the
903 time in those functions should be counted towards the time for the
904 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
905 calls another Python function; there's no point in charge all the
906 bytecode executed by the called function to the caller.
907
908 It's hard to make a useful judgement statically. In the presence
909 of operator overloading, it's impossible to tell if a call will
910 execute new Python code or not.
911
912 It's a case-by-case judgement. I'll use intr1 for the following
913 cases:
914
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000915 IMPORT_STAR
916 IMPORT_FROM
917 CALL_FUNCTION (and friends)
918
919 */
920 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
921 int ticked = 0;
922
Michael W. Hudson75eabd22005-01-18 15:56:11 +0000923 READ_TIMESTAMP(inst0);
924 READ_TIMESTAMP(inst1);
925 READ_TIMESTAMP(loop0);
926 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000927
928 /* shut up the compiler */
929 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000930#endif
931
Guido van Rossum374a9221991-04-04 10:40:29 +0000932/* Code access macros */
933
Martin v. Löwis18e16552006-02-15 17:27:45 +0000934#define INSTR_OFFSET() ((int)(next_instr - first_instr))
Guido van Rossum374a9221991-04-04 10:40:29 +0000935#define NEXTOP() (*next_instr++)
Raymond Hettinger5bed4562004-04-10 23:34:17 +0000936#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000937#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
Guido van Rossumd076c731998-10-07 19:42:25 +0000938#define JUMPTO(x) (next_instr = first_instr + (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000939#define JUMPBY(x) (next_instr += (x))
940
Raymond Hettingerf606f872003-03-16 03:11:04 +0000941/* OpCode prediction macros
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000942 Some opcodes tend to come in pairs thus making it possible to
943 predict the second code when the first is run. For example,
944 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
945 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000946
Georg Brandl86b2fb92008-07-16 03:43:04 +0000947 Verifying the prediction costs a single high-speed test of a register
Raymond Hettingerac2072922003-03-16 15:41:11 +0000948 variable against a constant. If the pairing was good, then the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000949 processor's own internal branch predication has a high likelihood of
950 success, resulting in a nearly zero-overhead transition to the
951 next opcode. A successful prediction saves a trip through the eval-loop
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000952 including its two unpredictable branches, the HAS_ARG test and the
Georg Brandl86b2fb92008-07-16 03:43:04 +0000953 switch-case. Combined with the processor's internal branch prediction,
Antoine Pitrou9a2310d2008-07-25 22:39:39 +0000954 a successful PREDICT has the effect of making the two opcodes run as if
Georg Brandl86b2fb92008-07-16 03:43:04 +0000955 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000956
Georg Brandl86b2fb92008-07-16 03:43:04 +0000957 If collecting opcode statistics, your choices are to either keep the
958 predictions turned-on and interpret the results as if some opcodes
959 had been combined or turn-off predictions so that the opcode frequency
960 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000961
962 Opcode prediction is disabled with threaded code, since the latter allows
963 the CPU to record separate branch prediction information for each
964 opcode.
965
Raymond Hettingerf606f872003-03-16 03:11:04 +0000966*/
967
Antoine Pitroub52ec782009-01-25 16:34:23 +0000968#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Raymond Hettingera7216982004-02-08 19:59:27 +0000969#define PREDICT(op) if (0) goto PRED_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000970#define PREDICTED(op) PRED_##op:
971#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000972#else
Raymond Hettingerac2072922003-03-16 15:41:11 +0000973#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingerf606f872003-03-16 03:11:04 +0000974#define PREDICTED(op) PRED_##op: next_instr++
Raymond Hettinger52a21b82004-08-06 18:43:09 +0000975#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000976#endif
977
Raymond Hettingerf606f872003-03-16 03:11:04 +0000978
Guido van Rossum374a9221991-04-04 10:40:29 +0000979/* Stack manipulation macros */
980
Martin v. Löwis18e16552006-02-15 17:27:45 +0000981/* The stack can grow at most MAXINT deep, as co_nlocals and
982 co_stacksize are ints. */
983#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
Guido van Rossum374a9221991-04-04 10:40:29 +0000984#define EMPTY() (STACK_LEVEL() == 0)
985#define TOP() (stack_pointer[-1])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000986#define SECOND() (stack_pointer[-2])
987#define THIRD() (stack_pointer[-3])
988#define FOURTH() (stack_pointer[-4])
Benjamin Peterson6d46a912009-06-28 16:17:34 +0000989#define PEEK(n) (stack_pointer[-(n)])
Raymond Hettinger663004b2003-01-09 15:24:30 +0000990#define SET_TOP(v) (stack_pointer[-1] = (v))
991#define SET_SECOND(v) (stack_pointer[-2] = (v))
992#define SET_THIRD(v) (stack_pointer[-3] = (v))
993#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Benjamin Peterson6d46a912009-06-28 16:17:34 +0000994#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
Raymond Hettinger663004b2003-01-09 15:24:30 +0000995#define BASIC_STACKADJ(n) (stack_pointer += n)
Guido van Rossum374a9221991-04-04 10:40:29 +0000996#define BASIC_PUSH(v) (*stack_pointer++ = (v))
997#define BASIC_POP() (*--stack_pointer)
998
Guido van Rossum96a42c81992-01-12 02:29:51 +0000999#ifdef LLTRACE
Jeremy Hylton14368152001-10-17 13:29:30 +00001000#define PUSH(v) { (void)(BASIC_PUSH(v), \
1001 lltrace && prtrace(TOP(), "push")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001002 assert(STACK_LEVEL() <= co->co_stacksize); }
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001003#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
1004 BASIC_POP())
Raymond Hettinger663004b2003-01-09 15:24:30 +00001005#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
1006 lltrace && prtrace(TOP(), "stackadj")); \
Thomas Wouters477c8d52006-05-27 19:21:47 +00001007 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +00001008#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
1009 prtrace((STACK_POINTER)[-1], "ext_pop")), \
1010 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001011#else
1012#define PUSH(v) BASIC_PUSH(v)
1013#define POP() BASIC_POP()
Raymond Hettinger663004b2003-01-09 15:24:30 +00001014#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001015#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001016#endif
1017
Guido van Rossum681d79a1995-07-18 14:51:37 +00001018/* Local variable macros */
1019
1020#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001021
1022/* The SETLOCAL() macro must not DECREF the local variable in-place and
1023 then store the new value; it must copy the old value to a temporary
1024 value, then store the new value, and then DECREF the temporary value.
1025 This is because it is possible that during the DECREF the frame is
1026 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1027 variable would be pointing to already-freed memory. */
1028#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
1029 GETLOCAL(i) = value; \
1030 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001031
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001032
1033#define UNWIND_BLOCK(b) \
1034 while (STACK_LEVEL() > (b)->b_level) { \
1035 PyObject *v = POP(); \
1036 Py_XDECREF(v); \
1037 }
1038
1039#define UNWIND_EXCEPT_HANDLER(b) \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001040 { \
1041 PyObject *type, *value, *traceback; \
1042 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1043 while (STACK_LEVEL() > (b)->b_level + 3) { \
1044 value = POP(); \
1045 Py_XDECREF(value); \
1046 } \
1047 type = tstate->exc_type; \
1048 value = tstate->exc_value; \
1049 traceback = tstate->exc_traceback; \
1050 tstate->exc_type = POP(); \
1051 tstate->exc_value = POP(); \
1052 tstate->exc_traceback = POP(); \
1053 Py_XDECREF(type); \
1054 Py_XDECREF(value); \
1055 Py_XDECREF(traceback); \
1056 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001057
1058#define SAVE_EXC_STATE() \
1059 { \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001060 PyObject *type, *value, *traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001061 Py_XINCREF(tstate->exc_type); \
1062 Py_XINCREF(tstate->exc_value); \
1063 Py_XINCREF(tstate->exc_traceback); \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001064 type = f->f_exc_type; \
1065 value = f->f_exc_value; \
1066 traceback = f->f_exc_traceback; \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001067 f->f_exc_type = tstate->exc_type; \
1068 f->f_exc_value = tstate->exc_value; \
1069 f->f_exc_traceback = tstate->exc_traceback; \
Benjamin Peterson27d63672008-06-15 20:09:12 +00001070 Py_XDECREF(type); \
1071 Py_XDECREF(value); \
1072 Py_XDECREF(traceback); \
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001073 }
1074
1075#define SWAP_EXC_STATE() \
1076 { \
1077 PyObject *tmp; \
1078 tmp = tstate->exc_type; \
1079 tstate->exc_type = f->f_exc_type; \
1080 f->f_exc_type = tmp; \
1081 tmp = tstate->exc_value; \
1082 tstate->exc_value = f->f_exc_value; \
1083 f->f_exc_value = tmp; \
1084 tmp = tstate->exc_traceback; \
1085 tstate->exc_traceback = f->f_exc_traceback; \
1086 f->f_exc_traceback = tmp; \
1087 }
1088
Guido van Rossuma027efa1997-05-05 20:56:21 +00001089/* Start of code */
1090
Tim Peters5ca576e2001-06-18 22:08:13 +00001091 if (f == NULL)
1092 return NULL;
1093
Armin Rigo1d313ab2003-10-25 14:33:09 +00001094 /* push frame */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001095 if (Py_EnterRecursiveCall(""))
Armin Rigo1d313ab2003-10-25 14:33:09 +00001096 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001097
Tim Peters5ca576e2001-06-18 22:08:13 +00001098 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001099
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001100 if (tstate->use_tracing) {
1101 if (tstate->c_tracefunc != NULL) {
1102 /* tstate->c_tracefunc, if defined, is a
1103 function that will be called on *every* entry
1104 to a code block. Its return value, if not
1105 None, is a function that will be called at
1106 the start of each executed line of code.
1107 (Actually, the function must return itself
1108 in order to continue tracing.) The trace
1109 functions are called with three arguments:
1110 a pointer to the current frame, a string
1111 indicating why the function is called, and
1112 an argument which depends on the situation.
1113 The global trace function is also called
1114 whenever an exception is detected. */
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001115 if (call_trace_protected(tstate->c_tracefunc,
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001116 tstate->c_traceobj,
1117 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001118 /* Trace function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001119 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001120 }
1121 }
1122 if (tstate->c_profilefunc != NULL) {
1123 /* Similar for c_profilefunc, except it needn't
1124 return itself and isn't called for "line" events */
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00001125 if (call_trace_protected(tstate->c_profilefunc,
1126 tstate->c_profileobj,
1127 f, PyTrace_CALL, Py_None)) {
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001128 /* Profile function raised an error */
Armin Rigo2b3eb402003-10-28 12:05:48 +00001129 goto exit_eval_frame;
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001130 }
1131 }
1132 }
1133
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001134 co = f->f_code;
1135 names = co->co_names;
1136 consts = co->co_consts;
1137 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138 freevars = f->f_localsplus + co->co_nlocals;
Christian Heimes72b710a2008-05-26 13:28:38 +00001139 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001140 /* An explanation is in order for the next line.
1141
1142 f->f_lasti now refers to the index of the last instruction
1143 executed. You might think this was obvious from the name, but
1144 this wasn't always true before 2.3! PyFrame_New now sets
1145 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1146 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001147 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001148
1149 When the PREDICT() macros are enabled, some opcode pairs follow in
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001150 direct succession without updating f->f_lasti. A successful
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001151 prediction effectively links the two codes together as if they
1152 were a single new opcode; accordingly,f->f_lasti will point to
1153 the first code in the pair (for instance, GET_ITER followed by
1154 FOR_ITER is effectively a single opcode and f->f_lasti will point
1155 at to the beginning of the combined pair.)
1156 */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001157 next_instr = first_instr + f->f_lasti + 1;
1158 stack_pointer = f->f_stacktop;
1159 assert(stack_pointer != NULL);
1160 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
1161
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001162 if (f->f_code->co_flags & CO_GENERATOR) {
1163 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1164 /* We were in an except handler when we left,
1165 restore the exception state which was put aside
1166 (see YIELD_VALUE). */
1167 SWAP_EXC_STATE();
1168 }
1169 else {
1170 SAVE_EXC_STATE();
1171 }
1172 }
1173
Tim Peters5ca576e2001-06-18 22:08:13 +00001174#ifdef LLTRACE
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001176#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001177#if defined(Py_DEBUG) || defined(LLTRACE)
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00001178 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001179#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001180
Guido van Rossum374a9221991-04-04 10:40:29 +00001181 why = WHY_NOT;
1182 err = 0;
Guido van Rossumb209a111997-04-29 18:18:01 +00001183 x = Py_None; /* Not a reference, just anything non-NULL */
Fred Drake48fba732000-10-11 13:54:07 +00001184 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001186 if (throwflag) { /* support for generator.throw() */
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001187 why = WHY_EXCEPTION;
1188 goto on_error;
1189 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001190
Guido van Rossum374a9221991-04-04 10:40:29 +00001191 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001192#ifdef WITH_TSC
1193 if (inst1 == 0) {
1194 /* Almost surely, the opcode executed a break
1195 or a continue, preventing inst1 from being set
1196 on the way out of the loop.
1197 */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001198 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001199 loop1 = inst1;
1200 }
1201 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1202 intr0, intr1);
1203 ticked = 0;
1204 inst1 = 0;
1205 intr0 = 0;
1206 intr1 = 0;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001207 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001208#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001209 assert(stack_pointer >= f->f_valuestack); /* else underflow */
Thomas Wouters477c8d52006-05-27 19:21:47 +00001210 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001211
Guido van Rossuma027efa1997-05-05 20:56:21 +00001212 /* Do periodic things. Doing this every time through
1213 the loop would add too much overhead, so we do it
1214 only every Nth instruction. We also do it if
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001215 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
Guido van Rossuma027efa1997-05-05 20:56:21 +00001216 event needs attention (e.g. a signal handler or
1217 async I/O handler); see Py_AddPendingCall() and
1218 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001220 if (eval_breaker) {
Thomas Woutersce272b62007-09-19 21:19:28 +00001221 if (*next_instr == SETUP_FINALLY) {
1222 /* Make the last opcode before
1223 a try: finally: block uninterruptable. */
1224 goto fast_next_opcode;
1225 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001226 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001227#ifdef WITH_TSC
1228 ticked = 1;
1229#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +00001230 if (pendingcalls_to_do) {
Guido van Rossum8861b741996-07-30 16:49:37 +00001231 if (Py_MakePendingCalls() < 0) {
1232 why = WHY_EXCEPTION;
1233 goto on_error;
1234 }
1235 }
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001236 if (gil_drop_request) {
Guido van Rossume59214e1994-08-30 08:01:59 +00001237#ifdef WITH_THREAD
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001238 /* Give another thread a chance */
Guido van Rossum25ce5661997-08-02 03:10:38 +00001239 if (PyThreadState_Swap(NULL) != tstate)
1240 Py_FatalError("ceval: tstate mix-up");
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001241 drop_gil(tstate);
1242
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001243 /* Other threads may run now */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001244
1245 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +00001246 if (PyThreadState_Swap(tstate) != NULL)
1247 Py_FatalError("ceval: orphan tstate");
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001248#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001249 }
1250 /* Check for asynchronous exceptions. */
1251 if (tstate->async_exc != NULL) {
1252 x = tstate->async_exc;
1253 tstate->async_exc = NULL;
1254 UNSIGNAL_ASYNC_EXC();
1255 PyErr_SetNone(x);
1256 Py_DECREF(x);
1257 why = WHY_EXCEPTION;
1258 goto on_error;
1259 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001260 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001261
Neil Schemenauer63543862002-02-17 19:10:14 +00001262 fast_next_opcode:
Guido van Rossum99bec951992-09-03 20:29:45 +00001263 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001265 /* line-by-line tracing support */
1266
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00001267 if (_Py_TracingPossible &&
1268 tstate->c_tracefunc != NULL && !tstate->tracing) {
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001269 /* see maybe_call_line_trace
1270 for expository comments */
1271 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001272
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001273 err = maybe_call_line_trace(tstate->c_tracefunc,
1274 tstate->c_traceobj,
Armin Rigobf57a142004-03-22 19:24:58 +00001275 f, &instr_lb, &instr_ub,
1276 &instr_prev);
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001277 /* Reload possibly changed frame fields */
1278 JUMPTO(f->f_lasti);
Michael W. Hudson58ee2af2003-04-29 16:18:47 +00001279 if (f->f_stacktop != NULL) {
1280 stack_pointer = f->f_stacktop;
1281 f->f_stacktop = NULL;
1282 }
1283 if (err) {
1284 /* trace function raised an exception */
1285 goto on_error;
1286 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001287 }
1288
1289 /* Extract opcode and argument */
1290
Guido van Rossum374a9221991-04-04 10:40:29 +00001291 opcode = NEXTOP();
Armin Rigo8817fcd2004-06-17 10:22:40 +00001292 oparg = 0; /* allows oparg to be stored in a register because
1293 it doesn't have to be remembered across a full loop */
Raymond Hettinger5bed4562004-04-10 23:34:17 +00001294 if (HAS_ARG(opcode))
1295 oparg = NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00001296 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001297#ifdef DYNAMIC_EXECUTION_PROFILE
1298#ifdef DXPAIRS
1299 dxpairs[lastopcode][opcode]++;
1300 lastopcode = opcode;
1301#endif
1302 dxp[opcode]++;
1303#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001304
Guido van Rossum96a42c81992-01-12 02:29:51 +00001305#ifdef LLTRACE
Guido van Rossum374a9221991-04-04 10:40:29 +00001306 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001307
Guido van Rossum96a42c81992-01-12 02:29:51 +00001308 if (lltrace) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001309 if (HAS_ARG(opcode)) {
1310 printf("%d: %d, %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001311 f->f_lasti, opcode, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001312 }
1313 else {
1314 printf("%d: %d\n",
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001315 f->f_lasti, opcode);
Guido van Rossum374a9221991-04-04 10:40:29 +00001316 }
1317 }
1318#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001319
Guido van Rossum374a9221991-04-04 10:40:29 +00001320 /* Main switch on opcode */
Michael W. Hudson75eabd22005-01-18 15:56:11 +00001321 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001322
Guido van Rossum374a9221991-04-04 10:40:29 +00001323 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001324
Guido van Rossum374a9221991-04-04 10:40:29 +00001325 /* BEWARE!
1326 It is essential that any operation that fails sets either
1327 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1328 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001329
Guido van Rossum374a9221991-04-04 10:40:29 +00001330 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001331
Antoine Pitroub52ec782009-01-25 16:34:23 +00001332 TARGET(NOP)
1333 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001334
Antoine Pitroub52ec782009-01-25 16:34:23 +00001335 TARGET(LOAD_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001336 x = GETLOCAL(oparg);
1337 if (x != NULL) {
1338 Py_INCREF(x);
1339 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001340 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001341 }
1342 format_exc_check_arg(PyExc_UnboundLocalError,
1343 UNBOUNDLOCAL_ERROR_MSG,
1344 PyTuple_GetItem(co->co_varnames, oparg));
1345 break;
1346
Antoine Pitroub52ec782009-01-25 16:34:23 +00001347 TARGET(LOAD_CONST)
Skip Montanaro04d80f82002-08-04 21:03:35 +00001348 x = GETITEM(consts, oparg);
Neil Schemenauer63543862002-02-17 19:10:14 +00001349 Py_INCREF(x);
1350 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001351 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001352
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001353 PREDICTED_WITH_ARG(STORE_FAST);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001354 TARGET(STORE_FAST)
Neil Schemenauer63543862002-02-17 19:10:14 +00001355 v = POP();
1356 SETLOCAL(oparg, v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001357 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001358
Antoine Pitroub52ec782009-01-25 16:34:23 +00001359 TARGET(POP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001360 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001361 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001362 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001363
Antoine Pitroub52ec782009-01-25 16:34:23 +00001364 TARGET(ROT_TWO)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001365 v = TOP();
1366 w = SECOND();
1367 SET_TOP(w);
1368 SET_SECOND(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001369 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Antoine Pitroub52ec782009-01-25 16:34:23 +00001371 TARGET(ROT_THREE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001372 v = TOP();
1373 w = SECOND();
1374 x = THIRD();
1375 SET_TOP(w);
1376 SET_SECOND(x);
1377 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001378 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001379
Antoine Pitroub52ec782009-01-25 16:34:23 +00001380 TARGET(ROT_FOUR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001381 u = TOP();
1382 v = SECOND();
1383 w = THIRD();
1384 x = FOURTH();
1385 SET_TOP(v);
1386 SET_SECOND(w);
1387 SET_THIRD(x);
1388 SET_FOURTH(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001389 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Antoine Pitroub52ec782009-01-25 16:34:23 +00001391 TARGET(DUP_TOP)
Guido van Rossum374a9221991-04-04 10:40:29 +00001392 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001393 Py_INCREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001394 PUSH(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001395 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Antoine Pitroub52ec782009-01-25 16:34:23 +00001397 TARGET(DUP_TOPX)
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001398 if (oparg == 2) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001399 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001400 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001401 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001402 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001403 STACKADJ(2);
1404 SET_TOP(x);
1405 SET_SECOND(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001406 FAST_DISPATCH();
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001407 } else if (oparg == 3) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00001408 x = TOP();
Tim Peters35ba6892000-10-11 07:04:49 +00001409 Py_INCREF(x);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001410 w = SECOND();
Tim Peters35ba6892000-10-11 07:04:49 +00001411 Py_INCREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001412 v = THIRD();
Tim Peters35ba6892000-10-11 07:04:49 +00001413 Py_INCREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001414 STACKADJ(3);
1415 SET_TOP(x);
1416 SET_SECOND(w);
1417 SET_THIRD(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001418 FAST_DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001419 }
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00001420 Py_FatalError("invalid argument to DUP_TOPX"
1421 " (bytecode corruption?)");
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00001422 /* Never returns, so don't bother to set why. */
Tim Peters35ba6892000-10-11 07:04:49 +00001423 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001424
Antoine Pitroub52ec782009-01-25 16:34:23 +00001425 TARGET(UNARY_POSITIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001426 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001427 x = PyNumber_Positive(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001428 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001429 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001430 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001431 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Antoine Pitroub52ec782009-01-25 16:34:23 +00001433 TARGET(UNARY_NEGATIVE)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001434 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001435 x = PyNumber_Negative(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001436 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001437 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001438 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Antoine Pitroub52ec782009-01-25 16:34:23 +00001441 TARGET(UNARY_NOT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001442 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001443 err = PyObject_IsTrue(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001444 Py_DECREF(v);
Guido van Rossumfc490731997-05-06 15:06:49 +00001445 if (err == 0) {
1446 Py_INCREF(Py_True);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001447 SET_TOP(Py_True);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001448 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001449 }
1450 else if (err > 0) {
1451 Py_INCREF(Py_False);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001452 SET_TOP(Py_False);
Guido van Rossumfc490731997-05-06 15:06:49 +00001453 err = 0;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001454 DISPATCH();
Guido van Rossumfc490731997-05-06 15:06:49 +00001455 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00001456 STACKADJ(-1);
Guido van Rossum374a9221991-04-04 10:40:29 +00001457 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Antoine Pitroub52ec782009-01-25 16:34:23 +00001459 TARGET(UNARY_INVERT)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001460 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001461 x = PyNumber_Invert(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00001462 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001463 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001464 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001465 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001466
Antoine Pitroub52ec782009-01-25 16:34:23 +00001467 TARGET(BINARY_POWER)
Guido van Rossum50564e81996-01-12 01:13:16 +00001468 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001469 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001470 x = PyNumber_Power(v, w, Py_None);
Guido van Rossumb209a111997-04-29 18:18:01 +00001471 Py_DECREF(v);
1472 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001473 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001474 if (x != NULL) DISPATCH();
Guido van Rossum50564e81996-01-12 01:13:16 +00001475 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Antoine Pitroub52ec782009-01-25 16:34:23 +00001477 TARGET(BINARY_MULTIPLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001478 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001479 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001480 x = PyNumber_Multiply(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001481 Py_DECREF(v);
1482 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001483 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001484 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001485 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001486
Antoine Pitroub52ec782009-01-25 16:34:23 +00001487 TARGET(BINARY_TRUE_DIVIDE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001488 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001489 v = TOP();
Tim Peters3caca232001-12-06 06:23:26 +00001490 x = PyNumber_TrueDivide(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001491 Py_DECREF(v);
1492 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001493 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001494 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001495 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Antoine Pitroub52ec782009-01-25 16:34:23 +00001497 TARGET(BINARY_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001498 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001499 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001500 x = PyNumber_FloorDivide(v, w);
1501 Py_DECREF(v);
1502 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001503 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001504 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001505 break;
1506
Antoine Pitroub52ec782009-01-25 16:34:23 +00001507 TARGET(BINARY_MODULO)
Guido van Rossum374a9221991-04-04 10:40:29 +00001508 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001509 v = TOP();
Benjamin Petersonefb06b02009-02-26 18:55:48 +00001510 if (PyUnicode_CheckExact(v))
1511 x = PyUnicode_Format(v, w);
1512 else
1513 x = PyNumber_Remainder(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001514 Py_DECREF(v);
1515 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001516 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001517 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001518 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001519
Antoine Pitroub52ec782009-01-25 16:34:23 +00001520 TARGET(BINARY_ADD)
Guido van Rossum374a9221991-04-04 10:40:29 +00001521 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001522 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001523 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001524 PyUnicode_CheckExact(w)) {
1525 x = unicode_concatenate(v, w, f, next_instr);
1526 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001527 goto skip_decref_vx;
1528 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001529 else {
Guido van Rossumc12da691997-07-17 23:12:42 +00001530 x = PyNumber_Add(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001531 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001532 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001533 skip_decref_vx:
Guido van Rossumb209a111997-04-29 18:18:01 +00001534 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001535 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001536 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001537 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001538
Antoine Pitroub52ec782009-01-25 16:34:23 +00001539 TARGET(BINARY_SUBTRACT)
Guido van Rossum374a9221991-04-04 10:40:29 +00001540 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001541 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001542 x = PyNumber_Subtract(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001543 Py_DECREF(v);
1544 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001545 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001546 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001547 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Antoine Pitroub52ec782009-01-25 16:34:23 +00001549 TARGET(BINARY_SUBSCR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001550 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001551 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001552 x = PyObject_GetItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001553 Py_DECREF(v);
1554 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001555 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001556 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001557 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Antoine Pitroub52ec782009-01-25 16:34:23 +00001559 TARGET(BINARY_LSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001560 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001561 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001562 x = PyNumber_Lshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001563 Py_DECREF(v);
1564 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001565 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001566 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001567 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001568
Antoine Pitroub52ec782009-01-25 16:34:23 +00001569 TARGET(BINARY_RSHIFT)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001570 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001571 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001572 x = PyNumber_Rshift(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001573 Py_DECREF(v);
1574 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001575 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001576 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001577 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001578
Antoine Pitroub52ec782009-01-25 16:34:23 +00001579 TARGET(BINARY_AND)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001580 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001581 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001582 x = PyNumber_And(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001583 Py_DECREF(v);
1584 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001585 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001586 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001587 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Antoine Pitroub52ec782009-01-25 16:34:23 +00001589 TARGET(BINARY_XOR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001590 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001591 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001592 x = PyNumber_Xor(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001593 Py_DECREF(v);
1594 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001595 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001596 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001597 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Antoine Pitroub52ec782009-01-25 16:34:23 +00001599 TARGET(BINARY_OR)
Guido van Rossum7928cd71991-10-24 14:59:31 +00001600 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001601 v = TOP();
Guido van Rossumfc490731997-05-06 15:06:49 +00001602 x = PyNumber_Or(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001603 Py_DECREF(v);
1604 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001605 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001606 if (x != NULL) DISPATCH();
Guido van Rossum7928cd71991-10-24 14:59:31 +00001607 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001608
Antoine Pitroub52ec782009-01-25 16:34:23 +00001609 TARGET(LIST_APPEND)
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001610 w = POP();
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001611 v = PEEK(oparg);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001612 err = PyList_Append(v, w);
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001613 Py_DECREF(w);
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001614 if (err == 0) {
1615 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001616 DISPATCH();
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00001617 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001618 break;
1619
Antoine Pitroub52ec782009-01-25 16:34:23 +00001620 TARGET(SET_ADD)
Nick Coghlan650f0d02007-04-15 12:05:43 +00001621 w = POP();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00001622 v = stack_pointer[-oparg];
Nick Coghlan650f0d02007-04-15 12:05:43 +00001623 err = PySet_Add(v, w);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001624 Py_DECREF(w);
1625 if (err == 0) {
1626 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001627 DISPATCH();
Nick Coghlan650f0d02007-04-15 12:05:43 +00001628 }
1629 break;
1630
Antoine Pitroub52ec782009-01-25 16:34:23 +00001631 TARGET(INPLACE_POWER)
Thomas Wouters434d0822000-08-24 20:11:32 +00001632 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001633 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001634 x = PyNumber_InPlacePower(v, w, Py_None);
1635 Py_DECREF(v);
1636 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001637 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001638 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001639 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001640
Antoine Pitroub52ec782009-01-25 16:34:23 +00001641 TARGET(INPLACE_MULTIPLY)
Thomas Wouters434d0822000-08-24 20:11:32 +00001642 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001643 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001644 x = PyNumber_InPlaceMultiply(v, w);
1645 Py_DECREF(v);
1646 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001647 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001648 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001649 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Antoine Pitroub52ec782009-01-25 16:34:23 +00001651 TARGET(INPLACE_TRUE_DIVIDE)
Thomas Wouters434d0822000-08-24 20:11:32 +00001652 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001653 v = TOP();
Tim Peters54b11912001-12-25 18:49:11 +00001654 x = PyNumber_InPlaceTrueDivide(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001655 Py_DECREF(v);
1656 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001657 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001658 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001659 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001660
Antoine Pitroub52ec782009-01-25 16:34:23 +00001661 TARGET(INPLACE_FLOOR_DIVIDE)
Guido van Rossum4668b002001-08-08 05:00:18 +00001662 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001663 v = TOP();
Guido van Rossum4668b002001-08-08 05:00:18 +00001664 x = PyNumber_InPlaceFloorDivide(v, w);
1665 Py_DECREF(v);
1666 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001667 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001668 if (x != NULL) DISPATCH();
Guido van Rossum4668b002001-08-08 05:00:18 +00001669 break;
1670
Antoine Pitroub52ec782009-01-25 16:34:23 +00001671 TARGET(INPLACE_MODULO)
Thomas Wouters434d0822000-08-24 20:11:32 +00001672 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001673 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001674 x = PyNumber_InPlaceRemainder(v, w);
1675 Py_DECREF(v);
1676 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001677 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001678 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001679 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001680
Antoine Pitroub52ec782009-01-25 16:34:23 +00001681 TARGET(INPLACE_ADD)
Thomas Wouters434d0822000-08-24 20:11:32 +00001682 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001683 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001684 if (PyUnicode_CheckExact(v) &&
Guido van Rossum98297ee2007-11-06 21:34:58 +00001685 PyUnicode_CheckExact(w)) {
1686 x = unicode_concatenate(v, w, f, next_instr);
1687 /* unicode_concatenate consumed the ref to v */
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001688 goto skip_decref_v;
1689 }
Guido van Rossum87780df2001-08-23 02:58:07 +00001690 else {
Thomas Wouters434d0822000-08-24 20:11:32 +00001691 x = PyNumber_InPlaceAdd(v, w);
Guido van Rossum87780df2001-08-23 02:58:07 +00001692 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001693 Py_DECREF(v);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00001694 skip_decref_v:
Thomas Wouters434d0822000-08-24 20:11:32 +00001695 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001696 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001697 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001698 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Antoine Pitroub52ec782009-01-25 16:34:23 +00001700 TARGET(INPLACE_SUBTRACT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001701 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001702 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00001703 x = PyNumber_InPlaceSubtract(v, w);
Thomas Wouters434d0822000-08-24 20:11:32 +00001704 Py_DECREF(v);
1705 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001706 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001707 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001708 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001709
Antoine Pitroub52ec782009-01-25 16:34:23 +00001710 TARGET(INPLACE_LSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001711 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001712 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001713 x = PyNumber_InPlaceLshift(v, w);
1714 Py_DECREF(v);
1715 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001716 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001717 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001718 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001719
Antoine Pitroub52ec782009-01-25 16:34:23 +00001720 TARGET(INPLACE_RSHIFT)
Thomas Wouters434d0822000-08-24 20:11:32 +00001721 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001722 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001723 x = PyNumber_InPlaceRshift(v, w);
1724 Py_DECREF(v);
1725 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001726 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001727 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Antoine Pitroub52ec782009-01-25 16:34:23 +00001730 TARGET(INPLACE_AND)
Thomas Wouters434d0822000-08-24 20:11:32 +00001731 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001732 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001733 x = PyNumber_InPlaceAnd(v, w);
1734 Py_DECREF(v);
1735 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001736 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001737 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001738 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Antoine Pitroub52ec782009-01-25 16:34:23 +00001740 TARGET(INPLACE_XOR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001741 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001742 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001743 x = PyNumber_InPlaceXor(v, w);
1744 Py_DECREF(v);
1745 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001746 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001747 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001748 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001749
Antoine Pitroub52ec782009-01-25 16:34:23 +00001750 TARGET(INPLACE_OR)
Thomas Wouters434d0822000-08-24 20:11:32 +00001751 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00001752 v = TOP();
Thomas Wouters434d0822000-08-24 20:11:32 +00001753 x = PyNumber_InPlaceOr(v, w);
1754 Py_DECREF(v);
1755 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001756 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001757 if (x != NULL) DISPATCH();
Thomas Wouters434d0822000-08-24 20:11:32 +00001758 break;
1759
Antoine Pitroub52ec782009-01-25 16:34:23 +00001760 TARGET(STORE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001761 w = TOP();
1762 v = SECOND();
1763 u = THIRD();
1764 STACKADJ(-3);
Guido van Rossum374a9221991-04-04 10:40:29 +00001765 /* v[w] = u */
Guido van Rossumfc490731997-05-06 15:06:49 +00001766 err = PyObject_SetItem(v, w, u);
Guido van Rossumb209a111997-04-29 18:18:01 +00001767 Py_DECREF(u);
1768 Py_DECREF(v);
1769 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001770 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001771 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001772
Antoine Pitroub52ec782009-01-25 16:34:23 +00001773 TARGET(DELETE_SUBSCR)
Raymond Hettinger663004b2003-01-09 15:24:30 +00001774 w = TOP();
1775 v = SECOND();
1776 STACKADJ(-2);
Guido van Rossum374a9221991-04-04 10:40:29 +00001777 /* del v[w] */
Guido van Rossumfc490731997-05-06 15:06:49 +00001778 err = PyObject_DelItem(v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00001779 Py_DECREF(v);
1780 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001781 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00001782 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001783
Antoine Pitroub52ec782009-01-25 16:34:23 +00001784 TARGET(PRINT_EXPR)
Guido van Rossum374a9221991-04-04 10:40:29 +00001785 v = POP();
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001786 w = PySys_GetObject("displayhook");
1787 if (w == NULL) {
1788 PyErr_SetString(PyExc_RuntimeError,
1789 "lost sys.displayhook");
1790 err = -1;
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001791 x = NULL;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001792 }
1793 if (err == 0) {
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001794 x = PyTuple_Pack(1, v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001795 if (x == NULL)
1796 err = -1;
1797 }
1798 if (err == 0) {
1799 w = PyEval_CallObject(w, x);
Moshe Zadkaf5df3832001-01-11 11:55:37 +00001800 Py_XDECREF(w);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001801 if (w == NULL)
1802 err = -1;
Guido van Rossum374a9221991-04-04 10:40:29 +00001803 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001804 Py_DECREF(v);
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001805 Py_XDECREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001806 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001807
Thomas Wouters434d0822000-08-24 20:11:32 +00001808#ifdef CASE_TOO_BIG
1809 default: switch (opcode) {
1810#endif
Antoine Pitroub52ec782009-01-25 16:34:23 +00001811 TARGET(RAISE_VARARGS)
Collin Winter828f04a2007-08-31 00:04:24 +00001812 v = w = NULL;
Guido van Rossumf10570b1995-07-07 22:53:21 +00001813 switch (oparg) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00001814 case 2:
1815 v = POP(); /* cause */
Guido van Rossumf10570b1995-07-07 22:53:21 +00001816 case 1:
1817 w = POP(); /* exc */
Guido van Rossumd295f121998-04-09 21:39:57 +00001818 case 0: /* Fallthrough */
Collin Winter828f04a2007-08-31 00:04:24 +00001819 why = do_raise(w, v);
Guido van Rossumf10570b1995-07-07 22:53:21 +00001820 break;
1821 default:
Guido van Rossumb209a111997-04-29 18:18:01 +00001822 PyErr_SetString(PyExc_SystemError,
Guido van Rossumf10570b1995-07-07 22:53:21 +00001823 "bad RAISE_VARARGS oparg");
Guido van Rossumf10570b1995-07-07 22:53:21 +00001824 why = WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00001825 break;
1826 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001827 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Antoine Pitroub52ec782009-01-25 16:34:23 +00001829 TARGET(STORE_LOCALS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001830 x = POP();
1831 v = f->f_locals;
1832 Py_XDECREF(v);
1833 f->f_locals = x;
Antoine Pitroub52ec782009-01-25 16:34:23 +00001834 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001835
Antoine Pitroub52ec782009-01-25 16:34:23 +00001836 TARGET(RETURN_VALUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001837 retval = POP();
1838 why = WHY_RETURN;
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001839 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001840
Antoine Pitroub52ec782009-01-25 16:34:23 +00001841 TARGET(YIELD_VALUE)
Tim Peters5ca576e2001-06-18 22:08:13 +00001842 retval = POP();
Tim Peters8c963692001-06-23 05:26:56 +00001843 f->f_stacktop = stack_pointer;
Tim Peters5ca576e2001-06-18 22:08:13 +00001844 why = WHY_YIELD;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001845 /* Put aside the current exception state and restore
1846 that of the calling frame. This only serves when
1847 "yield" is used inside an except handler. */
1848 SWAP_EXC_STATE();
Raymond Hettinger1dd83092004-02-06 18:32:33 +00001849 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001850
Antoine Pitroub52ec782009-01-25 16:34:23 +00001851 TARGET(POP_EXCEPT)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001852 {
1853 PyTryBlock *b = PyFrame_BlockPop(f);
1854 if (b->b_type != EXCEPT_HANDLER) {
1855 PyErr_SetString(PyExc_SystemError,
1856 "popped block is not an except handler");
1857 why = WHY_EXCEPTION;
1858 break;
1859 }
1860 UNWIND_EXCEPT_HANDLER(b);
1861 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001862 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001863
Antoine Pitroub52ec782009-01-25 16:34:23 +00001864 TARGET(POP_BLOCK)
Guido van Rossum374a9221991-04-04 10:40:29 +00001865 {
Guido van Rossumb209a111997-04-29 18:18:01 +00001866 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001867 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00001868 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001869 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001870
Christian Heimes180510d2008-03-03 19:15:45 +00001871 PREDICTED(END_FINALLY);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001872 TARGET(END_FINALLY)
Guido van Rossum374a9221991-04-04 10:40:29 +00001873 v = POP();
Christian Heimes217cfd12007-12-02 14:31:20 +00001874 if (PyLong_Check(v)) {
1875 why = (enum why_code) PyLong_AS_LONG(v);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001876 assert(why != WHY_YIELD);
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00001877 if (why == WHY_RETURN ||
1878 why == WHY_CONTINUE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001879 retval = POP();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001880 if (why == WHY_SILENCED) {
1881 /* An exception was silenced by 'with', we must
1882 manually unwind the EXCEPT_HANDLER block which was
1883 created when the exception was caught, otherwise
1884 the stack will be in an inconsistent state. */
1885 PyTryBlock *b = PyFrame_BlockPop(f);
Benjamin Petersonac8c7302009-06-28 16:03:15 +00001886 assert(b->b_type == EXCEPT_HANDLER);
1887 UNWIND_EXCEPT_HANDLER(b);
1888 why = WHY_NOT;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001889 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001890 }
Brett Cannonf74225d2007-02-26 21:10:16 +00001891 else if (PyExceptionClass_Check(v)) {
Guido van Rossum374a9221991-04-04 10:40:29 +00001892 w = POP();
Guido van Rossumf10570b1995-07-07 22:53:21 +00001893 u = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00001894 PyErr_Restore(v, w, u);
Guido van Rossum374a9221991-04-04 10:40:29 +00001895 why = WHY_RERAISE;
Guido van Rossum0db1ef91995-07-28 23:06:00 +00001896 break;
Guido van Rossum374a9221991-04-04 10:40:29 +00001897 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001898 else if (v != Py_None) {
1899 PyErr_SetString(PyExc_SystemError,
Guido van Rossum374a9221991-04-04 10:40:29 +00001900 "'finally' pops bad exception");
1901 why = WHY_EXCEPTION;
1902 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001903 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001904 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001905
Antoine Pitroub52ec782009-01-25 16:34:23 +00001906 TARGET(LOAD_BUILD_CLASS)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00001907 x = PyDict_GetItemString(f->f_builtins,
1908 "__build_class__");
1909 if (x == NULL) {
1910 PyErr_SetString(PyExc_ImportError,
1911 "__build_class__ not found");
1912 break;
1913 }
1914 Py_INCREF(x);
1915 PUSH(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00001916 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001917
Antoine Pitroub52ec782009-01-25 16:34:23 +00001918 TARGET(STORE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001919 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00001920 v = POP();
Raymond Hettinger467a6982004-04-07 11:39:21 +00001921 if ((x = f->f_locals) != NULL) {
Raymond Hettinger66bd2332004-08-02 08:30:07 +00001922 if (PyDict_CheckExact(x))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001923 err = PyDict_SetItem(x, w, v);
1924 else
1925 err = PyObject_SetItem(x, w, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001926 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001927 if (err == 0) DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00001928 break;
1929 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001930 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001931 "no locals found when storing %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001932 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001933
Antoine Pitroub52ec782009-01-25 16:34:23 +00001934 TARGET(DELETE_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00001935 w = GETITEM(names, oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00001936 if ((x = f->f_locals) != NULL) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00001937 if ((err = PyObject_DelItem(x, w)) != 0)
Raymond Hettinger467a6982004-04-07 11:39:21 +00001938 format_exc_check_arg(PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001939 NAME_ERROR_MSG,
1940 w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001941 break;
1942 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00001943 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00001944 "no locals when deleting %R", w);
Guido van Rossum374a9221991-04-04 10:40:29 +00001945 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001946
Raymond Hettinger7dc52212003-03-16 20:14:44 +00001947 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001948 TARGET(UNPACK_SEQUENCE)
Guido van Rossum374a9221991-04-04 10:40:29 +00001949 v = POP();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001950 if (PyTuple_CheckExact(v) &&
1951 PyTuple_GET_SIZE(v) == oparg) {
1952 PyObject **items = \
1953 ((PyTupleObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001954 while (oparg--) {
1955 w = items[oparg];
1956 Py_INCREF(w);
1957 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001958 }
Raymond Hettinger7eddd782004-04-07 14:38:08 +00001959 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00001960 DISPATCH();
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001961 } else if (PyList_CheckExact(v) &&
1962 PyList_GET_SIZE(v) == oparg) {
1963 PyObject **items = \
1964 ((PyListObject *)v)->ob_item;
Raymond Hettingerf114a3a2004-03-08 23:25:30 +00001965 while (oparg--) {
1966 w = items[oparg];
1967 Py_INCREF(w);
1968 PUSH(w);
Barry Warsawe42b18f1997-08-25 22:13:04 +00001969 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001970 } else if (unpack_iterable(v, oparg, -1,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00001971 stack_pointer + oparg)) {
Benjamin Peterson6d46a912009-06-28 16:17:34 +00001972 STACKADJ(oparg);
Guido van Rossumd8faa362007-04-27 19:54:29 +00001973 } else {
1974 /* unpack_iterable() raised an exception */
Barry Warsawe42b18f1997-08-25 22:13:04 +00001975 why = WHY_EXCEPTION;
Tim Peters8b13b3e2001-09-30 05:58:42 +00001976 }
Guido van Rossumb209a111997-04-29 18:18:01 +00001977 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00001978 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001979
Antoine Pitroub52ec782009-01-25 16:34:23 +00001980 TARGET(UNPACK_EX)
Guido van Rossum0368b722007-05-11 16:50:42 +00001981 {
1982 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1983 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001984
Guido van Rossum0368b722007-05-11 16:50:42 +00001985 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1986 stack_pointer + totalargs)) {
1987 stack_pointer += totalargs;
1988 } else {
1989 why = WHY_EXCEPTION;
1990 }
1991 Py_DECREF(v);
1992 break;
1993 }
1994
Antoine Pitroub52ec782009-01-25 16:34:23 +00001995 TARGET(STORE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00001996 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00001997 v = TOP();
1998 u = SECOND();
1999 STACKADJ(-2);
Guido van Rossumb209a111997-04-29 18:18:01 +00002000 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2001 Py_DECREF(v);
2002 Py_DECREF(u);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002003 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002004 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002005
Antoine Pitroub52ec782009-01-25 16:34:23 +00002006 TARGET(DELETE_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002007 w = GETITEM(names, oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002008 v = POP();
Guido van Rossuma027efa1997-05-05 20:56:21 +00002009 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2010 /* del v.w */
Guido van Rossumb209a111997-04-29 18:18:01 +00002011 Py_DECREF(v);
Guido van Rossum374a9221991-04-04 10:40:29 +00002012 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002013
Antoine Pitroub52ec782009-01-25 16:34:23 +00002014 TARGET(STORE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002015 w = GETITEM(names, oparg);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002016 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002017 err = PyDict_SetItem(f->f_globals, w, v);
2018 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002019 if (err == 0) DISPATCH();
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002020 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002021
Antoine Pitroub52ec782009-01-25 16:34:23 +00002022 TARGET(DELETE_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002023 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002024 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
Paul Prescode68140d2000-08-30 20:25:01 +00002025 format_exc_check_arg(
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002026 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum32c6cdf1991-12-10 13:52:46 +00002027 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002028
Antoine Pitroub52ec782009-01-25 16:34:23 +00002029 TARGET(LOAD_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002030 w = GETITEM(names, oparg);
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002031 if ((v = f->f_locals) == NULL) {
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002032 PyErr_Format(PyExc_SystemError,
Guido van Rossum98297ee2007-11-06 21:34:58 +00002033 "no locals when loading %R", w);
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002034 why = WHY_EXCEPTION;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002035 break;
2036 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002037 if (PyDict_CheckExact(v)) {
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002038 x = PyDict_GetItem(v, w);
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002039 Py_XINCREF(x);
2040 }
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002041 else {
2042 x = PyObject_GetItem(v, w);
2043 if (x == NULL && PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002044 if (!PyErr_ExceptionMatches(
2045 PyExc_KeyError))
Raymond Hettinger214b1c32004-07-02 06:41:07 +00002046 break;
2047 PyErr_Clear();
2048 }
2049 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002050 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002051 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002052 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002053 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002054 if (x == NULL) {
Paul Prescode68140d2000-08-30 20:25:01 +00002055 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002056 PyExc_NameError,
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002057 NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002058 break;
2059 }
2060 }
Michael W. Hudsona3711f72004-08-02 14:50:43 +00002061 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002062 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002063 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002064 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002065
Antoine Pitroub52ec782009-01-25 16:34:23 +00002066 TARGET(LOAD_GLOBAL)
Skip Montanaro496e6582002-08-06 17:47:40 +00002067 w = GETITEM(names, oparg);
Neal Norwitzda059e32007-08-26 05:33:45 +00002068 if (PyUnicode_CheckExact(w)) {
Guido van Rossumd8dbf842002-08-19 21:17:53 +00002069 /* Inline the PyDict_GetItem() calls.
2070 WARNING: this is an extreme speed hack.
2071 Do not try this at home. */
Neal Norwitzda059e32007-08-26 05:33:45 +00002072 long hash = ((PyUnicodeObject *)w)->hash;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002073 if (hash != -1) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002074 PyDictObject *d;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002075 PyDictEntry *e;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002076 d = (PyDictObject *)(f->f_globals);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002077 e = d->ma_lookup(d, w, hash);
2078 if (e == NULL) {
2079 x = NULL;
2080 break;
2081 }
2082 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002083 if (x != NULL) {
2084 Py_INCREF(x);
2085 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002086 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002087 }
2088 d = (PyDictObject *)(f->f_builtins);
Thomas Wouters4d70c3d2006-06-08 14:42:34 +00002089 e = d->ma_lookup(d, w, hash);
2090 if (e == NULL) {
2091 x = NULL;
2092 break;
2093 }
2094 x = e->me_value;
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002095 if (x != NULL) {
2096 Py_INCREF(x);
2097 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002098 DISPATCH();
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002099 }
2100 goto load_global_error;
2101 }
2102 }
2103 /* This is the un-inlined version of the code above */
Guido van Rossumb209a111997-04-29 18:18:01 +00002104 x = PyDict_GetItem(f->f_globals, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002105 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002106 x = PyDict_GetItem(f->f_builtins, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002107 if (x == NULL) {
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002108 load_global_error:
Paul Prescode68140d2000-08-30 20:25:01 +00002109 format_exc_check_arg(
Guido van Rossumac7be682001-01-17 15:42:30 +00002110 PyExc_NameError,
Guido van Rossum3a4dfc82002-08-19 20:24:07 +00002111 GLOBAL_NAME_ERROR_MSG, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002112 break;
2113 }
2114 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002115 Py_INCREF(x);
Guido van Rossum374a9221991-04-04 10:40:29 +00002116 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002117 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002118
Antoine Pitroub52ec782009-01-25 16:34:23 +00002119 TARGET(DELETE_FAST)
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002120 x = GETLOCAL(oparg);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002121 if (x != NULL) {
2122 SETLOCAL(oparg, NULL);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002123 DISPATCH();
Guido van Rossum2e4c8991998-05-12 20:27:36 +00002124 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002125 format_exc_check_arg(
2126 PyExc_UnboundLocalError,
2127 UNBOUNDLOCAL_ERROR_MSG,
2128 PyTuple_GetItem(co->co_varnames, oparg)
2129 );
2130 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002131
Antoine Pitroub52ec782009-01-25 16:34:23 +00002132 TARGET(LOAD_CLOSURE)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002133 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002134 Py_INCREF(x);
2135 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002136 if (x != NULL) DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002137 break;
2138
Antoine Pitroub52ec782009-01-25 16:34:23 +00002139 TARGET(LOAD_DEREF)
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002140 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002141 w = PyCell_Get(x);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002142 if (w != NULL) {
2143 PUSH(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002144 DISPATCH();
Jeremy Hylton2524d692001-02-05 17:23:16 +00002145 }
Raymond Hettinger467a6982004-04-07 11:39:21 +00002146 err = -1;
2147 /* Don't stomp existing exception */
2148 if (PyErr_Occurred())
2149 break;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002150 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2151 v = PyTuple_GET_ITEM(co->co_cellvars,
Raymond Hettinger467a6982004-04-07 11:39:21 +00002152 oparg);
2153 format_exc_check_arg(
2154 PyExc_UnboundLocalError,
2155 UNBOUNDLOCAL_ERROR_MSG,
2156 v);
2157 } else {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002158 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2159 PyTuple_GET_SIZE(co->co_cellvars));
2160 format_exc_check_arg(PyExc_NameError,
2161 UNBOUNDFREE_ERROR_MSG, v);
Raymond Hettinger467a6982004-04-07 11:39:21 +00002162 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002163 break;
2164
Antoine Pitroub52ec782009-01-25 16:34:23 +00002165 TARGET(STORE_DEREF)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002166 w = POP();
Jeremy Hylton2b724da2001-01-29 22:51:52 +00002167 x = freevars[oparg];
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002168 PyCell_Set(x, w);
Jeremy Hylton30c9f392001-03-13 01:58:22 +00002169 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002170 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002171
Antoine Pitroub52ec782009-01-25 16:34:23 +00002172 TARGET(BUILD_TUPLE)
Guido van Rossumb209a111997-04-29 18:18:01 +00002173 x = PyTuple_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002174 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002175 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002176 w = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002177 PyTuple_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002178 }
2179 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002180 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002181 }
2182 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002183
Antoine Pitroub52ec782009-01-25 16:34:23 +00002184 TARGET(BUILD_LIST)
Guido van Rossumb209a111997-04-29 18:18:01 +00002185 x = PyList_New(oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002186 if (x != NULL) {
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002187 for (; --oparg >= 0;) {
Guido van Rossum374a9221991-04-04 10:40:29 +00002188 w = POP();
Guido van Rossum5053efc1998-08-04 15:27:50 +00002189 PyList_SET_ITEM(x, oparg, w);
Guido van Rossum374a9221991-04-04 10:40:29 +00002190 }
2191 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002192 DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002193 }
2194 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002195
Antoine Pitroub52ec782009-01-25 16:34:23 +00002196 TARGET(BUILD_SET)
Guido van Rossum86e58e22006-08-28 15:27:34 +00002197 x = PySet_New(NULL);
2198 if (x != NULL) {
2199 for (; --oparg >= 0;) {
2200 w = POP();
2201 if (err == 0)
2202 err = PySet_Add(x, w);
2203 Py_DECREF(w);
2204 }
2205 if (err != 0) {
2206 Py_DECREF(x);
2207 break;
2208 }
2209 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002210 DISPATCH();
Guido van Rossum86e58e22006-08-28 15:27:34 +00002211 }
2212 break;
2213
Antoine Pitroub52ec782009-01-25 16:34:23 +00002214 TARGET(BUILD_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002215 x = _PyDict_NewPresized((Py_ssize_t)oparg);
Guido van Rossum374a9221991-04-04 10:40:29 +00002216 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002217 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Antoine Pitroub52ec782009-01-25 16:34:23 +00002220 TARGET(STORE_MAP)
Christian Heimes99170a52007-12-19 02:07:34 +00002221 w = TOP(); /* key */
2222 u = SECOND(); /* value */
2223 v = THIRD(); /* dict */
2224 STACKADJ(-2);
2225 assert (PyDict_CheckExact(v));
2226 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2227 Py_DECREF(u);
2228 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002229 if (err == 0) DISPATCH();
Christian Heimes99170a52007-12-19 02:07:34 +00002230 break;
2231
Antoine Pitroub52ec782009-01-25 16:34:23 +00002232 TARGET(MAP_ADD)
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002233 w = TOP(); /* key */
2234 u = SECOND(); /* value */
2235 STACKADJ(-2);
2236 v = stack_pointer[-oparg]; /* dict */
2237 assert (PyDict_CheckExact(v));
2238 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2239 Py_DECREF(u);
2240 Py_DECREF(w);
2241 if (err == 0) {
2242 PREDICT(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002243 DISPATCH();
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002244 }
2245 break;
2246
Antoine Pitroub52ec782009-01-25 16:34:23 +00002247 TARGET(LOAD_ATTR)
Skip Montanaro496e6582002-08-06 17:47:40 +00002248 w = GETITEM(names, oparg);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002249 v = TOP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002250 x = PyObject_GetAttr(v, w);
2251 Py_DECREF(v);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002252 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002253 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002254 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002255
Antoine Pitroub52ec782009-01-25 16:34:23 +00002256 TARGET(COMPARE_OP)
Guido van Rossum374a9221991-04-04 10:40:29 +00002257 w = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002258 v = TOP();
Martin v. Löwis3267f872007-12-04 20:34:12 +00002259 x = cmp_outcome(oparg, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002260 Py_DECREF(v);
2261 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002262 SET_TOP(x);
Raymond Hettingerf606f872003-03-16 03:11:04 +00002263 if (x == NULL) break;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002264 PREDICT(POP_JUMP_IF_FALSE);
2265 PREDICT(POP_JUMP_IF_TRUE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002266 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002267
Antoine Pitroub52ec782009-01-25 16:34:23 +00002268 TARGET(IMPORT_NAME)
Skip Montanaro496e6582002-08-06 17:47:40 +00002269 w = GETITEM(names, oparg);
Guido van Rossumb209a111997-04-29 18:18:01 +00002270 x = PyDict_GetItemString(f->f_builtins, "__import__");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002271 if (x == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002272 PyErr_SetString(PyExc_ImportError,
Guido van Rossumfc490731997-05-06 15:06:49 +00002273 "__import__ not found");
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002274 break;
2275 }
Christian Heimesbbffeb62008-01-24 09:42:52 +00002276 Py_INCREF(x);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002277 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002278 u = TOP();
Christian Heimes217cfd12007-12-02 14:31:20 +00002279 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
Thomas Woutersf7f438b2006-02-28 16:09:29 +00002280 w = PyTuple_Pack(5,
2281 w,
2282 f->f_globals,
2283 f->f_locals == NULL ?
2284 Py_None : f->f_locals,
2285 v,
2286 u);
2287 else
2288 w = PyTuple_Pack(4,
2289 w,
2290 f->f_globals,
2291 f->f_locals == NULL ?
2292 Py_None : f->f_locals,
2293 v);
2294 Py_DECREF(v);
Guido van Rossumb209a111997-04-29 18:18:01 +00002295 Py_DECREF(u);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002296 if (w == NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002297 u = POP();
Christian Heimesbbffeb62008-01-24 09:42:52 +00002298 Py_DECREF(x);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00002299 x = NULL;
2300 break;
2301 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002302 READ_TIMESTAMP(intr0);
Christian Heimesbbffeb62008-01-24 09:42:52 +00002303 v = x;
2304 x = PyEval_CallObject(v, w);
2305 Py_DECREF(v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002306 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002307 Py_DECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002308 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002309 if (x != NULL) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002310 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Antoine Pitroub52ec782009-01-25 16:34:23 +00002312 TARGET(IMPORT_STAR)
Thomas Wouters52152252000-08-17 22:55:00 +00002313 v = POP();
Guido van Rossumb209a111997-04-29 18:18:01 +00002314 PyFrame_FastToLocals(f);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002315 if ((x = f->f_locals) == NULL) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002316 PyErr_SetString(PyExc_SystemError,
Jeremy Hyltonc862cf42001-01-19 03:25:05 +00002317 "no locals found during 'import *'");
Guido van Rossum681d79a1995-07-18 14:51:37 +00002318 break;
2319 }
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002320 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002321 err = import_all_from(x, v);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002322 READ_TIMESTAMP(intr1);
Guido van Rossumb209a111997-04-29 18:18:01 +00002323 PyFrame_LocalsToFast(f, 0);
Thomas Wouters52152252000-08-17 22:55:00 +00002324 Py_DECREF(v);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002325 if (err == 0) DISPATCH();
Guido van Rossum374a9221991-04-04 10:40:29 +00002326 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002327
Antoine Pitroub52ec782009-01-25 16:34:23 +00002328 TARGET(IMPORT_FROM)
Skip Montanaro496e6582002-08-06 17:47:40 +00002329 w = GETITEM(names, oparg);
Thomas Wouters52152252000-08-17 22:55:00 +00002330 v = TOP();
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002331 READ_TIMESTAMP(intr0);
Thomas Wouters52152252000-08-17 22:55:00 +00002332 x = import_from(v, w);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002333 READ_TIMESTAMP(intr1);
Thomas Wouters52152252000-08-17 22:55:00 +00002334 PUSH(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002335 if (x != NULL) DISPATCH();
Thomas Wouters52152252000-08-17 22:55:00 +00002336 break;
2337
Antoine Pitroub52ec782009-01-25 16:34:23 +00002338 TARGET(JUMP_FORWARD)
Guido van Rossum374a9221991-04-04 10:40:29 +00002339 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002340 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002341
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002342 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2343 TARGET(POP_JUMP_IF_FALSE)
2344 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002345 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002346 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002347 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002348 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002349 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002350 Py_DECREF(w);
2351 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002352 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002353 }
2354 err = PyObject_IsTrue(w);
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002355 Py_DECREF(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002356 if (err > 0)
2357 err = 0;
2358 else if (err == 0)
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002359 JUMPTO(oparg);
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002360 else
2361 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002362 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002363
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002364 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2365 TARGET(POP_JUMP_IF_TRUE)
2366 w = POP();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002367 if (w == Py_False) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002368 Py_DECREF(w);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002369 FAST_DISPATCH();
Raymond Hettingerf606f872003-03-16 03:11:04 +00002370 }
Raymond Hettinger21012b82003-02-26 18:11:50 +00002371 if (w == Py_True) {
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002372 Py_DECREF(w);
2373 JUMPTO(oparg);
2374 FAST_DISPATCH();
2375 }
2376 err = PyObject_IsTrue(w);
2377 Py_DECREF(w);
2378 if (err > 0) {
2379 err = 0;
2380 JUMPTO(oparg);
2381 }
2382 else if (err == 0)
2383 ;
2384 else
2385 break;
2386 DISPATCH();
2387
2388 TARGET(JUMP_IF_FALSE_OR_POP)
2389 w = TOP();
2390 if (w == Py_True) {
2391 STACKADJ(-1);
2392 Py_DECREF(w);
2393 FAST_DISPATCH();
2394 }
2395 if (w == Py_False) {
2396 JUMPTO(oparg);
2397 FAST_DISPATCH();
2398 }
2399 err = PyObject_IsTrue(w);
2400 if (err > 0) {
2401 STACKADJ(-1);
2402 Py_DECREF(w);
2403 err = 0;
2404 }
2405 else if (err == 0)
2406 JUMPTO(oparg);
2407 else
2408 break;
2409 DISPATCH();
2410
2411 TARGET(JUMP_IF_TRUE_OR_POP)
2412 w = TOP();
2413 if (w == Py_False) {
2414 STACKADJ(-1);
2415 Py_DECREF(w);
2416 FAST_DISPATCH();
2417 }
2418 if (w == Py_True) {
2419 JUMPTO(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002420 FAST_DISPATCH();
Raymond Hettinger21012b82003-02-26 18:11:50 +00002421 }
2422 err = PyObject_IsTrue(w);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002423 if (err > 0) {
2424 err = 0;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002425 JUMPTO(oparg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00002426 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002427 else if (err == 0) {
2428 STACKADJ(-1);
2429 Py_DECREF(w);
2430 }
Guido van Rossum3dfd53b1997-01-18 02:46:13 +00002431 else
2432 break;
Antoine Pitroub52ec782009-01-25 16:34:23 +00002433 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002434
Raymond Hettingerfba1cfc2004-03-12 16:33:17 +00002435 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002436 TARGET(JUMP_ABSOLUTE)
Guido van Rossum374a9221991-04-04 10:40:29 +00002437 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002438#if FAST_LOOPS
2439 /* Enabling this path speeds-up all while and for-loops by bypassing
2440 the per-loop checks for signals. By default, this should be turned-off
2441 because it prevents detection of a control-break in tight loops like
2442 "while 1: pass". Compile with this option turned-on when you need
2443 the speed-up and do not need break checking inside tight loops (ones
Antoine Pitroub52ec782009-01-25 16:34:23 +00002444 that contain only instructions ending with FAST_DISPATCH).
Guido van Rossum58da9312007-11-10 23:39:45 +00002445 */
Antoine Pitroub52ec782009-01-25 16:34:23 +00002446 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002447#else
Antoine Pitroub52ec782009-01-25 16:34:23 +00002448 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002449#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002450
Antoine Pitroub52ec782009-01-25 16:34:23 +00002451 TARGET(GET_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002452 /* before: [obj]; after [getiter(obj)] */
Raymond Hettinger663004b2003-01-09 15:24:30 +00002453 v = TOP();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002454 x = PyObject_GetIter(v);
2455 Py_DECREF(v);
2456 if (x != NULL) {
Raymond Hettinger663004b2003-01-09 15:24:30 +00002457 SET_TOP(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002458 PREDICT(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002459 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002460 }
Raymond Hettinger8bb90a52003-01-14 12:43:10 +00002461 STACKADJ(-1);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002462 break;
2463
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002464 PREDICTED_WITH_ARG(FOR_ITER);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002465 TARGET(FOR_ITER)
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002466 /* before: [iter]; after: [iter, iter()] *or* [] */
2467 v = TOP();
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002468 x = (*v->ob_type->tp_iternext)(v);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002469 if (x != NULL) {
2470 PUSH(x);
Raymond Hettinger7dc52212003-03-16 20:14:44 +00002471 PREDICT(STORE_FAST);
2472 PREDICT(UNPACK_SEQUENCE);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002473 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002474 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002475 if (PyErr_Occurred()) {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002476 if (!PyErr_ExceptionMatches(
2477 PyExc_StopIteration))
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002478 break;
2479 PyErr_Clear();
Guido van Rossum213c7a62001-04-23 14:08:49 +00002480 }
Raymond Hettingerdb0de9e2004-03-12 08:41:36 +00002481 /* iterator ended normally */
2482 x = v = POP();
2483 Py_DECREF(v);
2484 JUMPBY(oparg);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002485 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002486
Antoine Pitroub52ec782009-01-25 16:34:23 +00002487 TARGET(BREAK_LOOP)
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002488 why = WHY_BREAK;
2489 goto fast_block_end;
2490
Antoine Pitroub52ec782009-01-25 16:34:23 +00002491 TARGET(CONTINUE_LOOP)
Christian Heimes217cfd12007-12-02 14:31:20 +00002492 retval = PyLong_FromLong(oparg);
Thomas Wouters477c8d52006-05-27 19:21:47 +00002493 if (!retval) {
2494 x = NULL;
2495 break;
2496 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002497 why = WHY_CONTINUE;
2498 goto fast_block_end;
2499
Antoine Pitroub52ec782009-01-25 16:34:23 +00002500 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2501 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2502 TARGET(SETUP_FINALLY)
2503 _setup_finally:
Thomas Wouters8ce81f72007-09-20 18:22:40 +00002504 /* NOTE: If you add any new block-setup opcodes that
2505 are not try/except/finally handlers, you may need
2506 to update the PyGen_NeedsFinalizing() function.
2507 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002508
Guido van Rossumb209a111997-04-29 18:18:01 +00002509 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002510 STACK_LEVEL());
Antoine Pitroub52ec782009-01-25 16:34:23 +00002511 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002512
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002513 TARGET(SETUP_WITH)
2514 {
2515 static PyObject *exit, *enter;
2516 w = TOP();
2517 x = special_lookup(w, "__exit__", &exit);
2518 if (!x)
2519 break;
2520 SET_TOP(x);
2521 u = special_lookup(w, "__enter__", &enter);
2522 Py_DECREF(w);
2523 if (!u) {
2524 x = NULL;
2525 break;
2526 }
2527 x = PyObject_CallFunctionObjArgs(u, NULL);
2528 Py_DECREF(u);
2529 if (!x)
2530 break;
2531 /* Setup the finally block before pushing the result
2532 of __enter__ on the stack. */
2533 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2534 STACK_LEVEL());
2535
2536 PUSH(x);
2537 DISPATCH();
2538 }
2539
Antoine Pitroub52ec782009-01-25 16:34:23 +00002540 TARGET(WITH_CLEANUP)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002541 {
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002542 /* At the top of the stack are 1-3 values indicating
2543 how/why we entered the finally clause:
2544 - TOP = None
2545 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2546 - TOP = WHY_*; no retval below it
2547 - (TOP, SECOND, THIRD) = exc_info()
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002548 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002549 Below them is EXIT, the context.__exit__ bound method.
Guido van Rossumc2e20742006-02-27 22:32:47 +00002550 In the last case, we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002551 EXIT(TOP, SECOND, THIRD)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002552 otherwise we must call
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002553 EXIT(None, None, None)
2554
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002555 In the first two cases, we remove EXIT from the
2556 stack, leaving the rest in the same order. In the
2557 third case, we shift the bottom 3 values of the
2558 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002559
2560 In addition, if the stack represents an exception,
Guido van Rossumf6694362006-03-10 02:28:35 +00002561 *and* the function call returns a 'true' value, we
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002562 push WHY_SILENCED onto the stack. END_FINALLY will
2563 then not re-raise the exception. (But non-local
2564 gotos should still be resumed.)
Guido van Rossumc2e20742006-02-27 22:32:47 +00002565 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002566
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002567 PyObject *exit_func;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002568 u = TOP();
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002569 if (u == Py_None) {
Ezio Melottib4d286d2009-10-03 16:14:07 +00002570 (void)POP();
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002571 exit_func = TOP();
2572 SET_TOP(u);
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002573 v = w = Py_None;
2574 }
2575 else if (PyLong_Check(u)) {
Ezio Melottib4d286d2009-10-03 16:14:07 +00002576 (void)POP();
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002577 switch(PyLong_AsLong(u)) {
2578 case WHY_RETURN:
2579 case WHY_CONTINUE:
2580 /* Retval in TOP. */
2581 exit_func = SECOND();
2582 SET_SECOND(TOP());
2583 SET_TOP(u);
2584 break;
2585 default:
2586 exit_func = TOP();
2587 SET_TOP(u);
2588 break;
2589 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002590 u = v = w = Py_None;
2591 }
2592 else {
Benjamin Peterson176101d2009-06-28 15:40:50 +00002593 PyObject *tp, *exc, *tb;
2594 PyTryBlock *block;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002595 v = SECOND();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002596 w = THIRD();
Benjamin Peterson176101d2009-06-28 15:40:50 +00002597 tp = FOURTH();
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002598 exc = PEEK(5);
2599 tb = PEEK(6);
2600 exit_func = PEEK(7);
2601 SET_VALUE(7, tb);
2602 SET_VALUE(6, exc);
2603 SET_VALUE(5, tp);
Benjamin Petersonf8b60b22010-02-11 02:31:04 +00002604 /* UNWIND_EXCEPT_HANDLER will pop this off. */
Benjamin Petersonb1715f12009-06-28 16:21:52 +00002605 SET_FOURTH(NULL);
Benjamin Petersonfec42da2009-06-28 15:55:46 +00002606 /* We just shifted the stack down, so we have
2607 to tell the except handler block that the
2608 values are lower than it expects. */
Benjamin Peterson176101d2009-06-28 15:40:50 +00002609 block = &f->f_blockstack[f->f_iblock - 1];
2610 assert(block->b_type == EXCEPT_HANDLER);
2611 block->b_level--;
Guido van Rossumc2e20742006-02-27 22:32:47 +00002612 }
Guido van Rossumf6694362006-03-10 02:28:35 +00002613 /* XXX Not the fastest way to call it... */
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002614 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2615 NULL);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002616 Py_DECREF(exit_func);
2617 if (x == NULL)
Guido van Rossumf6694362006-03-10 02:28:35 +00002618 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002619
2620 if (u != Py_None)
2621 err = PyObject_IsTrue(x);
2622 else
2623 err = 0;
2624 Py_DECREF(x);
2625
2626 if (err < 0)
2627 break; /* Go to error exit */
2628 else if (err > 0) {
2629 err = 0;
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002630 /* There was an exception and a True return */
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002631 PUSH(PyLong_FromLong((long) WHY_SILENCED));
Guido van Rossumf6694362006-03-10 02:28:35 +00002632 }
Christian Heimes180510d2008-03-03 19:15:45 +00002633 PREDICT(END_FINALLY);
Guido van Rossumc2e20742006-02-27 22:32:47 +00002634 break;
2635 }
2636
Antoine Pitroub52ec782009-01-25 16:34:23 +00002637 TARGET(CALL_FUNCTION)
Armin Rigo8817fcd2004-06-17 10:22:40 +00002638 {
2639 PyObject **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002640 PCALL(PCALL_ALL);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002641 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002642#ifdef WITH_TSC
Armin Rigo8817fcd2004-06-17 10:22:40 +00002643 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002644#else
Armin Rigo8817fcd2004-06-17 10:22:40 +00002645 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002646#endif
Armin Rigo8817fcd2004-06-17 10:22:40 +00002647 stack_pointer = sp;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002648 PUSH(x);
2649 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002650 DISPATCH();
Jeremy Hyltone8c04322002-08-16 17:47:26 +00002651 break;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002652 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002653
Antoine Pitroub52ec782009-01-25 16:34:23 +00002654 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2655 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2656 TARGET(CALL_FUNCTION_VAR_KW)
2657 _call_function_var_kw:
Guido van Rossumf10570b1995-07-07 22:53:21 +00002658 {
Jeremy Hylton76901512000-03-28 23:49:17 +00002659 int na = oparg & 0xff;
2660 int nk = (oparg>>8) & 0xff;
2661 int flags = (opcode - CALL_FUNCTION) & 3;
Jeremy Hylton52820442001-01-03 23:52:36 +00002662 int n = na + 2 * nk;
Armin Rigo8817fcd2004-06-17 10:22:40 +00002663 PyObject **pfunc, *func, **sp;
Jeremy Hylton985eba52003-02-05 23:13:00 +00002664 PCALL(PCALL_ALL);
Jeremy Hylton52820442001-01-03 23:52:36 +00002665 if (flags & CALL_FLAG_VAR)
2666 n++;
2667 if (flags & CALL_FLAG_KW)
2668 n++;
2669 pfunc = stack_pointer - n - 1;
2670 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002671
Guido van Rossumac7be682001-01-17 15:42:30 +00002672 if (PyMethod_Check(func)
Jeremy Hylton52820442001-01-03 23:52:36 +00002673 && PyMethod_GET_SELF(func) != NULL) {
2674 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002675 Py_INCREF(self);
Jeremy Hylton52820442001-01-03 23:52:36 +00002676 func = PyMethod_GET_FUNCTION(func);
2677 Py_INCREF(func);
Jeremy Hylton76901512000-03-28 23:49:17 +00002678 Py_DECREF(*pfunc);
2679 *pfunc = self;
2680 na++;
2681 n++;
Guido van Rossumac7be682001-01-17 15:42:30 +00002682 } else
Jeremy Hylton52820442001-01-03 23:52:36 +00002683 Py_INCREF(func);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002684 sp = stack_pointer;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002685 READ_TIMESTAMP(intr0);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002686 x = ext_do_call(func, &sp, flags, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002687 READ_TIMESTAMP(intr1);
Armin Rigo8817fcd2004-06-17 10:22:40 +00002688 stack_pointer = sp;
Jeremy Hylton76901512000-03-28 23:49:17 +00002689 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002690
Jeremy Hylton76901512000-03-28 23:49:17 +00002691 while (stack_pointer > pfunc) {
Jeremy Hylton52820442001-01-03 23:52:36 +00002692 w = POP();
2693 Py_DECREF(w);
Jeremy Hylton76901512000-03-28 23:49:17 +00002694 }
2695 PUSH(x);
Guido van Rossumac7be682001-01-17 15:42:30 +00002696 if (x != NULL)
Antoine Pitroub52ec782009-01-25 16:34:23 +00002697 DISPATCH();
Jeremy Hylton76901512000-03-28 23:49:17 +00002698 break;
Guido van Rossumf10570b1995-07-07 22:53:21 +00002699 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002700
Antoine Pitroub52ec782009-01-25 16:34:23 +00002701 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2702 TARGET(MAKE_FUNCTION)
2703 _make_function:
Guido van Rossum4f72a782006-10-27 23:31:49 +00002704 {
2705 int posdefaults = oparg & 0xff;
2706 int kwdefaults = (oparg>>8) & 0xff;
Neal Norwitzc1505362006-12-28 06:47:50 +00002707 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002708
Guido van Rossum681d79a1995-07-18 14:51:37 +00002709 v = POP(); /* code object */
Guido van Rossumb209a111997-04-29 18:18:01 +00002710 x = PyFunction_New(v, f->f_globals);
2711 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002712
Guido van Rossum0240b922007-02-26 21:23:50 +00002713 if (x != NULL && opcode == MAKE_CLOSURE) {
2714 v = POP();
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002715 if (PyFunction_SetClosure(x, v) != 0) {
2716 /* Can't happen unless bytecode is corrupt. */
2717 why = WHY_EXCEPTION;
2718 }
Guido van Rossum0240b922007-02-26 21:23:50 +00002719 Py_DECREF(v);
2720 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002721
2722 if (x != NULL && num_annotations > 0) {
2723 Py_ssize_t name_ix;
2724 u = POP(); /* names of args with annotations */
2725 v = PyDict_New();
2726 if (v == NULL) {
2727 Py_DECREF(x);
2728 x = NULL;
2729 break;
2730 }
2731 name_ix = PyTuple_Size(u);
2732 assert(num_annotations == name_ix+1);
2733 while (name_ix > 0) {
2734 --name_ix;
2735 t = PyTuple_GET_ITEM(u, name_ix);
2736 w = POP();
2737 /* XXX(nnorwitz): check for errors */
2738 PyDict_SetItem(v, t, w);
2739 Py_DECREF(w);
2740 }
2741
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002742 if (PyFunction_SetAnnotations(x, v) != 0) {
2743 /* Can't happen unless
2744 PyFunction_SetAnnotations changes. */
2745 why = WHY_EXCEPTION;
2746 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002747 Py_DECREF(v);
2748 Py_DECREF(u);
2749 }
2750
Guido van Rossum681d79a1995-07-18 14:51:37 +00002751 /* XXX Maybe this should be a separate opcode? */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002752 if (x != NULL && posdefaults > 0) {
2753 v = PyTuple_New(posdefaults);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002754 if (v == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002755 Py_DECREF(x);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002756 x = NULL;
2757 break;
2758 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002759 while (--posdefaults >= 0) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002760 w = POP();
Guido van Rossum4f72a782006-10-27 23:31:49 +00002761 PyTuple_SET_ITEM(v, posdefaults, w);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002762 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002763 if (PyFunction_SetDefaults(x, v) != 0) {
2764 /* Can't happen unless
2765 PyFunction_SetDefaults changes. */
2766 why = WHY_EXCEPTION;
2767 }
Guido van Rossumb209a111997-04-29 18:18:01 +00002768 Py_DECREF(v);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002769 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002770 if (x != NULL && kwdefaults > 0) {
2771 v = PyDict_New();
2772 if (v == NULL) {
2773 Py_DECREF(x);
2774 x = NULL;
2775 break;
2776 }
2777 while (--kwdefaults >= 0) {
2778 w = POP(); /* default value */
2779 u = POP(); /* kw only arg name */
Neal Norwitzc1505362006-12-28 06:47:50 +00002780 /* XXX(nnorwitz): check for errors */
Guido van Rossum4f72a782006-10-27 23:31:49 +00002781 PyDict_SetItem(v, u, w);
Georg Brandl94ab0002007-02-26 13:58:18 +00002782 Py_DECREF(w);
2783 Py_DECREF(u);
Guido van Rossum4f72a782006-10-27 23:31:49 +00002784 }
Jeffrey Yasskinb9083f32008-12-11 06:18:33 +00002785 if (PyFunction_SetKwDefaults(x, v) != 0) {
2786 /* Can't happen unless
2787 PyFunction_SetKwDefaults changes. */
2788 why = WHY_EXCEPTION;
2789 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00002790 Py_DECREF(v);
2791 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002792 PUSH(x);
2793 break;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002794 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002795
Antoine Pitroub52ec782009-01-25 16:34:23 +00002796 TARGET(BUILD_SLICE)
Guido van Rossum8861b741996-07-30 16:49:37 +00002797 if (oparg == 3)
2798 w = POP();
2799 else
2800 w = NULL;
2801 v = POP();
Raymond Hettinger663004b2003-01-09 15:24:30 +00002802 u = TOP();
Guido van Rossum1aa14831997-01-21 05:34:20 +00002803 x = PySlice_New(u, v, w);
Guido van Rossumb209a111997-04-29 18:18:01 +00002804 Py_DECREF(u);
2805 Py_DECREF(v);
2806 Py_XDECREF(w);
Raymond Hettinger663004b2003-01-09 15:24:30 +00002807 SET_TOP(x);
Antoine Pitroub52ec782009-01-25 16:34:23 +00002808 if (x != NULL) DISPATCH();
Guido van Rossum8861b741996-07-30 16:49:37 +00002809 break;
2810
Antoine Pitroub52ec782009-01-25 16:34:23 +00002811 TARGET(EXTENDED_ARG)
Fred Drakeef8ace32000-08-24 00:32:09 +00002812 opcode = NEXTOP();
Raymond Hettinger5bed4562004-04-10 23:34:17 +00002813 oparg = oparg<<16 | NEXTARG();
Fred Drakeef8ace32000-08-24 00:32:09 +00002814 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002815
Antoine Pitroub52ec782009-01-25 16:34:23 +00002816#ifdef USE_COMPUTED_GOTOS
2817 _unknown_opcode:
2818#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00002819 default:
2820 fprintf(stderr,
2821 "XXX lineno: %d, opcode: %d\n",
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00002822 PyFrame_GetLineNumber(f),
Michael W. Hudsondd32a912002-08-15 14:59:02 +00002823 opcode);
Guido van Rossumb209a111997-04-29 18:18:01 +00002824 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Guido van Rossum374a9221991-04-04 10:40:29 +00002825 why = WHY_EXCEPTION;
2826 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002827
2828#ifdef CASE_TOO_BIG
2829 }
2830#endif
2831
Guido van Rossum374a9221991-04-04 10:40:29 +00002832 } /* switch */
2833
2834 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002835
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002836 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002837
Guido van Rossum374a9221991-04-04 10:40:29 +00002838 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Guido van Rossum374a9221991-04-04 10:40:29 +00002840 if (why == WHY_NOT) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002841 if (err == 0 && x != NULL) {
2842#ifdef CHECKEXC
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002843 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002844 if (PyErr_Occurred())
Guido van Rossum681d79a1995-07-18 14:51:37 +00002845 fprintf(stderr,
2846 "XXX undetected error\n");
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002847 else {
2848#endif
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002849 READ_TIMESTAMP(loop1);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002850 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002851#ifdef CHECKEXC
2852 }
2853#endif
Guido van Rossum681d79a1995-07-18 14:51:37 +00002854 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002855 why = WHY_EXCEPTION;
Guido van Rossumb209a111997-04-29 18:18:01 +00002856 x = Py_None;
Guido van Rossum374a9221991-04-04 10:40:29 +00002857 err = 0;
2858 }
2859
Guido van Rossum374a9221991-04-04 10:40:29 +00002860 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002861
Raymond Hettingerc8aa08b2004-04-11 14:59:33 +00002862 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002863 if (!PyErr_Occurred()) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00002864 PyErr_SetString(PyExc_SystemError,
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002865 "error return without exception set");
Guido van Rossum374a9221991-04-04 10:40:29 +00002866 why = WHY_EXCEPTION;
2867 }
2868 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002869#ifdef CHECKEXC
Guido van Rossum374a9221991-04-04 10:40:29 +00002870 else {
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002871 /* This check is expensive! */
Guido van Rossumb209a111997-04-29 18:18:01 +00002872 if (PyErr_Occurred()) {
Christian Heimes412dc9c2008-01-27 18:55:54 +00002873 char buf[128];
Jeremy Hylton904ed862003-11-05 17:29:35 +00002874 sprintf(buf, "Stack unwind with exception "
2875 "set and why=%d", why);
2876 Py_FatalError(buf);
Guido van Rossum681d79a1995-07-18 14:51:37 +00002877 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002878 }
2879#endif
2880
2881 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002882
Guido van Rossum374a9221991-04-04 10:40:29 +00002883 if (why == WHY_EXCEPTION) {
Guido van Rossumb209a111997-04-29 18:18:01 +00002884 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002885
Fred Drake8f51f542001-10-04 14:48:42 +00002886 if (tstate->c_tracefunc != NULL)
2887 call_exc_trace(tstate->c_tracefunc,
2888 tstate->c_traceobj, f);
Guido van Rossum014518f1998-11-23 21:09:51 +00002889 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002890
Guido van Rossum374a9221991-04-04 10:40:29 +00002891 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Guido van Rossum374a9221991-04-04 10:40:29 +00002893 if (why == WHY_RERAISE)
2894 why = WHY_EXCEPTION;
2895
2896 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002897
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002898fast_block_end:
Tim Peters8a5c3c72004-04-05 19:36:21 +00002899 while (why != WHY_NOT && f->f_iblock > 0) {
Alexandre Vassalottibfc30992009-07-21 05:23:51 +00002900 /* Peek at the current block. */
2901 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002902
Tim Peters8a5c3c72004-04-05 19:36:21 +00002903 assert(why != WHY_YIELD);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002904 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002905 why = WHY_NOT;
Christian Heimes217cfd12007-12-02 14:31:20 +00002906 JUMPTO(PyLong_AS_LONG(retval));
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002907 Py_DECREF(retval);
2908 break;
2909 }
Alexandre Vassalottibfc30992009-07-21 05:23:51 +00002910 /* Now we have to pop the block. */
2911 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002912
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002913 if (b->b_type == EXCEPT_HANDLER) {
2914 UNWIND_EXCEPT_HANDLER(b);
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002915 continue;
Guido van Rossum374a9221991-04-04 10:40:29 +00002916 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002917 UNWIND_BLOCK(b);
Guido van Rossum374a9221991-04-04 10:40:29 +00002918 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2919 why = WHY_NOT;
2920 JUMPTO(b->b_handler);
2921 break;
2922 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002923 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2924 || b->b_type == SETUP_FINALLY)) {
2925 PyObject *exc, *val, *tb;
2926 int handler = b->b_handler;
2927 /* Beware, this invalidates all b->b_* fields */
2928 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2929 PUSH(tstate->exc_traceback);
2930 PUSH(tstate->exc_value);
2931 if (tstate->exc_type != NULL) {
2932 PUSH(tstate->exc_type);
Guido van Rossum374a9221991-04-04 10:40:29 +00002933 }
2934 else {
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002935 Py_INCREF(Py_None);
2936 PUSH(Py_None);
Guido van Rossum374a9221991-04-04 10:40:29 +00002937 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002938 PyErr_Fetch(&exc, &val, &tb);
2939 /* Make the raw exception data
2940 available to the handler,
2941 so a program can emulate the
2942 Python main loop. */
2943 PyErr_NormalizeException(
2944 &exc, &val, &tb);
2945 PyException_SetTraceback(val, tb);
2946 Py_INCREF(exc);
2947 tstate->exc_type = exc;
2948 Py_INCREF(val);
2949 tstate->exc_value = val;
2950 tstate->exc_traceback = tb;
2951 if (tb == NULL)
2952 tb = Py_None;
2953 Py_INCREF(tb);
2954 PUSH(tb);
2955 PUSH(val);
2956 PUSH(exc);
2957 why = WHY_NOT;
2958 JUMPTO(handler);
2959 break;
2960 }
2961 if (b->b_type == SETUP_FINALLY) {
2962 if (why & (WHY_RETURN | WHY_CONTINUE))
2963 PUSH(retval);
2964 PUSH(PyLong_FromLong((long)why));
Guido van Rossum374a9221991-04-04 10:40:29 +00002965 why = WHY_NOT;
2966 JUMPTO(b->b_handler);
2967 break;
2968 }
2969 } /* unwind stack */
2970
2971 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002972
Guido van Rossum374a9221991-04-04 10:40:29 +00002973 if (why != WHY_NOT)
2974 break;
Michael W. Hudson75eabd22005-01-18 15:56:11 +00002975 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002976
Guido van Rossum374a9221991-04-04 10:40:29 +00002977 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002978
Tim Peters8a5c3c72004-04-05 19:36:21 +00002979 assert(why != WHY_YIELD);
2980 /* Pop remaining stack entries. */
2981 while (!EMPTY()) {
2982 v = POP();
2983 Py_XDECREF(v);
Guido van Rossum35974fb2001-12-06 21:28:18 +00002984 }
2985
Tim Peters8a5c3c72004-04-05 19:36:21 +00002986 if (why != WHY_RETURN)
Guido van Rossum96a42c81992-01-12 02:29:51 +00002987 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002988
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002989fast_yield:
Fred Drake9e3ad782001-07-03 23:39:52 +00002990 if (tstate->use_tracing) {
Barry Warsawe2eca0b2005-08-15 18:14:19 +00002991 if (tstate->c_tracefunc) {
2992 if (why == WHY_RETURN || why == WHY_YIELD) {
2993 if (call_trace(tstate->c_tracefunc,
2994 tstate->c_traceobj, f,
2995 PyTrace_RETURN, retval)) {
2996 Py_XDECREF(retval);
2997 retval = NULL;
2998 why = WHY_EXCEPTION;
2999 }
3000 }
3001 else if (why == WHY_EXCEPTION) {
3002 call_trace_protected(tstate->c_tracefunc,
3003 tstate->c_traceobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003004 PyTrace_RETURN, NULL);
Guido van Rossum96a42c81992-01-12 02:29:51 +00003005 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00003006 }
Fred Drake8f51f542001-10-04 14:48:42 +00003007 if (tstate->c_profilefunc) {
Fred Drake4ec5d562001-10-04 19:26:43 +00003008 if (why == WHY_EXCEPTION)
3009 call_trace_protected(tstate->c_profilefunc,
3010 tstate->c_profileobj, f,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003011 PyTrace_RETURN, NULL);
Fred Drake4ec5d562001-10-04 19:26:43 +00003012 else if (call_trace(tstate->c_profilefunc,
3013 tstate->c_profileobj, f,
3014 PyTrace_RETURN, retval)) {
Fred Drake9e3ad782001-07-03 23:39:52 +00003015 Py_XDECREF(retval);
3016 retval = NULL;
3017 why = WHY_EXCEPTION;
3018 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003019 }
Guido van Rossum96a42c81992-01-12 02:29:51 +00003020 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003021
Tim Peters5ca576e2001-06-18 22:08:13 +00003022 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003023exit_eval_frame:
Armin Rigo2b3eb402003-10-28 12:05:48 +00003024 Py_LeaveRecursiveCall();
Guido van Rossuma027efa1997-05-05 20:56:21 +00003025 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003026
Guido van Rossum96a42c81992-01-12 02:29:51 +00003027 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003028}
3029
Guido van Rossumc2e20742006-02-27 22:32:47 +00003030/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003031 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003032 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003033
Tim Peters6d6c1a32001-08-02 04:15:00 +00003034PyObject *
3035PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Tim Peters5ca576e2001-06-18 22:08:13 +00003036 PyObject **args, int argcount, PyObject **kws, int kwcount,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003037 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003038{
3039 register PyFrameObject *f;
3040 register PyObject *retval = NULL;
3041 register PyObject **fastlocals, **freevars;
3042 PyThreadState *tstate = PyThreadState_GET();
3043 PyObject *x, *u;
3044
3045 if (globals == NULL) {
Tim Peters8a5c3c72004-04-05 19:36:21 +00003046 PyErr_SetString(PyExc_SystemError,
Jeremy Hylton910d7d42001-08-12 21:52:24 +00003047 "PyEval_EvalCodeEx: NULL globals");
Tim Peters5ca576e2001-06-18 22:08:13 +00003048 return NULL;
3049 }
3050
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003051 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003052 assert(globals != NULL);
3053 f = PyFrame_New(tstate, co, globals, locals);
Tim Peters5ca576e2001-06-18 22:08:13 +00003054 if (f == NULL)
3055 return NULL;
3056
3057 fastlocals = f->f_localsplus;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003058 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003059
3060 if (co->co_argcount > 0 ||
Guido van Rossum4f72a782006-10-27 23:31:49 +00003061 co->co_kwonlyargcount > 0 ||
Tim Peters5ca576e2001-06-18 22:08:13 +00003062 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3063 int i;
3064 int n = argcount;
3065 PyObject *kwdict = NULL;
3066 if (co->co_flags & CO_VARKEYWORDS) {
3067 kwdict = PyDict_New();
3068 if (kwdict == NULL)
3069 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003070 i = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003071 if (co->co_flags & CO_VARARGS)
3072 i++;
3073 SETLOCAL(i, kwdict);
3074 }
3075 if (argcount > co->co_argcount) {
3076 if (!(co->co_flags & CO_VARARGS)) {
3077 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003078 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003079 "%spositional argument%s (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003080 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003081 defcount ? "at most" : "exactly",
3082 co->co_argcount,
3083 kwcount ? "non-keyword " : "",
3084 co->co_argcount == 1 ? "" : "s",
3085 argcount);
3086 goto fail;
3087 }
3088 n = co->co_argcount;
3089 }
3090 for (i = 0; i < n; i++) {
3091 x = args[i];
3092 Py_INCREF(x);
3093 SETLOCAL(i, x);
3094 }
3095 if (co->co_flags & CO_VARARGS) {
3096 u = PyTuple_New(argcount - n);
3097 if (u == NULL)
3098 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003099 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
Tim Peters5ca576e2001-06-18 22:08:13 +00003100 for (i = n; i < argcount; i++) {
3101 x = args[i];
3102 Py_INCREF(x);
3103 PyTuple_SET_ITEM(u, i-n, x);
3104 }
3105 }
3106 for (i = 0; i < kwcount; i++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003107 PyObject **co_varnames;
Tim Peters5ca576e2001-06-18 22:08:13 +00003108 PyObject *keyword = kws[2*i];
3109 PyObject *value = kws[2*i + 1];
3110 int j;
Neal Norwitzda059e32007-08-26 05:33:45 +00003111 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003112 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003113 "%U() keywords must be strings",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003114 co->co_name);
Tim Peters5ca576e2001-06-18 22:08:13 +00003115 goto fail;
3116 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003117 /* Speed hack: do raw pointer compares. As names are
3118 normally interned this should almost always hit. */
3119 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003120 for (j = 0;
3121 j < co->co_argcount + co->co_kwonlyargcount;
3122 j++) {
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003123 PyObject *nm = co_varnames[j];
3124 if (nm == keyword)
3125 goto kw_found;
3126 }
3127 /* Slow fallback, just in case */
3128 for (j = 0;
3129 j < co->co_argcount + co->co_kwonlyargcount;
3130 j++) {
3131 PyObject *nm = co_varnames[j];
Tim Peters5ca576e2001-06-18 22:08:13 +00003132 int cmp = PyObject_RichCompareBool(
3133 keyword, nm, Py_EQ);
3134 if (cmp > 0)
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003135 goto kw_found;
Tim Peters5ca576e2001-06-18 22:08:13 +00003136 else if (cmp < 0)
3137 goto fail;
3138 }
3139 /* Check errors from Compare */
3140 if (PyErr_Occurred())
3141 goto fail;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003142 if (j >= co->co_argcount + co->co_kwonlyargcount) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003143 if (kwdict == NULL) {
3144 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003145 "%U() got an unexpected "
Walter Dörwald573c08c2007-05-25 15:46:59 +00003146 "keyword argument '%S'",
3147 co->co_name,
3148 keyword);
Tim Peters5ca576e2001-06-18 22:08:13 +00003149 goto fail;
3150 }
3151 PyDict_SetItem(kwdict, keyword, value);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003152 continue;
Tim Peters5ca576e2001-06-18 22:08:13 +00003153 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003154kw_found:
3155 if (GETLOCAL(j) != NULL) {
3156 PyErr_Format(PyExc_TypeError,
3157 "%U() got multiple "
3158 "values for keyword "
3159 "argument '%S'",
3160 co->co_name,
3161 keyword);
3162 goto fail;
Tim Peters5ca576e2001-06-18 22:08:13 +00003163 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003164 Py_INCREF(value);
3165 SETLOCAL(j, value);
Tim Peters5ca576e2001-06-18 22:08:13 +00003166 }
Guido van Rossum4f72a782006-10-27 23:31:49 +00003167 if (co->co_kwonlyargcount > 0) {
3168 for (i = co->co_argcount;
3169 i < co->co_argcount + co->co_kwonlyargcount;
3170 i++) {
Guido van Rossum29602e42006-11-22 04:45:33 +00003171 PyObject *name, *def;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003172 if (GETLOCAL(i) != NULL)
3173 continue;
Guido van Rossum29602e42006-11-22 04:45:33 +00003174 name = PyTuple_GET_ITEM(co->co_varnames, i);
3175 def = NULL;
Guido van Rossum4f72a782006-10-27 23:31:49 +00003176 if (kwdefs != NULL)
3177 def = PyDict_GetItem(kwdefs, name);
3178 if (def != NULL) {
3179 Py_INCREF(def);
3180 SETLOCAL(i, def);
3181 continue;
3182 }
3183 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003184 "%U() needs keyword-only argument %S",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003185 co->co_name, name);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003186 goto fail;
3187 }
3188 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003189 if (argcount < co->co_argcount) {
3190 int m = co->co_argcount - defcount;
3191 for (i = argcount; i < m; i++) {
3192 if (GETLOCAL(i) == NULL) {
3193 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003194 "%U() takes %s %d "
Guido van Rossum4f72a782006-10-27 23:31:49 +00003195 "%spositional argument%s "
3196 "(%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003197 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003198 ((co->co_flags & CO_VARARGS) ||
3199 defcount) ? "at least"
3200 : "exactly",
3201 m, kwcount ? "non-keyword " : "",
3202 m == 1 ? "" : "s", i);
3203 goto fail;
3204 }
3205 }
3206 if (n > m)
3207 i = n - m;
3208 else
3209 i = 0;
3210 for (; i < defcount; i++) {
3211 if (GETLOCAL(m+i) == NULL) {
3212 PyObject *def = defs[i];
3213 Py_INCREF(def);
3214 SETLOCAL(m+i, def);
3215 }
3216 }
3217 }
3218 }
3219 else {
3220 if (argcount > 0 || kwcount > 0) {
3221 PyErr_Format(PyExc_TypeError,
Guido van Rossum00bc0e02007-10-15 02:52:41 +00003222 "%U() takes no arguments (%d given)",
Walter Dörwald573c08c2007-05-25 15:46:59 +00003223 co->co_name,
Tim Peters5ca576e2001-06-18 22:08:13 +00003224 argcount + kwcount);
3225 goto fail;
3226 }
3227 }
3228 /* Allocate and initialize storage for cell vars, and copy free
3229 vars into frame. This isn't too efficient right now. */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003230 if (PyTuple_GET_SIZE(co->co_cellvars)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00003231 int i, j, nargs, found;
Martin v. Löwis5b222132007-06-10 09:51:05 +00003232 Py_UNICODE *cellname, *argname;
Tim Peters5ca576e2001-06-18 22:08:13 +00003233 PyObject *c;
3234
Amaury Forgeot d'Arce670bd42007-11-24 00:29:24 +00003235 nargs = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00003236 if (co->co_flags & CO_VARARGS)
3237 nargs++;
3238 if (co->co_flags & CO_VARKEYWORDS)
3239 nargs++;
3240
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003241 /* Initialize each cell var, taking into account
3242 cell vars that are initialized from arguments.
3243
3244 Should arrange for the compiler to put cellvars
3245 that are arguments at the beginning of the cellvars
3246 list so that we can march over it more efficiently?
3247 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003248 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003249 cellname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003250 PyTuple_GET_ITEM(co->co_cellvars, i));
3251 found = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003252 for (j = 0; j < nargs; j++) {
Martin v. Löwis5b222132007-06-10 09:51:05 +00003253 argname = PyUnicode_AS_UNICODE(
Tim Peters5ca576e2001-06-18 22:08:13 +00003254 PyTuple_GET_ITEM(co->co_varnames, j));
Martin v. Löwis5b222132007-06-10 09:51:05 +00003255 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003256 c = PyCell_New(GETLOCAL(j));
3257 if (c == NULL)
3258 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003259 GETLOCAL(co->co_nlocals + i) = c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003260 found = 1;
3261 break;
3262 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003263 }
3264 if (found == 0) {
3265 c = PyCell_New(NULL);
3266 if (c == NULL)
3267 goto fail;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003268 SETLOCAL(co->co_nlocals + i, c);
Tim Peters5ca576e2001-06-18 22:08:13 +00003269 }
3270 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003271 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00003272 if (PyTuple_GET_SIZE(co->co_freevars)) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003273 int i;
Thomas Wouters477c8d52006-05-27 19:21:47 +00003274 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Tim Peters5ca576e2001-06-18 22:08:13 +00003275 PyObject *o = PyTuple_GET_ITEM(closure, i);
3276 Py_INCREF(o);
Thomas Wouters477c8d52006-05-27 19:21:47 +00003277 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Tim Peters5ca576e2001-06-18 22:08:13 +00003278 }
3279 }
3280
Tim Peters5ca576e2001-06-18 22:08:13 +00003281 if (co->co_flags & CO_GENERATOR) {
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003282 /* Don't need to keep the reference to f_back, it will be set
3283 * when the generator is resumed. */
Tim Peters5ba58662001-07-16 02:29:45 +00003284 Py_XDECREF(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003285 f->f_back = NULL;
3286
Jeremy Hylton985eba52003-02-05 23:13:00 +00003287 PCALL(PCALL_GENERATOR);
3288
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003289 /* Create a new generator that owns the ready to run frame
3290 * and return that as the value. */
Martin v. Löwise440e472004-06-01 15:22:42 +00003291 return PyGen_New(f);
Tim Peters5ca576e2001-06-18 22:08:13 +00003292 }
3293
Thomas Woutersce272b62007-09-19 21:19:28 +00003294 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003295
Thomas Woutersce272b62007-09-19 21:19:28 +00003296fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003297
Tim Petersb13680b2001-11-27 23:29:29 +00003298 /* decref'ing the frame can cause __del__ methods to get invoked,
3299 which can call back into Python. While we're done with the
3300 current Python frame (f), the associated C stack is still in use,
3301 so recursion_depth must be boosted for the duration.
3302 */
3303 assert(tstate != NULL);
3304 ++tstate->recursion_depth;
Thomas Woutersce272b62007-09-19 21:19:28 +00003305 Py_DECREF(f);
Tim Petersb13680b2001-11-27 23:29:29 +00003306 --tstate->recursion_depth;
Tim Peters5ca576e2001-06-18 22:08:13 +00003307 return retval;
3308}
3309
3310
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003311static PyObject *
3312special_lookup(PyObject *o, char *meth, PyObject **cache)
3313{
3314 PyObject *res;
3315 res = _PyObject_LookupSpecial(o, meth, cache);
3316 if (res == NULL && !PyErr_Occurred()) {
3317 PyErr_SetObject(PyExc_AttributeError, *cache);
3318 return NULL;
3319 }
3320 return res;
3321}
3322
3323
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003324/* Logic for the raise statement (too complicated for inlining).
3325 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003326static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003327do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003328{
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003329 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003330
3331 if (exc == NULL) {
Guido van Rossumd295f121998-04-09 21:39:57 +00003332 /* Reraise */
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003333 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003334 PyObject *tb;
Collin Winter828f04a2007-08-31 00:04:24 +00003335 type = tstate->exc_type;
Guido van Rossumd295f121998-04-09 21:39:57 +00003336 value = tstate->exc_value;
3337 tb = tstate->exc_traceback;
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003338 if (type == Py_None) {
3339 PyErr_SetString(PyExc_RuntimeError,
3340 "No active exception to reraise");
3341 return WHY_EXCEPTION;
3342 }
3343 Py_XINCREF(type);
Guido van Rossumd295f121998-04-09 21:39:57 +00003344 Py_XINCREF(value);
3345 Py_XINCREF(tb);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003346 PyErr_Restore(type, value, tb);
3347 return WHY_RERAISE;
Guido van Rossumd295f121998-04-09 21:39:57 +00003348 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003349
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003350 /* We support the following forms of raise:
Collin Winter828f04a2007-08-31 00:04:24 +00003351 raise
3352 raise <instance>
3353 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003354
Collin Winter828f04a2007-08-31 00:04:24 +00003355 if (PyExceptionClass_Check(exc)) {
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003356 type = exc;
3357 value = PyObject_CallObject(exc, NULL);
Collin Winter828f04a2007-08-31 00:04:24 +00003358 if (value == NULL)
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003359 goto raise_error;
3360 }
Collin Winter828f04a2007-08-31 00:04:24 +00003361 else if (PyExceptionInstance_Check(exc)) {
3362 value = exc;
3363 type = PyExceptionInstance_Class(exc);
Guido van Rossumb209a111997-04-29 18:18:01 +00003364 Py_INCREF(type);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003365 }
3366 else {
3367 /* Not something you can raise. You get an exception
3368 anyway, just not what you specified :-) */
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003369 Py_DECREF(exc);
Guido van Rossum45aecf42006-03-15 04:58:47 +00003370 PyErr_SetString(PyExc_TypeError,
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003371 "exceptions must derive from BaseException");
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003372 goto raise_error;
3373 }
Collin Winter828f04a2007-08-31 00:04:24 +00003374
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003375 if (cause) {
3376 PyObject *fixed_cause;
3377 if (PyExceptionClass_Check(cause)) {
3378 fixed_cause = PyObject_CallObject(cause, NULL);
3379 if (fixed_cause == NULL)
3380 goto raise_error;
3381 Py_DECREF(cause);
3382 }
3383 else if (PyExceptionInstance_Check(cause)) {
3384 fixed_cause = cause;
3385 }
3386 else {
3387 PyErr_SetString(PyExc_TypeError,
3388 "exception causes must derive from "
3389 "BaseException");
3390 goto raise_error;
3391 }
3392 PyException_SetCause(value, fixed_cause);
3393 }
Collin Winter828f04a2007-08-31 00:04:24 +00003394
Guido van Rossumb4fb6e42008-06-14 20:20:24 +00003395 PyErr_SetObject(type, value);
3396 /* PyErr_SetObject incref's its arguments */
3397 Py_XDECREF(value);
3398 Py_XDECREF(type);
Collin Winter828f04a2007-08-31 00:04:24 +00003399 return WHY_EXCEPTION;
3400
3401raise_error:
Guido van Rossumb209a111997-04-29 18:18:01 +00003402 Py_XDECREF(value);
3403 Py_XDECREF(type);
Collin Winter1966f1c2007-09-01 20:26:44 +00003404 Py_XDECREF(cause);
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003405 return WHY_EXCEPTION;
3406}
3407
Tim Petersd6d010b2001-06-21 02:49:55 +00003408/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003409 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003410
Guido van Rossum0368b722007-05-11 16:50:42 +00003411 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3412 with a variable target.
3413*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003414
Barry Warsawe42b18f1997-08-25 22:13:04 +00003415static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003416unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003417{
Guido van Rossum0368b722007-05-11 16:50:42 +00003418 int i = 0, j = 0;
3419 Py_ssize_t ll = 0;
Tim Petersd6d010b2001-06-21 02:49:55 +00003420 PyObject *it; /* iter(v) */
Barry Warsawe42b18f1997-08-25 22:13:04 +00003421 PyObject *w;
Guido van Rossum0368b722007-05-11 16:50:42 +00003422 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003423
Tim Petersd6d010b2001-06-21 02:49:55 +00003424 assert(v != NULL);
3425
3426 it = PyObject_GetIter(v);
3427 if (it == NULL)
3428 goto Error;
3429
3430 for (; i < argcnt; i++) {
3431 w = PyIter_Next(it);
3432 if (w == NULL) {
3433 /* Iterator done, via error or exhaustion. */
3434 if (!PyErr_Occurred()) {
3435 PyErr_Format(PyExc_ValueError,
3436 "need more than %d value%s to unpack",
3437 i, i == 1 ? "" : "s");
3438 }
3439 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003440 }
3441 *--sp = w;
3442 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003443
Guido van Rossum0368b722007-05-11 16:50:42 +00003444 if (argcntafter == -1) {
3445 /* We better have exhausted the iterator now. */
3446 w = PyIter_Next(it);
3447 if (w == NULL) {
3448 if (PyErr_Occurred())
3449 goto Error;
3450 Py_DECREF(it);
3451 return 1;
3452 }
3453 Py_DECREF(w);
3454 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3455 goto Error;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003456 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003457
3458 l = PySequence_List(it);
3459 if (l == NULL)
3460 goto Error;
3461 *--sp = l;
3462 i++;
3463
3464 ll = PyList_GET_SIZE(l);
3465 if (ll < argcntafter) {
3466 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3467 argcnt + ll);
3468 goto Error;
3469 }
3470
3471 /* Pop the "after-variable" args off the list. */
3472 for (j = argcntafter; j > 0; j--, i++) {
3473 *--sp = PyList_GET_ITEM(l, ll - j);
3474 }
3475 /* Resize the list. */
Christian Heimes90aa7642007-12-19 02:45:37 +00003476 Py_SIZE(l) = ll - argcntafter;
Guido van Rossum0368b722007-05-11 16:50:42 +00003477 Py_DECREF(it);
3478 return 1;
3479
Tim Petersd6d010b2001-06-21 02:49:55 +00003480Error:
Barry Warsaw91010551997-08-25 22:30:51 +00003481 for (; i > 0; i--, sp++)
3482 Py_DECREF(*sp);
Tim Petersd6d010b2001-06-21 02:49:55 +00003483 Py_XDECREF(it);
Barry Warsawe42b18f1997-08-25 22:13:04 +00003484 return 0;
3485}
3486
3487
Guido van Rossum96a42c81992-01-12 02:29:51 +00003488#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003489static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003490prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003491{
Guido van Rossum3f5da241990-12-20 15:06:42 +00003492 printf("%s ", str);
Guido van Rossumb209a111997-04-29 18:18:01 +00003493 if (PyObject_Print(v, stdout, 0) != 0)
3494 PyErr_Clear(); /* Don't know what else to do */
Guido van Rossum3f5da241990-12-20 15:06:42 +00003495 printf("\n");
Guido van Rossumcc229ea2000-05-04 00:55:17 +00003496 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003497}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003498#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003499
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003500static void
Fred Drake5755ce62001-06-27 19:19:46 +00003501call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003502{
Guido van Rossumb209a111997-04-29 18:18:01 +00003503 PyObject *type, *value, *traceback, *arg;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003504 int err;
Guido van Rossumb209a111997-04-29 18:18:01 +00003505 PyErr_Fetch(&type, &value, &traceback);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003506 if (value == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003507 value = Py_None;
3508 Py_INCREF(value);
Guido van Rossumbd9ccca1992-04-09 14:58:08 +00003509 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00003510 arg = PyTuple_Pack(3, type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003511 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003512 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003513 return;
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003514 }
Fred Drake5755ce62001-06-27 19:19:46 +00003515 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
Guido van Rossumb209a111997-04-29 18:18:01 +00003516 Py_DECREF(arg);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003517 if (err == 0)
Guido van Rossumb209a111997-04-29 18:18:01 +00003518 PyErr_Restore(type, value, traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003519 else {
Guido van Rossumb209a111997-04-29 18:18:01 +00003520 Py_XDECREF(type);
3521 Py_XDECREF(value);
3522 Py_XDECREF(traceback);
Guido van Rossum1ae940a1995-01-02 19:04:15 +00003523 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003524}
3525
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003526static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003527call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003528 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003529{
3530 PyObject *type, *value, *traceback;
3531 int err;
3532 PyErr_Fetch(&type, &value, &traceback);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003533 err = call_trace(func, obj, frame, what, arg);
Fred Drake4ec5d562001-10-04 19:26:43 +00003534 if (err == 0)
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003535 {
Fred Drake4ec5d562001-10-04 19:26:43 +00003536 PyErr_Restore(type, value, traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003537 return 0;
3538 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003539 else {
3540 Py_XDECREF(type);
3541 Py_XDECREF(value);
3542 Py_XDECREF(traceback);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003543 return -1;
Fred Drake4ec5d562001-10-04 19:26:43 +00003544 }
3545}
3546
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003547static int
Fred Drake5755ce62001-06-27 19:19:46 +00003548call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
3549 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003550{
Fred Drake5755ce62001-06-27 19:19:46 +00003551 register PyThreadState *tstate = frame->f_tstate;
3552 int result;
3553 if (tstate->tracing)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003554 return 0;
Guido van Rossuma027efa1997-05-05 20:56:21 +00003555 tstate->tracing++;
Fred Drake9e3ad782001-07-03 23:39:52 +00003556 tstate->use_tracing = 0;
Fred Drake5755ce62001-06-27 19:19:46 +00003557 result = func(obj, frame, what, arg);
Fred Drake9e3ad782001-07-03 23:39:52 +00003558 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3559 || (tstate->c_profilefunc != NULL));
Guido van Rossuma027efa1997-05-05 20:56:21 +00003560 tstate->tracing--;
Fred Drake5755ce62001-06-27 19:19:46 +00003561 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003562}
3563
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003564PyObject *
3565_PyEval_CallTracing(PyObject *func, PyObject *args)
3566{
3567 PyFrameObject *frame = PyEval_GetFrame();
3568 PyThreadState *tstate = frame->f_tstate;
3569 int save_tracing = tstate->tracing;
3570 int save_use_tracing = tstate->use_tracing;
3571 PyObject *result;
3572
3573 tstate->tracing = 0;
3574 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3575 || (tstate->c_profilefunc != NULL));
3576 result = PyObject_Call(func, args, NULL);
3577 tstate->tracing = save_tracing;
3578 tstate->use_tracing = save_use_tracing;
3579 return result;
3580}
3581
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003582/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00003583static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003584maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Armin Rigobf57a142004-03-22 19:24:58 +00003585 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3586 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003587{
Michael W. Hudson006c7522002-11-08 13:08:46 +00003588 int result = 0;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003589 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003590
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003591 /* If the last instruction executed isn't in the current
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003592 instruction window, reset the window.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003593 */
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003594 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003595 PyAddrPair bounds;
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003596 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3597 &bounds);
Thomas Woutersce272b62007-09-19 21:19:28 +00003598 *instr_lb = bounds.ap_lower;
3599 *instr_ub = bounds.ap_upper;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003600 }
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00003601 /* If the last instruction falls at the start of a line or if
3602 it represents a jump backwards, update the frame's line
3603 number and call the trace function. */
3604 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3605 frame->f_lineno = line;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003606 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
Armin Rigobf57a142004-03-22 19:24:58 +00003607 }
3608 *instr_prev = frame->f_lasti;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003609 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003610}
3611
Fred Drake5755ce62001-06-27 19:19:46 +00003612void
3613PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003614{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003615 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003616 PyObject *temp = tstate->c_profileobj;
3617 Py_XINCREF(arg);
3618 tstate->c_profilefunc = NULL;
3619 tstate->c_profileobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003620 /* Must make sure that tracing is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003621 tstate->use_tracing = tstate->c_tracefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003622 Py_XDECREF(temp);
3623 tstate->c_profilefunc = func;
3624 tstate->c_profileobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003625 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003626 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003627}
3628
3629void
3630PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3631{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003632 PyThreadState *tstate = PyThreadState_GET();
Fred Drake5755ce62001-06-27 19:19:46 +00003633 PyObject *temp = tstate->c_traceobj;
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +00003634 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003635 Py_XINCREF(arg);
3636 tstate->c_tracefunc = NULL;
3637 tstate->c_traceobj = NULL;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003638 /* Must make sure that profiling is not ignored if 'temp' is freed */
Fred Drake9e3ad782001-07-03 23:39:52 +00003639 tstate->use_tracing = tstate->c_profilefunc != NULL;
Fred Drake5755ce62001-06-27 19:19:46 +00003640 Py_XDECREF(temp);
3641 tstate->c_tracefunc = func;
3642 tstate->c_traceobj = arg;
Brett Cannon55fa66d2005-06-25 07:07:35 +00003643 /* Flag that tracing or profiling is turned on */
Fred Drake9e3ad782001-07-03 23:39:52 +00003644 tstate->use_tracing = ((func != NULL)
3645 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003646}
3647
Guido van Rossumb209a111997-04-29 18:18:01 +00003648PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003649PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003650{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003651 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum6135a871995-01-09 17:53:26 +00003652 if (current_frame == NULL)
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003653 return PyThreadState_GET()->interp->builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003654 else
3655 return current_frame->f_builtins;
3656}
3657
Guido van Rossumb209a111997-04-29 18:18:01 +00003658PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003659PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003660{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003661 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum5b722181993-03-30 17:46:03 +00003662 if (current_frame == NULL)
3663 return NULL;
Guido van Rossumb209a111997-04-29 18:18:01 +00003664 PyFrame_FastToLocals(current_frame);
Guido van Rossum5b722181993-03-30 17:46:03 +00003665 return current_frame->f_locals;
3666}
3667
Guido van Rossumb209a111997-04-29 18:18:01 +00003668PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003669PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003670{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003671 PyFrameObject *current_frame = PyEval_GetFrame();
Guido van Rossum3f5da241990-12-20 15:06:42 +00003672 if (current_frame == NULL)
3673 return NULL;
3674 else
3675 return current_frame->f_globals;
3676}
3677
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003678PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003679PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003680{
Nicholas Bastine5662ae2004-03-24 22:22:12 +00003681 PyThreadState *tstate = PyThreadState_GET();
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003682 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003683}
3684
Guido van Rossum6135a871995-01-09 17:53:26 +00003685int
Tim Peters5ba58662001-07-16 02:29:45 +00003686PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003687{
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003688 PyFrameObject *current_frame = PyEval_GetFrame();
Just van Rossum3aaf42c2003-02-10 08:21:10 +00003689 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003690
3691 if (current_frame != NULL) {
3692 const int codeflags = current_frame->f_code->co_flags;
Tim Peterse2c18e92001-08-17 20:47:47 +00003693 const int compilerflags = codeflags & PyCF_MASK;
3694 if (compilerflags) {
Tim Peters5ba58662001-07-16 02:29:45 +00003695 result = 1;
Tim Peterse2c18e92001-08-17 20:47:47 +00003696 cf->cf_flags |= compilerflags;
Tim Peters5ba58662001-07-16 02:29:45 +00003697 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003698#if 0 /* future keyword */
Martin v. Löwis7198a522002-01-01 19:59:11 +00003699 if (codeflags & CO_GENERATOR_ALLOWED) {
3700 result = 1;
3701 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3702 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003703#endif
Tim Peters5ba58662001-07-16 02:29:45 +00003704 }
3705 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003706}
3707
Guido van Rossum3f5da241990-12-20 15:06:42 +00003708
Guido van Rossum681d79a1995-07-18 14:51:37 +00003709/* External interface to call any callable object.
3710 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003711
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003712#undef PyEval_CallObject
3713/* for backward compatibility: export this interface */
3714
Guido van Rossumb209a111997-04-29 18:18:01 +00003715PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003716PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003717{
Guido van Rossumb209a111997-04-29 18:18:01 +00003718 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003719}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003720#define PyEval_CallObject(func,arg) \
3721 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003722
Guido van Rossumb209a111997-04-29 18:18:01 +00003723PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003724PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003725{
Jeremy Hylton52820442001-01-03 23:52:36 +00003726 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003727
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003728 if (arg == NULL) {
Guido van Rossumb209a111997-04-29 18:18:01 +00003729 arg = PyTuple_New(0);
Hye-Shik Chang4af5c8c2006-03-07 15:39:21 +00003730 if (arg == NULL)
3731 return NULL;
3732 }
Guido van Rossumb209a111997-04-29 18:18:01 +00003733 else if (!PyTuple_Check(arg)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003734 PyErr_SetString(PyExc_TypeError,
3735 "argument list must be a tuple");
Guido van Rossum681d79a1995-07-18 14:51:37 +00003736 return NULL;
3737 }
3738 else
Guido van Rossumb209a111997-04-29 18:18:01 +00003739 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003740
Guido van Rossumb209a111997-04-29 18:18:01 +00003741 if (kw != NULL && !PyDict_Check(kw)) {
Guido van Rossuma027efa1997-05-05 20:56:21 +00003742 PyErr_SetString(PyExc_TypeError,
3743 "keyword list must be a dictionary");
Guido van Rossum25826c92000-04-21 21:17:39 +00003744 Py_DECREF(arg);
Guido van Rossume3e61c11995-08-04 04:14:47 +00003745 return NULL;
3746 }
3747
Tim Peters6d6c1a32001-08-02 04:15:00 +00003748 result = PyObject_Call(func, arg, kw);
Guido van Rossumb209a111997-04-29 18:18:01 +00003749 Py_DECREF(arg);
Jeremy Hylton52820442001-01-03 23:52:36 +00003750 return result;
3751}
3752
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003753const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003754PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003755{
3756 if (PyMethod_Check(func))
Tim Peters6d6c1a32001-08-02 04:15:00 +00003757 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
Jeremy Hylton512a2372001-04-11 13:52:29 +00003758 else if (PyFunction_Check(func))
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003759 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00003760 else if (PyCFunction_Check(func))
3761 return ((PyCFunctionObject*)func)->m_ml->ml_name;
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003762 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003763 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003764}
3765
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003766const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003767PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003768{
3769 if (PyMethod_Check(func))
3770 return "()";
3771 else if (PyFunction_Check(func))
3772 return "()";
3773 else if (PyCFunction_Check(func))
3774 return "()";
Guido van Rossum50e9fb92006-08-17 05:42:55 +00003775 else
Jeremy Hylton512a2372001-04-11 13:52:29 +00003776 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003777}
3778
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003779static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003780err_args(PyObject *func, int flags, int nargs)
3781{
3782 if (flags & METH_NOARGS)
Tim Peters8a5c3c72004-04-05 19:36:21 +00003783 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003784 "%.200s() takes no arguments (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003785 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003786 nargs);
3787 else
Tim Peters8a5c3c72004-04-05 19:36:21 +00003788 PyErr_Format(PyExc_TypeError,
Guido van Rossum86c659a2002-08-23 14:11:35 +00003789 "%.200s() takes exactly one argument (%d given)",
Tim Peters8a5c3c72004-04-05 19:36:21 +00003790 ((PyCFunctionObject *)func)->m_ml->ml_name,
Jeremy Hylton192690e2002-08-16 18:36:11 +00003791 nargs);
3792}
3793
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003794#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003795if (tstate->use_tracing && tstate->c_profilefunc) { \
3796 if (call_trace(tstate->c_profilefunc, \
3797 tstate->c_profileobj, \
3798 tstate->frame, PyTrace_C_CALL, \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003799 func)) { \
3800 x = NULL; \
3801 } \
3802 else { \
3803 x = call; \
3804 if (tstate->c_profilefunc != NULL) { \
3805 if (x == NULL) { \
3806 call_trace_protected(tstate->c_profilefunc, \
3807 tstate->c_profileobj, \
3808 tstate->frame, PyTrace_C_EXCEPTION, \
3809 func); \
3810 /* XXX should pass (type, value, tb) */ \
3811 } else { \
3812 if (call_trace(tstate->c_profilefunc, \
3813 tstate->c_profileobj, \
3814 tstate->frame, PyTrace_C_RETURN, \
3815 func)) { \
3816 Py_DECREF(x); \
3817 x = NULL; \
3818 } \
3819 } \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003820 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003821 } \
3822} else { \
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003823 x = call; \
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003824 }
3825
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003826static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003827call_function(PyObject ***pp_stack, int oparg
3828#ifdef WITH_TSC
3829 , uint64* pintr0, uint64* pintr1
3830#endif
3831 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003832{
3833 int na = oparg & 0xff;
3834 int nk = (oparg>>8) & 0xff;
3835 int n = na + 2 * nk;
3836 PyObject **pfunc = (*pp_stack) - n - 1;
3837 PyObject *func = *pfunc;
3838 PyObject *x, *w;
3839
Jeremy Hylton985eba52003-02-05 23:13:00 +00003840 /* Always dispatch PyCFunction first, because these are
3841 presumed to be the most frequent callable object.
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003842 */
3843 if (PyCFunction_Check(func) && nk == 0) {
3844 int flags = PyCFunction_GET_FLAGS(func);
Nicholas Bastind858a772004-06-25 23:31:06 +00003845 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003846
3847 PCALL(PCALL_CFUNCTION);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003848 if (flags & (METH_NOARGS | METH_O)) {
3849 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3850 PyObject *self = PyCFunction_GET_SELF(func);
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003851 if (flags & METH_NOARGS && na == 0) {
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003852 C_TRACE(x, (*meth)(self,NULL));
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003853 }
Jeremy Hylton192690e2002-08-16 18:36:11 +00003854 else if (flags & METH_O && na == 1) {
3855 PyObject *arg = EXT_POP(*pp_stack);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003856 C_TRACE(x, (*meth)(self,arg));
Jeremy Hylton192690e2002-08-16 18:36:11 +00003857 Py_DECREF(arg);
3858 }
3859 else {
3860 err_args(func, flags, na);
3861 x = NULL;
3862 }
3863 }
3864 else {
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003865 PyObject *callargs;
3866 callargs = load_args(pp_stack, na);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003867 READ_TIMESTAMP(*pintr0);
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003868 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003869 READ_TIMESTAMP(*pintr1);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003870 Py_XDECREF(callargs);
3871 }
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003872 } else {
3873 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3874 /* optimize access to bound methods */
3875 PyObject *self = PyMethod_GET_SELF(func);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003876 PCALL(PCALL_METHOD);
3877 PCALL(PCALL_BOUND_METHOD);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003878 Py_INCREF(self);
3879 func = PyMethod_GET_FUNCTION(func);
3880 Py_INCREF(func);
3881 Py_DECREF(*pfunc);
3882 *pfunc = self;
3883 na++;
3884 n++;
3885 } else
3886 Py_INCREF(func);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003887 READ_TIMESTAMP(*pintr0);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003888 if (PyFunction_Check(func))
3889 x = fast_function(func, pp_stack, n, na, nk);
Tim Peters8a5c3c72004-04-05 19:36:21 +00003890 else
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003891 x = do_call(func, pp_stack, na, nk);
Michael W. Hudson75eabd22005-01-18 15:56:11 +00003892 READ_TIMESTAMP(*pintr1);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003893 Py_DECREF(func);
3894 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003895
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003896 /* Clear the stack of the function object. Also removes
3897 the arguments in case they weren't consumed already
3898 (fast_function() and err_args() leave them on the stack).
Thomas Wouters7f597322006-03-01 05:32:33 +00003899 */
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003900 while ((*pp_stack) > pfunc) {
3901 w = EXT_POP(*pp_stack);
3902 Py_DECREF(w);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003903 PCALL(PCALL_POP);
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003904 }
3905 return x;
3906}
3907
Jeremy Hylton192690e2002-08-16 18:36:11 +00003908/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003909 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003910 For the simplest case -- a function that takes only positional
3911 arguments and is called with only positional arguments -- it
3912 inlines the most primitive frame setup code from
3913 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3914 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003915*/
3916
3917static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003918fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003919{
Jeremy Hylton985eba52003-02-05 23:13:00 +00003920 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003921 PyObject *globals = PyFunction_GET_GLOBALS(func);
3922 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003923 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003924 PyObject **d = NULL;
3925 int nd = 0;
3926
Jeremy Hylton985eba52003-02-05 23:13:00 +00003927 PCALL(PCALL_FUNCTION);
3928 PCALL(PCALL_FAST_FUNCTION);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003929 if (argdefs == NULL && co->co_argcount == n &&
3930 co->co_kwonlyargcount == 0 && nk==0 &&
Jeremy Hylton985eba52003-02-05 23:13:00 +00003931 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3932 PyFrameObject *f;
3933 PyObject *retval = NULL;
3934 PyThreadState *tstate = PyThreadState_GET();
3935 PyObject **fastlocals, **stack;
3936 int i;
3937
3938 PCALL(PCALL_FASTER_FUNCTION);
3939 assert(globals != NULL);
3940 /* XXX Perhaps we should create a specialized
3941 PyFrame_New() that doesn't take locals, but does
3942 take builtins without sanity checking them.
3943 */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00003944 assert(tstate != NULL);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003945 f = PyFrame_New(tstate, co, globals, NULL);
3946 if (f == NULL)
3947 return NULL;
3948
3949 fastlocals = f->f_localsplus;
3950 stack = (*pp_stack) - n;
3951
3952 for (i = 0; i < n; i++) {
3953 Py_INCREF(*stack);
3954 fastlocals[i] = *stack++;
3955 }
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00003956 retval = PyEval_EvalFrameEx(f,0);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003957 ++tstate->recursion_depth;
3958 Py_DECREF(f);
3959 --tstate->recursion_depth;
3960 return retval;
3961 }
Jeremy Hylton52820442001-01-03 23:52:36 +00003962 if (argdefs != NULL) {
3963 d = &PyTuple_GET_ITEM(argdefs, 0);
Christian Heimes90aa7642007-12-19 02:45:37 +00003964 nd = Py_SIZE(argdefs);
Jeremy Hylton52820442001-01-03 23:52:36 +00003965 }
Jeremy Hylton985eba52003-02-05 23:13:00 +00003966 return PyEval_EvalCodeEx(co, globals,
3967 (PyObject *)NULL, (*pp_stack)-n, na,
Guido van Rossum4f72a782006-10-27 23:31:49 +00003968 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
Jeremy Hylton985eba52003-02-05 23:13:00 +00003969 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003970}
3971
3972static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003973update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3974 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003975{
3976 PyObject *kwdict = NULL;
3977 if (orig_kwdict == NULL)
3978 kwdict = PyDict_New();
3979 else {
3980 kwdict = PyDict_Copy(orig_kwdict);
3981 Py_DECREF(orig_kwdict);
3982 }
3983 if (kwdict == NULL)
3984 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00003985 while (--nk >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00003986 int err;
3987 PyObject *value = EXT_POP(*pp_stack);
3988 PyObject *key = EXT_POP(*pp_stack);
3989 if (PyDict_GetItem(kwdict, key) != NULL) {
Thomas Woutersce272b62007-09-19 21:19:28 +00003990 PyErr_Format(PyExc_TypeError,
3991 "%.200s%s got multiple values "
3992 "for keyword argument '%.200s'",
Tim Peters6d6c1a32001-08-02 04:15:00 +00003993 PyEval_GetFuncName(func),
3994 PyEval_GetFuncDesc(func),
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00003995 _PyUnicode_AsString(key));
Jeremy Hylton52820442001-01-03 23:52:36 +00003996 Py_DECREF(key);
3997 Py_DECREF(value);
3998 Py_DECREF(kwdict);
3999 return NULL;
4000 }
4001 err = PyDict_SetItem(kwdict, key, value);
4002 Py_DECREF(key);
4003 Py_DECREF(value);
4004 if (err) {
4005 Py_DECREF(kwdict);
4006 return NULL;
4007 }
4008 }
4009 return kwdict;
4010}
4011
4012static PyObject *
4013update_star_args(int nstack, int nstar, PyObject *stararg,
4014 PyObject ***pp_stack)
4015{
4016 PyObject *callargs, *w;
4017
4018 callargs = PyTuple_New(nstack + nstar);
4019 if (callargs == NULL) {
4020 return NULL;
4021 }
4022 if (nstar) {
4023 int i;
4024 for (i = 0; i < nstar; i++) {
4025 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4026 Py_INCREF(a);
4027 PyTuple_SET_ITEM(callargs, nstack + i, a);
4028 }
4029 }
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004030 while (--nstack >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004031 w = EXT_POP(*pp_stack);
4032 PyTuple_SET_ITEM(callargs, nstack, w);
4033 }
4034 return callargs;
4035}
4036
4037static PyObject *
4038load_args(PyObject ***pp_stack, int na)
4039{
4040 PyObject *args = PyTuple_New(na);
4041 PyObject *w;
4042
4043 if (args == NULL)
4044 return NULL;
Raymond Hettinger5bed4562004-04-10 23:34:17 +00004045 while (--na >= 0) {
Jeremy Hylton52820442001-01-03 23:52:36 +00004046 w = EXT_POP(*pp_stack);
4047 PyTuple_SET_ITEM(args, na, w);
4048 }
4049 return args;
4050}
4051
4052static PyObject *
4053do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4054{
4055 PyObject *callargs = NULL;
4056 PyObject *kwdict = NULL;
4057 PyObject *result = NULL;
4058
4059 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004060 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004061 if (kwdict == NULL)
4062 goto call_fail;
4063 }
4064 callargs = load_args(pp_stack, na);
4065 if (callargs == NULL)
4066 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004067#ifdef CALL_PROFILE
4068 /* At this point, we have to look at the type of func to
4069 update the call stats properly. Do it here so as to avoid
4070 exposing the call stats machinery outside ceval.c
4071 */
4072 if (PyFunction_Check(func))
4073 PCALL(PCALL_FUNCTION);
4074 else if (PyMethod_Check(func))
4075 PCALL(PCALL_METHOD);
4076 else if (PyType_Check(func))
4077 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004078 else if (PyCFunction_Check(func))
4079 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004080 else
4081 PCALL(PCALL_OTHER);
4082#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004083 if (PyCFunction_Check(func)) {
4084 PyThreadState *tstate = PyThreadState_GET();
4085 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4086 }
4087 else
4088 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00004089call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004090 Py_XDECREF(callargs);
4091 Py_XDECREF(kwdict);
4092 return result;
4093}
4094
4095static PyObject *
4096ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4097{
4098 int nstar = 0;
4099 PyObject *callargs = NULL;
4100 PyObject *stararg = NULL;
4101 PyObject *kwdict = NULL;
4102 PyObject *result = NULL;
4103
4104 if (flags & CALL_FLAG_KW) {
4105 kwdict = EXT_POP(*pp_stack);
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004106 if (!PyDict_Check(kwdict)) {
4107 PyObject *d;
4108 d = PyDict_New();
4109 if (d == NULL)
4110 goto ext_call_fail;
4111 if (PyDict_Update(d, kwdict) != 0) {
4112 Py_DECREF(d);
4113 /* PyDict_Update raises attribute
4114 * error (percolated from an attempt
4115 * to get 'keys' attribute) instead of
4116 * a type error if its second argument
4117 * is not a mapping.
4118 */
4119 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4120 PyErr_Format(PyExc_TypeError,
4121 "%.200s%.200s argument after ** "
4122 "must be a mapping, not %.200s",
4123 PyEval_GetFuncName(func),
4124 PyEval_GetFuncDesc(func),
4125 kwdict->ob_type->tp_name);
4126 }
4127 goto ext_call_fail;
4128 }
4129 Py_DECREF(kwdict);
4130 kwdict = d;
Jeremy Hylton52820442001-01-03 23:52:36 +00004131 }
4132 }
4133 if (flags & CALL_FLAG_VAR) {
4134 stararg = EXT_POP(*pp_stack);
4135 if (!PyTuple_Check(stararg)) {
4136 PyObject *t = NULL;
4137 t = PySequence_Tuple(stararg);
4138 if (t == NULL) {
Jeremy Hylton512a2372001-04-11 13:52:29 +00004139 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4140 PyErr_Format(PyExc_TypeError,
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004141 "%.200s%.200s argument after * "
4142 "must be a sequence, not %200s",
Tim Peters6d6c1a32001-08-02 04:15:00 +00004143 PyEval_GetFuncName(func),
Guido van Rossumd59da4b2007-05-22 18:11:13 +00004144 PyEval_GetFuncDesc(func),
4145 stararg->ob_type->tp_name);
Jeremy Hylton512a2372001-04-11 13:52:29 +00004146 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004147 goto ext_call_fail;
4148 }
4149 Py_DECREF(stararg);
4150 stararg = t;
4151 }
4152 nstar = PyTuple_GET_SIZE(stararg);
4153 }
4154 if (nk > 0) {
Ka-Ping Yee20579702001-01-15 22:14:16 +00004155 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
Jeremy Hylton52820442001-01-03 23:52:36 +00004156 if (kwdict == NULL)
4157 goto ext_call_fail;
4158 }
4159 callargs = update_star_args(na, nstar, stararg, pp_stack);
4160 if (callargs == NULL)
4161 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004162#ifdef CALL_PROFILE
4163 /* At this point, we have to look at the type of func to
4164 update the call stats properly. Do it here so as to avoid
4165 exposing the call stats machinery outside ceval.c
4166 */
4167 if (PyFunction_Check(func))
4168 PCALL(PCALL_FUNCTION);
4169 else if (PyMethod_Check(func))
4170 PCALL(PCALL_METHOD);
4171 else if (PyType_Check(func))
4172 PCALL(PCALL_TYPE);
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004173 else if (PyCFunction_Check(func))
4174 PCALL(PCALL_CFUNCTION);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004175 else
4176 PCALL(PCALL_OTHER);
4177#endif
Antoine Pitrou8e124f32009-05-30 21:41:10 +00004178 if (PyCFunction_Check(func)) {
4179 PyThreadState *tstate = PyThreadState_GET();
4180 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4181 }
4182 else
4183 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004184ext_call_fail:
Jeremy Hylton52820442001-01-03 23:52:36 +00004185 Py_XDECREF(callargs);
4186 Py_XDECREF(kwdict);
4187 Py_XDECREF(stararg);
4188 return result;
4189}
4190
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004191/* Extract a slice index from a PyInt or PyLong or an object with the
4192 nb_index slot defined, and store in *pi.
4193 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4194 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 +00004195 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004196*/
Tim Petersb5196382001-12-16 19:44:20 +00004197/* Note: If v is NULL, return success without storing into *pi. This
4198 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4199 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004200*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004201int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004202_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004203{
Tim Petersb5196382001-12-16 19:44:20 +00004204 if (v != NULL) {
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004205 Py_ssize_t x;
Martin v. Löwisd1a1d1e2007-12-04 22:10:37 +00004206 if (PyIndex_Check(v)) {
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00004207 x = PyNumber_AsSsize_t(v, NULL);
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004208 if (x == -1 && PyErr_Occurred())
4209 return 0;
4210 }
4211 else {
Guido van Rossuma027efa1997-05-05 20:56:21 +00004212 PyErr_SetString(PyExc_TypeError,
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004213 "slice indices must be integers or "
4214 "None or have an __index__ method");
Guido van Rossum20c6add2000-05-08 14:06:50 +00004215 return 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004216 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00004217 *pi = x;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004218 }
Guido van Rossum20c6add2000-05-08 14:06:50 +00004219 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004220}
4221
Guido van Rossum486364b2007-06-30 05:01:58 +00004222#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Brett Cannon39590462007-02-26 22:01:14 +00004223 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004224
Guido van Rossumb209a111997-04-29 18:18:01 +00004225static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004226cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004227{
Guido van Rossumac7be682001-01-17 15:42:30 +00004228 int res = 0;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004229 switch (op) {
Martin v. Löwis7198a522002-01-01 19:59:11 +00004230 case PyCmp_IS:
Guido van Rossum3f5da241990-12-20 15:06:42 +00004231 res = (v == w);
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004232 break;
4233 case PyCmp_IS_NOT:
4234 res = (v != w);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004235 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004236 case PyCmp_IN:
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004237 res = PySequence_Contains(w, v);
4238 if (res < 0)
4239 return NULL;
4240 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004241 case PyCmp_NOT_IN:
Guido van Rossum7e33c6e1998-05-22 00:52:29 +00004242 res = PySequence_Contains(w, v);
Guido van Rossum3f5da241990-12-20 15:06:42 +00004243 if (res < 0)
4244 return NULL;
Raymond Hettinger4bad9ba2003-01-19 05:08:13 +00004245 res = !res;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004246 break;
Martin v. Löwis7198a522002-01-01 19:59:11 +00004247 case PyCmp_EXC_MATCH:
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004248 if (PyTuple_Check(w)) {
4249 Py_ssize_t i, length;
4250 length = PyTuple_Size(w);
4251 for (i = 0; i < length; i += 1) {
4252 PyObject *exc = PyTuple_GET_ITEM(w, i);
Brett Cannon39590462007-02-26 22:01:14 +00004253 if (!PyExceptionClass_Check(exc)) {
4254 PyErr_SetString(PyExc_TypeError,
4255 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004256 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004257 }
4258 }
4259 }
4260 else {
Brett Cannon39590462007-02-26 22:01:14 +00004261 if (!PyExceptionClass_Check(w)) {
4262 PyErr_SetString(PyExc_TypeError,
4263 CANNOT_CATCH_MSG);
Brett Cannonf74225d2007-02-26 21:10:16 +00004264 return NULL;
Thomas Wouters9fe394c2007-02-05 01:24:16 +00004265 }
4266 }
Barry Warsaw4249f541997-08-22 21:26:19 +00004267 res = PyErr_GivenExceptionMatches(v, w);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004268 break;
4269 default:
Guido van Rossumac7be682001-01-17 15:42:30 +00004270 return PyObject_RichCompare(v, w, op);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004271 }
Guido van Rossumb209a111997-04-29 18:18:01 +00004272 v = res ? Py_True : Py_False;
4273 Py_INCREF(v);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004274 return v;
4275}
4276
Thomas Wouters52152252000-08-17 22:55:00 +00004277static PyObject *
4278import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004279{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004280 PyObject *x;
4281
4282 x = PyObject_GetAttr(v, name);
4283 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Walter Dörwald573c08c2007-05-25 15:46:59 +00004284 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004285 }
Thomas Wouters52152252000-08-17 22:55:00 +00004286 return x;
4287}
Guido van Rossumac7be682001-01-17 15:42:30 +00004288
Thomas Wouters52152252000-08-17 22:55:00 +00004289static int
4290import_all_from(PyObject *locals, PyObject *v)
4291{
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004292 PyObject *all = PyObject_GetAttrString(v, "__all__");
4293 PyObject *dict, *name, *value;
4294 int skip_leading_underscores = 0;
4295 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004296
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004297 if (all == NULL) {
4298 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4299 return -1; /* Unexpected error */
4300 PyErr_Clear();
4301 dict = PyObject_GetAttrString(v, "__dict__");
4302 if (dict == NULL) {
4303 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4304 return -1;
4305 PyErr_SetString(PyExc_ImportError,
4306 "from-import-* object has no __dict__ and no __all__");
Guido van Rossum3f5da241990-12-20 15:06:42 +00004307 return -1;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004308 }
4309 all = PyMapping_Keys(dict);
4310 Py_DECREF(dict);
4311 if (all == NULL)
4312 return -1;
4313 skip_leading_underscores = 1;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004314 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004315
4316 for (pos = 0, err = 0; ; pos++) {
4317 name = PySequence_GetItem(all, pos);
4318 if (name == NULL) {
4319 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4320 err = -1;
4321 else
4322 PyErr_Clear();
4323 break;
4324 }
4325 if (skip_leading_underscores &&
Martin v. Löwis5b222132007-06-10 09:51:05 +00004326 PyUnicode_Check(name) &&
4327 PyUnicode_AS_UNICODE(name)[0] == '_')
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004328 {
4329 Py_DECREF(name);
4330 continue;
4331 }
4332 value = PyObject_GetAttr(v, name);
4333 if (value == NULL)
4334 err = -1;
Thomas Wouters89f507f2006-12-13 04:49:30 +00004335 else if (PyDict_CheckExact(locals))
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004336 err = PyDict_SetItem(locals, name, value);
Thomas Wouters89f507f2006-12-13 04:49:30 +00004337 else
4338 err = PyObject_SetItem(locals, name, value);
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004339 Py_DECREF(name);
4340 Py_XDECREF(value);
4341 if (err != 0)
4342 break;
4343 }
4344 Py_DECREF(all);
4345 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004346}
4347
Guido van Rossumac7be682001-01-17 15:42:30 +00004348static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004349format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004350{
Neal Norwitzda059e32007-08-26 05:33:45 +00004351 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004352
4353 if (!obj)
4354 return;
4355
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +00004356 obj_str = _PyUnicode_AsString(obj);
Paul Prescode68140d2000-08-30 20:25:01 +00004357 if (!obj_str)
4358 return;
4359
4360 PyErr_Format(exc, format_str, obj_str);
4361}
Guido van Rossum950361c1997-01-24 13:49:28 +00004362
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004363static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004364unicode_concatenate(PyObject *v, PyObject *w,
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004365 PyFrameObject *f, unsigned char *next_instr)
4366{
4367 /* This function implements 'variable += expr' when both arguments
Guido van Rossum98297ee2007-11-06 21:34:58 +00004368 are (Unicode) strings. */
4369 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4370 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00004371 Py_ssize_t new_len = v_len + w_len;
4372 if (new_len < 0) {
4373 PyErr_SetString(PyExc_OverflowError,
4374 "strings are too large to concat");
4375 return NULL;
4376 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004377
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004378 if (v->ob_refcnt == 2) {
4379 /* In the common case, there are 2 references to the value
4380 * stored in 'variable' when the += is performed: one on the
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004381 * value stack (in 'v') and one still stored in the
4382 * 'variable'. We try to delete the variable now to reduce
4383 * the refcnt to 1.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004384 */
4385 switch (*next_instr) {
4386 case STORE_FAST:
4387 {
4388 int oparg = PEEKARG();
4389 PyObject **fastlocals = f->f_localsplus;
4390 if (GETLOCAL(oparg) == v)
4391 SETLOCAL(oparg, NULL);
4392 break;
4393 }
4394 case STORE_DEREF:
4395 {
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004396 PyObject **freevars = (f->f_localsplus +
4397 f->f_code->co_nlocals);
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004398 PyObject *c = freevars[PEEKARG()];
4399 if (PyCell_GET(c) == v)
4400 PyCell_Set(c, NULL);
4401 break;
4402 }
4403 case STORE_NAME:
4404 {
4405 PyObject *names = f->f_code->co_names;
4406 PyObject *name = GETITEM(names, PEEKARG());
4407 PyObject *locals = f->f_locals;
4408 if (PyDict_CheckExact(locals) &&
4409 PyDict_GetItem(locals, name) == v) {
4410 if (PyDict_DelItem(locals, name) != 0) {
4411 PyErr_Clear();
4412 }
4413 }
4414 break;
4415 }
4416 }
4417 }
4418
Guido van Rossum98297ee2007-11-06 21:34:58 +00004419 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004420 /* Now we own the last reference to 'v', so we can resize it
4421 * in-place.
4422 */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004423 if (PyUnicode_Resize(&v, new_len) != 0) {
4424 /* XXX if PyUnicode_Resize() fails, 'v' has been
Thomas Wouters8ce81f72007-09-20 18:22:40 +00004425 * deallocated so it cannot be put back into
4426 * 'variable'. The MemoryError is raised when there
4427 * is no value in 'variable', which might (very
4428 * remotely) be a cause of incompatibilities.
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004429 */
4430 return NULL;
4431 }
4432 /* copy 'w' into the newly allocated area of 'v' */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004433 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4434 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004435 return v;
4436 }
4437 else {
4438 /* When in-place resizing is not an option. */
Guido van Rossum98297ee2007-11-06 21:34:58 +00004439 w = PyUnicode_Concat(v, w);
4440 Py_DECREF(v);
4441 return w;
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004442 }
4443}
4444
Guido van Rossum950361c1997-01-24 13:49:28 +00004445#ifdef DYNAMIC_EXECUTION_PROFILE
4446
Skip Montanarof118cb12001-10-15 20:51:38 +00004447static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004448getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004449{
4450 int i;
4451 PyObject *l = PyList_New(256);
4452 if (l == NULL) return NULL;
4453 for (i = 0; i < 256; i++) {
Christian Heimes217cfd12007-12-02 14:31:20 +00004454 PyObject *x = PyLong_FromLong(a[i]);
Guido van Rossum950361c1997-01-24 13:49:28 +00004455 if (x == NULL) {
4456 Py_DECREF(l);
4457 return NULL;
4458 }
4459 PyList_SetItem(l, i, x);
4460 }
4461 for (i = 0; i < 256; i++)
4462 a[i] = 0;
4463 return l;
4464}
4465
4466PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004467_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004468{
4469#ifndef DXPAIRS
4470 return getarray(dxp);
4471#else
4472 int i;
4473 PyObject *l = PyList_New(257);
4474 if (l == NULL) return NULL;
4475 for (i = 0; i < 257; i++) {
4476 PyObject *x = getarray(dxpairs[i]);
4477 if (x == NULL) {
4478 Py_DECREF(l);
4479 return NULL;
4480 }
4481 PyList_SetItem(l, i, x);
4482 }
4483 return l;
4484#endif
4485}
4486
4487#endif