blob: 18bc66b871cff2fbba408687172cf146d1459169 [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);
358 errno = err;
359 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000360#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000361 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000362}
363
364
Guido van Rossuma9672091994-09-14 13:31:22 +0000365/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
366 signal handlers or Mac I/O completion routines) can schedule calls
367 to a function to be called synchronously.
368 The synchronous function is called with one void* argument.
369 It should return 0 for success or -1 for failure -- failure should
370 be accompanied by an exception.
371
372 If registry succeeds, the registry function returns 0; if it fails
373 (e.g. due to too many pending calls) it returns -1 (without setting
374 an exception condition).
375
376 Note that because registry may occur from within signal handlers,
377 or other asynchronous events, calling malloc() is unsafe!
378
379#ifdef WITH_THREAD
380 Any thread can schedule pending calls, but only the main thread
381 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000382 There is no facility to schedule calls to a particular thread, but
383 that should be easy to change, should that ever be required. In
384 that case, the static variables here should go into the python
385 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000386#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000387*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000388
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000389#ifdef WITH_THREAD
390
391/* The WITH_THREAD implementation is thread-safe. It allows
392 scheduling to be made from any thread, and even from an executing
393 callback.
394 */
395
396#define NPENDINGCALLS 32
397static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000398 int (*func)(void *);
399 void *arg;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000400} pendingcalls[NPENDINGCALLS];
401static int pendingfirst = 0;
402static int pendinglast = 0;
403static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
404static char pendingbusy = 0;
405
406int
407Py_AddPendingCall(int (*func)(void *), void *arg)
408{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000409 int i, j, result=0;
410 PyThread_type_lock lock = pending_lock;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000411
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000412 /* try a few times for the lock. Since this mechanism is used
413 * for signal handling (on the main thread), there is a (slim)
414 * chance that a signal is delivered on the same thread while we
415 * hold the lock during the Py_MakePendingCalls() function.
416 * This avoids a deadlock in that case.
417 * Note that signals can be delivered on any thread. In particular,
418 * on Windows, a SIGINT is delivered on a system-created worker
419 * thread.
420 * We also check for lock being NULL, in the unlikely case that
421 * this function is called before any bytecode evaluation takes place.
422 */
423 if (lock != NULL) {
424 for (i = 0; i<100; i++) {
425 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
426 break;
427 }
428 if (i == 100)
429 return -1;
430 }
431
432 i = pendinglast;
433 j = (i + 1) % NPENDINGCALLS;
434 if (j == pendingfirst) {
435 result = -1; /* Queue full */
436 } else {
437 pendingcalls[i].func = func;
438 pendingcalls[i].arg = arg;
439 pendinglast = j;
440 }
441 /* signal main loop */
442 _Py_Ticker = 0;
443 pendingcalls_to_do = 1;
444 if (lock != NULL)
445 PyThread_release_lock(lock);
446 return result;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000447}
448
449int
450Py_MakePendingCalls(void)
451{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000452 int i;
453 int r = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000454
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000455 if (!pending_lock) {
456 /* initial allocation of the lock */
457 pending_lock = PyThread_allocate_lock();
458 if (pending_lock == NULL)
459 return -1;
460 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000461
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000462 /* only service pending calls on main thread */
463 if (main_thread && PyThread_get_thread_ident() != main_thread)
464 return 0;
465 /* don't perform recursive pending calls */
466 if (pendingbusy)
467 return 0;
468 pendingbusy = 1;
469 /* perform a bounded number of calls, in case of recursion */
470 for (i=0; i<NPENDINGCALLS; i++) {
471 int j;
472 int (*func)(void *);
473 void *arg = NULL;
474
475 /* pop one item off the queue while holding the lock */
476 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
477 j = pendingfirst;
478 if (j == pendinglast) {
479 func = NULL; /* Queue empty */
480 } else {
481 func = pendingcalls[j].func;
482 arg = pendingcalls[j].arg;
483 pendingfirst = (j + 1) % NPENDINGCALLS;
484 }
485 pendingcalls_to_do = pendingfirst != pendinglast;
486 PyThread_release_lock(pending_lock);
487 /* having released the lock, perform the callback */
488 if (func == NULL)
489 break;
490 r = func(arg);
491 if (r)
492 break;
493 }
494 pendingbusy = 0;
495 return r;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000496}
497
498#else /* if ! defined WITH_THREAD */
499
500/*
501 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
502 This code is used for signal handling in python that isn't built
503 with WITH_THREAD.
504 Don't use this implementation when Py_AddPendingCalls() can happen
505 on a different thread!
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000506
Guido van Rossuma9672091994-09-14 13:31:22 +0000507 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000508 (1) nested asynchronous calls to Py_AddPendingCall()
509 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000510
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000511 (1) is very unlikely because typically signal delivery
512 is blocked during signal handling. So it should be impossible.
513 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000514 The current code is safe against (2), but not against (1).
515 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000516 thread is present, interrupted by signals, and that the critical
517 section is protected with the "busy" variable. On Windows, which
518 delivers SIGINT on a system thread, this does not hold and therefore
519 Windows really shouldn't use this version.
520 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000521*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000522
Guido van Rossuma9672091994-09-14 13:31:22 +0000523#define NPENDINGCALLS 32
524static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000525 int (*func)(void *);
526 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000527} pendingcalls[NPENDINGCALLS];
528static volatile int pendingfirst = 0;
529static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000530static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000531
532int
Thomas Wouters334fb892000-07-25 12:56:38 +0000533Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000534{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000535 static volatile int busy = 0;
536 int i, j;
537 /* XXX Begin critical section */
538 if (busy)
539 return -1;
540 busy = 1;
541 i = pendinglast;
542 j = (i + 1) % NPENDINGCALLS;
543 if (j == pendingfirst) {
544 busy = 0;
545 return -1; /* Queue full */
546 }
547 pendingcalls[i].func = func;
548 pendingcalls[i].arg = arg;
549 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000550
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000551 _Py_Ticker = 0;
552 pendingcalls_to_do = 1; /* Signal main loop */
553 busy = 0;
554 /* XXX End critical section */
555 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000556}
557
Guido van Rossum180d7b41994-09-29 09:45:57 +0000558int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000559Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000560{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000561 static int busy = 0;
562 if (busy)
563 return 0;
564 busy = 1;
565 pendingcalls_to_do = 0;
566 for (;;) {
567 int i;
568 int (*func)(void *);
569 void *arg;
570 i = pendingfirst;
571 if (i == pendinglast)
572 break; /* Queue empty */
573 func = pendingcalls[i].func;
574 arg = pendingcalls[i].arg;
575 pendingfirst = (i + 1) % NPENDINGCALLS;
576 if (func(arg) < 0) {
577 busy = 0;
578 pendingcalls_to_do = 1; /* We're not done yet */
579 return -1;
580 }
581 }
582 busy = 0;
583 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000584}
585
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000586#endif /* WITH_THREAD */
587
Guido van Rossuma9672091994-09-14 13:31:22 +0000588
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000589/* The interpreter's recursion limit */
590
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000591#ifndef Py_DEFAULT_RECURSION_LIMIT
592#define Py_DEFAULT_RECURSION_LIMIT 1000
593#endif
594static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
595int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000596
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000597int
598Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000599{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000600 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000601}
602
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000603void
604Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000605{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 recursion_limit = new_limit;
607 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000608}
609
Armin Rigo2b3eb402003-10-28 12:05:48 +0000610/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
611 if the recursion_depth reaches _Py_CheckRecursionLimit.
612 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
613 to guarantee that _Py_CheckRecursiveCall() is regularly called.
614 Without USE_STACKCHECK, there is no need for this. */
615int
616_Py_CheckRecursiveCall(char *where)
617{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000618 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000619
620#ifdef USE_STACKCHECK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000621 if (PyOS_CheckStack()) {
622 --tstate->recursion_depth;
623 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
624 return -1;
625 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000626#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 if (tstate->recursion_depth > recursion_limit) {
628 --tstate->recursion_depth;
629 PyErr_Format(PyExc_RuntimeError,
630 "maximum recursion depth exceeded%s",
631 where);
632 return -1;
633 }
634 _Py_CheckRecursionLimit = recursion_limit;
635 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000636}
637
Guido van Rossum374a9221991-04-04 10:40:29 +0000638/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000639enum why_code {
Stefan Krah7ff78252010-06-23 18:12:09 +0000640 WHY_NOT = 0x0001, /* No error */
641 WHY_EXCEPTION = 0x0002, /* Exception occurred */
642 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
643 WHY_RETURN = 0x0008, /* 'return' statement */
644 WHY_BREAK = 0x0010, /* 'break' statement */
645 WHY_CONTINUE = 0x0020, /* 'continue' statement */
646 WHY_YIELD = 0x0040 /* 'yield' operator */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000647};
Guido van Rossum374a9221991-04-04 10:40:29 +0000648
Fredrik Lundh7a830892006-05-27 10:39:48 +0000649static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
650static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000651
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000652/* Records whether tracing is on for any thread. Counts the number of
653 threads for which tstate->c_tracefunc is non-NULL, so if the value
654 is 0, we know we don't have to check this thread's c_tracefunc.
655 This speeds up the if statement in PyEval_EvalFrameEx() after
656 fast_next_opcode*/
657static int _Py_TracingPossible = 0;
658
Skip Montanarod581d772002-09-03 20:10:45 +0000659/* for manipulating the thread switch and periodic "stuff" - used to be
660 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000661int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000662volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000663
Guido van Rossumb209a111997-04-29 18:18:01 +0000664PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000665PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000666{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000667 return PyEval_EvalCodeEx(co,
668 globals, locals,
669 (PyObject **)NULL, 0,
670 (PyObject **)NULL, 0,
671 (PyObject **)NULL, 0,
672 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000673}
674
675
676/* Interpreter main loop */
677
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000678PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000679PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000680 /* This is for backward compatibility with extension modules that
681 used this API; core interpreter code should call
682 PyEval_EvalFrameEx() */
683 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000684}
685
686PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000687PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000688{
Guido van Rossum950361c1997-01-24 13:49:28 +0000689#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000690 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000691#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000692 register PyObject **stack_pointer; /* Next free slot in value stack */
693 register unsigned char *next_instr;
694 register int opcode; /* Current opcode */
695 register int oparg; /* Current opcode argument, if any */
696 register enum why_code why; /* Reason for block stack unwind */
697 register int err; /* Error status -- nonzero if error */
698 register PyObject *x; /* Result object -- NULL if error */
699 register PyObject *v; /* Temporary objects popped off stack */
700 register PyObject *w;
701 register PyObject *u;
702 register PyObject *t;
703 register PyObject *stream = NULL; /* for PRINT opcodes */
704 register PyObject **fastlocals, **freevars;
705 PyObject *retval = NULL; /* Return value */
706 PyThreadState *tstate = PyThreadState_GET();
707 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000708
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000709 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000710
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000711 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000712
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000713 is true when the line being executed has changed. The
714 initial values are such as to make this false the first
715 time it is tested. */
716 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000717
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000718 unsigned char *first_instr;
719 PyObject *names;
720 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000721#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000722 /* Make it easier to find out where we are with a debugger */
723 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000724#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000725
Neal Norwitza81d2202002-07-14 00:27:26 +0000726/* Tuple access macros */
727
728#ifndef Py_DEBUG
729#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
730#else
731#define GETITEM(v, i) PyTuple_GetItem((v), (i))
732#endif
733
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000734#ifdef WITH_TSC
735/* Use Pentium timestamp counter to mark certain events:
736 inst0 -- beginning of switch statement for opcode dispatch
737 inst1 -- end of switch statement (may be skipped)
738 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000739 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000740 (may be skipped)
741 intr1 -- beginning of long interruption
742 intr2 -- end of long interruption
743
744 Many opcodes call out to helper C functions. In some cases, the
745 time in those functions should be counted towards the time for the
746 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
747 calls another Python function; there's no point in charge all the
748 bytecode executed by the called function to the caller.
749
750 It's hard to make a useful judgement statically. In the presence
751 of operator overloading, it's impossible to tell if a call will
752 execute new Python code or not.
753
754 It's a case-by-case judgement. I'll use intr1 for the following
755 cases:
756
757 EXEC_STMT
758 IMPORT_STAR
759 IMPORT_FROM
760 CALL_FUNCTION (and friends)
761
762 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000763 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
764 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000765
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000766 READ_TIMESTAMP(inst0);
767 READ_TIMESTAMP(inst1);
768 READ_TIMESTAMP(loop0);
769 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000770
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000771 /* shut up the compiler */
772 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000773#endif
774
Guido van Rossum374a9221991-04-04 10:40:29 +0000775/* Code access macros */
776
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000777#define INSTR_OFFSET() ((int)(next_instr - first_instr))
778#define NEXTOP() (*next_instr++)
779#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
780#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
781#define JUMPTO(x) (next_instr = first_instr + (x))
782#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000783
Raymond Hettingerf606f872003-03-16 03:11:04 +0000784/* OpCode prediction macros
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000785 Some opcodes tend to come in pairs thus making it possible to
786 predict the second code when the first is run. For example,
787 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
788 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000789
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000790 Verifying the prediction costs a single high-speed test of a register
791 variable against a constant. If the pairing was good, then the
792 processor's own internal branch predication has a high likelihood of
793 success, resulting in a nearly zero-overhead transition to the
794 next opcode. A successful prediction saves a trip through the eval-loop
795 including its two unpredictable branches, the HAS_ARG test and the
796 switch-case. Combined with the processor's internal branch prediction,
797 a successful PREDICT has the effect of making the two opcodes run as if
798 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000799
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000800 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000801 predictions turned-on and interpret the results as if some opcodes
802 had been combined or turn-off predictions so that the opcode frequency
803 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000804*/
805
Raymond Hettingera7216982004-02-08 19:59:27 +0000806#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000807#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000808#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000809#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000810#endif
811
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000812#define PREDICTED(op) PRED_##op: next_instr++
813#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000814
Guido van Rossum374a9221991-04-04 10:40:29 +0000815/* Stack manipulation macros */
816
Martin v. Löwis18e16552006-02-15 17:27:45 +0000817/* The stack can grow at most MAXINT deep, as co_nlocals and
818 co_stacksize are ints. */
Stefan Krah7ff78252010-06-23 18:12:09 +0000819#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
820#define EMPTY() (STACK_LEVEL() == 0)
821#define TOP() (stack_pointer[-1])
822#define SECOND() (stack_pointer[-2])
823#define THIRD() (stack_pointer[-3])
824#define FOURTH() (stack_pointer[-4])
825#define PEEK(n) (stack_pointer[-(n)])
826#define SET_TOP(v) (stack_pointer[-1] = (v))
827#define SET_SECOND(v) (stack_pointer[-2] = (v))
828#define SET_THIRD(v) (stack_pointer[-3] = (v))
829#define SET_FOURTH(v) (stack_pointer[-4] = (v))
830#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
831#define BASIC_STACKADJ(n) (stack_pointer += n)
832#define BASIC_PUSH(v) (*stack_pointer++ = (v))
833#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000834
Guido van Rossum96a42c81992-01-12 02:29:51 +0000835#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000836#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000837 lltrace && prtrace(TOP(), "push")); \
838 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000839#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000840 BASIC_POP())
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000841#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000842 lltrace && prtrace(TOP(), "stackadj")); \
843 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000844#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krah7ff78252010-06-23 18:12:09 +0000845 prtrace((STACK_POINTER)[-1], "ext_pop")), \
846 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000847#else
Stefan Krah7ff78252010-06-23 18:12:09 +0000848#define PUSH(v) BASIC_PUSH(v)
849#define POP() BASIC_POP()
850#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000851#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000852#endif
853
Guido van Rossum681d79a1995-07-18 14:51:37 +0000854/* Local variable macros */
855
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000856#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000857
858/* The SETLOCAL() macro must not DECREF the local variable in-place and
859 then store the new value; it must copy the old value to a temporary
860 value, then store the new value, and then DECREF the temporary value.
861 This is because it is possible that during the DECREF the frame is
862 accessed by other code (e.g. a __del__ method or gc.collect()) and the
863 variable would be pointing to already-freed memory. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000864#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krah7ff78252010-06-23 18:12:09 +0000865 GETLOCAL(i) = value; \
866 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000867
Guido van Rossuma027efa1997-05-05 20:56:21 +0000868/* Start of code */
869
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000870 if (f == NULL)
871 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000872
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873 /* push frame */
874 if (Py_EnterRecursiveCall(""))
875 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000876
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000877 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000878
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 if (tstate->use_tracing) {
880 if (tstate->c_tracefunc != NULL) {
881 /* tstate->c_tracefunc, if defined, is a
882 function that will be called on *every* entry
883 to a code block. Its return value, if not
884 None, is a function that will be called at
885 the start of each executed line of code.
886 (Actually, the function must return itself
887 in order to continue tracing.) The trace
888 functions are called with three arguments:
889 a pointer to the current frame, a string
890 indicating why the function is called, and
891 an argument which depends on the situation.
892 The global trace function is also called
893 whenever an exception is detected. */
894 if (call_trace_protected(tstate->c_tracefunc,
895 tstate->c_traceobj,
896 f, PyTrace_CALL, Py_None)) {
897 /* Trace function raised an error */
898 goto exit_eval_frame;
899 }
900 }
901 if (tstate->c_profilefunc != NULL) {
902 /* Similar for c_profilefunc, except it needn't
903 return itself and isn't called for "line" events */
904 if (call_trace_protected(tstate->c_profilefunc,
905 tstate->c_profileobj,
906 f, PyTrace_CALL, Py_None)) {
907 /* Profile function raised an error */
908 goto exit_eval_frame;
909 }
910 }
911 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000912
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000913 co = f->f_code;
914 names = co->co_names;
915 consts = co->co_consts;
916 fastlocals = f->f_localsplus;
917 freevars = f->f_localsplus + co->co_nlocals;
918 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
919 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000920
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000921 f->f_lasti now refers to the index of the last instruction
922 executed. You might think this was obvious from the name, but
923 this wasn't always true before 2.3! PyFrame_New now sets
924 f->f_lasti to -1 (i.e. the index *before* the first instruction)
925 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
926 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000927
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000928 When the PREDICT() macros are enabled, some opcode pairs follow in
929 direct succession without updating f->f_lasti. A successful
930 prediction effectively links the two codes together as if they
931 were a single new opcode; accordingly,f->f_lasti will point to
932 the first code in the pair (for instance, GET_ITER followed by
933 FOR_ITER is effectively a single opcode and f->f_lasti will point
934 at to the beginning of the combined pair.)
935 */
936 next_instr = first_instr + f->f_lasti + 1;
937 stack_pointer = f->f_stacktop;
938 assert(stack_pointer != NULL);
939 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000940
Tim Peters5ca576e2001-06-18 22:08:13 +0000941#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000942 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000943#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000944#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000945 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000946#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000947
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 why = WHY_NOT;
949 err = 0;
950 x = Py_None; /* Not a reference, just anything non-NULL */
951 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000953 if (throwflag) { /* support for generator.throw() */
954 why = WHY_EXCEPTION;
955 goto on_error;
956 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000957
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000958 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000959#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000960 if (inst1 == 0) {
961 /* Almost surely, the opcode executed a break
962 or a continue, preventing inst1 from being set
963 on the way out of the loop.
964 */
965 READ_TIMESTAMP(inst1);
966 loop1 = inst1;
967 }
968 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
969 intr0, intr1);
970 ticked = 0;
971 inst1 = 0;
972 intr0 = 0;
973 intr1 = 0;
974 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000975#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000976 assert(stack_pointer >= f->f_valuestack); /* else underflow */
977 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000978
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000979 /* Do periodic things. Doing this every time through
980 the loop would add too much overhead, so we do it
981 only every Nth instruction. We also do it if
982 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
983 event needs attention (e.g. a signal handler or
984 async I/O handler); see Py_AddPendingCall() and
985 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000986
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000987 if (--_Py_Ticker < 0) {
988 if (*next_instr == SETUP_FINALLY) {
989 /* Make the last opcode before
Ezio Melottic2077b02011-03-16 12:34:31 +0200990 a try: finally: block uninterruptible. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000991 goto fast_next_opcode;
992 }
993 _Py_Ticker = _Py_CheckInterval;
994 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000995#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000996 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000997#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000998 if (pendingcalls_to_do) {
999 if (Py_MakePendingCalls() < 0) {
1000 why = WHY_EXCEPTION;
1001 goto on_error;
1002 }
1003 if (pendingcalls_to_do)
1004 /* MakePendingCalls() didn't succeed.
1005 Force early re-execution of this
1006 "periodic" code, possibly after
1007 a thread switch */
1008 _Py_Ticker = 0;
1009 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001010#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001011 if (interpreter_lock) {
1012 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001013
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001014 if (PyThreadState_Swap(NULL) != tstate)
1015 Py_FatalError("ceval: tstate mix-up");
1016 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001017
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001018 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 PyThread_acquire_lock(interpreter_lock, 1);
1021 if (PyThreadState_Swap(tstate) != NULL)
1022 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001023
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 if (tstate->async_exc != NULL) {
1027 x = tstate->async_exc;
1028 tstate->async_exc = NULL;
1029 PyErr_SetNone(x);
1030 Py_DECREF(x);
1031 why = WHY_EXCEPTION;
1032 goto on_error;
1033 }
1034 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001035#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001036 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001037
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001038 fast_next_opcode:
1039 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001040
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001041 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001043 if (_Py_TracingPossible &&
1044 tstate->c_tracefunc != NULL && !tstate->tracing) {
1045 /* see maybe_call_line_trace
1046 for expository comments */
1047 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001048
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 err = maybe_call_line_trace(tstate->c_tracefunc,
1050 tstate->c_traceobj,
1051 f, &instr_lb, &instr_ub,
1052 &instr_prev);
1053 /* Reload possibly changed frame fields */
1054 JUMPTO(f->f_lasti);
1055 if (f->f_stacktop != NULL) {
1056 stack_pointer = f->f_stacktop;
1057 f->f_stacktop = NULL;
1058 }
1059 if (err) {
1060 /* trace function raised an exception */
1061 goto on_error;
1062 }
1063 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001065 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001066
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001067 opcode = NEXTOP();
1068 oparg = 0; /* allows oparg to be stored in a register because
1069 it doesn't have to be remembered across a full loop */
1070 if (HAS_ARG(opcode))
1071 oparg = NEXTARG();
Stefan Krah7ff78252010-06-23 18:12:09 +00001072 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001073#ifdef DYNAMIC_EXECUTION_PROFILE
1074#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001075 dxpairs[lastopcode][opcode]++;
1076 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001077#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001078 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001079#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001080
Guido van Rossum96a42c81992-01-12 02:29:51 +00001081#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001082 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001083
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001084 if (lltrace) {
1085 if (HAS_ARG(opcode)) {
1086 printf("%d: %d, %d\n",
1087 f->f_lasti, opcode, oparg);
1088 }
1089 else {
1090 printf("%d: %d\n",
1091 f->f_lasti, opcode);
1092 }
1093 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001094#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001095
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001096 /* Main switch on opcode */
1097 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001098
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001099 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001100
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001101 /* BEWARE!
1102 It is essential that any operation that fails sets either
1103 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1104 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001105
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001106 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001107
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001108 case NOP:
1109 goto fast_next_opcode;
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001110
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001111 case LOAD_FAST:
1112 x = GETLOCAL(oparg);
1113 if (x != NULL) {
1114 Py_INCREF(x);
1115 PUSH(x);
1116 goto fast_next_opcode;
1117 }
1118 format_exc_check_arg(PyExc_UnboundLocalError,
1119 UNBOUNDLOCAL_ERROR_MSG,
1120 PyTuple_GetItem(co->co_varnames, oparg));
1121 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001123 case LOAD_CONST:
1124 x = GETITEM(consts, oparg);
1125 Py_INCREF(x);
1126 PUSH(x);
1127 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001128
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001129 PREDICTED_WITH_ARG(STORE_FAST);
1130 case STORE_FAST:
1131 v = POP();
1132 SETLOCAL(oparg, v);
1133 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001134
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001135 case POP_TOP:
1136 v = POP();
1137 Py_DECREF(v);
1138 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001139
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001140 case ROT_TWO:
1141 v = TOP();
1142 w = SECOND();
1143 SET_TOP(w);
1144 SET_SECOND(v);
1145 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001147 case ROT_THREE:
1148 v = TOP();
1149 w = SECOND();
1150 x = THIRD();
1151 SET_TOP(w);
1152 SET_SECOND(x);
1153 SET_THIRD(v);
1154 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001155
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001156 case ROT_FOUR:
1157 u = TOP();
1158 v = SECOND();
1159 w = THIRD();
1160 x = FOURTH();
1161 SET_TOP(v);
1162 SET_SECOND(w);
1163 SET_THIRD(x);
1164 SET_FOURTH(u);
1165 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001167 case DUP_TOP:
1168 v = TOP();
1169 Py_INCREF(v);
1170 PUSH(v);
1171 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001172
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001173 case DUP_TOPX:
1174 if (oparg == 2) {
1175 x = TOP();
1176 Py_INCREF(x);
1177 w = SECOND();
1178 Py_INCREF(w);
1179 STACKADJ(2);
1180 SET_TOP(x);
1181 SET_SECOND(w);
1182 goto fast_next_opcode;
1183 } else if (oparg == 3) {
1184 x = TOP();
1185 Py_INCREF(x);
1186 w = SECOND();
1187 Py_INCREF(w);
1188 v = THIRD();
1189 Py_INCREF(v);
1190 STACKADJ(3);
1191 SET_TOP(x);
1192 SET_SECOND(w);
1193 SET_THIRD(v);
1194 goto fast_next_opcode;
1195 }
1196 Py_FatalError("invalid argument to DUP_TOPX"
1197 " (bytecode corruption?)");
1198 /* Never returns, so don't bother to set why. */
1199 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001201 case UNARY_POSITIVE:
1202 v = TOP();
1203 x = PyNumber_Positive(v);
1204 Py_DECREF(v);
1205 SET_TOP(x);
1206 if (x != NULL) continue;
1207 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001209 case UNARY_NEGATIVE:
1210 v = TOP();
1211 x = PyNumber_Negative(v);
1212 Py_DECREF(v);
1213 SET_TOP(x);
1214 if (x != NULL) continue;
1215 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001216
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001217 case UNARY_NOT:
1218 v = TOP();
1219 err = PyObject_IsTrue(v);
1220 Py_DECREF(v);
1221 if (err == 0) {
1222 Py_INCREF(Py_True);
1223 SET_TOP(Py_True);
1224 continue;
1225 }
1226 else if (err > 0) {
1227 Py_INCREF(Py_False);
1228 SET_TOP(Py_False);
1229 err = 0;
1230 continue;
1231 }
1232 STACKADJ(-1);
1233 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001234
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001235 case UNARY_CONVERT:
1236 v = TOP();
1237 x = PyObject_Repr(v);
1238 Py_DECREF(v);
1239 SET_TOP(x);
1240 if (x != NULL) continue;
1241 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001243 case UNARY_INVERT:
1244 v = TOP();
1245 x = PyNumber_Invert(v);
1246 Py_DECREF(v);
1247 SET_TOP(x);
1248 if (x != NULL) continue;
1249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001250
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001251 case BINARY_POWER:
1252 w = POP();
1253 v = TOP();
1254 x = PyNumber_Power(v, w, Py_None);
1255 Py_DECREF(v);
1256 Py_DECREF(w);
1257 SET_TOP(x);
1258 if (x != NULL) continue;
1259 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001260
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001261 case BINARY_MULTIPLY:
1262 w = POP();
1263 v = TOP();
1264 x = PyNumber_Multiply(v, w);
1265 Py_DECREF(v);
1266 Py_DECREF(w);
1267 SET_TOP(x);
1268 if (x != NULL) continue;
1269 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001270
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001271 case BINARY_DIVIDE:
1272 if (!_Py_QnewFlag) {
1273 w = POP();
1274 v = TOP();
1275 x = PyNumber_Divide(v, w);
1276 Py_DECREF(v);
1277 Py_DECREF(w);
1278 SET_TOP(x);
1279 if (x != NULL) continue;
1280 break;
1281 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001282 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001283 BINARY_TRUE_DIVIDE */
1284 case BINARY_TRUE_DIVIDE:
1285 w = POP();
1286 v = TOP();
1287 x = PyNumber_TrueDivide(v, w);
1288 Py_DECREF(v);
1289 Py_DECREF(w);
1290 SET_TOP(x);
1291 if (x != NULL) continue;
1292 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001293
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001294 case BINARY_FLOOR_DIVIDE:
1295 w = POP();
1296 v = TOP();
1297 x = PyNumber_FloorDivide(v, w);
1298 Py_DECREF(v);
1299 Py_DECREF(w);
1300 SET_TOP(x);
1301 if (x != NULL) continue;
1302 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001303
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001304 case BINARY_MODULO:
1305 w = POP();
1306 v = TOP();
1307 if (PyString_CheckExact(v))
1308 x = PyString_Format(v, w);
1309 else
1310 x = PyNumber_Remainder(v, w);
1311 Py_DECREF(v);
1312 Py_DECREF(w);
1313 SET_TOP(x);
1314 if (x != NULL) continue;
1315 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001316
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001317 case BINARY_ADD:
1318 w = POP();
1319 v = TOP();
1320 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1321 /* INLINE: int + int */
1322 register long a, b, i;
1323 a = PyInt_AS_LONG(v);
1324 b = PyInt_AS_LONG(w);
1325 /* cast to avoid undefined behaviour
1326 on overflow */
1327 i = (long)((unsigned long)a + b);
1328 if ((i^a) < 0 && (i^b) < 0)
1329 goto slow_add;
1330 x = PyInt_FromLong(i);
1331 }
1332 else if (PyString_CheckExact(v) &&
1333 PyString_CheckExact(w)) {
1334 x = string_concatenate(v, w, f, next_instr);
1335 /* string_concatenate consumed the ref to v */
1336 goto skip_decref_vx;
1337 }
1338 else {
1339 slow_add:
1340 x = PyNumber_Add(v, w);
1341 }
1342 Py_DECREF(v);
1343 skip_decref_vx:
1344 Py_DECREF(w);
1345 SET_TOP(x);
1346 if (x != NULL) continue;
1347 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001349 case BINARY_SUBTRACT:
1350 w = POP();
1351 v = TOP();
1352 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1353 /* INLINE: int - int */
1354 register long a, b, i;
1355 a = PyInt_AS_LONG(v);
1356 b = PyInt_AS_LONG(w);
1357 /* cast to avoid undefined behaviour
1358 on overflow */
1359 i = (long)((unsigned long)a - b);
1360 if ((i^a) < 0 && (i^~b) < 0)
1361 goto slow_sub;
1362 x = PyInt_FromLong(i);
1363 }
1364 else {
1365 slow_sub:
1366 x = PyNumber_Subtract(v, w);
1367 }
1368 Py_DECREF(v);
1369 Py_DECREF(w);
1370 SET_TOP(x);
1371 if (x != NULL) continue;
1372 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001373
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001374 case BINARY_SUBSCR:
1375 w = POP();
1376 v = TOP();
1377 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1378 /* INLINE: list[int] */
1379 Py_ssize_t i = PyInt_AsSsize_t(w);
1380 if (i < 0)
1381 i += PyList_GET_SIZE(v);
1382 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1383 x = PyList_GET_ITEM(v, i);
1384 Py_INCREF(x);
1385 }
1386 else
1387 goto slow_get;
1388 }
1389 else
1390 slow_get:
1391 x = PyObject_GetItem(v, w);
1392 Py_DECREF(v);
1393 Py_DECREF(w);
1394 SET_TOP(x);
1395 if (x != NULL) continue;
1396 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001397
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001398 case BINARY_LSHIFT:
1399 w = POP();
1400 v = TOP();
1401 x = PyNumber_Lshift(v, w);
1402 Py_DECREF(v);
1403 Py_DECREF(w);
1404 SET_TOP(x);
1405 if (x != NULL) continue;
1406 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001407
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001408 case BINARY_RSHIFT:
1409 w = POP();
1410 v = TOP();
1411 x = PyNumber_Rshift(v, w);
1412 Py_DECREF(v);
1413 Py_DECREF(w);
1414 SET_TOP(x);
1415 if (x != NULL) continue;
1416 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001417
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001418 case BINARY_AND:
1419 w = POP();
1420 v = TOP();
1421 x = PyNumber_And(v, w);
1422 Py_DECREF(v);
1423 Py_DECREF(w);
1424 SET_TOP(x);
1425 if (x != NULL) continue;
1426 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001427
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001428 case BINARY_XOR:
1429 w = POP();
1430 v = TOP();
1431 x = PyNumber_Xor(v, w);
1432 Py_DECREF(v);
1433 Py_DECREF(w);
1434 SET_TOP(x);
1435 if (x != NULL) continue;
1436 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001437
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001438 case BINARY_OR:
1439 w = POP();
1440 v = TOP();
1441 x = PyNumber_Or(v, w);
1442 Py_DECREF(v);
1443 Py_DECREF(w);
1444 SET_TOP(x);
1445 if (x != NULL) continue;
1446 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001447
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001448 case LIST_APPEND:
1449 w = POP();
1450 v = PEEK(oparg);
1451 err = PyList_Append(v, w);
1452 Py_DECREF(w);
1453 if (err == 0) {
1454 PREDICT(JUMP_ABSOLUTE);
1455 continue;
1456 }
1457 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001458
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001459 case SET_ADD:
1460 w = POP();
1461 v = stack_pointer[-oparg];
1462 err = PySet_Add(v, w);
1463 Py_DECREF(w);
1464 if (err == 0) {
1465 PREDICT(JUMP_ABSOLUTE);
1466 continue;
1467 }
1468 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001469
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001470 case INPLACE_POWER:
1471 w = POP();
1472 v = TOP();
1473 x = PyNumber_InPlacePower(v, w, Py_None);
1474 Py_DECREF(v);
1475 Py_DECREF(w);
1476 SET_TOP(x);
1477 if (x != NULL) continue;
1478 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001479
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001480 case INPLACE_MULTIPLY:
1481 w = POP();
1482 v = TOP();
1483 x = PyNumber_InPlaceMultiply(v, w);
1484 Py_DECREF(v);
1485 Py_DECREF(w);
1486 SET_TOP(x);
1487 if (x != NULL) continue;
1488 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001489
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001490 case INPLACE_DIVIDE:
1491 if (!_Py_QnewFlag) {
1492 w = POP();
1493 v = TOP();
1494 x = PyNumber_InPlaceDivide(v, w);
1495 Py_DECREF(v);
1496 Py_DECREF(w);
1497 SET_TOP(x);
1498 if (x != NULL) continue;
1499 break;
1500 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001501 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001502 INPLACE_TRUE_DIVIDE */
1503 case INPLACE_TRUE_DIVIDE:
1504 w = POP();
1505 v = TOP();
1506 x = PyNumber_InPlaceTrueDivide(v, w);
1507 Py_DECREF(v);
1508 Py_DECREF(w);
1509 SET_TOP(x);
1510 if (x != NULL) continue;
1511 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001512
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001513 case INPLACE_FLOOR_DIVIDE:
1514 w = POP();
1515 v = TOP();
1516 x = PyNumber_InPlaceFloorDivide(v, w);
1517 Py_DECREF(v);
1518 Py_DECREF(w);
1519 SET_TOP(x);
1520 if (x != NULL) continue;
1521 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001522
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001523 case INPLACE_MODULO:
1524 w = POP();
1525 v = TOP();
1526 x = PyNumber_InPlaceRemainder(v, w);
1527 Py_DECREF(v);
1528 Py_DECREF(w);
1529 SET_TOP(x);
1530 if (x != NULL) continue;
1531 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001533 case INPLACE_ADD:
1534 w = POP();
1535 v = TOP();
1536 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1537 /* INLINE: int + int */
1538 register long a, b, i;
1539 a = PyInt_AS_LONG(v);
1540 b = PyInt_AS_LONG(w);
1541 i = a + b;
1542 if ((i^a) < 0 && (i^b) < 0)
1543 goto slow_iadd;
1544 x = PyInt_FromLong(i);
1545 }
1546 else if (PyString_CheckExact(v) &&
1547 PyString_CheckExact(w)) {
1548 x = string_concatenate(v, w, f, next_instr);
1549 /* string_concatenate consumed the ref to v */
1550 goto skip_decref_v;
1551 }
1552 else {
1553 slow_iadd:
1554 x = PyNumber_InPlaceAdd(v, w);
1555 }
1556 Py_DECREF(v);
1557 skip_decref_v:
1558 Py_DECREF(w);
1559 SET_TOP(x);
1560 if (x != NULL) continue;
1561 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001562
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001563 case INPLACE_SUBTRACT:
1564 w = POP();
1565 v = TOP();
1566 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1567 /* INLINE: int - int */
1568 register long a, b, i;
1569 a = PyInt_AS_LONG(v);
1570 b = PyInt_AS_LONG(w);
1571 i = a - b;
1572 if ((i^a) < 0 && (i^~b) < 0)
1573 goto slow_isub;
1574 x = PyInt_FromLong(i);
1575 }
1576 else {
1577 slow_isub:
1578 x = PyNumber_InPlaceSubtract(v, w);
1579 }
1580 Py_DECREF(v);
1581 Py_DECREF(w);
1582 SET_TOP(x);
1583 if (x != NULL) continue;
1584 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001586 case INPLACE_LSHIFT:
1587 w = POP();
1588 v = TOP();
1589 x = PyNumber_InPlaceLshift(v, w);
1590 Py_DECREF(v);
1591 Py_DECREF(w);
1592 SET_TOP(x);
1593 if (x != NULL) continue;
1594 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001595
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001596 case INPLACE_RSHIFT:
1597 w = POP();
1598 v = TOP();
1599 x = PyNumber_InPlaceRshift(v, w);
1600 Py_DECREF(v);
1601 Py_DECREF(w);
1602 SET_TOP(x);
1603 if (x != NULL) continue;
1604 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001605
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001606 case INPLACE_AND:
1607 w = POP();
1608 v = TOP();
1609 x = PyNumber_InPlaceAnd(v, w);
1610 Py_DECREF(v);
1611 Py_DECREF(w);
1612 SET_TOP(x);
1613 if (x != NULL) continue;
1614 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001615
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001616 case INPLACE_XOR:
1617 w = POP();
1618 v = TOP();
1619 x = PyNumber_InPlaceXor(v, w);
1620 Py_DECREF(v);
1621 Py_DECREF(w);
1622 SET_TOP(x);
1623 if (x != NULL) continue;
1624 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001625
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001626 case INPLACE_OR:
1627 w = POP();
1628 v = TOP();
1629 x = PyNumber_InPlaceOr(v, w);
1630 Py_DECREF(v);
1631 Py_DECREF(w);
1632 SET_TOP(x);
1633 if (x != NULL) continue;
1634 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001635
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001636 case SLICE+0:
1637 case SLICE+1:
1638 case SLICE+2:
1639 case SLICE+3:
1640 if ((opcode-SLICE) & 2)
1641 w = POP();
1642 else
1643 w = NULL;
1644 if ((opcode-SLICE) & 1)
1645 v = POP();
1646 else
1647 v = NULL;
1648 u = TOP();
1649 x = apply_slice(u, v, w);
1650 Py_DECREF(u);
1651 Py_XDECREF(v);
1652 Py_XDECREF(w);
1653 SET_TOP(x);
1654 if (x != NULL) continue;
1655 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001657 case STORE_SLICE+0:
1658 case STORE_SLICE+1:
1659 case STORE_SLICE+2:
1660 case STORE_SLICE+3:
1661 if ((opcode-STORE_SLICE) & 2)
1662 w = POP();
1663 else
1664 w = NULL;
1665 if ((opcode-STORE_SLICE) & 1)
1666 v = POP();
1667 else
1668 v = NULL;
1669 u = POP();
1670 t = POP();
1671 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1672 Py_DECREF(t);
1673 Py_DECREF(u);
1674 Py_XDECREF(v);
1675 Py_XDECREF(w);
1676 if (err == 0) continue;
1677 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001678
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001679 case DELETE_SLICE+0:
1680 case DELETE_SLICE+1:
1681 case DELETE_SLICE+2:
1682 case DELETE_SLICE+3:
1683 if ((opcode-DELETE_SLICE) & 2)
1684 w = POP();
1685 else
1686 w = NULL;
1687 if ((opcode-DELETE_SLICE) & 1)
1688 v = POP();
1689 else
1690 v = NULL;
1691 u = POP();
1692 err = assign_slice(u, v, w, (PyObject *)NULL);
1693 /* del u[v:w] */
1694 Py_DECREF(u);
1695 Py_XDECREF(v);
1696 Py_XDECREF(w);
1697 if (err == 0) continue;
1698 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001699
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001700 case STORE_SUBSCR:
1701 w = TOP();
1702 v = SECOND();
1703 u = THIRD();
1704 STACKADJ(-3);
1705 /* v[w] = u */
1706 err = PyObject_SetItem(v, w, u);
1707 Py_DECREF(u);
1708 Py_DECREF(v);
1709 Py_DECREF(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 DELETE_SUBSCR:
1714 w = TOP();
1715 v = SECOND();
1716 STACKADJ(-2);
1717 /* del v[w] */
1718 err = PyObject_DelItem(v, w);
1719 Py_DECREF(v);
1720 Py_DECREF(w);
1721 if (err == 0) continue;
1722 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001723
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001724 case PRINT_EXPR:
1725 v = POP();
1726 w = PySys_GetObject("displayhook");
1727 if (w == NULL) {
1728 PyErr_SetString(PyExc_RuntimeError,
1729 "lost sys.displayhook");
1730 err = -1;
1731 x = NULL;
1732 }
1733 if (err == 0) {
1734 x = PyTuple_Pack(1, v);
1735 if (x == NULL)
1736 err = -1;
1737 }
1738 if (err == 0) {
1739 w = PyEval_CallObject(w, x);
1740 Py_XDECREF(w);
1741 if (w == NULL)
1742 err = -1;
1743 }
1744 Py_DECREF(v);
1745 Py_XDECREF(x);
1746 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001747
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001748 case PRINT_ITEM_TO:
1749 w = stream = POP();
1750 /* fall through to PRINT_ITEM */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001752 case PRINT_ITEM:
1753 v = POP();
1754 if (stream == NULL || stream == Py_None) {
1755 w = PySys_GetObject("stdout");
1756 if (w == NULL) {
1757 PyErr_SetString(PyExc_RuntimeError,
1758 "lost sys.stdout");
1759 err = -1;
1760 }
1761 }
1762 /* PyFile_SoftSpace() can exececute arbitrary code
1763 if sys.stdout is an instance with a __getattr__.
1764 If __getattr__ raises an exception, w will
1765 be freed, so we need to prevent that temporarily. */
1766 Py_XINCREF(w);
1767 if (w != NULL && PyFile_SoftSpace(w, 0))
1768 err = PyFile_WriteString(" ", w);
1769 if (err == 0)
1770 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1771 if (err == 0) {
1772 /* XXX move into writeobject() ? */
1773 if (PyString_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001774 char *s = PyString_AS_STRING(v);
1775 Py_ssize_t len = PyString_GET_SIZE(v);
1776 if (len == 0 ||
1777 !isspace(Py_CHARMASK(s[len-1])) ||
1778 s[len-1] == ' ')
1779 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001780 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001781#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001782 else if (PyUnicode_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001783 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1784 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1785 if (len == 0 ||
1786 !Py_UNICODE_ISSPACE(s[len-1]) ||
1787 s[len-1] == ' ')
1788 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001789 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001790#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001791 else
Stefan Krah7ff78252010-06-23 18:12:09 +00001792 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001793 }
1794 Py_XDECREF(w);
1795 Py_DECREF(v);
1796 Py_XDECREF(stream);
1797 stream = NULL;
1798 if (err == 0)
1799 continue;
1800 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001802 case PRINT_NEWLINE_TO:
1803 w = stream = POP();
1804 /* fall through to PRINT_NEWLINE */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001805
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001806 case PRINT_NEWLINE:
1807 if (stream == NULL || stream == Py_None) {
1808 w = PySys_GetObject("stdout");
1809 if (w == NULL) {
1810 PyErr_SetString(PyExc_RuntimeError,
1811 "lost sys.stdout");
1812 why = WHY_EXCEPTION;
1813 }
1814 }
1815 if (w != NULL) {
1816 /* w.write() may replace sys.stdout, so we
1817 * have to keep our reference to it */
1818 Py_INCREF(w);
1819 err = PyFile_WriteString("\n", w);
1820 if (err == 0)
1821 PyFile_SoftSpace(w, 0);
1822 Py_DECREF(w);
1823 }
1824 Py_XDECREF(stream);
1825 stream = NULL;
1826 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001827
Thomas Wouters434d0822000-08-24 20:11:32 +00001828
1829#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001830 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001831#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001832 case RAISE_VARARGS:
1833 u = v = w = NULL;
1834 switch (oparg) {
1835 case 3:
1836 u = POP(); /* traceback */
1837 /* Fallthrough */
1838 case 2:
1839 v = POP(); /* value */
1840 /* Fallthrough */
1841 case 1:
1842 w = POP(); /* exc */
1843 case 0: /* Fallthrough */
1844 why = do_raise(w, v, u);
1845 break;
1846 default:
1847 PyErr_SetString(PyExc_SystemError,
1848 "bad RAISE_VARARGS oparg");
1849 why = WHY_EXCEPTION;
1850 break;
1851 }
1852 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001854 case LOAD_LOCALS:
1855 if ((x = f->f_locals) != NULL) {
1856 Py_INCREF(x);
1857 PUSH(x);
1858 continue;
1859 }
1860 PyErr_SetString(PyExc_SystemError, "no locals");
1861 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001862
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001863 case RETURN_VALUE:
1864 retval = POP();
1865 why = WHY_RETURN;
1866 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001867
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001868 case YIELD_VALUE:
1869 retval = POP();
1870 f->f_stacktop = stack_pointer;
1871 why = WHY_YIELD;
1872 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001874 case EXEC_STMT:
1875 w = TOP();
1876 v = SECOND();
1877 u = THIRD();
1878 STACKADJ(-3);
1879 READ_TIMESTAMP(intr0);
1880 err = exec_statement(f, u, v, w);
1881 READ_TIMESTAMP(intr1);
1882 Py_DECREF(u);
1883 Py_DECREF(v);
1884 Py_DECREF(w);
1885 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 case POP_BLOCK:
1888 {
1889 PyTryBlock *b = PyFrame_BlockPop(f);
1890 while (STACK_LEVEL() > b->b_level) {
1891 v = POP();
1892 Py_DECREF(v);
1893 }
1894 }
1895 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001896
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001897 PREDICTED(END_FINALLY);
1898 case END_FINALLY:
1899 v = POP();
1900 if (PyInt_Check(v)) {
1901 why = (enum why_code) PyInt_AS_LONG(v);
1902 assert(why != WHY_YIELD);
1903 if (why == WHY_RETURN ||
1904 why == WHY_CONTINUE)
1905 retval = POP();
1906 }
1907 else if (PyExceptionClass_Check(v) ||
1908 PyString_Check(v)) {
1909 w = POP();
1910 u = POP();
1911 PyErr_Restore(v, w, u);
1912 why = WHY_RERAISE;
1913 break;
1914 }
1915 else if (v != Py_None) {
1916 PyErr_SetString(PyExc_SystemError,
1917 "'finally' pops bad exception");
1918 why = WHY_EXCEPTION;
1919 }
1920 Py_DECREF(v);
1921 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001923 case BUILD_CLASS:
1924 u = TOP();
1925 v = SECOND();
1926 w = THIRD();
1927 STACKADJ(-2);
1928 x = build_class(u, v, w);
1929 SET_TOP(x);
1930 Py_DECREF(u);
1931 Py_DECREF(v);
1932 Py_DECREF(w);
1933 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001935 case STORE_NAME:
1936 w = GETITEM(names, oparg);
1937 v = POP();
1938 if ((x = f->f_locals) != NULL) {
1939 if (PyDict_CheckExact(x))
1940 err = PyDict_SetItem(x, w, v);
1941 else
1942 err = PyObject_SetItem(x, w, v);
1943 Py_DECREF(v);
1944 if (err == 0) continue;
1945 break;
1946 }
1947 PyErr_Format(PyExc_SystemError,
1948 "no locals found when storing %s",
1949 PyObject_REPR(w));
1950 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001952 case DELETE_NAME:
1953 w = GETITEM(names, oparg);
1954 if ((x = f->f_locals) != NULL) {
1955 if ((err = PyObject_DelItem(x, w)) != 0)
1956 format_exc_check_arg(PyExc_NameError,
1957 NAME_ERROR_MSG,
1958 w);
1959 break;
1960 }
1961 PyErr_Format(PyExc_SystemError,
1962 "no locals when deleting %s",
1963 PyObject_REPR(w));
1964 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001966 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1967 case UNPACK_SEQUENCE:
1968 v = POP();
1969 if (PyTuple_CheckExact(v) &&
1970 PyTuple_GET_SIZE(v) == oparg) {
1971 PyObject **items = \
1972 ((PyTupleObject *)v)->ob_item;
1973 while (oparg--) {
1974 w = items[oparg];
1975 Py_INCREF(w);
1976 PUSH(w);
1977 }
1978 Py_DECREF(v);
1979 continue;
1980 } else if (PyList_CheckExact(v) &&
1981 PyList_GET_SIZE(v) == oparg) {
1982 PyObject **items = \
1983 ((PyListObject *)v)->ob_item;
1984 while (oparg--) {
1985 w = items[oparg];
1986 Py_INCREF(w);
1987 PUSH(w);
1988 }
1989 } else if (unpack_iterable(v, oparg,
1990 stack_pointer + oparg)) {
1991 STACKADJ(oparg);
1992 } else {
1993 /* unpack_iterable() raised an exception */
1994 why = WHY_EXCEPTION;
1995 }
1996 Py_DECREF(v);
1997 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001998
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001999 case STORE_ATTR:
2000 w = GETITEM(names, oparg);
2001 v = TOP();
2002 u = SECOND();
2003 STACKADJ(-2);
2004 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2005 Py_DECREF(v);
2006 Py_DECREF(u);
2007 if (err == 0) continue;
2008 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002009
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002010 case DELETE_ATTR:
2011 w = GETITEM(names, oparg);
2012 v = POP();
2013 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2014 /* del v.w */
2015 Py_DECREF(v);
2016 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002017
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002018 case STORE_GLOBAL:
2019 w = GETITEM(names, oparg);
2020 v = POP();
2021 err = PyDict_SetItem(f->f_globals, w, v);
2022 Py_DECREF(v);
2023 if (err == 0) continue;
2024 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002026 case DELETE_GLOBAL:
2027 w = GETITEM(names, oparg);
2028 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2029 format_exc_check_arg(
2030 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2031 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002032
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002033 case LOAD_NAME:
2034 w = GETITEM(names, oparg);
2035 if ((v = f->f_locals) == NULL) {
2036 PyErr_Format(PyExc_SystemError,
2037 "no locals when loading %s",
2038 PyObject_REPR(w));
2039 why = WHY_EXCEPTION;
2040 break;
2041 }
2042 if (PyDict_CheckExact(v)) {
2043 x = PyDict_GetItem(v, w);
2044 Py_XINCREF(x);
2045 }
2046 else {
2047 x = PyObject_GetItem(v, w);
2048 if (x == NULL && PyErr_Occurred()) {
2049 if (!PyErr_ExceptionMatches(
2050 PyExc_KeyError))
2051 break;
2052 PyErr_Clear();
2053 }
2054 }
2055 if (x == NULL) {
2056 x = PyDict_GetItem(f->f_globals, w);
2057 if (x == NULL) {
2058 x = PyDict_GetItem(f->f_builtins, w);
2059 if (x == NULL) {
2060 format_exc_check_arg(
2061 PyExc_NameError,
2062 NAME_ERROR_MSG, w);
2063 break;
2064 }
2065 }
2066 Py_INCREF(x);
2067 }
2068 PUSH(x);
2069 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002070
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002071 case LOAD_GLOBAL:
2072 w = GETITEM(names, oparg);
2073 if (PyString_CheckExact(w)) {
2074 /* Inline the PyDict_GetItem() calls.
2075 WARNING: this is an extreme speed hack.
2076 Do not try this at home. */
2077 long hash = ((PyStringObject *)w)->ob_shash;
2078 if (hash != -1) {
2079 PyDictObject *d;
2080 PyDictEntry *e;
2081 d = (PyDictObject *)(f->f_globals);
2082 e = d->ma_lookup(d, w, hash);
2083 if (e == NULL) {
2084 x = NULL;
2085 break;
2086 }
2087 x = e->me_value;
2088 if (x != NULL) {
2089 Py_INCREF(x);
2090 PUSH(x);
2091 continue;
2092 }
2093 d = (PyDictObject *)(f->f_builtins);
2094 e = d->ma_lookup(d, w, hash);
2095 if (e == NULL) {
2096 x = NULL;
2097 break;
2098 }
2099 x = e->me_value;
2100 if (x != NULL) {
2101 Py_INCREF(x);
2102 PUSH(x);
2103 continue;
2104 }
2105 goto load_global_error;
2106 }
2107 }
2108 /* This is the un-inlined version of the code above */
2109 x = PyDict_GetItem(f->f_globals, w);
2110 if (x == NULL) {
2111 x = PyDict_GetItem(f->f_builtins, w);
2112 if (x == NULL) {
2113 load_global_error:
2114 format_exc_check_arg(
2115 PyExc_NameError,
2116 GLOBAL_NAME_ERROR_MSG, w);
2117 break;
2118 }
2119 }
2120 Py_INCREF(x);
2121 PUSH(x);
2122 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002123
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002124 case DELETE_FAST:
2125 x = GETLOCAL(oparg);
2126 if (x != NULL) {
2127 SETLOCAL(oparg, NULL);
2128 continue;
2129 }
2130 format_exc_check_arg(
2131 PyExc_UnboundLocalError,
2132 UNBOUNDLOCAL_ERROR_MSG,
2133 PyTuple_GetItem(co->co_varnames, oparg)
2134 );
2135 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002136
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002137 case LOAD_CLOSURE:
2138 x = freevars[oparg];
2139 Py_INCREF(x);
2140 PUSH(x);
2141 if (x != NULL) continue;
2142 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002144 case LOAD_DEREF:
2145 x = freevars[oparg];
2146 w = PyCell_Get(x);
2147 if (w != NULL) {
2148 PUSH(w);
2149 continue;
2150 }
2151 err = -1;
2152 /* Don't stomp existing exception */
2153 if (PyErr_Occurred())
2154 break;
2155 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2156 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krah7ff78252010-06-23 18:12:09 +00002157 oparg);
2158 format_exc_check_arg(
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002159 PyExc_UnboundLocalError,
2160 UNBOUNDLOCAL_ERROR_MSG,
2161 v);
2162 } else {
2163 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2164 PyTuple_GET_SIZE(co->co_cellvars));
2165 format_exc_check_arg(PyExc_NameError,
2166 UNBOUNDFREE_ERROR_MSG, v);
2167 }
2168 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002170 case STORE_DEREF:
2171 w = POP();
2172 x = freevars[oparg];
2173 PyCell_Set(x, w);
2174 Py_DECREF(w);
2175 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002176
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002177 case BUILD_TUPLE:
2178 x = PyTuple_New(oparg);
2179 if (x != NULL) {
2180 for (; --oparg >= 0;) {
2181 w = POP();
2182 PyTuple_SET_ITEM(x, oparg, w);
2183 }
2184 PUSH(x);
2185 continue;
2186 }
2187 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002189 case BUILD_LIST:
2190 x = PyList_New(oparg);
2191 if (x != NULL) {
2192 for (; --oparg >= 0;) {
2193 w = POP();
2194 PyList_SET_ITEM(x, oparg, w);
2195 }
2196 PUSH(x);
2197 continue;
2198 }
2199 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002201 case BUILD_SET:
2202 x = PySet_New(NULL);
2203 if (x != NULL) {
2204 for (; --oparg >= 0;) {
2205 w = POP();
2206 if (err == 0)
2207 err = PySet_Add(x, w);
2208 Py_DECREF(w);
2209 }
2210 if (err != 0) {
2211 Py_DECREF(x);
2212 break;
2213 }
2214 PUSH(x);
2215 continue;
2216 }
2217 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002218
2219
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002220 case BUILD_MAP:
2221 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2222 PUSH(x);
2223 if (x != NULL) continue;
2224 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002226 case STORE_MAP:
2227 w = TOP(); /* key */
2228 u = SECOND(); /* value */
2229 v = THIRD(); /* dict */
2230 STACKADJ(-2);
2231 assert (PyDict_CheckExact(v));
2232 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2233 Py_DECREF(u);
2234 Py_DECREF(w);
2235 if (err == 0) continue;
2236 break;
Raymond Hettingereffde122007-12-18 18:26:18 +00002237
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002238 case MAP_ADD:
2239 w = TOP(); /* key */
2240 u = SECOND(); /* value */
2241 STACKADJ(-2);
2242 v = stack_pointer[-oparg]; /* dict */
2243 assert (PyDict_CheckExact(v));
2244 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2245 Py_DECREF(u);
2246 Py_DECREF(w);
2247 if (err == 0) {
2248 PREDICT(JUMP_ABSOLUTE);
2249 continue;
2250 }
2251 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002252
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002253 case LOAD_ATTR:
2254 w = GETITEM(names, oparg);
2255 v = TOP();
2256 x = PyObject_GetAttr(v, w);
2257 Py_DECREF(v);
2258 SET_TOP(x);
2259 if (x != NULL) continue;
2260 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002261
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002262 case COMPARE_OP:
2263 w = POP();
2264 v = TOP();
2265 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2266 /* INLINE: cmp(int, int) */
2267 register long a, b;
2268 register int res;
2269 a = PyInt_AS_LONG(v);
2270 b = PyInt_AS_LONG(w);
2271 switch (oparg) {
2272 case PyCmp_LT: res = a < b; break;
2273 case PyCmp_LE: res = a <= b; break;
2274 case PyCmp_EQ: res = a == b; break;
2275 case PyCmp_NE: res = a != b; break;
2276 case PyCmp_GT: res = a > b; break;
2277 case PyCmp_GE: res = a >= b; break;
2278 case PyCmp_IS: res = v == w; break;
2279 case PyCmp_IS_NOT: res = v != w; break;
2280 default: goto slow_compare;
2281 }
2282 x = res ? Py_True : Py_False;
2283 Py_INCREF(x);
2284 }
2285 else {
2286 slow_compare:
2287 x = cmp_outcome(oparg, v, w);
2288 }
2289 Py_DECREF(v);
2290 Py_DECREF(w);
2291 SET_TOP(x);
2292 if (x == NULL) break;
2293 PREDICT(POP_JUMP_IF_FALSE);
2294 PREDICT(POP_JUMP_IF_TRUE);
2295 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002297 case IMPORT_NAME:
2298 w = GETITEM(names, oparg);
2299 x = PyDict_GetItemString(f->f_builtins, "__import__");
2300 if (x == NULL) {
2301 PyErr_SetString(PyExc_ImportError,
2302 "__import__ not found");
2303 break;
2304 }
2305 Py_INCREF(x);
2306 v = POP();
2307 u = TOP();
2308 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2309 w = PyTuple_Pack(5,
2310 w,
2311 f->f_globals,
2312 f->f_locals == NULL ?
2313 Py_None : f->f_locals,
2314 v,
2315 u);
2316 else
2317 w = PyTuple_Pack(4,
2318 w,
2319 f->f_globals,
2320 f->f_locals == NULL ?
2321 Py_None : f->f_locals,
2322 v);
2323 Py_DECREF(v);
2324 Py_DECREF(u);
2325 if (w == NULL) {
2326 u = POP();
2327 Py_DECREF(x);
2328 x = NULL;
2329 break;
2330 }
2331 READ_TIMESTAMP(intr0);
2332 v = x;
2333 x = PyEval_CallObject(v, w);
2334 Py_DECREF(v);
2335 READ_TIMESTAMP(intr1);
2336 Py_DECREF(w);
2337 SET_TOP(x);
2338 if (x != NULL) continue;
2339 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002340
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002341 case IMPORT_STAR:
2342 v = POP();
2343 PyFrame_FastToLocals(f);
2344 if ((x = f->f_locals) == NULL) {
2345 PyErr_SetString(PyExc_SystemError,
2346 "no locals found during 'import *'");
2347 break;
2348 }
2349 READ_TIMESTAMP(intr0);
2350 err = import_all_from(x, v);
2351 READ_TIMESTAMP(intr1);
2352 PyFrame_LocalsToFast(f, 0);
2353 Py_DECREF(v);
2354 if (err == 0) continue;
2355 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002356
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002357 case IMPORT_FROM:
2358 w = GETITEM(names, oparg);
2359 v = TOP();
2360 READ_TIMESTAMP(intr0);
2361 x = import_from(v, w);
2362 READ_TIMESTAMP(intr1);
2363 PUSH(x);
2364 if (x != NULL) continue;
2365 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002366
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002367 case JUMP_FORWARD:
2368 JUMPBY(oparg);
2369 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002370
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002371 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2372 case POP_JUMP_IF_FALSE:
2373 w = POP();
2374 if (w == Py_True) {
2375 Py_DECREF(w);
2376 goto fast_next_opcode;
2377 }
2378 if (w == Py_False) {
2379 Py_DECREF(w);
2380 JUMPTO(oparg);
2381 goto fast_next_opcode;
2382 }
2383 err = PyObject_IsTrue(w);
2384 Py_DECREF(w);
2385 if (err > 0)
2386 err = 0;
2387 else if (err == 0)
2388 JUMPTO(oparg);
2389 else
2390 break;
2391 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002392
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002393 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2394 case POP_JUMP_IF_TRUE:
2395 w = POP();
2396 if (w == Py_False) {
2397 Py_DECREF(w);
2398 goto fast_next_opcode;
2399 }
2400 if (w == Py_True) {
2401 Py_DECREF(w);
2402 JUMPTO(oparg);
2403 goto fast_next_opcode;
2404 }
2405 err = PyObject_IsTrue(w);
2406 Py_DECREF(w);
2407 if (err > 0) {
2408 err = 0;
2409 JUMPTO(oparg);
2410 }
2411 else if (err == 0)
2412 ;
2413 else
2414 break;
2415 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002416
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002417 case JUMP_IF_FALSE_OR_POP:
2418 w = TOP();
2419 if (w == Py_True) {
2420 STACKADJ(-1);
2421 Py_DECREF(w);
2422 goto fast_next_opcode;
2423 }
2424 if (w == Py_False) {
2425 JUMPTO(oparg);
2426 goto fast_next_opcode;
2427 }
2428 err = PyObject_IsTrue(w);
2429 if (err > 0) {
2430 STACKADJ(-1);
2431 Py_DECREF(w);
2432 err = 0;
2433 }
2434 else if (err == 0)
2435 JUMPTO(oparg);
2436 else
2437 break;
2438 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002439
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002440 case JUMP_IF_TRUE_OR_POP:
2441 w = TOP();
2442 if (w == Py_False) {
2443 STACKADJ(-1);
2444 Py_DECREF(w);
2445 goto fast_next_opcode;
2446 }
2447 if (w == Py_True) {
2448 JUMPTO(oparg);
2449 goto fast_next_opcode;
2450 }
2451 err = PyObject_IsTrue(w);
2452 if (err > 0) {
2453 err = 0;
2454 JUMPTO(oparg);
2455 }
2456 else if (err == 0) {
2457 STACKADJ(-1);
2458 Py_DECREF(w);
2459 }
2460 else
2461 break;
2462 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002463
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002464 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2465 case JUMP_ABSOLUTE:
2466 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002467#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002468 /* Enabling this path speeds-up all while and for-loops by bypassing
2469 the per-loop checks for signals. By default, this should be turned-off
2470 because it prevents detection of a control-break in tight loops like
2471 "while 1: pass". Compile with this option turned-on when you need
2472 the speed-up and do not need break checking inside tight loops (ones
2473 that contain only instructions ending with goto fast_next_opcode).
2474 */
2475 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002476#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002477 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002478#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002479
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002480 case GET_ITER:
2481 /* before: [obj]; after [getiter(obj)] */
2482 v = TOP();
2483 x = PyObject_GetIter(v);
2484 Py_DECREF(v);
2485 if (x != NULL) {
2486 SET_TOP(x);
2487 PREDICT(FOR_ITER);
2488 continue;
2489 }
2490 STACKADJ(-1);
2491 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002492
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002493 PREDICTED_WITH_ARG(FOR_ITER);
2494 case FOR_ITER:
2495 /* before: [iter]; after: [iter, iter()] *or* [] */
2496 v = TOP();
2497 x = (*v->ob_type->tp_iternext)(v);
2498 if (x != NULL) {
2499 PUSH(x);
2500 PREDICT(STORE_FAST);
2501 PREDICT(UNPACK_SEQUENCE);
2502 continue;
2503 }
2504 if (PyErr_Occurred()) {
2505 if (!PyErr_ExceptionMatches(
2506 PyExc_StopIteration))
2507 break;
2508 PyErr_Clear();
2509 }
2510 /* iterator ended normally */
2511 x = v = POP();
2512 Py_DECREF(v);
2513 JUMPBY(oparg);
2514 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002516 case BREAK_LOOP:
2517 why = WHY_BREAK;
2518 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002519
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002520 case CONTINUE_LOOP:
2521 retval = PyInt_FromLong(oparg);
2522 if (!retval) {
2523 x = NULL;
2524 break;
2525 }
2526 why = WHY_CONTINUE;
2527 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002528
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002529 case SETUP_LOOP:
2530 case SETUP_EXCEPT:
2531 case SETUP_FINALLY:
2532 /* NOTE: If you add any new block-setup opcodes that
2533 are not try/except/finally handlers, you may need
2534 to update the PyGen_NeedsFinalizing() function.
2535 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002536
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002537 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2538 STACK_LEVEL());
2539 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002540
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002541 case SETUP_WITH:
2542 {
2543 static PyObject *exit, *enter;
2544 w = TOP();
2545 x = special_lookup(w, "__exit__", &exit);
2546 if (!x)
2547 break;
2548 SET_TOP(x);
2549 u = special_lookup(w, "__enter__", &enter);
2550 Py_DECREF(w);
2551 if (!u) {
2552 x = NULL;
2553 break;
2554 }
2555 x = PyObject_CallFunctionObjArgs(u, NULL);
2556 Py_DECREF(u);
2557 if (!x)
2558 break;
2559 /* Setup a finally block (SETUP_WITH as a block is
2560 equivalent to SETUP_FINALLY except it normalizes
2561 the exception) before pushing the result of
2562 __enter__ on the stack. */
2563 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2564 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002565
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002566 PUSH(x);
2567 continue;
2568 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002569
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002570 case WITH_CLEANUP:
2571 {
2572 /* At the top of the stack are 1-3 values indicating
2573 how/why we entered the finally clause:
2574 - TOP = None
2575 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2576 - TOP = WHY_*; no retval below it
2577 - (TOP, SECOND, THIRD) = exc_info()
2578 Below them is EXIT, the context.__exit__ bound method.
2579 In the last case, we must call
2580 EXIT(TOP, SECOND, THIRD)
2581 otherwise we must call
2582 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002583
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002584 In all cases, we remove EXIT from the stack, leaving
2585 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002586
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002587 In addition, if the stack represents an exception,
2588 *and* the function call returns a 'true' value, we
2589 "zap" this information, to prevent END_FINALLY from
2590 re-raising the exception. (But non-local gotos
2591 should still be resumed.)
2592 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002593
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002594 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002595
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002596 u = POP();
2597 if (u == Py_None) {
2598 exit_func = TOP();
2599 SET_TOP(u);
2600 v = w = Py_None;
2601 }
2602 else if (PyInt_Check(u)) {
2603 switch(PyInt_AS_LONG(u)) {
2604 case WHY_RETURN:
2605 case WHY_CONTINUE:
2606 /* Retval in TOP. */
2607 exit_func = SECOND();
2608 SET_SECOND(TOP());
2609 SET_TOP(u);
2610 break;
2611 default:
2612 exit_func = TOP();
2613 SET_TOP(u);
2614 break;
2615 }
2616 u = v = w = Py_None;
2617 }
2618 else {
2619 v = TOP();
2620 w = SECOND();
2621 exit_func = THIRD();
2622 SET_TOP(u);
2623 SET_SECOND(v);
2624 SET_THIRD(w);
2625 }
2626 /* XXX Not the fastest way to call it... */
2627 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2628 NULL);
2629 Py_DECREF(exit_func);
2630 if (x == NULL)
2631 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002632
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002633 if (u != Py_None)
2634 err = PyObject_IsTrue(x);
2635 else
2636 err = 0;
2637 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002639 if (err < 0)
2640 break; /* Go to error exit */
2641 else if (err > 0) {
2642 err = 0;
2643 /* There was an exception and a true return */
2644 STACKADJ(-2);
2645 Py_INCREF(Py_None);
2646 SET_TOP(Py_None);
2647 Py_DECREF(u);
2648 Py_DECREF(v);
2649 Py_DECREF(w);
2650 } else {
2651 /* The stack was rearranged to remove EXIT
2652 above. Let END_FINALLY do its thing */
2653 }
2654 PREDICT(END_FINALLY);
2655 break;
2656 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002657
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002658 case CALL_FUNCTION:
2659 {
2660 PyObject **sp;
2661 PCALL(PCALL_ALL);
2662 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002663#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002664 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002665#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002666 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002667#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002668 stack_pointer = sp;
2669 PUSH(x);
2670 if (x != NULL)
2671 continue;
2672 break;
2673 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002674
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002675 case CALL_FUNCTION_VAR:
2676 case CALL_FUNCTION_KW:
2677 case CALL_FUNCTION_VAR_KW:
2678 {
2679 int na = oparg & 0xff;
2680 int nk = (oparg>>8) & 0xff;
2681 int flags = (opcode - CALL_FUNCTION) & 3;
2682 int n = na + 2 * nk;
2683 PyObject **pfunc, *func, **sp;
2684 PCALL(PCALL_ALL);
2685 if (flags & CALL_FLAG_VAR)
2686 n++;
2687 if (flags & CALL_FLAG_KW)
2688 n++;
2689 pfunc = stack_pointer - n - 1;
2690 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002691
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002692 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00002693 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002694 PyObject *self = PyMethod_GET_SELF(func);
2695 Py_INCREF(self);
2696 func = PyMethod_GET_FUNCTION(func);
2697 Py_INCREF(func);
2698 Py_DECREF(*pfunc);
2699 *pfunc = self;
2700 na++;
2701 } else
2702 Py_INCREF(func);
2703 sp = stack_pointer;
2704 READ_TIMESTAMP(intr0);
2705 x = ext_do_call(func, &sp, flags, na, nk);
2706 READ_TIMESTAMP(intr1);
2707 stack_pointer = sp;
2708 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002709
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002710 while (stack_pointer > pfunc) {
2711 w = POP();
2712 Py_DECREF(w);
2713 }
2714 PUSH(x);
2715 if (x != NULL)
2716 continue;
2717 break;
2718 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002719
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002720 case MAKE_FUNCTION:
2721 v = POP(); /* code object */
2722 x = PyFunction_New(v, f->f_globals);
2723 Py_DECREF(v);
2724 /* XXX Maybe this should be a separate opcode? */
2725 if (x != NULL && oparg > 0) {
2726 v = PyTuple_New(oparg);
2727 if (v == NULL) {
2728 Py_DECREF(x);
2729 x = NULL;
2730 break;
2731 }
2732 while (--oparg >= 0) {
2733 w = POP();
2734 PyTuple_SET_ITEM(v, oparg, w);
2735 }
2736 err = PyFunction_SetDefaults(x, v);
2737 Py_DECREF(v);
2738 }
2739 PUSH(x);
2740 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002741
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002742 case MAKE_CLOSURE:
2743 {
2744 v = POP(); /* code object */
2745 x = PyFunction_New(v, f->f_globals);
2746 Py_DECREF(v);
2747 if (x != NULL) {
2748 v = POP();
2749 if (PyFunction_SetClosure(x, v) != 0) {
2750 /* Can't happen unless bytecode is corrupt. */
2751 why = WHY_EXCEPTION;
2752 }
2753 Py_DECREF(v);
2754 }
2755 if (x != NULL && oparg > 0) {
2756 v = PyTuple_New(oparg);
2757 if (v == NULL) {
2758 Py_DECREF(x);
2759 x = NULL;
2760 break;
2761 }
2762 while (--oparg >= 0) {
2763 w = POP();
2764 PyTuple_SET_ITEM(v, oparg, w);
2765 }
2766 if (PyFunction_SetDefaults(x, v) != 0) {
2767 /* Can't happen unless
2768 PyFunction_SetDefaults changes. */
2769 why = WHY_EXCEPTION;
2770 }
2771 Py_DECREF(v);
2772 }
2773 PUSH(x);
2774 break;
2775 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002777 case BUILD_SLICE:
2778 if (oparg == 3)
2779 w = POP();
2780 else
2781 w = NULL;
2782 v = POP();
2783 u = TOP();
2784 x = PySlice_New(u, v, w);
2785 Py_DECREF(u);
2786 Py_DECREF(v);
2787 Py_XDECREF(w);
2788 SET_TOP(x);
2789 if (x != NULL) continue;
2790 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002791
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002792 case EXTENDED_ARG:
2793 opcode = NEXTOP();
2794 oparg = oparg<<16 | NEXTARG();
2795 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002796
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002797 default:
2798 fprintf(stderr,
2799 "XXX lineno: %d, opcode: %d\n",
2800 PyFrame_GetLineNumber(f),
2801 opcode);
2802 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2803 why = WHY_EXCEPTION;
2804 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002805
2806#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002807 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002808#endif
2809
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002810 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002811
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002812 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002813
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002814 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002815
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002816 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002817
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002818 if (why == WHY_NOT) {
2819 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002820#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002821 /* This check is expensive! */
2822 if (PyErr_Occurred())
2823 fprintf(stderr,
2824 "XXX undetected error\n");
2825 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002826#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002827 READ_TIMESTAMP(loop1);
2828 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002829#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002830 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002831#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002832 }
2833 why = WHY_EXCEPTION;
2834 x = Py_None;
2835 err = 0;
2836 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002837
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002838 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002840 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2841 if (!PyErr_Occurred()) {
2842 PyErr_SetString(PyExc_SystemError,
2843 "error return without exception set");
2844 why = WHY_EXCEPTION;
2845 }
2846 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002847#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002848 else {
2849 /* This check is expensive! */
2850 if (PyErr_Occurred()) {
2851 char buf[128];
2852 sprintf(buf, "Stack unwind with exception "
2853 "set and why=%d", why);
2854 Py_FatalError(buf);
2855 }
2856 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002857#endif
2858
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002859 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002860
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002861 if (why == WHY_EXCEPTION) {
2862 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002863
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002864 if (tstate->c_tracefunc != NULL)
2865 call_exc_trace(tstate->c_tracefunc,
2866 tstate->c_traceobj, f);
2867 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002868
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002869 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002870
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002871 if (why == WHY_RERAISE)
2872 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002873
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002874 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002875
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002876fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002877 while (why != WHY_NOT && f->f_iblock > 0) {
2878 /* Peek at the current block. */
2879 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002880
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002881 assert(why != WHY_YIELD);
2882 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2883 why = WHY_NOT;
2884 JUMPTO(PyInt_AS_LONG(retval));
2885 Py_DECREF(retval);
2886 break;
2887 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002889 /* Now we have to pop the block. */
2890 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00002891
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002892 while (STACK_LEVEL() > b->b_level) {
2893 v = POP();
2894 Py_XDECREF(v);
2895 }
2896 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2897 why = WHY_NOT;
2898 JUMPTO(b->b_handler);
2899 break;
2900 }
2901 if (b->b_type == SETUP_FINALLY ||
2902 (b->b_type == SETUP_EXCEPT &&
2903 why == WHY_EXCEPTION) ||
2904 b->b_type == SETUP_WITH) {
2905 if (why == WHY_EXCEPTION) {
2906 PyObject *exc, *val, *tb;
2907 PyErr_Fetch(&exc, &val, &tb);
2908 if (val == NULL) {
2909 val = Py_None;
2910 Py_INCREF(val);
2911 }
2912 /* Make the raw exception data
2913 available to the handler,
2914 so a program can emulate the
2915 Python main loop. Don't do
2916 this for 'finally'. */
2917 if (b->b_type == SETUP_EXCEPT ||
2918 b->b_type == SETUP_WITH) {
2919 PyErr_NormalizeException(
2920 &exc, &val, &tb);
2921 set_exc_info(tstate,
2922 exc, val, tb);
2923 }
2924 if (tb == NULL) {
2925 Py_INCREF(Py_None);
2926 PUSH(Py_None);
2927 } else
2928 PUSH(tb);
2929 PUSH(val);
2930 PUSH(exc);
2931 }
2932 else {
2933 if (why & (WHY_RETURN | WHY_CONTINUE))
2934 PUSH(retval);
2935 v = PyInt_FromLong((long)why);
2936 PUSH(v);
2937 }
2938 why = WHY_NOT;
2939 JUMPTO(b->b_handler);
2940 break;
2941 }
2942 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002943
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002944 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002946 if (why != WHY_NOT)
2947 break;
2948 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002949
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002950 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002951
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002952 assert(why != WHY_YIELD);
2953 /* Pop remaining stack entries. */
2954 while (!EMPTY()) {
2955 v = POP();
2956 Py_XDECREF(v);
2957 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002958
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002959 if (why != WHY_RETURN)
2960 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002961
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002962fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002963 if (tstate->use_tracing) {
2964 if (tstate->c_tracefunc) {
2965 if (why == WHY_RETURN || why == WHY_YIELD) {
2966 if (call_trace(tstate->c_tracefunc,
2967 tstate->c_traceobj, f,
2968 PyTrace_RETURN, retval)) {
2969 Py_XDECREF(retval);
2970 retval = NULL;
2971 why = WHY_EXCEPTION;
2972 }
2973 }
2974 else if (why == WHY_EXCEPTION) {
2975 call_trace_protected(tstate->c_tracefunc,
2976 tstate->c_traceobj, f,
2977 PyTrace_RETURN, NULL);
2978 }
2979 }
2980 if (tstate->c_profilefunc) {
2981 if (why == WHY_EXCEPTION)
2982 call_trace_protected(tstate->c_profilefunc,
2983 tstate->c_profileobj, f,
2984 PyTrace_RETURN, NULL);
2985 else if (call_trace(tstate->c_profilefunc,
2986 tstate->c_profileobj, f,
2987 PyTrace_RETURN, retval)) {
2988 Py_XDECREF(retval);
2989 retval = NULL;
2990 why = WHY_EXCEPTION;
2991 }
2992 }
2993 }
Guido van Rossuma4240131997-01-21 21:18:36 +00002994
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002995 if (tstate->frame->f_exc_type != NULL)
2996 reset_exc_info(tstate);
2997 else {
2998 assert(tstate->frame->f_exc_value == NULL);
2999 assert(tstate->frame->f_exc_traceback == NULL);
3000 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003001
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003002 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003003exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003004 Py_LeaveRecursiveCall();
3005 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003006
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003007 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003008}
3009
Guido van Rossumc2e20742006-02-27 22:32:47 +00003010/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003011 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003012 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003013
Tim Peters6d6c1a32001-08-02 04:15:00 +00003014PyObject *
3015PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003016 PyObject **args, int argcount, PyObject **kws, int kwcount,
3017 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003018{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003019 register PyFrameObject *f;
3020 register PyObject *retval = NULL;
3021 register PyObject **fastlocals, **freevars;
3022 PyThreadState *tstate = PyThreadState_GET();
3023 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003024
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003025 if (globals == NULL) {
3026 PyErr_SetString(PyExc_SystemError,
3027 "PyEval_EvalCodeEx: NULL globals");
3028 return NULL;
3029 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003031 assert(tstate != NULL);
3032 assert(globals != NULL);
3033 f = PyFrame_New(tstate, co, globals, locals);
3034 if (f == NULL)
3035 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003036
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003037 fastlocals = f->f_localsplus;
3038 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003039
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003040 if (co->co_argcount > 0 ||
3041 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3042 int i;
3043 int n = argcount;
3044 PyObject *kwdict = NULL;
3045 if (co->co_flags & CO_VARKEYWORDS) {
3046 kwdict = PyDict_New();
3047 if (kwdict == NULL)
3048 goto fail;
3049 i = co->co_argcount;
3050 if (co->co_flags & CO_VARARGS)
3051 i++;
3052 SETLOCAL(i, kwdict);
3053 }
3054 if (argcount > co->co_argcount) {
3055 if (!(co->co_flags & CO_VARARGS)) {
3056 PyErr_Format(PyExc_TypeError,
3057 "%.200s() takes %s %d "
3058 "argument%s (%d given)",
3059 PyString_AsString(co->co_name),
3060 defcount ? "at most" : "exactly",
3061 co->co_argcount,
3062 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003063 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003064 goto fail;
3065 }
3066 n = co->co_argcount;
3067 }
3068 for (i = 0; i < n; i++) {
3069 x = args[i];
3070 Py_INCREF(x);
3071 SETLOCAL(i, x);
3072 }
3073 if (co->co_flags & CO_VARARGS) {
3074 u = PyTuple_New(argcount - n);
3075 if (u == NULL)
3076 goto fail;
3077 SETLOCAL(co->co_argcount, u);
3078 for (i = n; i < argcount; i++) {
3079 x = args[i];
3080 Py_INCREF(x);
3081 PyTuple_SET_ITEM(u, i-n, x);
3082 }
3083 }
3084 for (i = 0; i < kwcount; i++) {
3085 PyObject **co_varnames;
3086 PyObject *keyword = kws[2*i];
3087 PyObject *value = kws[2*i + 1];
3088 int j;
3089 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003090#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003091 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003092#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003093 )) {
3094 PyErr_Format(PyExc_TypeError,
3095 "%.200s() keywords must be strings",
3096 PyString_AsString(co->co_name));
3097 goto fail;
3098 }
3099 /* Speed hack: do raw pointer compares. As names are
3100 normally interned this should almost always hit. */
3101 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3102 for (j = 0; j < co->co_argcount; j++) {
3103 PyObject *nm = co_varnames[j];
3104 if (nm == keyword)
3105 goto kw_found;
3106 }
3107 /* Slow fallback, just in case */
3108 for (j = 0; j < co->co_argcount; j++) {
3109 PyObject *nm = co_varnames[j];
3110 int cmp = PyObject_RichCompareBool(
3111 keyword, nm, Py_EQ);
3112 if (cmp > 0)
3113 goto kw_found;
3114 else if (cmp < 0)
3115 goto fail;
3116 }
3117 if (kwdict == NULL) {
3118 PyObject *kwd_str = kwd_as_string(keyword);
3119 if (kwd_str) {
3120 PyErr_Format(PyExc_TypeError,
3121 "%.200s() got an unexpected "
3122 "keyword argument '%.400s'",
3123 PyString_AsString(co->co_name),
3124 PyString_AsString(kwd_str));
3125 Py_DECREF(kwd_str);
3126 }
3127 goto fail;
3128 }
3129 PyDict_SetItem(kwdict, keyword, value);
3130 continue;
3131 kw_found:
3132 if (GETLOCAL(j) != NULL) {
3133 PyObject *kwd_str = kwd_as_string(keyword);
3134 if (kwd_str) {
3135 PyErr_Format(PyExc_TypeError,
3136 "%.200s() got multiple "
3137 "values for keyword "
3138 "argument '%.400s'",
3139 PyString_AsString(co->co_name),
3140 PyString_AsString(kwd_str));
3141 Py_DECREF(kwd_str);
3142 }
3143 goto fail;
3144 }
3145 Py_INCREF(value);
3146 SETLOCAL(j, value);
3147 }
3148 if (argcount < co->co_argcount) {
3149 int m = co->co_argcount - defcount;
3150 for (i = argcount; i < m; i++) {
3151 if (GETLOCAL(i) == NULL) {
3152 int j, given = 0;
3153 for (j = 0; j < co->co_argcount; j++)
3154 if (GETLOCAL(j))
3155 given++;
3156 PyErr_Format(PyExc_TypeError,
3157 "%.200s() takes %s %d "
3158 "argument%s (%d given)",
3159 PyString_AsString(co->co_name),
3160 ((co->co_flags & CO_VARARGS) ||
3161 defcount) ? "at least"
3162 : "exactly",
3163 m, m == 1 ? "" : "s", given);
3164 goto fail;
3165 }
3166 }
3167 if (n > m)
3168 i = n - m;
3169 else
3170 i = 0;
3171 for (; i < defcount; i++) {
3172 if (GETLOCAL(m+i) == NULL) {
3173 PyObject *def = defs[i];
3174 Py_INCREF(def);
3175 SETLOCAL(m+i, def);
3176 }
3177 }
3178 }
3179 }
3180 else if (argcount > 0 || kwcount > 0) {
3181 PyErr_Format(PyExc_TypeError,
3182 "%.200s() takes no arguments (%d given)",
3183 PyString_AsString(co->co_name),
3184 argcount + kwcount);
3185 goto fail;
3186 }
3187 /* Allocate and initialize storage for cell vars, and copy free
3188 vars into frame. This isn't too efficient right now. */
3189 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3190 int i, j, nargs, found;
3191 char *cellname, *argname;
3192 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003194 nargs = co->co_argcount;
3195 if (co->co_flags & CO_VARARGS)
3196 nargs++;
3197 if (co->co_flags & CO_VARKEYWORDS)
3198 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003199
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003200 /* Initialize each cell var, taking into account
3201 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003202
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003203 Should arrange for the compiler to put cellvars
3204 that are arguments at the beginning of the cellvars
3205 list so that we can march over it more efficiently?
3206 */
3207 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3208 cellname = PyString_AS_STRING(
3209 PyTuple_GET_ITEM(co->co_cellvars, i));
3210 found = 0;
3211 for (j = 0; j < nargs; j++) {
3212 argname = PyString_AS_STRING(
3213 PyTuple_GET_ITEM(co->co_varnames, j));
3214 if (strcmp(cellname, argname) == 0) {
3215 c = PyCell_New(GETLOCAL(j));
3216 if (c == NULL)
3217 goto fail;
3218 GETLOCAL(co->co_nlocals + i) = c;
3219 found = 1;
3220 break;
3221 }
3222 }
3223 if (found == 0) {
3224 c = PyCell_New(NULL);
3225 if (c == NULL)
3226 goto fail;
3227 SETLOCAL(co->co_nlocals + i, c);
3228 }
3229 }
3230 }
3231 if (PyTuple_GET_SIZE(co->co_freevars)) {
3232 int i;
3233 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3234 PyObject *o = PyTuple_GET_ITEM(closure, i);
3235 Py_INCREF(o);
3236 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3237 }
3238 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003239
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003240 if (co->co_flags & CO_GENERATOR) {
3241 /* Don't need to keep the reference to f_back, it will be set
3242 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003243 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003244
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003245 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003246
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003247 /* Create a new generator that owns the ready to run frame
3248 * and return that as the value. */
3249 return PyGen_New(f);
3250 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003251
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003252 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003253
Thomas Woutersae406c62007-09-19 17:27:43 +00003254fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003256 /* decref'ing the frame can cause __del__ methods to get invoked,
3257 which can call back into Python. While we're done with the
3258 current Python frame (f), the associated C stack is still in use,
3259 so recursion_depth must be boosted for the duration.
3260 */
3261 assert(tstate != NULL);
3262 ++tstate->recursion_depth;
3263 Py_DECREF(f);
3264 --tstate->recursion_depth;
3265 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003266}
3267
3268
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003269static PyObject *
3270special_lookup(PyObject *o, char *meth, PyObject **cache)
3271{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003272 PyObject *res;
3273 if (PyInstance_Check(o)) {
3274 if (!*cache)
3275 return PyObject_GetAttrString(o, meth);
3276 else
3277 return PyObject_GetAttr(o, *cache);
3278 }
3279 res = _PyObject_LookupSpecial(o, meth, cache);
3280 if (res == NULL && !PyErr_Occurred()) {
3281 PyErr_SetObject(PyExc_AttributeError, *cache);
3282 return NULL;
3283 }
3284 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003285}
3286
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003287
Benjamin Petersone18ef192009-01-20 14:21:16 +00003288static PyObject *
3289kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003290#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003291 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003292#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003293 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003294#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003295 Py_INCREF(kwd);
3296 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003297#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003298 }
3299 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003300#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003301}
3302
3303
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003304/* Implementation notes for set_exc_info() and reset_exc_info():
3305
3306- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3307 'exc_traceback'. These always travel together.
3308
3309- tstate->curexc_ZZZ is the "hot" exception that is set by
3310 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3311
3312- Once an exception is caught by an except clause, it is transferred
3313 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3314 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003315 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003316
3317- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3318
3319 Long ago, when none of this existed, there were just a few globals:
3320 one set corresponding to the "hot" exception, and one set
3321 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3322 globals; they were simply stored as sys.exc_ZZZ. For backwards
3323 compatibility, they still are!) The problem was that in code like
3324 this:
3325
3326 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003327 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003328 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003329 "do something else first"
3330 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003331
3332 if "do something else first" invoked something that raised and caught
3333 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3334 cause of subtle bugs. I fixed this by changing the semantics as
3335 follows:
3336
3337 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3338 *in that frame*.
3339
3340 - But initially, and as long as no exception is caught in a given
3341 frame, sys.exc_ZZZ will hold the last exception caught in the
3342 previous frame (or the frame before that, etc.).
3343
3344 The first bullet fixed the bug in the above example. The second
3345 bullet was for backwards compatibility: it was (and is) common to
3346 have a function that is called when an exception is caught, and to
3347 have that function access the caught exception via sys.exc_ZZZ.
3348 (Example: traceback.print_exc()).
3349
3350 At the same time I fixed the problem that sys.exc_ZZZ weren't
3351 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3352 but that's really a separate improvement.
3353
3354 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3355 variables to what they were before the current frame was called. The
3356 set_exc_info() function saves them on the frame so that
3357 reset_exc_info() can restore them. The invariant is that
3358 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3359 exception (where "catching" an exception applies only to successful
3360 except clauses); and if the current frame ever caught an exception,
3361 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3362 at the start of the current frame.
3363
3364*/
3365
Fredrik Lundh7a830892006-05-27 10:39:48 +00003366static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003367set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003368 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003369{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003370 PyFrameObject *frame = tstate->frame;
3371 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003372
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003373 assert(type != NULL);
3374 assert(frame != NULL);
3375 if (frame->f_exc_type == NULL) {
3376 assert(frame->f_exc_value == NULL);
3377 assert(frame->f_exc_traceback == NULL);
3378 /* This frame didn't catch an exception before. */
3379 /* Save previous exception of this thread in this frame. */
3380 if (tstate->exc_type == NULL) {
3381 /* XXX Why is this set to Py_None? */
3382 Py_INCREF(Py_None);
3383 tstate->exc_type = Py_None;
3384 }
3385 Py_INCREF(tstate->exc_type);
3386 Py_XINCREF(tstate->exc_value);
3387 Py_XINCREF(tstate->exc_traceback);
3388 frame->f_exc_type = tstate->exc_type;
3389 frame->f_exc_value = tstate->exc_value;
3390 frame->f_exc_traceback = tstate->exc_traceback;
3391 }
3392 /* Set new exception for this thread. */
3393 tmp_type = tstate->exc_type;
3394 tmp_value = tstate->exc_value;
3395 tmp_tb = tstate->exc_traceback;
3396 Py_INCREF(type);
3397 Py_XINCREF(value);
3398 Py_XINCREF(tb);
3399 tstate->exc_type = type;
3400 tstate->exc_value = value;
3401 tstate->exc_traceback = tb;
3402 Py_XDECREF(tmp_type);
3403 Py_XDECREF(tmp_value);
3404 Py_XDECREF(tmp_tb);
3405 /* For b/w compatibility */
3406 PySys_SetObject("exc_type", type);
3407 PySys_SetObject("exc_value", value);
3408 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003409}
3410
Fredrik Lundh7a830892006-05-27 10:39:48 +00003411static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003412reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003413{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003414 PyFrameObject *frame;
3415 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003416
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003417 /* It's a precondition that the thread state's frame caught an
3418 * exception -- verify in a debug build.
3419 */
3420 assert(tstate != NULL);
3421 frame = tstate->frame;
3422 assert(frame != NULL);
3423 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003424
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003425 /* Copy the frame's exception info back to the thread state. */
3426 tmp_type = tstate->exc_type;
3427 tmp_value = tstate->exc_value;
3428 tmp_tb = tstate->exc_traceback;
3429 Py_INCREF(frame->f_exc_type);
3430 Py_XINCREF(frame->f_exc_value);
3431 Py_XINCREF(frame->f_exc_traceback);
3432 tstate->exc_type = frame->f_exc_type;
3433 tstate->exc_value = frame->f_exc_value;
3434 tstate->exc_traceback = frame->f_exc_traceback;
3435 Py_XDECREF(tmp_type);
3436 Py_XDECREF(tmp_value);
3437 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003439 /* For b/w compatibility */
3440 PySys_SetObject("exc_type", frame->f_exc_type);
3441 PySys_SetObject("exc_value", frame->f_exc_value);
3442 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003443
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003444 /* Clear the frame's exception info. */
3445 tmp_type = frame->f_exc_type;
3446 tmp_value = frame->f_exc_value;
3447 tmp_tb = frame->f_exc_traceback;
3448 frame->f_exc_type = NULL;
3449 frame->f_exc_value = NULL;
3450 frame->f_exc_traceback = NULL;
3451 Py_DECREF(tmp_type);
3452 Py_XDECREF(tmp_value);
3453 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003454}
3455
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003456/* Logic for the raise statement (too complicated for inlining).
3457 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003458static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003459do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003460{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003461 if (type == NULL) {
3462 /* Reraise */
3463 PyThreadState *tstate = PyThreadState_GET();
3464 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3465 value = tstate->exc_value;
3466 tb = tstate->exc_traceback;
3467 Py_XINCREF(type);
3468 Py_XINCREF(value);
3469 Py_XINCREF(tb);
3470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003471
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003472 /* We support the following forms of raise:
3473 raise <class>, <classinstance>
3474 raise <class>, <argument tuple>
3475 raise <class>, None
3476 raise <class>, <argument>
3477 raise <classinstance>, None
3478 raise <string>, <object>
3479 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003480
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003481 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003483 In addition, raise <tuple>, <anything> is the same as
3484 raising the tuple's first item (and it better have one!);
3485 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003486
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003487 Finally, an optional third argument can be supplied, which
3488 gives the traceback to be substituted (useful when
3489 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003490
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003491 /* First, check the traceback argument, replacing None with
3492 NULL. */
3493 if (tb == Py_None) {
3494 Py_DECREF(tb);
3495 tb = NULL;
3496 }
3497 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3498 PyErr_SetString(PyExc_TypeError,
3499 "raise: arg 3 must be a traceback or None");
3500 goto raise_error;
3501 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003502
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003503 /* Next, replace a missing value with None */
3504 if (value == NULL) {
3505 value = Py_None;
3506 Py_INCREF(value);
3507 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003508
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003509 /* Next, repeatedly, replace a tuple exception with its first item */
3510 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3511 PyObject *tmp = type;
3512 type = PyTuple_GET_ITEM(type, 0);
3513 Py_INCREF(type);
3514 Py_DECREF(tmp);
3515 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003516
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003517 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003518 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003519 if (!PyExceptionInstance_Check(value)) {
3520 PyErr_Format(PyExc_TypeError,
3521 "calling %s() should have returned an instance of "
3522 "BaseException, not '%s'",
3523 ((PyTypeObject *)type)->tp_name,
3524 Py_TYPE(value)->tp_name);
3525 goto raise_error;
3526 }
3527 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003528 else if (PyExceptionInstance_Check(type)) {
3529 /* Raising an instance. The value should be a dummy. */
3530 if (value != Py_None) {
3531 PyErr_SetString(PyExc_TypeError,
3532 "instance exception may not have a separate value");
3533 goto raise_error;
3534 }
3535 else {
3536 /* Normalize to raise <class>, <instance> */
3537 Py_DECREF(value);
3538 value = type;
3539 type = PyExceptionInstance_Class(type);
3540 Py_INCREF(type);
3541 }
3542 }
3543 else {
3544 /* Not something you can raise. You get an exception
3545 anyway, just not what you specified :-) */
3546 PyErr_Format(PyExc_TypeError,
3547 "exceptions must be old-style classes or "
3548 "derived from BaseException, not %s",
3549 type->ob_type->tp_name);
3550 goto raise_error;
3551 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003552
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003553 assert(PyExceptionClass_Check(type));
3554 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3555 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3556 "exceptions must derive from BaseException "
3557 "in 3.x", 1) < 0)
3558 goto raise_error;
3559 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003560
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003561 PyErr_Restore(type, value, tb);
3562 if (tb == NULL)
3563 return WHY_EXCEPTION;
3564 else
3565 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003566 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003567 Py_XDECREF(value);
3568 Py_XDECREF(type);
3569 Py_XDECREF(tb);
3570 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003571}
3572
Tim Petersd6d010b2001-06-21 02:49:55 +00003573/* Iterate v argcnt times and store the results on the stack (via decreasing
3574 sp). Return 1 for success, 0 if error. */
3575
Fredrik Lundh7a830892006-05-27 10:39:48 +00003576static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003577unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003578{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003579 int i = 0;
3580 PyObject *it; /* iter(v) */
3581 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003582
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003583 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003584
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003585 it = PyObject_GetIter(v);
3586 if (it == NULL)
3587 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003588
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003589 for (; i < argcnt; i++) {
3590 w = PyIter_Next(it);
3591 if (w == NULL) {
3592 /* Iterator done, via error or exhaustion. */
3593 if (!PyErr_Occurred()) {
3594 PyErr_Format(PyExc_ValueError,
3595 "need more than %d value%s to unpack",
3596 i, i == 1 ? "" : "s");
3597 }
3598 goto Error;
3599 }
3600 *--sp = w;
3601 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003602
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003603 /* We better have exhausted the iterator now. */
3604 w = PyIter_Next(it);
3605 if (w == NULL) {
3606 if (PyErr_Occurred())
3607 goto Error;
3608 Py_DECREF(it);
3609 return 1;
3610 }
3611 Py_DECREF(w);
3612 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3613 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003614Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003615 for (; i > 0; i--, sp++)
3616 Py_DECREF(*sp);
3617 Py_XDECREF(it);
3618 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003619}
3620
3621
Guido van Rossum96a42c81992-01-12 02:29:51 +00003622#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003623static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003624prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003625{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003626 printf("%s ", str);
3627 if (PyObject_Print(v, stdout, 0) != 0)
3628 PyErr_Clear(); /* Don't know what else to do */
3629 printf("\n");
3630 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003631}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003632#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003633
Fredrik Lundh7a830892006-05-27 10:39:48 +00003634static void
Fred Drake5755ce62001-06-27 19:19:46 +00003635call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003636{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003637 PyObject *type, *value, *traceback, *arg;
3638 int err;
3639 PyErr_Fetch(&type, &value, &traceback);
3640 if (value == NULL) {
3641 value = Py_None;
3642 Py_INCREF(value);
3643 }
3644 arg = PyTuple_Pack(3, type, value, traceback);
3645 if (arg == NULL) {
3646 PyErr_Restore(type, value, traceback);
3647 return;
3648 }
3649 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3650 Py_DECREF(arg);
3651 if (err == 0)
3652 PyErr_Restore(type, value, traceback);
3653 else {
3654 Py_XDECREF(type);
3655 Py_XDECREF(value);
3656 Py_XDECREF(traceback);
3657 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003658}
3659
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003660static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003661call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003662 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003663{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003664 PyObject *type, *value, *traceback;
3665 int err;
3666 PyErr_Fetch(&type, &value, &traceback);
3667 err = call_trace(func, obj, frame, what, arg);
3668 if (err == 0)
3669 {
3670 PyErr_Restore(type, value, traceback);
3671 return 0;
3672 }
3673 else {
3674 Py_XDECREF(type);
3675 Py_XDECREF(value);
3676 Py_XDECREF(traceback);
3677 return -1;
3678 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003679}
3680
Fredrik Lundh7a830892006-05-27 10:39:48 +00003681static int
Fred Drake5755ce62001-06-27 19:19:46 +00003682call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003683 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003684{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003685 register PyThreadState *tstate = frame->f_tstate;
3686 int result;
3687 if (tstate->tracing)
3688 return 0;
3689 tstate->tracing++;
3690 tstate->use_tracing = 0;
3691 result = func(obj, frame, what, arg);
3692 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3693 || (tstate->c_profilefunc != NULL));
3694 tstate->tracing--;
3695 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003696}
3697
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003698PyObject *
3699_PyEval_CallTracing(PyObject *func, PyObject *args)
3700{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003701 PyFrameObject *frame = PyEval_GetFrame();
3702 PyThreadState *tstate = frame->f_tstate;
3703 int save_tracing = tstate->tracing;
3704 int save_use_tracing = tstate->use_tracing;
3705 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003706
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003707 tstate->tracing = 0;
3708 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3709 || (tstate->c_profilefunc != NULL));
3710 result = PyObject_Call(func, args, NULL);
3711 tstate->tracing = save_tracing;
3712 tstate->use_tracing = save_use_tracing;
3713 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003714}
3715
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003716/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003717static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003718maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003719 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3720 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003721{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003722 int result = 0;
3723 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003725 /* If the last instruction executed isn't in the current
3726 instruction window, reset the window.
3727 */
3728 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3729 PyAddrPair bounds;
3730 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3731 &bounds);
3732 *instr_lb = bounds.ap_lower;
3733 *instr_ub = bounds.ap_upper;
3734 }
3735 /* If the last instruction falls at the start of a line or if
3736 it represents a jump backwards, update the frame's line
3737 number and call the trace function. */
3738 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3739 frame->f_lineno = line;
3740 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3741 }
3742 *instr_prev = frame->f_lasti;
3743 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003744}
3745
Fred Drake5755ce62001-06-27 19:19:46 +00003746void
3747PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003748{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003749 PyThreadState *tstate = PyThreadState_GET();
3750 PyObject *temp = tstate->c_profileobj;
3751 Py_XINCREF(arg);
3752 tstate->c_profilefunc = NULL;
3753 tstate->c_profileobj = NULL;
3754 /* Must make sure that tracing is not ignored if 'temp' is freed */
3755 tstate->use_tracing = tstate->c_tracefunc != NULL;
3756 Py_XDECREF(temp);
3757 tstate->c_profilefunc = func;
3758 tstate->c_profileobj = arg;
3759 /* Flag that tracing or profiling is turned on */
3760 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003761}
3762
3763void
3764PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3765{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003766 PyThreadState *tstate = PyThreadState_GET();
3767 PyObject *temp = tstate->c_traceobj;
3768 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3769 Py_XINCREF(arg);
3770 tstate->c_tracefunc = NULL;
3771 tstate->c_traceobj = NULL;
3772 /* Must make sure that profiling is not ignored if 'temp' is freed */
3773 tstate->use_tracing = tstate->c_profilefunc != NULL;
3774 Py_XDECREF(temp);
3775 tstate->c_tracefunc = func;
3776 tstate->c_traceobj = arg;
3777 /* Flag that tracing or profiling is turned on */
3778 tstate->use_tracing = ((func != NULL)
3779 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003780}
3781
Guido van Rossumb209a111997-04-29 18:18:01 +00003782PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003783PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003784{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003785 PyFrameObject *current_frame = PyEval_GetFrame();
3786 if (current_frame == NULL)
3787 return PyThreadState_GET()->interp->builtins;
3788 else
3789 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003790}
3791
Guido van Rossumb209a111997-04-29 18:18:01 +00003792PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003793PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003794{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003795 PyFrameObject *current_frame = PyEval_GetFrame();
3796 if (current_frame == NULL)
3797 return NULL;
3798 PyFrame_FastToLocals(current_frame);
3799 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003800}
3801
Guido van Rossumb209a111997-04-29 18:18:01 +00003802PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003803PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003804{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003805 PyFrameObject *current_frame = PyEval_GetFrame();
3806 if (current_frame == NULL)
3807 return NULL;
3808 else
3809 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003810}
3811
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003812PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003813PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003814{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003815 PyThreadState *tstate = PyThreadState_GET();
3816 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003817}
3818
Guido van Rossum6135a871995-01-09 17:53:26 +00003819int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003820PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003821{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003822 PyFrameObject *current_frame = PyEval_GetFrame();
3823 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003824}
3825
Guido van Rossumbe270261997-05-22 22:26:18 +00003826int
Tim Peters5ba58662001-07-16 02:29:45 +00003827PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003828{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003829 PyFrameObject *current_frame = PyEval_GetFrame();
3830 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003831
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003832 if (current_frame != NULL) {
3833 const int codeflags = current_frame->f_code->co_flags;
3834 const int compilerflags = codeflags & PyCF_MASK;
3835 if (compilerflags) {
3836 result = 1;
3837 cf->cf_flags |= compilerflags;
3838 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003839#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003840 if (codeflags & CO_GENERATOR_ALLOWED) {
3841 result = 1;
3842 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3843 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003844#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003845 }
3846 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003847}
3848
3849int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003850Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003851{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003852 PyObject *f = PySys_GetObject("stdout");
3853 if (f == NULL)
3854 return 0;
3855 if (!PyFile_SoftSpace(f, 0))
3856 return 0;
3857 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003858}
3859
Guido van Rossum3f5da241990-12-20 15:06:42 +00003860
Guido van Rossum681d79a1995-07-18 14:51:37 +00003861/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00003862 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003863
Guido van Rossumb209a111997-04-29 18:18:01 +00003864PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003865PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003866{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003867 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003868
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003869 if (arg == NULL) {
3870 arg = PyTuple_New(0);
3871 if (arg == NULL)
3872 return NULL;
3873 }
3874 else if (!PyTuple_Check(arg)) {
3875 PyErr_SetString(PyExc_TypeError,
3876 "argument list must be a tuple");
3877 return NULL;
3878 }
3879 else
3880 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003881
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003882 if (kw != NULL && !PyDict_Check(kw)) {
3883 PyErr_SetString(PyExc_TypeError,
3884 "keyword list must be a dictionary");
3885 Py_DECREF(arg);
3886 return NULL;
3887 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003889 result = PyObject_Call(func, arg, kw);
3890 Py_DECREF(arg);
3891 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003892}
3893
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003894const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003895PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003896{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003897 if (PyMethod_Check(func))
3898 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3899 else if (PyFunction_Check(func))
3900 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3901 else if (PyCFunction_Check(func))
3902 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3903 else if (PyClass_Check(func))
3904 return PyString_AsString(((PyClassObject*)func)->cl_name);
3905 else if (PyInstance_Check(func)) {
3906 return PyString_AsString(
3907 ((PyInstanceObject*)func)->in_class->cl_name);
3908 } else {
3909 return func->ob_type->tp_name;
3910 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003911}
3912
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003913const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003914PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003915{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003916 if (PyMethod_Check(func))
3917 return "()";
3918 else if (PyFunction_Check(func))
3919 return "()";
3920 else if (PyCFunction_Check(func))
3921 return "()";
3922 else if (PyClass_Check(func))
3923 return " constructor";
3924 else if (PyInstance_Check(func)) {
3925 return " instance";
3926 } else {
3927 return " object";
3928 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003929}
3930
Fredrik Lundh7a830892006-05-27 10:39:48 +00003931static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003932err_args(PyObject *func, int flags, int nargs)
3933{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003934 if (flags & METH_NOARGS)
3935 PyErr_Format(PyExc_TypeError,
3936 "%.200s() takes no arguments (%d given)",
3937 ((PyCFunctionObject *)func)->m_ml->ml_name,
3938 nargs);
3939 else
3940 PyErr_Format(PyExc_TypeError,
3941 "%.200s() takes exactly one argument (%d given)",
3942 ((PyCFunctionObject *)func)->m_ml->ml_name,
3943 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003944}
3945
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003946#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003947if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003948 if (call_trace(tstate->c_profilefunc, \
3949 tstate->c_profileobj, \
3950 tstate->frame, PyTrace_C_CALL, \
3951 func)) { \
3952 x = NULL; \
3953 } \
3954 else { \
3955 x = call; \
3956 if (tstate->c_profilefunc != NULL) { \
3957 if (x == NULL) { \
3958 call_trace_protected(tstate->c_profilefunc, \
3959 tstate->c_profileobj, \
3960 tstate->frame, PyTrace_C_EXCEPTION, \
3961 func); \
3962 /* XXX should pass (type, value, tb) */ \
3963 } else { \
3964 if (call_trace(tstate->c_profilefunc, \
3965 tstate->c_profileobj, \
3966 tstate->frame, PyTrace_C_RETURN, \
3967 func)) { \
3968 Py_DECREF(x); \
3969 x = NULL; \
3970 } \
3971 } \
3972 } \
3973 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003974} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003975 x = call; \
3976 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00003977
Fredrik Lundh7a830892006-05-27 10:39:48 +00003978static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003979call_function(PyObject ***pp_stack, int oparg
3980#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003981 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003982#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003983 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003984{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003985 int na = oparg & 0xff;
3986 int nk = (oparg>>8) & 0xff;
3987 int n = na + 2 * nk;
3988 PyObject **pfunc = (*pp_stack) - n - 1;
3989 PyObject *func = *pfunc;
3990 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00003991
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003992 /* Always dispatch PyCFunction first, because these are
3993 presumed to be the most frequent callable object.
3994 */
3995 if (PyCFunction_Check(func) && nk == 0) {
3996 int flags = PyCFunction_GET_FLAGS(func);
3997 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00003998
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003999 PCALL(PCALL_CFUNCTION);
4000 if (flags & (METH_NOARGS | METH_O)) {
4001 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4002 PyObject *self = PyCFunction_GET_SELF(func);
4003 if (flags & METH_NOARGS && na == 0) {
4004 C_TRACE(x, (*meth)(self,NULL));
4005 }
4006 else if (flags & METH_O && na == 1) {
4007 PyObject *arg = EXT_POP(*pp_stack);
4008 C_TRACE(x, (*meth)(self,arg));
4009 Py_DECREF(arg);
4010 }
4011 else {
4012 err_args(func, flags, na);
4013 x = NULL;
4014 }
4015 }
4016 else {
4017 PyObject *callargs;
4018 callargs = load_args(pp_stack, na);
4019 READ_TIMESTAMP(*pintr0);
4020 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4021 READ_TIMESTAMP(*pintr1);
4022 Py_XDECREF(callargs);
4023 }
4024 } else {
4025 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4026 /* optimize access to bound methods */
4027 PyObject *self = PyMethod_GET_SELF(func);
4028 PCALL(PCALL_METHOD);
4029 PCALL(PCALL_BOUND_METHOD);
4030 Py_INCREF(self);
4031 func = PyMethod_GET_FUNCTION(func);
4032 Py_INCREF(func);
4033 Py_DECREF(*pfunc);
4034 *pfunc = self;
4035 na++;
4036 n++;
4037 } else
4038 Py_INCREF(func);
4039 READ_TIMESTAMP(*pintr0);
4040 if (PyFunction_Check(func))
4041 x = fast_function(func, pp_stack, n, na, nk);
4042 else
4043 x = do_call(func, pp_stack, na, nk);
4044 READ_TIMESTAMP(*pintr1);
4045 Py_DECREF(func);
4046 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004047
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004048 /* Clear the stack of the function object. Also removes
4049 the arguments in case they weren't consumed already
4050 (fast_function() and err_args() leave them on the stack).
4051 */
4052 while ((*pp_stack) > pfunc) {
4053 w = EXT_POP(*pp_stack);
4054 Py_DECREF(w);
4055 PCALL(PCALL_POP);
4056 }
4057 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004058}
4059
Jeremy Hylton192690e2002-08-16 18:36:11 +00004060/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004061 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004062 For the simplest case -- a function that takes only positional
4063 arguments and is called with only positional arguments -- it
4064 inlines the most primitive frame setup code from
4065 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4066 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004067*/
4068
Fredrik Lundh7a830892006-05-27 10:39:48 +00004069static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004070fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004071{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004072 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4073 PyObject *globals = PyFunction_GET_GLOBALS(func);
4074 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4075 PyObject **d = NULL;
4076 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004078 PCALL(PCALL_FUNCTION);
4079 PCALL(PCALL_FAST_FUNCTION);
4080 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4081 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4082 PyFrameObject *f;
4083 PyObject *retval = NULL;
4084 PyThreadState *tstate = PyThreadState_GET();
4085 PyObject **fastlocals, **stack;
4086 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004087
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004088 PCALL(PCALL_FASTER_FUNCTION);
4089 assert(globals != NULL);
4090 /* XXX Perhaps we should create a specialized
4091 PyFrame_New() that doesn't take locals, but does
4092 take builtins without sanity checking them.
4093 */
4094 assert(tstate != NULL);
4095 f = PyFrame_New(tstate, co, globals, NULL);
4096 if (f == NULL)
4097 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004098
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004099 fastlocals = f->f_localsplus;
4100 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004101
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004102 for (i = 0; i < n; i++) {
4103 Py_INCREF(*stack);
4104 fastlocals[i] = *stack++;
4105 }
4106 retval = PyEval_EvalFrameEx(f,0);
4107 ++tstate->recursion_depth;
4108 Py_DECREF(f);
4109 --tstate->recursion_depth;
4110 return retval;
4111 }
4112 if (argdefs != NULL) {
4113 d = &PyTuple_GET_ITEM(argdefs, 0);
4114 nd = Py_SIZE(argdefs);
4115 }
4116 return PyEval_EvalCodeEx(co, globals,
4117 (PyObject *)NULL, (*pp_stack)-n, na,
4118 (*pp_stack)-2*nk, nk, d, nd,
4119 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004120}
4121
Fredrik Lundh7a830892006-05-27 10:39:48 +00004122static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004123update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4124 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004125{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004126 PyObject *kwdict = NULL;
4127 if (orig_kwdict == NULL)
4128 kwdict = PyDict_New();
4129 else {
4130 kwdict = PyDict_Copy(orig_kwdict);
4131 Py_DECREF(orig_kwdict);
4132 }
4133 if (kwdict == NULL)
4134 return NULL;
4135 while (--nk >= 0) {
4136 int err;
4137 PyObject *value = EXT_POP(*pp_stack);
4138 PyObject *key = EXT_POP(*pp_stack);
4139 if (PyDict_GetItem(kwdict, key) != NULL) {
4140 PyErr_Format(PyExc_TypeError,
4141 "%.200s%s got multiple values "
4142 "for keyword argument '%.200s'",
4143 PyEval_GetFuncName(func),
4144 PyEval_GetFuncDesc(func),
4145 PyString_AsString(key));
4146 Py_DECREF(key);
4147 Py_DECREF(value);
4148 Py_DECREF(kwdict);
4149 return NULL;
4150 }
4151 err = PyDict_SetItem(kwdict, key, value);
4152 Py_DECREF(key);
4153 Py_DECREF(value);
4154 if (err) {
4155 Py_DECREF(kwdict);
4156 return NULL;
4157 }
4158 }
4159 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004160}
4161
Fredrik Lundh7a830892006-05-27 10:39:48 +00004162static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004163update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004164 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004165{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004166 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004167
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004168 callargs = PyTuple_New(nstack + nstar);
4169 if (callargs == NULL) {
4170 return NULL;
4171 }
4172 if (nstar) {
4173 int i;
4174 for (i = 0; i < nstar; i++) {
4175 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4176 Py_INCREF(a);
4177 PyTuple_SET_ITEM(callargs, nstack + i, a);
4178 }
4179 }
4180 while (--nstack >= 0) {
4181 w = EXT_POP(*pp_stack);
4182 PyTuple_SET_ITEM(callargs, nstack, w);
4183 }
4184 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004185}
4186
Fredrik Lundh7a830892006-05-27 10:39:48 +00004187static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004188load_args(PyObject ***pp_stack, int na)
4189{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004190 PyObject *args = PyTuple_New(na);
4191 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004192
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004193 if (args == NULL)
4194 return NULL;
4195 while (--na >= 0) {
4196 w = EXT_POP(*pp_stack);
4197 PyTuple_SET_ITEM(args, na, w);
4198 }
4199 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004200}
4201
Fredrik Lundh7a830892006-05-27 10:39:48 +00004202static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004203do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4204{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004205 PyObject *callargs = NULL;
4206 PyObject *kwdict = NULL;
4207 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004208
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004209 if (nk > 0) {
4210 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4211 if (kwdict == NULL)
4212 goto call_fail;
4213 }
4214 callargs = load_args(pp_stack, na);
4215 if (callargs == NULL)
4216 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004217#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004218 /* At this point, we have to look at the type of func to
4219 update the call stats properly. Do it here so as to avoid
4220 exposing the call stats machinery outside ceval.c
4221 */
4222 if (PyFunction_Check(func))
4223 PCALL(PCALL_FUNCTION);
4224 else if (PyMethod_Check(func))
4225 PCALL(PCALL_METHOD);
4226 else if (PyType_Check(func))
4227 PCALL(PCALL_TYPE);
4228 else if (PyCFunction_Check(func))
4229 PCALL(PCALL_CFUNCTION);
4230 else
4231 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004232#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004233 if (PyCFunction_Check(func)) {
4234 PyThreadState *tstate = PyThreadState_GET();
4235 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4236 }
4237 else
4238 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004239 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004240 Py_XDECREF(callargs);
4241 Py_XDECREF(kwdict);
4242 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004243}
4244
Fredrik Lundh7a830892006-05-27 10:39:48 +00004245static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004246ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4247{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004248 int nstar = 0;
4249 PyObject *callargs = NULL;
4250 PyObject *stararg = NULL;
4251 PyObject *kwdict = NULL;
4252 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004253
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004254 if (flags & CALL_FLAG_KW) {
4255 kwdict = EXT_POP(*pp_stack);
4256 if (!PyDict_Check(kwdict)) {
4257 PyObject *d;
4258 d = PyDict_New();
4259 if (d == NULL)
4260 goto ext_call_fail;
4261 if (PyDict_Update(d, kwdict) != 0) {
4262 Py_DECREF(d);
4263 /* PyDict_Update raises attribute
4264 * error (percolated from an attempt
4265 * to get 'keys' attribute) instead of
4266 * a type error if its second argument
4267 * is not a mapping.
4268 */
4269 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4270 PyErr_Format(PyExc_TypeError,
4271 "%.200s%.200s argument after ** "
4272 "must be a mapping, not %.200s",
4273 PyEval_GetFuncName(func),
4274 PyEval_GetFuncDesc(func),
4275 kwdict->ob_type->tp_name);
4276 }
4277 goto ext_call_fail;
4278 }
4279 Py_DECREF(kwdict);
4280 kwdict = d;
4281 }
4282 }
4283 if (flags & CALL_FLAG_VAR) {
4284 stararg = EXT_POP(*pp_stack);
4285 if (!PyTuple_Check(stararg)) {
4286 PyObject *t = NULL;
4287 t = PySequence_Tuple(stararg);
4288 if (t == NULL) {
4289 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4290 PyErr_Format(PyExc_TypeError,
4291 "%.200s%.200s argument after * "
4292 "must be a sequence, not %200s",
4293 PyEval_GetFuncName(func),
4294 PyEval_GetFuncDesc(func),
4295 stararg->ob_type->tp_name);
4296 }
4297 goto ext_call_fail;
4298 }
4299 Py_DECREF(stararg);
4300 stararg = t;
4301 }
4302 nstar = PyTuple_GET_SIZE(stararg);
4303 }
4304 if (nk > 0) {
4305 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4306 if (kwdict == NULL)
4307 goto ext_call_fail;
4308 }
4309 callargs = update_star_args(na, nstar, stararg, pp_stack);
4310 if (callargs == NULL)
4311 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004312#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004313 /* At this point, we have to look at the type of func to
4314 update the call stats properly. Do it here so as to avoid
4315 exposing the call stats machinery outside ceval.c
4316 */
4317 if (PyFunction_Check(func))
4318 PCALL(PCALL_FUNCTION);
4319 else if (PyMethod_Check(func))
4320 PCALL(PCALL_METHOD);
4321 else if (PyType_Check(func))
4322 PCALL(PCALL_TYPE);
4323 else if (PyCFunction_Check(func))
4324 PCALL(PCALL_CFUNCTION);
4325 else
4326 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004327#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004328 if (PyCFunction_Check(func)) {
4329 PyThreadState *tstate = PyThreadState_GET();
4330 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4331 }
4332 else
4333 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004334ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004335 Py_XDECREF(callargs);
4336 Py_XDECREF(kwdict);
4337 Py_XDECREF(stararg);
4338 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004339}
4340
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004341/* Extract a slice index from a PyInt or PyLong or an object with the
4342 nb_index slot defined, and store in *pi.
4343 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4344 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 +00004345 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004346*/
Tim Petersb5196382001-12-16 19:44:20 +00004347/* Note: If v is NULL, return success without storing into *pi. This
4348 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4349 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004350*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004351int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004352_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004353{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004354 if (v != NULL) {
4355 Py_ssize_t x;
4356 if (PyInt_Check(v)) {
4357 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4358 however, it looks like it should be AsSsize_t.
4359 There should be a comment here explaining why.
4360 */
4361 x = PyInt_AS_LONG(v);
4362 }
4363 else if (PyIndex_Check(v)) {
4364 x = PyNumber_AsSsize_t(v, NULL);
4365 if (x == -1 && PyErr_Occurred())
4366 return 0;
4367 }
4368 else {
4369 PyErr_SetString(PyExc_TypeError,
4370 "slice indices must be integers or "
4371 "None or have an __index__ method");
4372 return 0;
4373 }
4374 *pi = x;
4375 }
4376 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004377}
4378
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004379#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004380#define ISINDEX(x) ((x) == NULL || \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004381 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004382
Fredrik Lundh7a830892006-05-27 10:39:48 +00004383static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004384apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004385{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004386 PyTypeObject *tp = u->ob_type;
4387 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004388
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004389 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4390 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4391 if (!_PyEval_SliceIndex(v, &ilow))
4392 return NULL;
4393 if (!_PyEval_SliceIndex(w, &ihigh))
4394 return NULL;
4395 return PySequence_GetSlice(u, ilow, ihigh);
4396 }
4397 else {
4398 PyObject *slice = PySlice_New(v, w, NULL);
4399 if (slice != NULL) {
4400 PyObject *res = PyObject_GetItem(u, slice);
4401 Py_DECREF(slice);
4402 return res;
4403 }
4404 else
4405 return NULL;
4406 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004407}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004408
Fredrik Lundh7a830892006-05-27 10:39:48 +00004409static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004410assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004411 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004412{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004413 PyTypeObject *tp = u->ob_type;
4414 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004415
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004416 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4417 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4418 if (!_PyEval_SliceIndex(v, &ilow))
4419 return -1;
4420 if (!_PyEval_SliceIndex(w, &ihigh))
4421 return -1;
4422 if (x == NULL)
4423 return PySequence_DelSlice(u, ilow, ihigh);
4424 else
4425 return PySequence_SetSlice(u, ilow, ihigh, x);
4426 }
4427 else {
4428 PyObject *slice = PySlice_New(v, w, NULL);
4429 if (slice != NULL) {
4430 int res;
4431 if (x != NULL)
4432 res = PyObject_SetItem(u, slice, x);
4433 else
4434 res = PyObject_DelItem(u, slice);
4435 Py_DECREF(slice);
4436 return res;
4437 }
4438 else
4439 return -1;
4440 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004441}
4442
Guido van Rossum04edb522008-03-18 02:49:46 +00004443#define Py3kExceptionClass_Check(x) \
4444 (PyType_Check((x)) && \
4445 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4446
4447#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004448 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004449
Fredrik Lundh7a830892006-05-27 10:39:48 +00004450static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004451cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004452{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004453 int res = 0;
4454 switch (op) {
4455 case PyCmp_IS:
4456 res = (v == w);
4457 break;
4458 case PyCmp_IS_NOT:
4459 res = (v != w);
4460 break;
4461 case PyCmp_IN:
4462 res = PySequence_Contains(w, v);
4463 if (res < 0)
4464 return NULL;
4465 break;
4466 case PyCmp_NOT_IN:
4467 res = PySequence_Contains(w, v);
4468 if (res < 0)
4469 return NULL;
4470 res = !res;
4471 break;
4472 case PyCmp_EXC_MATCH:
4473 if (PyTuple_Check(w)) {
4474 Py_ssize_t i, length;
4475 length = PyTuple_Size(w);
4476 for (i = 0; i < length; i += 1) {
4477 PyObject *exc = PyTuple_GET_ITEM(w, i);
4478 if (PyString_Check(exc)) {
4479 int ret_val;
4480 ret_val = PyErr_WarnEx(
4481 PyExc_DeprecationWarning,
4482 "catching of string "
4483 "exceptions is deprecated", 1);
4484 if (ret_val < 0)
4485 return NULL;
4486 }
4487 else if (Py_Py3kWarningFlag &&
4488 !PyTuple_Check(exc) &&
4489 !Py3kExceptionClass_Check(exc))
4490 {
4491 int ret_val;
4492 ret_val = PyErr_WarnEx(
4493 PyExc_DeprecationWarning,
4494 CANNOT_CATCH_MSG, 1);
4495 if (ret_val < 0)
4496 return NULL;
4497 }
4498 }
4499 }
4500 else {
4501 if (PyString_Check(w)) {
4502 int ret_val;
4503 ret_val = PyErr_WarnEx(
4504 PyExc_DeprecationWarning,
4505 "catching of string "
4506 "exceptions is deprecated", 1);
4507 if (ret_val < 0)
4508 return NULL;
4509 }
4510 else if (Py_Py3kWarningFlag &&
4511 !PyTuple_Check(w) &&
4512 !Py3kExceptionClass_Check(w))
4513 {
4514 int ret_val;
4515 ret_val = PyErr_WarnEx(
4516 PyExc_DeprecationWarning,
4517 CANNOT_CATCH_MSG, 1);
4518 if (ret_val < 0)
4519 return NULL;
4520 }
4521 }
4522 res = PyErr_GivenExceptionMatches(v, w);
4523 break;
4524 default:
4525 return PyObject_RichCompare(v, w, op);
4526 }
4527 v = res ? Py_True : Py_False;
4528 Py_INCREF(v);
4529 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004530}
4531
Fredrik Lundh7a830892006-05-27 10:39:48 +00004532static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004533import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004534{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004535 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004536
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004537 x = PyObject_GetAttr(v, name);
4538 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4539 PyErr_Format(PyExc_ImportError,
4540 "cannot import name %.230s",
4541 PyString_AsString(name));
4542 }
4543 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004544}
Guido van Rossumac7be682001-01-17 15:42:30 +00004545
Fredrik Lundh7a830892006-05-27 10:39:48 +00004546static int
Thomas Wouters52152252000-08-17 22:55:00 +00004547import_all_from(PyObject *locals, PyObject *v)
4548{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004549 PyObject *all = PyObject_GetAttrString(v, "__all__");
4550 PyObject *dict, *name, *value;
4551 int skip_leading_underscores = 0;
4552 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004553
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004554 if (all == NULL) {
4555 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4556 return -1; /* Unexpected error */
4557 PyErr_Clear();
4558 dict = PyObject_GetAttrString(v, "__dict__");
4559 if (dict == NULL) {
4560 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4561 return -1;
4562 PyErr_SetString(PyExc_ImportError,
4563 "from-import-* object has no __dict__ and no __all__");
4564 return -1;
4565 }
4566 all = PyMapping_Keys(dict);
4567 Py_DECREF(dict);
4568 if (all == NULL)
4569 return -1;
4570 skip_leading_underscores = 1;
4571 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004572
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004573 for (pos = 0, err = 0; ; pos++) {
4574 name = PySequence_GetItem(all, pos);
4575 if (name == NULL) {
4576 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4577 err = -1;
4578 else
4579 PyErr_Clear();
4580 break;
4581 }
4582 if (skip_leading_underscores &&
4583 PyString_Check(name) &&
4584 PyString_AS_STRING(name)[0] == '_')
4585 {
4586 Py_DECREF(name);
4587 continue;
4588 }
4589 value = PyObject_GetAttr(v, name);
4590 if (value == NULL)
4591 err = -1;
4592 else if (PyDict_CheckExact(locals))
4593 err = PyDict_SetItem(locals, name, value);
4594 else
4595 err = PyObject_SetItem(locals, name, value);
4596 Py_DECREF(name);
4597 Py_XDECREF(value);
4598 if (err != 0)
4599 break;
4600 }
4601 Py_DECREF(all);
4602 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004603}
4604
Fredrik Lundh7a830892006-05-27 10:39:48 +00004605static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004606build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004607{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004608 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004609
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004610 if (PyDict_Check(methods))
4611 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4612 if (metaclass != NULL)
4613 Py_INCREF(metaclass);
4614 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4615 base = PyTuple_GET_ITEM(bases, 0);
4616 metaclass = PyObject_GetAttrString(base, "__class__");
4617 if (metaclass == NULL) {
4618 PyErr_Clear();
4619 metaclass = (PyObject *)base->ob_type;
4620 Py_INCREF(metaclass);
4621 }
4622 }
4623 else {
4624 PyObject *g = PyEval_GetGlobals();
4625 if (g != NULL && PyDict_Check(g))
4626 metaclass = PyDict_GetItemString(g, "__metaclass__");
4627 if (metaclass == NULL)
4628 metaclass = (PyObject *) &PyClass_Type;
4629 Py_INCREF(metaclass);
4630 }
4631 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4632 NULL);
4633 Py_DECREF(metaclass);
4634 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4635 /* A type error here likely means that the user passed
4636 in a base that was not a class (such the random module
4637 instead of the random.random type). Help them out with
4638 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00004639
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004640 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00004641
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004642 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4643 if (PyString_Check(pvalue)) {
4644 PyObject *newmsg;
4645 newmsg = PyString_FromFormat(
4646 "Error when calling the metaclass bases\n"
4647 " %s",
4648 PyString_AS_STRING(pvalue));
4649 if (newmsg != NULL) {
4650 Py_DECREF(pvalue);
4651 pvalue = newmsg;
4652 }
4653 }
4654 PyErr_Restore(ptype, pvalue, ptraceback);
4655 }
4656 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004657}
4658
Fredrik Lundh7a830892006-05-27 10:39:48 +00004659static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004660exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004661 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004662{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004663 int n;
4664 PyObject *v;
4665 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004666
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004667 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4668 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4669 /* Backward compatibility hack */
4670 globals = PyTuple_GetItem(prog, 1);
4671 if (n == 3)
4672 locals = PyTuple_GetItem(prog, 2);
4673 prog = PyTuple_GetItem(prog, 0);
4674 }
4675 if (globals == Py_None) {
4676 globals = PyEval_GetGlobals();
4677 if (locals == Py_None) {
4678 locals = PyEval_GetLocals();
4679 plain = 1;
4680 }
4681 if (!globals || !locals) {
4682 PyErr_SetString(PyExc_SystemError,
4683 "globals and locals cannot be NULL");
4684 return -1;
4685 }
4686 }
4687 else if (locals == Py_None)
4688 locals = globals;
4689 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004690#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004691 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004692#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004693 !PyCode_Check(prog) &&
4694 !PyFile_Check(prog)) {
4695 PyErr_SetString(PyExc_TypeError,
4696 "exec: arg 1 must be a string, file, or code object");
4697 return -1;
4698 }
4699 if (!PyDict_Check(globals)) {
4700 PyErr_SetString(PyExc_TypeError,
4701 "exec: arg 2 must be a dictionary or None");
4702 return -1;
4703 }
4704 if (!PyMapping_Check(locals)) {
4705 PyErr_SetString(PyExc_TypeError,
4706 "exec: arg 3 must be a mapping or None");
4707 return -1;
4708 }
4709 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4710 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4711 if (PyCode_Check(prog)) {
4712 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4713 PyErr_SetString(PyExc_TypeError,
4714 "code object passed to exec may not contain free variables");
4715 return -1;
4716 }
4717 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4718 }
4719 else if (PyFile_Check(prog)) {
4720 FILE *fp = PyFile_AsFile(prog);
4721 char *name = PyString_AsString(PyFile_Name(prog));
4722 PyCompilerFlags cf;
4723 if (name == NULL)
4724 return -1;
4725 cf.cf_flags = 0;
4726 if (PyEval_MergeCompilerFlags(&cf))
4727 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4728 locals, &cf);
4729 else
4730 v = PyRun_File(fp, name, Py_file_input, globals,
4731 locals);
4732 }
4733 else {
4734 PyObject *tmp = NULL;
4735 char *str;
4736 PyCompilerFlags cf;
4737 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004738#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004739 if (PyUnicode_Check(prog)) {
4740 tmp = PyUnicode_AsUTF8String(prog);
4741 if (tmp == NULL)
4742 return -1;
4743 prog = tmp;
4744 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4745 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004746#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004747 if (PyString_AsStringAndSize(prog, &str, NULL))
4748 return -1;
4749 if (PyEval_MergeCompilerFlags(&cf))
4750 v = PyRun_StringFlags(str, Py_file_input, globals,
4751 locals, &cf);
4752 else
4753 v = PyRun_String(str, Py_file_input, globals, locals);
4754 Py_XDECREF(tmp);
4755 }
4756 if (plain)
4757 PyFrame_LocalsToFast(f, 0);
4758 if (v == NULL)
4759 return -1;
4760 Py_DECREF(v);
4761 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004762}
Guido van Rossum24c13741995-02-14 09:42:43 +00004763
Fredrik Lundh7a830892006-05-27 10:39:48 +00004764static void
Paul Prescode68140d2000-08-30 20:25:01 +00004765format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4766{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004767 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004768
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004769 if (!obj)
4770 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004771
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004772 obj_str = PyString_AsString(obj);
4773 if (!obj_str)
4774 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004775
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004776 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004777}
Guido van Rossum950361c1997-01-24 13:49:28 +00004778
Fredrik Lundh7a830892006-05-27 10:39:48 +00004779static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004780string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004781 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004782{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004783 /* This function implements 'variable += expr' when both arguments
4784 are strings. */
4785 Py_ssize_t v_len = PyString_GET_SIZE(v);
4786 Py_ssize_t w_len = PyString_GET_SIZE(w);
4787 Py_ssize_t new_len = v_len + w_len;
4788 if (new_len < 0) {
4789 PyErr_SetString(PyExc_OverflowError,
4790 "strings are too large to concat");
4791 return NULL;
4792 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004793
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004794 if (v->ob_refcnt == 2) {
4795 /* In the common case, there are 2 references to the value
4796 * stored in 'variable' when the += is performed: one on the
4797 * value stack (in 'v') and one still stored in the
4798 * 'variable'. We try to delete the variable now to reduce
4799 * the refcnt to 1.
4800 */
4801 switch (*next_instr) {
4802 case STORE_FAST:
4803 {
4804 int oparg = PEEKARG();
4805 PyObject **fastlocals = f->f_localsplus;
4806 if (GETLOCAL(oparg) == v)
4807 SETLOCAL(oparg, NULL);
4808 break;
4809 }
4810 case STORE_DEREF:
4811 {
4812 PyObject **freevars = (f->f_localsplus +
4813 f->f_code->co_nlocals);
4814 PyObject *c = freevars[PEEKARG()];
4815 if (PyCell_GET(c) == v)
4816 PyCell_Set(c, NULL);
4817 break;
4818 }
4819 case STORE_NAME:
4820 {
4821 PyObject *names = f->f_code->co_names;
4822 PyObject *name = GETITEM(names, PEEKARG());
4823 PyObject *locals = f->f_locals;
4824 if (PyDict_CheckExact(locals) &&
4825 PyDict_GetItem(locals, name) == v) {
4826 if (PyDict_DelItem(locals, name) != 0) {
4827 PyErr_Clear();
4828 }
4829 }
4830 break;
4831 }
4832 }
4833 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004834
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004835 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4836 /* Now we own the last reference to 'v', so we can resize it
4837 * in-place.
4838 */
4839 if (_PyString_Resize(&v, new_len) != 0) {
4840 /* XXX if _PyString_Resize() fails, 'v' has been
4841 * deallocated so it cannot be put back into
4842 * 'variable'. The MemoryError is raised when there
4843 * is no value in 'variable', which might (very
4844 * remotely) be a cause of incompatibilities.
4845 */
4846 return NULL;
4847 }
4848 /* copy 'w' into the newly allocated area of 'v' */
4849 memcpy(PyString_AS_STRING(v) + v_len,
4850 PyString_AS_STRING(w), w_len);
4851 return v;
4852 }
4853 else {
4854 /* When in-place resizing is not an option. */
4855 PyString_Concat(&v, w);
4856 return v;
4857 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004858}
4859
Guido van Rossum950361c1997-01-24 13:49:28 +00004860#ifdef DYNAMIC_EXECUTION_PROFILE
4861
Fredrik Lundh7a830892006-05-27 10:39:48 +00004862static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004863getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004864{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004865 int i;
4866 PyObject *l = PyList_New(256);
4867 if (l == NULL) return NULL;
4868 for (i = 0; i < 256; i++) {
4869 PyObject *x = PyInt_FromLong(a[i]);
4870 if (x == NULL) {
4871 Py_DECREF(l);
4872 return NULL;
4873 }
4874 PyList_SetItem(l, i, x);
4875 }
4876 for (i = 0; i < 256; i++)
4877 a[i] = 0;
4878 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004879}
4880
4881PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004882_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004883{
4884#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004885 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004886#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004887 int i;
4888 PyObject *l = PyList_New(257);
4889 if (l == NULL) return NULL;
4890 for (i = 0; i < 257; i++) {
4891 PyObject *x = getarray(dxpairs[i]);
4892 if (x == NULL) {
4893 Py_DECREF(l);
4894 return NULL;
4895 }
4896 PyList_SetItem(l, i, x);
4897 }
4898 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004899#endif
4900}
4901
4902#endif