blob: f3433f1d7dc3442bfac67754498571dddf3ebaf9 [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
David Malcolm8ad4cd92011-01-06 17:36:32 +000030/* PowerPC suppport.
31 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
32 "__powerpc__" appears to be the correct one for Linux with GCC
33*/
34#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
Michael W. Hudson75eabd22005-01-18 15:56:11 +000036#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000037
38static void
39ppc_getcounter(uint64 *v)
40{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000041 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000042
43 loop:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000044 asm volatile ("mftbu %0" : "=r" (tbu) );
45 asm volatile ("mftb %0" : "=r" (tb) );
46 asm volatile ("mftbu %0" : "=r" (tbu2));
47 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000048
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000049 /* The slightly peculiar way of writing the next lines is
50 compiled better by GCC than any other way I tried. */
51 ((long*)(v))[0] = tbu;
52 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000053}
54
Mark Dickinsonee2dd4a2009-10-31 10:20:38 +000055#elif defined(__i386__)
56
57/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
Michael W. Hudson75eabd22005-01-18 15:56:11 +000059#define READ_TIMESTAMP(val) \
60 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000061
Mark Dickinsonee2dd4a2009-10-31 10:20:38 +000062#elif defined(__x86_64__)
63
64/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
65 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
66 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
67 32-bit pieces of the result. */
68
69#define READ_TIMESTAMP(val) \
70 __asm__ __volatile__("rdtsc" : \
71 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
72
73
74#else
75
76#error "Don't know how to implement timestamp counter for this architecture"
77
Michael W. Hudson800ba232004-08-12 18:19:17 +000078#endif
79
Thomas Wouters477c8d52006-05-27 19:21:47 +000080void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000081 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000082{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000083 uint64 intr, inst, loop;
84 PyThreadState *tstate = PyThreadState_Get();
85 if (!tstate->interp->tscdump)
86 return;
87 intr = intr1 - intr0;
88 inst = inst1 - inst0 - intr;
89 loop = loop1 - loop0 - intr;
90 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krahc87901d2010-06-23 18:54:09 +000091 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092}
Michael W. Hudson800ba232004-08-12 18:19:17 +000093
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000094#endif
95
Guido van Rossum04691fc1992-08-12 15:35:34 +000096/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000097/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000098
Guido van Rossum408027e1996-12-30 16:17:54 +000099#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000100/* For debugging the interpreter: */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000101#define LLTRACE 1 /* Low-level trace feature */
102#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000103#endif
104
Jeremy Hylton52820442001-01-03 23:52:36 +0000105typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000106
Guido van Rossum374a9221991-04-04 10:40:29 +0000107/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000108#ifdef WITH_TSC
Thomas Wouters477c8d52006-05-27 19:21:47 +0000109static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000110#else
Thomas Wouters477c8d52006-05-27 19:21:47 +0000111static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000112#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +0000113static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
114static PyObject * do_call(PyObject *, PyObject ***, int, int);
115static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouters8ce81f72007-09-20 18:22:40 +0000116static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000117 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000118static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
119static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000120#define CALL_FLAG_VAR 1
121#define CALL_FLAG_KW 2
122
Guido van Rossum0a066c01992-03-27 17:29:15 +0000123#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +0000124static int lltrace;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000125static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000126#endif
Fred Drake5755ce62001-06-27 19:19:46 +0000127static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000128 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +0000129static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krahc87901d2010-06-23 18:54:09 +0000130 PyFrameObject *, int, PyObject *);
Fred Drake5755ce62001-06-27 19:19:46 +0000131static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +0000132static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krahc87901d2010-06-23 18:54:09 +0000133 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000134
Thomas Wouters477c8d52006-05-27 19:21:47 +0000135static PyObject * cmp_outcome(int, PyObject *, PyObject *);
136static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +0000137static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +0000138static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000139static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
Guido van Rossum374a9221991-04-04 10:40:29 +0000141
Paul Prescode68140d2000-08-30 20:25:01 +0000142#define NAME_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000143 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000144#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000145 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000146#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000147 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000148#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000149 "free variable '%.200s' referenced before assignment" \
150 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Guido van Rossum950361c1997-01-24 13:49:28 +0000152/* Dynamic execution profile */
153#ifdef DYNAMIC_EXECUTION_PROFILE
154#ifdef DXPAIRS
155static long dxpairs[257][256];
156#define dxp dxpairs[256]
157#else
158static long dxp[256];
159#endif
160#endif
161
Jeremy Hylton985eba52003-02-05 23:13:00 +0000162/* Function call profile */
163#ifdef CALL_PROFILE
164#define PCALL_NUM 11
165static int pcall[PCALL_NUM];
166
167#define PCALL_ALL 0
168#define PCALL_FUNCTION 1
169#define PCALL_FAST_FUNCTION 2
170#define PCALL_FASTER_FUNCTION 3
171#define PCALL_METHOD 4
172#define PCALL_BOUND_METHOD 5
173#define PCALL_CFUNCTION 6
174#define PCALL_TYPE 7
175#define PCALL_GENERATOR 8
176#define PCALL_OTHER 9
177#define PCALL_POP 10
178
179/* Notes about the statistics
180
181 PCALL_FAST stats
182
183 FAST_FUNCTION means no argument tuple needs to be created.
184 FASTER_FUNCTION means that the fast-path frame setup code is used.
185
186 If there is a method call where the call can be optimized by changing
187 the argument tuple and calling the function directly, it gets recorded
188 twice.
189
190 As a result, the relationship among the statistics appears to be
191 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
192 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
193 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
194 PCALL_METHOD > PCALL_BOUND_METHOD
195*/
196
197#define PCALL(POS) pcall[POS]++
198
199PyObject *
200PyEval_GetCallStats(PyObject *self)
201{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000202 return Py_BuildValue("iiiiiiiiiii",
203 pcall[0], pcall[1], pcall[2], pcall[3],
204 pcall[4], pcall[5], pcall[6], pcall[7],
205 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000206}
207#else
208#define PCALL(O)
209
210PyObject *
211PyEval_GetCallStats(PyObject *self)
212{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000213 Py_INCREF(Py_None);
214 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000215}
216#endif
217
Tim Peters5ca576e2001-06-18 22:08:13 +0000218
Guido van Rossume59214e1994-08-30 08:01:59 +0000219#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000220
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000221#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000222#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000223#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000224#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000225
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000226static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000227static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000228static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000229
Tim Peters7f468f22004-10-11 02:40:51 +0000230int
231PyEval_ThreadsInitialized(void)
232{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000233 return interpreter_lock != 0;
Tim Peters7f468f22004-10-11 02:40:51 +0000234}
235
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000236void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000237PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000238{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000239 if (interpreter_lock)
240 return;
241 interpreter_lock = PyThread_allocate_lock();
242 PyThread_acquire_lock(interpreter_lock, 1);
243 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000244}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000245
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000246void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000248{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000249 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000250}
251
252void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000253PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000254{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000255 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000256}
257
258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000260{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000261 if (tstate == NULL)
262 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
263 /* Check someone has called PyEval_InitThreads() to create the lock */
264 assert(interpreter_lock);
265 PyThread_acquire_lock(interpreter_lock, 1);
266 if (PyThreadState_Swap(tstate) != NULL)
267 Py_FatalError(
268 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000269}
270
271void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000272PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000273{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000274 if (tstate == NULL)
275 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
276 if (PyThreadState_Swap(NULL) != tstate)
277 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
278 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000279}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000280
281/* This function is called from PyOS_AfterFork to ensure that newly
282 created child processes don't hold locks referring to threads which
283 are not running in the child process. (This could also be done using
284 pthread_atfork mechanism, at least for the pthreads implementation.) */
285
286void
287PyEval_ReInitThreads(void)
288{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000289 PyObject *threading, *result;
290 PyThreadState *tstate;
Jesse Nollera8513972008-07-17 16:49:17 +0000291
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000292 if (!interpreter_lock)
293 return;
294 /*XXX Can't use PyThread_free_lock here because it does too
295 much error-checking. Doing this cleanly would require
296 adding a new function to each thread_*.h. Instead, just
297 create a new lock and waste a little bit of memory */
298 interpreter_lock = PyThread_allocate_lock();
299 pending_lock = PyThread_allocate_lock();
300 PyThread_acquire_lock(interpreter_lock, 1);
301 main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000302
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000303 /* Update the threading module with the new state.
304 */
305 tstate = PyThreadState_GET();
306 threading = PyMapping_GetItemString(tstate->interp->modules,
307 "threading");
308 if (threading == NULL) {
309 /* threading not imported */
310 PyErr_Clear();
311 return;
312 }
313 result = PyObject_CallMethod(threading, "_after_fork", NULL);
314 if (result == NULL)
315 PyErr_WriteUnraisable(threading);
316 else
317 Py_DECREF(result);
318 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000319}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000320#endif
321
Guido van Rossumff4949e1992-08-05 19:58:53 +0000322/* Functions save_thread and restore_thread are always defined so
323 dynamically loaded modules needn't be compiled separately for use
324 with and without threads: */
325
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000326PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000327PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000328{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000329 PyThreadState *tstate = PyThreadState_Swap(NULL);
330 if (tstate == NULL)
331 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000332#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000333 if (interpreter_lock)
334 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000335#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000336 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000337}
338
339void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000340PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000341{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000342 if (tstate == NULL)
343 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000344#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000345 if (interpreter_lock) {
346 int err = errno;
347 PyThread_acquire_lock(interpreter_lock, 1);
348 errno = err;
349 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000350#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000351 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352}
353
354
Guido van Rossuma9672091994-09-14 13:31:22 +0000355/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
356 signal handlers or Mac I/O completion routines) can schedule calls
357 to a function to be called synchronously.
358 The synchronous function is called with one void* argument.
359 It should return 0 for success or -1 for failure -- failure should
360 be accompanied by an exception.
361
362 If registry succeeds, the registry function returns 0; if it fails
363 (e.g. due to too many pending calls) it returns -1 (without setting
364 an exception condition).
365
366 Note that because registry may occur from within signal handlers,
367 or other asynchronous events, calling malloc() is unsafe!
368
369#ifdef WITH_THREAD
370 Any thread can schedule pending calls, but only the main thread
371 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000372 There is no facility to schedule calls to a particular thread, but
373 that should be easy to change, should that ever be required. In
374 that case, the static variables here should go into the python
375 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000376#endif
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000377*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000378
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000379#ifdef WITH_THREAD
380
381/* The WITH_THREAD implementation is thread-safe. It allows
382 scheduling to be made from any thread, and even from an executing
383 callback.
384 */
385
386#define NPENDINGCALLS 32
387static struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000388 int (*func)(void *);
389 void *arg;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000390} pendingcalls[NPENDINGCALLS];
391static int pendingfirst = 0;
392static int pendinglast = 0;
393static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
394static char pendingbusy = 0;
395
396int
397Py_AddPendingCall(int (*func)(void *), void *arg)
398{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000399 int i, j, result=0;
400 PyThread_type_lock lock = pending_lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000401
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000402 /* try a few times for the lock. Since this mechanism is used
403 * for signal handling (on the main thread), there is a (slim)
404 * chance that a signal is delivered on the same thread while we
405 * hold the lock during the Py_MakePendingCalls() function.
406 * This avoids a deadlock in that case.
407 * Note that signals can be delivered on any thread. In particular,
408 * on Windows, a SIGINT is delivered on a system-created worker
409 * thread.
410 * We also check for lock being NULL, in the unlikely case that
411 * this function is called before any bytecode evaluation takes place.
412 */
413 if (lock != NULL) {
414 for (i = 0; i<100; i++) {
415 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
416 break;
417 }
418 if (i == 100)
419 return -1;
420 }
421
422 i = pendinglast;
423 j = (i + 1) % NPENDINGCALLS;
424 if (j == pendingfirst) {
425 result = -1; /* Queue full */
426 } else {
427 pendingcalls[i].func = func;
428 pendingcalls[i].arg = arg;
429 pendinglast = j;
430 }
431 /* signal main loop */
432 _Py_Ticker = 0;
433 pendingcalls_to_do = 1;
434 if (lock != NULL)
435 PyThread_release_lock(lock);
436 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000437}
438
439int
440Py_MakePendingCalls(void)
441{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000442 int i;
443 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000444
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000445 if (!pending_lock) {
446 /* initial allocation of the lock */
447 pending_lock = PyThread_allocate_lock();
448 if (pending_lock == NULL)
449 return -1;
450 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000451
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000452 /* only service pending calls on main thread */
453 if (main_thread && PyThread_get_thread_ident() != main_thread)
454 return 0;
455 /* don't perform recursive pending calls */
456 if (pendingbusy)
457 return 0;
458 pendingbusy = 1;
459 /* perform a bounded number of calls, in case of recursion */
460 for (i=0; i<NPENDINGCALLS; i++) {
461 int j;
462 int (*func)(void *);
463 void *arg = NULL;
464
465 /* pop one item off the queue while holding the lock */
466 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
467 j = pendingfirst;
468 if (j == pendinglast) {
469 func = NULL; /* Queue empty */
470 } else {
471 func = pendingcalls[j].func;
472 arg = pendingcalls[j].arg;
473 pendingfirst = (j + 1) % NPENDINGCALLS;
474 }
475 pendingcalls_to_do = pendingfirst != pendinglast;
476 PyThread_release_lock(pending_lock);
477 /* having released the lock, perform the callback */
478 if (func == NULL)
479 break;
480 r = func(arg);
481 if (r)
482 break;
483 }
484 pendingbusy = 0;
485 return r;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000486}
487
488#else /* if ! defined WITH_THREAD */
489
490/*
491 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
492 This code is used for signal handling in python that isn't built
493 with WITH_THREAD.
494 Don't use this implementation when Py_AddPendingCalls() can happen
495 on a different thread!
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000496
Guido van Rossuma9672091994-09-14 13:31:22 +0000497 There are two possible race conditions:
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000498 (1) nested asynchronous calls to Py_AddPendingCall()
499 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000500
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000501 (1) is very unlikely because typically signal delivery
502 is blocked during signal handling. So it should be impossible.
503 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000504 The current code is safe against (2), but not against (1).
505 The safety against (2) is derived from the fact that only one
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000506 thread is present, interrupted by signals, and that the critical
507 section is protected with the "busy" variable. On Windows, which
508 delivers SIGINT on a system thread, this does not hold and therefore
509 Windows really shouldn't use this version.
510 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000511*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000512
Guido van Rossuma9672091994-09-14 13:31:22 +0000513#define NPENDINGCALLS 32
514static struct {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000515 int (*func)(void *);
516 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000517} pendingcalls[NPENDINGCALLS];
518static volatile int pendingfirst = 0;
519static volatile int pendinglast = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000520static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000521
522int
Thomas Wouters334fb892000-07-25 12:56:38 +0000523Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000524{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000525 static volatile int busy = 0;
526 int i, j;
527 /* XXX Begin critical section */
528 if (busy)
529 return -1;
530 busy = 1;
531 i = pendinglast;
532 j = (i + 1) % NPENDINGCALLS;
533 if (j == pendingfirst) {
534 busy = 0;
535 return -1; /* Queue full */
536 }
537 pendingcalls[i].func = func;
538 pendingcalls[i].arg = arg;
539 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000540
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000541 _Py_Ticker = 0;
542 pendingcalls_to_do = 1; /* Signal main loop */
543 busy = 0;
544 /* XXX End critical section */
545 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000546}
547
Guido van Rossum180d7b41994-09-29 09:45:57 +0000548int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000549Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000550{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000551 static int busy = 0;
552 if (busy)
553 return 0;
554 busy = 1;
555 pendingcalls_to_do = 0;
556 for (;;) {
557 int i;
558 int (*func)(void *);
559 void *arg;
560 i = pendingfirst;
561 if (i == pendinglast)
562 break; /* Queue empty */
563 func = pendingcalls[i].func;
564 arg = pendingcalls[i].arg;
565 pendingfirst = (i + 1) % NPENDINGCALLS;
566 if (func(arg) < 0) {
567 busy = 0;
568 pendingcalls_to_do = 1; /* We're not done yet */
569 return -1;
570 }
571 }
572 busy = 0;
573 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000574}
575
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000576#endif /* WITH_THREAD */
577
Guido van Rossuma9672091994-09-14 13:31:22 +0000578
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000579/* The interpreter's recursion limit */
580
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000581#ifndef Py_DEFAULT_RECURSION_LIMIT
582#define Py_DEFAULT_RECURSION_LIMIT 1000
583#endif
584static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
585int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000586
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000587int
588Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000589{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000590 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000591}
592
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000593void
594Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000595{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000596 recursion_limit = new_limit;
597 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598}
599
Armin Rigo2b3eb402003-10-28 12:05:48 +0000600/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
601 if the recursion_depth reaches _Py_CheckRecursionLimit.
602 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
603 to guarantee that _Py_CheckRecursiveCall() is regularly called.
604 Without USE_STACKCHECK, there is no need for this. */
605int
606_Py_CheckRecursiveCall(char *where)
607{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000608 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000609
610#ifdef USE_STACKCHECK
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000611 if (PyOS_CheckStack()) {
612 --tstate->recursion_depth;
613 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
614 return -1;
615 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000616#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000617 _Py_CheckRecursionLimit = recursion_limit;
618 if (tstate->recursion_critical)
619 /* Somebody asked that we don't check for recursion. */
620 return 0;
621 if (tstate->overflowed) {
622 if (tstate->recursion_depth > recursion_limit + 50) {
623 /* Overflowing while handling an overflow. Give up. */
624 Py_FatalError("Cannot recover from stack overflow.");
625 }
626 return 0;
627 }
628 if (tstate->recursion_depth > recursion_limit) {
629 --tstate->recursion_depth;
630 tstate->overflowed = 1;
631 PyErr_Format(PyExc_RuntimeError,
632 "maximum recursion depth exceeded%s",
633 where);
634 return -1;
635 }
636 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000637}
638
Guido van Rossum374a9221991-04-04 10:40:29 +0000639/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000640enum why_code {
Stefan Krahc87901d2010-06-23 18:54:09 +0000641 WHY_NOT = 0x0001, /* No error */
642 WHY_EXCEPTION = 0x0002, /* Exception occurred */
643 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
644 WHY_RETURN = 0x0008, /* 'return' statement */
645 WHY_BREAK = 0x0010, /* 'break' statement */
646 WHY_CONTINUE = 0x0020, /* 'continue' statement */
647 WHY_YIELD = 0x0040, /* 'yield' operator */
648 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000649};
Guido van Rossum374a9221991-04-04 10:40:29 +0000650
Collin Winter828f04a2007-08-31 00:04:24 +0000651static enum why_code do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000652static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000653
Jeffrey Yasskin008d8ef2008-12-06 17:09:27 +0000654/* Records whether tracing is on for any thread. Counts the number of
655 threads for which tstate->c_tracefunc is non-NULL, so if the value
656 is 0, we know we don't have to check this thread's c_tracefunc.
657 This speeds up the if statement in PyEval_EvalFrameEx() after
658 fast_next_opcode*/
659static int _Py_TracingPossible = 0;
660
Skip Montanarod581d772002-09-03 20:10:45 +0000661/* for manipulating the thread switch and periodic "stuff" - used to be
662 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000663int _Py_CheckInterval = 100;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000664volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000665
Guido van Rossumb209a111997-04-29 18:18:01 +0000666PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000667PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000669 return PyEval_EvalCodeEx(co,
670 globals, locals,
671 (PyObject **)NULL, 0,
672 (PyObject **)NULL, 0,
673 (PyObject **)NULL, 0,
674 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000675}
676
677
678/* Interpreter main loop */
679
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000680PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000681PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000682 /* This is for backward compatibility with extension modules that
683 used this API; core interpreter code should call
684 PyEval_EvalFrameEx() */
685 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000686}
687
688PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000689PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000690{
Guido van Rossum950361c1997-01-24 13:49:28 +0000691#ifdef DXPAIRS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000692 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000693#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000694 register PyObject **stack_pointer; /* Next free slot in value stack */
695 register unsigned char *next_instr;
696 register int opcode; /* Current opcode */
697 register int oparg; /* Current opcode argument, if any */
698 register enum why_code why; /* Reason for block stack unwind */
699 register int err; /* Error status -- nonzero if error */
700 register PyObject *x; /* Result object -- NULL if error */
701 register PyObject *v; /* Temporary objects popped off stack */
702 register PyObject *w;
703 register PyObject *u;
704 register PyObject *t;
705 register PyObject **fastlocals, **freevars;
706 PyObject *retval = NULL; /* Return value */
707 PyThreadState *tstate = PyThreadState_GET();
708 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000709
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000710 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000711
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000712 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000713
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000714 is true when the line being executed has changed. The
715 initial values are such as to make this false the first
716 time it is tested. */
717 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000718
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000719 unsigned char *first_instr;
720 PyObject *names;
721 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000722#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000723 /* Make it easier to find out where we are with a debugger */
724 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000725#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000726
Antoine Pitroub52ec782009-01-25 16:34:23 +0000727/* Computed GOTOs, or
728 the-optimization-commonly-but-improperly-known-as-"threaded code"
729 using gcc's labels-as-values extension
730 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
731
732 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000733 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000734 combined with a lookup table of jump addresses. However, since the
735 indirect jump instruction is shared by all opcodes, the CPU will have a
736 hard time making the right prediction for where to jump next (actually,
737 it will be always wrong except in the uncommon case of a sequence of
738 several identical opcodes).
739
740 "Threaded code" in contrast, uses an explicit jump table and an explicit
741 indirect jump instruction at the end of each opcode. Since the jump
742 instruction is at a different address for each opcode, the CPU will make a
743 separate prediction for each of these instructions, which is equivalent to
744 predicting the second opcode of each opcode pair. These predictions have
745 a much better chance to turn out valid, especially in small bytecode loops.
746
747 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000748 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000749 and potentially many more instructions (depending on the pipeline width).
750 A correctly predicted branch, however, is nearly free.
751
752 At the time of this writing, the "threaded code" version is up to 15-20%
753 faster than the normal "switch" version, depending on the compiler and the
754 CPU architecture.
755
756 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
757 because it would render the measurements invalid.
758
759
760 NOTE: care must be taken that the compiler doesn't try to "optimize" the
761 indirect jumps by sharing them between all opcodes. Such optimizations
762 can be disabled on gcc by using the -fno-gcse flag (or possibly
763 -fno-crossjumping).
764*/
765
766#if defined(USE_COMPUTED_GOTOS) && defined(DYNAMIC_EXECUTION_PROFILE)
767#undef USE_COMPUTED_GOTOS
768#endif
769
770#ifdef USE_COMPUTED_GOTOS
771/* Import the static jump table */
772#include "opcode_targets.h"
773
774/* This macro is used when several opcodes defer to the same implementation
775 (e.g. SETUP_LOOP, SETUP_FINALLY) */
776#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000777 TARGET_##op: \
778 opcode = op; \
779 if (HAS_ARG(op)) \
780 oparg = NEXTARG(); \
781 case op: \
782 goto impl; \
Antoine Pitroub52ec782009-01-25 16:34:23 +0000783
784#define TARGET(op) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000785 TARGET_##op: \
786 opcode = op; \
787 if (HAS_ARG(op)) \
788 oparg = NEXTARG(); \
789 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000790
791
792#define DISPATCH() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000793 { \
794 /* Avoid multiple loads from _Py_Ticker despite `volatile` */ \
795 int _tick = _Py_Ticker - 1; \
796 _Py_Ticker = _tick; \
797 if (_tick >= 0) { \
798 FAST_DISPATCH(); \
799 } \
800 continue; \
801 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000802
803#ifdef LLTRACE
804#define FAST_DISPATCH() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000805 { \
806 if (!lltrace && !_Py_TracingPossible) { \
807 f->f_lasti = INSTR_OFFSET(); \
808 goto *opcode_targets[*next_instr++]; \
809 } \
810 goto fast_next_opcode; \
811 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000812#else
813#define FAST_DISPATCH() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000814 { \
815 if (!_Py_TracingPossible) { \
816 f->f_lasti = INSTR_OFFSET(); \
817 goto *opcode_targets[*next_instr++]; \
818 } \
819 goto fast_next_opcode; \
820 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000821#endif
822
823#else
824#define TARGET(op) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000825 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000826#define TARGET_WITH_IMPL(op, impl) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000827 /* silence compiler warnings about `impl` unused */ \
828 if (0) goto impl; \
829 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000830#define DISPATCH() continue
831#define FAST_DISPATCH() goto fast_next_opcode
832#endif
833
834
Neal Norwitza81d2202002-07-14 00:27:26 +0000835/* Tuple access macros */
836
837#ifndef Py_DEBUG
838#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
839#else
840#define GETITEM(v, i) PyTuple_GetItem((v), (i))
841#endif
842
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000843#ifdef WITH_TSC
844/* Use Pentium timestamp counter to mark certain events:
845 inst0 -- beginning of switch statement for opcode dispatch
846 inst1 -- end of switch statement (may be skipped)
847 loop0 -- the top of the mainloop
Thomas Wouters477c8d52006-05-27 19:21:47 +0000848 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000849 (may be skipped)
850 intr1 -- beginning of long interruption
851 intr2 -- end of long interruption
852
853 Many opcodes call out to helper C functions. In some cases, the
854 time in those functions should be counted towards the time for the
855 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
856 calls another Python function; there's no point in charge all the
857 bytecode executed by the called function to the caller.
858
859 It's hard to make a useful judgement statically. In the presence
860 of operator overloading, it's impossible to tell if a call will
861 execute new Python code or not.
862
863 It's a case-by-case judgement. I'll use intr1 for the following
864 cases:
865
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000866 IMPORT_STAR
867 IMPORT_FROM
868 CALL_FUNCTION (and friends)
869
870 */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000871 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
872 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000873
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000874 READ_TIMESTAMP(inst0);
875 READ_TIMESTAMP(inst1);
876 READ_TIMESTAMP(loop0);
877 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000878
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000879 /* shut up the compiler */
880 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000881#endif
882
Guido van Rossum374a9221991-04-04 10:40:29 +0000883/* Code access macros */
884
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000885#define INSTR_OFFSET() ((int)(next_instr - first_instr))
886#define NEXTOP() (*next_instr++)
887#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
888#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
889#define JUMPTO(x) (next_instr = first_instr + (x))
890#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000891
Raymond Hettingerf606f872003-03-16 03:11:04 +0000892/* OpCode prediction macros
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000893 Some opcodes tend to come in pairs thus making it possible to
894 predict the second code when the first is run. For example,
895 COMPARE_OP is often followed by JUMP_IF_FALSE or JUMP_IF_TRUE. And,
896 those opcodes are often followed by a POP_TOP.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000897
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000898 Verifying the prediction costs a single high-speed test of a register
899 variable against a constant. If the pairing was good, then the
900 processor's own internal branch predication has a high likelihood of
901 success, resulting in a nearly zero-overhead transition to the
902 next opcode. A successful prediction saves a trip through the eval-loop
903 including its two unpredictable branches, the HAS_ARG test and the
904 switch-case. Combined with the processor's internal branch prediction,
905 a successful PREDICT has the effect of making the two opcodes run as if
906 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000907
Georg Brandl86b2fb92008-07-16 03:43:04 +0000908 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000909 predictions turned-on and interpret the results as if some opcodes
910 had been combined or turn-off predictions so that the opcode frequency
911 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000912
913 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000914 the CPU to record separate branch prediction information for each
915 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000916
Raymond Hettingerf606f872003-03-16 03:11:04 +0000917*/
918
Antoine Pitroub52ec782009-01-25 16:34:23 +0000919#if defined(DYNAMIC_EXECUTION_PROFILE) || defined(USE_COMPUTED_GOTOS)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000920#define PREDICT(op) if (0) goto PRED_##op
921#define PREDICTED(op) PRED_##op:
922#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000923#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000924#define PREDICT(op) if (*next_instr == op) goto PRED_##op
925#define PREDICTED(op) PRED_##op: next_instr++
926#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Antoine Pitroub52ec782009-01-25 16:34:23 +0000927#endif
928
Raymond Hettingerf606f872003-03-16 03:11:04 +0000929
Guido van Rossum374a9221991-04-04 10:40:29 +0000930/* Stack manipulation macros */
931
Martin v. Löwis18e16552006-02-15 17:27:45 +0000932/* The stack can grow at most MAXINT deep, as co_nlocals and
933 co_stacksize are ints. */
Stefan Krahc87901d2010-06-23 18:54:09 +0000934#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
935#define EMPTY() (STACK_LEVEL() == 0)
936#define TOP() (stack_pointer[-1])
937#define SECOND() (stack_pointer[-2])
938#define THIRD() (stack_pointer[-3])
939#define FOURTH() (stack_pointer[-4])
940#define SET_TOP(v) (stack_pointer[-1] = (v))
941#define SET_SECOND(v) (stack_pointer[-2] = (v))
942#define SET_THIRD(v) (stack_pointer[-3] = (v))
943#define SET_FOURTH(v) (stack_pointer[-4] = (v))
944#define BASIC_STACKADJ(n) (stack_pointer += n)
945#define BASIC_PUSH(v) (*stack_pointer++ = (v))
946#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000947
Guido van Rossum96a42c81992-01-12 02:29:51 +0000948#ifdef LLTRACE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000949#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahc87901d2010-06-23 18:54:09 +0000950 lltrace && prtrace(TOP(), "push")); \
951 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000952#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahc87901d2010-06-23 18:54:09 +0000953 BASIC_POP())
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000954#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahc87901d2010-06-23 18:54:09 +0000955 lltrace && prtrace(TOP(), "stackadj")); \
956 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000957#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahc87901d2010-06-23 18:54:09 +0000958 prtrace((STACK_POINTER)[-1], "ext_pop")), \
959 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000960#else
Stefan Krahc87901d2010-06-23 18:54:09 +0000961#define PUSH(v) BASIC_PUSH(v)
962#define POP() BASIC_POP()
963#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000964#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000965#endif
966
Guido van Rossum681d79a1995-07-18 14:51:37 +0000967/* Local variable macros */
968
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000969#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000970
971/* The SETLOCAL() macro must not DECREF the local variable in-place and
972 then store the new value; it must copy the old value to a temporary
973 value, then store the new value, and then DECREF the temporary value.
974 This is because it is possible that during the DECREF the frame is
975 accessed by other code (e.g. a __del__ method or gc.collect()) and the
976 variable would be pointing to already-freed memory. */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000977#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahc87901d2010-06-23 18:54:09 +0000978 GETLOCAL(i) = value; \
979 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000980
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000981
982#define UNWIND_BLOCK(b) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000983 while (STACK_LEVEL() > (b)->b_level) { \
984 PyObject *v = POP(); \
985 Py_XDECREF(v); \
986 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000987
988#define UNWIND_EXCEPT_HANDLER(b) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000989 { \
990 PyObject *type, *value, *traceback; \
991 assert(STACK_LEVEL() >= (b)->b_level + 3); \
992 while (STACK_LEVEL() > (b)->b_level + 3) { \
993 value = POP(); \
994 Py_XDECREF(value); \
995 } \
996 type = tstate->exc_type; \
997 value = tstate->exc_value; \
998 traceback = tstate->exc_traceback; \
999 tstate->exc_type = POP(); \
1000 tstate->exc_value = POP(); \
1001 tstate->exc_traceback = POP(); \
1002 Py_XDECREF(type); \
1003 Py_XDECREF(value); \
1004 Py_XDECREF(traceback); \
1005 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001006
1007#define SAVE_EXC_STATE() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001008 { \
1009 PyObject *type, *value, *traceback; \
1010 Py_XINCREF(tstate->exc_type); \
1011 Py_XINCREF(tstate->exc_value); \
1012 Py_XINCREF(tstate->exc_traceback); \
1013 type = f->f_exc_type; \
1014 value = f->f_exc_value; \
1015 traceback = f->f_exc_traceback; \
1016 f->f_exc_type = tstate->exc_type; \
1017 f->f_exc_value = tstate->exc_value; \
1018 f->f_exc_traceback = tstate->exc_traceback; \
1019 Py_XDECREF(type); \
1020 Py_XDECREF(value); \
1021 Py_XDECREF(traceback); \
1022 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001023
1024#define SWAP_EXC_STATE() \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001025 { \
1026 PyObject *tmp; \
1027 tmp = tstate->exc_type; \
1028 tstate->exc_type = f->f_exc_type; \
1029 f->f_exc_type = tmp; \
1030 tmp = tstate->exc_value; \
1031 tstate->exc_value = f->f_exc_value; \
1032 f->f_exc_value = tmp; \
1033 tmp = tstate->exc_traceback; \
1034 tstate->exc_traceback = f->f_exc_traceback; \
1035 f->f_exc_traceback = tmp; \
1036 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001037
Guido van Rossuma027efa1997-05-05 20:56:21 +00001038/* Start of code */
1039
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001040 if (f == NULL)
1041 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001042
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001043 /* push frame */
1044 if (Py_EnterRecursiveCall(""))
1045 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +00001046
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001047 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001048
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001049 if (tstate->use_tracing) {
1050 if (tstate->c_tracefunc != NULL) {
1051 /* tstate->c_tracefunc, if defined, is a
1052 function that will be called on *every* entry
1053 to a code block. Its return value, if not
1054 None, is a function that will be called at
1055 the start of each executed line of code.
1056 (Actually, the function must return itself
1057 in order to continue tracing.) The trace
1058 functions are called with three arguments:
1059 a pointer to the current frame, a string
1060 indicating why the function is called, and
1061 an argument which depends on the situation.
1062 The global trace function is also called
1063 whenever an exception is detected. */
1064 if (call_trace_protected(tstate->c_tracefunc,
1065 tstate->c_traceobj,
1066 f, PyTrace_CALL, Py_None)) {
1067 /* Trace function raised an error */
1068 goto exit_eval_frame;
1069 }
1070 }
1071 if (tstate->c_profilefunc != NULL) {
1072 /* Similar for c_profilefunc, except it needn't
1073 return itself and isn't called for "line" events */
1074 if (call_trace_protected(tstate->c_profilefunc,
1075 tstate->c_profileobj,
1076 f, PyTrace_CALL, Py_None)) {
1077 /* Profile function raised an error */
1078 goto exit_eval_frame;
1079 }
1080 }
1081 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001082
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001083 co = f->f_code;
1084 names = co->co_names;
1085 consts = co->co_consts;
1086 fastlocals = f->f_localsplus;
1087 freevars = f->f_localsplus + co->co_nlocals;
1088 first_instr = (unsigned char*) PyBytes_AS_STRING(co->co_code);
1089 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001090
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001091 f->f_lasti now refers to the index of the last instruction
1092 executed. You might think this was obvious from the name, but
1093 this wasn't always true before 2.3! PyFrame_New now sets
1094 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1095 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1096 does work. Promise.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001097
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001098 When the PREDICT() macros are enabled, some opcode pairs follow in
1099 direct succession without updating f->f_lasti. A successful
1100 prediction effectively links the two codes together as if they
1101 were a single new opcode; accordingly,f->f_lasti will point to
1102 the first code in the pair (for instance, GET_ITER followed by
1103 FOR_ITER is effectively a single opcode and f->f_lasti will point
1104 at to the beginning of the combined pair.)
1105 */
1106 next_instr = first_instr + f->f_lasti + 1;
1107 stack_pointer = f->f_stacktop;
1108 assert(stack_pointer != NULL);
1109 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001110
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001111 if (co->co_flags & CO_GENERATOR && !throwflag) {
1112 if (f->f_exc_type != NULL && f->f_exc_type != Py_None) {
1113 /* We were in an except handler when we left,
1114 restore the exception state which was put aside
1115 (see YIELD_VALUE). */
1116 SWAP_EXC_STATE();
1117 }
1118 else {
1119 SAVE_EXC_STATE();
1120 }
1121 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001122
Tim Peters5ca576e2001-06-18 22:08:13 +00001123#ifdef LLTRACE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001124 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001125#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001126#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001127 filename = _PyUnicode_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001128#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001129
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001130 why = WHY_NOT;
1131 err = 0;
1132 x = Py_None; /* Not a reference, just anything non-NULL */
1133 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001134
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001135 if (throwflag) { /* support for generator.throw() */
1136 why = WHY_EXCEPTION;
1137 goto on_error;
1138 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001139
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001140 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001141#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001142 if (inst1 == 0) {
1143 /* Almost surely, the opcode executed a break
1144 or a continue, preventing inst1 from being set
1145 on the way out of the loop.
1146 */
1147 READ_TIMESTAMP(inst1);
1148 loop1 = inst1;
1149 }
1150 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1151 intr0, intr1);
1152 ticked = 0;
1153 inst1 = 0;
1154 intr0 = 0;
1155 intr1 = 0;
1156 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001157#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001158 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1159 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001160
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001161 /* Do periodic things. Doing this every time through
1162 the loop would add too much overhead, so we do it
1163 only every Nth instruction. We also do it if
1164 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1165 event needs attention (e.g. a signal handler or
1166 async I/O handler); see Py_AddPendingCall() and
1167 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001169 if (--_Py_Ticker < 0) {
1170 if (*next_instr == SETUP_FINALLY) {
1171 /* Make the last opcode before
1172 a try: finally: block uninterruptable. */
1173 goto fast_next_opcode;
1174 }
1175 _Py_Ticker = _Py_CheckInterval;
1176 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001177#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001178 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001179#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001180 if (pendingcalls_to_do) {
1181 if (Py_MakePendingCalls() < 0) {
1182 why = WHY_EXCEPTION;
1183 goto on_error;
1184 }
1185 if (pendingcalls_to_do)
1186 /* MakePendingCalls() didn't succeed.
1187 Force early re-execution of this
1188 "periodic" code, possibly after
1189 a thread switch */
1190 _Py_Ticker = 0;
1191 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001192#ifdef WITH_THREAD
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001193 if (interpreter_lock) {
1194 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001195
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001196 if (PyThreadState_Swap(NULL) != tstate)
1197 Py_FatalError("ceval: tstate mix-up");
1198 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001199
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001200 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001201
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001202 PyThread_acquire_lock(interpreter_lock, 1);
1203 if (PyThreadState_Swap(tstate) != NULL)
1204 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001205
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001206 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001207
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001208 if (tstate->async_exc != NULL) {
1209 x = tstate->async_exc;
1210 tstate->async_exc = NULL;
1211 PyErr_SetNone(x);
1212 Py_DECREF(x);
1213 why = WHY_EXCEPTION;
1214 goto on_error;
1215 }
1216 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001217#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001218 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001219
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001220 fast_next_opcode:
1221 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001222
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001223 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001224
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001225 if (_Py_TracingPossible &&
1226 tstate->c_tracefunc != NULL && !tstate->tracing) {
1227 /* see maybe_call_line_trace
1228 for expository comments */
1229 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001230
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001231 err = maybe_call_line_trace(tstate->c_tracefunc,
1232 tstate->c_traceobj,
1233 f, &instr_lb, &instr_ub,
1234 &instr_prev);
1235 /* Reload possibly changed frame fields */
1236 JUMPTO(f->f_lasti);
1237 if (f->f_stacktop != NULL) {
1238 stack_pointer = f->f_stacktop;
1239 f->f_stacktop = NULL;
1240 }
1241 if (err) {
1242 /* trace function raised an exception */
1243 goto on_error;
1244 }
1245 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001246
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001247 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001248
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001249 opcode = NEXTOP();
1250 oparg = 0; /* allows oparg to be stored in a register because
1251 it doesn't have to be remembered across a full loop */
1252 if (HAS_ARG(opcode))
1253 oparg = NEXTARG();
Stefan Krahc87901d2010-06-23 18:54:09 +00001254 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001255#ifdef DYNAMIC_EXECUTION_PROFILE
1256#ifdef DXPAIRS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001257 dxpairs[lastopcode][opcode]++;
1258 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001259#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001260 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001261#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001262
Guido van Rossum96a42c81992-01-12 02:29:51 +00001263#ifdef LLTRACE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001264 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001265
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001266 if (lltrace) {
1267 if (HAS_ARG(opcode)) {
1268 printf("%d: %d, %d\n",
1269 f->f_lasti, opcode, oparg);
1270 }
1271 else {
1272 printf("%d: %d\n",
1273 f->f_lasti, opcode);
1274 }
1275 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001276#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001277
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001278 /* Main switch on opcode */
1279 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001280
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001281 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001282
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001283 /* BEWARE!
1284 It is essential that any operation that fails sets either
1285 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1286 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001287
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001288 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001289
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001290 TARGET(NOP)
1291 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001292
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001293 TARGET(LOAD_FAST)
1294 x = GETLOCAL(oparg);
1295 if (x != NULL) {
1296 Py_INCREF(x);
1297 PUSH(x);
1298 FAST_DISPATCH();
1299 }
1300 format_exc_check_arg(PyExc_UnboundLocalError,
1301 UNBOUNDLOCAL_ERROR_MSG,
1302 PyTuple_GetItem(co->co_varnames, oparg));
1303 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001304
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001305 TARGET(LOAD_CONST)
1306 x = GETITEM(consts, oparg);
1307 Py_INCREF(x);
1308 PUSH(x);
1309 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001310
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001311 PREDICTED_WITH_ARG(STORE_FAST);
1312 TARGET(STORE_FAST)
1313 v = POP();
1314 SETLOCAL(oparg, v);
1315 FAST_DISPATCH();
Neil Schemenauer63543862002-02-17 19:10:14 +00001316
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001317 TARGET(POP_TOP)
1318 v = POP();
1319 Py_DECREF(v);
1320 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001321
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001322 TARGET(ROT_TWO)
1323 v = TOP();
1324 w = SECOND();
1325 SET_TOP(w);
1326 SET_SECOND(v);
1327 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001328
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001329 TARGET(ROT_THREE)
1330 v = TOP();
1331 w = SECOND();
1332 x = THIRD();
1333 SET_TOP(w);
1334 SET_SECOND(x);
1335 SET_THIRD(v);
1336 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001337
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001338 TARGET(ROT_FOUR)
1339 u = TOP();
1340 v = SECOND();
1341 w = THIRD();
1342 x = FOURTH();
1343 SET_TOP(v);
1344 SET_SECOND(w);
1345 SET_THIRD(x);
1346 SET_FOURTH(u);
1347 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001349 TARGET(DUP_TOP)
1350 v = TOP();
1351 Py_INCREF(v);
1352 PUSH(v);
1353 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001354
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001355 TARGET(DUP_TOPX)
1356 if (oparg == 2) {
1357 x = TOP();
1358 Py_INCREF(x);
1359 w = SECOND();
1360 Py_INCREF(w);
1361 STACKADJ(2);
1362 SET_TOP(x);
1363 SET_SECOND(w);
1364 FAST_DISPATCH();
1365 } else if (oparg == 3) {
1366 x = TOP();
1367 Py_INCREF(x);
1368 w = SECOND();
1369 Py_INCREF(w);
1370 v = THIRD();
1371 Py_INCREF(v);
1372 STACKADJ(3);
1373 SET_TOP(x);
1374 SET_SECOND(w);
1375 SET_THIRD(v);
1376 FAST_DISPATCH();
1377 }
1378 Py_FatalError("invalid argument to DUP_TOPX"
1379 " (bytecode corruption?)");
1380 /* Never returns, so don't bother to set why. */
1381 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001382
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001383 TARGET(UNARY_POSITIVE)
1384 v = TOP();
1385 x = PyNumber_Positive(v);
1386 Py_DECREF(v);
1387 SET_TOP(x);
1388 if (x != NULL) DISPATCH();
1389 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001391 TARGET(UNARY_NEGATIVE)
1392 v = TOP();
1393 x = PyNumber_Negative(v);
1394 Py_DECREF(v);
1395 SET_TOP(x);
1396 if (x != NULL) DISPATCH();
1397 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001399 TARGET(UNARY_NOT)
1400 v = TOP();
1401 err = PyObject_IsTrue(v);
1402 Py_DECREF(v);
1403 if (err == 0) {
1404 Py_INCREF(Py_True);
1405 SET_TOP(Py_True);
1406 DISPATCH();
1407 }
1408 else if (err > 0) {
1409 Py_INCREF(Py_False);
1410 SET_TOP(Py_False);
1411 err = 0;
1412 DISPATCH();
1413 }
1414 STACKADJ(-1);
1415 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001416
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001417 TARGET(UNARY_INVERT)
1418 v = TOP();
1419 x = PyNumber_Invert(v);
1420 Py_DECREF(v);
1421 SET_TOP(x);
1422 if (x != NULL) DISPATCH();
1423 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001424
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001425 TARGET(BINARY_POWER)
1426 w = POP();
1427 v = TOP();
1428 x = PyNumber_Power(v, w, Py_None);
1429 Py_DECREF(v);
1430 Py_DECREF(w);
1431 SET_TOP(x);
1432 if (x != NULL) DISPATCH();
1433 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001435 TARGET(BINARY_MULTIPLY)
1436 w = POP();
1437 v = TOP();
1438 x = PyNumber_Multiply(v, w);
1439 Py_DECREF(v);
1440 Py_DECREF(w);
1441 SET_TOP(x);
1442 if (x != NULL) DISPATCH();
1443 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001444
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001445 TARGET(BINARY_TRUE_DIVIDE)
1446 w = POP();
1447 v = TOP();
1448 x = PyNumber_TrueDivide(v, w);
1449 Py_DECREF(v);
1450 Py_DECREF(w);
1451 SET_TOP(x);
1452 if (x != NULL) DISPATCH();
1453 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001454
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001455 TARGET(BINARY_FLOOR_DIVIDE)
1456 w = POP();
1457 v = TOP();
1458 x = PyNumber_FloorDivide(v, w);
1459 Py_DECREF(v);
1460 Py_DECREF(w);
1461 SET_TOP(x);
1462 if (x != NULL) DISPATCH();
1463 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001464
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001465 TARGET(BINARY_MODULO)
1466 w = POP();
1467 v = TOP();
1468 if (PyUnicode_CheckExact(v))
1469 x = PyUnicode_Format(v, w);
1470 else
1471 x = PyNumber_Remainder(v, w);
1472 Py_DECREF(v);
1473 Py_DECREF(w);
1474 SET_TOP(x);
1475 if (x != NULL) DISPATCH();
1476 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001477
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001478 TARGET(BINARY_ADD)
1479 w = POP();
1480 v = TOP();
1481 if (PyUnicode_CheckExact(v) &&
1482 PyUnicode_CheckExact(w)) {
1483 x = unicode_concatenate(v, w, f, next_instr);
1484 /* unicode_concatenate consumed the ref to v */
1485 goto skip_decref_vx;
1486 }
1487 else {
1488 x = PyNumber_Add(v, w);
1489 }
1490 Py_DECREF(v);
1491 skip_decref_vx:
1492 Py_DECREF(w);
1493 SET_TOP(x);
1494 if (x != NULL) DISPATCH();
1495 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001496
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001497 TARGET(BINARY_SUBTRACT)
1498 w = POP();
1499 v = TOP();
1500 x = PyNumber_Subtract(v, w);
1501 Py_DECREF(v);
1502 Py_DECREF(w);
1503 SET_TOP(x);
1504 if (x != NULL) DISPATCH();
1505 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001506
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001507 TARGET(BINARY_SUBSCR)
1508 w = POP();
1509 v = TOP();
1510 x = PyObject_GetItem(v, w);
1511 Py_DECREF(v);
1512 Py_DECREF(w);
1513 SET_TOP(x);
1514 if (x != NULL) DISPATCH();
1515 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001516
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001517 TARGET(BINARY_LSHIFT)
1518 w = POP();
1519 v = TOP();
1520 x = PyNumber_Lshift(v, w);
1521 Py_DECREF(v);
1522 Py_DECREF(w);
1523 SET_TOP(x);
1524 if (x != NULL) DISPATCH();
1525 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001527 TARGET(BINARY_RSHIFT)
1528 w = POP();
1529 v = TOP();
1530 x = PyNumber_Rshift(v, w);
1531 Py_DECREF(v);
1532 Py_DECREF(w);
1533 SET_TOP(x);
1534 if (x != NULL) DISPATCH();
1535 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001536
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001537 TARGET(BINARY_AND)
1538 w = POP();
1539 v = TOP();
1540 x = PyNumber_And(v, w);
1541 Py_DECREF(v);
1542 Py_DECREF(w);
1543 SET_TOP(x);
1544 if (x != NULL) DISPATCH();
1545 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001547 TARGET(BINARY_XOR)
1548 w = POP();
1549 v = TOP();
1550 x = PyNumber_Xor(v, w);
1551 Py_DECREF(v);
1552 Py_DECREF(w);
1553 SET_TOP(x);
1554 if (x != NULL) DISPATCH();
1555 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001556
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001557 TARGET(BINARY_OR)
1558 w = POP();
1559 v = TOP();
1560 x = PyNumber_Or(v, w);
1561 Py_DECREF(v);
1562 Py_DECREF(w);
1563 SET_TOP(x);
1564 if (x != NULL) DISPATCH();
1565 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001566
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001567 TARGET(LIST_APPEND)
1568 w = POP();
1569 v = stack_pointer[-oparg];
1570 err = PyList_Append(v, w);
1571 Py_DECREF(w);
1572 if (err == 0) {
1573 PREDICT(JUMP_ABSOLUTE);
1574 DISPATCH();
1575 }
1576 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001577
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001578 TARGET(SET_ADD)
1579 w = POP();
1580 v = stack_pointer[-oparg];
1581 err = PySet_Add(v, w);
1582 Py_DECREF(w);
1583 if (err == 0) {
1584 PREDICT(JUMP_ABSOLUTE);
1585 DISPATCH();
1586 }
1587 break;
Nick Coghlan650f0d02007-04-15 12:05:43 +00001588
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001589 TARGET(INPLACE_POWER)
1590 w = POP();
1591 v = TOP();
1592 x = PyNumber_InPlacePower(v, w, Py_None);
1593 Py_DECREF(v);
1594 Py_DECREF(w);
1595 SET_TOP(x);
1596 if (x != NULL) DISPATCH();
1597 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001599 TARGET(INPLACE_MULTIPLY)
1600 w = POP();
1601 v = TOP();
1602 x = PyNumber_InPlaceMultiply(v, w);
1603 Py_DECREF(v);
1604 Py_DECREF(w);
1605 SET_TOP(x);
1606 if (x != NULL) DISPATCH();
1607 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001609 TARGET(INPLACE_TRUE_DIVIDE)
1610 w = POP();
1611 v = TOP();
1612 x = PyNumber_InPlaceTrueDivide(v, w);
1613 Py_DECREF(v);
1614 Py_DECREF(w);
1615 SET_TOP(x);
1616 if (x != NULL) DISPATCH();
1617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001619 TARGET(INPLACE_FLOOR_DIVIDE)
1620 w = POP();
1621 v = TOP();
1622 x = PyNumber_InPlaceFloorDivide(v, w);
1623 Py_DECREF(v);
1624 Py_DECREF(w);
1625 SET_TOP(x);
1626 if (x != NULL) DISPATCH();
1627 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001628
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001629 TARGET(INPLACE_MODULO)
1630 w = POP();
1631 v = TOP();
1632 x = PyNumber_InPlaceRemainder(v, w);
1633 Py_DECREF(v);
1634 Py_DECREF(w);
1635 SET_TOP(x);
1636 if (x != NULL) DISPATCH();
1637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001639 TARGET(INPLACE_ADD)
1640 w = POP();
1641 v = TOP();
1642 if (PyUnicode_CheckExact(v) &&
1643 PyUnicode_CheckExact(w)) {
1644 x = unicode_concatenate(v, w, f, next_instr);
1645 /* unicode_concatenate consumed the ref to v */
1646 goto skip_decref_v;
1647 }
1648 else {
1649 x = PyNumber_InPlaceAdd(v, w);
1650 }
1651 Py_DECREF(v);
1652 skip_decref_v:
1653 Py_DECREF(w);
1654 SET_TOP(x);
1655 if (x != NULL) DISPATCH();
1656 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001657
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001658 TARGET(INPLACE_SUBTRACT)
1659 w = POP();
1660 v = TOP();
1661 x = PyNumber_InPlaceSubtract(v, w);
1662 Py_DECREF(v);
1663 Py_DECREF(w);
1664 SET_TOP(x);
1665 if (x != NULL) DISPATCH();
1666 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001667
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001668 TARGET(INPLACE_LSHIFT)
1669 w = POP();
1670 v = TOP();
1671 x = PyNumber_InPlaceLshift(v, w);
1672 Py_DECREF(v);
1673 Py_DECREF(w);
1674 SET_TOP(x);
1675 if (x != NULL) DISPATCH();
1676 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001677
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001678 TARGET(INPLACE_RSHIFT)
1679 w = POP();
1680 v = TOP();
1681 x = PyNumber_InPlaceRshift(v, w);
1682 Py_DECREF(v);
1683 Py_DECREF(w);
1684 SET_TOP(x);
1685 if (x != NULL) DISPATCH();
1686 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001687
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001688 TARGET(INPLACE_AND)
1689 w = POP();
1690 v = TOP();
1691 x = PyNumber_InPlaceAnd(v, w);
1692 Py_DECREF(v);
1693 Py_DECREF(w);
1694 SET_TOP(x);
1695 if (x != NULL) DISPATCH();
1696 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001697
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001698 TARGET(INPLACE_XOR)
1699 w = POP();
1700 v = TOP();
1701 x = PyNumber_InPlaceXor(v, w);
1702 Py_DECREF(v);
1703 Py_DECREF(w);
1704 SET_TOP(x);
1705 if (x != NULL) DISPATCH();
1706 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001708 TARGET(INPLACE_OR)
1709 w = POP();
1710 v = TOP();
1711 x = PyNumber_InPlaceOr(v, w);
1712 Py_DECREF(v);
1713 Py_DECREF(w);
1714 SET_TOP(x);
1715 if (x != NULL) DISPATCH();
1716 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001717
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001718 TARGET(STORE_SUBSCR)
1719 w = TOP();
1720 v = SECOND();
1721 u = THIRD();
1722 STACKADJ(-3);
1723 /* v[w] = u */
1724 err = PyObject_SetItem(v, w, u);
1725 Py_DECREF(u);
1726 Py_DECREF(v);
1727 Py_DECREF(w);
1728 if (err == 0) DISPATCH();
1729 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001730
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001731 TARGET(DELETE_SUBSCR)
1732 w = TOP();
1733 v = SECOND();
1734 STACKADJ(-2);
1735 /* del v[w] */
1736 err = PyObject_DelItem(v, w);
1737 Py_DECREF(v);
1738 Py_DECREF(w);
1739 if (err == 0) DISPATCH();
1740 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001741
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001742 TARGET(PRINT_EXPR)
1743 v = POP();
1744 w = PySys_GetObject("displayhook");
1745 if (w == NULL) {
1746 PyErr_SetString(PyExc_RuntimeError,
1747 "lost sys.displayhook");
1748 err = -1;
1749 x = NULL;
1750 }
1751 if (err == 0) {
1752 x = PyTuple_Pack(1, v);
1753 if (x == NULL)
1754 err = -1;
1755 }
1756 if (err == 0) {
1757 w = PyEval_CallObject(w, x);
1758 Py_XDECREF(w);
1759 if (w == NULL)
1760 err = -1;
1761 }
1762 Py_DECREF(v);
1763 Py_XDECREF(x);
1764 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001765
Thomas Wouters434d0822000-08-24 20:11:32 +00001766#ifdef CASE_TOO_BIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001767 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001768#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001769 TARGET(RAISE_VARARGS)
1770 v = w = NULL;
1771 switch (oparg) {
1772 case 2:
1773 v = POP(); /* cause */
1774 case 1:
1775 w = POP(); /* exc */
1776 case 0: /* Fallthrough */
1777 why = do_raise(w, v);
1778 break;
1779 default:
1780 PyErr_SetString(PyExc_SystemError,
1781 "bad RAISE_VARARGS oparg");
1782 why = WHY_EXCEPTION;
1783 break;
1784 }
1785 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001786
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001787 TARGET(STORE_LOCALS)
1788 x = POP();
1789 v = f->f_locals;
1790 Py_XDECREF(v);
1791 f->f_locals = x;
1792 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001793
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001794 TARGET(RETURN_VALUE)
1795 retval = POP();
1796 why = WHY_RETURN;
1797 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001798
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001799 TARGET(YIELD_VALUE)
1800 retval = POP();
1801 f->f_stacktop = stack_pointer;
1802 why = WHY_YIELD;
1803 /* Put aside the current exception state and restore
1804 that of the calling frame. This only serves when
1805 "yield" is used inside an except handler. */
1806 SWAP_EXC_STATE();
1807 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001808
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001809 TARGET(POP_EXCEPT)
1810 {
1811 PyTryBlock *b = PyFrame_BlockPop(f);
1812 if (b->b_type != EXCEPT_HANDLER) {
1813 PyErr_SetString(PyExc_SystemError,
1814 "popped block is not an except handler");
1815 why = WHY_EXCEPTION;
1816 break;
1817 }
1818 UNWIND_EXCEPT_HANDLER(b);
1819 }
1820 DISPATCH();
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001821
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001822 TARGET(POP_BLOCK)
1823 {
1824 PyTryBlock *b = PyFrame_BlockPop(f);
1825 UNWIND_BLOCK(b);
1826 }
1827 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00001828
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001829 PREDICTED(END_FINALLY);
1830 TARGET(END_FINALLY)
1831 v = POP();
1832 if (PyLong_Check(v)) {
1833 why = (enum why_code) PyLong_AS_LONG(v);
1834 assert(why != WHY_YIELD);
1835 if (why == WHY_RETURN ||
1836 why == WHY_CONTINUE)
1837 retval = POP();
1838 if (why == WHY_SILENCED) {
1839 /* An exception was silenced by 'with', we must
1840 manually unwind the EXCEPT_HANDLER block which was
1841 created when the exception was caught, otherwise
1842 the stack will be in an inconsistent state. */
1843 PyTryBlock *b = PyFrame_BlockPop(f);
1844 if (b->b_type != EXCEPT_HANDLER) {
1845 PyErr_SetString(PyExc_SystemError,
1846 "popped block is not an except handler");
1847 why = WHY_EXCEPTION;
1848 }
1849 else {
1850 UNWIND_EXCEPT_HANDLER(b);
1851 why = WHY_NOT;
1852 }
1853 }
1854 }
1855 else if (PyExceptionClass_Check(v)) {
1856 w = POP();
1857 u = POP();
1858 PyErr_Restore(v, w, u);
1859 why = WHY_RERAISE;
1860 break;
1861 }
1862 else if (v != Py_None) {
1863 PyErr_SetString(PyExc_SystemError,
1864 "'finally' pops bad exception");
1865 why = WHY_EXCEPTION;
1866 }
1867 Py_DECREF(v);
1868 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001869
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001870 TARGET(LOAD_BUILD_CLASS)
1871 x = PyDict_GetItemString(f->f_builtins,
1872 "__build_class__");
1873 if (x == NULL) {
1874 PyErr_SetString(PyExc_ImportError,
1875 "__build_class__ not found");
1876 break;
1877 }
1878 Py_INCREF(x);
1879 PUSH(x);
1880 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001881
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001882 TARGET(STORE_NAME)
1883 w = GETITEM(names, oparg);
1884 v = POP();
1885 if ((x = f->f_locals) != NULL) {
1886 if (PyDict_CheckExact(x))
1887 err = PyDict_SetItem(x, w, v);
1888 else
1889 err = PyObject_SetItem(x, w, v);
1890 Py_DECREF(v);
1891 if (err == 0) DISPATCH();
1892 break;
1893 }
1894 PyErr_Format(PyExc_SystemError,
1895 "no locals found when storing %R", w);
1896 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001897
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001898 TARGET(DELETE_NAME)
1899 w = GETITEM(names, oparg);
1900 if ((x = f->f_locals) != NULL) {
1901 if ((err = PyObject_DelItem(x, w)) != 0)
1902 format_exc_check_arg(PyExc_NameError,
1903 NAME_ERROR_MSG,
1904 w);
1905 break;
1906 }
1907 PyErr_Format(PyExc_SystemError,
1908 "no locals when deleting %R", w);
1909 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001910
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001911 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1912 TARGET(UNPACK_SEQUENCE)
1913 v = POP();
1914 if (PyTuple_CheckExact(v) &&
1915 PyTuple_GET_SIZE(v) == oparg) {
1916 PyObject **items = \
1917 ((PyTupleObject *)v)->ob_item;
1918 while (oparg--) {
1919 w = items[oparg];
1920 Py_INCREF(w);
1921 PUSH(w);
1922 }
1923 Py_DECREF(v);
1924 DISPATCH();
1925 } else if (PyList_CheckExact(v) &&
1926 PyList_GET_SIZE(v) == oparg) {
1927 PyObject **items = \
1928 ((PyListObject *)v)->ob_item;
1929 while (oparg--) {
1930 w = items[oparg];
1931 Py_INCREF(w);
1932 PUSH(w);
1933 }
1934 } else if (unpack_iterable(v, oparg, -1,
1935 stack_pointer + oparg)) {
1936 stack_pointer += oparg;
1937 } else {
1938 /* unpack_iterable() raised an exception */
1939 why = WHY_EXCEPTION;
1940 }
1941 Py_DECREF(v);
1942 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001943
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001944 TARGET(UNPACK_EX)
1945 {
1946 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
1947 v = POP();
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00001948
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001949 if (unpack_iterable(v, oparg & 0xFF, oparg >> 8,
1950 stack_pointer + totalargs)) {
1951 stack_pointer += totalargs;
1952 } else {
1953 why = WHY_EXCEPTION;
1954 }
1955 Py_DECREF(v);
1956 break;
1957 }
Guido van Rossum0368b722007-05-11 16:50:42 +00001958
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001959 TARGET(STORE_ATTR)
1960 w = GETITEM(names, oparg);
1961 v = TOP();
1962 u = SECOND();
1963 STACKADJ(-2);
1964 err = PyObject_SetAttr(v, w, u); /* v.w = u */
1965 Py_DECREF(v);
1966 Py_DECREF(u);
1967 if (err == 0) DISPATCH();
1968 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001969
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001970 TARGET(DELETE_ATTR)
1971 w = GETITEM(names, oparg);
1972 v = POP();
1973 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
1974 /* del v.w */
1975 Py_DECREF(v);
1976 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001977
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001978 TARGET(STORE_GLOBAL)
1979 w = GETITEM(names, oparg);
1980 v = POP();
1981 err = PyDict_SetItem(f->f_globals, w, v);
1982 Py_DECREF(v);
1983 if (err == 0) DISPATCH();
1984 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001985
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001986 TARGET(DELETE_GLOBAL)
1987 w = GETITEM(names, oparg);
1988 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
1989 format_exc_check_arg(
1990 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
1991 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001992
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00001993 TARGET(LOAD_NAME)
1994 w = GETITEM(names, oparg);
1995 if ((v = f->f_locals) == NULL) {
1996 PyErr_Format(PyExc_SystemError,
1997 "no locals when loading %R", w);
1998 why = WHY_EXCEPTION;
1999 break;
2000 }
2001 if (PyDict_CheckExact(v)) {
2002 x = PyDict_GetItem(v, w);
2003 Py_XINCREF(x);
2004 }
2005 else {
2006 x = PyObject_GetItem(v, w);
2007 if (x == NULL && PyErr_Occurred()) {
2008 if (!PyErr_ExceptionMatches(
2009 PyExc_KeyError))
2010 break;
2011 PyErr_Clear();
2012 }
2013 }
2014 if (x == NULL) {
2015 x = PyDict_GetItem(f->f_globals, w);
2016 if (x == NULL) {
2017 x = PyDict_GetItem(f->f_builtins, w);
2018 if (x == NULL) {
2019 format_exc_check_arg(
2020 PyExc_NameError,
2021 NAME_ERROR_MSG, w);
2022 break;
2023 }
2024 }
2025 Py_INCREF(x);
2026 }
2027 PUSH(x);
2028 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002029
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002030 TARGET(LOAD_GLOBAL)
2031 w = GETITEM(names, oparg);
2032 if (PyUnicode_CheckExact(w)) {
2033 /* Inline the PyDict_GetItem() calls.
2034 WARNING: this is an extreme speed hack.
2035 Do not try this at home. */
2036 long hash = ((PyUnicodeObject *)w)->hash;
2037 if (hash != -1) {
2038 PyDictObject *d;
2039 PyDictEntry *e;
2040 d = (PyDictObject *)(f->f_globals);
2041 e = d->ma_lookup(d, w, hash);
2042 if (e == NULL) {
2043 x = NULL;
2044 break;
2045 }
2046 x = e->me_value;
2047 if (x != NULL) {
2048 Py_INCREF(x);
2049 PUSH(x);
2050 DISPATCH();
2051 }
2052 d = (PyDictObject *)(f->f_builtins);
2053 e = d->ma_lookup(d, w, hash);
2054 if (e == NULL) {
2055 x = NULL;
2056 break;
2057 }
2058 x = e->me_value;
2059 if (x != NULL) {
2060 Py_INCREF(x);
2061 PUSH(x);
2062 DISPATCH();
2063 }
2064 goto load_global_error;
2065 }
2066 }
2067 /* This is the un-inlined version of the code above */
2068 x = PyDict_GetItem(f->f_globals, w);
2069 if (x == NULL) {
2070 x = PyDict_GetItem(f->f_builtins, w);
2071 if (x == NULL) {
2072 load_global_error:
2073 format_exc_check_arg(
2074 PyExc_NameError,
2075 GLOBAL_NAME_ERROR_MSG, w);
2076 break;
2077 }
2078 }
2079 Py_INCREF(x);
2080 PUSH(x);
2081 DISPATCH();
Guido van Rossum681d79a1995-07-18 14:51:37 +00002082
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002083 TARGET(DELETE_FAST)
2084 x = GETLOCAL(oparg);
2085 if (x != NULL) {
2086 SETLOCAL(oparg, NULL);
2087 DISPATCH();
2088 }
2089 format_exc_check_arg(
2090 PyExc_UnboundLocalError,
2091 UNBOUNDLOCAL_ERROR_MSG,
2092 PyTuple_GetItem(co->co_varnames, oparg)
2093 );
2094 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002095
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002096 TARGET(LOAD_CLOSURE)
2097 x = freevars[oparg];
2098 Py_INCREF(x);
2099 PUSH(x);
2100 if (x != NULL) DISPATCH();
2101 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002102
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002103 TARGET(LOAD_DEREF)
2104 x = freevars[oparg];
2105 w = PyCell_Get(x);
2106 if (w != NULL) {
2107 PUSH(w);
2108 DISPATCH();
2109 }
2110 err = -1;
2111 /* Don't stomp existing exception */
2112 if (PyErr_Occurred())
2113 break;
2114 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2115 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krahc87901d2010-06-23 18:54:09 +00002116 oparg);
2117 format_exc_check_arg(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002118 PyExc_UnboundLocalError,
2119 UNBOUNDLOCAL_ERROR_MSG,
2120 v);
2121 } else {
2122 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2123 PyTuple_GET_SIZE(co->co_cellvars));
2124 format_exc_check_arg(PyExc_NameError,
2125 UNBOUNDFREE_ERROR_MSG, v);
2126 }
2127 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002128
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002129 TARGET(STORE_DEREF)
2130 w = POP();
2131 x = freevars[oparg];
2132 PyCell_Set(x, w);
2133 Py_DECREF(w);
2134 DISPATCH();
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002135
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002136 TARGET(BUILD_TUPLE)
2137 x = PyTuple_New(oparg);
2138 if (x != NULL) {
2139 for (; --oparg >= 0;) {
2140 w = POP();
2141 PyTuple_SET_ITEM(x, oparg, w);
2142 }
2143 PUSH(x);
2144 DISPATCH();
2145 }
2146 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002147
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002148 TARGET(BUILD_LIST)
2149 x = PyList_New(oparg);
2150 if (x != NULL) {
2151 for (; --oparg >= 0;) {
2152 w = POP();
2153 PyList_SET_ITEM(x, oparg, w);
2154 }
2155 PUSH(x);
2156 DISPATCH();
2157 }
2158 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002159
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002160 TARGET(BUILD_SET)
2161 x = PySet_New(NULL);
2162 if (x != NULL) {
2163 for (; --oparg >= 0;) {
2164 w = POP();
2165 if (err == 0)
2166 err = PySet_Add(x, w);
2167 Py_DECREF(w);
2168 }
2169 if (err != 0) {
2170 Py_DECREF(x);
2171 break;
2172 }
2173 PUSH(x);
2174 DISPATCH();
2175 }
2176 break;
Guido van Rossum86e58e22006-08-28 15:27:34 +00002177
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002178 TARGET(BUILD_MAP)
2179 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2180 PUSH(x);
2181 if (x != NULL) DISPATCH();
2182 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002183
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002184 TARGET(STORE_MAP)
2185 w = TOP(); /* key */
2186 u = SECOND(); /* value */
2187 v = THIRD(); /* dict */
2188 STACKADJ(-2);
2189 assert (PyDict_CheckExact(v));
2190 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2191 Py_DECREF(u);
2192 Py_DECREF(w);
2193 if (err == 0) DISPATCH();
2194 break;
Christian Heimes99170a52007-12-19 02:07:34 +00002195
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002196 TARGET(MAP_ADD)
2197 w = TOP(); /* key */
2198 u = SECOND(); /* value */
2199 STACKADJ(-2);
2200 v = stack_pointer[-oparg]; /* dict */
2201 assert (PyDict_CheckExact(v));
2202 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2203 Py_DECREF(u);
2204 Py_DECREF(w);
2205 if (err == 0) {
2206 PREDICT(JUMP_ABSOLUTE);
2207 DISPATCH();
2208 }
2209 break;
Antoine Pitrouf289ae62008-12-18 11:06:25 +00002210
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002211 TARGET(LOAD_ATTR)
2212 w = GETITEM(names, oparg);
2213 v = TOP();
2214 x = PyObject_GetAttr(v, w);
2215 Py_DECREF(v);
2216 SET_TOP(x);
2217 if (x != NULL) DISPATCH();
2218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002219
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002220 TARGET(COMPARE_OP)
2221 w = POP();
2222 v = TOP();
2223 x = cmp_outcome(oparg, v, w);
2224 Py_DECREF(v);
2225 Py_DECREF(w);
2226 SET_TOP(x);
2227 if (x == NULL) break;
2228 PREDICT(POP_JUMP_IF_FALSE);
2229 PREDICT(POP_JUMP_IF_TRUE);
2230 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002231
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002232 TARGET(IMPORT_NAME)
2233 w = GETITEM(names, oparg);
2234 x = PyDict_GetItemString(f->f_builtins, "__import__");
2235 if (x == NULL) {
2236 PyErr_SetString(PyExc_ImportError,
2237 "__import__ not found");
2238 break;
2239 }
2240 Py_INCREF(x);
2241 v = POP();
2242 u = TOP();
2243 if (PyLong_AsLong(u) != -1 || PyErr_Occurred())
2244 w = PyTuple_Pack(5,
2245 w,
2246 f->f_globals,
2247 f->f_locals == NULL ?
2248 Py_None : f->f_locals,
2249 v,
2250 u);
2251 else
2252 w = PyTuple_Pack(4,
2253 w,
2254 f->f_globals,
2255 f->f_locals == NULL ?
2256 Py_None : f->f_locals,
2257 v);
2258 Py_DECREF(v);
2259 Py_DECREF(u);
2260 if (w == NULL) {
2261 u = POP();
2262 Py_DECREF(x);
2263 x = NULL;
2264 break;
2265 }
2266 READ_TIMESTAMP(intr0);
2267 v = x;
2268 x = PyEval_CallObject(v, w);
2269 Py_DECREF(v);
2270 READ_TIMESTAMP(intr1);
2271 Py_DECREF(w);
2272 SET_TOP(x);
2273 if (x != NULL) DISPATCH();
2274 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002275
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002276 TARGET(IMPORT_STAR)
2277 v = POP();
2278 PyFrame_FastToLocals(f);
2279 if ((x = f->f_locals) == NULL) {
2280 PyErr_SetString(PyExc_SystemError,
2281 "no locals found during 'import *'");
2282 break;
2283 }
2284 READ_TIMESTAMP(intr0);
2285 err = import_all_from(x, v);
2286 READ_TIMESTAMP(intr1);
2287 PyFrame_LocalsToFast(f, 0);
2288 Py_DECREF(v);
2289 if (err == 0) DISPATCH();
2290 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002291
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002292 TARGET(IMPORT_FROM)
2293 w = GETITEM(names, oparg);
2294 v = TOP();
2295 READ_TIMESTAMP(intr0);
2296 x = import_from(v, w);
2297 READ_TIMESTAMP(intr1);
2298 PUSH(x);
2299 if (x != NULL) DISPATCH();
2300 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002301
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002302 TARGET(JUMP_FORWARD)
2303 JUMPBY(oparg);
2304 FAST_DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002305
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002306 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2307 TARGET(POP_JUMP_IF_FALSE)
2308 w = POP();
2309 if (w == Py_True) {
2310 Py_DECREF(w);
2311 FAST_DISPATCH();
2312 }
2313 if (w == Py_False) {
2314 Py_DECREF(w);
2315 JUMPTO(oparg);
2316 FAST_DISPATCH();
2317 }
2318 err = PyObject_IsTrue(w);
2319 Py_DECREF(w);
2320 if (err > 0)
2321 err = 0;
2322 else if (err == 0)
2323 JUMPTO(oparg);
2324 else
2325 break;
2326 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002327
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002328 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2329 TARGET(POP_JUMP_IF_TRUE)
2330 w = POP();
2331 if (w == Py_False) {
2332 Py_DECREF(w);
2333 FAST_DISPATCH();
2334 }
2335 if (w == Py_True) {
2336 Py_DECREF(w);
2337 JUMPTO(oparg);
2338 FAST_DISPATCH();
2339 }
2340 err = PyObject_IsTrue(w);
2341 Py_DECREF(w);
2342 if (err > 0) {
2343 err = 0;
2344 JUMPTO(oparg);
2345 }
2346 else if (err == 0)
2347 ;
2348 else
2349 break;
2350 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002351
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002352 TARGET(JUMP_IF_FALSE_OR_POP)
2353 w = TOP();
2354 if (w == Py_True) {
2355 STACKADJ(-1);
2356 Py_DECREF(w);
2357 FAST_DISPATCH();
2358 }
2359 if (w == Py_False) {
2360 JUMPTO(oparg);
2361 FAST_DISPATCH();
2362 }
2363 err = PyObject_IsTrue(w);
2364 if (err > 0) {
2365 STACKADJ(-1);
2366 Py_DECREF(w);
2367 err = 0;
2368 }
2369 else if (err == 0)
2370 JUMPTO(oparg);
2371 else
2372 break;
2373 DISPATCH();
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002374
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002375 TARGET(JUMP_IF_TRUE_OR_POP)
2376 w = TOP();
2377 if (w == Py_False) {
2378 STACKADJ(-1);
2379 Py_DECREF(w);
2380 FAST_DISPATCH();
2381 }
2382 if (w == Py_True) {
2383 JUMPTO(oparg);
2384 FAST_DISPATCH();
2385 }
2386 err = PyObject_IsTrue(w);
2387 if (err > 0) {
2388 err = 0;
2389 JUMPTO(oparg);
2390 }
2391 else if (err == 0) {
2392 STACKADJ(-1);
2393 Py_DECREF(w);
2394 }
2395 else
2396 break;
2397 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002398
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002399 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2400 TARGET(JUMP_ABSOLUTE)
2401 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002402#if FAST_LOOPS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002403 /* Enabling this path speeds-up all while and for-loops by bypassing
2404 the per-loop checks for signals. By default, this should be turned-off
2405 because it prevents detection of a control-break in tight loops like
2406 "while 1: pass". Compile with this option turned-on when you need
2407 the speed-up and do not need break checking inside tight loops (ones
2408 that contain only instructions ending with FAST_DISPATCH).
2409 */
2410 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002411#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002412 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002413#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002414
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002415 TARGET(GET_ITER)
2416 /* before: [obj]; after [getiter(obj)] */
2417 v = TOP();
2418 x = PyObject_GetIter(v);
2419 Py_DECREF(v);
2420 if (x != NULL) {
2421 SET_TOP(x);
2422 PREDICT(FOR_ITER);
2423 DISPATCH();
2424 }
2425 STACKADJ(-1);
2426 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002427
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002428 PREDICTED_WITH_ARG(FOR_ITER);
2429 TARGET(FOR_ITER)
2430 /* before: [iter]; after: [iter, iter()] *or* [] */
2431 v = TOP();
2432 x = (*v->ob_type->tp_iternext)(v);
2433 if (x != NULL) {
2434 PUSH(x);
2435 PREDICT(STORE_FAST);
2436 PREDICT(UNPACK_SEQUENCE);
2437 DISPATCH();
2438 }
2439 if (PyErr_Occurred()) {
2440 if (!PyErr_ExceptionMatches(
2441 PyExc_StopIteration))
2442 break;
2443 PyErr_Clear();
2444 }
2445 /* iterator ended normally */
2446 x = v = POP();
2447 Py_DECREF(v);
2448 JUMPBY(oparg);
2449 DISPATCH();
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002450
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002451 TARGET(BREAK_LOOP)
2452 why = WHY_BREAK;
2453 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002454
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002455 TARGET(CONTINUE_LOOP)
2456 retval = PyLong_FromLong(oparg);
2457 if (!retval) {
2458 x = NULL;
2459 break;
2460 }
2461 why = WHY_CONTINUE;
2462 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002463
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002464 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2465 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2466 TARGET(SETUP_FINALLY)
2467 _setup_finally:
2468 /* NOTE: If you add any new block-setup opcodes that
2469 are not try/except/finally handlers, you may need
2470 to update the PyGen_NeedsFinalizing() function.
2471 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002472
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002473 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2474 STACK_LEVEL());
2475 DISPATCH();
Guido van Rossumac7be682001-01-17 15:42:30 +00002476
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002477 TARGET(WITH_CLEANUP)
2478 {
2479 /* At the top of the stack are 1-3 values indicating
2480 how/why we entered the finally clause:
2481 - TOP = None
2482 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2483 - TOP = WHY_*; no retval below it
2484 - (TOP, SECOND, THIRD) = exc_info()
2485 Below them is EXIT, the context.__exit__ bound method.
2486 In the last case, we must call
2487 EXIT(TOP, SECOND, THIRD)
2488 otherwise we must call
2489 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002490
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002491 In all cases, we remove EXIT from the stack, leaving
2492 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002493
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002494 In addition, if the stack represents an exception,
2495 *and* the function call returns a 'true' value, we
2496 "zap" this information, to prevent END_FINALLY from
2497 re-raising the exception. (But non-local gotos
2498 should still be resumed.)
2499 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00002500
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002501 PyObject *exit_func = POP();
2502 u = TOP();
2503 if (u == Py_None) {
2504 v = w = Py_None;
2505 }
2506 else if (PyLong_Check(u)) {
2507 u = v = w = Py_None;
2508 }
2509 else {
2510 v = SECOND();
2511 w = THIRD();
2512 }
2513 /* XXX Not the fastest way to call it... */
2514 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2515 NULL);
2516 Py_DECREF(exit_func);
2517 if (x == NULL)
2518 break; /* Go to error exit */
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002519
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002520 if (u != Py_None)
2521 err = PyObject_IsTrue(x);
2522 else
2523 err = 0;
2524 Py_DECREF(x);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00002525
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002526 if (err < 0)
2527 break; /* Go to error exit */
2528 else if (err > 0) {
2529 err = 0;
2530 /* There was an exception and a True return */
2531 STACKADJ(-2);
2532 SET_TOP(PyLong_FromLong((long) WHY_SILENCED));
2533 Py_DECREF(u);
2534 Py_DECREF(v);
2535 Py_DECREF(w);
2536 }
2537 PREDICT(END_FINALLY);
2538 break;
2539 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002540
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002541 TARGET(CALL_FUNCTION)
2542 {
2543 PyObject **sp;
2544 PCALL(PCALL_ALL);
2545 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002546#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002547 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002548#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002549 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002550#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002551 stack_pointer = sp;
2552 PUSH(x);
2553 if (x != NULL)
2554 DISPATCH();
2555 break;
2556 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002557
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002558 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2559 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
2560 TARGET(CALL_FUNCTION_VAR_KW)
2561 _call_function_var_kw:
2562 {
2563 int na = oparg & 0xff;
2564 int nk = (oparg>>8) & 0xff;
2565 int flags = (opcode - CALL_FUNCTION) & 3;
2566 int n = na + 2 * nk;
2567 PyObject **pfunc, *func, **sp;
2568 PCALL(PCALL_ALL);
2569 if (flags & CALL_FLAG_VAR)
2570 n++;
2571 if (flags & CALL_FLAG_KW)
2572 n++;
2573 pfunc = stack_pointer - n - 1;
2574 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002575
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002576 if (PyMethod_Check(func)
Stefan Krahc87901d2010-06-23 18:54:09 +00002577 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002578 PyObject *self = PyMethod_GET_SELF(func);
2579 Py_INCREF(self);
2580 func = PyMethod_GET_FUNCTION(func);
2581 Py_INCREF(func);
2582 Py_DECREF(*pfunc);
2583 *pfunc = self;
2584 na++;
2585 n++;
2586 } else
2587 Py_INCREF(func);
2588 sp = stack_pointer;
2589 READ_TIMESTAMP(intr0);
2590 x = ext_do_call(func, &sp, flags, na, nk);
2591 READ_TIMESTAMP(intr1);
2592 stack_pointer = sp;
2593 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002594
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002595 while (stack_pointer > pfunc) {
2596 w = POP();
2597 Py_DECREF(w);
2598 }
2599 PUSH(x);
2600 if (x != NULL)
2601 DISPATCH();
2602 break;
2603 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002604
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002605 TARGET_WITH_IMPL(MAKE_CLOSURE, _make_function)
2606 TARGET(MAKE_FUNCTION)
2607 _make_function:
2608 {
2609 int posdefaults = oparg & 0xff;
2610 int kwdefaults = (oparg>>8) & 0xff;
2611 int num_annotations = (oparg >> 16) & 0x7fff;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002612
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002613 v = POP(); /* code object */
2614 x = PyFunction_New(v, f->f_globals);
2615 Py_DECREF(v);
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00002616
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002617 if (x != NULL && opcode == MAKE_CLOSURE) {
2618 v = POP();
2619 if (PyFunction_SetClosure(x, v) != 0) {
2620 /* Can't happen unless bytecode is corrupt. */
2621 why = WHY_EXCEPTION;
2622 }
2623 Py_DECREF(v);
2624 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002625
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002626 if (x != NULL && num_annotations > 0) {
2627 Py_ssize_t name_ix;
2628 u = POP(); /* names of args with annotations */
2629 v = PyDict_New();
2630 if (v == NULL) {
2631 Py_DECREF(x);
2632 x = NULL;
2633 break;
2634 }
2635 name_ix = PyTuple_Size(u);
2636 assert(num_annotations == name_ix+1);
2637 while (name_ix > 0) {
2638 --name_ix;
2639 t = PyTuple_GET_ITEM(u, name_ix);
2640 w = POP();
2641 /* XXX(nnorwitz): check for errors */
2642 PyDict_SetItem(v, t, w);
2643 Py_DECREF(w);
2644 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002645
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002646 if (PyFunction_SetAnnotations(x, v) != 0) {
2647 /* Can't happen unless
2648 PyFunction_SetAnnotations changes. */
2649 why = WHY_EXCEPTION;
2650 }
2651 Py_DECREF(v);
2652 Py_DECREF(u);
2653 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002654
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002655 /* XXX Maybe this should be a separate opcode? */
2656 if (x != NULL && posdefaults > 0) {
2657 v = PyTuple_New(posdefaults);
2658 if (v == NULL) {
2659 Py_DECREF(x);
2660 x = NULL;
2661 break;
2662 }
2663 while (--posdefaults >= 0) {
2664 w = POP();
2665 PyTuple_SET_ITEM(v, posdefaults, w);
2666 }
2667 if (PyFunction_SetDefaults(x, v) != 0) {
2668 /* Can't happen unless
2669 PyFunction_SetDefaults changes. */
2670 why = WHY_EXCEPTION;
2671 }
2672 Py_DECREF(v);
2673 }
2674 if (x != NULL && kwdefaults > 0) {
2675 v = PyDict_New();
2676 if (v == NULL) {
2677 Py_DECREF(x);
2678 x = NULL;
2679 break;
2680 }
2681 while (--kwdefaults >= 0) {
2682 w = POP(); /* default value */
2683 u = POP(); /* kw only arg name */
2684 /* XXX(nnorwitz): check for errors */
2685 PyDict_SetItem(v, u, w);
2686 Py_DECREF(w);
2687 Py_DECREF(u);
2688 }
2689 if (PyFunction_SetKwDefaults(x, v) != 0) {
2690 /* Can't happen unless
2691 PyFunction_SetKwDefaults changes. */
2692 why = WHY_EXCEPTION;
2693 }
2694 Py_DECREF(v);
2695 }
2696 PUSH(x);
2697 break;
2698 }
Guido van Rossum8861b741996-07-30 16:49:37 +00002699
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002700 TARGET(BUILD_SLICE)
2701 if (oparg == 3)
2702 w = POP();
2703 else
2704 w = NULL;
2705 v = POP();
2706 u = TOP();
2707 x = PySlice_New(u, v, w);
2708 Py_DECREF(u);
2709 Py_DECREF(v);
2710 Py_XDECREF(w);
2711 SET_TOP(x);
2712 if (x != NULL) DISPATCH();
2713 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002714
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002715 TARGET(EXTENDED_ARG)
2716 opcode = NEXTOP();
2717 oparg = oparg<<16 | NEXTARG();
2718 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002719
Antoine Pitroub52ec782009-01-25 16:34:23 +00002720#ifdef USE_COMPUTED_GOTOS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002721 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00002722#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002723 default:
2724 fprintf(stderr,
2725 "XXX lineno: %d, opcode: %d\n",
2726 PyCode_Addr2Line(f->f_code, f->f_lasti),
2727 opcode);
2728 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2729 why = WHY_EXCEPTION;
2730 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002731
2732#ifdef CASE_TOO_BIG
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002733 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002734#endif
2735
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002736 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002737
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002738 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002739
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002740 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002741
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002742 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002743
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002744 if (why == WHY_NOT) {
2745 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002746#ifdef CHECKEXC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002747 /* This check is expensive! */
2748 if (PyErr_Occurred())
2749 fprintf(stderr,
2750 "XXX undetected error\n");
2751 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002752#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002753 READ_TIMESTAMP(loop1);
2754 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002755#ifdef CHECKEXC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002756 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002757#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002758 }
2759 why = WHY_EXCEPTION;
2760 x = Py_None;
2761 err = 0;
2762 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002763
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002764 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002765
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002766 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2767 if (!PyErr_Occurred()) {
2768 PyErr_SetString(PyExc_SystemError,
2769 "error return without exception set");
2770 why = WHY_EXCEPTION;
2771 }
2772 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002773#ifdef CHECKEXC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002774 else {
2775 /* This check is expensive! */
2776 if (PyErr_Occurred()) {
2777 char buf[128];
2778 sprintf(buf, "Stack unwind with exception "
2779 "set and why=%d", why);
2780 Py_FatalError(buf);
2781 }
2782 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002783#endif
2784
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002785 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002786
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002787 if (why == WHY_EXCEPTION) {
2788 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002789
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002790 if (tstate->c_tracefunc != NULL)
2791 call_exc_trace(tstate->c_tracefunc,
2792 tstate->c_traceobj, f);
2793 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002794
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002795 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002796
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002797 if (why == WHY_RERAISE)
2798 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002799
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002800 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002801
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002802fast_block_end:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002803 while (why != WHY_NOT && f->f_iblock > 0) {
2804 PyTryBlock *b = PyFrame_BlockPop(f);
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002805
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002806 assert(why != WHY_YIELD);
2807 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2808 /* For a continue inside a try block,
2809 don't pop the block for the loop. */
2810 PyFrame_BlockSetup(f, b->b_type, b->b_handler,
2811 b->b_level);
2812 why = WHY_NOT;
2813 JUMPTO(PyLong_AS_LONG(retval));
2814 Py_DECREF(retval);
2815 break;
2816 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002817
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002818 if (b->b_type == EXCEPT_HANDLER) {
2819 UNWIND_EXCEPT_HANDLER(b);
2820 continue;
2821 }
2822 UNWIND_BLOCK(b);
2823 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2824 why = WHY_NOT;
2825 JUMPTO(b->b_handler);
2826 break;
2827 }
2828 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
2829 || b->b_type == SETUP_FINALLY)) {
2830 PyObject *exc, *val, *tb;
2831 int handler = b->b_handler;
2832 /* Beware, this invalidates all b->b_* fields */
2833 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
2834 PUSH(tstate->exc_traceback);
2835 PUSH(tstate->exc_value);
2836 if (tstate->exc_type != NULL) {
2837 PUSH(tstate->exc_type);
2838 }
2839 else {
2840 Py_INCREF(Py_None);
2841 PUSH(Py_None);
2842 }
2843 PyErr_Fetch(&exc, &val, &tb);
2844 /* Make the raw exception data
2845 available to the handler,
2846 so a program can emulate the
2847 Python main loop. */
2848 PyErr_NormalizeException(
2849 &exc, &val, &tb);
2850 PyException_SetTraceback(val, tb);
2851 Py_INCREF(exc);
2852 tstate->exc_type = exc;
2853 Py_INCREF(val);
2854 tstate->exc_value = val;
2855 tstate->exc_traceback = tb;
2856 if (tb == NULL)
2857 tb = Py_None;
2858 Py_INCREF(tb);
2859 PUSH(tb);
2860 PUSH(val);
2861 PUSH(exc);
2862 why = WHY_NOT;
2863 JUMPTO(handler);
2864 break;
2865 }
2866 if (b->b_type == SETUP_FINALLY) {
2867 if (why & (WHY_RETURN | WHY_CONTINUE))
2868 PUSH(retval);
2869 PUSH(PyLong_FromLong((long)why));
2870 why = WHY_NOT;
2871 JUMPTO(b->b_handler);
2872 break;
2873 }
2874 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002875
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002876 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002877
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002878 if (why != WHY_NOT)
2879 break;
2880 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002881
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002882 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002883
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002884 assert(why != WHY_YIELD);
2885 /* Pop remaining stack entries. */
2886 while (!EMPTY()) {
2887 v = POP();
2888 Py_XDECREF(v);
2889 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002890
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002891 if (why != WHY_RETURN)
2892 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002893
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002894fast_yield:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002895 if (tstate->use_tracing) {
2896 if (tstate->c_tracefunc) {
2897 if (why == WHY_RETURN || why == WHY_YIELD) {
2898 if (call_trace(tstate->c_tracefunc,
2899 tstate->c_traceobj, f,
2900 PyTrace_RETURN, retval)) {
2901 Py_XDECREF(retval);
2902 retval = NULL;
2903 why = WHY_EXCEPTION;
2904 }
2905 }
2906 else if (why == WHY_EXCEPTION) {
2907 call_trace_protected(tstate->c_tracefunc,
2908 tstate->c_traceobj, f,
2909 PyTrace_RETURN, NULL);
2910 }
2911 }
2912 if (tstate->c_profilefunc) {
2913 if (why == WHY_EXCEPTION)
2914 call_trace_protected(tstate->c_profilefunc,
2915 tstate->c_profileobj, f,
2916 PyTrace_RETURN, NULL);
2917 else if (call_trace(tstate->c_profilefunc,
2918 tstate->c_profileobj, f,
2919 PyTrace_RETURN, retval)) {
2920 Py_XDECREF(retval);
2921 retval = NULL;
2922 why = WHY_EXCEPTION;
2923 }
2924 }
2925 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002926
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002927 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00002928exit_eval_frame:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002929 Py_LeaveRecursiveCall();
2930 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00002931
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002932 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00002933}
2934
Guido van Rossumc2e20742006-02-27 22:32:47 +00002935/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00002936 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00002937 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00002938
Tim Peters6d6c1a32001-08-02 04:15:00 +00002939PyObject *
2940PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002941 PyObject **args, int argcount, PyObject **kws, int kwcount,
2942 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00002943{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002944 register PyFrameObject *f;
2945 register PyObject *retval = NULL;
2946 register PyObject **fastlocals, **freevars;
2947 PyThreadState *tstate = PyThreadState_GET();
2948 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00002949
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002950 if (globals == NULL) {
2951 PyErr_SetString(PyExc_SystemError,
2952 "PyEval_EvalCodeEx: NULL globals");
2953 return NULL;
2954 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002955
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002956 assert(tstate != NULL);
2957 assert(globals != NULL);
2958 f = PyFrame_New(tstate, co, globals, locals);
2959 if (f == NULL)
2960 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00002961
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002962 fastlocals = f->f_localsplus;
2963 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00002964
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00002965 if (co->co_argcount > 0 ||
2966 co->co_kwonlyargcount > 0 ||
2967 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
2968 int i;
2969 int n = argcount;
2970 PyObject *kwdict = NULL;
2971 if (co->co_flags & CO_VARKEYWORDS) {
2972 kwdict = PyDict_New();
2973 if (kwdict == NULL)
2974 goto fail;
2975 i = co->co_argcount + co->co_kwonlyargcount;
2976 if (co->co_flags & CO_VARARGS)
2977 i++;
2978 SETLOCAL(i, kwdict);
2979 }
2980 if (argcount > co->co_argcount) {
2981 if (!(co->co_flags & CO_VARARGS)) {
2982 PyErr_Format(PyExc_TypeError,
2983 "%U() takes %s %d "
2984 "%spositional argument%s (%d given)",
2985 co->co_name,
2986 defcount ? "at most" : "exactly",
2987 co->co_argcount,
2988 kwcount ? "non-keyword " : "",
2989 co->co_argcount == 1 ? "" : "s",
2990 argcount);
2991 goto fail;
2992 }
2993 n = co->co_argcount;
2994 }
2995 for (i = 0; i < n; i++) {
2996 x = args[i];
2997 Py_INCREF(x);
2998 SETLOCAL(i, x);
2999 }
3000 if (co->co_flags & CO_VARARGS) {
3001 u = PyTuple_New(argcount - n);
3002 if (u == NULL)
3003 goto fail;
3004 SETLOCAL(co->co_argcount + co->co_kwonlyargcount, u);
3005 for (i = n; i < argcount; i++) {
3006 x = args[i];
3007 Py_INCREF(x);
3008 PyTuple_SET_ITEM(u, i-n, x);
3009 }
3010 }
3011 for (i = 0; i < kwcount; i++) {
3012 PyObject **co_varnames;
3013 PyObject *keyword = kws[2*i];
3014 PyObject *value = kws[2*i + 1];
3015 int j;
3016 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3017 PyErr_Format(PyExc_TypeError,
3018 "%U() keywords must be strings",
3019 co->co_name);
3020 goto fail;
3021 }
3022 /* Speed hack: do raw pointer compares. As names are
3023 normally interned this should almost always hit. */
3024 co_varnames = PySequence_Fast_ITEMS(co->co_varnames);
3025 for (j = 0;
3026 j < co->co_argcount + co->co_kwonlyargcount;
3027 j++) {
3028 PyObject *nm = co_varnames[j];
3029 if (nm == keyword)
3030 goto kw_found;
3031 }
3032 /* Slow fallback, just in case */
3033 for (j = 0;
3034 j < co->co_argcount + co->co_kwonlyargcount;
3035 j++) {
3036 PyObject *nm = co_varnames[j];
3037 int cmp = PyObject_RichCompareBool(
3038 keyword, nm, Py_EQ);
3039 if (cmp > 0)
3040 goto kw_found;
3041 else if (cmp < 0)
3042 goto fail;
3043 }
3044 /* Check errors from Compare */
3045 if (PyErr_Occurred())
3046 goto fail;
3047 if (j >= co->co_argcount + co->co_kwonlyargcount) {
3048 if (kwdict == NULL) {
3049 PyErr_Format(PyExc_TypeError,
3050 "%U() got an unexpected "
3051 "keyword argument '%S'",
3052 co->co_name,
3053 keyword);
3054 goto fail;
3055 }
3056 PyDict_SetItem(kwdict, keyword, value);
3057 continue;
3058 }
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003059kw_found:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003060 if (GETLOCAL(j) != NULL) {
3061 PyErr_Format(PyExc_TypeError,
3062 "%U() got multiple "
3063 "values for keyword "
3064 "argument '%S'",
3065 co->co_name,
3066 keyword);
3067 goto fail;
3068 }
3069 Py_INCREF(value);
3070 SETLOCAL(j, value);
3071 }
3072 if (co->co_kwonlyargcount > 0) {
3073 for (i = co->co_argcount;
3074 i < co->co_argcount + co->co_kwonlyargcount;
3075 i++) {
3076 PyObject *name, *def;
3077 if (GETLOCAL(i) != NULL)
3078 continue;
3079 name = PyTuple_GET_ITEM(co->co_varnames, i);
3080 def = NULL;
3081 if (kwdefs != NULL)
3082 def = PyDict_GetItem(kwdefs, name);
3083 if (def != NULL) {
3084 Py_INCREF(def);
3085 SETLOCAL(i, def);
3086 continue;
3087 }
3088 PyErr_Format(PyExc_TypeError,
3089 "%U() needs keyword-only argument %S",
3090 co->co_name, name);
3091 goto fail;
3092 }
3093 }
3094 if (argcount < co->co_argcount) {
3095 int m = co->co_argcount - defcount;
3096 for (i = argcount; i < m; i++) {
3097 if (GETLOCAL(i) == NULL) {
3098 PyErr_Format(PyExc_TypeError,
3099 "%U() takes %s %d "
3100 "%spositional argument%s "
3101 "(%d given)",
3102 co->co_name,
3103 ((co->co_flags & CO_VARARGS) ||
3104 defcount) ? "at least"
3105 : "exactly",
3106 m, kwcount ? "non-keyword " : "",
3107 m == 1 ? "" : "s", i);
3108 goto fail;
3109 }
3110 }
3111 if (n > m)
3112 i = n - m;
3113 else
3114 i = 0;
3115 for (; i < defcount; i++) {
3116 if (GETLOCAL(m+i) == NULL) {
3117 PyObject *def = defs[i];
3118 Py_INCREF(def);
3119 SETLOCAL(m+i, def);
3120 }
3121 }
3122 }
3123 }
3124 else {
3125 if (argcount > 0 || kwcount > 0) {
3126 PyErr_Format(PyExc_TypeError,
3127 "%U() takes no arguments (%d given)",
3128 co->co_name,
3129 argcount + kwcount);
3130 goto fail;
3131 }
3132 }
3133 /* Allocate and initialize storage for cell vars, and copy free
3134 vars into frame. This isn't too efficient right now. */
3135 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3136 int i, j, nargs, found;
3137 Py_UNICODE *cellname, *argname;
3138 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003139
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003140 nargs = co->co_argcount + co->co_kwonlyargcount;
3141 if (co->co_flags & CO_VARARGS)
3142 nargs++;
3143 if (co->co_flags & CO_VARKEYWORDS)
3144 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003145
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003146 /* Initialize each cell var, taking into account
3147 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003148
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003149 Should arrange for the compiler to put cellvars
3150 that are arguments at the beginning of the cellvars
3151 list so that we can march over it more efficiently?
3152 */
3153 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3154 cellname = PyUnicode_AS_UNICODE(
3155 PyTuple_GET_ITEM(co->co_cellvars, i));
3156 found = 0;
3157 for (j = 0; j < nargs; j++) {
3158 argname = PyUnicode_AS_UNICODE(
3159 PyTuple_GET_ITEM(co->co_varnames, j));
3160 if (Py_UNICODE_strcmp(cellname, argname) == 0) {
3161 c = PyCell_New(GETLOCAL(j));
3162 if (c == NULL)
3163 goto fail;
3164 GETLOCAL(co->co_nlocals + i) = c;
3165 found = 1;
3166 break;
3167 }
3168 }
3169 if (found == 0) {
3170 c = PyCell_New(NULL);
3171 if (c == NULL)
3172 goto fail;
3173 SETLOCAL(co->co_nlocals + i, c);
3174 }
3175 }
3176 }
3177 if (PyTuple_GET_SIZE(co->co_freevars)) {
3178 int i;
3179 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3180 PyObject *o = PyTuple_GET_ITEM(closure, i);
3181 Py_INCREF(o);
3182 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3183 }
3184 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003185
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003186 if (co->co_flags & CO_GENERATOR) {
3187 /* Don't need to keep the reference to f_back, it will be set
3188 * when the generator is resumed. */
3189 Py_XDECREF(f->f_back);
3190 f->f_back = NULL;
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003191
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003192 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003193
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003194 /* Create a new generator that owns the ready to run frame
3195 * and return that as the value. */
3196 return PyGen_New(f);
3197 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003198
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003199 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003200
Thomas Woutersce272b62007-09-19 21:19:28 +00003201fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003202
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003203 /* decref'ing the frame can cause __del__ methods to get invoked,
3204 which can call back into Python. While we're done with the
3205 current Python frame (f), the associated C stack is still in use,
3206 so recursion_depth must be boosted for the duration.
3207 */
3208 assert(tstate != NULL);
3209 ++tstate->recursion_depth;
3210 Py_DECREF(f);
3211 --tstate->recursion_depth;
3212 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003213}
3214
3215
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003216/* Logic for the raise statement (too complicated for inlining).
3217 This *consumes* a reference count to each of its arguments. */
Raymond Hettinger7c958652004-04-06 10:11:10 +00003218static enum why_code
Collin Winter828f04a2007-08-31 00:04:24 +00003219do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003220{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003221 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00003222
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003223 if (exc == NULL) {
3224 /* Reraise */
3225 PyThreadState *tstate = PyThreadState_GET();
3226 PyObject *tb;
3227 type = tstate->exc_type;
3228 value = tstate->exc_value;
3229 tb = tstate->exc_traceback;
3230 if (type == Py_None) {
3231 PyErr_SetString(PyExc_RuntimeError,
3232 "No active exception to reraise");
3233 return WHY_EXCEPTION;
3234 }
3235 Py_XINCREF(type);
3236 Py_XINCREF(value);
3237 Py_XINCREF(tb);
3238 PyErr_Restore(type, value, tb);
3239 return WHY_RERAISE;
3240 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003241
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003242 /* We support the following forms of raise:
3243 raise
Collin Winter828f04a2007-08-31 00:04:24 +00003244 raise <instance>
3245 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003246
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003247 if (PyExceptionClass_Check(exc)) {
3248 type = exc;
3249 value = PyObject_CallObject(exc, NULL);
3250 if (value == NULL)
3251 goto raise_error;
3252 }
3253 else if (PyExceptionInstance_Check(exc)) {
3254 value = exc;
3255 type = PyExceptionInstance_Class(exc);
3256 Py_INCREF(type);
3257 }
3258 else {
3259 /* Not something you can raise. You get an exception
3260 anyway, just not what you specified :-) */
3261 Py_DECREF(exc);
3262 PyErr_SetString(PyExc_TypeError,
3263 "exceptions must derive from BaseException");
3264 goto raise_error;
3265 }
Collin Winter828f04a2007-08-31 00:04:24 +00003266
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003267 if (cause) {
3268 PyObject *fixed_cause;
3269 if (PyExceptionClass_Check(cause)) {
3270 fixed_cause = PyObject_CallObject(cause, NULL);
3271 if (fixed_cause == NULL)
3272 goto raise_error;
3273 Py_DECREF(cause);
3274 }
3275 else if (PyExceptionInstance_Check(cause)) {
3276 fixed_cause = cause;
3277 }
3278 else {
3279 PyErr_SetString(PyExc_TypeError,
3280 "exception causes must derive from "
3281 "BaseException");
3282 goto raise_error;
3283 }
3284 PyException_SetCause(value, fixed_cause);
3285 }
Collin Winter828f04a2007-08-31 00:04:24 +00003286
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003287 PyErr_SetObject(type, value);
3288 /* PyErr_SetObject incref's its arguments */
3289 Py_XDECREF(value);
3290 Py_XDECREF(type);
3291 return WHY_EXCEPTION;
Collin Winter828f04a2007-08-31 00:04:24 +00003292
3293raise_error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003294 Py_XDECREF(value);
3295 Py_XDECREF(type);
3296 Py_XDECREF(cause);
3297 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003298}
3299
Tim Petersd6d010b2001-06-21 02:49:55 +00003300/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00003301 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00003302
Guido van Rossum0368b722007-05-11 16:50:42 +00003303 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
3304 with a variable target.
3305*/
Tim Petersd6d010b2001-06-21 02:49:55 +00003306
Barry Warsawe42b18f1997-08-25 22:13:04 +00003307static int
Guido van Rossum0368b722007-05-11 16:50:42 +00003308unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003309{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003310 int i = 0, j = 0;
3311 Py_ssize_t ll = 0;
3312 PyObject *it; /* iter(v) */
3313 PyObject *w;
3314 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00003315
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003316 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003317
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003318 it = PyObject_GetIter(v);
3319 if (it == NULL)
3320 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003321
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003322 for (; i < argcnt; i++) {
3323 w = PyIter_Next(it);
3324 if (w == NULL) {
3325 /* Iterator done, via error or exhaustion. */
3326 if (!PyErr_Occurred()) {
3327 PyErr_Format(PyExc_ValueError,
3328 "need more than %d value%s to unpack",
3329 i, i == 1 ? "" : "s");
3330 }
3331 goto Error;
3332 }
3333 *--sp = w;
3334 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003335
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003336 if (argcntafter == -1) {
3337 /* We better have exhausted the iterator now. */
3338 w = PyIter_Next(it);
3339 if (w == NULL) {
3340 if (PyErr_Occurred())
3341 goto Error;
3342 Py_DECREF(it);
3343 return 1;
3344 }
3345 Py_DECREF(w);
3346 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3347 goto Error;
3348 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003349
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003350 l = PySequence_List(it);
3351 if (l == NULL)
3352 goto Error;
3353 *--sp = l;
3354 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00003355
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003356 ll = PyList_GET_SIZE(l);
3357 if (ll < argcntafter) {
3358 PyErr_Format(PyExc_ValueError, "need more than %zd values to unpack",
3359 argcnt + ll);
3360 goto Error;
3361 }
Guido van Rossum0368b722007-05-11 16:50:42 +00003362
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003363 /* Pop the "after-variable" args off the list. */
3364 for (j = argcntafter; j > 0; j--, i++) {
3365 *--sp = PyList_GET_ITEM(l, ll - j);
3366 }
3367 /* Resize the list. */
3368 Py_SIZE(l) = ll - argcntafter;
3369 Py_DECREF(it);
3370 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00003371
Tim Petersd6d010b2001-06-21 02:49:55 +00003372Error:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003373 for (; i > 0; i--, sp++)
3374 Py_DECREF(*sp);
3375 Py_XDECREF(it);
3376 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003377}
3378
3379
Guido van Rossum96a42c81992-01-12 02:29:51 +00003380#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00003381static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003382prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003383{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003384 printf("%s ", str);
3385 if (PyObject_Print(v, stdout, 0) != 0)
3386 PyErr_Clear(); /* Don't know what else to do */
3387 printf("\n");
3388 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003389}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003390#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003391
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003392static void
Fred Drake5755ce62001-06-27 19:19:46 +00003393call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003394{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003395 PyObject *type, *value, *traceback, *arg;
3396 int err;
3397 PyErr_Fetch(&type, &value, &traceback);
3398 if (value == NULL) {
3399 value = Py_None;
3400 Py_INCREF(value);
3401 }
3402 arg = PyTuple_Pack(3, type, value, traceback);
3403 if (arg == NULL) {
3404 PyErr_Restore(type, value, traceback);
3405 return;
3406 }
3407 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3408 Py_DECREF(arg);
3409 if (err == 0)
3410 PyErr_Restore(type, value, traceback);
3411 else {
3412 Py_XDECREF(type);
3413 Py_XDECREF(value);
3414 Py_XDECREF(traceback);
3415 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003416}
3417
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00003418static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003419call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003420 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003421{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003422 PyObject *type, *value, *traceback;
3423 int err;
3424 PyErr_Fetch(&type, &value, &traceback);
3425 err = call_trace(func, obj, frame, what, arg);
3426 if (err == 0)
3427 {
3428 PyErr_Restore(type, value, traceback);
3429 return 0;
3430 }
3431 else {
3432 Py_XDECREF(type);
3433 Py_XDECREF(value);
3434 Py_XDECREF(traceback);
3435 return -1;
3436 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003437}
3438
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003439static int
Fred Drake5755ce62001-06-27 19:19:46 +00003440call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003441 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003442{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003443 register PyThreadState *tstate = frame->f_tstate;
3444 int result;
3445 if (tstate->tracing)
3446 return 0;
3447 tstate->tracing++;
3448 tstate->use_tracing = 0;
3449 result = func(obj, frame, what, arg);
3450 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3451 || (tstate->c_profilefunc != NULL));
3452 tstate->tracing--;
3453 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003454}
3455
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003456PyObject *
3457_PyEval_CallTracing(PyObject *func, PyObject *args)
3458{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003459 PyFrameObject *frame = PyEval_GetFrame();
3460 PyThreadState *tstate = frame->f_tstate;
3461 int save_tracing = tstate->tracing;
3462 int save_use_tracing = tstate->use_tracing;
3463 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003464
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003465 tstate->tracing = 0;
3466 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3467 || (tstate->c_profilefunc != NULL));
3468 result = PyObject_Call(func, args, NULL);
3469 tstate->tracing = save_tracing;
3470 tstate->use_tracing = save_use_tracing;
3471 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003472}
3473
Michael W. Hudson006c7522002-11-08 13:08:46 +00003474static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003475maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003476 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3477 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003478{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003479 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003480
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003481 /* If the last instruction executed isn't in the current
3482 instruction window, reset the window. If the last
3483 instruction happens to fall at the start of a line or if it
3484 represents a jump backwards, call the trace function.
3485 */
3486 if ((frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub)) {
3487 int line;
3488 PyAddrPair bounds;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003489
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003490 line = PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3491 &bounds);
3492 if (line >= 0) {
3493 frame->f_lineno = line;
3494 result = call_trace(func, obj, frame,
3495 PyTrace_LINE, Py_None);
3496 }
3497 *instr_lb = bounds.ap_lower;
3498 *instr_ub = bounds.ap_upper;
3499 }
3500 else if (frame->f_lasti <= *instr_prev) {
3501 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3502 }
3503 *instr_prev = frame->f_lasti;
3504 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003505}
3506
Fred Drake5755ce62001-06-27 19:19:46 +00003507void
3508PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003509{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003510 PyThreadState *tstate = PyThreadState_GET();
3511 PyObject *temp = tstate->c_profileobj;
3512 Py_XINCREF(arg);
3513 tstate->c_profilefunc = NULL;
3514 tstate->c_profileobj = NULL;
3515 /* Must make sure that tracing is not ignored if 'temp' is freed */
3516 tstate->use_tracing = tstate->c_tracefunc != NULL;
3517 Py_XDECREF(temp);
3518 tstate->c_profilefunc = func;
3519 tstate->c_profileobj = arg;
3520 /* Flag that tracing or profiling is turned on */
3521 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003522}
3523
3524void
3525PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3526{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003527 PyThreadState *tstate = PyThreadState_GET();
3528 PyObject *temp = tstate->c_traceobj;
3529 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3530 Py_XINCREF(arg);
3531 tstate->c_tracefunc = NULL;
3532 tstate->c_traceobj = NULL;
3533 /* Must make sure that profiling is not ignored if 'temp' is freed */
3534 tstate->use_tracing = tstate->c_profilefunc != NULL;
3535 Py_XDECREF(temp);
3536 tstate->c_tracefunc = func;
3537 tstate->c_traceobj = arg;
3538 /* Flag that tracing or profiling is turned on */
3539 tstate->use_tracing = ((func != NULL)
3540 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003541}
3542
Guido van Rossumb209a111997-04-29 18:18:01 +00003543PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003544PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003545{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003546 PyFrameObject *current_frame = PyEval_GetFrame();
3547 if (current_frame == NULL)
3548 return PyThreadState_GET()->interp->builtins;
3549 else
3550 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003551}
3552
Guido van Rossumb209a111997-04-29 18:18:01 +00003553PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003554PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003555{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003556 PyFrameObject *current_frame = PyEval_GetFrame();
3557 if (current_frame == NULL)
3558 return NULL;
3559 PyFrame_FastToLocals(current_frame);
3560 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003561}
3562
Guido van Rossumb209a111997-04-29 18:18:01 +00003563PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003564PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003565{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003566 PyFrameObject *current_frame = PyEval_GetFrame();
3567 if (current_frame == NULL)
3568 return NULL;
3569 else
3570 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003571}
3572
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003573PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003574PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003575{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003576 PyThreadState *tstate = PyThreadState_GET();
3577 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003578}
3579
Guido van Rossum6135a871995-01-09 17:53:26 +00003580int
Tim Peters5ba58662001-07-16 02:29:45 +00003581PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003582{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003583 PyFrameObject *current_frame = PyEval_GetFrame();
3584 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003585
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003586 if (current_frame != NULL) {
3587 const int codeflags = current_frame->f_code->co_flags;
3588 const int compilerflags = codeflags & PyCF_MASK;
3589 if (compilerflags) {
3590 result = 1;
3591 cf->cf_flags |= compilerflags;
3592 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003593#if 0 /* future keyword */
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003594 if (codeflags & CO_GENERATOR_ALLOWED) {
3595 result = 1;
3596 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3597 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003598#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003599 }
3600 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003601}
3602
Guido van Rossum3f5da241990-12-20 15:06:42 +00003603
Guido van Rossum681d79a1995-07-18 14:51:37 +00003604/* External interface to call any callable object.
3605 The arg must be a tuple or NULL. */
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003606
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003607#undef PyEval_CallObject
3608/* for backward compatibility: export this interface */
3609
Guido van Rossumb209a111997-04-29 18:18:01 +00003610PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003611PyEval_CallObject(PyObject *func, PyObject *arg)
Guido van Rossum83bf35c1991-07-27 21:32:34 +00003612{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003613 return PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003614}
Guido van Rossumd7ed6831997-08-30 15:02:50 +00003615#define PyEval_CallObject(func,arg) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003616 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossume59214e1994-08-30 08:01:59 +00003617
Guido van Rossumb209a111997-04-29 18:18:01 +00003618PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003619PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003620{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003621 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003622
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003623 if (arg == NULL) {
3624 arg = PyTuple_New(0);
3625 if (arg == NULL)
3626 return NULL;
3627 }
3628 else if (!PyTuple_Check(arg)) {
3629 PyErr_SetString(PyExc_TypeError,
3630 "argument list must be a tuple");
3631 return NULL;
3632 }
3633 else
3634 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003635
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003636 if (kw != NULL && !PyDict_Check(kw)) {
3637 PyErr_SetString(PyExc_TypeError,
3638 "keyword list must be a dictionary");
3639 Py_DECREF(arg);
3640 return NULL;
3641 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003642
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003643 result = PyObject_Call(func, arg, kw);
3644 Py_DECREF(arg);
3645 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003646}
3647
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003648const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003649PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003650{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003651 if (PyMethod_Check(func))
3652 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3653 else if (PyFunction_Check(func))
3654 return _PyUnicode_AsString(((PyFunctionObject*)func)->func_name);
3655 else if (PyCFunction_Check(func))
3656 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3657 else
3658 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00003659}
3660
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003661const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003662PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003663{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003664 if (PyMethod_Check(func))
3665 return "()";
3666 else if (PyFunction_Check(func))
3667 return "()";
3668 else if (PyCFunction_Check(func))
3669 return "()";
3670 else
3671 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00003672}
3673
Neal Norwitzaddfe0c2002-11-10 14:33:26 +00003674static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003675err_args(PyObject *func, int flags, int nargs)
3676{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003677 if (flags & METH_NOARGS)
3678 PyErr_Format(PyExc_TypeError,
3679 "%.200s() takes no arguments (%d given)",
3680 ((PyCFunctionObject *)func)->m_ml->ml_name,
3681 nargs);
3682 else
3683 PyErr_Format(PyExc_TypeError,
3684 "%.200s() takes exactly one argument (%d given)",
3685 ((PyCFunctionObject *)func)->m_ml->ml_name,
3686 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003687}
3688
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003689#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003690if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003691 if (call_trace(tstate->c_profilefunc, \
3692 tstate->c_profileobj, \
3693 tstate->frame, PyTrace_C_CALL, \
3694 func)) { \
3695 x = NULL; \
3696 } \
3697 else { \
3698 x = call; \
3699 if (tstate->c_profilefunc != NULL) { \
3700 if (x == NULL) { \
3701 call_trace_protected(tstate->c_profilefunc, \
3702 tstate->c_profileobj, \
3703 tstate->frame, PyTrace_C_EXCEPTION, \
3704 func); \
3705 /* XXX should pass (type, value, tb) */ \
3706 } else { \
3707 if (call_trace(tstate->c_profilefunc, \
3708 tstate->c_profileobj, \
3709 tstate->frame, PyTrace_C_RETURN, \
3710 func)) { \
3711 Py_DECREF(x); \
3712 x = NULL; \
3713 } \
3714 } \
3715 } \
3716 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003717} else { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003718 x = call; \
3719 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003720
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003721static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003722call_function(PyObject ***pp_stack, int oparg
3723#ifdef WITH_TSC
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003724 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003725#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003726 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003727{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003728 int na = oparg & 0xff;
3729 int nk = (oparg>>8) & 0xff;
3730 int n = na + 2 * nk;
3731 PyObject **pfunc = (*pp_stack) - n - 1;
3732 PyObject *func = *pfunc;
3733 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003734
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003735 /* Always dispatch PyCFunction first, because these are
3736 presumed to be the most frequent callable object.
3737 */
3738 if (PyCFunction_Check(func) && nk == 0) {
3739 int flags = PyCFunction_GET_FLAGS(func);
3740 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003741
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003742 PCALL(PCALL_CFUNCTION);
3743 if (flags & (METH_NOARGS | METH_O)) {
3744 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
3745 PyObject *self = PyCFunction_GET_SELF(func);
3746 if (flags & METH_NOARGS && na == 0) {
3747 C_TRACE(x, (*meth)(self,NULL));
3748 }
3749 else if (flags & METH_O && na == 1) {
3750 PyObject *arg = EXT_POP(*pp_stack);
3751 C_TRACE(x, (*meth)(self,arg));
3752 Py_DECREF(arg);
3753 }
3754 else {
3755 err_args(func, flags, na);
3756 x = NULL;
3757 }
3758 }
3759 else {
3760 PyObject *callargs;
3761 callargs = load_args(pp_stack, na);
3762 READ_TIMESTAMP(*pintr0);
3763 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
3764 READ_TIMESTAMP(*pintr1);
3765 Py_XDECREF(callargs);
3766 }
3767 } else {
3768 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
3769 /* optimize access to bound methods */
3770 PyObject *self = PyMethod_GET_SELF(func);
3771 PCALL(PCALL_METHOD);
3772 PCALL(PCALL_BOUND_METHOD);
3773 Py_INCREF(self);
3774 func = PyMethod_GET_FUNCTION(func);
3775 Py_INCREF(func);
3776 Py_DECREF(*pfunc);
3777 *pfunc = self;
3778 na++;
3779 n++;
3780 } else
3781 Py_INCREF(func);
3782 READ_TIMESTAMP(*pintr0);
3783 if (PyFunction_Check(func))
3784 x = fast_function(func, pp_stack, n, na, nk);
3785 else
3786 x = do_call(func, pp_stack, na, nk);
3787 READ_TIMESTAMP(*pintr1);
3788 Py_DECREF(func);
3789 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00003790
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003791 /* Clear the stack of the function object. Also removes
3792 the arguments in case they weren't consumed already
3793 (fast_function() and err_args() leave them on the stack).
3794 */
3795 while ((*pp_stack) > pfunc) {
3796 w = EXT_POP(*pp_stack);
3797 Py_DECREF(w);
3798 PCALL(PCALL_POP);
3799 }
3800 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003801}
3802
Jeremy Hylton192690e2002-08-16 18:36:11 +00003803/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00003804 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00003805 For the simplest case -- a function that takes only positional
3806 arguments and is called with only positional arguments -- it
3807 inlines the most primitive frame setup code from
3808 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
3809 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00003810*/
3811
3812static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00003813fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00003814{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003815 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
3816 PyObject *globals = PyFunction_GET_GLOBALS(func);
3817 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
3818 PyObject *kwdefs = PyFunction_GET_KW_DEFAULTS(func);
3819 PyObject **d = NULL;
3820 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00003821
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003822 PCALL(PCALL_FUNCTION);
3823 PCALL(PCALL_FAST_FUNCTION);
3824 if (argdefs == NULL && co->co_argcount == n &&
3825 co->co_kwonlyargcount == 0 && nk==0 &&
3826 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
3827 PyFrameObject *f;
3828 PyObject *retval = NULL;
3829 PyThreadState *tstate = PyThreadState_GET();
3830 PyObject **fastlocals, **stack;
3831 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003832
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003833 PCALL(PCALL_FASTER_FUNCTION);
3834 assert(globals != NULL);
3835 /* XXX Perhaps we should create a specialized
3836 PyFrame_New() that doesn't take locals, but does
3837 take builtins without sanity checking them.
3838 */
3839 assert(tstate != NULL);
3840 f = PyFrame_New(tstate, co, globals, NULL);
3841 if (f == NULL)
3842 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003843
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003844 fastlocals = f->f_localsplus;
3845 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003846
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003847 for (i = 0; i < n; i++) {
3848 Py_INCREF(*stack);
3849 fastlocals[i] = *stack++;
3850 }
3851 retval = PyEval_EvalFrameEx(f,0);
3852 ++tstate->recursion_depth;
3853 Py_DECREF(f);
3854 --tstate->recursion_depth;
3855 return retval;
3856 }
3857 if (argdefs != NULL) {
3858 d = &PyTuple_GET_ITEM(argdefs, 0);
3859 nd = Py_SIZE(argdefs);
3860 }
3861 return PyEval_EvalCodeEx(co, globals,
3862 (PyObject *)NULL, (*pp_stack)-n, na,
3863 (*pp_stack)-2*nk, nk, d, nd, kwdefs,
3864 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00003865}
3866
3867static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00003868update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
3869 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00003870{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003871 PyObject *kwdict = NULL;
3872 if (orig_kwdict == NULL)
3873 kwdict = PyDict_New();
3874 else {
3875 kwdict = PyDict_Copy(orig_kwdict);
3876 Py_DECREF(orig_kwdict);
3877 }
3878 if (kwdict == NULL)
3879 return NULL;
3880 while (--nk >= 0) {
3881 int err;
3882 PyObject *value = EXT_POP(*pp_stack);
3883 PyObject *key = EXT_POP(*pp_stack);
3884 if (PyDict_GetItem(kwdict, key) != NULL) {
3885 PyErr_Format(PyExc_TypeError,
3886 "%.200s%s got multiple values "
3887 "for keyword argument '%U'",
3888 PyEval_GetFuncName(func),
3889 PyEval_GetFuncDesc(func),
3890 key);
3891 Py_DECREF(key);
3892 Py_DECREF(value);
3893 Py_DECREF(kwdict);
3894 return NULL;
3895 }
3896 err = PyDict_SetItem(kwdict, key, value);
3897 Py_DECREF(key);
3898 Py_DECREF(value);
3899 if (err) {
3900 Py_DECREF(kwdict);
3901 return NULL;
3902 }
3903 }
3904 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00003905}
3906
3907static PyObject *
3908update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003909 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00003910{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003911 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00003912
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003913 callargs = PyTuple_New(nstack + nstar);
3914 if (callargs == NULL) {
3915 return NULL;
3916 }
3917 if (nstar) {
3918 int i;
3919 for (i = 0; i < nstar; i++) {
3920 PyObject *a = PyTuple_GET_ITEM(stararg, i);
3921 Py_INCREF(a);
3922 PyTuple_SET_ITEM(callargs, nstack + i, a);
3923 }
3924 }
3925 while (--nstack >= 0) {
3926 w = EXT_POP(*pp_stack);
3927 PyTuple_SET_ITEM(callargs, nstack, w);
3928 }
3929 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00003930}
3931
3932static PyObject *
3933load_args(PyObject ***pp_stack, int na)
3934{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003935 PyObject *args = PyTuple_New(na);
3936 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00003937
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003938 if (args == NULL)
3939 return NULL;
3940 while (--na >= 0) {
3941 w = EXT_POP(*pp_stack);
3942 PyTuple_SET_ITEM(args, na, w);
3943 }
3944 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00003945}
3946
3947static PyObject *
3948do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
3949{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003950 PyObject *callargs = NULL;
3951 PyObject *kwdict = NULL;
3952 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00003953
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003954 if (nk > 0) {
3955 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
3956 if (kwdict == NULL)
3957 goto call_fail;
3958 }
3959 callargs = load_args(pp_stack, na);
3960 if (callargs == NULL)
3961 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00003962#ifdef CALL_PROFILE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003963 /* At this point, we have to look at the type of func to
3964 update the call stats properly. Do it here so as to avoid
3965 exposing the call stats machinery outside ceval.c
3966 */
3967 if (PyFunction_Check(func))
3968 PCALL(PCALL_FUNCTION);
3969 else if (PyMethod_Check(func))
3970 PCALL(PCALL_METHOD);
3971 else if (PyType_Check(func))
3972 PCALL(PCALL_TYPE);
3973 else if (PyCFunction_Check(func))
3974 PCALL(PCALL_CFUNCTION);
3975 else
3976 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003977#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003978 if (PyCFunction_Check(func)) {
3979 PyThreadState *tstate = PyThreadState_GET();
3980 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
3981 }
3982 else
3983 result = PyObject_Call(func, callargs, kwdict);
Thomas Wouters7ce29ca2007-09-19 21:56:32 +00003984call_fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003985 Py_XDECREF(callargs);
3986 Py_XDECREF(kwdict);
3987 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003988}
3989
3990static PyObject *
3991ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
3992{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003993 int nstar = 0;
3994 PyObject *callargs = NULL;
3995 PyObject *stararg = NULL;
3996 PyObject *kwdict = NULL;
3997 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00003998
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00003999 if (flags & CALL_FLAG_KW) {
4000 kwdict = EXT_POP(*pp_stack);
4001 if (!PyDict_Check(kwdict)) {
4002 PyObject *d;
4003 d = PyDict_New();
4004 if (d == NULL)
4005 goto ext_call_fail;
4006 if (PyDict_Update(d, kwdict) != 0) {
4007 Py_DECREF(d);
4008 /* PyDict_Update raises attribute
4009 * error (percolated from an attempt
4010 * to get 'keys' attribute) instead of
4011 * a type error if its second argument
4012 * is not a mapping.
4013 */
4014 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4015 PyErr_Format(PyExc_TypeError,
4016 "%.200s%.200s argument after ** "
4017 "must be a mapping, not %.200s",
4018 PyEval_GetFuncName(func),
4019 PyEval_GetFuncDesc(func),
4020 kwdict->ob_type->tp_name);
4021 }
4022 goto ext_call_fail;
4023 }
4024 Py_DECREF(kwdict);
4025 kwdict = d;
4026 }
4027 }
4028 if (flags & CALL_FLAG_VAR) {
4029 stararg = EXT_POP(*pp_stack);
4030 if (!PyTuple_Check(stararg)) {
4031 PyObject *t = NULL;
4032 t = PySequence_Tuple(stararg);
4033 if (t == NULL) {
4034 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4035 PyErr_Format(PyExc_TypeError,
4036 "%.200s%.200s argument after * "
4037 "must be a sequence, not %200s",
4038 PyEval_GetFuncName(func),
4039 PyEval_GetFuncDesc(func),
4040 stararg->ob_type->tp_name);
4041 }
4042 goto ext_call_fail;
4043 }
4044 Py_DECREF(stararg);
4045 stararg = t;
4046 }
4047 nstar = PyTuple_GET_SIZE(stararg);
4048 }
4049 if (nk > 0) {
4050 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4051 if (kwdict == NULL)
4052 goto ext_call_fail;
4053 }
4054 callargs = update_star_args(na, nstar, stararg, pp_stack);
4055 if (callargs == NULL)
4056 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004057#ifdef CALL_PROFILE
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004058 /* At this point, we have to look at the type of func to
4059 update the call stats properly. Do it here so as to avoid
4060 exposing the call stats machinery outside ceval.c
4061 */
4062 if (PyFunction_Check(func))
4063 PCALL(PCALL_FUNCTION);
4064 else if (PyMethod_Check(func))
4065 PCALL(PCALL_METHOD);
4066 else if (PyType_Check(func))
4067 PCALL(PCALL_TYPE);
4068 else if (PyCFunction_Check(func))
4069 PCALL(PCALL_CFUNCTION);
4070 else
4071 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004072#endif
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004073 if (PyCFunction_Check(func)) {
4074 PyThreadState *tstate = PyThreadState_GET();
4075 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4076 }
4077 else
4078 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersce272b62007-09-19 21:19:28 +00004079ext_call_fail:
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004080 Py_XDECREF(callargs);
4081 Py_XDECREF(kwdict);
4082 Py_XDECREF(stararg);
4083 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004084}
4085
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004086/* Extract a slice index from a PyInt or PyLong or an object with the
4087 nb_index slot defined, and store in *pi.
4088 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4089 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 +00004090 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004091*/
Tim Petersb5196382001-12-16 19:44:20 +00004092/* Note: If v is NULL, return success without storing into *pi. This
4093 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4094 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004095*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004096int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004097_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004098{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004099 if (v != NULL) {
4100 Py_ssize_t x;
4101 if (PyIndex_Check(v)) {
4102 x = PyNumber_AsSsize_t(v, NULL);
4103 if (x == -1 && PyErr_Occurred())
4104 return 0;
4105 }
4106 else {
4107 PyErr_SetString(PyExc_TypeError,
4108 "slice indices must be integers or "
4109 "None or have an __index__ method");
4110 return 0;
4111 }
4112 *pi = x;
4113 }
4114 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004115}
4116
Guido van Rossum486364b2007-06-30 05:01:58 +00004117#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004118 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004119
Guido van Rossumb209a111997-04-29 18:18:01 +00004120static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004121cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004122{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004123 int res = 0;
4124 switch (op) {
4125 case PyCmp_IS:
4126 res = (v == w);
4127 break;
4128 case PyCmp_IS_NOT:
4129 res = (v != w);
4130 break;
4131 case PyCmp_IN:
4132 res = PySequence_Contains(w, v);
4133 if (res < 0)
4134 return NULL;
4135 break;
4136 case PyCmp_NOT_IN:
4137 res = PySequence_Contains(w, v);
4138 if (res < 0)
4139 return NULL;
4140 res = !res;
4141 break;
4142 case PyCmp_EXC_MATCH:
4143 if (PyTuple_Check(w)) {
4144 Py_ssize_t i, length;
4145 length = PyTuple_Size(w);
4146 for (i = 0; i < length; i += 1) {
4147 PyObject *exc = PyTuple_GET_ITEM(w, i);
4148 if (!PyExceptionClass_Check(exc)) {
4149 PyErr_SetString(PyExc_TypeError,
4150 CANNOT_CATCH_MSG);
4151 return NULL;
4152 }
4153 }
4154 }
4155 else {
4156 if (!PyExceptionClass_Check(w)) {
4157 PyErr_SetString(PyExc_TypeError,
4158 CANNOT_CATCH_MSG);
4159 return NULL;
4160 }
4161 }
4162 res = PyErr_GivenExceptionMatches(v, w);
4163 break;
4164 default:
4165 return PyObject_RichCompare(v, w, op);
4166 }
4167 v = res ? Py_True : Py_False;
4168 Py_INCREF(v);
4169 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004170}
4171
Thomas Wouters52152252000-08-17 22:55:00 +00004172static PyObject *
4173import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004174{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004175 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004176
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004177 x = PyObject_GetAttr(v, name);
4178 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4179 PyErr_Format(PyExc_ImportError, "cannot import name %S", name);
4180 }
4181 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004182}
Guido van Rossumac7be682001-01-17 15:42:30 +00004183
Thomas Wouters52152252000-08-17 22:55:00 +00004184static int
4185import_all_from(PyObject *locals, PyObject *v)
4186{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004187 PyObject *all = PyObject_GetAttrString(v, "__all__");
4188 PyObject *dict, *name, *value;
4189 int skip_leading_underscores = 0;
4190 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004191
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004192 if (all == NULL) {
4193 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4194 return -1; /* Unexpected error */
4195 PyErr_Clear();
4196 dict = PyObject_GetAttrString(v, "__dict__");
4197 if (dict == NULL) {
4198 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4199 return -1;
4200 PyErr_SetString(PyExc_ImportError,
4201 "from-import-* object has no __dict__ and no __all__");
4202 return -1;
4203 }
4204 all = PyMapping_Keys(dict);
4205 Py_DECREF(dict);
4206 if (all == NULL)
4207 return -1;
4208 skip_leading_underscores = 1;
4209 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004210
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004211 for (pos = 0, err = 0; ; pos++) {
4212 name = PySequence_GetItem(all, pos);
4213 if (name == NULL) {
4214 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4215 err = -1;
4216 else
4217 PyErr_Clear();
4218 break;
4219 }
4220 if (skip_leading_underscores &&
4221 PyUnicode_Check(name) &&
4222 PyUnicode_AS_UNICODE(name)[0] == '_')
4223 {
4224 Py_DECREF(name);
4225 continue;
4226 }
4227 value = PyObject_GetAttr(v, name);
4228 if (value == NULL)
4229 err = -1;
4230 else if (PyDict_CheckExact(locals))
4231 err = PyDict_SetItem(locals, name, value);
4232 else
4233 err = PyObject_SetItem(locals, name, value);
4234 Py_DECREF(name);
4235 Py_XDECREF(value);
4236 if (err != 0)
4237 break;
4238 }
4239 Py_DECREF(all);
4240 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004241}
4242
Guido van Rossumac7be682001-01-17 15:42:30 +00004243static void
Neal Norwitzda059e32007-08-26 05:33:45 +00004244format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00004245{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004246 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004247
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004248 if (!obj)
4249 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004250
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004251 obj_str = _PyUnicode_AsString(obj);
4252 if (!obj_str)
4253 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004254
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004255 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004256}
Guido van Rossum950361c1997-01-24 13:49:28 +00004257
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004258static PyObject *
Guido van Rossum98297ee2007-11-06 21:34:58 +00004259unicode_concatenate(PyObject *v, PyObject *w,
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004260 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004261{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004262 /* This function implements 'variable += expr' when both arguments
4263 are (Unicode) strings. */
4264 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4265 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4266 Py_ssize_t new_len = v_len + w_len;
4267 if (new_len < 0) {
4268 PyErr_SetString(PyExc_OverflowError,
4269 "strings are too large to concat");
4270 return NULL;
4271 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00004272
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004273 if (v->ob_refcnt == 2) {
4274 /* In the common case, there are 2 references to the value
4275 * stored in 'variable' when the += is performed: one on the
4276 * value stack (in 'v') and one still stored in the
4277 * 'variable'. We try to delete the variable now to reduce
4278 * the refcnt to 1.
4279 */
4280 switch (*next_instr) {
4281 case STORE_FAST:
4282 {
4283 int oparg = PEEKARG();
4284 PyObject **fastlocals = f->f_localsplus;
4285 if (GETLOCAL(oparg) == v)
4286 SETLOCAL(oparg, NULL);
4287 break;
4288 }
4289 case STORE_DEREF:
4290 {
4291 PyObject **freevars = (f->f_localsplus +
4292 f->f_code->co_nlocals);
4293 PyObject *c = freevars[PEEKARG()];
4294 if (PyCell_GET(c) == v)
4295 PyCell_Set(c, NULL);
4296 break;
4297 }
4298 case STORE_NAME:
4299 {
4300 PyObject *names = f->f_code->co_names;
4301 PyObject *name = GETITEM(names, PEEKARG());
4302 PyObject *locals = f->f_locals;
4303 if (PyDict_CheckExact(locals) &&
4304 PyDict_GetItem(locals, name) == v) {
4305 if (PyDict_DelItem(locals, name) != 0) {
4306 PyErr_Clear();
4307 }
4308 }
4309 break;
4310 }
4311 }
4312 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004313
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004314 if (v->ob_refcnt == 1 && !PyUnicode_CHECK_INTERNED(v)) {
4315 /* Now we own the last reference to 'v', so we can resize it
4316 * in-place.
4317 */
4318 if (PyUnicode_Resize(&v, new_len) != 0) {
4319 /* XXX if PyUnicode_Resize() fails, 'v' has been
4320 * deallocated so it cannot be put back into
4321 * 'variable'. The MemoryError is raised when there
4322 * is no value in 'variable', which might (very
4323 * remotely) be a cause of incompatibilities.
4324 */
4325 return NULL;
4326 }
4327 /* copy 'w' into the newly allocated area of 'v' */
4328 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4329 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4330 return v;
4331 }
4332 else {
4333 /* When in-place resizing is not an option. */
4334 w = PyUnicode_Concat(v, w);
4335 Py_DECREF(v);
4336 return w;
4337 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004338}
4339
Guido van Rossum950361c1997-01-24 13:49:28 +00004340#ifdef DYNAMIC_EXECUTION_PROFILE
4341
Skip Montanarof118cb12001-10-15 20:51:38 +00004342static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004343getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004344{
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004345 int i;
4346 PyObject *l = PyList_New(256);
4347 if (l == NULL) return NULL;
4348 for (i = 0; i < 256; i++) {
4349 PyObject *x = PyLong_FromLong(a[i]);
4350 if (x == NULL) {
4351 Py_DECREF(l);
4352 return NULL;
4353 }
4354 PyList_SetItem(l, i, x);
4355 }
4356 for (i = 0; i < 256; i++)
4357 a[i] = 0;
4358 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004359}
4360
4361PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004362_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004363{
4364#ifndef DXPAIRS
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004365 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004366#else
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +00004367 int i;
4368 PyObject *l = PyList_New(257);
4369 if (l == NULL) return NULL;
4370 for (i = 0; i < 257; i++) {
4371 PyObject *x = getarray(dxpairs[i]);
4372 if (x == NULL) {
4373 Py_DECREF(l);
4374 return NULL;
4375 }
4376 PyList_SetItem(l, i, x);
4377 }
4378 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004379#endif
4380}
4381
4382#endif