blob: fa6bb60517ba112cc554ed9a2bbf24e72988402a [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
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#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
Tim Peters7df5e7f2006-05-26 23:14:37 +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
Ezio Melottic2077b02011-03-16 12:34:31 +020030/* PowerPC support.
David Malcolm4c29e1c2011-01-06 17:39:24 +000031 "__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
Fredrik Lundh7a830892006-05-27 10:39:48 +000038static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000039ppc_getcounter(uint64 *v)
40{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000042
43 loop:
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 Pitrouc83ea132010-05-09 14:46:46 +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 Dickinson504a1512009-10-31 10:11:28 +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 Dickinson504a1512009-10-31 10:11:28 +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
Victor Stinner2b565bb2014-12-12 13:19:00 +010069#define READ_TIMESTAMP(val) do { \
70 unsigned int h, l; \
71 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
72 (val) = ((uint64)l) | (((uint64)h) << 32); \
73 } while(0)
Mark Dickinson504a1512009-10-31 10:11:28 +000074
75
76#else
77
78#error "Don't know how to implement timestamp counter for this architecture"
79
Michael W. Hudson800ba232004-08-12 18:19:17 +000080#endif
81
Tim Peters7df5e7f2006-05-26 23:14:37 +000082void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000084{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 uint64 intr, inst, loop;
86 PyThreadState *tstate = PyThreadState_Get();
87 if (!tstate->interp->tscdump)
88 return;
89 intr = intr1 - intr0;
90 inst = inst1 - inst0 - intr;
91 loop = loop1 - loop0 - intr;
92 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krah7ff78252010-06-23 18:12:09 +000093 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000094}
Michael W. Hudson800ba232004-08-12 18:19:17 +000095
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000096#endif
97
Guido van Rossum04691fc1992-08-12 15:35:34 +000098/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000099/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +0000100
Guido van Rossum408027e1996-12-30 16:17:54 +0000101#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000102/* For debugging the interpreter: */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000103#define LLTRACE 1 /* Low-level trace feature */
104#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000105#endif
106
Jeremy Hylton52820442001-01-03 23:52:36 +0000107typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000108
Guido van Rossum374a9221991-04-04 10:40:29 +0000109/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000110#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +0000111static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000112#else
Fredrik Lundh7a830892006-05-27 10:39:48 +0000113static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000114#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000115static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
116static PyObject * do_call(PyObject *, PyObject ***, int, int);
117static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +0000118static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000120static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
121static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000122#define CALL_FLAG_VAR 1
123#define CALL_FLAG_KW 2
124
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000126static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000127static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000128#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000129static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000130 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000131static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000132 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000133static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
134static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000135 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000136
Fredrik Lundh7a830892006-05-27 10:39:48 +0000137static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
138static int assign_slice(PyObject *, PyObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000139 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000140static PyObject * cmp_outcome(int, PyObject *, PyObject *);
141static PyObject * import_from(PyObject *, PyObject *);
142static int import_all_from(PyObject *, PyObject *);
143static PyObject * build_class(PyObject *, PyObject *, PyObject *);
144static int exec_statement(PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000145 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000146static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
147static void reset_exc_info(PyThreadState *);
148static void format_exc_check_arg(PyObject *, char *, PyObject *);
149static PyObject * string_concatenate(PyObject *, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000150 PyFrameObject *, unsigned char *);
Benjamin Petersone18ef192009-01-20 14:21:16 +0000151static PyObject * kwd_as_string(PyObject *);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000152static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000153
Paul Prescode68140d2000-08-30 20:25:01 +0000154#define NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000155 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000156#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000158#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000160#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 "free variable '%.200s' referenced before assignment" \
162 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000163
Guido van Rossum950361c1997-01-24 13:49:28 +0000164/* Dynamic execution profile */
165#ifdef DYNAMIC_EXECUTION_PROFILE
166#ifdef DXPAIRS
167static long dxpairs[257][256];
168#define dxp dxpairs[256]
169#else
170static long dxp[256];
171#endif
172#endif
173
Jeremy Hylton985eba52003-02-05 23:13:00 +0000174/* Function call profile */
175#ifdef CALL_PROFILE
176#define PCALL_NUM 11
177static int pcall[PCALL_NUM];
178
179#define PCALL_ALL 0
180#define PCALL_FUNCTION 1
181#define PCALL_FAST_FUNCTION 2
182#define PCALL_FASTER_FUNCTION 3
183#define PCALL_METHOD 4
184#define PCALL_BOUND_METHOD 5
185#define PCALL_CFUNCTION 6
186#define PCALL_TYPE 7
187#define PCALL_GENERATOR 8
188#define PCALL_OTHER 9
189#define PCALL_POP 10
190
191/* Notes about the statistics
192
193 PCALL_FAST stats
194
195 FAST_FUNCTION means no argument tuple needs to be created.
196 FASTER_FUNCTION means that the fast-path frame setup code is used.
197
198 If there is a method call where the call can be optimized by changing
199 the argument tuple and calling the function directly, it gets recorded
200 twice.
201
202 As a result, the relationship among the statistics appears to be
203 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
204 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
205 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
206 PCALL_METHOD > PCALL_BOUND_METHOD
207*/
208
209#define PCALL(POS) pcall[POS]++
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 return Py_BuildValue("iiiiiiiiiii",
215 pcall[0], pcall[1], pcall[2], pcall[3],
216 pcall[4], pcall[5], pcall[6], pcall[7],
217 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000218}
219#else
220#define PCALL(O)
221
222PyObject *
223PyEval_GetCallStats(PyObject *self)
224{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000225 Py_INCREF(Py_None);
226 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000227}
228#endif
229
Tim Peters5ca576e2001-06-18 22:08:13 +0000230
Guido van Rossume59214e1994-08-30 08:01:59 +0000231#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000232
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000233#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000234#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000235#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000236#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000237
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000238static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000239static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000240static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000241
Tim Peters7f468f22004-10-11 02:40:51 +0000242int
243PyEval_ThreadsInitialized(void)
244{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 return interpreter_lock != 0;
Tim Peters7f468f22004-10-11 02:40:51 +0000246}
247
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000250{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 if (interpreter_lock)
252 return;
253 interpreter_lock = PyThread_allocate_lock();
254 PyThread_acquire_lock(interpreter_lock, 1);
255 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000256}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000257
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000261 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000262}
263
264void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000265PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000268}
269
270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000272{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 if (tstate == NULL)
274 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
275 /* Check someone has called PyEval_InitThreads() to create the lock */
276 assert(interpreter_lock);
277 PyThread_acquire_lock(interpreter_lock, 1);
278 if (PyThreadState_Swap(tstate) != NULL)
279 Py_FatalError(
280 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000281}
282
283void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000284PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000285{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 if (tstate == NULL)
287 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
288 if (PyThreadState_Swap(NULL) != tstate)
289 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
290 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000291}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000292
293/* This function is called from PyOS_AfterFork to ensure that newly
294 created child processes don't hold locks referring to threads which
295 are not running in the child process. (This could also be done using
296 pthread_atfork mechanism, at least for the pthreads implementation.) */
297
298void
299PyEval_ReInitThreads(void)
300{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PyObject *threading, *result;
302 PyThreadState *tstate;
Jesse Noller5e62ca42008-07-16 20:03:47 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 if (!interpreter_lock)
305 return;
306 /*XXX Can't use PyThread_free_lock here because it does too
307 much error-checking. Doing this cleanly would require
308 adding a new function to each thread_*.h. Instead, just
309 create a new lock and waste a little bit of memory */
310 interpreter_lock = PyThread_allocate_lock();
311 pending_lock = PyThread_allocate_lock();
312 PyThread_acquire_lock(interpreter_lock, 1);
313 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000314
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000315 /* Update the threading module with the new state.
316 */
317 tstate = PyThreadState_GET();
318 threading = PyMapping_GetItemString(tstate->interp->modules,
319 "threading");
320 if (threading == NULL) {
321 /* threading not imported */
322 PyErr_Clear();
323 return;
324 }
325 result = PyObject_CallMethod(threading, "_after_fork", NULL);
326 if (result == NULL)
327 PyErr_WriteUnraisable(threading);
328 else
329 Py_DECREF(result);
330 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000331}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000332#endif
333
Guido van Rossumff4949e1992-08-05 19:58:53 +0000334/* Functions save_thread and restore_thread are always defined so
335 dynamically loaded modules needn't be compiled separately for use
336 with and without threads: */
337
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000338PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 PyThreadState *tstate = PyThreadState_Swap(NULL);
342 if (tstate == NULL)
343 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000344#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000345 if (interpreter_lock)
346 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000348 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000349}
350
351void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000352PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000353{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 if (tstate == NULL)
355 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000356#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000357 if (interpreter_lock) {
358 int err = errno;
359 PyThread_acquire_lock(interpreter_lock, 1);
360 errno = err;
361 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000362#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000364}
365
366
Guido van Rossuma9672091994-09-14 13:31:22 +0000367/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
368 signal handlers or Mac I/O completion routines) can schedule calls
369 to a function to be called synchronously.
370 The synchronous function is called with one void* argument.
371 It should return 0 for success or -1 for failure -- failure should
372 be accompanied by an exception.
373
374 If registry succeeds, the registry function returns 0; if it fails
375 (e.g. due to too many pending calls) it returns -1 (without setting
376 an exception condition).
377
378 Note that because registry may occur from within signal handlers,
379 or other asynchronous events, calling malloc() is unsafe!
380
381#ifdef WITH_THREAD
382 Any thread can schedule pending calls, but only the main thread
383 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000384 There is no facility to schedule calls to a particular thread, but
385 that should be easy to change, should that ever be required. In
386 that case, the static variables here should go into the python
387 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000388#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000389*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000390
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000391#ifdef WITH_THREAD
392
393/* The WITH_THREAD implementation is thread-safe. It allows
394 scheduling to be made from any thread, and even from an executing
395 callback.
396 */
397
398#define NPENDINGCALLS 32
399static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000400 int (*func)(void *);
401 void *arg;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000402} pendingcalls[NPENDINGCALLS];
403static int pendingfirst = 0;
404static int pendinglast = 0;
405static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
406static char pendingbusy = 0;
407
408int
409Py_AddPendingCall(int (*func)(void *), void *arg)
410{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000411 int i, j, result=0;
412 PyThread_type_lock lock = pending_lock;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000413
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000414 /* try a few times for the lock. Since this mechanism is used
415 * for signal handling (on the main thread), there is a (slim)
416 * chance that a signal is delivered on the same thread while we
417 * hold the lock during the Py_MakePendingCalls() function.
418 * This avoids a deadlock in that case.
419 * Note that signals can be delivered on any thread. In particular,
420 * on Windows, a SIGINT is delivered on a system-created worker
421 * thread.
422 * We also check for lock being NULL, in the unlikely case that
423 * this function is called before any bytecode evaluation takes place.
424 */
425 if (lock != NULL) {
426 for (i = 0; i<100; i++) {
427 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
428 break;
429 }
430 if (i == 100)
431 return -1;
432 }
433
434 i = pendinglast;
435 j = (i + 1) % NPENDINGCALLS;
436 if (j == pendingfirst) {
437 result = -1; /* Queue full */
438 } else {
439 pendingcalls[i].func = func;
440 pendingcalls[i].arg = arg;
441 pendinglast = j;
442 }
443 /* signal main loop */
444 _Py_Ticker = 0;
445 pendingcalls_to_do = 1;
446 if (lock != NULL)
447 PyThread_release_lock(lock);
448 return result;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000449}
450
451int
452Py_MakePendingCalls(void)
453{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000454 int i;
455 int r = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000456
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 if (!pending_lock) {
458 /* initial allocation of the lock */
459 pending_lock = PyThread_allocate_lock();
460 if (pending_lock == NULL)
461 return -1;
462 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000463
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000464 /* only service pending calls on main thread */
465 if (main_thread && PyThread_get_thread_ident() != main_thread)
466 return 0;
467 /* don't perform recursive pending calls */
468 if (pendingbusy)
469 return 0;
470 pendingbusy = 1;
471 /* perform a bounded number of calls, in case of recursion */
472 for (i=0; i<NPENDINGCALLS; i++) {
473 int j;
474 int (*func)(void *);
475 void *arg = NULL;
476
477 /* pop one item off the queue while holding the lock */
478 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
479 j = pendingfirst;
480 if (j == pendinglast) {
481 func = NULL; /* Queue empty */
482 } else {
483 func = pendingcalls[j].func;
484 arg = pendingcalls[j].arg;
485 pendingfirst = (j + 1) % NPENDINGCALLS;
486 }
487 pendingcalls_to_do = pendingfirst != pendinglast;
488 PyThread_release_lock(pending_lock);
489 /* having released the lock, perform the callback */
490 if (func == NULL)
491 break;
492 r = func(arg);
493 if (r)
494 break;
495 }
496 pendingbusy = 0;
497 return r;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000498}
499
500#else /* if ! defined WITH_THREAD */
501
502/*
503 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
504 This code is used for signal handling in python that isn't built
505 with WITH_THREAD.
506 Don't use this implementation when Py_AddPendingCalls() can happen
507 on a different thread!
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508
Guido van Rossuma9672091994-09-14 13:31:22 +0000509 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000510 (1) nested asynchronous calls to Py_AddPendingCall()
511 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000512
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000513 (1) is very unlikely because typically signal delivery
514 is blocked during signal handling. So it should be impossible.
515 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000516 The current code is safe against (2), but not against (1).
517 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000518 thread is present, interrupted by signals, and that the critical
519 section is protected with the "busy" variable. On Windows, which
520 delivers SIGINT on a system thread, this does not hold and therefore
521 Windows really shouldn't use this version.
522 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000523*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000524
Guido van Rossuma9672091994-09-14 13:31:22 +0000525#define NPENDINGCALLS 32
526static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000527 int (*func)(void *);
528 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000529} pendingcalls[NPENDINGCALLS];
530static volatile int pendingfirst = 0;
531static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000532static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000533
534int
Thomas Wouters334fb892000-07-25 12:56:38 +0000535Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000536{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000537 static volatile int busy = 0;
538 int i, j;
539 /* XXX Begin critical section */
540 if (busy)
541 return -1;
542 busy = 1;
543 i = pendinglast;
544 j = (i + 1) % NPENDINGCALLS;
545 if (j == pendingfirst) {
546 busy = 0;
547 return -1; /* Queue full */
548 }
549 pendingcalls[i].func = func;
550 pendingcalls[i].arg = arg;
551 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000552
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 _Py_Ticker = 0;
554 pendingcalls_to_do = 1; /* Signal main loop */
555 busy = 0;
556 /* XXX End critical section */
557 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000558}
559
Guido van Rossum180d7b41994-09-29 09:45:57 +0000560int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000562{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000563 static int busy = 0;
564 if (busy)
565 return 0;
566 busy = 1;
567 pendingcalls_to_do = 0;
568 for (;;) {
569 int i;
570 int (*func)(void *);
571 void *arg;
572 i = pendingfirst;
573 if (i == pendinglast)
574 break; /* Queue empty */
575 func = pendingcalls[i].func;
576 arg = pendingcalls[i].arg;
577 pendingfirst = (i + 1) % NPENDINGCALLS;
578 if (func(arg) < 0) {
579 busy = 0;
580 pendingcalls_to_do = 1; /* We're not done yet */
581 return -1;
582 }
583 }
584 busy = 0;
585 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000586}
587
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000588#endif /* WITH_THREAD */
589
Guido van Rossuma9672091994-09-14 13:31:22 +0000590
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000591/* The interpreter's recursion limit */
592
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000593#ifndef Py_DEFAULT_RECURSION_LIMIT
594#define Py_DEFAULT_RECURSION_LIMIT 1000
595#endif
596static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
597int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000599int
600Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000601{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000603}
604
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000605void
606Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000607{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 recursion_limit = new_limit;
609 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000610}
611
Armin Rigo2b3eb402003-10-28 12:05:48 +0000612/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
613 if the recursion_depth reaches _Py_CheckRecursionLimit.
614 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
615 to guarantee that _Py_CheckRecursiveCall() is regularly called.
616 Without USE_STACKCHECK, there is no need for this. */
617int
618_Py_CheckRecursiveCall(char *where)
619{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000620 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000621
622#ifdef USE_STACKCHECK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000623 if (PyOS_CheckStack()) {
624 --tstate->recursion_depth;
625 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
626 return -1;
627 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000628#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 if (tstate->recursion_depth > recursion_limit) {
630 --tstate->recursion_depth;
631 PyErr_Format(PyExc_RuntimeError,
632 "maximum recursion depth exceeded%s",
633 where);
634 return -1;
635 }
636 _Py_CheckRecursionLimit = recursion_limit;
637 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000638}
639
Guido van Rossum374a9221991-04-04 10:40:29 +0000640/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000641enum why_code {
Stefan Krah7ff78252010-06-23 18:12:09 +0000642 WHY_NOT = 0x0001, /* No error */
643 WHY_EXCEPTION = 0x0002, /* Exception occurred */
644 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
645 WHY_RETURN = 0x0008, /* 'return' statement */
646 WHY_BREAK = 0x0010, /* 'break' statement */
647 WHY_CONTINUE = 0x0020, /* 'continue' statement */
648 WHY_YIELD = 0x0040 /* 'yield' operator */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000649};
Guido van Rossum374a9221991-04-04 10:40:29 +0000650
Fredrik Lundh7a830892006-05-27 10:39:48 +0000651static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
652static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000653
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +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;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +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 Pitrouc83ea132010-05-09 14:46:46 +0000669 return PyEval_EvalCodeEx(co,
670 globals, locals,
671 (PyObject **)NULL, 0,
672 (PyObject **)NULL, 0,
673 (PyObject **)NULL, 0,
674 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 Pitrouc83ea132010-05-09 14:46:46 +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 *
Anthony Baxtera863d332006-04-11 07:43:46 +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 Pitrouc83ea132010-05-09 14:46:46 +0000692 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000693#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +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 *stream = NULL; /* for PRINT opcodes */
706 register PyObject **fastlocals, **freevars;
707 PyObject *retval = NULL; /* Return value */
708 PyThreadState *tstate = PyThreadState_GET();
709 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000710
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000712
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000713 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000714
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000715 is true when the line being executed has changed. The
716 initial values are such as to make this false the first
717 time it is tested. */
718 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000719
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000720 unsigned char *first_instr;
721 PyObject *names;
722 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000723#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 /* Make it easier to find out where we are with a debugger */
725 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000726#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000727
Neal Norwitza81d2202002-07-14 00:27:26 +0000728/* Tuple access macros */
729
730#ifndef Py_DEBUG
731#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
732#else
733#define GETITEM(v, i) PyTuple_GetItem((v), (i))
734#endif
735
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000736#ifdef WITH_TSC
737/* Use Pentium timestamp counter to mark certain events:
738 inst0 -- beginning of switch statement for opcode dispatch
739 inst1 -- end of switch statement (may be skipped)
740 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000741 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000742 (may be skipped)
743 intr1 -- beginning of long interruption
744 intr2 -- end of long interruption
745
746 Many opcodes call out to helper C functions. In some cases, the
747 time in those functions should be counted towards the time for the
748 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
749 calls another Python function; there's no point in charge all the
750 bytecode executed by the called function to the caller.
751
752 It's hard to make a useful judgement statically. In the presence
753 of operator overloading, it's impossible to tell if a call will
754 execute new Python code or not.
755
756 It's a case-by-case judgement. I'll use intr1 for the following
757 cases:
758
759 EXEC_STMT
760 IMPORT_STAR
761 IMPORT_FROM
762 CALL_FUNCTION (and friends)
763
764 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000765 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
766 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000767
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000768 READ_TIMESTAMP(inst0);
769 READ_TIMESTAMP(inst1);
770 READ_TIMESTAMP(loop0);
771 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000772
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000773 /* shut up the compiler */
774 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000775#endif
776
Guido van Rossum374a9221991-04-04 10:40:29 +0000777/* Code access macros */
778
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000779#define INSTR_OFFSET() ((int)(next_instr - first_instr))
780#define NEXTOP() (*next_instr++)
781#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
782#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
783#define JUMPTO(x) (next_instr = first_instr + (x))
784#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000785
Raymond Hettingerf606f872003-03-16 03:11:04 +0000786/* OpCode prediction macros
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000787 Some opcodes tend to come in pairs thus making it possible to
788 predict the second code when the first is run. For example,
789 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
790 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000791
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000792 Verifying the prediction costs a single high-speed test of a register
793 variable against a constant. If the pairing was good, then the
794 processor's own internal branch predication has a high likelihood of
795 success, resulting in a nearly zero-overhead transition to the
796 next opcode. A successful prediction saves a trip through the eval-loop
797 including its two unpredictable branches, the HAS_ARG test and the
798 switch-case. Combined with the processor's internal branch prediction,
799 a successful PREDICT has the effect of making the two opcodes run as if
800 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000801
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000802 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000803 predictions turned-on and interpret the results as if some opcodes
804 had been combined or turn-off predictions so that the opcode frequency
805 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000806*/
807
Raymond Hettingera7216982004-02-08 19:59:27 +0000808#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000809#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000810#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000811#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000812#endif
813
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000814#define PREDICTED(op) PRED_##op: next_instr++
815#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000816
Guido van Rossum374a9221991-04-04 10:40:29 +0000817/* Stack manipulation macros */
818
Martin v. Löwis18e16552006-02-15 17:27:45 +0000819/* The stack can grow at most MAXINT deep, as co_nlocals and
820 co_stacksize are ints. */
Stefan Krah7ff78252010-06-23 18:12:09 +0000821#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
822#define EMPTY() (STACK_LEVEL() == 0)
823#define TOP() (stack_pointer[-1])
824#define SECOND() (stack_pointer[-2])
825#define THIRD() (stack_pointer[-3])
826#define FOURTH() (stack_pointer[-4])
827#define PEEK(n) (stack_pointer[-(n)])
828#define SET_TOP(v) (stack_pointer[-1] = (v))
829#define SET_SECOND(v) (stack_pointer[-2] = (v))
830#define SET_THIRD(v) (stack_pointer[-3] = (v))
831#define SET_FOURTH(v) (stack_pointer[-4] = (v))
832#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
833#define BASIC_STACKADJ(n) (stack_pointer += n)
834#define BASIC_PUSH(v) (*stack_pointer++ = (v))
835#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000836
Guido van Rossum96a42c81992-01-12 02:29:51 +0000837#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000838#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000839 lltrace && prtrace(TOP(), "push")); \
840 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000842 BASIC_POP())
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000843#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000844 lltrace && prtrace(TOP(), "stackadj")); \
845 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000846#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krah7ff78252010-06-23 18:12:09 +0000847 prtrace((STACK_POINTER)[-1], "ext_pop")), \
848 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000849#else
Stefan Krah7ff78252010-06-23 18:12:09 +0000850#define PUSH(v) BASIC_PUSH(v)
851#define POP() BASIC_POP()
852#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000853#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000854#endif
855
Guido van Rossum681d79a1995-07-18 14:51:37 +0000856/* Local variable macros */
857
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000858#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000859
860/* The SETLOCAL() macro must not DECREF the local variable in-place and
861 then store the new value; it must copy the old value to a temporary
862 value, then store the new value, and then DECREF the temporary value.
863 This is because it is possible that during the DECREF the frame is
864 accessed by other code (e.g. a __del__ method or gc.collect()) and the
865 variable would be pointing to already-freed memory. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000866#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krah7ff78252010-06-23 18:12:09 +0000867 GETLOCAL(i) = value; \
868 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000869
Guido van Rossuma027efa1997-05-05 20:56:21 +0000870/* Start of code */
871
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000872 if (f == NULL)
873 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000874
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 /* push frame */
876 if (Py_EnterRecursiveCall(""))
877 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000878
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000880
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000881 if (tstate->use_tracing) {
882 if (tstate->c_tracefunc != NULL) {
883 /* tstate->c_tracefunc, if defined, is a
884 function that will be called on *every* entry
885 to a code block. Its return value, if not
886 None, is a function that will be called at
887 the start of each executed line of code.
888 (Actually, the function must return itself
889 in order to continue tracing.) The trace
890 functions are called with three arguments:
891 a pointer to the current frame, a string
892 indicating why the function is called, and
893 an argument which depends on the situation.
894 The global trace function is also called
895 whenever an exception is detected. */
896 if (call_trace_protected(tstate->c_tracefunc,
897 tstate->c_traceobj,
898 f, PyTrace_CALL, Py_None)) {
899 /* Trace function raised an error */
900 goto exit_eval_frame;
901 }
902 }
903 if (tstate->c_profilefunc != NULL) {
904 /* Similar for c_profilefunc, except it needn't
905 return itself and isn't called for "line" events */
906 if (call_trace_protected(tstate->c_profilefunc,
907 tstate->c_profileobj,
908 f, PyTrace_CALL, Py_None)) {
909 /* Profile function raised an error */
910 goto exit_eval_frame;
911 }
912 }
913 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000914
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000915 co = f->f_code;
916 names = co->co_names;
917 consts = co->co_consts;
918 fastlocals = f->f_localsplus;
919 freevars = f->f_localsplus + co->co_nlocals;
920 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
921 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000922
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000923 f->f_lasti now refers to the index of the last instruction
924 executed. You might think this was obvious from the name, but
925 this wasn't always true before 2.3! PyFrame_New now sets
926 f->f_lasti to -1 (i.e. the index *before* the first instruction)
927 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
928 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000929
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000930 When the PREDICT() macros are enabled, some opcode pairs follow in
931 direct succession without updating f->f_lasti. A successful
932 prediction effectively links the two codes together as if they
933 were a single new opcode; accordingly,f->f_lasti will point to
934 the first code in the pair (for instance, GET_ITER followed by
935 FOR_ITER is effectively a single opcode and f->f_lasti will point
936 at to the beginning of the combined pair.)
937 */
938 next_instr = first_instr + f->f_lasti + 1;
939 stack_pointer = f->f_stacktop;
940 assert(stack_pointer != NULL);
941 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000942
Tim Peters5ca576e2001-06-18 22:08:13 +0000943#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000944 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000945#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000946#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000947 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000948#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000949
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000950 why = WHY_NOT;
951 err = 0;
952 x = Py_None; /* Not a reference, just anything non-NULL */
953 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000954
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955 if (throwflag) { /* support for generator.throw() */
956 why = WHY_EXCEPTION;
957 goto on_error;
958 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000959
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000960 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000961#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000962 if (inst1 == 0) {
963 /* Almost surely, the opcode executed a break
964 or a continue, preventing inst1 from being set
965 on the way out of the loop.
966 */
967 READ_TIMESTAMP(inst1);
968 loop1 = inst1;
969 }
970 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
971 intr0, intr1);
972 ticked = 0;
973 inst1 = 0;
974 intr0 = 0;
975 intr1 = 0;
976 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000977#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000978 assert(stack_pointer >= f->f_valuestack); /* else underflow */
979 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000980
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000981 /* Do periodic things. Doing this every time through
982 the loop would add too much overhead, so we do it
983 only every Nth instruction. We also do it if
984 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
985 event needs attention (e.g. a signal handler or
986 async I/O handler); see Py_AddPendingCall() and
987 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000988
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000989 if (--_Py_Ticker < 0) {
990 if (*next_instr == SETUP_FINALLY) {
991 /* Make the last opcode before
Ezio Melottic2077b02011-03-16 12:34:31 +0200992 a try: finally: block uninterruptible. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 goto fast_next_opcode;
994 }
995 _Py_Ticker = _Py_CheckInterval;
996 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000997#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000998 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000999#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001000 if (pendingcalls_to_do) {
1001 if (Py_MakePendingCalls() < 0) {
1002 why = WHY_EXCEPTION;
1003 goto on_error;
1004 }
1005 if (pendingcalls_to_do)
1006 /* MakePendingCalls() didn't succeed.
1007 Force early re-execution of this
1008 "periodic" code, possibly after
1009 a thread switch */
1010 _Py_Ticker = 0;
1011 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001012#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001013 if (interpreter_lock) {
1014 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001015
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001016 if (PyThreadState_Swap(NULL) != tstate)
1017 Py_FatalError("ceval: tstate mix-up");
1018 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001021
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001022 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Peterson1c78e6d2014-06-16 22:59:07 -07001023
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 if (PyThreadState_Swap(tstate) != NULL)
1025 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001026
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001027 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001028
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001029 if (tstate->async_exc != NULL) {
1030 x = tstate->async_exc;
1031 tstate->async_exc = NULL;
1032 PyErr_SetNone(x);
1033 Py_DECREF(x);
1034 why = WHY_EXCEPTION;
1035 goto on_error;
1036 }
1037 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001038#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001039 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001040
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001041 fast_next_opcode:
1042 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001043
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001044 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001046 if (_Py_TracingPossible &&
1047 tstate->c_tracefunc != NULL && !tstate->tracing) {
1048 /* see maybe_call_line_trace
1049 for expository comments */
1050 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001052 err = maybe_call_line_trace(tstate->c_tracefunc,
1053 tstate->c_traceobj,
1054 f, &instr_lb, &instr_ub,
1055 &instr_prev);
1056 /* Reload possibly changed frame fields */
1057 JUMPTO(f->f_lasti);
1058 if (f->f_stacktop != NULL) {
1059 stack_pointer = f->f_stacktop;
1060 f->f_stacktop = NULL;
1061 }
1062 if (err) {
1063 /* trace function raised an exception */
1064 goto on_error;
1065 }
1066 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001067
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001068 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001070 opcode = NEXTOP();
1071 oparg = 0; /* allows oparg to be stored in a register because
1072 it doesn't have to be remembered across a full loop */
1073 if (HAS_ARG(opcode))
1074 oparg = NEXTARG();
Stefan Krah7ff78252010-06-23 18:12:09 +00001075 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001076#ifdef DYNAMIC_EXECUTION_PROFILE
1077#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001078 dxpairs[lastopcode][opcode]++;
1079 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001080#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001081 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001082#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001083
Guido van Rossum96a42c81992-01-12 02:29:51 +00001084#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001085 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001086
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001087 if (lltrace) {
1088 if (HAS_ARG(opcode)) {
1089 printf("%d: %d, %d\n",
1090 f->f_lasti, opcode, oparg);
1091 }
1092 else {
1093 printf("%d: %d\n",
1094 f->f_lasti, opcode);
1095 }
1096 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001097#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001098
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001099 /* Main switch on opcode */
1100 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001101
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001102 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001103
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001104 /* BEWARE!
1105 It is essential that any operation that fails sets either
1106 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1107 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001109 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001111 case NOP:
1112 goto fast_next_opcode;
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001113
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001114 case LOAD_FAST:
1115 x = GETLOCAL(oparg);
1116 if (x != NULL) {
1117 Py_INCREF(x);
1118 PUSH(x);
1119 goto fast_next_opcode;
1120 }
1121 format_exc_check_arg(PyExc_UnboundLocalError,
1122 UNBOUNDLOCAL_ERROR_MSG,
1123 PyTuple_GetItem(co->co_varnames, oparg));
1124 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001125
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001126 case LOAD_CONST:
1127 x = GETITEM(consts, oparg);
1128 Py_INCREF(x);
1129 PUSH(x);
1130 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001131
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001132 PREDICTED_WITH_ARG(STORE_FAST);
1133 case STORE_FAST:
1134 v = POP();
1135 SETLOCAL(oparg, v);
1136 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001137
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001138 case POP_TOP:
1139 v = POP();
1140 Py_DECREF(v);
1141 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001142
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001143 case ROT_TWO:
1144 v = TOP();
1145 w = SECOND();
1146 SET_TOP(w);
1147 SET_SECOND(v);
1148 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001149
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001150 case ROT_THREE:
1151 v = TOP();
1152 w = SECOND();
1153 x = THIRD();
1154 SET_TOP(w);
1155 SET_SECOND(x);
1156 SET_THIRD(v);
1157 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001159 case ROT_FOUR:
1160 u = TOP();
1161 v = SECOND();
1162 w = THIRD();
1163 x = FOURTH();
1164 SET_TOP(v);
1165 SET_SECOND(w);
1166 SET_THIRD(x);
1167 SET_FOURTH(u);
1168 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001170 case DUP_TOP:
1171 v = TOP();
1172 Py_INCREF(v);
1173 PUSH(v);
1174 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001175
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001176 case DUP_TOPX:
1177 if (oparg == 2) {
1178 x = TOP();
1179 Py_INCREF(x);
1180 w = SECOND();
1181 Py_INCREF(w);
1182 STACKADJ(2);
1183 SET_TOP(x);
1184 SET_SECOND(w);
1185 goto fast_next_opcode;
1186 } else if (oparg == 3) {
1187 x = TOP();
1188 Py_INCREF(x);
1189 w = SECOND();
1190 Py_INCREF(w);
1191 v = THIRD();
1192 Py_INCREF(v);
1193 STACKADJ(3);
1194 SET_TOP(x);
1195 SET_SECOND(w);
1196 SET_THIRD(v);
1197 goto fast_next_opcode;
1198 }
1199 Py_FatalError("invalid argument to DUP_TOPX"
1200 " (bytecode corruption?)");
1201 /* Never returns, so don't bother to set why. */
1202 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001204 case UNARY_POSITIVE:
1205 v = TOP();
1206 x = PyNumber_Positive(v);
1207 Py_DECREF(v);
1208 SET_TOP(x);
1209 if (x != NULL) continue;
1210 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001212 case UNARY_NEGATIVE:
1213 v = TOP();
1214 x = PyNumber_Negative(v);
1215 Py_DECREF(v);
1216 SET_TOP(x);
1217 if (x != NULL) continue;
1218 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001219
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001220 case UNARY_NOT:
1221 v = TOP();
1222 err = PyObject_IsTrue(v);
1223 Py_DECREF(v);
1224 if (err == 0) {
1225 Py_INCREF(Py_True);
1226 SET_TOP(Py_True);
1227 continue;
1228 }
1229 else if (err > 0) {
1230 Py_INCREF(Py_False);
1231 SET_TOP(Py_False);
1232 err = 0;
1233 continue;
1234 }
1235 STACKADJ(-1);
1236 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001237
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001238 case UNARY_CONVERT:
1239 v = TOP();
1240 x = PyObject_Repr(v);
1241 Py_DECREF(v);
1242 SET_TOP(x);
1243 if (x != NULL) continue;
1244 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001245
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 case UNARY_INVERT:
1247 v = TOP();
1248 x = PyNumber_Invert(v);
1249 Py_DECREF(v);
1250 SET_TOP(x);
1251 if (x != NULL) continue;
1252 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001253
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001254 case BINARY_POWER:
1255 w = POP();
1256 v = TOP();
1257 x = PyNumber_Power(v, w, Py_None);
1258 Py_DECREF(v);
1259 Py_DECREF(w);
1260 SET_TOP(x);
1261 if (x != NULL) continue;
1262 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001264 case BINARY_MULTIPLY:
1265 w = POP();
1266 v = TOP();
1267 x = PyNumber_Multiply(v, w);
1268 Py_DECREF(v);
1269 Py_DECREF(w);
1270 SET_TOP(x);
1271 if (x != NULL) continue;
1272 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001273
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001274 case BINARY_DIVIDE:
1275 if (!_Py_QnewFlag) {
1276 w = POP();
1277 v = TOP();
1278 x = PyNumber_Divide(v, w);
1279 Py_DECREF(v);
1280 Py_DECREF(w);
1281 SET_TOP(x);
1282 if (x != NULL) continue;
1283 break;
1284 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001285 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001286 BINARY_TRUE_DIVIDE */
1287 case BINARY_TRUE_DIVIDE:
1288 w = POP();
1289 v = TOP();
1290 x = PyNumber_TrueDivide(v, w);
1291 Py_DECREF(v);
1292 Py_DECREF(w);
1293 SET_TOP(x);
1294 if (x != NULL) continue;
1295 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001297 case BINARY_FLOOR_DIVIDE:
1298 w = POP();
1299 v = TOP();
1300 x = PyNumber_FloorDivide(v, w);
1301 Py_DECREF(v);
1302 Py_DECREF(w);
1303 SET_TOP(x);
1304 if (x != NULL) continue;
1305 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001306
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001307 case BINARY_MODULO:
1308 w = POP();
1309 v = TOP();
1310 if (PyString_CheckExact(v))
1311 x = PyString_Format(v, w);
1312 else
1313 x = PyNumber_Remainder(v, w);
1314 Py_DECREF(v);
1315 Py_DECREF(w);
1316 SET_TOP(x);
1317 if (x != NULL) continue;
1318 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001319
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001320 case BINARY_ADD:
1321 w = POP();
1322 v = TOP();
1323 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1324 /* INLINE: int + int */
1325 register long a, b, i;
1326 a = PyInt_AS_LONG(v);
1327 b = PyInt_AS_LONG(w);
1328 /* cast to avoid undefined behaviour
1329 on overflow */
1330 i = (long)((unsigned long)a + b);
1331 if ((i^a) < 0 && (i^b) < 0)
1332 goto slow_add;
1333 x = PyInt_FromLong(i);
1334 }
1335 else if (PyString_CheckExact(v) &&
1336 PyString_CheckExact(w)) {
1337 x = string_concatenate(v, w, f, next_instr);
1338 /* string_concatenate consumed the ref to v */
1339 goto skip_decref_vx;
1340 }
1341 else {
1342 slow_add:
1343 x = PyNumber_Add(v, w);
1344 }
1345 Py_DECREF(v);
1346 skip_decref_vx:
1347 Py_DECREF(w);
1348 SET_TOP(x);
1349 if (x != NULL) continue;
1350 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001351
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001352 case BINARY_SUBTRACT:
1353 w = POP();
1354 v = TOP();
1355 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1356 /* INLINE: int - int */
1357 register long a, b, i;
1358 a = PyInt_AS_LONG(v);
1359 b = PyInt_AS_LONG(w);
1360 /* cast to avoid undefined behaviour
1361 on overflow */
1362 i = (long)((unsigned long)a - b);
1363 if ((i^a) < 0 && (i^~b) < 0)
1364 goto slow_sub;
1365 x = PyInt_FromLong(i);
1366 }
1367 else {
1368 slow_sub:
1369 x = PyNumber_Subtract(v, w);
1370 }
1371 Py_DECREF(v);
1372 Py_DECREF(w);
1373 SET_TOP(x);
1374 if (x != NULL) continue;
1375 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001377 case BINARY_SUBSCR:
1378 w = POP();
1379 v = TOP();
1380 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1381 /* INLINE: list[int] */
1382 Py_ssize_t i = PyInt_AsSsize_t(w);
1383 if (i < 0)
1384 i += PyList_GET_SIZE(v);
1385 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1386 x = PyList_GET_ITEM(v, i);
1387 Py_INCREF(x);
1388 }
1389 else
1390 goto slow_get;
1391 }
1392 else
1393 slow_get:
1394 x = PyObject_GetItem(v, w);
1395 Py_DECREF(v);
1396 Py_DECREF(w);
1397 SET_TOP(x);
1398 if (x != NULL) continue;
1399 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001401 case BINARY_LSHIFT:
1402 w = POP();
1403 v = TOP();
1404 x = PyNumber_Lshift(v, w);
1405 Py_DECREF(v);
1406 Py_DECREF(w);
1407 SET_TOP(x);
1408 if (x != NULL) continue;
1409 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001411 case BINARY_RSHIFT:
1412 w = POP();
1413 v = TOP();
1414 x = PyNumber_Rshift(v, w);
1415 Py_DECREF(v);
1416 Py_DECREF(w);
1417 SET_TOP(x);
1418 if (x != NULL) continue;
1419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001421 case BINARY_AND:
1422 w = POP();
1423 v = TOP();
1424 x = PyNumber_And(v, w);
1425 Py_DECREF(v);
1426 Py_DECREF(w);
1427 SET_TOP(x);
1428 if (x != NULL) continue;
1429 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001430
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001431 case BINARY_XOR:
1432 w = POP();
1433 v = TOP();
1434 x = PyNumber_Xor(v, w);
1435 Py_DECREF(v);
1436 Py_DECREF(w);
1437 SET_TOP(x);
1438 if (x != NULL) continue;
1439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001441 case BINARY_OR:
1442 w = POP();
1443 v = TOP();
1444 x = PyNumber_Or(v, w);
1445 Py_DECREF(v);
1446 Py_DECREF(w);
1447 SET_TOP(x);
1448 if (x != NULL) continue;
1449 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001450
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001451 case LIST_APPEND:
1452 w = POP();
1453 v = PEEK(oparg);
1454 err = PyList_Append(v, w);
1455 Py_DECREF(w);
1456 if (err == 0) {
1457 PREDICT(JUMP_ABSOLUTE);
1458 continue;
1459 }
1460 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001461
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001462 case SET_ADD:
1463 w = POP();
1464 v = stack_pointer[-oparg];
1465 err = PySet_Add(v, w);
1466 Py_DECREF(w);
1467 if (err == 0) {
1468 PREDICT(JUMP_ABSOLUTE);
1469 continue;
1470 }
1471 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001472
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001473 case INPLACE_POWER:
1474 w = POP();
1475 v = TOP();
1476 x = PyNumber_InPlacePower(v, w, Py_None);
1477 Py_DECREF(v);
1478 Py_DECREF(w);
1479 SET_TOP(x);
1480 if (x != NULL) continue;
1481 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 case INPLACE_MULTIPLY:
1484 w = POP();
1485 v = TOP();
1486 x = PyNumber_InPlaceMultiply(v, w);
1487 Py_DECREF(v);
1488 Py_DECREF(w);
1489 SET_TOP(x);
1490 if (x != NULL) continue;
1491 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001493 case INPLACE_DIVIDE:
1494 if (!_Py_QnewFlag) {
1495 w = POP();
1496 v = TOP();
1497 x = PyNumber_InPlaceDivide(v, w);
1498 Py_DECREF(v);
1499 Py_DECREF(w);
1500 SET_TOP(x);
1501 if (x != NULL) continue;
1502 break;
1503 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001504 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001505 INPLACE_TRUE_DIVIDE */
1506 case INPLACE_TRUE_DIVIDE:
1507 w = POP();
1508 v = TOP();
1509 x = PyNumber_InPlaceTrueDivide(v, w);
1510 Py_DECREF(v);
1511 Py_DECREF(w);
1512 SET_TOP(x);
1513 if (x != NULL) continue;
1514 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001516 case INPLACE_FLOOR_DIVIDE:
1517 w = POP();
1518 v = TOP();
1519 x = PyNumber_InPlaceFloorDivide(v, w);
1520 Py_DECREF(v);
1521 Py_DECREF(w);
1522 SET_TOP(x);
1523 if (x != NULL) continue;
1524 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001525
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001526 case INPLACE_MODULO:
1527 w = POP();
1528 v = TOP();
1529 x = PyNumber_InPlaceRemainder(v, w);
1530 Py_DECREF(v);
1531 Py_DECREF(w);
1532 SET_TOP(x);
1533 if (x != NULL) continue;
1534 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001536 case INPLACE_ADD:
1537 w = POP();
1538 v = TOP();
1539 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1540 /* INLINE: int + int */
1541 register long a, b, i;
1542 a = PyInt_AS_LONG(v);
1543 b = PyInt_AS_LONG(w);
1544 i = a + b;
1545 if ((i^a) < 0 && (i^b) < 0)
1546 goto slow_iadd;
1547 x = PyInt_FromLong(i);
1548 }
1549 else if (PyString_CheckExact(v) &&
1550 PyString_CheckExact(w)) {
1551 x = string_concatenate(v, w, f, next_instr);
1552 /* string_concatenate consumed the ref to v */
1553 goto skip_decref_v;
1554 }
1555 else {
1556 slow_iadd:
1557 x = PyNumber_InPlaceAdd(v, w);
1558 }
1559 Py_DECREF(v);
1560 skip_decref_v:
1561 Py_DECREF(w);
1562 SET_TOP(x);
1563 if (x != NULL) continue;
1564 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001565
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001566 case INPLACE_SUBTRACT:
1567 w = POP();
1568 v = TOP();
1569 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1570 /* INLINE: int - int */
1571 register long a, b, i;
1572 a = PyInt_AS_LONG(v);
1573 b = PyInt_AS_LONG(w);
1574 i = a - b;
1575 if ((i^a) < 0 && (i^~b) < 0)
1576 goto slow_isub;
1577 x = PyInt_FromLong(i);
1578 }
1579 else {
1580 slow_isub:
1581 x = PyNumber_InPlaceSubtract(v, w);
1582 }
1583 Py_DECREF(v);
1584 Py_DECREF(w);
1585 SET_TOP(x);
1586 if (x != NULL) continue;
1587 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001589 case INPLACE_LSHIFT:
1590 w = POP();
1591 v = TOP();
1592 x = PyNumber_InPlaceLshift(v, w);
1593 Py_DECREF(v);
1594 Py_DECREF(w);
1595 SET_TOP(x);
1596 if (x != NULL) continue;
1597 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001599 case INPLACE_RSHIFT:
1600 w = POP();
1601 v = TOP();
1602 x = PyNumber_InPlaceRshift(v, w);
1603 Py_DECREF(v);
1604 Py_DECREF(w);
1605 SET_TOP(x);
1606 if (x != NULL) continue;
1607 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001609 case INPLACE_AND:
1610 w = POP();
1611 v = TOP();
1612 x = PyNumber_InPlaceAnd(v, w);
1613 Py_DECREF(v);
1614 Py_DECREF(w);
1615 SET_TOP(x);
1616 if (x != NULL) continue;
1617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001619 case INPLACE_XOR:
1620 w = POP();
1621 v = TOP();
1622 x = PyNumber_InPlaceXor(v, w);
1623 Py_DECREF(v);
1624 Py_DECREF(w);
1625 SET_TOP(x);
1626 if (x != NULL) continue;
1627 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001629 case INPLACE_OR:
1630 w = POP();
1631 v = TOP();
1632 x = PyNumber_InPlaceOr(v, w);
1633 Py_DECREF(v);
1634 Py_DECREF(w);
1635 SET_TOP(x);
1636 if (x != NULL) continue;
1637 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001639 case SLICE+0:
1640 case SLICE+1:
1641 case SLICE+2:
1642 case SLICE+3:
1643 if ((opcode-SLICE) & 2)
1644 w = POP();
1645 else
1646 w = NULL;
1647 if ((opcode-SLICE) & 1)
1648 v = POP();
1649 else
1650 v = NULL;
1651 u = TOP();
1652 x = apply_slice(u, v, w);
1653 Py_DECREF(u);
1654 Py_XDECREF(v);
1655 Py_XDECREF(w);
1656 SET_TOP(x);
1657 if (x != NULL) continue;
1658 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001659
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001660 case STORE_SLICE+0:
1661 case STORE_SLICE+1:
1662 case STORE_SLICE+2:
1663 case STORE_SLICE+3:
1664 if ((opcode-STORE_SLICE) & 2)
1665 w = POP();
1666 else
1667 w = NULL;
1668 if ((opcode-STORE_SLICE) & 1)
1669 v = POP();
1670 else
1671 v = NULL;
1672 u = POP();
1673 t = POP();
1674 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1675 Py_DECREF(t);
1676 Py_DECREF(u);
1677 Py_XDECREF(v);
1678 Py_XDECREF(w);
1679 if (err == 0) continue;
1680 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001681
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001682 case DELETE_SLICE+0:
1683 case DELETE_SLICE+1:
1684 case DELETE_SLICE+2:
1685 case DELETE_SLICE+3:
1686 if ((opcode-DELETE_SLICE) & 2)
1687 w = POP();
1688 else
1689 w = NULL;
1690 if ((opcode-DELETE_SLICE) & 1)
1691 v = POP();
1692 else
1693 v = NULL;
1694 u = POP();
1695 err = assign_slice(u, v, w, (PyObject *)NULL);
1696 /* del u[v:w] */
1697 Py_DECREF(u);
1698 Py_XDECREF(v);
1699 Py_XDECREF(w);
1700 if (err == 0) continue;
1701 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001703 case STORE_SUBSCR:
1704 w = TOP();
1705 v = SECOND();
1706 u = THIRD();
1707 STACKADJ(-3);
1708 /* v[w] = u */
1709 err = PyObject_SetItem(v, w, u);
1710 Py_DECREF(u);
1711 Py_DECREF(v);
1712 Py_DECREF(w);
1713 if (err == 0) continue;
1714 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001715
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001716 case DELETE_SUBSCR:
1717 w = TOP();
1718 v = SECOND();
1719 STACKADJ(-2);
1720 /* del v[w] */
1721 err = PyObject_DelItem(v, w);
1722 Py_DECREF(v);
1723 Py_DECREF(w);
1724 if (err == 0) continue;
1725 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001726
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001727 case PRINT_EXPR:
1728 v = POP();
1729 w = PySys_GetObject("displayhook");
1730 if (w == NULL) {
1731 PyErr_SetString(PyExc_RuntimeError,
1732 "lost sys.displayhook");
1733 err = -1;
1734 x = NULL;
1735 }
1736 if (err == 0) {
1737 x = PyTuple_Pack(1, v);
1738 if (x == NULL)
1739 err = -1;
1740 }
1741 if (err == 0) {
1742 w = PyEval_CallObject(w, x);
1743 Py_XDECREF(w);
1744 if (w == NULL)
1745 err = -1;
1746 }
1747 Py_DECREF(v);
1748 Py_XDECREF(x);
1749 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001750
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001751 case PRINT_ITEM_TO:
1752 w = stream = POP();
1753 /* fall through to PRINT_ITEM */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001755 case PRINT_ITEM:
1756 v = POP();
1757 if (stream == NULL || stream == Py_None) {
1758 w = PySys_GetObject("stdout");
1759 if (w == NULL) {
1760 PyErr_SetString(PyExc_RuntimeError,
1761 "lost sys.stdout");
1762 err = -1;
1763 }
1764 }
1765 /* PyFile_SoftSpace() can exececute arbitrary code
1766 if sys.stdout is an instance with a __getattr__.
1767 If __getattr__ raises an exception, w will
1768 be freed, so we need to prevent that temporarily. */
1769 Py_XINCREF(w);
1770 if (w != NULL && PyFile_SoftSpace(w, 0))
1771 err = PyFile_WriteString(" ", w);
1772 if (err == 0)
1773 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1774 if (err == 0) {
1775 /* XXX move into writeobject() ? */
1776 if (PyString_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001777 char *s = PyString_AS_STRING(v);
1778 Py_ssize_t len = PyString_GET_SIZE(v);
1779 if (len == 0 ||
1780 !isspace(Py_CHARMASK(s[len-1])) ||
1781 s[len-1] == ' ')
1782 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001783 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001784#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001785 else if (PyUnicode_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001786 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1787 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1788 if (len == 0 ||
1789 !Py_UNICODE_ISSPACE(s[len-1]) ||
1790 s[len-1] == ' ')
1791 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001792 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001793#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001794 else
Stefan Krah7ff78252010-06-23 18:12:09 +00001795 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001796 }
1797 Py_XDECREF(w);
1798 Py_DECREF(v);
1799 Py_XDECREF(stream);
1800 stream = NULL;
1801 if (err == 0)
1802 continue;
1803 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001804
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001805 case PRINT_NEWLINE_TO:
1806 w = stream = POP();
1807 /* fall through to PRINT_NEWLINE */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001808
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001809 case PRINT_NEWLINE:
1810 if (stream == NULL || stream == Py_None) {
1811 w = PySys_GetObject("stdout");
1812 if (w == NULL) {
1813 PyErr_SetString(PyExc_RuntimeError,
1814 "lost sys.stdout");
1815 why = WHY_EXCEPTION;
1816 }
1817 }
1818 if (w != NULL) {
1819 /* w.write() may replace sys.stdout, so we
1820 * have to keep our reference to it */
1821 Py_INCREF(w);
1822 err = PyFile_WriteString("\n", w);
1823 if (err == 0)
1824 PyFile_SoftSpace(w, 0);
1825 Py_DECREF(w);
1826 }
1827 Py_XDECREF(stream);
1828 stream = NULL;
1829 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001830
Thomas Wouters434d0822000-08-24 20:11:32 +00001831
1832#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001833 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001834#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001835 case RAISE_VARARGS:
1836 u = v = w = NULL;
1837 switch (oparg) {
1838 case 3:
1839 u = POP(); /* traceback */
1840 /* Fallthrough */
1841 case 2:
1842 v = POP(); /* value */
1843 /* Fallthrough */
1844 case 1:
1845 w = POP(); /* exc */
1846 case 0: /* Fallthrough */
1847 why = do_raise(w, v, u);
1848 break;
1849 default:
1850 PyErr_SetString(PyExc_SystemError,
1851 "bad RAISE_VARARGS oparg");
1852 why = WHY_EXCEPTION;
1853 break;
1854 }
1855 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001856
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001857 case LOAD_LOCALS:
1858 if ((x = f->f_locals) != NULL) {
1859 Py_INCREF(x);
1860 PUSH(x);
1861 continue;
1862 }
1863 PyErr_SetString(PyExc_SystemError, "no locals");
1864 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001865
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001866 case RETURN_VALUE:
1867 retval = POP();
1868 why = WHY_RETURN;
1869 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001870
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001871 case YIELD_VALUE:
1872 retval = POP();
1873 f->f_stacktop = stack_pointer;
1874 why = WHY_YIELD;
1875 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001876
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001877 case EXEC_STMT:
1878 w = TOP();
1879 v = SECOND();
1880 u = THIRD();
1881 STACKADJ(-3);
1882 READ_TIMESTAMP(intr0);
1883 err = exec_statement(f, u, v, w);
1884 READ_TIMESTAMP(intr1);
1885 Py_DECREF(u);
1886 Py_DECREF(v);
1887 Py_DECREF(w);
1888 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001889
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001890 case POP_BLOCK:
1891 {
1892 PyTryBlock *b = PyFrame_BlockPop(f);
1893 while (STACK_LEVEL() > b->b_level) {
1894 v = POP();
1895 Py_DECREF(v);
1896 }
1897 }
1898 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001900 PREDICTED(END_FINALLY);
1901 case END_FINALLY:
1902 v = POP();
1903 if (PyInt_Check(v)) {
1904 why = (enum why_code) PyInt_AS_LONG(v);
1905 assert(why != WHY_YIELD);
1906 if (why == WHY_RETURN ||
1907 why == WHY_CONTINUE)
1908 retval = POP();
1909 }
1910 else if (PyExceptionClass_Check(v) ||
1911 PyString_Check(v)) {
1912 w = POP();
1913 u = POP();
1914 PyErr_Restore(v, w, u);
1915 why = WHY_RERAISE;
1916 break;
1917 }
1918 else if (v != Py_None) {
1919 PyErr_SetString(PyExc_SystemError,
1920 "'finally' pops bad exception");
1921 why = WHY_EXCEPTION;
1922 }
1923 Py_DECREF(v);
1924 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001925
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001926 case BUILD_CLASS:
1927 u = TOP();
1928 v = SECOND();
1929 w = THIRD();
1930 STACKADJ(-2);
1931 x = build_class(u, v, w);
1932 SET_TOP(x);
1933 Py_DECREF(u);
1934 Py_DECREF(v);
1935 Py_DECREF(w);
1936 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001937
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001938 case STORE_NAME:
1939 w = GETITEM(names, oparg);
1940 v = POP();
1941 if ((x = f->f_locals) != NULL) {
1942 if (PyDict_CheckExact(x))
1943 err = PyDict_SetItem(x, w, v);
1944 else
1945 err = PyObject_SetItem(x, w, v);
1946 Py_DECREF(v);
1947 if (err == 0) continue;
1948 break;
1949 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001950 t = PyObject_Repr(w);
1951 if (t == NULL)
1952 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001953 PyErr_Format(PyExc_SystemError,
1954 "no locals found when storing %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001955 PyString_AS_STRING(t));
1956 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001957 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001959 case DELETE_NAME:
1960 w = GETITEM(names, oparg);
1961 if ((x = f->f_locals) != NULL) {
1962 if ((err = PyObject_DelItem(x, w)) != 0)
1963 format_exc_check_arg(PyExc_NameError,
1964 NAME_ERROR_MSG,
1965 w);
1966 break;
1967 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001968 t = PyObject_Repr(w);
1969 if (t == NULL)
1970 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001971 PyErr_Format(PyExc_SystemError,
1972 "no locals when deleting %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001973 PyString_AS_STRING(w));
1974 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001975 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001976
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001977 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1978 case UNPACK_SEQUENCE:
1979 v = POP();
1980 if (PyTuple_CheckExact(v) &&
1981 PyTuple_GET_SIZE(v) == oparg) {
1982 PyObject **items = \
1983 ((PyTupleObject *)v)->ob_item;
1984 while (oparg--) {
1985 w = items[oparg];
1986 Py_INCREF(w);
1987 PUSH(w);
1988 }
1989 Py_DECREF(v);
1990 continue;
1991 } else if (PyList_CheckExact(v) &&
1992 PyList_GET_SIZE(v) == oparg) {
1993 PyObject **items = \
1994 ((PyListObject *)v)->ob_item;
1995 while (oparg--) {
1996 w = items[oparg];
1997 Py_INCREF(w);
1998 PUSH(w);
1999 }
2000 } else if (unpack_iterable(v, oparg,
2001 stack_pointer + oparg)) {
2002 STACKADJ(oparg);
2003 } else {
2004 /* unpack_iterable() raised an exception */
2005 why = WHY_EXCEPTION;
2006 }
2007 Py_DECREF(v);
2008 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002009
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002010 case STORE_ATTR:
2011 w = GETITEM(names, oparg);
2012 v = TOP();
2013 u = SECOND();
2014 STACKADJ(-2);
2015 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2016 Py_DECREF(v);
2017 Py_DECREF(u);
2018 if (err == 0) continue;
2019 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002020
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002021 case DELETE_ATTR:
2022 w = GETITEM(names, oparg);
2023 v = POP();
2024 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2025 /* del v.w */
2026 Py_DECREF(v);
2027 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002028
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002029 case STORE_GLOBAL:
2030 w = GETITEM(names, oparg);
2031 v = POP();
2032 err = PyDict_SetItem(f->f_globals, w, v);
2033 Py_DECREF(v);
2034 if (err == 0) continue;
2035 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002036
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002037 case DELETE_GLOBAL:
2038 w = GETITEM(names, oparg);
2039 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2040 format_exc_check_arg(
2041 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2042 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002043
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002044 case LOAD_NAME:
2045 w = GETITEM(names, oparg);
2046 if ((v = f->f_locals) == NULL) {
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002047 why = WHY_EXCEPTION;
2048 t = PyObject_Repr(w);
2049 if (t == NULL)
2050 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002051 PyErr_Format(PyExc_SystemError,
2052 "no locals when loading %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002053 PyString_AS_STRING(w));
2054 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002055 break;
2056 }
2057 if (PyDict_CheckExact(v)) {
2058 x = PyDict_GetItem(v, w);
2059 Py_XINCREF(x);
2060 }
2061 else {
2062 x = PyObject_GetItem(v, w);
2063 if (x == NULL && PyErr_Occurred()) {
2064 if (!PyErr_ExceptionMatches(
2065 PyExc_KeyError))
2066 break;
2067 PyErr_Clear();
2068 }
2069 }
2070 if (x == NULL) {
2071 x = PyDict_GetItem(f->f_globals, w);
2072 if (x == NULL) {
2073 x = PyDict_GetItem(f->f_builtins, w);
2074 if (x == NULL) {
2075 format_exc_check_arg(
2076 PyExc_NameError,
2077 NAME_ERROR_MSG, w);
2078 break;
2079 }
2080 }
2081 Py_INCREF(x);
2082 }
2083 PUSH(x);
2084 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002085
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002086 case LOAD_GLOBAL:
2087 w = GETITEM(names, oparg);
2088 if (PyString_CheckExact(w)) {
2089 /* Inline the PyDict_GetItem() calls.
2090 WARNING: this is an extreme speed hack.
2091 Do not try this at home. */
2092 long hash = ((PyStringObject *)w)->ob_shash;
2093 if (hash != -1) {
2094 PyDictObject *d;
2095 PyDictEntry *e;
2096 d = (PyDictObject *)(f->f_globals);
2097 e = d->ma_lookup(d, w, hash);
2098 if (e == NULL) {
2099 x = NULL;
2100 break;
2101 }
2102 x = e->me_value;
2103 if (x != NULL) {
2104 Py_INCREF(x);
2105 PUSH(x);
2106 continue;
2107 }
2108 d = (PyDictObject *)(f->f_builtins);
2109 e = d->ma_lookup(d, w, hash);
2110 if (e == NULL) {
2111 x = NULL;
2112 break;
2113 }
2114 x = e->me_value;
2115 if (x != NULL) {
2116 Py_INCREF(x);
2117 PUSH(x);
2118 continue;
2119 }
2120 goto load_global_error;
2121 }
2122 }
2123 /* This is the un-inlined version of the code above */
2124 x = PyDict_GetItem(f->f_globals, w);
2125 if (x == NULL) {
2126 x = PyDict_GetItem(f->f_builtins, w);
2127 if (x == NULL) {
2128 load_global_error:
2129 format_exc_check_arg(
2130 PyExc_NameError,
2131 GLOBAL_NAME_ERROR_MSG, w);
2132 break;
2133 }
2134 }
2135 Py_INCREF(x);
2136 PUSH(x);
2137 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002138
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002139 case DELETE_FAST:
2140 x = GETLOCAL(oparg);
2141 if (x != NULL) {
2142 SETLOCAL(oparg, NULL);
2143 continue;
2144 }
2145 format_exc_check_arg(
2146 PyExc_UnboundLocalError,
2147 UNBOUNDLOCAL_ERROR_MSG,
2148 PyTuple_GetItem(co->co_varnames, oparg)
2149 );
2150 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002151
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002152 case LOAD_CLOSURE:
2153 x = freevars[oparg];
2154 Py_INCREF(x);
2155 PUSH(x);
2156 if (x != NULL) continue;
2157 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002159 case LOAD_DEREF:
2160 x = freevars[oparg];
2161 w = PyCell_Get(x);
2162 if (w != NULL) {
2163 PUSH(w);
2164 continue;
2165 }
2166 err = -1;
2167 /* Don't stomp existing exception */
2168 if (PyErr_Occurred())
2169 break;
2170 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2171 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krah7ff78252010-06-23 18:12:09 +00002172 oparg);
2173 format_exc_check_arg(
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002174 PyExc_UnboundLocalError,
2175 UNBOUNDLOCAL_ERROR_MSG,
2176 v);
2177 } else {
2178 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2179 PyTuple_GET_SIZE(co->co_cellvars));
2180 format_exc_check_arg(PyExc_NameError,
2181 UNBOUNDFREE_ERROR_MSG, v);
2182 }
2183 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002184
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002185 case STORE_DEREF:
2186 w = POP();
2187 x = freevars[oparg];
2188 PyCell_Set(x, w);
2189 Py_DECREF(w);
2190 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002191
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002192 case BUILD_TUPLE:
2193 x = PyTuple_New(oparg);
2194 if (x != NULL) {
2195 for (; --oparg >= 0;) {
2196 w = POP();
2197 PyTuple_SET_ITEM(x, oparg, w);
2198 }
2199 PUSH(x);
2200 continue;
2201 }
2202 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002204 case BUILD_LIST:
2205 x = PyList_New(oparg);
2206 if (x != NULL) {
2207 for (; --oparg >= 0;) {
2208 w = POP();
2209 PyList_SET_ITEM(x, oparg, w);
2210 }
2211 PUSH(x);
2212 continue;
2213 }
2214 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002216 case BUILD_SET:
2217 x = PySet_New(NULL);
2218 if (x != NULL) {
2219 for (; --oparg >= 0;) {
2220 w = POP();
2221 if (err == 0)
2222 err = PySet_Add(x, w);
2223 Py_DECREF(w);
2224 }
2225 if (err != 0) {
2226 Py_DECREF(x);
2227 break;
2228 }
2229 PUSH(x);
2230 continue;
2231 }
2232 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002233
2234
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002235 case BUILD_MAP:
2236 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2237 PUSH(x);
2238 if (x != NULL) continue;
2239 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002241 case STORE_MAP:
2242 w = TOP(); /* key */
2243 u = SECOND(); /* value */
2244 v = THIRD(); /* dict */
2245 STACKADJ(-2);
2246 assert (PyDict_CheckExact(v));
2247 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2248 Py_DECREF(u);
2249 Py_DECREF(w);
2250 if (err == 0) continue;
2251 break;
Raymond Hettingereffde122007-12-18 18:26:18 +00002252
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002253 case MAP_ADD:
2254 w = TOP(); /* key */
2255 u = SECOND(); /* value */
2256 STACKADJ(-2);
2257 v = stack_pointer[-oparg]; /* dict */
2258 assert (PyDict_CheckExact(v));
2259 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2260 Py_DECREF(u);
2261 Py_DECREF(w);
2262 if (err == 0) {
2263 PREDICT(JUMP_ABSOLUTE);
2264 continue;
2265 }
2266 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002267
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002268 case LOAD_ATTR:
2269 w = GETITEM(names, oparg);
2270 v = TOP();
2271 x = PyObject_GetAttr(v, w);
2272 Py_DECREF(v);
2273 SET_TOP(x);
2274 if (x != NULL) continue;
2275 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002276
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002277 case COMPARE_OP:
2278 w = POP();
2279 v = TOP();
2280 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2281 /* INLINE: cmp(int, int) */
2282 register long a, b;
2283 register int res;
2284 a = PyInt_AS_LONG(v);
2285 b = PyInt_AS_LONG(w);
2286 switch (oparg) {
2287 case PyCmp_LT: res = a < b; break;
2288 case PyCmp_LE: res = a <= b; break;
2289 case PyCmp_EQ: res = a == b; break;
2290 case PyCmp_NE: res = a != b; break;
2291 case PyCmp_GT: res = a > b; break;
2292 case PyCmp_GE: res = a >= b; break;
2293 case PyCmp_IS: res = v == w; break;
2294 case PyCmp_IS_NOT: res = v != w; break;
2295 default: goto slow_compare;
2296 }
2297 x = res ? Py_True : Py_False;
2298 Py_INCREF(x);
2299 }
2300 else {
2301 slow_compare:
2302 x = cmp_outcome(oparg, v, w);
2303 }
2304 Py_DECREF(v);
2305 Py_DECREF(w);
2306 SET_TOP(x);
2307 if (x == NULL) break;
2308 PREDICT(POP_JUMP_IF_FALSE);
2309 PREDICT(POP_JUMP_IF_TRUE);
2310 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002311
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002312 case IMPORT_NAME:
2313 w = GETITEM(names, oparg);
2314 x = PyDict_GetItemString(f->f_builtins, "__import__");
2315 if (x == NULL) {
2316 PyErr_SetString(PyExc_ImportError,
2317 "__import__ not found");
2318 break;
2319 }
2320 Py_INCREF(x);
2321 v = POP();
2322 u = TOP();
2323 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2324 w = PyTuple_Pack(5,
2325 w,
2326 f->f_globals,
2327 f->f_locals == NULL ?
2328 Py_None : f->f_locals,
2329 v,
2330 u);
2331 else
2332 w = PyTuple_Pack(4,
2333 w,
2334 f->f_globals,
2335 f->f_locals == NULL ?
2336 Py_None : f->f_locals,
2337 v);
2338 Py_DECREF(v);
2339 Py_DECREF(u);
2340 if (w == NULL) {
2341 u = POP();
2342 Py_DECREF(x);
2343 x = NULL;
2344 break;
2345 }
2346 READ_TIMESTAMP(intr0);
2347 v = x;
2348 x = PyEval_CallObject(v, w);
2349 Py_DECREF(v);
2350 READ_TIMESTAMP(intr1);
2351 Py_DECREF(w);
2352 SET_TOP(x);
2353 if (x != NULL) continue;
2354 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002355
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002356 case IMPORT_STAR:
2357 v = POP();
2358 PyFrame_FastToLocals(f);
2359 if ((x = f->f_locals) == NULL) {
2360 PyErr_SetString(PyExc_SystemError,
2361 "no locals found during 'import *'");
2362 break;
2363 }
2364 READ_TIMESTAMP(intr0);
2365 err = import_all_from(x, v);
2366 READ_TIMESTAMP(intr1);
2367 PyFrame_LocalsToFast(f, 0);
2368 Py_DECREF(v);
2369 if (err == 0) continue;
2370 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002371
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002372 case IMPORT_FROM:
2373 w = GETITEM(names, oparg);
2374 v = TOP();
2375 READ_TIMESTAMP(intr0);
2376 x = import_from(v, w);
2377 READ_TIMESTAMP(intr1);
2378 PUSH(x);
2379 if (x != NULL) continue;
2380 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002382 case JUMP_FORWARD:
2383 JUMPBY(oparg);
2384 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002385
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002386 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2387 case POP_JUMP_IF_FALSE:
2388 w = POP();
2389 if (w == Py_True) {
2390 Py_DECREF(w);
2391 goto fast_next_opcode;
2392 }
2393 if (w == Py_False) {
2394 Py_DECREF(w);
2395 JUMPTO(oparg);
2396 goto fast_next_opcode;
2397 }
2398 err = PyObject_IsTrue(w);
2399 Py_DECREF(w);
2400 if (err > 0)
2401 err = 0;
2402 else if (err == 0)
2403 JUMPTO(oparg);
2404 else
2405 break;
2406 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002407
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002408 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2409 case POP_JUMP_IF_TRUE:
2410 w = POP();
2411 if (w == Py_False) {
2412 Py_DECREF(w);
2413 goto fast_next_opcode;
2414 }
2415 if (w == Py_True) {
2416 Py_DECREF(w);
2417 JUMPTO(oparg);
2418 goto fast_next_opcode;
2419 }
2420 err = PyObject_IsTrue(w);
2421 Py_DECREF(w);
2422 if (err > 0) {
2423 err = 0;
2424 JUMPTO(oparg);
2425 }
2426 else if (err == 0)
2427 ;
2428 else
2429 break;
2430 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002432 case JUMP_IF_FALSE_OR_POP:
2433 w = TOP();
2434 if (w == Py_True) {
2435 STACKADJ(-1);
2436 Py_DECREF(w);
2437 goto fast_next_opcode;
2438 }
2439 if (w == Py_False) {
2440 JUMPTO(oparg);
2441 goto fast_next_opcode;
2442 }
2443 err = PyObject_IsTrue(w);
2444 if (err > 0) {
2445 STACKADJ(-1);
2446 Py_DECREF(w);
2447 err = 0;
2448 }
2449 else if (err == 0)
2450 JUMPTO(oparg);
2451 else
2452 break;
2453 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002454
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002455 case JUMP_IF_TRUE_OR_POP:
2456 w = TOP();
2457 if (w == Py_False) {
2458 STACKADJ(-1);
2459 Py_DECREF(w);
2460 goto fast_next_opcode;
2461 }
2462 if (w == Py_True) {
2463 JUMPTO(oparg);
2464 goto fast_next_opcode;
2465 }
2466 err = PyObject_IsTrue(w);
2467 if (err > 0) {
2468 err = 0;
2469 JUMPTO(oparg);
2470 }
2471 else if (err == 0) {
2472 STACKADJ(-1);
2473 Py_DECREF(w);
2474 }
2475 else
2476 break;
2477 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002478
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002479 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2480 case JUMP_ABSOLUTE:
2481 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002482#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002483 /* Enabling this path speeds-up all while and for-loops by bypassing
2484 the per-loop checks for signals. By default, this should be turned-off
2485 because it prevents detection of a control-break in tight loops like
2486 "while 1: pass". Compile with this option turned-on when you need
2487 the speed-up and do not need break checking inside tight loops (ones
2488 that contain only instructions ending with goto fast_next_opcode).
2489 */
2490 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002491#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002492 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002493#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002494
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002495 case GET_ITER:
2496 /* before: [obj]; after [getiter(obj)] */
2497 v = TOP();
2498 x = PyObject_GetIter(v);
2499 Py_DECREF(v);
2500 if (x != NULL) {
2501 SET_TOP(x);
2502 PREDICT(FOR_ITER);
2503 continue;
2504 }
2505 STACKADJ(-1);
2506 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002507
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002508 PREDICTED_WITH_ARG(FOR_ITER);
2509 case FOR_ITER:
2510 /* before: [iter]; after: [iter, iter()] *or* [] */
2511 v = TOP();
2512 x = (*v->ob_type->tp_iternext)(v);
2513 if (x != NULL) {
2514 PUSH(x);
2515 PREDICT(STORE_FAST);
2516 PREDICT(UNPACK_SEQUENCE);
2517 continue;
2518 }
2519 if (PyErr_Occurred()) {
2520 if (!PyErr_ExceptionMatches(
2521 PyExc_StopIteration))
2522 break;
2523 PyErr_Clear();
2524 }
2525 /* iterator ended normally */
2526 x = v = POP();
2527 Py_DECREF(v);
2528 JUMPBY(oparg);
2529 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002530
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002531 case BREAK_LOOP:
2532 why = WHY_BREAK;
2533 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002534
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002535 case CONTINUE_LOOP:
2536 retval = PyInt_FromLong(oparg);
2537 if (!retval) {
2538 x = NULL;
2539 break;
2540 }
2541 why = WHY_CONTINUE;
2542 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002543
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002544 case SETUP_LOOP:
2545 case SETUP_EXCEPT:
2546 case SETUP_FINALLY:
2547 /* NOTE: If you add any new block-setup opcodes that
2548 are not try/except/finally handlers, you may need
2549 to update the PyGen_NeedsFinalizing() function.
2550 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002551
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002552 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2553 STACK_LEVEL());
2554 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002555
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002556 case SETUP_WITH:
2557 {
2558 static PyObject *exit, *enter;
2559 w = TOP();
2560 x = special_lookup(w, "__exit__", &exit);
2561 if (!x)
2562 break;
2563 SET_TOP(x);
2564 u = special_lookup(w, "__enter__", &enter);
2565 Py_DECREF(w);
2566 if (!u) {
2567 x = NULL;
2568 break;
2569 }
2570 x = PyObject_CallFunctionObjArgs(u, NULL);
2571 Py_DECREF(u);
2572 if (!x)
2573 break;
2574 /* Setup a finally block (SETUP_WITH as a block is
2575 equivalent to SETUP_FINALLY except it normalizes
2576 the exception) before pushing the result of
2577 __enter__ on the stack. */
2578 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2579 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002581 PUSH(x);
2582 continue;
2583 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002584
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002585 case WITH_CLEANUP:
2586 {
2587 /* At the top of the stack are 1-3 values indicating
2588 how/why we entered the finally clause:
2589 - TOP = None
2590 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2591 - TOP = WHY_*; no retval below it
2592 - (TOP, SECOND, THIRD) = exc_info()
2593 Below them is EXIT, the context.__exit__ bound method.
2594 In the last case, we must call
2595 EXIT(TOP, SECOND, THIRD)
2596 otherwise we must call
2597 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002599 In all cases, we remove EXIT from the stack, leaving
2600 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002601
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002602 In addition, if the stack represents an exception,
2603 *and* the function call returns a 'true' value, we
2604 "zap" this information, to prevent END_FINALLY from
2605 re-raising the exception. (But non-local gotos
2606 should still be resumed.)
2607 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002609 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002610
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002611 u = POP();
2612 if (u == Py_None) {
2613 exit_func = TOP();
2614 SET_TOP(u);
2615 v = w = Py_None;
2616 }
2617 else if (PyInt_Check(u)) {
2618 switch(PyInt_AS_LONG(u)) {
2619 case WHY_RETURN:
2620 case WHY_CONTINUE:
2621 /* Retval in TOP. */
2622 exit_func = SECOND();
2623 SET_SECOND(TOP());
2624 SET_TOP(u);
2625 break;
2626 default:
2627 exit_func = TOP();
2628 SET_TOP(u);
2629 break;
2630 }
2631 u = v = w = Py_None;
2632 }
2633 else {
2634 v = TOP();
2635 w = SECOND();
2636 exit_func = THIRD();
2637 SET_TOP(u);
2638 SET_SECOND(v);
2639 SET_THIRD(w);
2640 }
2641 /* XXX Not the fastest way to call it... */
2642 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2643 NULL);
2644 Py_DECREF(exit_func);
2645 if (x == NULL)
2646 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002647
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002648 if (u != Py_None)
2649 err = PyObject_IsTrue(x);
2650 else
2651 err = 0;
2652 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002653
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002654 if (err < 0)
2655 break; /* Go to error exit */
2656 else if (err > 0) {
2657 err = 0;
2658 /* There was an exception and a true return */
2659 STACKADJ(-2);
2660 Py_INCREF(Py_None);
2661 SET_TOP(Py_None);
2662 Py_DECREF(u);
2663 Py_DECREF(v);
2664 Py_DECREF(w);
2665 } else {
2666 /* The stack was rearranged to remove EXIT
2667 above. Let END_FINALLY do its thing */
2668 }
2669 PREDICT(END_FINALLY);
2670 break;
2671 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002672
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002673 case CALL_FUNCTION:
2674 {
2675 PyObject **sp;
2676 PCALL(PCALL_ALL);
2677 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002678#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002679 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002680#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002681 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002682#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002683 stack_pointer = sp;
2684 PUSH(x);
2685 if (x != NULL)
2686 continue;
2687 break;
2688 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002689
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002690 case CALL_FUNCTION_VAR:
2691 case CALL_FUNCTION_KW:
2692 case CALL_FUNCTION_VAR_KW:
2693 {
2694 int na = oparg & 0xff;
2695 int nk = (oparg>>8) & 0xff;
2696 int flags = (opcode - CALL_FUNCTION) & 3;
2697 int n = na + 2 * nk;
2698 PyObject **pfunc, *func, **sp;
2699 PCALL(PCALL_ALL);
2700 if (flags & CALL_FLAG_VAR)
2701 n++;
2702 if (flags & CALL_FLAG_KW)
2703 n++;
2704 pfunc = stack_pointer - n - 1;
2705 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002706
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002707 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00002708 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002709 PyObject *self = PyMethod_GET_SELF(func);
2710 Py_INCREF(self);
2711 func = PyMethod_GET_FUNCTION(func);
2712 Py_INCREF(func);
2713 Py_DECREF(*pfunc);
2714 *pfunc = self;
2715 na++;
2716 } else
2717 Py_INCREF(func);
2718 sp = stack_pointer;
2719 READ_TIMESTAMP(intr0);
2720 x = ext_do_call(func, &sp, flags, na, nk);
2721 READ_TIMESTAMP(intr1);
2722 stack_pointer = sp;
2723 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002725 while (stack_pointer > pfunc) {
2726 w = POP();
2727 Py_DECREF(w);
2728 }
2729 PUSH(x);
2730 if (x != NULL)
2731 continue;
2732 break;
2733 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002734
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002735 case MAKE_FUNCTION:
2736 v = POP(); /* code object */
2737 x = PyFunction_New(v, f->f_globals);
2738 Py_DECREF(v);
2739 /* XXX Maybe this should be a separate opcode? */
2740 if (x != NULL && oparg > 0) {
2741 v = PyTuple_New(oparg);
2742 if (v == NULL) {
2743 Py_DECREF(x);
2744 x = NULL;
2745 break;
2746 }
2747 while (--oparg >= 0) {
2748 w = POP();
2749 PyTuple_SET_ITEM(v, oparg, w);
2750 }
2751 err = PyFunction_SetDefaults(x, v);
2752 Py_DECREF(v);
2753 }
2754 PUSH(x);
2755 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002756
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002757 case MAKE_CLOSURE:
2758 {
2759 v = POP(); /* code object */
2760 x = PyFunction_New(v, f->f_globals);
2761 Py_DECREF(v);
2762 if (x != NULL) {
2763 v = POP();
2764 if (PyFunction_SetClosure(x, v) != 0) {
2765 /* Can't happen unless bytecode is corrupt. */
2766 why = WHY_EXCEPTION;
2767 }
2768 Py_DECREF(v);
2769 }
2770 if (x != NULL && oparg > 0) {
2771 v = PyTuple_New(oparg);
2772 if (v == NULL) {
2773 Py_DECREF(x);
2774 x = NULL;
2775 break;
2776 }
2777 while (--oparg >= 0) {
2778 w = POP();
2779 PyTuple_SET_ITEM(v, oparg, w);
2780 }
2781 if (PyFunction_SetDefaults(x, v) != 0) {
2782 /* Can't happen unless
2783 PyFunction_SetDefaults changes. */
2784 why = WHY_EXCEPTION;
2785 }
2786 Py_DECREF(v);
2787 }
2788 PUSH(x);
2789 break;
2790 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002791
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002792 case BUILD_SLICE:
2793 if (oparg == 3)
2794 w = POP();
2795 else
2796 w = NULL;
2797 v = POP();
2798 u = TOP();
2799 x = PySlice_New(u, v, w);
2800 Py_DECREF(u);
2801 Py_DECREF(v);
2802 Py_XDECREF(w);
2803 SET_TOP(x);
2804 if (x != NULL) continue;
2805 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002807 case EXTENDED_ARG:
2808 opcode = NEXTOP();
2809 oparg = oparg<<16 | NEXTARG();
2810 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002811
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002812 default:
2813 fprintf(stderr,
2814 "XXX lineno: %d, opcode: %d\n",
2815 PyFrame_GetLineNumber(f),
2816 opcode);
2817 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2818 why = WHY_EXCEPTION;
2819 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002820
2821#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002822 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002823#endif
2824
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002825 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002826
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002827 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002828
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002829 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002830
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002831 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002832
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002833 if (why == WHY_NOT) {
2834 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002835#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002836 /* This check is expensive! */
2837 if (PyErr_Occurred())
2838 fprintf(stderr,
2839 "XXX undetected error\n");
2840 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002841#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002842 READ_TIMESTAMP(loop1);
2843 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002844#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002845 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002846#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002847 }
2848 why = WHY_EXCEPTION;
2849 x = Py_None;
2850 err = 0;
2851 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002852
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002853 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002854
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002855 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2856 if (!PyErr_Occurred()) {
2857 PyErr_SetString(PyExc_SystemError,
2858 "error return without exception set");
2859 why = WHY_EXCEPTION;
2860 }
2861 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002862#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002863 else {
2864 /* This check is expensive! */
2865 if (PyErr_Occurred()) {
2866 char buf[128];
2867 sprintf(buf, "Stack unwind with exception "
2868 "set and why=%d", why);
2869 Py_FatalError(buf);
2870 }
2871 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002872#endif
2873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002874 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002875
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002876 if (why == WHY_EXCEPTION) {
2877 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002878
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002879 if (tstate->c_tracefunc != NULL)
2880 call_exc_trace(tstate->c_tracefunc,
2881 tstate->c_traceobj, f);
2882 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002884 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002885
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002886 if (why == WHY_RERAISE)
2887 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002889 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002890
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002891fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002892 while (why != WHY_NOT && f->f_iblock > 0) {
2893 /* Peek at the current block. */
2894 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002895
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002896 assert(why != WHY_YIELD);
2897 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2898 why = WHY_NOT;
2899 JUMPTO(PyInt_AS_LONG(retval));
2900 Py_DECREF(retval);
2901 break;
2902 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002904 /* Now we have to pop the block. */
2905 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00002906
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002907 while (STACK_LEVEL() > b->b_level) {
2908 v = POP();
2909 Py_XDECREF(v);
2910 }
2911 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2912 why = WHY_NOT;
2913 JUMPTO(b->b_handler);
2914 break;
2915 }
2916 if (b->b_type == SETUP_FINALLY ||
2917 (b->b_type == SETUP_EXCEPT &&
2918 why == WHY_EXCEPTION) ||
2919 b->b_type == SETUP_WITH) {
2920 if (why == WHY_EXCEPTION) {
2921 PyObject *exc, *val, *tb;
2922 PyErr_Fetch(&exc, &val, &tb);
2923 if (val == NULL) {
2924 val = Py_None;
2925 Py_INCREF(val);
2926 }
2927 /* Make the raw exception data
2928 available to the handler,
2929 so a program can emulate the
2930 Python main loop. Don't do
2931 this for 'finally'. */
2932 if (b->b_type == SETUP_EXCEPT ||
2933 b->b_type == SETUP_WITH) {
2934 PyErr_NormalizeException(
2935 &exc, &val, &tb);
2936 set_exc_info(tstate,
2937 exc, val, tb);
2938 }
2939 if (tb == NULL) {
2940 Py_INCREF(Py_None);
2941 PUSH(Py_None);
2942 } else
2943 PUSH(tb);
2944 PUSH(val);
2945 PUSH(exc);
2946 }
2947 else {
2948 if (why & (WHY_RETURN | WHY_CONTINUE))
2949 PUSH(retval);
2950 v = PyInt_FromLong((long)why);
2951 PUSH(v);
2952 }
2953 why = WHY_NOT;
2954 JUMPTO(b->b_handler);
2955 break;
2956 }
2957 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002959 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002960
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002961 if (why != WHY_NOT)
2962 break;
2963 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002964
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002965 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002966
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002967 assert(why != WHY_YIELD);
2968 /* Pop remaining stack entries. */
2969 while (!EMPTY()) {
2970 v = POP();
2971 Py_XDECREF(v);
2972 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002973
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002974 if (why != WHY_RETURN)
2975 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002976
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002977fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002978 if (tstate->use_tracing) {
2979 if (tstate->c_tracefunc) {
2980 if (why == WHY_RETURN || why == WHY_YIELD) {
2981 if (call_trace(tstate->c_tracefunc,
2982 tstate->c_traceobj, f,
2983 PyTrace_RETURN, retval)) {
2984 Py_XDECREF(retval);
2985 retval = NULL;
2986 why = WHY_EXCEPTION;
2987 }
2988 }
2989 else if (why == WHY_EXCEPTION) {
2990 call_trace_protected(tstate->c_tracefunc,
2991 tstate->c_traceobj, f,
2992 PyTrace_RETURN, NULL);
2993 }
2994 }
2995 if (tstate->c_profilefunc) {
2996 if (why == WHY_EXCEPTION)
2997 call_trace_protected(tstate->c_profilefunc,
2998 tstate->c_profileobj, f,
2999 PyTrace_RETURN, NULL);
3000 else if (call_trace(tstate->c_profilefunc,
3001 tstate->c_profileobj, f,
3002 PyTrace_RETURN, retval)) {
3003 Py_XDECREF(retval);
3004 retval = NULL;
3005 why = WHY_EXCEPTION;
3006 }
3007 }
3008 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003009
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003010 if (tstate->frame->f_exc_type != NULL)
3011 reset_exc_info(tstate);
3012 else {
3013 assert(tstate->frame->f_exc_value == NULL);
3014 assert(tstate->frame->f_exc_traceback == NULL);
3015 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003017 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003018exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003019 Py_LeaveRecursiveCall();
3020 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003021
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003022 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003023}
3024
Guido van Rossumc2e20742006-02-27 22:32:47 +00003025/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003026 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003027 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003028
Tim Peters6d6c1a32001-08-02 04:15:00 +00003029PyObject *
3030PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003031 PyObject **args, int argcount, PyObject **kws, int kwcount,
3032 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003033{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003034 register PyFrameObject *f;
3035 register PyObject *retval = NULL;
3036 register PyObject **fastlocals, **freevars;
3037 PyThreadState *tstate = PyThreadState_GET();
3038 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003039
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003040 if (globals == NULL) {
3041 PyErr_SetString(PyExc_SystemError,
3042 "PyEval_EvalCodeEx: NULL globals");
3043 return NULL;
3044 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003046 assert(tstate != NULL);
3047 assert(globals != NULL);
3048 f = PyFrame_New(tstate, co, globals, locals);
3049 if (f == NULL)
3050 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003052 fastlocals = f->f_localsplus;
3053 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003054
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003055 if (co->co_argcount > 0 ||
3056 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3057 int i;
3058 int n = argcount;
3059 PyObject *kwdict = NULL;
3060 if (co->co_flags & CO_VARKEYWORDS) {
3061 kwdict = PyDict_New();
3062 if (kwdict == NULL)
3063 goto fail;
3064 i = co->co_argcount;
3065 if (co->co_flags & CO_VARARGS)
3066 i++;
3067 SETLOCAL(i, kwdict);
3068 }
3069 if (argcount > co->co_argcount) {
3070 if (!(co->co_flags & CO_VARARGS)) {
3071 PyErr_Format(PyExc_TypeError,
3072 "%.200s() takes %s %d "
3073 "argument%s (%d given)",
3074 PyString_AsString(co->co_name),
3075 defcount ? "at most" : "exactly",
3076 co->co_argcount,
3077 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003078 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003079 goto fail;
3080 }
3081 n = co->co_argcount;
3082 }
3083 for (i = 0; i < n; i++) {
3084 x = args[i];
3085 Py_INCREF(x);
3086 SETLOCAL(i, x);
3087 }
3088 if (co->co_flags & CO_VARARGS) {
3089 u = PyTuple_New(argcount - n);
3090 if (u == NULL)
3091 goto fail;
3092 SETLOCAL(co->co_argcount, u);
3093 for (i = n; i < argcount; i++) {
3094 x = args[i];
3095 Py_INCREF(x);
3096 PyTuple_SET_ITEM(u, i-n, x);
3097 }
3098 }
3099 for (i = 0; i < kwcount; i++) {
3100 PyObject **co_varnames;
3101 PyObject *keyword = kws[2*i];
3102 PyObject *value = kws[2*i + 1];
3103 int j;
3104 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003105#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003106 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003107#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003108 )) {
3109 PyErr_Format(PyExc_TypeError,
3110 "%.200s() keywords must be strings",
3111 PyString_AsString(co->co_name));
3112 goto fail;
3113 }
3114 /* Speed hack: do raw pointer compares. As names are
3115 normally interned this should almost always hit. */
3116 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3117 for (j = 0; j < co->co_argcount; j++) {
3118 PyObject *nm = co_varnames[j];
3119 if (nm == keyword)
3120 goto kw_found;
3121 }
3122 /* Slow fallback, just in case */
3123 for (j = 0; j < co->co_argcount; j++) {
3124 PyObject *nm = co_varnames[j];
3125 int cmp = PyObject_RichCompareBool(
3126 keyword, nm, Py_EQ);
3127 if (cmp > 0)
3128 goto kw_found;
3129 else if (cmp < 0)
3130 goto fail;
3131 }
3132 if (kwdict == NULL) {
3133 PyObject *kwd_str = kwd_as_string(keyword);
3134 if (kwd_str) {
3135 PyErr_Format(PyExc_TypeError,
3136 "%.200s() got an unexpected "
3137 "keyword argument '%.400s'",
3138 PyString_AsString(co->co_name),
3139 PyString_AsString(kwd_str));
3140 Py_DECREF(kwd_str);
3141 }
3142 goto fail;
3143 }
3144 PyDict_SetItem(kwdict, keyword, value);
3145 continue;
3146 kw_found:
3147 if (GETLOCAL(j) != NULL) {
3148 PyObject *kwd_str = kwd_as_string(keyword);
3149 if (kwd_str) {
3150 PyErr_Format(PyExc_TypeError,
3151 "%.200s() got multiple "
3152 "values for keyword "
3153 "argument '%.400s'",
3154 PyString_AsString(co->co_name),
3155 PyString_AsString(kwd_str));
3156 Py_DECREF(kwd_str);
3157 }
3158 goto fail;
3159 }
3160 Py_INCREF(value);
3161 SETLOCAL(j, value);
3162 }
3163 if (argcount < co->co_argcount) {
3164 int m = co->co_argcount - defcount;
3165 for (i = argcount; i < m; i++) {
3166 if (GETLOCAL(i) == NULL) {
3167 int j, given = 0;
3168 for (j = 0; j < co->co_argcount; j++)
3169 if (GETLOCAL(j))
3170 given++;
3171 PyErr_Format(PyExc_TypeError,
3172 "%.200s() takes %s %d "
3173 "argument%s (%d given)",
3174 PyString_AsString(co->co_name),
3175 ((co->co_flags & CO_VARARGS) ||
3176 defcount) ? "at least"
3177 : "exactly",
3178 m, m == 1 ? "" : "s", given);
3179 goto fail;
3180 }
3181 }
3182 if (n > m)
3183 i = n - m;
3184 else
3185 i = 0;
3186 for (; i < defcount; i++) {
3187 if (GETLOCAL(m+i) == NULL) {
3188 PyObject *def = defs[i];
3189 Py_INCREF(def);
3190 SETLOCAL(m+i, def);
3191 }
3192 }
3193 }
3194 }
3195 else if (argcount > 0 || kwcount > 0) {
3196 PyErr_Format(PyExc_TypeError,
3197 "%.200s() takes no arguments (%d given)",
3198 PyString_AsString(co->co_name),
3199 argcount + kwcount);
3200 goto fail;
3201 }
3202 /* Allocate and initialize storage for cell vars, and copy free
3203 vars into frame. This isn't too efficient right now. */
3204 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3205 int i, j, nargs, found;
3206 char *cellname, *argname;
3207 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003208
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003209 nargs = co->co_argcount;
3210 if (co->co_flags & CO_VARARGS)
3211 nargs++;
3212 if (co->co_flags & CO_VARKEYWORDS)
3213 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003214
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003215 /* Initialize each cell var, taking into account
3216 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003217
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003218 Should arrange for the compiler to put cellvars
3219 that are arguments at the beginning of the cellvars
3220 list so that we can march over it more efficiently?
3221 */
3222 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3223 cellname = PyString_AS_STRING(
3224 PyTuple_GET_ITEM(co->co_cellvars, i));
3225 found = 0;
3226 for (j = 0; j < nargs; j++) {
3227 argname = PyString_AS_STRING(
3228 PyTuple_GET_ITEM(co->co_varnames, j));
3229 if (strcmp(cellname, argname) == 0) {
3230 c = PyCell_New(GETLOCAL(j));
3231 if (c == NULL)
3232 goto fail;
3233 GETLOCAL(co->co_nlocals + i) = c;
3234 found = 1;
3235 break;
3236 }
3237 }
3238 if (found == 0) {
3239 c = PyCell_New(NULL);
3240 if (c == NULL)
3241 goto fail;
3242 SETLOCAL(co->co_nlocals + i, c);
3243 }
3244 }
3245 }
3246 if (PyTuple_GET_SIZE(co->co_freevars)) {
3247 int i;
3248 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3249 PyObject *o = PyTuple_GET_ITEM(closure, i);
3250 Py_INCREF(o);
3251 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3252 }
3253 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003254
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003255 if (co->co_flags & CO_GENERATOR) {
3256 /* Don't need to keep the reference to f_back, it will be set
3257 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003258 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003259
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003260 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003261
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003262 /* Create a new generator that owns the ready to run frame
3263 * and return that as the value. */
3264 return PyGen_New(f);
3265 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003266
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003267 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003268
Thomas Woutersae406c62007-09-19 17:27:43 +00003269fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003270
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003271 /* decref'ing the frame can cause __del__ methods to get invoked,
3272 which can call back into Python. While we're done with the
3273 current Python frame (f), the associated C stack is still in use,
3274 so recursion_depth must be boosted for the duration.
3275 */
3276 assert(tstate != NULL);
3277 ++tstate->recursion_depth;
3278 Py_DECREF(f);
3279 --tstate->recursion_depth;
3280 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003281}
3282
3283
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003284static PyObject *
3285special_lookup(PyObject *o, char *meth, PyObject **cache)
3286{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003287 PyObject *res;
3288 if (PyInstance_Check(o)) {
3289 if (!*cache)
3290 return PyObject_GetAttrString(o, meth);
3291 else
3292 return PyObject_GetAttr(o, *cache);
3293 }
3294 res = _PyObject_LookupSpecial(o, meth, cache);
3295 if (res == NULL && !PyErr_Occurred()) {
3296 PyErr_SetObject(PyExc_AttributeError, *cache);
3297 return NULL;
3298 }
3299 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003300}
3301
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003302
Benjamin Petersone18ef192009-01-20 14:21:16 +00003303static PyObject *
3304kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003305#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003306 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003307#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003308 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003309#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003310 Py_INCREF(kwd);
3311 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003312#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003313 }
3314 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003315#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003316}
3317
3318
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003319/* Implementation notes for set_exc_info() and reset_exc_info():
3320
3321- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3322 'exc_traceback'. These always travel together.
3323
3324- tstate->curexc_ZZZ is the "hot" exception that is set by
3325 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3326
3327- Once an exception is caught by an except clause, it is transferred
3328 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3329 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003330 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003331
3332- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3333
3334 Long ago, when none of this existed, there were just a few globals:
3335 one set corresponding to the "hot" exception, and one set
3336 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3337 globals; they were simply stored as sys.exc_ZZZ. For backwards
3338 compatibility, they still are!) The problem was that in code like
3339 this:
3340
3341 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003342 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003343 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003344 "do something else first"
3345 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003346
3347 if "do something else first" invoked something that raised and caught
3348 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3349 cause of subtle bugs. I fixed this by changing the semantics as
3350 follows:
3351
3352 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3353 *in that frame*.
3354
3355 - But initially, and as long as no exception is caught in a given
3356 frame, sys.exc_ZZZ will hold the last exception caught in the
3357 previous frame (or the frame before that, etc.).
3358
3359 The first bullet fixed the bug in the above example. The second
3360 bullet was for backwards compatibility: it was (and is) common to
3361 have a function that is called when an exception is caught, and to
3362 have that function access the caught exception via sys.exc_ZZZ.
3363 (Example: traceback.print_exc()).
3364
3365 At the same time I fixed the problem that sys.exc_ZZZ weren't
3366 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3367 but that's really a separate improvement.
3368
3369 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3370 variables to what they were before the current frame was called. The
3371 set_exc_info() function saves them on the frame so that
3372 reset_exc_info() can restore them. The invariant is that
3373 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3374 exception (where "catching" an exception applies only to successful
3375 except clauses); and if the current frame ever caught an exception,
3376 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3377 at the start of the current frame.
3378
3379*/
3380
Fredrik Lundh7a830892006-05-27 10:39:48 +00003381static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003382set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003383 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003384{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003385 PyFrameObject *frame = tstate->frame;
3386 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003387
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003388 assert(type != NULL);
3389 assert(frame != NULL);
3390 if (frame->f_exc_type == NULL) {
3391 assert(frame->f_exc_value == NULL);
3392 assert(frame->f_exc_traceback == NULL);
3393 /* This frame didn't catch an exception before. */
3394 /* Save previous exception of this thread in this frame. */
3395 if (tstate->exc_type == NULL) {
3396 /* XXX Why is this set to Py_None? */
3397 Py_INCREF(Py_None);
3398 tstate->exc_type = Py_None;
3399 }
3400 Py_INCREF(tstate->exc_type);
3401 Py_XINCREF(tstate->exc_value);
3402 Py_XINCREF(tstate->exc_traceback);
3403 frame->f_exc_type = tstate->exc_type;
3404 frame->f_exc_value = tstate->exc_value;
3405 frame->f_exc_traceback = tstate->exc_traceback;
3406 }
3407 /* Set new exception for this thread. */
3408 tmp_type = tstate->exc_type;
3409 tmp_value = tstate->exc_value;
3410 tmp_tb = tstate->exc_traceback;
3411 Py_INCREF(type);
3412 Py_XINCREF(value);
3413 Py_XINCREF(tb);
3414 tstate->exc_type = type;
3415 tstate->exc_value = value;
3416 tstate->exc_traceback = tb;
3417 Py_XDECREF(tmp_type);
3418 Py_XDECREF(tmp_value);
3419 Py_XDECREF(tmp_tb);
3420 /* For b/w compatibility */
3421 PySys_SetObject("exc_type", type);
3422 PySys_SetObject("exc_value", value);
3423 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003424}
3425
Fredrik Lundh7a830892006-05-27 10:39:48 +00003426static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003427reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003428{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003429 PyFrameObject *frame;
3430 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003432 /* It's a precondition that the thread state's frame caught an
3433 * exception -- verify in a debug build.
3434 */
3435 assert(tstate != NULL);
3436 frame = tstate->frame;
3437 assert(frame != NULL);
3438 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003439
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003440 /* Copy the frame's exception info back to the thread state. */
3441 tmp_type = tstate->exc_type;
3442 tmp_value = tstate->exc_value;
3443 tmp_tb = tstate->exc_traceback;
3444 Py_INCREF(frame->f_exc_type);
3445 Py_XINCREF(frame->f_exc_value);
3446 Py_XINCREF(frame->f_exc_traceback);
3447 tstate->exc_type = frame->f_exc_type;
3448 tstate->exc_value = frame->f_exc_value;
3449 tstate->exc_traceback = frame->f_exc_traceback;
3450 Py_XDECREF(tmp_type);
3451 Py_XDECREF(tmp_value);
3452 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003453
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003454 /* For b/w compatibility */
3455 PySys_SetObject("exc_type", frame->f_exc_type);
3456 PySys_SetObject("exc_value", frame->f_exc_value);
3457 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003458
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003459 /* Clear the frame's exception info. */
3460 tmp_type = frame->f_exc_type;
3461 tmp_value = frame->f_exc_value;
3462 tmp_tb = frame->f_exc_traceback;
3463 frame->f_exc_type = NULL;
3464 frame->f_exc_value = NULL;
3465 frame->f_exc_traceback = NULL;
3466 Py_DECREF(tmp_type);
3467 Py_XDECREF(tmp_value);
3468 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003469}
3470
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003471/* Logic for the raise statement (too complicated for inlining).
3472 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003473static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003474do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003475{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003476 if (type == NULL) {
3477 /* Reraise */
3478 PyThreadState *tstate = PyThreadState_GET();
3479 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3480 value = tstate->exc_value;
3481 tb = tstate->exc_traceback;
3482 Py_XINCREF(type);
3483 Py_XINCREF(value);
3484 Py_XINCREF(tb);
3485 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003486
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003487 /* We support the following forms of raise:
3488 raise <class>, <classinstance>
3489 raise <class>, <argument tuple>
3490 raise <class>, None
3491 raise <class>, <argument>
3492 raise <classinstance>, None
3493 raise <string>, <object>
3494 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003495
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003496 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003497
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003498 In addition, raise <tuple>, <anything> is the same as
3499 raising the tuple's first item (and it better have one!);
3500 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003501
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003502 Finally, an optional third argument can be supplied, which
3503 gives the traceback to be substituted (useful when
3504 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003506 /* First, check the traceback argument, replacing None with
3507 NULL. */
3508 if (tb == Py_None) {
3509 Py_DECREF(tb);
3510 tb = NULL;
3511 }
3512 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3513 PyErr_SetString(PyExc_TypeError,
3514 "raise: arg 3 must be a traceback or None");
3515 goto raise_error;
3516 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003517
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003518 /* Next, replace a missing value with None */
3519 if (value == NULL) {
3520 value = Py_None;
3521 Py_INCREF(value);
3522 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003523
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003524 /* Next, repeatedly, replace a tuple exception with its first item */
3525 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3526 PyObject *tmp = type;
3527 type = PyTuple_GET_ITEM(type, 0);
3528 Py_INCREF(type);
3529 Py_DECREF(tmp);
3530 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003531
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003532 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003533 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003534 if (!PyExceptionInstance_Check(value)) {
3535 PyErr_Format(PyExc_TypeError,
3536 "calling %s() should have returned an instance of "
3537 "BaseException, not '%s'",
3538 ((PyTypeObject *)type)->tp_name,
3539 Py_TYPE(value)->tp_name);
3540 goto raise_error;
3541 }
3542 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003543 else if (PyExceptionInstance_Check(type)) {
3544 /* Raising an instance. The value should be a dummy. */
3545 if (value != Py_None) {
3546 PyErr_SetString(PyExc_TypeError,
3547 "instance exception may not have a separate value");
3548 goto raise_error;
3549 }
3550 else {
3551 /* Normalize to raise <class>, <instance> */
3552 Py_DECREF(value);
3553 value = type;
3554 type = PyExceptionInstance_Class(type);
3555 Py_INCREF(type);
3556 }
3557 }
3558 else {
3559 /* Not something you can raise. You get an exception
3560 anyway, just not what you specified :-) */
3561 PyErr_Format(PyExc_TypeError,
3562 "exceptions must be old-style classes or "
3563 "derived from BaseException, not %s",
3564 type->ob_type->tp_name);
3565 goto raise_error;
3566 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003567
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003568 assert(PyExceptionClass_Check(type));
3569 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3570 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3571 "exceptions must derive from BaseException "
3572 "in 3.x", 1) < 0)
3573 goto raise_error;
3574 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003575
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003576 PyErr_Restore(type, value, tb);
3577 if (tb == NULL)
3578 return WHY_EXCEPTION;
3579 else
3580 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003581 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003582 Py_XDECREF(value);
3583 Py_XDECREF(type);
3584 Py_XDECREF(tb);
3585 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003586}
3587
Tim Petersd6d010b2001-06-21 02:49:55 +00003588/* Iterate v argcnt times and store the results on the stack (via decreasing
3589 sp). Return 1 for success, 0 if error. */
3590
Fredrik Lundh7a830892006-05-27 10:39:48 +00003591static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003592unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003593{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003594 int i = 0;
3595 PyObject *it; /* iter(v) */
3596 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003597
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003598 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003599
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003600 it = PyObject_GetIter(v);
3601 if (it == NULL)
3602 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003603
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003604 for (; i < argcnt; i++) {
3605 w = PyIter_Next(it);
3606 if (w == NULL) {
3607 /* Iterator done, via error or exhaustion. */
3608 if (!PyErr_Occurred()) {
3609 PyErr_Format(PyExc_ValueError,
3610 "need more than %d value%s to unpack",
3611 i, i == 1 ? "" : "s");
3612 }
3613 goto Error;
3614 }
3615 *--sp = w;
3616 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003617
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003618 /* We better have exhausted the iterator now. */
3619 w = PyIter_Next(it);
3620 if (w == NULL) {
3621 if (PyErr_Occurred())
3622 goto Error;
3623 Py_DECREF(it);
3624 return 1;
3625 }
3626 Py_DECREF(w);
3627 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3628 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003629Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003630 for (; i > 0; i--, sp++)
3631 Py_DECREF(*sp);
3632 Py_XDECREF(it);
3633 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003634}
3635
3636
Guido van Rossum96a42c81992-01-12 02:29:51 +00003637#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003638static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003639prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003640{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003641 printf("%s ", str);
3642 if (PyObject_Print(v, stdout, 0) != 0)
3643 PyErr_Clear(); /* Don't know what else to do */
3644 printf("\n");
3645 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003646}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003647#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003648
Fredrik Lundh7a830892006-05-27 10:39:48 +00003649static void
Fred Drake5755ce62001-06-27 19:19:46 +00003650call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003651{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003652 PyObject *type, *value, *traceback, *arg;
3653 int err;
3654 PyErr_Fetch(&type, &value, &traceback);
3655 if (value == NULL) {
3656 value = Py_None;
3657 Py_INCREF(value);
3658 }
3659 arg = PyTuple_Pack(3, type, value, traceback);
3660 if (arg == NULL) {
3661 PyErr_Restore(type, value, traceback);
3662 return;
3663 }
3664 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3665 Py_DECREF(arg);
3666 if (err == 0)
3667 PyErr_Restore(type, value, traceback);
3668 else {
3669 Py_XDECREF(type);
3670 Py_XDECREF(value);
3671 Py_XDECREF(traceback);
3672 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003673}
3674
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003675static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003676call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003677 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003678{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003679 PyObject *type, *value, *traceback;
3680 int err;
3681 PyErr_Fetch(&type, &value, &traceback);
3682 err = call_trace(func, obj, frame, what, arg);
3683 if (err == 0)
3684 {
3685 PyErr_Restore(type, value, traceback);
3686 return 0;
3687 }
3688 else {
3689 Py_XDECREF(type);
3690 Py_XDECREF(value);
3691 Py_XDECREF(traceback);
3692 return -1;
3693 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003694}
3695
Fredrik Lundh7a830892006-05-27 10:39:48 +00003696static int
Fred Drake5755ce62001-06-27 19:19:46 +00003697call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003698 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003699{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003700 register PyThreadState *tstate = frame->f_tstate;
3701 int result;
3702 if (tstate->tracing)
3703 return 0;
3704 tstate->tracing++;
3705 tstate->use_tracing = 0;
3706 result = func(obj, frame, what, arg);
3707 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3708 || (tstate->c_profilefunc != NULL));
3709 tstate->tracing--;
3710 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003711}
3712
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003713PyObject *
3714_PyEval_CallTracing(PyObject *func, PyObject *args)
3715{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003716 PyFrameObject *frame = PyEval_GetFrame();
3717 PyThreadState *tstate = frame->f_tstate;
3718 int save_tracing = tstate->tracing;
3719 int save_use_tracing = tstate->use_tracing;
3720 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003721
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003722 tstate->tracing = 0;
3723 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3724 || (tstate->c_profilefunc != NULL));
3725 result = PyObject_Call(func, args, NULL);
3726 tstate->tracing = save_tracing;
3727 tstate->use_tracing = save_use_tracing;
3728 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003729}
3730
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003731/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003732static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003733maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003734 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3735 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003736{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003737 int result = 0;
3738 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003739
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003740 /* If the last instruction executed isn't in the current
3741 instruction window, reset the window.
3742 */
3743 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3744 PyAddrPair bounds;
3745 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3746 &bounds);
3747 *instr_lb = bounds.ap_lower;
3748 *instr_ub = bounds.ap_upper;
3749 }
3750 /* If the last instruction falls at the start of a line or if
3751 it represents a jump backwards, update the frame's line
3752 number and call the trace function. */
3753 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3754 frame->f_lineno = line;
3755 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3756 }
3757 *instr_prev = frame->f_lasti;
3758 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003759}
3760
Fred Drake5755ce62001-06-27 19:19:46 +00003761void
3762PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003763{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003764 PyThreadState *tstate = PyThreadState_GET();
3765 PyObject *temp = tstate->c_profileobj;
3766 Py_XINCREF(arg);
3767 tstate->c_profilefunc = NULL;
3768 tstate->c_profileobj = NULL;
3769 /* Must make sure that tracing is not ignored if 'temp' is freed */
3770 tstate->use_tracing = tstate->c_tracefunc != NULL;
3771 Py_XDECREF(temp);
3772 tstate->c_profilefunc = func;
3773 tstate->c_profileobj = arg;
3774 /* Flag that tracing or profiling is turned on */
3775 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003776}
3777
3778void
3779PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3780{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003781 PyThreadState *tstate = PyThreadState_GET();
3782 PyObject *temp = tstate->c_traceobj;
3783 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3784 Py_XINCREF(arg);
3785 tstate->c_tracefunc = NULL;
3786 tstate->c_traceobj = NULL;
3787 /* Must make sure that profiling is not ignored if 'temp' is freed */
3788 tstate->use_tracing = tstate->c_profilefunc != NULL;
3789 Py_XDECREF(temp);
3790 tstate->c_tracefunc = func;
3791 tstate->c_traceobj = arg;
3792 /* Flag that tracing or profiling is turned on */
3793 tstate->use_tracing = ((func != NULL)
3794 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003795}
3796
Guido van Rossumb209a111997-04-29 18:18:01 +00003797PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003798PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003799{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003800 PyFrameObject *current_frame = PyEval_GetFrame();
3801 if (current_frame == NULL)
3802 return PyThreadState_GET()->interp->builtins;
3803 else
3804 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003805}
3806
Guido van Rossumb209a111997-04-29 18:18:01 +00003807PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003808PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003809{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003810 PyFrameObject *current_frame = PyEval_GetFrame();
3811 if (current_frame == NULL)
3812 return NULL;
3813 PyFrame_FastToLocals(current_frame);
3814 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003815}
3816
Guido van Rossumb209a111997-04-29 18:18:01 +00003817PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003818PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003819{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003820 PyFrameObject *current_frame = PyEval_GetFrame();
3821 if (current_frame == NULL)
3822 return NULL;
3823 else
3824 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003825}
3826
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003827PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003828PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003829{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003830 PyThreadState *tstate = PyThreadState_GET();
3831 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003832}
3833
Guido van Rossum6135a871995-01-09 17:53:26 +00003834int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003835PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003836{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003837 PyFrameObject *current_frame = PyEval_GetFrame();
3838 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003839}
3840
Guido van Rossumbe270261997-05-22 22:26:18 +00003841int
Tim Peters5ba58662001-07-16 02:29:45 +00003842PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003843{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003844 PyFrameObject *current_frame = PyEval_GetFrame();
3845 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003846
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003847 if (current_frame != NULL) {
3848 const int codeflags = current_frame->f_code->co_flags;
3849 const int compilerflags = codeflags & PyCF_MASK;
3850 if (compilerflags) {
3851 result = 1;
3852 cf->cf_flags |= compilerflags;
3853 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003854#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003855 if (codeflags & CO_GENERATOR_ALLOWED) {
3856 result = 1;
3857 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3858 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003859#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003860 }
3861 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003862}
3863
3864int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003865Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003866{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003867 PyObject *f = PySys_GetObject("stdout");
3868 if (f == NULL)
3869 return 0;
3870 if (!PyFile_SoftSpace(f, 0))
3871 return 0;
3872 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003873}
3874
Guido van Rossum3f5da241990-12-20 15:06:42 +00003875
Guido van Rossum681d79a1995-07-18 14:51:37 +00003876/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00003877 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003878
Guido van Rossumb209a111997-04-29 18:18:01 +00003879PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003880PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003881{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003882 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003884 if (arg == NULL) {
3885 arg = PyTuple_New(0);
3886 if (arg == NULL)
3887 return NULL;
3888 }
3889 else if (!PyTuple_Check(arg)) {
3890 PyErr_SetString(PyExc_TypeError,
3891 "argument list must be a tuple");
3892 return NULL;
3893 }
3894 else
3895 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003896
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003897 if (kw != NULL && !PyDict_Check(kw)) {
3898 PyErr_SetString(PyExc_TypeError,
3899 "keyword list must be a dictionary");
3900 Py_DECREF(arg);
3901 return NULL;
3902 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003903
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003904 result = PyObject_Call(func, arg, kw);
3905 Py_DECREF(arg);
3906 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003907}
3908
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003909const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003910PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003911{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003912 if (PyMethod_Check(func))
3913 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3914 else if (PyFunction_Check(func))
3915 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3916 else if (PyCFunction_Check(func))
3917 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3918 else if (PyClass_Check(func))
3919 return PyString_AsString(((PyClassObject*)func)->cl_name);
3920 else if (PyInstance_Check(func)) {
3921 return PyString_AsString(
3922 ((PyInstanceObject*)func)->in_class->cl_name);
3923 } else {
3924 return func->ob_type->tp_name;
3925 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003926}
3927
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003928const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003929PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003930{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003931 if (PyMethod_Check(func))
3932 return "()";
3933 else if (PyFunction_Check(func))
3934 return "()";
3935 else if (PyCFunction_Check(func))
3936 return "()";
3937 else if (PyClass_Check(func))
3938 return " constructor";
3939 else if (PyInstance_Check(func)) {
3940 return " instance";
3941 } else {
3942 return " object";
3943 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003944}
3945
Fredrik Lundh7a830892006-05-27 10:39:48 +00003946static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003947err_args(PyObject *func, int flags, int nargs)
3948{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003949 if (flags & METH_NOARGS)
3950 PyErr_Format(PyExc_TypeError,
3951 "%.200s() takes no arguments (%d given)",
3952 ((PyCFunctionObject *)func)->m_ml->ml_name,
3953 nargs);
3954 else
3955 PyErr_Format(PyExc_TypeError,
3956 "%.200s() takes exactly one argument (%d given)",
3957 ((PyCFunctionObject *)func)->m_ml->ml_name,
3958 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003959}
3960
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003961#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003962if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003963 if (call_trace(tstate->c_profilefunc, \
3964 tstate->c_profileobj, \
3965 tstate->frame, PyTrace_C_CALL, \
3966 func)) { \
3967 x = NULL; \
3968 } \
3969 else { \
3970 x = call; \
3971 if (tstate->c_profilefunc != NULL) { \
3972 if (x == NULL) { \
3973 call_trace_protected(tstate->c_profilefunc, \
3974 tstate->c_profileobj, \
3975 tstate->frame, PyTrace_C_EXCEPTION, \
3976 func); \
3977 /* XXX should pass (type, value, tb) */ \
3978 } else { \
3979 if (call_trace(tstate->c_profilefunc, \
3980 tstate->c_profileobj, \
3981 tstate->frame, PyTrace_C_RETURN, \
3982 func)) { \
3983 Py_DECREF(x); \
3984 x = NULL; \
3985 } \
3986 } \
3987 } \
3988 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003989} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003990 x = call; \
3991 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003992
Fredrik Lundh7a830892006-05-27 10:39:48 +00003993static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003994call_function(PyObject ***pp_stack, int oparg
3995#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003996 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003997#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003998 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003999{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004000 int na = oparg & 0xff;
4001 int nk = (oparg>>8) & 0xff;
4002 int n = na + 2 * nk;
4003 PyObject **pfunc = (*pp_stack) - n - 1;
4004 PyObject *func = *pfunc;
4005 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004007 /* Always dispatch PyCFunction first, because these are
4008 presumed to be the most frequent callable object.
4009 */
4010 if (PyCFunction_Check(func) && nk == 0) {
4011 int flags = PyCFunction_GET_FLAGS(func);
4012 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004013
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004014 PCALL(PCALL_CFUNCTION);
4015 if (flags & (METH_NOARGS | METH_O)) {
4016 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4017 PyObject *self = PyCFunction_GET_SELF(func);
4018 if (flags & METH_NOARGS && na == 0) {
4019 C_TRACE(x, (*meth)(self,NULL));
4020 }
4021 else if (flags & METH_O && na == 1) {
4022 PyObject *arg = EXT_POP(*pp_stack);
4023 C_TRACE(x, (*meth)(self,arg));
4024 Py_DECREF(arg);
4025 }
4026 else {
4027 err_args(func, flags, na);
4028 x = NULL;
4029 }
4030 }
4031 else {
4032 PyObject *callargs;
4033 callargs = load_args(pp_stack, na);
4034 READ_TIMESTAMP(*pintr0);
4035 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4036 READ_TIMESTAMP(*pintr1);
4037 Py_XDECREF(callargs);
4038 }
4039 } else {
4040 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4041 /* optimize access to bound methods */
4042 PyObject *self = PyMethod_GET_SELF(func);
4043 PCALL(PCALL_METHOD);
4044 PCALL(PCALL_BOUND_METHOD);
4045 Py_INCREF(self);
4046 func = PyMethod_GET_FUNCTION(func);
4047 Py_INCREF(func);
4048 Py_DECREF(*pfunc);
4049 *pfunc = self;
4050 na++;
4051 n++;
4052 } else
4053 Py_INCREF(func);
4054 READ_TIMESTAMP(*pintr0);
4055 if (PyFunction_Check(func))
4056 x = fast_function(func, pp_stack, n, na, nk);
4057 else
4058 x = do_call(func, pp_stack, na, nk);
4059 READ_TIMESTAMP(*pintr1);
4060 Py_DECREF(func);
4061 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004062
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004063 /* Clear the stack of the function object. Also removes
4064 the arguments in case they weren't consumed already
4065 (fast_function() and err_args() leave them on the stack).
4066 */
4067 while ((*pp_stack) > pfunc) {
4068 w = EXT_POP(*pp_stack);
4069 Py_DECREF(w);
4070 PCALL(PCALL_POP);
4071 }
4072 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004073}
4074
Jeremy Hylton192690e2002-08-16 18:36:11 +00004075/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004076 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004077 For the simplest case -- a function that takes only positional
4078 arguments and is called with only positional arguments -- it
4079 inlines the most primitive frame setup code from
4080 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4081 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004082*/
4083
Fredrik Lundh7a830892006-05-27 10:39:48 +00004084static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004085fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004086{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004087 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4088 PyObject *globals = PyFunction_GET_GLOBALS(func);
4089 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4090 PyObject **d = NULL;
4091 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004092
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004093 PCALL(PCALL_FUNCTION);
4094 PCALL(PCALL_FAST_FUNCTION);
4095 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4096 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4097 PyFrameObject *f;
4098 PyObject *retval = NULL;
4099 PyThreadState *tstate = PyThreadState_GET();
4100 PyObject **fastlocals, **stack;
4101 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004102
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004103 PCALL(PCALL_FASTER_FUNCTION);
4104 assert(globals != NULL);
4105 /* XXX Perhaps we should create a specialized
4106 PyFrame_New() that doesn't take locals, but does
4107 take builtins without sanity checking them.
4108 */
4109 assert(tstate != NULL);
4110 f = PyFrame_New(tstate, co, globals, NULL);
4111 if (f == NULL)
4112 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004113
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004114 fastlocals = f->f_localsplus;
4115 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004116
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004117 for (i = 0; i < n; i++) {
4118 Py_INCREF(*stack);
4119 fastlocals[i] = *stack++;
4120 }
4121 retval = PyEval_EvalFrameEx(f,0);
4122 ++tstate->recursion_depth;
4123 Py_DECREF(f);
4124 --tstate->recursion_depth;
4125 return retval;
4126 }
4127 if (argdefs != NULL) {
4128 d = &PyTuple_GET_ITEM(argdefs, 0);
4129 nd = Py_SIZE(argdefs);
4130 }
4131 return PyEval_EvalCodeEx(co, globals,
4132 (PyObject *)NULL, (*pp_stack)-n, na,
4133 (*pp_stack)-2*nk, nk, d, nd,
4134 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004135}
4136
Fredrik Lundh7a830892006-05-27 10:39:48 +00004137static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004138update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4139 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004140{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004141 PyObject *kwdict = NULL;
4142 if (orig_kwdict == NULL)
4143 kwdict = PyDict_New();
4144 else {
4145 kwdict = PyDict_Copy(orig_kwdict);
4146 Py_DECREF(orig_kwdict);
4147 }
4148 if (kwdict == NULL)
4149 return NULL;
4150 while (--nk >= 0) {
4151 int err;
4152 PyObject *value = EXT_POP(*pp_stack);
4153 PyObject *key = EXT_POP(*pp_stack);
4154 if (PyDict_GetItem(kwdict, key) != NULL) {
4155 PyErr_Format(PyExc_TypeError,
4156 "%.200s%s got multiple values "
4157 "for keyword argument '%.200s'",
4158 PyEval_GetFuncName(func),
4159 PyEval_GetFuncDesc(func),
4160 PyString_AsString(key));
4161 Py_DECREF(key);
4162 Py_DECREF(value);
4163 Py_DECREF(kwdict);
4164 return NULL;
4165 }
4166 err = PyDict_SetItem(kwdict, key, value);
4167 Py_DECREF(key);
4168 Py_DECREF(value);
4169 if (err) {
4170 Py_DECREF(kwdict);
4171 return NULL;
4172 }
4173 }
4174 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004175}
4176
Fredrik Lundh7a830892006-05-27 10:39:48 +00004177static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004178update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004179 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004180{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004181 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004182
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004183 callargs = PyTuple_New(nstack + nstar);
4184 if (callargs == NULL) {
4185 return NULL;
4186 }
4187 if (nstar) {
4188 int i;
4189 for (i = 0; i < nstar; i++) {
4190 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4191 Py_INCREF(a);
4192 PyTuple_SET_ITEM(callargs, nstack + i, a);
4193 }
4194 }
4195 while (--nstack >= 0) {
4196 w = EXT_POP(*pp_stack);
4197 PyTuple_SET_ITEM(callargs, nstack, w);
4198 }
4199 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004200}
4201
Fredrik Lundh7a830892006-05-27 10:39:48 +00004202static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004203load_args(PyObject ***pp_stack, int na)
4204{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004205 PyObject *args = PyTuple_New(na);
4206 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004207
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004208 if (args == NULL)
4209 return NULL;
4210 while (--na >= 0) {
4211 w = EXT_POP(*pp_stack);
4212 PyTuple_SET_ITEM(args, na, w);
4213 }
4214 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004215}
4216
Fredrik Lundh7a830892006-05-27 10:39:48 +00004217static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004218do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4219{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004220 PyObject *callargs = NULL;
4221 PyObject *kwdict = NULL;
4222 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004223
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004224 if (nk > 0) {
4225 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4226 if (kwdict == NULL)
4227 goto call_fail;
4228 }
4229 callargs = load_args(pp_stack, na);
4230 if (callargs == NULL)
4231 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004232#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004233 /* At this point, we have to look at the type of func to
4234 update the call stats properly. Do it here so as to avoid
4235 exposing the call stats machinery outside ceval.c
4236 */
4237 if (PyFunction_Check(func))
4238 PCALL(PCALL_FUNCTION);
4239 else if (PyMethod_Check(func))
4240 PCALL(PCALL_METHOD);
4241 else if (PyType_Check(func))
4242 PCALL(PCALL_TYPE);
4243 else if (PyCFunction_Check(func))
4244 PCALL(PCALL_CFUNCTION);
4245 else
4246 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004247#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004248 if (PyCFunction_Check(func)) {
4249 PyThreadState *tstate = PyThreadState_GET();
4250 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4251 }
4252 else
4253 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004254 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004255 Py_XDECREF(callargs);
4256 Py_XDECREF(kwdict);
4257 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004258}
4259
Fredrik Lundh7a830892006-05-27 10:39:48 +00004260static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004261ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4262{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004263 int nstar = 0;
4264 PyObject *callargs = NULL;
4265 PyObject *stararg = NULL;
4266 PyObject *kwdict = NULL;
4267 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004268
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004269 if (flags & CALL_FLAG_KW) {
4270 kwdict = EXT_POP(*pp_stack);
4271 if (!PyDict_Check(kwdict)) {
4272 PyObject *d;
4273 d = PyDict_New();
4274 if (d == NULL)
4275 goto ext_call_fail;
4276 if (PyDict_Update(d, kwdict) != 0) {
4277 Py_DECREF(d);
4278 /* PyDict_Update raises attribute
4279 * error (percolated from an attempt
4280 * to get 'keys' attribute) instead of
4281 * a type error if its second argument
4282 * is not a mapping.
4283 */
4284 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4285 PyErr_Format(PyExc_TypeError,
4286 "%.200s%.200s argument after ** "
4287 "must be a mapping, not %.200s",
4288 PyEval_GetFuncName(func),
4289 PyEval_GetFuncDesc(func),
4290 kwdict->ob_type->tp_name);
4291 }
4292 goto ext_call_fail;
4293 }
4294 Py_DECREF(kwdict);
4295 kwdict = d;
4296 }
4297 }
4298 if (flags & CALL_FLAG_VAR) {
4299 stararg = EXT_POP(*pp_stack);
4300 if (!PyTuple_Check(stararg)) {
4301 PyObject *t = NULL;
4302 t = PySequence_Tuple(stararg);
4303 if (t == NULL) {
4304 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4305 PyErr_Format(PyExc_TypeError,
4306 "%.200s%.200s argument after * "
4307 "must be a sequence, not %200s",
4308 PyEval_GetFuncName(func),
4309 PyEval_GetFuncDesc(func),
4310 stararg->ob_type->tp_name);
4311 }
4312 goto ext_call_fail;
4313 }
4314 Py_DECREF(stararg);
4315 stararg = t;
4316 }
4317 nstar = PyTuple_GET_SIZE(stararg);
4318 }
4319 if (nk > 0) {
4320 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4321 if (kwdict == NULL)
4322 goto ext_call_fail;
4323 }
4324 callargs = update_star_args(na, nstar, stararg, pp_stack);
4325 if (callargs == NULL)
4326 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004327#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004328 /* At this point, we have to look at the type of func to
4329 update the call stats properly. Do it here so as to avoid
4330 exposing the call stats machinery outside ceval.c
4331 */
4332 if (PyFunction_Check(func))
4333 PCALL(PCALL_FUNCTION);
4334 else if (PyMethod_Check(func))
4335 PCALL(PCALL_METHOD);
4336 else if (PyType_Check(func))
4337 PCALL(PCALL_TYPE);
4338 else if (PyCFunction_Check(func))
4339 PCALL(PCALL_CFUNCTION);
4340 else
4341 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004342#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004343 if (PyCFunction_Check(func)) {
4344 PyThreadState *tstate = PyThreadState_GET();
4345 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4346 }
4347 else
4348 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004349ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004350 Py_XDECREF(callargs);
4351 Py_XDECREF(kwdict);
4352 Py_XDECREF(stararg);
4353 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004354}
4355
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004356/* Extract a slice index from a PyInt or PyLong or an object with the
4357 nb_index slot defined, and store in *pi.
4358 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4359 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 +00004360 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004361*/
Tim Petersb5196382001-12-16 19:44:20 +00004362/* Note: If v is NULL, return success without storing into *pi. This
4363 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4364 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004365*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004366int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004367_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004368{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004369 if (v != NULL) {
4370 Py_ssize_t x;
4371 if (PyInt_Check(v)) {
4372 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4373 however, it looks like it should be AsSsize_t.
4374 There should be a comment here explaining why.
4375 */
4376 x = PyInt_AS_LONG(v);
4377 }
4378 else if (PyIndex_Check(v)) {
4379 x = PyNumber_AsSsize_t(v, NULL);
4380 if (x == -1 && PyErr_Occurred())
4381 return 0;
4382 }
4383 else {
4384 PyErr_SetString(PyExc_TypeError,
4385 "slice indices must be integers or "
4386 "None or have an __index__ method");
4387 return 0;
4388 }
4389 *pi = x;
4390 }
4391 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004392}
4393
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004394#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004395#define ISINDEX(x) ((x) == NULL || \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004396 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004397
Fredrik Lundh7a830892006-05-27 10:39:48 +00004398static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004399apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004400{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004401 PyTypeObject *tp = u->ob_type;
4402 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004403
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004404 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4405 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4406 if (!_PyEval_SliceIndex(v, &ilow))
4407 return NULL;
4408 if (!_PyEval_SliceIndex(w, &ihigh))
4409 return NULL;
4410 return PySequence_GetSlice(u, ilow, ihigh);
4411 }
4412 else {
4413 PyObject *slice = PySlice_New(v, w, NULL);
4414 if (slice != NULL) {
4415 PyObject *res = PyObject_GetItem(u, slice);
4416 Py_DECREF(slice);
4417 return res;
4418 }
4419 else
4420 return NULL;
4421 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004422}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004423
Fredrik Lundh7a830892006-05-27 10:39:48 +00004424static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004425assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004426 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004427{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004428 PyTypeObject *tp = u->ob_type;
4429 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004430
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004431 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4432 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4433 if (!_PyEval_SliceIndex(v, &ilow))
4434 return -1;
4435 if (!_PyEval_SliceIndex(w, &ihigh))
4436 return -1;
4437 if (x == NULL)
4438 return PySequence_DelSlice(u, ilow, ihigh);
4439 else
4440 return PySequence_SetSlice(u, ilow, ihigh, x);
4441 }
4442 else {
4443 PyObject *slice = PySlice_New(v, w, NULL);
4444 if (slice != NULL) {
4445 int res;
4446 if (x != NULL)
4447 res = PyObject_SetItem(u, slice, x);
4448 else
4449 res = PyObject_DelItem(u, slice);
4450 Py_DECREF(slice);
4451 return res;
4452 }
4453 else
4454 return -1;
4455 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004456}
4457
Guido van Rossum04edb522008-03-18 02:49:46 +00004458#define Py3kExceptionClass_Check(x) \
4459 (PyType_Check((x)) && \
4460 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4461
4462#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004463 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004464
Fredrik Lundh7a830892006-05-27 10:39:48 +00004465static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004466cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004467{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004468 int res = 0;
4469 switch (op) {
4470 case PyCmp_IS:
4471 res = (v == w);
4472 break;
4473 case PyCmp_IS_NOT:
4474 res = (v != w);
4475 break;
4476 case PyCmp_IN:
4477 res = PySequence_Contains(w, v);
4478 if (res < 0)
4479 return NULL;
4480 break;
4481 case PyCmp_NOT_IN:
4482 res = PySequence_Contains(w, v);
4483 if (res < 0)
4484 return NULL;
4485 res = !res;
4486 break;
4487 case PyCmp_EXC_MATCH:
4488 if (PyTuple_Check(w)) {
4489 Py_ssize_t i, length;
4490 length = PyTuple_Size(w);
4491 for (i = 0; i < length; i += 1) {
4492 PyObject *exc = PyTuple_GET_ITEM(w, i);
4493 if (PyString_Check(exc)) {
4494 int ret_val;
4495 ret_val = PyErr_WarnEx(
4496 PyExc_DeprecationWarning,
4497 "catching of string "
4498 "exceptions is deprecated", 1);
4499 if (ret_val < 0)
4500 return NULL;
4501 }
4502 else if (Py_Py3kWarningFlag &&
4503 !PyTuple_Check(exc) &&
4504 !Py3kExceptionClass_Check(exc))
4505 {
4506 int ret_val;
4507 ret_val = PyErr_WarnEx(
4508 PyExc_DeprecationWarning,
4509 CANNOT_CATCH_MSG, 1);
4510 if (ret_val < 0)
4511 return NULL;
4512 }
4513 }
4514 }
4515 else {
4516 if (PyString_Check(w)) {
4517 int ret_val;
4518 ret_val = PyErr_WarnEx(
4519 PyExc_DeprecationWarning,
4520 "catching of string "
4521 "exceptions is deprecated", 1);
4522 if (ret_val < 0)
4523 return NULL;
4524 }
4525 else if (Py_Py3kWarningFlag &&
4526 !PyTuple_Check(w) &&
4527 !Py3kExceptionClass_Check(w))
4528 {
4529 int ret_val;
4530 ret_val = PyErr_WarnEx(
4531 PyExc_DeprecationWarning,
4532 CANNOT_CATCH_MSG, 1);
4533 if (ret_val < 0)
4534 return NULL;
4535 }
4536 }
4537 res = PyErr_GivenExceptionMatches(v, w);
4538 break;
4539 default:
4540 return PyObject_RichCompare(v, w, op);
4541 }
4542 v = res ? Py_True : Py_False;
4543 Py_INCREF(v);
4544 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004545}
4546
Fredrik Lundh7a830892006-05-27 10:39:48 +00004547static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004548import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004549{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004550 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004551
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004552 x = PyObject_GetAttr(v, name);
4553 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4554 PyErr_Format(PyExc_ImportError,
4555 "cannot import name %.230s",
4556 PyString_AsString(name));
4557 }
4558 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004559}
Guido van Rossumac7be682001-01-17 15:42:30 +00004560
Fredrik Lundh7a830892006-05-27 10:39:48 +00004561static int
Thomas Wouters52152252000-08-17 22:55:00 +00004562import_all_from(PyObject *locals, PyObject *v)
4563{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004564 PyObject *all = PyObject_GetAttrString(v, "__all__");
4565 PyObject *dict, *name, *value;
4566 int skip_leading_underscores = 0;
4567 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004568
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004569 if (all == NULL) {
4570 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4571 return -1; /* Unexpected error */
4572 PyErr_Clear();
4573 dict = PyObject_GetAttrString(v, "__dict__");
4574 if (dict == NULL) {
4575 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4576 return -1;
4577 PyErr_SetString(PyExc_ImportError,
4578 "from-import-* object has no __dict__ and no __all__");
4579 return -1;
4580 }
4581 all = PyMapping_Keys(dict);
4582 Py_DECREF(dict);
4583 if (all == NULL)
4584 return -1;
4585 skip_leading_underscores = 1;
4586 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004587
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004588 for (pos = 0, err = 0; ; pos++) {
4589 name = PySequence_GetItem(all, pos);
4590 if (name == NULL) {
4591 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4592 err = -1;
4593 else
4594 PyErr_Clear();
4595 break;
4596 }
4597 if (skip_leading_underscores &&
4598 PyString_Check(name) &&
4599 PyString_AS_STRING(name)[0] == '_')
4600 {
4601 Py_DECREF(name);
4602 continue;
4603 }
4604 value = PyObject_GetAttr(v, name);
4605 if (value == NULL)
4606 err = -1;
4607 else if (PyDict_CheckExact(locals))
4608 err = PyDict_SetItem(locals, name, value);
4609 else
4610 err = PyObject_SetItem(locals, name, value);
4611 Py_DECREF(name);
4612 Py_XDECREF(value);
4613 if (err != 0)
4614 break;
4615 }
4616 Py_DECREF(all);
4617 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004618}
4619
Fredrik Lundh7a830892006-05-27 10:39:48 +00004620static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004621build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004622{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004623 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004624
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004625 if (PyDict_Check(methods))
4626 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4627 if (metaclass != NULL)
4628 Py_INCREF(metaclass);
4629 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4630 base = PyTuple_GET_ITEM(bases, 0);
4631 metaclass = PyObject_GetAttrString(base, "__class__");
4632 if (metaclass == NULL) {
4633 PyErr_Clear();
4634 metaclass = (PyObject *)base->ob_type;
4635 Py_INCREF(metaclass);
4636 }
4637 }
4638 else {
4639 PyObject *g = PyEval_GetGlobals();
4640 if (g != NULL && PyDict_Check(g))
4641 metaclass = PyDict_GetItemString(g, "__metaclass__");
4642 if (metaclass == NULL)
4643 metaclass = (PyObject *) &PyClass_Type;
4644 Py_INCREF(metaclass);
4645 }
4646 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4647 NULL);
4648 Py_DECREF(metaclass);
4649 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4650 /* A type error here likely means that the user passed
4651 in a base that was not a class (such the random module
4652 instead of the random.random type). Help them out with
4653 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00004654
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004655 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00004656
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004657 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4658 if (PyString_Check(pvalue)) {
4659 PyObject *newmsg;
4660 newmsg = PyString_FromFormat(
4661 "Error when calling the metaclass bases\n"
4662 " %s",
4663 PyString_AS_STRING(pvalue));
4664 if (newmsg != NULL) {
4665 Py_DECREF(pvalue);
4666 pvalue = newmsg;
4667 }
4668 }
4669 PyErr_Restore(ptype, pvalue, ptraceback);
4670 }
4671 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004672}
4673
Fredrik Lundh7a830892006-05-27 10:39:48 +00004674static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004675exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004676 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004677{
Benjamin Petersond2903bd2014-08-09 19:39:36 -07004678 int n;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004679 PyObject *v;
4680 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004681
Benjamin Petersond2903bd2014-08-09 19:39:36 -07004682 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4683 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4684 /* Backward compatibility hack */
4685 globals = PyTuple_GetItem(prog, 1);
4686 if (n == 3)
4687 locals = PyTuple_GetItem(prog, 2);
4688 prog = PyTuple_GetItem(prog, 0);
4689 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004690 if (globals == Py_None) {
4691 globals = PyEval_GetGlobals();
4692 if (locals == Py_None) {
4693 locals = PyEval_GetLocals();
4694 plain = 1;
4695 }
4696 if (!globals || !locals) {
4697 PyErr_SetString(PyExc_SystemError,
4698 "globals and locals cannot be NULL");
4699 return -1;
4700 }
4701 }
4702 else if (locals == Py_None)
4703 locals = globals;
4704 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004705#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004706 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004707#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004708 !PyCode_Check(prog) &&
4709 !PyFile_Check(prog)) {
4710 PyErr_SetString(PyExc_TypeError,
4711 "exec: arg 1 must be a string, file, or code object");
4712 return -1;
4713 }
4714 if (!PyDict_Check(globals)) {
4715 PyErr_SetString(PyExc_TypeError,
4716 "exec: arg 2 must be a dictionary or None");
4717 return -1;
4718 }
4719 if (!PyMapping_Check(locals)) {
4720 PyErr_SetString(PyExc_TypeError,
4721 "exec: arg 3 must be a mapping or None");
4722 return -1;
4723 }
4724 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4725 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4726 if (PyCode_Check(prog)) {
4727 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4728 PyErr_SetString(PyExc_TypeError,
4729 "code object passed to exec may not contain free variables");
4730 return -1;
4731 }
4732 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4733 }
4734 else if (PyFile_Check(prog)) {
4735 FILE *fp = PyFile_AsFile(prog);
4736 char *name = PyString_AsString(PyFile_Name(prog));
4737 PyCompilerFlags cf;
4738 if (name == NULL)
4739 return -1;
4740 cf.cf_flags = 0;
4741 if (PyEval_MergeCompilerFlags(&cf))
4742 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4743 locals, &cf);
4744 else
4745 v = PyRun_File(fp, name, Py_file_input, globals,
4746 locals);
4747 }
4748 else {
4749 PyObject *tmp = NULL;
4750 char *str;
4751 PyCompilerFlags cf;
4752 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004753#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004754 if (PyUnicode_Check(prog)) {
4755 tmp = PyUnicode_AsUTF8String(prog);
4756 if (tmp == NULL)
4757 return -1;
4758 prog = tmp;
4759 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4760 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004761#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004762 if (PyString_AsStringAndSize(prog, &str, NULL))
4763 return -1;
4764 if (PyEval_MergeCompilerFlags(&cf))
4765 v = PyRun_StringFlags(str, Py_file_input, globals,
4766 locals, &cf);
4767 else
4768 v = PyRun_String(str, Py_file_input, globals, locals);
4769 Py_XDECREF(tmp);
4770 }
4771 if (plain)
4772 PyFrame_LocalsToFast(f, 0);
4773 if (v == NULL)
4774 return -1;
4775 Py_DECREF(v);
4776 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004777}
Guido van Rossum24c13741995-02-14 09:42:43 +00004778
Fredrik Lundh7a830892006-05-27 10:39:48 +00004779static void
Paul Prescode68140d2000-08-30 20:25:01 +00004780format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4781{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004782 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004783
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004784 if (!obj)
4785 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004786
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004787 obj_str = PyString_AsString(obj);
4788 if (!obj_str)
4789 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004790
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004791 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004792}
Guido van Rossum950361c1997-01-24 13:49:28 +00004793
Fredrik Lundh7a830892006-05-27 10:39:48 +00004794static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004795string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004796 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004797{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004798 /* This function implements 'variable += expr' when both arguments
4799 are strings. */
4800 Py_ssize_t v_len = PyString_GET_SIZE(v);
4801 Py_ssize_t w_len = PyString_GET_SIZE(w);
4802 Py_ssize_t new_len = v_len + w_len;
4803 if (new_len < 0) {
4804 PyErr_SetString(PyExc_OverflowError,
4805 "strings are too large to concat");
4806 return NULL;
4807 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004808
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004809 if (v->ob_refcnt == 2) {
4810 /* In the common case, there are 2 references to the value
4811 * stored in 'variable' when the += is performed: one on the
4812 * value stack (in 'v') and one still stored in the
4813 * 'variable'. We try to delete the variable now to reduce
4814 * the refcnt to 1.
4815 */
4816 switch (*next_instr) {
4817 case STORE_FAST:
4818 {
4819 int oparg = PEEKARG();
4820 PyObject **fastlocals = f->f_localsplus;
4821 if (GETLOCAL(oparg) == v)
4822 SETLOCAL(oparg, NULL);
4823 break;
4824 }
4825 case STORE_DEREF:
4826 {
4827 PyObject **freevars = (f->f_localsplus +
4828 f->f_code->co_nlocals);
4829 PyObject *c = freevars[PEEKARG()];
4830 if (PyCell_GET(c) == v)
4831 PyCell_Set(c, NULL);
4832 break;
4833 }
4834 case STORE_NAME:
4835 {
4836 PyObject *names = f->f_code->co_names;
4837 PyObject *name = GETITEM(names, PEEKARG());
4838 PyObject *locals = f->f_locals;
4839 if (PyDict_CheckExact(locals) &&
4840 PyDict_GetItem(locals, name) == v) {
4841 if (PyDict_DelItem(locals, name) != 0) {
4842 PyErr_Clear();
4843 }
4844 }
4845 break;
4846 }
4847 }
4848 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004849
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004850 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4851 /* Now we own the last reference to 'v', so we can resize it
4852 * in-place.
4853 */
4854 if (_PyString_Resize(&v, new_len) != 0) {
4855 /* XXX if _PyString_Resize() fails, 'v' has been
4856 * deallocated so it cannot be put back into
4857 * 'variable'. The MemoryError is raised when there
4858 * is no value in 'variable', which might (very
4859 * remotely) be a cause of incompatibilities.
4860 */
4861 return NULL;
4862 }
4863 /* copy 'w' into the newly allocated area of 'v' */
4864 memcpy(PyString_AS_STRING(v) + v_len,
4865 PyString_AS_STRING(w), w_len);
4866 return v;
4867 }
4868 else {
4869 /* When in-place resizing is not an option. */
4870 PyString_Concat(&v, w);
4871 return v;
4872 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004873}
4874
Guido van Rossum950361c1997-01-24 13:49:28 +00004875#ifdef DYNAMIC_EXECUTION_PROFILE
4876
Fredrik Lundh7a830892006-05-27 10:39:48 +00004877static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004878getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004879{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004880 int i;
4881 PyObject *l = PyList_New(256);
4882 if (l == NULL) return NULL;
4883 for (i = 0; i < 256; i++) {
4884 PyObject *x = PyInt_FromLong(a[i]);
4885 if (x == NULL) {
4886 Py_DECREF(l);
4887 return NULL;
4888 }
4889 PyList_SetItem(l, i, x);
4890 }
4891 for (i = 0; i < 256; i++)
4892 a[i] = 0;
4893 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004894}
4895
4896PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004897_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004898{
4899#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004900 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004901#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004902 int i;
4903 PyObject *l = PyList_New(257);
4904 if (l == NULL) return NULL;
4905 for (i = 0; i < 257; i++) {
4906 PyObject *x = getarray(dxpairs[i]);
4907 if (x == NULL) {
4908 Py_DECREF(l);
4909 return NULL;
4910 }
4911 PyList_SetItem(l, i, x);
4912 }
4913 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004914#endif
4915}
4916
4917#endif