blob: 6e5e27280e9cf1e50fca104511d50256b4e6e73c [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
Serhiy Storchaka1670af62015-06-21 16:26:28 +0300618_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000619{
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) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700712 TARGET_##op: \
713 opcode = op; \
714 oparg = NEXTARG(); \
715 case op: \
716 goto impl; \
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500717
718#define TARGET_WITH_IMPL_NOARG(op, impl) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700719 TARGET_##op: \
720 opcode = op; \
721 case op: \
722 goto impl; \
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500723
724#define TARGET_NOARG(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700725 TARGET_##op: \
726 opcode = op; \
727 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500728
729#define TARGET(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700730 TARGET_##op: \
731 opcode = op; \
732 oparg = NEXTARG(); \
733 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500734
735
736#define DISPATCH() \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700737 { \
738 int _tick = _Py_Ticker - 1; \
739 _Py_Ticker = _tick; \
740 if (_tick >= 0) { \
741 FAST_DISPATCH(); \
742 } \
743 continue; \
744 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500745
746#ifdef LLTRACE
747#define FAST_DISPATCH() \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700748 { \
749 if (!lltrace && !_Py_TracingPossible) { \
750 f->f_lasti = INSTR_OFFSET(); \
751 goto *opcode_targets[*next_instr++]; \
752 } \
753 goto fast_next_opcode; \
754 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500755#else
756#define FAST_DISPATCH() { \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700757 if (!_Py_TracingPossible) { \
758 f->f_lasti = INSTR_OFFSET(); \
759 goto *opcode_targets[*next_instr++]; \
760 } \
761 goto fast_next_opcode;\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500762}
763#endif
764
765#else
766#define TARGET(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700767 case op:
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500768#define TARGET_WITH_IMPL(op, impl) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700769 /* silence compiler warnings about `impl` unused */ \
770 if (0) goto impl; \
771 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500772
773#define TARGET_NOARG(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700774 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500775
776#define TARGET_WITH_IMPL_NOARG(op, impl) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700777 if (0) goto impl; \
778 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500779
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
Benjamin Petersoncc06dbf2015-06-01 18:24:31 -0500903#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
904#define PREDICT(op) if (0) goto PRED_##op
905#define PREDICTED(op) PRED_##op:
906#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000907#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000908#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500909#define PREDICTED(op) PRED_##op: next_instr++
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500910#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
911#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500912
Raymond Hettingerf606f872003-03-16 03:11:04 +0000913
Guido van Rossum374a9221991-04-04 10:40:29 +0000914/* Stack manipulation macros */
915
Martin v. Löwis18e16552006-02-15 17:27:45 +0000916/* The stack can grow at most MAXINT deep, as co_nlocals and
917 co_stacksize are ints. */
Stefan Krah7ff78252010-06-23 18:12:09 +0000918#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
919#define EMPTY() (STACK_LEVEL() == 0)
920#define TOP() (stack_pointer[-1])
921#define SECOND() (stack_pointer[-2])
922#define THIRD() (stack_pointer[-3])
923#define FOURTH() (stack_pointer[-4])
924#define PEEK(n) (stack_pointer[-(n)])
925#define SET_TOP(v) (stack_pointer[-1] = (v))
926#define SET_SECOND(v) (stack_pointer[-2] = (v))
927#define SET_THIRD(v) (stack_pointer[-3] = (v))
928#define SET_FOURTH(v) (stack_pointer[-4] = (v))
929#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
930#define BASIC_STACKADJ(n) (stack_pointer += n)
931#define BASIC_PUSH(v) (*stack_pointer++ = (v))
932#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000933
Guido van Rossum96a42c81992-01-12 02:29:51 +0000934#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000935#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000936 lltrace && prtrace(TOP(), "push")); \
937 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000938#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000939 BASIC_POP())
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000940#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000941 lltrace && prtrace(TOP(), "stackadj")); \
942 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000943#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krah7ff78252010-06-23 18:12:09 +0000944 prtrace((STACK_POINTER)[-1], "ext_pop")), \
945 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000946#else
Stefan Krah7ff78252010-06-23 18:12:09 +0000947#define PUSH(v) BASIC_PUSH(v)
948#define POP() BASIC_POP()
949#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000950#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000951#endif
952
Guido van Rossum681d79a1995-07-18 14:51:37 +0000953/* Local variable macros */
954
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000955#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000956
957/* The SETLOCAL() macro must not DECREF the local variable in-place and
958 then store the new value; it must copy the old value to a temporary
959 value, then store the new value, and then DECREF the temporary value.
960 This is because it is possible that during the DECREF the frame is
961 accessed by other code (e.g. a __del__ method or gc.collect()) and the
962 variable would be pointing to already-freed memory. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000963#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krah7ff78252010-06-23 18:12:09 +0000964 GETLOCAL(i) = value; \
965 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000966
Guido van Rossuma027efa1997-05-05 20:56:21 +0000967/* Start of code */
968
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000969 if (f == NULL)
970 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000971
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000972 /* push frame */
973 if (Py_EnterRecursiveCall(""))
974 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000975
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000976 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000977
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000978 if (tstate->use_tracing) {
979 if (tstate->c_tracefunc != NULL) {
980 /* tstate->c_tracefunc, if defined, is a
981 function that will be called on *every* entry
982 to a code block. Its return value, if not
983 None, is a function that will be called at
984 the start of each executed line of code.
985 (Actually, the function must return itself
986 in order to continue tracing.) The trace
987 functions are called with three arguments:
988 a pointer to the current frame, a string
989 indicating why the function is called, and
990 an argument which depends on the situation.
991 The global trace function is also called
992 whenever an exception is detected. */
993 if (call_trace_protected(tstate->c_tracefunc,
994 tstate->c_traceobj,
995 f, PyTrace_CALL, Py_None)) {
996 /* Trace function raised an error */
997 goto exit_eval_frame;
998 }
999 }
1000 if (tstate->c_profilefunc != NULL) {
1001 /* Similar for c_profilefunc, except it needn't
1002 return itself and isn't called for "line" events */
1003 if (call_trace_protected(tstate->c_profilefunc,
1004 tstate->c_profileobj,
1005 f, PyTrace_CALL, Py_None)) {
1006 /* Profile function raised an error */
1007 goto exit_eval_frame;
1008 }
1009 }
1010 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001012 co = f->f_code;
1013 names = co->co_names;
1014 consts = co->co_consts;
1015 fastlocals = f->f_localsplus;
1016 freevars = f->f_localsplus + co->co_nlocals;
1017 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
1018 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 f->f_lasti now refers to the index of the last instruction
1021 executed. You might think this was obvious from the name, but
1022 this wasn't always true before 2.3! PyFrame_New now sets
1023 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1024 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1025 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +00001026
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001027 When the PREDICT() macros are enabled, some opcode pairs follow in
1028 direct succession without updating f->f_lasti. A successful
1029 prediction effectively links the two codes together as if they
1030 were a single new opcode; accordingly,f->f_lasti will point to
1031 the first code in the pair (for instance, GET_ITER followed by
1032 FOR_ITER is effectively a single opcode and f->f_lasti will point
1033 at to the beginning of the combined pair.)
1034 */
1035 next_instr = first_instr + f->f_lasti + 1;
1036 stack_pointer = f->f_stacktop;
1037 assert(stack_pointer != NULL);
1038 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001039
Tim Peters5ca576e2001-06-18 22:08:13 +00001040#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001041 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001042#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001043#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001044 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001045#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001046
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001047 why = WHY_NOT;
1048 err = 0;
1049 x = Py_None; /* Not a reference, just anything non-NULL */
1050 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001052 if (throwflag) { /* support for generator.throw() */
1053 why = WHY_EXCEPTION;
1054 goto on_error;
1055 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00001056
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001057 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001058#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001059 if (inst1 == 0) {
1060 /* Almost surely, the opcode executed a break
1061 or a continue, preventing inst1 from being set
1062 on the way out of the loop.
1063 */
1064 READ_TIMESTAMP(inst1);
1065 loop1 = inst1;
1066 }
1067 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1068 intr0, intr1);
1069 ticked = 0;
1070 inst1 = 0;
1071 intr0 = 0;
1072 intr1 = 0;
1073 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001074#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001075 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1076 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001077
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001078 /* Do periodic things. Doing this every time through
1079 the loop would add too much overhead, so we do it
1080 only every Nth instruction. We also do it if
1081 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1082 event needs attention (e.g. a signal handler or
1083 async I/O handler); see Py_AddPendingCall() and
1084 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001085
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001086 if (--_Py_Ticker < 0) {
1087 if (*next_instr == SETUP_FINALLY) {
1088 /* Make the last opcode before
Ezio Melottic2077b02011-03-16 12:34:31 +02001089 a try: finally: block uninterruptible. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001090 goto fast_next_opcode;
1091 }
1092 _Py_Ticker = _Py_CheckInterval;
1093 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001094#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001095 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001096#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001097 if (pendingcalls_to_do) {
1098 if (Py_MakePendingCalls() < 0) {
1099 why = WHY_EXCEPTION;
1100 goto on_error;
1101 }
1102 if (pendingcalls_to_do)
1103 /* MakePendingCalls() didn't succeed.
1104 Force early re-execution of this
1105 "periodic" code, possibly after
1106 a thread switch */
1107 _Py_Ticker = 0;
1108 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001109#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001110 if (interpreter_lock) {
1111 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001112
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001113 if (PyThreadState_Swap(NULL) != tstate)
1114 Py_FatalError("ceval: tstate mix-up");
1115 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001116
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001117 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001118
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001119 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Peterson1c78e6d2014-06-16 22:59:07 -07001120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001121 if (PyThreadState_Swap(tstate) != NULL)
1122 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001123
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001124 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001125
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001126 if (tstate->async_exc != NULL) {
1127 x = tstate->async_exc;
1128 tstate->async_exc = NULL;
1129 PyErr_SetNone(x);
1130 Py_DECREF(x);
1131 why = WHY_EXCEPTION;
1132 goto on_error;
1133 }
1134 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001135#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001136 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001137
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001138 fast_next_opcode:
1139 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001140
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001141 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001142
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001143 if (_Py_TracingPossible &&
1144 tstate->c_tracefunc != NULL && !tstate->tracing) {
1145 /* see maybe_call_line_trace
1146 for expository comments */
1147 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001148
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001149 err = maybe_call_line_trace(tstate->c_tracefunc,
1150 tstate->c_traceobj,
1151 f, &instr_lb, &instr_ub,
1152 &instr_prev);
1153 /* Reload possibly changed frame fields */
1154 JUMPTO(f->f_lasti);
1155 if (f->f_stacktop != NULL) {
1156 stack_pointer = f->f_stacktop;
1157 f->f_stacktop = NULL;
1158 }
1159 if (err) {
1160 /* trace function raised an exception */
1161 goto on_error;
1162 }
1163 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001164
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001165 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001166
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001167 opcode = NEXTOP();
1168 oparg = 0; /* allows oparg to be stored in a register because
1169 it doesn't have to be remembered across a full loop */
1170 if (HAS_ARG(opcode))
1171 oparg = NEXTARG();
Stefan Krah7ff78252010-06-23 18:12:09 +00001172 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001173#ifdef DYNAMIC_EXECUTION_PROFILE
1174#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001175 dxpairs[lastopcode][opcode]++;
1176 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001177#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001178 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001179#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001180
Guido van Rossum96a42c81992-01-12 02:29:51 +00001181#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001182 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001184 if (lltrace) {
1185 if (HAS_ARG(opcode)) {
1186 printf("%d: %d, %d\n",
1187 f->f_lasti, opcode, oparg);
1188 }
1189 else {
1190 printf("%d: %d\n",
1191 f->f_lasti, opcode);
1192 }
1193 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001194#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001195
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001196 /* Main switch on opcode */
1197 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001199 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001201 /* BEWARE!
1202 It is essential that any operation that fails sets either
1203 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1204 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001205
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001206 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001207
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001208 TARGET_NOARG(NOP)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001209 {
1210 FAST_DISPATCH();
1211 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001212
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001213 TARGET(LOAD_FAST)
1214 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001215 x = GETLOCAL(oparg);
1216 if (x != NULL) {
1217 Py_INCREF(x);
1218 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001219 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001220 }
1221 format_exc_check_arg(PyExc_UnboundLocalError,
1222 UNBOUNDLOCAL_ERROR_MSG,
1223 PyTuple_GetItem(co->co_varnames, oparg));
1224 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001225 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001226
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001227 TARGET(LOAD_CONST)
1228 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001229 x = GETITEM(consts, oparg);
1230 Py_INCREF(x);
1231 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001232 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001233 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001234
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001235 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001236 TARGET(STORE_FAST)
1237 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001238 v = POP();
1239 SETLOCAL(oparg, v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001240 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001241 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001242
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001243 TARGET_NOARG(POP_TOP)
1244 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001245 v = POP();
1246 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001247 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001248 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001249
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001250 TARGET_NOARG(ROT_TWO)
1251 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001252 v = TOP();
1253 w = SECOND();
1254 SET_TOP(w);
1255 SET_SECOND(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001256 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001257 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001258
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001259 TARGET_NOARG(ROT_THREE)
1260 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001261 v = TOP();
1262 w = SECOND();
1263 x = THIRD();
1264 SET_TOP(w);
1265 SET_SECOND(x);
1266 SET_THIRD(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001267 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001269
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001270 TARGET_NOARG(ROT_FOUR)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001271 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001272 u = TOP();
1273 v = SECOND();
1274 w = THIRD();
1275 x = FOURTH();
1276 SET_TOP(v);
1277 SET_SECOND(w);
1278 SET_THIRD(x);
1279 SET_FOURTH(u);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001280 FAST_DISPATCH();
Benjamin Peterson14462d42015-08-19 20:38:39 -07001281 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001282
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001283
1284 TARGET_NOARG(DUP_TOP)
1285 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001286 v = TOP();
1287 Py_INCREF(v);
1288 PUSH(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001289 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001290 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001291
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001292
1293 TARGET(DUP_TOPX)
1294 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001295 if (oparg == 2) {
1296 x = TOP();
1297 Py_INCREF(x);
1298 w = SECOND();
1299 Py_INCREF(w);
1300 STACKADJ(2);
1301 SET_TOP(x);
1302 SET_SECOND(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001303 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001304 } else if (oparg == 3) {
1305 x = TOP();
1306 Py_INCREF(x);
1307 w = SECOND();
1308 Py_INCREF(w);
1309 v = THIRD();
1310 Py_INCREF(v);
1311 STACKADJ(3);
1312 SET_TOP(x);
1313 SET_SECOND(w);
1314 SET_THIRD(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001315 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001316 }
1317 Py_FatalError("invalid argument to DUP_TOPX"
1318 " (bytecode corruption?)");
1319 /* Never returns, so don't bother to set why. */
1320 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001321 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001322
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001323 TARGET_NOARG(UNARY_POSITIVE)
1324 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001325 v = TOP();
1326 x = PyNumber_Positive(v);
1327 Py_DECREF(v);
1328 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001329 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001330 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001332
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001333 TARGET_NOARG( UNARY_NEGATIVE)
1334 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001335 v = TOP();
1336 x = PyNumber_Negative(v);
1337 Py_DECREF(v);
1338 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001339 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001340 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001342
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001343 TARGET_NOARG(UNARY_NOT)
1344 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001345 v = TOP();
1346 err = PyObject_IsTrue(v);
1347 Py_DECREF(v);
1348 if (err == 0) {
1349 Py_INCREF(Py_True);
1350 SET_TOP(Py_True);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001351 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001352 }
1353 else if (err > 0) {
1354 Py_INCREF(Py_False);
1355 SET_TOP(Py_False);
1356 err = 0;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001357 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001358 }
1359 STACKADJ(-1);
1360 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001361 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001362
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001363 TARGET_NOARG(UNARY_CONVERT)
1364 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001365 v = TOP();
1366 x = PyObject_Repr(v);
1367 Py_DECREF(v);
1368 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001369 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001370 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001371 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001372
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001373 TARGET_NOARG(UNARY_INVERT)
1374 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001375 v = TOP();
1376 x = PyNumber_Invert(v);
1377 Py_DECREF(v);
1378 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001379 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001380 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001382
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001383 TARGET_NOARG(BINARY_POWER)
1384 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001385 w = POP();
1386 v = TOP();
1387 x = PyNumber_Power(v, w, Py_None);
1388 Py_DECREF(v);
1389 Py_DECREF(w);
1390 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001391 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001392 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001393 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001394
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001395 TARGET_NOARG(BINARY_MULTIPLY)
1396 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001397 w = POP();
1398 v = TOP();
1399 x = PyNumber_Multiply(v, w);
1400 Py_DECREF(v);
1401 Py_DECREF(w);
1402 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001403 if(x!=NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001404 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001405 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001406
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001407 TARGET_NOARG(BINARY_DIVIDE)
1408 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001409 if (!_Py_QnewFlag) {
1410 w = POP();
1411 v = TOP();
1412 x = PyNumber_Divide(v, w);
1413 Py_DECREF(v);
1414 Py_DECREF(w);
1415 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001416 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001417 break;
1418 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001419 }
1420 /* -Qnew is in effect: fall through to BINARY_TRUE_DIVIDE */
1421 TARGET_NOARG(BINARY_TRUE_DIVIDE)
1422 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001423 w = POP();
1424 v = TOP();
1425 x = PyNumber_TrueDivide(v, w);
1426 Py_DECREF(v);
1427 Py_DECREF(w);
1428 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001429 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001430 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001431 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001433 TARGET_NOARG(BINARY_FLOOR_DIVIDE)
1434 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001435 w = POP();
1436 v = TOP();
1437 x = PyNumber_FloorDivide(v, w);
1438 Py_DECREF(v);
1439 Py_DECREF(w);
1440 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001441 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001442 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001443 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001444
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001445 TARGET_NOARG(BINARY_MODULO)
1446 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001447 w = POP();
1448 v = TOP();
1449 if (PyString_CheckExact(v))
1450 x = PyString_Format(v, w);
1451 else
1452 x = PyNumber_Remainder(v, w);
1453 Py_DECREF(v);
1454 Py_DECREF(w);
1455 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001456 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001457 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001458 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001459
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001460 TARGET_NOARG(BINARY_ADD)
1461 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001462 w = POP();
1463 v = TOP();
1464 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1465 /* INLINE: int + int */
1466 register long a, b, i;
1467 a = PyInt_AS_LONG(v);
1468 b = PyInt_AS_LONG(w);
1469 /* cast to avoid undefined behaviour
1470 on overflow */
1471 i = (long)((unsigned long)a + b);
1472 if ((i^a) < 0 && (i^b) < 0)
1473 goto slow_add;
1474 x = PyInt_FromLong(i);
1475 }
1476 else if (PyString_CheckExact(v) &&
1477 PyString_CheckExact(w)) {
1478 x = string_concatenate(v, w, f, next_instr);
1479 /* string_concatenate consumed the ref to v */
1480 goto skip_decref_vx;
1481 }
1482 else {
1483 slow_add:
1484 x = PyNumber_Add(v, w);
1485 }
1486 Py_DECREF(v);
1487 skip_decref_vx:
1488 Py_DECREF(w);
1489 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001490 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001491 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001492 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001493
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001494 TARGET_NOARG(BINARY_SUBTRACT)
1495 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001496 w = POP();
1497 v = TOP();
1498 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1499 /* INLINE: int - int */
1500 register long a, b, i;
1501 a = PyInt_AS_LONG(v);
1502 b = PyInt_AS_LONG(w);
1503 /* cast to avoid undefined behaviour
1504 on overflow */
1505 i = (long)((unsigned long)a - b);
1506 if ((i^a) < 0 && (i^~b) < 0)
1507 goto slow_sub;
1508 x = PyInt_FromLong(i);
1509 }
1510 else {
1511 slow_sub:
1512 x = PyNumber_Subtract(v, w);
1513 }
1514 Py_DECREF(v);
1515 Py_DECREF(w);
1516 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001517 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001518 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001519 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001520
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001521 TARGET_NOARG(BINARY_SUBSCR)
1522 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001523 w = POP();
1524 v = TOP();
1525 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1526 /* INLINE: list[int] */
1527 Py_ssize_t i = PyInt_AsSsize_t(w);
1528 if (i < 0)
1529 i += PyList_GET_SIZE(v);
1530 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1531 x = PyList_GET_ITEM(v, i);
1532 Py_INCREF(x);
1533 }
1534 else
1535 goto slow_get;
1536 }
1537 else
1538 slow_get:
1539 x = PyObject_GetItem(v, w);
1540 Py_DECREF(v);
1541 Py_DECREF(w);
1542 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001543 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001544 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001545 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001546
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001547 TARGET_NOARG(BINARY_LSHIFT)
1548 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001549 w = POP();
1550 v = TOP();
1551 x = PyNumber_Lshift(v, w);
1552 Py_DECREF(v);
1553 Py_DECREF(w);
1554 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001555 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001556 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001557 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001558
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001559 TARGET_NOARG(BINARY_RSHIFT)
1560 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001561 w = POP();
1562 v = TOP();
1563 x = PyNumber_Rshift(v, w);
1564 Py_DECREF(v);
1565 Py_DECREF(w);
1566 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001567 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001568 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001569 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001570
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001571 TARGET_NOARG(BINARY_AND)
1572 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001573 w = POP();
1574 v = TOP();
1575 x = PyNumber_And(v, w);
1576 Py_DECREF(v);
1577 Py_DECREF(w);
1578 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001579 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001580 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001581 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001582
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001583 TARGET_NOARG(BINARY_XOR)
1584 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001585 w = POP();
1586 v = TOP();
1587 x = PyNumber_Xor(v, w);
1588 Py_DECREF(v);
1589 Py_DECREF(w);
1590 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001591 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001592 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001593 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001594
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001595 TARGET_NOARG(BINARY_OR)
1596 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001597 w = POP();
1598 v = TOP();
1599 x = PyNumber_Or(v, w);
1600 Py_DECREF(v);
1601 Py_DECREF(w);
1602 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001603 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001604 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001605 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001606
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001607 TARGET(LIST_APPEND)
1608 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001609 w = POP();
1610 v = PEEK(oparg);
1611 err = PyList_Append(v, w);
1612 Py_DECREF(w);
1613 if (err == 0) {
1614 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001615 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001616 }
1617 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001618 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001619
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001620 TARGET(SET_ADD)
1621 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001622 w = POP();
1623 v = stack_pointer[-oparg];
1624 err = PySet_Add(v, w);
1625 Py_DECREF(w);
1626 if (err == 0) {
1627 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001628 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001629 }
1630 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001631 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001632
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001633 TARGET_NOARG(INPLACE_POWER)
1634 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001635 w = POP();
1636 v = TOP();
1637 x = PyNumber_InPlacePower(v, w, Py_None);
1638 Py_DECREF(v);
1639 Py_DECREF(w);
1640 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001641 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001642 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001643 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001644
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001645 TARGET_NOARG(INPLACE_MULTIPLY)
1646 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001647 w = POP();
1648 v = TOP();
1649 x = PyNumber_InPlaceMultiply(v, w);
1650 Py_DECREF(v);
1651 Py_DECREF(w);
1652 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001653 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001654 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001655 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001656
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001657 TARGET_NOARG(INPLACE_DIVIDE)
1658 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001659 if (!_Py_QnewFlag) {
1660 w = POP();
1661 v = TOP();
1662 x = PyNumber_InPlaceDivide(v, w);
1663 Py_DECREF(v);
1664 Py_DECREF(w);
1665 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001666 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001667 break;
1668 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001669 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001670 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001671 INPLACE_TRUE_DIVIDE */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001672 TARGET_NOARG(INPLACE_TRUE_DIVIDE)
1673 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001674 w = POP();
1675 v = TOP();
1676 x = PyNumber_InPlaceTrueDivide(v, w);
1677 Py_DECREF(v);
1678 Py_DECREF(w);
1679 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001680 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001681 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001682 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001683
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001684 TARGET_NOARG(INPLACE_FLOOR_DIVIDE)
1685 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001686 w = POP();
1687 v = TOP();
1688 x = PyNumber_InPlaceFloorDivide(v, w);
1689 Py_DECREF(v);
1690 Py_DECREF(w);
1691 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001692 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001693 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001694 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001695
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001696 TARGET_NOARG(INPLACE_MODULO)
1697 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001698 w = POP();
1699 v = TOP();
1700 x = PyNumber_InPlaceRemainder(v, w);
1701 Py_DECREF(v);
1702 Py_DECREF(w);
1703 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001704 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001705 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001706 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001707
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001708 TARGET_NOARG(INPLACE_ADD)
1709 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001710 w = POP();
1711 v = TOP();
1712 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1713 /* INLINE: int + int */
1714 register long a, b, i;
1715 a = PyInt_AS_LONG(v);
1716 b = PyInt_AS_LONG(w);
1717 i = a + b;
1718 if ((i^a) < 0 && (i^b) < 0)
1719 goto slow_iadd;
1720 x = PyInt_FromLong(i);
1721 }
1722 else if (PyString_CheckExact(v) &&
1723 PyString_CheckExact(w)) {
1724 x = string_concatenate(v, w, f, next_instr);
1725 /* string_concatenate consumed the ref to v */
1726 goto skip_decref_v;
1727 }
1728 else {
1729 slow_iadd:
1730 x = PyNumber_InPlaceAdd(v, w);
1731 }
1732 Py_DECREF(v);
1733 skip_decref_v:
1734 Py_DECREF(w);
1735 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001736 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001737 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001738 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001740 TARGET_NOARG(INPLACE_SUBTRACT)
1741 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001742 w = POP();
1743 v = TOP();
1744 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1745 /* INLINE: int - int */
1746 register long a, b, i;
1747 a = PyInt_AS_LONG(v);
1748 b = PyInt_AS_LONG(w);
1749 i = a - b;
1750 if ((i^a) < 0 && (i^~b) < 0)
1751 goto slow_isub;
1752 x = PyInt_FromLong(i);
1753 }
1754 else {
1755 slow_isub:
1756 x = PyNumber_InPlaceSubtract(v, w);
1757 }
1758 Py_DECREF(v);
1759 Py_DECREF(w);
1760 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001761 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001762 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001763 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001765 TARGET_NOARG(INPLACE_LSHIFT)
1766 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001767 w = POP();
1768 v = TOP();
1769 x = PyNumber_InPlaceLshift(v, w);
1770 Py_DECREF(v);
1771 Py_DECREF(w);
1772 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001773 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001774 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001775 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001776
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001777 TARGET_NOARG(INPLACE_RSHIFT)
1778 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001779 w = POP();
1780 v = TOP();
1781 x = PyNumber_InPlaceRshift(v, w);
1782 Py_DECREF(v);
1783 Py_DECREF(w);
1784 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001785 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001786 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001787 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001789 TARGET_NOARG(INPLACE_AND)
1790 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001791 w = POP();
1792 v = TOP();
1793 x = PyNumber_InPlaceAnd(v, w);
1794 Py_DECREF(v);
1795 Py_DECREF(w);
1796 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001797 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001798 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001799 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001800
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001801 TARGET_NOARG(INPLACE_XOR)
1802 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001803 w = POP();
1804 v = TOP();
1805 x = PyNumber_InPlaceXor(v, w);
1806 Py_DECREF(v);
1807 Py_DECREF(w);
1808 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001809 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001810 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001811 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001812
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001813 TARGET_NOARG(INPLACE_OR)
1814 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001815 w = POP();
1816 v = TOP();
1817 x = PyNumber_InPlaceOr(v, w);
1818 Py_DECREF(v);
1819 Py_DECREF(w);
1820 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001821 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001822 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001823 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001824
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001825
1826
1827 TARGET_WITH_IMPL_NOARG(SLICE, _slice)
1828 TARGET_WITH_IMPL_NOARG(SLICE_1, _slice)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001829 TARGET_WITH_IMPL_NOARG(SLICE_2, _slice)
1830 TARGET_WITH_IMPL_NOARG(SLICE_3, _slice)
1831 _slice:
1832 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001833 if ((opcode-SLICE) & 2)
1834 w = POP();
1835 else
1836 w = NULL;
1837 if ((opcode-SLICE) & 1)
1838 v = POP();
1839 else
1840 v = NULL;
1841 u = TOP();
1842 x = apply_slice(u, v, w);
1843 Py_DECREF(u);
1844 Py_XDECREF(v);
1845 Py_XDECREF(w);
1846 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001847 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001848 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001849 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001850
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001851
1852 TARGET_WITH_IMPL_NOARG(STORE_SLICE, _store_slice)
1853 TARGET_WITH_IMPL_NOARG(STORE_SLICE_1, _store_slice)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001854 TARGET_WITH_IMPL_NOARG(STORE_SLICE_2, _store_slice)
1855 TARGET_WITH_IMPL_NOARG(STORE_SLICE_3, _store_slice)
1856 _store_slice:
1857 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001858 if ((opcode-STORE_SLICE) & 2)
1859 w = POP();
1860 else
1861 w = NULL;
1862 if ((opcode-STORE_SLICE) & 1)
1863 v = POP();
1864 else
1865 v = NULL;
1866 u = POP();
1867 t = POP();
1868 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1869 Py_DECREF(t);
1870 Py_DECREF(u);
1871 Py_XDECREF(v);
1872 Py_XDECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001873 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001874 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001875 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001876
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001877
1878 TARGET_WITH_IMPL_NOARG(DELETE_SLICE, _delete_slice)
1879 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_1, _delete_slice)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001880 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_2, _delete_slice)
1881 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_3, _delete_slice)
1882 _delete_slice:
1883 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001884 if ((opcode-DELETE_SLICE) & 2)
1885 w = POP();
1886 else
1887 w = NULL;
1888 if ((opcode-DELETE_SLICE) & 1)
1889 v = POP();
1890 else
1891 v = NULL;
1892 u = POP();
1893 err = assign_slice(u, v, w, (PyObject *)NULL);
1894 /* del u[v:w] */
1895 Py_DECREF(u);
1896 Py_XDECREF(v);
1897 Py_XDECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001898 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001899 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001900 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001901
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001902 TARGET_NOARG(STORE_SUBSCR)
1903 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001904 w = TOP();
1905 v = SECOND();
1906 u = THIRD();
1907 STACKADJ(-3);
1908 /* v[w] = u */
1909 err = PyObject_SetItem(v, w, u);
1910 Py_DECREF(u);
1911 Py_DECREF(v);
1912 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001913 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001914 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001915 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001916
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001917 TARGET_NOARG(DELETE_SUBSCR)
1918 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001919 w = TOP();
1920 v = SECOND();
1921 STACKADJ(-2);
1922 /* del v[w] */
1923 err = PyObject_DelItem(v, w);
1924 Py_DECREF(v);
1925 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001926 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001927 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001928 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001929
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001930 TARGET_NOARG(PRINT_EXPR)
1931 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001932 v = POP();
1933 w = PySys_GetObject("displayhook");
1934 if (w == NULL) {
1935 PyErr_SetString(PyExc_RuntimeError,
1936 "lost sys.displayhook");
1937 err = -1;
1938 x = NULL;
1939 }
1940 if (err == 0) {
1941 x = PyTuple_Pack(1, v);
1942 if (x == NULL)
1943 err = -1;
1944 }
1945 if (err == 0) {
1946 w = PyEval_CallObject(w, x);
1947 Py_XDECREF(w);
1948 if (w == NULL)
1949 err = -1;
1950 }
1951 Py_DECREF(v);
1952 Py_XDECREF(x);
1953 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001954 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001955
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001956 TARGET_NOARG(PRINT_ITEM_TO)
1957 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001958 w = stream = POP();
1959 /* fall through to PRINT_ITEM */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001960 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001961
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001962 TARGET_NOARG(PRINT_ITEM)
1963 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001964 v = POP();
1965 if (stream == NULL || stream == Py_None) {
1966 w = PySys_GetObject("stdout");
1967 if (w == NULL) {
1968 PyErr_SetString(PyExc_RuntimeError,
1969 "lost sys.stdout");
1970 err = -1;
1971 }
1972 }
1973 /* PyFile_SoftSpace() can exececute arbitrary code
1974 if sys.stdout is an instance with a __getattr__.
1975 If __getattr__ raises an exception, w will
1976 be freed, so we need to prevent that temporarily. */
1977 Py_XINCREF(w);
1978 if (w != NULL && PyFile_SoftSpace(w, 0))
1979 err = PyFile_WriteString(" ", w);
1980 if (err == 0)
1981 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1982 if (err == 0) {
1983 /* XXX move into writeobject() ? */
1984 if (PyString_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001985 char *s = PyString_AS_STRING(v);
1986 Py_ssize_t len = PyString_GET_SIZE(v);
1987 if (len == 0 ||
1988 !isspace(Py_CHARMASK(s[len-1])) ||
1989 s[len-1] == ' ')
1990 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001991 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00001992#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001993 else if (PyUnicode_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001994 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
1995 Py_ssize_t len = PyUnicode_GET_SIZE(v);
1996 if (len == 0 ||
1997 !Py_UNICODE_ISSPACE(s[len-1]) ||
1998 s[len-1] == ' ')
1999 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002000 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00002001#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002002 else
Stefan Krah7ff78252010-06-23 18:12:09 +00002003 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002004 }
2005 Py_XDECREF(w);
2006 Py_DECREF(v);
2007 Py_XDECREF(stream);
2008 stream = NULL;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002009 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002010 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002011 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002012
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002013 TARGET_NOARG(PRINT_NEWLINE_TO)
2014 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002015 w = stream = POP();
2016 /* fall through to PRINT_NEWLINE */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002017 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002018
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002019 TARGET_NOARG(PRINT_NEWLINE)
2020 {
Benjamin Peterson14462d42015-08-19 20:38:39 -07002021 if (stream == NULL || stream == Py_None)
2022 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002023 w = PySys_GetObject("stdout");
2024 if (w == NULL) {
2025 PyErr_SetString(PyExc_RuntimeError,
2026 "lost sys.stdout");
2027 why = WHY_EXCEPTION;
2028 }
2029 }
2030 if (w != NULL) {
2031 /* w.write() may replace sys.stdout, so we
2032 * have to keep our reference to it */
2033 Py_INCREF(w);
2034 err = PyFile_WriteString("\n", w);
2035 if (err == 0)
2036 PyFile_SoftSpace(w, 0);
2037 Py_DECREF(w);
2038 }
2039 Py_XDECREF(stream);
2040 stream = NULL;
2041 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002042 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002043
2044#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002045 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002046#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002047
2048 TARGET(RAISE_VARARGS)
Benjamin Peterson14462d42015-08-19 20:38:39 -07002049 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002050 u = v = w = NULL;
2051 switch (oparg) {
2052 case 3:
2053 u = POP(); /* traceback */
2054 /* Fallthrough */
2055 case 2:
2056 v = POP(); /* value */
2057 /* Fallthrough */
2058 case 1:
2059 w = POP(); /* exc */
2060 case 0: /* Fallthrough */
2061 why = do_raise(w, v, u);
2062 break;
2063 default:
2064 PyErr_SetString(PyExc_SystemError,
2065 "bad RAISE_VARARGS oparg");
2066 why = WHY_EXCEPTION;
2067 break;
2068 }
2069 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002070 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002071
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002072 TARGET_NOARG(LOAD_LOCALS)
2073 {
Benjamin Peterson14462d42015-08-19 20:38:39 -07002074 if ((x = f->f_locals) != NULL)
2075 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002076 Py_INCREF(x);
2077 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002078 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002079 }
2080 PyErr_SetString(PyExc_SystemError, "no locals");
2081 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002082 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002084 TARGET_NOARG(RETURN_VALUE)
2085 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002086 retval = POP();
2087 why = WHY_RETURN;
2088 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002089 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002090
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002091 TARGET_NOARG(YIELD_VALUE)
2092 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002093 retval = POP();
2094 f->f_stacktop = stack_pointer;
2095 why = WHY_YIELD;
2096 goto fast_yield;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002097 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002098
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002099 TARGET_NOARG(EXEC_STMT)
2100 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002101 w = TOP();
2102 v = SECOND();
2103 u = THIRD();
2104 STACKADJ(-3);
2105 READ_TIMESTAMP(intr0);
2106 err = exec_statement(f, u, v, w);
2107 READ_TIMESTAMP(intr1);
2108 Py_DECREF(u);
2109 Py_DECREF(v);
2110 Py_DECREF(w);
2111 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002112 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002113
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002114 TARGET_NOARG(POP_BLOCK)
2115 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002116 {
2117 PyTryBlock *b = PyFrame_BlockPop(f);
2118 while (STACK_LEVEL() > b->b_level) {
2119 v = POP();
2120 Py_DECREF(v);
2121 }
2122 }
Benjamin Peterson14462d42015-08-19 20:38:39 -07002123 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002126 PREDICTED(END_FINALLY);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002127 TARGET_NOARG(END_FINALLY)
2128 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002129 v = POP();
2130 if (PyInt_Check(v)) {
2131 why = (enum why_code) PyInt_AS_LONG(v);
2132 assert(why != WHY_YIELD);
2133 if (why == WHY_RETURN ||
2134 why == WHY_CONTINUE)
2135 retval = POP();
2136 }
2137 else if (PyExceptionClass_Check(v) ||
2138 PyString_Check(v)) {
2139 w = POP();
2140 u = POP();
2141 PyErr_Restore(v, w, u);
2142 why = WHY_RERAISE;
2143 break;
2144 }
2145 else if (v != Py_None) {
2146 PyErr_SetString(PyExc_SystemError,
2147 "'finally' pops bad exception");
2148 why = WHY_EXCEPTION;
2149 }
2150 Py_DECREF(v);
2151 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002152 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002153
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002154 TARGET_NOARG(BUILD_CLASS)
2155 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002156 u = TOP();
2157 v = SECOND();
2158 w = THIRD();
2159 STACKADJ(-2);
2160 x = build_class(u, v, w);
2161 SET_TOP(x);
2162 Py_DECREF(u);
2163 Py_DECREF(v);
2164 Py_DECREF(w);
2165 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002166 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002167
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002168 TARGET(STORE_NAME)
2169 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002170 w = GETITEM(names, oparg);
2171 v = POP();
2172 if ((x = f->f_locals) != NULL) {
2173 if (PyDict_CheckExact(x))
2174 err = PyDict_SetItem(x, w, v);
2175 else
2176 err = PyObject_SetItem(x, w, v);
2177 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002178 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002179 break;
2180 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002181 t = PyObject_Repr(w);
2182 if (t == NULL)
2183 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002184 PyErr_Format(PyExc_SystemError,
2185 "no locals found when storing %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002186 PyString_AS_STRING(t));
2187 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002188 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002189 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002190
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002191 TARGET(DELETE_NAME)
2192 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002193 w = GETITEM(names, oparg);
2194 if ((x = f->f_locals) != NULL) {
2195 if ((err = PyObject_DelItem(x, w)) != 0)
2196 format_exc_check_arg(PyExc_NameError,
2197 NAME_ERROR_MSG,
2198 w);
2199 break;
2200 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002201 t = PyObject_Repr(w);
2202 if (t == NULL)
2203 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002204 PyErr_Format(PyExc_SystemError,
2205 "no locals when deleting %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002206 PyString_AS_STRING(w));
2207 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002208 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002209 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002210
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002211 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002212 TARGET(UNPACK_SEQUENCE)
2213 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002214 v = POP();
2215 if (PyTuple_CheckExact(v) &&
2216 PyTuple_GET_SIZE(v) == oparg) {
2217 PyObject **items = \
2218 ((PyTupleObject *)v)->ob_item;
2219 while (oparg--) {
2220 w = items[oparg];
2221 Py_INCREF(w);
2222 PUSH(w);
2223 }
2224 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002225 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002226 } else if (PyList_CheckExact(v) &&
2227 PyList_GET_SIZE(v) == oparg) {
2228 PyObject **items = \
2229 ((PyListObject *)v)->ob_item;
2230 while (oparg--) {
2231 w = items[oparg];
2232 Py_INCREF(w);
2233 PUSH(w);
2234 }
2235 } else if (unpack_iterable(v, oparg,
2236 stack_pointer + oparg)) {
2237 STACKADJ(oparg);
2238 } else {
2239 /* unpack_iterable() raised an exception */
2240 why = WHY_EXCEPTION;
2241 }
2242 Py_DECREF(v);
2243 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002244 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002245
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002246
2247 TARGET(STORE_ATTR)
2248 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002249 w = GETITEM(names, oparg);
2250 v = TOP();
2251 u = SECOND();
2252 STACKADJ(-2);
2253 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2254 Py_DECREF(v);
2255 Py_DECREF(u);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002256 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002257 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002259
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002260 TARGET(DELETE_ATTR)
2261 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002262 w = GETITEM(names, oparg);
2263 v = POP();
2264 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2265 /* del v.w */
2266 Py_DECREF(v);
2267 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002268 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002269
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002270
2271 TARGET(STORE_GLOBAL)
2272 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002273 w = GETITEM(names, oparg);
2274 v = POP();
2275 err = PyDict_SetItem(f->f_globals, w, v);
2276 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002277 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002278 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002279 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002280
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002281 TARGET(DELETE_GLOBAL)
2282 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002283 w = GETITEM(names, oparg);
2284 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2285 format_exc_check_arg(
2286 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2287 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002288 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002289
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002290 TARGET(LOAD_NAME)
2291 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002292 w = GETITEM(names, oparg);
2293 if ((v = f->f_locals) == NULL) {
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002294 why = WHY_EXCEPTION;
2295 t = PyObject_Repr(w);
2296 if (t == NULL)
2297 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002298 PyErr_Format(PyExc_SystemError,
2299 "no locals when loading %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002300 PyString_AS_STRING(w));
2301 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002302 break;
2303 }
2304 if (PyDict_CheckExact(v)) {
2305 x = PyDict_GetItem(v, w);
2306 Py_XINCREF(x);
2307 }
2308 else {
2309 x = PyObject_GetItem(v, w);
2310 if (x == NULL && PyErr_Occurred()) {
2311 if (!PyErr_ExceptionMatches(
2312 PyExc_KeyError))
2313 break;
2314 PyErr_Clear();
2315 }
2316 }
2317 if (x == NULL) {
2318 x = PyDict_GetItem(f->f_globals, w);
2319 if (x == NULL) {
2320 x = PyDict_GetItem(f->f_builtins, w);
2321 if (x == NULL) {
2322 format_exc_check_arg(
2323 PyExc_NameError,
2324 NAME_ERROR_MSG, w);
2325 break;
2326 }
2327 }
2328 Py_INCREF(x);
2329 }
2330 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002331 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002334 TARGET(LOAD_GLOBAL)
2335 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002336 w = GETITEM(names, oparg);
2337 if (PyString_CheckExact(w)) {
2338 /* Inline the PyDict_GetItem() calls.
2339 WARNING: this is an extreme speed hack.
2340 Do not try this at home. */
2341 long hash = ((PyStringObject *)w)->ob_shash;
2342 if (hash != -1) {
2343 PyDictObject *d;
2344 PyDictEntry *e;
2345 d = (PyDictObject *)(f->f_globals);
2346 e = d->ma_lookup(d, w, hash);
2347 if (e == NULL) {
2348 x = NULL;
2349 break;
2350 }
2351 x = e->me_value;
2352 if (x != NULL) {
2353 Py_INCREF(x);
2354 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002355 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002356 }
2357 d = (PyDictObject *)(f->f_builtins);
2358 e = d->ma_lookup(d, w, hash);
2359 if (e == NULL) {
2360 x = NULL;
2361 break;
2362 }
2363 x = e->me_value;
2364 if (x != NULL) {
2365 Py_INCREF(x);
2366 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002367 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002368 }
2369 goto load_global_error;
2370 }
2371 }
2372 /* This is the un-inlined version of the code above */
2373 x = PyDict_GetItem(f->f_globals, w);
2374 if (x == NULL) {
2375 x = PyDict_GetItem(f->f_builtins, w);
2376 if (x == NULL) {
2377 load_global_error:
2378 format_exc_check_arg(
2379 PyExc_NameError,
2380 GLOBAL_NAME_ERROR_MSG, w);
2381 break;
2382 }
2383 }
2384 Py_INCREF(x);
2385 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002386 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002387 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002388
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002389 TARGET(DELETE_FAST)
2390 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002391 x = GETLOCAL(oparg);
2392 if (x != NULL) {
2393 SETLOCAL(oparg, NULL);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002394 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002395 }
2396 format_exc_check_arg(
2397 PyExc_UnboundLocalError,
2398 UNBOUNDLOCAL_ERROR_MSG,
2399 PyTuple_GetItem(co->co_varnames, oparg)
2400 );
2401 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002402 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002403
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002404 TARGET(LOAD_CLOSURE)
2405 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002406 x = freevars[oparg];
2407 Py_INCREF(x);
2408 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002409 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002410 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002411 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002412
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002413 TARGET(LOAD_DEREF)
2414 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002415 x = freevars[oparg];
2416 w = PyCell_Get(x);
2417 if (w != NULL) {
2418 PUSH(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002419 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002420 }
2421 err = -1;
2422 /* Don't stomp existing exception */
2423 if (PyErr_Occurred())
2424 break;
2425 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2426 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krah7ff78252010-06-23 18:12:09 +00002427 oparg);
2428 format_exc_check_arg(
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002429 PyExc_UnboundLocalError,
2430 UNBOUNDLOCAL_ERROR_MSG,
2431 v);
2432 } else {
2433 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2434 PyTuple_GET_SIZE(co->co_cellvars));
2435 format_exc_check_arg(PyExc_NameError,
2436 UNBOUNDFREE_ERROR_MSG, v);
2437 }
2438 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002439 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002440
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002441 TARGET(STORE_DEREF)
2442 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002443 w = POP();
2444 x = freevars[oparg];
2445 PyCell_Set(x, w);
2446 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002447 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002448 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002449
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002450 TARGET(BUILD_TUPLE)
2451 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002452 x = PyTuple_New(oparg);
2453 if (x != NULL) {
2454 for (; --oparg >= 0;) {
2455 w = POP();
2456 PyTuple_SET_ITEM(x, oparg, w);
2457 }
2458 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002459 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002460 }
2461 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002462 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002463
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002464 TARGET(BUILD_LIST)
2465 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002466 x = PyList_New(oparg);
2467 if (x != NULL) {
2468 for (; --oparg >= 0;) {
2469 w = POP();
2470 PyList_SET_ITEM(x, oparg, w);
2471 }
2472 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002473 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002474 }
2475 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002476 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002477
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002478 TARGET(BUILD_SET)
2479 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002480 x = PySet_New(NULL);
2481 if (x != NULL) {
2482 for (; --oparg >= 0;) {
2483 w = POP();
2484 if (err == 0)
2485 err = PySet_Add(x, w);
2486 Py_DECREF(w);
2487 }
2488 if (err != 0) {
2489 Py_DECREF(x);
2490 break;
2491 }
2492 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002493 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002494 }
2495 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002496 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002497
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002498 TARGET(BUILD_MAP)
2499 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002500 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2501 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002502 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002503 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002505
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002506 TARGET_NOARG(STORE_MAP)
2507 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002508 w = TOP(); /* key */
2509 u = SECOND(); /* value */
2510 v = THIRD(); /* dict */
2511 STACKADJ(-2);
2512 assert (PyDict_CheckExact(v));
2513 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2514 Py_DECREF(u);
2515 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002516 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002517 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002518 }
Raymond Hettingereffde122007-12-18 18:26:18 +00002519
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002520 TARGET(MAP_ADD)
2521 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002522 w = TOP(); /* key */
2523 u = SECOND(); /* value */
2524 STACKADJ(-2);
2525 v = stack_pointer[-oparg]; /* dict */
2526 assert (PyDict_CheckExact(v));
2527 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2528 Py_DECREF(u);
2529 Py_DECREF(w);
2530 if (err == 0) {
2531 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002532 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002533 }
2534 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002535 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002536
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002537 TARGET(LOAD_ATTR)
2538 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002539 w = GETITEM(names, oparg);
2540 v = TOP();
2541 x = PyObject_GetAttr(v, w);
2542 Py_DECREF(v);
2543 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002544 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002545 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002547
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002548 TARGET(COMPARE_OP)
2549 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002550 w = POP();
2551 v = TOP();
2552 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2553 /* INLINE: cmp(int, int) */
2554 register long a, b;
2555 register int res;
2556 a = PyInt_AS_LONG(v);
2557 b = PyInt_AS_LONG(w);
2558 switch (oparg) {
2559 case PyCmp_LT: res = a < b; break;
2560 case PyCmp_LE: res = a <= b; break;
2561 case PyCmp_EQ: res = a == b; break;
2562 case PyCmp_NE: res = a != b; break;
2563 case PyCmp_GT: res = a > b; break;
2564 case PyCmp_GE: res = a >= b; break;
2565 case PyCmp_IS: res = v == w; break;
2566 case PyCmp_IS_NOT: res = v != w; break;
2567 default: goto slow_compare;
2568 }
2569 x = res ? Py_True : Py_False;
2570 Py_INCREF(x);
2571 }
2572 else {
2573 slow_compare:
2574 x = cmp_outcome(oparg, v, w);
2575 }
2576 Py_DECREF(v);
2577 Py_DECREF(w);
2578 SET_TOP(x);
2579 if (x == NULL) break;
2580 PREDICT(POP_JUMP_IF_FALSE);
2581 PREDICT(POP_JUMP_IF_TRUE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002582 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002583 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002584
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002585 TARGET(IMPORT_NAME)
2586 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002587 w = GETITEM(names, oparg);
2588 x = PyDict_GetItemString(f->f_builtins, "__import__");
2589 if (x == NULL) {
2590 PyErr_SetString(PyExc_ImportError,
2591 "__import__ not found");
2592 break;
2593 }
2594 Py_INCREF(x);
2595 v = POP();
2596 u = TOP();
2597 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2598 w = PyTuple_Pack(5,
2599 w,
2600 f->f_globals,
2601 f->f_locals == NULL ?
2602 Py_None : f->f_locals,
2603 v,
2604 u);
2605 else
2606 w = PyTuple_Pack(4,
2607 w,
2608 f->f_globals,
2609 f->f_locals == NULL ?
2610 Py_None : f->f_locals,
2611 v);
2612 Py_DECREF(v);
2613 Py_DECREF(u);
2614 if (w == NULL) {
2615 u = POP();
2616 Py_DECREF(x);
2617 x = NULL;
2618 break;
2619 }
2620 READ_TIMESTAMP(intr0);
2621 v = x;
2622 x = PyEval_CallObject(v, w);
2623 Py_DECREF(v);
2624 READ_TIMESTAMP(intr1);
2625 Py_DECREF(w);
2626 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002627 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002628 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002629 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002630
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002631 TARGET_NOARG(IMPORT_STAR)
2632 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002633 v = POP();
2634 PyFrame_FastToLocals(f);
2635 if ((x = f->f_locals) == NULL) {
2636 PyErr_SetString(PyExc_SystemError,
2637 "no locals found during 'import *'");
2638 break;
2639 }
2640 READ_TIMESTAMP(intr0);
2641 err = import_all_from(x, v);
2642 READ_TIMESTAMP(intr1);
2643 PyFrame_LocalsToFast(f, 0);
2644 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002645 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002646 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002647 }
Guido van Rossum25831651993-05-19 14:50:45 +00002648
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002649 TARGET(IMPORT_FROM)
2650 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002651 w = GETITEM(names, oparg);
2652 v = TOP();
2653 READ_TIMESTAMP(intr0);
2654 x = import_from(v, w);
2655 READ_TIMESTAMP(intr1);
2656 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002657 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002658 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002659 }
Thomas Wouters52152252000-08-17 22:55:00 +00002660
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002661 TARGET(JUMP_FORWARD)
2662 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002663 JUMPBY(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002664 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002665 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002666
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002667 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002668 TARGET(POP_JUMP_IF_FALSE)
2669 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002670 w = POP();
2671 if (w == Py_True) {
2672 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002673 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002674 }
2675 if (w == Py_False) {
2676 Py_DECREF(w);
2677 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002678 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002679 }
2680 err = PyObject_IsTrue(w);
2681 Py_DECREF(w);
2682 if (err > 0)
2683 err = 0;
2684 else if (err == 0)
2685 JUMPTO(oparg);
2686 else
2687 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002688 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002689 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002690
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002691 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002692 TARGET(POP_JUMP_IF_TRUE)
2693 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002694 w = POP();
2695 if (w == Py_False) {
2696 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002697 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002698 }
2699 if (w == Py_True) {
2700 Py_DECREF(w);
2701 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002702 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002703 }
2704 err = PyObject_IsTrue(w);
2705 Py_DECREF(w);
2706 if (err > 0) {
2707 err = 0;
2708 JUMPTO(oparg);
2709 }
2710 else if (err == 0)
2711 ;
2712 else
2713 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002714 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002715 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002716
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002717 TARGET(JUMP_IF_FALSE_OR_POP)
2718 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002719 w = TOP();
2720 if (w == Py_True) {
2721 STACKADJ(-1);
2722 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002723 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002724 }
2725 if (w == Py_False) {
2726 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002727 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002728 }
2729 err = PyObject_IsTrue(w);
2730 if (err > 0) {
2731 STACKADJ(-1);
2732 Py_DECREF(w);
2733 err = 0;
2734 }
2735 else if (err == 0)
2736 JUMPTO(oparg);
2737 else
2738 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002739 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002740 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002741
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002742 TARGET(JUMP_IF_TRUE_OR_POP)
2743 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002744 w = TOP();
2745 if (w == Py_False) {
2746 STACKADJ(-1);
2747 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002748 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002749 }
2750 if (w == Py_True) {
2751 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002752 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002753 }
2754 err = PyObject_IsTrue(w);
2755 if (err > 0) {
2756 err = 0;
2757 JUMPTO(oparg);
2758 }
2759 else if (err == 0) {
2760 STACKADJ(-1);
2761 Py_DECREF(w);
2762 }
2763 else
2764 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002765 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002766 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002767
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002768 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002769 TARGET(JUMP_ABSOLUTE)
2770 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002771 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002772#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002773 /* Enabling this path speeds-up all while and for-loops by bypassing
2774 the per-loop checks for signals. By default, this should be turned-off
2775 because it prevents detection of a control-break in tight loops like
2776 "while 1: pass". Compile with this option turned-on when you need
2777 the speed-up and do not need break checking inside tight loops (ones
2778 that contain only instructions ending with goto fast_next_opcode).
2779 */
2780 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002781#else
Benjamin Peterson14462d42015-08-19 20:38:39 -07002782 DISPATCH();
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002783#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002784 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002785
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002786 TARGET_NOARG(GET_ITER)
2787 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002788 /* before: [obj]; after [getiter(obj)] */
2789 v = TOP();
2790 x = PyObject_GetIter(v);
2791 Py_DECREF(v);
2792 if (x != NULL) {
2793 SET_TOP(x);
2794 PREDICT(FOR_ITER);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002795 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002796 }
2797 STACKADJ(-1);
2798 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002799 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002800
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002801 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002802 TARGET(FOR_ITER)
2803 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002804 /* before: [iter]; after: [iter, iter()] *or* [] */
2805 v = TOP();
2806 x = (*v->ob_type->tp_iternext)(v);
2807 if (x != NULL) {
2808 PUSH(x);
2809 PREDICT(STORE_FAST);
2810 PREDICT(UNPACK_SEQUENCE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002811 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002812 }
2813 if (PyErr_Occurred()) {
2814 if (!PyErr_ExceptionMatches(
2815 PyExc_StopIteration))
2816 break;
2817 PyErr_Clear();
2818 }
2819 /* iterator ended normally */
2820 x = v = POP();
2821 Py_DECREF(v);
2822 JUMPBY(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002823 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002824 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002825
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002826 TARGET_NOARG(BREAK_LOOP)
2827 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002828 why = WHY_BREAK;
2829 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002830 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002831
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002832 TARGET(CONTINUE_LOOP)
2833 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002834 retval = PyInt_FromLong(oparg);
2835 if (!retval) {
2836 x = NULL;
2837 break;
2838 }
2839 why = WHY_CONTINUE;
2840 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002841 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002842
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002843 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2844 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
Benjamin Peterson14462d42015-08-19 20:38:39 -07002845 TARGET(SETUP_FINALLY)
2846 _setup_finally:
2847 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002848 /* NOTE: If you add any new block-setup opcodes that
2849 are not try/except/finally handlers, you may need
2850 to update the PyGen_NeedsFinalizing() function.
2851 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002852
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002853 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2854 STACK_LEVEL());
Benjamin Peterson14462d42015-08-19 20:38:39 -07002855 DISPATCH();
2856 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002857
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002858
2859
2860 TARGET(SETUP_WITH)
2861 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002862 {
2863 static PyObject *exit, *enter;
2864 w = TOP();
2865 x = special_lookup(w, "__exit__", &exit);
2866 if (!x)
2867 break;
2868 SET_TOP(x);
2869 u = special_lookup(w, "__enter__", &enter);
2870 Py_DECREF(w);
2871 if (!u) {
2872 x = NULL;
2873 break;
2874 }
2875 x = PyObject_CallFunctionObjArgs(u, NULL);
2876 Py_DECREF(u);
2877 if (!x)
2878 break;
2879 /* Setup a finally block (SETUP_WITH as a block is
2880 equivalent to SETUP_FINALLY except it normalizes
2881 the exception) before pushing the result of
2882 __enter__ on the stack. */
2883 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2884 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002885
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002886 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002887 DISPATCH();
2888 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002889 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002890
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002891 TARGET_NOARG(WITH_CLEANUP)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002892 {
2893 /* At the top of the stack are 1-3 values indicating
2894 how/why we entered the finally clause:
2895 - TOP = None
2896 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2897 - TOP = WHY_*; no retval below it
2898 - (TOP, SECOND, THIRD) = exc_info()
2899 Below them is EXIT, the context.__exit__ bound method.
2900 In the last case, we must call
2901 EXIT(TOP, SECOND, THIRD)
2902 otherwise we must call
2903 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002905 In all cases, we remove EXIT from the stack, leaving
2906 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002907
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002908 In addition, if the stack represents an exception,
2909 *and* the function call returns a 'true' value, we
2910 "zap" this information, to prevent END_FINALLY from
2911 re-raising the exception. (But non-local gotos
2912 should still be resumed.)
2913 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002914
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002915 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002916
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002917 u = POP();
2918 if (u == Py_None) {
2919 exit_func = TOP();
2920 SET_TOP(u);
2921 v = w = Py_None;
2922 }
2923 else if (PyInt_Check(u)) {
2924 switch(PyInt_AS_LONG(u)) {
2925 case WHY_RETURN:
2926 case WHY_CONTINUE:
2927 /* Retval in TOP. */
2928 exit_func = SECOND();
2929 SET_SECOND(TOP());
2930 SET_TOP(u);
2931 break;
2932 default:
2933 exit_func = TOP();
2934 SET_TOP(u);
2935 break;
2936 }
2937 u = v = w = Py_None;
2938 }
2939 else {
2940 v = TOP();
2941 w = SECOND();
2942 exit_func = THIRD();
2943 SET_TOP(u);
2944 SET_SECOND(v);
2945 SET_THIRD(w);
2946 }
2947 /* XXX Not the fastest way to call it... */
2948 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2949 NULL);
2950 Py_DECREF(exit_func);
2951 if (x == NULL)
2952 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002953
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002954 if (u != Py_None)
2955 err = PyObject_IsTrue(x);
2956 else
2957 err = 0;
2958 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002959
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002960 if (err < 0)
2961 break; /* Go to error exit */
2962 else if (err > 0) {
2963 err = 0;
2964 /* There was an exception and a true return */
2965 STACKADJ(-2);
2966 Py_INCREF(Py_None);
2967 SET_TOP(Py_None);
2968 Py_DECREF(u);
2969 Py_DECREF(v);
2970 Py_DECREF(w);
2971 } else {
2972 /* The stack was rearranged to remove EXIT
2973 above. Let END_FINALLY do its thing */
2974 }
2975 PREDICT(END_FINALLY);
2976 break;
2977 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002978
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002979 TARGET(CALL_FUNCTION)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002980 {
2981 PyObject **sp;
2982 PCALL(PCALL_ALL);
2983 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002984#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002985 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002986#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002987 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002988#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002989 stack_pointer = sp;
2990 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002991 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002992 break;
2993 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002994
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002995 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
2996 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
Benjamin Peterson14462d42015-08-19 20:38:39 -07002997 TARGET(CALL_FUNCTION_VAR_KW)
2998 _call_function_var_kw:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002999 {
3000 int na = oparg & 0xff;
3001 int nk = (oparg>>8) & 0xff;
3002 int flags = (opcode - CALL_FUNCTION) & 3;
3003 int n = na + 2 * nk;
3004 PyObject **pfunc, *func, **sp;
3005 PCALL(PCALL_ALL);
3006 if (flags & CALL_FLAG_VAR)
3007 n++;
3008 if (flags & CALL_FLAG_KW)
3009 n++;
3010 pfunc = stack_pointer - n - 1;
3011 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003012
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003013 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00003014 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003015 PyObject *self = PyMethod_GET_SELF(func);
3016 Py_INCREF(self);
3017 func = PyMethod_GET_FUNCTION(func);
3018 Py_INCREF(func);
3019 Py_DECREF(*pfunc);
3020 *pfunc = self;
3021 na++;
3022 } else
3023 Py_INCREF(func);
3024 sp = stack_pointer;
3025 READ_TIMESTAMP(intr0);
3026 x = ext_do_call(func, &sp, flags, na, nk);
3027 READ_TIMESTAMP(intr1);
3028 stack_pointer = sp;
3029 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003030
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003031 while (stack_pointer > pfunc) {
3032 w = POP();
3033 Py_DECREF(w);
3034 }
3035 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003036 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003037 break;
3038 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003039
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003040
3041 TARGET(MAKE_FUNCTION)
3042 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003043 v = POP(); /* code object */
3044 x = PyFunction_New(v, f->f_globals);
3045 Py_DECREF(v);
3046 /* XXX Maybe this should be a separate opcode? */
3047 if (x != NULL && oparg > 0) {
3048 v = PyTuple_New(oparg);
3049 if (v == NULL) {
3050 Py_DECREF(x);
3051 x = NULL;
3052 break;
3053 }
3054 while (--oparg >= 0) {
3055 w = POP();
3056 PyTuple_SET_ITEM(v, oparg, w);
3057 }
3058 err = PyFunction_SetDefaults(x, v);
3059 Py_DECREF(v);
3060 }
3061 PUSH(x);
3062 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003063 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003064
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003065 TARGET(MAKE_CLOSURE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003066 {
3067 v = POP(); /* code object */
3068 x = PyFunction_New(v, f->f_globals);
3069 Py_DECREF(v);
3070 if (x != NULL) {
3071 v = POP();
3072 if (PyFunction_SetClosure(x, v) != 0) {
3073 /* Can't happen unless bytecode is corrupt. */
3074 why = WHY_EXCEPTION;
3075 }
3076 Py_DECREF(v);
3077 }
3078 if (x != NULL && oparg > 0) {
3079 v = PyTuple_New(oparg);
3080 if (v == NULL) {
3081 Py_DECREF(x);
3082 x = NULL;
3083 break;
3084 }
3085 while (--oparg >= 0) {
3086 w = POP();
3087 PyTuple_SET_ITEM(v, oparg, w);
3088 }
3089 if (PyFunction_SetDefaults(x, v) != 0) {
3090 /* Can't happen unless
3091 PyFunction_SetDefaults changes. */
3092 why = WHY_EXCEPTION;
3093 }
3094 Py_DECREF(v);
3095 }
3096 PUSH(x);
3097 break;
3098 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003099
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003100 TARGET(BUILD_SLICE)
3101 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003102 if (oparg == 3)
3103 w = POP();
3104 else
3105 w = NULL;
3106 v = POP();
3107 u = TOP();
3108 x = PySlice_New(u, v, w);
3109 Py_DECREF(u);
3110 Py_DECREF(v);
3111 Py_XDECREF(w);
3112 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003113 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003114 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003115 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003116
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003117 TARGET(EXTENDED_ARG)
3118 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003119 opcode = NEXTOP();
3120 oparg = oparg<<16 | NEXTARG();
3121 goto dispatch_opcode;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003122 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003123
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003124#if USE_COMPUTED_GOTOS
3125 _unknown_opcode:
3126#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003127 default:
3128 fprintf(stderr,
3129 "XXX lineno: %d, opcode: %d\n",
3130 PyFrame_GetLineNumber(f),
3131 opcode);
3132 PyErr_SetString(PyExc_SystemError, "unknown opcode");
3133 why = WHY_EXCEPTION;
3134 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003135
3136#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003137 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003138#endif
3139
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003140 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003142 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00003143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003144 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003145
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003146 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003147
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003148 if (why == WHY_NOT) {
3149 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003150#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003151 /* This check is expensive! */
3152 if (PyErr_Occurred())
3153 fprintf(stderr,
3154 "XXX undetected error\n");
3155 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003156#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003157 READ_TIMESTAMP(loop1);
3158 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003159#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003160 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003161#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003162 }
3163 why = WHY_EXCEPTION;
3164 x = Py_None;
3165 err = 0;
3166 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003167
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003168 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00003169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003170 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
3171 if (!PyErr_Occurred()) {
3172 PyErr_SetString(PyExc_SystemError,
3173 "error return without exception set");
3174 why = WHY_EXCEPTION;
3175 }
3176 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00003177#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003178 else {
3179 /* This check is expensive! */
3180 if (PyErr_Occurred()) {
3181 char buf[128];
3182 sprintf(buf, "Stack unwind with exception "
3183 "set and why=%d", why);
3184 Py_FatalError(buf);
3185 }
3186 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003187#endif
3188
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003189 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00003190
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003191 if (why == WHY_EXCEPTION) {
3192 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00003193
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003194 if (tstate->c_tracefunc != NULL)
3195 call_exc_trace(tstate->c_tracefunc,
3196 tstate->c_traceobj, f);
3197 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003199 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00003200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003201 if (why == WHY_RERAISE)
3202 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00003203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003204 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003205
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003206fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003207 while (why != WHY_NOT && f->f_iblock > 0) {
3208 /* Peek at the current block. */
3209 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003210
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003211 assert(why != WHY_YIELD);
3212 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3213 why = WHY_NOT;
3214 JUMPTO(PyInt_AS_LONG(retval));
3215 Py_DECREF(retval);
3216 break;
3217 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003219 /* Now we have to pop the block. */
3220 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00003221
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003222 while (STACK_LEVEL() > b->b_level) {
3223 v = POP();
3224 Py_XDECREF(v);
3225 }
3226 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3227 why = WHY_NOT;
3228 JUMPTO(b->b_handler);
3229 break;
3230 }
3231 if (b->b_type == SETUP_FINALLY ||
3232 (b->b_type == SETUP_EXCEPT &&
3233 why == WHY_EXCEPTION) ||
3234 b->b_type == SETUP_WITH) {
3235 if (why == WHY_EXCEPTION) {
3236 PyObject *exc, *val, *tb;
3237 PyErr_Fetch(&exc, &val, &tb);
3238 if (val == NULL) {
3239 val = Py_None;
3240 Py_INCREF(val);
3241 }
3242 /* Make the raw exception data
3243 available to the handler,
3244 so a program can emulate the
3245 Python main loop. Don't do
3246 this for 'finally'. */
3247 if (b->b_type == SETUP_EXCEPT ||
3248 b->b_type == SETUP_WITH) {
3249 PyErr_NormalizeException(
3250 &exc, &val, &tb);
3251 set_exc_info(tstate,
3252 exc, val, tb);
3253 }
3254 if (tb == NULL) {
3255 Py_INCREF(Py_None);
3256 PUSH(Py_None);
3257 } else
3258 PUSH(tb);
3259 PUSH(val);
3260 PUSH(exc);
3261 }
3262 else {
3263 if (why & (WHY_RETURN | WHY_CONTINUE))
3264 PUSH(retval);
3265 v = PyInt_FromLong((long)why);
3266 PUSH(v);
3267 }
3268 why = WHY_NOT;
3269 JUMPTO(b->b_handler);
3270 break;
3271 }
3272 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003273
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003274 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003275
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003276 if (why != WHY_NOT)
3277 break;
3278 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003279
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003280 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003281
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003282 assert(why != WHY_YIELD);
3283 /* Pop remaining stack entries. */
3284 while (!EMPTY()) {
3285 v = POP();
3286 Py_XDECREF(v);
3287 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003288
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003289 if (why != WHY_RETURN)
3290 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003291
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003292fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003293 if (tstate->use_tracing) {
3294 if (tstate->c_tracefunc) {
3295 if (why == WHY_RETURN || why == WHY_YIELD) {
3296 if (call_trace(tstate->c_tracefunc,
3297 tstate->c_traceobj, f,
3298 PyTrace_RETURN, retval)) {
3299 Py_XDECREF(retval);
3300 retval = NULL;
3301 why = WHY_EXCEPTION;
3302 }
3303 }
3304 else if (why == WHY_EXCEPTION) {
3305 call_trace_protected(tstate->c_tracefunc,
3306 tstate->c_traceobj, f,
3307 PyTrace_RETURN, NULL);
3308 }
3309 }
3310 if (tstate->c_profilefunc) {
3311 if (why == WHY_EXCEPTION)
3312 call_trace_protected(tstate->c_profilefunc,
3313 tstate->c_profileobj, f,
3314 PyTrace_RETURN, NULL);
3315 else if (call_trace(tstate->c_profilefunc,
3316 tstate->c_profileobj, f,
3317 PyTrace_RETURN, retval)) {
3318 Py_XDECREF(retval);
3319 retval = NULL;
3320 why = WHY_EXCEPTION;
3321 }
3322 }
3323 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003324
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003325 if (tstate->frame->f_exc_type != NULL)
3326 reset_exc_info(tstate);
3327 else {
3328 assert(tstate->frame->f_exc_value == NULL);
3329 assert(tstate->frame->f_exc_traceback == NULL);
3330 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003331
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003332 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003333exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003334 Py_LeaveRecursiveCall();
3335 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003336
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003337 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003338}
3339
Guido van Rossumc2e20742006-02-27 22:32:47 +00003340/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003341 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003342 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003343
Tim Peters6d6c1a32001-08-02 04:15:00 +00003344PyObject *
3345PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003346 PyObject **args, int argcount, PyObject **kws, int kwcount,
3347 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003348{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003349 register PyFrameObject *f;
3350 register PyObject *retval = NULL;
3351 register PyObject **fastlocals, **freevars;
3352 PyThreadState *tstate = PyThreadState_GET();
3353 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003354
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003355 if (globals == NULL) {
3356 PyErr_SetString(PyExc_SystemError,
3357 "PyEval_EvalCodeEx: NULL globals");
3358 return NULL;
3359 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003360
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003361 assert(tstate != NULL);
3362 assert(globals != NULL);
3363 f = PyFrame_New(tstate, co, globals, locals);
3364 if (f == NULL)
3365 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003366
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003367 fastlocals = f->f_localsplus;
3368 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003369
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003370 if (co->co_argcount > 0 ||
3371 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3372 int i;
3373 int n = argcount;
3374 PyObject *kwdict = NULL;
3375 if (co->co_flags & CO_VARKEYWORDS) {
3376 kwdict = PyDict_New();
3377 if (kwdict == NULL)
3378 goto fail;
3379 i = co->co_argcount;
3380 if (co->co_flags & CO_VARARGS)
3381 i++;
3382 SETLOCAL(i, kwdict);
3383 }
3384 if (argcount > co->co_argcount) {
3385 if (!(co->co_flags & CO_VARARGS)) {
3386 PyErr_Format(PyExc_TypeError,
3387 "%.200s() takes %s %d "
3388 "argument%s (%d given)",
3389 PyString_AsString(co->co_name),
3390 defcount ? "at most" : "exactly",
3391 co->co_argcount,
3392 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003393 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003394 goto fail;
3395 }
3396 n = co->co_argcount;
3397 }
3398 for (i = 0; i < n; i++) {
3399 x = args[i];
3400 Py_INCREF(x);
3401 SETLOCAL(i, x);
3402 }
3403 if (co->co_flags & CO_VARARGS) {
3404 u = PyTuple_New(argcount - n);
3405 if (u == NULL)
3406 goto fail;
3407 SETLOCAL(co->co_argcount, u);
3408 for (i = n; i < argcount; i++) {
3409 x = args[i];
3410 Py_INCREF(x);
3411 PyTuple_SET_ITEM(u, i-n, x);
3412 }
3413 }
3414 for (i = 0; i < kwcount; i++) {
3415 PyObject **co_varnames;
3416 PyObject *keyword = kws[2*i];
3417 PyObject *value = kws[2*i + 1];
3418 int j;
3419 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003420#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003421 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003422#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003423 )) {
3424 PyErr_Format(PyExc_TypeError,
3425 "%.200s() keywords must be strings",
3426 PyString_AsString(co->co_name));
3427 goto fail;
3428 }
3429 /* Speed hack: do raw pointer compares. As names are
3430 normally interned this should almost always hit. */
3431 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3432 for (j = 0; j < co->co_argcount; j++) {
3433 PyObject *nm = co_varnames[j];
3434 if (nm == keyword)
3435 goto kw_found;
3436 }
3437 /* Slow fallback, just in case */
3438 for (j = 0; j < co->co_argcount; j++) {
3439 PyObject *nm = co_varnames[j];
3440 int cmp = PyObject_RichCompareBool(
3441 keyword, nm, Py_EQ);
3442 if (cmp > 0)
3443 goto kw_found;
3444 else if (cmp < 0)
3445 goto fail;
3446 }
3447 if (kwdict == NULL) {
3448 PyObject *kwd_str = kwd_as_string(keyword);
3449 if (kwd_str) {
3450 PyErr_Format(PyExc_TypeError,
3451 "%.200s() got an unexpected "
3452 "keyword argument '%.400s'",
3453 PyString_AsString(co->co_name),
3454 PyString_AsString(kwd_str));
3455 Py_DECREF(kwd_str);
3456 }
3457 goto fail;
3458 }
3459 PyDict_SetItem(kwdict, keyword, value);
3460 continue;
3461 kw_found:
3462 if (GETLOCAL(j) != NULL) {
3463 PyObject *kwd_str = kwd_as_string(keyword);
3464 if (kwd_str) {
3465 PyErr_Format(PyExc_TypeError,
3466 "%.200s() got multiple "
3467 "values for keyword "
3468 "argument '%.400s'",
3469 PyString_AsString(co->co_name),
3470 PyString_AsString(kwd_str));
3471 Py_DECREF(kwd_str);
3472 }
3473 goto fail;
3474 }
3475 Py_INCREF(value);
3476 SETLOCAL(j, value);
3477 }
3478 if (argcount < co->co_argcount) {
3479 int m = co->co_argcount - defcount;
3480 for (i = argcount; i < m; i++) {
3481 if (GETLOCAL(i) == NULL) {
3482 int j, given = 0;
3483 for (j = 0; j < co->co_argcount; j++)
3484 if (GETLOCAL(j))
3485 given++;
3486 PyErr_Format(PyExc_TypeError,
3487 "%.200s() takes %s %d "
3488 "argument%s (%d given)",
3489 PyString_AsString(co->co_name),
3490 ((co->co_flags & CO_VARARGS) ||
3491 defcount) ? "at least"
3492 : "exactly",
3493 m, m == 1 ? "" : "s", given);
3494 goto fail;
3495 }
3496 }
3497 if (n > m)
3498 i = n - m;
3499 else
3500 i = 0;
3501 for (; i < defcount; i++) {
3502 if (GETLOCAL(m+i) == NULL) {
3503 PyObject *def = defs[i];
3504 Py_INCREF(def);
3505 SETLOCAL(m+i, def);
3506 }
3507 }
3508 }
3509 }
3510 else if (argcount > 0 || kwcount > 0) {
3511 PyErr_Format(PyExc_TypeError,
3512 "%.200s() takes no arguments (%d given)",
3513 PyString_AsString(co->co_name),
3514 argcount + kwcount);
3515 goto fail;
3516 }
3517 /* Allocate and initialize storage for cell vars, and copy free
3518 vars into frame. This isn't too efficient right now. */
3519 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3520 int i, j, nargs, found;
3521 char *cellname, *argname;
3522 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003523
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003524 nargs = co->co_argcount;
3525 if (co->co_flags & CO_VARARGS)
3526 nargs++;
3527 if (co->co_flags & CO_VARKEYWORDS)
3528 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003529
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003530 /* Initialize each cell var, taking into account
3531 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003532
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003533 Should arrange for the compiler to put cellvars
3534 that are arguments at the beginning of the cellvars
3535 list so that we can march over it more efficiently?
3536 */
3537 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3538 cellname = PyString_AS_STRING(
3539 PyTuple_GET_ITEM(co->co_cellvars, i));
3540 found = 0;
3541 for (j = 0; j < nargs; j++) {
3542 argname = PyString_AS_STRING(
3543 PyTuple_GET_ITEM(co->co_varnames, j));
3544 if (strcmp(cellname, argname) == 0) {
3545 c = PyCell_New(GETLOCAL(j));
3546 if (c == NULL)
3547 goto fail;
3548 GETLOCAL(co->co_nlocals + i) = c;
3549 found = 1;
3550 break;
3551 }
3552 }
3553 if (found == 0) {
3554 c = PyCell_New(NULL);
3555 if (c == NULL)
3556 goto fail;
3557 SETLOCAL(co->co_nlocals + i, c);
3558 }
3559 }
3560 }
3561 if (PyTuple_GET_SIZE(co->co_freevars)) {
3562 int i;
3563 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3564 PyObject *o = PyTuple_GET_ITEM(closure, i);
3565 Py_INCREF(o);
3566 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3567 }
3568 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003569
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003570 if (co->co_flags & CO_GENERATOR) {
3571 /* Don't need to keep the reference to f_back, it will be set
3572 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003573 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003574
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003575 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003576
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003577 /* Create a new generator that owns the ready to run frame
3578 * and return that as the value. */
3579 return PyGen_New(f);
3580 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003581
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003582 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003583
Thomas Woutersae406c62007-09-19 17:27:43 +00003584fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003585
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003586 /* decref'ing the frame can cause __del__ methods to get invoked,
3587 which can call back into Python. While we're done with the
3588 current Python frame (f), the associated C stack is still in use,
3589 so recursion_depth must be boosted for the duration.
3590 */
3591 assert(tstate != NULL);
3592 ++tstate->recursion_depth;
3593 Py_DECREF(f);
3594 --tstate->recursion_depth;
3595 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003596}
3597
3598
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003599static PyObject *
3600special_lookup(PyObject *o, char *meth, PyObject **cache)
3601{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003602 PyObject *res;
3603 if (PyInstance_Check(o)) {
3604 if (!*cache)
3605 return PyObject_GetAttrString(o, meth);
3606 else
3607 return PyObject_GetAttr(o, *cache);
3608 }
3609 res = _PyObject_LookupSpecial(o, meth, cache);
3610 if (res == NULL && !PyErr_Occurred()) {
3611 PyErr_SetObject(PyExc_AttributeError, *cache);
3612 return NULL;
3613 }
3614 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003615}
3616
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003617
Benjamin Petersone18ef192009-01-20 14:21:16 +00003618static PyObject *
3619kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003620#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003621 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003622#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003623 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003624#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003625 Py_INCREF(kwd);
3626 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003627#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003628 }
3629 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003630#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003631}
3632
3633
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003634/* Implementation notes for set_exc_info() and reset_exc_info():
3635
3636- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3637 'exc_traceback'. These always travel together.
3638
3639- tstate->curexc_ZZZ is the "hot" exception that is set by
3640 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3641
3642- Once an exception is caught by an except clause, it is transferred
3643 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3644 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003645 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003646
3647- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3648
3649 Long ago, when none of this existed, there were just a few globals:
3650 one set corresponding to the "hot" exception, and one set
3651 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3652 globals; they were simply stored as sys.exc_ZZZ. For backwards
3653 compatibility, they still are!) The problem was that in code like
3654 this:
3655
3656 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003657 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003658 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003659 "do something else first"
3660 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003661
3662 if "do something else first" invoked something that raised and caught
3663 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3664 cause of subtle bugs. I fixed this by changing the semantics as
3665 follows:
3666
3667 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3668 *in that frame*.
3669
3670 - But initially, and as long as no exception is caught in a given
3671 frame, sys.exc_ZZZ will hold the last exception caught in the
3672 previous frame (or the frame before that, etc.).
3673
3674 The first bullet fixed the bug in the above example. The second
3675 bullet was for backwards compatibility: it was (and is) common to
3676 have a function that is called when an exception is caught, and to
3677 have that function access the caught exception via sys.exc_ZZZ.
3678 (Example: traceback.print_exc()).
3679
3680 At the same time I fixed the problem that sys.exc_ZZZ weren't
3681 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3682 but that's really a separate improvement.
3683
3684 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3685 variables to what they were before the current frame was called. The
3686 set_exc_info() function saves them on the frame so that
3687 reset_exc_info() can restore them. The invariant is that
3688 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3689 exception (where "catching" an exception applies only to successful
3690 except clauses); and if the current frame ever caught an exception,
3691 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3692 at the start of the current frame.
3693
3694*/
3695
Fredrik Lundh7a830892006-05-27 10:39:48 +00003696static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003697set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003698 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003699{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003700 PyFrameObject *frame = tstate->frame;
3701 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003702
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003703 assert(type != NULL);
3704 assert(frame != NULL);
3705 if (frame->f_exc_type == NULL) {
3706 assert(frame->f_exc_value == NULL);
3707 assert(frame->f_exc_traceback == NULL);
3708 /* This frame didn't catch an exception before. */
3709 /* Save previous exception of this thread in this frame. */
3710 if (tstate->exc_type == NULL) {
3711 /* XXX Why is this set to Py_None? */
3712 Py_INCREF(Py_None);
3713 tstate->exc_type = Py_None;
3714 }
3715 Py_INCREF(tstate->exc_type);
3716 Py_XINCREF(tstate->exc_value);
3717 Py_XINCREF(tstate->exc_traceback);
3718 frame->f_exc_type = tstate->exc_type;
3719 frame->f_exc_value = tstate->exc_value;
3720 frame->f_exc_traceback = tstate->exc_traceback;
3721 }
3722 /* Set new exception for this thread. */
3723 tmp_type = tstate->exc_type;
3724 tmp_value = tstate->exc_value;
3725 tmp_tb = tstate->exc_traceback;
3726 Py_INCREF(type);
3727 Py_XINCREF(value);
3728 Py_XINCREF(tb);
3729 tstate->exc_type = type;
3730 tstate->exc_value = value;
3731 tstate->exc_traceback = tb;
3732 Py_XDECREF(tmp_type);
3733 Py_XDECREF(tmp_value);
3734 Py_XDECREF(tmp_tb);
3735 /* For b/w compatibility */
3736 PySys_SetObject("exc_type", type);
3737 PySys_SetObject("exc_value", value);
3738 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003739}
3740
Fredrik Lundh7a830892006-05-27 10:39:48 +00003741static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003742reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003743{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003744 PyFrameObject *frame;
3745 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003746
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003747 /* It's a precondition that the thread state's frame caught an
3748 * exception -- verify in a debug build.
3749 */
3750 assert(tstate != NULL);
3751 frame = tstate->frame;
3752 assert(frame != NULL);
3753 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003755 /* Copy the frame's exception info back to the thread state. */
3756 tmp_type = tstate->exc_type;
3757 tmp_value = tstate->exc_value;
3758 tmp_tb = tstate->exc_traceback;
3759 Py_INCREF(frame->f_exc_type);
3760 Py_XINCREF(frame->f_exc_value);
3761 Py_XINCREF(frame->f_exc_traceback);
3762 tstate->exc_type = frame->f_exc_type;
3763 tstate->exc_value = frame->f_exc_value;
3764 tstate->exc_traceback = frame->f_exc_traceback;
3765 Py_XDECREF(tmp_type);
3766 Py_XDECREF(tmp_value);
3767 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003768
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003769 /* For b/w compatibility */
3770 PySys_SetObject("exc_type", frame->f_exc_type);
3771 PySys_SetObject("exc_value", frame->f_exc_value);
3772 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003773
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003774 /* Clear the frame's exception info. */
3775 tmp_type = frame->f_exc_type;
3776 tmp_value = frame->f_exc_value;
3777 tmp_tb = frame->f_exc_traceback;
3778 frame->f_exc_type = NULL;
3779 frame->f_exc_value = NULL;
3780 frame->f_exc_traceback = NULL;
3781 Py_DECREF(tmp_type);
3782 Py_XDECREF(tmp_value);
3783 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003784}
3785
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003786/* Logic for the raise statement (too complicated for inlining).
3787 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003788static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003789do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003790{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003791 if (type == NULL) {
3792 /* Reraise */
3793 PyThreadState *tstate = PyThreadState_GET();
3794 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3795 value = tstate->exc_value;
3796 tb = tstate->exc_traceback;
3797 Py_XINCREF(type);
3798 Py_XINCREF(value);
3799 Py_XINCREF(tb);
3800 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003801
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003802 /* We support the following forms of raise:
3803 raise <class>, <classinstance>
3804 raise <class>, <argument tuple>
3805 raise <class>, None
3806 raise <class>, <argument>
3807 raise <classinstance>, None
3808 raise <string>, <object>
3809 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003810
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003811 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003812
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003813 In addition, raise <tuple>, <anything> is the same as
3814 raising the tuple's first item (and it better have one!);
3815 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003816
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003817 Finally, an optional third argument can be supplied, which
3818 gives the traceback to be substituted (useful when
3819 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003820
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003821 /* First, check the traceback argument, replacing None with
3822 NULL. */
3823 if (tb == Py_None) {
3824 Py_DECREF(tb);
3825 tb = NULL;
3826 }
3827 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3828 PyErr_SetString(PyExc_TypeError,
3829 "raise: arg 3 must be a traceback or None");
3830 goto raise_error;
3831 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003832
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003833 /* Next, replace a missing value with None */
3834 if (value == NULL) {
3835 value = Py_None;
3836 Py_INCREF(value);
3837 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003839 /* Next, repeatedly, replace a tuple exception with its first item */
3840 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3841 PyObject *tmp = type;
3842 type = PyTuple_GET_ITEM(type, 0);
3843 Py_INCREF(type);
3844 Py_DECREF(tmp);
3845 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003846
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003847 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003848 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003849 if (!PyExceptionInstance_Check(value)) {
3850 PyErr_Format(PyExc_TypeError,
3851 "calling %s() should have returned an instance of "
3852 "BaseException, not '%s'",
3853 ((PyTypeObject *)type)->tp_name,
3854 Py_TYPE(value)->tp_name);
3855 goto raise_error;
3856 }
3857 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003858 else if (PyExceptionInstance_Check(type)) {
3859 /* Raising an instance. The value should be a dummy. */
3860 if (value != Py_None) {
3861 PyErr_SetString(PyExc_TypeError,
3862 "instance exception may not have a separate value");
3863 goto raise_error;
3864 }
3865 else {
3866 /* Normalize to raise <class>, <instance> */
3867 Py_DECREF(value);
3868 value = type;
3869 type = PyExceptionInstance_Class(type);
3870 Py_INCREF(type);
3871 }
3872 }
3873 else {
3874 /* Not something you can raise. You get an exception
3875 anyway, just not what you specified :-) */
3876 PyErr_Format(PyExc_TypeError,
3877 "exceptions must be old-style classes or "
3878 "derived from BaseException, not %s",
3879 type->ob_type->tp_name);
3880 goto raise_error;
3881 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003882
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003883 assert(PyExceptionClass_Check(type));
3884 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3885 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3886 "exceptions must derive from BaseException "
3887 "in 3.x", 1) < 0)
3888 goto raise_error;
3889 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003890
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003891 PyErr_Restore(type, value, tb);
3892 if (tb == NULL)
3893 return WHY_EXCEPTION;
3894 else
3895 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003896 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003897 Py_XDECREF(value);
3898 Py_XDECREF(type);
3899 Py_XDECREF(tb);
3900 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003901}
3902
Tim Petersd6d010b2001-06-21 02:49:55 +00003903/* Iterate v argcnt times and store the results on the stack (via decreasing
3904 sp). Return 1 for success, 0 if error. */
3905
Fredrik Lundh7a830892006-05-27 10:39:48 +00003906static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003907unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003908{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003909 int i = 0;
3910 PyObject *it; /* iter(v) */
3911 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003912
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003913 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003914
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003915 it = PyObject_GetIter(v);
3916 if (it == NULL)
3917 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003918
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003919 for (; i < argcnt; i++) {
3920 w = PyIter_Next(it);
3921 if (w == NULL) {
3922 /* Iterator done, via error or exhaustion. */
3923 if (!PyErr_Occurred()) {
3924 PyErr_Format(PyExc_ValueError,
3925 "need more than %d value%s to unpack",
3926 i, i == 1 ? "" : "s");
3927 }
3928 goto Error;
3929 }
3930 *--sp = w;
3931 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003932
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003933 /* We better have exhausted the iterator now. */
3934 w = PyIter_Next(it);
3935 if (w == NULL) {
3936 if (PyErr_Occurred())
3937 goto Error;
3938 Py_DECREF(it);
3939 return 1;
3940 }
3941 Py_DECREF(w);
3942 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3943 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003944Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003945 for (; i > 0; i--, sp++)
3946 Py_DECREF(*sp);
3947 Py_XDECREF(it);
3948 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003949}
3950
3951
Guido van Rossum96a42c81992-01-12 02:29:51 +00003952#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003953static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003954prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003955{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003956 printf("%s ", str);
3957 if (PyObject_Print(v, stdout, 0) != 0)
3958 PyErr_Clear(); /* Don't know what else to do */
3959 printf("\n");
3960 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003961}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003962#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003963
Fredrik Lundh7a830892006-05-27 10:39:48 +00003964static void
Fred Drake5755ce62001-06-27 19:19:46 +00003965call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003966{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003967 PyObject *type, *value, *traceback, *arg;
3968 int err;
3969 PyErr_Fetch(&type, &value, &traceback);
3970 if (value == NULL) {
3971 value = Py_None;
3972 Py_INCREF(value);
3973 }
3974 arg = PyTuple_Pack(3, type, value, traceback);
3975 if (arg == NULL) {
3976 PyErr_Restore(type, value, traceback);
3977 return;
3978 }
3979 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3980 Py_DECREF(arg);
3981 if (err == 0)
3982 PyErr_Restore(type, value, traceback);
3983 else {
3984 Py_XDECREF(type);
3985 Py_XDECREF(value);
3986 Py_XDECREF(traceback);
3987 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003988}
3989
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00003990static int
Fred Drake4ec5d562001-10-04 19:26:43 +00003991call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003992 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00003993{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003994 PyObject *type, *value, *traceback;
3995 int err;
3996 PyErr_Fetch(&type, &value, &traceback);
3997 err = call_trace(func, obj, frame, what, arg);
3998 if (err == 0)
3999 {
4000 PyErr_Restore(type, value, traceback);
4001 return 0;
4002 }
4003 else {
4004 Py_XDECREF(type);
4005 Py_XDECREF(value);
4006 Py_XDECREF(traceback);
4007 return -1;
4008 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004009}
4010
Fredrik Lundh7a830892006-05-27 10:39:48 +00004011static int
Fred Drake5755ce62001-06-27 19:19:46 +00004012call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004013 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004014{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004015 register PyThreadState *tstate = frame->f_tstate;
4016 int result;
4017 if (tstate->tracing)
4018 return 0;
4019 tstate->tracing++;
4020 tstate->use_tracing = 0;
4021 result = func(obj, frame, what, arg);
4022 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4023 || (tstate->c_profilefunc != NULL));
4024 tstate->tracing--;
4025 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004026}
4027
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004028PyObject *
4029_PyEval_CallTracing(PyObject *func, PyObject *args)
4030{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004031 PyFrameObject *frame = PyEval_GetFrame();
4032 PyThreadState *tstate = frame->f_tstate;
4033 int save_tracing = tstate->tracing;
4034 int save_use_tracing = tstate->use_tracing;
4035 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004036
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004037 tstate->tracing = 0;
4038 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4039 || (tstate->c_profilefunc != NULL));
4040 result = PyObject_Call(func, args, NULL);
4041 tstate->tracing = save_tracing;
4042 tstate->use_tracing = save_use_tracing;
4043 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004044}
4045
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00004046/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00004047static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004048maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004049 PyFrameObject *frame, int *instr_lb, int *instr_ub,
4050 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004051{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004052 int result = 0;
4053 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004054
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004055 /* If the last instruction executed isn't in the current
4056 instruction window, reset the window.
4057 */
4058 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4059 PyAddrPair bounds;
4060 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4061 &bounds);
4062 *instr_lb = bounds.ap_lower;
4063 *instr_ub = bounds.ap_upper;
4064 }
4065 /* If the last instruction falls at the start of a line or if
4066 it represents a jump backwards, update the frame's line
4067 number and call the trace function. */
4068 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4069 frame->f_lineno = line;
4070 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
4071 }
4072 *instr_prev = frame->f_lasti;
4073 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004074}
4075
Fred Drake5755ce62001-06-27 19:19:46 +00004076void
4077PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004078{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004079 PyThreadState *tstate = PyThreadState_GET();
4080 PyObject *temp = tstate->c_profileobj;
4081 Py_XINCREF(arg);
4082 tstate->c_profilefunc = NULL;
4083 tstate->c_profileobj = NULL;
4084 /* Must make sure that tracing is not ignored if 'temp' is freed */
4085 tstate->use_tracing = tstate->c_tracefunc != NULL;
4086 Py_XDECREF(temp);
4087 tstate->c_profilefunc = func;
4088 tstate->c_profileobj = arg;
4089 /* Flag that tracing or profiling is turned on */
4090 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004091}
4092
4093void
4094PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4095{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004096 PyThreadState *tstate = PyThreadState_GET();
4097 PyObject *temp = tstate->c_traceobj;
4098 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4099 Py_XINCREF(arg);
4100 tstate->c_tracefunc = NULL;
4101 tstate->c_traceobj = NULL;
4102 /* Must make sure that profiling is not ignored if 'temp' is freed */
4103 tstate->use_tracing = tstate->c_profilefunc != NULL;
4104 Py_XDECREF(temp);
4105 tstate->c_tracefunc = func;
4106 tstate->c_traceobj = arg;
4107 /* Flag that tracing or profiling is turned on */
4108 tstate->use_tracing = ((func != NULL)
4109 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004110}
4111
Guido van Rossumb209a111997-04-29 18:18:01 +00004112PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004113PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004114{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004115 PyFrameObject *current_frame = PyEval_GetFrame();
4116 if (current_frame == NULL)
4117 return PyThreadState_GET()->interp->builtins;
4118 else
4119 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004120}
4121
Guido van Rossumb209a111997-04-29 18:18:01 +00004122PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004123PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004124{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004125 PyFrameObject *current_frame = PyEval_GetFrame();
4126 if (current_frame == NULL)
4127 return NULL;
4128 PyFrame_FastToLocals(current_frame);
4129 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004130}
4131
Guido van Rossumb209a111997-04-29 18:18:01 +00004132PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004133PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004134{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004135 PyFrameObject *current_frame = PyEval_GetFrame();
4136 if (current_frame == NULL)
4137 return NULL;
4138 else
4139 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004140}
4141
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004142PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004143PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004144{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004145 PyThreadState *tstate = PyThreadState_GET();
4146 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004147}
4148
Guido van Rossum6135a871995-01-09 17:53:26 +00004149int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004150PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004151{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004152 PyFrameObject *current_frame = PyEval_GetFrame();
4153 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00004154}
4155
Guido van Rossumbe270261997-05-22 22:26:18 +00004156int
Tim Peters5ba58662001-07-16 02:29:45 +00004157PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004158{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004159 PyFrameObject *current_frame = PyEval_GetFrame();
4160 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004161
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004162 if (current_frame != NULL) {
4163 const int codeflags = current_frame->f_code->co_flags;
4164 const int compilerflags = codeflags & PyCF_MASK;
4165 if (compilerflags) {
4166 result = 1;
4167 cf->cf_flags |= compilerflags;
4168 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004169#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004170 if (codeflags & CO_GENERATOR_ALLOWED) {
4171 result = 1;
4172 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4173 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004174#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004175 }
4176 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004177}
4178
4179int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004180Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004181{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004182 PyObject *f = PySys_GetObject("stdout");
4183 if (f == NULL)
4184 return 0;
4185 if (!PyFile_SoftSpace(f, 0))
4186 return 0;
4187 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004188}
4189
Guido van Rossum3f5da241990-12-20 15:06:42 +00004190
Guido van Rossum681d79a1995-07-18 14:51:37 +00004191/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00004192 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004193
Guido van Rossumb209a111997-04-29 18:18:01 +00004194PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004195PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004196{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004197 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004198
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004199 if (arg == NULL) {
4200 arg = PyTuple_New(0);
4201 if (arg == NULL)
4202 return NULL;
4203 }
4204 else if (!PyTuple_Check(arg)) {
4205 PyErr_SetString(PyExc_TypeError,
4206 "argument list must be a tuple");
4207 return NULL;
4208 }
4209 else
4210 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004211
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004212 if (kw != NULL && !PyDict_Check(kw)) {
4213 PyErr_SetString(PyExc_TypeError,
4214 "keyword list must be a dictionary");
4215 Py_DECREF(arg);
4216 return NULL;
4217 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004219 result = PyObject_Call(func, arg, kw);
4220 Py_DECREF(arg);
4221 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004222}
4223
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004224const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004225PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004226{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004227 if (PyMethod_Check(func))
4228 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4229 else if (PyFunction_Check(func))
4230 return PyString_AsString(((PyFunctionObject*)func)->func_name);
4231 else if (PyCFunction_Check(func))
4232 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4233 else if (PyClass_Check(func))
4234 return PyString_AsString(((PyClassObject*)func)->cl_name);
4235 else if (PyInstance_Check(func)) {
4236 return PyString_AsString(
4237 ((PyInstanceObject*)func)->in_class->cl_name);
4238 } else {
4239 return func->ob_type->tp_name;
4240 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004241}
4242
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004243const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004244PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004245{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004246 if (PyMethod_Check(func))
4247 return "()";
4248 else if (PyFunction_Check(func))
4249 return "()";
4250 else if (PyCFunction_Check(func))
4251 return "()";
4252 else if (PyClass_Check(func))
4253 return " constructor";
4254 else if (PyInstance_Check(func)) {
4255 return " instance";
4256 } else {
4257 return " object";
4258 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004259}
4260
Fredrik Lundh7a830892006-05-27 10:39:48 +00004261static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004262err_args(PyObject *func, int flags, int nargs)
4263{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004264 if (flags & METH_NOARGS)
4265 PyErr_Format(PyExc_TypeError,
4266 "%.200s() takes no arguments (%d given)",
4267 ((PyCFunctionObject *)func)->m_ml->ml_name,
4268 nargs);
4269 else
4270 PyErr_Format(PyExc_TypeError,
4271 "%.200s() takes exactly one argument (%d given)",
4272 ((PyCFunctionObject *)func)->m_ml->ml_name,
4273 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004274}
4275
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004276#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004277if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004278 if (call_trace(tstate->c_profilefunc, \
4279 tstate->c_profileobj, \
4280 tstate->frame, PyTrace_C_CALL, \
4281 func)) { \
4282 x = NULL; \
4283 } \
4284 else { \
4285 x = call; \
4286 if (tstate->c_profilefunc != NULL) { \
4287 if (x == NULL) { \
4288 call_trace_protected(tstate->c_profilefunc, \
4289 tstate->c_profileobj, \
4290 tstate->frame, PyTrace_C_EXCEPTION, \
4291 func); \
4292 /* XXX should pass (type, value, tb) */ \
4293 } else { \
4294 if (call_trace(tstate->c_profilefunc, \
4295 tstate->c_profileobj, \
4296 tstate->frame, PyTrace_C_RETURN, \
4297 func)) { \
4298 Py_DECREF(x); \
4299 x = NULL; \
4300 } \
4301 } \
4302 } \
4303 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004304} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004305 x = call; \
4306 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004307
Fredrik Lundh7a830892006-05-27 10:39:48 +00004308static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004309call_function(PyObject ***pp_stack, int oparg
4310#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004311 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004312#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004313 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004314{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004315 int na = oparg & 0xff;
4316 int nk = (oparg>>8) & 0xff;
4317 int n = na + 2 * nk;
4318 PyObject **pfunc = (*pp_stack) - n - 1;
4319 PyObject *func = *pfunc;
4320 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004321
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004322 /* Always dispatch PyCFunction first, because these are
4323 presumed to be the most frequent callable object.
4324 */
4325 if (PyCFunction_Check(func) && nk == 0) {
4326 int flags = PyCFunction_GET_FLAGS(func);
4327 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004328
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004329 PCALL(PCALL_CFUNCTION);
4330 if (flags & (METH_NOARGS | METH_O)) {
4331 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4332 PyObject *self = PyCFunction_GET_SELF(func);
4333 if (flags & METH_NOARGS && na == 0) {
4334 C_TRACE(x, (*meth)(self,NULL));
4335 }
4336 else if (flags & METH_O && na == 1) {
4337 PyObject *arg = EXT_POP(*pp_stack);
4338 C_TRACE(x, (*meth)(self,arg));
4339 Py_DECREF(arg);
4340 }
4341 else {
4342 err_args(func, flags, na);
4343 x = NULL;
4344 }
4345 }
4346 else {
4347 PyObject *callargs;
4348 callargs = load_args(pp_stack, na);
4349 READ_TIMESTAMP(*pintr0);
4350 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4351 READ_TIMESTAMP(*pintr1);
4352 Py_XDECREF(callargs);
4353 }
4354 } else {
4355 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4356 /* optimize access to bound methods */
4357 PyObject *self = PyMethod_GET_SELF(func);
4358 PCALL(PCALL_METHOD);
4359 PCALL(PCALL_BOUND_METHOD);
4360 Py_INCREF(self);
4361 func = PyMethod_GET_FUNCTION(func);
4362 Py_INCREF(func);
Serhiy Storchaka5951f232015-12-24 10:35:35 +02004363 Py_SETREF(*pfunc, self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004364 na++;
4365 n++;
4366 } else
4367 Py_INCREF(func);
4368 READ_TIMESTAMP(*pintr0);
4369 if (PyFunction_Check(func))
4370 x = fast_function(func, pp_stack, n, na, nk);
4371 else
4372 x = do_call(func, pp_stack, na, nk);
4373 READ_TIMESTAMP(*pintr1);
4374 Py_DECREF(func);
4375 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004377 /* Clear the stack of the function object. Also removes
4378 the arguments in case they weren't consumed already
4379 (fast_function() and err_args() leave them on the stack).
4380 */
4381 while ((*pp_stack) > pfunc) {
4382 w = EXT_POP(*pp_stack);
4383 Py_DECREF(w);
4384 PCALL(PCALL_POP);
4385 }
4386 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004387}
4388
Jeremy Hylton192690e2002-08-16 18:36:11 +00004389/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004390 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004391 For the simplest case -- a function that takes only positional
4392 arguments and is called with only positional arguments -- it
4393 inlines the most primitive frame setup code from
4394 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4395 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004396*/
4397
Fredrik Lundh7a830892006-05-27 10:39:48 +00004398static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004399fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004400{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004401 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4402 PyObject *globals = PyFunction_GET_GLOBALS(func);
4403 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4404 PyObject **d = NULL;
4405 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004406
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004407 PCALL(PCALL_FUNCTION);
4408 PCALL(PCALL_FAST_FUNCTION);
4409 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4410 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4411 PyFrameObject *f;
4412 PyObject *retval = NULL;
4413 PyThreadState *tstate = PyThreadState_GET();
4414 PyObject **fastlocals, **stack;
4415 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004416
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004417 PCALL(PCALL_FASTER_FUNCTION);
4418 assert(globals != NULL);
4419 /* XXX Perhaps we should create a specialized
4420 PyFrame_New() that doesn't take locals, but does
4421 take builtins without sanity checking them.
4422 */
4423 assert(tstate != NULL);
4424 f = PyFrame_New(tstate, co, globals, NULL);
4425 if (f == NULL)
4426 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004427
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004428 fastlocals = f->f_localsplus;
4429 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004430
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004431 for (i = 0; i < n; i++) {
4432 Py_INCREF(*stack);
4433 fastlocals[i] = *stack++;
4434 }
4435 retval = PyEval_EvalFrameEx(f,0);
4436 ++tstate->recursion_depth;
4437 Py_DECREF(f);
4438 --tstate->recursion_depth;
4439 return retval;
4440 }
4441 if (argdefs != NULL) {
4442 d = &PyTuple_GET_ITEM(argdefs, 0);
4443 nd = Py_SIZE(argdefs);
4444 }
4445 return PyEval_EvalCodeEx(co, globals,
4446 (PyObject *)NULL, (*pp_stack)-n, na,
4447 (*pp_stack)-2*nk, nk, d, nd,
4448 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004449}
4450
Fredrik Lundh7a830892006-05-27 10:39:48 +00004451static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004452update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4453 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004454{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004455 PyObject *kwdict = NULL;
4456 if (orig_kwdict == NULL)
4457 kwdict = PyDict_New();
4458 else {
4459 kwdict = PyDict_Copy(orig_kwdict);
4460 Py_DECREF(orig_kwdict);
4461 }
4462 if (kwdict == NULL)
4463 return NULL;
4464 while (--nk >= 0) {
4465 int err;
4466 PyObject *value = EXT_POP(*pp_stack);
4467 PyObject *key = EXT_POP(*pp_stack);
4468 if (PyDict_GetItem(kwdict, key) != NULL) {
4469 PyErr_Format(PyExc_TypeError,
4470 "%.200s%s got multiple values "
4471 "for keyword argument '%.200s'",
4472 PyEval_GetFuncName(func),
4473 PyEval_GetFuncDesc(func),
4474 PyString_AsString(key));
4475 Py_DECREF(key);
4476 Py_DECREF(value);
4477 Py_DECREF(kwdict);
4478 return NULL;
4479 }
4480 err = PyDict_SetItem(kwdict, key, value);
4481 Py_DECREF(key);
4482 Py_DECREF(value);
4483 if (err) {
4484 Py_DECREF(kwdict);
4485 return NULL;
4486 }
4487 }
4488 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004489}
4490
Fredrik Lundh7a830892006-05-27 10:39:48 +00004491static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004492update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004493 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004494{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004495 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004496
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004497 callargs = PyTuple_New(nstack + nstar);
4498 if (callargs == NULL) {
4499 return NULL;
4500 }
4501 if (nstar) {
4502 int i;
4503 for (i = 0; i < nstar; i++) {
4504 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4505 Py_INCREF(a);
4506 PyTuple_SET_ITEM(callargs, nstack + i, a);
4507 }
4508 }
4509 while (--nstack >= 0) {
4510 w = EXT_POP(*pp_stack);
4511 PyTuple_SET_ITEM(callargs, nstack, w);
4512 }
4513 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004514}
4515
Fredrik Lundh7a830892006-05-27 10:39:48 +00004516static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004517load_args(PyObject ***pp_stack, int na)
4518{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004519 PyObject *args = PyTuple_New(na);
4520 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004521
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004522 if (args == NULL)
4523 return NULL;
4524 while (--na >= 0) {
4525 w = EXT_POP(*pp_stack);
4526 PyTuple_SET_ITEM(args, na, w);
4527 }
4528 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004529}
4530
Fredrik Lundh7a830892006-05-27 10:39:48 +00004531static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004532do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4533{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004534 PyObject *callargs = NULL;
4535 PyObject *kwdict = NULL;
4536 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004537
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004538 if (nk > 0) {
4539 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4540 if (kwdict == NULL)
4541 goto call_fail;
4542 }
4543 callargs = load_args(pp_stack, na);
4544 if (callargs == NULL)
4545 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004546#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004547 /* At this point, we have to look at the type of func to
4548 update the call stats properly. Do it here so as to avoid
4549 exposing the call stats machinery outside ceval.c
4550 */
4551 if (PyFunction_Check(func))
4552 PCALL(PCALL_FUNCTION);
4553 else if (PyMethod_Check(func))
4554 PCALL(PCALL_METHOD);
4555 else if (PyType_Check(func))
4556 PCALL(PCALL_TYPE);
4557 else if (PyCFunction_Check(func))
4558 PCALL(PCALL_CFUNCTION);
4559 else
4560 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004561#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004562 if (PyCFunction_Check(func)) {
4563 PyThreadState *tstate = PyThreadState_GET();
4564 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4565 }
4566 else
4567 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004568 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004569 Py_XDECREF(callargs);
4570 Py_XDECREF(kwdict);
4571 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004572}
4573
Fredrik Lundh7a830892006-05-27 10:39:48 +00004574static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004575ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4576{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004577 int nstar = 0;
4578 PyObject *callargs = NULL;
4579 PyObject *stararg = NULL;
4580 PyObject *kwdict = NULL;
4581 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004582
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004583 if (flags & CALL_FLAG_KW) {
4584 kwdict = EXT_POP(*pp_stack);
4585 if (!PyDict_Check(kwdict)) {
4586 PyObject *d;
4587 d = PyDict_New();
4588 if (d == NULL)
4589 goto ext_call_fail;
4590 if (PyDict_Update(d, kwdict) != 0) {
4591 Py_DECREF(d);
4592 /* PyDict_Update raises attribute
4593 * error (percolated from an attempt
4594 * to get 'keys' attribute) instead of
4595 * a type error if its second argument
4596 * is not a mapping.
4597 */
4598 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4599 PyErr_Format(PyExc_TypeError,
4600 "%.200s%.200s argument after ** "
4601 "must be a mapping, not %.200s",
4602 PyEval_GetFuncName(func),
4603 PyEval_GetFuncDesc(func),
4604 kwdict->ob_type->tp_name);
4605 }
4606 goto ext_call_fail;
4607 }
4608 Py_DECREF(kwdict);
4609 kwdict = d;
4610 }
4611 }
4612 if (flags & CALL_FLAG_VAR) {
4613 stararg = EXT_POP(*pp_stack);
4614 if (!PyTuple_Check(stararg)) {
4615 PyObject *t = NULL;
4616 t = PySequence_Tuple(stararg);
4617 if (t == NULL) {
4618 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
4619 PyErr_Format(PyExc_TypeError,
4620 "%.200s%.200s argument after * "
4621 "must be a sequence, not %200s",
4622 PyEval_GetFuncName(func),
4623 PyEval_GetFuncDesc(func),
4624 stararg->ob_type->tp_name);
4625 }
4626 goto ext_call_fail;
4627 }
4628 Py_DECREF(stararg);
4629 stararg = t;
4630 }
4631 nstar = PyTuple_GET_SIZE(stararg);
4632 }
4633 if (nk > 0) {
4634 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4635 if (kwdict == NULL)
4636 goto ext_call_fail;
4637 }
4638 callargs = update_star_args(na, nstar, stararg, pp_stack);
4639 if (callargs == NULL)
4640 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004641#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004642 /* At this point, we have to look at the type of func to
4643 update the call stats properly. Do it here so as to avoid
4644 exposing the call stats machinery outside ceval.c
4645 */
4646 if (PyFunction_Check(func))
4647 PCALL(PCALL_FUNCTION);
4648 else if (PyMethod_Check(func))
4649 PCALL(PCALL_METHOD);
4650 else if (PyType_Check(func))
4651 PCALL(PCALL_TYPE);
4652 else if (PyCFunction_Check(func))
4653 PCALL(PCALL_CFUNCTION);
4654 else
4655 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004656#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004657 if (PyCFunction_Check(func)) {
4658 PyThreadState *tstate = PyThreadState_GET();
4659 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4660 }
4661 else
4662 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004663ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004664 Py_XDECREF(callargs);
4665 Py_XDECREF(kwdict);
4666 Py_XDECREF(stararg);
4667 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004668}
4669
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004670/* Extract a slice index from a PyInt or PyLong or an object with the
4671 nb_index slot defined, and store in *pi.
4672 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
4673 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 +00004674 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004675*/
Tim Petersb5196382001-12-16 19:44:20 +00004676/* Note: If v is NULL, return success without storing into *pi. This
4677 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4678 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004679*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004680int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004681_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004682{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004683 if (v != NULL) {
4684 Py_ssize_t x;
4685 if (PyInt_Check(v)) {
4686 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4687 however, it looks like it should be AsSsize_t.
4688 There should be a comment here explaining why.
4689 */
4690 x = PyInt_AS_LONG(v);
4691 }
4692 else if (PyIndex_Check(v)) {
4693 x = PyNumber_AsSsize_t(v, NULL);
4694 if (x == -1 && PyErr_Occurred())
4695 return 0;
4696 }
4697 else {
4698 PyErr_SetString(PyExc_TypeError,
4699 "slice indices must be integers or "
4700 "None or have an __index__ method");
4701 return 0;
4702 }
4703 *pi = x;
4704 }
4705 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004706}
4707
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004708#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004709#define ISINDEX(x) ((x) == NULL || \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004710 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004711
Fredrik Lundh7a830892006-05-27 10:39:48 +00004712static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004713apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004714{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004715 PyTypeObject *tp = u->ob_type;
4716 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004717
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004718 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4719 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4720 if (!_PyEval_SliceIndex(v, &ilow))
4721 return NULL;
4722 if (!_PyEval_SliceIndex(w, &ihigh))
4723 return NULL;
4724 return PySequence_GetSlice(u, ilow, ihigh);
4725 }
4726 else {
4727 PyObject *slice = PySlice_New(v, w, NULL);
4728 if (slice != NULL) {
4729 PyObject *res = PyObject_GetItem(u, slice);
4730 Py_DECREF(slice);
4731 return res;
4732 }
4733 else
4734 return NULL;
4735 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004736}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004737
Fredrik Lundh7a830892006-05-27 10:39:48 +00004738static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004739assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004740 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004741{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004742 PyTypeObject *tp = u->ob_type;
4743 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004744
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004745 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4746 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4747 if (!_PyEval_SliceIndex(v, &ilow))
4748 return -1;
4749 if (!_PyEval_SliceIndex(w, &ihigh))
4750 return -1;
4751 if (x == NULL)
4752 return PySequence_DelSlice(u, ilow, ihigh);
4753 else
4754 return PySequence_SetSlice(u, ilow, ihigh, x);
4755 }
4756 else {
4757 PyObject *slice = PySlice_New(v, w, NULL);
4758 if (slice != NULL) {
4759 int res;
4760 if (x != NULL)
4761 res = PyObject_SetItem(u, slice, x);
4762 else
4763 res = PyObject_DelItem(u, slice);
4764 Py_DECREF(slice);
4765 return res;
4766 }
4767 else
4768 return -1;
4769 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004770}
4771
Guido van Rossum04edb522008-03-18 02:49:46 +00004772#define Py3kExceptionClass_Check(x) \
4773 (PyType_Check((x)) && \
4774 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4775
4776#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004777 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004778
Fredrik Lundh7a830892006-05-27 10:39:48 +00004779static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004780cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004781{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004782 int res = 0;
4783 switch (op) {
4784 case PyCmp_IS:
4785 res = (v == w);
4786 break;
4787 case PyCmp_IS_NOT:
4788 res = (v != w);
4789 break;
4790 case PyCmp_IN:
4791 res = PySequence_Contains(w, v);
4792 if (res < 0)
4793 return NULL;
4794 break;
4795 case PyCmp_NOT_IN:
4796 res = PySequence_Contains(w, v);
4797 if (res < 0)
4798 return NULL;
4799 res = !res;
4800 break;
4801 case PyCmp_EXC_MATCH:
4802 if (PyTuple_Check(w)) {
4803 Py_ssize_t i, length;
4804 length = PyTuple_Size(w);
4805 for (i = 0; i < length; i += 1) {
4806 PyObject *exc = PyTuple_GET_ITEM(w, i);
4807 if (PyString_Check(exc)) {
4808 int ret_val;
4809 ret_val = PyErr_WarnEx(
4810 PyExc_DeprecationWarning,
4811 "catching of string "
4812 "exceptions is deprecated", 1);
4813 if (ret_val < 0)
4814 return NULL;
4815 }
4816 else if (Py_Py3kWarningFlag &&
4817 !PyTuple_Check(exc) &&
4818 !Py3kExceptionClass_Check(exc))
4819 {
4820 int ret_val;
4821 ret_val = PyErr_WarnEx(
4822 PyExc_DeprecationWarning,
4823 CANNOT_CATCH_MSG, 1);
4824 if (ret_val < 0)
4825 return NULL;
4826 }
4827 }
4828 }
4829 else {
4830 if (PyString_Check(w)) {
4831 int ret_val;
4832 ret_val = PyErr_WarnEx(
4833 PyExc_DeprecationWarning,
4834 "catching of string "
4835 "exceptions is deprecated", 1);
4836 if (ret_val < 0)
4837 return NULL;
4838 }
4839 else if (Py_Py3kWarningFlag &&
4840 !PyTuple_Check(w) &&
4841 !Py3kExceptionClass_Check(w))
4842 {
4843 int ret_val;
4844 ret_val = PyErr_WarnEx(
4845 PyExc_DeprecationWarning,
4846 CANNOT_CATCH_MSG, 1);
4847 if (ret_val < 0)
4848 return NULL;
4849 }
4850 }
4851 res = PyErr_GivenExceptionMatches(v, w);
4852 break;
4853 default:
4854 return PyObject_RichCompare(v, w, op);
4855 }
4856 v = res ? Py_True : Py_False;
4857 Py_INCREF(v);
4858 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004859}
4860
Fredrik Lundh7a830892006-05-27 10:39:48 +00004861static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004862import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004863{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004864 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004865
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004866 x = PyObject_GetAttr(v, name);
4867 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4868 PyErr_Format(PyExc_ImportError,
4869 "cannot import name %.230s",
4870 PyString_AsString(name));
4871 }
4872 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004873}
Guido van Rossumac7be682001-01-17 15:42:30 +00004874
Fredrik Lundh7a830892006-05-27 10:39:48 +00004875static int
Thomas Wouters52152252000-08-17 22:55:00 +00004876import_all_from(PyObject *locals, PyObject *v)
4877{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004878 PyObject *all = PyObject_GetAttrString(v, "__all__");
4879 PyObject *dict, *name, *value;
4880 int skip_leading_underscores = 0;
4881 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004882
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004883 if (all == NULL) {
4884 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4885 return -1; /* Unexpected error */
4886 PyErr_Clear();
4887 dict = PyObject_GetAttrString(v, "__dict__");
4888 if (dict == NULL) {
4889 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4890 return -1;
4891 PyErr_SetString(PyExc_ImportError,
4892 "from-import-* object has no __dict__ and no __all__");
4893 return -1;
4894 }
4895 all = PyMapping_Keys(dict);
4896 Py_DECREF(dict);
4897 if (all == NULL)
4898 return -1;
4899 skip_leading_underscores = 1;
4900 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004901
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004902 for (pos = 0, err = 0; ; pos++) {
4903 name = PySequence_GetItem(all, pos);
4904 if (name == NULL) {
4905 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4906 err = -1;
4907 else
4908 PyErr_Clear();
4909 break;
4910 }
4911 if (skip_leading_underscores &&
4912 PyString_Check(name) &&
4913 PyString_AS_STRING(name)[0] == '_')
4914 {
4915 Py_DECREF(name);
4916 continue;
4917 }
4918 value = PyObject_GetAttr(v, name);
4919 if (value == NULL)
4920 err = -1;
4921 else if (PyDict_CheckExact(locals))
4922 err = PyDict_SetItem(locals, name, value);
4923 else
4924 err = PyObject_SetItem(locals, name, value);
4925 Py_DECREF(name);
4926 Py_XDECREF(value);
4927 if (err != 0)
4928 break;
4929 }
4930 Py_DECREF(all);
4931 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004932}
4933
Fredrik Lundh7a830892006-05-27 10:39:48 +00004934static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004935build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004936{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004937 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004939 if (PyDict_Check(methods))
4940 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4941 if (metaclass != NULL)
4942 Py_INCREF(metaclass);
4943 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4944 base = PyTuple_GET_ITEM(bases, 0);
4945 metaclass = PyObject_GetAttrString(base, "__class__");
4946 if (metaclass == NULL) {
4947 PyErr_Clear();
4948 metaclass = (PyObject *)base->ob_type;
4949 Py_INCREF(metaclass);
4950 }
4951 }
4952 else {
4953 PyObject *g = PyEval_GetGlobals();
4954 if (g != NULL && PyDict_Check(g))
4955 metaclass = PyDict_GetItemString(g, "__metaclass__");
4956 if (metaclass == NULL)
4957 metaclass = (PyObject *) &PyClass_Type;
4958 Py_INCREF(metaclass);
4959 }
4960 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4961 NULL);
4962 Py_DECREF(metaclass);
4963 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
4964 /* A type error here likely means that the user passed
4965 in a base that was not a class (such the random module
4966 instead of the random.random type). Help them out with
4967 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00004968
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004969 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00004970
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004971 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
4972 if (PyString_Check(pvalue)) {
4973 PyObject *newmsg;
4974 newmsg = PyString_FromFormat(
4975 "Error when calling the metaclass bases\n"
4976 " %s",
4977 PyString_AS_STRING(pvalue));
4978 if (newmsg != NULL) {
4979 Py_DECREF(pvalue);
4980 pvalue = newmsg;
4981 }
4982 }
4983 PyErr_Restore(ptype, pvalue, ptraceback);
4984 }
4985 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00004986}
4987
Fredrik Lundh7a830892006-05-27 10:39:48 +00004988static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004989exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004990 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004991{
Benjamin Petersond2903bd2014-08-09 19:39:36 -07004992 int n;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004993 PyObject *v;
4994 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00004995
Benjamin Petersond2903bd2014-08-09 19:39:36 -07004996 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
4997 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
4998 /* Backward compatibility hack */
4999 globals = PyTuple_GetItem(prog, 1);
5000 if (n == 3)
5001 locals = PyTuple_GetItem(prog, 2);
5002 prog = PyTuple_GetItem(prog, 0);
5003 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005004 if (globals == Py_None) {
5005 globals = PyEval_GetGlobals();
5006 if (locals == Py_None) {
5007 locals = PyEval_GetLocals();
5008 plain = 1;
5009 }
5010 if (!globals || !locals) {
5011 PyErr_SetString(PyExc_SystemError,
5012 "globals and locals cannot be NULL");
5013 return -1;
5014 }
5015 }
5016 else if (locals == Py_None)
5017 locals = globals;
5018 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005019#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005020 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005021#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005022 !PyCode_Check(prog) &&
5023 !PyFile_Check(prog)) {
5024 PyErr_SetString(PyExc_TypeError,
5025 "exec: arg 1 must be a string, file, or code object");
5026 return -1;
5027 }
5028 if (!PyDict_Check(globals)) {
5029 PyErr_SetString(PyExc_TypeError,
5030 "exec: arg 2 must be a dictionary or None");
5031 return -1;
5032 }
5033 if (!PyMapping_Check(locals)) {
5034 PyErr_SetString(PyExc_TypeError,
5035 "exec: arg 3 must be a mapping or None");
5036 return -1;
5037 }
5038 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
5039 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
5040 if (PyCode_Check(prog)) {
5041 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
5042 PyErr_SetString(PyExc_TypeError,
5043 "code object passed to exec may not contain free variables");
5044 return -1;
5045 }
5046 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
5047 }
5048 else if (PyFile_Check(prog)) {
5049 FILE *fp = PyFile_AsFile(prog);
5050 char *name = PyString_AsString(PyFile_Name(prog));
5051 PyCompilerFlags cf;
5052 if (name == NULL)
5053 return -1;
5054 cf.cf_flags = 0;
5055 if (PyEval_MergeCompilerFlags(&cf))
5056 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
5057 locals, &cf);
5058 else
5059 v = PyRun_File(fp, name, Py_file_input, globals,
5060 locals);
5061 }
5062 else {
5063 PyObject *tmp = NULL;
5064 char *str;
5065 PyCompilerFlags cf;
5066 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005067#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005068 if (PyUnicode_Check(prog)) {
5069 tmp = PyUnicode_AsUTF8String(prog);
5070 if (tmp == NULL)
5071 return -1;
5072 prog = tmp;
5073 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
5074 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005075#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005076 if (PyString_AsStringAndSize(prog, &str, NULL))
5077 return -1;
5078 if (PyEval_MergeCompilerFlags(&cf))
5079 v = PyRun_StringFlags(str, Py_file_input, globals,
5080 locals, &cf);
5081 else
5082 v = PyRun_String(str, Py_file_input, globals, locals);
5083 Py_XDECREF(tmp);
5084 }
5085 if (plain)
5086 PyFrame_LocalsToFast(f, 0);
5087 if (v == NULL)
5088 return -1;
5089 Py_DECREF(v);
5090 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005091}
Guido van Rossum24c13741995-02-14 09:42:43 +00005092
Fredrik Lundh7a830892006-05-27 10:39:48 +00005093static void
Paul Prescode68140d2000-08-30 20:25:01 +00005094format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
5095{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005096 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005097
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005098 if (!obj)
5099 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005100
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005101 obj_str = PyString_AsString(obj);
5102 if (!obj_str)
5103 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005104
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005105 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005106}
Guido van Rossum950361c1997-01-24 13:49:28 +00005107
Fredrik Lundh7a830892006-05-27 10:39:48 +00005108static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005109string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005110 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005111{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005112 /* This function implements 'variable += expr' when both arguments
5113 are strings. */
5114 Py_ssize_t v_len = PyString_GET_SIZE(v);
5115 Py_ssize_t w_len = PyString_GET_SIZE(w);
5116 Py_ssize_t new_len = v_len + w_len;
5117 if (new_len < 0) {
5118 PyErr_SetString(PyExc_OverflowError,
5119 "strings are too large to concat");
5120 return NULL;
5121 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00005122
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005123 if (v->ob_refcnt == 2) {
5124 /* In the common case, there are 2 references to the value
5125 * stored in 'variable' when the += is performed: one on the
5126 * value stack (in 'v') and one still stored in the
5127 * 'variable'. We try to delete the variable now to reduce
5128 * the refcnt to 1.
5129 */
5130 switch (*next_instr) {
5131 case STORE_FAST:
5132 {
5133 int oparg = PEEKARG();
5134 PyObject **fastlocals = f->f_localsplus;
5135 if (GETLOCAL(oparg) == v)
5136 SETLOCAL(oparg, NULL);
5137 break;
5138 }
5139 case STORE_DEREF:
5140 {
5141 PyObject **freevars = (f->f_localsplus +
5142 f->f_code->co_nlocals);
5143 PyObject *c = freevars[PEEKARG()];
5144 if (PyCell_GET(c) == v)
5145 PyCell_Set(c, NULL);
5146 break;
5147 }
5148 case STORE_NAME:
5149 {
5150 PyObject *names = f->f_code->co_names;
5151 PyObject *name = GETITEM(names, PEEKARG());
5152 PyObject *locals = f->f_locals;
5153 if (PyDict_CheckExact(locals) &&
5154 PyDict_GetItem(locals, name) == v) {
5155 if (PyDict_DelItem(locals, name) != 0) {
5156 PyErr_Clear();
5157 }
5158 }
5159 break;
5160 }
5161 }
5162 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005163
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005164 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
5165 /* Now we own the last reference to 'v', so we can resize it
5166 * in-place.
5167 */
5168 if (_PyString_Resize(&v, new_len) != 0) {
5169 /* XXX if _PyString_Resize() fails, 'v' has been
5170 * deallocated so it cannot be put back into
5171 * 'variable'. The MemoryError is raised when there
5172 * is no value in 'variable', which might (very
5173 * remotely) be a cause of incompatibilities.
5174 */
5175 return NULL;
5176 }
5177 /* copy 'w' into the newly allocated area of 'v' */
5178 memcpy(PyString_AS_STRING(v) + v_len,
5179 PyString_AS_STRING(w), w_len);
5180 return v;
5181 }
5182 else {
5183 /* When in-place resizing is not an option. */
5184 PyString_Concat(&v, w);
5185 return v;
5186 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005187}
5188
Guido van Rossum950361c1997-01-24 13:49:28 +00005189#ifdef DYNAMIC_EXECUTION_PROFILE
5190
Fredrik Lundh7a830892006-05-27 10:39:48 +00005191static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005192getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005193{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005194 int i;
5195 PyObject *l = PyList_New(256);
5196 if (l == NULL) return NULL;
5197 for (i = 0; i < 256; i++) {
5198 PyObject *x = PyInt_FromLong(a[i]);
5199 if (x == NULL) {
5200 Py_DECREF(l);
5201 return NULL;
5202 }
5203 PyList_SetItem(l, i, x);
5204 }
5205 for (i = 0; i < 256; i++)
5206 a[i] = 0;
5207 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005208}
5209
5210PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005211_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005212{
5213#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005214 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005215#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005216 int i;
5217 PyObject *l = PyList_New(257);
5218 if (l == NULL) return NULL;
5219 for (i = 0; i < 257; i++) {
5220 PyObject *x = getarray(dxpairs[i]);
5221 if (x == NULL) {
5222 Py_DECREF(l);
5223 return NULL;
5224 }
5225 PyList_SetItem(l, i, x);
5226 }
5227 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005228#endif
5229}
5230
5231#endif