blob: c41cbb4bc320b15c35be964b29eb6661c450ef8a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum3f5da241990-12-20 15:06:42 +00002/* Execute compiled code */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003
Guido van Rossum681d79a1995-07-18 14:51:37 +00004/* XXX TO DO:
Guido van Rossum681d79a1995-07-18 14:51:37 +00005 XXX speed up searching for keywords by using a dictionary
Guido van Rossum681d79a1995-07-18 14:51:37 +00006 XXX document it!
7 */
8
Fredrik Lundh7a830892006-05-27 10:39:48 +00009/* enable more aggressive intra-module optimizations, where available */
Fredrik Lundh57640f52006-05-26 11:54:04 +000010#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000013
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#include "code.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000015#include "frameobject.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000016#include "eval.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000017#include "opcode.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000018#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000019
Guido van Rossumc6004111993-11-05 10:22:19 +000020#include <ctype.h>
21
Tim Peters7df5e7f2006-05-26 23:14:37 +000022#ifndef WITH_TSC
Michael W. Hudson75eabd22005-01-18 15:56:11 +000023
24#define READ_TIMESTAMP(var)
25
26#else
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000027
28typedef unsigned long long uint64;
29
Ezio Melottic2077b02011-03-16 12:34:31 +020030/* PowerPC support.
David Malcolm4c29e1c2011-01-06 17:39:24 +000031 "__ppc__" appears to be the preprocessor definition to detect on OS X, whereas
32 "__powerpc__" appears to be the correct one for Linux with GCC
33*/
34#if defined(__ppc__) || defined (__powerpc__)
Michael W. Hudson800ba232004-08-12 18:19:17 +000035
Michael W. Hudson75eabd22005-01-18 15:56:11 +000036#define READ_TIMESTAMP(var) ppc_getcounter(&var)
Michael W. Hudson800ba232004-08-12 18:19:17 +000037
Fredrik Lundh7a830892006-05-27 10:39:48 +000038static void
Michael W. Hudson800ba232004-08-12 18:19:17 +000039ppc_getcounter(uint64 *v)
40{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000041 register unsigned long tbu, tb, tbu2;
Michael W. Hudson800ba232004-08-12 18:19:17 +000042
43 loop:
Antoine Pitrouc83ea132010-05-09 14:46:46 +000044 asm volatile ("mftbu %0" : "=r" (tbu) );
45 asm volatile ("mftb %0" : "=r" (tb) );
46 asm volatile ("mftbu %0" : "=r" (tbu2));
47 if (__builtin_expect(tbu != tbu2, 0)) goto loop;
Michael W. Hudson800ba232004-08-12 18:19:17 +000048
Antoine Pitrouc83ea132010-05-09 14:46:46 +000049 /* The slightly peculiar way of writing the next lines is
50 compiled better by GCC than any other way I tried. */
51 ((long*)(v))[0] = tbu;
52 ((long*)(v))[1] = tb;
Michael W. Hudson800ba232004-08-12 18:19:17 +000053}
54
Mark Dickinson504a1512009-10-31 10:11:28 +000055#elif defined(__i386__)
56
57/* this is for linux/x86 (and probably any other GCC/x86 combo) */
Michael W. Hudson800ba232004-08-12 18:19:17 +000058
Michael W. Hudson75eabd22005-01-18 15:56:11 +000059#define READ_TIMESTAMP(val) \
60 __asm__ __volatile__("rdtsc" : "=A" (val))
Michael W. Hudson800ba232004-08-12 18:19:17 +000061
Mark Dickinson504a1512009-10-31 10:11:28 +000062#elif defined(__x86_64__)
63
64/* for gcc/x86_64, the "A" constraint in DI mode means *either* rax *or* rdx;
65 not edx:eax as it does for i386. Since rdtsc puts its result in edx:eax
66 even in 64-bit mode, we need to use "a" and "d" for the lower and upper
67 32-bit pieces of the result. */
68
Victor Stinner2b565bb2014-12-12 13:19:00 +010069#define READ_TIMESTAMP(val) do { \
70 unsigned int h, l; \
71 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h)); \
72 (val) = ((uint64)l) | (((uint64)h) << 32); \
73 } while(0)
Mark Dickinson504a1512009-10-31 10:11:28 +000074
75
76#else
77
78#error "Don't know how to implement timestamp counter for this architecture"
79
Michael W. Hudson800ba232004-08-12 18:19:17 +000080#endif
81
Tim Peters7df5e7f2006-05-26 23:14:37 +000082void dump_tsc(int opcode, int ticked, uint64 inst0, uint64 inst1,
Antoine Pitrouc83ea132010-05-09 14:46:46 +000083 uint64 loop0, uint64 loop1, uint64 intr0, uint64 intr1)
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000084{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000085 uint64 intr, inst, loop;
86 PyThreadState *tstate = PyThreadState_Get();
87 if (!tstate->interp->tscdump)
88 return;
89 intr = intr1 - intr0;
90 inst = inst1 - inst0 - intr;
91 loop = loop1 - loop0 - intr;
92 fprintf(stderr, "opcode=%03d t=%d inst=%06lld loop=%06lld\n",
Stefan Krah7ff78252010-06-23 18:12:09 +000093 opcode, ticked, inst, loop);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000094}
Michael W. Hudson800ba232004-08-12 18:19:17 +000095
Martin v. Löwisf30d60e2004-06-08 08:17:44 +000096#endif
97
Guido van Rossum04691fc1992-08-12 15:35:34 +000098/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000099/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +0000100
Guido van Rossum408027e1996-12-30 16:17:54 +0000101#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +0000102/* For debugging the interpreter: */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000103#define LLTRACE 1 /* Low-level trace feature */
104#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000105#endif
106
Jeremy Hylton52820442001-01-03 23:52:36 +0000107typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +0000108
Guido van Rossum374a9221991-04-04 10:40:29 +0000109/* Forward declarations */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000110#ifdef WITH_TSC
Fredrik Lundh7a830892006-05-27 10:39:48 +0000111static PyObject * call_function(PyObject ***, int, uint64*, uint64*);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000112#else
Fredrik Lundh7a830892006-05-27 10:39:48 +0000113static PyObject * call_function(PyObject ***, int);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000114#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000115static PyObject * fast_function(PyObject *, PyObject ***, int, int, int);
116static PyObject * do_call(PyObject *, PyObject ***, int, int);
117static PyObject * ext_do_call(PyObject *, PyObject ***, int, int, int);
Thomas Wouterse2176022007-09-20 17:35:10 +0000118static PyObject * update_keyword_args(PyObject *, int, PyObject ***,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000119 PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000120static PyObject * update_star_args(int, int, PyObject *, PyObject ***);
121static PyObject * load_args(PyObject ***, int);
Jeremy Hylton52820442001-01-03 23:52:36 +0000122#define CALL_FLAG_VAR 1
123#define CALL_FLAG_KW 2
124
Guido van Rossum0a066c01992-03-27 17:29:15 +0000125#ifdef LLTRACE
Fredrik Lundh1b949402006-05-26 12:01:49 +0000126static int lltrace;
Fredrik Lundh7a830892006-05-27 10:39:48 +0000127static int prtrace(PyObject *, char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +0000128#endif
Fredrik Lundh7a830892006-05-27 10:39:48 +0000129static int call_trace(Py_tracefunc, PyObject *, PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000130 int, PyObject *);
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +0000131static int call_trace_protected(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000132 PyFrameObject *, int, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000133static void call_exc_trace(Py_tracefunc, PyObject *, PyFrameObject *);
134static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000135 PyFrameObject *, int *, int *, int *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000136
Fredrik Lundh7a830892006-05-27 10:39:48 +0000137static PyObject * apply_slice(PyObject *, PyObject *, PyObject *);
138static int assign_slice(PyObject *, PyObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000139 PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000140static PyObject * cmp_outcome(int, PyObject *, PyObject *);
141static PyObject * import_from(PyObject *, PyObject *);
142static int import_all_from(PyObject *, PyObject *);
143static PyObject * build_class(PyObject *, PyObject *, PyObject *);
144static int exec_statement(PyFrameObject *,
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000145 PyObject *, PyObject *, PyObject *);
Fredrik Lundh7a830892006-05-27 10:39:48 +0000146static void set_exc_info(PyThreadState *, PyObject *, PyObject *, PyObject *);
147static void reset_exc_info(PyThreadState *);
148static void format_exc_check_arg(PyObject *, char *, PyObject *);
149static PyObject * string_concatenate(PyObject *, PyObject *,
Stefan Krah7ff78252010-06-23 18:12:09 +0000150 PyFrameObject *, unsigned char *);
Benjamin Petersone18ef192009-01-20 14:21:16 +0000151static PyObject * kwd_as_string(PyObject *);
Benjamin Peterson1880d8b2009-05-25 13:13:44 +0000152static PyObject * special_lookup(PyObject *, char *, PyObject **);
Guido van Rossum374a9221991-04-04 10:40:29 +0000153
Paul Prescode68140d2000-08-30 20:25:01 +0000154#define NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000155 "name '%.200s' is not defined"
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000156#define GLOBAL_NAME_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000157 "global name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000158#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000159 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000160#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000161 "free variable '%.200s' referenced before assignment" \
162 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000163
Guido van Rossum950361c1997-01-24 13:49:28 +0000164/* Dynamic execution profile */
165#ifdef DYNAMIC_EXECUTION_PROFILE
166#ifdef DXPAIRS
167static long dxpairs[257][256];
168#define dxp dxpairs[256]
169#else
170static long dxp[256];
171#endif
172#endif
173
Jeremy Hylton985eba52003-02-05 23:13:00 +0000174/* Function call profile */
175#ifdef CALL_PROFILE
176#define PCALL_NUM 11
177static int pcall[PCALL_NUM];
178
179#define PCALL_ALL 0
180#define PCALL_FUNCTION 1
181#define PCALL_FAST_FUNCTION 2
182#define PCALL_FASTER_FUNCTION 3
183#define PCALL_METHOD 4
184#define PCALL_BOUND_METHOD 5
185#define PCALL_CFUNCTION 6
186#define PCALL_TYPE 7
187#define PCALL_GENERATOR 8
188#define PCALL_OTHER 9
189#define PCALL_POP 10
190
191/* Notes about the statistics
192
193 PCALL_FAST stats
194
195 FAST_FUNCTION means no argument tuple needs to be created.
196 FASTER_FUNCTION means that the fast-path frame setup code is used.
197
198 If there is a method call where the call can be optimized by changing
199 the argument tuple and calling the function directly, it gets recorded
200 twice.
201
202 As a result, the relationship among the statistics appears to be
203 PCALL_ALL == PCALL_FUNCTION + PCALL_METHOD - PCALL_BOUND_METHOD +
204 PCALL_CFUNCTION + PCALL_TYPE + PCALL_GENERATOR + PCALL_OTHER
205 PCALL_FUNCTION > PCALL_FAST_FUNCTION > PCALL_FASTER_FUNCTION
206 PCALL_METHOD > PCALL_BOUND_METHOD
207*/
208
209#define PCALL(POS) pcall[POS]++
210
211PyObject *
212PyEval_GetCallStats(PyObject *self)
213{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000214 return Py_BuildValue("iiiiiiiiiii",
215 pcall[0], pcall[1], pcall[2], pcall[3],
216 pcall[4], pcall[5], pcall[6], pcall[7],
217 pcall[8], pcall[9], pcall[10]);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000218}
219#else
220#define PCALL(O)
221
222PyObject *
223PyEval_GetCallStats(PyObject *self)
224{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000225 Py_INCREF(Py_None);
226 return Py_None;
Jeremy Hylton985eba52003-02-05 23:13:00 +0000227}
228#endif
229
Tim Peters5ca576e2001-06-18 22:08:13 +0000230
Guido van Rossume59214e1994-08-30 08:01:59 +0000231#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000232
Martin v. Löwis0e8bd7e2006-06-10 12:23:46 +0000233#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000234#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000235#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000236#include "pythread.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +0000237
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +0000238static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000239static PyThread_type_lock pending_lock = 0; /* for pending calls */
Guido van Rossuma9672091994-09-14 13:31:22 +0000240static long main_thread = 0;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000241
Tim Peters7f468f22004-10-11 02:40:51 +0000242int
243PyEval_ThreadsInitialized(void)
244{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000245 return interpreter_lock != 0;
Tim Peters7f468f22004-10-11 02:40:51 +0000246}
247
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000249PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000250{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000251 if (interpreter_lock)
252 return;
253 interpreter_lock = PyThread_allocate_lock();
254 PyThread_acquire_lock(interpreter_lock, 1);
255 main_thread = PyThread_get_thread_ident();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000256}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000257
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000258void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000259PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000260{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000261 PyThread_acquire_lock(interpreter_lock, 1);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000262}
263
264void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000265PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000266{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000267 PyThread_release_lock(interpreter_lock);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000268}
269
270void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000271PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000272{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000273 if (tstate == NULL)
274 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
275 /* Check someone has called PyEval_InitThreads() to create the lock */
276 assert(interpreter_lock);
277 PyThread_acquire_lock(interpreter_lock, 1);
278 if (PyThreadState_Swap(tstate) != NULL)
279 Py_FatalError(
280 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000281}
282
283void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000284PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000285{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000286 if (tstate == NULL)
287 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
288 if (PyThreadState_Swap(NULL) != tstate)
289 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
290 PyThread_release_lock(interpreter_lock);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000291}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000292
293/* This function is called from PyOS_AfterFork to ensure that newly
294 created child processes don't hold locks referring to threads which
295 are not running in the child process. (This could also be done using
296 pthread_atfork mechanism, at least for the pthreads implementation.) */
297
298void
299PyEval_ReInitThreads(void)
300{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000301 PyObject *threading, *result;
302 PyThreadState *tstate;
Jesse Noller5e62ca42008-07-16 20:03:47 +0000303
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000304 if (!interpreter_lock)
305 return;
306 /*XXX Can't use PyThread_free_lock here because it does too
307 much error-checking. Doing this cleanly would require
308 adding a new function to each thread_*.h. Instead, just
309 create a new lock and waste a little bit of memory */
310 interpreter_lock = PyThread_allocate_lock();
311 pending_lock = PyThread_allocate_lock();
312 PyThread_acquire_lock(interpreter_lock, 1);
313 main_thread = PyThread_get_thread_ident();
Jesse Noller5e62ca42008-07-16 20:03:47 +0000314
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000315 /* Update the threading module with the new state.
316 */
317 tstate = PyThreadState_GET();
318 threading = PyMapping_GetItemString(tstate->interp->modules,
319 "threading");
320 if (threading == NULL) {
321 /* threading not imported */
322 PyErr_Clear();
323 return;
324 }
325 result = PyObject_CallMethod(threading, "_after_fork", NULL);
326 if (result == NULL)
327 PyErr_WriteUnraisable(threading);
328 else
329 Py_DECREF(result);
330 Py_DECREF(threading);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000331}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000332#endif
333
Guido van Rossumff4949e1992-08-05 19:58:53 +0000334/* Functions save_thread and restore_thread are always defined so
335 dynamically loaded modules needn't be compiled separately for use
336 with and without threads: */
337
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000338PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000339PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000340{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000341 PyThreadState *tstate = PyThreadState_Swap(NULL);
342 if (tstate == NULL)
343 Py_FatalError("PyEval_SaveThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000344#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000345 if (interpreter_lock)
346 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000347#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000348 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000349}
350
351void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000352PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000353{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000354 if (tstate == NULL)
355 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Guido van Rossume59214e1994-08-30 08:01:59 +0000356#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000357 if (interpreter_lock) {
358 int err = errno;
359 PyThread_acquire_lock(interpreter_lock, 1);
360 errno = err;
361 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000362#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000363 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000364}
365
366
Guido van Rossuma9672091994-09-14 13:31:22 +0000367/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
368 signal handlers or Mac I/O completion routines) can schedule calls
369 to a function to be called synchronously.
370 The synchronous function is called with one void* argument.
371 It should return 0 for success or -1 for failure -- failure should
372 be accompanied by an exception.
373
374 If registry succeeds, the registry function returns 0; if it fails
375 (e.g. due to too many pending calls) it returns -1 (without setting
376 an exception condition).
377
378 Note that because registry may occur from within signal handlers,
379 or other asynchronous events, calling malloc() is unsafe!
380
381#ifdef WITH_THREAD
382 Any thread can schedule pending calls, but only the main thread
383 will execute them.
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000384 There is no facility to schedule calls to a particular thread, but
385 that should be easy to change, should that ever be required. In
386 that case, the static variables here should go into the python
387 threadstate.
Guido van Rossuma9672091994-09-14 13:31:22 +0000388#endif
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000389*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000390
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000391#ifdef WITH_THREAD
392
393/* The WITH_THREAD implementation is thread-safe. It allows
394 scheduling to be made from any thread, and even from an executing
395 callback.
396 */
397
398#define NPENDINGCALLS 32
399static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000400 int (*func)(void *);
401 void *arg;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000402} pendingcalls[NPENDINGCALLS];
403static int pendingfirst = 0;
404static int pendinglast = 0;
405static volatile int pendingcalls_to_do = 1; /* trigger initialization of lock */
406static char pendingbusy = 0;
407
408int
409Py_AddPendingCall(int (*func)(void *), void *arg)
410{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000411 int i, j, result=0;
412 PyThread_type_lock lock = pending_lock;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000413
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000414 /* try a few times for the lock. Since this mechanism is used
415 * for signal handling (on the main thread), there is a (slim)
416 * chance that a signal is delivered on the same thread while we
417 * hold the lock during the Py_MakePendingCalls() function.
418 * This avoids a deadlock in that case.
419 * Note that signals can be delivered on any thread. In particular,
420 * on Windows, a SIGINT is delivered on a system-created worker
421 * thread.
422 * We also check for lock being NULL, in the unlikely case that
423 * this function is called before any bytecode evaluation takes place.
424 */
425 if (lock != NULL) {
426 for (i = 0; i<100; i++) {
427 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
428 break;
429 }
430 if (i == 100)
431 return -1;
432 }
433
434 i = pendinglast;
435 j = (i + 1) % NPENDINGCALLS;
436 if (j == pendingfirst) {
437 result = -1; /* Queue full */
438 } else {
439 pendingcalls[i].func = func;
440 pendingcalls[i].arg = arg;
441 pendinglast = j;
442 }
443 /* signal main loop */
444 _Py_Ticker = 0;
445 pendingcalls_to_do = 1;
446 if (lock != NULL)
447 PyThread_release_lock(lock);
448 return result;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000449}
450
451int
452Py_MakePendingCalls(void)
453{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000454 int i;
455 int r = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000456
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000457 if (!pending_lock) {
458 /* initial allocation of the lock */
459 pending_lock = PyThread_allocate_lock();
460 if (pending_lock == NULL)
461 return -1;
462 }
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000463
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000464 /* only service pending calls on main thread */
465 if (main_thread && PyThread_get_thread_ident() != main_thread)
466 return 0;
467 /* don't perform recursive pending calls */
468 if (pendingbusy)
469 return 0;
470 pendingbusy = 1;
471 /* perform a bounded number of calls, in case of recursion */
472 for (i=0; i<NPENDINGCALLS; i++) {
473 int j;
474 int (*func)(void *);
475 void *arg = NULL;
476
477 /* pop one item off the queue while holding the lock */
478 PyThread_acquire_lock(pending_lock, WAIT_LOCK);
479 j = pendingfirst;
480 if (j == pendinglast) {
481 func = NULL; /* Queue empty */
482 } else {
483 func = pendingcalls[j].func;
484 arg = pendingcalls[j].arg;
485 pendingfirst = (j + 1) % NPENDINGCALLS;
486 }
487 pendingcalls_to_do = pendingfirst != pendinglast;
488 PyThread_release_lock(pending_lock);
489 /* having released the lock, perform the callback */
490 if (func == NULL)
491 break;
492 r = func(arg);
493 if (r)
494 break;
495 }
496 pendingbusy = 0;
497 return r;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000498}
499
500#else /* if ! defined WITH_THREAD */
501
502/*
503 WARNING! ASYNCHRONOUSLY EXECUTING CODE!
504 This code is used for signal handling in python that isn't built
505 with WITH_THREAD.
506 Don't use this implementation when Py_AddPendingCalls() can happen
507 on a different thread!
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000508
Guido van Rossuma9672091994-09-14 13:31:22 +0000509 There are two possible race conditions:
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000510 (1) nested asynchronous calls to Py_AddPendingCall()
511 (2) AddPendingCall() calls made while pending calls are being processed.
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000512
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000513 (1) is very unlikely because typically signal delivery
514 is blocked during signal handling. So it should be impossible.
515 (2) is a real possibility.
Guido van Rossuma9672091994-09-14 13:31:22 +0000516 The current code is safe against (2), but not against (1).
517 The safety against (2) is derived from the fact that only one
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000518 thread is present, interrupted by signals, and that the critical
519 section is protected with the "busy" variable. On Windows, which
520 delivers SIGINT on a system thread, this does not hold and therefore
521 Windows really shouldn't use this version.
522 The two threads could theoretically wiggle around the "busy" variable.
Guido van Rossuma027efa1997-05-05 20:56:21 +0000523*/
Guido van Rossum8861b741996-07-30 16:49:37 +0000524
Guido van Rossuma9672091994-09-14 13:31:22 +0000525#define NPENDINGCALLS 32
526static struct {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000527 int (*func)(void *);
528 void *arg;
Guido van Rossuma9672091994-09-14 13:31:22 +0000529} pendingcalls[NPENDINGCALLS];
530static volatile int pendingfirst = 0;
531static volatile int pendinglast = 0;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000532static volatile int pendingcalls_to_do = 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000533
534int
Thomas Wouters334fb892000-07-25 12:56:38 +0000535Py_AddPendingCall(int (*func)(void *), void *arg)
Guido van Rossuma9672091994-09-14 13:31:22 +0000536{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000537 static volatile int busy = 0;
538 int i, j;
539 /* XXX Begin critical section */
540 if (busy)
541 return -1;
542 busy = 1;
543 i = pendinglast;
544 j = (i + 1) % NPENDINGCALLS;
545 if (j == pendingfirst) {
546 busy = 0;
547 return -1; /* Queue full */
548 }
549 pendingcalls[i].func = func;
550 pendingcalls[i].arg = arg;
551 pendinglast = j;
Skip Montanarod581d772002-09-03 20:10:45 +0000552
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000553 _Py_Ticker = 0;
554 pendingcalls_to_do = 1; /* Signal main loop */
555 busy = 0;
556 /* XXX End critical section */
557 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000558}
559
Guido van Rossum180d7b41994-09-29 09:45:57 +0000560int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000561Py_MakePendingCalls(void)
Guido van Rossuma9672091994-09-14 13:31:22 +0000562{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000563 static int busy = 0;
564 if (busy)
565 return 0;
566 busy = 1;
567 pendingcalls_to_do = 0;
568 for (;;) {
569 int i;
570 int (*func)(void *);
571 void *arg;
572 i = pendingfirst;
573 if (i == pendinglast)
574 break; /* Queue empty */
575 func = pendingcalls[i].func;
576 arg = pendingcalls[i].arg;
577 pendingfirst = (i + 1) % NPENDINGCALLS;
578 if (func(arg) < 0) {
579 busy = 0;
580 pendingcalls_to_do = 1; /* We're not done yet */
581 return -1;
582 }
583 }
584 busy = 0;
585 return 0;
Guido van Rossuma9672091994-09-14 13:31:22 +0000586}
587
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000588#endif /* WITH_THREAD */
589
Guido van Rossuma9672091994-09-14 13:31:22 +0000590
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000591/* The interpreter's recursion limit */
592
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000593#ifndef Py_DEFAULT_RECURSION_LIMIT
594#define Py_DEFAULT_RECURSION_LIMIT 1000
595#endif
596static int recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
597int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000598
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000599int
600Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000601{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000602 return recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000603}
604
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000605void
606Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000607{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000608 recursion_limit = new_limit;
609 _Py_CheckRecursionLimit = recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000610}
611
Armin Rigo2b3eb402003-10-28 12:05:48 +0000612/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
613 if the recursion_depth reaches _Py_CheckRecursionLimit.
614 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
615 to guarantee that _Py_CheckRecursiveCall() is regularly called.
616 Without USE_STACKCHECK, there is no need for this. */
617int
618_Py_CheckRecursiveCall(char *where)
619{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000620 PyThreadState *tstate = PyThreadState_GET();
Armin Rigo2b3eb402003-10-28 12:05:48 +0000621
622#ifdef USE_STACKCHECK
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000623 if (PyOS_CheckStack()) {
624 --tstate->recursion_depth;
625 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
626 return -1;
627 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000628#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000629 if (tstate->recursion_depth > recursion_limit) {
630 --tstate->recursion_depth;
631 PyErr_Format(PyExc_RuntimeError,
632 "maximum recursion depth exceeded%s",
633 where);
634 return -1;
635 }
636 _Py_CheckRecursionLimit = recursion_limit;
637 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000638}
639
Guido van Rossum374a9221991-04-04 10:40:29 +0000640/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000641enum why_code {
Stefan Krah7ff78252010-06-23 18:12:09 +0000642 WHY_NOT = 0x0001, /* No error */
643 WHY_EXCEPTION = 0x0002, /* Exception occurred */
644 WHY_RERAISE = 0x0004, /* Exception re-raised by 'finally' */
645 WHY_RETURN = 0x0008, /* 'return' statement */
646 WHY_BREAK = 0x0010, /* 'break' statement */
647 WHY_CONTINUE = 0x0020, /* 'continue' statement */
648 WHY_YIELD = 0x0040 /* 'yield' operator */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000649};
Guido van Rossum374a9221991-04-04 10:40:29 +0000650
Fredrik Lundh7a830892006-05-27 10:39:48 +0000651static enum why_code do_raise(PyObject *, PyObject *, PyObject *);
652static int unpack_iterable(PyObject *, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000653
Jeffrey Yasskinfd8a1ec2008-12-03 06:46:45 +0000654/* Records whether tracing is on for any thread. Counts the number of
655 threads for which tstate->c_tracefunc is non-NULL, so if the value
656 is 0, we know we don't have to check this thread's c_tracefunc.
657 This speeds up the if statement in PyEval_EvalFrameEx() after
658 fast_next_opcode*/
659static int _Py_TracingPossible = 0;
660
Skip Montanarod581d772002-09-03 20:10:45 +0000661/* for manipulating the thread switch and periodic "stuff" - used to be
662 per thread, now just a pair o' globals */
Skip Montanaro99dba272002-09-03 20:19:06 +0000663int _Py_CheckInterval = 100;
Kristján Valur Jónsson0e919382009-01-09 20:31:26 +0000664volatile int _Py_Ticker = 0; /* so that we hit a "tick" first thing */
Guido van Rossum374a9221991-04-04 10:40:29 +0000665
Guido van Rossumb209a111997-04-29 18:18:01 +0000666PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000667PyEval_EvalCode(PyCodeObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000668{
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000669 return PyEval_EvalCodeEx(co,
670 globals, locals,
671 (PyObject **)NULL, 0,
672 (PyObject **)NULL, 0,
673 (PyObject **)NULL, 0,
674 NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000675}
676
677
678/* Interpreter main loop */
679
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000680PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000681PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000682 /* This is for backward compatibility with extension modules that
683 used this API; core interpreter code should call
684 PyEval_EvalFrameEx() */
685 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000686}
687
688PyObject *
Anthony Baxtera863d332006-04-11 07:43:46 +0000689PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000690{
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500691#ifdef DYNAMIC_EXECUTION_PROFILE
692 #undef USE_COMPUTED_GOTOS
693#endif
694#ifdef HAVE_COMPUTED_GOTOS
695 #ifndef USE_COMPUTED_GOTOS
696 #define USE_COMPUTED_GOTOS 1
697 #endif
698#else
699 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
700 #error "Computed gotos are not supported on this compiler."
701 #endif
702 #undef USE_COMPUTED_GOTOS
703 #define USE_COMPUTED_GOTOS 0
704#endif
705#if USE_COMPUTED_GOTOS
706/* Import the static jump table */
707#include "opcode_targets.h"
708
709 /* This macro is used when several opcodes defer to the same implementation
710 (e.g. SETUP_LOOP, SETUP_FINALLY) */
711#define TARGET_WITH_IMPL(op, impl) \
712 TARGET_##op: \
713 opcode = op; \
714 oparg = NEXTARG(); \
715 case op: \
716 goto impl; \
717
718#define TARGET_WITH_IMPL_NOARG(op, impl) \
719 TARGET_##op: \
720 opcode = op; \
721 case op: \
722 goto impl; \
723
724#define TARGET_NOARG(op) \
725 TARGET_##op: \
726 opcode = op; \
727 case op:\
728
729#define TARGET(op) \
730 TARGET_##op: \
731 opcode = op; \
732 oparg = NEXTARG(); \
733 case op:\
734
735
736#define DISPATCH() \
737 { \
738 int _tick = _Py_Ticker - 1; \
739 _Py_Ticker = _tick; \
740 if (_tick >= 0) { \
741 FAST_DISPATCH(); \
742 } \
743 continue; \
744 }
745
746#ifdef LLTRACE
747#define FAST_DISPATCH() \
748 { \
749 if (!lltrace && !_Py_TracingPossible) { \
750 f->f_lasti = INSTR_OFFSET(); \
751 goto *opcode_targets[*next_instr++]; \
752 } \
753 goto fast_next_opcode; \
754 }
755#else
756#define FAST_DISPATCH() { \
757 if (!_Py_TracingPossible) { \
758 f->f_lasti = INSTR_OFFSET(); \
759 goto *opcode_targets[*next_instr++]; \
760 } \
761 goto fast_next_opcode;\
762}
763#endif
764
765#else
766#define TARGET(op) \
767 case op:
768#define TARGET_WITH_IMPL(op, impl) \
769 /* silence compiler warnings about `impl` unused */ \
770 if (0) goto impl; \
771 case op:\
772
773#define TARGET_NOARG(op) \
774 case op:\
775
776#define TARGET_WITH_IMPL_NOARG(op, impl) \
777 if (0) goto impl; \
778 case op:\
779
780#define DISPATCH() continue
781#define FAST_DISPATCH() goto fast_next_opcode
782#endif
783
784
Guido van Rossum950361c1997-01-24 13:49:28 +0000785#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000786 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000787#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000788 register PyObject **stack_pointer; /* Next free slot in value stack */
789 register unsigned char *next_instr;
790 register int opcode; /* Current opcode */
791 register int oparg; /* Current opcode argument, if any */
792 register enum why_code why; /* Reason for block stack unwind */
793 register int err; /* Error status -- nonzero if error */
794 register PyObject *x; /* Result object -- NULL if error */
795 register PyObject *v; /* Temporary objects popped off stack */
796 register PyObject *w;
797 register PyObject *u;
798 register PyObject *t;
799 register PyObject *stream = NULL; /* for PRINT opcodes */
800 register PyObject **fastlocals, **freevars;
801 PyObject *retval = NULL; /* Return value */
802 PyThreadState *tstate = PyThreadState_GET();
803 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000804
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000805 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000806
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000807 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000808
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000809 is true when the line being executed has changed. The
810 initial values are such as to make this false the first
811 time it is tested. */
812 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000813
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000814 unsigned char *first_instr;
815 PyObject *names;
816 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000817#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000818 /* Make it easier to find out where we are with a debugger */
819 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000820#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000821
Neal Norwitza81d2202002-07-14 00:27:26 +0000822/* Tuple access macros */
823
824#ifndef Py_DEBUG
825#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
826#else
827#define GETITEM(v, i) PyTuple_GetItem((v), (i))
828#endif
829
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000830#ifdef WITH_TSC
831/* Use Pentium timestamp counter to mark certain events:
832 inst0 -- beginning of switch statement for opcode dispatch
833 inst1 -- end of switch statement (may be skipped)
834 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000835 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000836 (may be skipped)
837 intr1 -- beginning of long interruption
838 intr2 -- end of long interruption
839
840 Many opcodes call out to helper C functions. In some cases, the
841 time in those functions should be counted towards the time for the
842 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
843 calls another Python function; there's no point in charge all the
844 bytecode executed by the called function to the caller.
845
846 It's hard to make a useful judgement statically. In the presence
847 of operator overloading, it's impossible to tell if a call will
848 execute new Python code or not.
849
850 It's a case-by-case judgement. I'll use intr1 for the following
851 cases:
852
853 EXEC_STMT
854 IMPORT_STAR
855 IMPORT_FROM
856 CALL_FUNCTION (and friends)
857
858 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000859 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
860 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000861
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000862 READ_TIMESTAMP(inst0);
863 READ_TIMESTAMP(inst1);
864 READ_TIMESTAMP(loop0);
865 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000866
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000867 /* shut up the compiler */
868 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000869#endif
870
Guido van Rossum374a9221991-04-04 10:40:29 +0000871/* Code access macros */
872
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000873#define INSTR_OFFSET() ((int)(next_instr - first_instr))
874#define NEXTOP() (*next_instr++)
875#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
876#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
877#define JUMPTO(x) (next_instr = first_instr + (x))
878#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000879
Raymond Hettingerf606f872003-03-16 03:11:04 +0000880/* OpCode prediction macros
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000881 Some opcodes tend to come in pairs thus making it possible to
882 predict the second code when the first is run. For example,
883 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
884 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000885
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000886 Verifying the prediction costs a single high-speed test of a register
887 variable against a constant. If the pairing was good, then the
888 processor's own internal branch predication has a high likelihood of
889 success, resulting in a nearly zero-overhead transition to the
890 next opcode. A successful prediction saves a trip through the eval-loop
891 including its two unpredictable branches, the HAS_ARG test and the
892 switch-case. Combined with the processor's internal branch prediction,
893 a successful PREDICT has the effect of making the two opcodes run as if
894 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000895
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000896 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000897 predictions turned-on and interpret the results as if some opcodes
898 had been combined or turn-off predictions so that the opcode frequency
899 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000900*/
901
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500902
903// Next opcode prediction is also enabled for Computed Gotos as well.
Raymond Hettingera7216982004-02-08 19:59:27 +0000904#ifdef DYNAMIC_EXECUTION_PROFILE
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500905#define PREDICT(op) //if (0) goto PRED_##op
906#define PREDICTED(op)
907#define PREDICTED_WITH_ARG(op)
Raymond Hettingera7216982004-02-08 19:59:27 +0000908#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000909#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500910#define PREDICTED(op) PRED_##op: next_instr++
911#ifdef USE_COMPUTED_GOTOS
912#define PREDICTED_WITH_ARG(op) PRED_##op: next_instr++
913#else
914#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
915#endif
Raymond Hettingera7216982004-02-08 19:59:27 +0000916#endif
917
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500918
Raymond Hettingerf606f872003-03-16 03:11:04 +0000919
Guido van Rossum374a9221991-04-04 10:40:29 +0000920/* Stack manipulation macros */
921
Martin v. Löwis18e16552006-02-15 17:27:45 +0000922/* The stack can grow at most MAXINT deep, as co_nlocals and
923 co_stacksize are ints. */
Stefan Krah7ff78252010-06-23 18:12:09 +0000924#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
925#define EMPTY() (STACK_LEVEL() == 0)
926#define TOP() (stack_pointer[-1])
927#define SECOND() (stack_pointer[-2])
928#define THIRD() (stack_pointer[-3])
929#define FOURTH() (stack_pointer[-4])
930#define PEEK(n) (stack_pointer[-(n)])
931#define SET_TOP(v) (stack_pointer[-1] = (v))
932#define SET_SECOND(v) (stack_pointer[-2] = (v))
933#define SET_THIRD(v) (stack_pointer[-3] = (v))
934#define SET_FOURTH(v) (stack_pointer[-4] = (v))
935#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
936#define BASIC_STACKADJ(n) (stack_pointer += n)
937#define BASIC_PUSH(v) (*stack_pointer++ = (v))
938#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000939
Guido van Rossum96a42c81992-01-12 02:29:51 +0000940#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000941#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000942 lltrace && prtrace(TOP(), "push")); \
943 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000944#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000945 BASIC_POP())
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000946#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000947 lltrace && prtrace(TOP(), "stackadj")); \
948 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000949#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krah7ff78252010-06-23 18:12:09 +0000950 prtrace((STACK_POINTER)[-1], "ext_pop")), \
951 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000952#else
Stefan Krah7ff78252010-06-23 18:12:09 +0000953#define PUSH(v) BASIC_PUSH(v)
954#define POP() BASIC_POP()
955#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000956#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000957#endif
958
Guido van Rossum681d79a1995-07-18 14:51:37 +0000959/* Local variable macros */
960
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000961#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000962
963/* The SETLOCAL() macro must not DECREF the local variable in-place and
964 then store the new value; it must copy the old value to a temporary
965 value, then store the new value, and then DECREF the temporary value.
966 This is because it is possible that during the DECREF the frame is
967 accessed by other code (e.g. a __del__ method or gc.collect()) and the
968 variable would be pointing to already-freed memory. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000969#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krah7ff78252010-06-23 18:12:09 +0000970 GETLOCAL(i) = value; \
971 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000972
Guido van Rossuma027efa1997-05-05 20:56:21 +0000973/* Start of code */
974
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000975 if (f == NULL)
976 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000977
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000978 /* push frame */
979 if (Py_EnterRecursiveCall(""))
980 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000981
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000982 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000983
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000984 if (tstate->use_tracing) {
985 if (tstate->c_tracefunc != NULL) {
986 /* tstate->c_tracefunc, if defined, is a
987 function that will be called on *every* entry
988 to a code block. Its return value, if not
989 None, is a function that will be called at
990 the start of each executed line of code.
991 (Actually, the function must return itself
992 in order to continue tracing.) The trace
993 functions are called with three arguments:
994 a pointer to the current frame, a string
995 indicating why the function is called, and
996 an argument which depends on the situation.
997 The global trace function is also called
998 whenever an exception is detected. */
999 if (call_trace_protected(tstate->c_tracefunc,
1000 tstate->c_traceobj,
1001 f, PyTrace_CALL, Py_None)) {
1002 /* Trace function raised an error */
1003 goto exit_eval_frame;
1004 }
1005 }
1006 if (tstate->c_profilefunc != NULL) {
1007 /* Similar for c_profilefunc, except it needn't
1008 return itself and isn't called for "line" events */
1009 if (call_trace_protected(tstate->c_profilefunc,
1010 tstate->c_profileobj,
1011 f, PyTrace_CALL, Py_None)) {
1012 /* Profile function raised an error */
1013 goto exit_eval_frame;
1014 }
1015 }
1016 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001017
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001018 co = f->f_code;
1019 names = co->co_names;
1020 consts = co->co_consts;
1021 fastlocals = f->f_localsplus;
1022 freevars = f->f_localsplus + co->co_nlocals;
1023 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
1024 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001025
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001026 f->f_lasti now refers to the index of the last instruction
1027 executed. You might think this was obvious from the name, but
1028 this wasn't always true before 2.3! PyFrame_New now sets
1029 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1030 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1031 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +00001032
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001033 When the PREDICT() macros are enabled, some opcode pairs follow in
1034 direct succession without updating f->f_lasti. A successful
1035 prediction effectively links the two codes together as if they
1036 were a single new opcode; accordingly,f->f_lasti will point to
1037 the first code in the pair (for instance, GET_ITER followed by
1038 FOR_ITER is effectively a single opcode and f->f_lasti will point
1039 at to the beginning of the combined pair.)
1040 */
1041 next_instr = first_instr + f->f_lasti + 1;
1042 stack_pointer = f->f_stacktop;
1043 assert(stack_pointer != NULL);
1044 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001045
Tim Peters5ca576e2001-06-18 22:08:13 +00001046#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001047 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001048#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001049#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001050 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001051#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001053 why = WHY_NOT;
1054 err = 0;
1055 x = Py_None; /* Not a reference, just anything non-NULL */
1056 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001057
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001058 if (throwflag) { /* support for generator.throw() */
1059 why = WHY_EXCEPTION;
1060 goto on_error;
1061 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00001062
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001063 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001064#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001065 if (inst1 == 0) {
1066 /* Almost surely, the opcode executed a break
1067 or a continue, preventing inst1 from being set
1068 on the way out of the loop.
1069 */
1070 READ_TIMESTAMP(inst1);
1071 loop1 = inst1;
1072 }
1073 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1074 intr0, intr1);
1075 ticked = 0;
1076 inst1 = 0;
1077 intr0 = 0;
1078 intr1 = 0;
1079 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001080#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001081 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1082 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001083
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001084 /* Do periodic things. Doing this every time through
1085 the loop would add too much overhead, so we do it
1086 only every Nth instruction. We also do it if
1087 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1088 event needs attention (e.g. a signal handler or
1089 async I/O handler); see Py_AddPendingCall() and
1090 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001091
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001092 if (--_Py_Ticker < 0) {
1093 if (*next_instr == SETUP_FINALLY) {
1094 /* Make the last opcode before
Ezio Melottic2077b02011-03-16 12:34:31 +02001095 a try: finally: block uninterruptible. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001096 goto fast_next_opcode;
1097 }
1098 _Py_Ticker = _Py_CheckInterval;
1099 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001100#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001101 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001102#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001103 if (pendingcalls_to_do) {
1104 if (Py_MakePendingCalls() < 0) {
1105 why = WHY_EXCEPTION;
1106 goto on_error;
1107 }
1108 if (pendingcalls_to_do)
1109 /* MakePendingCalls() didn't succeed.
1110 Force early re-execution of this
1111 "periodic" code, possibly after
1112 a thread switch */
1113 _Py_Ticker = 0;
1114 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001115#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001116 if (interpreter_lock) {
1117 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001118
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001119 if (PyThreadState_Swap(NULL) != tstate)
1120 Py_FatalError("ceval: tstate mix-up");
1121 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001123 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001124
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001125 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Peterson1c78e6d2014-06-16 22:59:07 -07001126
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001127 if (PyThreadState_Swap(tstate) != NULL)
1128 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001129
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001130 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001131
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001132 if (tstate->async_exc != NULL) {
1133 x = tstate->async_exc;
1134 tstate->async_exc = NULL;
1135 PyErr_SetNone(x);
1136 Py_DECREF(x);
1137 why = WHY_EXCEPTION;
1138 goto on_error;
1139 }
1140 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001141#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001142 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001144 fast_next_opcode:
1145 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001146
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001147 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001148
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001149 if (_Py_TracingPossible &&
1150 tstate->c_tracefunc != NULL && !tstate->tracing) {
1151 /* see maybe_call_line_trace
1152 for expository comments */
1153 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001154
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001155 err = maybe_call_line_trace(tstate->c_tracefunc,
1156 tstate->c_traceobj,
1157 f, &instr_lb, &instr_ub,
1158 &instr_prev);
1159 /* Reload possibly changed frame fields */
1160 JUMPTO(f->f_lasti);
1161 if (f->f_stacktop != NULL) {
1162 stack_pointer = f->f_stacktop;
1163 f->f_stacktop = NULL;
1164 }
1165 if (err) {
1166 /* trace function raised an exception */
1167 goto on_error;
1168 }
1169 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001170
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001171 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001172
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001173 opcode = NEXTOP();
1174 oparg = 0; /* allows oparg to be stored in a register because
1175 it doesn't have to be remembered across a full loop */
1176 if (HAS_ARG(opcode))
1177 oparg = NEXTARG();
Stefan Krah7ff78252010-06-23 18:12:09 +00001178 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001179#ifdef DYNAMIC_EXECUTION_PROFILE
1180#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001181 dxpairs[lastopcode][opcode]++;
1182 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001183#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001185#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001186
Guido van Rossum96a42c81992-01-12 02:29:51 +00001187#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001188 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001189
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001190 if (lltrace) {
1191 if (HAS_ARG(opcode)) {
1192 printf("%d: %d, %d\n",
1193 f->f_lasti, opcode, oparg);
1194 }
1195 else {
1196 printf("%d: %d\n",
1197 f->f_lasti, opcode);
1198 }
1199 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001200#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001201
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001202 /* Main switch on opcode */
1203 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001204
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001205 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001207 /* BEWARE!
1208 It is essential that any operation that fails sets either
1209 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1210 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001211
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001212 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001214 TARGET_NOARG(NOP)
1215 {
1216 FAST_DISPATCH();
1217 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001218
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001219 TARGET(LOAD_FAST)
1220 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001221 x = GETLOCAL(oparg);
1222 if (x != NULL) {
1223 Py_INCREF(x);
1224 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001225 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001226 }
1227 format_exc_check_arg(PyExc_UnboundLocalError,
1228 UNBOUNDLOCAL_ERROR_MSG,
1229 PyTuple_GetItem(co->co_varnames, oparg));
1230 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001231 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001232
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001233 TARGET(LOAD_CONST)
1234 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001235 x = GETITEM(consts, oparg);
1236 Py_INCREF(x);
1237 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001238 FAST_DISPATCH();
1239 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001241 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001242 TARGET(STORE_FAST)
1243 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001244 v = POP();
1245 SETLOCAL(oparg, v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001246 FAST_DISPATCH();
1247 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001248
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001249 TARGET_NOARG(POP_TOP)
1250 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001251 v = POP();
1252 Py_DECREF(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001253 FAST_DISPATCH();
1254 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001255
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001256 TARGET_NOARG(ROT_TWO)
1257 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001258 v = TOP();
1259 w = SECOND();
1260 SET_TOP(w);
1261 SET_SECOND(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001262 FAST_DISPATCH();
1263 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001264
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001265 TARGET_NOARG(ROT_THREE)
1266 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001267 v = TOP();
1268 w = SECOND();
1269 x = THIRD();
1270 SET_TOP(w);
1271 SET_SECOND(x);
1272 SET_THIRD(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001273 FAST_DISPATCH();
1274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001275
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001276 TARGET_NOARG(ROT_FOUR)
1277 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001278 u = TOP();
1279 v = SECOND();
1280 w = THIRD();
1281 x = FOURTH();
1282 SET_TOP(v);
1283 SET_SECOND(w);
1284 SET_THIRD(x);
1285 SET_FOURTH(u);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001286 FAST_DISPATCH();
1287 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001288
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001289
1290 TARGET_NOARG(DUP_TOP)
1291 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001292 v = TOP();
1293 Py_INCREF(v);
1294 PUSH(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001295 FAST_DISPATCH();
1296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001297
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001298
1299 TARGET(DUP_TOPX)
1300 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001301 if (oparg == 2) {
1302 x = TOP();
1303 Py_INCREF(x);
1304 w = SECOND();
1305 Py_INCREF(w);
1306 STACKADJ(2);
1307 SET_TOP(x);
1308 SET_SECOND(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001309 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001310 } else if (oparg == 3) {
1311 x = TOP();
1312 Py_INCREF(x);
1313 w = SECOND();
1314 Py_INCREF(w);
1315 v = THIRD();
1316 Py_INCREF(v);
1317 STACKADJ(3);
1318 SET_TOP(x);
1319 SET_SECOND(w);
1320 SET_THIRD(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001321 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001322 }
1323 Py_FatalError("invalid argument to DUP_TOPX"
1324 " (bytecode corruption?)");
1325 /* Never returns, so don't bother to set why. */
1326 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001327 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001328
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001329 TARGET_NOARG(UNARY_POSITIVE)
1330 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001331 v = TOP();
1332 x = PyNumber_Positive(v);
1333 Py_DECREF(v);
1334 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001335 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001336 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001337 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001338
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001339 TARGET_NOARG( UNARY_NEGATIVE)
1340 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001341 v = TOP();
1342 x = PyNumber_Negative(v);
1343 Py_DECREF(v);
1344 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001345 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001346 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001347 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001348
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001349 TARGET_NOARG(UNARY_NOT)
1350 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001351 v = TOP();
1352 err = PyObject_IsTrue(v);
1353 Py_DECREF(v);
1354 if (err == 0) {
1355 Py_INCREF(Py_True);
1356 SET_TOP(Py_True);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001357 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001358 }
1359 else if (err > 0) {
1360 Py_INCREF(Py_False);
1361 SET_TOP(Py_False);
1362 err = 0;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001363 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001364 }
1365 STACKADJ(-1);
1366 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001367 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001368
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001369 TARGET_NOARG(UNARY_CONVERT)
1370 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001371 v = TOP();
1372 x = PyObject_Repr(v);
1373 Py_DECREF(v);
1374 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001375 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001376 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001377 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001378
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001379 TARGET_NOARG(UNARY_INVERT)
1380 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001381 v = TOP();
1382 x = PyNumber_Invert(v);
1383 Py_DECREF(v);
1384 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001385 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001386 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001387 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001388
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001389 TARGET_NOARG(BINARY_POWER)
1390 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001391 w = POP();
1392 v = TOP();
1393 x = PyNumber_Power(v, w, Py_None);
1394 Py_DECREF(v);
1395 Py_DECREF(w);
1396 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001397 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001398 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001399 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001400
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001401 TARGET_NOARG(BINARY_MULTIPLY)
1402 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001403 w = POP();
1404 v = TOP();
1405 x = PyNumber_Multiply(v, w);
1406 Py_DECREF(v);
1407 Py_DECREF(w);
1408 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001409 if(x!=NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001410 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001411 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001412
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001413 TARGET_NOARG(BINARY_DIVIDE)
1414 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001415 if (!_Py_QnewFlag) {
1416 w = POP();
1417 v = TOP();
1418 x = PyNumber_Divide(v, w);
1419 Py_DECREF(v);
1420 Py_DECREF(w);
1421 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001422 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001423 break;
1424 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001425 }
1426 /* -Qnew is in effect: fall through to BINARY_TRUE_DIVIDE */
1427 TARGET_NOARG(BINARY_TRUE_DIVIDE)
1428 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001429 w = POP();
1430 v = TOP();
1431 x = PyNumber_TrueDivide(v, w);
1432 Py_DECREF(v);
1433 Py_DECREF(w);
1434 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001435 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001436 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001437 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001438
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001439 TARGET_NOARG(BINARY_FLOOR_DIVIDE)
1440 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001441 w = POP();
1442 v = TOP();
1443 x = PyNumber_FloorDivide(v, w);
1444 Py_DECREF(v);
1445 Py_DECREF(w);
1446 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001447 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001448 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001449 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001450
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001451 TARGET_NOARG(BINARY_MODULO)
1452 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001453 w = POP();
1454 v = TOP();
1455 if (PyString_CheckExact(v))
1456 x = PyString_Format(v, w);
1457 else
1458 x = PyNumber_Remainder(v, w);
1459 Py_DECREF(v);
1460 Py_DECREF(w);
1461 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001462 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001464 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001465
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001466 TARGET_NOARG(BINARY_ADD)
1467 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001468 w = POP();
1469 v = TOP();
1470 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1471 /* INLINE: int + int */
1472 register long a, b, i;
1473 a = PyInt_AS_LONG(v);
1474 b = PyInt_AS_LONG(w);
1475 /* cast to avoid undefined behaviour
1476 on overflow */
1477 i = (long)((unsigned long)a + b);
1478 if ((i^a) < 0 && (i^b) < 0)
1479 goto slow_add;
1480 x = PyInt_FromLong(i);
1481 }
1482 else if (PyString_CheckExact(v) &&
1483 PyString_CheckExact(w)) {
1484 x = string_concatenate(v, w, f, next_instr);
1485 /* string_concatenate consumed the ref to v */
1486 goto skip_decref_vx;
1487 }
1488 else {
1489 slow_add:
1490 x = PyNumber_Add(v, w);
1491 }
1492 Py_DECREF(v);
1493 skip_decref_vx:
1494 Py_DECREF(w);
1495 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001496 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001497 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001500 TARGET_NOARG(BINARY_SUBTRACT)
1501 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001502 w = POP();
1503 v = TOP();
1504 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1505 /* INLINE: int - int */
1506 register long a, b, i;
1507 a = PyInt_AS_LONG(v);
1508 b = PyInt_AS_LONG(w);
1509 /* cast to avoid undefined behaviour
1510 on overflow */
1511 i = (long)((unsigned long)a - b);
1512 if ((i^a) < 0 && (i^~b) < 0)
1513 goto slow_sub;
1514 x = PyInt_FromLong(i);
1515 }
1516 else {
1517 slow_sub:
1518 x = PyNumber_Subtract(v, w);
1519 }
1520 Py_DECREF(v);
1521 Py_DECREF(w);
1522 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001523 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001524 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001525 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001526
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001527 TARGET_NOARG(BINARY_SUBSCR)
1528 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001529 w = POP();
1530 v = TOP();
1531 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1532 /* INLINE: list[int] */
1533 Py_ssize_t i = PyInt_AsSsize_t(w);
1534 if (i < 0)
1535 i += PyList_GET_SIZE(v);
1536 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1537 x = PyList_GET_ITEM(v, i);
1538 Py_INCREF(x);
1539 }
1540 else
1541 goto slow_get;
1542 }
1543 else
1544 slow_get:
1545 x = PyObject_GetItem(v, w);
1546 Py_DECREF(v);
1547 Py_DECREF(w);
1548 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001549 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001550 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001551 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001552
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001553 TARGET_NOARG(BINARY_LSHIFT)
1554 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001555 w = POP();
1556 v = TOP();
1557 x = PyNumber_Lshift(v, w);
1558 Py_DECREF(v);
1559 Py_DECREF(w);
1560 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001561 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001562 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001563 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001564
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001565 TARGET_NOARG(BINARY_RSHIFT)
1566 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001567 w = POP();
1568 v = TOP();
1569 x = PyNumber_Rshift(v, w);
1570 Py_DECREF(v);
1571 Py_DECREF(w);
1572 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001573 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001574 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001575 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001576
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001577 TARGET_NOARG(BINARY_AND)
1578 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001579 w = POP();
1580 v = TOP();
1581 x = PyNumber_And(v, w);
1582 Py_DECREF(v);
1583 Py_DECREF(w);
1584 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001585 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001586 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001587 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001589 TARGET_NOARG(BINARY_XOR)
1590 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001591 w = POP();
1592 v = TOP();
1593 x = PyNumber_Xor(v, w);
1594 Py_DECREF(v);
1595 Py_DECREF(w);
1596 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001597 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001598 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001599 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001600
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001601 TARGET_NOARG(BINARY_OR)
1602 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001603 w = POP();
1604 v = TOP();
1605 x = PyNumber_Or(v, w);
1606 Py_DECREF(v);
1607 Py_DECREF(w);
1608 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001609 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001610 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001611 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001612
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001613 TARGET(LIST_APPEND)
1614 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001615 w = POP();
1616 v = PEEK(oparg);
1617 err = PyList_Append(v, w);
1618 Py_DECREF(w);
1619 if (err == 0) {
1620 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001621 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001622 }
1623 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001624 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001625
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001626 TARGET(SET_ADD)
1627 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001628 w = POP();
1629 v = stack_pointer[-oparg];
1630 err = PySet_Add(v, w);
1631 Py_DECREF(w);
1632 if (err == 0) {
1633 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001634 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001635 }
1636 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001637 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001638
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001639 TARGET_NOARG(INPLACE_POWER)
1640 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001641 w = POP();
1642 v = TOP();
1643 x = PyNumber_InPlacePower(v, w, Py_None);
1644 Py_DECREF(v);
1645 Py_DECREF(w);
1646 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001647 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001648 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001650
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001651 TARGET_NOARG(INPLACE_MULTIPLY)
1652 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001653 w = POP();
1654 v = TOP();
1655 x = PyNumber_InPlaceMultiply(v, w);
1656 Py_DECREF(v);
1657 Py_DECREF(w);
1658 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001659 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001660 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001663 TARGET_NOARG(INPLACE_DIVIDE)
1664 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001665 if (!_Py_QnewFlag) {
1666 w = POP();
1667 v = TOP();
1668 x = PyNumber_InPlaceDivide(v, w);
1669 Py_DECREF(v);
1670 Py_DECREF(w);
1671 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001672 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001673 break;
1674 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001675 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001676 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001677 INPLACE_TRUE_DIVIDE */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001678 TARGET_NOARG(INPLACE_TRUE_DIVIDE)
1679 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001680 w = POP();
1681 v = TOP();
1682 x = PyNumber_InPlaceTrueDivide(v, w);
1683 Py_DECREF(v);
1684 Py_DECREF(w);
1685 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001686 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001687 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001688 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001689
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001690 TARGET_NOARG(INPLACE_FLOOR_DIVIDE)
1691 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001692 w = POP();
1693 v = TOP();
1694 x = PyNumber_InPlaceFloorDivide(v, w);
1695 Py_DECREF(v);
1696 Py_DECREF(w);
1697 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001698 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001699 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001700 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001701
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001702 TARGET_NOARG(INPLACE_MODULO)
1703 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001704 w = POP();
1705 v = TOP();
1706 x = PyNumber_InPlaceRemainder(v, w);
1707 Py_DECREF(v);
1708 Py_DECREF(w);
1709 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001710 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001711 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001712 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001713
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001714 TARGET_NOARG(INPLACE_ADD)
1715 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001716 w = POP();
1717 v = TOP();
1718 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1719 /* INLINE: int + int */
1720 register long a, b, i;
1721 a = PyInt_AS_LONG(v);
1722 b = PyInt_AS_LONG(w);
1723 i = a + b;
1724 if ((i^a) < 0 && (i^b) < 0)
1725 goto slow_iadd;
1726 x = PyInt_FromLong(i);
1727 }
1728 else if (PyString_CheckExact(v) &&
1729 PyString_CheckExact(w)) {
1730 x = string_concatenate(v, w, f, next_instr);
1731 /* string_concatenate consumed the ref to v */
1732 goto skip_decref_v;
1733 }
1734 else {
1735 slow_iadd:
1736 x = PyNumber_InPlaceAdd(v, w);
1737 }
1738 Py_DECREF(v);
1739 skip_decref_v:
1740 Py_DECREF(w);
1741 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001742 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001743 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001744 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001745
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001746 TARGET_NOARG(INPLACE_SUBTRACT)
1747 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001748 w = POP();
1749 v = TOP();
1750 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1751 /* INLINE: int - int */
1752 register long a, b, i;
1753 a = PyInt_AS_LONG(v);
1754 b = PyInt_AS_LONG(w);
1755 i = a - b;
1756 if ((i^a) < 0 && (i^~b) < 0)
1757 goto slow_isub;
1758 x = PyInt_FromLong(i);
1759 }
1760 else {
1761 slow_isub:
1762 x = PyNumber_InPlaceSubtract(v, w);
1763 }
1764 Py_DECREF(v);
1765 Py_DECREF(w);
1766 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001767 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001768 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001769 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001770
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001771 TARGET_NOARG(INPLACE_LSHIFT)
1772 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001773 w = POP();
1774 v = TOP();
1775 x = PyNumber_InPlaceLshift(v, w);
1776 Py_DECREF(v);
1777 Py_DECREF(w);
1778 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001779 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001780 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001781 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001782
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001783 TARGET_NOARG(INPLACE_RSHIFT)
1784 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001785 w = POP();
1786 v = TOP();
1787 x = PyNumber_InPlaceRshift(v, w);
1788 Py_DECREF(v);
1789 Py_DECREF(w);
1790 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001791 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001792 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001793 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001794
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001795 TARGET_NOARG(INPLACE_AND)
1796 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001797 w = POP();
1798 v = TOP();
1799 x = PyNumber_InPlaceAnd(v, w);
1800 Py_DECREF(v);
1801 Py_DECREF(w);
1802 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001803 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001804 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001805 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001806
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001807 TARGET_NOARG(INPLACE_XOR)
1808 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001809 w = POP();
1810 v = TOP();
1811 x = PyNumber_InPlaceXor(v, w);
1812 Py_DECREF(v);
1813 Py_DECREF(w);
1814 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001815 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001816 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001818
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001819 TARGET_NOARG(INPLACE_OR)
1820 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001821 w = POP();
1822 v = TOP();
1823 x = PyNumber_InPlaceOr(v, w);
1824 Py_DECREF(v);
1825 Py_DECREF(w);
1826 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001827 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001828 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001829 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001830
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001831
1832
1833 TARGET_WITH_IMPL_NOARG(SLICE, _slice)
1834 TARGET_WITH_IMPL_NOARG(SLICE_1, _slice)
1835 TARGET_WITH_IMPL_NOARG(SLICE_2, _slice)
1836 TARGET_WITH_IMPL_NOARG(SLICE_3, _slice)
1837 _slice:
1838 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001839 if ((opcode-SLICE) & 2)
1840 w = POP();
1841 else
1842 w = NULL;
1843 if ((opcode-SLICE) & 1)
1844 v = POP();
1845 else
1846 v = NULL;
1847 u = TOP();
1848 x = apply_slice(u, v, w);
1849 Py_DECREF(u);
1850 Py_XDECREF(v);
1851 Py_XDECREF(w);
1852 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001853 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001854 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001855 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001856
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001857
1858 TARGET_WITH_IMPL_NOARG(STORE_SLICE, _store_slice)
1859 TARGET_WITH_IMPL_NOARG(STORE_SLICE_1, _store_slice)
1860 TARGET_WITH_IMPL_NOARG(STORE_SLICE_2, _store_slice)
1861 TARGET_WITH_IMPL_NOARG(STORE_SLICE_3, _store_slice)
1862 _store_slice:
1863 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001864 if ((opcode-STORE_SLICE) & 2)
1865 w = POP();
1866 else
1867 w = NULL;
1868 if ((opcode-STORE_SLICE) & 1)
1869 v = POP();
1870 else
1871 v = NULL;
1872 u = POP();
1873 t = POP();
1874 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1875 Py_DECREF(t);
1876 Py_DECREF(u);
1877 Py_XDECREF(v);
1878 Py_XDECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001879 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001880 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001881 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001882
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001883
1884 TARGET_WITH_IMPL_NOARG(DELETE_SLICE, _delete_slice)
1885 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_1, _delete_slice)
1886 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_2, _delete_slice)
1887 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_3, _delete_slice)
1888 _delete_slice:
1889 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001890 if ((opcode-DELETE_SLICE) & 2)
1891 w = POP();
1892 else
1893 w = NULL;
1894 if ((opcode-DELETE_SLICE) & 1)
1895 v = POP();
1896 else
1897 v = NULL;
1898 u = POP();
1899 err = assign_slice(u, v, w, (PyObject *)NULL);
1900 /* del u[v:w] */
1901 Py_DECREF(u);
1902 Py_XDECREF(v);
1903 Py_XDECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001904 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001905 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001906 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001907
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001908 TARGET_NOARG(STORE_SUBSCR)
1909 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001910 w = TOP();
1911 v = SECOND();
1912 u = THIRD();
1913 STACKADJ(-3);
1914 /* v[w] = u */
1915 err = PyObject_SetItem(v, w, u);
1916 Py_DECREF(u);
1917 Py_DECREF(v);
1918 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001919 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001920 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001921 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001922
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001923 TARGET_NOARG(DELETE_SUBSCR)
1924 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001925 w = TOP();
1926 v = SECOND();
1927 STACKADJ(-2);
1928 /* del v[w] */
1929 err = PyObject_DelItem(v, w);
1930 Py_DECREF(v);
1931 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001932 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001933 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001934 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001935
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001936 TARGET_NOARG(PRINT_EXPR)
1937 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001938 v = POP();
1939 w = PySys_GetObject("displayhook");
1940 if (w == NULL) {
1941 PyErr_SetString(PyExc_RuntimeError,
1942 "lost sys.displayhook");
1943 err = -1;
1944 x = NULL;
1945 }
1946 if (err == 0) {
1947 x = PyTuple_Pack(1, v);
1948 if (x == NULL)
1949 err = -1;
1950 }
1951 if (err == 0) {
1952 w = PyEval_CallObject(w, x);
1953 Py_XDECREF(w);
1954 if (w == NULL)
1955 err = -1;
1956 }
1957 Py_DECREF(v);
1958 Py_XDECREF(x);
1959 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001960 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001961
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001962 TARGET_NOARG(PRINT_ITEM_TO)
1963 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001964 w = stream = POP();
1965 /* fall through to PRINT_ITEM */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001966 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001967
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001968 TARGET_NOARG(PRINT_ITEM)
1969 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001970 v = POP();
1971 if (stream == NULL || stream == Py_None) {
1972 w = PySys_GetObject("stdout");
1973 if (w == NULL) {
1974 PyErr_SetString(PyExc_RuntimeError,
1975 "lost sys.stdout");
1976 err = -1;
1977 }
1978 }
1979 /* PyFile_SoftSpace() can exececute arbitrary code
1980 if sys.stdout is an instance with a __getattr__.
1981 If __getattr__ raises an exception, w will
1982 be freed, so we need to prevent that temporarily. */
1983 Py_XINCREF(w);
1984 if (w != NULL && PyFile_SoftSpace(w, 0))
1985 err = PyFile_WriteString(" ", w);
1986 if (err == 0)
1987 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1988 if (err == 0) {
1989 /* XXX move into writeobject() ? */
1990 if (PyString_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001991 char *s = PyString_AS_STRING(v);
1992 Py_ssize_t len = PyString_GET_SIZE(v);
1993 if (len == 0 ||
1994 !isspace(Py_CHARMASK(s[len-1])) ||
1995 s[len-1] == ' ')
1996 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001997 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001998#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001999 else if (PyUnicode_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00002000 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
2001 Py_ssize_t len = PyUnicode_GET_SIZE(v);
2002 if (len == 0 ||
2003 !Py_UNICODE_ISSPACE(s[len-1]) ||
2004 s[len-1] == ' ')
2005 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002006 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00002007#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002008 else
Stefan Krah7ff78252010-06-23 18:12:09 +00002009 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002010 }
2011 Py_XDECREF(w);
2012 Py_DECREF(v);
2013 Py_XDECREF(stream);
2014 stream = NULL;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002015 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002016 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002017 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002018
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002019 TARGET_NOARG(PRINT_NEWLINE_TO)
2020 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002021 w = stream = POP();
2022 /* fall through to PRINT_NEWLINE */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002023 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002024
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002025 TARGET_NOARG(PRINT_NEWLINE)
2026 {
2027 if (stream == NULL || stream == Py_None)
2028 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002029 w = PySys_GetObject("stdout");
2030 if (w == NULL) {
2031 PyErr_SetString(PyExc_RuntimeError,
2032 "lost sys.stdout");
2033 why = WHY_EXCEPTION;
2034 }
2035 }
2036 if (w != NULL) {
2037 /* w.write() may replace sys.stdout, so we
2038 * have to keep our reference to it */
2039 Py_INCREF(w);
2040 err = PyFile_WriteString("\n", w);
2041 if (err == 0)
2042 PyFile_SoftSpace(w, 0);
2043 Py_DECREF(w);
2044 }
2045 Py_XDECREF(stream);
2046 stream = NULL;
2047 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002048 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002049
2050#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002051 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002052#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002053
2054 TARGET(RAISE_VARARGS)
2055 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002056 u = v = w = NULL;
2057 switch (oparg) {
2058 case 3:
2059 u = POP(); /* traceback */
2060 /* Fallthrough */
2061 case 2:
2062 v = POP(); /* value */
2063 /* Fallthrough */
2064 case 1:
2065 w = POP(); /* exc */
2066 case 0: /* Fallthrough */
2067 why = do_raise(w, v, u);
2068 break;
2069 default:
2070 PyErr_SetString(PyExc_SystemError,
2071 "bad RAISE_VARARGS oparg");
2072 why = WHY_EXCEPTION;
2073 break;
2074 }
2075 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002076 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002077
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002078 TARGET_NOARG(LOAD_LOCALS)
2079 {
2080 if ((x = f->f_locals) != NULL)
2081 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002082 Py_INCREF(x);
2083 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002084 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002085 }
2086 PyErr_SetString(PyExc_SystemError, "no locals");
2087 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002088 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002089
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002090 TARGET_NOARG(RETURN_VALUE)
2091 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002092 retval = POP();
2093 why = WHY_RETURN;
2094 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002095 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002096
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002097 TARGET_NOARG(YIELD_VALUE)
2098 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002099 retval = POP();
2100 f->f_stacktop = stack_pointer;
2101 why = WHY_YIELD;
2102 goto fast_yield;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002103 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002104
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002105 TARGET_NOARG(EXEC_STMT)
2106 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002107 w = TOP();
2108 v = SECOND();
2109 u = THIRD();
2110 STACKADJ(-3);
2111 READ_TIMESTAMP(intr0);
2112 err = exec_statement(f, u, v, w);
2113 READ_TIMESTAMP(intr1);
2114 Py_DECREF(u);
2115 Py_DECREF(v);
2116 Py_DECREF(w);
2117 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002118 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002119
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002120 TARGET_NOARG(POP_BLOCK)
2121 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002122 {
2123 PyTryBlock *b = PyFrame_BlockPop(f);
2124 while (STACK_LEVEL() > b->b_level) {
2125 v = POP();
2126 Py_DECREF(v);
2127 }
2128 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002129 DISPATCH();
2130 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002131
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002132 PREDICTED(END_FINALLY);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002133 TARGET_NOARG(END_FINALLY)
2134 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002135 v = POP();
2136 if (PyInt_Check(v)) {
2137 why = (enum why_code) PyInt_AS_LONG(v);
2138 assert(why != WHY_YIELD);
2139 if (why == WHY_RETURN ||
2140 why == WHY_CONTINUE)
2141 retval = POP();
2142 }
2143 else if (PyExceptionClass_Check(v) ||
2144 PyString_Check(v)) {
2145 w = POP();
2146 u = POP();
2147 PyErr_Restore(v, w, u);
2148 why = WHY_RERAISE;
2149 break;
2150 }
2151 else if (v != Py_None) {
2152 PyErr_SetString(PyExc_SystemError,
2153 "'finally' pops bad exception");
2154 why = WHY_EXCEPTION;
2155 }
2156 Py_DECREF(v);
2157 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002158 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002159
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002160 TARGET_NOARG(BUILD_CLASS)
2161 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002162 u = TOP();
2163 v = SECOND();
2164 w = THIRD();
2165 STACKADJ(-2);
2166 x = build_class(u, v, w);
2167 SET_TOP(x);
2168 Py_DECREF(u);
2169 Py_DECREF(v);
2170 Py_DECREF(w);
2171 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002172 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002173
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002174 TARGET(STORE_NAME)
2175 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002176 w = GETITEM(names, oparg);
2177 v = POP();
2178 if ((x = f->f_locals) != NULL) {
2179 if (PyDict_CheckExact(x))
2180 err = PyDict_SetItem(x, w, v);
2181 else
2182 err = PyObject_SetItem(x, w, v);
2183 Py_DECREF(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002184 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002185 break;
2186 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002187 t = PyObject_Repr(w);
2188 if (t == NULL)
2189 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002190 PyErr_Format(PyExc_SystemError,
2191 "no locals found when storing %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002192 PyString_AS_STRING(t));
2193 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002194 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002196
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002197 TARGET(DELETE_NAME)
2198 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002199 w = GETITEM(names, oparg);
2200 if ((x = f->f_locals) != NULL) {
2201 if ((err = PyObject_DelItem(x, w)) != 0)
2202 format_exc_check_arg(PyExc_NameError,
2203 NAME_ERROR_MSG,
2204 w);
2205 break;
2206 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002207 t = PyObject_Repr(w);
2208 if (t == NULL)
2209 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002210 PyErr_Format(PyExc_SystemError,
2211 "no locals when deleting %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002212 PyString_AS_STRING(w));
2213 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002214 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002215 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002216
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002217 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002218 TARGET(UNPACK_SEQUENCE)
2219 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002220 v = POP();
2221 if (PyTuple_CheckExact(v) &&
2222 PyTuple_GET_SIZE(v) == oparg) {
2223 PyObject **items = \
2224 ((PyTupleObject *)v)->ob_item;
2225 while (oparg--) {
2226 w = items[oparg];
2227 Py_INCREF(w);
2228 PUSH(w);
2229 }
2230 Py_DECREF(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002231 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002232 } else if (PyList_CheckExact(v) &&
2233 PyList_GET_SIZE(v) == oparg) {
2234 PyObject **items = \
2235 ((PyListObject *)v)->ob_item;
2236 while (oparg--) {
2237 w = items[oparg];
2238 Py_INCREF(w);
2239 PUSH(w);
2240 }
2241 } else if (unpack_iterable(v, oparg,
2242 stack_pointer + oparg)) {
2243 STACKADJ(oparg);
2244 } else {
2245 /* unpack_iterable() raised an exception */
2246 why = WHY_EXCEPTION;
2247 }
2248 Py_DECREF(v);
2249 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002250 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002251
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002252
2253 TARGET(STORE_ATTR)
2254 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002255 w = GETITEM(names, oparg);
2256 v = TOP();
2257 u = SECOND();
2258 STACKADJ(-2);
2259 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2260 Py_DECREF(v);
2261 Py_DECREF(u);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002262 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002263 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002264 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002265
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002266 TARGET(DELETE_ATTR)
2267 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002268 w = GETITEM(names, oparg);
2269 v = POP();
2270 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2271 /* del v.w */
2272 Py_DECREF(v);
2273 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002275
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002276
2277 TARGET(STORE_GLOBAL)
2278 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002279 w = GETITEM(names, oparg);
2280 v = POP();
2281 err = PyDict_SetItem(f->f_globals, w, v);
2282 Py_DECREF(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002283 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002284 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002285 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002287 TARGET(DELETE_GLOBAL)
2288 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002289 w = GETITEM(names, oparg);
2290 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2291 format_exc_check_arg(
2292 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2293 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002294 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002296 TARGET(LOAD_NAME)
2297 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002298 w = GETITEM(names, oparg);
2299 if ((v = f->f_locals) == NULL) {
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002300 why = WHY_EXCEPTION;
2301 t = PyObject_Repr(w);
2302 if (t == NULL)
2303 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002304 PyErr_Format(PyExc_SystemError,
2305 "no locals when loading %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002306 PyString_AS_STRING(w));
2307 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002308 break;
2309 }
2310 if (PyDict_CheckExact(v)) {
2311 x = PyDict_GetItem(v, w);
2312 Py_XINCREF(x);
2313 }
2314 else {
2315 x = PyObject_GetItem(v, w);
2316 if (x == NULL && PyErr_Occurred()) {
2317 if (!PyErr_ExceptionMatches(
2318 PyExc_KeyError))
2319 break;
2320 PyErr_Clear();
2321 }
2322 }
2323 if (x == NULL) {
2324 x = PyDict_GetItem(f->f_globals, w);
2325 if (x == NULL) {
2326 x = PyDict_GetItem(f->f_builtins, w);
2327 if (x == NULL) {
2328 format_exc_check_arg(
2329 PyExc_NameError,
2330 NAME_ERROR_MSG, w);
2331 break;
2332 }
2333 }
2334 Py_INCREF(x);
2335 }
2336 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002337 DISPATCH();
2338 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002339
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002340 TARGET(LOAD_GLOBAL)
2341 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002342 w = GETITEM(names, oparg);
2343 if (PyString_CheckExact(w)) {
2344 /* Inline the PyDict_GetItem() calls.
2345 WARNING: this is an extreme speed hack.
2346 Do not try this at home. */
2347 long hash = ((PyStringObject *)w)->ob_shash;
2348 if (hash != -1) {
2349 PyDictObject *d;
2350 PyDictEntry *e;
2351 d = (PyDictObject *)(f->f_globals);
2352 e = d->ma_lookup(d, w, hash);
2353 if (e == NULL) {
2354 x = NULL;
2355 break;
2356 }
2357 x = e->me_value;
2358 if (x != NULL) {
2359 Py_INCREF(x);
2360 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002361 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002362 }
2363 d = (PyDictObject *)(f->f_builtins);
2364 e = d->ma_lookup(d, w, hash);
2365 if (e == NULL) {
2366 x = NULL;
2367 break;
2368 }
2369 x = e->me_value;
2370 if (x != NULL) {
2371 Py_INCREF(x);
2372 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002373 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002374 }
2375 goto load_global_error;
2376 }
2377 }
2378 /* This is the un-inlined version of the code above */
2379 x = PyDict_GetItem(f->f_globals, w);
2380 if (x == NULL) {
2381 x = PyDict_GetItem(f->f_builtins, w);
2382 if (x == NULL) {
2383 load_global_error:
2384 format_exc_check_arg(
2385 PyExc_NameError,
2386 GLOBAL_NAME_ERROR_MSG, w);
2387 break;
2388 }
2389 }
2390 Py_INCREF(x);
2391 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002392 DISPATCH();
2393 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002394
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002395 TARGET(DELETE_FAST)
2396 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002397 x = GETLOCAL(oparg);
2398 if (x != NULL) {
2399 SETLOCAL(oparg, NULL);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002400 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002401 }
2402 format_exc_check_arg(
2403 PyExc_UnboundLocalError,
2404 UNBOUNDLOCAL_ERROR_MSG,
2405 PyTuple_GetItem(co->co_varnames, oparg)
2406 );
2407 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002408 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002409
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002410 TARGET(LOAD_CLOSURE)
2411 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002412 x = freevars[oparg];
2413 Py_INCREF(x);
2414 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002415 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002416 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002417 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002418
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002419 TARGET(LOAD_DEREF)
2420 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002421 x = freevars[oparg];
2422 w = PyCell_Get(x);
2423 if (w != NULL) {
2424 PUSH(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002425 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002426 }
2427 err = -1;
2428 /* Don't stomp existing exception */
2429 if (PyErr_Occurred())
2430 break;
2431 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2432 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krah7ff78252010-06-23 18:12:09 +00002433 oparg);
2434 format_exc_check_arg(
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002435 PyExc_UnboundLocalError,
2436 UNBOUNDLOCAL_ERROR_MSG,
2437 v);
2438 } else {
2439 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2440 PyTuple_GET_SIZE(co->co_cellvars));
2441 format_exc_check_arg(PyExc_NameError,
2442 UNBOUNDFREE_ERROR_MSG, v);
2443 }
2444 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002445 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002446
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002447 TARGET(STORE_DEREF)
2448 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002449 w = POP();
2450 x = freevars[oparg];
2451 PyCell_Set(x, w);
2452 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002453 DISPATCH();
2454 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002455
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002456 TARGET(BUILD_TUPLE)
2457 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002458 x = PyTuple_New(oparg);
2459 if (x != NULL) {
2460 for (; --oparg >= 0;) {
2461 w = POP();
2462 PyTuple_SET_ITEM(x, oparg, w);
2463 }
2464 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002465 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002466 }
2467 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002468 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002469
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002470 TARGET(BUILD_LIST)
2471 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002472 x = PyList_New(oparg);
2473 if (x != NULL) {
2474 for (; --oparg >= 0;) {
2475 w = POP();
2476 PyList_SET_ITEM(x, oparg, w);
2477 }
2478 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002479 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002480 }
2481 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002482 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002483
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002484 TARGET(BUILD_SET)
2485 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002486 x = PySet_New(NULL);
2487 if (x != NULL) {
2488 for (; --oparg >= 0;) {
2489 w = POP();
2490 if (err == 0)
2491 err = PySet_Add(x, w);
2492 Py_DECREF(w);
2493 }
2494 if (err != 0) {
2495 Py_DECREF(x);
2496 break;
2497 }
2498 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002499 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002500 }
2501 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002502 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002503
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002504 TARGET(BUILD_MAP)
2505 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002506 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2507 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002508 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002509 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002511
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002512 TARGET_NOARG(STORE_MAP)
2513 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002514 w = TOP(); /* key */
2515 u = SECOND(); /* value */
2516 v = THIRD(); /* dict */
2517 STACKADJ(-2);
2518 assert (PyDict_CheckExact(v));
2519 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2520 Py_DECREF(u);
2521 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002522 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002523 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002524 }
Raymond Hettingereffde122007-12-18 18:26:18 +00002525
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002526 TARGET(MAP_ADD)
2527 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002528 w = TOP(); /* key */
2529 u = SECOND(); /* value */
2530 STACKADJ(-2);
2531 v = stack_pointer[-oparg]; /* dict */
2532 assert (PyDict_CheckExact(v));
2533 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2534 Py_DECREF(u);
2535 Py_DECREF(w);
2536 if (err == 0) {
2537 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002538 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002539 }
2540 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002541 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002542
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002543 TARGET(LOAD_ATTR)
2544 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002545 w = GETITEM(names, oparg);
2546 v = TOP();
2547 x = PyObject_GetAttr(v, w);
2548 Py_DECREF(v);
2549 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002550 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002551 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002552 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002553
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002554 TARGET(COMPARE_OP)
2555 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002556 w = POP();
2557 v = TOP();
2558 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2559 /* INLINE: cmp(int, int) */
2560 register long a, b;
2561 register int res;
2562 a = PyInt_AS_LONG(v);
2563 b = PyInt_AS_LONG(w);
2564 switch (oparg) {
2565 case PyCmp_LT: res = a < b; break;
2566 case PyCmp_LE: res = a <= b; break;
2567 case PyCmp_EQ: res = a == b; break;
2568 case PyCmp_NE: res = a != b; break;
2569 case PyCmp_GT: res = a > b; break;
2570 case PyCmp_GE: res = a >= b; break;
2571 case PyCmp_IS: res = v == w; break;
2572 case PyCmp_IS_NOT: res = v != w; break;
2573 default: goto slow_compare;
2574 }
2575 x = res ? Py_True : Py_False;
2576 Py_INCREF(x);
2577 }
2578 else {
2579 slow_compare:
2580 x = cmp_outcome(oparg, v, w);
2581 }
2582 Py_DECREF(v);
2583 Py_DECREF(w);
2584 SET_TOP(x);
2585 if (x == NULL) break;
2586 PREDICT(POP_JUMP_IF_FALSE);
2587 PREDICT(POP_JUMP_IF_TRUE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002588 DISPATCH();
2589 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002590
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002591 TARGET(IMPORT_NAME)
2592 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002593 w = GETITEM(names, oparg);
2594 x = PyDict_GetItemString(f->f_builtins, "__import__");
2595 if (x == NULL) {
2596 PyErr_SetString(PyExc_ImportError,
2597 "__import__ not found");
2598 break;
2599 }
2600 Py_INCREF(x);
2601 v = POP();
2602 u = TOP();
2603 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2604 w = PyTuple_Pack(5,
2605 w,
2606 f->f_globals,
2607 f->f_locals == NULL ?
2608 Py_None : f->f_locals,
2609 v,
2610 u);
2611 else
2612 w = PyTuple_Pack(4,
2613 w,
2614 f->f_globals,
2615 f->f_locals == NULL ?
2616 Py_None : f->f_locals,
2617 v);
2618 Py_DECREF(v);
2619 Py_DECREF(u);
2620 if (w == NULL) {
2621 u = POP();
2622 Py_DECREF(x);
2623 x = NULL;
2624 break;
2625 }
2626 READ_TIMESTAMP(intr0);
2627 v = x;
2628 x = PyEval_CallObject(v, w);
2629 Py_DECREF(v);
2630 READ_TIMESTAMP(intr1);
2631 Py_DECREF(w);
2632 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002633 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002634 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002635 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002636
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002637 TARGET_NOARG(IMPORT_STAR)
2638 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002639 v = POP();
2640 PyFrame_FastToLocals(f);
2641 if ((x = f->f_locals) == NULL) {
2642 PyErr_SetString(PyExc_SystemError,
2643 "no locals found during 'import *'");
2644 break;
2645 }
2646 READ_TIMESTAMP(intr0);
2647 err = import_all_from(x, v);
2648 READ_TIMESTAMP(intr1);
2649 PyFrame_LocalsToFast(f, 0);
2650 Py_DECREF(v);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002651 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002652 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002653 }
Guido van Rossum25831651993-05-19 14:50:45 +00002654
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002655 TARGET(IMPORT_FROM)
2656 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002657 w = GETITEM(names, oparg);
2658 v = TOP();
2659 READ_TIMESTAMP(intr0);
2660 x = import_from(v, w);
2661 READ_TIMESTAMP(intr1);
2662 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002663 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002664 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002665 }
Thomas Wouters52152252000-08-17 22:55:00 +00002666
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002667 TARGET(JUMP_FORWARD)
2668 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002669 JUMPBY(oparg);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002670 FAST_DISPATCH();
2671 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002672
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002673 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002674 TARGET(POP_JUMP_IF_FALSE)
2675 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002676 w = POP();
2677 if (w == Py_True) {
2678 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002679 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002680 }
2681 if (w == Py_False) {
2682 Py_DECREF(w);
2683 JUMPTO(oparg);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002684 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002685 }
2686 err = PyObject_IsTrue(w);
2687 Py_DECREF(w);
2688 if (err > 0)
2689 err = 0;
2690 else if (err == 0)
2691 JUMPTO(oparg);
2692 else
2693 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002694 DISPATCH();
2695 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002696
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002697 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002698 TARGET(POP_JUMP_IF_TRUE)
2699 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002700 w = POP();
2701 if (w == Py_False) {
2702 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002703 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002704 }
2705 if (w == Py_True) {
2706 Py_DECREF(w);
2707 JUMPTO(oparg);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002708 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002709 }
2710 err = PyObject_IsTrue(w);
2711 Py_DECREF(w);
2712 if (err > 0) {
2713 err = 0;
2714 JUMPTO(oparg);
2715 }
2716 else if (err == 0)
2717 ;
2718 else
2719 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002720 DISPATCH();
2721 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002722
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002723 TARGET(JUMP_IF_FALSE_OR_POP)
2724 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002725 w = TOP();
2726 if (w == Py_True) {
2727 STACKADJ(-1);
2728 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002729 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002730 }
2731 if (w == Py_False) {
2732 JUMPTO(oparg);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002733 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002734 }
2735 err = PyObject_IsTrue(w);
2736 if (err > 0) {
2737 STACKADJ(-1);
2738 Py_DECREF(w);
2739 err = 0;
2740 }
2741 else if (err == 0)
2742 JUMPTO(oparg);
2743 else
2744 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002745 DISPATCH();
2746 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002747
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002748 TARGET(JUMP_IF_TRUE_OR_POP)
2749 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002750 w = TOP();
2751 if (w == Py_False) {
2752 STACKADJ(-1);
2753 Py_DECREF(w);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002754 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002755 }
2756 if (w == Py_True) {
2757 JUMPTO(oparg);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002758 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002759 }
2760 err = PyObject_IsTrue(w);
2761 if (err > 0) {
2762 err = 0;
2763 JUMPTO(oparg);
2764 }
2765 else if (err == 0) {
2766 STACKADJ(-1);
2767 Py_DECREF(w);
2768 }
2769 else
2770 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002771 DISPATCH();
2772 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002773
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002774 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002775 TARGET(JUMP_ABSOLUTE)
2776 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002777 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002778#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002779 /* Enabling this path speeds-up all while and for-loops by bypassing
2780 the per-loop checks for signals. By default, this should be turned-off
2781 because it prevents detection of a control-break in tight loops like
2782 "while 1: pass". Compile with this option turned-on when you need
2783 the speed-up and do not need break checking inside tight loops (ones
2784 that contain only instructions ending with goto fast_next_opcode).
2785 */
2786 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002787#else
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002788 DISPATCH();
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002789#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002790 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002791
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002792 TARGET_NOARG(GET_ITER)
2793 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002794 /* before: [obj]; after [getiter(obj)] */
2795 v = TOP();
2796 x = PyObject_GetIter(v);
2797 Py_DECREF(v);
2798 if (x != NULL) {
2799 SET_TOP(x);
2800 PREDICT(FOR_ITER);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002801 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002802 }
2803 STACKADJ(-1);
2804 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002805 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002806
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002807 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002808 TARGET(FOR_ITER)
2809 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002810 /* before: [iter]; after: [iter, iter()] *or* [] */
2811 v = TOP();
2812 x = (*v->ob_type->tp_iternext)(v);
2813 if (x != NULL) {
2814 PUSH(x);
2815 PREDICT(STORE_FAST);
2816 PREDICT(UNPACK_SEQUENCE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002817 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002818 }
2819 if (PyErr_Occurred()) {
2820 if (!PyErr_ExceptionMatches(
2821 PyExc_StopIteration))
2822 break;
2823 PyErr_Clear();
2824 }
2825 /* iterator ended normally */
2826 x = v = POP();
2827 Py_DECREF(v);
2828 JUMPBY(oparg);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002829 DISPATCH();
2830 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002831
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002832 TARGET_NOARG(BREAK_LOOP)
2833 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002834 why = WHY_BREAK;
2835 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002836 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002837
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002838 TARGET(CONTINUE_LOOP)
2839 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002840 retval = PyInt_FromLong(oparg);
2841 if (!retval) {
2842 x = NULL;
2843 break;
2844 }
2845 why = WHY_CONTINUE;
2846 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002847 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002848
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002849 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2850 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
2851 TARGET(SETUP_FINALLY)
2852 _setup_finally:
2853 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002854 /* NOTE: If you add any new block-setup opcodes that
2855 are not try/except/finally handlers, you may need
2856 to update the PyGen_NeedsFinalizing() function.
2857 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002858
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002859 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2860 STACK_LEVEL());
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002861 DISPATCH();
2862 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002863
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002864
2865
2866 TARGET(SETUP_WITH)
2867 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002868 {
2869 static PyObject *exit, *enter;
2870 w = TOP();
2871 x = special_lookup(w, "__exit__", &exit);
2872 if (!x)
2873 break;
2874 SET_TOP(x);
2875 u = special_lookup(w, "__enter__", &enter);
2876 Py_DECREF(w);
2877 if (!u) {
2878 x = NULL;
2879 break;
2880 }
2881 x = PyObject_CallFunctionObjArgs(u, NULL);
2882 Py_DECREF(u);
2883 if (!x)
2884 break;
2885 /* Setup a finally block (SETUP_WITH as a block is
2886 equivalent to SETUP_FINALLY except it normalizes
2887 the exception) before pushing the result of
2888 __enter__ on the stack. */
2889 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2890 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002891
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002892 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002893 DISPATCH();
2894 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002895 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002896
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002897 TARGET_NOARG(WITH_CLEANUP)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002898 {
2899 /* At the top of the stack are 1-3 values indicating
2900 how/why we entered the finally clause:
2901 - TOP = None
2902 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2903 - TOP = WHY_*; no retval below it
2904 - (TOP, SECOND, THIRD) = exc_info()
2905 Below them is EXIT, the context.__exit__ bound method.
2906 In the last case, we must call
2907 EXIT(TOP, SECOND, THIRD)
2908 otherwise we must call
2909 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002910
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002911 In all cases, we remove EXIT from the stack, leaving
2912 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002913
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002914 In addition, if the stack represents an exception,
2915 *and* the function call returns a 'true' value, we
2916 "zap" this information, to prevent END_FINALLY from
2917 re-raising the exception. (But non-local gotos
2918 should still be resumed.)
2919 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002920
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002921 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002923 u = POP();
2924 if (u == Py_None) {
2925 exit_func = TOP();
2926 SET_TOP(u);
2927 v = w = Py_None;
2928 }
2929 else if (PyInt_Check(u)) {
2930 switch(PyInt_AS_LONG(u)) {
2931 case WHY_RETURN:
2932 case WHY_CONTINUE:
2933 /* Retval in TOP. */
2934 exit_func = SECOND();
2935 SET_SECOND(TOP());
2936 SET_TOP(u);
2937 break;
2938 default:
2939 exit_func = TOP();
2940 SET_TOP(u);
2941 break;
2942 }
2943 u = v = w = Py_None;
2944 }
2945 else {
2946 v = TOP();
2947 w = SECOND();
2948 exit_func = THIRD();
2949 SET_TOP(u);
2950 SET_SECOND(v);
2951 SET_THIRD(w);
2952 }
2953 /* XXX Not the fastest way to call it... */
2954 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2955 NULL);
2956 Py_DECREF(exit_func);
2957 if (x == NULL)
2958 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002959
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002960 if (u != Py_None)
2961 err = PyObject_IsTrue(x);
2962 else
2963 err = 0;
2964 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002965
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002966 if (err < 0)
2967 break; /* Go to error exit */
2968 else if (err > 0) {
2969 err = 0;
2970 /* There was an exception and a true return */
2971 STACKADJ(-2);
2972 Py_INCREF(Py_None);
2973 SET_TOP(Py_None);
2974 Py_DECREF(u);
2975 Py_DECREF(v);
2976 Py_DECREF(w);
2977 } else {
2978 /* The stack was rearranged to remove EXIT
2979 above. Let END_FINALLY do its thing */
2980 }
2981 PREDICT(END_FINALLY);
2982 break;
2983 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002984
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002985 TARGET(CALL_FUNCTION)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002986 {
2987 PyObject **sp;
2988 PCALL(PCALL_ALL);
2989 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002990#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002991 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002992#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002993 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002994#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002995 stack_pointer = sp;
2996 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002997 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002998 break;
2999 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003000
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003001 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
3002 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
3003 TARGET(CALL_FUNCTION_VAR_KW)
3004 _call_function_var_kw:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003005 {
3006 int na = oparg & 0xff;
3007 int nk = (oparg>>8) & 0xff;
3008 int flags = (opcode - CALL_FUNCTION) & 3;
3009 int n = na + 2 * nk;
3010 PyObject **pfunc, *func, **sp;
3011 PCALL(PCALL_ALL);
3012 if (flags & CALL_FLAG_VAR)
3013 n++;
3014 if (flags & CALL_FLAG_KW)
3015 n++;
3016 pfunc = stack_pointer - n - 1;
3017 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003018
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003019 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00003020 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003021 PyObject *self = PyMethod_GET_SELF(func);
3022 Py_INCREF(self);
3023 func = PyMethod_GET_FUNCTION(func);
3024 Py_INCREF(func);
3025 Py_DECREF(*pfunc);
3026 *pfunc = self;
3027 na++;
3028 } else
3029 Py_INCREF(func);
3030 sp = stack_pointer;
3031 READ_TIMESTAMP(intr0);
3032 x = ext_do_call(func, &sp, flags, na, nk);
3033 READ_TIMESTAMP(intr1);
3034 stack_pointer = sp;
3035 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003036
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003037 while (stack_pointer > pfunc) {
3038 w = POP();
3039 Py_DECREF(w);
3040 }
3041 PUSH(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003042 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003043 break;
3044 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003045
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003046
3047 TARGET(MAKE_FUNCTION)
3048 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003049 v = POP(); /* code object */
3050 x = PyFunction_New(v, f->f_globals);
3051 Py_DECREF(v);
3052 /* XXX Maybe this should be a separate opcode? */
3053 if (x != NULL && oparg > 0) {
3054 v = PyTuple_New(oparg);
3055 if (v == NULL) {
3056 Py_DECREF(x);
3057 x = NULL;
3058 break;
3059 }
3060 while (--oparg >= 0) {
3061 w = POP();
3062 PyTuple_SET_ITEM(v, oparg, w);
3063 }
3064 err = PyFunction_SetDefaults(x, v);
3065 Py_DECREF(v);
3066 }
3067 PUSH(x);
3068 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003069 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003070
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003071 TARGET(MAKE_CLOSURE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003072 {
3073 v = POP(); /* code object */
3074 x = PyFunction_New(v, f->f_globals);
3075 Py_DECREF(v);
3076 if (x != NULL) {
3077 v = POP();
3078 if (PyFunction_SetClosure(x, v) != 0) {
3079 /* Can't happen unless bytecode is corrupt. */
3080 why = WHY_EXCEPTION;
3081 }
3082 Py_DECREF(v);
3083 }
3084 if (x != NULL && oparg > 0) {
3085 v = PyTuple_New(oparg);
3086 if (v == NULL) {
3087 Py_DECREF(x);
3088 x = NULL;
3089 break;
3090 }
3091 while (--oparg >= 0) {
3092 w = POP();
3093 PyTuple_SET_ITEM(v, oparg, w);
3094 }
3095 if (PyFunction_SetDefaults(x, v) != 0) {
3096 /* Can't happen unless
3097 PyFunction_SetDefaults changes. */
3098 why = WHY_EXCEPTION;
3099 }
3100 Py_DECREF(v);
3101 }
3102 PUSH(x);
3103 break;
3104 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003105
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003106 TARGET(BUILD_SLICE)
3107 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003108 if (oparg == 3)
3109 w = POP();
3110 else
3111 w = NULL;
3112 v = POP();
3113 u = TOP();
3114 x = PySlice_New(u, v, w);
3115 Py_DECREF(u);
3116 Py_DECREF(v);
3117 Py_XDECREF(w);
3118 SET_TOP(x);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003119 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003120 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003121 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003122
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003123 TARGET(EXTENDED_ARG)
3124 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003125 opcode = NEXTOP();
3126 oparg = oparg<<16 | NEXTARG();
3127 goto dispatch_opcode;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003128 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003129
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003130#if USE_COMPUTED_GOTOS
3131 _unknown_opcode:
3132#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003133 default:
3134 fprintf(stderr,
3135 "XXX lineno: %d, opcode: %d\n",
3136 PyFrame_GetLineNumber(f),
3137 opcode);
3138 PyErr_SetString(PyExc_SystemError, "unknown opcode");
3139 why = WHY_EXCEPTION;
3140 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003141
3142#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003143 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003144#endif
3145
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003146 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003147
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003148 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00003149
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003150 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003151
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003152 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003153
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003154 if (why == WHY_NOT) {
3155 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003156#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003157 /* This check is expensive! */
3158 if (PyErr_Occurred())
3159 fprintf(stderr,
3160 "XXX undetected error\n");
3161 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003162#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003163 READ_TIMESTAMP(loop1);
3164 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003165#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003166 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003167#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003168 }
3169 why = WHY_EXCEPTION;
3170 x = Py_None;
3171 err = 0;
3172 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003173
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003174 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00003175
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003176 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
3177 if (!PyErr_Occurred()) {
3178 PyErr_SetString(PyExc_SystemError,
3179 "error return without exception set");
3180 why = WHY_EXCEPTION;
3181 }
3182 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00003183#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003184 else {
3185 /* This check is expensive! */
3186 if (PyErr_Occurred()) {
3187 char buf[128];
3188 sprintf(buf, "Stack unwind with exception "
3189 "set and why=%d", why);
3190 Py_FatalError(buf);
3191 }
3192 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003193#endif
3194
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003195 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00003196
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003197 if (why == WHY_EXCEPTION) {
3198 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00003199
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003200 if (tstate->c_tracefunc != NULL)
3201 call_exc_trace(tstate->c_tracefunc,
3202 tstate->c_traceobj, f);
3203 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003204
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003205 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00003206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003207 if (why == WHY_RERAISE)
3208 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00003209
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003210 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003211
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003212fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003213 while (why != WHY_NOT && f->f_iblock > 0) {
3214 /* Peek at the current block. */
3215 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003216
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003217 assert(why != WHY_YIELD);
3218 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3219 why = WHY_NOT;
3220 JUMPTO(PyInt_AS_LONG(retval));
3221 Py_DECREF(retval);
3222 break;
3223 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003224
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003225 /* Now we have to pop the block. */
3226 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00003227
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003228 while (STACK_LEVEL() > b->b_level) {
3229 v = POP();
3230 Py_XDECREF(v);
3231 }
3232 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3233 why = WHY_NOT;
3234 JUMPTO(b->b_handler);
3235 break;
3236 }
3237 if (b->b_type == SETUP_FINALLY ||
3238 (b->b_type == SETUP_EXCEPT &&
3239 why == WHY_EXCEPTION) ||
3240 b->b_type == SETUP_WITH) {
3241 if (why == WHY_EXCEPTION) {
3242 PyObject *exc, *val, *tb;
3243 PyErr_Fetch(&exc, &val, &tb);
3244 if (val == NULL) {
3245 val = Py_None;
3246 Py_INCREF(val);
3247 }
3248 /* Make the raw exception data
3249 available to the handler,
3250 so a program can emulate the
3251 Python main loop. Don't do
3252 this for 'finally'. */
3253 if (b->b_type == SETUP_EXCEPT ||
3254 b->b_type == SETUP_WITH) {
3255 PyErr_NormalizeException(
3256 &exc, &val, &tb);
3257 set_exc_info(tstate,
3258 exc, val, tb);
3259 }
3260 if (tb == NULL) {
3261 Py_INCREF(Py_None);
3262 PUSH(Py_None);
3263 } else
3264 PUSH(tb);
3265 PUSH(val);
3266 PUSH(exc);
3267 }
3268 else {
3269 if (why & (WHY_RETURN | WHY_CONTINUE))
3270 PUSH(retval);
3271 v = PyInt_FromLong((long)why);
3272 PUSH(v);
3273 }
3274 why = WHY_NOT;
3275 JUMPTO(b->b_handler);
3276 break;
3277 }
3278 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003279
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003280 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003281
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003282 if (why != WHY_NOT)
3283 break;
3284 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003285
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003286 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003287
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003288 assert(why != WHY_YIELD);
3289 /* Pop remaining stack entries. */
3290 while (!EMPTY()) {
3291 v = POP();
3292 Py_XDECREF(v);
3293 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003294
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003295 if (why != WHY_RETURN)
3296 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003297
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003298fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003299 if (tstate->use_tracing) {
3300 if (tstate->c_tracefunc) {
3301 if (why == WHY_RETURN || why == WHY_YIELD) {
3302 if (call_trace(tstate->c_tracefunc,
3303 tstate->c_traceobj, f,
3304 PyTrace_RETURN, retval)) {
3305 Py_XDECREF(retval);
3306 retval = NULL;
3307 why = WHY_EXCEPTION;
3308 }
3309 }
3310 else if (why == WHY_EXCEPTION) {
3311 call_trace_protected(tstate->c_tracefunc,
3312 tstate->c_traceobj, f,
3313 PyTrace_RETURN, NULL);
3314 }
3315 }
3316 if (tstate->c_profilefunc) {
3317 if (why == WHY_EXCEPTION)
3318 call_trace_protected(tstate->c_profilefunc,
3319 tstate->c_profileobj, f,
3320 PyTrace_RETURN, NULL);
3321 else if (call_trace(tstate->c_profilefunc,
3322 tstate->c_profileobj, f,
3323 PyTrace_RETURN, retval)) {
3324 Py_XDECREF(retval);
3325 retval = NULL;
3326 why = WHY_EXCEPTION;
3327 }
3328 }
3329 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003330
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003331 if (tstate->frame->f_exc_type != NULL)
3332 reset_exc_info(tstate);
3333 else {
3334 assert(tstate->frame->f_exc_value == NULL);
3335 assert(tstate->frame->f_exc_traceback == NULL);
3336 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003337
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003338 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003339exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003340 Py_LeaveRecursiveCall();
3341 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003342
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003343 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003344}
3345
Guido van Rossumc2e20742006-02-27 22:32:47 +00003346/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003347 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003348 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003349
Tim Peters6d6c1a32001-08-02 04:15:00 +00003350PyObject *
3351PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003352 PyObject **args, int argcount, PyObject **kws, int kwcount,
3353 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003354{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003355 register PyFrameObject *f;
3356 register PyObject *retval = NULL;
3357 register PyObject **fastlocals, **freevars;
3358 PyThreadState *tstate = PyThreadState_GET();
3359 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003360
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003361 if (globals == NULL) {
3362 PyErr_SetString(PyExc_SystemError,
3363 "PyEval_EvalCodeEx: NULL globals");
3364 return NULL;
3365 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003366
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003367 assert(tstate != NULL);
3368 assert(globals != NULL);
3369 f = PyFrame_New(tstate, co, globals, locals);
3370 if (f == NULL)
3371 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003372
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003373 fastlocals = f->f_localsplus;
3374 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003375
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003376 if (co->co_argcount > 0 ||
3377 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3378 int i;
3379 int n = argcount;
3380 PyObject *kwdict = NULL;
3381 if (co->co_flags & CO_VARKEYWORDS) {
3382 kwdict = PyDict_New();
3383 if (kwdict == NULL)
3384 goto fail;
3385 i = co->co_argcount;
3386 if (co->co_flags & CO_VARARGS)
3387 i++;
3388 SETLOCAL(i, kwdict);
3389 }
3390 if (argcount > co->co_argcount) {
3391 if (!(co->co_flags & CO_VARARGS)) {
3392 PyErr_Format(PyExc_TypeError,
3393 "%.200s() takes %s %d "
3394 "argument%s (%d given)",
3395 PyString_AsString(co->co_name),
3396 defcount ? "at most" : "exactly",
3397 co->co_argcount,
3398 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003399 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003400 goto fail;
3401 }
3402 n = co->co_argcount;
3403 }
3404 for (i = 0; i < n; i++) {
3405 x = args[i];
3406 Py_INCREF(x);
3407 SETLOCAL(i, x);
3408 }
3409 if (co->co_flags & CO_VARARGS) {
3410 u = PyTuple_New(argcount - n);
3411 if (u == NULL)
3412 goto fail;
3413 SETLOCAL(co->co_argcount, u);
3414 for (i = n; i < argcount; i++) {
3415 x = args[i];
3416 Py_INCREF(x);
3417 PyTuple_SET_ITEM(u, i-n, x);
3418 }
3419 }
3420 for (i = 0; i < kwcount; i++) {
3421 PyObject **co_varnames;
3422 PyObject *keyword = kws[2*i];
3423 PyObject *value = kws[2*i + 1];
3424 int j;
3425 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003426#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003427 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003428#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003429 )) {
3430 PyErr_Format(PyExc_TypeError,
3431 "%.200s() keywords must be strings",
3432 PyString_AsString(co->co_name));
3433 goto fail;
3434 }
3435 /* Speed hack: do raw pointer compares. As names are
3436 normally interned this should almost always hit. */
3437 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3438 for (j = 0; j < co->co_argcount; j++) {
3439 PyObject *nm = co_varnames[j];
3440 if (nm == keyword)
3441 goto kw_found;
3442 }
3443 /* Slow fallback, just in case */
3444 for (j = 0; j < co->co_argcount; j++) {
3445 PyObject *nm = co_varnames[j];
3446 int cmp = PyObject_RichCompareBool(
3447 keyword, nm, Py_EQ);
3448 if (cmp > 0)
3449 goto kw_found;
3450 else if (cmp < 0)
3451 goto fail;
3452 }
3453 if (kwdict == NULL) {
3454 PyObject *kwd_str = kwd_as_string(keyword);
3455 if (kwd_str) {
3456 PyErr_Format(PyExc_TypeError,
3457 "%.200s() got an unexpected "
3458 "keyword argument '%.400s'",
3459 PyString_AsString(co->co_name),
3460 PyString_AsString(kwd_str));
3461 Py_DECREF(kwd_str);
3462 }
3463 goto fail;
3464 }
3465 PyDict_SetItem(kwdict, keyword, value);
3466 continue;
3467 kw_found:
3468 if (GETLOCAL(j) != NULL) {
3469 PyObject *kwd_str = kwd_as_string(keyword);
3470 if (kwd_str) {
3471 PyErr_Format(PyExc_TypeError,
3472 "%.200s() got multiple "
3473 "values for keyword "
3474 "argument '%.400s'",
3475 PyString_AsString(co->co_name),
3476 PyString_AsString(kwd_str));
3477 Py_DECREF(kwd_str);
3478 }
3479 goto fail;
3480 }
3481 Py_INCREF(value);
3482 SETLOCAL(j, value);
3483 }
3484 if (argcount < co->co_argcount) {
3485 int m = co->co_argcount - defcount;
3486 for (i = argcount; i < m; i++) {
3487 if (GETLOCAL(i) == NULL) {
3488 int j, given = 0;
3489 for (j = 0; j < co->co_argcount; j++)
3490 if (GETLOCAL(j))
3491 given++;
3492 PyErr_Format(PyExc_TypeError,
3493 "%.200s() takes %s %d "
3494 "argument%s (%d given)",
3495 PyString_AsString(co->co_name),
3496 ((co->co_flags & CO_VARARGS) ||
3497 defcount) ? "at least"
3498 : "exactly",
3499 m, m == 1 ? "" : "s", given);
3500 goto fail;
3501 }
3502 }
3503 if (n > m)
3504 i = n - m;
3505 else
3506 i = 0;
3507 for (; i < defcount; i++) {
3508 if (GETLOCAL(m+i) == NULL) {
3509 PyObject *def = defs[i];
3510 Py_INCREF(def);
3511 SETLOCAL(m+i, def);
3512 }
3513 }
3514 }
3515 }
3516 else if (argcount > 0 || kwcount > 0) {
3517 PyErr_Format(PyExc_TypeError,
3518 "%.200s() takes no arguments (%d given)",
3519 PyString_AsString(co->co_name),
3520 argcount + kwcount);
3521 goto fail;
3522 }
3523 /* Allocate and initialize storage for cell vars, and copy free
3524 vars into frame. This isn't too efficient right now. */
3525 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3526 int i, j, nargs, found;
3527 char *cellname, *argname;
3528 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003529
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003530 nargs = co->co_argcount;
3531 if (co->co_flags & CO_VARARGS)
3532 nargs++;
3533 if (co->co_flags & CO_VARKEYWORDS)
3534 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003535
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003536 /* Initialize each cell var, taking into account
3537 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003538
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003539 Should arrange for the compiler to put cellvars
3540 that are arguments at the beginning of the cellvars
3541 list so that we can march over it more efficiently?
3542 */
3543 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3544 cellname = PyString_AS_STRING(
3545 PyTuple_GET_ITEM(co->co_cellvars, i));
3546 found = 0;
3547 for (j = 0; j < nargs; j++) {
3548 argname = PyString_AS_STRING(
3549 PyTuple_GET_ITEM(co->co_varnames, j));
3550 if (strcmp(cellname, argname) == 0) {
3551 c = PyCell_New(GETLOCAL(j));
3552 if (c == NULL)
3553 goto fail;
3554 GETLOCAL(co->co_nlocals + i) = c;
3555 found = 1;
3556 break;
3557 }
3558 }
3559 if (found == 0) {
3560 c = PyCell_New(NULL);
3561 if (c == NULL)
3562 goto fail;
3563 SETLOCAL(co->co_nlocals + i, c);
3564 }
3565 }
3566 }
3567 if (PyTuple_GET_SIZE(co->co_freevars)) {
3568 int i;
3569 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3570 PyObject *o = PyTuple_GET_ITEM(closure, i);
3571 Py_INCREF(o);
3572 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3573 }
3574 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003575
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003576 if (co->co_flags & CO_GENERATOR) {
3577 /* Don't need to keep the reference to f_back, it will be set
3578 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003579 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003580
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003581 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003582
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003583 /* Create a new generator that owns the ready to run frame
3584 * and return that as the value. */
3585 return PyGen_New(f);
3586 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003587
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003588 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003589
Thomas Woutersae406c62007-09-19 17:27:43 +00003590fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003591
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003592 /* decref'ing the frame can cause __del__ methods to get invoked,
3593 which can call back into Python. While we're done with the
3594 current Python frame (f), the associated C stack is still in use,
3595 so recursion_depth must be boosted for the duration.
3596 */
3597 assert(tstate != NULL);
3598 ++tstate->recursion_depth;
3599 Py_DECREF(f);
3600 --tstate->recursion_depth;
3601 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003602}
3603
3604
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003605static PyObject *
3606special_lookup(PyObject *o, char *meth, PyObject **cache)
3607{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003608 PyObject *res;
3609 if (PyInstance_Check(o)) {
3610 if (!*cache)
3611 return PyObject_GetAttrString(o, meth);
3612 else
3613 return PyObject_GetAttr(o, *cache);
3614 }
3615 res = _PyObject_LookupSpecial(o, meth, cache);
3616 if (res == NULL && !PyErr_Occurred()) {
3617 PyErr_SetObject(PyExc_AttributeError, *cache);
3618 return NULL;
3619 }
3620 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003621}
3622
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003623
Benjamin Petersone18ef192009-01-20 14:21:16 +00003624static PyObject *
3625kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003626#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003627 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003628#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003629 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003630#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003631 Py_INCREF(kwd);
3632 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003633#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003634 }
3635 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003636#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003637}
3638
3639
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003640/* Implementation notes for set_exc_info() and reset_exc_info():
3641
3642- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3643 'exc_traceback'. These always travel together.
3644
3645- tstate->curexc_ZZZ is the "hot" exception that is set by
3646 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3647
3648- Once an exception is caught by an except clause, it is transferred
3649 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3650 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003651 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003652
3653- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3654
3655 Long ago, when none of this existed, there were just a few globals:
3656 one set corresponding to the "hot" exception, and one set
3657 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3658 globals; they were simply stored as sys.exc_ZZZ. For backwards
3659 compatibility, they still are!) The problem was that in code like
3660 this:
3661
3662 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003663 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003664 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003665 "do something else first"
3666 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003667
3668 if "do something else first" invoked something that raised and caught
3669 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3670 cause of subtle bugs. I fixed this by changing the semantics as
3671 follows:
3672
3673 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3674 *in that frame*.
3675
3676 - But initially, and as long as no exception is caught in a given
3677 frame, sys.exc_ZZZ will hold the last exception caught in the
3678 previous frame (or the frame before that, etc.).
3679
3680 The first bullet fixed the bug in the above example. The second
3681 bullet was for backwards compatibility: it was (and is) common to
3682 have a function that is called when an exception is caught, and to
3683 have that function access the caught exception via sys.exc_ZZZ.
3684 (Example: traceback.print_exc()).
3685
3686 At the same time I fixed the problem that sys.exc_ZZZ weren't
3687 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3688 but that's really a separate improvement.
3689
3690 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3691 variables to what they were before the current frame was called. The
3692 set_exc_info() function saves them on the frame so that
3693 reset_exc_info() can restore them. The invariant is that
3694 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3695 exception (where "catching" an exception applies only to successful
3696 except clauses); and if the current frame ever caught an exception,
3697 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3698 at the start of the current frame.
3699
3700*/
3701
Fredrik Lundh7a830892006-05-27 10:39:48 +00003702static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003703set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003704 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003705{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003706 PyFrameObject *frame = tstate->frame;
3707 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003708
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003709 assert(type != NULL);
3710 assert(frame != NULL);
3711 if (frame->f_exc_type == NULL) {
3712 assert(frame->f_exc_value == NULL);
3713 assert(frame->f_exc_traceback == NULL);
3714 /* This frame didn't catch an exception before. */
3715 /* Save previous exception of this thread in this frame. */
3716 if (tstate->exc_type == NULL) {
3717 /* XXX Why is this set to Py_None? */
3718 Py_INCREF(Py_None);
3719 tstate->exc_type = Py_None;
3720 }
3721 Py_INCREF(tstate->exc_type);
3722 Py_XINCREF(tstate->exc_value);
3723 Py_XINCREF(tstate->exc_traceback);
3724 frame->f_exc_type = tstate->exc_type;
3725 frame->f_exc_value = tstate->exc_value;
3726 frame->f_exc_traceback = tstate->exc_traceback;
3727 }
3728 /* Set new exception for this thread. */
3729 tmp_type = tstate->exc_type;
3730 tmp_value = tstate->exc_value;
3731 tmp_tb = tstate->exc_traceback;
3732 Py_INCREF(type);
3733 Py_XINCREF(value);
3734 Py_XINCREF(tb);
3735 tstate->exc_type = type;
3736 tstate->exc_value = value;
3737 tstate->exc_traceback = tb;
3738 Py_XDECREF(tmp_type);
3739 Py_XDECREF(tmp_value);
3740 Py_XDECREF(tmp_tb);
3741 /* For b/w compatibility */
3742 PySys_SetObject("exc_type", type);
3743 PySys_SetObject("exc_value", value);
3744 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003745}
3746
Fredrik Lundh7a830892006-05-27 10:39:48 +00003747static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003748reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003749{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003750 PyFrameObject *frame;
3751 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003752
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003753 /* It's a precondition that the thread state's frame caught an
3754 * exception -- verify in a debug build.
3755 */
3756 assert(tstate != NULL);
3757 frame = tstate->frame;
3758 assert(frame != NULL);
3759 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003760
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003761 /* Copy the frame's exception info back to the thread state. */
3762 tmp_type = tstate->exc_type;
3763 tmp_value = tstate->exc_value;
3764 tmp_tb = tstate->exc_traceback;
3765 Py_INCREF(frame->f_exc_type);
3766 Py_XINCREF(frame->f_exc_value);
3767 Py_XINCREF(frame->f_exc_traceback);
3768 tstate->exc_type = frame->f_exc_type;
3769 tstate->exc_value = frame->f_exc_value;
3770 tstate->exc_traceback = frame->f_exc_traceback;
3771 Py_XDECREF(tmp_type);
3772 Py_XDECREF(tmp_value);
3773 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003774
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003775 /* For b/w compatibility */
3776 PySys_SetObject("exc_type", frame->f_exc_type);
3777 PySys_SetObject("exc_value", frame->f_exc_value);
3778 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003779
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003780 /* Clear the frame's exception info. */
3781 tmp_type = frame->f_exc_type;
3782 tmp_value = frame->f_exc_value;
3783 tmp_tb = frame->f_exc_traceback;
3784 frame->f_exc_type = NULL;
3785 frame->f_exc_value = NULL;
3786 frame->f_exc_traceback = NULL;
3787 Py_DECREF(tmp_type);
3788 Py_XDECREF(tmp_value);
3789 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003790}
3791
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003792/* Logic for the raise statement (too complicated for inlining).
3793 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003794static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003795do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003796{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003797 if (type == NULL) {
3798 /* Reraise */
3799 PyThreadState *tstate = PyThreadState_GET();
3800 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3801 value = tstate->exc_value;
3802 tb = tstate->exc_traceback;
3803 Py_XINCREF(type);
3804 Py_XINCREF(value);
3805 Py_XINCREF(tb);
3806 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003807
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003808 /* We support the following forms of raise:
3809 raise <class>, <classinstance>
3810 raise <class>, <argument tuple>
3811 raise <class>, None
3812 raise <class>, <argument>
3813 raise <classinstance>, None
3814 raise <string>, <object>
3815 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003816
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003817 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003818
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003819 In addition, raise <tuple>, <anything> is the same as
3820 raising the tuple's first item (and it better have one!);
3821 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003822
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003823 Finally, an optional third argument can be supplied, which
3824 gives the traceback to be substituted (useful when
3825 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003826
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003827 /* First, check the traceback argument, replacing None with
3828 NULL. */
3829 if (tb == Py_None) {
3830 Py_DECREF(tb);
3831 tb = NULL;
3832 }
3833 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3834 PyErr_SetString(PyExc_TypeError,
3835 "raise: arg 3 must be a traceback or None");
3836 goto raise_error;
3837 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003839 /* Next, replace a missing value with None */
3840 if (value == NULL) {
3841 value = Py_None;
3842 Py_INCREF(value);
3843 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003844
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003845 /* Next, repeatedly, replace a tuple exception with its first item */
3846 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3847 PyObject *tmp = type;
3848 type = PyTuple_GET_ITEM(type, 0);
3849 Py_INCREF(type);
3850 Py_DECREF(tmp);
3851 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003852
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003853 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003854 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003855 if (!PyExceptionInstance_Check(value)) {
3856 PyErr_Format(PyExc_TypeError,
3857 "calling %s() should have returned an instance of "
3858 "BaseException, not '%s'",
3859 ((PyTypeObject *)type)->tp_name,
3860 Py_TYPE(value)->tp_name);
3861 goto raise_error;
3862 }
3863 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003864 else if (PyExceptionInstance_Check(type)) {
3865 /* Raising an instance. The value should be a dummy. */
3866 if (value != Py_None) {
3867 PyErr_SetString(PyExc_TypeError,
3868 "instance exception may not have a separate value");
3869 goto raise_error;
3870 }
3871 else {
3872 /* Normalize to raise <class>, <instance> */
3873 Py_DECREF(value);
3874 value = type;
3875 type = PyExceptionInstance_Class(type);
3876 Py_INCREF(type);
3877 }
3878 }
3879 else {
3880 /* Not something you can raise. You get an exception
3881 anyway, just not what you specified :-) */
3882 PyErr_Format(PyExc_TypeError,
3883 "exceptions must be old-style classes or "
3884 "derived from BaseException, not %s",
3885 type->ob_type->tp_name);
3886 goto raise_error;
3887 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003888
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003889 assert(PyExceptionClass_Check(type));
3890 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3891 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3892 "exceptions must derive from BaseException "
3893 "in 3.x", 1) < 0)
3894 goto raise_error;
3895 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003896
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003897 PyErr_Restore(type, value, tb);
3898 if (tb == NULL)
3899 return WHY_EXCEPTION;
3900 else
3901 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003902 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003903 Py_XDECREF(value);
3904 Py_XDECREF(type);
3905 Py_XDECREF(tb);
3906 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003907}
3908
Tim Petersd6d010b2001-06-21 02:49:55 +00003909/* Iterate v argcnt times and store the results on the stack (via decreasing
3910 sp). Return 1 for success, 0 if error. */
3911
Fredrik Lundh7a830892006-05-27 10:39:48 +00003912static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003913unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003914{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003915 int i = 0;
3916 PyObject *it; /* iter(v) */
3917 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003918
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003919 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003920
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003921 it = PyObject_GetIter(v);
3922 if (it == NULL)
3923 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003924
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003925 for (; i < argcnt; i++) {
3926 w = PyIter_Next(it);
3927 if (w == NULL) {
3928 /* Iterator done, via error or exhaustion. */
3929 if (!PyErr_Occurred()) {
3930 PyErr_Format(PyExc_ValueError,
3931 "need more than %d value%s to unpack",
3932 i, i == 1 ? "" : "s");
3933 }
3934 goto Error;
3935 }
3936 *--sp = w;
3937 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003939 /* We better have exhausted the iterator now. */
3940 w = PyIter_Next(it);
3941 if (w == NULL) {
3942 if (PyErr_Occurred())
3943 goto Error;
3944 Py_DECREF(it);
3945 return 1;
3946 }
3947 Py_DECREF(w);
3948 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3949 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003950Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003951 for (; i > 0; i--, sp++)
3952 Py_DECREF(*sp);
3953 Py_XDECREF(it);
3954 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003955}
3956
3957
Guido van Rossum96a42c81992-01-12 02:29:51 +00003958#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003959static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003960prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003961{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003962 printf("%s ", str);
3963 if (PyObject_Print(v, stdout, 0) != 0)
3964 PyErr_Clear(); /* Don't know what else to do */
3965 printf("\n");
3966 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003967}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003968#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003969
Fredrik Lundh7a830892006-05-27 10:39:48 +00003970static void
Fred Drake5755ce62001-06-27 19:19:46 +00003971call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003972{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003973 PyObject *type, *value, *traceback, *arg;
3974 int err;
3975 PyErr_Fetch(&type, &value, &traceback);
3976 if (value == NULL) {
3977 value = Py_None;
3978 Py_INCREF(value);
3979 }
3980 arg = PyTuple_Pack(3, type, value, traceback);
3981 if (arg == NULL) {
3982 PyErr_Restore(type, value, traceback);
3983 return;
3984 }
3985 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3986 Py_DECREF(arg);
3987 if (err == 0)
3988 PyErr_Restore(type, value, traceback);
3989 else {
3990 Py_XDECREF(type);
3991 Py_XDECREF(value);
3992 Py_XDECREF(traceback);
3993 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003994}
3995
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003996static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003997call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003998 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003999{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004000 PyObject *type, *value, *traceback;
4001 int err;
4002 PyErr_Fetch(&type, &value, &traceback);
4003 err = call_trace(func, obj, frame, what, arg);
4004 if (err == 0)
4005 {
4006 PyErr_Restore(type, value, traceback);
4007 return 0;
4008 }
4009 else {
4010 Py_XDECREF(type);
4011 Py_XDECREF(value);
4012 Py_XDECREF(traceback);
4013 return -1;
4014 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004015}
4016
Fredrik Lundh7a830892006-05-27 10:39:48 +00004017static int
Fred Drake5755ce62001-06-27 19:19:46 +00004018call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004019 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004020{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004021 register PyThreadState *tstate = frame->f_tstate;
4022 int result;
4023 if (tstate->tracing)
4024 return 0;
4025 tstate->tracing++;
4026 tstate->use_tracing = 0;
4027 result = func(obj, frame, what, arg);
4028 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4029 || (tstate->c_profilefunc != NULL));
4030 tstate->tracing--;
4031 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004032}
4033
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004034PyObject *
4035_PyEval_CallTracing(PyObject *func, PyObject *args)
4036{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004037 PyFrameObject *frame = PyEval_GetFrame();
4038 PyThreadState *tstate = frame->f_tstate;
4039 int save_tracing = tstate->tracing;
4040 int save_use_tracing = tstate->use_tracing;
4041 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004042
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004043 tstate->tracing = 0;
4044 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4045 || (tstate->c_profilefunc != NULL));
4046 result = PyObject_Call(func, args, NULL);
4047 tstate->tracing = save_tracing;
4048 tstate->use_tracing = save_use_tracing;
4049 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004050}
4051
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00004052/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00004053static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004054maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004055 PyFrameObject *frame, int *instr_lb, int *instr_ub,
4056 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004057{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004058 int result = 0;
4059 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004060
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004061 /* If the last instruction executed isn't in the current
4062 instruction window, reset the window.
4063 */
4064 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4065 PyAddrPair bounds;
4066 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4067 &bounds);
4068 *instr_lb = bounds.ap_lower;
4069 *instr_ub = bounds.ap_upper;
4070 }
4071 /* If the last instruction falls at the start of a line or if
4072 it represents a jump backwards, update the frame's line
4073 number and call the trace function. */
4074 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4075 frame->f_lineno = line;
4076 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
4077 }
4078 *instr_prev = frame->f_lasti;
4079 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004080}
4081
Fred Drake5755ce62001-06-27 19:19:46 +00004082void
4083PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004084{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004085 PyThreadState *tstate = PyThreadState_GET();
4086 PyObject *temp = tstate->c_profileobj;
4087 Py_XINCREF(arg);
4088 tstate->c_profilefunc = NULL;
4089 tstate->c_profileobj = NULL;
4090 /* Must make sure that tracing is not ignored if 'temp' is freed */
4091 tstate->use_tracing = tstate->c_tracefunc != NULL;
4092 Py_XDECREF(temp);
4093 tstate->c_profilefunc = func;
4094 tstate->c_profileobj = arg;
4095 /* Flag that tracing or profiling is turned on */
4096 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004097}
4098
4099void
4100PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4101{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004102 PyThreadState *tstate = PyThreadState_GET();
4103 PyObject *temp = tstate->c_traceobj;
4104 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4105 Py_XINCREF(arg);
4106 tstate->c_tracefunc = NULL;
4107 tstate->c_traceobj = NULL;
4108 /* Must make sure that profiling is not ignored if 'temp' is freed */
4109 tstate->use_tracing = tstate->c_profilefunc != NULL;
4110 Py_XDECREF(temp);
4111 tstate->c_tracefunc = func;
4112 tstate->c_traceobj = arg;
4113 /* Flag that tracing or profiling is turned on */
4114 tstate->use_tracing = ((func != NULL)
4115 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004116}
4117
Guido van Rossumb209a111997-04-29 18:18:01 +00004118PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004119PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004120{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004121 PyFrameObject *current_frame = PyEval_GetFrame();
4122 if (current_frame == NULL)
4123 return PyThreadState_GET()->interp->builtins;
4124 else
4125 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004126}
4127
Guido van Rossumb209a111997-04-29 18:18:01 +00004128PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004129PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004130{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004131 PyFrameObject *current_frame = PyEval_GetFrame();
4132 if (current_frame == NULL)
4133 return NULL;
4134 PyFrame_FastToLocals(current_frame);
4135 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004136}
4137
Guido van Rossumb209a111997-04-29 18:18:01 +00004138PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004139PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004140{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004141 PyFrameObject *current_frame = PyEval_GetFrame();
4142 if (current_frame == NULL)
4143 return NULL;
4144 else
4145 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004146}
4147
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004148PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004149PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004150{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004151 PyThreadState *tstate = PyThreadState_GET();
4152 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004153}
4154
Guido van Rossum6135a871995-01-09 17:53:26 +00004155int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004156PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004157{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004158 PyFrameObject *current_frame = PyEval_GetFrame();
4159 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00004160}
4161
Guido van Rossumbe270261997-05-22 22:26:18 +00004162int
Tim Peters5ba58662001-07-16 02:29:45 +00004163PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004164{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004165 PyFrameObject *current_frame = PyEval_GetFrame();
4166 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004167
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004168 if (current_frame != NULL) {
4169 const int codeflags = current_frame->f_code->co_flags;
4170 const int compilerflags = codeflags & PyCF_MASK;
4171 if (compilerflags) {
4172 result = 1;
4173 cf->cf_flags |= compilerflags;
4174 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004175#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004176 if (codeflags & CO_GENERATOR_ALLOWED) {
4177 result = 1;
4178 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4179 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004180#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004181 }
4182 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004183}
4184
4185int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004186Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004187{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004188 PyObject *f = PySys_GetObject("stdout");
4189 if (f == NULL)
4190 return 0;
4191 if (!PyFile_SoftSpace(f, 0))
4192 return 0;
4193 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004194}
4195
Guido van Rossum3f5da241990-12-20 15:06:42 +00004196
Guido van Rossum681d79a1995-07-18 14:51:37 +00004197/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00004198 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004199
Guido van Rossumb209a111997-04-29 18:18:01 +00004200PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004201PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004202{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004203 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004204
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004205 if (arg == NULL) {
4206 arg = PyTuple_New(0);
4207 if (arg == NULL)
4208 return NULL;
4209 }
4210 else if (!PyTuple_Check(arg)) {
4211 PyErr_SetString(PyExc_TypeError,
4212 "argument list must be a tuple");
4213 return NULL;
4214 }
4215 else
4216 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004217
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004218 if (kw != NULL && !PyDict_Check(kw)) {
4219 PyErr_SetString(PyExc_TypeError,
4220 "keyword list must be a dictionary");
4221 Py_DECREF(arg);
4222 return NULL;
4223 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004224
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004225 result = PyObject_Call(func, arg, kw);
4226 Py_DECREF(arg);
4227 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004228}
4229
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004230const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004231PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004232{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004233 if (PyMethod_Check(func))
4234 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4235 else if (PyFunction_Check(func))
4236 return PyString_AsString(((PyFunctionObject*)func)->func_name);
4237 else if (PyCFunction_Check(func))
4238 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4239 else if (PyClass_Check(func))
4240 return PyString_AsString(((PyClassObject*)func)->cl_name);
4241 else if (PyInstance_Check(func)) {
4242 return PyString_AsString(
4243 ((PyInstanceObject*)func)->in_class->cl_name);
4244 } else {
4245 return func->ob_type->tp_name;
4246 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004247}
4248
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004249const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004250PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004251{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004252 if (PyMethod_Check(func))
4253 return "()";
4254 else if (PyFunction_Check(func))
4255 return "()";
4256 else if (PyCFunction_Check(func))
4257 return "()";
4258 else if (PyClass_Check(func))
4259 return " constructor";
4260 else if (PyInstance_Check(func)) {
4261 return " instance";
4262 } else {
4263 return " object";
4264 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004265}
4266
Fredrik Lundh7a830892006-05-27 10:39:48 +00004267static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004268err_args(PyObject *func, int flags, int nargs)
4269{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004270 if (flags & METH_NOARGS)
4271 PyErr_Format(PyExc_TypeError,
4272 "%.200s() takes no arguments (%d given)",
4273 ((PyCFunctionObject *)func)->m_ml->ml_name,
4274 nargs);
4275 else
4276 PyErr_Format(PyExc_TypeError,
4277 "%.200s() takes exactly one argument (%d given)",
4278 ((PyCFunctionObject *)func)->m_ml->ml_name,
4279 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004280}
4281
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004282#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004283if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004284 if (call_trace(tstate->c_profilefunc, \
4285 tstate->c_profileobj, \
4286 tstate->frame, PyTrace_C_CALL, \
4287 func)) { \
4288 x = NULL; \
4289 } \
4290 else { \
4291 x = call; \
4292 if (tstate->c_profilefunc != NULL) { \
4293 if (x == NULL) { \
4294 call_trace_protected(tstate->c_profilefunc, \
4295 tstate->c_profileobj, \
4296 tstate->frame, PyTrace_C_EXCEPTION, \
4297 func); \
4298 /* XXX should pass (type, value, tb) */ \
4299 } else { \
4300 if (call_trace(tstate->c_profilefunc, \
4301 tstate->c_profileobj, \
4302 tstate->frame, PyTrace_C_RETURN, \
4303 func)) { \
4304 Py_DECREF(x); \
4305 x = NULL; \
4306 } \
4307 } \
4308 } \
4309 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004310} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004311 x = call; \
4312 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004313
Fredrik Lundh7a830892006-05-27 10:39:48 +00004314static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004315call_function(PyObject ***pp_stack, int oparg
4316#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004317 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004318#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004319 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004320{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004321 int na = oparg & 0xff;
4322 int nk = (oparg>>8) & 0xff;
4323 int n = na + 2 * nk;
4324 PyObject **pfunc = (*pp_stack) - n - 1;
4325 PyObject *func = *pfunc;
4326 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004327
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004328 /* Always dispatch PyCFunction first, because these are
4329 presumed to be the most frequent callable object.
4330 */
4331 if (PyCFunction_Check(func) && nk == 0) {
4332 int flags = PyCFunction_GET_FLAGS(func);
4333 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004334
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004335 PCALL(PCALL_CFUNCTION);
4336 if (flags & (METH_NOARGS | METH_O)) {
4337 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4338 PyObject *self = PyCFunction_GET_SELF(func);
4339 if (flags & METH_NOARGS && na == 0) {
4340 C_TRACE(x, (*meth)(self,NULL));
4341 }
4342 else if (flags & METH_O && na == 1) {
4343 PyObject *arg = EXT_POP(*pp_stack);
4344 C_TRACE(x, (*meth)(self,arg));
4345 Py_DECREF(arg);
4346 }
4347 else {
4348 err_args(func, flags, na);
4349 x = NULL;
4350 }
4351 }
4352 else {
4353 PyObject *callargs;
4354 callargs = load_args(pp_stack, na);
4355 READ_TIMESTAMP(*pintr0);
4356 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4357 READ_TIMESTAMP(*pintr1);
4358 Py_XDECREF(callargs);
4359 }
4360 } else {
4361 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4362 /* optimize access to bound methods */
4363 PyObject *self = PyMethod_GET_SELF(func);
4364 PCALL(PCALL_METHOD);
4365 PCALL(PCALL_BOUND_METHOD);
4366 Py_INCREF(self);
4367 func = PyMethod_GET_FUNCTION(func);
4368 Py_INCREF(func);
4369 Py_DECREF(*pfunc);
4370 *pfunc = self;
4371 na++;
4372 n++;
4373 } else
4374 Py_INCREF(func);
4375 READ_TIMESTAMP(*pintr0);
4376 if (PyFunction_Check(func))
4377 x = fast_function(func, pp_stack, n, na, nk);
4378 else
4379 x = do_call(func, pp_stack, na, nk);
4380 READ_TIMESTAMP(*pintr1);
4381 Py_DECREF(func);
4382 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004383
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004384 /* Clear the stack of the function object. Also removes
4385 the arguments in case they weren't consumed already
4386 (fast_function() and err_args() leave them on the stack).
4387 */
4388 while ((*pp_stack) > pfunc) {
4389 w = EXT_POP(*pp_stack);
4390 Py_DECREF(w);
4391 PCALL(PCALL_POP);
4392 }
4393 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004394}
4395
Jeremy Hylton192690e2002-08-16 18:36:11 +00004396/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004397 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004398 For the simplest case -- a function that takes only positional
4399 arguments and is called with only positional arguments -- it
4400 inlines the most primitive frame setup code from
4401 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4402 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004403*/
4404
Fredrik Lundh7a830892006-05-27 10:39:48 +00004405static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004406fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004407{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004408 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4409 PyObject *globals = PyFunction_GET_GLOBALS(func);
4410 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4411 PyObject **d = NULL;
4412 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004413
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004414 PCALL(PCALL_FUNCTION);
4415 PCALL(PCALL_FAST_FUNCTION);
4416 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4417 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4418 PyFrameObject *f;
4419 PyObject *retval = NULL;
4420 PyThreadState *tstate = PyThreadState_GET();
4421 PyObject **fastlocals, **stack;
4422 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004423
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004424 PCALL(PCALL_FASTER_FUNCTION);
4425 assert(globals != NULL);
4426 /* XXX Perhaps we should create a specialized
4427 PyFrame_New() that doesn't take locals, but does
4428 take builtins without sanity checking them.
4429 */
4430 assert(tstate != NULL);
4431 f = PyFrame_New(tstate, co, globals, NULL);
4432 if (f == NULL)
4433 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004434
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004435 fastlocals = f->f_localsplus;
4436 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004437
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004438 for (i = 0; i < n; i++) {
4439 Py_INCREF(*stack);
4440 fastlocals[i] = *stack++;
4441 }
4442 retval = PyEval_EvalFrameEx(f,0);
4443 ++tstate->recursion_depth;
4444 Py_DECREF(f);
4445 --tstate->recursion_depth;
4446 return retval;
4447 }
4448 if (argdefs != NULL) {
4449 d = &PyTuple_GET_ITEM(argdefs, 0);
4450 nd = Py_SIZE(argdefs);
4451 }
4452 return PyEval_EvalCodeEx(co, globals,
4453 (PyObject *)NULL, (*pp_stack)-n, na,
4454 (*pp_stack)-2*nk, nk, d, nd,
4455 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004456}
4457
Fredrik Lundh7a830892006-05-27 10:39:48 +00004458static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004459update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4460 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004461{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004462 PyObject *kwdict = NULL;
4463 if (orig_kwdict == NULL)
4464 kwdict = PyDict_New();
4465 else {
4466 kwdict = PyDict_Copy(orig_kwdict);
4467 Py_DECREF(orig_kwdict);
4468 }
4469 if (kwdict == NULL)
4470 return NULL;
4471 while (--nk >= 0) {
4472 int err;
4473 PyObject *value = EXT_POP(*pp_stack);
4474 PyObject *key = EXT_POP(*pp_stack);
4475 if (PyDict_GetItem(kwdict, key) != NULL) {
4476 PyErr_Format(PyExc_TypeError,
4477 "%.200s%s got multiple values "
4478 "for keyword argument '%.200s'",
4479 PyEval_GetFuncName(func),
4480 PyEval_GetFuncDesc(func),
4481 PyString_AsString(key));
4482 Py_DECREF(key);
4483 Py_DECREF(value);
4484 Py_DECREF(kwdict);
4485 return NULL;
4486 }
4487 err = PyDict_SetItem(kwdict, key, value);
4488 Py_DECREF(key);
4489 Py_DECREF(value);
4490 if (err) {
4491 Py_DECREF(kwdict);
4492 return NULL;
4493 }
4494 }
4495 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004496}
4497
Fredrik Lundh7a830892006-05-27 10:39:48 +00004498static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004499update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004500 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004501{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004502 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004503
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004504 callargs = PyTuple_New(nstack + nstar);
4505 if (callargs == NULL) {
4506 return NULL;
4507 }
4508 if (nstar) {
4509 int i;
4510 for (i = 0; i < nstar; i++) {
4511 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4512 Py_INCREF(a);
4513 PyTuple_SET_ITEM(callargs, nstack + i, a);
4514 }
4515 }
4516 while (--nstack >= 0) {
4517 w = EXT_POP(*pp_stack);
4518 PyTuple_SET_ITEM(callargs, nstack, w);
4519 }
4520 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004521}
4522
Fredrik Lundh7a830892006-05-27 10:39:48 +00004523static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004524load_args(PyObject ***pp_stack, int na)
4525{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004526 PyObject *args = PyTuple_New(na);
4527 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004528
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004529 if (args == NULL)
4530 return NULL;
4531 while (--na >= 0) {
4532 w = EXT_POP(*pp_stack);
4533 PyTuple_SET_ITEM(args, na, w);
4534 }
4535 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004536}
4537
Fredrik Lundh7a830892006-05-27 10:39:48 +00004538static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004539do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4540{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004541 PyObject *callargs = NULL;
4542 PyObject *kwdict = NULL;
4543 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004544
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004545 if (nk > 0) {
4546 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4547 if (kwdict == NULL)
4548 goto call_fail;
4549 }
4550 callargs = load_args(pp_stack, na);
4551 if (callargs == NULL)
4552 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004553#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004554 /* At this point, we have to look at the type of func to
4555 update the call stats properly. Do it here so as to avoid
4556 exposing the call stats machinery outside ceval.c
4557 */
4558 if (PyFunction_Check(func))
4559 PCALL(PCALL_FUNCTION);
4560 else if (PyMethod_Check(func))
4561 PCALL(PCALL_METHOD);
4562 else if (PyType_Check(func))
4563 PCALL(PCALL_TYPE);
4564 else if (PyCFunction_Check(func))
4565 PCALL(PCALL_CFUNCTION);
4566 else
4567 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004568#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004569 if (PyCFunction_Check(func)) {
4570 PyThreadState *tstate = PyThreadState_GET();
4571 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4572 }
4573 else
4574 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004575 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004576 Py_XDECREF(callargs);
4577 Py_XDECREF(kwdict);
4578 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004579}
4580
Fredrik Lundh7a830892006-05-27 10:39:48 +00004581static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004582ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4583{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004584 int nstar = 0;
4585 PyObject *callargs = NULL;
4586 PyObject *stararg = NULL;
4587 PyObject *kwdict = NULL;
4588 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004589
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004590 if (flags & CALL_FLAG_KW) {
4591 kwdict = EXT_POP(*pp_stack);
4592 if (!PyDict_Check(kwdict)) {
4593 PyObject *d;
4594 d = PyDict_New();
4595 if (d == NULL)
4596 goto ext_call_fail;
4597 if (PyDict_Update(d, kwdict) != 0) {
4598 Py_DECREF(d);
4599 /* PyDict_Update raises attribute
4600 * error (percolated from an attempt
4601 * to get 'keys' attribute) instead of
4602 * a type error if its second argument
4603 * is not a mapping.
4604 */
4605 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4606 PyErr_Format(PyExc_TypeError,
4607 "%.200s%.200s argument after ** "
4608 "must be a mapping, not %.200s",
4609 PyEval_GetFuncName(func),
4610 PyEval_GetFuncDesc(func),
4611 kwdict->ob_type->tp_name);
4612 }
4613 goto ext_call_fail;
4614 }
4615 Py_DECREF(kwdict);
4616 kwdict = d;
4617 }
4618 }
4619 if (flags & CALL_FLAG_VAR) {
4620 stararg = EXT_POP(*pp_stack);
4621 if (!PyTuple_Check(stararg)) {
4622 PyObject *t = NULL;
4623 t = PySequence_Tuple(stararg);
4624 if (t == NULL) {
4625 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4626 PyErr_Format(PyExc_TypeError,
4627 "%.200s%.200s argument after * "
4628 "must be a sequence, not %200s",
4629 PyEval_GetFuncName(func),
4630 PyEval_GetFuncDesc(func),
4631 stararg->ob_type->tp_name);
4632 }
4633 goto ext_call_fail;
4634 }
4635 Py_DECREF(stararg);
4636 stararg = t;
4637 }
4638 nstar = PyTuple_GET_SIZE(stararg);
4639 }
4640 if (nk > 0) {
4641 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4642 if (kwdict == NULL)
4643 goto ext_call_fail;
4644 }
4645 callargs = update_star_args(na, nstar, stararg, pp_stack);
4646 if (callargs == NULL)
4647 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004648#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004649 /* At this point, we have to look at the type of func to
4650 update the call stats properly. Do it here so as to avoid
4651 exposing the call stats machinery outside ceval.c
4652 */
4653 if (PyFunction_Check(func))
4654 PCALL(PCALL_FUNCTION);
4655 else if (PyMethod_Check(func))
4656 PCALL(PCALL_METHOD);
4657 else if (PyType_Check(func))
4658 PCALL(PCALL_TYPE);
4659 else if (PyCFunction_Check(func))
4660 PCALL(PCALL_CFUNCTION);
4661 else
4662 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004663#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004664 if (PyCFunction_Check(func)) {
4665 PyThreadState *tstate = PyThreadState_GET();
4666 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4667 }
4668 else
4669 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004670ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004671 Py_XDECREF(callargs);
4672 Py_XDECREF(kwdict);
4673 Py_XDECREF(stararg);
4674 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004675}
4676
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004677/* Extract a slice index from a PyInt or PyLong or an object with the
4678 nb_index slot defined, and store in *pi.
4679 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4680 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 +00004681 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004682*/
Tim Petersb5196382001-12-16 19:44:20 +00004683/* Note: If v is NULL, return success without storing into *pi. This
4684 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4685 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004686*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004687int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004688_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004689{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004690 if (v != NULL) {
4691 Py_ssize_t x;
4692 if (PyInt_Check(v)) {
4693 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4694 however, it looks like it should be AsSsize_t.
4695 There should be a comment here explaining why.
4696 */
4697 x = PyInt_AS_LONG(v);
4698 }
4699 else if (PyIndex_Check(v)) {
4700 x = PyNumber_AsSsize_t(v, NULL);
4701 if (x == -1 && PyErr_Occurred())
4702 return 0;
4703 }
4704 else {
4705 PyErr_SetString(PyExc_TypeError,
4706 "slice indices must be integers or "
4707 "None or have an __index__ method");
4708 return 0;
4709 }
4710 *pi = x;
4711 }
4712 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004713}
4714
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004715#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004716#define ISINDEX(x) ((x) == NULL || \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004717 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004718
Fredrik Lundh7a830892006-05-27 10:39:48 +00004719static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004720apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004721{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004722 PyTypeObject *tp = u->ob_type;
4723 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004725 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4726 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4727 if (!_PyEval_SliceIndex(v, &ilow))
4728 return NULL;
4729 if (!_PyEval_SliceIndex(w, &ihigh))
4730 return NULL;
4731 return PySequence_GetSlice(u, ilow, ihigh);
4732 }
4733 else {
4734 PyObject *slice = PySlice_New(v, w, NULL);
4735 if (slice != NULL) {
4736 PyObject *res = PyObject_GetItem(u, slice);
4737 Py_DECREF(slice);
4738 return res;
4739 }
4740 else
4741 return NULL;
4742 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004743}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004744
Fredrik Lundh7a830892006-05-27 10:39:48 +00004745static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004746assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004747 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004748{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004749 PyTypeObject *tp = u->ob_type;
4750 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004751
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004752 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4753 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4754 if (!_PyEval_SliceIndex(v, &ilow))
4755 return -1;
4756 if (!_PyEval_SliceIndex(w, &ihigh))
4757 return -1;
4758 if (x == NULL)
4759 return PySequence_DelSlice(u, ilow, ihigh);
4760 else
4761 return PySequence_SetSlice(u, ilow, ihigh, x);
4762 }
4763 else {
4764 PyObject *slice = PySlice_New(v, w, NULL);
4765 if (slice != NULL) {
4766 int res;
4767 if (x != NULL)
4768 res = PyObject_SetItem(u, slice, x);
4769 else
4770 res = PyObject_DelItem(u, slice);
4771 Py_DECREF(slice);
4772 return res;
4773 }
4774 else
4775 return -1;
4776 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004777}
4778
Guido van Rossum04edb522008-03-18 02:49:46 +00004779#define Py3kExceptionClass_Check(x) \
4780 (PyType_Check((x)) && \
4781 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4782
4783#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004784 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004785
Fredrik Lundh7a830892006-05-27 10:39:48 +00004786static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004787cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004788{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004789 int res = 0;
4790 switch (op) {
4791 case PyCmp_IS:
4792 res = (v == w);
4793 break;
4794 case PyCmp_IS_NOT:
4795 res = (v != w);
4796 break;
4797 case PyCmp_IN:
4798 res = PySequence_Contains(w, v);
4799 if (res < 0)
4800 return NULL;
4801 break;
4802 case PyCmp_NOT_IN:
4803 res = PySequence_Contains(w, v);
4804 if (res < 0)
4805 return NULL;
4806 res = !res;
4807 break;
4808 case PyCmp_EXC_MATCH:
4809 if (PyTuple_Check(w)) {
4810 Py_ssize_t i, length;
4811 length = PyTuple_Size(w);
4812 for (i = 0; i < length; i += 1) {
4813 PyObject *exc = PyTuple_GET_ITEM(w, i);
4814 if (PyString_Check(exc)) {
4815 int ret_val;
4816 ret_val = PyErr_WarnEx(
4817 PyExc_DeprecationWarning,
4818 "catching of string "
4819 "exceptions is deprecated", 1);
4820 if (ret_val < 0)
4821 return NULL;
4822 }
4823 else if (Py_Py3kWarningFlag &&
4824 !PyTuple_Check(exc) &&
4825 !Py3kExceptionClass_Check(exc))
4826 {
4827 int ret_val;
4828 ret_val = PyErr_WarnEx(
4829 PyExc_DeprecationWarning,
4830 CANNOT_CATCH_MSG, 1);
4831 if (ret_val < 0)
4832 return NULL;
4833 }
4834 }
4835 }
4836 else {
4837 if (PyString_Check(w)) {
4838 int ret_val;
4839 ret_val = PyErr_WarnEx(
4840 PyExc_DeprecationWarning,
4841 "catching of string "
4842 "exceptions is deprecated", 1);
4843 if (ret_val < 0)
4844 return NULL;
4845 }
4846 else if (Py_Py3kWarningFlag &&
4847 !PyTuple_Check(w) &&
4848 !Py3kExceptionClass_Check(w))
4849 {
4850 int ret_val;
4851 ret_val = PyErr_WarnEx(
4852 PyExc_DeprecationWarning,
4853 CANNOT_CATCH_MSG, 1);
4854 if (ret_val < 0)
4855 return NULL;
4856 }
4857 }
4858 res = PyErr_GivenExceptionMatches(v, w);
4859 break;
4860 default:
4861 return PyObject_RichCompare(v, w, op);
4862 }
4863 v = res ? Py_True : Py_False;
4864 Py_INCREF(v);
4865 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004866}
4867
Fredrik Lundh7a830892006-05-27 10:39:48 +00004868static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004869import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004870{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004871 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004872
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004873 x = PyObject_GetAttr(v, name);
4874 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4875 PyErr_Format(PyExc_ImportError,
4876 "cannot import name %.230s",
4877 PyString_AsString(name));
4878 }
4879 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004880}
Guido van Rossumac7be682001-01-17 15:42:30 +00004881
Fredrik Lundh7a830892006-05-27 10:39:48 +00004882static int
Thomas Wouters52152252000-08-17 22:55:00 +00004883import_all_from(PyObject *locals, PyObject *v)
4884{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004885 PyObject *all = PyObject_GetAttrString(v, "__all__");
4886 PyObject *dict, *name, *value;
4887 int skip_leading_underscores = 0;
4888 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004889
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004890 if (all == NULL) {
4891 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4892 return -1; /* Unexpected error */
4893 PyErr_Clear();
4894 dict = PyObject_GetAttrString(v, "__dict__");
4895 if (dict == NULL) {
4896 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4897 return -1;
4898 PyErr_SetString(PyExc_ImportError,
4899 "from-import-* object has no __dict__ and no __all__");
4900 return -1;
4901 }
4902 all = PyMapping_Keys(dict);
4903 Py_DECREF(dict);
4904 if (all == NULL)
4905 return -1;
4906 skip_leading_underscores = 1;
4907 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004908
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004909 for (pos = 0, err = 0; ; pos++) {
4910 name = PySequence_GetItem(all, pos);
4911 if (name == NULL) {
4912 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4913 err = -1;
4914 else
4915 PyErr_Clear();
4916 break;
4917 }
4918 if (skip_leading_underscores &&
4919 PyString_Check(name) &&
4920 PyString_AS_STRING(name)[0] == '_')
4921 {
4922 Py_DECREF(name);
4923 continue;
4924 }
4925 value = PyObject_GetAttr(v, name);
4926 if (value == NULL)
4927 err = -1;
4928 else if (PyDict_CheckExact(locals))
4929 err = PyDict_SetItem(locals, name, value);
4930 else
4931 err = PyObject_SetItem(locals, name, value);
4932 Py_DECREF(name);
4933 Py_XDECREF(value);
4934 if (err != 0)
4935 break;
4936 }
4937 Py_DECREF(all);
4938 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004939}
4940
Fredrik Lundh7a830892006-05-27 10:39:48 +00004941static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004942build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004943{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004944 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004945
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004946 if (PyDict_Check(methods))
4947 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4948 if (metaclass != NULL)
4949 Py_INCREF(metaclass);
4950 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4951 base = PyTuple_GET_ITEM(bases, 0);
4952 metaclass = PyObject_GetAttrString(base, "__class__");
4953 if (metaclass == NULL) {
4954 PyErr_Clear();
4955 metaclass = (PyObject *)base->ob_type;
4956 Py_INCREF(metaclass);
4957 }
4958 }
4959 else {
4960 PyObject *g = PyEval_GetGlobals();
4961 if (g != NULL && PyDict_Check(g))
4962 metaclass = PyDict_GetItemString(g, "__metaclass__");
4963 if (metaclass == NULL)
4964 metaclass = (PyObject *) &PyClass_Type;
4965 Py_INCREF(metaclass);
4966 }
4967 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4968 NULL);
4969 Py_DECREF(metaclass);
4970 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4971 /* A type error here likely means that the user passed
4972 in a base that was not a class (such the random module
4973 instead of the random.random type). Help them out with
4974 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00004975
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004976 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00004977
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004978 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4979 if (PyString_Check(pvalue)) {
4980 PyObject *newmsg;
4981 newmsg = PyString_FromFormat(
4982 "Error when calling the metaclass bases\n"
4983 " %s",
4984 PyString_AS_STRING(pvalue));
4985 if (newmsg != NULL) {
4986 Py_DECREF(pvalue);
4987 pvalue = newmsg;
4988 }
4989 }
4990 PyErr_Restore(ptype, pvalue, ptraceback);
4991 }
4992 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004993}
4994
Fredrik Lundh7a830892006-05-27 10:39:48 +00004995static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004996exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004997 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004998{
Benjamin Petersond2903bd2014-08-09 19:39:36 -07004999 int n;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005000 PyObject *v;
5001 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005002
Benjamin Petersond2903bd2014-08-09 19:39:36 -07005003 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
5004 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
5005 /* Backward compatibility hack */
5006 globals = PyTuple_GetItem(prog, 1);
5007 if (n == 3)
5008 locals = PyTuple_GetItem(prog, 2);
5009 prog = PyTuple_GetItem(prog, 0);
5010 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005011 if (globals == Py_None) {
5012 globals = PyEval_GetGlobals();
5013 if (locals == Py_None) {
5014 locals = PyEval_GetLocals();
5015 plain = 1;
5016 }
5017 if (!globals || !locals) {
5018 PyErr_SetString(PyExc_SystemError,
5019 "globals and locals cannot be NULL");
5020 return -1;
5021 }
5022 }
5023 else if (locals == Py_None)
5024 locals = globals;
5025 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005026#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005027 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005028#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005029 !PyCode_Check(prog) &&
5030 !PyFile_Check(prog)) {
5031 PyErr_SetString(PyExc_TypeError,
5032 "exec: arg 1 must be a string, file, or code object");
5033 return -1;
5034 }
5035 if (!PyDict_Check(globals)) {
5036 PyErr_SetString(PyExc_TypeError,
5037 "exec: arg 2 must be a dictionary or None");
5038 return -1;
5039 }
5040 if (!PyMapping_Check(locals)) {
5041 PyErr_SetString(PyExc_TypeError,
5042 "exec: arg 3 must be a mapping or None");
5043 return -1;
5044 }
5045 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
5046 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
5047 if (PyCode_Check(prog)) {
5048 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
5049 PyErr_SetString(PyExc_TypeError,
5050 "code object passed to exec may not contain free variables");
5051 return -1;
5052 }
5053 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
5054 }
5055 else if (PyFile_Check(prog)) {
5056 FILE *fp = PyFile_AsFile(prog);
5057 char *name = PyString_AsString(PyFile_Name(prog));
5058 PyCompilerFlags cf;
5059 if (name == NULL)
5060 return -1;
5061 cf.cf_flags = 0;
5062 if (PyEval_MergeCompilerFlags(&cf))
5063 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
5064 locals, &cf);
5065 else
5066 v = PyRun_File(fp, name, Py_file_input, globals,
5067 locals);
5068 }
5069 else {
5070 PyObject *tmp = NULL;
5071 char *str;
5072 PyCompilerFlags cf;
5073 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005074#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005075 if (PyUnicode_Check(prog)) {
5076 tmp = PyUnicode_AsUTF8String(prog);
5077 if (tmp == NULL)
5078 return -1;
5079 prog = tmp;
5080 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
5081 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005082#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005083 if (PyString_AsStringAndSize(prog, &str, NULL))
5084 return -1;
5085 if (PyEval_MergeCompilerFlags(&cf))
5086 v = PyRun_StringFlags(str, Py_file_input, globals,
5087 locals, &cf);
5088 else
5089 v = PyRun_String(str, Py_file_input, globals, locals);
5090 Py_XDECREF(tmp);
5091 }
5092 if (plain)
5093 PyFrame_LocalsToFast(f, 0);
5094 if (v == NULL)
5095 return -1;
5096 Py_DECREF(v);
5097 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005098}
Guido van Rossum24c13741995-02-14 09:42:43 +00005099
Fredrik Lundh7a830892006-05-27 10:39:48 +00005100static void
Paul Prescode68140d2000-08-30 20:25:01 +00005101format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
5102{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005103 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005104
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005105 if (!obj)
5106 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005107
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005108 obj_str = PyString_AsString(obj);
5109 if (!obj_str)
5110 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005111
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005112 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005113}
Guido van Rossum950361c1997-01-24 13:49:28 +00005114
Fredrik Lundh7a830892006-05-27 10:39:48 +00005115static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005116string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005117 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005118{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005119 /* This function implements 'variable += expr' when both arguments
5120 are strings. */
5121 Py_ssize_t v_len = PyString_GET_SIZE(v);
5122 Py_ssize_t w_len = PyString_GET_SIZE(w);
5123 Py_ssize_t new_len = v_len + w_len;
5124 if (new_len < 0) {
5125 PyErr_SetString(PyExc_OverflowError,
5126 "strings are too large to concat");
5127 return NULL;
5128 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00005129
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005130 if (v->ob_refcnt == 2) {
5131 /* In the common case, there are 2 references to the value
5132 * stored in 'variable' when the += is performed: one on the
5133 * value stack (in 'v') and one still stored in the
5134 * 'variable'. We try to delete the variable now to reduce
5135 * the refcnt to 1.
5136 */
5137 switch (*next_instr) {
5138 case STORE_FAST:
5139 {
5140 int oparg = PEEKARG();
5141 PyObject **fastlocals = f->f_localsplus;
5142 if (GETLOCAL(oparg) == v)
5143 SETLOCAL(oparg, NULL);
5144 break;
5145 }
5146 case STORE_DEREF:
5147 {
5148 PyObject **freevars = (f->f_localsplus +
5149 f->f_code->co_nlocals);
5150 PyObject *c = freevars[PEEKARG()];
5151 if (PyCell_GET(c) == v)
5152 PyCell_Set(c, NULL);
5153 break;
5154 }
5155 case STORE_NAME:
5156 {
5157 PyObject *names = f->f_code->co_names;
5158 PyObject *name = GETITEM(names, PEEKARG());
5159 PyObject *locals = f->f_locals;
5160 if (PyDict_CheckExact(locals) &&
5161 PyDict_GetItem(locals, name) == v) {
5162 if (PyDict_DelItem(locals, name) != 0) {
5163 PyErr_Clear();
5164 }
5165 }
5166 break;
5167 }
5168 }
5169 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005170
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005171 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
5172 /* Now we own the last reference to 'v', so we can resize it
5173 * in-place.
5174 */
5175 if (_PyString_Resize(&v, new_len) != 0) {
5176 /* XXX if _PyString_Resize() fails, 'v' has been
5177 * deallocated so it cannot be put back into
5178 * 'variable'. The MemoryError is raised when there
5179 * is no value in 'variable', which might (very
5180 * remotely) be a cause of incompatibilities.
5181 */
5182 return NULL;
5183 }
5184 /* copy 'w' into the newly allocated area of 'v' */
5185 memcpy(PyString_AS_STRING(v) + v_len,
5186 PyString_AS_STRING(w), w_len);
5187 return v;
5188 }
5189 else {
5190 /* When in-place resizing is not an option. */
5191 PyString_Concat(&v, w);
5192 return v;
5193 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005194}
5195
Guido van Rossum950361c1997-01-24 13:49:28 +00005196#ifdef DYNAMIC_EXECUTION_PROFILE
5197
Fredrik Lundh7a830892006-05-27 10:39:48 +00005198static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005199getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005200{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005201 int i;
5202 PyObject *l = PyList_New(256);
5203 if (l == NULL) return NULL;
5204 for (i = 0; i < 256; i++) {
5205 PyObject *x = PyInt_FromLong(a[i]);
5206 if (x == NULL) {
5207 Py_DECREF(l);
5208 return NULL;
5209 }
5210 PyList_SetItem(l, i, x);
5211 }
5212 for (i = 0; i < 256; i++)
5213 a[i] = 0;
5214 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005215}
5216
5217PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005218_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005219{
5220#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005221 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005222#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005223 int i;
5224 PyObject *l = PyList_New(257);
5225 if (l == NULL) return NULL;
5226 for (i = 0; i < 257; i++) {
5227 PyObject *x = getarray(dxpairs[i]);
5228 if (x == NULL) {
5229 Py_DECREF(l);
5230 return NULL;
5231 }
5232 PyList_SetItem(l, i, x);
5233 }
5234 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005235#endif
5236}
5237
5238#endif