blob: bae158dc1402b12446c7a27dd453d581a4d46ed2 [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
INADA Naoki2942b902018-02-07 19:09:36 +0900692 #undef USE_COMPUTED_GOTOS
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500693#endif
694#ifdef HAVE_COMPUTED_GOTOS
695 #ifndef USE_COMPUTED_GOTOS
INADA Naoki2942b902018-02-07 19:09:36 +0900696 #if defined(__clang__) && (__clang_major__ < 5)
697 /* Computed gotos caused significant performance regression
698 * with clang < 5.0.
699 * https://bugs.python.org/issue32616
700 */
701 #define USE_COMPUTED_GOTOS 0
702 #else
703 #define USE_COMPUTED_GOTOS 1
704 #endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500705 #endif
706#else
707 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
708 #error "Computed gotos are not supported on this compiler."
709 #endif
710 #undef USE_COMPUTED_GOTOS
711 #define USE_COMPUTED_GOTOS 0
712#endif
713#if USE_COMPUTED_GOTOS
714/* Import the static jump table */
715#include "opcode_targets.h"
716
717 /* This macro is used when several opcodes defer to the same implementation
718 (e.g. SETUP_LOOP, SETUP_FINALLY) */
719#define TARGET_WITH_IMPL(op, impl) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700720 TARGET_##op: \
721 opcode = op; \
722 oparg = NEXTARG(); \
723 case op: \
724 goto impl; \
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500725
726#define TARGET_WITH_IMPL_NOARG(op, impl) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700727 TARGET_##op: \
728 opcode = op; \
729 case op: \
730 goto impl; \
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500731
732#define TARGET_NOARG(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700733 TARGET_##op: \
734 opcode = op; \
735 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500736
737#define TARGET(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700738 TARGET_##op: \
739 opcode = op; \
740 oparg = NEXTARG(); \
741 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500742
743
744#define DISPATCH() \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700745 { \
746 int _tick = _Py_Ticker - 1; \
747 _Py_Ticker = _tick; \
748 if (_tick >= 0) { \
749 FAST_DISPATCH(); \
750 } \
751 continue; \
752 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500753
754#ifdef LLTRACE
755#define FAST_DISPATCH() \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700756 { \
757 if (!lltrace && !_Py_TracingPossible) { \
758 f->f_lasti = INSTR_OFFSET(); \
759 goto *opcode_targets[*next_instr++]; \
760 } \
761 goto fast_next_opcode; \
762 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500763#else
764#define FAST_DISPATCH() { \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700765 if (!_Py_TracingPossible) { \
766 f->f_lasti = INSTR_OFFSET(); \
767 goto *opcode_targets[*next_instr++]; \
768 } \
769 goto fast_next_opcode;\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500770}
771#endif
772
773#else
774#define TARGET(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700775 case op:
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500776#define TARGET_WITH_IMPL(op, impl) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700777 /* silence compiler warnings about `impl` unused */ \
778 if (0) goto impl; \
779 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500780
781#define TARGET_NOARG(op) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700782 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500783
784#define TARGET_WITH_IMPL_NOARG(op, impl) \
Benjamin Peterson14462d42015-08-19 20:38:39 -0700785 if (0) goto impl; \
786 case op:\
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500787
788#define DISPATCH() continue
789#define FAST_DISPATCH() goto fast_next_opcode
790#endif
791
792
Guido van Rossum950361c1997-01-24 13:49:28 +0000793#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000794 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000795#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000796 register PyObject **stack_pointer; /* Next free slot in value stack */
797 register unsigned char *next_instr;
798 register int opcode; /* Current opcode */
799 register int oparg; /* Current opcode argument, if any */
800 register enum why_code why; /* Reason for block stack unwind */
801 register int err; /* Error status -- nonzero if error */
802 register PyObject *x; /* Result object -- NULL if error */
803 register PyObject *v; /* Temporary objects popped off stack */
804 register PyObject *w;
805 register PyObject *u;
806 register PyObject *t;
807 register PyObject *stream = NULL; /* for PRINT opcodes */
808 register PyObject **fastlocals, **freevars;
809 PyObject *retval = NULL; /* Return value */
810 PyThreadState *tstate = PyThreadState_GET();
811 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000812
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000813 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000814
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000815 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000816
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000817 is true when the line being executed has changed. The
818 initial values are such as to make this false the first
819 time it is tested. */
820 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000821
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000822 unsigned char *first_instr;
823 PyObject *names;
824 PyObject *consts;
Neal Norwitz5f5153e2005-10-21 04:28:38 +0000825#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000826 /* Make it easier to find out where we are with a debugger */
827 char *filename;
Guido van Rossum99bec951992-09-03 20:29:45 +0000828#endif
Guido van Rossum374a9221991-04-04 10:40:29 +0000829
Neal Norwitza81d2202002-07-14 00:27:26 +0000830/* Tuple access macros */
831
832#ifndef Py_DEBUG
833#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
834#else
835#define GETITEM(v, i) PyTuple_GetItem((v), (i))
836#endif
837
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000838#ifdef WITH_TSC
839/* Use Pentium timestamp counter to mark certain events:
840 inst0 -- beginning of switch statement for opcode dispatch
841 inst1 -- end of switch statement (may be skipped)
842 loop0 -- the top of the mainloop
Tim Peters7df5e7f2006-05-26 23:14:37 +0000843 loop1 -- place where control returns again to top of mainloop
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000844 (may be skipped)
845 intr1 -- beginning of long interruption
846 intr2 -- end of long interruption
847
848 Many opcodes call out to helper C functions. In some cases, the
849 time in those functions should be counted towards the time for the
850 opcode, but not in all cases. For example, a CALL_FUNCTION opcode
851 calls another Python function; there's no point in charge all the
852 bytecode executed by the called function to the caller.
853
854 It's hard to make a useful judgement statically. In the presence
855 of operator overloading, it's impossible to tell if a call will
856 execute new Python code or not.
857
858 It's a case-by-case judgement. I'll use intr1 for the following
859 cases:
860
861 EXEC_STMT
862 IMPORT_STAR
863 IMPORT_FROM
864 CALL_FUNCTION (and friends)
865
866 */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000867 uint64 inst0, inst1, loop0, loop1, intr0 = 0, intr1 = 0;
868 int ticked = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000869
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000870 READ_TIMESTAMP(inst0);
871 READ_TIMESTAMP(inst1);
872 READ_TIMESTAMP(loop0);
873 READ_TIMESTAMP(loop1);
Michael W. Hudson800ba232004-08-12 18:19:17 +0000874
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000875 /* shut up the compiler */
876 opcode = 0;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000877#endif
878
Guido van Rossum374a9221991-04-04 10:40:29 +0000879/* Code access macros */
880
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000881#define INSTR_OFFSET() ((int)(next_instr - first_instr))
882#define NEXTOP() (*next_instr++)
883#define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
884#define PEEKARG() ((next_instr[2]<<8) + next_instr[1])
885#define JUMPTO(x) (next_instr = first_instr + (x))
886#define JUMPBY(x) (next_instr += (x))
Guido van Rossum374a9221991-04-04 10:40:29 +0000887
Raymond Hettingerf606f872003-03-16 03:11:04 +0000888/* OpCode prediction macros
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000889 Some opcodes tend to come in pairs thus making it possible to
890 predict the second code when the first is run. For example,
891 GET_ITER is often followed by FOR_ITER. And FOR_ITER is often
892 followed by STORE_FAST or UNPACK_SEQUENCE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000893
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000894 Verifying the prediction costs a single high-speed test of a register
895 variable against a constant. If the pairing was good, then the
896 processor's own internal branch predication has a high likelihood of
897 success, resulting in a nearly zero-overhead transition to the
898 next opcode. A successful prediction saves a trip through the eval-loop
899 including its two unpredictable branches, the HAS_ARG test and the
900 switch-case. Combined with the processor's internal branch prediction,
901 a successful PREDICT has the effect of making the two opcodes run as if
902 they were a single new opcode with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000903
Raymond Hettingerafae11e2008-07-05 02:11:55 +0000904 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000905 predictions turned-on and interpret the results as if some opcodes
906 had been combined or turn-off predictions so that the opcode frequency
907 counter updates for both opcodes.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000908*/
909
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500910
Benjamin Petersoncc06dbf2015-06-01 18:24:31 -0500911#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
912#define PREDICT(op) if (0) goto PRED_##op
913#define PREDICTED(op) PRED_##op:
914#define PREDICTED_WITH_ARG(op) PRED_##op:
Raymond Hettingera7216982004-02-08 19:59:27 +0000915#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000916#define PREDICT(op) if (*next_instr == op) goto PRED_##op
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500917#define PREDICTED(op) PRED_##op: next_instr++
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500918#define PREDICTED_WITH_ARG(op) PRED_##op: oparg = PEEKARG(); next_instr += 3
919#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -0500920
Raymond Hettingerf606f872003-03-16 03:11:04 +0000921
Guido van Rossum374a9221991-04-04 10:40:29 +0000922/* Stack manipulation macros */
923
Martin v. Löwis18e16552006-02-15 17:27:45 +0000924/* The stack can grow at most MAXINT deep, as co_nlocals and
925 co_stacksize are ints. */
Stefan Krah7ff78252010-06-23 18:12:09 +0000926#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
927#define EMPTY() (STACK_LEVEL() == 0)
928#define TOP() (stack_pointer[-1])
929#define SECOND() (stack_pointer[-2])
930#define THIRD() (stack_pointer[-3])
931#define FOURTH() (stack_pointer[-4])
932#define PEEK(n) (stack_pointer[-(n)])
933#define SET_TOP(v) (stack_pointer[-1] = (v))
934#define SET_SECOND(v) (stack_pointer[-2] = (v))
935#define SET_THIRD(v) (stack_pointer[-3] = (v))
936#define SET_FOURTH(v) (stack_pointer[-4] = (v))
937#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
938#define BASIC_STACKADJ(n) (stack_pointer += n)
939#define BASIC_PUSH(v) (*stack_pointer++ = (v))
940#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000941
Guido van Rossum96a42c81992-01-12 02:29:51 +0000942#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000943#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000944 lltrace && prtrace(TOP(), "push")); \
945 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000946#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000947 BASIC_POP())
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000948#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krah7ff78252010-06-23 18:12:09 +0000949 lltrace && prtrace(TOP(), "stackadj")); \
950 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes52729ac2007-12-14 02:33:57 +0000951#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krah7ff78252010-06-23 18:12:09 +0000952 prtrace((STACK_POINTER)[-1], "ext_pop")), \
953 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000954#else
Stefan Krah7ff78252010-06-23 18:12:09 +0000955#define PUSH(v) BASIC_PUSH(v)
956#define POP() BASIC_POP()
957#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000958#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000959#endif
960
Guido van Rossum681d79a1995-07-18 14:51:37 +0000961/* Local variable macros */
962
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000963#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000964
965/* The SETLOCAL() macro must not DECREF the local variable in-place and
966 then store the new value; it must copy the old value to a temporary
967 value, then store the new value, and then DECREF the temporary value.
968 This is because it is possible that during the DECREF the frame is
969 accessed by other code (e.g. a __del__ method or gc.collect()) and the
970 variable would be pointing to already-freed memory. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000971#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krah7ff78252010-06-23 18:12:09 +0000972 GETLOCAL(i) = value; \
973 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000974
Guido van Rossuma027efa1997-05-05 20:56:21 +0000975/* Start of code */
976
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000977 if (f == NULL)
978 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000979
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000980 /* push frame */
981 if (Py_EnterRecursiveCall(""))
982 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000983
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000984 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000985
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000986 if (tstate->use_tracing) {
987 if (tstate->c_tracefunc != NULL) {
988 /* tstate->c_tracefunc, if defined, is a
989 function that will be called on *every* entry
990 to a code block. Its return value, if not
991 None, is a function that will be called at
992 the start of each executed line of code.
993 (Actually, the function must return itself
994 in order to continue tracing.) The trace
995 functions are called with three arguments:
996 a pointer to the current frame, a string
997 indicating why the function is called, and
998 an argument which depends on the situation.
999 The global trace function is also called
1000 whenever an exception is detected. */
1001 if (call_trace_protected(tstate->c_tracefunc,
1002 tstate->c_traceobj,
1003 f, PyTrace_CALL, Py_None)) {
1004 /* Trace function raised an error */
1005 goto exit_eval_frame;
1006 }
1007 }
1008 if (tstate->c_profilefunc != NULL) {
1009 /* Similar for c_profilefunc, except it needn't
1010 return itself and isn't called for "line" events */
1011 if (call_trace_protected(tstate->c_profilefunc,
1012 tstate->c_profileobj,
1013 f, PyTrace_CALL, Py_None)) {
1014 /* Profile function raised an error */
1015 goto exit_eval_frame;
1016 }
1017 }
1018 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001019
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001020 co = f->f_code;
1021 names = co->co_names;
1022 consts = co->co_consts;
1023 fastlocals = f->f_localsplus;
1024 freevars = f->f_localsplus + co->co_nlocals;
1025 first_instr = (unsigned char*) PyString_AS_STRING(co->co_code);
1026 /* An explanation is in order for the next line.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001028 f->f_lasti now refers to the index of the last instruction
1029 executed. You might think this was obvious from the name, but
1030 this wasn't always true before 2.3! PyFrame_New now sets
1031 f->f_lasti to -1 (i.e. the index *before* the first instruction)
1032 and YIELD_VALUE doesn't fiddle with f_lasti any more. So this
1033 does work. Promise.
Raymond Hettinger4bd97d42007-01-06 01:14:41 +00001034
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001035 When the PREDICT() macros are enabled, some opcode pairs follow in
1036 direct succession without updating f->f_lasti. A successful
1037 prediction effectively links the two codes together as if they
1038 were a single new opcode; accordingly,f->f_lasti will point to
1039 the first code in the pair (for instance, GET_ITER followed by
1040 FOR_ITER is effectively a single opcode and f->f_lasti will point
1041 at to the beginning of the combined pair.)
1042 */
1043 next_instr = first_instr + f->f_lasti + 1;
1044 stack_pointer = f->f_stacktop;
1045 assert(stack_pointer != NULL);
1046 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001047
Tim Peters5ca576e2001-06-18 22:08:13 +00001048#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001049 lltrace = PyDict_GetItemString(f->f_globals, "__lltrace__") != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00001050#endif
Neal Norwitz5f5153e2005-10-21 04:28:38 +00001051#if defined(Py_DEBUG) || defined(LLTRACE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001052 filename = PyString_AsString(co->co_filename);
Tim Peters5ca576e2001-06-18 22:08:13 +00001053#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001054
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001055 why = WHY_NOT;
1056 err = 0;
1057 x = Py_None; /* Not a reference, just anything non-NULL */
1058 w = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00001059
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001060 if (throwflag) { /* support for generator.throw() */
1061 why = WHY_EXCEPTION;
1062 goto on_error;
1063 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00001064
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001065 for (;;) {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001066#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001067 if (inst1 == 0) {
1068 /* Almost surely, the opcode executed a break
1069 or a continue, preventing inst1 from being set
1070 on the way out of the loop.
1071 */
1072 READ_TIMESTAMP(inst1);
1073 loop1 = inst1;
1074 }
1075 dump_tsc(opcode, ticked, inst0, inst1, loop0, loop1,
1076 intr0, intr1);
1077 ticked = 0;
1078 inst1 = 0;
1079 intr0 = 0;
1080 intr1 = 0;
1081 READ_TIMESTAMP(loop0);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001082#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001083 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1084 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001085
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001086 /* Do periodic things. Doing this every time through
1087 the loop would add too much overhead, so we do it
1088 only every Nth instruction. We also do it if
1089 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
1090 event needs attention (e.g. a signal handler or
1091 async I/O handler); see Py_AddPendingCall() and
1092 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001093
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001094 if (--_Py_Ticker < 0) {
1095 if (*next_instr == SETUP_FINALLY) {
1096 /* Make the last opcode before
Ezio Melottic2077b02011-03-16 12:34:31 +02001097 a try: finally: block uninterruptible. */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001098 goto fast_next_opcode;
1099 }
1100 _Py_Ticker = _Py_CheckInterval;
1101 tstate->tick_counter++;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001102#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001103 ticked = 1;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00001104#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001105 if (pendingcalls_to_do) {
1106 if (Py_MakePendingCalls() < 0) {
1107 why = WHY_EXCEPTION;
1108 goto on_error;
1109 }
1110 if (pendingcalls_to_do)
1111 /* MakePendingCalls() didn't succeed.
1112 Force early re-execution of this
1113 "periodic" code, possibly after
1114 a thread switch */
1115 _Py_Ticker = 0;
1116 }
Guido van Rossume59214e1994-08-30 08:01:59 +00001117#ifdef WITH_THREAD
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001118 if (interpreter_lock) {
1119 /* Give another thread a chance */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001120
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001121 if (PyThreadState_Swap(NULL) != tstate)
1122 Py_FatalError("ceval: tstate mix-up");
1123 PyThread_release_lock(interpreter_lock);
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001124
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001125 /* Other threads may run now */
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001126
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001127 PyThread_acquire_lock(interpreter_lock, 1);
Benjamin Peterson1c78e6d2014-06-16 22:59:07 -07001128
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001129 if (PyThreadState_Swap(tstate) != NULL)
1130 Py_FatalError("ceval: orphan tstate");
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001131
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001132 /* Check for thread interrupts */
Guido van Rossumb8b6d0c2003-06-28 21:53:52 +00001133
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001134 if (tstate->async_exc != NULL) {
1135 x = tstate->async_exc;
1136 tstate->async_exc = NULL;
1137 PyErr_SetNone(x);
1138 Py_DECREF(x);
1139 why = WHY_EXCEPTION;
1140 goto on_error;
1141 }
1142 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001143#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001144 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001145
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001146 fast_next_opcode:
1147 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001148
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001149 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001150
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001151 if (_Py_TracingPossible &&
1152 tstate->c_tracefunc != NULL && !tstate->tracing) {
1153 /* see maybe_call_line_trace
1154 for expository comments */
1155 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001156
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001157 err = maybe_call_line_trace(tstate->c_tracefunc,
1158 tstate->c_traceobj,
1159 f, &instr_lb, &instr_ub,
1160 &instr_prev);
1161 /* Reload possibly changed frame fields */
1162 JUMPTO(f->f_lasti);
1163 if (f->f_stacktop != NULL) {
1164 stack_pointer = f->f_stacktop;
1165 f->f_stacktop = NULL;
1166 }
1167 if (err) {
1168 /* trace function raised an exception */
1169 goto on_error;
1170 }
1171 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001172
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001173 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001174
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001175 opcode = NEXTOP();
1176 oparg = 0; /* allows oparg to be stored in a register because
1177 it doesn't have to be remembered across a full loop */
1178 if (HAS_ARG(opcode))
1179 oparg = NEXTARG();
Stefan Krah7ff78252010-06-23 18:12:09 +00001180 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001181#ifdef DYNAMIC_EXECUTION_PROFILE
1182#ifdef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001183 dxpairs[lastopcode][opcode]++;
1184 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001185#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001186 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001187#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001188
Guido van Rossum96a42c81992-01-12 02:29:51 +00001189#ifdef LLTRACE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001190 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001191
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001192 if (lltrace) {
1193 if (HAS_ARG(opcode)) {
1194 printf("%d: %d, %d\n",
1195 f->f_lasti, opcode, oparg);
1196 }
1197 else {
1198 printf("%d: %d\n",
1199 f->f_lasti, opcode);
1200 }
1201 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001202#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001204 /* Main switch on opcode */
1205 READ_TIMESTAMP(inst0);
Jeremy Hylton52820442001-01-03 23:52:36 +00001206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001207 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001209 /* BEWARE!
1210 It is essential that any operation that fails sets either
1211 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1212 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001214 /* case STOP_CODE: this is an error! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001215
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001216 TARGET_NOARG(NOP)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001217 {
1218 FAST_DISPATCH();
1219 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001220
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001221 TARGET(LOAD_FAST)
1222 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001223 x = GETLOCAL(oparg);
1224 if (x != NULL) {
1225 Py_INCREF(x);
1226 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001227 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001228 }
1229 format_exc_check_arg(PyExc_UnboundLocalError,
1230 UNBOUNDLOCAL_ERROR_MSG,
1231 PyTuple_GetItem(co->co_varnames, oparg));
1232 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001233 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001234
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001235 TARGET(LOAD_CONST)
1236 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001237 x = GETITEM(consts, oparg);
1238 Py_INCREF(x);
1239 PUSH(x);
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
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001243 PREDICTED_WITH_ARG(STORE_FAST);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001244 TARGET(STORE_FAST)
1245 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001246 v = POP();
1247 SETLOCAL(oparg, v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001248 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001249 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001250
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001251 TARGET_NOARG(POP_TOP)
1252 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001253 v = POP();
1254 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001255 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001256 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001257
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001258 TARGET_NOARG(ROT_TWO)
1259 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001260 v = TOP();
1261 w = SECOND();
1262 SET_TOP(w);
1263 SET_SECOND(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001264 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001265 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001266
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001267 TARGET_NOARG(ROT_THREE)
1268 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001269 v = TOP();
1270 w = SECOND();
1271 x = THIRD();
1272 SET_TOP(w);
1273 SET_SECOND(x);
1274 SET_THIRD(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001275 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001276 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001277
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001278 TARGET_NOARG(ROT_FOUR)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001279 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001280 u = TOP();
1281 v = SECOND();
1282 w = THIRD();
1283 x = FOURTH();
1284 SET_TOP(v);
1285 SET_SECOND(w);
1286 SET_THIRD(x);
1287 SET_FOURTH(u);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001288 FAST_DISPATCH();
Benjamin Peterson14462d42015-08-19 20:38:39 -07001289 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001290
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001291
1292 TARGET_NOARG(DUP_TOP)
1293 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001294 v = TOP();
1295 Py_INCREF(v);
1296 PUSH(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001297 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001298 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001299
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001300
1301 TARGET(DUP_TOPX)
1302 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001303 if (oparg == 2) {
1304 x = TOP();
1305 Py_INCREF(x);
1306 w = SECOND();
1307 Py_INCREF(w);
1308 STACKADJ(2);
1309 SET_TOP(x);
1310 SET_SECOND(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001311 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001312 } else if (oparg == 3) {
1313 x = TOP();
1314 Py_INCREF(x);
1315 w = SECOND();
1316 Py_INCREF(w);
1317 v = THIRD();
1318 Py_INCREF(v);
1319 STACKADJ(3);
1320 SET_TOP(x);
1321 SET_SECOND(w);
1322 SET_THIRD(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001323 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001324 }
1325 Py_FatalError("invalid argument to DUP_TOPX"
1326 " (bytecode corruption?)");
1327 /* Never returns, so don't bother to set why. */
1328 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001329 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001330
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001331 TARGET_NOARG(UNARY_POSITIVE)
1332 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001333 v = TOP();
1334 x = PyNumber_Positive(v);
1335 Py_DECREF(v);
1336 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001337 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001338 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001339 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001340
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001341 TARGET_NOARG( UNARY_NEGATIVE)
1342 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001343 v = TOP();
1344 x = PyNumber_Negative(v);
1345 Py_DECREF(v);
1346 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001347 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001348 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001349 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001350
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001351 TARGET_NOARG(UNARY_NOT)
1352 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001353 v = TOP();
1354 err = PyObject_IsTrue(v);
1355 Py_DECREF(v);
1356 if (err == 0) {
1357 Py_INCREF(Py_True);
1358 SET_TOP(Py_True);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001359 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001360 }
1361 else if (err > 0) {
1362 Py_INCREF(Py_False);
1363 SET_TOP(Py_False);
1364 err = 0;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001365 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001366 }
1367 STACKADJ(-1);
1368 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001369 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001370
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001371 TARGET_NOARG(UNARY_CONVERT)
1372 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001373 v = TOP();
1374 x = PyObject_Repr(v);
1375 Py_DECREF(v);
1376 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001377 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001378 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001379 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001380
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001381 TARGET_NOARG(UNARY_INVERT)
1382 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001383 v = TOP();
1384 x = PyNumber_Invert(v);
1385 Py_DECREF(v);
1386 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001387 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001388 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001389 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001390
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001391 TARGET_NOARG(BINARY_POWER)
1392 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001393 w = POP();
1394 v = TOP();
1395 x = PyNumber_Power(v, w, Py_None);
1396 Py_DECREF(v);
1397 Py_DECREF(w);
1398 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001399 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001400 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001401 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001402
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001403 TARGET_NOARG(BINARY_MULTIPLY)
1404 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001405 w = POP();
1406 v = TOP();
1407 x = PyNumber_Multiply(v, w);
1408 Py_DECREF(v);
1409 Py_DECREF(w);
1410 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001411 if(x!=NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001412 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001413 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001414
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001415 TARGET_NOARG(BINARY_DIVIDE)
1416 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001417 if (!_Py_QnewFlag) {
1418 w = POP();
1419 v = TOP();
1420 x = PyNumber_Divide(v, w);
1421 Py_DECREF(v);
1422 Py_DECREF(w);
1423 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001424 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001425 break;
1426 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001427 }
1428 /* -Qnew is in effect: fall through to BINARY_TRUE_DIVIDE */
1429 TARGET_NOARG(BINARY_TRUE_DIVIDE)
1430 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001431 w = POP();
1432 v = TOP();
1433 x = PyNumber_TrueDivide(v, w);
1434 Py_DECREF(v);
1435 Py_DECREF(w);
1436 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001437 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001438 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001439 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001440
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001441 TARGET_NOARG(BINARY_FLOOR_DIVIDE)
1442 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001443 w = POP();
1444 v = TOP();
1445 x = PyNumber_FloorDivide(v, w);
1446 Py_DECREF(v);
1447 Py_DECREF(w);
1448 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001449 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001450 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001451 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001452
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001453 TARGET_NOARG(BINARY_MODULO)
1454 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001455 w = POP();
1456 v = TOP();
Xiang Zhangb4f0e982017-03-01 14:28:14 +08001457 if (PyString_CheckExact(v)
1458 && (!PyString_Check(w) || PyString_CheckExact(w))) {
1459 /* fast path; string formatting, but not if the RHS is a str subclass
1460 (see issue28598) */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001461 x = PyString_Format(v, w);
Xiang Zhangb4f0e982017-03-01 14:28:14 +08001462 } else {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001463 x = PyNumber_Remainder(v, w);
Xiang Zhangb4f0e982017-03-01 14:28:14 +08001464 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001465 Py_DECREF(v);
1466 Py_DECREF(w);
1467 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001468 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001469 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001470 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001471
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001472 TARGET_NOARG(BINARY_ADD)
1473 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001474 w = POP();
1475 v = TOP();
1476 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1477 /* INLINE: int + int */
1478 register long a, b, i;
1479 a = PyInt_AS_LONG(v);
1480 b = PyInt_AS_LONG(w);
1481 /* cast to avoid undefined behaviour
1482 on overflow */
1483 i = (long)((unsigned long)a + b);
1484 if ((i^a) < 0 && (i^b) < 0)
1485 goto slow_add;
1486 x = PyInt_FromLong(i);
1487 }
1488 else if (PyString_CheckExact(v) &&
1489 PyString_CheckExact(w)) {
1490 x = string_concatenate(v, w, f, next_instr);
1491 /* string_concatenate consumed the ref to v */
1492 goto skip_decref_vx;
1493 }
1494 else {
1495 slow_add:
1496 x = PyNumber_Add(v, w);
1497 }
1498 Py_DECREF(v);
1499 skip_decref_vx:
1500 Py_DECREF(w);
1501 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001502 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001503 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001504 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001505
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001506 TARGET_NOARG(BINARY_SUBTRACT)
1507 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001508 w = POP();
1509 v = TOP();
1510 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1511 /* INLINE: int - int */
1512 register long a, b, i;
1513 a = PyInt_AS_LONG(v);
1514 b = PyInt_AS_LONG(w);
1515 /* cast to avoid undefined behaviour
1516 on overflow */
1517 i = (long)((unsigned long)a - b);
1518 if ((i^a) < 0 && (i^~b) < 0)
1519 goto slow_sub;
1520 x = PyInt_FromLong(i);
1521 }
1522 else {
1523 slow_sub:
1524 x = PyNumber_Subtract(v, w);
1525 }
1526 Py_DECREF(v);
1527 Py_DECREF(w);
1528 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001529 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001530 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001531 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001532
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001533 TARGET_NOARG(BINARY_SUBSCR)
1534 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001535 w = POP();
1536 v = TOP();
1537 if (PyList_CheckExact(v) && PyInt_CheckExact(w)) {
1538 /* INLINE: list[int] */
1539 Py_ssize_t i = PyInt_AsSsize_t(w);
1540 if (i < 0)
1541 i += PyList_GET_SIZE(v);
1542 if (i >= 0 && i < PyList_GET_SIZE(v)) {
1543 x = PyList_GET_ITEM(v, i);
1544 Py_INCREF(x);
1545 }
1546 else
1547 goto slow_get;
1548 }
1549 else
1550 slow_get:
1551 x = PyObject_GetItem(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_LSHIFT)
1560 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001561 w = POP();
1562 v = TOP();
1563 x = PyNumber_Lshift(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_RSHIFT)
1572 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001573 w = POP();
1574 v = TOP();
1575 x = PyNumber_Rshift(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_AND)
1584 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001585 w = POP();
1586 v = TOP();
1587 x = PyNumber_And(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_XOR)
1596 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001597 w = POP();
1598 v = TOP();
1599 x = PyNumber_Xor(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 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001607 TARGET_NOARG(BINARY_OR)
1608 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001609 w = POP();
1610 v = TOP();
1611 x = PyNumber_Or(v, w);
1612 Py_DECREF(v);
1613 Py_DECREF(w);
1614 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001615 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001616 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001617 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001618
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001619 TARGET(LIST_APPEND)
1620 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001621 w = POP();
1622 v = PEEK(oparg);
1623 err = PyList_Append(v, w);
1624 Py_DECREF(w);
1625 if (err == 0) {
1626 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001627 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001628 }
1629 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001630 }
Raymond Hettingerdd80f762004-03-07 07:31:06 +00001631
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001632 TARGET(SET_ADD)
1633 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001634 w = POP();
1635 v = stack_pointer[-oparg];
1636 err = PySet_Add(v, w);
1637 Py_DECREF(w);
1638 if (err == 0) {
1639 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001640 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001641 }
1642 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001643 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00001644
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001645 TARGET_NOARG(INPLACE_POWER)
1646 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001647 w = POP();
1648 v = TOP();
1649 x = PyNumber_InPlacePower(v, w, Py_None);
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_MULTIPLY)
1658 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001659 w = POP();
1660 v = TOP();
1661 x = PyNumber_InPlaceMultiply(v, w);
1662 Py_DECREF(v);
1663 Py_DECREF(w);
1664 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001665 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001666 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001667 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001668
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001669 TARGET_NOARG(INPLACE_DIVIDE)
1670 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001671 if (!_Py_QnewFlag) {
1672 w = POP();
1673 v = TOP();
1674 x = PyNumber_InPlaceDivide(v, w);
1675 Py_DECREF(v);
1676 Py_DECREF(w);
1677 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001678 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001679 break;
1680 }
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001681 }
Stefan Krah7ff78252010-06-23 18:12:09 +00001682 /* -Qnew is in effect: fall through to
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001683 INPLACE_TRUE_DIVIDE */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001684 TARGET_NOARG(INPLACE_TRUE_DIVIDE)
1685 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001686 w = POP();
1687 v = TOP();
1688 x = PyNumber_InPlaceTrueDivide(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 Rossumac7be682001-01-17 15:42:30 +00001695
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001696 TARGET_NOARG(INPLACE_FLOOR_DIVIDE)
1697 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001698 w = POP();
1699 v = TOP();
1700 x = PyNumber_InPlaceFloorDivide(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 Rossum4668b002001-08-08 05:00:18 +00001707
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001708 TARGET_NOARG(INPLACE_MODULO)
1709 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001710 w = POP();
1711 v = TOP();
1712 x = PyNumber_InPlaceRemainder(v, w);
1713 Py_DECREF(v);
1714 Py_DECREF(w);
1715 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001716 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001717 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001718 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001719
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001720 TARGET_NOARG(INPLACE_ADD)
1721 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001722 w = POP();
1723 v = TOP();
1724 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1725 /* INLINE: int + int */
1726 register long a, b, i;
1727 a = PyInt_AS_LONG(v);
1728 b = PyInt_AS_LONG(w);
1729 i = a + b;
1730 if ((i^a) < 0 && (i^b) < 0)
1731 goto slow_iadd;
1732 x = PyInt_FromLong(i);
1733 }
1734 else if (PyString_CheckExact(v) &&
1735 PyString_CheckExact(w)) {
1736 x = string_concatenate(v, w, f, next_instr);
1737 /* string_concatenate consumed the ref to v */
1738 goto skip_decref_v;
1739 }
1740 else {
1741 slow_iadd:
1742 x = PyNumber_InPlaceAdd(v, w);
1743 }
1744 Py_DECREF(v);
1745 skip_decref_v:
1746 Py_DECREF(w);
1747 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001748 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001749 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001751
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001752 TARGET_NOARG(INPLACE_SUBTRACT)
1753 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001754 w = POP();
1755 v = TOP();
1756 if (PyInt_CheckExact(v) && PyInt_CheckExact(w)) {
1757 /* INLINE: int - int */
1758 register long a, b, i;
1759 a = PyInt_AS_LONG(v);
1760 b = PyInt_AS_LONG(w);
1761 i = a - b;
1762 if ((i^a) < 0 && (i^~b) < 0)
1763 goto slow_isub;
1764 x = PyInt_FromLong(i);
1765 }
1766 else {
1767 slow_isub:
1768 x = PyNumber_InPlaceSubtract(v, w);
1769 }
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_LSHIFT)
1778 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001779 w = POP();
1780 v = TOP();
1781 x = PyNumber_InPlaceLshift(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_RSHIFT)
1790 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001791 w = POP();
1792 v = TOP();
1793 x = PyNumber_InPlaceRshift(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_AND)
1802 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001803 w = POP();
1804 v = TOP();
1805 x = PyNumber_InPlaceAnd(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_XOR)
1814 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001815 w = POP();
1816 v = TOP();
1817 x = PyNumber_InPlaceXor(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 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001824
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001825 TARGET_NOARG(INPLACE_OR)
1826 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001827 w = POP();
1828 v = TOP();
1829 x = PyNumber_InPlaceOr(v, w);
1830 Py_DECREF(v);
1831 Py_DECREF(w);
1832 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001833 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001834 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001835 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001836
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001837
1838
1839 TARGET_WITH_IMPL_NOARG(SLICE, _slice)
1840 TARGET_WITH_IMPL_NOARG(SLICE_1, _slice)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001841 TARGET_WITH_IMPL_NOARG(SLICE_2, _slice)
1842 TARGET_WITH_IMPL_NOARG(SLICE_3, _slice)
1843 _slice:
1844 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001845 if ((opcode-SLICE) & 2)
1846 w = POP();
1847 else
1848 w = NULL;
1849 if ((opcode-SLICE) & 1)
1850 v = POP();
1851 else
1852 v = NULL;
1853 u = TOP();
1854 x = apply_slice(u, v, w);
1855 Py_DECREF(u);
1856 Py_XDECREF(v);
1857 Py_XDECREF(w);
1858 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001859 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001860 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001861 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001862
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001863
1864 TARGET_WITH_IMPL_NOARG(STORE_SLICE, _store_slice)
1865 TARGET_WITH_IMPL_NOARG(STORE_SLICE_1, _store_slice)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001866 TARGET_WITH_IMPL_NOARG(STORE_SLICE_2, _store_slice)
1867 TARGET_WITH_IMPL_NOARG(STORE_SLICE_3, _store_slice)
1868 _store_slice:
1869 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001870 if ((opcode-STORE_SLICE) & 2)
1871 w = POP();
1872 else
1873 w = NULL;
1874 if ((opcode-STORE_SLICE) & 1)
1875 v = POP();
1876 else
1877 v = NULL;
1878 u = POP();
1879 t = POP();
1880 err = assign_slice(u, v, w, t); /* u[v:w] = t */
1881 Py_DECREF(t);
1882 Py_DECREF(u);
1883 Py_XDECREF(v);
1884 Py_XDECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001885 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001886 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001887 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001888
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001889
1890 TARGET_WITH_IMPL_NOARG(DELETE_SLICE, _delete_slice)
1891 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_1, _delete_slice)
Benjamin Peterson14462d42015-08-19 20:38:39 -07001892 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_2, _delete_slice)
1893 TARGET_WITH_IMPL_NOARG(DELETE_SLICE_3, _delete_slice)
1894 _delete_slice:
1895 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001896 if ((opcode-DELETE_SLICE) & 2)
1897 w = POP();
1898 else
1899 w = NULL;
1900 if ((opcode-DELETE_SLICE) & 1)
1901 v = POP();
1902 else
1903 v = NULL;
1904 u = POP();
1905 err = assign_slice(u, v, w, (PyObject *)NULL);
1906 /* del u[v:w] */
1907 Py_DECREF(u);
1908 Py_XDECREF(v);
1909 Py_XDECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001910 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001911 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07001912 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001913
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001914 TARGET_NOARG(STORE_SUBSCR)
1915 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001916 w = TOP();
1917 v = SECOND();
1918 u = THIRD();
1919 STACKADJ(-3);
1920 /* v[w] = u */
1921 err = PyObject_SetItem(v, w, u);
1922 Py_DECREF(u);
1923 Py_DECREF(v);
1924 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001925 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001926 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001927 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001928
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001929 TARGET_NOARG(DELETE_SUBSCR)
1930 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001931 w = TOP();
1932 v = SECOND();
1933 STACKADJ(-2);
1934 /* del v[w] */
1935 err = PyObject_DelItem(v, w);
1936 Py_DECREF(v);
1937 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07001938 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001939 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001940 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001941
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001942 TARGET_NOARG(PRINT_EXPR)
1943 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001944 v = POP();
1945 w = PySys_GetObject("displayhook");
1946 if (w == NULL) {
1947 PyErr_SetString(PyExc_RuntimeError,
1948 "lost sys.displayhook");
1949 err = -1;
1950 x = NULL;
1951 }
1952 if (err == 0) {
1953 x = PyTuple_Pack(1, v);
1954 if (x == NULL)
1955 err = -1;
1956 }
1957 if (err == 0) {
1958 w = PyEval_CallObject(w, x);
1959 Py_XDECREF(w);
1960 if (w == NULL)
1961 err = -1;
1962 }
1963 Py_DECREF(v);
1964 Py_XDECREF(x);
1965 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001966 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001967
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001968 TARGET_NOARG(PRINT_ITEM_TO)
1969 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001970 w = stream = POP();
1971 /* fall through to PRINT_ITEM */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001972 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001973
Benjamin Peterson2c992a02015-05-28 12:45:31 -05001974 TARGET_NOARG(PRINT_ITEM)
1975 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00001976 v = POP();
1977 if (stream == NULL || stream == Py_None) {
1978 w = PySys_GetObject("stdout");
1979 if (w == NULL) {
1980 PyErr_SetString(PyExc_RuntimeError,
1981 "lost sys.stdout");
1982 err = -1;
1983 }
1984 }
1985 /* PyFile_SoftSpace() can exececute arbitrary code
1986 if sys.stdout is an instance with a __getattr__.
1987 If __getattr__ raises an exception, w will
1988 be freed, so we need to prevent that temporarily. */
1989 Py_XINCREF(w);
1990 if (w != NULL && PyFile_SoftSpace(w, 0))
1991 err = PyFile_WriteString(" ", w);
1992 if (err == 0)
1993 err = PyFile_WriteObject(v, w, Py_PRINT_RAW);
1994 if (err == 0) {
1995 /* XXX move into writeobject() ? */
1996 if (PyString_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00001997 char *s = PyString_AS_STRING(v);
1998 Py_ssize_t len = PyString_GET_SIZE(v);
1999 if (len == 0 ||
2000 !isspace(Py_CHARMASK(s[len-1])) ||
2001 s[len-1] == ' ')
2002 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002003 }
Martin v. Löwis8d3ce5a2001-12-18 22:36:40 +00002004#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002005 else if (PyUnicode_Check(v)) {
Stefan Krah7ff78252010-06-23 18:12:09 +00002006 Py_UNICODE *s = PyUnicode_AS_UNICODE(v);
2007 Py_ssize_t len = PyUnicode_GET_SIZE(v);
2008 if (len == 0 ||
2009 !Py_UNICODE_ISSPACE(s[len-1]) ||
2010 s[len-1] == ' ')
2011 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002012 }
Michael W. Hudsond95c8282002-05-20 13:56:11 +00002013#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002014 else
Stefan Krah7ff78252010-06-23 18:12:09 +00002015 PyFile_SoftSpace(w, 1);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002016 }
2017 Py_XDECREF(w);
2018 Py_DECREF(v);
2019 Py_XDECREF(stream);
2020 stream = NULL;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002021 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002022 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002023 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002024
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002025 TARGET_NOARG(PRINT_NEWLINE_TO)
2026 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002027 w = stream = POP();
2028 /* fall through to PRINT_NEWLINE */
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002029 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002030
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002031 TARGET_NOARG(PRINT_NEWLINE)
2032 {
Benjamin Peterson14462d42015-08-19 20:38:39 -07002033 if (stream == NULL || stream == Py_None)
2034 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002035 w = PySys_GetObject("stdout");
2036 if (w == NULL) {
2037 PyErr_SetString(PyExc_RuntimeError,
2038 "lost sys.stdout");
2039 why = WHY_EXCEPTION;
2040 }
2041 }
2042 if (w != NULL) {
2043 /* w.write() may replace sys.stdout, so we
2044 * have to keep our reference to it */
2045 Py_INCREF(w);
2046 err = PyFile_WriteString("\n", w);
2047 if (err == 0)
2048 PyFile_SoftSpace(w, 0);
2049 Py_DECREF(w);
2050 }
2051 Py_XDECREF(stream);
2052 stream = NULL;
2053 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002054 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002055
2056#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002057 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00002058#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002059
2060 TARGET(RAISE_VARARGS)
Benjamin Peterson14462d42015-08-19 20:38:39 -07002061 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002062 u = v = w = NULL;
2063 switch (oparg) {
2064 case 3:
2065 u = POP(); /* traceback */
2066 /* Fallthrough */
2067 case 2:
2068 v = POP(); /* value */
2069 /* Fallthrough */
2070 case 1:
2071 w = POP(); /* exc */
2072 case 0: /* Fallthrough */
2073 why = do_raise(w, v, u);
2074 break;
2075 default:
2076 PyErr_SetString(PyExc_SystemError,
2077 "bad RAISE_VARARGS oparg");
2078 why = WHY_EXCEPTION;
2079 break;
2080 }
2081 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002082 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002083
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002084 TARGET_NOARG(LOAD_LOCALS)
2085 {
Benjamin Peterson14462d42015-08-19 20:38:39 -07002086 if ((x = f->f_locals) != NULL)
2087 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002088 Py_INCREF(x);
2089 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002090 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002091 }
2092 PyErr_SetString(PyExc_SystemError, "no locals");
2093 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002094 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002095
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002096 TARGET_NOARG(RETURN_VALUE)
2097 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002098 retval = POP();
2099 why = WHY_RETURN;
2100 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002101 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002102
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002103 TARGET_NOARG(YIELD_VALUE)
2104 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002105 retval = POP();
2106 f->f_stacktop = stack_pointer;
2107 why = WHY_YIELD;
2108 goto fast_yield;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002109 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002110
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002111 TARGET_NOARG(EXEC_STMT)
2112 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002113 w = TOP();
2114 v = SECOND();
2115 u = THIRD();
2116 STACKADJ(-3);
2117 READ_TIMESTAMP(intr0);
2118 err = exec_statement(f, u, v, w);
2119 READ_TIMESTAMP(intr1);
2120 Py_DECREF(u);
2121 Py_DECREF(v);
2122 Py_DECREF(w);
2123 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002124 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002125
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002126 TARGET_NOARG(POP_BLOCK)
2127 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002128 {
2129 PyTryBlock *b = PyFrame_BlockPop(f);
2130 while (STACK_LEVEL() > b->b_level) {
2131 v = POP();
2132 Py_DECREF(v);
2133 }
2134 }
Benjamin Peterson14462d42015-08-19 20:38:39 -07002135 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002136 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002137
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002138 PREDICTED(END_FINALLY);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002139 TARGET_NOARG(END_FINALLY)
2140 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002141 v = POP();
2142 if (PyInt_Check(v)) {
2143 why = (enum why_code) PyInt_AS_LONG(v);
2144 assert(why != WHY_YIELD);
2145 if (why == WHY_RETURN ||
2146 why == WHY_CONTINUE)
2147 retval = POP();
2148 }
2149 else if (PyExceptionClass_Check(v) ||
2150 PyString_Check(v)) {
2151 w = POP();
2152 u = POP();
2153 PyErr_Restore(v, w, u);
2154 why = WHY_RERAISE;
2155 break;
2156 }
2157 else if (v != Py_None) {
2158 PyErr_SetString(PyExc_SystemError,
2159 "'finally' pops bad exception");
2160 why = WHY_EXCEPTION;
2161 }
2162 Py_DECREF(v);
2163 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002164 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002165
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002166 TARGET_NOARG(BUILD_CLASS)
2167 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002168 u = TOP();
2169 v = SECOND();
2170 w = THIRD();
2171 STACKADJ(-2);
2172 x = build_class(u, v, w);
2173 SET_TOP(x);
2174 Py_DECREF(u);
2175 Py_DECREF(v);
2176 Py_DECREF(w);
2177 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002178 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002179
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002180 TARGET(STORE_NAME)
2181 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002182 w = GETITEM(names, oparg);
2183 v = POP();
2184 if ((x = f->f_locals) != NULL) {
2185 if (PyDict_CheckExact(x))
2186 err = PyDict_SetItem(x, w, v);
2187 else
2188 err = PyObject_SetItem(x, w, v);
2189 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002190 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002191 break;
2192 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002193 t = PyObject_Repr(w);
2194 if (t == NULL)
2195 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002196 PyErr_Format(PyExc_SystemError,
2197 "no locals found when storing %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002198 PyString_AS_STRING(t));
2199 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002200 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002201 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002202
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002203 TARGET(DELETE_NAME)
2204 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002205 w = GETITEM(names, oparg);
2206 if ((x = f->f_locals) != NULL) {
2207 if ((err = PyObject_DelItem(x, w)) != 0)
2208 format_exc_check_arg(PyExc_NameError,
2209 NAME_ERROR_MSG,
2210 w);
2211 break;
2212 }
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002213 t = PyObject_Repr(w);
2214 if (t == NULL)
2215 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002216 PyErr_Format(PyExc_SystemError,
2217 "no locals when deleting %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002218 PyString_AS_STRING(w));
2219 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002220 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002221 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002222
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002223 PREDICTED_WITH_ARG(UNPACK_SEQUENCE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002224 TARGET(UNPACK_SEQUENCE)
2225 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002226 v = POP();
2227 if (PyTuple_CheckExact(v) &&
2228 PyTuple_GET_SIZE(v) == oparg) {
2229 PyObject **items = \
2230 ((PyTupleObject *)v)->ob_item;
2231 while (oparg--) {
2232 w = items[oparg];
2233 Py_INCREF(w);
2234 PUSH(w);
2235 }
2236 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002237 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002238 } else if (PyList_CheckExact(v) &&
2239 PyList_GET_SIZE(v) == oparg) {
2240 PyObject **items = \
2241 ((PyListObject *)v)->ob_item;
2242 while (oparg--) {
2243 w = items[oparg];
2244 Py_INCREF(w);
2245 PUSH(w);
2246 }
2247 } else if (unpack_iterable(v, oparg,
2248 stack_pointer + oparg)) {
2249 STACKADJ(oparg);
2250 } else {
2251 /* unpack_iterable() raised an exception */
2252 why = WHY_EXCEPTION;
2253 }
2254 Py_DECREF(v);
2255 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002256 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002257
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002258
2259 TARGET(STORE_ATTR)
2260 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002261 w = GETITEM(names, oparg);
2262 v = TOP();
2263 u = SECOND();
2264 STACKADJ(-2);
2265 err = PyObject_SetAttr(v, w, u); /* v.w = u */
2266 Py_DECREF(v);
2267 Py_DECREF(u);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002268 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002269 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002270 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002271
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002272 TARGET(DELETE_ATTR)
2273 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002274 w = GETITEM(names, oparg);
2275 v = POP();
2276 err = PyObject_SetAttr(v, w, (PyObject *)NULL);
2277 /* del v.w */
2278 Py_DECREF(v);
2279 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002280 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002281
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002282
2283 TARGET(STORE_GLOBAL)
2284 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002285 w = GETITEM(names, oparg);
2286 v = POP();
2287 err = PyDict_SetItem(f->f_globals, w, v);
2288 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002289 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002290 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002291 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002292
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002293 TARGET(DELETE_GLOBAL)
2294 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002295 w = GETITEM(names, oparg);
2296 if ((err = PyDict_DelItem(f->f_globals, w)) != 0)
2297 format_exc_check_arg(
2298 PyExc_NameError, GLOBAL_NAME_ERROR_MSG, w);
2299 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002300 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002301
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002302 TARGET(LOAD_NAME)
2303 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002304 w = GETITEM(names, oparg);
2305 if ((v = f->f_locals) == NULL) {
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002306 why = WHY_EXCEPTION;
2307 t = PyObject_Repr(w);
2308 if (t == NULL)
2309 break;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002310 PyErr_Format(PyExc_SystemError,
2311 "no locals when loading %s",
Serhiy Storchakaa86c0912014-11-19 00:11:05 +02002312 PyString_AS_STRING(w));
2313 Py_DECREF(t);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002314 break;
2315 }
2316 if (PyDict_CheckExact(v)) {
2317 x = PyDict_GetItem(v, w);
2318 Py_XINCREF(x);
2319 }
2320 else {
2321 x = PyObject_GetItem(v, w);
2322 if (x == NULL && PyErr_Occurred()) {
2323 if (!PyErr_ExceptionMatches(
2324 PyExc_KeyError))
2325 break;
2326 PyErr_Clear();
2327 }
2328 }
2329 if (x == NULL) {
2330 x = PyDict_GetItem(f->f_globals, w);
2331 if (x == NULL) {
2332 x = PyDict_GetItem(f->f_builtins, w);
2333 if (x == NULL) {
2334 format_exc_check_arg(
2335 PyExc_NameError,
2336 NAME_ERROR_MSG, w);
2337 break;
2338 }
2339 }
2340 Py_INCREF(x);
2341 }
2342 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002343 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002344 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002345
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002346 TARGET(LOAD_GLOBAL)
2347 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002348 w = GETITEM(names, oparg);
2349 if (PyString_CheckExact(w)) {
2350 /* Inline the PyDict_GetItem() calls.
2351 WARNING: this is an extreme speed hack.
2352 Do not try this at home. */
2353 long hash = ((PyStringObject *)w)->ob_shash;
2354 if (hash != -1) {
2355 PyDictObject *d;
2356 PyDictEntry *e;
2357 d = (PyDictObject *)(f->f_globals);
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 d = (PyDictObject *)(f->f_builtins);
2370 e = d->ma_lookup(d, w, hash);
2371 if (e == NULL) {
2372 x = NULL;
2373 break;
2374 }
2375 x = e->me_value;
2376 if (x != NULL) {
2377 Py_INCREF(x);
2378 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002379 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002380 }
2381 goto load_global_error;
2382 }
2383 }
2384 /* This is the un-inlined version of the code above */
2385 x = PyDict_GetItem(f->f_globals, w);
2386 if (x == NULL) {
2387 x = PyDict_GetItem(f->f_builtins, w);
2388 if (x == NULL) {
2389 load_global_error:
2390 format_exc_check_arg(
2391 PyExc_NameError,
2392 GLOBAL_NAME_ERROR_MSG, w);
2393 break;
2394 }
2395 }
2396 Py_INCREF(x);
2397 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002398 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002399 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002400
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002401 TARGET(DELETE_FAST)
2402 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002403 x = GETLOCAL(oparg);
2404 if (x != NULL) {
2405 SETLOCAL(oparg, NULL);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002406 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002407 }
2408 format_exc_check_arg(
2409 PyExc_UnboundLocalError,
2410 UNBOUNDLOCAL_ERROR_MSG,
2411 PyTuple_GetItem(co->co_varnames, oparg)
2412 );
2413 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002414 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002415
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002416 TARGET(LOAD_CLOSURE)
2417 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002418 x = freevars[oparg];
2419 Py_INCREF(x);
2420 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002421 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002422 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002423 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002424
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002425 TARGET(LOAD_DEREF)
2426 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002427 x = freevars[oparg];
2428 w = PyCell_Get(x);
2429 if (w != NULL) {
2430 PUSH(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002431 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002432 }
2433 err = -1;
2434 /* Don't stomp existing exception */
2435 if (PyErr_Occurred())
2436 break;
2437 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
2438 v = PyTuple_GET_ITEM(co->co_cellvars,
Stefan Krah7ff78252010-06-23 18:12:09 +00002439 oparg);
2440 format_exc_check_arg(
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002441 PyExc_UnboundLocalError,
2442 UNBOUNDLOCAL_ERROR_MSG,
2443 v);
2444 } else {
2445 v = PyTuple_GET_ITEM(co->co_freevars, oparg -
2446 PyTuple_GET_SIZE(co->co_cellvars));
2447 format_exc_check_arg(PyExc_NameError,
2448 UNBOUNDFREE_ERROR_MSG, v);
2449 }
2450 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002451 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002452
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002453 TARGET(STORE_DEREF)
2454 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002455 w = POP();
2456 x = freevars[oparg];
2457 PyCell_Set(x, w);
2458 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002459 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002460 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002461
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002462 TARGET(BUILD_TUPLE)
2463 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002464 x = PyTuple_New(oparg);
2465 if (x != NULL) {
2466 for (; --oparg >= 0;) {
2467 w = POP();
2468 PyTuple_SET_ITEM(x, oparg, w);
2469 }
2470 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002471 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002472 }
2473 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002474 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002475
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002476 TARGET(BUILD_LIST)
2477 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002478 x = PyList_New(oparg);
2479 if (x != NULL) {
2480 for (; --oparg >= 0;) {
2481 w = POP();
2482 PyList_SET_ITEM(x, oparg, w);
2483 }
2484 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002485 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002486 }
2487 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002488 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002489
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002490 TARGET(BUILD_SET)
2491 {
Raymond Hettingere62a6942016-09-08 15:25:19 -07002492 int i;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002493 x = PySet_New(NULL);
2494 if (x != NULL) {
Raymond Hettingere62a6942016-09-08 15:25:19 -07002495 for (i = oparg; i > 0; i--) {
2496 w = PEEK(i);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002497 if (err == 0)
2498 err = PySet_Add(x, w);
2499 Py_DECREF(w);
2500 }
Raymond Hettingere62a6942016-09-08 15:25:19 -07002501 STACKADJ(-oparg);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002502 if (err != 0) {
2503 Py_DECREF(x);
2504 break;
2505 }
2506 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002507 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002508 }
2509 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002510 }
Alexandre Vassalottiee936a22010-01-09 23:35:54 +00002511
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002512 TARGET(BUILD_MAP)
2513 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002514 x = _PyDict_NewPresized((Py_ssize_t)oparg);
2515 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002516 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002517 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002518 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002519
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002520 TARGET_NOARG(STORE_MAP)
2521 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002522 w = TOP(); /* key */
2523 u = SECOND(); /* value */
2524 v = THIRD(); /* dict */
2525 STACKADJ(-2);
2526 assert (PyDict_CheckExact(v));
2527 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2528 Py_DECREF(u);
2529 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002530 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002531 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002532 }
Raymond Hettingereffde122007-12-18 18:26:18 +00002533
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002534 TARGET(MAP_ADD)
2535 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002536 w = TOP(); /* key */
2537 u = SECOND(); /* value */
2538 STACKADJ(-2);
2539 v = stack_pointer[-oparg]; /* dict */
2540 assert (PyDict_CheckExact(v));
2541 err = PyDict_SetItem(v, w, u); /* v[w] = u */
2542 Py_DECREF(u);
2543 Py_DECREF(w);
2544 if (err == 0) {
2545 PREDICT(JUMP_ABSOLUTE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002546 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002547 }
2548 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002549 }
Alexandre Vassalottib6465472010-01-11 22:36:12 +00002550
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002551 TARGET(LOAD_ATTR)
2552 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002553 w = GETITEM(names, oparg);
2554 v = TOP();
2555 x = PyObject_GetAttr(v, w);
2556 Py_DECREF(v);
2557 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002558 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002559 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002560 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002561
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002562 TARGET(COMPARE_OP)
2563 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002564 w = POP();
2565 v = TOP();
2566 if (PyInt_CheckExact(w) && PyInt_CheckExact(v)) {
2567 /* INLINE: cmp(int, int) */
2568 register long a, b;
2569 register int res;
2570 a = PyInt_AS_LONG(v);
2571 b = PyInt_AS_LONG(w);
2572 switch (oparg) {
2573 case PyCmp_LT: res = a < b; break;
2574 case PyCmp_LE: res = a <= b; break;
2575 case PyCmp_EQ: res = a == b; break;
2576 case PyCmp_NE: res = a != b; break;
2577 case PyCmp_GT: res = a > b; break;
2578 case PyCmp_GE: res = a >= b; break;
2579 case PyCmp_IS: res = v == w; break;
2580 case PyCmp_IS_NOT: res = v != w; break;
2581 default: goto slow_compare;
2582 }
2583 x = res ? Py_True : Py_False;
2584 Py_INCREF(x);
2585 }
2586 else {
2587 slow_compare:
2588 x = cmp_outcome(oparg, v, w);
2589 }
2590 Py_DECREF(v);
2591 Py_DECREF(w);
2592 SET_TOP(x);
2593 if (x == NULL) break;
2594 PREDICT(POP_JUMP_IF_FALSE);
2595 PREDICT(POP_JUMP_IF_TRUE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002596 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002597 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002598
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002599 TARGET(IMPORT_NAME)
2600 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002601 w = GETITEM(names, oparg);
2602 x = PyDict_GetItemString(f->f_builtins, "__import__");
2603 if (x == NULL) {
2604 PyErr_SetString(PyExc_ImportError,
2605 "__import__ not found");
2606 break;
2607 }
2608 Py_INCREF(x);
2609 v = POP();
2610 u = TOP();
2611 if (PyInt_AsLong(u) != -1 || PyErr_Occurred())
2612 w = PyTuple_Pack(5,
2613 w,
2614 f->f_globals,
2615 f->f_locals == NULL ?
2616 Py_None : f->f_locals,
2617 v,
2618 u);
2619 else
2620 w = PyTuple_Pack(4,
2621 w,
2622 f->f_globals,
2623 f->f_locals == NULL ?
2624 Py_None : f->f_locals,
2625 v);
2626 Py_DECREF(v);
2627 Py_DECREF(u);
2628 if (w == NULL) {
2629 u = POP();
2630 Py_DECREF(x);
2631 x = NULL;
2632 break;
2633 }
2634 READ_TIMESTAMP(intr0);
2635 v = x;
2636 x = PyEval_CallObject(v, w);
2637 Py_DECREF(v);
2638 READ_TIMESTAMP(intr1);
2639 Py_DECREF(w);
2640 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002641 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002642 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002643 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002644
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002645 TARGET_NOARG(IMPORT_STAR)
2646 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002647 v = POP();
2648 PyFrame_FastToLocals(f);
2649 if ((x = f->f_locals) == NULL) {
2650 PyErr_SetString(PyExc_SystemError,
2651 "no locals found during 'import *'");
Serhiy Storchaka9fbb65e2017-03-08 13:44:33 +02002652 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002653 break;
2654 }
2655 READ_TIMESTAMP(intr0);
2656 err = import_all_from(x, v);
2657 READ_TIMESTAMP(intr1);
2658 PyFrame_LocalsToFast(f, 0);
2659 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002660 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002661 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002662 }
Guido van Rossum25831651993-05-19 14:50:45 +00002663
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002664 TARGET(IMPORT_FROM)
2665 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002666 w = GETITEM(names, oparg);
2667 v = TOP();
2668 READ_TIMESTAMP(intr0);
2669 x = import_from(v, w);
2670 READ_TIMESTAMP(intr1);
2671 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002672 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002673 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002674 }
Thomas Wouters52152252000-08-17 22:55:00 +00002675
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002676 TARGET(JUMP_FORWARD)
2677 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002678 JUMPBY(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002679 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002680 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002681
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002682 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002683 TARGET(POP_JUMP_IF_FALSE)
2684 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002685 w = POP();
2686 if (w == Py_True) {
2687 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002688 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002689 }
2690 if (w == Py_False) {
2691 Py_DECREF(w);
2692 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002693 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002694 }
2695 err = PyObject_IsTrue(w);
2696 Py_DECREF(w);
2697 if (err > 0)
2698 err = 0;
2699 else if (err == 0)
2700 JUMPTO(oparg);
2701 else
2702 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002703 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002704 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002705
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002706 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002707 TARGET(POP_JUMP_IF_TRUE)
2708 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002709 w = POP();
2710 if (w == Py_False) {
2711 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002712 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002713 }
2714 if (w == Py_True) {
2715 Py_DECREF(w);
2716 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002717 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002718 }
2719 err = PyObject_IsTrue(w);
2720 Py_DECREF(w);
2721 if (err > 0) {
2722 err = 0;
2723 JUMPTO(oparg);
2724 }
2725 else if (err == 0)
2726 ;
2727 else
2728 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002729 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002730 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002731
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002732 TARGET(JUMP_IF_FALSE_OR_POP)
2733 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002734 w = TOP();
2735 if (w == Py_True) {
2736 STACKADJ(-1);
2737 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002738 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002739 }
2740 if (w == Py_False) {
2741 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002742 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002743 }
2744 err = PyObject_IsTrue(w);
2745 if (err > 0) {
2746 STACKADJ(-1);
2747 Py_DECREF(w);
2748 err = 0;
2749 }
2750 else if (err == 0)
2751 JUMPTO(oparg);
2752 else
2753 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002754 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002755 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002756
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002757 TARGET(JUMP_IF_TRUE_OR_POP)
2758 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002759 w = TOP();
2760 if (w == Py_False) {
2761 STACKADJ(-1);
2762 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002763 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002764 }
2765 if (w == Py_True) {
2766 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002767 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002768 }
2769 err = PyObject_IsTrue(w);
2770 if (err > 0) {
2771 err = 0;
2772 JUMPTO(oparg);
2773 }
2774 else if (err == 0) {
2775 STACKADJ(-1);
2776 Py_DECREF(w);
2777 }
2778 else
2779 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002780 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002781 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002782
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002783 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002784 TARGET(JUMP_ABSOLUTE)
2785 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002786 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002787#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002788 /* Enabling this path speeds-up all while and for-loops by bypassing
2789 the per-loop checks for signals. By default, this should be turned-off
2790 because it prevents detection of a control-break in tight loops like
2791 "while 1: pass". Compile with this option turned-on when you need
2792 the speed-up and do not need break checking inside tight loops (ones
2793 that contain only instructions ending with goto fast_next_opcode).
2794 */
2795 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002796#else
Benjamin Peterson14462d42015-08-19 20:38:39 -07002797 DISPATCH();
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002798#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002799 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002800
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002801 TARGET_NOARG(GET_ITER)
2802 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002803 /* before: [obj]; after [getiter(obj)] */
2804 v = TOP();
2805 x = PyObject_GetIter(v);
2806 Py_DECREF(v);
2807 if (x != NULL) {
2808 SET_TOP(x);
2809 PREDICT(FOR_ITER);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002810 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002811 }
2812 STACKADJ(-1);
2813 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002814 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002815
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002816 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002817 TARGET(FOR_ITER)
2818 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002819 /* before: [iter]; after: [iter, iter()] *or* [] */
2820 v = TOP();
2821 x = (*v->ob_type->tp_iternext)(v);
2822 if (x != NULL) {
2823 PUSH(x);
2824 PREDICT(STORE_FAST);
2825 PREDICT(UNPACK_SEQUENCE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002826 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002827 }
2828 if (PyErr_Occurred()) {
2829 if (!PyErr_ExceptionMatches(
2830 PyExc_StopIteration))
2831 break;
2832 PyErr_Clear();
2833 }
2834 /* iterator ended normally */
2835 x = v = POP();
2836 Py_DECREF(v);
2837 JUMPBY(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002838 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002839 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002840
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002841 TARGET_NOARG(BREAK_LOOP)
2842 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002843 why = WHY_BREAK;
2844 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002845 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002846
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002847 TARGET(CONTINUE_LOOP)
2848 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002849 retval = PyInt_FromLong(oparg);
2850 if (!retval) {
2851 x = NULL;
2852 break;
2853 }
2854 why = WHY_CONTINUE;
2855 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002856 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002857
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002858 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2859 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
Benjamin Peterson14462d42015-08-19 20:38:39 -07002860 TARGET(SETUP_FINALLY)
2861 _setup_finally:
2862 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002863 /* NOTE: If you add any new block-setup opcodes that
2864 are not try/except/finally handlers, you may need
2865 to update the PyGen_NeedsFinalizing() function.
2866 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002867
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002868 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2869 STACK_LEVEL());
Benjamin Peterson14462d42015-08-19 20:38:39 -07002870 DISPATCH();
2871 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002872
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002873
2874
2875 TARGET(SETUP_WITH)
2876 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002877 {
2878 static PyObject *exit, *enter;
2879 w = TOP();
2880 x = special_lookup(w, "__exit__", &exit);
2881 if (!x)
2882 break;
2883 SET_TOP(x);
2884 u = special_lookup(w, "__enter__", &enter);
2885 Py_DECREF(w);
2886 if (!u) {
2887 x = NULL;
2888 break;
2889 }
2890 x = PyObject_CallFunctionObjArgs(u, NULL);
2891 Py_DECREF(u);
2892 if (!x)
2893 break;
2894 /* Setup a finally block (SETUP_WITH as a block is
2895 equivalent to SETUP_FINALLY except it normalizes
2896 the exception) before pushing the result of
2897 __enter__ on the stack. */
2898 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2899 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002900
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002901 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002902 DISPATCH();
2903 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002904 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002905
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002906 TARGET_NOARG(WITH_CLEANUP)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002907 {
2908 /* At the top of the stack are 1-3 values indicating
2909 how/why we entered the finally clause:
2910 - TOP = None
2911 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2912 - TOP = WHY_*; no retval below it
2913 - (TOP, SECOND, THIRD) = exc_info()
2914 Below them is EXIT, the context.__exit__ bound method.
2915 In the last case, we must call
2916 EXIT(TOP, SECOND, THIRD)
2917 otherwise we must call
2918 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002920 In all cases, we remove EXIT from the stack, leaving
2921 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002922
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002923 In addition, if the stack represents an exception,
2924 *and* the function call returns a 'true' value, we
2925 "zap" this information, to prevent END_FINALLY from
2926 re-raising the exception. (But non-local gotos
2927 should still be resumed.)
2928 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002929
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002930 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002931
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002932 u = POP();
2933 if (u == Py_None) {
2934 exit_func = TOP();
2935 SET_TOP(u);
2936 v = w = Py_None;
2937 }
2938 else if (PyInt_Check(u)) {
2939 switch(PyInt_AS_LONG(u)) {
2940 case WHY_RETURN:
2941 case WHY_CONTINUE:
2942 /* Retval in TOP. */
2943 exit_func = SECOND();
2944 SET_SECOND(TOP());
2945 SET_TOP(u);
2946 break;
2947 default:
2948 exit_func = TOP();
2949 SET_TOP(u);
2950 break;
2951 }
2952 u = v = w = Py_None;
2953 }
2954 else {
2955 v = TOP();
2956 w = SECOND();
2957 exit_func = THIRD();
2958 SET_TOP(u);
2959 SET_SECOND(v);
2960 SET_THIRD(w);
2961 }
2962 /* XXX Not the fastest way to call it... */
2963 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2964 NULL);
2965 Py_DECREF(exit_func);
2966 if (x == NULL)
2967 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002968
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002969 if (u != Py_None)
2970 err = PyObject_IsTrue(x);
2971 else
2972 err = 0;
2973 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002974
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002975 if (err < 0)
2976 break; /* Go to error exit */
2977 else if (err > 0) {
2978 err = 0;
2979 /* There was an exception and a true return */
2980 STACKADJ(-2);
2981 Py_INCREF(Py_None);
2982 SET_TOP(Py_None);
2983 Py_DECREF(u);
2984 Py_DECREF(v);
2985 Py_DECREF(w);
2986 } else {
2987 /* The stack was rearranged to remove EXIT
2988 above. Let END_FINALLY do its thing */
2989 }
2990 PREDICT(END_FINALLY);
2991 break;
2992 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00002993
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002994 TARGET(CALL_FUNCTION)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002995 {
2996 PyObject **sp;
2997 PCALL(PCALL_ALL);
2998 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00002999#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003000 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003001#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003002 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003003#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003004 stack_pointer = sp;
3005 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003006 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003007 break;
3008 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003009
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003010 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
3011 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
Benjamin Peterson14462d42015-08-19 20:38:39 -07003012 TARGET(CALL_FUNCTION_VAR_KW)
3013 _call_function_var_kw:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003014 {
3015 int na = oparg & 0xff;
3016 int nk = (oparg>>8) & 0xff;
3017 int flags = (opcode - CALL_FUNCTION) & 3;
3018 int n = na + 2 * nk;
3019 PyObject **pfunc, *func, **sp;
3020 PCALL(PCALL_ALL);
3021 if (flags & CALL_FLAG_VAR)
3022 n++;
3023 if (flags & CALL_FLAG_KW)
3024 n++;
3025 pfunc = stack_pointer - n - 1;
3026 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003027
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003028 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00003029 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003030 PyObject *self = PyMethod_GET_SELF(func);
3031 Py_INCREF(self);
3032 func = PyMethod_GET_FUNCTION(func);
3033 Py_INCREF(func);
3034 Py_DECREF(*pfunc);
3035 *pfunc = self;
3036 na++;
3037 } else
3038 Py_INCREF(func);
3039 sp = stack_pointer;
3040 READ_TIMESTAMP(intr0);
3041 x = ext_do_call(func, &sp, flags, na, nk);
3042 READ_TIMESTAMP(intr1);
3043 stack_pointer = sp;
3044 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003045
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003046 while (stack_pointer > pfunc) {
3047 w = POP();
3048 Py_DECREF(w);
3049 }
3050 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003051 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003052 break;
3053 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003054
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003055
3056 TARGET(MAKE_FUNCTION)
3057 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003058 v = POP(); /* code object */
3059 x = PyFunction_New(v, f->f_globals);
3060 Py_DECREF(v);
3061 /* XXX Maybe this should be a separate opcode? */
3062 if (x != NULL && oparg > 0) {
3063 v = PyTuple_New(oparg);
3064 if (v == NULL) {
3065 Py_DECREF(x);
3066 x = NULL;
3067 break;
3068 }
3069 while (--oparg >= 0) {
3070 w = POP();
3071 PyTuple_SET_ITEM(v, oparg, w);
3072 }
3073 err = PyFunction_SetDefaults(x, v);
3074 Py_DECREF(v);
3075 }
3076 PUSH(x);
3077 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003078 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003079
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003080 TARGET(MAKE_CLOSURE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003081 {
3082 v = POP(); /* code object */
3083 x = PyFunction_New(v, f->f_globals);
3084 Py_DECREF(v);
3085 if (x != NULL) {
3086 v = POP();
3087 if (PyFunction_SetClosure(x, v) != 0) {
3088 /* Can't happen unless bytecode is corrupt. */
3089 why = WHY_EXCEPTION;
3090 }
3091 Py_DECREF(v);
3092 }
3093 if (x != NULL && oparg > 0) {
3094 v = PyTuple_New(oparg);
3095 if (v == NULL) {
3096 Py_DECREF(x);
3097 x = NULL;
3098 break;
3099 }
3100 while (--oparg >= 0) {
3101 w = POP();
3102 PyTuple_SET_ITEM(v, oparg, w);
3103 }
3104 if (PyFunction_SetDefaults(x, v) != 0) {
3105 /* Can't happen unless
3106 PyFunction_SetDefaults changes. */
3107 why = WHY_EXCEPTION;
3108 }
3109 Py_DECREF(v);
3110 }
3111 PUSH(x);
3112 break;
3113 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003114
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003115 TARGET(BUILD_SLICE)
3116 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003117 if (oparg == 3)
3118 w = POP();
3119 else
3120 w = NULL;
3121 v = POP();
3122 u = TOP();
3123 x = PySlice_New(u, v, w);
3124 Py_DECREF(u);
3125 Py_DECREF(v);
3126 Py_XDECREF(w);
3127 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003128 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003129 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003130 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003131
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003132 TARGET(EXTENDED_ARG)
3133 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003134 opcode = NEXTOP();
3135 oparg = oparg<<16 | NEXTARG();
3136 goto dispatch_opcode;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003137 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003138
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003139#if USE_COMPUTED_GOTOS
3140 _unknown_opcode:
3141#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003142 default:
3143 fprintf(stderr,
3144 "XXX lineno: %d, opcode: %d\n",
3145 PyFrame_GetLineNumber(f),
3146 opcode);
3147 PyErr_SetString(PyExc_SystemError, "unknown opcode");
3148 why = WHY_EXCEPTION;
3149 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003150
3151#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003152 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003153#endif
3154
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003155 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003156
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003157 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00003158
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003159 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003160
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003161 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003162
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003163 if (why == WHY_NOT) {
3164 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003165#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003166 /* This check is expensive! */
3167 if (PyErr_Occurred())
3168 fprintf(stderr,
3169 "XXX undetected error\n");
3170 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003171#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003172 READ_TIMESTAMP(loop1);
3173 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003174#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003175 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003176#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003177 }
3178 why = WHY_EXCEPTION;
3179 x = Py_None;
3180 err = 0;
3181 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003182
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003183 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00003184
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003185 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
3186 if (!PyErr_Occurred()) {
3187 PyErr_SetString(PyExc_SystemError,
3188 "error return without exception set");
3189 why = WHY_EXCEPTION;
3190 }
3191 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00003192#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003193 else {
3194 /* This check is expensive! */
3195 if (PyErr_Occurred()) {
3196 char buf[128];
3197 sprintf(buf, "Stack unwind with exception "
3198 "set and why=%d", why);
3199 Py_FatalError(buf);
3200 }
3201 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003202#endif
3203
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003204 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00003205
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003206 if (why == WHY_EXCEPTION) {
3207 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00003208
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003209 if (tstate->c_tracefunc != NULL)
3210 call_exc_trace(tstate->c_tracefunc,
3211 tstate->c_traceobj, f);
3212 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003214 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00003215
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003216 if (why == WHY_RERAISE)
3217 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00003218
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003219 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003220
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003221fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003222 while (why != WHY_NOT && f->f_iblock > 0) {
3223 /* Peek at the current block. */
3224 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003226 assert(why != WHY_YIELD);
3227 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3228 why = WHY_NOT;
3229 JUMPTO(PyInt_AS_LONG(retval));
3230 Py_DECREF(retval);
3231 break;
3232 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003234 /* Now we have to pop the block. */
3235 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00003236
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003237 while (STACK_LEVEL() > b->b_level) {
3238 v = POP();
3239 Py_XDECREF(v);
3240 }
3241 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3242 why = WHY_NOT;
3243 JUMPTO(b->b_handler);
3244 break;
3245 }
3246 if (b->b_type == SETUP_FINALLY ||
3247 (b->b_type == SETUP_EXCEPT &&
3248 why == WHY_EXCEPTION) ||
3249 b->b_type == SETUP_WITH) {
3250 if (why == WHY_EXCEPTION) {
3251 PyObject *exc, *val, *tb;
3252 PyErr_Fetch(&exc, &val, &tb);
3253 if (val == NULL) {
3254 val = Py_None;
3255 Py_INCREF(val);
3256 }
3257 /* Make the raw exception data
3258 available to the handler,
3259 so a program can emulate the
3260 Python main loop. Don't do
3261 this for 'finally'. */
3262 if (b->b_type == SETUP_EXCEPT ||
3263 b->b_type == SETUP_WITH) {
3264 PyErr_NormalizeException(
3265 &exc, &val, &tb);
3266 set_exc_info(tstate,
3267 exc, val, tb);
3268 }
3269 if (tb == NULL) {
3270 Py_INCREF(Py_None);
3271 PUSH(Py_None);
3272 } else
3273 PUSH(tb);
3274 PUSH(val);
3275 PUSH(exc);
3276 }
3277 else {
3278 if (why & (WHY_RETURN | WHY_CONTINUE))
3279 PUSH(retval);
3280 v = PyInt_FromLong((long)why);
3281 PUSH(v);
3282 }
3283 why = WHY_NOT;
3284 JUMPTO(b->b_handler);
3285 break;
3286 }
3287 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003288
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003289 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003290
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003291 if (why != WHY_NOT)
3292 break;
3293 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003294
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003295 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003296
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003297 assert(why != WHY_YIELD);
3298 /* Pop remaining stack entries. */
3299 while (!EMPTY()) {
3300 v = POP();
3301 Py_XDECREF(v);
3302 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003303
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003304 if (why != WHY_RETURN)
3305 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003306
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003307fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003308 if (tstate->use_tracing) {
3309 if (tstate->c_tracefunc) {
3310 if (why == WHY_RETURN || why == WHY_YIELD) {
3311 if (call_trace(tstate->c_tracefunc,
3312 tstate->c_traceobj, f,
3313 PyTrace_RETURN, retval)) {
3314 Py_XDECREF(retval);
3315 retval = NULL;
3316 why = WHY_EXCEPTION;
3317 }
3318 }
3319 else if (why == WHY_EXCEPTION) {
3320 call_trace_protected(tstate->c_tracefunc,
3321 tstate->c_traceobj, f,
3322 PyTrace_RETURN, NULL);
3323 }
3324 }
3325 if (tstate->c_profilefunc) {
3326 if (why == WHY_EXCEPTION)
3327 call_trace_protected(tstate->c_profilefunc,
3328 tstate->c_profileobj, f,
3329 PyTrace_RETURN, NULL);
3330 else if (call_trace(tstate->c_profilefunc,
3331 tstate->c_profileobj, f,
3332 PyTrace_RETURN, retval)) {
3333 Py_XDECREF(retval);
3334 retval = NULL;
3335 why = WHY_EXCEPTION;
3336 }
3337 }
3338 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003339
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003340 if (tstate->frame->f_exc_type != NULL)
3341 reset_exc_info(tstate);
3342 else {
3343 assert(tstate->frame->f_exc_value == NULL);
3344 assert(tstate->frame->f_exc_traceback == NULL);
3345 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003346
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003347 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003348exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003349 Py_LeaveRecursiveCall();
3350 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003351
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003352 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003353}
3354
Guido van Rossumc2e20742006-02-27 22:32:47 +00003355/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003356 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003357 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003358
Tim Peters6d6c1a32001-08-02 04:15:00 +00003359PyObject *
3360PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003361 PyObject **args, int argcount, PyObject **kws, int kwcount,
3362 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003363{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003364 register PyFrameObject *f;
3365 register PyObject *retval = NULL;
3366 register PyObject **fastlocals, **freevars;
3367 PyThreadState *tstate = PyThreadState_GET();
3368 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003369
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003370 if (globals == NULL) {
3371 PyErr_SetString(PyExc_SystemError,
3372 "PyEval_EvalCodeEx: NULL globals");
3373 return NULL;
3374 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003375
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003376 assert(tstate != NULL);
3377 assert(globals != NULL);
3378 f = PyFrame_New(tstate, co, globals, locals);
3379 if (f == NULL)
3380 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003381
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003382 fastlocals = f->f_localsplus;
3383 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003384
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003385 if (co->co_argcount > 0 ||
3386 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3387 int i;
3388 int n = argcount;
3389 PyObject *kwdict = NULL;
3390 if (co->co_flags & CO_VARKEYWORDS) {
3391 kwdict = PyDict_New();
3392 if (kwdict == NULL)
3393 goto fail;
3394 i = co->co_argcount;
3395 if (co->co_flags & CO_VARARGS)
3396 i++;
3397 SETLOCAL(i, kwdict);
3398 }
3399 if (argcount > co->co_argcount) {
3400 if (!(co->co_flags & CO_VARARGS)) {
3401 PyErr_Format(PyExc_TypeError,
3402 "%.200s() takes %s %d "
3403 "argument%s (%d given)",
3404 PyString_AsString(co->co_name),
3405 defcount ? "at most" : "exactly",
3406 co->co_argcount,
3407 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003408 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003409 goto fail;
3410 }
3411 n = co->co_argcount;
3412 }
3413 for (i = 0; i < n; i++) {
3414 x = args[i];
3415 Py_INCREF(x);
3416 SETLOCAL(i, x);
3417 }
3418 if (co->co_flags & CO_VARARGS) {
3419 u = PyTuple_New(argcount - n);
3420 if (u == NULL)
3421 goto fail;
3422 SETLOCAL(co->co_argcount, u);
3423 for (i = n; i < argcount; i++) {
3424 x = args[i];
3425 Py_INCREF(x);
3426 PyTuple_SET_ITEM(u, i-n, x);
3427 }
3428 }
3429 for (i = 0; i < kwcount; i++) {
3430 PyObject **co_varnames;
3431 PyObject *keyword = kws[2*i];
3432 PyObject *value = kws[2*i + 1];
3433 int j;
3434 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003435#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003436 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003437#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003438 )) {
3439 PyErr_Format(PyExc_TypeError,
3440 "%.200s() keywords must be strings",
3441 PyString_AsString(co->co_name));
3442 goto fail;
3443 }
3444 /* Speed hack: do raw pointer compares. As names are
3445 normally interned this should almost always hit. */
3446 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3447 for (j = 0; j < co->co_argcount; j++) {
3448 PyObject *nm = co_varnames[j];
3449 if (nm == keyword)
3450 goto kw_found;
3451 }
3452 /* Slow fallback, just in case */
3453 for (j = 0; j < co->co_argcount; j++) {
3454 PyObject *nm = co_varnames[j];
3455 int cmp = PyObject_RichCompareBool(
3456 keyword, nm, Py_EQ);
3457 if (cmp > 0)
3458 goto kw_found;
3459 else if (cmp < 0)
3460 goto fail;
3461 }
3462 if (kwdict == NULL) {
3463 PyObject *kwd_str = kwd_as_string(keyword);
3464 if (kwd_str) {
3465 PyErr_Format(PyExc_TypeError,
3466 "%.200s() got an unexpected "
3467 "keyword argument '%.400s'",
3468 PyString_AsString(co->co_name),
3469 PyString_AsString(kwd_str));
3470 Py_DECREF(kwd_str);
3471 }
3472 goto fail;
3473 }
3474 PyDict_SetItem(kwdict, keyword, value);
3475 continue;
3476 kw_found:
3477 if (GETLOCAL(j) != NULL) {
3478 PyObject *kwd_str = kwd_as_string(keyword);
3479 if (kwd_str) {
3480 PyErr_Format(PyExc_TypeError,
3481 "%.200s() got multiple "
3482 "values for keyword "
3483 "argument '%.400s'",
3484 PyString_AsString(co->co_name),
3485 PyString_AsString(kwd_str));
3486 Py_DECREF(kwd_str);
3487 }
3488 goto fail;
3489 }
3490 Py_INCREF(value);
3491 SETLOCAL(j, value);
3492 }
3493 if (argcount < co->co_argcount) {
3494 int m = co->co_argcount - defcount;
3495 for (i = argcount; i < m; i++) {
3496 if (GETLOCAL(i) == NULL) {
3497 int j, given = 0;
3498 for (j = 0; j < co->co_argcount; j++)
3499 if (GETLOCAL(j))
3500 given++;
3501 PyErr_Format(PyExc_TypeError,
3502 "%.200s() takes %s %d "
3503 "argument%s (%d given)",
3504 PyString_AsString(co->co_name),
3505 ((co->co_flags & CO_VARARGS) ||
3506 defcount) ? "at least"
3507 : "exactly",
3508 m, m == 1 ? "" : "s", given);
3509 goto fail;
3510 }
3511 }
3512 if (n > m)
3513 i = n - m;
3514 else
3515 i = 0;
3516 for (; i < defcount; i++) {
3517 if (GETLOCAL(m+i) == NULL) {
3518 PyObject *def = defs[i];
3519 Py_INCREF(def);
3520 SETLOCAL(m+i, def);
3521 }
3522 }
3523 }
3524 }
3525 else if (argcount > 0 || kwcount > 0) {
3526 PyErr_Format(PyExc_TypeError,
3527 "%.200s() takes no arguments (%d given)",
3528 PyString_AsString(co->co_name),
3529 argcount + kwcount);
3530 goto fail;
3531 }
3532 /* Allocate and initialize storage for cell vars, and copy free
3533 vars into frame. This isn't too efficient right now. */
3534 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3535 int i, j, nargs, found;
3536 char *cellname, *argname;
3537 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003538
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003539 nargs = co->co_argcount;
3540 if (co->co_flags & CO_VARARGS)
3541 nargs++;
3542 if (co->co_flags & CO_VARKEYWORDS)
3543 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003544
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003545 /* Initialize each cell var, taking into account
3546 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003547
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003548 Should arrange for the compiler to put cellvars
3549 that are arguments at the beginning of the cellvars
3550 list so that we can march over it more efficiently?
3551 */
3552 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3553 cellname = PyString_AS_STRING(
3554 PyTuple_GET_ITEM(co->co_cellvars, i));
3555 found = 0;
3556 for (j = 0; j < nargs; j++) {
3557 argname = PyString_AS_STRING(
3558 PyTuple_GET_ITEM(co->co_varnames, j));
3559 if (strcmp(cellname, argname) == 0) {
3560 c = PyCell_New(GETLOCAL(j));
3561 if (c == NULL)
3562 goto fail;
3563 GETLOCAL(co->co_nlocals + i) = c;
3564 found = 1;
3565 break;
3566 }
3567 }
3568 if (found == 0) {
3569 c = PyCell_New(NULL);
3570 if (c == NULL)
3571 goto fail;
3572 SETLOCAL(co->co_nlocals + i, c);
3573 }
3574 }
3575 }
3576 if (PyTuple_GET_SIZE(co->co_freevars)) {
3577 int i;
3578 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3579 PyObject *o = PyTuple_GET_ITEM(closure, i);
3580 Py_INCREF(o);
3581 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3582 }
3583 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003584
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003585 if (co->co_flags & CO_GENERATOR) {
3586 /* Don't need to keep the reference to f_back, it will be set
3587 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003588 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003589
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003590 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003591
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003592 /* Create a new generator that owns the ready to run frame
3593 * and return that as the value. */
3594 return PyGen_New(f);
3595 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003596
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003597 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003598
Thomas Woutersae406c62007-09-19 17:27:43 +00003599fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003600
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003601 /* decref'ing the frame can cause __del__ methods to get invoked,
3602 which can call back into Python. While we're done with the
3603 current Python frame (f), the associated C stack is still in use,
3604 so recursion_depth must be boosted for the duration.
3605 */
3606 assert(tstate != NULL);
3607 ++tstate->recursion_depth;
3608 Py_DECREF(f);
3609 --tstate->recursion_depth;
3610 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003611}
3612
3613
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003614static PyObject *
3615special_lookup(PyObject *o, char *meth, PyObject **cache)
3616{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003617 PyObject *res;
3618 if (PyInstance_Check(o)) {
3619 if (!*cache)
3620 return PyObject_GetAttrString(o, meth);
3621 else
3622 return PyObject_GetAttr(o, *cache);
3623 }
3624 res = _PyObject_LookupSpecial(o, meth, cache);
3625 if (res == NULL && !PyErr_Occurred()) {
3626 PyErr_SetObject(PyExc_AttributeError, *cache);
3627 return NULL;
3628 }
3629 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003630}
3631
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003632
Benjamin Petersone18ef192009-01-20 14:21:16 +00003633static PyObject *
3634kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003635#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003636 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003637#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003638 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003639#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003640 Py_INCREF(kwd);
3641 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003642#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003643 }
3644 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003645#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003646}
3647
3648
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003649/* Implementation notes for set_exc_info() and reset_exc_info():
3650
3651- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3652 'exc_traceback'. These always travel together.
3653
3654- tstate->curexc_ZZZ is the "hot" exception that is set by
3655 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3656
3657- Once an exception is caught by an except clause, it is transferred
3658 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3659 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003660 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003661
3662- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3663
3664 Long ago, when none of this existed, there were just a few globals:
3665 one set corresponding to the "hot" exception, and one set
3666 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3667 globals; they were simply stored as sys.exc_ZZZ. For backwards
3668 compatibility, they still are!) The problem was that in code like
3669 this:
3670
3671 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003672 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003673 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003674 "do something else first"
3675 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003676
3677 if "do something else first" invoked something that raised and caught
3678 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3679 cause of subtle bugs. I fixed this by changing the semantics as
3680 follows:
3681
3682 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3683 *in that frame*.
3684
3685 - But initially, and as long as no exception is caught in a given
3686 frame, sys.exc_ZZZ will hold the last exception caught in the
3687 previous frame (or the frame before that, etc.).
3688
3689 The first bullet fixed the bug in the above example. The second
3690 bullet was for backwards compatibility: it was (and is) common to
3691 have a function that is called when an exception is caught, and to
3692 have that function access the caught exception via sys.exc_ZZZ.
3693 (Example: traceback.print_exc()).
3694
3695 At the same time I fixed the problem that sys.exc_ZZZ weren't
3696 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3697 but that's really a separate improvement.
3698
3699 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3700 variables to what they were before the current frame was called. The
3701 set_exc_info() function saves them on the frame so that
3702 reset_exc_info() can restore them. The invariant is that
3703 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3704 exception (where "catching" an exception applies only to successful
3705 except clauses); and if the current frame ever caught an exception,
3706 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3707 at the start of the current frame.
3708
3709*/
3710
Fredrik Lundh7a830892006-05-27 10:39:48 +00003711static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003712set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003713 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003714{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003715 PyFrameObject *frame = tstate->frame;
3716 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003717
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003718 assert(type != NULL);
3719 assert(frame != NULL);
3720 if (frame->f_exc_type == NULL) {
3721 assert(frame->f_exc_value == NULL);
3722 assert(frame->f_exc_traceback == NULL);
3723 /* This frame didn't catch an exception before. */
3724 /* Save previous exception of this thread in this frame. */
3725 if (tstate->exc_type == NULL) {
3726 /* XXX Why is this set to Py_None? */
3727 Py_INCREF(Py_None);
3728 tstate->exc_type = Py_None;
3729 }
3730 Py_INCREF(tstate->exc_type);
3731 Py_XINCREF(tstate->exc_value);
3732 Py_XINCREF(tstate->exc_traceback);
3733 frame->f_exc_type = tstate->exc_type;
3734 frame->f_exc_value = tstate->exc_value;
3735 frame->f_exc_traceback = tstate->exc_traceback;
3736 }
3737 /* Set new exception for this thread. */
3738 tmp_type = tstate->exc_type;
3739 tmp_value = tstate->exc_value;
3740 tmp_tb = tstate->exc_traceback;
3741 Py_INCREF(type);
3742 Py_XINCREF(value);
3743 Py_XINCREF(tb);
3744 tstate->exc_type = type;
3745 tstate->exc_value = value;
3746 tstate->exc_traceback = tb;
3747 Py_XDECREF(tmp_type);
3748 Py_XDECREF(tmp_value);
3749 Py_XDECREF(tmp_tb);
3750 /* For b/w compatibility */
3751 PySys_SetObject("exc_type", type);
3752 PySys_SetObject("exc_value", value);
3753 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003754}
3755
Fredrik Lundh7a830892006-05-27 10:39:48 +00003756static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003757reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003758{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003759 PyFrameObject *frame;
3760 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003761
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003762 /* It's a precondition that the thread state's frame caught an
3763 * exception -- verify in a debug build.
3764 */
3765 assert(tstate != NULL);
3766 frame = tstate->frame;
3767 assert(frame != NULL);
3768 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003769
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003770 /* Copy the frame's exception info back to the thread state. */
3771 tmp_type = tstate->exc_type;
3772 tmp_value = tstate->exc_value;
3773 tmp_tb = tstate->exc_traceback;
3774 Py_INCREF(frame->f_exc_type);
3775 Py_XINCREF(frame->f_exc_value);
3776 Py_XINCREF(frame->f_exc_traceback);
3777 tstate->exc_type = frame->f_exc_type;
3778 tstate->exc_value = frame->f_exc_value;
3779 tstate->exc_traceback = frame->f_exc_traceback;
3780 Py_XDECREF(tmp_type);
3781 Py_XDECREF(tmp_value);
3782 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003783
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003784 /* For b/w compatibility */
3785 PySys_SetObject("exc_type", frame->f_exc_type);
3786 PySys_SetObject("exc_value", frame->f_exc_value);
3787 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003788
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003789 /* Clear the frame's exception info. */
3790 tmp_type = frame->f_exc_type;
3791 tmp_value = frame->f_exc_value;
3792 tmp_tb = frame->f_exc_traceback;
3793 frame->f_exc_type = NULL;
3794 frame->f_exc_value = NULL;
3795 frame->f_exc_traceback = NULL;
3796 Py_DECREF(tmp_type);
3797 Py_XDECREF(tmp_value);
3798 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003799}
3800
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003801/* Logic for the raise statement (too complicated for inlining).
3802 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003803static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003804do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003805{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003806 if (type == NULL) {
3807 /* Reraise */
3808 PyThreadState *tstate = PyThreadState_GET();
3809 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3810 value = tstate->exc_value;
3811 tb = tstate->exc_traceback;
3812 Py_XINCREF(type);
3813 Py_XINCREF(value);
3814 Py_XINCREF(tb);
3815 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003816
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003817 /* We support the following forms of raise:
3818 raise <class>, <classinstance>
3819 raise <class>, <argument tuple>
3820 raise <class>, None
3821 raise <class>, <argument>
3822 raise <classinstance>, None
3823 raise <string>, <object>
3824 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003825
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003826 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003827
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003828 In addition, raise <tuple>, <anything> is the same as
3829 raising the tuple's first item (and it better have one!);
3830 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003831
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003832 Finally, an optional third argument can be supplied, which
3833 gives the traceback to be substituted (useful when
3834 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003835
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003836 /* First, check the traceback argument, replacing None with
3837 NULL. */
3838 if (tb == Py_None) {
3839 Py_DECREF(tb);
3840 tb = NULL;
3841 }
3842 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3843 PyErr_SetString(PyExc_TypeError,
3844 "raise: arg 3 must be a traceback or None");
3845 goto raise_error;
3846 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003847
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003848 /* Next, replace a missing value with None */
3849 if (value == NULL) {
3850 value = Py_None;
3851 Py_INCREF(value);
3852 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003853
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003854 /* Next, repeatedly, replace a tuple exception with its first item */
3855 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3856 PyObject *tmp = type;
3857 type = PyTuple_GET_ITEM(type, 0);
3858 Py_INCREF(type);
3859 Py_DECREF(tmp);
3860 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003861
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003862 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003863 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003864 if (!PyExceptionInstance_Check(value)) {
3865 PyErr_Format(PyExc_TypeError,
3866 "calling %s() should have returned an instance of "
3867 "BaseException, not '%s'",
3868 ((PyTypeObject *)type)->tp_name,
3869 Py_TYPE(value)->tp_name);
3870 goto raise_error;
3871 }
3872 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003873 else if (PyExceptionInstance_Check(type)) {
3874 /* Raising an instance. The value should be a dummy. */
3875 if (value != Py_None) {
3876 PyErr_SetString(PyExc_TypeError,
3877 "instance exception may not have a separate value");
3878 goto raise_error;
3879 }
3880 else {
3881 /* Normalize to raise <class>, <instance> */
3882 Py_DECREF(value);
3883 value = type;
3884 type = PyExceptionInstance_Class(type);
3885 Py_INCREF(type);
3886 }
3887 }
3888 else {
3889 /* Not something you can raise. You get an exception
3890 anyway, just not what you specified :-) */
3891 PyErr_Format(PyExc_TypeError,
3892 "exceptions must be old-style classes or "
3893 "derived from BaseException, not %s",
3894 type->ob_type->tp_name);
3895 goto raise_error;
3896 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003897
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003898 assert(PyExceptionClass_Check(type));
3899 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3900 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3901 "exceptions must derive from BaseException "
3902 "in 3.x", 1) < 0)
3903 goto raise_error;
3904 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003905
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003906 PyErr_Restore(type, value, tb);
3907 if (tb == NULL)
3908 return WHY_EXCEPTION;
3909 else
3910 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003911 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003912 Py_XDECREF(value);
3913 Py_XDECREF(type);
3914 Py_XDECREF(tb);
3915 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003916}
3917
Tim Petersd6d010b2001-06-21 02:49:55 +00003918/* Iterate v argcnt times and store the results on the stack (via decreasing
3919 sp). Return 1 for success, 0 if error. */
3920
Fredrik Lundh7a830892006-05-27 10:39:48 +00003921static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003922unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003923{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003924 int i = 0;
3925 PyObject *it; /* iter(v) */
3926 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003927
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003928 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003929
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003930 it = PyObject_GetIter(v);
3931 if (it == NULL)
3932 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003933
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003934 for (; i < argcnt; i++) {
3935 w = PyIter_Next(it);
3936 if (w == NULL) {
3937 /* Iterator done, via error or exhaustion. */
3938 if (!PyErr_Occurred()) {
3939 PyErr_Format(PyExc_ValueError,
3940 "need more than %d value%s to unpack",
3941 i, i == 1 ? "" : "s");
3942 }
3943 goto Error;
3944 }
3945 *--sp = w;
3946 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003947
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003948 /* We better have exhausted the iterator now. */
3949 w = PyIter_Next(it);
3950 if (w == NULL) {
3951 if (PyErr_Occurred())
3952 goto Error;
3953 Py_DECREF(it);
3954 return 1;
3955 }
3956 Py_DECREF(w);
3957 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3958 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003959Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003960 for (; i > 0; i--, sp++)
3961 Py_DECREF(*sp);
3962 Py_XDECREF(it);
3963 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003964}
3965
3966
Guido van Rossum96a42c81992-01-12 02:29:51 +00003967#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003968static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003969prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003970{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003971 printf("%s ", str);
3972 if (PyObject_Print(v, stdout, 0) != 0)
3973 PyErr_Clear(); /* Don't know what else to do */
3974 printf("\n");
3975 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003976}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003977#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003978
Fredrik Lundh7a830892006-05-27 10:39:48 +00003979static void
Fred Drake5755ce62001-06-27 19:19:46 +00003980call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003981{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003982 PyObject *type, *value, *traceback, *arg;
3983 int err;
3984 PyErr_Fetch(&type, &value, &traceback);
3985 if (value == NULL) {
3986 value = Py_None;
3987 Py_INCREF(value);
3988 }
3989 arg = PyTuple_Pack(3, type, value, traceback);
3990 if (arg == NULL) {
3991 PyErr_Restore(type, value, traceback);
3992 return;
3993 }
3994 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
3995 Py_DECREF(arg);
3996 if (err == 0)
3997 PyErr_Restore(type, value, traceback);
3998 else {
3999 Py_XDECREF(type);
4000 Py_XDECREF(value);
4001 Py_XDECREF(traceback);
4002 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004003}
4004
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00004005static int
Fred Drake4ec5d562001-10-04 19:26:43 +00004006call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004007 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004008{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004009 PyObject *type, *value, *traceback;
4010 int err;
4011 PyErr_Fetch(&type, &value, &traceback);
4012 err = call_trace(func, obj, frame, what, arg);
4013 if (err == 0)
4014 {
4015 PyErr_Restore(type, value, traceback);
4016 return 0;
4017 }
4018 else {
4019 Py_XDECREF(type);
4020 Py_XDECREF(value);
4021 Py_XDECREF(traceback);
4022 return -1;
4023 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004024}
4025
Fredrik Lundh7a830892006-05-27 10:39:48 +00004026static int
Fred Drake5755ce62001-06-27 19:19:46 +00004027call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004028 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004029{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004030 register PyThreadState *tstate = frame->f_tstate;
4031 int result;
4032 if (tstate->tracing)
4033 return 0;
4034 tstate->tracing++;
4035 tstate->use_tracing = 0;
4036 result = func(obj, frame, what, arg);
4037 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4038 || (tstate->c_profilefunc != NULL));
4039 tstate->tracing--;
4040 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004041}
4042
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004043PyObject *
4044_PyEval_CallTracing(PyObject *func, PyObject *args)
4045{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004046 PyFrameObject *frame = PyEval_GetFrame();
4047 PyThreadState *tstate = frame->f_tstate;
4048 int save_tracing = tstate->tracing;
4049 int save_use_tracing = tstate->use_tracing;
4050 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004051
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004052 tstate->tracing = 0;
4053 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4054 || (tstate->c_profilefunc != NULL));
4055 result = PyObject_Call(func, args, NULL);
4056 tstate->tracing = save_tracing;
4057 tstate->use_tracing = save_use_tracing;
4058 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004059}
4060
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00004061/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00004062static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004063maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004064 PyFrameObject *frame, int *instr_lb, int *instr_ub,
4065 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004066{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004067 int result = 0;
4068 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004069
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004070 /* If the last instruction executed isn't in the current
4071 instruction window, reset the window.
4072 */
4073 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4074 PyAddrPair bounds;
4075 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4076 &bounds);
4077 *instr_lb = bounds.ap_lower;
4078 *instr_ub = bounds.ap_upper;
4079 }
4080 /* If the last instruction falls at the start of a line or if
4081 it represents a jump backwards, update the frame's line
4082 number and call the trace function. */
4083 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4084 frame->f_lineno = line;
4085 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
4086 }
4087 *instr_prev = frame->f_lasti;
4088 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004089}
4090
Fred Drake5755ce62001-06-27 19:19:46 +00004091void
4092PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004093{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004094 PyThreadState *tstate = PyThreadState_GET();
4095 PyObject *temp = tstate->c_profileobj;
4096 Py_XINCREF(arg);
4097 tstate->c_profilefunc = NULL;
4098 tstate->c_profileobj = NULL;
4099 /* Must make sure that tracing is not ignored if 'temp' is freed */
4100 tstate->use_tracing = tstate->c_tracefunc != NULL;
4101 Py_XDECREF(temp);
4102 tstate->c_profilefunc = func;
4103 tstate->c_profileobj = arg;
4104 /* Flag that tracing or profiling is turned on */
4105 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004106}
4107
4108void
4109PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4110{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004111 PyThreadState *tstate = PyThreadState_GET();
4112 PyObject *temp = tstate->c_traceobj;
4113 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4114 Py_XINCREF(arg);
4115 tstate->c_tracefunc = NULL;
4116 tstate->c_traceobj = NULL;
4117 /* Must make sure that profiling is not ignored if 'temp' is freed */
4118 tstate->use_tracing = tstate->c_profilefunc != NULL;
4119 Py_XDECREF(temp);
4120 tstate->c_tracefunc = func;
4121 tstate->c_traceobj = arg;
4122 /* Flag that tracing or profiling is turned on */
4123 tstate->use_tracing = ((func != NULL)
4124 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004125}
4126
Guido van Rossumb209a111997-04-29 18:18:01 +00004127PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004128PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004129{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004130 PyFrameObject *current_frame = PyEval_GetFrame();
4131 if (current_frame == NULL)
4132 return PyThreadState_GET()->interp->builtins;
4133 else
4134 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004135}
4136
Guido van Rossumb209a111997-04-29 18:18:01 +00004137PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004138PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004139{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004140 PyFrameObject *current_frame = PyEval_GetFrame();
4141 if (current_frame == NULL)
4142 return NULL;
4143 PyFrame_FastToLocals(current_frame);
4144 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004145}
4146
Guido van Rossumb209a111997-04-29 18:18:01 +00004147PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004148PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004149{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004150 PyFrameObject *current_frame = PyEval_GetFrame();
4151 if (current_frame == NULL)
4152 return NULL;
4153 else
4154 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004155}
4156
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004157PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004158PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004159{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004160 PyThreadState *tstate = PyThreadState_GET();
4161 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004162}
4163
Guido van Rossum6135a871995-01-09 17:53:26 +00004164int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004165PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004166{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004167 PyFrameObject *current_frame = PyEval_GetFrame();
4168 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00004169}
4170
Guido van Rossumbe270261997-05-22 22:26:18 +00004171int
Tim Peters5ba58662001-07-16 02:29:45 +00004172PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004173{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004174 PyFrameObject *current_frame = PyEval_GetFrame();
4175 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004176
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004177 if (current_frame != NULL) {
4178 const int codeflags = current_frame->f_code->co_flags;
4179 const int compilerflags = codeflags & PyCF_MASK;
4180 if (compilerflags) {
4181 result = 1;
4182 cf->cf_flags |= compilerflags;
4183 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004184#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004185 if (codeflags & CO_GENERATOR_ALLOWED) {
4186 result = 1;
4187 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4188 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004189#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004190 }
4191 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004192}
4193
4194int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004195Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004196{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004197 PyObject *f = PySys_GetObject("stdout");
4198 if (f == NULL)
4199 return 0;
4200 if (!PyFile_SoftSpace(f, 0))
4201 return 0;
4202 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004203}
4204
Guido van Rossum3f5da241990-12-20 15:06:42 +00004205
Guido van Rossum681d79a1995-07-18 14:51:37 +00004206/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00004207 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004208
Guido van Rossumb209a111997-04-29 18:18:01 +00004209PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004210PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004211{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004212 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004213
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004214 if (arg == NULL) {
4215 arg = PyTuple_New(0);
4216 if (arg == NULL)
4217 return NULL;
4218 }
4219 else if (!PyTuple_Check(arg)) {
4220 PyErr_SetString(PyExc_TypeError,
4221 "argument list must be a tuple");
4222 return NULL;
4223 }
4224 else
4225 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004226
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004227 if (kw != NULL && !PyDict_Check(kw)) {
4228 PyErr_SetString(PyExc_TypeError,
4229 "keyword list must be a dictionary");
4230 Py_DECREF(arg);
4231 return NULL;
4232 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004234 result = PyObject_Call(func, arg, kw);
4235 Py_DECREF(arg);
4236 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004237}
4238
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004239const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004240PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004241{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004242 if (PyMethod_Check(func))
4243 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4244 else if (PyFunction_Check(func))
4245 return PyString_AsString(((PyFunctionObject*)func)->func_name);
4246 else if (PyCFunction_Check(func))
4247 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4248 else if (PyClass_Check(func))
4249 return PyString_AsString(((PyClassObject*)func)->cl_name);
4250 else if (PyInstance_Check(func)) {
4251 return PyString_AsString(
4252 ((PyInstanceObject*)func)->in_class->cl_name);
4253 } else {
4254 return func->ob_type->tp_name;
4255 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004256}
4257
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004258const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004259PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004260{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004261 if (PyMethod_Check(func))
4262 return "()";
4263 else if (PyFunction_Check(func))
4264 return "()";
4265 else if (PyCFunction_Check(func))
4266 return "()";
4267 else if (PyClass_Check(func))
4268 return " constructor";
4269 else if (PyInstance_Check(func)) {
4270 return " instance";
4271 } else {
4272 return " object";
4273 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004274}
4275
Fredrik Lundh7a830892006-05-27 10:39:48 +00004276static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004277err_args(PyObject *func, int flags, int nargs)
4278{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004279 if (flags & METH_NOARGS)
4280 PyErr_Format(PyExc_TypeError,
4281 "%.200s() takes no arguments (%d given)",
4282 ((PyCFunctionObject *)func)->m_ml->ml_name,
4283 nargs);
4284 else
4285 PyErr_Format(PyExc_TypeError,
4286 "%.200s() takes exactly one argument (%d given)",
4287 ((PyCFunctionObject *)func)->m_ml->ml_name,
4288 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004289}
4290
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004291#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004292if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004293 if (call_trace(tstate->c_profilefunc, \
4294 tstate->c_profileobj, \
4295 tstate->frame, PyTrace_C_CALL, \
4296 func)) { \
4297 x = NULL; \
4298 } \
4299 else { \
4300 x = call; \
4301 if (tstate->c_profilefunc != NULL) { \
4302 if (x == NULL) { \
4303 call_trace_protected(tstate->c_profilefunc, \
4304 tstate->c_profileobj, \
4305 tstate->frame, PyTrace_C_EXCEPTION, \
4306 func); \
4307 /* XXX should pass (type, value, tb) */ \
4308 } else { \
4309 if (call_trace(tstate->c_profilefunc, \
4310 tstate->c_profileobj, \
4311 tstate->frame, PyTrace_C_RETURN, \
4312 func)) { \
4313 Py_DECREF(x); \
4314 x = NULL; \
4315 } \
4316 } \
4317 } \
4318 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004319} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004320 x = call; \
4321 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004322
Fredrik Lundh7a830892006-05-27 10:39:48 +00004323static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004324call_function(PyObject ***pp_stack, int oparg
4325#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004326 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004327#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004328 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004329{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004330 int na = oparg & 0xff;
4331 int nk = (oparg>>8) & 0xff;
4332 int n = na + 2 * nk;
4333 PyObject **pfunc = (*pp_stack) - n - 1;
4334 PyObject *func = *pfunc;
4335 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004336
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004337 /* Always dispatch PyCFunction first, because these are
4338 presumed to be the most frequent callable object.
4339 */
4340 if (PyCFunction_Check(func) && nk == 0) {
4341 int flags = PyCFunction_GET_FLAGS(func);
4342 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004343
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004344 PCALL(PCALL_CFUNCTION);
4345 if (flags & (METH_NOARGS | METH_O)) {
4346 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4347 PyObject *self = PyCFunction_GET_SELF(func);
4348 if (flags & METH_NOARGS && na == 0) {
4349 C_TRACE(x, (*meth)(self,NULL));
4350 }
4351 else if (flags & METH_O && na == 1) {
4352 PyObject *arg = EXT_POP(*pp_stack);
4353 C_TRACE(x, (*meth)(self,arg));
4354 Py_DECREF(arg);
4355 }
4356 else {
4357 err_args(func, flags, na);
4358 x = NULL;
4359 }
4360 }
4361 else {
4362 PyObject *callargs;
4363 callargs = load_args(pp_stack, na);
4364 READ_TIMESTAMP(*pintr0);
4365 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4366 READ_TIMESTAMP(*pintr1);
4367 Py_XDECREF(callargs);
4368 }
4369 } else {
4370 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4371 /* optimize access to bound methods */
4372 PyObject *self = PyMethod_GET_SELF(func);
4373 PCALL(PCALL_METHOD);
4374 PCALL(PCALL_BOUND_METHOD);
4375 Py_INCREF(self);
4376 func = PyMethod_GET_FUNCTION(func);
4377 Py_INCREF(func);
Serhiy Storchaka763a61c2016-04-10 18:05:12 +03004378 Py_SETREF(*pfunc, self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004379 na++;
4380 n++;
4381 } else
4382 Py_INCREF(func);
4383 READ_TIMESTAMP(*pintr0);
4384 if (PyFunction_Check(func))
4385 x = fast_function(func, pp_stack, n, na, nk);
4386 else
4387 x = do_call(func, pp_stack, na, nk);
4388 READ_TIMESTAMP(*pintr1);
4389 Py_DECREF(func);
4390 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004391
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004392 /* Clear the stack of the function object. Also removes
4393 the arguments in case they weren't consumed already
4394 (fast_function() and err_args() leave them on the stack).
4395 */
4396 while ((*pp_stack) > pfunc) {
4397 w = EXT_POP(*pp_stack);
4398 Py_DECREF(w);
4399 PCALL(PCALL_POP);
4400 }
4401 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004402}
4403
Jeremy Hylton192690e2002-08-16 18:36:11 +00004404/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004405 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004406 For the simplest case -- a function that takes only positional
4407 arguments and is called with only positional arguments -- it
4408 inlines the most primitive frame setup code from
4409 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4410 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004411*/
4412
Fredrik Lundh7a830892006-05-27 10:39:48 +00004413static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004414fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004415{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004416 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4417 PyObject *globals = PyFunction_GET_GLOBALS(func);
4418 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4419 PyObject **d = NULL;
4420 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004421
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004422 PCALL(PCALL_FUNCTION);
4423 PCALL(PCALL_FAST_FUNCTION);
4424 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4425 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4426 PyFrameObject *f;
4427 PyObject *retval = NULL;
4428 PyThreadState *tstate = PyThreadState_GET();
4429 PyObject **fastlocals, **stack;
4430 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004431
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004432 PCALL(PCALL_FASTER_FUNCTION);
4433 assert(globals != NULL);
4434 /* XXX Perhaps we should create a specialized
4435 PyFrame_New() that doesn't take locals, but does
4436 take builtins without sanity checking them.
4437 */
4438 assert(tstate != NULL);
4439 f = PyFrame_New(tstate, co, globals, NULL);
4440 if (f == NULL)
4441 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004442
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004443 fastlocals = f->f_localsplus;
4444 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004445
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004446 for (i = 0; i < n; i++) {
4447 Py_INCREF(*stack);
4448 fastlocals[i] = *stack++;
4449 }
4450 retval = PyEval_EvalFrameEx(f,0);
4451 ++tstate->recursion_depth;
4452 Py_DECREF(f);
4453 --tstate->recursion_depth;
4454 return retval;
4455 }
4456 if (argdefs != NULL) {
4457 d = &PyTuple_GET_ITEM(argdefs, 0);
4458 nd = Py_SIZE(argdefs);
4459 }
4460 return PyEval_EvalCodeEx(co, globals,
4461 (PyObject *)NULL, (*pp_stack)-n, na,
4462 (*pp_stack)-2*nk, nk, d, nd,
4463 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004464}
4465
Fredrik Lundh7a830892006-05-27 10:39:48 +00004466static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004467update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4468 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004469{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004470 PyObject *kwdict = NULL;
4471 if (orig_kwdict == NULL)
4472 kwdict = PyDict_New();
4473 else {
4474 kwdict = PyDict_Copy(orig_kwdict);
4475 Py_DECREF(orig_kwdict);
4476 }
4477 if (kwdict == NULL)
4478 return NULL;
4479 while (--nk >= 0) {
4480 int err;
4481 PyObject *value = EXT_POP(*pp_stack);
4482 PyObject *key = EXT_POP(*pp_stack);
4483 if (PyDict_GetItem(kwdict, key) != NULL) {
4484 PyErr_Format(PyExc_TypeError,
4485 "%.200s%s got multiple values "
4486 "for keyword argument '%.200s'",
4487 PyEval_GetFuncName(func),
4488 PyEval_GetFuncDesc(func),
4489 PyString_AsString(key));
4490 Py_DECREF(key);
4491 Py_DECREF(value);
4492 Py_DECREF(kwdict);
4493 return NULL;
4494 }
4495 err = PyDict_SetItem(kwdict, key, value);
4496 Py_DECREF(key);
4497 Py_DECREF(value);
4498 if (err) {
4499 Py_DECREF(kwdict);
4500 return NULL;
4501 }
4502 }
4503 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004504}
4505
Fredrik Lundh7a830892006-05-27 10:39:48 +00004506static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004507update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004508 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004509{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004510 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004511
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004512 callargs = PyTuple_New(nstack + nstar);
4513 if (callargs == NULL) {
4514 return NULL;
4515 }
4516 if (nstar) {
4517 int i;
4518 for (i = 0; i < nstar; i++) {
4519 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4520 Py_INCREF(a);
4521 PyTuple_SET_ITEM(callargs, nstack + i, a);
4522 }
4523 }
4524 while (--nstack >= 0) {
4525 w = EXT_POP(*pp_stack);
4526 PyTuple_SET_ITEM(callargs, nstack, w);
4527 }
4528 return callargs;
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 +00004532load_args(PyObject ***pp_stack, int na)
4533{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004534 PyObject *args = PyTuple_New(na);
4535 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004536
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004537 if (args == NULL)
4538 return NULL;
4539 while (--na >= 0) {
4540 w = EXT_POP(*pp_stack);
4541 PyTuple_SET_ITEM(args, na, w);
4542 }
4543 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004544}
4545
Fredrik Lundh7a830892006-05-27 10:39:48 +00004546static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004547do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4548{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004549 PyObject *callargs = NULL;
4550 PyObject *kwdict = NULL;
4551 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004552
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004553 if (nk > 0) {
4554 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4555 if (kwdict == NULL)
4556 goto call_fail;
4557 }
4558 callargs = load_args(pp_stack, na);
4559 if (callargs == NULL)
4560 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004561#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004562 /* At this point, we have to look at the type of func to
4563 update the call stats properly. Do it here so as to avoid
4564 exposing the call stats machinery outside ceval.c
4565 */
4566 if (PyFunction_Check(func))
4567 PCALL(PCALL_FUNCTION);
4568 else if (PyMethod_Check(func))
4569 PCALL(PCALL_METHOD);
4570 else if (PyType_Check(func))
4571 PCALL(PCALL_TYPE);
4572 else if (PyCFunction_Check(func))
4573 PCALL(PCALL_CFUNCTION);
4574 else
4575 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004576#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004577 if (PyCFunction_Check(func)) {
4578 PyThreadState *tstate = PyThreadState_GET();
4579 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4580 }
4581 else
4582 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004583 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004584 Py_XDECREF(callargs);
4585 Py_XDECREF(kwdict);
4586 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004587}
4588
Fredrik Lundh7a830892006-05-27 10:39:48 +00004589static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004590ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4591{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004592 int nstar = 0;
4593 PyObject *callargs = NULL;
4594 PyObject *stararg = NULL;
4595 PyObject *kwdict = NULL;
4596 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004597
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004598 if (flags & CALL_FLAG_KW) {
4599 kwdict = EXT_POP(*pp_stack);
4600 if (!PyDict_Check(kwdict)) {
4601 PyObject *d;
4602 d = PyDict_New();
4603 if (d == NULL)
4604 goto ext_call_fail;
4605 if (PyDict_Update(d, kwdict) != 0) {
4606 Py_DECREF(d);
4607 /* PyDict_Update raises attribute
4608 * error (percolated from an attempt
4609 * to get 'keys' attribute) instead of
4610 * a type error if its second argument
4611 * is not a mapping.
4612 */
4613 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4614 PyErr_Format(PyExc_TypeError,
4615 "%.200s%.200s argument after ** "
4616 "must be a mapping, not %.200s",
4617 PyEval_GetFuncName(func),
4618 PyEval_GetFuncDesc(func),
4619 kwdict->ob_type->tp_name);
4620 }
4621 goto ext_call_fail;
4622 }
4623 Py_DECREF(kwdict);
4624 kwdict = d;
4625 }
4626 }
4627 if (flags & CALL_FLAG_VAR) {
4628 stararg = EXT_POP(*pp_stack);
4629 if (!PyTuple_Check(stararg)) {
4630 PyObject *t = NULL;
4631 t = PySequence_Tuple(stararg);
4632 if (t == NULL) {
Martin Panter0bb165e2016-01-31 06:30:56 +00004633 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4634 /* Don't mask TypeError raised from a generator */
4635 !PyGen_Check(stararg)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004636 PyErr_Format(PyExc_TypeError,
4637 "%.200s%.200s argument after * "
Martin Panter0bb165e2016-01-31 06:30:56 +00004638 "must be an iterable, not %200s",
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004639 PyEval_GetFuncName(func),
4640 PyEval_GetFuncDesc(func),
4641 stararg->ob_type->tp_name);
4642 }
4643 goto ext_call_fail;
4644 }
4645 Py_DECREF(stararg);
4646 stararg = t;
4647 }
4648 nstar = PyTuple_GET_SIZE(stararg);
4649 }
4650 if (nk > 0) {
4651 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4652 if (kwdict == NULL)
4653 goto ext_call_fail;
4654 }
4655 callargs = update_star_args(na, nstar, stararg, pp_stack);
4656 if (callargs == NULL)
4657 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004658#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004659 /* At this point, we have to look at the type of func to
4660 update the call stats properly. Do it here so as to avoid
4661 exposing the call stats machinery outside ceval.c
4662 */
4663 if (PyFunction_Check(func))
4664 PCALL(PCALL_FUNCTION);
4665 else if (PyMethod_Check(func))
4666 PCALL(PCALL_METHOD);
4667 else if (PyType_Check(func))
4668 PCALL(PCALL_TYPE);
4669 else if (PyCFunction_Check(func))
4670 PCALL(PCALL_CFUNCTION);
4671 else
4672 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004673#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004674 if (PyCFunction_Check(func)) {
4675 PyThreadState *tstate = PyThreadState_GET();
4676 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4677 }
4678 else
4679 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004680ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004681 Py_XDECREF(callargs);
4682 Py_XDECREF(kwdict);
4683 Py_XDECREF(stararg);
4684 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004685}
4686
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004687/* Extract a slice index from a PyInt or PyLong or an object with the
4688 nb_index slot defined, and store in *pi.
4689 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang05469fa2017-05-10 19:20:28 +08004690 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004691 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004692*/
Tim Petersb5196382001-12-16 19:44:20 +00004693/* Note: If v is NULL, return success without storing into *pi. This
4694 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4695 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004696*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004697int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004698_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004699{
Serhiy Storchaka079f21f2017-03-30 20:32:18 +03004700 if (v != NULL && v != Py_None) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004701 Py_ssize_t x;
4702 if (PyInt_Check(v)) {
4703 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4704 however, it looks like it should be AsSsize_t.
4705 There should be a comment here explaining why.
4706 */
4707 x = PyInt_AS_LONG(v);
4708 }
4709 else if (PyIndex_Check(v)) {
4710 x = PyNumber_AsSsize_t(v, NULL);
4711 if (x == -1 && PyErr_Occurred())
4712 return 0;
4713 }
4714 else {
4715 PyErr_SetString(PyExc_TypeError,
4716 "slice indices must be integers or "
4717 "None or have an __index__ method");
4718 return 0;
4719 }
4720 *pi = x;
4721 }
4722 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004723}
4724
Serhiy Storchaka079f21f2017-03-30 20:32:18 +03004725int
4726_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
4727{
4728 Py_ssize_t x;
4729 if (PyIndex_Check(v)) {
4730 x = PyNumber_AsSsize_t(v, NULL);
4731 if (x == -1 && PyErr_Occurred())
4732 return 0;
4733 }
4734 else {
4735 PyErr_SetString(PyExc_TypeError,
4736 "slice indices must be integers or "
4737 "have an __index__ method");
4738 return 0;
4739 }
4740 *pi = x;
4741 return 1;
4742}
4743
4744
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004745#undef ISINDEX
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00004746#define ISINDEX(x) ((x) == NULL || \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004747 PyInt_Check(x) || PyLong_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004748
Fredrik Lundh7a830892006-05-27 10:39:48 +00004749static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004750apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004751{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004752 PyTypeObject *tp = u->ob_type;
4753 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004754
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004755 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4756 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4757 if (!_PyEval_SliceIndex(v, &ilow))
4758 return NULL;
4759 if (!_PyEval_SliceIndex(w, &ihigh))
4760 return NULL;
4761 return PySequence_GetSlice(u, ilow, ihigh);
4762 }
4763 else {
4764 PyObject *slice = PySlice_New(v, w, NULL);
4765 if (slice != NULL) {
4766 PyObject *res = PyObject_GetItem(u, slice);
4767 Py_DECREF(slice);
4768 return res;
4769 }
4770 else
4771 return NULL;
4772 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004773}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004774
Fredrik Lundh7a830892006-05-27 10:39:48 +00004775static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004776assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004777 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004778{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004779 PyTypeObject *tp = u->ob_type;
4780 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004781
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004782 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4783 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4784 if (!_PyEval_SliceIndex(v, &ilow))
4785 return -1;
4786 if (!_PyEval_SliceIndex(w, &ihigh))
4787 return -1;
4788 if (x == NULL)
4789 return PySequence_DelSlice(u, ilow, ihigh);
4790 else
4791 return PySequence_SetSlice(u, ilow, ihigh, x);
4792 }
4793 else {
4794 PyObject *slice = PySlice_New(v, w, NULL);
4795 if (slice != NULL) {
4796 int res;
4797 if (x != NULL)
4798 res = PyObject_SetItem(u, slice, x);
4799 else
4800 res = PyObject_DelItem(u, slice);
4801 Py_DECREF(slice);
4802 return res;
4803 }
4804 else
4805 return -1;
4806 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004807}
4808
Guido van Rossum04edb522008-03-18 02:49:46 +00004809#define Py3kExceptionClass_Check(x) \
4810 (PyType_Check((x)) && \
4811 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4812
4813#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004814 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004815
Fredrik Lundh7a830892006-05-27 10:39:48 +00004816static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004817cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004818{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004819 int res = 0;
4820 switch (op) {
4821 case PyCmp_IS:
4822 res = (v == w);
4823 break;
4824 case PyCmp_IS_NOT:
4825 res = (v != w);
4826 break;
4827 case PyCmp_IN:
4828 res = PySequence_Contains(w, v);
4829 if (res < 0)
4830 return NULL;
4831 break;
4832 case PyCmp_NOT_IN:
4833 res = PySequence_Contains(w, v);
4834 if (res < 0)
4835 return NULL;
4836 res = !res;
4837 break;
4838 case PyCmp_EXC_MATCH:
4839 if (PyTuple_Check(w)) {
4840 Py_ssize_t i, length;
4841 length = PyTuple_Size(w);
4842 for (i = 0; i < length; i += 1) {
4843 PyObject *exc = PyTuple_GET_ITEM(w, i);
4844 if (PyString_Check(exc)) {
4845 int ret_val;
4846 ret_val = PyErr_WarnEx(
4847 PyExc_DeprecationWarning,
4848 "catching of string "
4849 "exceptions is deprecated", 1);
4850 if (ret_val < 0)
4851 return NULL;
4852 }
4853 else if (Py_Py3kWarningFlag &&
4854 !PyTuple_Check(exc) &&
4855 !Py3kExceptionClass_Check(exc))
4856 {
4857 int ret_val;
4858 ret_val = PyErr_WarnEx(
4859 PyExc_DeprecationWarning,
4860 CANNOT_CATCH_MSG, 1);
4861 if (ret_val < 0)
4862 return NULL;
4863 }
4864 }
4865 }
4866 else {
4867 if (PyString_Check(w)) {
4868 int ret_val;
4869 ret_val = PyErr_WarnEx(
4870 PyExc_DeprecationWarning,
4871 "catching of string "
4872 "exceptions is deprecated", 1);
4873 if (ret_val < 0)
4874 return NULL;
4875 }
4876 else if (Py_Py3kWarningFlag &&
4877 !PyTuple_Check(w) &&
4878 !Py3kExceptionClass_Check(w))
4879 {
4880 int ret_val;
4881 ret_val = PyErr_WarnEx(
4882 PyExc_DeprecationWarning,
4883 CANNOT_CATCH_MSG, 1);
4884 if (ret_val < 0)
4885 return NULL;
4886 }
4887 }
4888 res = PyErr_GivenExceptionMatches(v, w);
4889 break;
4890 default:
4891 return PyObject_RichCompare(v, w, op);
4892 }
4893 v = res ? Py_True : Py_False;
4894 Py_INCREF(v);
4895 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004896}
4897
Fredrik Lundh7a830892006-05-27 10:39:48 +00004898static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004899import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004900{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004901 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004902
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004903 x = PyObject_GetAttr(v, name);
4904 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4905 PyErr_Format(PyExc_ImportError,
4906 "cannot import name %.230s",
4907 PyString_AsString(name));
4908 }
4909 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004910}
Guido van Rossumac7be682001-01-17 15:42:30 +00004911
Fredrik Lundh7a830892006-05-27 10:39:48 +00004912static int
Thomas Wouters52152252000-08-17 22:55:00 +00004913import_all_from(PyObject *locals, PyObject *v)
4914{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004915 PyObject *all = PyObject_GetAttrString(v, "__all__");
4916 PyObject *dict, *name, *value;
4917 int skip_leading_underscores = 0;
4918 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004919
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004920 if (all == NULL) {
4921 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4922 return -1; /* Unexpected error */
4923 PyErr_Clear();
4924 dict = PyObject_GetAttrString(v, "__dict__");
4925 if (dict == NULL) {
4926 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4927 return -1;
4928 PyErr_SetString(PyExc_ImportError,
4929 "from-import-* object has no __dict__ and no __all__");
4930 return -1;
4931 }
4932 all = PyMapping_Keys(dict);
4933 Py_DECREF(dict);
4934 if (all == NULL)
4935 return -1;
4936 skip_leading_underscores = 1;
4937 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004939 for (pos = 0, err = 0; ; pos++) {
4940 name = PySequence_GetItem(all, pos);
4941 if (name == NULL) {
4942 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4943 err = -1;
4944 else
4945 PyErr_Clear();
4946 break;
4947 }
4948 if (skip_leading_underscores &&
4949 PyString_Check(name) &&
4950 PyString_AS_STRING(name)[0] == '_')
4951 {
4952 Py_DECREF(name);
4953 continue;
4954 }
4955 value = PyObject_GetAttr(v, name);
4956 if (value == NULL)
4957 err = -1;
4958 else if (PyDict_CheckExact(locals))
4959 err = PyDict_SetItem(locals, name, value);
4960 else
4961 err = PyObject_SetItem(locals, name, value);
4962 Py_DECREF(name);
4963 Py_XDECREF(value);
4964 if (err != 0)
4965 break;
4966 }
4967 Py_DECREF(all);
4968 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004969}
4970
Fredrik Lundh7a830892006-05-27 10:39:48 +00004971static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004972build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004973{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004974 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004975
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004976 if (PyDict_Check(methods))
4977 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4978 if (metaclass != NULL)
4979 Py_INCREF(metaclass);
4980 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4981 base = PyTuple_GET_ITEM(bases, 0);
4982 metaclass = PyObject_GetAttrString(base, "__class__");
4983 if (metaclass == NULL) {
4984 PyErr_Clear();
4985 metaclass = (PyObject *)base->ob_type;
4986 Py_INCREF(metaclass);
4987 }
4988 }
4989 else {
4990 PyObject *g = PyEval_GetGlobals();
4991 if (g != NULL && PyDict_Check(g))
4992 metaclass = PyDict_GetItemString(g, "__metaclass__");
4993 if (metaclass == NULL)
4994 metaclass = (PyObject *) &PyClass_Type;
4995 Py_INCREF(metaclass);
4996 }
4997 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
4998 NULL);
4999 Py_DECREF(metaclass);
5000 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
5001 /* A type error here likely means that the user passed
5002 in a base that was not a class (such the random module
5003 instead of the random.random type). Help them out with
5004 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00005005
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005006 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00005007
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005008 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
5009 if (PyString_Check(pvalue)) {
5010 PyObject *newmsg;
5011 newmsg = PyString_FromFormat(
5012 "Error when calling the metaclass bases\n"
5013 " %s",
5014 PyString_AS_STRING(pvalue));
5015 if (newmsg != NULL) {
5016 Py_DECREF(pvalue);
5017 pvalue = newmsg;
5018 }
5019 }
5020 PyErr_Restore(ptype, pvalue, ptraceback);
5021 }
5022 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00005023}
5024
Fredrik Lundh7a830892006-05-27 10:39:48 +00005025static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005026exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005027 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005028{
Benjamin Petersond2903bd2014-08-09 19:39:36 -07005029 int n;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005030 PyObject *v;
5031 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005032
Benjamin Petersond2903bd2014-08-09 19:39:36 -07005033 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
5034 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
5035 /* Backward compatibility hack */
5036 globals = PyTuple_GetItem(prog, 1);
5037 if (n == 3)
5038 locals = PyTuple_GetItem(prog, 2);
5039 prog = PyTuple_GetItem(prog, 0);
5040 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005041 if (globals == Py_None) {
5042 globals = PyEval_GetGlobals();
5043 if (locals == Py_None) {
5044 locals = PyEval_GetLocals();
5045 plain = 1;
5046 }
5047 if (!globals || !locals) {
5048 PyErr_SetString(PyExc_SystemError,
5049 "globals and locals cannot be NULL");
5050 return -1;
5051 }
5052 }
5053 else if (locals == Py_None)
5054 locals = globals;
5055 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005056#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005057 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005058#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005059 !PyCode_Check(prog) &&
5060 !PyFile_Check(prog)) {
5061 PyErr_SetString(PyExc_TypeError,
5062 "exec: arg 1 must be a string, file, or code object");
5063 return -1;
5064 }
5065 if (!PyDict_Check(globals)) {
5066 PyErr_SetString(PyExc_TypeError,
5067 "exec: arg 2 must be a dictionary or None");
5068 return -1;
5069 }
5070 if (!PyMapping_Check(locals)) {
5071 PyErr_SetString(PyExc_TypeError,
5072 "exec: arg 3 must be a mapping or None");
5073 return -1;
5074 }
5075 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
5076 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
5077 if (PyCode_Check(prog)) {
5078 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
5079 PyErr_SetString(PyExc_TypeError,
5080 "code object passed to exec may not contain free variables");
5081 return -1;
5082 }
5083 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
5084 }
5085 else if (PyFile_Check(prog)) {
5086 FILE *fp = PyFile_AsFile(prog);
5087 char *name = PyString_AsString(PyFile_Name(prog));
5088 PyCompilerFlags cf;
5089 if (name == NULL)
5090 return -1;
5091 cf.cf_flags = 0;
5092 if (PyEval_MergeCompilerFlags(&cf))
5093 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
5094 locals, &cf);
5095 else
5096 v = PyRun_File(fp, name, Py_file_input, globals,
5097 locals);
5098 }
5099 else {
5100 PyObject *tmp = NULL;
5101 char *str;
5102 PyCompilerFlags cf;
5103 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005104#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005105 if (PyUnicode_Check(prog)) {
5106 tmp = PyUnicode_AsUTF8String(prog);
5107 if (tmp == NULL)
5108 return -1;
5109 prog = tmp;
5110 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
5111 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005112#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005113 if (PyString_AsStringAndSize(prog, &str, NULL))
5114 return -1;
5115 if (PyEval_MergeCompilerFlags(&cf))
5116 v = PyRun_StringFlags(str, Py_file_input, globals,
5117 locals, &cf);
5118 else
5119 v = PyRun_String(str, Py_file_input, globals, locals);
5120 Py_XDECREF(tmp);
5121 }
5122 if (plain)
5123 PyFrame_LocalsToFast(f, 0);
5124 if (v == NULL)
5125 return -1;
5126 Py_DECREF(v);
5127 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005128}
Guido van Rossum24c13741995-02-14 09:42:43 +00005129
Fredrik Lundh7a830892006-05-27 10:39:48 +00005130static void
Paul Prescode68140d2000-08-30 20:25:01 +00005131format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
5132{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005133 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005134
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005135 if (!obj)
5136 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005137
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005138 obj_str = PyString_AsString(obj);
5139 if (!obj_str)
5140 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005141
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005142 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005143}
Guido van Rossum950361c1997-01-24 13:49:28 +00005144
Fredrik Lundh7a830892006-05-27 10:39:48 +00005145static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005146string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005147 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005148{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005149 /* This function implements 'variable += expr' when both arguments
5150 are strings. */
5151 Py_ssize_t v_len = PyString_GET_SIZE(v);
5152 Py_ssize_t w_len = PyString_GET_SIZE(w);
5153 Py_ssize_t new_len = v_len + w_len;
5154 if (new_len < 0) {
5155 PyErr_SetString(PyExc_OverflowError,
5156 "strings are too large to concat");
5157 return NULL;
5158 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00005159
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005160 if (v->ob_refcnt == 2) {
5161 /* In the common case, there are 2 references to the value
5162 * stored in 'variable' when the += is performed: one on the
5163 * value stack (in 'v') and one still stored in the
5164 * 'variable'. We try to delete the variable now to reduce
5165 * the refcnt to 1.
5166 */
5167 switch (*next_instr) {
5168 case STORE_FAST:
5169 {
5170 int oparg = PEEKARG();
5171 PyObject **fastlocals = f->f_localsplus;
5172 if (GETLOCAL(oparg) == v)
5173 SETLOCAL(oparg, NULL);
5174 break;
5175 }
5176 case STORE_DEREF:
5177 {
5178 PyObject **freevars = (f->f_localsplus +
5179 f->f_code->co_nlocals);
5180 PyObject *c = freevars[PEEKARG()];
5181 if (PyCell_GET(c) == v)
5182 PyCell_Set(c, NULL);
5183 break;
5184 }
5185 case STORE_NAME:
5186 {
5187 PyObject *names = f->f_code->co_names;
5188 PyObject *name = GETITEM(names, PEEKARG());
5189 PyObject *locals = f->f_locals;
5190 if (PyDict_CheckExact(locals) &&
5191 PyDict_GetItem(locals, name) == v) {
5192 if (PyDict_DelItem(locals, name) != 0) {
5193 PyErr_Clear();
5194 }
5195 }
5196 break;
5197 }
5198 }
5199 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005200
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005201 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
5202 /* Now we own the last reference to 'v', so we can resize it
5203 * in-place.
5204 */
5205 if (_PyString_Resize(&v, new_len) != 0) {
5206 /* XXX if _PyString_Resize() fails, 'v' has been
5207 * deallocated so it cannot be put back into
5208 * 'variable'. The MemoryError is raised when there
5209 * is no value in 'variable', which might (very
5210 * remotely) be a cause of incompatibilities.
5211 */
5212 return NULL;
5213 }
5214 /* copy 'w' into the newly allocated area of 'v' */
5215 memcpy(PyString_AS_STRING(v) + v_len,
5216 PyString_AS_STRING(w), w_len);
5217 return v;
5218 }
5219 else {
5220 /* When in-place resizing is not an option. */
5221 PyString_Concat(&v, w);
5222 return v;
5223 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005224}
5225
Guido van Rossum950361c1997-01-24 13:49:28 +00005226#ifdef DYNAMIC_EXECUTION_PROFILE
5227
Fredrik Lundh7a830892006-05-27 10:39:48 +00005228static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005229getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005230{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005231 int i;
5232 PyObject *l = PyList_New(256);
5233 if (l == NULL) return NULL;
5234 for (i = 0; i < 256; i++) {
5235 PyObject *x = PyInt_FromLong(a[i]);
5236 if (x == NULL) {
5237 Py_DECREF(l);
5238 return NULL;
5239 }
5240 PyList_SetItem(l, i, x);
5241 }
5242 for (i = 0; i < 256; i++)
5243 a[i] = 0;
5244 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005245}
5246
5247PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005248_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005249{
5250#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005251 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005252#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005253 int i;
5254 PyObject *l = PyList_New(257);
5255 if (l == NULL) return NULL;
5256 for (i = 0; i < 257; i++) {
5257 PyObject *x = getarray(dxpairs[i]);
5258 if (x == NULL) {
5259 Py_DECREF(l);
5260 return NULL;
5261 }
5262 PyList_SetItem(l, i, x);
5263 }
5264 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005265#endif
5266}
5267
5268#endif