blob: dad5e1cdc9606f46a48ce9b536635531722cf3f9 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Tim Peters7df5e7f2006-05-26 23:14:37 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Ezio Melottic2077b02011-03-16 12:34:31 +020030/* PowerPC support.
David Malcolm4c29e1c2011-01-06 17:39:24 +000031 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
32 "__powerpc__" appears to be the correct one for Linux with GCC
33*/
34#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
Michael W. Hudson75eabd22005-01-18 15:56:11 +000036#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000037
Fredrik Lundh7a830892006-05-27 10:39:48 +000038static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000039ppc_getcounter(uint64 *v)
40{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000042
43 loop:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000044 asm volatile ("mftbu %0" : "=r" (tbu) );
45 asm volatile ("mftb %0" : "=r" (tb) );
46 asm volatile ("mftbu %0" : "=r" (tbu2));
47 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000048
Antoine Pitrouc83ea132010-05-09 14:46:46 +000049 /* The slightly peculiar way of writing the next lines is
50 compiled better by GCC than any other way I tried. */
51 ((long*)(v))[0] = tbu;
52 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000053}
54
Mark Dickinson504a1512009-10-31 10:11:28 +000055#elif defined(__i386__)
56
57/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
Michael W. Hudson75eabd22005-01-18 15:56:11 +000059#define READ_TIMESTAMP(val) \
60 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000061
Mark Dickinson504a1512009-10-31 10:11:28 +000062#elif defined(__x86_64__)
63
64/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
65 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
66 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
67 32-bit pieces of the result. */
68
69#define READ_TIMESTAMP(val) \
70 __asm__ __volatile__("rdtsc" : \
71 "=a" (((int*)&(val))[0]), "=d" (((int*)&(val))[1]));
72
73
74#else
75
76#error "Don't know how to implement timestamp counter for this architecture"
77
Michael W. Hudson800ba232004-08-12 18:19:17 +000078#endif
79
Tim Peters7df5e7f2006-05-26 23:14:37 +000080void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000082{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 uint64 intr, inst, loop;
84 PyThreadState *tstate = PyThreadState_Get();
85 if (!tstate->interp->tscdump)
86 return;
87 intr = intr1 - intr0;
88 inst = inst1 - inst0 - intr;
89 loop = loop1 - loop0 - intr;
90 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krah7ff78252010-06-23 18:12:09 +000091 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000092}
Michael W. Hudson800ba232004-08-12 18:19:17 +000093
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000094#endif
95
Guido van Rossum04691fc1992-08-12 15:35:34 +000096/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000097/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000098
Guido van Rossum408027e1996-12-30 16:17:54 +000099#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000100/* For debugging the interpreter: */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000101#define LLTRACE 1 /* Low-level trace feature */
102#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000103#endif
104
Jeremy Hylton52820442001-01-03 23:52:36 +0000105typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000106
Guido van Rossum374a9221991-04-04 10:40:29 +0000107/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000108#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +0000109static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000110#else
Fredrik Lundh7a830892006-05-27 10:39:48 +0000111static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000112#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000113static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
114static PyObject * do_call(PyObject *, PyObject ***, int, int);
115static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +0000116static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000117 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000118static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
119static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000120#define CALL_FLAG_VAR 1
121#define CALL_FLAG_KW 2
122
Guido van Rossum0a066c01992-03-27 17:29:15 +0000123#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000124static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000125static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000126#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000127static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000128 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000129static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000130 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000131static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
132static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000133 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000134
Fredrik Lundh7a830892006-05-27 10:39:48 +0000135static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
136static int assign_slice(PyObject *, PyObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000137 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000138static PyObject * cmp_outcome(int, PyObject *, PyObject *);
139static PyObject * import_from(PyObject *, PyObject *);
140static int import_all_from(PyObject *, PyObject *);
141static PyObject * build_class(PyObject *, PyObject *, PyObject *);
142static int exec_statement(PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000143 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000144static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
145static void reset_exc_info(PyThreadState *);
146static void format_exc_check_arg(PyObject *, char *, PyObject *);
147static PyObject * string_concatenate(PyObject *, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000148 PyFrameObject *, unsigned char *);
Benjamin Petersone18ef192009-01-20 14:21:16 +0000149static PyObject * kwd_as_string(PyObject *);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000150static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000151
Paul Prescode68140d2000-08-30 20:25:01 +0000152#define NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000153 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000154#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000155 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000156#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000158#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 "free variable '%.200s' referenced before assignment" \
160 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000161
Guido van Rossum950361c1997-01-24 13:49:28 +0000162/* Dynamic execution profile */
163#ifdef DYNAMIC_EXECUTION_PROFILE
164#ifdef DXPAIRS
165static long dxpairs[257][256];
166#define dxp dxpairs[256]
167#else
168static long dxp[256];
169#endif
170#endif
171
Jeremy Hylton985eba52003-02-05 23:13:00 +0000172/* Function call profile */
173#ifdef CALL_PROFILE
174#define PCALL_NUM 11
175static int pcall[PCALL_NUM];
176
177#define PCALL_ALL 0
178#define PCALL_FUNCTION 1
179#define PCALL_FAST_FUNCTION 2
180#define PCALL_FASTER_FUNCTION 3
181#define PCALL_METHOD 4
182#define PCALL_BOUND_METHOD 5
183#define PCALL_CFUNCTION 6
184#define PCALL_TYPE 7
185#define PCALL_GENERATOR 8
186#define PCALL_OTHER 9
187#define PCALL_POP 10
188
189/* Notes about the statistics
190
191 PCALL_FAST stats
192
193 FAST_FUNCTION means no argument tuple needs to be created.
194 FASTER_FUNCTION means that the fast-path frame setup code is used.
195
196 If there is a method call where the call can be optimized by changing
197 the argument tuple and calling the function directly, it gets recorded
198 twice.
199
200 As a result, the relationship among the statistics appears to be
201 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
202 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
203 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
204 PCALL_METHOD > PCALL_BOUND_METHOD
205*/
206
207#define PCALL(POS) pcall[POS]++
208
209PyObject *
210PyEval_GetCallStats(PyObject *self)
211{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000212 return Py_BuildValue("iiiiiiiiiii",
213 pcall[0], pcall[1], pcall[2], pcall[3],
214 pcall[4], pcall[5], pcall[6], pcall[7],
215 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000216}
217#else
218#define PCALL(O)
219
220PyObject *
221PyEval_GetCallStats(PyObject *self)
222{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000223 Py_INCREF(Py_None);
224 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000225}
226#endif
227
Tim Peters5ca576e2001-06-18 22:08:13 +0000228
Guido van Rossume59214e1994-08-30 08:01:59 +0000229#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000230
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000231#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000232#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000233#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000234#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000235
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000236static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000237static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000238static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000239
Tim Peters7f468f22004-10-11 02:40:51 +0000240int
241PyEval_ThreadsInitialized(void)
242{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000243 return interpreter_lock != 0;
Tim Peters7f468f22004-10-11 02:40:51 +0000244}
245
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000246void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000247PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000249 if (interpreter_lock)
250 return;
251 interpreter_lock = PyThread_allocate_lock();
252 PyThread_acquire_lock(interpreter_lock, 1);
253 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000254}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000255
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000256void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000257PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000258{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000259 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260}
261
262void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000263PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000264{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000265 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266}
267
268void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000269PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000270{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000271 if (tstate == NULL)
272 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
273 /* Check someone has called PyEval_InitThreads() to create the lock */
274 assert(interpreter_lock);
275 PyThread_acquire_lock(interpreter_lock, 1);
276 if (PyThreadState_Swap(tstate) != NULL)
277 Py_FatalError(
278 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000279}
280
281void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000282PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000283{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000284 if (tstate == NULL)
285 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
286 if (PyThreadState_Swap(NULL) != tstate)
287 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
288 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000289}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000290
291/* This function is called from PyOS_AfterFork to ensure that newly
292 created child processes don't hold locks referring to threads which
293 are not running in the child process. (This could also be done using
294 pthread_atfork mechanism, at least for the pthreads implementation.) */
295
296void
297PyEval_ReInitThreads(void)
298{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000299 PyObject *threading, *result;
300 PyThreadState *tstate;
Jesse Noller5e62ca42008-07-16 20:03:47 +0000301
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000302 if (!interpreter_lock)
303 return;
304 /*XXX Can't use PyThread_free_lock here because it does too
305 much error-checking. Doing this cleanly would require
306 adding a new function to each thread_*.h. Instead, just
307 create a new lock and waste a little bit of memory */
308 interpreter_lock = PyThread_allocate_lock();
309 pending_lock = PyThread_allocate_lock();
310 PyThread_acquire_lock(interpreter_lock, 1);
311 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000312
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000313 /* Update the threading module with the new state.
314 */
315 tstate = PyThreadState_GET();
316 threading = PyMapping_GetItemString(tstate->interp->modules,
317 "threading");
318 if (threading == NULL) {
319 /* threading not imported */
320 PyErr_Clear();
321 return;
322 }
323 result = PyObject_CallMethod(threading, "_after_fork", NULL);
324 if (result == NULL)
325 PyErr_WriteUnraisable(threading);
326 else
327 Py_DECREF(result);
328 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000329}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000330#endif
331
Guido van Rossumff4949e1992-08-05 19:58:53 +0000332/* Functions save_thread and restore_thread are always defined so
333 dynamically loaded modules needn't be compiled separately for use
334 with and without threads: */
335
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000336PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000337PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000338{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000339 PyThreadState *tstate = PyThreadState_Swap(NULL);
340 if (tstate == NULL)
341 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000342#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000343 if (interpreter_lock)
344 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000345#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000346 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347}
348
349void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000350PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000351{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000352 if (tstate == NULL)
353 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000354#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000355 if (interpreter_lock) {
356 int err = errno;
357 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Petersonc0bc4ef2014-06-16 19:39:18 -0700358 /* _Py_Finalizing is protected by the GIL */
359 if (_Py_Finalizing && tstate != _Py_Finalizing) {
360 PyThread_release_lock(interpreter_lock);
361 PyThread_exit_thread();
362 assert(0); /* unreachable */
363 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000364 errno = err;
365 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000366#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000367 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000368}
369
370
Guido van Rossuma9672091994-09-14 13:31:22 +0000371/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
372 signal handlers or Mac I/O completion routines) can schedule calls
373 to a function to be called synchronously.
374 The synchronous function is called with one void* argument.
375 It should return 0 for success or -1 for failure -- failure should
376 be accompanied by an exception.
377
378 If registry succeeds, the registry function returns 0; if it fails
379 (e.g. due to too many pending calls) it returns -1 (without setting
380 an exception condition).
381
382 Note that because registry may occur from within signal handlers,
383 or other asynchronous events, calling malloc() is unsafe!
384
385#ifdef WITH_THREAD
386 Any thread can schedule pending calls, but only the main thread
387 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000388 There is no facility to schedule calls to a particular thread, but
389 that should be easy to change, should that ever be required. In
390 that case, the static variables here should go into the python
391 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000392#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000393*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000394
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000395#ifdef WITH_THREAD
396
397/* The WITH_THREAD implementation is thread-safe. It allows
398 scheduling to be made from any thread, and even from an executing
399 callback.
400 */
401
402#define NPENDINGCALLS 32
403static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000404 int (*func)(void *);
405 void *arg;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000406} pendingcalls[NPENDINGCALLS];
407static int pendingfirst = 0;
408static int pendinglast = 0;
409static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
410static char pendingbusy = 0;
411
412int
413Py_AddPendingCall(int (*func)(void *), void *arg)
414{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000415 int i, j, result=0;
416 PyThread_type_lock lock = pending_lock;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000417
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000418 /* try a few times for the lock. Since this mechanism is used
419 * for signal handling (on the main thread), there is a (slim)
420 * chance that a signal is delivered on the same thread while we
421 * hold the lock during the Py_MakePendingCalls() function.
422 * This avoids a deadlock in that case.
423 * Note that signals can be delivered on any thread. In particular,
424 * on Windows, a SIGINT is delivered on a system-created worker
425 * thread.
426 * We also check for lock being NULL, in the unlikely case that
427 * this function is called before any bytecode evaluation takes place.
428 */
429 if (lock != NULL) {
430 for (i = 0; i<100; i++) {
431 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
432 break;
433 }
434 if (i == 100)
435 return -1;
436 }
437
438 i = pendinglast;
439 j = (i + 1) % NPENDINGCALLS;
440 if (j == pendingfirst) {
441 result = -1; /* Queue full */
442 } else {
443 pendingcalls[i].func = func;
444 pendingcalls[i].arg = arg;
445 pendinglast = j;
446 }
447 /* signal main loop */
448 _Py_Ticker = 0;
449 pendingcalls_to_do = 1;
450 if (lock != NULL)
451 PyThread_release_lock(lock);
452 return result;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000453}
454
455int
456Py_MakePendingCalls(void)
457{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000458 int i;
459 int r = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000460
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000461 if (!pending_lock) {
462 /* initial allocation of the lock */
463 pending_lock = PyThread_allocate_lock();
464 if (pending_lock == NULL)
465 return -1;
466 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000467
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000468 /* only service pending calls on main thread */
469 if (main_thread && PyThread_get_thread_ident() != main_thread)
470 return 0;
471 /* don't perform recursive pending calls */
472 if (pendingbusy)
473 return 0;
474 pendingbusy = 1;
475 /* perform a bounded number of calls, in case of recursion */
476 for (i=0; i<NPENDINGCALLS; i++) {
477 int j;
478 int (*func)(void *);
479 void *arg = NULL;
480
481 /* pop one item off the queue while holding the lock */
482 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
483 j = pendingfirst;
484 if (j == pendinglast) {
485 func = NULL; /* Queue empty */
486 } else {
487 func = pendingcalls[j].func;
488 arg = pendingcalls[j].arg;
489 pendingfirst = (j + 1) % NPENDINGCALLS;
490 }
491 pendingcalls_to_do = pendingfirst != pendinglast;
492 PyThread_release_lock(pending_lock);
493 /* having released the lock, perform the callback */
494 if (func == NULL)
495 break;
496 r = func(arg);
497 if (r)
498 break;
499 }
500 pendingbusy = 0;
501 return r;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000502}
503
504#else /* if ! defined WITH_THREAD */
505
506/*
507 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
508 This code is used for signal handling in python that isn't built
509 with WITH_THREAD.
510 Don't use this implementation when Py_AddPendingCalls() can happen
511 on a different thread!
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000512
Guido van Rossuma9672091994-09-14 13:31:22 +0000513 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000514 (1) nested asynchronous calls to Py_AddPendingCall()
515 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000516
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000517 (1) is very unlikely because typically signal delivery
518 is blocked during signal handling. So it should be impossible.
519 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000520 The current code is safe against (2), but not against (1).
521 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000522 thread is present, interrupted by signals, and that the critical
523 section is protected with the "busy" variable. On Windows, which
524 delivers SIGINT on a system thread, this does not hold and therefore
525 Windows really shouldn't use this version.
526 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000527*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000528
Guido van Rossuma9672091994-09-14 13:31:22 +0000529#define NPENDINGCALLS 32
530static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000531 int (*func)(void *);
532 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000533} pendingcalls[NPENDINGCALLS];
534static volatile int pendingfirst = 0;
535static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000536static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000537
538int
Thomas Wouters334fb892000-07-25 12:56:38 +0000539Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000540{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000541 static volatile int busy = 0;
542 int i, j;
543 /* XXX Begin critical section */
544 if (busy)
545 return -1;
546 busy = 1;
547 i = pendinglast;
548 j = (i + 1) % NPENDINGCALLS;
549 if (j == pendingfirst) {
550 busy = 0;
551 return -1; /* Queue full */
552 }
553 pendingcalls[i].func = func;
554 pendingcalls[i].arg = arg;
555 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000556
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000557 _Py_Ticker = 0;
558 pendingcalls_to_do = 1; /* Signal main loop */
559 busy = 0;
560 /* XXX End critical section */
561 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000562}
563
Guido van Rossum180d7b41994-09-29 09:45:57 +0000564int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000565Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000566{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000567 static int busy = 0;
568 if (busy)
569 return 0;
570 busy = 1;
571 pendingcalls_to_do = 0;
572 for (;;) {
573 int i;
574 int (*func)(void *);
575 void *arg;
576 i = pendingfirst;
577 if (i == pendinglast)
578 break; /* Queue empty */
579 func = pendingcalls[i].func;
580 arg = pendingcalls[i].arg;
581 pendingfirst = (i + 1) % NPENDINGCALLS;
582 if (func(arg) < 0) {
583 busy = 0;
584 pendingcalls_to_do = 1; /* We're not done yet */
585 return -1;
586 }
587 }
588 busy = 0;
589 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000590}
591
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000592#endif /* WITH_THREAD */
593
Guido van Rossuma9672091994-09-14 13:31:22 +0000594
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000595/* The interpreter's recursion limit */
596
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000597#ifndef Py_DEFAULT_RECURSION_LIMIT
598#define Py_DEFAULT_RECURSION_LIMIT 1000
599#endif
600static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
601int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000602
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000603int
604Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000605{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000606 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000607}
608
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000609void
610Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000611{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000612 recursion_limit = new_limit;
613 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000614}
615
Armin Rigo2b3eb402003-10-28 12:05:48 +0000616/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
617 if the recursion_depth reaches _Py_CheckRecursionLimit.
618 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
619 to guarantee that _Py_CheckRecursiveCall() is regularly called.
620 Without USE_STACKCHECK, there is no need for this. */
621int
622_Py_CheckRecursiveCall(char *where)
623{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000624 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000625
626#ifdef USE_STACKCHECK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000627 if (PyOS_CheckStack()) {
628 --tstate->recursion_depth;
629 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
630 return -1;
631 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000632#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000633 if (tstate->recursion_depth > recursion_limit) {
634 --tstate->recursion_depth;
635 PyErr_Format(PyExc_RuntimeError,
636 "maximum recursion depth exceeded%s",
637 where);
638 return -1;
639 }
640 _Py_CheckRecursionLimit = recursion_limit;
641 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000642}
643
Guido van Rossum374a9221991-04-04 10:40:29 +0000644/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000645enum why_code {
Stefan Krah7ff78252010-06-23 18:12:09 +0000646 WHY_NOT = 0x0001, /* No error */
647 WHY_EXCEPTION = 0x0002, /* Exception occurred */
648 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
649 WHY_RETURN = 0x0008, /* 'return' statement */
650 WHY_BREAK = 0x0010, /* 'break' statement */
651 WHY_CONTINUE = 0x0020, /* 'continue' statement */
652 WHY_YIELD = 0x0040 /* 'yield' operator */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000653};
Guido van Rossum374a9221991-04-04 10:40:29 +0000654
Fredrik Lundh7a830892006-05-27 10:39:48 +0000655static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
656static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000657
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000658/* Records whether tracing is on for any thread. Counts the number of
659 threads for which tstate->c_tracefunc is non-NULL, so if the value
660 is 0, we know we don't have to check this thread's c_tracefunc.
661 This speeds up the if statement in PyEval_EvalFrameEx() after
662 fast_next_opcode*/
663static int _Py_TracingPossible = 0;
664
Skip Montanarod581d772002-09-03 20:10:45 +0000665/* for manipulating the thread switch and periodic "stuff" - used to be
666 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000667int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000668volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000669
Guido van Rossumb209a111997-04-29 18:18:01 +0000670PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000671PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000672{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000673 return PyEval_EvalCodeEx(co,
674 globals, locals,
675 (PyObject **)NULL, 0,
676 (PyObject **)NULL, 0,
677 (PyObject **)NULL, 0,
678 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000679}
680
681
682/* Interpreter main loop */
683
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000684PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000685PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000686 /* This is for backward compatibility with extension modules that
687 used this API; core interpreter code should call
688 PyEval_EvalFrameEx() */
689 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000690}
691
692PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000693PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000694{
Guido van Rossum950361c1997-01-24 13:49:28 +0000695#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000696 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000697#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000698 register PyObject **stack_pointer; /* Next free slot in value stack */
699 register unsigned char *next_instr;
700 register int opcode; /* Current opcode */
701 register int oparg; /* Current opcode argument, if any */
702 register enum why_code why; /* Reason for block stack unwind */
703 register int err; /* Error status -- nonzero if error */
704 register PyObject *x; /* Result object -- NULL if error */
705 register PyObject *v; /* Temporary objects popped off stack */
706 register PyObject *w;
707 register PyObject *u;
708 register PyObject *t;
709 register PyObject *stream = NULL; /* for PRINT opcodes */
710 register PyObject **fastlocals, **freevars;
711 PyObject *retval = NULL; /* Return value */
712 PyThreadState *tstate = PyThreadState_GET();
713 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000714
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000715 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000716
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000717 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000718
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000719 is true when the line being executed has changed. The
720 initial values are such as to make this false the first
721 time it is tested. */
722 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000723
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000724 unsigned char *first_instr;
725 PyObject *names;
726 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000727#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000728 /* Make it easier to find out where we are with a debugger */
729 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000730#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000731
Neal Norwitza81d2202002-07-14 00:27:26 +0000732/* Tuple access macros */
733
734#ifndef Py_DEBUG
735#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
736#else
737#define GETITEM(v, i) PyTuple_GetItem((v), (i))
738#endif
739
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000740#ifdef WITH_TSC
741/* Use Pentium timestamp counter to mark certain events:
742 inst0 -- beginning of switch statement for opcode dispatch
743 inst1 -- end of switch statement (may be skipped)
744 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000745 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000746 (may be skipped)
747 intr1 -- beginning of long interruption
748 intr2 -- end of long interruption
749
750 Many opcodes call out to helper C functions. In some cases, the
751 time in those functions should be counted towards the time for the
752 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
753 calls another Python function; there's no point in charge all the
754 bytecode executed by the called function to the caller.
755
756 It's hard to make a useful judgement statically. In the presence
757 of operator overloading, it's impossible to tell if a call will
758 execute new Python code or not.
759
760 It's a case-by-case judgement. I'll use intr1 for the following
761 cases:
762
763 EXEC_STMT
764 IMPORT_STAR
765 IMPORT_FROM
766 CALL_FUNCTION (and friends)
767
768 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000769 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
770 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000771
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000772 READ_TIMESTAMP(inst0);
773 READ_TIMESTAMP(inst1);
774 READ_TIMESTAMP(loop0);
775 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000776
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000777 /* shut up the compiler */
778 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000779#endif
780
Guido van Rossum374a9221991-04-04 10:40:29 +0000781/* Code access macros */
782
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000783#define INSTR_OFFSET() ((int)(next_instr - first_instr))
784#define NEXTOP() (*next_instr++)
785#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
786#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
787#define JUMPTO(x) (next_instr = first_instr + (x))
788#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000789
Raymond Hettingerf606f872003-03-16 03:11:04 +0000790/* OpCode prediction macros
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000791 Some opcodes tend to come in pairs thus making it possible to
792 predict the second code when the first is run. For example,
793 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
794 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000795
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 Verifying the prediction costs a single high-speed test of a register
797 variable against a constant. If the pairing was good, then the
798 processor's own internal branch predication has a high likelihood of
799 success, resulting in a nearly zero-overhead transition to the
800 next opcode. A successful prediction saves a trip through the eval-loop
801 including its two unpredictable branches, the HAS_ARG test and the
802 switch-case. Combined with the processor's internal branch prediction,
803 a successful PREDICT has the effect of making the two opcodes run as if
804 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000805
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000806 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000807 predictions turned-on and interpret the results as if some opcodes
808 had been combined or turn-off predictions so that the opcode frequency
809 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000810*/
811
Raymond Hettingera7216982004-02-08 19:59:27 +0000812#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000814#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000816#endif
817
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818#define PREDICTED(op) PRED_##op: next_instr++
819#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
Raymond Hettingerf606f872003-03-16 03:11:04 +0000820
Guido van Rossum374a9221991-04-04 10:40:29 +0000821/* Stack manipulation macros */
822
Martin v. Löwis18e16552006-02-15 17:27:45 +0000823/* The stack can grow at most MAXINT deep, as co_nlocals and
824 co_stacksize are ints. */
Stefan Krah7ff78252010-06-23 18:12:09 +0000825#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
826#define EMPTY() (STACK_LEVEL() == 0)
827#define TOP() (stack_pointer[-1])
828#define SECOND() (stack_pointer[-2])
829#define THIRD() (stack_pointer[-3])
830#define FOURTH() (stack_pointer[-4])
831#define PEEK(n) (stack_pointer[-(n)])
832#define SET_TOP(v) (stack_pointer[-1] = (v))
833#define SET_SECOND(v) (stack_pointer[-2] = (v))
834#define SET_THIRD(v) (stack_pointer[-3] = (v))
835#define SET_FOURTH(v) (stack_pointer[-4] = (v))
836#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
837#define BASIC_STACKADJ(n) (stack_pointer += n)
838#define BASIC_PUSH(v) (*stack_pointer++ = (v))
839#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000840
Guido van Rossum96a42c81992-01-12 02:29:51 +0000841#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000842#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000843 lltrace && prtrace(TOP(), "push")); \
844 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000845#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000846 BASIC_POP())
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000847#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000848 lltrace && prtrace(TOP(), "stackadj")); \
849 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000850#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krah7ff78252010-06-23 18:12:09 +0000851 prtrace((STACK_POINTER)[-1], "ext_pop")), \
852 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000853#else
Stefan Krah7ff78252010-06-23 18:12:09 +0000854#define PUSH(v) BASIC_PUSH(v)
855#define POP() BASIC_POP()
856#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000857#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000858#endif
859
Guido van Rossum681d79a1995-07-18 14:51:37 +0000860/* Local variable macros */
861
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000862#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000863
864/* The SETLOCAL() macro must not DECREF the local variable in-place and
865 then store the new value; it must copy the old value to a temporary
866 value, then store the new value, and then DECREF the temporary value.
867 This is because it is possible that during the DECREF the frame is
868 accessed by other code (e.g. a __del__ method or gc.collect()) and the
869 variable would be pointing to already-freed memory. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000870#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krah7ff78252010-06-23 18:12:09 +0000871 GETLOCAL(i) = value; \
872 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000873
Guido van Rossuma027efa1997-05-05 20:56:21 +0000874/* Start of code */
875
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000876 if (f == NULL)
877 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000878
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000879 /* push frame */
880 if (Py_EnterRecursiveCall(""))
881 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000882
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000883 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000884
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000885 if (tstate->use_tracing) {
886 if (tstate->c_tracefunc != NULL) {
887 /* tstate->c_tracefunc, if defined, is a
888 function that will be called on *every* entry
889 to a code block. Its return value, if not
890 None, is a function that will be called at
891 the start of each executed line of code.
892 (Actually, the function must return itself
893 in order to continue tracing.) The trace
894 functions are called with three arguments:
895 a pointer to the current frame, a string
896 indicating why the function is called, and
897 an argument which depends on the situation.
898 The global trace function is also called
899 whenever an exception is detected. */
900 if (call_trace_protected(tstate->c_tracefunc,
901 tstate->c_traceobj,
902 f, PyTrace_CALL, Py_None)) {
903 /* Trace function raised an error */
904 goto exit_eval_frame;
905 }
906 }
907 if (tstate->c_profilefunc != NULL) {
908 /* Similar for c_profilefunc, except it needn't
909 return itself and isn't called for "line" events */
910 if (call_trace_protected(tstate->c_profilefunc,
911 tstate->c_profileobj,
912 f, PyTrace_CALL, Py_None)) {
913 /* Profile function raised an error */
914 goto exit_eval_frame;
915 }
916 }
917 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000918
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000919 co = f->f_code;
920 names = co->co_names;
921 consts = co->co_consts;
922 fastlocals = f->f_localsplus;
923 freevars = f->f_localsplus + co->co_nlocals;
924 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
925 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000926
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000927 f->f_lasti now refers to the index of the last instruction
928 executed. You might think this was obvious from the name, but
929 this wasn't always true before 2.3! PyFrame_New now sets
930 f->f_lasti to -1 (i.e. the index *before* the first instruction)
931 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
932 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +0000933
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000934 When the PREDICT() macros are enabled, some opcode pairs follow in
935 direct succession without updating f->f_lasti. A successful
936 prediction effectively links the two codes together as if they
937 were a single new opcode; accordingly,f->f_lasti will point to
938 the first code in the pair (for instance, GET_ITER followed by
939 FOR_ITER is effectively a single opcode and f->f_lasti will point
940 at to the beginning of the combined pair.)
941 */
942 next_instr = first_instr + f->f_lasti + 1;
943 stack_pointer = f->f_stacktop;
944 assert(stack_pointer != NULL);
945 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000946
Tim Peters5ca576e2001-06-18 22:08:13 +0000947#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000949#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000950#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000951 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +0000952#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000953
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000954 why = WHY_NOT;
955 err = 0;
956 x = Py_None; /* Not a reference, just anything non-NULL */
957 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +0000958
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000959 if (throwflag) { /* support for generator.throw() */
960 why = WHY_EXCEPTION;
961 goto on_error;
962 }
Tim Peters7df5e7f2006-05-26 23:14:37 +0000963
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000964 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000965#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000966 if (inst1 == 0) {
967 /* Almost surely, the opcode executed a break
968 or a continue, preventing inst1 from being set
969 on the way out of the loop.
970 */
971 READ_TIMESTAMP(inst1);
972 loop1 = inst1;
973 }
974 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
975 intr0, intr1);
976 ticked = 0;
977 inst1 = 0;
978 intr0 = 0;
979 intr1 = 0;
980 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000981#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000982 assert(stack_pointer >= f->f_valuestack); /* else underflow */
983 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000984
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000985 /* Do periodic things. Doing this every time through
986 the loop would add too much overhead, so we do it
987 only every Nth instruction. We also do it if
988 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
989 event needs attention (e.g. a signal handler or
990 async I/O handler); see Py_AddPendingCall() and
991 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000992
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000993 if (--_Py_Ticker < 0) {
994 if (*next_instr == SETUP_FINALLY) {
995 /* Make the last opcode before
Ezio Melottic2077b02011-03-16 12:34:31 +0200996 a try: finally: block uninterruptible. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000997 goto fast_next_opcode;
998 }
999 _Py_Ticker = _Py_CheckInterval;
1000 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001001#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001002 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001003#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001004 if (pendingcalls_to_do) {
1005 if (Py_MakePendingCalls() < 0) {
1006 why = WHY_EXCEPTION;
1007 goto on_error;
1008 }
1009 if (pendingcalls_to_do)
1010 /* MakePendingCalls() didn't succeed.
1011 Force early re-execution of this
1012 "periodic" code, possibly after
1013 a thread switch */
1014 _Py_Ticker = 0;
1015 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001016#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001017 if (interpreter_lock) {
1018 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 if (PyThreadState_Swap(NULL) != tstate)
1021 Py_FatalError("ceval: tstate mix-up");
1022 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001023
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001024 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Peterson1c78e6d2014-06-16 22:59:07 -07001027
1028 /* Check if we should make a quick exit. */
1029 if (_Py_Finalizing && _Py_Finalizing != tstate) {
1030 PyThread_release_lock(interpreter_lock);
1031 PyThread_exit_thread();
1032 }
1033
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001034 if (PyThreadState_Swap(tstate) != NULL)
1035 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001036
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001037 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001039 if (tstate->async_exc != NULL) {
1040 x = tstate->async_exc;
1041 tstate->async_exc = NULL;
1042 PyErr_SetNone(x);
1043 Py_DECREF(x);
1044 why = WHY_EXCEPTION;
1045 goto on_error;
1046 }
1047 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001048#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001050
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001051 fast_next_opcode:
1052 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001053
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001054 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001055
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001056 if (_Py_TracingPossible &&
1057 tstate->c_tracefunc != NULL && !tstate->tracing) {
1058 /* see maybe_call_line_trace
1059 for expository comments */
1060 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001061
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001062 err = maybe_call_line_trace(tstate->c_tracefunc,
1063 tstate->c_traceobj,
1064 f, &instr_lb, &instr_ub,
1065 &instr_prev);
1066 /* Reload possibly changed frame fields */
1067 JUMPTO(f->f_lasti);
1068 if (f->f_stacktop != NULL) {
1069 stack_pointer = f->f_stacktop;
1070 f->f_stacktop = NULL;
1071 }
1072 if (err) {
1073 /* trace function raised an exception */
1074 goto on_error;
1075 }
1076 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001078 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001079
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001080 opcode = NEXTOP();
1081 oparg = 0; /* allows oparg to be stored in a register because
1082 it doesn't have to be remembered across a full loop */
1083 if (HAS_ARG(opcode))
1084 oparg = NEXTARG();
Stefan Krah7ff78252010-06-23 18:12:09 +00001085 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001086#ifdef DYNAMIC_EXECUTION_PROFILE
1087#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001088 dxpairs[lastopcode][opcode]++;
1089 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001090#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001091 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001092#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001093
Guido van Rossum96a42c81992-01-12 02:29:51 +00001094#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001095 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001096
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001097 if (lltrace) {
1098 if (HAS_ARG(opcode)) {
1099 printf("%d: %d, %d\n",
1100 f->f_lasti, opcode, oparg);
1101 }
1102 else {
1103 printf("%d: %d\n",
1104 f->f_lasti, opcode);
1105 }
1106 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001107#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001108
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001109 /* Main switch on opcode */
1110 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001111
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001112 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001113
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001114 /* BEWARE!
1115 It is essential that any operation that fails sets either
1116 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1117 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001119 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001121 case NOP:
1122 goto fast_next_opcode;
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001123
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001124 case LOAD_FAST:
1125 x = GETLOCAL(oparg);
1126 if (x != NULL) {
1127 Py_INCREF(x);
1128 PUSH(x);
1129 goto fast_next_opcode;
1130 }
1131 format_exc_check_arg(PyExc_UnboundLocalError,
1132 UNBOUNDLOCAL_ERROR_MSG,
1133 PyTuple_GetItem(co->co_varnames, oparg));
1134 break;
Neil Schemenauer63543862002-02-17 19:10:14 +00001135
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001136 case LOAD_CONST:
1137 x = GETITEM(consts, oparg);
1138 Py_INCREF(x);
1139 PUSH(x);
1140 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001142 PREDICTED_WITH_ARG(STORE_FAST);
1143 case STORE_FAST:
1144 v = POP();
1145 SETLOCAL(oparg, v);
1146 goto fast_next_opcode;
Neil Schemenauer63543862002-02-17 19:10:14 +00001147
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001148 case POP_TOP:
1149 v = POP();
1150 Py_DECREF(v);
1151 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001152
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001153 case ROT_TWO:
1154 v = TOP();
1155 w = SECOND();
1156 SET_TOP(w);
1157 SET_SECOND(v);
1158 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001159
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001160 case ROT_THREE:
1161 v = TOP();
1162 w = SECOND();
1163 x = THIRD();
1164 SET_TOP(w);
1165 SET_SECOND(x);
1166 SET_THIRD(v);
1167 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001169 case ROT_FOUR:
1170 u = TOP();
1171 v = SECOND();
1172 w = THIRD();
1173 x = FOURTH();
1174 SET_TOP(v);
1175 SET_SECOND(w);
1176 SET_THIRD(x);
1177 SET_FOURTH(u);
1178 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001179
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001180 case DUP_TOP:
1181 v = TOP();
1182 Py_INCREF(v);
1183 PUSH(v);
1184 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00001185
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001186 case DUP_TOPX:
1187 if (oparg == 2) {
1188 x = TOP();
1189 Py_INCREF(x);
1190 w = SECOND();
1191 Py_INCREF(w);
1192 STACKADJ(2);
1193 SET_TOP(x);
1194 SET_SECOND(w);
1195 goto fast_next_opcode;
1196 } else if (oparg == 3) {
1197 x = TOP();
1198 Py_INCREF(x);
1199 w = SECOND();
1200 Py_INCREF(w);
1201 v = THIRD();
1202 Py_INCREF(v);
1203 STACKADJ(3);
1204 SET_TOP(x);
1205 SET_SECOND(w);
1206 SET_THIRD(v);
1207 goto fast_next_opcode;
1208 }
1209 Py_FatalError("invalid argument to DUP_TOPX"
1210 " (bytecode corruption?)");
1211 /* Never returns, so don't bother to set why. */
1212 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001214 case UNARY_POSITIVE:
1215 v = TOP();
1216 x = PyNumber_Positive(v);
1217 Py_DECREF(v);
1218 SET_TOP(x);
1219 if (x != NULL) continue;
1220 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001221
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001222 case UNARY_NEGATIVE:
1223 v = TOP();
1224 x = PyNumber_Negative(v);
1225 Py_DECREF(v);
1226 SET_TOP(x);
1227 if (x != NULL) continue;
1228 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001229
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001230 case UNARY_NOT:
1231 v = TOP();
1232 err = PyObject_IsTrue(v);
1233 Py_DECREF(v);
1234 if (err == 0) {
1235 Py_INCREF(Py_True);
1236 SET_TOP(Py_True);
1237 continue;
1238 }
1239 else if (err > 0) {
1240 Py_INCREF(Py_False);
1241 SET_TOP(Py_False);
1242 err = 0;
1243 continue;
1244 }
1245 STACKADJ(-1);
1246 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001247
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001248 case UNARY_CONVERT:
1249 v = TOP();
1250 x = PyObject_Repr(v);
1251 Py_DECREF(v);
1252 SET_TOP(x);
1253 if (x != NULL) continue;
1254 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001256 case UNARY_INVERT:
1257 v = TOP();
1258 x = PyNumber_Invert(v);
1259 Py_DECREF(v);
1260 SET_TOP(x);
1261 if (x != NULL) continue;
1262 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001263
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001264 case BINARY_POWER:
1265 w = POP();
1266 v = TOP();
1267 x = PyNumber_Power(v, w, Py_None);
1268 Py_DECREF(v);
1269 Py_DECREF(w);
1270 SET_TOP(x);
1271 if (x != NULL) continue;
1272 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001273
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001274 case BINARY_MULTIPLY:
1275 w = POP();
1276 v = TOP();
1277 x = PyNumber_Multiply(v, w);
1278 Py_DECREF(v);
1279 Py_DECREF(w);
1280 SET_TOP(x);
1281 if (x != NULL) continue;
1282 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001283
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001284 case BINARY_DIVIDE:
1285 if (!_Py_QnewFlag) {
1286 w = POP();
1287 v = TOP();
1288 x = PyNumber_Divide(v, w);
1289 Py_DECREF(v);
1290 Py_DECREF(w);
1291 SET_TOP(x);
1292 if (x != NULL) continue;
1293 break;
1294 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001295 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001296 BINARY_TRUE_DIVIDE */
1297 case BINARY_TRUE_DIVIDE:
1298 w = POP();
1299 v = TOP();
1300 x = PyNumber_TrueDivide(v, w);
1301 Py_DECREF(v);
1302 Py_DECREF(w);
1303 SET_TOP(x);
1304 if (x != NULL) continue;
1305 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001306
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001307 case BINARY_FLOOR_DIVIDE:
1308 w = POP();
1309 v = TOP();
1310 x = PyNumber_FloorDivide(v, w);
1311 Py_DECREF(v);
1312 Py_DECREF(w);
1313 SET_TOP(x);
1314 if (x != NULL) continue;
1315 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001316
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001317 case BINARY_MODULO:
1318 w = POP();
1319 v = TOP();
1320 if (PyString_CheckExact(v))
1321 x = PyString_Format(v, w);
1322 else
1323 x = PyNumber_Remainder(v, w);
1324 Py_DECREF(v);
1325 Py_DECREF(w);
1326 SET_TOP(x);
1327 if (x != NULL) continue;
1328 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001329
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001330 case BINARY_ADD:
1331 w = POP();
1332 v = TOP();
1333 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1334 /* INLINE: int + int */
1335 register long a, b, i;
1336 a = PyInt_AS_LONG(v);
1337 b = PyInt_AS_LONG(w);
1338 /* cast to avoid undefined behaviour
1339 on overflow */
1340 i = (long)((unsigned long)a + b);
1341 if ((i^a) < 0 && (i^b) < 0)
1342 goto slow_add;
1343 x = PyInt_FromLong(i);
1344 }
1345 else if (PyString_CheckExact(v) &&
1346 PyString_CheckExact(w)) {
1347 x = string_concatenate(v, w, f, next_instr);
1348 /* string_concatenate consumed the ref to v */
1349 goto skip_decref_vx;
1350 }
1351 else {
1352 slow_add:
1353 x = PyNumber_Add(v, w);
1354 }
1355 Py_DECREF(v);
1356 skip_decref_vx:
1357 Py_DECREF(w);
1358 SET_TOP(x);
1359 if (x != NULL) continue;
1360 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001361
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001362 case BINARY_SUBTRACT:
1363 w = POP();
1364 v = TOP();
1365 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1366 /* INLINE: int - int */
1367 register long a, b, i;
1368 a = PyInt_AS_LONG(v);
1369 b = PyInt_AS_LONG(w);
1370 /* cast to avoid undefined behaviour
1371 on overflow */
1372 i = (long)((unsigned long)a - b);
1373 if ((i^a) < 0 && (i^~b) < 0)
1374 goto slow_sub;
1375 x = PyInt_FromLong(i);
1376 }
1377 else {
1378 slow_sub:
1379 x = PyNumber_Subtract(v, w);
1380 }
1381 Py_DECREF(v);
1382 Py_DECREF(w);
1383 SET_TOP(x);
1384 if (x != NULL) continue;
1385 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001386
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001387 case BINARY_SUBSCR:
1388 w = POP();
1389 v = TOP();
1390 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1391 /* INLINE: list[int] */
1392 Py_ssize_t i = PyInt_AsSsize_t(w);
1393 if (i < 0)
1394 i += PyList_GET_SIZE(v);
1395 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1396 x = PyList_GET_ITEM(v, i);
1397 Py_INCREF(x);
1398 }
1399 else
1400 goto slow_get;
1401 }
1402 else
1403 slow_get:
1404 x = PyObject_GetItem(v, w);
1405 Py_DECREF(v);
1406 Py_DECREF(w);
1407 SET_TOP(x);
1408 if (x != NULL) continue;
1409 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001411 case BINARY_LSHIFT:
1412 w = POP();
1413 v = TOP();
1414 x = PyNumber_Lshift(v, w);
1415 Py_DECREF(v);
1416 Py_DECREF(w);
1417 SET_TOP(x);
1418 if (x != NULL) continue;
1419 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001421 case BINARY_RSHIFT:
1422 w = POP();
1423 v = TOP();
1424 x = PyNumber_Rshift(v, w);
1425 Py_DECREF(v);
1426 Py_DECREF(w);
1427 SET_TOP(x);
1428 if (x != NULL) continue;
1429 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001430
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001431 case BINARY_AND:
1432 w = POP();
1433 v = TOP();
1434 x = PyNumber_And(v, w);
1435 Py_DECREF(v);
1436 Py_DECREF(w);
1437 SET_TOP(x);
1438 if (x != NULL) continue;
1439 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001441 case BINARY_XOR:
1442 w = POP();
1443 v = TOP();
1444 x = PyNumber_Xor(v, w);
1445 Py_DECREF(v);
1446 Py_DECREF(w);
1447 SET_TOP(x);
1448 if (x != NULL) continue;
1449 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001450
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001451 case BINARY_OR:
1452 w = POP();
1453 v = TOP();
1454 x = PyNumber_Or(v, w);
1455 Py_DECREF(v);
1456 Py_DECREF(w);
1457 SET_TOP(x);
1458 if (x != NULL) continue;
1459 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001460
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001461 case LIST_APPEND:
1462 w = POP();
1463 v = PEEK(oparg);
1464 err = PyList_Append(v, w);
1465 Py_DECREF(w);
1466 if (err == 0) {
1467 PREDICT(JUMP_ABSOLUTE);
1468 continue;
1469 }
1470 break;
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001471
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001472 case SET_ADD:
1473 w = POP();
1474 v = stack_pointer[-oparg];
1475 err = PySet_Add(v, w);
1476 Py_DECREF(w);
1477 if (err == 0) {
1478 PREDICT(JUMP_ABSOLUTE);
1479 continue;
1480 }
1481 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001482
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001483 case INPLACE_POWER:
1484 w = POP();
1485 v = TOP();
1486 x = PyNumber_InPlacePower(v, w, Py_None);
1487 Py_DECREF(v);
1488 Py_DECREF(w);
1489 SET_TOP(x);
1490 if (x != NULL) continue;
1491 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001492
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001493 case INPLACE_MULTIPLY:
1494 w = POP();
1495 v = TOP();
1496 x = PyNumber_InPlaceMultiply(v, w);
1497 Py_DECREF(v);
1498 Py_DECREF(w);
1499 SET_TOP(x);
1500 if (x != NULL) continue;
1501 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001502
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001503 case INPLACE_DIVIDE:
1504 if (!_Py_QnewFlag) {
1505 w = POP();
1506 v = TOP();
1507 x = PyNumber_InPlaceDivide(v, w);
1508 Py_DECREF(v);
1509 Py_DECREF(w);
1510 SET_TOP(x);
1511 if (x != NULL) continue;
1512 break;
1513 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001514 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001515 INPLACE_TRUE_DIVIDE */
1516 case INPLACE_TRUE_DIVIDE:
1517 w = POP();
1518 v = TOP();
1519 x = PyNumber_InPlaceTrueDivide(v, w);
1520 Py_DECREF(v);
1521 Py_DECREF(w);
1522 SET_TOP(x);
1523 if (x != NULL) continue;
1524 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001526 case INPLACE_FLOOR_DIVIDE:
1527 w = POP();
1528 v = TOP();
1529 x = PyNumber_InPlaceFloorDivide(v, w);
1530 Py_DECREF(v);
1531 Py_DECREF(w);
1532 SET_TOP(x);
1533 if (x != NULL) continue;
1534 break;
Guido van Rossum4668b002001-08-08 05:00:18 +00001535
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001536 case INPLACE_MODULO:
1537 w = POP();
1538 v = TOP();
1539 x = PyNumber_InPlaceRemainder(v, w);
1540 Py_DECREF(v);
1541 Py_DECREF(w);
1542 SET_TOP(x);
1543 if (x != NULL) continue;
1544 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001545
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001546 case INPLACE_ADD:
1547 w = POP();
1548 v = TOP();
1549 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1550 /* INLINE: int + int */
1551 register long a, b, i;
1552 a = PyInt_AS_LONG(v);
1553 b = PyInt_AS_LONG(w);
1554 i = a + b;
1555 if ((i^a) < 0 && (i^b) < 0)
1556 goto slow_iadd;
1557 x = PyInt_FromLong(i);
1558 }
1559 else if (PyString_CheckExact(v) &&
1560 PyString_CheckExact(w)) {
1561 x = string_concatenate(v, w, f, next_instr);
1562 /* string_concatenate consumed the ref to v */
1563 goto skip_decref_v;
1564 }
1565 else {
1566 slow_iadd:
1567 x = PyNumber_InPlaceAdd(v, w);
1568 }
1569 Py_DECREF(v);
1570 skip_decref_v:
1571 Py_DECREF(w);
1572 SET_TOP(x);
1573 if (x != NULL) continue;
1574 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001575
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001576 case INPLACE_SUBTRACT:
1577 w = POP();
1578 v = TOP();
1579 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1580 /* INLINE: int - int */
1581 register long a, b, i;
1582 a = PyInt_AS_LONG(v);
1583 b = PyInt_AS_LONG(w);
1584 i = a - b;
1585 if ((i^a) < 0 && (i^~b) < 0)
1586 goto slow_isub;
1587 x = PyInt_FromLong(i);
1588 }
1589 else {
1590 slow_isub:
1591 x = PyNumber_InPlaceSubtract(v, w);
1592 }
1593 Py_DECREF(v);
1594 Py_DECREF(w);
1595 SET_TOP(x);
1596 if (x != NULL) continue;
1597 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001599 case INPLACE_LSHIFT:
1600 w = POP();
1601 v = TOP();
1602 x = PyNumber_InPlaceLshift(v, w);
1603 Py_DECREF(v);
1604 Py_DECREF(w);
1605 SET_TOP(x);
1606 if (x != NULL) continue;
1607 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001609 case INPLACE_RSHIFT:
1610 w = POP();
1611 v = TOP();
1612 x = PyNumber_InPlaceRshift(v, w);
1613 Py_DECREF(v);
1614 Py_DECREF(w);
1615 SET_TOP(x);
1616 if (x != NULL) continue;
1617 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001618
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001619 case INPLACE_AND:
1620 w = POP();
1621 v = TOP();
1622 x = PyNumber_InPlaceAnd(v, w);
1623 Py_DECREF(v);
1624 Py_DECREF(w);
1625 SET_TOP(x);
1626 if (x != NULL) continue;
1627 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001628
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001629 case INPLACE_XOR:
1630 w = POP();
1631 v = TOP();
1632 x = PyNumber_InPlaceXor(v, w);
1633 Py_DECREF(v);
1634 Py_DECREF(w);
1635 SET_TOP(x);
1636 if (x != NULL) continue;
1637 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001638
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001639 case INPLACE_OR:
1640 w = POP();
1641 v = TOP();
1642 x = PyNumber_InPlaceOr(v, w);
1643 Py_DECREF(v);
1644 Py_DECREF(w);
1645 SET_TOP(x);
1646 if (x != NULL) continue;
1647 break;
Thomas Wouters434d0822000-08-24 20:11:32 +00001648
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001649 case SLICE+0:
1650 case SLICE+1:
1651 case SLICE+2:
1652 case SLICE+3:
1653 if ((opcode-SLICE) & 2)
1654 w = POP();
1655 else
1656 w = NULL;
1657 if ((opcode-SLICE) & 1)
1658 v = POP();
1659 else
1660 v = NULL;
1661 u = TOP();
1662 x = apply_slice(u, v, w);
1663 Py_DECREF(u);
1664 Py_XDECREF(v);
1665 Py_XDECREF(w);
1666 SET_TOP(x);
1667 if (x != NULL) continue;
1668 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001669
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001670 case STORE_SLICE+0:
1671 case STORE_SLICE+1:
1672 case STORE_SLICE+2:
1673 case STORE_SLICE+3:
1674 if ((opcode-STORE_SLICE) & 2)
1675 w = POP();
1676 else
1677 w = NULL;
1678 if ((opcode-STORE_SLICE) & 1)
1679 v = POP();
1680 else
1681 v = NULL;
1682 u = POP();
1683 t = POP();
1684 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1685 Py_DECREF(t);
1686 Py_DECREF(u);
1687 Py_XDECREF(v);
1688 Py_XDECREF(w);
1689 if (err == 0) continue;
1690 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001691
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001692 case DELETE_SLICE+0:
1693 case DELETE_SLICE+1:
1694 case DELETE_SLICE+2:
1695 case DELETE_SLICE+3:
1696 if ((opcode-DELETE_SLICE) & 2)
1697 w = POP();
1698 else
1699 w = NULL;
1700 if ((opcode-DELETE_SLICE) & 1)
1701 v = POP();
1702 else
1703 v = NULL;
1704 u = POP();
1705 err = assign_slice(u, v, w, (PyObject *)NULL);
1706 /* del u[v:w] */
1707 Py_DECREF(u);
1708 Py_XDECREF(v);
1709 Py_XDECREF(w);
1710 if (err == 0) continue;
1711 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001713 case STORE_SUBSCR:
1714 w = TOP();
1715 v = SECOND();
1716 u = THIRD();
1717 STACKADJ(-3);
1718 /* v[w] = u */
1719 err = PyObject_SetItem(v, w, u);
1720 Py_DECREF(u);
1721 Py_DECREF(v);
1722 Py_DECREF(w);
1723 if (err == 0) continue;
1724 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001725
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001726 case DELETE_SUBSCR:
1727 w = TOP();
1728 v = SECOND();
1729 STACKADJ(-2);
1730 /* del v[w] */
1731 err = PyObject_DelItem(v, w);
1732 Py_DECREF(v);
1733 Py_DECREF(w);
1734 if (err == 0) continue;
1735 break;
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001736
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001737 case PRINT_EXPR:
1738 v = POP();
1739 w = PySys_GetObject("displayhook");
1740 if (w == NULL) {
1741 PyErr_SetString(PyExc_RuntimeError,
1742 "lost sys.displayhook");
1743 err = -1;
1744 x = NULL;
1745 }
1746 if (err == 0) {
1747 x = PyTuple_Pack(1, v);
1748 if (x == NULL)
1749 err = -1;
1750 }
1751 if (err == 0) {
1752 w = PyEval_CallObject(w, x);
1753 Py_XDECREF(w);
1754 if (w == NULL)
1755 err = -1;
1756 }
1757 Py_DECREF(v);
1758 Py_XDECREF(x);
1759 break;
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001760
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001761 case PRINT_ITEM_TO:
1762 w = stream = POP();
1763 /* fall through to PRINT_ITEM */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001764
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001765 case PRINT_ITEM:
1766 v = POP();
1767 if (stream == NULL || stream == Py_None) {
1768 w = PySys_GetObject("stdout");
1769 if (w == NULL) {
1770 PyErr_SetString(PyExc_RuntimeError,
1771 "lost sys.stdout");
1772 err = -1;
1773 }
1774 }
1775 /* PyFile_SoftSpace() can exececute arbitrary code
1776 if sys.stdout is an instance with a __getattr__.
1777 If __getattr__ raises an exception, w will
1778 be freed, so we need to prevent that temporarily. */
1779 Py_XINCREF(w);
1780 if (w != NULL && PyFile_SoftSpace(w, 0))
1781 err = PyFile_WriteString(" ", w);
1782 if (err == 0)
1783 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1784 if (err == 0) {
1785 /* XXX move into writeobject() ? */
1786 if (PyString_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001787 char *s = PyString_AS_STRING(v);
1788 Py_ssize_t len = PyString_GET_SIZE(v);
1789 if (len == 0 ||
1790 !isspace(Py_CHARMASK(s[len-1])) ||
1791 s[len-1] == ' ')
1792 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001793 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001794#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001795 else if (PyUnicode_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001796 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1797 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1798 if (len == 0 ||
1799 !Py_UNICODE_ISSPACE(s[len-1]) ||
1800 s[len-1] == ' ')
1801 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001802 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00001803#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001804 else
Stefan Krah7ff78252010-06-23 18:12:09 +00001805 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001806 }
1807 Py_XDECREF(w);
1808 Py_DECREF(v);
1809 Py_XDECREF(stream);
1810 stream = NULL;
1811 if (err == 0)
1812 continue;
1813 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001814
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001815 case PRINT_NEWLINE_TO:
1816 w = stream = POP();
1817 /* fall through to PRINT_NEWLINE */
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001818
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001819 case PRINT_NEWLINE:
1820 if (stream == NULL || stream == Py_None) {
1821 w = PySys_GetObject("stdout");
1822 if (w == NULL) {
1823 PyErr_SetString(PyExc_RuntimeError,
1824 "lost sys.stdout");
1825 why = WHY_EXCEPTION;
1826 }
1827 }
1828 if (w != NULL) {
1829 /* w.write() may replace sys.stdout, so we
1830 * have to keep our reference to it */
1831 Py_INCREF(w);
1832 err = PyFile_WriteString("\n", w);
1833 if (err == 0)
1834 PyFile_SoftSpace(w, 0);
1835 Py_DECREF(w);
1836 }
1837 Py_XDECREF(stream);
1838 stream = NULL;
1839 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001840
Thomas Wouters434d0822000-08-24 20:11:32 +00001841
1842#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001843 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001844#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001845 case RAISE_VARARGS:
1846 u = v = w = NULL;
1847 switch (oparg) {
1848 case 3:
1849 u = POP(); /* traceback */
1850 /* Fallthrough */
1851 case 2:
1852 v = POP(); /* value */
1853 /* Fallthrough */
1854 case 1:
1855 w = POP(); /* exc */
1856 case 0: /* Fallthrough */
1857 why = do_raise(w, v, u);
1858 break;
1859 default:
1860 PyErr_SetString(PyExc_SystemError,
1861 "bad RAISE_VARARGS oparg");
1862 why = WHY_EXCEPTION;
1863 break;
1864 }
1865 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001866
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001867 case LOAD_LOCALS:
1868 if ((x = f->f_locals) != NULL) {
1869 Py_INCREF(x);
1870 PUSH(x);
1871 continue;
1872 }
1873 PyErr_SetString(PyExc_SystemError, "no locals");
1874 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001875
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001876 case RETURN_VALUE:
1877 retval = POP();
1878 why = WHY_RETURN;
1879 goto fast_block_end;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001880
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001881 case YIELD_VALUE:
1882 retval = POP();
1883 f->f_stacktop = stack_pointer;
1884 why = WHY_YIELD;
1885 goto fast_yield;
Tim Peters5ca576e2001-06-18 22:08:13 +00001886
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001887 case EXEC_STMT:
1888 w = TOP();
1889 v = SECOND();
1890 u = THIRD();
1891 STACKADJ(-3);
1892 READ_TIMESTAMP(intr0);
1893 err = exec_statement(f, u, v, w);
1894 READ_TIMESTAMP(intr1);
1895 Py_DECREF(u);
1896 Py_DECREF(v);
1897 Py_DECREF(w);
1898 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001899
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001900 case POP_BLOCK:
1901 {
1902 PyTryBlock *b = PyFrame_BlockPop(f);
1903 while (STACK_LEVEL() > b->b_level) {
1904 v = POP();
1905 Py_DECREF(v);
1906 }
1907 }
1908 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00001909
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001910 PREDICTED(END_FINALLY);
1911 case END_FINALLY:
1912 v = POP();
1913 if (PyInt_Check(v)) {
1914 why = (enum why_code) PyInt_AS_LONG(v);
1915 assert(why != WHY_YIELD);
1916 if (why == WHY_RETURN ||
1917 why == WHY_CONTINUE)
1918 retval = POP();
1919 }
1920 else if (PyExceptionClass_Check(v) ||
1921 PyString_Check(v)) {
1922 w = POP();
1923 u = POP();
1924 PyErr_Restore(v, w, u);
1925 why = WHY_RERAISE;
1926 break;
1927 }
1928 else if (v != Py_None) {
1929 PyErr_SetString(PyExc_SystemError,
1930 "'finally' pops bad exception");
1931 why = WHY_EXCEPTION;
1932 }
1933 Py_DECREF(v);
1934 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001935
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001936 case BUILD_CLASS:
1937 u = TOP();
1938 v = SECOND();
1939 w = THIRD();
1940 STACKADJ(-2);
1941 x = build_class(u, v, w);
1942 SET_TOP(x);
1943 Py_DECREF(u);
1944 Py_DECREF(v);
1945 Py_DECREF(w);
1946 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001948 case STORE_NAME:
1949 w = GETITEM(names, oparg);
1950 v = POP();
1951 if ((x = f->f_locals) != NULL) {
1952 if (PyDict_CheckExact(x))
1953 err = PyDict_SetItem(x, w, v);
1954 else
1955 err = PyObject_SetItem(x, w, v);
1956 Py_DECREF(v);
1957 if (err == 0) continue;
1958 break;
1959 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001960 t = PyObject_Repr(w);
1961 if (t == NULL)
1962 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001963 PyErr_Format(PyExc_SystemError,
1964 "no locals found when storing %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001965 PyString_AS_STRING(t));
1966 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001967 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00001968
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001969 case DELETE_NAME:
1970 w = GETITEM(names, oparg);
1971 if ((x = f->f_locals) != NULL) {
1972 if ((err = PyObject_DelItem(x, w)) != 0)
1973 format_exc_check_arg(PyExc_NameError,
1974 NAME_ERROR_MSG,
1975 w);
1976 break;
1977 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001978 t = PyObject_Repr(w);
1979 if (t == NULL)
1980 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001981 PyErr_Format(PyExc_SystemError,
1982 "no locals when deleting %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02001983 PyString_AS_STRING(w));
1984 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001985 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001986
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001987 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
1988 case UNPACK_SEQUENCE:
1989 v = POP();
1990 if (PyTuple_CheckExact(v) &&
1991 PyTuple_GET_SIZE(v) == oparg) {
1992 PyObject **items = \
1993 ((PyTupleObject *)v)->ob_item;
1994 while (oparg--) {
1995 w = items[oparg];
1996 Py_INCREF(w);
1997 PUSH(w);
1998 }
1999 Py_DECREF(v);
2000 continue;
2001 } else if (PyList_CheckExact(v) &&
2002 PyList_GET_SIZE(v) == oparg) {
2003 PyObject **items = \
2004 ((PyListObject *)v)->ob_item;
2005 while (oparg--) {
2006 w = items[oparg];
2007 Py_INCREF(w);
2008 PUSH(w);
2009 }
2010 } else if (unpack_iterable(v, oparg,
2011 stack_pointer + oparg)) {
2012 STACKADJ(oparg);
2013 } else {
2014 /* unpack_iterable() raised an exception */
2015 why = WHY_EXCEPTION;
2016 }
2017 Py_DECREF(v);
2018 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002020 case STORE_ATTR:
2021 w = GETITEM(names, oparg);
2022 v = TOP();
2023 u = SECOND();
2024 STACKADJ(-2);
2025 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2026 Py_DECREF(v);
2027 Py_DECREF(u);
2028 if (err == 0) continue;
2029 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002031 case DELETE_ATTR:
2032 w = GETITEM(names, oparg);
2033 v = POP();
2034 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2035 /* del v.w */
2036 Py_DECREF(v);
2037 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002038
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002039 case STORE_GLOBAL:
2040 w = GETITEM(names, oparg);
2041 v = POP();
2042 err = PyDict_SetItem(f->f_globals, w, v);
2043 Py_DECREF(v);
2044 if (err == 0) continue;
2045 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002046
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002047 case DELETE_GLOBAL:
2048 w = GETITEM(names, oparg);
2049 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2050 format_exc_check_arg(
2051 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2052 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002053
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002054 case LOAD_NAME:
2055 w = GETITEM(names, oparg);
2056 if ((v = f->f_locals) == NULL) {
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002057 why = WHY_EXCEPTION;
2058 t = PyObject_Repr(w);
2059 if (t == NULL)
2060 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002061 PyErr_Format(PyExc_SystemError,
2062 "no locals when loading %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002063 PyString_AS_STRING(w));
2064 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002065 break;
2066 }
2067 if (PyDict_CheckExact(v)) {
2068 x = PyDict_GetItem(v, w);
2069 Py_XINCREF(x);
2070 }
2071 else {
2072 x = PyObject_GetItem(v, w);
2073 if (x == NULL && PyErr_Occurred()) {
2074 if (!PyErr_ExceptionMatches(
2075 PyExc_KeyError))
2076 break;
2077 PyErr_Clear();
2078 }
2079 }
2080 if (x == NULL) {
2081 x = PyDict_GetItem(f->f_globals, w);
2082 if (x == NULL) {
2083 x = PyDict_GetItem(f->f_builtins, w);
2084 if (x == NULL) {
2085 format_exc_check_arg(
2086 PyExc_NameError,
2087 NAME_ERROR_MSG, w);
2088 break;
2089 }
2090 }
2091 Py_INCREF(x);
2092 }
2093 PUSH(x);
2094 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002095
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002096 case LOAD_GLOBAL:
2097 w = GETITEM(names, oparg);
2098 if (PyString_CheckExact(w)) {
2099 /* Inline the PyDict_GetItem() calls.
2100 WARNING: this is an extreme speed hack.
2101 Do not try this at home. */
2102 long hash = ((PyStringObject *)w)->ob_shash;
2103 if (hash != -1) {
2104 PyDictObject *d;
2105 PyDictEntry *e;
2106 d = (PyDictObject *)(f->f_globals);
2107 e = d->ma_lookup(d, w, hash);
2108 if (e == NULL) {
2109 x = NULL;
2110 break;
2111 }
2112 x = e->me_value;
2113 if (x != NULL) {
2114 Py_INCREF(x);
2115 PUSH(x);
2116 continue;
2117 }
2118 d = (PyDictObject *)(f->f_builtins);
2119 e = d->ma_lookup(d, w, hash);
2120 if (e == NULL) {
2121 x = NULL;
2122 break;
2123 }
2124 x = e->me_value;
2125 if (x != NULL) {
2126 Py_INCREF(x);
2127 PUSH(x);
2128 continue;
2129 }
2130 goto load_global_error;
2131 }
2132 }
2133 /* This is the un-inlined version of the code above */
2134 x = PyDict_GetItem(f->f_globals, w);
2135 if (x == NULL) {
2136 x = PyDict_GetItem(f->f_builtins, w);
2137 if (x == NULL) {
2138 load_global_error:
2139 format_exc_check_arg(
2140 PyExc_NameError,
2141 GLOBAL_NAME_ERROR_MSG, w);
2142 break;
2143 }
2144 }
2145 Py_INCREF(x);
2146 PUSH(x);
2147 continue;
Guido van Rossum681d79a1995-07-18 14:51:37 +00002148
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002149 case DELETE_FAST:
2150 x = GETLOCAL(oparg);
2151 if (x != NULL) {
2152 SETLOCAL(oparg, NULL);
2153 continue;
2154 }
2155 format_exc_check_arg(
2156 PyExc_UnboundLocalError,
2157 UNBOUNDLOCAL_ERROR_MSG,
2158 PyTuple_GetItem(co->co_varnames, oparg)
2159 );
2160 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002161
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002162 case LOAD_CLOSURE:
2163 x = freevars[oparg];
2164 Py_INCREF(x);
2165 PUSH(x);
2166 if (x != NULL) continue;
2167 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002168
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002169 case LOAD_DEREF:
2170 x = freevars[oparg];
2171 w = PyCell_Get(x);
2172 if (w != NULL) {
2173 PUSH(w);
2174 continue;
2175 }
2176 err = -1;
2177 /* Don't stomp existing exception */
2178 if (PyErr_Occurred())
2179 break;
2180 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2181 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krah7ff78252010-06-23 18:12:09 +00002182 oparg);
2183 format_exc_check_arg(
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002184 PyExc_UnboundLocalError,
2185 UNBOUNDLOCAL_ERROR_MSG,
2186 v);
2187 } else {
2188 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2189 PyTuple_GET_SIZE(co->co_cellvars));
2190 format_exc_check_arg(PyExc_NameError,
2191 UNBOUNDFREE_ERROR_MSG, v);
2192 }
2193 break;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002194
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002195 case STORE_DEREF:
2196 w = POP();
2197 x = freevars[oparg];
2198 PyCell_Set(x, w);
2199 Py_DECREF(w);
2200 continue;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002201
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002202 case BUILD_TUPLE:
2203 x = PyTuple_New(oparg);
2204 if (x != NULL) {
2205 for (; --oparg >= 0;) {
2206 w = POP();
2207 PyTuple_SET_ITEM(x, oparg, w);
2208 }
2209 PUSH(x);
2210 continue;
2211 }
2212 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002214 case BUILD_LIST:
2215 x = PyList_New(oparg);
2216 if (x != NULL) {
2217 for (; --oparg >= 0;) {
2218 w = POP();
2219 PyList_SET_ITEM(x, oparg, w);
2220 }
2221 PUSH(x);
2222 continue;
2223 }
2224 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002226 case BUILD_SET:
2227 x = PySet_New(NULL);
2228 if (x != NULL) {
2229 for (; --oparg >= 0;) {
2230 w = POP();
2231 if (err == 0)
2232 err = PySet_Add(x, w);
2233 Py_DECREF(w);
2234 }
2235 if (err != 0) {
2236 Py_DECREF(x);
2237 break;
2238 }
2239 PUSH(x);
2240 continue;
2241 }
2242 break;
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002243
2244
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002245 case BUILD_MAP:
2246 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2247 PUSH(x);
2248 if (x != NULL) continue;
2249 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002250
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002251 case STORE_MAP:
2252 w = TOP(); /* key */
2253 u = SECOND(); /* value */
2254 v = THIRD(); /* dict */
2255 STACKADJ(-2);
2256 assert (PyDict_CheckExact(v));
2257 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2258 Py_DECREF(u);
2259 Py_DECREF(w);
2260 if (err == 0) continue;
2261 break;
Raymond Hettingereffde122007-12-18 18:26:18 +00002262
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002263 case MAP_ADD:
2264 w = TOP(); /* key */
2265 u = SECOND(); /* value */
2266 STACKADJ(-2);
2267 v = stack_pointer[-oparg]; /* dict */
2268 assert (PyDict_CheckExact(v));
2269 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2270 Py_DECREF(u);
2271 Py_DECREF(w);
2272 if (err == 0) {
2273 PREDICT(JUMP_ABSOLUTE);
2274 continue;
2275 }
2276 break;
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002277
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002278 case LOAD_ATTR:
2279 w = GETITEM(names, oparg);
2280 v = TOP();
2281 x = PyObject_GetAttr(v, w);
2282 Py_DECREF(v);
2283 SET_TOP(x);
2284 if (x != NULL) continue;
2285 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002287 case COMPARE_OP:
2288 w = POP();
2289 v = TOP();
2290 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2291 /* INLINE: cmp(int, int) */
2292 register long a, b;
2293 register int res;
2294 a = PyInt_AS_LONG(v);
2295 b = PyInt_AS_LONG(w);
2296 switch (oparg) {
2297 case PyCmp_LT: res = a < b; break;
2298 case PyCmp_LE: res = a <= b; break;
2299 case PyCmp_EQ: res = a == b; break;
2300 case PyCmp_NE: res = a != b; break;
2301 case PyCmp_GT: res = a > b; break;
2302 case PyCmp_GE: res = a >= b; break;
2303 case PyCmp_IS: res = v == w; break;
2304 case PyCmp_IS_NOT: res = v != w; break;
2305 default: goto slow_compare;
2306 }
2307 x = res ? Py_True : Py_False;
2308 Py_INCREF(x);
2309 }
2310 else {
2311 slow_compare:
2312 x = cmp_outcome(oparg, v, w);
2313 }
2314 Py_DECREF(v);
2315 Py_DECREF(w);
2316 SET_TOP(x);
2317 if (x == NULL) break;
2318 PREDICT(POP_JUMP_IF_FALSE);
2319 PREDICT(POP_JUMP_IF_TRUE);
2320 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002321
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002322 case IMPORT_NAME:
2323 w = GETITEM(names, oparg);
2324 x = PyDict_GetItemString(f->f_builtins, "__import__");
2325 if (x == NULL) {
2326 PyErr_SetString(PyExc_ImportError,
2327 "__import__ not found");
2328 break;
2329 }
2330 Py_INCREF(x);
2331 v = POP();
2332 u = TOP();
2333 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2334 w = PyTuple_Pack(5,
2335 w,
2336 f->f_globals,
2337 f->f_locals == NULL ?
2338 Py_None : f->f_locals,
2339 v,
2340 u);
2341 else
2342 w = PyTuple_Pack(4,
2343 w,
2344 f->f_globals,
2345 f->f_locals == NULL ?
2346 Py_None : f->f_locals,
2347 v);
2348 Py_DECREF(v);
2349 Py_DECREF(u);
2350 if (w == NULL) {
2351 u = POP();
2352 Py_DECREF(x);
2353 x = NULL;
2354 break;
2355 }
2356 READ_TIMESTAMP(intr0);
2357 v = x;
2358 x = PyEval_CallObject(v, w);
2359 Py_DECREF(v);
2360 READ_TIMESTAMP(intr1);
2361 Py_DECREF(w);
2362 SET_TOP(x);
2363 if (x != NULL) continue;
2364 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00002365
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002366 case IMPORT_STAR:
2367 v = POP();
2368 PyFrame_FastToLocals(f);
2369 if ((x = f->f_locals) == NULL) {
2370 PyErr_SetString(PyExc_SystemError,
2371 "no locals found during 'import *'");
2372 break;
2373 }
2374 READ_TIMESTAMP(intr0);
2375 err = import_all_from(x, v);
2376 READ_TIMESTAMP(intr1);
2377 PyFrame_LocalsToFast(f, 0);
2378 Py_DECREF(v);
2379 if (err == 0) continue;
2380 break;
Guido van Rossum25831651993-05-19 14:50:45 +00002381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002382 case IMPORT_FROM:
2383 w = GETITEM(names, oparg);
2384 v = TOP();
2385 READ_TIMESTAMP(intr0);
2386 x = import_from(v, w);
2387 READ_TIMESTAMP(intr1);
2388 PUSH(x);
2389 if (x != NULL) continue;
2390 break;
Thomas Wouters52152252000-08-17 22:55:00 +00002391
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002392 case JUMP_FORWARD:
2393 JUMPBY(oparg);
2394 goto fast_next_opcode;
Guido van Rossumac7be682001-01-17 15:42:30 +00002395
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002396 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
2397 case POP_JUMP_IF_FALSE:
2398 w = POP();
2399 if (w == Py_True) {
2400 Py_DECREF(w);
2401 goto fast_next_opcode;
2402 }
2403 if (w == Py_False) {
2404 Py_DECREF(w);
2405 JUMPTO(oparg);
2406 goto fast_next_opcode;
2407 }
2408 err = PyObject_IsTrue(w);
2409 Py_DECREF(w);
2410 if (err > 0)
2411 err = 0;
2412 else if (err == 0)
2413 JUMPTO(oparg);
2414 else
2415 break;
2416 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002417
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002418 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
2419 case POP_JUMP_IF_TRUE:
2420 w = POP();
2421 if (w == Py_False) {
2422 Py_DECREF(w);
2423 goto fast_next_opcode;
2424 }
2425 if (w == Py_True) {
2426 Py_DECREF(w);
2427 JUMPTO(oparg);
2428 goto fast_next_opcode;
2429 }
2430 err = PyObject_IsTrue(w);
2431 Py_DECREF(w);
2432 if (err > 0) {
2433 err = 0;
2434 JUMPTO(oparg);
2435 }
2436 else if (err == 0)
2437 ;
2438 else
2439 break;
2440 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002441
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002442 case JUMP_IF_FALSE_OR_POP:
2443 w = TOP();
2444 if (w == Py_True) {
2445 STACKADJ(-1);
2446 Py_DECREF(w);
2447 goto fast_next_opcode;
2448 }
2449 if (w == Py_False) {
2450 JUMPTO(oparg);
2451 goto fast_next_opcode;
2452 }
2453 err = PyObject_IsTrue(w);
2454 if (err > 0) {
2455 STACKADJ(-1);
2456 Py_DECREF(w);
2457 err = 0;
2458 }
2459 else if (err == 0)
2460 JUMPTO(oparg);
2461 else
2462 break;
2463 continue;
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002464
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002465 case JUMP_IF_TRUE_OR_POP:
2466 w = TOP();
2467 if (w == Py_False) {
2468 STACKADJ(-1);
2469 Py_DECREF(w);
2470 goto fast_next_opcode;
2471 }
2472 if (w == Py_True) {
2473 JUMPTO(oparg);
2474 goto fast_next_opcode;
2475 }
2476 err = PyObject_IsTrue(w);
2477 if (err > 0) {
2478 err = 0;
2479 JUMPTO(oparg);
2480 }
2481 else if (err == 0) {
2482 STACKADJ(-1);
2483 Py_DECREF(w);
2484 }
2485 else
2486 break;
2487 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002488
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002489 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
2490 case JUMP_ABSOLUTE:
2491 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002492#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002493 /* Enabling this path speeds-up all while and for-loops by bypassing
2494 the per-loop checks for signals. By default, this should be turned-off
2495 because it prevents detection of a control-break in tight loops like
2496 "while 1: pass". Compile with this option turned-on when you need
2497 the speed-up and do not need break checking inside tight loops (ones
2498 that contain only instructions ending with goto fast_next_opcode).
2499 */
2500 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002501#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002502 continue;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002503#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00002504
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002505 case GET_ITER:
2506 /* before: [obj]; after [getiter(obj)] */
2507 v = TOP();
2508 x = PyObject_GetIter(v);
2509 Py_DECREF(v);
2510 if (x != NULL) {
2511 SET_TOP(x);
2512 PREDICT(FOR_ITER);
2513 continue;
2514 }
2515 STACKADJ(-1);
2516 break;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002517
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002518 PREDICTED_WITH_ARG(FOR_ITER);
2519 case FOR_ITER:
2520 /* before: [iter]; after: [iter, iter()] *or* [] */
2521 v = TOP();
2522 x = (*v->ob_type->tp_iternext)(v);
2523 if (x != NULL) {
2524 PUSH(x);
2525 PREDICT(STORE_FAST);
2526 PREDICT(UNPACK_SEQUENCE);
2527 continue;
2528 }
2529 if (PyErr_Occurred()) {
2530 if (!PyErr_ExceptionMatches(
2531 PyExc_StopIteration))
2532 break;
2533 PyErr_Clear();
2534 }
2535 /* iterator ended normally */
2536 x = v = POP();
2537 Py_DECREF(v);
2538 JUMPBY(oparg);
2539 continue;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002540
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002541 case BREAK_LOOP:
2542 why = WHY_BREAK;
2543 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002544
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002545 case CONTINUE_LOOP:
2546 retval = PyInt_FromLong(oparg);
2547 if (!retval) {
2548 x = NULL;
2549 break;
2550 }
2551 why = WHY_CONTINUE;
2552 goto fast_block_end;
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002553
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002554 case SETUP_LOOP:
2555 case SETUP_EXCEPT:
2556 case SETUP_FINALLY:
2557 /* NOTE: If you add any new block-setup opcodes that
2558 are not try/except/finally handlers, you may need
2559 to update the PyGen_NeedsFinalizing() function.
2560 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002561
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002562 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2563 STACK_LEVEL());
2564 continue;
Guido van Rossumac7be682001-01-17 15:42:30 +00002565
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002566 case SETUP_WITH:
2567 {
2568 static PyObject *exit, *enter;
2569 w = TOP();
2570 x = special_lookup(w, "__exit__", &exit);
2571 if (!x)
2572 break;
2573 SET_TOP(x);
2574 u = special_lookup(w, "__enter__", &enter);
2575 Py_DECREF(w);
2576 if (!u) {
2577 x = NULL;
2578 break;
2579 }
2580 x = PyObject_CallFunctionObjArgs(u, NULL);
2581 Py_DECREF(u);
2582 if (!x)
2583 break;
2584 /* Setup a finally block (SETUP_WITH as a block is
2585 equivalent to SETUP_FINALLY except it normalizes
2586 the exception) before pushing the result of
2587 __enter__ on the stack. */
2588 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2589 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002590
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002591 PUSH(x);
2592 continue;
2593 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002594
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002595 case WITH_CLEANUP:
2596 {
2597 /* At the top of the stack are 1-3 values indicating
2598 how/why we entered the finally clause:
2599 - TOP = None
2600 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2601 - TOP = WHY_*; no retval below it
2602 - (TOP, SECOND, THIRD) = exc_info()
2603 Below them is EXIT, the context.__exit__ bound method.
2604 In the last case, we must call
2605 EXIT(TOP, SECOND, THIRD)
2606 otherwise we must call
2607 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002608
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002609 In all cases, we remove EXIT from the stack, leaving
2610 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002611
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002612 In addition, if the stack represents an exception,
2613 *and* the function call returns a 'true' value, we
2614 "zap" this information, to prevent END_FINALLY from
2615 re-raising the exception. (But non-local gotos
2616 should still be resumed.)
2617 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002618
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002619 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002620
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002621 u = POP();
2622 if (u == Py_None) {
2623 exit_func = TOP();
2624 SET_TOP(u);
2625 v = w = Py_None;
2626 }
2627 else if (PyInt_Check(u)) {
2628 switch(PyInt_AS_LONG(u)) {
2629 case WHY_RETURN:
2630 case WHY_CONTINUE:
2631 /* Retval in TOP. */
2632 exit_func = SECOND();
2633 SET_SECOND(TOP());
2634 SET_TOP(u);
2635 break;
2636 default:
2637 exit_func = TOP();
2638 SET_TOP(u);
2639 break;
2640 }
2641 u = v = w = Py_None;
2642 }
2643 else {
2644 v = TOP();
2645 w = SECOND();
2646 exit_func = THIRD();
2647 SET_TOP(u);
2648 SET_SECOND(v);
2649 SET_THIRD(w);
2650 }
2651 /* XXX Not the fastest way to call it... */
2652 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2653 NULL);
2654 Py_DECREF(exit_func);
2655 if (x == NULL)
2656 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002657
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002658 if (u != Py_None)
2659 err = PyObject_IsTrue(x);
2660 else
2661 err = 0;
2662 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002663
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002664 if (err < 0)
2665 break; /* Go to error exit */
2666 else if (err > 0) {
2667 err = 0;
2668 /* There was an exception and a true return */
2669 STACKADJ(-2);
2670 Py_INCREF(Py_None);
2671 SET_TOP(Py_None);
2672 Py_DECREF(u);
2673 Py_DECREF(v);
2674 Py_DECREF(w);
2675 } else {
2676 /* The stack was rearranged to remove EXIT
2677 above. Let END_FINALLY do its thing */
2678 }
2679 PREDICT(END_FINALLY);
2680 break;
2681 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002682
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002683 case CALL_FUNCTION:
2684 {
2685 PyObject **sp;
2686 PCALL(PCALL_ALL);
2687 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002688#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002689 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002690#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002691 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002692#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002693 stack_pointer = sp;
2694 PUSH(x);
2695 if (x != NULL)
2696 continue;
2697 break;
2698 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002699
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002700 case CALL_FUNCTION_VAR:
2701 case CALL_FUNCTION_KW:
2702 case CALL_FUNCTION_VAR_KW:
2703 {
2704 int na = oparg & 0xff;
2705 int nk = (oparg>>8) & 0xff;
2706 int flags = (opcode - CALL_FUNCTION) & 3;
2707 int n = na + 2 * nk;
2708 PyObject **pfunc, *func, **sp;
2709 PCALL(PCALL_ALL);
2710 if (flags & CALL_FLAG_VAR)
2711 n++;
2712 if (flags & CALL_FLAG_KW)
2713 n++;
2714 pfunc = stack_pointer - n - 1;
2715 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00002716
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002717 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00002718 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002719 PyObject *self = PyMethod_GET_SELF(func);
2720 Py_INCREF(self);
2721 func = PyMethod_GET_FUNCTION(func);
2722 Py_INCREF(func);
2723 Py_DECREF(*pfunc);
2724 *pfunc = self;
2725 na++;
2726 } else
2727 Py_INCREF(func);
2728 sp = stack_pointer;
2729 READ_TIMESTAMP(intr0);
2730 x = ext_do_call(func, &sp, flags, na, nk);
2731 READ_TIMESTAMP(intr1);
2732 stack_pointer = sp;
2733 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00002734
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002735 while (stack_pointer > pfunc) {
2736 w = POP();
2737 Py_DECREF(w);
2738 }
2739 PUSH(x);
2740 if (x != NULL)
2741 continue;
2742 break;
2743 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002744
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002745 case MAKE_FUNCTION:
2746 v = POP(); /* code object */
2747 x = PyFunction_New(v, f->f_globals);
2748 Py_DECREF(v);
2749 /* XXX Maybe this should be a separate opcode? */
2750 if (x != NULL && oparg > 0) {
2751 v = PyTuple_New(oparg);
2752 if (v == NULL) {
2753 Py_DECREF(x);
2754 x = NULL;
2755 break;
2756 }
2757 while (--oparg >= 0) {
2758 w = POP();
2759 PyTuple_SET_ITEM(v, oparg, w);
2760 }
2761 err = PyFunction_SetDefaults(x, v);
2762 Py_DECREF(v);
2763 }
2764 PUSH(x);
2765 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002766
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002767 case MAKE_CLOSURE:
2768 {
2769 v = POP(); /* code object */
2770 x = PyFunction_New(v, f->f_globals);
2771 Py_DECREF(v);
2772 if (x != NULL) {
2773 v = POP();
2774 if (PyFunction_SetClosure(x, v) != 0) {
2775 /* Can't happen unless bytecode is corrupt. */
2776 why = WHY_EXCEPTION;
2777 }
2778 Py_DECREF(v);
2779 }
2780 if (x != NULL && oparg > 0) {
2781 v = PyTuple_New(oparg);
2782 if (v == NULL) {
2783 Py_DECREF(x);
2784 x = NULL;
2785 break;
2786 }
2787 while (--oparg >= 0) {
2788 w = POP();
2789 PyTuple_SET_ITEM(v, oparg, w);
2790 }
2791 if (PyFunction_SetDefaults(x, v) != 0) {
2792 /* Can't happen unless
2793 PyFunction_SetDefaults changes. */
2794 why = WHY_EXCEPTION;
2795 }
2796 Py_DECREF(v);
2797 }
2798 PUSH(x);
2799 break;
2800 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002802 case BUILD_SLICE:
2803 if (oparg == 3)
2804 w = POP();
2805 else
2806 w = NULL;
2807 v = POP();
2808 u = TOP();
2809 x = PySlice_New(u, v, w);
2810 Py_DECREF(u);
2811 Py_DECREF(v);
2812 Py_XDECREF(w);
2813 SET_TOP(x);
2814 if (x != NULL) continue;
2815 break;
Guido van Rossum8861b741996-07-30 16:49:37 +00002816
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002817 case EXTENDED_ARG:
2818 opcode = NEXTOP();
2819 oparg = oparg<<16 | NEXTARG();
2820 goto dispatch_opcode;
Guido van Rossum8861b741996-07-30 16:49:37 +00002821
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002822 default:
2823 fprintf(stderr,
2824 "XXX lineno: %d, opcode: %d\n",
2825 PyFrame_GetLineNumber(f),
2826 opcode);
2827 PyErr_SetString(PyExc_SystemError, "unknown opcode");
2828 why = WHY_EXCEPTION;
2829 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00002830
2831#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002832 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002833#endif
2834
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002835 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00002836
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002837 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00002838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002839 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002840
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002841 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002842
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002843 if (why == WHY_NOT) {
2844 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00002845#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002846 /* This check is expensive! */
2847 if (PyErr_Occurred())
2848 fprintf(stderr,
2849 "XXX undetected error\n");
2850 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002851#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002852 READ_TIMESTAMP(loop1);
2853 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002854#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002855 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002856#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002857 }
2858 why = WHY_EXCEPTION;
2859 x = Py_None;
2860 err = 0;
2861 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002862
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002863 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00002864
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002865 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
2866 if (!PyErr_Occurred()) {
2867 PyErr_SetString(PyExc_SystemError,
2868 "error return without exception set");
2869 why = WHY_EXCEPTION;
2870 }
2871 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00002872#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002873 else {
2874 /* This check is expensive! */
2875 if (PyErr_Occurred()) {
2876 char buf[128];
2877 sprintf(buf, "Stack unwind with exception "
2878 "set and why=%d", why);
2879 Py_FatalError(buf);
2880 }
2881 }
Guido van Rossum374a9221991-04-04 10:40:29 +00002882#endif
2883
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002884 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00002885
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002886 if (why == WHY_EXCEPTION) {
2887 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00002888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002889 if (tstate->c_tracefunc != NULL)
2890 call_exc_trace(tstate->c_tracefunc,
2891 tstate->c_traceobj, f);
2892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002894 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00002895
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002896 if (why == WHY_RERAISE)
2897 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00002898
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002899 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00002900
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002901fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002902 while (why != WHY_NOT && f->f_iblock > 0) {
2903 /* Peek at the current block. */
2904 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002905
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002906 assert(why != WHY_YIELD);
2907 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
2908 why = WHY_NOT;
2909 JUMPTO(PyInt_AS_LONG(retval));
2910 Py_DECREF(retval);
2911 break;
2912 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00002913
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002914 /* Now we have to pop the block. */
2915 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00002916
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002917 while (STACK_LEVEL() > b->b_level) {
2918 v = POP();
2919 Py_XDECREF(v);
2920 }
2921 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
2922 why = WHY_NOT;
2923 JUMPTO(b->b_handler);
2924 break;
2925 }
2926 if (b->b_type == SETUP_FINALLY ||
2927 (b->b_type == SETUP_EXCEPT &&
2928 why == WHY_EXCEPTION) ||
2929 b->b_type == SETUP_WITH) {
2930 if (why == WHY_EXCEPTION) {
2931 PyObject *exc, *val, *tb;
2932 PyErr_Fetch(&exc, &val, &tb);
2933 if (val == NULL) {
2934 val = Py_None;
2935 Py_INCREF(val);
2936 }
2937 /* Make the raw exception data
2938 available to the handler,
2939 so a program can emulate the
2940 Python main loop. Don't do
2941 this for 'finally'. */
2942 if (b->b_type == SETUP_EXCEPT ||
2943 b->b_type == SETUP_WITH) {
2944 PyErr_NormalizeException(
2945 &exc, &val, &tb);
2946 set_exc_info(tstate,
2947 exc, val, tb);
2948 }
2949 if (tb == NULL) {
2950 Py_INCREF(Py_None);
2951 PUSH(Py_None);
2952 } else
2953 PUSH(tb);
2954 PUSH(val);
2955 PUSH(exc);
2956 }
2957 else {
2958 if (why & (WHY_RETURN | WHY_CONTINUE))
2959 PUSH(retval);
2960 v = PyInt_FromLong((long)why);
2961 PUSH(v);
2962 }
2963 why = WHY_NOT;
2964 JUMPTO(b->b_handler);
2965 break;
2966 }
2967 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00002968
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002969 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00002970
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002971 if (why != WHY_NOT)
2972 break;
2973 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00002974
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002975 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00002976
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002977 assert(why != WHY_YIELD);
2978 /* Pop remaining stack entries. */
2979 while (!EMPTY()) {
2980 v = POP();
2981 Py_XDECREF(v);
2982 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00002983
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002984 if (why != WHY_RETURN)
2985 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00002986
Raymond Hettinger1dd83092004-02-06 18:32:33 +00002987fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002988 if (tstate->use_tracing) {
2989 if (tstate->c_tracefunc) {
2990 if (why == WHY_RETURN || why == WHY_YIELD) {
2991 if (call_trace(tstate->c_tracefunc,
2992 tstate->c_traceobj, f,
2993 PyTrace_RETURN, retval)) {
2994 Py_XDECREF(retval);
2995 retval = NULL;
2996 why = WHY_EXCEPTION;
2997 }
2998 }
2999 else if (why == WHY_EXCEPTION) {
3000 call_trace_protected(tstate->c_tracefunc,
3001 tstate->c_traceobj, f,
3002 PyTrace_RETURN, NULL);
3003 }
3004 }
3005 if (tstate->c_profilefunc) {
3006 if (why == WHY_EXCEPTION)
3007 call_trace_protected(tstate->c_profilefunc,
3008 tstate->c_profileobj, f,
3009 PyTrace_RETURN, NULL);
3010 else if (call_trace(tstate->c_profilefunc,
3011 tstate->c_profileobj, f,
3012 PyTrace_RETURN, retval)) {
3013 Py_XDECREF(retval);
3014 retval = NULL;
3015 why = WHY_EXCEPTION;
3016 }
3017 }
3018 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003020 if (tstate->frame->f_exc_type != NULL)
3021 reset_exc_info(tstate);
3022 else {
3023 assert(tstate->frame->f_exc_value == NULL);
3024 assert(tstate->frame->f_exc_traceback == NULL);
3025 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003026
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003027 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003028exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003029 Py_LeaveRecursiveCall();
3030 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003031
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003032 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003033}
3034
Guido van Rossumc2e20742006-02-27 22:32:47 +00003035/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003036 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003037 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003038
Tim Peters6d6c1a32001-08-02 04:15:00 +00003039PyObject *
3040PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003041 PyObject **args, int argcount, PyObject **kws, int kwcount,
3042 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003043{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003044 register PyFrameObject *f;
3045 register PyObject *retval = NULL;
3046 register PyObject **fastlocals, **freevars;
3047 PyThreadState *tstate = PyThreadState_GET();
3048 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003049
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003050 if (globals == NULL) {
3051 PyErr_SetString(PyExc_SystemError,
3052 "PyEval_EvalCodeEx: NULL globals");
3053 return NULL;
3054 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003055
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003056 assert(tstate != NULL);
3057 assert(globals != NULL);
3058 f = PyFrame_New(tstate, co, globals, locals);
3059 if (f == NULL)
3060 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003061
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003062 fastlocals = f->f_localsplus;
3063 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003065 if (co->co_argcount > 0 ||
3066 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3067 int i;
3068 int n = argcount;
3069 PyObject *kwdict = NULL;
3070 if (co->co_flags & CO_VARKEYWORDS) {
3071 kwdict = PyDict_New();
3072 if (kwdict == NULL)
3073 goto fail;
3074 i = co->co_argcount;
3075 if (co->co_flags & CO_VARARGS)
3076 i++;
3077 SETLOCAL(i, kwdict);
3078 }
3079 if (argcount > co->co_argcount) {
3080 if (!(co->co_flags & CO_VARARGS)) {
3081 PyErr_Format(PyExc_TypeError,
3082 "%.200s() takes %s %d "
3083 "argument%s (%d given)",
3084 PyString_AsString(co->co_name),
3085 defcount ? "at most" : "exactly",
3086 co->co_argcount,
3087 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003088 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003089 goto fail;
3090 }
3091 n = co->co_argcount;
3092 }
3093 for (i = 0; i < n; i++) {
3094 x = args[i];
3095 Py_INCREF(x);
3096 SETLOCAL(i, x);
3097 }
3098 if (co->co_flags & CO_VARARGS) {
3099 u = PyTuple_New(argcount - n);
3100 if (u == NULL)
3101 goto fail;
3102 SETLOCAL(co->co_argcount, u);
3103 for (i = n; i < argcount; i++) {
3104 x = args[i];
3105 Py_INCREF(x);
3106 PyTuple_SET_ITEM(u, i-n, x);
3107 }
3108 }
3109 for (i = 0; i < kwcount; i++) {
3110 PyObject **co_varnames;
3111 PyObject *keyword = kws[2*i];
3112 PyObject *value = kws[2*i + 1];
3113 int j;
3114 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003115#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003116 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003117#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003118 )) {
3119 PyErr_Format(PyExc_TypeError,
3120 "%.200s() keywords must be strings",
3121 PyString_AsString(co->co_name));
3122 goto fail;
3123 }
3124 /* Speed hack: do raw pointer compares. As names are
3125 normally interned this should almost always hit. */
3126 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3127 for (j = 0; j < co->co_argcount; j++) {
3128 PyObject *nm = co_varnames[j];
3129 if (nm == keyword)
3130 goto kw_found;
3131 }
3132 /* Slow fallback, just in case */
3133 for (j = 0; j < co->co_argcount; j++) {
3134 PyObject *nm = co_varnames[j];
3135 int cmp = PyObject_RichCompareBool(
3136 keyword, nm, Py_EQ);
3137 if (cmp > 0)
3138 goto kw_found;
3139 else if (cmp < 0)
3140 goto fail;
3141 }
3142 if (kwdict == NULL) {
3143 PyObject *kwd_str = kwd_as_string(keyword);
3144 if (kwd_str) {
3145 PyErr_Format(PyExc_TypeError,
3146 "%.200s() got an unexpected "
3147 "keyword argument '%.400s'",
3148 PyString_AsString(co->co_name),
3149 PyString_AsString(kwd_str));
3150 Py_DECREF(kwd_str);
3151 }
3152 goto fail;
3153 }
3154 PyDict_SetItem(kwdict, keyword, value);
3155 continue;
3156 kw_found:
3157 if (GETLOCAL(j) != NULL) {
3158 PyObject *kwd_str = kwd_as_string(keyword);
3159 if (kwd_str) {
3160 PyErr_Format(PyExc_TypeError,
3161 "%.200s() got multiple "
3162 "values for keyword "
3163 "argument '%.400s'",
3164 PyString_AsString(co->co_name),
3165 PyString_AsString(kwd_str));
3166 Py_DECREF(kwd_str);
3167 }
3168 goto fail;
3169 }
3170 Py_INCREF(value);
3171 SETLOCAL(j, value);
3172 }
3173 if (argcount < co->co_argcount) {
3174 int m = co->co_argcount - defcount;
3175 for (i = argcount; i < m; i++) {
3176 if (GETLOCAL(i) == NULL) {
3177 int j, given = 0;
3178 for (j = 0; j < co->co_argcount; j++)
3179 if (GETLOCAL(j))
3180 given++;
3181 PyErr_Format(PyExc_TypeError,
3182 "%.200s() takes %s %d "
3183 "argument%s (%d given)",
3184 PyString_AsString(co->co_name),
3185 ((co->co_flags & CO_VARARGS) ||
3186 defcount) ? "at least"
3187 : "exactly",
3188 m, m == 1 ? "" : "s", given);
3189 goto fail;
3190 }
3191 }
3192 if (n > m)
3193 i = n - m;
3194 else
3195 i = 0;
3196 for (; i < defcount; i++) {
3197 if (GETLOCAL(m+i) == NULL) {
3198 PyObject *def = defs[i];
3199 Py_INCREF(def);
3200 SETLOCAL(m+i, def);
3201 }
3202 }
3203 }
3204 }
3205 else if (argcount > 0 || kwcount > 0) {
3206 PyErr_Format(PyExc_TypeError,
3207 "%.200s() takes no arguments (%d given)",
3208 PyString_AsString(co->co_name),
3209 argcount + kwcount);
3210 goto fail;
3211 }
3212 /* Allocate and initialize storage for cell vars, and copy free
3213 vars into frame. This isn't too efficient right now. */
3214 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3215 int i, j, nargs, found;
3216 char *cellname, *argname;
3217 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003219 nargs = co->co_argcount;
3220 if (co->co_flags & CO_VARARGS)
3221 nargs++;
3222 if (co->co_flags & CO_VARKEYWORDS)
3223 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003224
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003225 /* Initialize each cell var, taking into account
3226 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003227
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003228 Should arrange for the compiler to put cellvars
3229 that are arguments at the beginning of the cellvars
3230 list so that we can march over it more efficiently?
3231 */
3232 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3233 cellname = PyString_AS_STRING(
3234 PyTuple_GET_ITEM(co->co_cellvars, i));
3235 found = 0;
3236 for (j = 0; j < nargs; j++) {
3237 argname = PyString_AS_STRING(
3238 PyTuple_GET_ITEM(co->co_varnames, j));
3239 if (strcmp(cellname, argname) == 0) {
3240 c = PyCell_New(GETLOCAL(j));
3241 if (c == NULL)
3242 goto fail;
3243 GETLOCAL(co->co_nlocals + i) = c;
3244 found = 1;
3245 break;
3246 }
3247 }
3248 if (found == 0) {
3249 c = PyCell_New(NULL);
3250 if (c == NULL)
3251 goto fail;
3252 SETLOCAL(co->co_nlocals + i, c);
3253 }
3254 }
3255 }
3256 if (PyTuple_GET_SIZE(co->co_freevars)) {
3257 int i;
3258 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3259 PyObject *o = PyTuple_GET_ITEM(closure, i);
3260 Py_INCREF(o);
3261 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3262 }
3263 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003264
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003265 if (co->co_flags & CO_GENERATOR) {
3266 /* Don't need to keep the reference to f_back, it will be set
3267 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003268 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003269
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003270 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003271
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003272 /* Create a new generator that owns the ready to run frame
3273 * and return that as the value. */
3274 return PyGen_New(f);
3275 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003276
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003277 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003278
Thomas Woutersae406c62007-09-19 17:27:43 +00003279fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003280
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003281 /* decref'ing the frame can cause __del__ methods to get invoked,
3282 which can call back into Python. While we're done with the
3283 current Python frame (f), the associated C stack is still in use,
3284 so recursion_depth must be boosted for the duration.
3285 */
3286 assert(tstate != NULL);
3287 ++tstate->recursion_depth;
3288 Py_DECREF(f);
3289 --tstate->recursion_depth;
3290 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003291}
3292
3293
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003294static PyObject *
3295special_lookup(PyObject *o, char *meth, PyObject **cache)
3296{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003297 PyObject *res;
3298 if (PyInstance_Check(o)) {
3299 if (!*cache)
3300 return PyObject_GetAttrString(o, meth);
3301 else
3302 return PyObject_GetAttr(o, *cache);
3303 }
3304 res = _PyObject_LookupSpecial(o, meth, cache);
3305 if (res == NULL && !PyErr_Occurred()) {
3306 PyErr_SetObject(PyExc_AttributeError, *cache);
3307 return NULL;
3308 }
3309 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003310}
3311
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003312
Benjamin Petersone18ef192009-01-20 14:21:16 +00003313static PyObject *
3314kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003315#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003316 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003317#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003318 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003319#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003320 Py_INCREF(kwd);
3321 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003322#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003323 }
3324 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003325#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003326}
3327
3328
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003329/* Implementation notes for set_exc_info() and reset_exc_info():
3330
3331- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3332 'exc_traceback'. These always travel together.
3333
3334- tstate->curexc_ZZZ is the "hot" exception that is set by
3335 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3336
3337- Once an exception is caught by an except clause, it is transferred
3338 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3339 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003340 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003341
3342- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3343
3344 Long ago, when none of this existed, there were just a few globals:
3345 one set corresponding to the "hot" exception, and one set
3346 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3347 globals; they were simply stored as sys.exc_ZZZ. For backwards
3348 compatibility, they still are!) The problem was that in code like
3349 this:
3350
3351 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003352 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003353 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003354 "do something else first"
3355 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003356
3357 if "do something else first" invoked something that raised and caught
3358 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3359 cause of subtle bugs. I fixed this by changing the semantics as
3360 follows:
3361
3362 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3363 *in that frame*.
3364
3365 - But initially, and as long as no exception is caught in a given
3366 frame, sys.exc_ZZZ will hold the last exception caught in the
3367 previous frame (or the frame before that, etc.).
3368
3369 The first bullet fixed the bug in the above example. The second
3370 bullet was for backwards compatibility: it was (and is) common to
3371 have a function that is called when an exception is caught, and to
3372 have that function access the caught exception via sys.exc_ZZZ.
3373 (Example: traceback.print_exc()).
3374
3375 At the same time I fixed the problem that sys.exc_ZZZ weren't
3376 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3377 but that's really a separate improvement.
3378
3379 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3380 variables to what they were before the current frame was called. The
3381 set_exc_info() function saves them on the frame so that
3382 reset_exc_info() can restore them. The invariant is that
3383 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3384 exception (where "catching" an exception applies only to successful
3385 except clauses); and if the current frame ever caught an exception,
3386 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3387 at the start of the current frame.
3388
3389*/
3390
Fredrik Lundh7a830892006-05-27 10:39:48 +00003391static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003392set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003393 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003394{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003395 PyFrameObject *frame = tstate->frame;
3396 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003397
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003398 assert(type != NULL);
3399 assert(frame != NULL);
3400 if (frame->f_exc_type == NULL) {
3401 assert(frame->f_exc_value == NULL);
3402 assert(frame->f_exc_traceback == NULL);
3403 /* This frame didn't catch an exception before. */
3404 /* Save previous exception of this thread in this frame. */
3405 if (tstate->exc_type == NULL) {
3406 /* XXX Why is this set to Py_None? */
3407 Py_INCREF(Py_None);
3408 tstate->exc_type = Py_None;
3409 }
3410 Py_INCREF(tstate->exc_type);
3411 Py_XINCREF(tstate->exc_value);
3412 Py_XINCREF(tstate->exc_traceback);
3413 frame->f_exc_type = tstate->exc_type;
3414 frame->f_exc_value = tstate->exc_value;
3415 frame->f_exc_traceback = tstate->exc_traceback;
3416 }
3417 /* Set new exception for this thread. */
3418 tmp_type = tstate->exc_type;
3419 tmp_value = tstate->exc_value;
3420 tmp_tb = tstate->exc_traceback;
3421 Py_INCREF(type);
3422 Py_XINCREF(value);
3423 Py_XINCREF(tb);
3424 tstate->exc_type = type;
3425 tstate->exc_value = value;
3426 tstate->exc_traceback = tb;
3427 Py_XDECREF(tmp_type);
3428 Py_XDECREF(tmp_value);
3429 Py_XDECREF(tmp_tb);
3430 /* For b/w compatibility */
3431 PySys_SetObject("exc_type", type);
3432 PySys_SetObject("exc_value", value);
3433 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003434}
3435
Fredrik Lundh7a830892006-05-27 10:39:48 +00003436static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003437reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003438{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003439 PyFrameObject *frame;
3440 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003441
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003442 /* It's a precondition that the thread state's frame caught an
3443 * exception -- verify in a debug build.
3444 */
3445 assert(tstate != NULL);
3446 frame = tstate->frame;
3447 assert(frame != NULL);
3448 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003449
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003450 /* Copy the frame's exception info back to the thread state. */
3451 tmp_type = tstate->exc_type;
3452 tmp_value = tstate->exc_value;
3453 tmp_tb = tstate->exc_traceback;
3454 Py_INCREF(frame->f_exc_type);
3455 Py_XINCREF(frame->f_exc_value);
3456 Py_XINCREF(frame->f_exc_traceback);
3457 tstate->exc_type = frame->f_exc_type;
3458 tstate->exc_value = frame->f_exc_value;
3459 tstate->exc_traceback = frame->f_exc_traceback;
3460 Py_XDECREF(tmp_type);
3461 Py_XDECREF(tmp_value);
3462 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003463
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003464 /* For b/w compatibility */
3465 PySys_SetObject("exc_type", frame->f_exc_type);
3466 PySys_SetObject("exc_value", frame->f_exc_value);
3467 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003468
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003469 /* Clear the frame's exception info. */
3470 tmp_type = frame->f_exc_type;
3471 tmp_value = frame->f_exc_value;
3472 tmp_tb = frame->f_exc_traceback;
3473 frame->f_exc_type = NULL;
3474 frame->f_exc_value = NULL;
3475 frame->f_exc_traceback = NULL;
3476 Py_DECREF(tmp_type);
3477 Py_XDECREF(tmp_value);
3478 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003479}
3480
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003481/* Logic for the raise statement (too complicated for inlining).
3482 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003483static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003484do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003485{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003486 if (type == NULL) {
3487 /* Reraise */
3488 PyThreadState *tstate = PyThreadState_GET();
3489 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3490 value = tstate->exc_value;
3491 tb = tstate->exc_traceback;
3492 Py_XINCREF(type);
3493 Py_XINCREF(value);
3494 Py_XINCREF(tb);
3495 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003496
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003497 /* We support the following forms of raise:
3498 raise <class>, <classinstance>
3499 raise <class>, <argument tuple>
3500 raise <class>, None
3501 raise <class>, <argument>
3502 raise <classinstance>, None
3503 raise <string>, <object>
3504 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003505
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003506 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003507
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003508 In addition, raise <tuple>, <anything> is the same as
3509 raising the tuple's first item (and it better have one!);
3510 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003511
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003512 Finally, an optional third argument can be supplied, which
3513 gives the traceback to be substituted (useful when
3514 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003515
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003516 /* First, check the traceback argument, replacing None with
3517 NULL. */
3518 if (tb == Py_None) {
3519 Py_DECREF(tb);
3520 tb = NULL;
3521 }
3522 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3523 PyErr_SetString(PyExc_TypeError,
3524 "raise: arg 3 must be a traceback or None");
3525 goto raise_error;
3526 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003527
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003528 /* Next, replace a missing value with None */
3529 if (value == NULL) {
3530 value = Py_None;
3531 Py_INCREF(value);
3532 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003533
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003534 /* Next, repeatedly, replace a tuple exception with its first item */
3535 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3536 PyObject *tmp = type;
3537 type = PyTuple_GET_ITEM(type, 0);
3538 Py_INCREF(type);
3539 Py_DECREF(tmp);
3540 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003541
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003542 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003543 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003544 if (!PyExceptionInstance_Check(value)) {
3545 PyErr_Format(PyExc_TypeError,
3546 "calling %s() should have returned an instance of "
3547 "BaseException, not '%s'",
3548 ((PyTypeObject *)type)->tp_name,
3549 Py_TYPE(value)->tp_name);
3550 goto raise_error;
3551 }
3552 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003553 else if (PyExceptionInstance_Check(type)) {
3554 /* Raising an instance. The value should be a dummy. */
3555 if (value != Py_None) {
3556 PyErr_SetString(PyExc_TypeError,
3557 "instance exception may not have a separate value");
3558 goto raise_error;
3559 }
3560 else {
3561 /* Normalize to raise <class>, <instance> */
3562 Py_DECREF(value);
3563 value = type;
3564 type = PyExceptionInstance_Class(type);
3565 Py_INCREF(type);
3566 }
3567 }
3568 else {
3569 /* Not something you can raise. You get an exception
3570 anyway, just not what you specified :-) */
3571 PyErr_Format(PyExc_TypeError,
3572 "exceptions must be old-style classes or "
3573 "derived from BaseException, not %s",
3574 type->ob_type->tp_name);
3575 goto raise_error;
3576 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003577
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003578 assert(PyExceptionClass_Check(type));
3579 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3580 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3581 "exceptions must derive from BaseException "
3582 "in 3.x", 1) < 0)
3583 goto raise_error;
3584 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003586 PyErr_Restore(type, value, tb);
3587 if (tb == NULL)
3588 return WHY_EXCEPTION;
3589 else
3590 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003591 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003592 Py_XDECREF(value);
3593 Py_XDECREF(type);
3594 Py_XDECREF(tb);
3595 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003596}
3597
Tim Petersd6d010b2001-06-21 02:49:55 +00003598/* Iterate v argcnt times and store the results on the stack (via decreasing
3599 sp). Return 1 for success, 0 if error. */
3600
Fredrik Lundh7a830892006-05-27 10:39:48 +00003601static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003602unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003603{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003604 int i = 0;
3605 PyObject *it; /* iter(v) */
3606 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003607
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003608 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003609
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003610 it = PyObject_GetIter(v);
3611 if (it == NULL)
3612 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003613
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003614 for (; i < argcnt; i++) {
3615 w = PyIter_Next(it);
3616 if (w == NULL) {
3617 /* Iterator done, via error or exhaustion. */
3618 if (!PyErr_Occurred()) {
3619 PyErr_Format(PyExc_ValueError,
3620 "need more than %d value%s to unpack",
3621 i, i == 1 ? "" : "s");
3622 }
3623 goto Error;
3624 }
3625 *--sp = w;
3626 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003627
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003628 /* We better have exhausted the iterator now. */
3629 w = PyIter_Next(it);
3630 if (w == NULL) {
3631 if (PyErr_Occurred())
3632 goto Error;
3633 Py_DECREF(it);
3634 return 1;
3635 }
3636 Py_DECREF(w);
3637 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3638 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003639Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003640 for (; i > 0; i--, sp++)
3641 Py_DECREF(*sp);
3642 Py_XDECREF(it);
3643 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003644}
3645
3646
Guido van Rossum96a42c81992-01-12 02:29:51 +00003647#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003648static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003649prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003650{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003651 printf("%s ", str);
3652 if (PyObject_Print(v, stdout, 0) != 0)
3653 PyErr_Clear(); /* Don't know what else to do */
3654 printf("\n");
3655 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003656}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003657#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003658
Fredrik Lundh7a830892006-05-27 10:39:48 +00003659static void
Fred Drake5755ce62001-06-27 19:19:46 +00003660call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003661{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003662 PyObject *type, *value, *traceback, *arg;
3663 int err;
3664 PyErr_Fetch(&type, &value, &traceback);
3665 if (value == NULL) {
3666 value = Py_None;
3667 Py_INCREF(value);
3668 }
3669 arg = PyTuple_Pack(3, type, value, traceback);
3670 if (arg == NULL) {
3671 PyErr_Restore(type, value, traceback);
3672 return;
3673 }
3674 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3675 Py_DECREF(arg);
3676 if (err == 0)
3677 PyErr_Restore(type, value, traceback);
3678 else {
3679 Py_XDECREF(type);
3680 Py_XDECREF(value);
3681 Py_XDECREF(traceback);
3682 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003683}
3684
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003685static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003686call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003687 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003688{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003689 PyObject *type, *value, *traceback;
3690 int err;
3691 PyErr_Fetch(&type, &value, &traceback);
3692 err = call_trace(func, obj, frame, what, arg);
3693 if (err == 0)
3694 {
3695 PyErr_Restore(type, value, traceback);
3696 return 0;
3697 }
3698 else {
3699 Py_XDECREF(type);
3700 Py_XDECREF(value);
3701 Py_XDECREF(traceback);
3702 return -1;
3703 }
Fred Drake4ec5d562001-10-04 19:26:43 +00003704}
3705
Fredrik Lundh7a830892006-05-27 10:39:48 +00003706static int
Fred Drake5755ce62001-06-27 19:19:46 +00003707call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003708 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00003709{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003710 register PyThreadState *tstate = frame->f_tstate;
3711 int result;
3712 if (tstate->tracing)
3713 return 0;
3714 tstate->tracing++;
3715 tstate->use_tracing = 0;
3716 result = func(obj, frame, what, arg);
3717 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3718 || (tstate->c_profilefunc != NULL));
3719 tstate->tracing--;
3720 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00003721}
3722
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003723PyObject *
3724_PyEval_CallTracing(PyObject *func, PyObject *args)
3725{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003726 PyFrameObject *frame = PyEval_GetFrame();
3727 PyThreadState *tstate = frame->f_tstate;
3728 int save_tracing = tstate->tracing;
3729 int save_use_tracing = tstate->use_tracing;
3730 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003731
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003732 tstate->tracing = 0;
3733 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
3734 || (tstate->c_profilefunc != NULL));
3735 result = PyObject_Call(func, args, NULL);
3736 tstate->tracing = save_tracing;
3737 tstate->use_tracing = save_use_tracing;
3738 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00003739}
3740
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00003741/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003742static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00003743maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003744 PyFrameObject *frame, int *instr_lb, int *instr_ub,
3745 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003746{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003747 int result = 0;
3748 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00003749
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003750 /* If the last instruction executed isn't in the current
3751 instruction window, reset the window.
3752 */
3753 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
3754 PyAddrPair bounds;
3755 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
3756 &bounds);
3757 *instr_lb = bounds.ap_lower;
3758 *instr_ub = bounds.ap_upper;
3759 }
3760 /* If the last instruction falls at the start of a line or if
3761 it represents a jump backwards, update the frame's line
3762 number and call the trace function. */
3763 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
3764 frame->f_lineno = line;
3765 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
3766 }
3767 *instr_prev = frame->f_lasti;
3768 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00003769}
3770
Fred Drake5755ce62001-06-27 19:19:46 +00003771void
3772PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00003773{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003774 PyThreadState *tstate = PyThreadState_GET();
3775 PyObject *temp = tstate->c_profileobj;
3776 Py_XINCREF(arg);
3777 tstate->c_profilefunc = NULL;
3778 tstate->c_profileobj = NULL;
3779 /* Must make sure that tracing is not ignored if 'temp' is freed */
3780 tstate->use_tracing = tstate->c_tracefunc != NULL;
3781 Py_XDECREF(temp);
3782 tstate->c_profilefunc = func;
3783 tstate->c_profileobj = arg;
3784 /* Flag that tracing or profiling is turned on */
3785 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00003786}
3787
3788void
3789PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
3790{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003791 PyThreadState *tstate = PyThreadState_GET();
3792 PyObject *temp = tstate->c_traceobj;
3793 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
3794 Py_XINCREF(arg);
3795 tstate->c_tracefunc = NULL;
3796 tstate->c_traceobj = NULL;
3797 /* Must make sure that profiling is not ignored if 'temp' is freed */
3798 tstate->use_tracing = tstate->c_profilefunc != NULL;
3799 Py_XDECREF(temp);
3800 tstate->c_tracefunc = func;
3801 tstate->c_traceobj = arg;
3802 /* Flag that tracing or profiling is turned on */
3803 tstate->use_tracing = ((func != NULL)
3804 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00003805}
3806
Guido van Rossumb209a111997-04-29 18:18:01 +00003807PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003808PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003809{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003810 PyFrameObject *current_frame = PyEval_GetFrame();
3811 if (current_frame == NULL)
3812 return PyThreadState_GET()->interp->builtins;
3813 else
3814 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00003815}
3816
Guido van Rossumb209a111997-04-29 18:18:01 +00003817PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003818PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00003819{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003820 PyFrameObject *current_frame = PyEval_GetFrame();
3821 if (current_frame == NULL)
3822 return NULL;
3823 PyFrame_FastToLocals(current_frame);
3824 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00003825}
3826
Guido van Rossumb209a111997-04-29 18:18:01 +00003827PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003828PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00003829{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003830 PyFrameObject *current_frame = PyEval_GetFrame();
3831 if (current_frame == NULL)
3832 return NULL;
3833 else
3834 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00003835}
3836
Guido van Rossum6297a7a2003-02-19 15:53:17 +00003837PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003838PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00003839{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003840 PyThreadState *tstate = PyThreadState_GET();
3841 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00003842}
3843
Guido van Rossum6135a871995-01-09 17:53:26 +00003844int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003845PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00003846{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003847 PyFrameObject *current_frame = PyEval_GetFrame();
3848 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00003849}
3850
Guido van Rossumbe270261997-05-22 22:26:18 +00003851int
Tim Peters5ba58662001-07-16 02:29:45 +00003852PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00003853{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003854 PyFrameObject *current_frame = PyEval_GetFrame();
3855 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00003856
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003857 if (current_frame != NULL) {
3858 const int codeflags = current_frame->f_code->co_flags;
3859 const int compilerflags = codeflags & PyCF_MASK;
3860 if (compilerflags) {
3861 result = 1;
3862 cf->cf_flags |= compilerflags;
3863 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003864#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003865 if (codeflags & CO_GENERATOR_ALLOWED) {
3866 result = 1;
3867 cf->cf_flags |= CO_GENERATOR_ALLOWED;
3868 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00003869#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003870 }
3871 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00003872}
3873
3874int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003875Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003876{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003877 PyObject *f = PySys_GetObject("stdout");
3878 if (f == NULL)
3879 return 0;
3880 if (!PyFile_SoftSpace(f, 0))
3881 return 0;
3882 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003883}
3884
Guido van Rossum3f5da241990-12-20 15:06:42 +00003885
Guido van Rossum681d79a1995-07-18 14:51:37 +00003886/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00003887 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00003888
Guido van Rossumb209a111997-04-29 18:18:01 +00003889PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003890PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00003891{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003892 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00003893
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003894 if (arg == NULL) {
3895 arg = PyTuple_New(0);
3896 if (arg == NULL)
3897 return NULL;
3898 }
3899 else if (!PyTuple_Check(arg)) {
3900 PyErr_SetString(PyExc_TypeError,
3901 "argument list must be a tuple");
3902 return NULL;
3903 }
3904 else
3905 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00003906
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003907 if (kw != NULL && !PyDict_Check(kw)) {
3908 PyErr_SetString(PyExc_TypeError,
3909 "keyword list must be a dictionary");
3910 Py_DECREF(arg);
3911 return NULL;
3912 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00003913
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003914 result = PyObject_Call(func, arg, kw);
3915 Py_DECREF(arg);
3916 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00003917}
3918
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003919const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003920PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003921{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003922 if (PyMethod_Check(func))
3923 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
3924 else if (PyFunction_Check(func))
3925 return PyString_AsString(((PyFunctionObject*)func)->func_name);
3926 else if (PyCFunction_Check(func))
3927 return ((PyCFunctionObject*)func)->m_ml->ml_name;
3928 else if (PyClass_Check(func))
3929 return PyString_AsString(((PyClassObject*)func)->cl_name);
3930 else if (PyInstance_Check(func)) {
3931 return PyString_AsString(
3932 ((PyInstanceObject*)func)->in_class->cl_name);
3933 } else {
3934 return func->ob_type->tp_name;
3935 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003936}
3937
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00003938const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00003939PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00003940{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003941 if (PyMethod_Check(func))
3942 return "()";
3943 else if (PyFunction_Check(func))
3944 return "()";
3945 else if (PyCFunction_Check(func))
3946 return "()";
3947 else if (PyClass_Check(func))
3948 return " constructor";
3949 else if (PyInstance_Check(func)) {
3950 return " instance";
3951 } else {
3952 return " object";
3953 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00003954}
3955
Fredrik Lundh7a830892006-05-27 10:39:48 +00003956static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00003957err_args(PyObject *func, int flags, int nargs)
3958{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003959 if (flags & METH_NOARGS)
3960 PyErr_Format(PyExc_TypeError,
3961 "%.200s() takes no arguments (%d given)",
3962 ((PyCFunctionObject *)func)->m_ml->ml_name,
3963 nargs);
3964 else
3965 PyErr_Format(PyExc_TypeError,
3966 "%.200s() takes exactly one argument (%d given)",
3967 ((PyCFunctionObject *)func)->m_ml->ml_name,
3968 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00003969}
3970
Armin Rigo1c2d7e52005-09-20 18:34:01 +00003971#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00003972if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003973 if (call_trace(tstate->c_profilefunc, \
3974 tstate->c_profileobj, \
3975 tstate->frame, PyTrace_C_CALL, \
3976 func)) { \
3977 x = NULL; \
3978 } \
3979 else { \
3980 x = call; \
3981 if (tstate->c_profilefunc != NULL) { \
3982 if (x == NULL) { \
3983 call_trace_protected(tstate->c_profilefunc, \
3984 tstate->c_profileobj, \
3985 tstate->frame, PyTrace_C_EXCEPTION, \
3986 func); \
3987 /* XXX should pass (type, value, tb) */ \
3988 } else { \
3989 if (call_trace(tstate->c_profilefunc, \
3990 tstate->c_profileobj, \
3991 tstate->frame, PyTrace_C_RETURN, \
3992 func)) { \
3993 Py_DECREF(x); \
3994 x = NULL; \
3995 } \
3996 } \
3997 } \
3998 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00003999} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004000 x = call; \
4001 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004002
Fredrik Lundh7a830892006-05-27 10:39:48 +00004003static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004004call_function(PyObject ***pp_stack, int oparg
4005#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004006 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004007#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004008 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004009{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004010 int na = oparg & 0xff;
4011 int nk = (oparg>>8) & 0xff;
4012 int n = na + 2 * nk;
4013 PyObject **pfunc = (*pp_stack) - n - 1;
4014 PyObject *func = *pfunc;
4015 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004016
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004017 /* Always dispatch PyCFunction first, because these are
4018 presumed to be the most frequent callable object.
4019 */
4020 if (PyCFunction_Check(func) && nk == 0) {
4021 int flags = PyCFunction_GET_FLAGS(func);
4022 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004023
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004024 PCALL(PCALL_CFUNCTION);
4025 if (flags & (METH_NOARGS | METH_O)) {
4026 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4027 PyObject *self = PyCFunction_GET_SELF(func);
4028 if (flags & METH_NOARGS && na == 0) {
4029 C_TRACE(x, (*meth)(self,NULL));
4030 }
4031 else if (flags & METH_O && na == 1) {
4032 PyObject *arg = EXT_POP(*pp_stack);
4033 C_TRACE(x, (*meth)(self,arg));
4034 Py_DECREF(arg);
4035 }
4036 else {
4037 err_args(func, flags, na);
4038 x = NULL;
4039 }
4040 }
4041 else {
4042 PyObject *callargs;
4043 callargs = load_args(pp_stack, na);
4044 READ_TIMESTAMP(*pintr0);
4045 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4046 READ_TIMESTAMP(*pintr1);
4047 Py_XDECREF(callargs);
4048 }
4049 } else {
4050 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4051 /* optimize access to bound methods */
4052 PyObject *self = PyMethod_GET_SELF(func);
4053 PCALL(PCALL_METHOD);
4054 PCALL(PCALL_BOUND_METHOD);
4055 Py_INCREF(self);
4056 func = PyMethod_GET_FUNCTION(func);
4057 Py_INCREF(func);
4058 Py_DECREF(*pfunc);
4059 *pfunc = self;
4060 na++;
4061 n++;
4062 } else
4063 Py_INCREF(func);
4064 READ_TIMESTAMP(*pintr0);
4065 if (PyFunction_Check(func))
4066 x = fast_function(func, pp_stack, n, na, nk);
4067 else
4068 x = do_call(func, pp_stack, na, nk);
4069 READ_TIMESTAMP(*pintr1);
4070 Py_DECREF(func);
4071 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004072
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004073 /* Clear the stack of the function object. Also removes
4074 the arguments in case they weren't consumed already
4075 (fast_function() and err_args() leave them on the stack).
4076 */
4077 while ((*pp_stack) > pfunc) {
4078 w = EXT_POP(*pp_stack);
4079 Py_DECREF(w);
4080 PCALL(PCALL_POP);
4081 }
4082 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004083}
4084
Jeremy Hylton192690e2002-08-16 18:36:11 +00004085/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004086 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004087 For the simplest case -- a function that takes only positional
4088 arguments and is called with only positional arguments -- it
4089 inlines the most primitive frame setup code from
4090 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4091 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004092*/
4093
Fredrik Lundh7a830892006-05-27 10:39:48 +00004094static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004095fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004096{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004097 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4098 PyObject *globals = PyFunction_GET_GLOBALS(func);
4099 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4100 PyObject **d = NULL;
4101 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004102
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004103 PCALL(PCALL_FUNCTION);
4104 PCALL(PCALL_FAST_FUNCTION);
4105 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4106 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4107 PyFrameObject *f;
4108 PyObject *retval = NULL;
4109 PyThreadState *tstate = PyThreadState_GET();
4110 PyObject **fastlocals, **stack;
4111 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004112
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004113 PCALL(PCALL_FASTER_FUNCTION);
4114 assert(globals != NULL);
4115 /* XXX Perhaps we should create a specialized
4116 PyFrame_New() that doesn't take locals, but does
4117 take builtins without sanity checking them.
4118 */
4119 assert(tstate != NULL);
4120 f = PyFrame_New(tstate, co, globals, NULL);
4121 if (f == NULL)
4122 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004123
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004124 fastlocals = f->f_localsplus;
4125 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004126
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004127 for (i = 0; i < n; i++) {
4128 Py_INCREF(*stack);
4129 fastlocals[i] = *stack++;
4130 }
4131 retval = PyEval_EvalFrameEx(f,0);
4132 ++tstate->recursion_depth;
4133 Py_DECREF(f);
4134 --tstate->recursion_depth;
4135 return retval;
4136 }
4137 if (argdefs != NULL) {
4138 d = &PyTuple_GET_ITEM(argdefs, 0);
4139 nd = Py_SIZE(argdefs);
4140 }
4141 return PyEval_EvalCodeEx(co, globals,
4142 (PyObject *)NULL, (*pp_stack)-n, na,
4143 (*pp_stack)-2*nk, nk, d, nd,
4144 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004145}
4146
Fredrik Lundh7a830892006-05-27 10:39:48 +00004147static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004148update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4149 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004150{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004151 PyObject *kwdict = NULL;
4152 if (orig_kwdict == NULL)
4153 kwdict = PyDict_New();
4154 else {
4155 kwdict = PyDict_Copy(orig_kwdict);
4156 Py_DECREF(orig_kwdict);
4157 }
4158 if (kwdict == NULL)
4159 return NULL;
4160 while (--nk >= 0) {
4161 int err;
4162 PyObject *value = EXT_POP(*pp_stack);
4163 PyObject *key = EXT_POP(*pp_stack);
4164 if (PyDict_GetItem(kwdict, key) != NULL) {
4165 PyErr_Format(PyExc_TypeError,
4166 "%.200s%s got multiple values "
4167 "for keyword argument '%.200s'",
4168 PyEval_GetFuncName(func),
4169 PyEval_GetFuncDesc(func),
4170 PyString_AsString(key));
4171 Py_DECREF(key);
4172 Py_DECREF(value);
4173 Py_DECREF(kwdict);
4174 return NULL;
4175 }
4176 err = PyDict_SetItem(kwdict, key, value);
4177 Py_DECREF(key);
4178 Py_DECREF(value);
4179 if (err) {
4180 Py_DECREF(kwdict);
4181 return NULL;
4182 }
4183 }
4184 return kwdict;
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 +00004188update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004189 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004190{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004191 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004192
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004193 callargs = PyTuple_New(nstack + nstar);
4194 if (callargs == NULL) {
4195 return NULL;
4196 }
4197 if (nstar) {
4198 int i;
4199 for (i = 0; i < nstar; i++) {
4200 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4201 Py_INCREF(a);
4202 PyTuple_SET_ITEM(callargs, nstack + i, a);
4203 }
4204 }
4205 while (--nstack >= 0) {
4206 w = EXT_POP(*pp_stack);
4207 PyTuple_SET_ITEM(callargs, nstack, w);
4208 }
4209 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004210}
4211
Fredrik Lundh7a830892006-05-27 10:39:48 +00004212static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004213load_args(PyObject ***pp_stack, int na)
4214{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004215 PyObject *args = PyTuple_New(na);
4216 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004217
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004218 if (args == NULL)
4219 return NULL;
4220 while (--na >= 0) {
4221 w = EXT_POP(*pp_stack);
4222 PyTuple_SET_ITEM(args, na, w);
4223 }
4224 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004225}
4226
Fredrik Lundh7a830892006-05-27 10:39:48 +00004227static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004228do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4229{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004230 PyObject *callargs = NULL;
4231 PyObject *kwdict = NULL;
4232 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004234 if (nk > 0) {
4235 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4236 if (kwdict == NULL)
4237 goto call_fail;
4238 }
4239 callargs = load_args(pp_stack, na);
4240 if (callargs == NULL)
4241 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004242#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004243 /* At this point, we have to look at the type of func to
4244 update the call stats properly. Do it here so as to avoid
4245 exposing the call stats machinery outside ceval.c
4246 */
4247 if (PyFunction_Check(func))
4248 PCALL(PCALL_FUNCTION);
4249 else if (PyMethod_Check(func))
4250 PCALL(PCALL_METHOD);
4251 else if (PyType_Check(func))
4252 PCALL(PCALL_TYPE);
4253 else if (PyCFunction_Check(func))
4254 PCALL(PCALL_CFUNCTION);
4255 else
4256 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004257#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004258 if (PyCFunction_Check(func)) {
4259 PyThreadState *tstate = PyThreadState_GET();
4260 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4261 }
4262 else
4263 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004264 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004265 Py_XDECREF(callargs);
4266 Py_XDECREF(kwdict);
4267 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004268}
4269
Fredrik Lundh7a830892006-05-27 10:39:48 +00004270static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004271ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4272{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004273 int nstar = 0;
4274 PyObject *callargs = NULL;
4275 PyObject *stararg = NULL;
4276 PyObject *kwdict = NULL;
4277 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004278
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004279 if (flags & CALL_FLAG_KW) {
4280 kwdict = EXT_POP(*pp_stack);
4281 if (!PyDict_Check(kwdict)) {
4282 PyObject *d;
4283 d = PyDict_New();
4284 if (d == NULL)
4285 goto ext_call_fail;
4286 if (PyDict_Update(d, kwdict) != 0) {
4287 Py_DECREF(d);
4288 /* PyDict_Update raises attribute
4289 * error (percolated from an attempt
4290 * to get 'keys' attribute) instead of
4291 * a type error if its second argument
4292 * is not a mapping.
4293 */
4294 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4295 PyErr_Format(PyExc_TypeError,
4296 "%.200s%.200s argument after ** "
4297 "must be a mapping, not %.200s",
4298 PyEval_GetFuncName(func),
4299 PyEval_GetFuncDesc(func),
4300 kwdict->ob_type->tp_name);
4301 }
4302 goto ext_call_fail;
4303 }
4304 Py_DECREF(kwdict);
4305 kwdict = d;
4306 }
4307 }
4308 if (flags & CALL_FLAG_VAR) {
4309 stararg = EXT_POP(*pp_stack);
4310 if (!PyTuple_Check(stararg)) {
4311 PyObject *t = NULL;
4312 t = PySequence_Tuple(stararg);
4313 if (t == NULL) {
4314 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4315 PyErr_Format(PyExc_TypeError,
4316 "%.200s%.200s argument after * "
4317 "must be a sequence, not %200s",
4318 PyEval_GetFuncName(func),
4319 PyEval_GetFuncDesc(func),
4320 stararg->ob_type->tp_name);
4321 }
4322 goto ext_call_fail;
4323 }
4324 Py_DECREF(stararg);
4325 stararg = t;
4326 }
4327 nstar = PyTuple_GET_SIZE(stararg);
4328 }
4329 if (nk > 0) {
4330 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4331 if (kwdict == NULL)
4332 goto ext_call_fail;
4333 }
4334 callargs = update_star_args(na, nstar, stararg, pp_stack);
4335 if (callargs == NULL)
4336 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004337#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004338 /* At this point, we have to look at the type of func to
4339 update the call stats properly. Do it here so as to avoid
4340 exposing the call stats machinery outside ceval.c
4341 */
4342 if (PyFunction_Check(func))
4343 PCALL(PCALL_FUNCTION);
4344 else if (PyMethod_Check(func))
4345 PCALL(PCALL_METHOD);
4346 else if (PyType_Check(func))
4347 PCALL(PCALL_TYPE);
4348 else if (PyCFunction_Check(func))
4349 PCALL(PCALL_CFUNCTION);
4350 else
4351 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004352#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004353 if (PyCFunction_Check(func)) {
4354 PyThreadState *tstate = PyThreadState_GET();
4355 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4356 }
4357 else
4358 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004359ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004360 Py_XDECREF(callargs);
4361 Py_XDECREF(kwdict);
4362 Py_XDECREF(stararg);
4363 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004364}
4365
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004366/* Extract a slice index from a PyInt or PyLong or an object with the
4367 nb_index slot defined, and store in *pi.
4368 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4369 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 +00004370 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004371*/
Tim Petersb5196382001-12-16 19:44:20 +00004372/* Note: If v is NULL, return success without storing into *pi. This
4373 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4374 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004375*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004376int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004377_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004378{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004379 if (v != NULL) {
4380 Py_ssize_t x;
4381 if (PyInt_Check(v)) {
4382 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4383 however, it looks like it should be AsSsize_t.
4384 There should be a comment here explaining why.
4385 */
4386 x = PyInt_AS_LONG(v);
4387 }
4388 else if (PyIndex_Check(v)) {
4389 x = PyNumber_AsSsize_t(v, NULL);
4390 if (x == -1 && PyErr_Occurred())
4391 return 0;
4392 }
4393 else {
4394 PyErr_SetString(PyExc_TypeError,
4395 "slice indices must be integers or "
4396 "None or have an __index__ method");
4397 return 0;
4398 }
4399 *pi = x;
4400 }
4401 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004402}
4403
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004404#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004405#define ISINDEX(x) ((x) == NULL || \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004406 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004407
Fredrik Lundh7a830892006-05-27 10:39:48 +00004408static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004409apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004410{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004411 PyTypeObject *tp = u->ob_type;
4412 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004413
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004414 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4415 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4416 if (!_PyEval_SliceIndex(v, &ilow))
4417 return NULL;
4418 if (!_PyEval_SliceIndex(w, &ihigh))
4419 return NULL;
4420 return PySequence_GetSlice(u, ilow, ihigh);
4421 }
4422 else {
4423 PyObject *slice = PySlice_New(v, w, NULL);
4424 if (slice != NULL) {
4425 PyObject *res = PyObject_GetItem(u, slice);
4426 Py_DECREF(slice);
4427 return res;
4428 }
4429 else
4430 return NULL;
4431 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004432}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004433
Fredrik Lundh7a830892006-05-27 10:39:48 +00004434static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004435assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004436 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004437{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004438 PyTypeObject *tp = u->ob_type;
4439 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004440
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004441 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4442 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4443 if (!_PyEval_SliceIndex(v, &ilow))
4444 return -1;
4445 if (!_PyEval_SliceIndex(w, &ihigh))
4446 return -1;
4447 if (x == NULL)
4448 return PySequence_DelSlice(u, ilow, ihigh);
4449 else
4450 return PySequence_SetSlice(u, ilow, ihigh, x);
4451 }
4452 else {
4453 PyObject *slice = PySlice_New(v, w, NULL);
4454 if (slice != NULL) {
4455 int res;
4456 if (x != NULL)
4457 res = PyObject_SetItem(u, slice, x);
4458 else
4459 res = PyObject_DelItem(u, slice);
4460 Py_DECREF(slice);
4461 return res;
4462 }
4463 else
4464 return -1;
4465 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004466}
4467
Guido van Rossum04edb522008-03-18 02:49:46 +00004468#define Py3kExceptionClass_Check(x) \
4469 (PyType_Check((x)) && \
4470 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4471
4472#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004473 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004474
Fredrik Lundh7a830892006-05-27 10:39:48 +00004475static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004476cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004477{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004478 int res = 0;
4479 switch (op) {
4480 case PyCmp_IS:
4481 res = (v == w);
4482 break;
4483 case PyCmp_IS_NOT:
4484 res = (v != w);
4485 break;
4486 case PyCmp_IN:
4487 res = PySequence_Contains(w, v);
4488 if (res < 0)
4489 return NULL;
4490 break;
4491 case PyCmp_NOT_IN:
4492 res = PySequence_Contains(w, v);
4493 if (res < 0)
4494 return NULL;
4495 res = !res;
4496 break;
4497 case PyCmp_EXC_MATCH:
4498 if (PyTuple_Check(w)) {
4499 Py_ssize_t i, length;
4500 length = PyTuple_Size(w);
4501 for (i = 0; i < length; i += 1) {
4502 PyObject *exc = PyTuple_GET_ITEM(w, i);
4503 if (PyString_Check(exc)) {
4504 int ret_val;
4505 ret_val = PyErr_WarnEx(
4506 PyExc_DeprecationWarning,
4507 "catching of string "
4508 "exceptions is deprecated", 1);
4509 if (ret_val < 0)
4510 return NULL;
4511 }
4512 else if (Py_Py3kWarningFlag &&
4513 !PyTuple_Check(exc) &&
4514 !Py3kExceptionClass_Check(exc))
4515 {
4516 int ret_val;
4517 ret_val = PyErr_WarnEx(
4518 PyExc_DeprecationWarning,
4519 CANNOT_CATCH_MSG, 1);
4520 if (ret_val < 0)
4521 return NULL;
4522 }
4523 }
4524 }
4525 else {
4526 if (PyString_Check(w)) {
4527 int ret_val;
4528 ret_val = PyErr_WarnEx(
4529 PyExc_DeprecationWarning,
4530 "catching of string "
4531 "exceptions is deprecated", 1);
4532 if (ret_val < 0)
4533 return NULL;
4534 }
4535 else if (Py_Py3kWarningFlag &&
4536 !PyTuple_Check(w) &&
4537 !Py3kExceptionClass_Check(w))
4538 {
4539 int ret_val;
4540 ret_val = PyErr_WarnEx(
4541 PyExc_DeprecationWarning,
4542 CANNOT_CATCH_MSG, 1);
4543 if (ret_val < 0)
4544 return NULL;
4545 }
4546 }
4547 res = PyErr_GivenExceptionMatches(v, w);
4548 break;
4549 default:
4550 return PyObject_RichCompare(v, w, op);
4551 }
4552 v = res ? Py_True : Py_False;
4553 Py_INCREF(v);
4554 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004555}
4556
Fredrik Lundh7a830892006-05-27 10:39:48 +00004557static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004558import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004559{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004560 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004561
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004562 x = PyObject_GetAttr(v, name);
4563 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4564 PyErr_Format(PyExc_ImportError,
4565 "cannot import name %.230s",
4566 PyString_AsString(name));
4567 }
4568 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004569}
Guido van Rossumac7be682001-01-17 15:42:30 +00004570
Fredrik Lundh7a830892006-05-27 10:39:48 +00004571static int
Thomas Wouters52152252000-08-17 22:55:00 +00004572import_all_from(PyObject *locals, PyObject *v)
4573{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004574 PyObject *all = PyObject_GetAttrString(v, "__all__");
4575 PyObject *dict, *name, *value;
4576 int skip_leading_underscores = 0;
4577 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004578
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004579 if (all == NULL) {
4580 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4581 return -1; /* Unexpected error */
4582 PyErr_Clear();
4583 dict = PyObject_GetAttrString(v, "__dict__");
4584 if (dict == NULL) {
4585 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4586 return -1;
4587 PyErr_SetString(PyExc_ImportError,
4588 "from-import-* object has no __dict__ and no __all__");
4589 return -1;
4590 }
4591 all = PyMapping_Keys(dict);
4592 Py_DECREF(dict);
4593 if (all == NULL)
4594 return -1;
4595 skip_leading_underscores = 1;
4596 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004597
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004598 for (pos = 0, err = 0; ; pos++) {
4599 name = PySequence_GetItem(all, pos);
4600 if (name == NULL) {
4601 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4602 err = -1;
4603 else
4604 PyErr_Clear();
4605 break;
4606 }
4607 if (skip_leading_underscores &&
4608 PyString_Check(name) &&
4609 PyString_AS_STRING(name)[0] == '_')
4610 {
4611 Py_DECREF(name);
4612 continue;
4613 }
4614 value = PyObject_GetAttr(v, name);
4615 if (value == NULL)
4616 err = -1;
4617 else if (PyDict_CheckExact(locals))
4618 err = PyDict_SetItem(locals, name, value);
4619 else
4620 err = PyObject_SetItem(locals, name, value);
4621 Py_DECREF(name);
4622 Py_XDECREF(value);
4623 if (err != 0)
4624 break;
4625 }
4626 Py_DECREF(all);
4627 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004628}
4629
Fredrik Lundh7a830892006-05-27 10:39:48 +00004630static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004631build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004632{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004633 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004634
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004635 if (PyDict_Check(methods))
4636 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4637 if (metaclass != NULL)
4638 Py_INCREF(metaclass);
4639 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4640 base = PyTuple_GET_ITEM(bases, 0);
4641 metaclass = PyObject_GetAttrString(base, "__class__");
4642 if (metaclass == NULL) {
4643 PyErr_Clear();
4644 metaclass = (PyObject *)base->ob_type;
4645 Py_INCREF(metaclass);
4646 }
4647 }
4648 else {
4649 PyObject *g = PyEval_GetGlobals();
4650 if (g != NULL && PyDict_Check(g))
4651 metaclass = PyDict_GetItemString(g, "__metaclass__");
4652 if (metaclass == NULL)
4653 metaclass = (PyObject *) &PyClass_Type;
4654 Py_INCREF(metaclass);
4655 }
4656 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4657 NULL);
4658 Py_DECREF(metaclass);
4659 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4660 /* A type error here likely means that the user passed
4661 in a base that was not a class (such the random module
4662 instead of the random.random type). Help them out with
4663 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00004664
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004665 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00004666
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004667 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4668 if (PyString_Check(pvalue)) {
4669 PyObject *newmsg;
4670 newmsg = PyString_FromFormat(
4671 "Error when calling the metaclass bases\n"
4672 " %s",
4673 PyString_AS_STRING(pvalue));
4674 if (newmsg != NULL) {
4675 Py_DECREF(pvalue);
4676 pvalue = newmsg;
4677 }
4678 }
4679 PyErr_Restore(ptype, pvalue, ptraceback);
4680 }
4681 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004682}
4683
Fredrik Lundh7a830892006-05-27 10:39:48 +00004684static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004685exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004686 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004687{
Benjamin Petersond2903bd2014-08-09 19:39:36 -07004688 int n;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004689 PyObject *v;
4690 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004691
Benjamin Petersond2903bd2014-08-09 19:39:36 -07004692 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4693 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4694 /* Backward compatibility hack */
4695 globals = PyTuple_GetItem(prog, 1);
4696 if (n == 3)
4697 locals = PyTuple_GetItem(prog, 2);
4698 prog = PyTuple_GetItem(prog, 0);
4699 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004700 if (globals == Py_None) {
4701 globals = PyEval_GetGlobals();
4702 if (locals == Py_None) {
4703 locals = PyEval_GetLocals();
4704 plain = 1;
4705 }
4706 if (!globals || !locals) {
4707 PyErr_SetString(PyExc_SystemError,
4708 "globals and locals cannot be NULL");
4709 return -1;
4710 }
4711 }
4712 else if (locals == Py_None)
4713 locals = globals;
4714 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004715#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004716 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00004717#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004718 !PyCode_Check(prog) &&
4719 !PyFile_Check(prog)) {
4720 PyErr_SetString(PyExc_TypeError,
4721 "exec: arg 1 must be a string, file, or code object");
4722 return -1;
4723 }
4724 if (!PyDict_Check(globals)) {
4725 PyErr_SetString(PyExc_TypeError,
4726 "exec: arg 2 must be a dictionary or None");
4727 return -1;
4728 }
4729 if (!PyMapping_Check(locals)) {
4730 PyErr_SetString(PyExc_TypeError,
4731 "exec: arg 3 must be a mapping or None");
4732 return -1;
4733 }
4734 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
4735 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
4736 if (PyCode_Check(prog)) {
4737 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
4738 PyErr_SetString(PyExc_TypeError,
4739 "code object passed to exec may not contain free variables");
4740 return -1;
4741 }
4742 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
4743 }
4744 else if (PyFile_Check(prog)) {
4745 FILE *fp = PyFile_AsFile(prog);
4746 char *name = PyString_AsString(PyFile_Name(prog));
4747 PyCompilerFlags cf;
4748 if (name == NULL)
4749 return -1;
4750 cf.cf_flags = 0;
4751 if (PyEval_MergeCompilerFlags(&cf))
4752 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
4753 locals, &cf);
4754 else
4755 v = PyRun_File(fp, name, Py_file_input, globals,
4756 locals);
4757 }
4758 else {
4759 PyObject *tmp = NULL;
4760 char *str;
4761 PyCompilerFlags cf;
4762 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004763#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004764 if (PyUnicode_Check(prog)) {
4765 tmp = PyUnicode_AsUTF8String(prog);
4766 if (tmp == NULL)
4767 return -1;
4768 prog = tmp;
4769 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
4770 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00004771#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004772 if (PyString_AsStringAndSize(prog, &str, NULL))
4773 return -1;
4774 if (PyEval_MergeCompilerFlags(&cf))
4775 v = PyRun_StringFlags(str, Py_file_input, globals,
4776 locals, &cf);
4777 else
4778 v = PyRun_String(str, Py_file_input, globals, locals);
4779 Py_XDECREF(tmp);
4780 }
4781 if (plain)
4782 PyFrame_LocalsToFast(f, 0);
4783 if (v == NULL)
4784 return -1;
4785 Py_DECREF(v);
4786 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004787}
Guido van Rossum24c13741995-02-14 09:42:43 +00004788
Fredrik Lundh7a830892006-05-27 10:39:48 +00004789static void
Paul Prescode68140d2000-08-30 20:25:01 +00004790format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
4791{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004792 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00004793
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004794 if (!obj)
4795 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004796
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004797 obj_str = PyString_AsString(obj);
4798 if (!obj_str)
4799 return;
Paul Prescode68140d2000-08-30 20:25:01 +00004800
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004801 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00004802}
Guido van Rossum950361c1997-01-24 13:49:28 +00004803
Fredrik Lundh7a830892006-05-27 10:39:48 +00004804static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004805string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004806 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004807{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004808 /* This function implements 'variable += expr' when both arguments
4809 are strings. */
4810 Py_ssize_t v_len = PyString_GET_SIZE(v);
4811 Py_ssize_t w_len = PyString_GET_SIZE(w);
4812 Py_ssize_t new_len = v_len + w_len;
4813 if (new_len < 0) {
4814 PyErr_SetString(PyExc_OverflowError,
4815 "strings are too large to concat");
4816 return NULL;
4817 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00004818
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004819 if (v->ob_refcnt == 2) {
4820 /* In the common case, there are 2 references to the value
4821 * stored in 'variable' when the += is performed: one on the
4822 * value stack (in 'v') and one still stored in the
4823 * 'variable'. We try to delete the variable now to reduce
4824 * the refcnt to 1.
4825 */
4826 switch (*next_instr) {
4827 case STORE_FAST:
4828 {
4829 int oparg = PEEKARG();
4830 PyObject **fastlocals = f->f_localsplus;
4831 if (GETLOCAL(oparg) == v)
4832 SETLOCAL(oparg, NULL);
4833 break;
4834 }
4835 case STORE_DEREF:
4836 {
4837 PyObject **freevars = (f->f_localsplus +
4838 f->f_code->co_nlocals);
4839 PyObject *c = freevars[PEEKARG()];
4840 if (PyCell_GET(c) == v)
4841 PyCell_Set(c, NULL);
4842 break;
4843 }
4844 case STORE_NAME:
4845 {
4846 PyObject *names = f->f_code->co_names;
4847 PyObject *name = GETITEM(names, PEEKARG());
4848 PyObject *locals = f->f_locals;
4849 if (PyDict_CheckExact(locals) &&
4850 PyDict_GetItem(locals, name) == v) {
4851 if (PyDict_DelItem(locals, name) != 0) {
4852 PyErr_Clear();
4853 }
4854 }
4855 break;
4856 }
4857 }
4858 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004859
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004860 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
4861 /* Now we own the last reference to 'v', so we can resize it
4862 * in-place.
4863 */
4864 if (_PyString_Resize(&v, new_len) != 0) {
4865 /* XXX if _PyString_Resize() fails, 'v' has been
4866 * deallocated so it cannot be put back into
4867 * 'variable'. The MemoryError is raised when there
4868 * is no value in 'variable', which might (very
4869 * remotely) be a cause of incompatibilities.
4870 */
4871 return NULL;
4872 }
4873 /* copy 'w' into the newly allocated area of 'v' */
4874 memcpy(PyString_AS_STRING(v) + v_len,
4875 PyString_AS_STRING(w), w_len);
4876 return v;
4877 }
4878 else {
4879 /* When in-place resizing is not an option. */
4880 PyString_Concat(&v, w);
4881 return v;
4882 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00004883}
4884
Guido van Rossum950361c1997-01-24 13:49:28 +00004885#ifdef DYNAMIC_EXECUTION_PROFILE
4886
Fredrik Lundh7a830892006-05-27 10:39:48 +00004887static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004888getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00004889{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004890 int i;
4891 PyObject *l = PyList_New(256);
4892 if (l == NULL) return NULL;
4893 for (i = 0; i < 256; i++) {
4894 PyObject *x = PyInt_FromLong(a[i]);
4895 if (x == NULL) {
4896 Py_DECREF(l);
4897 return NULL;
4898 }
4899 PyList_SetItem(l, i, x);
4900 }
4901 for (i = 0; i < 256; i++)
4902 a[i] = 0;
4903 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004904}
4905
4906PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004907_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00004908{
4909#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004910 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00004911#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004912 int i;
4913 PyObject *l = PyList_New(257);
4914 if (l == NULL) return NULL;
4915 for (i = 0; i < 257; i++) {
4916 PyObject *x = getarray(dxpairs[i]);
4917 if (x == NULL) {
4918 Py_DECREF(l);
4919 return NULL;
4920 }
4921 PyList_SetItem(l, i, x);
4922 }
4923 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00004924#endif
4925}
4926
4927#endif