blob: e848fb07cf96f85cf8730b8f8e0bbde322229d04 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Michael W. Hudson800ba232004-08-12 18:19:17 +000030#if defined(__ppc__) /* <- Don't know if this is the correct symbol; this
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000031 section should work for GCC on any PowerPC
32 platform, irrespective of OS.
33 POWER? Who knows :-) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000034
Michael W. Hudson75eabd22005-01-18 15:56:11 +000035#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000036
37static void
38ppc_getcounter(uint64 *v)
39{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000040 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000041
42 loop:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000043 asm volatile ("mftbu %0" : "=r" (tbu) );
44 asm volatile ("mftb %0" : "=r" (tb) );
45 asm volatile ("mftbu %0" : "=r" (tbu2));
46 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000047
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000048 /* The slightly peculiar way of writing the next lines is
49 compiled better by GCC than any other way I tried. */
50 ((long*)(v))[0] = tbu;
51 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000052}
53
Mark Dickinsonee2dd4a2009-10-31 10:20:38 +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 Dickinsonee2dd4a2009-10-31 10:20:38 +000061#elif defined(__x86_64__)
62
63/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
64 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
65 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
66 32-bit pieces of the result. */
67
68#define READ_TIMESTAMP(val) \
69 __asm__ __volatile__("rdtsc" : \
70 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
71
72
73#else
74
75#error "Don't know how to implement timestamp counter for this architecture"
76
Michael W. Hudson800ba232004-08-12 18:19:17 +000077#endif
78
Thomas Wouters477c8d52006-05-27 19:21:47 +000079void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000080 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000081{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000082 uint64 intr, inst, loop;
83 PyThreadState *tstate = PyThreadState_Get();
84 if (!tstate->interp->tscdump)
85 return;
86 intr = intr1 - intr0;
87 inst = inst1 - inst0 - intr;
88 loop = loop1 - loop0 - intr;
89 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahc87901d2010-06-23 18:54:09 +000090 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000091}
Michael W. Hudson800ba232004-08-12 18:19:17 +000092
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000093#endif
94
Guido van Rossum04691fc1992-08-12 15:35:34 +000095/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000096/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000097
Guido van Rossum408027e1996-12-30 16:17:54 +000098#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000099/* For debugging the interpreter: */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000100#define LLTRACE 1 /* Low-level trace feature */
101#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000102#endif
103
Jeremy Hylton52820442001-01-03 23:52:36 +0000104typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000105
Guido van Rossum374a9221991-04-04 10:40:29 +0000106/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000107#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000108static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000109#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000110static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000111#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000112static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
113static PyObject * do_call(PyObject *, PyObject ***, int, int);
114static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000115static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000116 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000117static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
118static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000119#define CALL_FLAG_VAR 1
120#define CALL_FLAG_KW 2
121
Guido van Rossum0a066c01992-03-27 17:29:15 +0000122#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000123static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000124static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000126static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000127 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000128static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahc87901d2010-06-23 18:54:09 +0000129 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000130static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000131static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahc87901d2010-06-23 18:54:09 +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 *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000140
Paul Prescode68140d2000-08-30 20:25:01 +0000141#define NAME_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000142 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000143#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000144 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000145#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000146 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000147#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000148 "free variable '%.200s' referenced before assignment" \
149 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000150
Guido van Rossum950361c1997-01-24 13:49:28 +0000151/* Dynamic execution profile */
152#ifdef DYNAMIC_EXECUTION_PROFILE
153#ifdef DXPAIRS
154static long dxpairs[257][256];
155#define dxp dxpairs[256]
156#else
157static long dxp[256];
158#endif
159#endif
160
Jeremy Hylton985eba52003-02-05 23:13:00 +0000161/* Function call profile */
162#ifdef CALL_PROFILE
163#define PCALL_NUM 11
164static int pcall[PCALL_NUM];
165
166#define PCALL_ALL 0
167#define PCALL_FUNCTION 1
168#define PCALL_FAST_FUNCTION 2
169#define PCALL_FASTER_FUNCTION 3
170#define PCALL_METHOD 4
171#define PCALL_BOUND_METHOD 5
172#define PCALL_CFUNCTION 6
173#define PCALL_TYPE 7
174#define PCALL_GENERATOR 8
175#define PCALL_OTHER 9
176#define PCALL_POP 10
177
178/* Notes about the statistics
179
180 PCALL_FAST stats
181
182 FAST_FUNCTION means no argument tuple needs to be created.
183 FASTER_FUNCTION means that the fast-path frame setup code is used.
184
185 If there is a method call where the call can be optimized by changing
186 the argument tuple and calling the function directly, it gets recorded
187 twice.
188
189 As a result, the relationship among the statistics appears to be
190 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
191 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
192 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
193 PCALL_METHOD > PCALL_BOUND_METHOD
194*/
195
196#define PCALL(POS) pcall[POS]++
197
198PyObject *
199PyEval_GetCallStats(PyObject *self)
200{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000201 return Py_BuildValue("iiiiiiiiiii",
202 pcall[0], pcall[1], pcall[2], pcall[3],
203 pcall[4], pcall[5], pcall[6], pcall[7],
204 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000205}
206#else
207#define PCALL(O)
208
209PyObject *
210PyEval_GetCallStats(PyObject *self)
211{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000212 Py_INCREF(Py_None);
213 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000214}
215#endif
216
Tim Peters5ca576e2001-06-18 22:08:13 +0000217
Guido van Rossume59214e1994-08-30 08:01:59 +0000218#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000219
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000220#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000221#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000222#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000223#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000224
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000225static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000226static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000227static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000228
Tim Peters7f468f22004-10-11 02:40:51 +0000229int
230PyEval_ThreadsInitialized(void)
231{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000232 return interpreter_lock != 0;
Tim Peters7f468f22004-10-11 02:40:51 +0000233}
234
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000235void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000236PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000237{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000238 if (interpreter_lock)
239 return;
240 interpreter_lock = PyThread_allocate_lock();
241 PyThread_acquire_lock(interpreter_lock, 1);
242 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000243}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000244
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000245void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000246PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000247{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000248 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000249}
250
251void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000252PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000253{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000254 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000255}
256
257void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000258PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000259{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000260 if (tstate == NULL)
261 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
262 /* Check someone has called PyEval_InitThreads() to create the lock */
263 assert(interpreter_lock);
264 PyThread_acquire_lock(interpreter_lock, 1);
265 if (PyThreadState_Swap(tstate) != NULL)
266 Py_FatalError(
267 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000268}
269
270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000272{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000273 if (tstate == NULL)
274 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
275 if (PyThreadState_Swap(NULL) != tstate)
276 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
277 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000278}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000279
280/* This function is called from PyOS_AfterFork to ensure that newly
281 created child processes don't hold locks referring to threads which
282 are not running in the child process. (This could also be done using
283 pthread_atfork mechanism, at least for the pthreads implementation.) */
284
285void
286PyEval_ReInitThreads(void)
287{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000288 PyObject *threading, *result;
289 PyThreadState *tstate;
Jesse Nollera8513972008-07-17 16:49:17 +0000290
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000291 if (!interpreter_lock)
292 return;
293 /*XXX Can't use PyThread_free_lock here because it does too
294 much error-checking. Doing this cleanly would require
295 adding a new function to each thread_*.h. Instead, just
296 create a new lock and waste a little bit of memory */
297 interpreter_lock = PyThread_allocate_lock();
298 pending_lock = PyThread_allocate_lock();
299 PyThread_acquire_lock(interpreter_lock, 1);
300 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000301
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000302 /* Update the threading module with the new state.
303 */
304 tstate = PyThreadState_GET();
305 threading = PyMapping_GetItemString(tstate->interp->modules,
306 "threading");
307 if (threading == NULL) {
308 /* threading not imported */
309 PyErr_Clear();
310 return;
311 }
312 result = PyObject_CallMethod(threading, "_after_fork", NULL);
313 if (result == NULL)
314 PyErr_WriteUnraisable(threading);
315 else
316 Py_DECREF(result);
317 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000318}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000319#endif
320
Guido van Rossumff4949e1992-08-05 19:58:53 +0000321/* Functions save_thread and restore_thread are always defined so
322 dynamically loaded modules needn't be compiled separately for use
323 with and without threads: */
324
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000325PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000326PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000327{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000328 PyThreadState *tstate = PyThreadState_Swap(NULL);
329 if (tstate == NULL)
330 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000331#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000332 if (interpreter_lock)
333 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000334#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000335 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000336}
337
338void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000341 if (tstate == NULL)
342 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000343#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000344 if (interpreter_lock) {
345 int err = errno;
346 PyThread_acquire_lock(interpreter_lock, 1);
347 errno = err;
348 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000349#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000350 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000351}
352
353
Guido van Rossuma9672091994-09-14 13:31:22 +0000354/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
355 signal handlers or Mac I/O completion routines) can schedule calls
356 to a function to be called synchronously.
357 The synchronous function is called with one void* argument.
358 It should return 0 for success or -1 for failure -- failure should
359 be accompanied by an exception.
360
361 If registry succeeds, the registry function returns 0; if it fails
362 (e.g. due to too many pending calls) it returns -1 (without setting
363 an exception condition).
364
365 Note that because registry may occur from within signal handlers,
366 or other asynchronous events, calling malloc() is unsafe!
367
368#ifdef WITH_THREAD
369 Any thread can schedule pending calls, but only the main thread
370 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000371 There is no facility to schedule calls to a particular thread, but
372 that should be easy to change, should that ever be required. In
373 that case, the static variables here should go into the python
374 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000375#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000376*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000377
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000378#ifdef WITH_THREAD
379
380/* The WITH_THREAD implementation is thread-safe. It allows
381 scheduling to be made from any thread, and even from an executing
382 callback.
383 */
384
385#define NPENDINGCALLS 32
386static struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000387 int (*func)(void *);
388 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000389} pendingcalls[NPENDINGCALLS];
390static int pendingfirst = 0;
391static int pendinglast = 0;
392static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
393static char pendingbusy = 0;
394
395int
396Py_AddPendingCall(int (*func)(void *), void *arg)
397{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000398 int i, j, result=0;
399 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000400
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000401 /* try a few times for the lock. Since this mechanism is used
402 * for signal handling (on the main thread), there is a (slim)
403 * chance that a signal is delivered on the same thread while we
404 * hold the lock during the Py_MakePendingCalls() function.
405 * This avoids a deadlock in that case.
406 * Note that signals can be delivered on any thread. In particular,
407 * on Windows, a SIGINT is delivered on a system-created worker
408 * thread.
409 * We also check for lock being NULL, in the unlikely case that
410 * this function is called before any bytecode evaluation takes place.
411 */
412 if (lock != NULL) {
413 for (i = 0; i<100; i++) {
414 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
415 break;
416 }
417 if (i == 100)
418 return -1;
419 }
420
421 i = pendinglast;
422 j = (i + 1) % NPENDINGCALLS;
423 if (j == pendingfirst) {
424 result = -1; /* Queue full */
425 } else {
426 pendingcalls[i].func = func;
427 pendingcalls[i].arg = arg;
428 pendinglast = j;
429 }
430 /* signal main loop */
431 _Py_Ticker = 0;
432 pendingcalls_to_do = 1;
433 if (lock != NULL)
434 PyThread_release_lock(lock);
435 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000436}
437
438int
439Py_MakePendingCalls(void)
440{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000441 int i;
442 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000443
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000444 if (!pending_lock) {
445 /* initial allocation of the lock */
446 pending_lock = PyThread_allocate_lock();
447 if (pending_lock == NULL)
448 return -1;
449 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000450
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000451 /* only service pending calls on main thread */
452 if (main_thread && PyThread_get_thread_ident() != main_thread)
453 return 0;
454 /* don't perform recursive pending calls */
455 if (pendingbusy)
456 return 0;
457 pendingbusy = 1;
458 /* perform a bounded number of calls, in case of recursion */
459 for (i=0; i<NPENDINGCALLS; i++) {
460 int j;
461 int (*func)(void *);
462 void *arg = NULL;
463
464 /* pop one item off the queue while holding the lock */
465 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
466 j = pendingfirst;
467 if (j == pendinglast) {
468 func = NULL; /* Queue empty */
469 } else {
470 func = pendingcalls[j].func;
471 arg = pendingcalls[j].arg;
472 pendingfirst = (j + 1) % NPENDINGCALLS;
473 }
474 pendingcalls_to_do = pendingfirst != pendinglast;
475 PyThread_release_lock(pending_lock);
476 /* having released the lock, perform the callback */
477 if (func == NULL)
478 break;
479 r = func(arg);
480 if (r)
481 break;
482 }
483 pendingbusy = 0;
484 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000485}
486
487#else /* if ! defined WITH_THREAD */
488
489/*
490 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
491 This code is used for signal handling in python that isn't built
492 with WITH_THREAD.
493 Don't use this implementation when Py_AddPendingCalls() can happen
494 on a different thread!
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000495
Guido van Rossuma9672091994-09-14 13:31:22 +0000496 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000497 (1) nested asynchronous calls to Py_AddPendingCall()
498 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000499
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000500 (1) is very unlikely because typically signal delivery
501 is blocked during signal handling. So it should be impossible.
502 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000503 The current code is safe against (2), but not against (1).
504 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000505 thread is present, interrupted by signals, and that the critical
506 section is protected with the "busy" variable. On Windows, which
507 delivers SIGINT on a system thread, this does not hold and therefore
508 Windows really shouldn't use this version.
509 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000510*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000511
Guido van Rossuma9672091994-09-14 13:31:22 +0000512#define NPENDINGCALLS 32
513static struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000514 int (*func)(void *);
515 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000516} pendingcalls[NPENDINGCALLS];
517static volatile int pendingfirst = 0;
518static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000519static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000520
521int
Thomas Wouters334fb892000-07-25 12:56:38 +0000522Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000523{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000524 static volatile int busy = 0;
525 int i, j;
526 /* XXX Begin critical section */
527 if (busy)
528 return -1;
529 busy = 1;
530 i = pendinglast;
531 j = (i + 1) % NPENDINGCALLS;
532 if (j == pendingfirst) {
533 busy = 0;
534 return -1; /* Queue full */
535 }
536 pendingcalls[i].func = func;
537 pendingcalls[i].arg = arg;
538 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000539
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000540 _Py_Ticker = 0;
541 pendingcalls_to_do = 1; /* Signal main loop */
542 busy = 0;
543 /* XXX End critical section */
544 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000545}
546
Guido van Rossum180d7b41994-09-29 09:45:57 +0000547int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000548Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000549{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000550 static int busy = 0;
551 if (busy)
552 return 0;
553 busy = 1;
554 pendingcalls_to_do = 0;
555 for (;;) {
556 int i;
557 int (*func)(void *);
558 void *arg;
559 i = pendingfirst;
560 if (i == pendinglast)
561 break; /* Queue empty */
562 func = pendingcalls[i].func;
563 arg = pendingcalls[i].arg;
564 pendingfirst = (i + 1) % NPENDINGCALLS;
565 if (func(arg) < 0) {
566 busy = 0;
567 pendingcalls_to_do = 1; /* We're not done yet */
568 return -1;
569 }
570 }
571 busy = 0;
572 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000573}
574
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000575#endif /* WITH_THREAD */
576
Guido van Rossuma9672091994-09-14 13:31:22 +0000577
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000578/* The interpreter's recursion limit */
579
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000580#ifndef Py_DEFAULT_RECURSION_LIMIT
581#define Py_DEFAULT_RECURSION_LIMIT 1000
582#endif
583static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
584int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000585
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000586int
587Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000588{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000589 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000590}
591
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000592void
593Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000594{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000595 recursion_limit = new_limit;
596 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000597}
598
Armin Rigo2b3eb402003-10-28 12:05:48 +0000599/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
600 if the recursion_depth reaches _Py_CheckRecursionLimit.
601 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
602 to guarantee that _Py_CheckRecursiveCall() is regularly called.
603 Without USE_STACKCHECK, there is no need for this. */
604int
605_Py_CheckRecursiveCall(char *where)
606{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000607 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000608
609#ifdef USE_STACKCHECK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000610 if (PyOS_CheckStack()) {
611 --tstate->recursion_depth;
612 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
613 return -1;
614 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000615#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000616 _Py_CheckRecursionLimit = recursion_limit;
617 if (tstate->recursion_critical)
618 /* Somebody asked that we don't check for recursion. */
619 return 0;
620 if (tstate->overflowed) {
621 if (tstate->recursion_depth > recursion_limit + 50) {
622 /* Overflowing while handling an overflow. Give up. */
623 Py_FatalError("Cannot recover from stack overflow.");
624 }
625 return 0;
626 }
627 if (tstate->recursion_depth > recursion_limit) {
628 --tstate->recursion_depth;
629 tstate->overflowed = 1;
630 PyErr_Format(PyExc_RuntimeError,
631 "maximum recursion depth exceeded%s",
632 where);
633 return -1;
634 }
635 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000636}
637
Guido van Rossum374a9221991-04-04 10:40:29 +0000638/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000639enum why_code {
Stefan Krahc87901d2010-06-23 18:54:09 +0000640 WHY_NOT = 0x0001, /* No error */
641 WHY_EXCEPTION = 0x0002, /* Exception occurred */
642 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
643 WHY_RETURN = 0x0008, /* 'return' statement */
644 WHY_BREAK = 0x0010, /* 'break' statement */
645 WHY_CONTINUE = 0x0020, /* 'continue' statement */
646 WHY_YIELD = 0x0040, /* 'yield' operator */
647 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000648};
Guido van Rossum374a9221991-04-04 10:40:29 +0000649
Collin Winter828f04a2007-08-31 00:04:24 +0000650static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000651static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000652
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000653/* Records whether tracing is on for any thread. Counts the number of
654 threads for which tstate->c_tracefunc is non-NULL, so if the value
655 is 0, we know we don't have to check this thread's c_tracefunc.
656 This speeds up the if statement in PyEval_EvalFrameEx() after
657 fast_next_opcode*/
658static int _Py_TracingPossible = 0;
659
Skip Montanarod581d772002-09-03 20:10:45 +0000660/* for manipulating the thread switch and periodic "stuff" - used to be
661 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000662int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000663volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000664
Guido van Rossumb209a111997-04-29 18:18:01 +0000665PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000666PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000667{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000668 return PyEval_EvalCodeEx(co,
669 globals, locals,
670 (PyObject **)NULL, 0,
671 (PyObject **)NULL, 0,
672 (PyObject **)NULL, 0,
673 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000674}
675
676
677/* Interpreter main loop */
678
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000679PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000680PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000681 /* This is for backward compatibility with extension modules that
682 used this API; core interpreter code should call
683 PyEval_EvalFrameEx() */
684 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000685}
686
687PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000688PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000689{
Guido van Rossum950361c1997-01-24 13:49:28 +0000690#ifdef DXPAIRS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000691 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000692#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000693 register PyObject **stack_pointer; /* Next free slot in value stack */
694 register unsigned char *next_instr;
695 register int opcode; /* Current opcode */
696 register int oparg; /* Current opcode argument, if any */
697 register enum why_code why; /* Reason for block stack unwind */
698 register int err; /* Error status -- nonzero if error */
699 register PyObject *x; /* Result object -- NULL if error */
700 register PyObject *v; /* Temporary objects popped off stack */
701 register PyObject *w;
702 register PyObject *u;
703 register PyObject *t;
704 register PyObject **fastlocals, **freevars;
705 PyObject *retval = NULL; /* Return value */
706 PyThreadState *tstate = PyThreadState_GET();
707 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000708
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000709 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000710
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000711 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000712
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000713 is true when the line being executed has changed. The
714 initial values are such as to make this false the first
715 time it is tested. */
716 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000717
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000718 unsigned char *first_instr;
719 PyObject *names;
720 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000721#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000722 /* Make it easier to find out where we are with a debugger */
723 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000724#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000725
Antoine Pitroub52ec782009-01-25 16:34:23 +0000726/* Computed GOTOs, or
727 the-optimization-commonly-but-improperly-known-as-"threaded code"
728 using gcc's labels-as-values extension
729 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
730
731 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000732 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000733 combined with a lookup table of jump addresses. However, since the
734 indirect jump instruction is shared by all opcodes, the CPU will have a
735 hard time making the right prediction for where to jump next (actually,
736 it will be always wrong except in the uncommon case of a sequence of
737 several identical opcodes).
738
739 "Threaded code" in contrast, uses an explicit jump table and an explicit
740 indirect jump instruction at the end of each opcode. Since the jump
741 instruction is at a different address for each opcode, the CPU will make a
742 separate prediction for each of these instructions, which is equivalent to
743 predicting the second opcode of each opcode pair. These predictions have
744 a much better chance to turn out valid, especially in small bytecode loops.
745
746 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000747 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000748 and potentially many more instructions (depending on the pipeline width).
749 A correctly predicted branch, however, is nearly free.
750
751 At the time of this writing, the "threaded code" version is up to 15-20%
752 faster than the normal "switch" version, depending on the compiler and the
753 CPU architecture.
754
755 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
756 because it would render the measurements invalid.
757
758
759 NOTE: care must be taken that the compiler doesn't try to "optimize" the
760 indirect jumps by sharing them between all opcodes. Such optimizations
761 can be disabled on gcc by using the -fno-gcse flag (or possibly
762 -fno-crossjumping).
763*/
764
765#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
766#undef USE_COMPUTED_GOTOS
767#endif
768
769#ifdef USE_COMPUTED_GOTOS
770/* Import the static jump table */
771#include "opcode_targets.h"
772
773/* This macro is used when several opcodes defer to the same implementation
774 (e.g. SETUP_LOOP, SETUP_FINALLY) */
775#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000776 TARGET_##op: \
777 opcode = op; \
778 if (HAS_ARG(op)) \
779 oparg = NEXTARG(); \
780 case op: \
781 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000782
783#define TARGET(op) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000784 TARGET_##op: \
785 opcode = op; \
786 if (HAS_ARG(op)) \
787 oparg = NEXTARG(); \
788 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000789
790
791#define DISPATCH() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000792 { \
793 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
794 int _tick = _Py_Ticker - 1; \
795 _Py_Ticker = _tick; \
796 if (_tick >= 0) { \
797 FAST_DISPATCH(); \
798 } \
799 continue; \
800 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000801
802#ifdef LLTRACE
803#define FAST_DISPATCH() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000804 { \
805 if (!lltrace && !_Py_TracingPossible) { \
806 f->f_lasti = INSTR_OFFSET(); \
807 goto *opcode_targets[*next_instr++]; \
808 } \
809 goto fast_next_opcode; \
810 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000811#else
812#define FAST_DISPATCH() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000813 { \
814 if (!_Py_TracingPossible) { \
815 f->f_lasti = INSTR_OFFSET(); \
816 goto *opcode_targets[*next_instr++]; \
817 } \
818 goto fast_next_opcode; \
819 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000820#endif
821
822#else
823#define TARGET(op) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000824 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000825#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000826 /* silence compiler warnings about `impl` unused */ \
827 if (0) goto impl; \
828 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000829#define DISPATCH() continue
830#define FAST_DISPATCH() goto fast_next_opcode
831#endif
832
833
Neal Norwitza81d2202002-07-14 00:27:26 +0000834/* Tuple access macros */
835
836#ifndef Py_DEBUG
837#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
838#else
839#define GETITEM(v, i) PyTuple_GetItem((v), (i))
840#endif
841
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000842#ifdef WITH_TSC
843/* Use Pentium timestamp counter to mark certain events:
844 inst0 -- beginning of switch statement for opcode dispatch
845 inst1 -- end of switch statement (may be skipped)
846 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000847 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000848 (may be skipped)
849 intr1 -- beginning of long interruption
850 intr2 -- end of long interruption
851
852 Many opcodes call out to helper C functions. In some cases, the
853 time in those functions should be counted towards the time for the
854 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
855 calls another Python function; there's no point in charge all the
856 bytecode executed by the called function to the caller.
857
858 It's hard to make a useful judgement statically. In the presence
859 of operator overloading, it's impossible to tell if a call will
860 execute new Python code or not.
861
862 It's a case-by-case judgement. I'll use intr1 for the following
863 cases:
864
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000865 IMPORT_STAR
866 IMPORT_FROM
867 CALL_FUNCTION (and friends)
868
869 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000870 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
871 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000872
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000873 READ_TIMESTAMP(inst0);
874 READ_TIMESTAMP(inst1);
875 READ_TIMESTAMP(loop0);
876 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000877
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000878 /* shut up the compiler */
879 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000880#endif
881
Guido van Rossum374a9221991-04-04 10:40:29 +0000882/* Code access macros */
883
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000884#define INSTR_OFFSET() ((int)(next_instr - first_instr))
885#define NEXTOP() (*next_instr++)
886#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
887#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
888#define JUMPTO(x) (next_instr = first_instr + (x))
889#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000890
Raymond Hettingerf606f872003-03-16 03:11:04 +0000891/* OpCode prediction macros
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000892 Some opcodes tend to come in pairs thus making it possible to
893 predict the second code when the first is run. For example,
894 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
895 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000897 Verifying the prediction costs a single high-speed test of a register
898 variable against a constant. If the pairing was good, then the
899 processor's own internal branch predication has a high likelihood of
900 success, resulting in a nearly zero-overhead transition to the
901 next opcode. A successful prediction saves a trip through the eval-loop
902 including its two unpredictable branches, the HAS_ARG test and the
903 switch-case. Combined with the processor's internal branch prediction,
904 a successful PREDICT has the effect of making the two opcodes run as if
905 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000906
Georg Brandl86b2fb92008-07-16 03:43:04 +0000907 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000908 predictions turned-on and interpret the results as if some opcodes
909 had been combined or turn-off predictions so that the opcode frequency
910 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000911
912 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000913 the CPU to record separate branch prediction information for each
914 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000915
Raymond Hettingerf606f872003-03-16 03:11:04 +0000916*/
917
Antoine Pitroub52ec782009-01-25 16:34:23 +0000918#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000919#define PREDICT(op) if (0) goto PRED_##op
920#define PREDICTED(op) PRED_##op:
921#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000922#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000923#define PREDICT(op) if (*next_instr == op) goto PRED_##op
924#define PREDICTED(op) PRED_##op: next_instr++
925#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000926#endif
927
Raymond Hettingerf606f872003-03-16 03:11:04 +0000928
Guido van Rossum374a9221991-04-04 10:40:29 +0000929/* Stack manipulation macros */
930
Martin v. Löwis18e16552006-02-15 17:27:45 +0000931/* The stack can grow at most MAXINT deep, as co_nlocals and
932 co_stacksize are ints. */
Stefan Krahc87901d2010-06-23 18:54:09 +0000933#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
934#define EMPTY() (STACK_LEVEL() == 0)
935#define TOP() (stack_pointer[-1])
936#define SECOND() (stack_pointer[-2])
937#define THIRD() (stack_pointer[-3])
938#define FOURTH() (stack_pointer[-4])
939#define SET_TOP(v) (stack_pointer[-1] = (v))
940#define SET_SECOND(v) (stack_pointer[-2] = (v))
941#define SET_THIRD(v) (stack_pointer[-3] = (v))
942#define SET_FOURTH(v) (stack_pointer[-4] = (v))
943#define BASIC_STACKADJ(n) (stack_pointer += n)
944#define BASIC_PUSH(v) (*stack_pointer++ = (v))
945#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000946
Guido van Rossum96a42c81992-01-12 02:29:51 +0000947#ifdef LLTRACE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000948#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahc87901d2010-06-23 18:54:09 +0000949 lltrace && prtrace(TOP(), "push")); \
950 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000951#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahc87901d2010-06-23 18:54:09 +0000952 BASIC_POP())
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000953#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahc87901d2010-06-23 18:54:09 +0000954 lltrace && prtrace(TOP(), "stackadj")); \
955 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000956#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahc87901d2010-06-23 18:54:09 +0000957 prtrace((STACK_POINTER)[-1], "ext_pop")), \
958 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000959#else
Stefan Krahc87901d2010-06-23 18:54:09 +0000960#define PUSH(v) BASIC_PUSH(v)
961#define POP() BASIC_POP()
962#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000963#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000964#endif
965
Guido van Rossum681d79a1995-07-18 14:51:37 +0000966/* Local variable macros */
967
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000968#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000969
970/* The SETLOCAL() macro must not DECREF the local variable in-place and
971 then store the new value; it must copy the old value to a temporary
972 value, then store the new value, and then DECREF the temporary value.
973 This is because it is possible that during the DECREF the frame is
974 accessed by other code (e.g. a __del__ method or gc.collect()) and the
975 variable would be pointing to already-freed memory. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000976#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahc87901d2010-06-23 18:54:09 +0000977 GETLOCAL(i) = value; \
978 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000979
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000980
981#define UNWIND_BLOCK(b) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000982 while (STACK_LEVEL() > (b)->b_level) { \
983 PyObject *v = POP(); \
984 Py_XDECREF(v); \
985 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000986
987#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000988 { \
989 PyObject *type, *value, *traceback; \
990 assert(STACK_LEVEL() >= (b)->b_level + 3); \
991 while (STACK_LEVEL() > (b)->b_level + 3) { \
992 value = POP(); \
993 Py_XDECREF(value); \
994 } \
995 type = tstate->exc_type; \
996 value = tstate->exc_value; \
997 traceback = tstate->exc_traceback; \
998 tstate->exc_type = POP(); \
999 tstate->exc_value = POP(); \
1000 tstate->exc_traceback = POP(); \
1001 Py_XDECREF(type); \
1002 Py_XDECREF(value); \
1003 Py_XDECREF(traceback); \
1004 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001005
1006#define SAVE_EXC_STATE() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001007 { \
1008 PyObject *type, *value, *traceback; \
1009 Py_XINCREF(tstate->exc_type); \
1010 Py_XINCREF(tstate->exc_value); \
1011 Py_XINCREF(tstate->exc_traceback); \
1012 type = f->f_exc_type; \
1013 value = f->f_exc_value; \
1014 traceback = f->f_exc_traceback; \
1015 f->f_exc_type = tstate->exc_type; \
1016 f->f_exc_value = tstate->exc_value; \
1017 f->f_exc_traceback = tstate->exc_traceback; \
1018 Py_XDECREF(type); \
1019 Py_XDECREF(value); \
1020 Py_XDECREF(traceback); \
1021 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001022
1023#define SWAP_EXC_STATE() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001024 { \
1025 PyObject *tmp; \
1026 tmp = tstate->exc_type; \
1027 tstate->exc_type = f->f_exc_type; \
1028 f->f_exc_type = tmp; \
1029 tmp = tstate->exc_value; \
1030 tstate->exc_value = f->f_exc_value; \
1031 f->f_exc_value = tmp; \
1032 tmp = tstate->exc_traceback; \
1033 tstate->exc_traceback = f->f_exc_traceback; \
1034 f->f_exc_traceback = tmp; \
1035 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001036
Guido van Rossuma027efa1997-05-05 20:56:21 +00001037/* Start of code */
1038
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001039 if (f == NULL)
1040 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001041
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001042 /* push frame */
1043 if (Py_EnterRecursiveCall(""))
1044 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001045
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001046 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001047
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001048 if (tstate->use_tracing) {
1049 if (tstate->c_tracefunc != NULL) {
1050 /* tstate->c_tracefunc, if defined, is a
1051 function that will be called on *every* entry
1052 to a code block. Its return value, if not
1053 None, is a function that will be called at
1054 the start of each executed line of code.
1055 (Actually, the function must return itself
1056 in order to continue tracing.) The trace
1057 functions are called with three arguments:
1058 a pointer to the current frame, a string
1059 indicating why the function is called, and
1060 an argument which depends on the situation.
1061 The global trace function is also called
1062 whenever an exception is detected. */
1063 if (call_trace_protected(tstate->c_tracefunc,
1064 tstate->c_traceobj,
1065 f, PyTrace_CALL, Py_None)) {
1066 /* Trace function raised an error */
1067 goto exit_eval_frame;
1068 }
1069 }
1070 if (tstate->c_profilefunc != NULL) {
1071 /* Similar for c_profilefunc, except it needn't
1072 return itself and isn't called for "line" events */
1073 if (call_trace_protected(tstate->c_profilefunc,
1074 tstate->c_profileobj,
1075 f, PyTrace_CALL, Py_None)) {
1076 /* Profile function raised an error */
1077 goto exit_eval_frame;
1078 }
1079 }
1080 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001081
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001082 co = f->f_code;
1083 names = co->co_names;
1084 consts = co->co_consts;
1085 fastlocals = f->f_localsplus;
1086 freevars = f->f_localsplus + co->co_nlocals;
1087 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1088 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001089
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001090 f->f_lasti now refers to the index of the last instruction
1091 executed. You might think this was obvious from the name, but
1092 this wasn't always true before 2.3! PyFrame_New now sets
1093 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1094 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1095 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001096
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001097 When the PREDICT() macros are enabled, some opcode pairs follow in
1098 direct succession without updating f->f_lasti. A successful
1099 prediction effectively links the two codes together as if they
1100 were a single new opcode; accordingly,f->f_lasti will point to
1101 the first code in the pair (for instance, GET_ITER followed by
1102 FOR_ITER is effectively a single opcode and f->f_lasti will point
1103 at to the beginning of the combined pair.)
1104 */
1105 next_instr = first_instr + f->f_lasti + 1;
1106 stack_pointer = f->f_stacktop;
1107 assert(stack_pointer != NULL);
1108 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001109
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001110 if (co->co_flags & CO_GENERATOR && !throwflag) {
1111 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1112 /* We were in an except handler when we left,
1113 restore the exception state which was put aside
1114 (see YIELD_VALUE). */
1115 SWAP_EXC_STATE();
1116 }
1117 else {
1118 SAVE_EXC_STATE();
1119 }
1120 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001121
Tim Peters5ca576e2001-06-18 22:08:13 +00001122#ifdef LLTRACE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001123 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001124#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001125#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001126 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001127#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001129 why = WHY_NOT;
1130 err = 0;
1131 x = Py_None; /* Not a reference, just anything non-NULL */
1132 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001133
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001134 if (throwflag) { /* support for generator.throw() */
1135 why = WHY_EXCEPTION;
1136 goto on_error;
1137 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001138
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001139 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001140#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001141 if (inst1 == 0) {
1142 /* Almost surely, the opcode executed a break
1143 or a continue, preventing inst1 from being set
1144 on the way out of the loop.
1145 */
1146 READ_TIMESTAMP(inst1);
1147 loop1 = inst1;
1148 }
1149 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1150 intr0, intr1);
1151 ticked = 0;
1152 inst1 = 0;
1153 intr0 = 0;
1154 intr1 = 0;
1155 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001156#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001157 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1158 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001159
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001160 /* Do periodic things. Doing this every time through
1161 the loop would add too much overhead, so we do it
1162 only every Nth instruction. We also do it if
1163 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1164 event needs attention (e.g. a signal handler or
1165 async I/O handler); see Py_AddPendingCall() and
1166 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001167
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001168 if (--_Py_Ticker < 0) {
1169 if (*next_instr == SETUP_FINALLY) {
1170 /* Make the last opcode before
1171 a try: finally: block uninterruptable. */
1172 goto fast_next_opcode;
1173 }
1174 _Py_Ticker = _Py_CheckInterval;
1175 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001176#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001177 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001178#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001179 if (pendingcalls_to_do) {
1180 if (Py_MakePendingCalls() < 0) {
1181 why = WHY_EXCEPTION;
1182 goto on_error;
1183 }
1184 if (pendingcalls_to_do)
1185 /* MakePendingCalls() didn't succeed.
1186 Force early re-execution of this
1187 "periodic" code, possibly after
1188 a thread switch */
1189 _Py_Ticker = 0;
1190 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001191#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001192 if (interpreter_lock) {
1193 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001194
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001195 if (PyThreadState_Swap(NULL) != tstate)
1196 Py_FatalError("ceval: tstate mix-up");
1197 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001198
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001199 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001200
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001201 PyThread_acquire_lock(interpreter_lock, 1);
1202 if (PyThreadState_Swap(tstate) != NULL)
1203 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001204
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001205 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001206
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001207 if (tstate->async_exc != NULL) {
1208 x = tstate->async_exc;
1209 tstate->async_exc = NULL;
1210 PyErr_SetNone(x);
1211 Py_DECREF(x);
1212 why = WHY_EXCEPTION;
1213 goto on_error;
1214 }
1215 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001216#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001217 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001218
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001219 fast_next_opcode:
1220 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001222 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001223
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001224 if (_Py_TracingPossible &&
1225 tstate->c_tracefunc != NULL && !tstate->tracing) {
1226 /* see maybe_call_line_trace
1227 for expository comments */
1228 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001229
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001230 err = maybe_call_line_trace(tstate->c_tracefunc,
1231 tstate->c_traceobj,
1232 f, &instr_lb, &instr_ub,
1233 &instr_prev);
1234 /* Reload possibly changed frame fields */
1235 JUMPTO(f->f_lasti);
1236 if (f->f_stacktop != NULL) {
1237 stack_pointer = f->f_stacktop;
1238 f->f_stacktop = NULL;
1239 }
1240 if (err) {
1241 /* trace function raised an exception */
1242 goto on_error;
1243 }
1244 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001245
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001246 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001247
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001248 opcode = NEXTOP();
1249 oparg = 0; /* allows oparg to be stored in a register because
1250 it doesn't have to be remembered across a full loop */
1251 if (HAS_ARG(opcode))
1252 oparg = NEXTARG();
Stefan Krahc87901d2010-06-23 18:54:09 +00001253 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001254#ifdef DYNAMIC_EXECUTION_PROFILE
1255#ifdef DXPAIRS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001256 dxpairs[lastopcode][opcode]++;
1257 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001258#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001259 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001260#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001261
Guido van Rossum96a42c81992-01-12 02:29:51 +00001262#ifdef LLTRACE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001263 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001265 if (lltrace) {
1266 if (HAS_ARG(opcode)) {
1267 printf("%d: %d, %d\n",
1268 f->f_lasti, opcode, oparg);
1269 }
1270 else {
1271 printf("%d: %d\n",
1272 f->f_lasti, opcode);
1273 }
1274 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001275#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001276
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001277 /* Main switch on opcode */
1278 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001279
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001280 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001281
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001282 /* BEWARE!
1283 It is essential that any operation that fails sets either
1284 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1285 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001286
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001287 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001289 TARGET(NOP)
1290 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001291
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001292 TARGET(LOAD_FAST)
1293 x = GETLOCAL(oparg);
1294 if (x != NULL) {
1295 Py_INCREF(x);
1296 PUSH(x);
1297 FAST_DISPATCH();
1298 }
1299 format_exc_check_arg(PyExc_UnboundLocalError,
1300 UNBOUNDLOCAL_ERROR_MSG,
1301 PyTuple_GetItem(co->co_varnames, oparg));
1302 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001303
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001304 TARGET(LOAD_CONST)
1305 x = GETITEM(consts, oparg);
1306 Py_INCREF(x);
1307 PUSH(x);
1308 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001309
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001310 PREDICTED_WITH_ARG(STORE_FAST);
1311 TARGET(STORE_FAST)
1312 v = POP();
1313 SETLOCAL(oparg, v);
1314 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001315
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001316 TARGET(POP_TOP)
1317 v = POP();
1318 Py_DECREF(v);
1319 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001320
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001321 TARGET(ROT_TWO)
1322 v = TOP();
1323 w = SECOND();
1324 SET_TOP(w);
1325 SET_SECOND(v);
1326 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001327
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001328 TARGET(ROT_THREE)
1329 v = TOP();
1330 w = SECOND();
1331 x = THIRD();
1332 SET_TOP(w);
1333 SET_SECOND(x);
1334 SET_THIRD(v);
1335 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001336
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001337 TARGET(ROT_FOUR)
1338 u = TOP();
1339 v = SECOND();
1340 w = THIRD();
1341 x = FOURTH();
1342 SET_TOP(v);
1343 SET_SECOND(w);
1344 SET_THIRD(x);
1345 SET_FOURTH(u);
1346 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001347
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001348 TARGET(DUP_TOP)
1349 v = TOP();
1350 Py_INCREF(v);
1351 PUSH(v);
1352 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001353
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001354 TARGET(DUP_TOPX)
1355 if (oparg == 2) {
1356 x = TOP();
1357 Py_INCREF(x);
1358 w = SECOND();
1359 Py_INCREF(w);
1360 STACKADJ(2);
1361 SET_TOP(x);
1362 SET_SECOND(w);
1363 FAST_DISPATCH();
1364 } else if (oparg == 3) {
1365 x = TOP();
1366 Py_INCREF(x);
1367 w = SECOND();
1368 Py_INCREF(w);
1369 v = THIRD();
1370 Py_INCREF(v);
1371 STACKADJ(3);
1372 SET_TOP(x);
1373 SET_SECOND(w);
1374 SET_THIRD(v);
1375 FAST_DISPATCH();
1376 }
1377 Py_FatalError("invalid argument to DUP_TOPX"
1378 " (bytecode corruption?)");
1379 /* Never returns, so don't bother to set why. */
1380 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001381
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001382 TARGET(UNARY_POSITIVE)
1383 v = TOP();
1384 x = PyNumber_Positive(v);
1385 Py_DECREF(v);
1386 SET_TOP(x);
1387 if (x != NULL) DISPATCH();
1388 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001389
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001390 TARGET(UNARY_NEGATIVE)
1391 v = TOP();
1392 x = PyNumber_Negative(v);
1393 Py_DECREF(v);
1394 SET_TOP(x);
1395 if (x != NULL) DISPATCH();
1396 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001398 TARGET(UNARY_NOT)
1399 v = TOP();
1400 err = PyObject_IsTrue(v);
1401 Py_DECREF(v);
1402 if (err == 0) {
1403 Py_INCREF(Py_True);
1404 SET_TOP(Py_True);
1405 DISPATCH();
1406 }
1407 else if (err > 0) {
1408 Py_INCREF(Py_False);
1409 SET_TOP(Py_False);
1410 err = 0;
1411 DISPATCH();
1412 }
1413 STACKADJ(-1);
1414 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001415
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001416 TARGET(UNARY_INVERT)
1417 v = TOP();
1418 x = PyNumber_Invert(v);
1419 Py_DECREF(v);
1420 SET_TOP(x);
1421 if (x != NULL) DISPATCH();
1422 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001423
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001424 TARGET(BINARY_POWER)
1425 w = POP();
1426 v = TOP();
1427 x = PyNumber_Power(v, w, Py_None);
1428 Py_DECREF(v);
1429 Py_DECREF(w);
1430 SET_TOP(x);
1431 if (x != NULL) DISPATCH();
1432 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001433
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001434 TARGET(BINARY_MULTIPLY)
1435 w = POP();
1436 v = TOP();
1437 x = PyNumber_Multiply(v, w);
1438 Py_DECREF(v);
1439 Py_DECREF(w);
1440 SET_TOP(x);
1441 if (x != NULL) DISPATCH();
1442 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001443
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001444 TARGET(BINARY_TRUE_DIVIDE)
1445 w = POP();
1446 v = TOP();
1447 x = PyNumber_TrueDivide(v, w);
1448 Py_DECREF(v);
1449 Py_DECREF(w);
1450 SET_TOP(x);
1451 if (x != NULL) DISPATCH();
1452 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001453
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001454 TARGET(BINARY_FLOOR_DIVIDE)
1455 w = POP();
1456 v = TOP();
1457 x = PyNumber_FloorDivide(v, w);
1458 Py_DECREF(v);
1459 Py_DECREF(w);
1460 SET_TOP(x);
1461 if (x != NULL) DISPATCH();
1462 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001463
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001464 TARGET(BINARY_MODULO)
1465 w = POP();
1466 v = TOP();
1467 if (PyUnicode_CheckExact(v))
1468 x = PyUnicode_Format(v, w);
1469 else
1470 x = PyNumber_Remainder(v, w);
1471 Py_DECREF(v);
1472 Py_DECREF(w);
1473 SET_TOP(x);
1474 if (x != NULL) DISPATCH();
1475 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001476
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001477 TARGET(BINARY_ADD)
1478 w = POP();
1479 v = TOP();
1480 if (PyUnicode_CheckExact(v) &&
1481 PyUnicode_CheckExact(w)) {
1482 x = unicode_concatenate(v, w, f, next_instr);
1483 /* unicode_concatenate consumed the ref to v */
1484 goto skip_decref_vx;
1485 }
1486 else {
1487 x = PyNumber_Add(v, w);
1488 }
1489 Py_DECREF(v);
1490 skip_decref_vx:
1491 Py_DECREF(w);
1492 SET_TOP(x);
1493 if (x != NULL) DISPATCH();
1494 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001495
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001496 TARGET(BINARY_SUBTRACT)
1497 w = POP();
1498 v = TOP();
1499 x = PyNumber_Subtract(v, w);
1500 Py_DECREF(v);
1501 Py_DECREF(w);
1502 SET_TOP(x);
1503 if (x != NULL) DISPATCH();
1504 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001506 TARGET(BINARY_SUBSCR)
1507 w = POP();
1508 v = TOP();
1509 x = PyObject_GetItem(v, w);
1510 Py_DECREF(v);
1511 Py_DECREF(w);
1512 SET_TOP(x);
1513 if (x != NULL) DISPATCH();
1514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001516 TARGET(BINARY_LSHIFT)
1517 w = POP();
1518 v = TOP();
1519 x = PyNumber_Lshift(v, w);
1520 Py_DECREF(v);
1521 Py_DECREF(w);
1522 SET_TOP(x);
1523 if (x != NULL) DISPATCH();
1524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001526 TARGET(BINARY_RSHIFT)
1527 w = POP();
1528 v = TOP();
1529 x = PyNumber_Rshift(v, w);
1530 Py_DECREF(v);
1531 Py_DECREF(w);
1532 SET_TOP(x);
1533 if (x != NULL) DISPATCH();
1534 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001536 TARGET(BINARY_AND)
1537 w = POP();
1538 v = TOP();
1539 x = PyNumber_And(v, w);
1540 Py_DECREF(v);
1541 Py_DECREF(w);
1542 SET_TOP(x);
1543 if (x != NULL) DISPATCH();
1544 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001546 TARGET(BINARY_XOR)
1547 w = POP();
1548 v = TOP();
1549 x = PyNumber_Xor(v, w);
1550 Py_DECREF(v);
1551 Py_DECREF(w);
1552 SET_TOP(x);
1553 if (x != NULL) DISPATCH();
1554 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001555
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001556 TARGET(BINARY_OR)
1557 w = POP();
1558 v = TOP();
1559 x = PyNumber_Or(v, w);
1560 Py_DECREF(v);
1561 Py_DECREF(w);
1562 SET_TOP(x);
1563 if (x != NULL) DISPATCH();
1564 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001565
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001566 TARGET(LIST_APPEND)
1567 w = POP();
1568 v = stack_pointer[-oparg];
1569 err = PyList_Append(v, w);
1570 Py_DECREF(w);
1571 if (err == 0) {
1572 PREDICT(JUMP_ABSOLUTE);
1573 DISPATCH();
1574 }
1575 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001576
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001577 TARGET(SET_ADD)
1578 w = POP();
1579 v = stack_pointer[-oparg];
1580 err = PySet_Add(v, w);
1581 Py_DECREF(w);
1582 if (err == 0) {
1583 PREDICT(JUMP_ABSOLUTE);
1584 DISPATCH();
1585 }
1586 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001587
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001588 TARGET(INPLACE_POWER)
1589 w = POP();
1590 v = TOP();
1591 x = PyNumber_InPlacePower(v, w, Py_None);
1592 Py_DECREF(v);
1593 Py_DECREF(w);
1594 SET_TOP(x);
1595 if (x != NULL) DISPATCH();
1596 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001597
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001598 TARGET(INPLACE_MULTIPLY)
1599 w = POP();
1600 v = TOP();
1601 x = PyNumber_InPlaceMultiply(v, w);
1602 Py_DECREF(v);
1603 Py_DECREF(w);
1604 SET_TOP(x);
1605 if (x != NULL) DISPATCH();
1606 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001607
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001608 TARGET(INPLACE_TRUE_DIVIDE)
1609 w = POP();
1610 v = TOP();
1611 x = PyNumber_InPlaceTrueDivide(v, w);
1612 Py_DECREF(v);
1613 Py_DECREF(w);
1614 SET_TOP(x);
1615 if (x != NULL) DISPATCH();
1616 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001617
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001618 TARGET(INPLACE_FLOOR_DIVIDE)
1619 w = POP();
1620 v = TOP();
1621 x = PyNumber_InPlaceFloorDivide(v, w);
1622 Py_DECREF(v);
1623 Py_DECREF(w);
1624 SET_TOP(x);
1625 if (x != NULL) DISPATCH();
1626 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001627
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001628 TARGET(INPLACE_MODULO)
1629 w = POP();
1630 v = TOP();
1631 x = PyNumber_InPlaceRemainder(v, w);
1632 Py_DECREF(v);
1633 Py_DECREF(w);
1634 SET_TOP(x);
1635 if (x != NULL) DISPATCH();
1636 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001637
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001638 TARGET(INPLACE_ADD)
1639 w = POP();
1640 v = TOP();
1641 if (PyUnicode_CheckExact(v) &&
1642 PyUnicode_CheckExact(w)) {
1643 x = unicode_concatenate(v, w, f, next_instr);
1644 /* unicode_concatenate consumed the ref to v */
1645 goto skip_decref_v;
1646 }
1647 else {
1648 x = PyNumber_InPlaceAdd(v, w);
1649 }
1650 Py_DECREF(v);
1651 skip_decref_v:
1652 Py_DECREF(w);
1653 SET_TOP(x);
1654 if (x != NULL) DISPATCH();
1655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001657 TARGET(INPLACE_SUBTRACT)
1658 w = POP();
1659 v = TOP();
1660 x = PyNumber_InPlaceSubtract(v, w);
1661 Py_DECREF(v);
1662 Py_DECREF(w);
1663 SET_TOP(x);
1664 if (x != NULL) DISPATCH();
1665 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001666
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001667 TARGET(INPLACE_LSHIFT)
1668 w = POP();
1669 v = TOP();
1670 x = PyNumber_InPlaceLshift(v, w);
1671 Py_DECREF(v);
1672 Py_DECREF(w);
1673 SET_TOP(x);
1674 if (x != NULL) DISPATCH();
1675 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001676
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001677 TARGET(INPLACE_RSHIFT)
1678 w = POP();
1679 v = TOP();
1680 x = PyNumber_InPlaceRshift(v, w);
1681 Py_DECREF(v);
1682 Py_DECREF(w);
1683 SET_TOP(x);
1684 if (x != NULL) DISPATCH();
1685 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001686
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001687 TARGET(INPLACE_AND)
1688 w = POP();
1689 v = TOP();
1690 x = PyNumber_InPlaceAnd(v, w);
1691 Py_DECREF(v);
1692 Py_DECREF(w);
1693 SET_TOP(x);
1694 if (x != NULL) DISPATCH();
1695 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001696
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001697 TARGET(INPLACE_XOR)
1698 w = POP();
1699 v = TOP();
1700 x = PyNumber_InPlaceXor(v, w);
1701 Py_DECREF(v);
1702 Py_DECREF(w);
1703 SET_TOP(x);
1704 if (x != NULL) DISPATCH();
1705 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001706
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001707 TARGET(INPLACE_OR)
1708 w = POP();
1709 v = TOP();
1710 x = PyNumber_InPlaceOr(v, w);
1711 Py_DECREF(v);
1712 Py_DECREF(w);
1713 SET_TOP(x);
1714 if (x != NULL) DISPATCH();
1715 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001716
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001717 TARGET(STORE_SUBSCR)
1718 w = TOP();
1719 v = SECOND();
1720 u = THIRD();
1721 STACKADJ(-3);
1722 /* v[w] = u */
1723 err = PyObject_SetItem(v, w, u);
1724 Py_DECREF(u);
1725 Py_DECREF(v);
1726 Py_DECREF(w);
1727 if (err == 0) DISPATCH();
1728 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001729
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001730 TARGET(DELETE_SUBSCR)
1731 w = TOP();
1732 v = SECOND();
1733 STACKADJ(-2);
1734 /* del v[w] */
1735 err = PyObject_DelItem(v, w);
1736 Py_DECREF(v);
1737 Py_DECREF(w);
1738 if (err == 0) DISPATCH();
1739 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001740
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001741 TARGET(PRINT_EXPR)
1742 v = POP();
1743 w = PySys_GetObject("displayhook");
1744 if (w == NULL) {
1745 PyErr_SetString(PyExc_RuntimeError,
1746 "lost sys.displayhook");
1747 err = -1;
1748 x = NULL;
1749 }
1750 if (err == 0) {
1751 x = PyTuple_Pack(1, v);
1752 if (x == NULL)
1753 err = -1;
1754 }
1755 if (err == 0) {
1756 w = PyEval_CallObject(w, x);
1757 Py_XDECREF(w);
1758 if (w == NULL)
1759 err = -1;
1760 }
1761 Py_DECREF(v);
1762 Py_XDECREF(x);
1763 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001764
Thomas Wouters434d0822000-08-24 20:11:32 +00001765#ifdef CASE_TOO_BIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001766 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001767#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001768 TARGET(RAISE_VARARGS)
1769 v = w = NULL;
1770 switch (oparg) {
1771 case 2:
1772 v = POP(); /* cause */
1773 case 1:
1774 w = POP(); /* exc */
1775 case 0: /* Fallthrough */
1776 why = do_raise(w, v);
1777 break;
1778 default:
1779 PyErr_SetString(PyExc_SystemError,
1780 "bad RAISE_VARARGS oparg");
1781 why = WHY_EXCEPTION;
1782 break;
1783 }
1784 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001785
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001786 TARGET(STORE_LOCALS)
1787 x = POP();
1788 v = f->f_locals;
1789 Py_XDECREF(v);
1790 f->f_locals = x;
1791 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001792
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001793 TARGET(RETURN_VALUE)
1794 retval = POP();
1795 why = WHY_RETURN;
1796 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001797
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001798 TARGET(YIELD_VALUE)
1799 retval = POP();
1800 f->f_stacktop = stack_pointer;
1801 why = WHY_YIELD;
1802 /* Put aside the current exception state and restore
1803 that of the calling frame. This only serves when
1804 "yield" is used inside an except handler. */
1805 SWAP_EXC_STATE();
1806 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001807
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001808 TARGET(POP_EXCEPT)
1809 {
1810 PyTryBlock *b = PyFrame_BlockPop(f);
1811 if (b->b_type != EXCEPT_HANDLER) {
1812 PyErr_SetString(PyExc_SystemError,
1813 "popped block is not an except handler");
1814 why = WHY_EXCEPTION;
1815 break;
1816 }
1817 UNWIND_EXCEPT_HANDLER(b);
1818 }
1819 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001820
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001821 TARGET(POP_BLOCK)
1822 {
1823 PyTryBlock *b = PyFrame_BlockPop(f);
1824 UNWIND_BLOCK(b);
1825 }
1826 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001828 PREDICTED(END_FINALLY);
1829 TARGET(END_FINALLY)
1830 v = POP();
1831 if (PyLong_Check(v)) {
1832 why = (enum why_code) PyLong_AS_LONG(v);
1833 assert(why != WHY_YIELD);
1834 if (why == WHY_RETURN ||
1835 why == WHY_CONTINUE)
1836 retval = POP();
1837 if (why == WHY_SILENCED) {
1838 /* An exception was silenced by 'with', we must
1839 manually unwind the EXCEPT_HANDLER block which was
1840 created when the exception was caught, otherwise
1841 the stack will be in an inconsistent state. */
1842 PyTryBlock *b = PyFrame_BlockPop(f);
1843 if (b->b_type != EXCEPT_HANDLER) {
1844 PyErr_SetString(PyExc_SystemError,
1845 "popped block is not an except handler");
1846 why = WHY_EXCEPTION;
1847 }
1848 else {
1849 UNWIND_EXCEPT_HANDLER(b);
1850 why = WHY_NOT;
1851 }
1852 }
1853 }
1854 else if (PyExceptionClass_Check(v)) {
1855 w = POP();
1856 u = POP();
1857 PyErr_Restore(v, w, u);
1858 why = WHY_RERAISE;
1859 break;
1860 }
1861 else if (v != Py_None) {
1862 PyErr_SetString(PyExc_SystemError,
1863 "'finally' pops bad exception");
1864 why = WHY_EXCEPTION;
1865 }
1866 Py_DECREF(v);
1867 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001868
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001869 TARGET(LOAD_BUILD_CLASS)
1870 x = PyDict_GetItemString(f->f_builtins,
1871 "__build_class__");
1872 if (x == NULL) {
1873 PyErr_SetString(PyExc_ImportError,
1874 "__build_class__ not found");
1875 break;
1876 }
1877 Py_INCREF(x);
1878 PUSH(x);
1879 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001880
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001881 TARGET(STORE_NAME)
1882 w = GETITEM(names, oparg);
1883 v = POP();
1884 if ((x = f->f_locals) != NULL) {
1885 if (PyDict_CheckExact(x))
1886 err = PyDict_SetItem(x, w, v);
1887 else
1888 err = PyObject_SetItem(x, w, v);
1889 Py_DECREF(v);
1890 if (err == 0) DISPATCH();
1891 break;
1892 }
1893 PyErr_Format(PyExc_SystemError,
1894 "no locals found when storing %R", w);
1895 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001896
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001897 TARGET(DELETE_NAME)
1898 w = GETITEM(names, oparg);
1899 if ((x = f->f_locals) != NULL) {
1900 if ((err = PyObject_DelItem(x, w)) != 0)
1901 format_exc_check_arg(PyExc_NameError,
1902 NAME_ERROR_MSG,
1903 w);
1904 break;
1905 }
1906 PyErr_Format(PyExc_SystemError,
1907 "no locals when deleting %R", w);
1908 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001909
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001910 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1911 TARGET(UNPACK_SEQUENCE)
1912 v = POP();
1913 if (PyTuple_CheckExact(v) &&
1914 PyTuple_GET_SIZE(v) == oparg) {
1915 PyObject **items = \
1916 ((PyTupleObject *)v)->ob_item;
1917 while (oparg--) {
1918 w = items[oparg];
1919 Py_INCREF(w);
1920 PUSH(w);
1921 }
1922 Py_DECREF(v);
1923 DISPATCH();
1924 } else if (PyList_CheckExact(v) &&
1925 PyList_GET_SIZE(v) == oparg) {
1926 PyObject **items = \
1927 ((PyListObject *)v)->ob_item;
1928 while (oparg--) {
1929 w = items[oparg];
1930 Py_INCREF(w);
1931 PUSH(w);
1932 }
1933 } else if (unpack_iterable(v, oparg, -1,
1934 stack_pointer + oparg)) {
1935 stack_pointer += oparg;
1936 } else {
1937 /* unpack_iterable() raised an exception */
1938 why = WHY_EXCEPTION;
1939 }
1940 Py_DECREF(v);
1941 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001943 TARGET(UNPACK_EX)
1944 {
1945 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1946 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001947
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001948 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1949 stack_pointer + totalargs)) {
1950 stack_pointer += totalargs;
1951 } else {
1952 why = WHY_EXCEPTION;
1953 }
1954 Py_DECREF(v);
1955 break;
1956 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001957
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001958 TARGET(STORE_ATTR)
1959 w = GETITEM(names, oparg);
1960 v = TOP();
1961 u = SECOND();
1962 STACKADJ(-2);
1963 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1964 Py_DECREF(v);
1965 Py_DECREF(u);
1966 if (err == 0) DISPATCH();
1967 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001969 TARGET(DELETE_ATTR)
1970 w = GETITEM(names, oparg);
1971 v = POP();
1972 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1973 /* del v.w */
1974 Py_DECREF(v);
1975 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001976
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001977 TARGET(STORE_GLOBAL)
1978 w = GETITEM(names, oparg);
1979 v = POP();
1980 err = PyDict_SetItem(f->f_globals, w, v);
1981 Py_DECREF(v);
1982 if (err == 0) DISPATCH();
1983 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001984
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001985 TARGET(DELETE_GLOBAL)
1986 w = GETITEM(names, oparg);
1987 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1988 format_exc_check_arg(
1989 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1990 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001991
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001992 TARGET(LOAD_NAME)
1993 w = GETITEM(names, oparg);
1994 if ((v = f->f_locals) == NULL) {
1995 PyErr_Format(PyExc_SystemError,
1996 "no locals when loading %R", w);
1997 why = WHY_EXCEPTION;
1998 break;
1999 }
2000 if (PyDict_CheckExact(v)) {
2001 x = PyDict_GetItem(v, w);
2002 Py_XINCREF(x);
2003 }
2004 else {
2005 x = PyObject_GetItem(v, w);
2006 if (x == NULL && PyErr_Occurred()) {
2007 if (!PyErr_ExceptionMatches(
2008 PyExc_KeyError))
2009 break;
2010 PyErr_Clear();
2011 }
2012 }
2013 if (x == NULL) {
2014 x = PyDict_GetItem(f->f_globals, w);
2015 if (x == NULL) {
2016 x = PyDict_GetItem(f->f_builtins, w);
2017 if (x == NULL) {
2018 format_exc_check_arg(
2019 PyExc_NameError,
2020 NAME_ERROR_MSG, w);
2021 break;
2022 }
2023 }
2024 Py_INCREF(x);
2025 }
2026 PUSH(x);
2027 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002028
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002029 TARGET(LOAD_GLOBAL)
2030 w = GETITEM(names, oparg);
2031 if (PyUnicode_CheckExact(w)) {
2032 /* Inline the PyDict_GetItem() calls.
2033 WARNING: this is an extreme speed hack.
2034 Do not try this at home. */
2035 long hash = ((PyUnicodeObject *)w)->hash;
2036 if (hash != -1) {
2037 PyDictObject *d;
2038 PyDictEntry *e;
2039 d = (PyDictObject *)(f->f_globals);
2040 e = d->ma_lookup(d, w, hash);
2041 if (e == NULL) {
2042 x = NULL;
2043 break;
2044 }
2045 x = e->me_value;
2046 if (x != NULL) {
2047 Py_INCREF(x);
2048 PUSH(x);
2049 DISPATCH();
2050 }
2051 d = (PyDictObject *)(f->f_builtins);
2052 e = d->ma_lookup(d, w, hash);
2053 if (e == NULL) {
2054 x = NULL;
2055 break;
2056 }
2057 x = e->me_value;
2058 if (x != NULL) {
2059 Py_INCREF(x);
2060 PUSH(x);
2061 DISPATCH();
2062 }
2063 goto load_global_error;
2064 }
2065 }
2066 /* This is the un-inlined version of the code above */
2067 x = PyDict_GetItem(f->f_globals, w);
2068 if (x == NULL) {
2069 x = PyDict_GetItem(f->f_builtins, w);
2070 if (x == NULL) {
2071 load_global_error:
2072 format_exc_check_arg(
2073 PyExc_NameError,
2074 GLOBAL_NAME_ERROR_MSG, w);
2075 break;
2076 }
2077 }
2078 Py_INCREF(x);
2079 PUSH(x);
2080 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002081
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002082 TARGET(DELETE_FAST)
2083 x = GETLOCAL(oparg);
2084 if (x != NULL) {
2085 SETLOCAL(oparg, NULL);
2086 DISPATCH();
2087 }
2088 format_exc_check_arg(
2089 PyExc_UnboundLocalError,
2090 UNBOUNDLOCAL_ERROR_MSG,
2091 PyTuple_GetItem(co->co_varnames, oparg)
2092 );
2093 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002094
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002095 TARGET(LOAD_CLOSURE)
2096 x = freevars[oparg];
2097 Py_INCREF(x);
2098 PUSH(x);
2099 if (x != NULL) DISPATCH();
2100 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002101
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002102 TARGET(LOAD_DEREF)
2103 x = freevars[oparg];
2104 w = PyCell_Get(x);
2105 if (w != NULL) {
2106 PUSH(w);
2107 DISPATCH();
2108 }
2109 err = -1;
2110 /* Don't stomp existing exception */
2111 if (PyErr_Occurred())
2112 break;
2113 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2114 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krahc87901d2010-06-23 18:54:09 +00002115 oparg);
2116 format_exc_check_arg(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002117 PyExc_UnboundLocalError,
2118 UNBOUNDLOCAL_ERROR_MSG,
2119 v);
2120 } else {
2121 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2122 PyTuple_GET_SIZE(co->co_cellvars));
2123 format_exc_check_arg(PyExc_NameError,
2124 UNBOUNDFREE_ERROR_MSG, v);
2125 }
2126 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002127
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002128 TARGET(STORE_DEREF)
2129 w = POP();
2130 x = freevars[oparg];
2131 PyCell_Set(x, w);
2132 Py_DECREF(w);
2133 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002134
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002135 TARGET(BUILD_TUPLE)
2136 x = PyTuple_New(oparg);
2137 if (x != NULL) {
2138 for (; --oparg >= 0;) {
2139 w = POP();
2140 PyTuple_SET_ITEM(x, oparg, w);
2141 }
2142 PUSH(x);
2143 DISPATCH();
2144 }
2145 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002146
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002147 TARGET(BUILD_LIST)
2148 x = PyList_New(oparg);
2149 if (x != NULL) {
2150 for (; --oparg >= 0;) {
2151 w = POP();
2152 PyList_SET_ITEM(x, oparg, w);
2153 }
2154 PUSH(x);
2155 DISPATCH();
2156 }
2157 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002158
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002159 TARGET(BUILD_SET)
2160 x = PySet_New(NULL);
2161 if (x != NULL) {
2162 for (; --oparg >= 0;) {
2163 w = POP();
2164 if (err == 0)
2165 err = PySet_Add(x, w);
2166 Py_DECREF(w);
2167 }
2168 if (err != 0) {
2169 Py_DECREF(x);
2170 break;
2171 }
2172 PUSH(x);
2173 DISPATCH();
2174 }
2175 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002176
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002177 TARGET(BUILD_MAP)
2178 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2179 PUSH(x);
2180 if (x != NULL) DISPATCH();
2181 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002182
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002183 TARGET(STORE_MAP)
2184 w = TOP(); /* key */
2185 u = SECOND(); /* value */
2186 v = THIRD(); /* dict */
2187 STACKADJ(-2);
2188 assert (PyDict_CheckExact(v));
2189 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2190 Py_DECREF(u);
2191 Py_DECREF(w);
2192 if (err == 0) DISPATCH();
2193 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002194
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002195 TARGET(MAP_ADD)
2196 w = TOP(); /* key */
2197 u = SECOND(); /* value */
2198 STACKADJ(-2);
2199 v = stack_pointer[-oparg]; /* dict */
2200 assert (PyDict_CheckExact(v));
2201 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2202 Py_DECREF(u);
2203 Py_DECREF(w);
2204 if (err == 0) {
2205 PREDICT(JUMP_ABSOLUTE);
2206 DISPATCH();
2207 }
2208 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002209
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002210 TARGET(LOAD_ATTR)
2211 w = GETITEM(names, oparg);
2212 v = TOP();
2213 x = PyObject_GetAttr(v, w);
2214 Py_DECREF(v);
2215 SET_TOP(x);
2216 if (x != NULL) DISPATCH();
2217 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002218
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002219 TARGET(COMPARE_OP)
2220 w = POP();
2221 v = TOP();
2222 x = cmp_outcome(oparg, v, w);
2223 Py_DECREF(v);
2224 Py_DECREF(w);
2225 SET_TOP(x);
2226 if (x == NULL) break;
2227 PREDICT(POP_JUMP_IF_FALSE);
2228 PREDICT(POP_JUMP_IF_TRUE);
2229 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002230
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002231 TARGET(IMPORT_NAME)
2232 w = GETITEM(names, oparg);
2233 x = PyDict_GetItemString(f->f_builtins, "__import__");
2234 if (x == NULL) {
2235 PyErr_SetString(PyExc_ImportError,
2236 "__import__ not found");
2237 break;
2238 }
2239 Py_INCREF(x);
2240 v = POP();
2241 u = TOP();
2242 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2243 w = PyTuple_Pack(5,
2244 w,
2245 f->f_globals,
2246 f->f_locals == NULL ?
2247 Py_None : f->f_locals,
2248 v,
2249 u);
2250 else
2251 w = PyTuple_Pack(4,
2252 w,
2253 f->f_globals,
2254 f->f_locals == NULL ?
2255 Py_None : f->f_locals,
2256 v);
2257 Py_DECREF(v);
2258 Py_DECREF(u);
2259 if (w == NULL) {
2260 u = POP();
2261 Py_DECREF(x);
2262 x = NULL;
2263 break;
2264 }
2265 READ_TIMESTAMP(intr0);
2266 v = x;
2267 x = PyEval_CallObject(v, w);
2268 Py_DECREF(v);
2269 READ_TIMESTAMP(intr1);
2270 Py_DECREF(w);
2271 SET_TOP(x);
2272 if (x != NULL) DISPATCH();
2273 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002275 TARGET(IMPORT_STAR)
2276 v = POP();
2277 PyFrame_FastToLocals(f);
2278 if ((x = f->f_locals) == NULL) {
2279 PyErr_SetString(PyExc_SystemError,
2280 "no locals found during 'import *'");
2281 break;
2282 }
2283 READ_TIMESTAMP(intr0);
2284 err = import_all_from(x, v);
2285 READ_TIMESTAMP(intr1);
2286 PyFrame_LocalsToFast(f, 0);
2287 Py_DECREF(v);
2288 if (err == 0) DISPATCH();
2289 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002290
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002291 TARGET(IMPORT_FROM)
2292 w = GETITEM(names, oparg);
2293 v = TOP();
2294 READ_TIMESTAMP(intr0);
2295 x = import_from(v, w);
2296 READ_TIMESTAMP(intr1);
2297 PUSH(x);
2298 if (x != NULL) DISPATCH();
2299 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002300
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002301 TARGET(JUMP_FORWARD)
2302 JUMPBY(oparg);
2303 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002304
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002305 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2306 TARGET(POP_JUMP_IF_FALSE)
2307 w = POP();
2308 if (w == Py_True) {
2309 Py_DECREF(w);
2310 FAST_DISPATCH();
2311 }
2312 if (w == Py_False) {
2313 Py_DECREF(w);
2314 JUMPTO(oparg);
2315 FAST_DISPATCH();
2316 }
2317 err = PyObject_IsTrue(w);
2318 Py_DECREF(w);
2319 if (err > 0)
2320 err = 0;
2321 else if (err == 0)
2322 JUMPTO(oparg);
2323 else
2324 break;
2325 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002326
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002327 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2328 TARGET(POP_JUMP_IF_TRUE)
2329 w = POP();
2330 if (w == Py_False) {
2331 Py_DECREF(w);
2332 FAST_DISPATCH();
2333 }
2334 if (w == Py_True) {
2335 Py_DECREF(w);
2336 JUMPTO(oparg);
2337 FAST_DISPATCH();
2338 }
2339 err = PyObject_IsTrue(w);
2340 Py_DECREF(w);
2341 if (err > 0) {
2342 err = 0;
2343 JUMPTO(oparg);
2344 }
2345 else if (err == 0)
2346 ;
2347 else
2348 break;
2349 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002350
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002351 TARGET(JUMP_IF_FALSE_OR_POP)
2352 w = TOP();
2353 if (w == Py_True) {
2354 STACKADJ(-1);
2355 Py_DECREF(w);
2356 FAST_DISPATCH();
2357 }
2358 if (w == Py_False) {
2359 JUMPTO(oparg);
2360 FAST_DISPATCH();
2361 }
2362 err = PyObject_IsTrue(w);
2363 if (err > 0) {
2364 STACKADJ(-1);
2365 Py_DECREF(w);
2366 err = 0;
2367 }
2368 else if (err == 0)
2369 JUMPTO(oparg);
2370 else
2371 break;
2372 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002373
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002374 TARGET(JUMP_IF_TRUE_OR_POP)
2375 w = TOP();
2376 if (w == Py_False) {
2377 STACKADJ(-1);
2378 Py_DECREF(w);
2379 FAST_DISPATCH();
2380 }
2381 if (w == Py_True) {
2382 JUMPTO(oparg);
2383 FAST_DISPATCH();
2384 }
2385 err = PyObject_IsTrue(w);
2386 if (err > 0) {
2387 err = 0;
2388 JUMPTO(oparg);
2389 }
2390 else if (err == 0) {
2391 STACKADJ(-1);
2392 Py_DECREF(w);
2393 }
2394 else
2395 break;
2396 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002397
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002398 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2399 TARGET(JUMP_ABSOLUTE)
2400 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002401#if FAST_LOOPS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002402 /* Enabling this path speeds-up all while and for-loops by bypassing
2403 the per-loop checks for signals. By default, this should be turned-off
2404 because it prevents detection of a control-break in tight loops like
2405 "while 1: pass". Compile with this option turned-on when you need
2406 the speed-up and do not need break checking inside tight loops (ones
2407 that contain only instructions ending with FAST_DISPATCH).
2408 */
2409 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002410#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002411 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002412#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002413
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002414 TARGET(GET_ITER)
2415 /* before: [obj]; after [getiter(obj)] */
2416 v = TOP();
2417 x = PyObject_GetIter(v);
2418 Py_DECREF(v);
2419 if (x != NULL) {
2420 SET_TOP(x);
2421 PREDICT(FOR_ITER);
2422 DISPATCH();
2423 }
2424 STACKADJ(-1);
2425 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002426
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002427 PREDICTED_WITH_ARG(FOR_ITER);
2428 TARGET(FOR_ITER)
2429 /* before: [iter]; after: [iter, iter()] *or* [] */
2430 v = TOP();
2431 x = (*v->ob_type->tp_iternext)(v);
2432 if (x != NULL) {
2433 PUSH(x);
2434 PREDICT(STORE_FAST);
2435 PREDICT(UNPACK_SEQUENCE);
2436 DISPATCH();
2437 }
2438 if (PyErr_Occurred()) {
2439 if (!PyErr_ExceptionMatches(
2440 PyExc_StopIteration))
2441 break;
2442 PyErr_Clear();
2443 }
2444 /* iterator ended normally */
2445 x = v = POP();
2446 Py_DECREF(v);
2447 JUMPBY(oparg);
2448 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002449
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002450 TARGET(BREAK_LOOP)
2451 why = WHY_BREAK;
2452 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002453
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002454 TARGET(CONTINUE_LOOP)
2455 retval = PyLong_FromLong(oparg);
2456 if (!retval) {
2457 x = NULL;
2458 break;
2459 }
2460 why = WHY_CONTINUE;
2461 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002462
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002463 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2464 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2465 TARGET(SETUP_FINALLY)
2466 _setup_finally:
2467 /* NOTE: If you add any new block-setup opcodes that
2468 are not try/except/finally handlers, you may need
2469 to update the PyGen_NeedsFinalizing() function.
2470 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002471
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002472 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2473 STACK_LEVEL());
2474 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002475
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002476 TARGET(WITH_CLEANUP)
2477 {
2478 /* At the top of the stack are 1-3 values indicating
2479 how/why we entered the finally clause:
2480 - TOP = None
2481 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2482 - TOP = WHY_*; no retval below it
2483 - (TOP, SECOND, THIRD) = exc_info()
2484 Below them is EXIT, the context.__exit__ bound method.
2485 In the last case, we must call
2486 EXIT(TOP, SECOND, THIRD)
2487 otherwise we must call
2488 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002489
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002490 In all cases, we remove EXIT from the stack, leaving
2491 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002492
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002493 In addition, if the stack represents an exception,
2494 *and* the function call returns a 'true' value, we
2495 "zap" this information, to prevent END_FINALLY from
2496 re-raising the exception. (But non-local gotos
2497 should still be resumed.)
2498 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002499
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002500 PyObject *exit_func = POP();
2501 u = TOP();
2502 if (u == Py_None) {
2503 v = w = Py_None;
2504 }
2505 else if (PyLong_Check(u)) {
2506 u = v = w = Py_None;
2507 }
2508 else {
2509 v = SECOND();
2510 w = THIRD();
2511 }
2512 /* XXX Not the fastest way to call it... */
2513 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2514 NULL);
2515 Py_DECREF(exit_func);
2516 if (x == NULL)
2517 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002518
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002519 if (u != Py_None)
2520 err = PyObject_IsTrue(x);
2521 else
2522 err = 0;
2523 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002524
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002525 if (err < 0)
2526 break; /* Go to error exit */
2527 else if (err > 0) {
2528 err = 0;
2529 /* There was an exception and a True return */
2530 STACKADJ(-2);
2531 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
2532 Py_DECREF(u);
2533 Py_DECREF(v);
2534 Py_DECREF(w);
2535 }
2536 PREDICT(END_FINALLY);
2537 break;
2538 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002539
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002540 TARGET(CALL_FUNCTION)
2541 {
2542 PyObject **sp;
2543 PCALL(PCALL_ALL);
2544 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002545#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002546 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002547#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002548 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002549#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002550 stack_pointer = sp;
2551 PUSH(x);
2552 if (x != NULL)
2553 DISPATCH();
2554 break;
2555 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002556
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002557 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2558 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2559 TARGET(CALL_FUNCTION_VAR_KW)
2560 _call_function_var_kw:
2561 {
2562 int na = oparg & 0xff;
2563 int nk = (oparg>>8) & 0xff;
2564 int flags = (opcode - CALL_FUNCTION) & 3;
2565 int n = na + 2 * nk;
2566 PyObject **pfunc, *func, **sp;
2567 PCALL(PCALL_ALL);
2568 if (flags & CALL_FLAG_VAR)
2569 n++;
2570 if (flags & CALL_FLAG_KW)
2571 n++;
2572 pfunc = stack_pointer - n - 1;
2573 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002574
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002575 if (PyMethod_Check(func)
Stefan Krahc87901d2010-06-23 18:54:09 +00002576 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002577 PyObject *self = PyMethod_GET_SELF(func);
2578 Py_INCREF(self);
2579 func = PyMethod_GET_FUNCTION(func);
2580 Py_INCREF(func);
2581 Py_DECREF(*pfunc);
2582 *pfunc = self;
2583 na++;
2584 n++;
2585 } else
2586 Py_INCREF(func);
2587 sp = stack_pointer;
2588 READ_TIMESTAMP(intr0);
2589 x = ext_do_call(func, &sp, flags, na, nk);
2590 READ_TIMESTAMP(intr1);
2591 stack_pointer = sp;
2592 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002593
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002594 while (stack_pointer > pfunc) {
2595 w = POP();
2596 Py_DECREF(w);
2597 }
2598 PUSH(x);
2599 if (x != NULL)
2600 DISPATCH();
2601 break;
2602 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002603
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002604 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2605 TARGET(MAKE_FUNCTION)
2606 _make_function:
2607 {
2608 int posdefaults = oparg & 0xff;
2609 int kwdefaults = (oparg>>8) & 0xff;
2610 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002611
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002612 v = POP(); /* code object */
2613 x = PyFunction_New(v, f->f_globals);
2614 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002615
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002616 if (x != NULL && opcode == MAKE_CLOSURE) {
2617 v = POP();
2618 if (PyFunction_SetClosure(x, v) != 0) {
2619 /* Can't happen unless bytecode is corrupt. */
2620 why = WHY_EXCEPTION;
2621 }
2622 Py_DECREF(v);
2623 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002624
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002625 if (x != NULL && num_annotations > 0) {
2626 Py_ssize_t name_ix;
2627 u = POP(); /* names of args with annotations */
2628 v = PyDict_New();
2629 if (v == NULL) {
2630 Py_DECREF(x);
2631 x = NULL;
2632 break;
2633 }
2634 name_ix = PyTuple_Size(u);
2635 assert(num_annotations == name_ix+1);
2636 while (name_ix > 0) {
2637 --name_ix;
2638 t = PyTuple_GET_ITEM(u, name_ix);
2639 w = POP();
2640 /* XXX(nnorwitz): check for errors */
2641 PyDict_SetItem(v, t, w);
2642 Py_DECREF(w);
2643 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002644
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002645 if (PyFunction_SetAnnotations(x, v) != 0) {
2646 /* Can't happen unless
2647 PyFunction_SetAnnotations changes. */
2648 why = WHY_EXCEPTION;
2649 }
2650 Py_DECREF(v);
2651 Py_DECREF(u);
2652 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002653
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002654 /* XXX Maybe this should be a separate opcode? */
2655 if (x != NULL && posdefaults > 0) {
2656 v = PyTuple_New(posdefaults);
2657 if (v == NULL) {
2658 Py_DECREF(x);
2659 x = NULL;
2660 break;
2661 }
2662 while (--posdefaults >= 0) {
2663 w = POP();
2664 PyTuple_SET_ITEM(v, posdefaults, w);
2665 }
2666 if (PyFunction_SetDefaults(x, v) != 0) {
2667 /* Can't happen unless
2668 PyFunction_SetDefaults changes. */
2669 why = WHY_EXCEPTION;
2670 }
2671 Py_DECREF(v);
2672 }
2673 if (x != NULL && kwdefaults > 0) {
2674 v = PyDict_New();
2675 if (v == NULL) {
2676 Py_DECREF(x);
2677 x = NULL;
2678 break;
2679 }
2680 while (--kwdefaults >= 0) {
2681 w = POP(); /* default value */
2682 u = POP(); /* kw only arg name */
2683 /* XXX(nnorwitz): check for errors */
2684 PyDict_SetItem(v, u, w);
2685 Py_DECREF(w);
2686 Py_DECREF(u);
2687 }
2688 if (PyFunction_SetKwDefaults(x, v) != 0) {
2689 /* Can't happen unless
2690 PyFunction_SetKwDefaults changes. */
2691 why = WHY_EXCEPTION;
2692 }
2693 Py_DECREF(v);
2694 }
2695 PUSH(x);
2696 break;
2697 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002698
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002699 TARGET(BUILD_SLICE)
2700 if (oparg == 3)
2701 w = POP();
2702 else
2703 w = NULL;
2704 v = POP();
2705 u = TOP();
2706 x = PySlice_New(u, v, w);
2707 Py_DECREF(u);
2708 Py_DECREF(v);
2709 Py_XDECREF(w);
2710 SET_TOP(x);
2711 if (x != NULL) DISPATCH();
2712 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002713
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002714 TARGET(EXTENDED_ARG)
2715 opcode = NEXTOP();
2716 oparg = oparg<<16 | NEXTARG();
2717 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002718
Antoine Pitroub52ec782009-01-25 16:34:23 +00002719#ifdef USE_COMPUTED_GOTOS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002720 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002721#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002722 default:
2723 fprintf(stderr,
2724 "XXX lineno: %d, opcode: %d\n",
2725 PyCode_Addr2Line(f->f_code, f->f_lasti),
2726 opcode);
2727 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2728 why = WHY_EXCEPTION;
2729 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002730
2731#ifdef CASE_TOO_BIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002732 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002733#endif
2734
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002735 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002736
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002737 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002738
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002739 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002740
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002741 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002742
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002743 if (why == WHY_NOT) {
2744 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002745#ifdef CHECKEXC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002746 /* This check is expensive! */
2747 if (PyErr_Occurred())
2748 fprintf(stderr,
2749 "XXX undetected error\n");
2750 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002751#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002752 READ_TIMESTAMP(loop1);
2753 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002754#ifdef CHECKEXC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002755 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002756#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002757 }
2758 why = WHY_EXCEPTION;
2759 x = Py_None;
2760 err = 0;
2761 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002762
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002763 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002764
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002765 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2766 if (!PyErr_Occurred()) {
2767 PyErr_SetString(PyExc_SystemError,
2768 "error return without exception set");
2769 why = WHY_EXCEPTION;
2770 }
2771 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002772#ifdef CHECKEXC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002773 else {
2774 /* This check is expensive! */
2775 if (PyErr_Occurred()) {
2776 char buf[128];
2777 sprintf(buf, "Stack unwind with exception "
2778 "set and why=%d", why);
2779 Py_FatalError(buf);
2780 }
2781 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002782#endif
2783
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002784 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002785
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002786 if (why == WHY_EXCEPTION) {
2787 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002788
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002789 if (tstate->c_tracefunc != NULL)
2790 call_exc_trace(tstate->c_tracefunc,
2791 tstate->c_traceobj, f);
2792 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002793
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002794 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002795
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002796 if (why == WHY_RERAISE)
2797 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002798
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002799 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002800
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002801fast_block_end:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002802 while (why != WHY_NOT && f->f_iblock > 0) {
2803 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002804
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002805 assert(why != WHY_YIELD);
2806 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2807 /* For a continue inside a try block,
2808 don't pop the block for the loop. */
2809 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2810 b->b_level);
2811 why = WHY_NOT;
2812 JUMPTO(PyLong_AS_LONG(retval));
2813 Py_DECREF(retval);
2814 break;
2815 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002816
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002817 if (b->b_type == EXCEPT_HANDLER) {
2818 UNWIND_EXCEPT_HANDLER(b);
2819 continue;
2820 }
2821 UNWIND_BLOCK(b);
2822 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2823 why = WHY_NOT;
2824 JUMPTO(b->b_handler);
2825 break;
2826 }
2827 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2828 || b->b_type == SETUP_FINALLY)) {
2829 PyObject *exc, *val, *tb;
2830 int handler = b->b_handler;
2831 /* Beware, this invalidates all b->b_* fields */
2832 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2833 PUSH(tstate->exc_traceback);
2834 PUSH(tstate->exc_value);
2835 if (tstate->exc_type != NULL) {
2836 PUSH(tstate->exc_type);
2837 }
2838 else {
2839 Py_INCREF(Py_None);
2840 PUSH(Py_None);
2841 }
2842 PyErr_Fetch(&exc, &val, &tb);
2843 /* Make the raw exception data
2844 available to the handler,
2845 so a program can emulate the
2846 Python main loop. */
2847 PyErr_NormalizeException(
2848 &exc, &val, &tb);
2849 PyException_SetTraceback(val, tb);
2850 Py_INCREF(exc);
2851 tstate->exc_type = exc;
2852 Py_INCREF(val);
2853 tstate->exc_value = val;
2854 tstate->exc_traceback = tb;
2855 if (tb == NULL)
2856 tb = Py_None;
2857 Py_INCREF(tb);
2858 PUSH(tb);
2859 PUSH(val);
2860 PUSH(exc);
2861 why = WHY_NOT;
2862 JUMPTO(handler);
2863 break;
2864 }
2865 if (b->b_type == SETUP_FINALLY) {
2866 if (why & (WHY_RETURN | WHY_CONTINUE))
2867 PUSH(retval);
2868 PUSH(PyLong_FromLong((long)why));
2869 why = WHY_NOT;
2870 JUMPTO(b->b_handler);
2871 break;
2872 }
2873 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002874
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002875 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002876
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002877 if (why != WHY_NOT)
2878 break;
2879 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002880
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002881 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002882
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002883 assert(why != WHY_YIELD);
2884 /* Pop remaining stack entries. */
2885 while (!EMPTY()) {
2886 v = POP();
2887 Py_XDECREF(v);
2888 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002889
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002890 if (why != WHY_RETURN)
2891 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002892
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002893fast_yield:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002894 if (tstate->use_tracing) {
2895 if (tstate->c_tracefunc) {
2896 if (why == WHY_RETURN || why == WHY_YIELD) {
2897 if (call_trace(tstate->c_tracefunc,
2898 tstate->c_traceobj, f,
2899 PyTrace_RETURN, retval)) {
2900 Py_XDECREF(retval);
2901 retval = NULL;
2902 why = WHY_EXCEPTION;
2903 }
2904 }
2905 else if (why == WHY_EXCEPTION) {
2906 call_trace_protected(tstate->c_tracefunc,
2907 tstate->c_traceobj, f,
2908 PyTrace_RETURN, NULL);
2909 }
2910 }
2911 if (tstate->c_profilefunc) {
2912 if (why == WHY_EXCEPTION)
2913 call_trace_protected(tstate->c_profilefunc,
2914 tstate->c_profileobj, f,
2915 PyTrace_RETURN, NULL);
2916 else if (call_trace(tstate->c_profilefunc,
2917 tstate->c_profileobj, f,
2918 PyTrace_RETURN, retval)) {
2919 Py_XDECREF(retval);
2920 retval = NULL;
2921 why = WHY_EXCEPTION;
2922 }
2923 }
2924 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002925
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002926 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002927exit_eval_frame:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002928 Py_LeaveRecursiveCall();
2929 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002930
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002931 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002932}
2933
Guido van Rossumc2e20742006-02-27 22:32:47 +00002934/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002935 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002936 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002937
Tim Peters6d6c1a32001-08-02 04:15:00 +00002938PyObject *
2939PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002940 PyObject **args, int argcount, PyObject **kws, int kwcount,
2941 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002942{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002943 register PyFrameObject *f;
2944 register PyObject *retval = NULL;
2945 register PyObject **fastlocals, **freevars;
2946 PyThreadState *tstate = PyThreadState_GET();
2947 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00002948
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002949 if (globals == NULL) {
2950 PyErr_SetString(PyExc_SystemError,
2951 "PyEval_EvalCodeEx: NULL globals");
2952 return NULL;
2953 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002954
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002955 assert(tstate != NULL);
2956 assert(globals != NULL);
2957 f = PyFrame_New(tstate, co, globals, locals);
2958 if (f == NULL)
2959 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00002960
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002961 fastlocals = f->f_localsplus;
2962 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002963
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002964 if (co->co_argcount > 0 ||
2965 co->co_kwonlyargcount > 0 ||
2966 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2967 int i;
2968 int n = argcount;
2969 PyObject *kwdict = NULL;
2970 if (co->co_flags & CO_VARKEYWORDS) {
2971 kwdict = PyDict_New();
2972 if (kwdict == NULL)
2973 goto fail;
2974 i = co->co_argcount + co->co_kwonlyargcount;
2975 if (co->co_flags & CO_VARARGS)
2976 i++;
2977 SETLOCAL(i, kwdict);
2978 }
2979 if (argcount > co->co_argcount) {
2980 if (!(co->co_flags & CO_VARARGS)) {
2981 PyErr_Format(PyExc_TypeError,
2982 "%U() takes %s %d "
2983 "%spositional argument%s (%d given)",
2984 co->co_name,
2985 defcount ? "at most" : "exactly",
2986 co->co_argcount,
2987 kwcount ? "non-keyword " : "",
2988 co->co_argcount == 1 ? "" : "s",
2989 argcount);
2990 goto fail;
2991 }
2992 n = co->co_argcount;
2993 }
2994 for (i = 0; i < n; i++) {
2995 x = args[i];
2996 Py_INCREF(x);
2997 SETLOCAL(i, x);
2998 }
2999 if (co->co_flags & CO_VARARGS) {
3000 u = PyTuple_New(argcount - n);
3001 if (u == NULL)
3002 goto fail;
3003 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
3004 for (i = n; i < argcount; i++) {
3005 x = args[i];
3006 Py_INCREF(x);
3007 PyTuple_SET_ITEM(u, i-n, x);
3008 }
3009 }
3010 for (i = 0; i < kwcount; i++) {
3011 PyObject **co_varnames;
3012 PyObject *keyword = kws[2*i];
3013 PyObject *value = kws[2*i + 1];
3014 int j;
3015 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3016 PyErr_Format(PyExc_TypeError,
3017 "%U() keywords must be strings",
3018 co->co_name);
3019 goto fail;
3020 }
3021 /* Speed hack: do raw pointer compares. As names are
3022 normally interned this should almost always hit. */
3023 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
3024 for (j = 0;
3025 j < co->co_argcount + co->co_kwonlyargcount;
3026 j++) {
3027 PyObject *nm = co_varnames[j];
3028 if (nm == keyword)
3029 goto kw_found;
3030 }
3031 /* Slow fallback, just in case */
3032 for (j = 0;
3033 j < co->co_argcount + co->co_kwonlyargcount;
3034 j++) {
3035 PyObject *nm = co_varnames[j];
3036 int cmp = PyObject_RichCompareBool(
3037 keyword, nm, Py_EQ);
3038 if (cmp > 0)
3039 goto kw_found;
3040 else if (cmp < 0)
3041 goto fail;
3042 }
3043 /* Check errors from Compare */
3044 if (PyErr_Occurred())
3045 goto fail;
3046 if (j >= co->co_argcount + co->co_kwonlyargcount) {
3047 if (kwdict == NULL) {
3048 PyErr_Format(PyExc_TypeError,
3049 "%U() got an unexpected "
3050 "keyword argument '%S'",
3051 co->co_name,
3052 keyword);
3053 goto fail;
3054 }
3055 PyDict_SetItem(kwdict, keyword, value);
3056 continue;
3057 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003058kw_found:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003059 if (GETLOCAL(j) != NULL) {
3060 PyErr_Format(PyExc_TypeError,
3061 "%U() got multiple "
3062 "values for keyword "
3063 "argument '%S'",
3064 co->co_name,
3065 keyword);
3066 goto fail;
3067 }
3068 Py_INCREF(value);
3069 SETLOCAL(j, value);
3070 }
3071 if (co->co_kwonlyargcount > 0) {
3072 for (i = co->co_argcount;
3073 i < co->co_argcount + co->co_kwonlyargcount;
3074 i++) {
3075 PyObject *name, *def;
3076 if (GETLOCAL(i) != NULL)
3077 continue;
3078 name = PyTuple_GET_ITEM(co->co_varnames, i);
3079 def = NULL;
3080 if (kwdefs != NULL)
3081 def = PyDict_GetItem(kwdefs, name);
3082 if (def != NULL) {
3083 Py_INCREF(def);
3084 SETLOCAL(i, def);
3085 continue;
3086 }
3087 PyErr_Format(PyExc_TypeError,
3088 "%U() needs keyword-only argument %S",
3089 co->co_name, name);
3090 goto fail;
3091 }
3092 }
3093 if (argcount < co->co_argcount) {
3094 int m = co->co_argcount - defcount;
3095 for (i = argcount; i < m; i++) {
3096 if (GETLOCAL(i) == NULL) {
3097 PyErr_Format(PyExc_TypeError,
3098 "%U() takes %s %d "
3099 "%spositional argument%s "
3100 "(%d given)",
3101 co->co_name,
3102 ((co->co_flags & CO_VARARGS) ||
3103 defcount) ? "at least"
3104 : "exactly",
3105 m, kwcount ? "non-keyword " : "",
3106 m == 1 ? "" : "s", i);
3107 goto fail;
3108 }
3109 }
3110 if (n > m)
3111 i = n - m;
3112 else
3113 i = 0;
3114 for (; i < defcount; i++) {
3115 if (GETLOCAL(m+i) == NULL) {
3116 PyObject *def = defs[i];
3117 Py_INCREF(def);
3118 SETLOCAL(m+i, def);
3119 }
3120 }
3121 }
3122 }
3123 else {
3124 if (argcount > 0 || kwcount > 0) {
3125 PyErr_Format(PyExc_TypeError,
3126 "%U() takes no arguments (%d given)",
3127 co->co_name,
3128 argcount + kwcount);
3129 goto fail;
3130 }
3131 }
3132 /* Allocate and initialize storage for cell vars, and copy free
3133 vars into frame. This isn't too efficient right now. */
3134 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3135 int i, j, nargs, found;
3136 Py_UNICODE *cellname, *argname;
3137 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003138
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003139 nargs = co->co_argcount + co->co_kwonlyargcount;
3140 if (co->co_flags & CO_VARARGS)
3141 nargs++;
3142 if (co->co_flags & CO_VARKEYWORDS)
3143 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003144
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003145 /* Initialize each cell var, taking into account
3146 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003147
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003148 Should arrange for the compiler to put cellvars
3149 that are arguments at the beginning of the cellvars
3150 list so that we can march over it more efficiently?
3151 */
3152 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3153 cellname = PyUnicode_AS_UNICODE(
3154 PyTuple_GET_ITEM(co->co_cellvars, i));
3155 found = 0;
3156 for (j = 0; j < nargs; j++) {
3157 argname = PyUnicode_AS_UNICODE(
3158 PyTuple_GET_ITEM(co->co_varnames, j));
3159 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3160 c = PyCell_New(GETLOCAL(j));
3161 if (c == NULL)
3162 goto fail;
3163 GETLOCAL(co->co_nlocals + i) = c;
3164 found = 1;
3165 break;
3166 }
3167 }
3168 if (found == 0) {
3169 c = PyCell_New(NULL);
3170 if (c == NULL)
3171 goto fail;
3172 SETLOCAL(co->co_nlocals + i, c);
3173 }
3174 }
3175 }
3176 if (PyTuple_GET_SIZE(co->co_freevars)) {
3177 int i;
3178 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3179 PyObject *o = PyTuple_GET_ITEM(closure, i);
3180 Py_INCREF(o);
3181 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3182 }
3183 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003184
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003185 if (co->co_flags & CO_GENERATOR) {
3186 /* Don't need to keep the reference to f_back, it will be set
3187 * when the generator is resumed. */
3188 Py_XDECREF(f->f_back);
3189 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003190
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003191 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003192
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003193 /* Create a new generator that owns the ready to run frame
3194 * and return that as the value. */
3195 return PyGen_New(f);
3196 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003197
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003198 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003199
Thomas Woutersce272b62007-09-19 21:19:28 +00003200fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003201
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003202 /* decref'ing the frame can cause __del__ methods to get invoked,
3203 which can call back into Python. While we're done with the
3204 current Python frame (f), the associated C stack is still in use,
3205 so recursion_depth must be boosted for the duration.
3206 */
3207 assert(tstate != NULL);
3208 ++tstate->recursion_depth;
3209 Py_DECREF(f);
3210 --tstate->recursion_depth;
3211 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003212}
3213
3214
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003215/* Logic for the raise statement (too complicated for inlining).
3216 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003217static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003218do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003219{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003220 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003221
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003222 if (exc == NULL) {
3223 /* Reraise */
3224 PyThreadState *tstate = PyThreadState_GET();
3225 PyObject *tb;
3226 type = tstate->exc_type;
3227 value = tstate->exc_value;
3228 tb = tstate->exc_traceback;
3229 if (type == Py_None) {
3230 PyErr_SetString(PyExc_RuntimeError,
3231 "No active exception to reraise");
3232 return WHY_EXCEPTION;
3233 }
3234 Py_XINCREF(type);
3235 Py_XINCREF(value);
3236 Py_XINCREF(tb);
3237 PyErr_Restore(type, value, tb);
3238 return WHY_RERAISE;
3239 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003240
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003241 /* We support the following forms of raise:
3242 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003243 raise <instance>
3244 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003245
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003246 if (PyExceptionClass_Check(exc)) {
3247 type = exc;
3248 value = PyObject_CallObject(exc, NULL);
3249 if (value == NULL)
3250 goto raise_error;
3251 }
3252 else if (PyExceptionInstance_Check(exc)) {
3253 value = exc;
3254 type = PyExceptionInstance_Class(exc);
3255 Py_INCREF(type);
3256 }
3257 else {
3258 /* Not something you can raise. You get an exception
3259 anyway, just not what you specified :-) */
3260 Py_DECREF(exc);
3261 PyErr_SetString(PyExc_TypeError,
3262 "exceptions must derive from BaseException");
3263 goto raise_error;
3264 }
Collin Winter828f04a2007-08-31 00:04:24 +00003265
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003266 if (cause) {
3267 PyObject *fixed_cause;
3268 if (PyExceptionClass_Check(cause)) {
3269 fixed_cause = PyObject_CallObject(cause, NULL);
3270 if (fixed_cause == NULL)
3271 goto raise_error;
3272 Py_DECREF(cause);
3273 }
3274 else if (PyExceptionInstance_Check(cause)) {
3275 fixed_cause = cause;
3276 }
3277 else {
3278 PyErr_SetString(PyExc_TypeError,
3279 "exception causes must derive from "
3280 "BaseException");
3281 goto raise_error;
3282 }
3283 PyException_SetCause(value, fixed_cause);
3284 }
Collin Winter828f04a2007-08-31 00:04:24 +00003285
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003286 PyErr_SetObject(type, value);
3287 /* PyErr_SetObject incref's its arguments */
3288 Py_XDECREF(value);
3289 Py_XDECREF(type);
3290 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003291
3292raise_error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003293 Py_XDECREF(value);
3294 Py_XDECREF(type);
3295 Py_XDECREF(cause);
3296 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003297}
3298
Tim Petersd6d010b2001-06-21 02:49:55 +00003299/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003300 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003301
Guido van Rossum0368b722007-05-11 16:50:42 +00003302 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3303 with a variable target.
3304*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003305
Barry Warsawe42b18f1997-08-25 22:13:04 +00003306static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003307unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003308{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003309 int i = 0, j = 0;
3310 Py_ssize_t ll = 0;
3311 PyObject *it; /* iter(v) */
3312 PyObject *w;
3313 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003314
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003315 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003316
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003317 it = PyObject_GetIter(v);
3318 if (it == NULL)
3319 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003320
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003321 for (; i < argcnt; i++) {
3322 w = PyIter_Next(it);
3323 if (w == NULL) {
3324 /* Iterator done, via error or exhaustion. */
3325 if (!PyErr_Occurred()) {
3326 PyErr_Format(PyExc_ValueError,
3327 "need more than %d value%s to unpack",
3328 i, i == 1 ? "" : "s");
3329 }
3330 goto Error;
3331 }
3332 *--sp = w;
3333 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003334
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003335 if (argcntafter == -1) {
3336 /* We better have exhausted the iterator now. */
3337 w = PyIter_Next(it);
3338 if (w == NULL) {
3339 if (PyErr_Occurred())
3340 goto Error;
3341 Py_DECREF(it);
3342 return 1;
3343 }
3344 Py_DECREF(w);
3345 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3346 goto Error;
3347 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003348
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003349 l = PySequence_List(it);
3350 if (l == NULL)
3351 goto Error;
3352 *--sp = l;
3353 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003354
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003355 ll = PyList_GET_SIZE(l);
3356 if (ll < argcntafter) {
3357 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3358 argcnt + ll);
3359 goto Error;
3360 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003361
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003362 /* Pop the "after-variable" args off the list. */
3363 for (j = argcntafter; j > 0; j--, i++) {
3364 *--sp = PyList_GET_ITEM(l, ll - j);
3365 }
3366 /* Resize the list. */
3367 Py_SIZE(l) = ll - argcntafter;
3368 Py_DECREF(it);
3369 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003370
Tim Petersd6d010b2001-06-21 02:49:55 +00003371Error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003372 for (; i > 0; i--, sp++)
3373 Py_DECREF(*sp);
3374 Py_XDECREF(it);
3375 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003376}
3377
3378
Guido van Rossum96a42c81992-01-12 02:29:51 +00003379#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003380static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003381prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003382{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003383 printf("%s ", str);
3384 if (PyObject_Print(v, stdout, 0) != 0)
3385 PyErr_Clear(); /* Don't know what else to do */
3386 printf("\n");
3387 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003388}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003389#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003390
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003391static void
Fred Drake5755ce62001-06-27 19:19:46 +00003392call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003393{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003394 PyObject *type, *value, *traceback, *arg;
3395 int err;
3396 PyErr_Fetch(&type, &value, &traceback);
3397 if (value == NULL) {
3398 value = Py_None;
3399 Py_INCREF(value);
3400 }
3401 arg = PyTuple_Pack(3, type, value, traceback);
3402 if (arg == NULL) {
3403 PyErr_Restore(type, value, traceback);
3404 return;
3405 }
3406 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3407 Py_DECREF(arg);
3408 if (err == 0)
3409 PyErr_Restore(type, value, traceback);
3410 else {
3411 Py_XDECREF(type);
3412 Py_XDECREF(value);
3413 Py_XDECREF(traceback);
3414 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003415}
3416
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003417static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003418call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003419 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003420{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003421 PyObject *type, *value, *traceback;
3422 int err;
3423 PyErr_Fetch(&type, &value, &traceback);
3424 err = call_trace(func, obj, frame, what, arg);
3425 if (err == 0)
3426 {
3427 PyErr_Restore(type, value, traceback);
3428 return 0;
3429 }
3430 else {
3431 Py_XDECREF(type);
3432 Py_XDECREF(value);
3433 Py_XDECREF(traceback);
3434 return -1;
3435 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003436}
3437
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003438static int
Fred Drake5755ce62001-06-27 19:19:46 +00003439call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003440 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003441{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003442 register PyThreadState *tstate = frame->f_tstate;
3443 int result;
3444 if (tstate->tracing)
3445 return 0;
3446 tstate->tracing++;
3447 tstate->use_tracing = 0;
3448 result = func(obj, frame, what, arg);
3449 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3450 || (tstate->c_profilefunc != NULL));
3451 tstate->tracing--;
3452 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003453}
3454
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003455PyObject *
3456_PyEval_CallTracing(PyObject *func, PyObject *args)
3457{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003458 PyFrameObject *frame = PyEval_GetFrame();
3459 PyThreadState *tstate = frame->f_tstate;
3460 int save_tracing = tstate->tracing;
3461 int save_use_tracing = tstate->use_tracing;
3462 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003463
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003464 tstate->tracing = 0;
3465 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3466 || (tstate->c_profilefunc != NULL));
3467 result = PyObject_Call(func, args, NULL);
3468 tstate->tracing = save_tracing;
3469 tstate->use_tracing = save_use_tracing;
3470 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003471}
3472
Michael W. Hudson006c7522002-11-08 13:08:46 +00003473static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003474maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003475 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3476 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003477{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003478 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003479
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003480 /* If the last instruction executed isn't in the current
3481 instruction window, reset the window. If the last
3482 instruction happens to fall at the start of a line or if it
3483 represents a jump backwards, call the trace function.
3484 */
3485 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3486 int line;
3487 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003488
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003489 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3490 &bounds);
3491 if (line >= 0) {
3492 frame->f_lineno = line;
3493 result = call_trace(func, obj, frame,
3494 PyTrace_LINE, Py_None);
3495 }
3496 *instr_lb = bounds.ap_lower;
3497 *instr_ub = bounds.ap_upper;
3498 }
3499 else if (frame->f_lasti <= *instr_prev) {
3500 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3501 }
3502 *instr_prev = frame->f_lasti;
3503 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003504}
3505
Fred Drake5755ce62001-06-27 19:19:46 +00003506void
3507PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003508{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003509 PyThreadState *tstate = PyThreadState_GET();
3510 PyObject *temp = tstate->c_profileobj;
3511 Py_XINCREF(arg);
3512 tstate->c_profilefunc = NULL;
3513 tstate->c_profileobj = NULL;
3514 /* Must make sure that tracing is not ignored if 'temp' is freed */
3515 tstate->use_tracing = tstate->c_tracefunc != NULL;
3516 Py_XDECREF(temp);
3517 tstate->c_profilefunc = func;
3518 tstate->c_profileobj = arg;
3519 /* Flag that tracing or profiling is turned on */
3520 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003521}
3522
3523void
3524PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3525{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003526 PyThreadState *tstate = PyThreadState_GET();
3527 PyObject *temp = tstate->c_traceobj;
3528 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3529 Py_XINCREF(arg);
3530 tstate->c_tracefunc = NULL;
3531 tstate->c_traceobj = NULL;
3532 /* Must make sure that profiling is not ignored if 'temp' is freed */
3533 tstate->use_tracing = tstate->c_profilefunc != NULL;
3534 Py_XDECREF(temp);
3535 tstate->c_tracefunc = func;
3536 tstate->c_traceobj = arg;
3537 /* Flag that tracing or profiling is turned on */
3538 tstate->use_tracing = ((func != NULL)
3539 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003540}
3541
Guido van Rossumb209a111997-04-29 18:18:01 +00003542PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003543PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003544{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003545 PyFrameObject *current_frame = PyEval_GetFrame();
3546 if (current_frame == NULL)
3547 return PyThreadState_GET()->interp->builtins;
3548 else
3549 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003550}
3551
Guido van Rossumb209a111997-04-29 18:18:01 +00003552PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003553PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003554{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003555 PyFrameObject *current_frame = PyEval_GetFrame();
3556 if (current_frame == NULL)
3557 return NULL;
3558 PyFrame_FastToLocals(current_frame);
3559 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003560}
3561
Guido van Rossumb209a111997-04-29 18:18:01 +00003562PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003563PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003564{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003565 PyFrameObject *current_frame = PyEval_GetFrame();
3566 if (current_frame == NULL)
3567 return NULL;
3568 else
3569 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003570}
3571
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003572PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003573PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003574{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003575 PyThreadState *tstate = PyThreadState_GET();
3576 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003577}
3578
Guido van Rossum6135a871995-01-09 17:53:26 +00003579int
Tim Peters5ba58662001-07-16 02:29:45 +00003580PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003581{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003582 PyFrameObject *current_frame = PyEval_GetFrame();
3583 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003584
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003585 if (current_frame != NULL) {
3586 const int codeflags = current_frame->f_code->co_flags;
3587 const int compilerflags = codeflags & PyCF_MASK;
3588 if (compilerflags) {
3589 result = 1;
3590 cf->cf_flags |= compilerflags;
3591 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003592#if 0 /* future keyword */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003593 if (codeflags & CO_GENERATOR_ALLOWED) {
3594 result = 1;
3595 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3596 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003597#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003598 }
3599 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003600}
3601
Guido van Rossum3f5da241990-12-20 15:06:42 +00003602
Guido van Rossum681d79a1995-07-18 14:51:37 +00003603/* External interface to call any callable object.
3604 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003605
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003606#undef PyEval_CallObject
3607/* for backward compatibility: export this interface */
3608
Guido van Rossumb209a111997-04-29 18:18:01 +00003609PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003610PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003611{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003612 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003613}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003614#define PyEval_CallObject(func,arg) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003615 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003616
Guido van Rossumb209a111997-04-29 18:18:01 +00003617PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003618PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003619{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003620 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003621
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003622 if (arg == NULL) {
3623 arg = PyTuple_New(0);
3624 if (arg == NULL)
3625 return NULL;
3626 }
3627 else if (!PyTuple_Check(arg)) {
3628 PyErr_SetString(PyExc_TypeError,
3629 "argument list must be a tuple");
3630 return NULL;
3631 }
3632 else
3633 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003634
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003635 if (kw != NULL && !PyDict_Check(kw)) {
3636 PyErr_SetString(PyExc_TypeError,
3637 "keyword list must be a dictionary");
3638 Py_DECREF(arg);
3639 return NULL;
3640 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003641
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003642 result = PyObject_Call(func, arg, kw);
3643 Py_DECREF(arg);
3644 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003645}
3646
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003647const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003648PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003649{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003650 if (PyMethod_Check(func))
3651 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3652 else if (PyFunction_Check(func))
3653 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3654 else if (PyCFunction_Check(func))
3655 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3656 else
3657 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003658}
3659
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003660const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003661PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003662{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003663 if (PyMethod_Check(func))
3664 return "()";
3665 else if (PyFunction_Check(func))
3666 return "()";
3667 else if (PyCFunction_Check(func))
3668 return "()";
3669 else
3670 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003671}
3672
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003673static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003674err_args(PyObject *func, int flags, int nargs)
3675{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003676 if (flags & METH_NOARGS)
3677 PyErr_Format(PyExc_TypeError,
3678 "%.200s() takes no arguments (%d given)",
3679 ((PyCFunctionObject *)func)->m_ml->ml_name,
3680 nargs);
3681 else
3682 PyErr_Format(PyExc_TypeError,
3683 "%.200s() takes exactly one argument (%d given)",
3684 ((PyCFunctionObject *)func)->m_ml->ml_name,
3685 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003686}
3687
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003688#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003689if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003690 if (call_trace(tstate->c_profilefunc, \
3691 tstate->c_profileobj, \
3692 tstate->frame, PyTrace_C_CALL, \
3693 func)) { \
3694 x = NULL; \
3695 } \
3696 else { \
3697 x = call; \
3698 if (tstate->c_profilefunc != NULL) { \
3699 if (x == NULL) { \
3700 call_trace_protected(tstate->c_profilefunc, \
3701 tstate->c_profileobj, \
3702 tstate->frame, PyTrace_C_EXCEPTION, \
3703 func); \
3704 /* XXX should pass (type, value, tb) */ \
3705 } else { \
3706 if (call_trace(tstate->c_profilefunc, \
3707 tstate->c_profileobj, \
3708 tstate->frame, PyTrace_C_RETURN, \
3709 func)) { \
3710 Py_DECREF(x); \
3711 x = NULL; \
3712 } \
3713 } \
3714 } \
3715 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003716} else { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003717 x = call; \
3718 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003719
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003720static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003721call_function(PyObject ***pp_stack, int oparg
3722#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003723 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003724#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003725 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003726{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003727 int na = oparg & 0xff;
3728 int nk = (oparg>>8) & 0xff;
3729 int n = na + 2 * nk;
3730 PyObject **pfunc = (*pp_stack) - n - 1;
3731 PyObject *func = *pfunc;
3732 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003733
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003734 /* Always dispatch PyCFunction first, because these are
3735 presumed to be the most frequent callable object.
3736 */
3737 if (PyCFunction_Check(func) && nk == 0) {
3738 int flags = PyCFunction_GET_FLAGS(func);
3739 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003740
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003741 PCALL(PCALL_CFUNCTION);
3742 if (flags & (METH_NOARGS | METH_O)) {
3743 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3744 PyObject *self = PyCFunction_GET_SELF(func);
3745 if (flags & METH_NOARGS && na == 0) {
3746 C_TRACE(x, (*meth)(self,NULL));
3747 }
3748 else if (flags & METH_O && na == 1) {
3749 PyObject *arg = EXT_POP(*pp_stack);
3750 C_TRACE(x, (*meth)(self,arg));
3751 Py_DECREF(arg);
3752 }
3753 else {
3754 err_args(func, flags, na);
3755 x = NULL;
3756 }
3757 }
3758 else {
3759 PyObject *callargs;
3760 callargs = load_args(pp_stack, na);
3761 READ_TIMESTAMP(*pintr0);
3762 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3763 READ_TIMESTAMP(*pintr1);
3764 Py_XDECREF(callargs);
3765 }
3766 } else {
3767 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3768 /* optimize access to bound methods */
3769 PyObject *self = PyMethod_GET_SELF(func);
3770 PCALL(PCALL_METHOD);
3771 PCALL(PCALL_BOUND_METHOD);
3772 Py_INCREF(self);
3773 func = PyMethod_GET_FUNCTION(func);
3774 Py_INCREF(func);
3775 Py_DECREF(*pfunc);
3776 *pfunc = self;
3777 na++;
3778 n++;
3779 } else
3780 Py_INCREF(func);
3781 READ_TIMESTAMP(*pintr0);
3782 if (PyFunction_Check(func))
3783 x = fast_function(func, pp_stack, n, na, nk);
3784 else
3785 x = do_call(func, pp_stack, na, nk);
3786 READ_TIMESTAMP(*pintr1);
3787 Py_DECREF(func);
3788 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003789
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003790 /* Clear the stack of the function object. Also removes
3791 the arguments in case they weren't consumed already
3792 (fast_function() and err_args() leave them on the stack).
3793 */
3794 while ((*pp_stack) > pfunc) {
3795 w = EXT_POP(*pp_stack);
3796 Py_DECREF(w);
3797 PCALL(PCALL_POP);
3798 }
3799 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003800}
3801
Jeremy Hylton192690e2002-08-16 18:36:11 +00003802/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003803 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003804 For the simplest case -- a function that takes only positional
3805 arguments and is called with only positional arguments -- it
3806 inlines the most primitive frame setup code from
3807 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3808 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003809*/
3810
3811static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003812fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003813{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003814 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3815 PyObject *globals = PyFunction_GET_GLOBALS(func);
3816 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3817 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3818 PyObject **d = NULL;
3819 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003820
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003821 PCALL(PCALL_FUNCTION);
3822 PCALL(PCALL_FAST_FUNCTION);
3823 if (argdefs == NULL && co->co_argcount == n &&
3824 co->co_kwonlyargcount == 0 && nk==0 &&
3825 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3826 PyFrameObject *f;
3827 PyObject *retval = NULL;
3828 PyThreadState *tstate = PyThreadState_GET();
3829 PyObject **fastlocals, **stack;
3830 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003831
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003832 PCALL(PCALL_FASTER_FUNCTION);
3833 assert(globals != NULL);
3834 /* XXX Perhaps we should create a specialized
3835 PyFrame_New() that doesn't take locals, but does
3836 take builtins without sanity checking them.
3837 */
3838 assert(tstate != NULL);
3839 f = PyFrame_New(tstate, co, globals, NULL);
3840 if (f == NULL)
3841 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003842
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003843 fastlocals = f->f_localsplus;
3844 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003845
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003846 for (i = 0; i < n; i++) {
3847 Py_INCREF(*stack);
3848 fastlocals[i] = *stack++;
3849 }
3850 retval = PyEval_EvalFrameEx(f,0);
3851 ++tstate->recursion_depth;
3852 Py_DECREF(f);
3853 --tstate->recursion_depth;
3854 return retval;
3855 }
3856 if (argdefs != NULL) {
3857 d = &PyTuple_GET_ITEM(argdefs, 0);
3858 nd = Py_SIZE(argdefs);
3859 }
3860 return PyEval_EvalCodeEx(co, globals,
3861 (PyObject *)NULL, (*pp_stack)-n, na,
3862 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3863 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003864}
3865
3866static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003867update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3868 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003869{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003870 PyObject *kwdict = NULL;
3871 if (orig_kwdict == NULL)
3872 kwdict = PyDict_New();
3873 else {
3874 kwdict = PyDict_Copy(orig_kwdict);
3875 Py_DECREF(orig_kwdict);
3876 }
3877 if (kwdict == NULL)
3878 return NULL;
3879 while (--nk >= 0) {
3880 int err;
3881 PyObject *value = EXT_POP(*pp_stack);
3882 PyObject *key = EXT_POP(*pp_stack);
3883 if (PyDict_GetItem(kwdict, key) != NULL) {
3884 PyErr_Format(PyExc_TypeError,
3885 "%.200s%s got multiple values "
3886 "for keyword argument '%U'",
3887 PyEval_GetFuncName(func),
3888 PyEval_GetFuncDesc(func),
3889 key);
3890 Py_DECREF(key);
3891 Py_DECREF(value);
3892 Py_DECREF(kwdict);
3893 return NULL;
3894 }
3895 err = PyDict_SetItem(kwdict, key, value);
3896 Py_DECREF(key);
3897 Py_DECREF(value);
3898 if (err) {
3899 Py_DECREF(kwdict);
3900 return NULL;
3901 }
3902 }
3903 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00003904}
3905
3906static PyObject *
3907update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003908 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00003909{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003910 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00003911
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003912 callargs = PyTuple_New(nstack + nstar);
3913 if (callargs == NULL) {
3914 return NULL;
3915 }
3916 if (nstar) {
3917 int i;
3918 for (i = 0; i < nstar; i++) {
3919 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3920 Py_INCREF(a);
3921 PyTuple_SET_ITEM(callargs, nstack + i, a);
3922 }
3923 }
3924 while (--nstack >= 0) {
3925 w = EXT_POP(*pp_stack);
3926 PyTuple_SET_ITEM(callargs, nstack, w);
3927 }
3928 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00003929}
3930
3931static PyObject *
3932load_args(PyObject ***pp_stack, int na)
3933{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003934 PyObject *args = PyTuple_New(na);
3935 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00003936
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003937 if (args == NULL)
3938 return NULL;
3939 while (--na >= 0) {
3940 w = EXT_POP(*pp_stack);
3941 PyTuple_SET_ITEM(args, na, w);
3942 }
3943 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00003944}
3945
3946static PyObject *
3947do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3948{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003949 PyObject *callargs = NULL;
3950 PyObject *kwdict = NULL;
3951 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00003952
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003953 if (nk > 0) {
3954 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
3955 if (kwdict == NULL)
3956 goto call_fail;
3957 }
3958 callargs = load_args(pp_stack, na);
3959 if (callargs == NULL)
3960 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003961#ifdef CALL_PROFILE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003962 /* At this point, we have to look at the type of func to
3963 update the call stats properly. Do it here so as to avoid
3964 exposing the call stats machinery outside ceval.c
3965 */
3966 if (PyFunction_Check(func))
3967 PCALL(PCALL_FUNCTION);
3968 else if (PyMethod_Check(func))
3969 PCALL(PCALL_METHOD);
3970 else if (PyType_Check(func))
3971 PCALL(PCALL_TYPE);
3972 else if (PyCFunction_Check(func))
3973 PCALL(PCALL_CFUNCTION);
3974 else
3975 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003976#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003977 if (PyCFunction_Check(func)) {
3978 PyThreadState *tstate = PyThreadState_GET();
3979 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
3980 }
3981 else
3982 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003983call_fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003984 Py_XDECREF(callargs);
3985 Py_XDECREF(kwdict);
3986 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003987}
3988
3989static PyObject *
3990ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3991{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003992 int nstar = 0;
3993 PyObject *callargs = NULL;
3994 PyObject *stararg = NULL;
3995 PyObject *kwdict = NULL;
3996 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00003997
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003998 if (flags & CALL_FLAG_KW) {
3999 kwdict = EXT_POP(*pp_stack);
4000 if (!PyDict_Check(kwdict)) {
4001 PyObject *d;
4002 d = PyDict_New();
4003 if (d == NULL)
4004 goto ext_call_fail;
4005 if (PyDict_Update(d, kwdict) != 0) {
4006 Py_DECREF(d);
4007 /* PyDict_Update raises attribute
4008 * error (percolated from an attempt
4009 * to get 'keys' attribute) instead of
4010 * a type error if its second argument
4011 * is not a mapping.
4012 */
4013 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4014 PyErr_Format(PyExc_TypeError,
4015 "%.200s%.200s argument after ** "
4016 "must be a mapping, not %.200s",
4017 PyEval_GetFuncName(func),
4018 PyEval_GetFuncDesc(func),
4019 kwdict->ob_type->tp_name);
4020 }
4021 goto ext_call_fail;
4022 }
4023 Py_DECREF(kwdict);
4024 kwdict = d;
4025 }
4026 }
4027 if (flags & CALL_FLAG_VAR) {
4028 stararg = EXT_POP(*pp_stack);
4029 if (!PyTuple_Check(stararg)) {
4030 PyObject *t = NULL;
4031 t = PySequence_Tuple(stararg);
4032 if (t == NULL) {
4033 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4034 PyErr_Format(PyExc_TypeError,
4035 "%.200s%.200s argument after * "
4036 "must be a sequence, not %200s",
4037 PyEval_GetFuncName(func),
4038 PyEval_GetFuncDesc(func),
4039 stararg->ob_type->tp_name);
4040 }
4041 goto ext_call_fail;
4042 }
4043 Py_DECREF(stararg);
4044 stararg = t;
4045 }
4046 nstar = PyTuple_GET_SIZE(stararg);
4047 }
4048 if (nk > 0) {
4049 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4050 if (kwdict == NULL)
4051 goto ext_call_fail;
4052 }
4053 callargs = update_star_args(na, nstar, stararg, pp_stack);
4054 if (callargs == NULL)
4055 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004056#ifdef CALL_PROFILE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004057 /* At this point, we have to look at the type of func to
4058 update the call stats properly. Do it here so as to avoid
4059 exposing the call stats machinery outside ceval.c
4060 */
4061 if (PyFunction_Check(func))
4062 PCALL(PCALL_FUNCTION);
4063 else if (PyMethod_Check(func))
4064 PCALL(PCALL_METHOD);
4065 else if (PyType_Check(func))
4066 PCALL(PCALL_TYPE);
4067 else if (PyCFunction_Check(func))
4068 PCALL(PCALL_CFUNCTION);
4069 else
4070 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004071#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004072 if (PyCFunction_Check(func)) {
4073 PyThreadState *tstate = PyThreadState_GET();
4074 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4075 }
4076 else
4077 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004078ext_call_fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004079 Py_XDECREF(callargs);
4080 Py_XDECREF(kwdict);
4081 Py_XDECREF(stararg);
4082 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004083}
4084
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004085/* Extract a slice index from a PyInt or PyLong or an object with the
4086 nb_index slot defined, and store in *pi.
4087 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4088 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 +00004089 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004090*/
Tim Petersb5196382001-12-16 19:44:20 +00004091/* Note: If v is NULL, return success without storing into *pi. This
4092 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4093 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004094*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004095int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004096_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004097{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004098 if (v != NULL) {
4099 Py_ssize_t x;
4100 if (PyIndex_Check(v)) {
4101 x = PyNumber_AsSsize_t(v, NULL);
4102 if (x == -1 && PyErr_Occurred())
4103 return 0;
4104 }
4105 else {
4106 PyErr_SetString(PyExc_TypeError,
4107 "slice indices must be integers or "
4108 "None or have an __index__ method");
4109 return 0;
4110 }
4111 *pi = x;
4112 }
4113 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004114}
4115
Guido van Rossum486364b2007-06-30 05:01:58 +00004116#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004117 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004118
Guido van Rossumb209a111997-04-29 18:18:01 +00004119static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004120cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004121{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004122 int res = 0;
4123 switch (op) {
4124 case PyCmp_IS:
4125 res = (v == w);
4126 break;
4127 case PyCmp_IS_NOT:
4128 res = (v != w);
4129 break;
4130 case PyCmp_IN:
4131 res = PySequence_Contains(w, v);
4132 if (res < 0)
4133 return NULL;
4134 break;
4135 case PyCmp_NOT_IN:
4136 res = PySequence_Contains(w, v);
4137 if (res < 0)
4138 return NULL;
4139 res = !res;
4140 break;
4141 case PyCmp_EXC_MATCH:
4142 if (PyTuple_Check(w)) {
4143 Py_ssize_t i, length;
4144 length = PyTuple_Size(w);
4145 for (i = 0; i < length; i += 1) {
4146 PyObject *exc = PyTuple_GET_ITEM(w, i);
4147 if (!PyExceptionClass_Check(exc)) {
4148 PyErr_SetString(PyExc_TypeError,
4149 CANNOT_CATCH_MSG);
4150 return NULL;
4151 }
4152 }
4153 }
4154 else {
4155 if (!PyExceptionClass_Check(w)) {
4156 PyErr_SetString(PyExc_TypeError,
4157 CANNOT_CATCH_MSG);
4158 return NULL;
4159 }
4160 }
4161 res = PyErr_GivenExceptionMatches(v, w);
4162 break;
4163 default:
4164 return PyObject_RichCompare(v, w, op);
4165 }
4166 v = res ? Py_True : Py_False;
4167 Py_INCREF(v);
4168 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004169}
4170
Thomas Wouters52152252000-08-17 22:55:00 +00004171static PyObject *
4172import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004173{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004174 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004175
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004176 x = PyObject_GetAttr(v, name);
4177 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4178 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4179 }
4180 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004181}
Guido van Rossumac7be682001-01-17 15:42:30 +00004182
Thomas Wouters52152252000-08-17 22:55:00 +00004183static int
4184import_all_from(PyObject *locals, PyObject *v)
4185{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004186 PyObject *all = PyObject_GetAttrString(v, "__all__");
4187 PyObject *dict, *name, *value;
4188 int skip_leading_underscores = 0;
4189 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004190
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004191 if (all == NULL) {
4192 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4193 return -1; /* Unexpected error */
4194 PyErr_Clear();
4195 dict = PyObject_GetAttrString(v, "__dict__");
4196 if (dict == NULL) {
4197 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4198 return -1;
4199 PyErr_SetString(PyExc_ImportError,
4200 "from-import-* object has no __dict__ and no __all__");
4201 return -1;
4202 }
4203 all = PyMapping_Keys(dict);
4204 Py_DECREF(dict);
4205 if (all == NULL)
4206 return -1;
4207 skip_leading_underscores = 1;
4208 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004209
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004210 for (pos = 0, err = 0; ; pos++) {
4211 name = PySequence_GetItem(all, pos);
4212 if (name == NULL) {
4213 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4214 err = -1;
4215 else
4216 PyErr_Clear();
4217 break;
4218 }
4219 if (skip_leading_underscores &&
4220 PyUnicode_Check(name) &&
4221 PyUnicode_AS_UNICODE(name)[0] == '_')
4222 {
4223 Py_DECREF(name);
4224 continue;
4225 }
4226 value = PyObject_GetAttr(v, name);
4227 if (value == NULL)
4228 err = -1;
4229 else if (PyDict_CheckExact(locals))
4230 err = PyDict_SetItem(locals, name, value);
4231 else
4232 err = PyObject_SetItem(locals, name, value);
4233 Py_DECREF(name);
4234 Py_XDECREF(value);
4235 if (err != 0)
4236 break;
4237 }
4238 Py_DECREF(all);
4239 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004240}
4241
Guido van Rossumac7be682001-01-17 15:42:30 +00004242static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004243format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004244{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004245 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004246
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004247 if (!obj)
4248 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004249
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004250 obj_str = _PyUnicode_AsString(obj);
4251 if (!obj_str)
4252 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004253
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004254 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004255}
Guido van Rossum950361c1997-01-24 13:49:28 +00004256
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004257static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004258unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004259 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004260{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004261 /* This function implements 'variable += expr' when both arguments
4262 are (Unicode) strings. */
4263 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4264 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4265 Py_ssize_t new_len = v_len + w_len;
4266 if (new_len < 0) {
4267 PyErr_SetString(PyExc_OverflowError,
4268 "strings are too large to concat");
4269 return NULL;
4270 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004271
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004272 if (v->ob_refcnt == 2) {
4273 /* In the common case, there are 2 references to the value
4274 * stored in 'variable' when the += is performed: one on the
4275 * value stack (in 'v') and one still stored in the
4276 * 'variable'. We try to delete the variable now to reduce
4277 * the refcnt to 1.
4278 */
4279 switch (*next_instr) {
4280 case STORE_FAST:
4281 {
4282 int oparg = PEEKARG();
4283 PyObject **fastlocals = f->f_localsplus;
4284 if (GETLOCAL(oparg) == v)
4285 SETLOCAL(oparg, NULL);
4286 break;
4287 }
4288 case STORE_DEREF:
4289 {
4290 PyObject **freevars = (f->f_localsplus +
4291 f->f_code->co_nlocals);
4292 PyObject *c = freevars[PEEKARG()];
4293 if (PyCell_GET(c) == v)
4294 PyCell_Set(c, NULL);
4295 break;
4296 }
4297 case STORE_NAME:
4298 {
4299 PyObject *names = f->f_code->co_names;
4300 PyObject *name = GETITEM(names, PEEKARG());
4301 PyObject *locals = f->f_locals;
4302 if (PyDict_CheckExact(locals) &&
4303 PyDict_GetItem(locals, name) == v) {
4304 if (PyDict_DelItem(locals, name) != 0) {
4305 PyErr_Clear();
4306 }
4307 }
4308 break;
4309 }
4310 }
4311 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004312
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004313 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4314 /* Now we own the last reference to 'v', so we can resize it
4315 * in-place.
4316 */
4317 if (PyUnicode_Resize(&v, new_len) != 0) {
4318 /* XXX if PyUnicode_Resize() fails, 'v' has been
4319 * deallocated so it cannot be put back into
4320 * 'variable'. The MemoryError is raised when there
4321 * is no value in 'variable', which might (very
4322 * remotely) be a cause of incompatibilities.
4323 */
4324 return NULL;
4325 }
4326 /* copy 'w' into the newly allocated area of 'v' */
4327 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4328 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4329 return v;
4330 }
4331 else {
4332 /* When in-place resizing is not an option. */
4333 w = PyUnicode_Concat(v, w);
4334 Py_DECREF(v);
4335 return w;
4336 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004337}
4338
Guido van Rossum950361c1997-01-24 13:49:28 +00004339#ifdef DYNAMIC_EXECUTION_PROFILE
4340
Skip Montanarof118cb12001-10-15 20:51:38 +00004341static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004342getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004343{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004344 int i;
4345 PyObject *l = PyList_New(256);
4346 if (l == NULL) return NULL;
4347 for (i = 0; i < 256; i++) {
4348 PyObject *x = PyLong_FromLong(a[i]);
4349 if (x == NULL) {
4350 Py_DECREF(l);
4351 return NULL;
4352 }
4353 PyList_SetItem(l, i, x);
4354 }
4355 for (i = 0; i < 256; i++)
4356 a[i] = 0;
4357 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004358}
4359
4360PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004361_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004362{
4363#ifndef DXPAIRS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004364 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004365#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004366 int i;
4367 PyObject *l = PyList_New(257);
4368 if (l == NULL) return NULL;
4369 for (i = 0; i < 257; i++) {
4370 PyObject *x = getarray(dxpairs[i]);
4371 if (x == NULL) {
4372 Py_DECREF(l);
4373 return NULL;
4374 }
4375 PyList_SetItem(l, i, x);
4376 }
4377 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004378#endif
4379}
4380
4381#endif