blob: e00860831f15f3d4425db0f42b167628480da45b [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
69#define READ_TIMESTAMP(val) \
70 __asm__ __volatile__("rdtsc" : \
71 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
72
73
74#else
75
76#error "Don't know how to implement timestamp counter for this architecture"
77
Michael W. Hudson800ba232004-08-12 18:19:17 +000078#endif
79
Tim Peters7df5e7f2006-05-26 23:14:37 +000080void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000082{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 uint64 intr, inst, loop;
84 PyThreadState *tstate = PyThreadState_Get();
85 if (!tstate->interp->tscdump)
86 return;
87 intr = intr1 - intr0;
88 inst = inst1 - inst0 - intr;
89 loop = loop1 - loop0 - intr;
90 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krah7ff78252010-06-23 18:12:09 +000091 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092}
Michael W. Hudson800ba232004-08-12 18:19:17 +000093
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000094#endif
95
Guido van Rossum04691fc1992-08-12 15:35:34 +000096/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000097/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000098
Guido van Rossum408027e1996-12-30 16:17:54 +000099#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000100/* For debugging the interpreter: */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000101#define LLTRACE 1 /* Low-level trace feature */
102#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000103#endif
104
Jeremy Hylton52820442001-01-03 23:52:36 +0000105typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000106
Guido van Rossum374a9221991-04-04 10:40:29 +0000107/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000108#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +0000109static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000110#else
Fredrik Lundh7a830892006-05-27 10:39:48 +0000111static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000112#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000113static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
114static PyObject * do_call(PyObject *, PyObject ***, int, int);
115static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +0000116static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000117 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000118static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
119static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000120#define CALL_FLAG_VAR 1
121#define CALL_FLAG_KW 2
122
Guido van Rossum0a066c01992-03-27 17:29:15 +0000123#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000124static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000125static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000126#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000127static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000128 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000129static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000130 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000131static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
132static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000133 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000134
Fredrik Lundh7a830892006-05-27 10:39:48 +0000135static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
136static int assign_slice(PyObject *, PyObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000137 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000138static PyObject * cmp_outcome(int, PyObject *, PyObject *);
139static PyObject * import_from(PyObject *, PyObject *);
140static int import_all_from(PyObject *, PyObject *);
141static PyObject * build_class(PyObject *, PyObject *, PyObject *);
142static int exec_statement(PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000143 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000144static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
145static void reset_exc_info(PyThreadState *);
146static void format_exc_check_arg(PyObject *, char *, PyObject *);
147static PyObject * string_concatenate(PyObject *, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000148 PyFrameObject *, unsigned char *);
Benjamin Petersone18ef192009-01-20 14:21:16 +0000149static PyObject * kwd_as_string(PyObject *);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000150static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Paul Prescode68140d2000-08-30 20:25:01 +0000152#define NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000154#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000155 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000156#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000158#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 "free variable '%.200s' referenced before assignment" \
160 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000161
Guido van Rossum950361c1997-01-24 13:49:28 +0000162/* Dynamic execution profile */
163#ifdef DYNAMIC_EXECUTION_PROFILE
164#ifdef DXPAIRS
165static long dxpairs[257][256];
166#define dxp dxpairs[256]
167#else
168static long dxp[256];
169#endif
170#endif
171
Jeremy Hylton985eba52003-02-05 23:13:00 +0000172/* Function call profile */
173#ifdef CALL_PROFILE
174#define PCALL_NUM 11
175static int pcall[PCALL_NUM];
176
177#define PCALL_ALL 0
178#define PCALL_FUNCTION 1
179#define PCALL_FAST_FUNCTION 2
180#define PCALL_FASTER_FUNCTION 3
181#define PCALL_METHOD 4
182#define PCALL_BOUND_METHOD 5
183#define PCALL_CFUNCTION 6
184#define PCALL_TYPE 7
185#define PCALL_GENERATOR 8
186#define PCALL_OTHER 9
187#define PCALL_POP 10
188
189/* Notes about the statistics
190
191 PCALL_FAST stats
192
193 FAST_FUNCTION means no argument tuple needs to be created.
194 FASTER_FUNCTION means that the fast-path frame setup code is used.
195
196 If there is a method call where the call can be optimized by changing
197 the argument tuple and calling the function directly, it gets recorded
198 twice.
199
200 As a result, the relationship among the statistics appears to be
201 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
202 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
203 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
204 PCALL_METHOD > PCALL_BOUND_METHOD
205*/
206
207#define PCALL(POS) pcall[POS]++
208
209PyObject *
210PyEval_GetCallStats(PyObject *self)
211{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 return Py_BuildValue("iiiiiiiiiii",
213 pcall[0], pcall[1], pcall[2], pcall[3],
214 pcall[4], pcall[5], pcall[6], pcall[7],
215 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#else
218#define PCALL(O)
219
220PyObject *
221PyEval_GetCallStats(PyObject *self)
222{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 Py_INCREF(Py_None);
224 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000225}
226#endif
227
Tim Peters5ca576e2001-06-18 22:08:13 +0000228
Guido van Rossume59214e1994-08-30 08:01:59 +0000229#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000230
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000231#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000232#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000233#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000234#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000235
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000236static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000237static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000238static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000239
Tim Peters7f468f22004-10-11 02:40:51 +0000240int
241PyEval_ThreadsInitialized(void)
242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 return interpreter_lock != 0;
Tim Peters7f468f22004-10-11 02:40:51 +0000244}
245
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 if (interpreter_lock)
250 return;
251 interpreter_lock = PyThread_allocate_lock();
252 PyThread_acquire_lock(interpreter_lock, 1);
253 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000254}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000255
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000256void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000258{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000259 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260}
261
262void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000264{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266}
267
268void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000270{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 if (tstate == NULL)
272 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
273 /* Check someone has called PyEval_InitThreads() to create the lock */
274 assert(interpreter_lock);
275 PyThread_acquire_lock(interpreter_lock, 1);
276 if (PyThreadState_Swap(tstate) != NULL)
277 Py_FatalError(
278 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000279}
280
281void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 if (tstate == NULL)
285 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
286 if (PyThreadState_Swap(NULL) != tstate)
287 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
288 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000289}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000290
291/* This function is called from PyOS_AfterFork to ensure that newly
292 created child processes don't hold locks referring to threads which
293 are not running in the child process. (This could also be done using
294 pthread_atfork mechanism, at least for the pthreads implementation.) */
295
296void
297PyEval_ReInitThreads(void)
298{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000299 PyObject *threading, *result;
300 PyThreadState *tstate;
Jesse Noller5e62ca42008-07-16 20:03:47 +0000301
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 if (!interpreter_lock)
303 return;
304 /*XXX Can't use PyThread_free_lock here because it does too
305 much error-checking. Doing this cleanly would require
306 adding a new function to each thread_*.h. Instead, just
307 create a new lock and waste a little bit of memory */
308 interpreter_lock = PyThread_allocate_lock();
309 pending_lock = PyThread_allocate_lock();
310 PyThread_acquire_lock(interpreter_lock, 1);
311 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000312
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 /* Update the threading module with the new state.
314 */
315 tstate = PyThreadState_GET();
316 threading = PyMapping_GetItemString(tstate->interp->modules,
317 "threading");
318 if (threading == NULL) {
319 /* threading not imported */
320 PyErr_Clear();
321 return;
322 }
323 result = PyObject_CallMethod(threading, "_after_fork", NULL);
324 if (result == NULL)
325 PyErr_WriteUnraisable(threading);
326 else
327 Py_DECREF(result);
328 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000329}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330#endif
331
Guido van Rossumff4949e1992-08-05 19:58:53 +0000332/* Functions save_thread and restore_thread are always defined so
333 dynamically loaded modules needn't be compiled separately for use
334 with and without threads: */
335
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000336PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000337PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000338{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000339 PyThreadState *tstate = PyThreadState_Swap(NULL);
340 if (tstate == NULL)
341 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000342#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000343 if (interpreter_lock)
344 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000345#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347}
348
349void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000350PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000351{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 if (tstate == NULL)
353 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000354#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000355 if (interpreter_lock) {
356 int err = errno;
357 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Petersonc0bc4ef2014-06-16 19:39:18 -0700358 /* _Py_Finalizing is protected by the GIL */
359 if (_Py_Finalizing && tstate != _Py_Finalizing) {
360 PyThread_release_lock(interpreter_lock);
361 PyThread_exit_thread();
362 assert(0); /* unreachable */
363 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 errno = err;
365 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000366#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000367 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368}
369
370
Guido van Rossuma9672091994-09-14 13:31:22 +0000371/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
372 signal handlers or Mac I/O completion routines) can schedule calls
373 to a function to be called synchronously.
374 The synchronous function is called with one void* argument.
375 It should return 0 for success or -1 for failure -- failure should
376 be accompanied by an exception.
377
378 If registry succeeds, the registry function returns 0; if it fails
379 (e.g. due to too many pending calls) it returns -1 (without setting
380 an exception condition).
381
382 Note that because registry may occur from within signal handlers,
383 or other asynchronous events, calling malloc() is unsafe!
384
385#ifdef WITH_THREAD
386 Any thread can schedule pending calls, but only the main thread
387 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000388 There is no facility to schedule calls to a particular thread, but
389 that should be easy to change, should that ever be required. In
390 that case, the static variables here should go into the python
391 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000392#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000393*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000394
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000395#ifdef WITH_THREAD
396
397/* The WITH_THREAD implementation is thread-safe. It allows
398 scheduling to be made from any thread, and even from an executing
399 callback.
400 */
401
402#define NPENDINGCALLS 32
403static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 int (*func)(void *);
405 void *arg;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000406} pendingcalls[NPENDINGCALLS];
407static int pendingfirst = 0;
408static int pendinglast = 0;
409static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
410static char pendingbusy = 0;
411
412int
413Py_AddPendingCall(int (*func)(void *), void *arg)
414{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 int i, j, result=0;
416 PyThread_type_lock lock = pending_lock;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000417
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 /* try a few times for the lock. Since this mechanism is used
419 * for signal handling (on the main thread), there is a (slim)
420 * chance that a signal is delivered on the same thread while we
421 * hold the lock during the Py_MakePendingCalls() function.
422 * This avoids a deadlock in that case.
423 * Note that signals can be delivered on any thread. In particular,
424 * on Windows, a SIGINT is delivered on a system-created worker
425 * thread.
426 * We also check for lock being NULL, in the unlikely case that
427 * this function is called before any bytecode evaluation takes place.
428 */
429 if (lock != NULL) {
430 for (i = 0; i<100; i++) {
431 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
432 break;
433 }
434 if (i == 100)
435 return -1;
436 }
437
438 i = pendinglast;
439 j = (i + 1) % NPENDINGCALLS;
440 if (j == pendingfirst) {
441 result = -1; /* Queue full */
442 } else {
443 pendingcalls[i].func = func;
444 pendingcalls[i].arg = arg;
445 pendinglast = j;
446 }
447 /* signal main loop */
448 _Py_Ticker = 0;
449 pendingcalls_to_do = 1;
450 if (lock != NULL)
451 PyThread_release_lock(lock);
452 return result;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000453}
454
455int
456Py_MakePendingCalls(void)
457{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 int i;
459 int r = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000460
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000461 if (!pending_lock) {
462 /* initial allocation of the lock */
463 pending_lock = PyThread_allocate_lock();
464 if (pending_lock == NULL)
465 return -1;
466 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 /* only service pending calls on main thread */
469 if (main_thread && PyThread_get_thread_ident() != main_thread)
470 return 0;
471 /* don't perform recursive pending calls */
472 if (pendingbusy)
473 return 0;
474 pendingbusy = 1;
475 /* perform a bounded number of calls, in case of recursion */
476 for (i=0; i<NPENDINGCALLS; i++) {
477 int j;
478 int (*func)(void *);
479 void *arg = NULL;
480
481 /* pop one item off the queue while holding the lock */
482 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
483 j = pendingfirst;
484 if (j == pendinglast) {
485 func = NULL; /* Queue empty */
486 } else {
487 func = pendingcalls[j].func;
488 arg = pendingcalls[j].arg;
489 pendingfirst = (j + 1) % NPENDINGCALLS;
490 }
491 pendingcalls_to_do = pendingfirst != pendinglast;
492 PyThread_release_lock(pending_lock);
493 /* having released the lock, perform the callback */
494 if (func == NULL)
495 break;
496 r = func(arg);
497 if (r)
498 break;
499 }
500 pendingbusy = 0;
501 return r;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000502}
503
504#else /* if ! defined WITH_THREAD */
505
506/*
507 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
508 This code is used for signal handling in python that isn't built
509 with WITH_THREAD.
510 Don't use this implementation when Py_AddPendingCalls() can happen
511 on a different thread!
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000512
Guido van Rossuma9672091994-09-14 13:31:22 +0000513 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000514 (1) nested asynchronous calls to Py_AddPendingCall()
515 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000517 (1) is very unlikely because typically signal delivery
518 is blocked during signal handling. So it should be impossible.
519 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000520 The current code is safe against (2), but not against (1).
521 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000522 thread is present, interrupted by signals, and that the critical
523 section is protected with the "busy" variable. On Windows, which
524 delivers SIGINT on a system thread, this does not hold and therefore
525 Windows really shouldn't use this version.
526 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000527*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000528
Guido van Rossuma9672091994-09-14 13:31:22 +0000529#define NPENDINGCALLS 32
530static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531 int (*func)(void *);
532 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000533} pendingcalls[NPENDINGCALLS];
534static volatile int pendingfirst = 0;
535static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000536static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000537
538int
Thomas Wouters334fb892000-07-25 12:56:38 +0000539Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000540{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000541 static volatile int busy = 0;
542 int i, j;
543 /* XXX Begin critical section */
544 if (busy)
545 return -1;
546 busy = 1;
547 i = pendinglast;
548 j = (i + 1) % NPENDINGCALLS;
549 if (j == pendingfirst) {
550 busy = 0;
551 return -1; /* Queue full */
552 }
553 pendingcalls[i].func = func;
554 pendingcalls[i].arg = arg;
555 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000556
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000557 _Py_Ticker = 0;
558 pendingcalls_to_do = 1; /* Signal main loop */
559 busy = 0;
560 /* XXX End critical section */
561 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000562}
563
Guido van Rossum180d7b41994-09-29 09:45:57 +0000564int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000565Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000566{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000567 static int busy = 0;
568 if (busy)
569 return 0;
570 busy = 1;
571 pendingcalls_to_do = 0;
572 for (;;) {
573 int i;
574 int (*func)(void *);
575 void *arg;
576 i = pendingfirst;
577 if (i == pendinglast)
578 break; /* Queue empty */
579 func = pendingcalls[i].func;
580 arg = pendingcalls[i].arg;
581 pendingfirst = (i + 1) % NPENDINGCALLS;
582 if (func(arg) < 0) {
583 busy = 0;
584 pendingcalls_to_do = 1; /* We're not done yet */
585 return -1;
586 }
587 }
588 busy = 0;
589 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000590}
591
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000592#endif /* WITH_THREAD */
593
Guido van Rossuma9672091994-09-14 13:31:22 +0000594
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000595/* The interpreter's recursion limit */
596
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000597#ifndef Py_DEFAULT_RECURSION_LIMIT
598#define Py_DEFAULT_RECURSION_LIMIT 1000
599#endif
600static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
601int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000602
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000603int
604Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000605{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000607}
608
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000609void
610Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000611{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000612 recursion_limit = new_limit;
613 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000614}
615
Armin Rigo2b3eb402003-10-28 12:05:48 +0000616/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
617 if the recursion_depth reaches _Py_CheckRecursionLimit.
618 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
619 to guarantee that _Py_CheckRecursiveCall() is regularly called.
620 Without USE_STACKCHECK, there is no need for this. */
621int
622_Py_CheckRecursiveCall(char *where)
623{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000625
626#ifdef USE_STACKCHECK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 if (PyOS_CheckStack()) {
628 --tstate->recursion_depth;
629 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
630 return -1;
631 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000632#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633 if (tstate->recursion_depth > recursion_limit) {
634 --tstate->recursion_depth;
635 PyErr_Format(PyExc_RuntimeError,
636 "maximum recursion depth exceeded%s",
637 where);
638 return -1;
639 }
640 _Py_CheckRecursionLimit = recursion_limit;
641 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000642}
643
Guido van Rossum374a9221991-04-04 10:40:29 +0000644/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000645enum why_code {
Stefan Krah7ff78252010-06-23 18:12:09 +0000646 WHY_NOT = 0x0001, /* No error */
647 WHY_EXCEPTION = 0x0002, /* Exception occurred */
648 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
649 WHY_RETURN = 0x0008, /* 'return' statement */
650 WHY_BREAK = 0x0010, /* 'break' statement */
651 WHY_CONTINUE = 0x0020, /* 'continue' statement */
652 WHY_YIELD = 0x0040 /* 'yield' operator */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000653};
Guido van Rossum374a9221991-04-04 10:40:29 +0000654
Fredrik Lundh7a830892006-05-27 10:39:48 +0000655static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
656static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000657
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000658/* Records whether tracing is on for any thread. Counts the number of
659 threads for which tstate->c_tracefunc is non-NULL, so if the value
660 is 0, we know we don't have to check this thread's c_tracefunc.
661 This speeds up the if statement in PyEval_EvalFrameEx() after
662 fast_next_opcode*/
663static int _Py_TracingPossible = 0;
664
Skip Montanarod581d772002-09-03 20:10:45 +0000665/* for manipulating the thread switch and periodic "stuff" - used to be
666 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000667int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000668volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000669
Guido van Rossumb209a111997-04-29 18:18:01 +0000670PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000671PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000672{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000673 return PyEval_EvalCodeEx(co,
674 globals, locals,
675 (PyObject **)NULL, 0,
676 (PyObject **)NULL, 0,
677 (PyObject **)NULL, 0,
678 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000679}
680
681
682/* Interpreter main loop */
683
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000684PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000685PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000686 /* This is for backward compatibility with extension modules that
687 used this API; core interpreter code should call
688 PyEval_EvalFrameEx() */
689 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000690}
691
692PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000693PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000694{
Guido van Rossum950361c1997-01-24 13:49:28 +0000695#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000696 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000697#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000698 register PyObject **stack_pointer; /* Next free slot in value stack */
699 register unsigned char *next_instr;
700 register int opcode; /* Current opcode */
701 register int oparg; /* Current opcode argument, if any */
702 register enum why_code why; /* Reason for block stack unwind */
703 register int err; /* Error status -- nonzero if error */
704 register PyObject *x; /* Result object -- NULL if error */
705 register PyObject *v; /* Temporary objects popped off stack */
706 register PyObject *w;
707 register PyObject *u;
708 register PyObject *t;
709 register PyObject *stream = NULL; /* for PRINT opcodes */
710 register PyObject **fastlocals, **freevars;
711 PyObject *retval = NULL; /* Return value */
712 PyThreadState *tstate = PyThreadState_GET();
713 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000714
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000715 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000716
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000717 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000718
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 is true when the line being executed has changed. The
720 initial values are such as to make this false the first
721 time it is tested. */
722 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000723
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 unsigned char *first_instr;
725 PyObject *names;
726 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000727#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000728 /* Make it easier to find out where we are with a debugger */
729 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000730#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000731
Neal Norwitza81d2202002-07-14 00:27:26 +0000732/* Tuple access macros */
733
734#ifndef Py_DEBUG
735#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
736#else
737#define GETITEM(v, i) PyTuple_GetItem((v), (i))
738#endif
739
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000740#ifdef WITH_TSC
741/* Use Pentium timestamp counter to mark certain events:
742 inst0 -- beginning of switch statement for opcode dispatch
743 inst1 -- end of switch statement (may be skipped)
744 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000745 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000746 (may be skipped)
747 intr1 -- beginning of long interruption
748 intr2 -- end of long interruption
749
750 Many opcodes call out to helper C functions. In some cases, the
751 time in those functions should be counted towards the time for the
752 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
753 calls another Python function; there's no point in charge all the
754 bytecode executed by the called function to the caller.
755
756 It's hard to make a useful judgement statically. In the presence
757 of operator overloading, it's impossible to tell if a call will
758 execute new Python code or not.
759
760 It's a case-by-case judgement. I'll use intr1 for the following
761 cases:
762
763 EXEC_STMT
764 IMPORT_STAR
765 IMPORT_FROM
766 CALL_FUNCTION (and friends)
767
768 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000769 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
770 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000771
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 READ_TIMESTAMP(inst0);
773 READ_TIMESTAMP(inst1);
774 READ_TIMESTAMP(loop0);
775 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000776
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000777 /* shut up the compiler */
778 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000779#endif
780
Guido van Rossum374a9221991-04-04 10:40:29 +0000781/* Code access macros */
782
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000783#define INSTR_OFFSET() ((int)(next_instr - first_instr))
784#define NEXTOP() (*next_instr++)
785#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
786#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
787#define JUMPTO(x) (next_instr = first_instr + (x))
788#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000789
Raymond Hettingerf606f872003-03-16 03:11:04 +0000790/* OpCode prediction macros
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000791 Some opcodes tend to come in pairs thus making it possible to
792 predict the second code when the first is run. For example,
793 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
794 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000795
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 Verifying the prediction costs a single high-speed test of a register
797 variable against a constant. If the pairing was good, then the
798 processor's own internal branch predication has a high likelihood of
799 success, resulting in a nearly zero-overhead transition to the
800 next opcode. A successful prediction saves a trip through the eval-loop
801 including its two unpredictable branches, the HAS_ARG test and the
802 switch-case. Combined with the processor's internal branch prediction,
803 a successful PREDICT has the effect of making the two opcodes run as if
804 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000805
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000806 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000807 predictions turned-on and interpret the results as if some opcodes
808 had been combined or turn-off predictions so that the opcode frequency
809 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000810*/
811
Raymond Hettingera7216982004-02-08 19:59:27 +0000812#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000814#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000816#endif
817
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818#define PREDICTED(op) PRED_##op: next_instr++
819#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000820
Guido van Rossum374a9221991-04-04 10:40:29 +0000821/* Stack manipulation macros */
822
Martin v. Löwis18e16552006-02-15 17:27:45 +0000823/* The stack can grow at most MAXINT deep, as co_nlocals and
824 co_stacksize are ints. */
Stefan Krah7ff78252010-06-23 18:12:09 +0000825#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
826#define EMPTY() (STACK_LEVEL() == 0)
827#define TOP() (stack_pointer[-1])
828#define SECOND() (stack_pointer[-2])
829#define THIRD() (stack_pointer[-3])
830#define FOURTH() (stack_pointer[-4])
831#define PEEK(n) (stack_pointer[-(n)])
832#define SET_TOP(v) (stack_pointer[-1] = (v))
833#define SET_SECOND(v) (stack_pointer[-2] = (v))
834#define SET_THIRD(v) (stack_pointer[-3] = (v))
835#define SET_FOURTH(v) (stack_pointer[-4] = (v))
836#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
837#define BASIC_STACKADJ(n) (stack_pointer += n)
838#define BASIC_PUSH(v) (*stack_pointer++ = (v))
839#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000840
Guido van Rossum96a42c81992-01-12 02:29:51 +0000841#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000842#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000843 lltrace && prtrace(TOP(), "push")); \
844 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000845#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000846 BASIC_POP())
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000848 lltrace && prtrace(TOP(), "stackadj")); \
849 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000850#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krah7ff78252010-06-23 18:12:09 +0000851 prtrace((STACK_POINTER)[-1], "ext_pop")), \
852 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000853#else
Stefan Krah7ff78252010-06-23 18:12:09 +0000854#define PUSH(v) BASIC_PUSH(v)
855#define POP() BASIC_POP()
856#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000857#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000858#endif
859
Guido van Rossum681d79a1995-07-18 14:51:37 +0000860/* Local variable macros */
861
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000862#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000863
864/* The SETLOCAL() macro must not DECREF the local variable in-place and
865 then store the new value; it must copy the old value to a temporary
866 value, then store the new value, and then DECREF the temporary value.
867 This is because it is possible that during the DECREF the frame is
868 accessed by other code (e.g. a __del__ method or gc.collect()) and the
869 variable would be pointing to already-freed memory. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000870#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krah7ff78252010-06-23 18:12:09 +0000871 GETLOCAL(i) = value; \
872 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000873
Guido van Rossuma027efa1997-05-05 20:56:21 +0000874/* Start of code */
875
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000876 if (f == NULL)
877 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000878
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 /* push frame */
880 if (Py_EnterRecursiveCall(""))
881 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000882
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000883 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000884
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 if (tstate->use_tracing) {
886 if (tstate->c_tracefunc != NULL) {
887 /* tstate->c_tracefunc, if defined, is a
888 function that will be called on *every* entry
889 to a code block. Its return value, if not
890 None, is a function that will be called at
891 the start of each executed line of code.
892 (Actually, the function must return itself
893 in order to continue tracing.) The trace
894 functions are called with three arguments:
895 a pointer to the current frame, a string
896 indicating why the function is called, and
897 an argument which depends on the situation.
898 The global trace function is also called
899 whenever an exception is detected. */
900 if (call_trace_protected(tstate->c_tracefunc,
901 tstate->c_traceobj,
902 f, PyTrace_CALL, Py_None)) {
903 /* Trace function raised an error */
904 goto exit_eval_frame;
905 }
906 }
907 if (tstate->c_profilefunc != NULL) {
908 /* Similar for c_profilefunc, except it needn't
909 return itself and isn't called for "line" events */
910 if (call_trace_protected(tstate->c_profilefunc,
911 tstate->c_profileobj,
912 f, PyTrace_CALL, Py_None)) {
913 /* Profile function raised an error */
914 goto exit_eval_frame;
915 }
916 }
917 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000918
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000919 co = f->f_code;
920 names = co->co_names;
921 consts = co->co_consts;
922 fastlocals = f->f_localsplus;
923 freevars = f->f_localsplus + co->co_nlocals;
924 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
925 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 f->f_lasti now refers to the index of the last instruction
928 executed. You might think this was obvious from the name, but
929 this wasn't always true before 2.3! PyFrame_New now sets
930 f->f_lasti to -1 (i.e. the index *before* the first instruction)
931 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
932 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000933
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 When the PREDICT() macros are enabled, some opcode pairs follow in
935 direct succession without updating f->f_lasti. A successful
936 prediction effectively links the two codes together as if they
937 were a single new opcode; accordingly,f->f_lasti will point to
938 the first code in the pair (for instance, GET_ITER followed by
939 FOR_ITER is effectively a single opcode and f->f_lasti will point
940 at to the beginning of the combined pair.)
941 */
942 next_instr = first_instr + f->f_lasti + 1;
943 stack_pointer = f->f_stacktop;
944 assert(stack_pointer != NULL);
945 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000946
Tim Peters5ca576e2001-06-18 22:08:13 +0000947#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000949#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000950#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000951 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000952#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000953
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000954 why = WHY_NOT;
955 err = 0;
956 x = Py_None; /* Not a reference, just anything non-NULL */
957 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000959 if (throwflag) { /* support for generator.throw() */
960 why = WHY_EXCEPTION;
961 goto on_error;
962 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000963
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000965#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000966 if (inst1 == 0) {
967 /* Almost surely, the opcode executed a break
968 or a continue, preventing inst1 from being set
969 on the way out of the loop.
970 */
971 READ_TIMESTAMP(inst1);
972 loop1 = inst1;
973 }
974 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
975 intr0, intr1);
976 ticked = 0;
977 inst1 = 0;
978 intr0 = 0;
979 intr1 = 0;
980 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000981#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000982 assert(stack_pointer >= f->f_valuestack); /* else underflow */
983 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000984
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000985 /* Do periodic things. Doing this every time through
986 the loop would add too much overhead, so we do it
987 only every Nth instruction. We also do it if
988 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
989 event needs attention (e.g. a signal handler or
990 async I/O handler); see Py_AddPendingCall() and
991 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000992
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 if (--_Py_Ticker < 0) {
994 if (*next_instr == SETUP_FINALLY) {
995 /* Make the last opcode before
Ezio Melottic2077b02011-03-16 12:34:31 +0200996 a try: finally: block uninterruptible. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000997 goto fast_next_opcode;
998 }
999 _Py_Ticker = _Py_CheckInterval;
1000 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001001#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001002 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001003#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001004 if (pendingcalls_to_do) {
1005 if (Py_MakePendingCalls() < 0) {
1006 why = WHY_EXCEPTION;
1007 goto on_error;
1008 }
1009 if (pendingcalls_to_do)
1010 /* MakePendingCalls() didn't succeed.
1011 Force early re-execution of this
1012 "periodic" code, possibly after
1013 a thread switch */
1014 _Py_Ticker = 0;
1015 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001016#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001017 if (interpreter_lock) {
1018 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 if (PyThreadState_Swap(NULL) != tstate)
1021 Py_FatalError("ceval: tstate mix-up");
1022 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001023
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Peterson1c78e6d2014-06-16 22:59:07 -07001027
1028 /* Check if we should make a quick exit. */
1029 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1030 PyThread_release_lock(interpreter_lock);
1031 PyThread_exit_thread();
1032 }
1033
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001034 if (PyThreadState_Swap(tstate) != NULL)
1035 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001036
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001037 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001039 if (tstate->async_exc != NULL) {
1040 x = tstate->async_exc;
1041 tstate->async_exc = NULL;
1042 PyErr_SetNone(x);
1043 Py_DECREF(x);
1044 why = WHY_EXCEPTION;
1045 goto on_error;
1046 }
1047 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001048#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001050
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001051 fast_next_opcode:
1052 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001054 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001055
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001056 if (_Py_TracingPossible &&
1057 tstate->c_tracefunc != NULL && !tstate->tracing) {
1058 /* see maybe_call_line_trace
1059 for expository comments */
1060 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001061
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001062 err = maybe_call_line_trace(tstate->c_tracefunc,
1063 tstate->c_traceobj,
1064 f, &instr_lb, &instr_ub,
1065 &instr_prev);
1066 /* Reload possibly changed frame fields */
1067 JUMPTO(f->f_lasti);
1068 if (f->f_stacktop != NULL) {
1069 stack_pointer = f->f_stacktop;
1070 f->f_stacktop = NULL;
1071 }
1072 if (err) {
1073 /* trace function raised an exception */
1074 goto on_error;
1075 }
1076 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001078 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001079
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001080 opcode = NEXTOP();
1081 oparg = 0; /* allows oparg to be stored in a register because
1082 it doesn't have to be remembered across a full loop */
1083 if (HAS_ARG(opcode))
1084 oparg = NEXTARG();
Stefan Krah7ff78252010-06-23 18:12:09 +00001085 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001086#ifdef DYNAMIC_EXECUTION_PROFILE
1087#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001088 dxpairs[lastopcode][opcode]++;
1089 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001090#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001091 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001092#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001093
Guido van Rossum96a42c81992-01-12 02:29:51 +00001094#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001095 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001096
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001097 if (lltrace) {
1098 if (HAS_ARG(opcode)) {
1099 printf("%d: %d, %d\n",
1100 f->f_lasti, opcode, oparg);
1101 }
1102 else {
1103 printf("%d: %d\n",
1104 f->f_lasti, opcode);
1105 }
1106 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001107#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001109 /* Main switch on opcode */
1110 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001111
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001112 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001113
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001114 /* BEWARE!
1115 It is essential that any operation that fails sets either
1116 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1117 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001119 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001121 case NOP:
1122 goto fast_next_opcode;
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001123
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001124 case LOAD_FAST:
1125 x = GETLOCAL(oparg);
1126 if (x != NULL) {
1127 Py_INCREF(x);
1128 PUSH(x);
1129 goto fast_next_opcode;
1130 }
1131 format_exc_check_arg(PyExc_UnboundLocalError,
1132 UNBOUNDLOCAL_ERROR_MSG,
1133 PyTuple_GetItem(co->co_varnames, oparg));
1134 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001135
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001136 case LOAD_CONST:
1137 x = GETITEM(consts, oparg);
1138 Py_INCREF(x);
1139 PUSH(x);
1140 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001142 PREDICTED_WITH_ARG(STORE_FAST);
1143 case STORE_FAST:
1144 v = POP();
1145 SETLOCAL(oparg, v);
1146 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001147
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001148 case POP_TOP:
1149 v = POP();
1150 Py_DECREF(v);
1151 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001153 case ROT_TWO:
1154 v = TOP();
1155 w = SECOND();
1156 SET_TOP(w);
1157 SET_SECOND(v);
1158 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001159
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001160 case ROT_THREE:
1161 v = TOP();
1162 w = SECOND();
1163 x = THIRD();
1164 SET_TOP(w);
1165 SET_SECOND(x);
1166 SET_THIRD(v);
1167 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001169 case ROT_FOUR:
1170 u = TOP();
1171 v = SECOND();
1172 w = THIRD();
1173 x = FOURTH();
1174 SET_TOP(v);
1175 SET_SECOND(w);
1176 SET_THIRD(x);
1177 SET_FOURTH(u);
1178 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001179
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001180 case DUP_TOP:
1181 v = TOP();
1182 Py_INCREF(v);
1183 PUSH(v);
1184 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001186 case DUP_TOPX:
1187 if (oparg == 2) {
1188 x = TOP();
1189 Py_INCREF(x);
1190 w = SECOND();
1191 Py_INCREF(w);
1192 STACKADJ(2);
1193 SET_TOP(x);
1194 SET_SECOND(w);
1195 goto fast_next_opcode;
1196 } else if (oparg == 3) {
1197 x = TOP();
1198 Py_INCREF(x);
1199 w = SECOND();
1200 Py_INCREF(w);
1201 v = THIRD();
1202 Py_INCREF(v);
1203 STACKADJ(3);
1204 SET_TOP(x);
1205 SET_SECOND(w);
1206 SET_THIRD(v);
1207 goto fast_next_opcode;
1208 }
1209 Py_FatalError("invalid argument to DUP_TOPX"
1210 " (bytecode corruption?)");
1211 /* Never returns, so don't bother to set why. */
1212 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001214 case UNARY_POSITIVE:
1215 v = TOP();
1216 x = PyNumber_Positive(v);
1217 Py_DECREF(v);
1218 SET_TOP(x);
1219 if (x != NULL) continue;
1220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001222 case UNARY_NEGATIVE:
1223 v = TOP();
1224 x = PyNumber_Negative(v);
1225 Py_DECREF(v);
1226 SET_TOP(x);
1227 if (x != NULL) continue;
1228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001230 case UNARY_NOT:
1231 v = TOP();
1232 err = PyObject_IsTrue(v);
1233 Py_DECREF(v);
1234 if (err == 0) {
1235 Py_INCREF(Py_True);
1236 SET_TOP(Py_True);
1237 continue;
1238 }
1239 else if (err > 0) {
1240 Py_INCREF(Py_False);
1241 SET_TOP(Py_False);
1242 err = 0;
1243 continue;
1244 }
1245 STACKADJ(-1);
1246 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001248 case UNARY_CONVERT:
1249 v = TOP();
1250 x = PyObject_Repr(v);
1251 Py_DECREF(v);
1252 SET_TOP(x);
1253 if (x != NULL) continue;
1254 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001256 case UNARY_INVERT:
1257 v = TOP();
1258 x = PyNumber_Invert(v);
1259 Py_DECREF(v);
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_POWER:
1265 w = POP();
1266 v = TOP();
1267 x = PyNumber_Power(v, w, Py_None);
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_MULTIPLY:
1275 w = POP();
1276 v = TOP();
1277 x = PyNumber_Multiply(v, w);
1278 Py_DECREF(v);
1279 Py_DECREF(w);
1280 SET_TOP(x);
1281 if (x != NULL) continue;
1282 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001284 case BINARY_DIVIDE:
1285 if (!_Py_QnewFlag) {
1286 w = POP();
1287 v = TOP();
1288 x = PyNumber_Divide(v, w);
1289 Py_DECREF(v);
1290 Py_DECREF(w);
1291 SET_TOP(x);
1292 if (x != NULL) continue;
1293 break;
1294 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001295 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001296 BINARY_TRUE_DIVIDE */
1297 case BINARY_TRUE_DIVIDE:
1298 w = POP();
1299 v = TOP();
1300 x = PyNumber_TrueDivide(v, w);
1301 Py_DECREF(v);
1302 Py_DECREF(w);
1303 SET_TOP(x);
1304 if (x != NULL) continue;
1305 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001307 case BINARY_FLOOR_DIVIDE:
1308 w = POP();
1309 v = TOP();
1310 x = PyNumber_FloorDivide(v, w);
1311 Py_DECREF(v);
1312 Py_DECREF(w);
1313 SET_TOP(x);
1314 if (x != NULL) continue;
1315 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001316
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001317 case BINARY_MODULO:
1318 w = POP();
1319 v = TOP();
1320 if (PyString_CheckExact(v))
1321 x = PyString_Format(v, w);
1322 else
1323 x = PyNumber_Remainder(v, w);
1324 Py_DECREF(v);
1325 Py_DECREF(w);
1326 SET_TOP(x);
1327 if (x != NULL) continue;
1328 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001329
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001330 case BINARY_ADD:
1331 w = POP();
1332 v = TOP();
1333 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1334 /* INLINE: int + int */
1335 register long a, b, i;
1336 a = PyInt_AS_LONG(v);
1337 b = PyInt_AS_LONG(w);
1338 /* cast to avoid undefined behaviour
1339 on overflow */
1340 i = (long)((unsigned long)a + b);
1341 if ((i^a) < 0 && (i^b) < 0)
1342 goto slow_add;
1343 x = PyInt_FromLong(i);
1344 }
1345 else if (PyString_CheckExact(v) &&
1346 PyString_CheckExact(w)) {
1347 x = string_concatenate(v, w, f, next_instr);
1348 /* string_concatenate consumed the ref to v */
1349 goto skip_decref_vx;
1350 }
1351 else {
1352 slow_add:
1353 x = PyNumber_Add(v, w);
1354 }
1355 Py_DECREF(v);
1356 skip_decref_vx:
1357 Py_DECREF(w);
1358 SET_TOP(x);
1359 if (x != NULL) continue;
1360 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001362 case BINARY_SUBTRACT:
1363 w = POP();
1364 v = TOP();
1365 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1366 /* INLINE: int - int */
1367 register long a, b, i;
1368 a = PyInt_AS_LONG(v);
1369 b = PyInt_AS_LONG(w);
1370 /* cast to avoid undefined behaviour
1371 on overflow */
1372 i = (long)((unsigned long)a - b);
1373 if ((i^a) < 0 && (i^~b) < 0)
1374 goto slow_sub;
1375 x = PyInt_FromLong(i);
1376 }
1377 else {
1378 slow_sub:
1379 x = PyNumber_Subtract(v, w);
1380 }
1381 Py_DECREF(v);
1382 Py_DECREF(w);
1383 SET_TOP(x);
1384 if (x != NULL) continue;
1385 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001387 case BINARY_SUBSCR:
1388 w = POP();
1389 v = TOP();
1390 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1391 /* INLINE: list[int] */
1392 Py_ssize_t i = PyInt_AsSsize_t(w);
1393 if (i < 0)
1394 i += PyList_GET_SIZE(v);
1395 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1396 x = PyList_GET_ITEM(v, i);
1397 Py_INCREF(x);
1398 }
1399 else
1400 goto slow_get;
1401 }
1402 else
1403 slow_get:
1404 x = PyObject_GetItem(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_LSHIFT:
1412 w = POP();
1413 v = TOP();
1414 x = PyNumber_Lshift(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_RSHIFT:
1422 w = POP();
1423 v = TOP();
1424 x = PyNumber_Rshift(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_AND:
1432 w = POP();
1433 v = TOP();
1434 x = PyNumber_And(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_XOR:
1442 w = POP();
1443 v = TOP();
1444 x = PyNumber_Xor(v, w);
1445 Py_DECREF(v);
1446 Py_DECREF(w);
1447 SET_TOP(x);
1448 if (x != NULL) continue;
1449 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001451 case BINARY_OR:
1452 w = POP();
1453 v = TOP();
1454 x = PyNumber_Or(v, w);
1455 Py_DECREF(v);
1456 Py_DECREF(w);
1457 SET_TOP(x);
1458 if (x != NULL) continue;
1459 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001460
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001461 case LIST_APPEND:
1462 w = POP();
1463 v = PEEK(oparg);
1464 err = PyList_Append(v, w);
1465 Py_DECREF(w);
1466 if (err == 0) {
1467 PREDICT(JUMP_ABSOLUTE);
1468 continue;
1469 }
1470 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001471
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001472 case SET_ADD:
1473 w = POP();
1474 v = stack_pointer[-oparg];
1475 err = PySet_Add(v, w);
1476 Py_DECREF(w);
1477 if (err == 0) {
1478 PREDICT(JUMP_ABSOLUTE);
1479 continue;
1480 }
1481 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 case INPLACE_POWER:
1484 w = POP();
1485 v = TOP();
1486 x = PyNumber_InPlacePower(v, w, Py_None);
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_MULTIPLY:
1494 w = POP();
1495 v = TOP();
1496 x = PyNumber_InPlaceMultiply(v, w);
1497 Py_DECREF(v);
1498 Py_DECREF(w);
1499 SET_TOP(x);
1500 if (x != NULL) continue;
1501 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001502
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001503 case INPLACE_DIVIDE:
1504 if (!_Py_QnewFlag) {
1505 w = POP();
1506 v = TOP();
1507 x = PyNumber_InPlaceDivide(v, w);
1508 Py_DECREF(v);
1509 Py_DECREF(w);
1510 SET_TOP(x);
1511 if (x != NULL) continue;
1512 break;
1513 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001514 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001515 INPLACE_TRUE_DIVIDE */
1516 case INPLACE_TRUE_DIVIDE:
1517 w = POP();
1518 v = TOP();
1519 x = PyNumber_InPlaceTrueDivide(v, w);
1520 Py_DECREF(v);
1521 Py_DECREF(w);
1522 SET_TOP(x);
1523 if (x != NULL) continue;
1524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001526 case INPLACE_FLOOR_DIVIDE:
1527 w = POP();
1528 v = TOP();
1529 x = PyNumber_InPlaceFloorDivide(v, w);
1530 Py_DECREF(v);
1531 Py_DECREF(w);
1532 SET_TOP(x);
1533 if (x != NULL) continue;
1534 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001535
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001536 case INPLACE_MODULO:
1537 w = POP();
1538 v = TOP();
1539 x = PyNumber_InPlaceRemainder(v, w);
1540 Py_DECREF(v);
1541 Py_DECREF(w);
1542 SET_TOP(x);
1543 if (x != NULL) continue;
1544 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001546 case INPLACE_ADD:
1547 w = POP();
1548 v = TOP();
1549 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1550 /* INLINE: int + int */
1551 register long a, b, i;
1552 a = PyInt_AS_LONG(v);
1553 b = PyInt_AS_LONG(w);
1554 i = a + b;
1555 if ((i^a) < 0 && (i^b) < 0)
1556 goto slow_iadd;
1557 x = PyInt_FromLong(i);
1558 }
1559 else if (PyString_CheckExact(v) &&
1560 PyString_CheckExact(w)) {
1561 x = string_concatenate(v, w, f, next_instr);
1562 /* string_concatenate consumed the ref to v */
1563 goto skip_decref_v;
1564 }
1565 else {
1566 slow_iadd:
1567 x = PyNumber_InPlaceAdd(v, w);
1568 }
1569 Py_DECREF(v);
1570 skip_decref_v:
1571 Py_DECREF(w);
1572 SET_TOP(x);
1573 if (x != NULL) continue;
1574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001576 case INPLACE_SUBTRACT:
1577 w = POP();
1578 v = TOP();
1579 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1580 /* INLINE: int - int */
1581 register long a, b, i;
1582 a = PyInt_AS_LONG(v);
1583 b = PyInt_AS_LONG(w);
1584 i = a - b;
1585 if ((i^a) < 0 && (i^~b) < 0)
1586 goto slow_isub;
1587 x = PyInt_FromLong(i);
1588 }
1589 else {
1590 slow_isub:
1591 x = PyNumber_InPlaceSubtract(v, w);
1592 }
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_LSHIFT:
1600 w = POP();
1601 v = TOP();
1602 x = PyNumber_InPlaceLshift(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_RSHIFT:
1610 w = POP();
1611 v = TOP();
1612 x = PyNumber_InPlaceRshift(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_AND:
1620 w = POP();
1621 v = TOP();
1622 x = PyNumber_InPlaceAnd(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_XOR:
1630 w = POP();
1631 v = TOP();
1632 x = PyNumber_InPlaceXor(v, w);
1633 Py_DECREF(v);
1634 Py_DECREF(w);
1635 SET_TOP(x);
1636 if (x != NULL) continue;
1637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001639 case INPLACE_OR:
1640 w = POP();
1641 v = TOP();
1642 x = PyNumber_InPlaceOr(v, w);
1643 Py_DECREF(v);
1644 Py_DECREF(w);
1645 SET_TOP(x);
1646 if (x != NULL) continue;
1647 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001648
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001649 case SLICE+0:
1650 case SLICE+1:
1651 case SLICE+2:
1652 case SLICE+3:
1653 if ((opcode-SLICE) & 2)
1654 w = POP();
1655 else
1656 w = NULL;
1657 if ((opcode-SLICE) & 1)
1658 v = POP();
1659 else
1660 v = NULL;
1661 u = TOP();
1662 x = apply_slice(u, v, w);
1663 Py_DECREF(u);
1664 Py_XDECREF(v);
1665 Py_XDECREF(w);
1666 SET_TOP(x);
1667 if (x != NULL) continue;
1668 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001669
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001670 case STORE_SLICE+0:
1671 case STORE_SLICE+1:
1672 case STORE_SLICE+2:
1673 case STORE_SLICE+3:
1674 if ((opcode-STORE_SLICE) & 2)
1675 w = POP();
1676 else
1677 w = NULL;
1678 if ((opcode-STORE_SLICE) & 1)
1679 v = POP();
1680 else
1681 v = NULL;
1682 u = POP();
1683 t = POP();
1684 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1685 Py_DECREF(t);
1686 Py_DECREF(u);
1687 Py_XDECREF(v);
1688 Py_XDECREF(w);
1689 if (err == 0) continue;
1690 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001692 case DELETE_SLICE+0:
1693 case DELETE_SLICE+1:
1694 case DELETE_SLICE+2:
1695 case DELETE_SLICE+3:
1696 if ((opcode-DELETE_SLICE) & 2)
1697 w = POP();
1698 else
1699 w = NULL;
1700 if ((opcode-DELETE_SLICE) & 1)
1701 v = POP();
1702 else
1703 v = NULL;
1704 u = POP();
1705 err = assign_slice(u, v, w, (PyObject *)NULL);
1706 /* del u[v:w] */
1707 Py_DECREF(u);
1708 Py_XDECREF(v);
1709 Py_XDECREF(w);
1710 if (err == 0) continue;
1711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001713 case STORE_SUBSCR:
1714 w = TOP();
1715 v = SECOND();
1716 u = THIRD();
1717 STACKADJ(-3);
1718 /* v[w] = u */
1719 err = PyObject_SetItem(v, w, u);
1720 Py_DECREF(u);
1721 Py_DECREF(v);
1722 Py_DECREF(w);
1723 if (err == 0) continue;
1724 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001725
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001726 case DELETE_SUBSCR:
1727 w = TOP();
1728 v = SECOND();
1729 STACKADJ(-2);
1730 /* del v[w] */
1731 err = PyObject_DelItem(v, w);
1732 Py_DECREF(v);
1733 Py_DECREF(w);
1734 if (err == 0) continue;
1735 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001736
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001737 case PRINT_EXPR:
1738 v = POP();
1739 w = PySys_GetObject("displayhook");
1740 if (w == NULL) {
1741 PyErr_SetString(PyExc_RuntimeError,
1742 "lost sys.displayhook");
1743 err = -1;
1744 x = NULL;
1745 }
1746 if (err == 0) {
1747 x = PyTuple_Pack(1, v);
1748 if (x == NULL)
1749 err = -1;
1750 }
1751 if (err == 0) {
1752 w = PyEval_CallObject(w, x);
1753 Py_XDECREF(w);
1754 if (w == NULL)
1755 err = -1;
1756 }
1757 Py_DECREF(v);
1758 Py_XDECREF(x);
1759 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001760
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001761 case PRINT_ITEM_TO:
1762 w = stream = POP();
1763 /* fall through to PRINT_ITEM */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001764
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001765 case PRINT_ITEM:
1766 v = POP();
1767 if (stream == NULL || stream == Py_None) {
1768 w = PySys_GetObject("stdout");
1769 if (w == NULL) {
1770 PyErr_SetString(PyExc_RuntimeError,
1771 "lost sys.stdout");
1772 err = -1;
1773 }
1774 }
1775 /* PyFile_SoftSpace() can exececute arbitrary code
1776 if sys.stdout is an instance with a __getattr__.
1777 If __getattr__ raises an exception, w will
1778 be freed, so we need to prevent that temporarily. */
1779 Py_XINCREF(w);
1780 if (w != NULL && PyFile_SoftSpace(w, 0))
1781 err = PyFile_WriteString(" ", w);
1782 if (err == 0)
1783 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1784 if (err == 0) {
1785 /* XXX move into writeobject() ? */
1786 if (PyString_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001787 char *s = PyString_AS_STRING(v);
1788 Py_ssize_t len = PyString_GET_SIZE(v);
1789 if (len == 0 ||
1790 !isspace(Py_CHARMASK(s[len-1])) ||
1791 s[len-1] == ' ')
1792 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001793 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001794#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001795 else if (PyUnicode_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001796 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1797 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1798 if (len == 0 ||
1799 !Py_UNICODE_ISSPACE(s[len-1]) ||
1800 s[len-1] == ' ')
1801 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001802 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001803#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001804 else
Stefan Krah7ff78252010-06-23 18:12:09 +00001805 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001806 }
1807 Py_XDECREF(w);
1808 Py_DECREF(v);
1809 Py_XDECREF(stream);
1810 stream = NULL;
1811 if (err == 0)
1812 continue;
1813 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001814
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001815 case PRINT_NEWLINE_TO:
1816 w = stream = POP();
1817 /* fall through to PRINT_NEWLINE */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001818
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001819 case PRINT_NEWLINE:
1820 if (stream == NULL || stream == Py_None) {
1821 w = PySys_GetObject("stdout");
1822 if (w == NULL) {
1823 PyErr_SetString(PyExc_RuntimeError,
1824 "lost sys.stdout");
1825 why = WHY_EXCEPTION;
1826 }
1827 }
1828 if (w != NULL) {
1829 /* w.write() may replace sys.stdout, so we
1830 * have to keep our reference to it */
1831 Py_INCREF(w);
1832 err = PyFile_WriteString("\n", w);
1833 if (err == 0)
1834 PyFile_SoftSpace(w, 0);
1835 Py_DECREF(w);
1836 }
1837 Py_XDECREF(stream);
1838 stream = NULL;
1839 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Thomas Wouters434d0822000-08-24 20:11:32 +00001841
1842#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001843 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001844#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001845 case RAISE_VARARGS:
1846 u = v = w = NULL;
1847 switch (oparg) {
1848 case 3:
1849 u = POP(); /* traceback */
1850 /* Fallthrough */
1851 case 2:
1852 v = POP(); /* value */
1853 /* Fallthrough */
1854 case 1:
1855 w = POP(); /* exc */
1856 case 0: /* Fallthrough */
1857 why = do_raise(w, v, u);
1858 break;
1859 default:
1860 PyErr_SetString(PyExc_SystemError,
1861 "bad RAISE_VARARGS oparg");
1862 why = WHY_EXCEPTION;
1863 break;
1864 }
1865 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001866
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001867 case LOAD_LOCALS:
1868 if ((x = f->f_locals) != NULL) {
1869 Py_INCREF(x);
1870 PUSH(x);
1871 continue;
1872 }
1873 PyErr_SetString(PyExc_SystemError, "no locals");
1874 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001876 case RETURN_VALUE:
1877 retval = POP();
1878 why = WHY_RETURN;
1879 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001880
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001881 case YIELD_VALUE:
1882 retval = POP();
1883 f->f_stacktop = stack_pointer;
1884 why = WHY_YIELD;
1885 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 case EXEC_STMT:
1888 w = TOP();
1889 v = SECOND();
1890 u = THIRD();
1891 STACKADJ(-3);
1892 READ_TIMESTAMP(intr0);
1893 err = exec_statement(f, u, v, w);
1894 READ_TIMESTAMP(intr1);
1895 Py_DECREF(u);
1896 Py_DECREF(v);
1897 Py_DECREF(w);
1898 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001900 case POP_BLOCK:
1901 {
1902 PyTryBlock *b = PyFrame_BlockPop(f);
1903 while (STACK_LEVEL() > b->b_level) {
1904 v = POP();
1905 Py_DECREF(v);
1906 }
1907 }
1908 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001910 PREDICTED(END_FINALLY);
1911 case END_FINALLY:
1912 v = POP();
1913 if (PyInt_Check(v)) {
1914 why = (enum why_code) PyInt_AS_LONG(v);
1915 assert(why != WHY_YIELD);
1916 if (why == WHY_RETURN ||
1917 why == WHY_CONTINUE)
1918 retval = POP();
1919 }
1920 else if (PyExceptionClass_Check(v) ||
1921 PyString_Check(v)) {
1922 w = POP();
1923 u = POP();
1924 PyErr_Restore(v, w, u);
1925 why = WHY_RERAISE;
1926 break;
1927 }
1928 else if (v != Py_None) {
1929 PyErr_SetString(PyExc_SystemError,
1930 "'finally' pops bad exception");
1931 why = WHY_EXCEPTION;
1932 }
1933 Py_DECREF(v);
1934 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001935
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001936 case BUILD_CLASS:
1937 u = TOP();
1938 v = SECOND();
1939 w = THIRD();
1940 STACKADJ(-2);
1941 x = build_class(u, v, w);
1942 SET_TOP(x);
1943 Py_DECREF(u);
1944 Py_DECREF(v);
1945 Py_DECREF(w);
1946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001948 case STORE_NAME:
1949 w = GETITEM(names, oparg);
1950 v = POP();
1951 if ((x = f->f_locals) != NULL) {
1952 if (PyDict_CheckExact(x))
1953 err = PyDict_SetItem(x, w, v);
1954 else
1955 err = PyObject_SetItem(x, w, v);
1956 Py_DECREF(v);
1957 if (err == 0) continue;
1958 break;
1959 }
1960 PyErr_Format(PyExc_SystemError,
1961 "no locals found when storing %s",
1962 PyObject_REPR(w));
1963 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001964
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001965 case DELETE_NAME:
1966 w = GETITEM(names, oparg);
1967 if ((x = f->f_locals) != NULL) {
1968 if ((err = PyObject_DelItem(x, w)) != 0)
1969 format_exc_check_arg(PyExc_NameError,
1970 NAME_ERROR_MSG,
1971 w);
1972 break;
1973 }
1974 PyErr_Format(PyExc_SystemError,
1975 "no locals when deleting %s",
1976 PyObject_REPR(w));
1977 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001978
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001979 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1980 case UNPACK_SEQUENCE:
1981 v = POP();
1982 if (PyTuple_CheckExact(v) &&
1983 PyTuple_GET_SIZE(v) == oparg) {
1984 PyObject **items = \
1985 ((PyTupleObject *)v)->ob_item;
1986 while (oparg--) {
1987 w = items[oparg];
1988 Py_INCREF(w);
1989 PUSH(w);
1990 }
1991 Py_DECREF(v);
1992 continue;
1993 } else if (PyList_CheckExact(v) &&
1994 PyList_GET_SIZE(v) == oparg) {
1995 PyObject **items = \
1996 ((PyListObject *)v)->ob_item;
1997 while (oparg--) {
1998 w = items[oparg];
1999 Py_INCREF(w);
2000 PUSH(w);
2001 }
2002 } else if (unpack_iterable(v, oparg,
2003 stack_pointer + oparg)) {
2004 STACKADJ(oparg);
2005 } else {
2006 /* unpack_iterable() raised an exception */
2007 why = WHY_EXCEPTION;
2008 }
2009 Py_DECREF(v);
2010 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002012 case STORE_ATTR:
2013 w = GETITEM(names, oparg);
2014 v = TOP();
2015 u = SECOND();
2016 STACKADJ(-2);
2017 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2018 Py_DECREF(v);
2019 Py_DECREF(u);
2020 if (err == 0) continue;
2021 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002022
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002023 case DELETE_ATTR:
2024 w = GETITEM(names, oparg);
2025 v = POP();
2026 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2027 /* del v.w */
2028 Py_DECREF(v);
2029 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002031 case STORE_GLOBAL:
2032 w = GETITEM(names, oparg);
2033 v = POP();
2034 err = PyDict_SetItem(f->f_globals, w, v);
2035 Py_DECREF(v);
2036 if (err == 0) continue;
2037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002039 case DELETE_GLOBAL:
2040 w = GETITEM(names, oparg);
2041 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2042 format_exc_check_arg(
2043 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2044 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002046 case LOAD_NAME:
2047 w = GETITEM(names, oparg);
2048 if ((v = f->f_locals) == NULL) {
2049 PyErr_Format(PyExc_SystemError,
2050 "no locals when loading %s",
2051 PyObject_REPR(w));
2052 why = WHY_EXCEPTION;
2053 break;
2054 }
2055 if (PyDict_CheckExact(v)) {
2056 x = PyDict_GetItem(v, w);
2057 Py_XINCREF(x);
2058 }
2059 else {
2060 x = PyObject_GetItem(v, w);
2061 if (x == NULL && PyErr_Occurred()) {
2062 if (!PyErr_ExceptionMatches(
2063 PyExc_KeyError))
2064 break;
2065 PyErr_Clear();
2066 }
2067 }
2068 if (x == NULL) {
2069 x = PyDict_GetItem(f->f_globals, w);
2070 if (x == NULL) {
2071 x = PyDict_GetItem(f->f_builtins, w);
2072 if (x == NULL) {
2073 format_exc_check_arg(
2074 PyExc_NameError,
2075 NAME_ERROR_MSG, w);
2076 break;
2077 }
2078 }
2079 Py_INCREF(x);
2080 }
2081 PUSH(x);
2082 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002084 case LOAD_GLOBAL:
2085 w = GETITEM(names, oparg);
2086 if (PyString_CheckExact(w)) {
2087 /* Inline the PyDict_GetItem() calls.
2088 WARNING: this is an extreme speed hack.
2089 Do not try this at home. */
2090 long hash = ((PyStringObject *)w)->ob_shash;
2091 if (hash != -1) {
2092 PyDictObject *d;
2093 PyDictEntry *e;
2094 d = (PyDictObject *)(f->f_globals);
2095 e = d->ma_lookup(d, w, hash);
2096 if (e == NULL) {
2097 x = NULL;
2098 break;
2099 }
2100 x = e->me_value;
2101 if (x != NULL) {
2102 Py_INCREF(x);
2103 PUSH(x);
2104 continue;
2105 }
2106 d = (PyDictObject *)(f->f_builtins);
2107 e = d->ma_lookup(d, w, hash);
2108 if (e == NULL) {
2109 x = NULL;
2110 break;
2111 }
2112 x = e->me_value;
2113 if (x != NULL) {
2114 Py_INCREF(x);
2115 PUSH(x);
2116 continue;
2117 }
2118 goto load_global_error;
2119 }
2120 }
2121 /* This is the un-inlined version of the code above */
2122 x = PyDict_GetItem(f->f_globals, w);
2123 if (x == NULL) {
2124 x = PyDict_GetItem(f->f_builtins, w);
2125 if (x == NULL) {
2126 load_global_error:
2127 format_exc_check_arg(
2128 PyExc_NameError,
2129 GLOBAL_NAME_ERROR_MSG, w);
2130 break;
2131 }
2132 }
2133 Py_INCREF(x);
2134 PUSH(x);
2135 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002136
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002137 case DELETE_FAST:
2138 x = GETLOCAL(oparg);
2139 if (x != NULL) {
2140 SETLOCAL(oparg, NULL);
2141 continue;
2142 }
2143 format_exc_check_arg(
2144 PyExc_UnboundLocalError,
2145 UNBOUNDLOCAL_ERROR_MSG,
2146 PyTuple_GetItem(co->co_varnames, oparg)
2147 );
2148 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002149
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002150 case LOAD_CLOSURE:
2151 x = freevars[oparg];
2152 Py_INCREF(x);
2153 PUSH(x);
2154 if (x != NULL) continue;
2155 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002156
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002157 case LOAD_DEREF:
2158 x = freevars[oparg];
2159 w = PyCell_Get(x);
2160 if (w != NULL) {
2161 PUSH(w);
2162 continue;
2163 }
2164 err = -1;
2165 /* Don't stomp existing exception */
2166 if (PyErr_Occurred())
2167 break;
2168 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2169 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krah7ff78252010-06-23 18:12:09 +00002170 oparg);
2171 format_exc_check_arg(
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002172 PyExc_UnboundLocalError,
2173 UNBOUNDLOCAL_ERROR_MSG,
2174 v);
2175 } else {
2176 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2177 PyTuple_GET_SIZE(co->co_cellvars));
2178 format_exc_check_arg(PyExc_NameError,
2179 UNBOUNDFREE_ERROR_MSG, v);
2180 }
2181 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002182
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002183 case STORE_DEREF:
2184 w = POP();
2185 x = freevars[oparg];
2186 PyCell_Set(x, w);
2187 Py_DECREF(w);
2188 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002189
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002190 case BUILD_TUPLE:
2191 x = PyTuple_New(oparg);
2192 if (x != NULL) {
2193 for (; --oparg >= 0;) {
2194 w = POP();
2195 PyTuple_SET_ITEM(x, oparg, w);
2196 }
2197 PUSH(x);
2198 continue;
2199 }
2200 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002202 case BUILD_LIST:
2203 x = PyList_New(oparg);
2204 if (x != NULL) {
2205 for (; --oparg >= 0;) {
2206 w = POP();
2207 PyList_SET_ITEM(x, oparg, w);
2208 }
2209 PUSH(x);
2210 continue;
2211 }
2212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002214 case BUILD_SET:
2215 x = PySet_New(NULL);
2216 if (x != NULL) {
2217 for (; --oparg >= 0;) {
2218 w = POP();
2219 if (err == 0)
2220 err = PySet_Add(x, w);
2221 Py_DECREF(w);
2222 }
2223 if (err != 0) {
2224 Py_DECREF(x);
2225 break;
2226 }
2227 PUSH(x);
2228 continue;
2229 }
2230 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002231
2232
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002233 case BUILD_MAP:
2234 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2235 PUSH(x);
2236 if (x != NULL) continue;
2237 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002238
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002239 case STORE_MAP:
2240 w = TOP(); /* key */
2241 u = SECOND(); /* value */
2242 v = THIRD(); /* dict */
2243 STACKADJ(-2);
2244 assert (PyDict_CheckExact(v));
2245 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2246 Py_DECREF(u);
2247 Py_DECREF(w);
2248 if (err == 0) continue;
2249 break;
Raymond Hettingereffde122007-12-18 18:26:18 +00002250
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002251 case MAP_ADD:
2252 w = TOP(); /* key */
2253 u = SECOND(); /* value */
2254 STACKADJ(-2);
2255 v = stack_pointer[-oparg]; /* dict */
2256 assert (PyDict_CheckExact(v));
2257 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2258 Py_DECREF(u);
2259 Py_DECREF(w);
2260 if (err == 0) {
2261 PREDICT(JUMP_ABSOLUTE);
2262 continue;
2263 }
2264 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002265
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002266 case LOAD_ATTR:
2267 w = GETITEM(names, oparg);
2268 v = TOP();
2269 x = PyObject_GetAttr(v, w);
2270 Py_DECREF(v);
2271 SET_TOP(x);
2272 if (x != NULL) continue;
2273 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002274
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002275 case COMPARE_OP:
2276 w = POP();
2277 v = TOP();
2278 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2279 /* INLINE: cmp(int, int) */
2280 register long a, b;
2281 register int res;
2282 a = PyInt_AS_LONG(v);
2283 b = PyInt_AS_LONG(w);
2284 switch (oparg) {
2285 case PyCmp_LT: res = a < b; break;
2286 case PyCmp_LE: res = a <= b; break;
2287 case PyCmp_EQ: res = a == b; break;
2288 case PyCmp_NE: res = a != b; break;
2289 case PyCmp_GT: res = a > b; break;
2290 case PyCmp_GE: res = a >= b; break;
2291 case PyCmp_IS: res = v == w; break;
2292 case PyCmp_IS_NOT: res = v != w; break;
2293 default: goto slow_compare;
2294 }
2295 x = res ? Py_True : Py_False;
2296 Py_INCREF(x);
2297 }
2298 else {
2299 slow_compare:
2300 x = cmp_outcome(oparg, v, w);
2301 }
2302 Py_DECREF(v);
2303 Py_DECREF(w);
2304 SET_TOP(x);
2305 if (x == NULL) break;
2306 PREDICT(POP_JUMP_IF_FALSE);
2307 PREDICT(POP_JUMP_IF_TRUE);
2308 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002309
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002310 case IMPORT_NAME:
2311 w = GETITEM(names, oparg);
2312 x = PyDict_GetItemString(f->f_builtins, "__import__");
2313 if (x == NULL) {
2314 PyErr_SetString(PyExc_ImportError,
2315 "__import__ not found");
2316 break;
2317 }
2318 Py_INCREF(x);
2319 v = POP();
2320 u = TOP();
2321 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2322 w = PyTuple_Pack(5,
2323 w,
2324 f->f_globals,
2325 f->f_locals == NULL ?
2326 Py_None : f->f_locals,
2327 v,
2328 u);
2329 else
2330 w = PyTuple_Pack(4,
2331 w,
2332 f->f_globals,
2333 f->f_locals == NULL ?
2334 Py_None : f->f_locals,
2335 v);
2336 Py_DECREF(v);
2337 Py_DECREF(u);
2338 if (w == NULL) {
2339 u = POP();
2340 Py_DECREF(x);
2341 x = NULL;
2342 break;
2343 }
2344 READ_TIMESTAMP(intr0);
2345 v = x;
2346 x = PyEval_CallObject(v, w);
2347 Py_DECREF(v);
2348 READ_TIMESTAMP(intr1);
2349 Py_DECREF(w);
2350 SET_TOP(x);
2351 if (x != NULL) continue;
2352 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002353
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002354 case IMPORT_STAR:
2355 v = POP();
2356 PyFrame_FastToLocals(f);
2357 if ((x = f->f_locals) == NULL) {
2358 PyErr_SetString(PyExc_SystemError,
2359 "no locals found during 'import *'");
2360 break;
2361 }
2362 READ_TIMESTAMP(intr0);
2363 err = import_all_from(x, v);
2364 READ_TIMESTAMP(intr1);
2365 PyFrame_LocalsToFast(f, 0);
2366 Py_DECREF(v);
2367 if (err == 0) continue;
2368 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002369
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002370 case IMPORT_FROM:
2371 w = GETITEM(names, oparg);
2372 v = TOP();
2373 READ_TIMESTAMP(intr0);
2374 x = import_from(v, w);
2375 READ_TIMESTAMP(intr1);
2376 PUSH(x);
2377 if (x != NULL) continue;
2378 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002379
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002380 case JUMP_FORWARD:
2381 JUMPBY(oparg);
2382 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002383
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002384 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2385 case POP_JUMP_IF_FALSE:
2386 w = POP();
2387 if (w == Py_True) {
2388 Py_DECREF(w);
2389 goto fast_next_opcode;
2390 }
2391 if (w == Py_False) {
2392 Py_DECREF(w);
2393 JUMPTO(oparg);
2394 goto fast_next_opcode;
2395 }
2396 err = PyObject_IsTrue(w);
2397 Py_DECREF(w);
2398 if (err > 0)
2399 err = 0;
2400 else if (err == 0)
2401 JUMPTO(oparg);
2402 else
2403 break;
2404 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002405
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002406 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2407 case POP_JUMP_IF_TRUE:
2408 w = POP();
2409 if (w == Py_False) {
2410 Py_DECREF(w);
2411 goto fast_next_opcode;
2412 }
2413 if (w == Py_True) {
2414 Py_DECREF(w);
2415 JUMPTO(oparg);
2416 goto fast_next_opcode;
2417 }
2418 err = PyObject_IsTrue(w);
2419 Py_DECREF(w);
2420 if (err > 0) {
2421 err = 0;
2422 JUMPTO(oparg);
2423 }
2424 else if (err == 0)
2425 ;
2426 else
2427 break;
2428 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002430 case JUMP_IF_FALSE_OR_POP:
2431 w = TOP();
2432 if (w == Py_True) {
2433 STACKADJ(-1);
2434 Py_DECREF(w);
2435 goto fast_next_opcode;
2436 }
2437 if (w == Py_False) {
2438 JUMPTO(oparg);
2439 goto fast_next_opcode;
2440 }
2441 err = PyObject_IsTrue(w);
2442 if (err > 0) {
2443 STACKADJ(-1);
2444 Py_DECREF(w);
2445 err = 0;
2446 }
2447 else if (err == 0)
2448 JUMPTO(oparg);
2449 else
2450 break;
2451 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002452
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002453 case JUMP_IF_TRUE_OR_POP:
2454 w = TOP();
2455 if (w == Py_False) {
2456 STACKADJ(-1);
2457 Py_DECREF(w);
2458 goto fast_next_opcode;
2459 }
2460 if (w == Py_True) {
2461 JUMPTO(oparg);
2462 goto fast_next_opcode;
2463 }
2464 err = PyObject_IsTrue(w);
2465 if (err > 0) {
2466 err = 0;
2467 JUMPTO(oparg);
2468 }
2469 else if (err == 0) {
2470 STACKADJ(-1);
2471 Py_DECREF(w);
2472 }
2473 else
2474 break;
2475 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002476
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002477 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2478 case JUMP_ABSOLUTE:
2479 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002480#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002481 /* Enabling this path speeds-up all while and for-loops by bypassing
2482 the per-loop checks for signals. By default, this should be turned-off
2483 because it prevents detection of a control-break in tight loops like
2484 "while 1: pass". Compile with this option turned-on when you need
2485 the speed-up and do not need break checking inside tight loops (ones
2486 that contain only instructions ending with goto fast_next_opcode).
2487 */
2488 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002489#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002490 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002491#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002492
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002493 case GET_ITER:
2494 /* before: [obj]; after [getiter(obj)] */
2495 v = TOP();
2496 x = PyObject_GetIter(v);
2497 Py_DECREF(v);
2498 if (x != NULL) {
2499 SET_TOP(x);
2500 PREDICT(FOR_ITER);
2501 continue;
2502 }
2503 STACKADJ(-1);
2504 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002506 PREDICTED_WITH_ARG(FOR_ITER);
2507 case FOR_ITER:
2508 /* before: [iter]; after: [iter, iter()] *or* [] */
2509 v = TOP();
2510 x = (*v->ob_type->tp_iternext)(v);
2511 if (x != NULL) {
2512 PUSH(x);
2513 PREDICT(STORE_FAST);
2514 PREDICT(UNPACK_SEQUENCE);
2515 continue;
2516 }
2517 if (PyErr_Occurred()) {
2518 if (!PyErr_ExceptionMatches(
2519 PyExc_StopIteration))
2520 break;
2521 PyErr_Clear();
2522 }
2523 /* iterator ended normally */
2524 x = v = POP();
2525 Py_DECREF(v);
2526 JUMPBY(oparg);
2527 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002528
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002529 case BREAK_LOOP:
2530 why = WHY_BREAK;
2531 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002533 case CONTINUE_LOOP:
2534 retval = PyInt_FromLong(oparg);
2535 if (!retval) {
2536 x = NULL;
2537 break;
2538 }
2539 why = WHY_CONTINUE;
2540 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002541
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002542 case SETUP_LOOP:
2543 case SETUP_EXCEPT:
2544 case SETUP_FINALLY:
2545 /* NOTE: If you add any new block-setup opcodes that
2546 are not try/except/finally handlers, you may need
2547 to update the PyGen_NeedsFinalizing() function.
2548 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002549
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002550 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2551 STACK_LEVEL());
2552 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002553
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002554 case SETUP_WITH:
2555 {
2556 static PyObject *exit, *enter;
2557 w = TOP();
2558 x = special_lookup(w, "__exit__", &exit);
2559 if (!x)
2560 break;
2561 SET_TOP(x);
2562 u = special_lookup(w, "__enter__", &enter);
2563 Py_DECREF(w);
2564 if (!u) {
2565 x = NULL;
2566 break;
2567 }
2568 x = PyObject_CallFunctionObjArgs(u, NULL);
2569 Py_DECREF(u);
2570 if (!x)
2571 break;
2572 /* Setup a finally block (SETUP_WITH as a block is
2573 equivalent to SETUP_FINALLY except it normalizes
2574 the exception) before pushing the result of
2575 __enter__ on the stack. */
2576 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2577 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002578
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002579 PUSH(x);
2580 continue;
2581 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002582
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002583 case WITH_CLEANUP:
2584 {
2585 /* At the top of the stack are 1-3 values indicating
2586 how/why we entered the finally clause:
2587 - TOP = None
2588 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2589 - TOP = WHY_*; no retval below it
2590 - (TOP, SECOND, THIRD) = exc_info()
2591 Below them is EXIT, the context.__exit__ bound method.
2592 In the last case, we must call
2593 EXIT(TOP, SECOND, THIRD)
2594 otherwise we must call
2595 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002596
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002597 In all cases, we remove EXIT from the stack, leaving
2598 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002599
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002600 In addition, if the stack represents an exception,
2601 *and* the function call returns a 'true' value, we
2602 "zap" this information, to prevent END_FINALLY from
2603 re-raising the exception. (But non-local gotos
2604 should still be resumed.)
2605 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002606
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002607 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002609 u = POP();
2610 if (u == Py_None) {
2611 exit_func = TOP();
2612 SET_TOP(u);
2613 v = w = Py_None;
2614 }
2615 else if (PyInt_Check(u)) {
2616 switch(PyInt_AS_LONG(u)) {
2617 case WHY_RETURN:
2618 case WHY_CONTINUE:
2619 /* Retval in TOP. */
2620 exit_func = SECOND();
2621 SET_SECOND(TOP());
2622 SET_TOP(u);
2623 break;
2624 default:
2625 exit_func = TOP();
2626 SET_TOP(u);
2627 break;
2628 }
2629 u = v = w = Py_None;
2630 }
2631 else {
2632 v = TOP();
2633 w = SECOND();
2634 exit_func = THIRD();
2635 SET_TOP(u);
2636 SET_SECOND(v);
2637 SET_THIRD(w);
2638 }
2639 /* XXX Not the fastest way to call it... */
2640 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2641 NULL);
2642 Py_DECREF(exit_func);
2643 if (x == NULL)
2644 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002645
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002646 if (u != Py_None)
2647 err = PyObject_IsTrue(x);
2648 else
2649 err = 0;
2650 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002651
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002652 if (err < 0)
2653 break; /* Go to error exit */
2654 else if (err > 0) {
2655 err = 0;
2656 /* There was an exception and a true return */
2657 STACKADJ(-2);
2658 Py_INCREF(Py_None);
2659 SET_TOP(Py_None);
2660 Py_DECREF(u);
2661 Py_DECREF(v);
2662 Py_DECREF(w);
2663 } else {
2664 /* The stack was rearranged to remove EXIT
2665 above. Let END_FINALLY do its thing */
2666 }
2667 PREDICT(END_FINALLY);
2668 break;
2669 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002670
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002671 case CALL_FUNCTION:
2672 {
2673 PyObject **sp;
2674 PCALL(PCALL_ALL);
2675 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002676#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002677 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002678#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002679 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002680#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002681 stack_pointer = sp;
2682 PUSH(x);
2683 if (x != NULL)
2684 continue;
2685 break;
2686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002687
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002688 case CALL_FUNCTION_VAR:
2689 case CALL_FUNCTION_KW:
2690 case CALL_FUNCTION_VAR_KW:
2691 {
2692 int na = oparg & 0xff;
2693 int nk = (oparg>>8) & 0xff;
2694 int flags = (opcode - CALL_FUNCTION) & 3;
2695 int n = na + 2 * nk;
2696 PyObject **pfunc, *func, **sp;
2697 PCALL(PCALL_ALL);
2698 if (flags & CALL_FLAG_VAR)
2699 n++;
2700 if (flags & CALL_FLAG_KW)
2701 n++;
2702 pfunc = stack_pointer - n - 1;
2703 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002704
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002705 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00002706 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002707 PyObject *self = PyMethod_GET_SELF(func);
2708 Py_INCREF(self);
2709 func = PyMethod_GET_FUNCTION(func);
2710 Py_INCREF(func);
2711 Py_DECREF(*pfunc);
2712 *pfunc = self;
2713 na++;
2714 } else
2715 Py_INCREF(func);
2716 sp = stack_pointer;
2717 READ_TIMESTAMP(intr0);
2718 x = ext_do_call(func, &sp, flags, na, nk);
2719 READ_TIMESTAMP(intr1);
2720 stack_pointer = sp;
2721 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002722
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002723 while (stack_pointer > pfunc) {
2724 w = POP();
2725 Py_DECREF(w);
2726 }
2727 PUSH(x);
2728 if (x != NULL)
2729 continue;
2730 break;
2731 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002732
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002733 case MAKE_FUNCTION:
2734 v = POP(); /* code object */
2735 x = PyFunction_New(v, f->f_globals);
2736 Py_DECREF(v);
2737 /* XXX Maybe this should be a separate opcode? */
2738 if (x != NULL && oparg > 0) {
2739 v = PyTuple_New(oparg);
2740 if (v == NULL) {
2741 Py_DECREF(x);
2742 x = NULL;
2743 break;
2744 }
2745 while (--oparg >= 0) {
2746 w = POP();
2747 PyTuple_SET_ITEM(v, oparg, w);
2748 }
2749 err = PyFunction_SetDefaults(x, v);
2750 Py_DECREF(v);
2751 }
2752 PUSH(x);
2753 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002755 case MAKE_CLOSURE:
2756 {
2757 v = POP(); /* code object */
2758 x = PyFunction_New(v, f->f_globals);
2759 Py_DECREF(v);
2760 if (x != NULL) {
2761 v = POP();
2762 if (PyFunction_SetClosure(x, v) != 0) {
2763 /* Can't happen unless bytecode is corrupt. */
2764 why = WHY_EXCEPTION;
2765 }
2766 Py_DECREF(v);
2767 }
2768 if (x != NULL && oparg > 0) {
2769 v = PyTuple_New(oparg);
2770 if (v == NULL) {
2771 Py_DECREF(x);
2772 x = NULL;
2773 break;
2774 }
2775 while (--oparg >= 0) {
2776 w = POP();
2777 PyTuple_SET_ITEM(v, oparg, w);
2778 }
2779 if (PyFunction_SetDefaults(x, v) != 0) {
2780 /* Can't happen unless
2781 PyFunction_SetDefaults changes. */
2782 why = WHY_EXCEPTION;
2783 }
2784 Py_DECREF(v);
2785 }
2786 PUSH(x);
2787 break;
2788 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002789
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002790 case BUILD_SLICE:
2791 if (oparg == 3)
2792 w = POP();
2793 else
2794 w = NULL;
2795 v = POP();
2796 u = TOP();
2797 x = PySlice_New(u, v, w);
2798 Py_DECREF(u);
2799 Py_DECREF(v);
2800 Py_XDECREF(w);
2801 SET_TOP(x);
2802 if (x != NULL) continue;
2803 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002804
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002805 case EXTENDED_ARG:
2806 opcode = NEXTOP();
2807 oparg = oparg<<16 | NEXTARG();
2808 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002809
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002810 default:
2811 fprintf(stderr,
2812 "XXX lineno: %d, opcode: %d\n",
2813 PyFrame_GetLineNumber(f),
2814 opcode);
2815 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2816 why = WHY_EXCEPTION;
2817 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002818
2819#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002820 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002821#endif
2822
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002823 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002824
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002825 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002826
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002827 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002828
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002829 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002830
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002831 if (why == WHY_NOT) {
2832 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002833#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002834 /* This check is expensive! */
2835 if (PyErr_Occurred())
2836 fprintf(stderr,
2837 "XXX undetected error\n");
2838 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002839#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002840 READ_TIMESTAMP(loop1);
2841 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002842#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002843 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002844#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002845 }
2846 why = WHY_EXCEPTION;
2847 x = Py_None;
2848 err = 0;
2849 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002850
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002851 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002852
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002853 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2854 if (!PyErr_Occurred()) {
2855 PyErr_SetString(PyExc_SystemError,
2856 "error return without exception set");
2857 why = WHY_EXCEPTION;
2858 }
2859 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002860#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002861 else {
2862 /* This check is expensive! */
2863 if (PyErr_Occurred()) {
2864 char buf[128];
2865 sprintf(buf, "Stack unwind with exception "
2866 "set and why=%d", why);
2867 Py_FatalError(buf);
2868 }
2869 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002870#endif
2871
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002872 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002874 if (why == WHY_EXCEPTION) {
2875 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002876
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002877 if (tstate->c_tracefunc != NULL)
2878 call_exc_trace(tstate->c_tracefunc,
2879 tstate->c_traceobj, f);
2880 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002881
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002882 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002884 if (why == WHY_RERAISE)
2885 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002887 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002888
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002889fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002890 while (why != WHY_NOT && f->f_iblock > 0) {
2891 /* Peek at the current block. */
2892 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002894 assert(why != WHY_YIELD);
2895 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2896 why = WHY_NOT;
2897 JUMPTO(PyInt_AS_LONG(retval));
2898 Py_DECREF(retval);
2899 break;
2900 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002901
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002902 /* Now we have to pop the block. */
2903 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00002904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002905 while (STACK_LEVEL() > b->b_level) {
2906 v = POP();
2907 Py_XDECREF(v);
2908 }
2909 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2910 why = WHY_NOT;
2911 JUMPTO(b->b_handler);
2912 break;
2913 }
2914 if (b->b_type == SETUP_FINALLY ||
2915 (b->b_type == SETUP_EXCEPT &&
2916 why == WHY_EXCEPTION) ||
2917 b->b_type == SETUP_WITH) {
2918 if (why == WHY_EXCEPTION) {
2919 PyObject *exc, *val, *tb;
2920 PyErr_Fetch(&exc, &val, &tb);
2921 if (val == NULL) {
2922 val = Py_None;
2923 Py_INCREF(val);
2924 }
2925 /* Make the raw exception data
2926 available to the handler,
2927 so a program can emulate the
2928 Python main loop. Don't do
2929 this for 'finally'. */
2930 if (b->b_type == SETUP_EXCEPT ||
2931 b->b_type == SETUP_WITH) {
2932 PyErr_NormalizeException(
2933 &exc, &val, &tb);
2934 set_exc_info(tstate,
2935 exc, val, tb);
2936 }
2937 if (tb == NULL) {
2938 Py_INCREF(Py_None);
2939 PUSH(Py_None);
2940 } else
2941 PUSH(tb);
2942 PUSH(val);
2943 PUSH(exc);
2944 }
2945 else {
2946 if (why & (WHY_RETURN | WHY_CONTINUE))
2947 PUSH(retval);
2948 v = PyInt_FromLong((long)why);
2949 PUSH(v);
2950 }
2951 why = WHY_NOT;
2952 JUMPTO(b->b_handler);
2953 break;
2954 }
2955 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002956
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002957 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002959 if (why != WHY_NOT)
2960 break;
2961 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002962
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002963 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002964
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002965 assert(why != WHY_YIELD);
2966 /* Pop remaining stack entries. */
2967 while (!EMPTY()) {
2968 v = POP();
2969 Py_XDECREF(v);
2970 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002971
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002972 if (why != WHY_RETURN)
2973 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002974
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002975fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002976 if (tstate->use_tracing) {
2977 if (tstate->c_tracefunc) {
2978 if (why == WHY_RETURN || why == WHY_YIELD) {
2979 if (call_trace(tstate->c_tracefunc,
2980 tstate->c_traceobj, f,
2981 PyTrace_RETURN, retval)) {
2982 Py_XDECREF(retval);
2983 retval = NULL;
2984 why = WHY_EXCEPTION;
2985 }
2986 }
2987 else if (why == WHY_EXCEPTION) {
2988 call_trace_protected(tstate->c_tracefunc,
2989 tstate->c_traceobj, f,
2990 PyTrace_RETURN, NULL);
2991 }
2992 }
2993 if (tstate->c_profilefunc) {
2994 if (why == WHY_EXCEPTION)
2995 call_trace_protected(tstate->c_profilefunc,
2996 tstate->c_profileobj, f,
2997 PyTrace_RETURN, NULL);
2998 else if (call_trace(tstate->c_profilefunc,
2999 tstate->c_profileobj, f,
3000 PyTrace_RETURN, retval)) {
3001 Py_XDECREF(retval);
3002 retval = NULL;
3003 why = WHY_EXCEPTION;
3004 }
3005 }
3006 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003007
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003008 if (tstate->frame->f_exc_type != NULL)
3009 reset_exc_info(tstate);
3010 else {
3011 assert(tstate->frame->f_exc_value == NULL);
3012 assert(tstate->frame->f_exc_traceback == NULL);
3013 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003014
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003015 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003016exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003017 Py_LeaveRecursiveCall();
3018 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003020 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003021}
3022
Guido van Rossumc2e20742006-02-27 22:32:47 +00003023/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003024 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003025 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003026
Tim Peters6d6c1a32001-08-02 04:15:00 +00003027PyObject *
3028PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003029 PyObject **args, int argcount, PyObject **kws, int kwcount,
3030 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003031{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003032 register PyFrameObject *f;
3033 register PyObject *retval = NULL;
3034 register PyObject **fastlocals, **freevars;
3035 PyThreadState *tstate = PyThreadState_GET();
3036 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003037
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003038 if (globals == NULL) {
3039 PyErr_SetString(PyExc_SystemError,
3040 "PyEval_EvalCodeEx: NULL globals");
3041 return NULL;
3042 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003043
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003044 assert(tstate != NULL);
3045 assert(globals != NULL);
3046 f = PyFrame_New(tstate, co, globals, locals);
3047 if (f == NULL)
3048 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003049
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003050 fastlocals = f->f_localsplus;
3051 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003053 if (co->co_argcount > 0 ||
3054 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3055 int i;
3056 int n = argcount;
3057 PyObject *kwdict = NULL;
3058 if (co->co_flags & CO_VARKEYWORDS) {
3059 kwdict = PyDict_New();
3060 if (kwdict == NULL)
3061 goto fail;
3062 i = co->co_argcount;
3063 if (co->co_flags & CO_VARARGS)
3064 i++;
3065 SETLOCAL(i, kwdict);
3066 }
3067 if (argcount > co->co_argcount) {
3068 if (!(co->co_flags & CO_VARARGS)) {
3069 PyErr_Format(PyExc_TypeError,
3070 "%.200s() takes %s %d "
3071 "argument%s (%d given)",
3072 PyString_AsString(co->co_name),
3073 defcount ? "at most" : "exactly",
3074 co->co_argcount,
3075 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003076 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003077 goto fail;
3078 }
3079 n = co->co_argcount;
3080 }
3081 for (i = 0; i < n; i++) {
3082 x = args[i];
3083 Py_INCREF(x);
3084 SETLOCAL(i, x);
3085 }
3086 if (co->co_flags & CO_VARARGS) {
3087 u = PyTuple_New(argcount - n);
3088 if (u == NULL)
3089 goto fail;
3090 SETLOCAL(co->co_argcount, u);
3091 for (i = n; i < argcount; i++) {
3092 x = args[i];
3093 Py_INCREF(x);
3094 PyTuple_SET_ITEM(u, i-n, x);
3095 }
3096 }
3097 for (i = 0; i < kwcount; i++) {
3098 PyObject **co_varnames;
3099 PyObject *keyword = kws[2*i];
3100 PyObject *value = kws[2*i + 1];
3101 int j;
3102 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003103#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003104 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003105#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003106 )) {
3107 PyErr_Format(PyExc_TypeError,
3108 "%.200s() keywords must be strings",
3109 PyString_AsString(co->co_name));
3110 goto fail;
3111 }
3112 /* Speed hack: do raw pointer compares. As names are
3113 normally interned this should almost always hit. */
3114 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3115 for (j = 0; j < co->co_argcount; j++) {
3116 PyObject *nm = co_varnames[j];
3117 if (nm == keyword)
3118 goto kw_found;
3119 }
3120 /* Slow fallback, just in case */
3121 for (j = 0; j < co->co_argcount; j++) {
3122 PyObject *nm = co_varnames[j];
3123 int cmp = PyObject_RichCompareBool(
3124 keyword, nm, Py_EQ);
3125 if (cmp > 0)
3126 goto kw_found;
3127 else if (cmp < 0)
3128 goto fail;
3129 }
3130 if (kwdict == NULL) {
3131 PyObject *kwd_str = kwd_as_string(keyword);
3132 if (kwd_str) {
3133 PyErr_Format(PyExc_TypeError,
3134 "%.200s() got an unexpected "
3135 "keyword argument '%.400s'",
3136 PyString_AsString(co->co_name),
3137 PyString_AsString(kwd_str));
3138 Py_DECREF(kwd_str);
3139 }
3140 goto fail;
3141 }
3142 PyDict_SetItem(kwdict, keyword, value);
3143 continue;
3144 kw_found:
3145 if (GETLOCAL(j) != NULL) {
3146 PyObject *kwd_str = kwd_as_string(keyword);
3147 if (kwd_str) {
3148 PyErr_Format(PyExc_TypeError,
3149 "%.200s() got multiple "
3150 "values for keyword "
3151 "argument '%.400s'",
3152 PyString_AsString(co->co_name),
3153 PyString_AsString(kwd_str));
3154 Py_DECREF(kwd_str);
3155 }
3156 goto fail;
3157 }
3158 Py_INCREF(value);
3159 SETLOCAL(j, value);
3160 }
3161 if (argcount < co->co_argcount) {
3162 int m = co->co_argcount - defcount;
3163 for (i = argcount; i < m; i++) {
3164 if (GETLOCAL(i) == NULL) {
3165 int j, given = 0;
3166 for (j = 0; j < co->co_argcount; j++)
3167 if (GETLOCAL(j))
3168 given++;
3169 PyErr_Format(PyExc_TypeError,
3170 "%.200s() takes %s %d "
3171 "argument%s (%d given)",
3172 PyString_AsString(co->co_name),
3173 ((co->co_flags & CO_VARARGS) ||
3174 defcount) ? "at least"
3175 : "exactly",
3176 m, m == 1 ? "" : "s", given);
3177 goto fail;
3178 }
3179 }
3180 if (n > m)
3181 i = n - m;
3182 else
3183 i = 0;
3184 for (; i < defcount; i++) {
3185 if (GETLOCAL(m+i) == NULL) {
3186 PyObject *def = defs[i];
3187 Py_INCREF(def);
3188 SETLOCAL(m+i, def);
3189 }
3190 }
3191 }
3192 }
3193 else if (argcount > 0 || kwcount > 0) {
3194 PyErr_Format(PyExc_TypeError,
3195 "%.200s() takes no arguments (%d given)",
3196 PyString_AsString(co->co_name),
3197 argcount + kwcount);
3198 goto fail;
3199 }
3200 /* Allocate and initialize storage for cell vars, and copy free
3201 vars into frame. This isn't too efficient right now. */
3202 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3203 int i, j, nargs, found;
3204 char *cellname, *argname;
3205 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003207 nargs = co->co_argcount;
3208 if (co->co_flags & CO_VARARGS)
3209 nargs++;
3210 if (co->co_flags & CO_VARKEYWORDS)
3211 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003212
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003213 /* Initialize each cell var, taking into account
3214 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003215
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003216 Should arrange for the compiler to put cellvars
3217 that are arguments at the beginning of the cellvars
3218 list so that we can march over it more efficiently?
3219 */
3220 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3221 cellname = PyString_AS_STRING(
3222 PyTuple_GET_ITEM(co->co_cellvars, i));
3223 found = 0;
3224 for (j = 0; j < nargs; j++) {
3225 argname = PyString_AS_STRING(
3226 PyTuple_GET_ITEM(co->co_varnames, j));
3227 if (strcmp(cellname, argname) == 0) {
3228 c = PyCell_New(GETLOCAL(j));
3229 if (c == NULL)
3230 goto fail;
3231 GETLOCAL(co->co_nlocals + i) = c;
3232 found = 1;
3233 break;
3234 }
3235 }
3236 if (found == 0) {
3237 c = PyCell_New(NULL);
3238 if (c == NULL)
3239 goto fail;
3240 SETLOCAL(co->co_nlocals + i, c);
3241 }
3242 }
3243 }
3244 if (PyTuple_GET_SIZE(co->co_freevars)) {
3245 int i;
3246 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3247 PyObject *o = PyTuple_GET_ITEM(closure, i);
3248 Py_INCREF(o);
3249 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3250 }
3251 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003252
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003253 if (co->co_flags & CO_GENERATOR) {
3254 /* Don't need to keep the reference to f_back, it will be set
3255 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003256 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003257
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003258 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003259
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003260 /* Create a new generator that owns the ready to run frame
3261 * and return that as the value. */
3262 return PyGen_New(f);
3263 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003264
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003265 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003266
Thomas Woutersae406c62007-09-19 17:27:43 +00003267fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003268
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003269 /* decref'ing the frame can cause __del__ methods to get invoked,
3270 which can call back into Python. While we're done with the
3271 current Python frame (f), the associated C stack is still in use,
3272 so recursion_depth must be boosted for the duration.
3273 */
3274 assert(tstate != NULL);
3275 ++tstate->recursion_depth;
3276 Py_DECREF(f);
3277 --tstate->recursion_depth;
3278 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003279}
3280
3281
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003282static PyObject *
3283special_lookup(PyObject *o, char *meth, PyObject **cache)
3284{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003285 PyObject *res;
3286 if (PyInstance_Check(o)) {
3287 if (!*cache)
3288 return PyObject_GetAttrString(o, meth);
3289 else
3290 return PyObject_GetAttr(o, *cache);
3291 }
3292 res = _PyObject_LookupSpecial(o, meth, cache);
3293 if (res == NULL && !PyErr_Occurred()) {
3294 PyErr_SetObject(PyExc_AttributeError, *cache);
3295 return NULL;
3296 }
3297 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003298}
3299
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003300
Benjamin Petersone18ef192009-01-20 14:21:16 +00003301static PyObject *
3302kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003303#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003304 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003305#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003306 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003307#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003308 Py_INCREF(kwd);
3309 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003310#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003311 }
3312 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003313#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003314}
3315
3316
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003317/* Implementation notes for set_exc_info() and reset_exc_info():
3318
3319- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3320 'exc_traceback'. These always travel together.
3321
3322- tstate->curexc_ZZZ is the "hot" exception that is set by
3323 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3324
3325- Once an exception is caught by an except clause, it is transferred
3326 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3327 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003328 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003329
3330- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3331
3332 Long ago, when none of this existed, there were just a few globals:
3333 one set corresponding to the "hot" exception, and one set
3334 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3335 globals; they were simply stored as sys.exc_ZZZ. For backwards
3336 compatibility, they still are!) The problem was that in code like
3337 this:
3338
3339 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003340 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003341 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003342 "do something else first"
3343 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003344
3345 if "do something else first" invoked something that raised and caught
3346 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3347 cause of subtle bugs. I fixed this by changing the semantics as
3348 follows:
3349
3350 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3351 *in that frame*.
3352
3353 - But initially, and as long as no exception is caught in a given
3354 frame, sys.exc_ZZZ will hold the last exception caught in the
3355 previous frame (or the frame before that, etc.).
3356
3357 The first bullet fixed the bug in the above example. The second
3358 bullet was for backwards compatibility: it was (and is) common to
3359 have a function that is called when an exception is caught, and to
3360 have that function access the caught exception via sys.exc_ZZZ.
3361 (Example: traceback.print_exc()).
3362
3363 At the same time I fixed the problem that sys.exc_ZZZ weren't
3364 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3365 but that's really a separate improvement.
3366
3367 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3368 variables to what they were before the current frame was called. The
3369 set_exc_info() function saves them on the frame so that
3370 reset_exc_info() can restore them. The invariant is that
3371 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3372 exception (where "catching" an exception applies only to successful
3373 except clauses); and if the current frame ever caught an exception,
3374 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3375 at the start of the current frame.
3376
3377*/
3378
Fredrik Lundh7a830892006-05-27 10:39:48 +00003379static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003380set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003381 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003382{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003383 PyFrameObject *frame = tstate->frame;
3384 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003385
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003386 assert(type != NULL);
3387 assert(frame != NULL);
3388 if (frame->f_exc_type == NULL) {
3389 assert(frame->f_exc_value == NULL);
3390 assert(frame->f_exc_traceback == NULL);
3391 /* This frame didn't catch an exception before. */
3392 /* Save previous exception of this thread in this frame. */
3393 if (tstate->exc_type == NULL) {
3394 /* XXX Why is this set to Py_None? */
3395 Py_INCREF(Py_None);
3396 tstate->exc_type = Py_None;
3397 }
3398 Py_INCREF(tstate->exc_type);
3399 Py_XINCREF(tstate->exc_value);
3400 Py_XINCREF(tstate->exc_traceback);
3401 frame->f_exc_type = tstate->exc_type;
3402 frame->f_exc_value = tstate->exc_value;
3403 frame->f_exc_traceback = tstate->exc_traceback;
3404 }
3405 /* Set new exception for this thread. */
3406 tmp_type = tstate->exc_type;
3407 tmp_value = tstate->exc_value;
3408 tmp_tb = tstate->exc_traceback;
3409 Py_INCREF(type);
3410 Py_XINCREF(value);
3411 Py_XINCREF(tb);
3412 tstate->exc_type = type;
3413 tstate->exc_value = value;
3414 tstate->exc_traceback = tb;
3415 Py_XDECREF(tmp_type);
3416 Py_XDECREF(tmp_value);
3417 Py_XDECREF(tmp_tb);
3418 /* For b/w compatibility */
3419 PySys_SetObject("exc_type", type);
3420 PySys_SetObject("exc_value", value);
3421 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003422}
3423
Fredrik Lundh7a830892006-05-27 10:39:48 +00003424static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003425reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003426{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003427 PyFrameObject *frame;
3428 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003429
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003430 /* It's a precondition that the thread state's frame caught an
3431 * exception -- verify in a debug build.
3432 */
3433 assert(tstate != NULL);
3434 frame = tstate->frame;
3435 assert(frame != NULL);
3436 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003437
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003438 /* Copy the frame's exception info back to the thread state. */
3439 tmp_type = tstate->exc_type;
3440 tmp_value = tstate->exc_value;
3441 tmp_tb = tstate->exc_traceback;
3442 Py_INCREF(frame->f_exc_type);
3443 Py_XINCREF(frame->f_exc_value);
3444 Py_XINCREF(frame->f_exc_traceback);
3445 tstate->exc_type = frame->f_exc_type;
3446 tstate->exc_value = frame->f_exc_value;
3447 tstate->exc_traceback = frame->f_exc_traceback;
3448 Py_XDECREF(tmp_type);
3449 Py_XDECREF(tmp_value);
3450 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003451
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003452 /* For b/w compatibility */
3453 PySys_SetObject("exc_type", frame->f_exc_type);
3454 PySys_SetObject("exc_value", frame->f_exc_value);
3455 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003456
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003457 /* Clear the frame's exception info. */
3458 tmp_type = frame->f_exc_type;
3459 tmp_value = frame->f_exc_value;
3460 tmp_tb = frame->f_exc_traceback;
3461 frame->f_exc_type = NULL;
3462 frame->f_exc_value = NULL;
3463 frame->f_exc_traceback = NULL;
3464 Py_DECREF(tmp_type);
3465 Py_XDECREF(tmp_value);
3466 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003467}
3468
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003469/* Logic for the raise statement (too complicated for inlining).
3470 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003471static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003472do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003473{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003474 if (type == NULL) {
3475 /* Reraise */
3476 PyThreadState *tstate = PyThreadState_GET();
3477 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3478 value = tstate->exc_value;
3479 tb = tstate->exc_traceback;
3480 Py_XINCREF(type);
3481 Py_XINCREF(value);
3482 Py_XINCREF(tb);
3483 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003484
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003485 /* We support the following forms of raise:
3486 raise <class>, <classinstance>
3487 raise <class>, <argument tuple>
3488 raise <class>, None
3489 raise <class>, <argument>
3490 raise <classinstance>, None
3491 raise <string>, <object>
3492 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003493
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003494 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003495
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003496 In addition, raise <tuple>, <anything> is the same as
3497 raising the tuple's first item (and it better have one!);
3498 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003499
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003500 Finally, an optional third argument can be supplied, which
3501 gives the traceback to be substituted (useful when
3502 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003503
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003504 /* First, check the traceback argument, replacing None with
3505 NULL. */
3506 if (tb == Py_None) {
3507 Py_DECREF(tb);
3508 tb = NULL;
3509 }
3510 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3511 PyErr_SetString(PyExc_TypeError,
3512 "raise: arg 3 must be a traceback or None");
3513 goto raise_error;
3514 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003516 /* Next, replace a missing value with None */
3517 if (value == NULL) {
3518 value = Py_None;
3519 Py_INCREF(value);
3520 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003521
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003522 /* Next, repeatedly, replace a tuple exception with its first item */
3523 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3524 PyObject *tmp = type;
3525 type = PyTuple_GET_ITEM(type, 0);
3526 Py_INCREF(type);
3527 Py_DECREF(tmp);
3528 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003529
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003530 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003531 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003532 if (!PyExceptionInstance_Check(value)) {
3533 PyErr_Format(PyExc_TypeError,
3534 "calling %s() should have returned an instance of "
3535 "BaseException, not '%s'",
3536 ((PyTypeObject *)type)->tp_name,
3537 Py_TYPE(value)->tp_name);
3538 goto raise_error;
3539 }
3540 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003541 else if (PyExceptionInstance_Check(type)) {
3542 /* Raising an instance. The value should be a dummy. */
3543 if (value != Py_None) {
3544 PyErr_SetString(PyExc_TypeError,
3545 "instance exception may not have a separate value");
3546 goto raise_error;
3547 }
3548 else {
3549 /* Normalize to raise <class>, <instance> */
3550 Py_DECREF(value);
3551 value = type;
3552 type = PyExceptionInstance_Class(type);
3553 Py_INCREF(type);
3554 }
3555 }
3556 else {
3557 /* Not something you can raise. You get an exception
3558 anyway, just not what you specified :-) */
3559 PyErr_Format(PyExc_TypeError,
3560 "exceptions must be old-style classes or "
3561 "derived from BaseException, not %s",
3562 type->ob_type->tp_name);
3563 goto raise_error;
3564 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003565
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003566 assert(PyExceptionClass_Check(type));
3567 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3568 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3569 "exceptions must derive from BaseException "
3570 "in 3.x", 1) < 0)
3571 goto raise_error;
3572 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003573
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003574 PyErr_Restore(type, value, tb);
3575 if (tb == NULL)
3576 return WHY_EXCEPTION;
3577 else
3578 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003579 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003580 Py_XDECREF(value);
3581 Py_XDECREF(type);
3582 Py_XDECREF(tb);
3583 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003584}
3585
Tim Petersd6d010b2001-06-21 02:49:55 +00003586/* Iterate v argcnt times and store the results on the stack (via decreasing
3587 sp). Return 1 for success, 0 if error. */
3588
Fredrik Lundh7a830892006-05-27 10:39:48 +00003589static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003590unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003591{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003592 int i = 0;
3593 PyObject *it; /* iter(v) */
3594 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003595
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003596 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003597
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003598 it = PyObject_GetIter(v);
3599 if (it == NULL)
3600 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003601
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003602 for (; i < argcnt; i++) {
3603 w = PyIter_Next(it);
3604 if (w == NULL) {
3605 /* Iterator done, via error or exhaustion. */
3606 if (!PyErr_Occurred()) {
3607 PyErr_Format(PyExc_ValueError,
3608 "need more than %d value%s to unpack",
3609 i, i == 1 ? "" : "s");
3610 }
3611 goto Error;
3612 }
3613 *--sp = w;
3614 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003615
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003616 /* We better have exhausted the iterator now. */
3617 w = PyIter_Next(it);
3618 if (w == NULL) {
3619 if (PyErr_Occurred())
3620 goto Error;
3621 Py_DECREF(it);
3622 return 1;
3623 }
3624 Py_DECREF(w);
3625 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3626 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003627Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003628 for (; i > 0; i--, sp++)
3629 Py_DECREF(*sp);
3630 Py_XDECREF(it);
3631 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003632}
3633
3634
Guido van Rossum96a42c81992-01-12 02:29:51 +00003635#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003636static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003637prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003638{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003639 printf("%s ", str);
3640 if (PyObject_Print(v, stdout, 0) != 0)
3641 PyErr_Clear(); /* Don't know what else to do */
3642 printf("\n");
3643 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003644}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003645#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003646
Fredrik Lundh7a830892006-05-27 10:39:48 +00003647static void
Fred Drake5755ce62001-06-27 19:19:46 +00003648call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003649{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003650 PyObject *type, *value, *traceback, *arg;
3651 int err;
3652 PyErr_Fetch(&type, &value, &traceback);
3653 if (value == NULL) {
3654 value = Py_None;
3655 Py_INCREF(value);
3656 }
3657 arg = PyTuple_Pack(3, type, value, traceback);
3658 if (arg == NULL) {
3659 PyErr_Restore(type, value, traceback);
3660 return;
3661 }
3662 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3663 Py_DECREF(arg);
3664 if (err == 0)
3665 PyErr_Restore(type, value, traceback);
3666 else {
3667 Py_XDECREF(type);
3668 Py_XDECREF(value);
3669 Py_XDECREF(traceback);
3670 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003671}
3672
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003673static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003674call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003675 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003676{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003677 PyObject *type, *value, *traceback;
3678 int err;
3679 PyErr_Fetch(&type, &value, &traceback);
3680 err = call_trace(func, obj, frame, what, arg);
3681 if (err == 0)
3682 {
3683 PyErr_Restore(type, value, traceback);
3684 return 0;
3685 }
3686 else {
3687 Py_XDECREF(type);
3688 Py_XDECREF(value);
3689 Py_XDECREF(traceback);
3690 return -1;
3691 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003692}
3693
Fredrik Lundh7a830892006-05-27 10:39:48 +00003694static int
Fred Drake5755ce62001-06-27 19:19:46 +00003695call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003696 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003697{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003698 register PyThreadState *tstate = frame->f_tstate;
3699 int result;
3700 if (tstate->tracing)
3701 return 0;
3702 tstate->tracing++;
3703 tstate->use_tracing = 0;
3704 result = func(obj, frame, what, arg);
3705 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3706 || (tstate->c_profilefunc != NULL));
3707 tstate->tracing--;
3708 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003709}
3710
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003711PyObject *
3712_PyEval_CallTracing(PyObject *func, PyObject *args)
3713{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003714 PyFrameObject *frame = PyEval_GetFrame();
3715 PyThreadState *tstate = frame->f_tstate;
3716 int save_tracing = tstate->tracing;
3717 int save_use_tracing = tstate->use_tracing;
3718 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003719
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003720 tstate->tracing = 0;
3721 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3722 || (tstate->c_profilefunc != NULL));
3723 result = PyObject_Call(func, args, NULL);
3724 tstate->tracing = save_tracing;
3725 tstate->use_tracing = save_use_tracing;
3726 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003727}
3728
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003729/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003730static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003731maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003732 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3733 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003734{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003735 int result = 0;
3736 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003737
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003738 /* If the last instruction executed isn't in the current
3739 instruction window, reset the window.
3740 */
3741 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3742 PyAddrPair bounds;
3743 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3744 &bounds);
3745 *instr_lb = bounds.ap_lower;
3746 *instr_ub = bounds.ap_upper;
3747 }
3748 /* If the last instruction falls at the start of a line or if
3749 it represents a jump backwards, update the frame's line
3750 number and call the trace function. */
3751 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3752 frame->f_lineno = line;
3753 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3754 }
3755 *instr_prev = frame->f_lasti;
3756 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003757}
3758
Fred Drake5755ce62001-06-27 19:19:46 +00003759void
3760PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003761{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003762 PyThreadState *tstate = PyThreadState_GET();
3763 PyObject *temp = tstate->c_profileobj;
3764 Py_XINCREF(arg);
3765 tstate->c_profilefunc = NULL;
3766 tstate->c_profileobj = NULL;
3767 /* Must make sure that tracing is not ignored if 'temp' is freed */
3768 tstate->use_tracing = tstate->c_tracefunc != NULL;
3769 Py_XDECREF(temp);
3770 tstate->c_profilefunc = func;
3771 tstate->c_profileobj = arg;
3772 /* Flag that tracing or profiling is turned on */
3773 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003774}
3775
3776void
3777PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3778{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003779 PyThreadState *tstate = PyThreadState_GET();
3780 PyObject *temp = tstate->c_traceobj;
3781 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3782 Py_XINCREF(arg);
3783 tstate->c_tracefunc = NULL;
3784 tstate->c_traceobj = NULL;
3785 /* Must make sure that profiling is not ignored if 'temp' is freed */
3786 tstate->use_tracing = tstate->c_profilefunc != NULL;
3787 Py_XDECREF(temp);
3788 tstate->c_tracefunc = func;
3789 tstate->c_traceobj = arg;
3790 /* Flag that tracing or profiling is turned on */
3791 tstate->use_tracing = ((func != NULL)
3792 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003793}
3794
Guido van Rossumb209a111997-04-29 18:18:01 +00003795PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003796PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003797{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003798 PyFrameObject *current_frame = PyEval_GetFrame();
3799 if (current_frame == NULL)
3800 return PyThreadState_GET()->interp->builtins;
3801 else
3802 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003803}
3804
Guido van Rossumb209a111997-04-29 18:18:01 +00003805PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003806PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003807{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003808 PyFrameObject *current_frame = PyEval_GetFrame();
3809 if (current_frame == NULL)
3810 return NULL;
3811 PyFrame_FastToLocals(current_frame);
3812 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003813}
3814
Guido van Rossumb209a111997-04-29 18:18:01 +00003815PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003816PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003817{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003818 PyFrameObject *current_frame = PyEval_GetFrame();
3819 if (current_frame == NULL)
3820 return NULL;
3821 else
3822 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003823}
3824
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003825PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003826PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003827{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003828 PyThreadState *tstate = PyThreadState_GET();
3829 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003830}
3831
Guido van Rossum6135a871995-01-09 17:53:26 +00003832int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003833PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003834{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003835 PyFrameObject *current_frame = PyEval_GetFrame();
3836 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003837}
3838
Guido van Rossumbe270261997-05-22 22:26:18 +00003839int
Tim Peters5ba58662001-07-16 02:29:45 +00003840PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003841{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003842 PyFrameObject *current_frame = PyEval_GetFrame();
3843 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003844
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003845 if (current_frame != NULL) {
3846 const int codeflags = current_frame->f_code->co_flags;
3847 const int compilerflags = codeflags & PyCF_MASK;
3848 if (compilerflags) {
3849 result = 1;
3850 cf->cf_flags |= compilerflags;
3851 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003852#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003853 if (codeflags & CO_GENERATOR_ALLOWED) {
3854 result = 1;
3855 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3856 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003857#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003858 }
3859 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003860}
3861
3862int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003863Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003864{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003865 PyObject *f = PySys_GetObject("stdout");
3866 if (f == NULL)
3867 return 0;
3868 if (!PyFile_SoftSpace(f, 0))
3869 return 0;
3870 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003871}
3872
Guido van Rossum3f5da241990-12-20 15:06:42 +00003873
Guido van Rossum681d79a1995-07-18 14:51:37 +00003874/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00003875 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003876
Guido van Rossumb209a111997-04-29 18:18:01 +00003877PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003878PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003879{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003880 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003881
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003882 if (arg == NULL) {
3883 arg = PyTuple_New(0);
3884 if (arg == NULL)
3885 return NULL;
3886 }
3887 else if (!PyTuple_Check(arg)) {
3888 PyErr_SetString(PyExc_TypeError,
3889 "argument list must be a tuple");
3890 return NULL;
3891 }
3892 else
3893 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003894
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003895 if (kw != NULL && !PyDict_Check(kw)) {
3896 PyErr_SetString(PyExc_TypeError,
3897 "keyword list must be a dictionary");
3898 Py_DECREF(arg);
3899 return NULL;
3900 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003901
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003902 result = PyObject_Call(func, arg, kw);
3903 Py_DECREF(arg);
3904 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003905}
3906
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003907const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003908PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003909{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003910 if (PyMethod_Check(func))
3911 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3912 else if (PyFunction_Check(func))
3913 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3914 else if (PyCFunction_Check(func))
3915 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3916 else if (PyClass_Check(func))
3917 return PyString_AsString(((PyClassObject*)func)->cl_name);
3918 else if (PyInstance_Check(func)) {
3919 return PyString_AsString(
3920 ((PyInstanceObject*)func)->in_class->cl_name);
3921 } else {
3922 return func->ob_type->tp_name;
3923 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003924}
3925
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003926const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003927PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003928{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003929 if (PyMethod_Check(func))
3930 return "()";
3931 else if (PyFunction_Check(func))
3932 return "()";
3933 else if (PyCFunction_Check(func))
3934 return "()";
3935 else if (PyClass_Check(func))
3936 return " constructor";
3937 else if (PyInstance_Check(func)) {
3938 return " instance";
3939 } else {
3940 return " object";
3941 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003942}
3943
Fredrik Lundh7a830892006-05-27 10:39:48 +00003944static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003945err_args(PyObject *func, int flags, int nargs)
3946{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003947 if (flags & METH_NOARGS)
3948 PyErr_Format(PyExc_TypeError,
3949 "%.200s() takes no arguments (%d given)",
3950 ((PyCFunctionObject *)func)->m_ml->ml_name,
3951 nargs);
3952 else
3953 PyErr_Format(PyExc_TypeError,
3954 "%.200s() takes exactly one argument (%d given)",
3955 ((PyCFunctionObject *)func)->m_ml->ml_name,
3956 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003957}
3958
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003959#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003960if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003961 if (call_trace(tstate->c_profilefunc, \
3962 tstate->c_profileobj, \
3963 tstate->frame, PyTrace_C_CALL, \
3964 func)) { \
3965 x = NULL; \
3966 } \
3967 else { \
3968 x = call; \
3969 if (tstate->c_profilefunc != NULL) { \
3970 if (x == NULL) { \
3971 call_trace_protected(tstate->c_profilefunc, \
3972 tstate->c_profileobj, \
3973 tstate->frame, PyTrace_C_EXCEPTION, \
3974 func); \
3975 /* XXX should pass (type, value, tb) */ \
3976 } else { \
3977 if (call_trace(tstate->c_profilefunc, \
3978 tstate->c_profileobj, \
3979 tstate->frame, PyTrace_C_RETURN, \
3980 func)) { \
3981 Py_DECREF(x); \
3982 x = NULL; \
3983 } \
3984 } \
3985 } \
3986 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003987} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003988 x = call; \
3989 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003990
Fredrik Lundh7a830892006-05-27 10:39:48 +00003991static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003992call_function(PyObject ***pp_stack, int oparg
3993#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003994 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003995#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003996 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003997{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003998 int na = oparg & 0xff;
3999 int nk = (oparg>>8) & 0xff;
4000 int n = na + 2 * nk;
4001 PyObject **pfunc = (*pp_stack) - n - 1;
4002 PyObject *func = *pfunc;
4003 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004004
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004005 /* Always dispatch PyCFunction first, because these are
4006 presumed to be the most frequent callable object.
4007 */
4008 if (PyCFunction_Check(func) && nk == 0) {
4009 int flags = PyCFunction_GET_FLAGS(func);
4010 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004012 PCALL(PCALL_CFUNCTION);
4013 if (flags & (METH_NOARGS | METH_O)) {
4014 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4015 PyObject *self = PyCFunction_GET_SELF(func);
4016 if (flags & METH_NOARGS && na == 0) {
4017 C_TRACE(x, (*meth)(self,NULL));
4018 }
4019 else if (flags & METH_O && na == 1) {
4020 PyObject *arg = EXT_POP(*pp_stack);
4021 C_TRACE(x, (*meth)(self,arg));
4022 Py_DECREF(arg);
4023 }
4024 else {
4025 err_args(func, flags, na);
4026 x = NULL;
4027 }
4028 }
4029 else {
4030 PyObject *callargs;
4031 callargs = load_args(pp_stack, na);
4032 READ_TIMESTAMP(*pintr0);
4033 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4034 READ_TIMESTAMP(*pintr1);
4035 Py_XDECREF(callargs);
4036 }
4037 } else {
4038 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4039 /* optimize access to bound methods */
4040 PyObject *self = PyMethod_GET_SELF(func);
4041 PCALL(PCALL_METHOD);
4042 PCALL(PCALL_BOUND_METHOD);
4043 Py_INCREF(self);
4044 func = PyMethod_GET_FUNCTION(func);
4045 Py_INCREF(func);
4046 Py_DECREF(*pfunc);
4047 *pfunc = self;
4048 na++;
4049 n++;
4050 } else
4051 Py_INCREF(func);
4052 READ_TIMESTAMP(*pintr0);
4053 if (PyFunction_Check(func))
4054 x = fast_function(func, pp_stack, n, na, nk);
4055 else
4056 x = do_call(func, pp_stack, na, nk);
4057 READ_TIMESTAMP(*pintr1);
4058 Py_DECREF(func);
4059 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004060
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004061 /* Clear the stack of the function object. Also removes
4062 the arguments in case they weren't consumed already
4063 (fast_function() and err_args() leave them on the stack).
4064 */
4065 while ((*pp_stack) > pfunc) {
4066 w = EXT_POP(*pp_stack);
4067 Py_DECREF(w);
4068 PCALL(PCALL_POP);
4069 }
4070 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004071}
4072
Jeremy Hylton192690e2002-08-16 18:36:11 +00004073/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004074 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004075 For the simplest case -- a function that takes only positional
4076 arguments and is called with only positional arguments -- it
4077 inlines the most primitive frame setup code from
4078 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4079 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004080*/
4081
Fredrik Lundh7a830892006-05-27 10:39:48 +00004082static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004083fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004084{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004085 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4086 PyObject *globals = PyFunction_GET_GLOBALS(func);
4087 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4088 PyObject **d = NULL;
4089 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004090
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004091 PCALL(PCALL_FUNCTION);
4092 PCALL(PCALL_FAST_FUNCTION);
4093 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4094 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4095 PyFrameObject *f;
4096 PyObject *retval = NULL;
4097 PyThreadState *tstate = PyThreadState_GET();
4098 PyObject **fastlocals, **stack;
4099 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004100
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004101 PCALL(PCALL_FASTER_FUNCTION);
4102 assert(globals != NULL);
4103 /* XXX Perhaps we should create a specialized
4104 PyFrame_New() that doesn't take locals, but does
4105 take builtins without sanity checking them.
4106 */
4107 assert(tstate != NULL);
4108 f = PyFrame_New(tstate, co, globals, NULL);
4109 if (f == NULL)
4110 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004111
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004112 fastlocals = f->f_localsplus;
4113 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004114
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004115 for (i = 0; i < n; i++) {
4116 Py_INCREF(*stack);
4117 fastlocals[i] = *stack++;
4118 }
4119 retval = PyEval_EvalFrameEx(f,0);
4120 ++tstate->recursion_depth;
4121 Py_DECREF(f);
4122 --tstate->recursion_depth;
4123 return retval;
4124 }
4125 if (argdefs != NULL) {
4126 d = &PyTuple_GET_ITEM(argdefs, 0);
4127 nd = Py_SIZE(argdefs);
4128 }
4129 return PyEval_EvalCodeEx(co, globals,
4130 (PyObject *)NULL, (*pp_stack)-n, na,
4131 (*pp_stack)-2*nk, nk, d, nd,
4132 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004133}
4134
Fredrik Lundh7a830892006-05-27 10:39:48 +00004135static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004136update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4137 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004138{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004139 PyObject *kwdict = NULL;
4140 if (orig_kwdict == NULL)
4141 kwdict = PyDict_New();
4142 else {
4143 kwdict = PyDict_Copy(orig_kwdict);
4144 Py_DECREF(orig_kwdict);
4145 }
4146 if (kwdict == NULL)
4147 return NULL;
4148 while (--nk >= 0) {
4149 int err;
4150 PyObject *value = EXT_POP(*pp_stack);
4151 PyObject *key = EXT_POP(*pp_stack);
4152 if (PyDict_GetItem(kwdict, key) != NULL) {
4153 PyErr_Format(PyExc_TypeError,
4154 "%.200s%s got multiple values "
4155 "for keyword argument '%.200s'",
4156 PyEval_GetFuncName(func),
4157 PyEval_GetFuncDesc(func),
4158 PyString_AsString(key));
4159 Py_DECREF(key);
4160 Py_DECREF(value);
4161 Py_DECREF(kwdict);
4162 return NULL;
4163 }
4164 err = PyDict_SetItem(kwdict, key, value);
4165 Py_DECREF(key);
4166 Py_DECREF(value);
4167 if (err) {
4168 Py_DECREF(kwdict);
4169 return NULL;
4170 }
4171 }
4172 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004173}
4174
Fredrik Lundh7a830892006-05-27 10:39:48 +00004175static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004176update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004177 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004178{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004179 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004180
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004181 callargs = PyTuple_New(nstack + nstar);
4182 if (callargs == NULL) {
4183 return NULL;
4184 }
4185 if (nstar) {
4186 int i;
4187 for (i = 0; i < nstar; i++) {
4188 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4189 Py_INCREF(a);
4190 PyTuple_SET_ITEM(callargs, nstack + i, a);
4191 }
4192 }
4193 while (--nstack >= 0) {
4194 w = EXT_POP(*pp_stack);
4195 PyTuple_SET_ITEM(callargs, nstack, w);
4196 }
4197 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004198}
4199
Fredrik Lundh7a830892006-05-27 10:39:48 +00004200static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004201load_args(PyObject ***pp_stack, int na)
4202{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004203 PyObject *args = PyTuple_New(na);
4204 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004205
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004206 if (args == NULL)
4207 return NULL;
4208 while (--na >= 0) {
4209 w = EXT_POP(*pp_stack);
4210 PyTuple_SET_ITEM(args, na, w);
4211 }
4212 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004213}
4214
Fredrik Lundh7a830892006-05-27 10:39:48 +00004215static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004216do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4217{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004218 PyObject *callargs = NULL;
4219 PyObject *kwdict = NULL;
4220 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004221
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004222 if (nk > 0) {
4223 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4224 if (kwdict == NULL)
4225 goto call_fail;
4226 }
4227 callargs = load_args(pp_stack, na);
4228 if (callargs == NULL)
4229 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004230#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004231 /* At this point, we have to look at the type of func to
4232 update the call stats properly. Do it here so as to avoid
4233 exposing the call stats machinery outside ceval.c
4234 */
4235 if (PyFunction_Check(func))
4236 PCALL(PCALL_FUNCTION);
4237 else if (PyMethod_Check(func))
4238 PCALL(PCALL_METHOD);
4239 else if (PyType_Check(func))
4240 PCALL(PCALL_TYPE);
4241 else if (PyCFunction_Check(func))
4242 PCALL(PCALL_CFUNCTION);
4243 else
4244 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004245#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004246 if (PyCFunction_Check(func)) {
4247 PyThreadState *tstate = PyThreadState_GET();
4248 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4249 }
4250 else
4251 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004252 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004253 Py_XDECREF(callargs);
4254 Py_XDECREF(kwdict);
4255 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004256}
4257
Fredrik Lundh7a830892006-05-27 10:39:48 +00004258static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004259ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4260{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004261 int nstar = 0;
4262 PyObject *callargs = NULL;
4263 PyObject *stararg = NULL;
4264 PyObject *kwdict = NULL;
4265 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004266
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004267 if (flags & CALL_FLAG_KW) {
4268 kwdict = EXT_POP(*pp_stack);
4269 if (!PyDict_Check(kwdict)) {
4270 PyObject *d;
4271 d = PyDict_New();
4272 if (d == NULL)
4273 goto ext_call_fail;
4274 if (PyDict_Update(d, kwdict) != 0) {
4275 Py_DECREF(d);
4276 /* PyDict_Update raises attribute
4277 * error (percolated from an attempt
4278 * to get 'keys' attribute) instead of
4279 * a type error if its second argument
4280 * is not a mapping.
4281 */
4282 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4283 PyErr_Format(PyExc_TypeError,
4284 "%.200s%.200s argument after ** "
4285 "must be a mapping, not %.200s",
4286 PyEval_GetFuncName(func),
4287 PyEval_GetFuncDesc(func),
4288 kwdict->ob_type->tp_name);
4289 }
4290 goto ext_call_fail;
4291 }
4292 Py_DECREF(kwdict);
4293 kwdict = d;
4294 }
4295 }
4296 if (flags & CALL_FLAG_VAR) {
4297 stararg = EXT_POP(*pp_stack);
4298 if (!PyTuple_Check(stararg)) {
4299 PyObject *t = NULL;
4300 t = PySequence_Tuple(stararg);
4301 if (t == NULL) {
4302 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4303 PyErr_Format(PyExc_TypeError,
4304 "%.200s%.200s argument after * "
4305 "must be a sequence, not %200s",
4306 PyEval_GetFuncName(func),
4307 PyEval_GetFuncDesc(func),
4308 stararg->ob_type->tp_name);
4309 }
4310 goto ext_call_fail;
4311 }
4312 Py_DECREF(stararg);
4313 stararg = t;
4314 }
4315 nstar = PyTuple_GET_SIZE(stararg);
4316 }
4317 if (nk > 0) {
4318 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4319 if (kwdict == NULL)
4320 goto ext_call_fail;
4321 }
4322 callargs = update_star_args(na, nstar, stararg, pp_stack);
4323 if (callargs == NULL)
4324 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004325#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004326 /* At this point, we have to look at the type of func to
4327 update the call stats properly. Do it here so as to avoid
4328 exposing the call stats machinery outside ceval.c
4329 */
4330 if (PyFunction_Check(func))
4331 PCALL(PCALL_FUNCTION);
4332 else if (PyMethod_Check(func))
4333 PCALL(PCALL_METHOD);
4334 else if (PyType_Check(func))
4335 PCALL(PCALL_TYPE);
4336 else if (PyCFunction_Check(func))
4337 PCALL(PCALL_CFUNCTION);
4338 else
4339 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004340#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004341 if (PyCFunction_Check(func)) {
4342 PyThreadState *tstate = PyThreadState_GET();
4343 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4344 }
4345 else
4346 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004347ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004348 Py_XDECREF(callargs);
4349 Py_XDECREF(kwdict);
4350 Py_XDECREF(stararg);
4351 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004352}
4353
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004354/* Extract a slice index from a PyInt or PyLong or an object with the
4355 nb_index slot defined, and store in *pi.
4356 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4357 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 +00004358 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004359*/
Tim Petersb5196382001-12-16 19:44:20 +00004360/* Note: If v is NULL, return success without storing into *pi. This
4361 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4362 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004363*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004364int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004365_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004366{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004367 if (v != NULL) {
4368 Py_ssize_t x;
4369 if (PyInt_Check(v)) {
4370 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4371 however, it looks like it should be AsSsize_t.
4372 There should be a comment here explaining why.
4373 */
4374 x = PyInt_AS_LONG(v);
4375 }
4376 else if (PyIndex_Check(v)) {
4377 x = PyNumber_AsSsize_t(v, NULL);
4378 if (x == -1 && PyErr_Occurred())
4379 return 0;
4380 }
4381 else {
4382 PyErr_SetString(PyExc_TypeError,
4383 "slice indices must be integers or "
4384 "None or have an __index__ method");
4385 return 0;
4386 }
4387 *pi = x;
4388 }
4389 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004390}
4391
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004392#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004393#define ISINDEX(x) ((x) == NULL || \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004394 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004395
Fredrik Lundh7a830892006-05-27 10:39:48 +00004396static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004397apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004398{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004399 PyTypeObject *tp = u->ob_type;
4400 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004401
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004402 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4403 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4404 if (!_PyEval_SliceIndex(v, &ilow))
4405 return NULL;
4406 if (!_PyEval_SliceIndex(w, &ihigh))
4407 return NULL;
4408 return PySequence_GetSlice(u, ilow, ihigh);
4409 }
4410 else {
4411 PyObject *slice = PySlice_New(v, w, NULL);
4412 if (slice != NULL) {
4413 PyObject *res = PyObject_GetItem(u, slice);
4414 Py_DECREF(slice);
4415 return res;
4416 }
4417 else
4418 return NULL;
4419 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004420}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004421
Fredrik Lundh7a830892006-05-27 10:39:48 +00004422static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004423assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004424 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004425{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004426 PyTypeObject *tp = u->ob_type;
4427 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004428
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004429 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4430 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4431 if (!_PyEval_SliceIndex(v, &ilow))
4432 return -1;
4433 if (!_PyEval_SliceIndex(w, &ihigh))
4434 return -1;
4435 if (x == NULL)
4436 return PySequence_DelSlice(u, ilow, ihigh);
4437 else
4438 return PySequence_SetSlice(u, ilow, ihigh, x);
4439 }
4440 else {
4441 PyObject *slice = PySlice_New(v, w, NULL);
4442 if (slice != NULL) {
4443 int res;
4444 if (x != NULL)
4445 res = PyObject_SetItem(u, slice, x);
4446 else
4447 res = PyObject_DelItem(u, slice);
4448 Py_DECREF(slice);
4449 return res;
4450 }
4451 else
4452 return -1;
4453 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004454}
4455
Guido van Rossum04edb522008-03-18 02:49:46 +00004456#define Py3kExceptionClass_Check(x) \
4457 (PyType_Check((x)) && \
4458 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4459
4460#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004461 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004462
Fredrik Lundh7a830892006-05-27 10:39:48 +00004463static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004464cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004465{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004466 int res = 0;
4467 switch (op) {
4468 case PyCmp_IS:
4469 res = (v == w);
4470 break;
4471 case PyCmp_IS_NOT:
4472 res = (v != w);
4473 break;
4474 case PyCmp_IN:
4475 res = PySequence_Contains(w, v);
4476 if (res < 0)
4477 return NULL;
4478 break;
4479 case PyCmp_NOT_IN:
4480 res = PySequence_Contains(w, v);
4481 if (res < 0)
4482 return NULL;
4483 res = !res;
4484 break;
4485 case PyCmp_EXC_MATCH:
4486 if (PyTuple_Check(w)) {
4487 Py_ssize_t i, length;
4488 length = PyTuple_Size(w);
4489 for (i = 0; i < length; i += 1) {
4490 PyObject *exc = PyTuple_GET_ITEM(w, i);
4491 if (PyString_Check(exc)) {
4492 int ret_val;
4493 ret_val = PyErr_WarnEx(
4494 PyExc_DeprecationWarning,
4495 "catching of string "
4496 "exceptions is deprecated", 1);
4497 if (ret_val < 0)
4498 return NULL;
4499 }
4500 else if (Py_Py3kWarningFlag &&
4501 !PyTuple_Check(exc) &&
4502 !Py3kExceptionClass_Check(exc))
4503 {
4504 int ret_val;
4505 ret_val = PyErr_WarnEx(
4506 PyExc_DeprecationWarning,
4507 CANNOT_CATCH_MSG, 1);
4508 if (ret_val < 0)
4509 return NULL;
4510 }
4511 }
4512 }
4513 else {
4514 if (PyString_Check(w)) {
4515 int ret_val;
4516 ret_val = PyErr_WarnEx(
4517 PyExc_DeprecationWarning,
4518 "catching of string "
4519 "exceptions is deprecated", 1);
4520 if (ret_val < 0)
4521 return NULL;
4522 }
4523 else if (Py_Py3kWarningFlag &&
4524 !PyTuple_Check(w) &&
4525 !Py3kExceptionClass_Check(w))
4526 {
4527 int ret_val;
4528 ret_val = PyErr_WarnEx(
4529 PyExc_DeprecationWarning,
4530 CANNOT_CATCH_MSG, 1);
4531 if (ret_val < 0)
4532 return NULL;
4533 }
4534 }
4535 res = PyErr_GivenExceptionMatches(v, w);
4536 break;
4537 default:
4538 return PyObject_RichCompare(v, w, op);
4539 }
4540 v = res ? Py_True : Py_False;
4541 Py_INCREF(v);
4542 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004543}
4544
Fredrik Lundh7a830892006-05-27 10:39:48 +00004545static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004546import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004547{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004548 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004549
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004550 x = PyObject_GetAttr(v, name);
4551 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4552 PyErr_Format(PyExc_ImportError,
4553 "cannot import name %.230s",
4554 PyString_AsString(name));
4555 }
4556 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004557}
Guido van Rossumac7be682001-01-17 15:42:30 +00004558
Fredrik Lundh7a830892006-05-27 10:39:48 +00004559static int
Thomas Wouters52152252000-08-17 22:55:00 +00004560import_all_from(PyObject *locals, PyObject *v)
4561{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004562 PyObject *all = PyObject_GetAttrString(v, "__all__");
4563 PyObject *dict, *name, *value;
4564 int skip_leading_underscores = 0;
4565 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004566
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004567 if (all == NULL) {
4568 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4569 return -1; /* Unexpected error */
4570 PyErr_Clear();
4571 dict = PyObject_GetAttrString(v, "__dict__");
4572 if (dict == NULL) {
4573 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4574 return -1;
4575 PyErr_SetString(PyExc_ImportError,
4576 "from-import-* object has no __dict__ and no __all__");
4577 return -1;
4578 }
4579 all = PyMapping_Keys(dict);
4580 Py_DECREF(dict);
4581 if (all == NULL)
4582 return -1;
4583 skip_leading_underscores = 1;
4584 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004586 for (pos = 0, err = 0; ; pos++) {
4587 name = PySequence_GetItem(all, pos);
4588 if (name == NULL) {
4589 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4590 err = -1;
4591 else
4592 PyErr_Clear();
4593 break;
4594 }
4595 if (skip_leading_underscores &&
4596 PyString_Check(name) &&
4597 PyString_AS_STRING(name)[0] == '_')
4598 {
4599 Py_DECREF(name);
4600 continue;
4601 }
4602 value = PyObject_GetAttr(v, name);
4603 if (value == NULL)
4604 err = -1;
4605 else if (PyDict_CheckExact(locals))
4606 err = PyDict_SetItem(locals, name, value);
4607 else
4608 err = PyObject_SetItem(locals, name, value);
4609 Py_DECREF(name);
4610 Py_XDECREF(value);
4611 if (err != 0)
4612 break;
4613 }
4614 Py_DECREF(all);
4615 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004616}
4617
Fredrik Lundh7a830892006-05-27 10:39:48 +00004618static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004619build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004620{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004621 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004622
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004623 if (PyDict_Check(methods))
4624 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4625 if (metaclass != NULL)
4626 Py_INCREF(metaclass);
4627 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4628 base = PyTuple_GET_ITEM(bases, 0);
4629 metaclass = PyObject_GetAttrString(base, "__class__");
4630 if (metaclass == NULL) {
4631 PyErr_Clear();
4632 metaclass = (PyObject *)base->ob_type;
4633 Py_INCREF(metaclass);
4634 }
4635 }
4636 else {
4637 PyObject *g = PyEval_GetGlobals();
4638 if (g != NULL && PyDict_Check(g))
4639 metaclass = PyDict_GetItemString(g, "__metaclass__");
4640 if (metaclass == NULL)
4641 metaclass = (PyObject *) &PyClass_Type;
4642 Py_INCREF(metaclass);
4643 }
4644 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4645 NULL);
4646 Py_DECREF(metaclass);
4647 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4648 /* A type error here likely means that the user passed
4649 in a base that was not a class (such the random module
4650 instead of the random.random type). Help them out with
4651 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00004652
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004653 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00004654
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004655 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4656 if (PyString_Check(pvalue)) {
4657 PyObject *newmsg;
4658 newmsg = PyString_FromFormat(
4659 "Error when calling the metaclass bases\n"
4660 " %s",
4661 PyString_AS_STRING(pvalue));
4662 if (newmsg != NULL) {
4663 Py_DECREF(pvalue);
4664 pvalue = newmsg;
4665 }
4666 }
4667 PyErr_Restore(ptype, pvalue, ptraceback);
4668 }
4669 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004670}
4671
Fredrik Lundh7a830892006-05-27 10:39:48 +00004672static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004673exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004674 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004675{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004676 int n;
4677 PyObject *v;
4678 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004679
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004680 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4681 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4682 /* Backward compatibility hack */
4683 globals = PyTuple_GetItem(prog, 1);
4684 if (n == 3)
4685 locals = PyTuple_GetItem(prog, 2);
4686 prog = PyTuple_GetItem(prog, 0);
4687 }
4688 if (globals == Py_None) {
4689 globals = PyEval_GetGlobals();
4690 if (locals == Py_None) {
4691 locals = PyEval_GetLocals();
4692 plain = 1;
4693 }
4694 if (!globals || !locals) {
4695 PyErr_SetString(PyExc_SystemError,
4696 "globals and locals cannot be NULL");
4697 return -1;
4698 }
4699 }
4700 else if (locals == Py_None)
4701 locals = globals;
4702 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004703#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004704 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004705#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004706 !PyCode_Check(prog) &&
4707 !PyFile_Check(prog)) {
4708 PyErr_SetString(PyExc_TypeError,
4709 "exec: arg 1 must be a string, file, or code object");
4710 return -1;
4711 }
4712 if (!PyDict_Check(globals)) {
4713 PyErr_SetString(PyExc_TypeError,
4714 "exec: arg 2 must be a dictionary or None");
4715 return -1;
4716 }
4717 if (!PyMapping_Check(locals)) {
4718 PyErr_SetString(PyExc_TypeError,
4719 "exec: arg 3 must be a mapping or None");
4720 return -1;
4721 }
4722 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4723 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4724 if (PyCode_Check(prog)) {
4725 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4726 PyErr_SetString(PyExc_TypeError,
4727 "code object passed to exec may not contain free variables");
4728 return -1;
4729 }
4730 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4731 }
4732 else if (PyFile_Check(prog)) {
4733 FILE *fp = PyFile_AsFile(prog);
4734 char *name = PyString_AsString(PyFile_Name(prog));
4735 PyCompilerFlags cf;
4736 if (name == NULL)
4737 return -1;
4738 cf.cf_flags = 0;
4739 if (PyEval_MergeCompilerFlags(&cf))
4740 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4741 locals, &cf);
4742 else
4743 v = PyRun_File(fp, name, Py_file_input, globals,
4744 locals);
4745 }
4746 else {
4747 PyObject *tmp = NULL;
4748 char *str;
4749 PyCompilerFlags cf;
4750 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004751#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004752 if (PyUnicode_Check(prog)) {
4753 tmp = PyUnicode_AsUTF8String(prog);
4754 if (tmp == NULL)
4755 return -1;
4756 prog = tmp;
4757 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4758 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004759#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004760 if (PyString_AsStringAndSize(prog, &str, NULL))
4761 return -1;
4762 if (PyEval_MergeCompilerFlags(&cf))
4763 v = PyRun_StringFlags(str, Py_file_input, globals,
4764 locals, &cf);
4765 else
4766 v = PyRun_String(str, Py_file_input, globals, locals);
4767 Py_XDECREF(tmp);
4768 }
4769 if (plain)
4770 PyFrame_LocalsToFast(f, 0);
4771 if (v == NULL)
4772 return -1;
4773 Py_DECREF(v);
4774 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004775}
Guido van Rossum24c13741995-02-14 09:42:43 +00004776
Fredrik Lundh7a830892006-05-27 10:39:48 +00004777static void
Paul Prescode68140d2000-08-30 20:25:01 +00004778format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4779{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004780 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004781
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004782 if (!obj)
4783 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004784
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004785 obj_str = PyString_AsString(obj);
4786 if (!obj_str)
4787 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004788
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004789 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004790}
Guido van Rossum950361c1997-01-24 13:49:28 +00004791
Fredrik Lundh7a830892006-05-27 10:39:48 +00004792static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004793string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004794 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004795{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004796 /* This function implements 'variable += expr' when both arguments
4797 are strings. */
4798 Py_ssize_t v_len = PyString_GET_SIZE(v);
4799 Py_ssize_t w_len = PyString_GET_SIZE(w);
4800 Py_ssize_t new_len = v_len + w_len;
4801 if (new_len < 0) {
4802 PyErr_SetString(PyExc_OverflowError,
4803 "strings are too large to concat");
4804 return NULL;
4805 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004807 if (v->ob_refcnt == 2) {
4808 /* In the common case, there are 2 references to the value
4809 * stored in 'variable' when the += is performed: one on the
4810 * value stack (in 'v') and one still stored in the
4811 * 'variable'. We try to delete the variable now to reduce
4812 * the refcnt to 1.
4813 */
4814 switch (*next_instr) {
4815 case STORE_FAST:
4816 {
4817 int oparg = PEEKARG();
4818 PyObject **fastlocals = f->f_localsplus;
4819 if (GETLOCAL(oparg) == v)
4820 SETLOCAL(oparg, NULL);
4821 break;
4822 }
4823 case STORE_DEREF:
4824 {
4825 PyObject **freevars = (f->f_localsplus +
4826 f->f_code->co_nlocals);
4827 PyObject *c = freevars[PEEKARG()];
4828 if (PyCell_GET(c) == v)
4829 PyCell_Set(c, NULL);
4830 break;
4831 }
4832 case STORE_NAME:
4833 {
4834 PyObject *names = f->f_code->co_names;
4835 PyObject *name = GETITEM(names, PEEKARG());
4836 PyObject *locals = f->f_locals;
4837 if (PyDict_CheckExact(locals) &&
4838 PyDict_GetItem(locals, name) == v) {
4839 if (PyDict_DelItem(locals, name) != 0) {
4840 PyErr_Clear();
4841 }
4842 }
4843 break;
4844 }
4845 }
4846 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004847
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004848 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4849 /* Now we own the last reference to 'v', so we can resize it
4850 * in-place.
4851 */
4852 if (_PyString_Resize(&v, new_len) != 0) {
4853 /* XXX if _PyString_Resize() fails, 'v' has been
4854 * deallocated so it cannot be put back into
4855 * 'variable'. The MemoryError is raised when there
4856 * is no value in 'variable', which might (very
4857 * remotely) be a cause of incompatibilities.
4858 */
4859 return NULL;
4860 }
4861 /* copy 'w' into the newly allocated area of 'v' */
4862 memcpy(PyString_AS_STRING(v) + v_len,
4863 PyString_AS_STRING(w), w_len);
4864 return v;
4865 }
4866 else {
4867 /* When in-place resizing is not an option. */
4868 PyString_Concat(&v, w);
4869 return v;
4870 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004871}
4872
Guido van Rossum950361c1997-01-24 13:49:28 +00004873#ifdef DYNAMIC_EXECUTION_PROFILE
4874
Fredrik Lundh7a830892006-05-27 10:39:48 +00004875static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004876getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004877{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004878 int i;
4879 PyObject *l = PyList_New(256);
4880 if (l == NULL) return NULL;
4881 for (i = 0; i < 256; i++) {
4882 PyObject *x = PyInt_FromLong(a[i]);
4883 if (x == NULL) {
4884 Py_DECREF(l);
4885 return NULL;
4886 }
4887 PyList_SetItem(l, i, x);
4888 }
4889 for (i = 0; i < 256; i++)
4890 a[i] = 0;
4891 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004892}
4893
4894PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004895_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004896{
4897#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004898 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004899#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004900 int i;
4901 PyObject *l = PyList_New(257);
4902 if (l == NULL) return NULL;
4903 for (i = 0; i < 257; i++) {
4904 PyObject *x = getarray(dxpairs[i]);
4905 if (x == NULL) {
4906 Py_DECREF(l);
4907 return NULL;
4908 }
4909 PyList_SetItem(l, i, x);
4910 }
4911 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004912#endif
4913}
4914
4915#endif