blob: 2088a271d86e1bf33216f3f8b4fcfa98fbdbc28d [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 {
Xiang Zhang34bb88d2018-03-09 10:21:58 +08002601 long res;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002602 w = GETITEM(names, oparg);
2603 x = PyDict_GetItemString(f->f_builtins, "__import__");
2604 if (x == NULL) {
2605 PyErr_SetString(PyExc_ImportError,
2606 "__import__ not found");
2607 break;
2608 }
2609 Py_INCREF(x);
2610 v = POP();
2611 u = TOP();
Xiang Zhang34bb88d2018-03-09 10:21:58 +08002612 res = PyInt_AsLong(u);
2613 if (res != -1 || PyErr_Occurred()) {
2614 if (res == -1) {
2615 assert(PyErr_Occurred());
2616 PyErr_Clear();
2617 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002618 w = PyTuple_Pack(5,
2619 w,
2620 f->f_globals,
2621 f->f_locals == NULL ?
2622 Py_None : f->f_locals,
2623 v,
2624 u);
Xiang Zhang34bb88d2018-03-09 10:21:58 +08002625 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002626 else
2627 w = PyTuple_Pack(4,
2628 w,
2629 f->f_globals,
2630 f->f_locals == NULL ?
2631 Py_None : f->f_locals,
2632 v);
2633 Py_DECREF(v);
2634 Py_DECREF(u);
2635 if (w == NULL) {
2636 u = POP();
2637 Py_DECREF(x);
2638 x = NULL;
2639 break;
2640 }
2641 READ_TIMESTAMP(intr0);
2642 v = x;
2643 x = PyEval_CallObject(v, w);
2644 Py_DECREF(v);
2645 READ_TIMESTAMP(intr1);
2646 Py_DECREF(w);
2647 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002648 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002649 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002650 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002651
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002652 TARGET_NOARG(IMPORT_STAR)
2653 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002654 v = POP();
2655 PyFrame_FastToLocals(f);
2656 if ((x = f->f_locals) == NULL) {
2657 PyErr_SetString(PyExc_SystemError,
2658 "no locals found during 'import *'");
Serhiy Storchaka9fbb65e2017-03-08 13:44:33 +02002659 Py_DECREF(v);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002660 break;
2661 }
2662 READ_TIMESTAMP(intr0);
2663 err = import_all_from(x, v);
2664 READ_TIMESTAMP(intr1);
2665 PyFrame_LocalsToFast(f, 0);
2666 Py_DECREF(v);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002667 if (err == 0) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002668 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002669 }
Guido van Rossum25831651993-05-19 14:50:45 +00002670
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002671 TARGET(IMPORT_FROM)
2672 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002673 w = GETITEM(names, oparg);
2674 v = TOP();
2675 READ_TIMESTAMP(intr0);
2676 x = import_from(v, w);
2677 READ_TIMESTAMP(intr1);
2678 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002679 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002680 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002681 }
Thomas Wouters52152252000-08-17 22:55:00 +00002682
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002683 TARGET(JUMP_FORWARD)
2684 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002685 JUMPBY(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002686 FAST_DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002688
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002689 PREDICTED_WITH_ARG(POP_JUMP_IF_FALSE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002690 TARGET(POP_JUMP_IF_FALSE)
2691 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002692 w = POP();
2693 if (w == Py_True) {
2694 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002695 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002696 }
2697 if (w == Py_False) {
2698 Py_DECREF(w);
2699 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002700 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002701 }
2702 err = PyObject_IsTrue(w);
2703 Py_DECREF(w);
2704 if (err > 0)
2705 err = 0;
2706 else if (err == 0)
2707 JUMPTO(oparg);
2708 else
2709 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002710 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002712
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002713 PREDICTED_WITH_ARG(POP_JUMP_IF_TRUE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002714 TARGET(POP_JUMP_IF_TRUE)
2715 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002716 w = POP();
2717 if (w == Py_False) {
2718 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002719 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002720 }
2721 if (w == Py_True) {
2722 Py_DECREF(w);
2723 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002724 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002725 }
2726 err = PyObject_IsTrue(w);
2727 Py_DECREF(w);
2728 if (err > 0) {
2729 err = 0;
2730 JUMPTO(oparg);
2731 }
2732 else if (err == 0)
2733 ;
2734 else
2735 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002736 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002737 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002738
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002739 TARGET(JUMP_IF_FALSE_OR_POP)
2740 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002741 w = TOP();
2742 if (w == Py_True) {
2743 STACKADJ(-1);
2744 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002745 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002746 }
2747 if (w == Py_False) {
2748 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002749 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002750 }
2751 err = PyObject_IsTrue(w);
2752 if (err > 0) {
2753 STACKADJ(-1);
2754 Py_DECREF(w);
2755 err = 0;
2756 }
2757 else if (err == 0)
2758 JUMPTO(oparg);
2759 else
2760 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002761 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002762 }
Jeffrey Yasskin68d68522009-02-28 19:03:21 +00002763
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002764 TARGET(JUMP_IF_TRUE_OR_POP)
2765 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002766 w = TOP();
2767 if (w == Py_False) {
2768 STACKADJ(-1);
2769 Py_DECREF(w);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002770 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002771 }
2772 if (w == Py_True) {
2773 JUMPTO(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002774 FAST_DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002775 }
2776 err = PyObject_IsTrue(w);
2777 if (err > 0) {
2778 err = 0;
2779 JUMPTO(oparg);
2780 }
2781 else if (err == 0) {
2782 STACKADJ(-1);
2783 Py_DECREF(w);
2784 }
2785 else
2786 break;
Benjamin Peterson14462d42015-08-19 20:38:39 -07002787 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002788 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002789
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002790 PREDICTED_WITH_ARG(JUMP_ABSOLUTE);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002791 TARGET(JUMP_ABSOLUTE)
2792 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002793 JUMPTO(oparg);
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002794#if FAST_LOOPS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002795 /* Enabling this path speeds-up all while and for-loops by bypassing
2796 the per-loop checks for signals. By default, this should be turned-off
2797 because it prevents detection of a control-break in tight loops like
2798 "while 1: pass". Compile with this option turned-on when you need
2799 the speed-up and do not need break checking inside tight loops (ones
2800 that contain only instructions ending with goto fast_next_opcode).
2801 */
2802 goto fast_next_opcode;
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002803#else
Benjamin Peterson14462d42015-08-19 20:38:39 -07002804 DISPATCH();
Raymond Hettingerdc1d1ba2007-11-07 02:45:46 +00002805#endif
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002806 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002807
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002808 TARGET_NOARG(GET_ITER)
2809 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002810 /* before: [obj]; after [getiter(obj)] */
2811 v = TOP();
2812 x = PyObject_GetIter(v);
2813 Py_DECREF(v);
2814 if (x != NULL) {
2815 SET_TOP(x);
2816 PREDICT(FOR_ITER);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002817 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002818 }
2819 STACKADJ(-1);
2820 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002821 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002822
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002823 PREDICTED_WITH_ARG(FOR_ITER);
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002824 TARGET(FOR_ITER)
2825 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002826 /* before: [iter]; after: [iter, iter()] *or* [] */
2827 v = TOP();
2828 x = (*v->ob_type->tp_iternext)(v);
2829 if (x != NULL) {
2830 PUSH(x);
2831 PREDICT(STORE_FAST);
2832 PREDICT(UNPACK_SEQUENCE);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002833 DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002834 }
2835 if (PyErr_Occurred()) {
2836 if (!PyErr_ExceptionMatches(
2837 PyExc_StopIteration))
2838 break;
2839 PyErr_Clear();
2840 }
2841 /* iterator ended normally */
2842 x = v = POP();
2843 Py_DECREF(v);
2844 JUMPBY(oparg);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002845 DISPATCH();
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002846 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002847
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002848 TARGET_NOARG(BREAK_LOOP)
2849 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002850 why = WHY_BREAK;
2851 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002852 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002853
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002854 TARGET(CONTINUE_LOOP)
2855 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002856 retval = PyInt_FromLong(oparg);
2857 if (!retval) {
2858 x = NULL;
2859 break;
2860 }
2861 why = WHY_CONTINUE;
2862 goto fast_block_end;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002863 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002864
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002865 TARGET_WITH_IMPL(SETUP_LOOP, _setup_finally)
2866 TARGET_WITH_IMPL(SETUP_EXCEPT, _setup_finally)
Benjamin Peterson14462d42015-08-19 20:38:39 -07002867 TARGET(SETUP_FINALLY)
2868 _setup_finally:
2869 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002870 /* NOTE: If you add any new block-setup opcodes that
2871 are not try/except/finally handlers, you may need
2872 to update the PyGen_NeedsFinalizing() function.
2873 */
Phillip J. Eby2ba96612006-04-10 17:51:05 +00002874
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002875 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2876 STACK_LEVEL());
Benjamin Peterson14462d42015-08-19 20:38:39 -07002877 DISPATCH();
2878 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002879
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002880
2881
2882 TARGET(SETUP_WITH)
2883 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002884 {
2885 static PyObject *exit, *enter;
2886 w = TOP();
2887 x = special_lookup(w, "__exit__", &exit);
2888 if (!x)
2889 break;
2890 SET_TOP(x);
2891 u = special_lookup(w, "__enter__", &enter);
2892 Py_DECREF(w);
2893 if (!u) {
2894 x = NULL;
2895 break;
2896 }
2897 x = PyObject_CallFunctionObjArgs(u, NULL);
2898 Py_DECREF(u);
2899 if (!x)
2900 break;
2901 /* Setup a finally block (SETUP_WITH as a block is
2902 equivalent to SETUP_FINALLY except it normalizes
2903 the exception) before pushing the result of
2904 __enter__ on the stack. */
2905 PyFrame_BlockSetup(f, SETUP_WITH, INSTR_OFFSET() + oparg,
2906 STACK_LEVEL());
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002907
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002908 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07002909 DISPATCH();
2910 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002911 }
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00002912
Benjamin Peterson2c992a02015-05-28 12:45:31 -05002913 TARGET_NOARG(WITH_CLEANUP)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002914 {
2915 /* At the top of the stack are 1-3 values indicating
2916 how/why we entered the finally clause:
2917 - TOP = None
2918 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2919 - TOP = WHY_*; no retval below it
2920 - (TOP, SECOND, THIRD) = exc_info()
2921 Below them is EXIT, the context.__exit__ bound method.
2922 In the last case, we must call
2923 EXIT(TOP, SECOND, THIRD)
2924 otherwise we must call
2925 EXIT(None, None, None)
Nick Coghlan7af53be2008-03-07 14:13:28 +00002926
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002927 In all cases, we remove EXIT from the stack, leaving
2928 the rest in the same order.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002929
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002930 In addition, if the stack represents an exception,
2931 *and* the function call returns a 'true' value, we
2932 "zap" this information, to prevent END_FINALLY from
2933 re-raising the exception. (But non-local gotos
2934 should still be resumed.)
2935 */
Tim Peters7df5e7f2006-05-26 23:14:37 +00002936
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002937 PyObject *exit_func;
Nick Coghlan7af53be2008-03-07 14:13:28 +00002938
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002939 u = POP();
2940 if (u == Py_None) {
2941 exit_func = TOP();
2942 SET_TOP(u);
2943 v = w = Py_None;
2944 }
2945 else if (PyInt_Check(u)) {
2946 switch(PyInt_AS_LONG(u)) {
2947 case WHY_RETURN:
2948 case WHY_CONTINUE:
2949 /* Retval in TOP. */
2950 exit_func = SECOND();
2951 SET_SECOND(TOP());
2952 SET_TOP(u);
2953 break;
2954 default:
2955 exit_func = TOP();
2956 SET_TOP(u);
2957 break;
2958 }
2959 u = v = w = Py_None;
2960 }
2961 else {
2962 v = TOP();
2963 w = SECOND();
2964 exit_func = THIRD();
2965 SET_TOP(u);
2966 SET_SECOND(v);
2967 SET_THIRD(w);
2968 }
2969 /* XXX Not the fastest way to call it... */
2970 x = PyObject_CallFunctionObjArgs(exit_func, u, v, w,
2971 NULL);
2972 Py_DECREF(exit_func);
2973 if (x == NULL)
2974 break; /* Go to error exit */
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002975
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002976 if (u != Py_None)
2977 err = PyObject_IsTrue(x);
2978 else
2979 err = 0;
2980 Py_DECREF(x);
Amaury Forgeot d'Arcad9b5992008-12-10 23:22:49 +00002981
Antoine Pitrouc83ea132010-05-09 14:46:46 +00002982 if (err < 0)
2983 break; /* Go to error exit */
2984 else if (err > 0) {
2985 err = 0;
2986 /* There was an exception and a true return */
2987 STACKADJ(-2);
2988 Py_INCREF(Py_None);
2989 SET_TOP(Py_None);
2990 Py_DECREF(u);
2991 Py_DECREF(v);
2992 Py_DECREF(w);
2993 } else {
2994 /* The stack was rearranged to remove EXIT
2995 above. Let END_FINALLY do its thing */
2996 }
2997 PREDICT(END_FINALLY);
2998 break;
2999 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003000
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003001 TARGET(CALL_FUNCTION)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003002 {
3003 PyObject **sp;
3004 PCALL(PCALL_ALL);
3005 sp = stack_pointer;
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003006#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003007 x = call_function(&sp, oparg, &intr0, &intr1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003008#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003009 x = call_function(&sp, oparg);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003010#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003011 stack_pointer = sp;
3012 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003013 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003014 break;
3015 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003016
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003017 TARGET_WITH_IMPL(CALL_FUNCTION_VAR, _call_function_var_kw)
3018 TARGET_WITH_IMPL(CALL_FUNCTION_KW, _call_function_var_kw)
Benjamin Peterson14462d42015-08-19 20:38:39 -07003019 TARGET(CALL_FUNCTION_VAR_KW)
3020 _call_function_var_kw:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003021 {
3022 int na = oparg & 0xff;
3023 int nk = (oparg>>8) & 0xff;
3024 int flags = (opcode - CALL_FUNCTION) & 3;
3025 int n = na + 2 * nk;
3026 PyObject **pfunc, *func, **sp;
3027 PCALL(PCALL_ALL);
3028 if (flags & CALL_FLAG_VAR)
3029 n++;
3030 if (flags & CALL_FLAG_KW)
3031 n++;
3032 pfunc = stack_pointer - n - 1;
3033 func = *pfunc;
Jeremy Hylton52820442001-01-03 23:52:36 +00003034
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003035 if (PyMethod_Check(func)
Stefan Krah7ff78252010-06-23 18:12:09 +00003036 && PyMethod_GET_SELF(func) != NULL) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003037 PyObject *self = PyMethod_GET_SELF(func);
3038 Py_INCREF(self);
3039 func = PyMethod_GET_FUNCTION(func);
3040 Py_INCREF(func);
3041 Py_DECREF(*pfunc);
3042 *pfunc = self;
3043 na++;
3044 } else
3045 Py_INCREF(func);
3046 sp = stack_pointer;
3047 READ_TIMESTAMP(intr0);
3048 x = ext_do_call(func, &sp, flags, na, nk);
3049 READ_TIMESTAMP(intr1);
3050 stack_pointer = sp;
3051 Py_DECREF(func);
Jeremy Hylton52820442001-01-03 23:52:36 +00003052
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003053 while (stack_pointer > pfunc) {
3054 w = POP();
3055 Py_DECREF(w);
3056 }
3057 PUSH(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003058 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003059 break;
3060 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003061
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003062
3063 TARGET(MAKE_FUNCTION)
3064 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003065 v = POP(); /* code object */
3066 x = PyFunction_New(v, f->f_globals);
3067 Py_DECREF(v);
3068 /* XXX Maybe this should be a separate opcode? */
3069 if (x != NULL && oparg > 0) {
3070 v = PyTuple_New(oparg);
3071 if (v == NULL) {
3072 Py_DECREF(x);
3073 x = NULL;
3074 break;
3075 }
3076 while (--oparg >= 0) {
3077 w = POP();
3078 PyTuple_SET_ITEM(v, oparg, w);
3079 }
3080 err = PyFunction_SetDefaults(x, v);
3081 Py_DECREF(v);
3082 }
3083 PUSH(x);
3084 break;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003085 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003086
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003087 TARGET(MAKE_CLOSURE)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003088 {
3089 v = POP(); /* code object */
3090 x = PyFunction_New(v, f->f_globals);
3091 Py_DECREF(v);
3092 if (x != NULL) {
3093 v = POP();
3094 if (PyFunction_SetClosure(x, v) != 0) {
3095 /* Can't happen unless bytecode is corrupt. */
3096 why = WHY_EXCEPTION;
3097 }
3098 Py_DECREF(v);
3099 }
3100 if (x != NULL && oparg > 0) {
3101 v = PyTuple_New(oparg);
3102 if (v == NULL) {
3103 Py_DECREF(x);
3104 x = NULL;
3105 break;
3106 }
3107 while (--oparg >= 0) {
3108 w = POP();
3109 PyTuple_SET_ITEM(v, oparg, w);
3110 }
3111 if (PyFunction_SetDefaults(x, v) != 0) {
3112 /* Can't happen unless
3113 PyFunction_SetDefaults changes. */
3114 why = WHY_EXCEPTION;
3115 }
3116 Py_DECREF(v);
3117 }
3118 PUSH(x);
3119 break;
3120 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003121
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003122 TARGET(BUILD_SLICE)
3123 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003124 if (oparg == 3)
3125 w = POP();
3126 else
3127 w = NULL;
3128 v = POP();
3129 u = TOP();
3130 x = PySlice_New(u, v, w);
3131 Py_DECREF(u);
3132 Py_DECREF(v);
3133 Py_XDECREF(w);
3134 SET_TOP(x);
Benjamin Peterson14462d42015-08-19 20:38:39 -07003135 if (x != NULL) DISPATCH();
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003136 break;
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 TARGET(EXTENDED_ARG)
3140 {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003141 opcode = NEXTOP();
3142 oparg = oparg<<16 | NEXTARG();
3143 goto dispatch_opcode;
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003144 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003145
Benjamin Peterson2c992a02015-05-28 12:45:31 -05003146#if USE_COMPUTED_GOTOS
3147 _unknown_opcode:
3148#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003149 default:
3150 fprintf(stderr,
3151 "XXX lineno: %d, opcode: %d\n",
3152 PyFrame_GetLineNumber(f),
3153 opcode);
3154 PyErr_SetString(PyExc_SystemError, "unknown opcode");
3155 why = WHY_EXCEPTION;
3156 break;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003157
3158#ifdef CASE_TOO_BIG
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003159 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003160#endif
3161
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003162 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003163
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003164 on_error:
Guido van Rossumac7be682001-01-17 15:42:30 +00003165
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003166 READ_TIMESTAMP(inst1);
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003167
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003168 /* Quickly continue if no error occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003169
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003170 if (why == WHY_NOT) {
3171 if (err == 0 && x != NULL) {
Guido van Rossum681d79a1995-07-18 14:51:37 +00003172#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003173 /* This check is expensive! */
3174 if (PyErr_Occurred())
3175 fprintf(stderr,
3176 "XXX undetected error\n");
3177 else {
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003178#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003179 READ_TIMESTAMP(loop1);
3180 continue; /* Normal, fast path */
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003181#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003182 }
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003183#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003184 }
3185 why = WHY_EXCEPTION;
3186 x = Py_None;
3187 err = 0;
3188 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003189
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003190 /* Double-check exception status */
Guido van Rossumac7be682001-01-17 15:42:30 +00003191
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003192 if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
3193 if (!PyErr_Occurred()) {
3194 PyErr_SetString(PyExc_SystemError,
3195 "error return without exception set");
3196 why = WHY_EXCEPTION;
3197 }
3198 }
Guido van Rossumeb894eb1999-03-09 16:16:45 +00003199#ifdef CHECKEXC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003200 else {
3201 /* This check is expensive! */
3202 if (PyErr_Occurred()) {
3203 char buf[128];
3204 sprintf(buf, "Stack unwind with exception "
3205 "set and why=%d", why);
3206 Py_FatalError(buf);
3207 }
3208 }
Guido van Rossum374a9221991-04-04 10:40:29 +00003209#endif
3210
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003211 /* Log traceback info if this is a real exception */
Guido van Rossumac7be682001-01-17 15:42:30 +00003212
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003213 if (why == WHY_EXCEPTION) {
3214 PyTraceBack_Here(f);
Guido van Rossum96a42c81992-01-12 02:29:51 +00003215
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003216 if (tstate->c_tracefunc != NULL)
3217 call_exc_trace(tstate->c_tracefunc,
3218 tstate->c_traceobj, f);
3219 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003220
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003221 /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
Guido van Rossumac7be682001-01-17 15:42:30 +00003222
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003223 if (why == WHY_RERAISE)
3224 why = WHY_EXCEPTION;
Guido van Rossum374a9221991-04-04 10:40:29 +00003225
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003226 /* Unwind stacks if a (pseudo) exception occurred */
Guido van Rossumac7be682001-01-17 15:42:30 +00003227
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003228fast_block_end:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003229 while (why != WHY_NOT && f->f_iblock > 0) {
3230 /* Peek at the current block. */
3231 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003232
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003233 assert(why != WHY_YIELD);
3234 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3235 why = WHY_NOT;
3236 JUMPTO(PyInt_AS_LONG(retval));
3237 Py_DECREF(retval);
3238 break;
3239 }
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003241 /* Now we have to pop the block. */
3242 f->f_iblock--;
Benjamin Peterson4a3cf192009-07-01 23:45:19 +00003243
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003244 while (STACK_LEVEL() > b->b_level) {
3245 v = POP();
3246 Py_XDECREF(v);
3247 }
3248 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3249 why = WHY_NOT;
3250 JUMPTO(b->b_handler);
3251 break;
3252 }
3253 if (b->b_type == SETUP_FINALLY ||
3254 (b->b_type == SETUP_EXCEPT &&
3255 why == WHY_EXCEPTION) ||
3256 b->b_type == SETUP_WITH) {
3257 if (why == WHY_EXCEPTION) {
3258 PyObject *exc, *val, *tb;
3259 PyErr_Fetch(&exc, &val, &tb);
3260 if (val == NULL) {
3261 val = Py_None;
3262 Py_INCREF(val);
3263 }
3264 /* Make the raw exception data
3265 available to the handler,
3266 so a program can emulate the
3267 Python main loop. Don't do
3268 this for 'finally'. */
3269 if (b->b_type == SETUP_EXCEPT ||
3270 b->b_type == SETUP_WITH) {
3271 PyErr_NormalizeException(
3272 &exc, &val, &tb);
3273 set_exc_info(tstate,
3274 exc, val, tb);
3275 }
3276 if (tb == NULL) {
3277 Py_INCREF(Py_None);
3278 PUSH(Py_None);
3279 } else
3280 PUSH(tb);
3281 PUSH(val);
3282 PUSH(exc);
3283 }
3284 else {
3285 if (why & (WHY_RETURN | WHY_CONTINUE))
3286 PUSH(retval);
3287 v = PyInt_FromLong((long)why);
3288 PUSH(v);
3289 }
3290 why = WHY_NOT;
3291 JUMPTO(b->b_handler);
3292 break;
3293 }
3294 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003295
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003296 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003297
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003298 if (why != WHY_NOT)
3299 break;
3300 READ_TIMESTAMP(loop1);
Guido van Rossumac7be682001-01-17 15:42:30 +00003301
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003302 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003303
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003304 assert(why != WHY_YIELD);
3305 /* Pop remaining stack entries. */
3306 while (!EMPTY()) {
3307 v = POP();
3308 Py_XDECREF(v);
3309 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003310
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003311 if (why != WHY_RETURN)
3312 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003313
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003314fast_yield:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003315 if (tstate->use_tracing) {
3316 if (tstate->c_tracefunc) {
3317 if (why == WHY_RETURN || why == WHY_YIELD) {
3318 if (call_trace(tstate->c_tracefunc,
3319 tstate->c_traceobj, f,
3320 PyTrace_RETURN, retval)) {
3321 Py_XDECREF(retval);
3322 retval = NULL;
3323 why = WHY_EXCEPTION;
3324 }
3325 }
3326 else if (why == WHY_EXCEPTION) {
3327 call_trace_protected(tstate->c_tracefunc,
3328 tstate->c_traceobj, f,
3329 PyTrace_RETURN, NULL);
3330 }
3331 }
3332 if (tstate->c_profilefunc) {
3333 if (why == WHY_EXCEPTION)
3334 call_trace_protected(tstate->c_profilefunc,
3335 tstate->c_profileobj, f,
3336 PyTrace_RETURN, NULL);
3337 else if (call_trace(tstate->c_profilefunc,
3338 tstate->c_profileobj, f,
3339 PyTrace_RETURN, retval)) {
3340 Py_XDECREF(retval);
3341 retval = NULL;
3342 why = WHY_EXCEPTION;
3343 }
3344 }
3345 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003346
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003347 if (tstate->frame->f_exc_type != NULL)
3348 reset_exc_info(tstate);
3349 else {
3350 assert(tstate->frame->f_exc_value == NULL);
3351 assert(tstate->frame->f_exc_traceback == NULL);
3352 }
Guido van Rossuma027efa1997-05-05 20:56:21 +00003353
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003354 /* pop frame */
Thomas Woutersae406c62007-09-19 17:27:43 +00003355exit_eval_frame:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003356 Py_LeaveRecursiveCall();
3357 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003358
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003359 return retval;
Guido van Rossum374a9221991-04-04 10:40:29 +00003360}
3361
Guido van Rossumc2e20742006-02-27 22:32:47 +00003362/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003363 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003364 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003365
Tim Peters6d6c1a32001-08-02 04:15:00 +00003366PyObject *
3367PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003368 PyObject **args, int argcount, PyObject **kws, int kwcount,
3369 PyObject **defs, int defcount, PyObject *closure)
Tim Peters5ca576e2001-06-18 22:08:13 +00003370{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003371 register PyFrameObject *f;
3372 register PyObject *retval = NULL;
3373 register PyObject **fastlocals, **freevars;
3374 PyThreadState *tstate = PyThreadState_GET();
3375 PyObject *x, *u;
Tim Peters5ca576e2001-06-18 22:08:13 +00003376
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003377 if (globals == NULL) {
3378 PyErr_SetString(PyExc_SystemError,
3379 "PyEval_EvalCodeEx: NULL globals");
3380 return NULL;
3381 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003382
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003383 assert(tstate != NULL);
3384 assert(globals != NULL);
3385 f = PyFrame_New(tstate, co, globals, locals);
3386 if (f == NULL)
3387 return NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +00003388
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003389 fastlocals = f->f_localsplus;
3390 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003391
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003392 if (co->co_argcount > 0 ||
3393 co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
3394 int i;
3395 int n = argcount;
3396 PyObject *kwdict = NULL;
3397 if (co->co_flags & CO_VARKEYWORDS) {
3398 kwdict = PyDict_New();
3399 if (kwdict == NULL)
3400 goto fail;
3401 i = co->co_argcount;
3402 if (co->co_flags & CO_VARARGS)
3403 i++;
3404 SETLOCAL(i, kwdict);
3405 }
3406 if (argcount > co->co_argcount) {
3407 if (!(co->co_flags & CO_VARARGS)) {
3408 PyErr_Format(PyExc_TypeError,
3409 "%.200s() takes %s %d "
3410 "argument%s (%d given)",
3411 PyString_AsString(co->co_name),
3412 defcount ? "at most" : "exactly",
3413 co->co_argcount,
3414 co->co_argcount == 1 ? "" : "s",
Benjamin Petersonda4faba2010-09-25 03:27:12 +00003415 argcount + kwcount);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003416 goto fail;
3417 }
3418 n = co->co_argcount;
3419 }
3420 for (i = 0; i < n; i++) {
3421 x = args[i];
3422 Py_INCREF(x);
3423 SETLOCAL(i, x);
3424 }
3425 if (co->co_flags & CO_VARARGS) {
3426 u = PyTuple_New(argcount - n);
3427 if (u == NULL)
3428 goto fail;
3429 SETLOCAL(co->co_argcount, u);
3430 for (i = n; i < argcount; i++) {
3431 x = args[i];
3432 Py_INCREF(x);
3433 PyTuple_SET_ITEM(u, i-n, x);
3434 }
3435 }
3436 for (i = 0; i < kwcount; i++) {
3437 PyObject **co_varnames;
3438 PyObject *keyword = kws[2*i];
3439 PyObject *value = kws[2*i + 1];
3440 int j;
3441 if (keyword == NULL || !(PyString_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003442#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003443 || PyUnicode_Check(keyword)
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003444#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003445 )) {
3446 PyErr_Format(PyExc_TypeError,
3447 "%.200s() keywords must be strings",
3448 PyString_AsString(co->co_name));
3449 goto fail;
3450 }
3451 /* Speed hack: do raw pointer compares. As names are
3452 normally interned this should almost always hit. */
3453 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3454 for (j = 0; j < co->co_argcount; j++) {
3455 PyObject *nm = co_varnames[j];
3456 if (nm == keyword)
3457 goto kw_found;
3458 }
3459 /* Slow fallback, just in case */
3460 for (j = 0; j < co->co_argcount; j++) {
3461 PyObject *nm = co_varnames[j];
3462 int cmp = PyObject_RichCompareBool(
3463 keyword, nm, Py_EQ);
3464 if (cmp > 0)
3465 goto kw_found;
3466 else if (cmp < 0)
3467 goto fail;
3468 }
3469 if (kwdict == NULL) {
3470 PyObject *kwd_str = kwd_as_string(keyword);
3471 if (kwd_str) {
3472 PyErr_Format(PyExc_TypeError,
3473 "%.200s() got an unexpected "
3474 "keyword argument '%.400s'",
3475 PyString_AsString(co->co_name),
3476 PyString_AsString(kwd_str));
3477 Py_DECREF(kwd_str);
3478 }
3479 goto fail;
3480 }
3481 PyDict_SetItem(kwdict, keyword, value);
3482 continue;
3483 kw_found:
3484 if (GETLOCAL(j) != NULL) {
3485 PyObject *kwd_str = kwd_as_string(keyword);
3486 if (kwd_str) {
3487 PyErr_Format(PyExc_TypeError,
3488 "%.200s() got multiple "
3489 "values for keyword "
3490 "argument '%.400s'",
3491 PyString_AsString(co->co_name),
3492 PyString_AsString(kwd_str));
3493 Py_DECREF(kwd_str);
3494 }
3495 goto fail;
3496 }
3497 Py_INCREF(value);
3498 SETLOCAL(j, value);
3499 }
3500 if (argcount < co->co_argcount) {
3501 int m = co->co_argcount - defcount;
3502 for (i = argcount; i < m; i++) {
3503 if (GETLOCAL(i) == NULL) {
3504 int j, given = 0;
3505 for (j = 0; j < co->co_argcount; j++)
3506 if (GETLOCAL(j))
3507 given++;
3508 PyErr_Format(PyExc_TypeError,
3509 "%.200s() takes %s %d "
3510 "argument%s (%d given)",
3511 PyString_AsString(co->co_name),
3512 ((co->co_flags & CO_VARARGS) ||
3513 defcount) ? "at least"
3514 : "exactly",
3515 m, m == 1 ? "" : "s", given);
3516 goto fail;
3517 }
3518 }
3519 if (n > m)
3520 i = n - m;
3521 else
3522 i = 0;
3523 for (; i < defcount; i++) {
3524 if (GETLOCAL(m+i) == NULL) {
3525 PyObject *def = defs[i];
3526 Py_INCREF(def);
3527 SETLOCAL(m+i, def);
3528 }
3529 }
3530 }
3531 }
3532 else if (argcount > 0 || kwcount > 0) {
3533 PyErr_Format(PyExc_TypeError,
3534 "%.200s() takes no arguments (%d given)",
3535 PyString_AsString(co->co_name),
3536 argcount + kwcount);
3537 goto fail;
3538 }
3539 /* Allocate and initialize storage for cell vars, and copy free
3540 vars into frame. This isn't too efficient right now. */
3541 if (PyTuple_GET_SIZE(co->co_cellvars)) {
3542 int i, j, nargs, found;
3543 char *cellname, *argname;
3544 PyObject *c;
Tim Peters5ca576e2001-06-18 22:08:13 +00003545
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003546 nargs = co->co_argcount;
3547 if (co->co_flags & CO_VARARGS)
3548 nargs++;
3549 if (co->co_flags & CO_VARKEYWORDS)
3550 nargs++;
Tim Peters5ca576e2001-06-18 22:08:13 +00003551
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003552 /* Initialize each cell var, taking into account
3553 cell vars that are initialized from arguments.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003555 Should arrange for the compiler to put cellvars
3556 that are arguments at the beginning of the cellvars
3557 list so that we can march over it more efficiently?
3558 */
3559 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
3560 cellname = PyString_AS_STRING(
3561 PyTuple_GET_ITEM(co->co_cellvars, i));
3562 found = 0;
3563 for (j = 0; j < nargs; j++) {
3564 argname = PyString_AS_STRING(
3565 PyTuple_GET_ITEM(co->co_varnames, j));
3566 if (strcmp(cellname, argname) == 0) {
3567 c = PyCell_New(GETLOCAL(j));
3568 if (c == NULL)
3569 goto fail;
3570 GETLOCAL(co->co_nlocals + i) = c;
3571 found = 1;
3572 break;
3573 }
3574 }
3575 if (found == 0) {
3576 c = PyCell_New(NULL);
3577 if (c == NULL)
3578 goto fail;
3579 SETLOCAL(co->co_nlocals + i, c);
3580 }
3581 }
3582 }
3583 if (PyTuple_GET_SIZE(co->co_freevars)) {
3584 int i;
3585 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3586 PyObject *o = PyTuple_GET_ITEM(closure, i);
3587 Py_INCREF(o);
3588 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
3589 }
3590 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003591
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003592 if (co->co_flags & CO_GENERATOR) {
3593 /* Don't need to keep the reference to f_back, it will be set
3594 * when the generator is resumed. */
Serhiy Storchaka98a97222014-02-09 13:14:04 +02003595 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003596
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003597 PCALL(PCALL_GENERATOR);
Jeremy Hylton985eba52003-02-05 23:13:00 +00003598
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003599 /* Create a new generator that owns the ready to run frame
3600 * and return that as the value. */
3601 return PyGen_New(f);
3602 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003603
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003604 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003605
Thomas Woutersae406c62007-09-19 17:27:43 +00003606fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003607
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003608 /* decref'ing the frame can cause __del__ methods to get invoked,
3609 which can call back into Python. While we're done with the
3610 current Python frame (f), the associated C stack is still in use,
3611 so recursion_depth must be boosted for the duration.
3612 */
3613 assert(tstate != NULL);
3614 ++tstate->recursion_depth;
3615 Py_DECREF(f);
3616 --tstate->recursion_depth;
3617 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003618}
3619
3620
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003621static PyObject *
3622special_lookup(PyObject *o, char *meth, PyObject **cache)
3623{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003624 PyObject *res;
3625 if (PyInstance_Check(o)) {
3626 if (!*cache)
3627 return PyObject_GetAttrString(o, meth);
3628 else
3629 return PyObject_GetAttr(o, *cache);
3630 }
3631 res = _PyObject_LookupSpecial(o, meth, cache);
3632 if (res == NULL && !PyErr_Occurred()) {
3633 PyErr_SetObject(PyExc_AttributeError, *cache);
3634 return NULL;
3635 }
3636 return res;
Benjamin Peterson1880d8b2009-05-25 13:13:44 +00003637}
3638
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003639
Benjamin Petersone18ef192009-01-20 14:21:16 +00003640static PyObject *
3641kwd_as_string(PyObject *kwd) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003642#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003643 if (PyString_Check(kwd)) {
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003644#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003645 assert(PyString_Check(kwd));
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003646#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003647 Py_INCREF(kwd);
3648 return kwd;
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003649#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003650 }
3651 return _PyUnicode_AsDefaultEncodedString(kwd, "replace");
Benjamin Peterson78821dd2009-01-25 17:15:10 +00003652#endif
Benjamin Petersone18ef192009-01-20 14:21:16 +00003653}
3654
3655
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003656/* Implementation notes for set_exc_info() and reset_exc_info():
3657
3658- Below, 'exc_ZZZ' stands for 'exc_type', 'exc_value' and
3659 'exc_traceback'. These always travel together.
3660
3661- tstate->curexc_ZZZ is the "hot" exception that is set by
3662 PyErr_SetString(), cleared by PyErr_Clear(), and so on.
3663
3664- Once an exception is caught by an except clause, it is transferred
3665 from tstate->curexc_ZZZ to tstate->exc_ZZZ, from which sys.exc_info()
3666 can pick it up. This is the primary task of set_exc_info().
Tim Peters7df5e7f2006-05-26 23:14:37 +00003667 XXX That can't be right: set_exc_info() doesn't look at tstate->curexc_ZZZ.
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003668
3669- Now let me explain the complicated dance with frame->f_exc_ZZZ.
3670
3671 Long ago, when none of this existed, there were just a few globals:
3672 one set corresponding to the "hot" exception, and one set
3673 corresponding to sys.exc_ZZZ. (Actually, the latter weren't C
3674 globals; they were simply stored as sys.exc_ZZZ. For backwards
3675 compatibility, they still are!) The problem was that in code like
3676 this:
3677
3678 try:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003679 "something that may fail"
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003680 except "some exception":
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003681 "do something else first"
3682 "print the exception from sys.exc_ZZZ."
Guido van Rossumc9fbb722003-03-01 03:36:33 +00003683
3684 if "do something else first" invoked something that raised and caught
3685 an exception, sys.exc_ZZZ were overwritten. That was a frequent
3686 cause of subtle bugs. I fixed this by changing the semantics as
3687 follows:
3688
3689 - Within one frame, sys.exc_ZZZ will hold the last exception caught
3690 *in that frame*.
3691
3692 - But initially, and as long as no exception is caught in a given
3693 frame, sys.exc_ZZZ will hold the last exception caught in the
3694 previous frame (or the frame before that, etc.).
3695
3696 The first bullet fixed the bug in the above example. The second
3697 bullet was for backwards compatibility: it was (and is) common to
3698 have a function that is called when an exception is caught, and to
3699 have that function access the caught exception via sys.exc_ZZZ.
3700 (Example: traceback.print_exc()).
3701
3702 At the same time I fixed the problem that sys.exc_ZZZ weren't
3703 thread-safe, by introducing sys.exc_info() which gets it from tstate;
3704 but that's really a separate improvement.
3705
3706 The reset_exc_info() function in ceval.c restores the tstate->exc_ZZZ
3707 variables to what they were before the current frame was called. The
3708 set_exc_info() function saves them on the frame so that
3709 reset_exc_info() can restore them. The invariant is that
3710 frame->f_exc_ZZZ is NULL iff the current frame never caught an
3711 exception (where "catching" an exception applies only to successful
3712 except clauses); and if the current frame ever caught an exception,
3713 frame->f_exc_ZZZ is the exception that was stored in tstate->exc_ZZZ
3714 at the start of the current frame.
3715
3716*/
3717
Fredrik Lundh7a830892006-05-27 10:39:48 +00003718static void
Guido van Rossumac7be682001-01-17 15:42:30 +00003719set_exc_info(PyThreadState *tstate,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003720 PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003721{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003722 PyFrameObject *frame = tstate->frame;
3723 PyObject *tmp_type, *tmp_value, *tmp_tb;
Barry Warsaw4249f541997-08-22 21:26:19 +00003724
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003725 assert(type != NULL);
3726 assert(frame != NULL);
3727 if (frame->f_exc_type == NULL) {
3728 assert(frame->f_exc_value == NULL);
3729 assert(frame->f_exc_traceback == NULL);
3730 /* This frame didn't catch an exception before. */
3731 /* Save previous exception of this thread in this frame. */
3732 if (tstate->exc_type == NULL) {
3733 /* XXX Why is this set to Py_None? */
3734 Py_INCREF(Py_None);
3735 tstate->exc_type = Py_None;
3736 }
3737 Py_INCREF(tstate->exc_type);
3738 Py_XINCREF(tstate->exc_value);
3739 Py_XINCREF(tstate->exc_traceback);
3740 frame->f_exc_type = tstate->exc_type;
3741 frame->f_exc_value = tstate->exc_value;
3742 frame->f_exc_traceback = tstate->exc_traceback;
3743 }
3744 /* Set new exception for this thread. */
3745 tmp_type = tstate->exc_type;
3746 tmp_value = tstate->exc_value;
3747 tmp_tb = tstate->exc_traceback;
3748 Py_INCREF(type);
3749 Py_XINCREF(value);
3750 Py_XINCREF(tb);
3751 tstate->exc_type = type;
3752 tstate->exc_value = value;
3753 tstate->exc_traceback = tb;
3754 Py_XDECREF(tmp_type);
3755 Py_XDECREF(tmp_value);
3756 Py_XDECREF(tmp_tb);
3757 /* For b/w compatibility */
3758 PySys_SetObject("exc_type", type);
3759 PySys_SetObject("exc_value", value);
3760 PySys_SetObject("exc_traceback", tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003761}
3762
Fredrik Lundh7a830892006-05-27 10:39:48 +00003763static void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003764reset_exc_info(PyThreadState *tstate)
Guido van Rossuma027efa1997-05-05 20:56:21 +00003765{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003766 PyFrameObject *frame;
3767 PyObject *tmp_type, *tmp_value, *tmp_tb;
Tim Peters7df5e7f2006-05-26 23:14:37 +00003768
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003769 /* It's a precondition that the thread state's frame caught an
3770 * exception -- verify in a debug build.
3771 */
3772 assert(tstate != NULL);
3773 frame = tstate->frame;
3774 assert(frame != NULL);
3775 assert(frame->f_exc_type != NULL);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003776
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003777 /* Copy the frame's exception info back to the thread state. */
3778 tmp_type = tstate->exc_type;
3779 tmp_value = tstate->exc_value;
3780 tmp_tb = tstate->exc_traceback;
3781 Py_INCREF(frame->f_exc_type);
3782 Py_XINCREF(frame->f_exc_value);
3783 Py_XINCREF(frame->f_exc_traceback);
3784 tstate->exc_type = frame->f_exc_type;
3785 tstate->exc_value = frame->f_exc_value;
3786 tstate->exc_traceback = frame->f_exc_traceback;
3787 Py_XDECREF(tmp_type);
3788 Py_XDECREF(tmp_value);
3789 Py_XDECREF(tmp_tb);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003790
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003791 /* For b/w compatibility */
3792 PySys_SetObject("exc_type", frame->f_exc_type);
3793 PySys_SetObject("exc_value", frame->f_exc_value);
3794 PySys_SetObject("exc_traceback", frame->f_exc_traceback);
Tim Peters7df5e7f2006-05-26 23:14:37 +00003795
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003796 /* Clear the frame's exception info. */
3797 tmp_type = frame->f_exc_type;
3798 tmp_value = frame->f_exc_value;
3799 tmp_tb = frame->f_exc_traceback;
3800 frame->f_exc_type = NULL;
3801 frame->f_exc_value = NULL;
3802 frame->f_exc_traceback = NULL;
3803 Py_DECREF(tmp_type);
3804 Py_XDECREF(tmp_value);
3805 Py_XDECREF(tmp_tb);
Guido van Rossuma027efa1997-05-05 20:56:21 +00003806}
3807
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003808/* Logic for the raise statement (too complicated for inlining).
3809 This *consumes* a reference count to each of its arguments. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00003810static enum why_code
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003811do_raise(PyObject *type, PyObject *value, PyObject *tb)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003812{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003813 if (type == NULL) {
3814 /* Reraise */
3815 PyThreadState *tstate = PyThreadState_GET();
3816 type = tstate->exc_type == NULL ? Py_None : tstate->exc_type;
3817 value = tstate->exc_value;
3818 tb = tstate->exc_traceback;
3819 Py_XINCREF(type);
3820 Py_XINCREF(value);
3821 Py_XINCREF(tb);
3822 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003823
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003824 /* We support the following forms of raise:
3825 raise <class>, <classinstance>
3826 raise <class>, <argument tuple>
3827 raise <class>, None
3828 raise <class>, <argument>
3829 raise <classinstance>, None
3830 raise <string>, <object>
3831 raise <string>, None
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003832
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003833 An omitted second argument is the same as None.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003834
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003835 In addition, raise <tuple>, <anything> is the same as
3836 raising the tuple's first item (and it better have one!);
3837 this rule is applied recursively.
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003838
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003839 Finally, an optional third argument can be supplied, which
3840 gives the traceback to be substituted (useful when
3841 re-raising an exception after examining it). */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003842
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003843 /* First, check the traceback argument, replacing None with
3844 NULL. */
3845 if (tb == Py_None) {
3846 Py_DECREF(tb);
3847 tb = NULL;
3848 }
3849 else if (tb != NULL && !PyTraceBack_Check(tb)) {
3850 PyErr_SetString(PyExc_TypeError,
3851 "raise: arg 3 must be a traceback or None");
3852 goto raise_error;
3853 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003854
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003855 /* Next, replace a missing value with None */
3856 if (value == NULL) {
3857 value = Py_None;
3858 Py_INCREF(value);
3859 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003860
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003861 /* Next, repeatedly, replace a tuple exception with its first item */
3862 while (PyTuple_Check(type) && PyTuple_Size(type) > 0) {
3863 PyObject *tmp = type;
3864 type = PyTuple_GET_ITEM(type, 0);
3865 Py_INCREF(type);
3866 Py_DECREF(tmp);
3867 }
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003868
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003869 if (PyExceptionClass_Check(type)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003870 PyErr_NormalizeException(&type, &value, &tb);
Benjamin Petersonc3349cd2011-07-15 14:15:40 -05003871 if (!PyExceptionInstance_Check(value)) {
3872 PyErr_Format(PyExc_TypeError,
3873 "calling %s() should have returned an instance of "
3874 "BaseException, not '%s'",
3875 ((PyTypeObject *)type)->tp_name,
3876 Py_TYPE(value)->tp_name);
3877 goto raise_error;
3878 }
3879 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003880 else if (PyExceptionInstance_Check(type)) {
3881 /* Raising an instance. The value should be a dummy. */
3882 if (value != Py_None) {
3883 PyErr_SetString(PyExc_TypeError,
3884 "instance exception may not have a separate value");
3885 goto raise_error;
3886 }
3887 else {
3888 /* Normalize to raise <class>, <instance> */
3889 Py_DECREF(value);
3890 value = type;
3891 type = PyExceptionInstance_Class(type);
3892 Py_INCREF(type);
3893 }
3894 }
3895 else {
3896 /* Not something you can raise. You get an exception
3897 anyway, just not what you specified :-) */
3898 PyErr_Format(PyExc_TypeError,
3899 "exceptions must be old-style classes or "
3900 "derived from BaseException, not %s",
3901 type->ob_type->tp_name);
3902 goto raise_error;
3903 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003904
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003905 assert(PyExceptionClass_Check(type));
3906 if (Py_Py3kWarningFlag && PyClass_Check(type)) {
3907 if (PyErr_WarnEx(PyExc_DeprecationWarning,
3908 "exceptions must derive from BaseException "
3909 "in 3.x", 1) < 0)
3910 goto raise_error;
3911 }
Guido van Rossum504153d2008-03-18 04:26:48 +00003912
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003913 PyErr_Restore(type, value, tb);
3914 if (tb == NULL)
3915 return WHY_EXCEPTION;
3916 else
3917 return WHY_RERAISE;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003918 raise_error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003919 Py_XDECREF(value);
3920 Py_XDECREF(type);
3921 Py_XDECREF(tb);
3922 return WHY_EXCEPTION;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003923}
3924
Tim Petersd6d010b2001-06-21 02:49:55 +00003925/* Iterate v argcnt times and store the results on the stack (via decreasing
3926 sp). Return 1 for success, 0 if error. */
3927
Fredrik Lundh7a830892006-05-27 10:39:48 +00003928static int
Tim Petersd6d010b2001-06-21 02:49:55 +00003929unpack_iterable(PyObject *v, int argcnt, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00003930{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003931 int i = 0;
3932 PyObject *it; /* iter(v) */
3933 PyObject *w;
Guido van Rossumac7be682001-01-17 15:42:30 +00003934
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003935 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00003936
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003937 it = PyObject_GetIter(v);
3938 if (it == NULL)
3939 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00003940
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003941 for (; i < argcnt; i++) {
3942 w = PyIter_Next(it);
3943 if (w == NULL) {
3944 /* Iterator done, via error or exhaustion. */
3945 if (!PyErr_Occurred()) {
3946 PyErr_Format(PyExc_ValueError,
3947 "need more than %d value%s to unpack",
3948 i, i == 1 ? "" : "s");
3949 }
3950 goto Error;
3951 }
3952 *--sp = w;
3953 }
Tim Petersd6d010b2001-06-21 02:49:55 +00003954
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003955 /* We better have exhausted the iterator now. */
3956 w = PyIter_Next(it);
3957 if (w == NULL) {
3958 if (PyErr_Occurred())
3959 goto Error;
3960 Py_DECREF(it);
3961 return 1;
3962 }
3963 Py_DECREF(w);
3964 PyErr_SetString(PyExc_ValueError, "too many values to unpack");
3965 /* fall through */
Tim Petersd6d010b2001-06-21 02:49:55 +00003966Error:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003967 for (; i > 0; i--, sp++)
3968 Py_DECREF(*sp);
3969 Py_XDECREF(it);
3970 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00003971}
3972
3973
Guido van Rossum96a42c81992-01-12 02:29:51 +00003974#ifdef LLTRACE
Fredrik Lundh7a830892006-05-27 10:39:48 +00003975static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00003976prtrace(PyObject *v, char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003977{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003978 printf("%s ", str);
3979 if (PyObject_Print(v, stdout, 0) != 0)
3980 PyErr_Clear(); /* Don't know what else to do */
3981 printf("\n");
3982 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003983}
Guido van Rossum3f5da241990-12-20 15:06:42 +00003984#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00003985
Fredrik Lundh7a830892006-05-27 10:39:48 +00003986static void
Fred Drake5755ce62001-06-27 19:19:46 +00003987call_exc_trace(Py_tracefunc func, PyObject *self, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00003988{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00003989 PyObject *type, *value, *traceback, *arg;
3990 int err;
3991 PyErr_Fetch(&type, &value, &traceback);
3992 if (value == NULL) {
3993 value = Py_None;
3994 Py_INCREF(value);
3995 }
3996 arg = PyTuple_Pack(3, type, value, traceback);
3997 if (arg == NULL) {
3998 PyErr_Restore(type, value, traceback);
3999 return;
4000 }
4001 err = call_trace(func, self, f, PyTrace_EXCEPTION, arg);
4002 Py_DECREF(arg);
4003 if (err == 0)
4004 PyErr_Restore(type, value, traceback);
4005 else {
4006 Py_XDECREF(type);
4007 Py_XDECREF(value);
4008 Py_XDECREF(traceback);
4009 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004010}
4011
Amaury Forgeot d'Arc0d75f092007-11-13 21:54:28 +00004012static int
Fred Drake4ec5d562001-10-04 19:26:43 +00004013call_trace_protected(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004014 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004015{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004016 PyObject *type, *value, *traceback;
4017 int err;
4018 PyErr_Fetch(&type, &value, &traceback);
4019 err = call_trace(func, obj, frame, what, arg);
4020 if (err == 0)
4021 {
4022 PyErr_Restore(type, value, traceback);
4023 return 0;
4024 }
4025 else {
4026 Py_XDECREF(type);
4027 Py_XDECREF(value);
4028 Py_XDECREF(traceback);
4029 return -1;
4030 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004031}
4032
Fredrik Lundh7a830892006-05-27 10:39:48 +00004033static int
Fred Drake5755ce62001-06-27 19:19:46 +00004034call_trace(Py_tracefunc func, PyObject *obj, PyFrameObject *frame,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004035 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004036{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004037 register PyThreadState *tstate = frame->f_tstate;
4038 int result;
4039 if (tstate->tracing)
4040 return 0;
4041 tstate->tracing++;
4042 tstate->use_tracing = 0;
4043 result = func(obj, frame, what, arg);
4044 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4045 || (tstate->c_profilefunc != NULL));
4046 tstate->tracing--;
4047 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004048}
4049
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004050PyObject *
4051_PyEval_CallTracing(PyObject *func, PyObject *args)
4052{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004053 PyFrameObject *frame = PyEval_GetFrame();
4054 PyThreadState *tstate = frame->f_tstate;
4055 int save_tracing = tstate->tracing;
4056 int save_use_tracing = tstate->use_tracing;
4057 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004058
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004059 tstate->tracing = 0;
4060 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4061 || (tstate->c_profilefunc != NULL));
4062 result = PyObject_Call(func, args, NULL);
4063 tstate->tracing = save_tracing;
4064 tstate->use_tracing = save_use_tracing;
4065 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004066}
4067
Jeffrey Yasskin655d8352009-05-23 23:23:01 +00004068/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Fredrik Lundh7a830892006-05-27 10:39:48 +00004069static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004070maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004071 PyFrameObject *frame, int *instr_lb, int *instr_ub,
4072 int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004073{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004074 int result = 0;
4075 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004076
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004077 /* If the last instruction executed isn't in the current
4078 instruction window, reset the window.
4079 */
4080 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4081 PyAddrPair bounds;
4082 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4083 &bounds);
4084 *instr_lb = bounds.ap_lower;
4085 *instr_ub = bounds.ap_upper;
4086 }
4087 /* If the last instruction falls at the start of a line or if
4088 it represents a jump backwards, update the frame's line
4089 number and call the trace function. */
4090 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
4091 frame->f_lineno = line;
4092 result = call_trace(func, obj, frame, PyTrace_LINE, Py_None);
4093 }
4094 *instr_prev = frame->f_lasti;
4095 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004096}
4097
Fred Drake5755ce62001-06-27 19:19:46 +00004098void
4099PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004100{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004101 PyThreadState *tstate = PyThreadState_GET();
4102 PyObject *temp = tstate->c_profileobj;
4103 Py_XINCREF(arg);
4104 tstate->c_profilefunc = NULL;
4105 tstate->c_profileobj = NULL;
4106 /* Must make sure that tracing is not ignored if 'temp' is freed */
4107 tstate->use_tracing = tstate->c_tracefunc != NULL;
4108 Py_XDECREF(temp);
4109 tstate->c_profilefunc = func;
4110 tstate->c_profileobj = arg;
4111 /* Flag that tracing or profiling is turned on */
4112 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004113}
4114
4115void
4116PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4117{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004118 PyThreadState *tstate = PyThreadState_GET();
4119 PyObject *temp = tstate->c_traceobj;
4120 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4121 Py_XINCREF(arg);
4122 tstate->c_tracefunc = NULL;
4123 tstate->c_traceobj = NULL;
4124 /* Must make sure that profiling is not ignored if 'temp' is freed */
4125 tstate->use_tracing = tstate->c_profilefunc != NULL;
4126 Py_XDECREF(temp);
4127 tstate->c_tracefunc = func;
4128 tstate->c_traceobj = arg;
4129 /* Flag that tracing or profiling is turned on */
4130 tstate->use_tracing = ((func != NULL)
4131 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004132}
4133
Guido van Rossumb209a111997-04-29 18:18:01 +00004134PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004135PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004136{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004137 PyFrameObject *current_frame = PyEval_GetFrame();
4138 if (current_frame == NULL)
4139 return PyThreadState_GET()->interp->builtins;
4140 else
4141 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004142}
4143
Guido van Rossumb209a111997-04-29 18:18:01 +00004144PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004145PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004146{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004147 PyFrameObject *current_frame = PyEval_GetFrame();
4148 if (current_frame == NULL)
4149 return NULL;
4150 PyFrame_FastToLocals(current_frame);
4151 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004152}
4153
Guido van Rossumb209a111997-04-29 18:18:01 +00004154PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004155PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004156{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004157 PyFrameObject *current_frame = PyEval_GetFrame();
4158 if (current_frame == NULL)
4159 return NULL;
4160 else
4161 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004162}
4163
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004164PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004165PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004166{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004167 PyThreadState *tstate = PyThreadState_GET();
4168 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004169}
4170
Guido van Rossum6135a871995-01-09 17:53:26 +00004171int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004172PyEval_GetRestricted(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004173{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004174 PyFrameObject *current_frame = PyEval_GetFrame();
4175 return current_frame == NULL ? 0 : PyFrame_IsRestricted(current_frame);
Guido van Rossum6135a871995-01-09 17:53:26 +00004176}
4177
Guido van Rossumbe270261997-05-22 22:26:18 +00004178int
Tim Peters5ba58662001-07-16 02:29:45 +00004179PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004180{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004181 PyFrameObject *current_frame = PyEval_GetFrame();
4182 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004183
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004184 if (current_frame != NULL) {
4185 const int codeflags = current_frame->f_code->co_flags;
4186 const int compilerflags = codeflags & PyCF_MASK;
4187 if (compilerflags) {
4188 result = 1;
4189 cf->cf_flags |= compilerflags;
4190 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004191#if 0 /* future keyword */
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004192 if (codeflags & CO_GENERATOR_ALLOWED) {
4193 result = 1;
4194 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4195 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004196#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004197 }
4198 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004199}
4200
4201int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004202Py_FlushLine(void)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004203{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004204 PyObject *f = PySys_GetObject("stdout");
4205 if (f == NULL)
4206 return 0;
4207 if (!PyFile_SoftSpace(f, 0))
4208 return 0;
4209 return PyFile_WriteString("\n", f);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004210}
4211
Guido van Rossum3f5da241990-12-20 15:06:42 +00004212
Guido van Rossum681d79a1995-07-18 14:51:37 +00004213/* External interface to call any callable object.
Antoine Pitrou76c86492010-04-01 16:42:11 +00004214 The arg must be a tuple or NULL. The kw must be a dict or NULL. */
Guido van Rossume59214e1994-08-30 08:01:59 +00004215
Guido van Rossumb209a111997-04-29 18:18:01 +00004216PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004217PyEval_CallObjectWithKeywords(PyObject *func, PyObject *arg, PyObject *kw)
Guido van Rossum681d79a1995-07-18 14:51:37 +00004218{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004219 PyObject *result;
Guido van Rossum681d79a1995-07-18 14:51:37 +00004220
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004221 if (arg == NULL) {
4222 arg = PyTuple_New(0);
4223 if (arg == NULL)
4224 return NULL;
4225 }
4226 else if (!PyTuple_Check(arg)) {
4227 PyErr_SetString(PyExc_TypeError,
4228 "argument list must be a tuple");
4229 return NULL;
4230 }
4231 else
4232 Py_INCREF(arg);
Guido van Rossum681d79a1995-07-18 14:51:37 +00004233
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004234 if (kw != NULL && !PyDict_Check(kw)) {
4235 PyErr_SetString(PyExc_TypeError,
4236 "keyword list must be a dictionary");
4237 Py_DECREF(arg);
4238 return NULL;
4239 }
Guido van Rossume3e61c11995-08-04 04:14:47 +00004240
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004241 result = PyObject_Call(func, arg, kw);
4242 Py_DECREF(arg);
4243 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004244}
4245
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004246const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004247PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004248{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004249 if (PyMethod_Check(func))
4250 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4251 else if (PyFunction_Check(func))
4252 return PyString_AsString(((PyFunctionObject*)func)->func_name);
4253 else if (PyCFunction_Check(func))
4254 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4255 else if (PyClass_Check(func))
4256 return PyString_AsString(((PyClassObject*)func)->cl_name);
4257 else if (PyInstance_Check(func)) {
4258 return PyString_AsString(
4259 ((PyInstanceObject*)func)->in_class->cl_name);
4260 } else {
4261 return func->ob_type->tp_name;
4262 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004263}
4264
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004265const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004266PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004267{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004268 if (PyMethod_Check(func))
4269 return "()";
4270 else if (PyFunction_Check(func))
4271 return "()";
4272 else if (PyCFunction_Check(func))
4273 return "()";
4274 else if (PyClass_Check(func))
4275 return " constructor";
4276 else if (PyInstance_Check(func)) {
4277 return " instance";
4278 } else {
4279 return " object";
4280 }
Jeremy Hylton512a2372001-04-11 13:52:29 +00004281}
4282
Fredrik Lundh7a830892006-05-27 10:39:48 +00004283static void
Jeremy Hylton192690e2002-08-16 18:36:11 +00004284err_args(PyObject *func, int flags, int nargs)
4285{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004286 if (flags & METH_NOARGS)
4287 PyErr_Format(PyExc_TypeError,
4288 "%.200s() takes no arguments (%d given)",
4289 ((PyCFunctionObject *)func)->m_ml->ml_name,
4290 nargs);
4291 else
4292 PyErr_Format(PyExc_TypeError,
4293 "%.200s() takes exactly one argument (%d given)",
4294 ((PyCFunctionObject *)func)->m_ml->ml_name,
4295 nargs);
Jeremy Hylton192690e2002-08-16 18:36:11 +00004296}
4297
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004298#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004299if (tstate->use_tracing && tstate->c_profilefunc) { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004300 if (call_trace(tstate->c_profilefunc, \
4301 tstate->c_profileobj, \
4302 tstate->frame, PyTrace_C_CALL, \
4303 func)) { \
4304 x = NULL; \
4305 } \
4306 else { \
4307 x = call; \
4308 if (tstate->c_profilefunc != NULL) { \
4309 if (x == NULL) { \
4310 call_trace_protected(tstate->c_profilefunc, \
4311 tstate->c_profileobj, \
4312 tstate->frame, PyTrace_C_EXCEPTION, \
4313 func); \
4314 /* XXX should pass (type, value, tb) */ \
4315 } else { \
4316 if (call_trace(tstate->c_profilefunc, \
4317 tstate->c_profileobj, \
4318 tstate->frame, PyTrace_C_RETURN, \
4319 func)) { \
4320 Py_DECREF(x); \
4321 x = NULL; \
4322 } \
4323 } \
4324 } \
4325 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004326} else { \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004327 x = call; \
4328 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004329
Fredrik Lundh7a830892006-05-27 10:39:48 +00004330static PyObject *
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004331call_function(PyObject ***pp_stack, int oparg
4332#ifdef WITH_TSC
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004333 , uint64* pintr0, uint64* pintr1
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00004334#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004335 )
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004336{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004337 int na = oparg & 0xff;
4338 int nk = (oparg>>8) & 0xff;
4339 int n = na + 2 * nk;
4340 PyObject **pfunc = (*pp_stack) - n - 1;
4341 PyObject *func = *pfunc;
4342 PyObject *x, *w;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004343
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004344 /* Always dispatch PyCFunction first, because these are
4345 presumed to be the most frequent callable object.
4346 */
4347 if (PyCFunction_Check(func) && nk == 0) {
4348 int flags = PyCFunction_GET_FLAGS(func);
4349 PyThreadState *tstate = PyThreadState_GET();
Raymond Hettingera7f56bc2004-06-26 04:34:33 +00004350
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004351 PCALL(PCALL_CFUNCTION);
4352 if (flags & (METH_NOARGS | METH_O)) {
4353 PyCFunction meth = PyCFunction_GET_FUNCTION(func);
4354 PyObject *self = PyCFunction_GET_SELF(func);
4355 if (flags & METH_NOARGS && na == 0) {
4356 C_TRACE(x, (*meth)(self,NULL));
4357 }
4358 else if (flags & METH_O && na == 1) {
4359 PyObject *arg = EXT_POP(*pp_stack);
4360 C_TRACE(x, (*meth)(self,arg));
4361 Py_DECREF(arg);
4362 }
4363 else {
4364 err_args(func, flags, na);
4365 x = NULL;
4366 }
4367 }
4368 else {
4369 PyObject *callargs;
4370 callargs = load_args(pp_stack, na);
4371 READ_TIMESTAMP(*pintr0);
4372 C_TRACE(x, PyCFunction_Call(func,callargs,NULL));
4373 READ_TIMESTAMP(*pintr1);
4374 Py_XDECREF(callargs);
4375 }
4376 } else {
4377 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
4378 /* optimize access to bound methods */
4379 PyObject *self = PyMethod_GET_SELF(func);
4380 PCALL(PCALL_METHOD);
4381 PCALL(PCALL_BOUND_METHOD);
4382 Py_INCREF(self);
4383 func = PyMethod_GET_FUNCTION(func);
4384 Py_INCREF(func);
Serhiy Storchaka763a61c2016-04-10 18:05:12 +03004385 Py_SETREF(*pfunc, self);
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004386 na++;
4387 n++;
4388 } else
4389 Py_INCREF(func);
4390 READ_TIMESTAMP(*pintr0);
4391 if (PyFunction_Check(func))
4392 x = fast_function(func, pp_stack, n, na, nk);
4393 else
4394 x = do_call(func, pp_stack, na, nk);
4395 READ_TIMESTAMP(*pintr1);
4396 Py_DECREF(func);
4397 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004398
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004399 /* Clear the stack of the function object. Also removes
4400 the arguments in case they weren't consumed already
4401 (fast_function() and err_args() leave them on the stack).
4402 */
4403 while ((*pp_stack) > pfunc) {
4404 w = EXT_POP(*pp_stack);
4405 Py_DECREF(w);
4406 PCALL(PCALL_POP);
4407 }
4408 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004409}
4410
Jeremy Hylton192690e2002-08-16 18:36:11 +00004411/* The fast_function() function optimize calls for which no argument
Jeremy Hylton52820442001-01-03 23:52:36 +00004412 tuple is necessary; the objects are passed directly from the stack.
Jeremy Hylton985eba52003-02-05 23:13:00 +00004413 For the simplest case -- a function that takes only positional
4414 arguments and is called with only positional arguments -- it
4415 inlines the most primitive frame setup code from
4416 PyEval_EvalCodeEx(), which vastly reduces the checks that must be
4417 done before evaluating the frame.
Jeremy Hylton52820442001-01-03 23:52:36 +00004418*/
4419
Fredrik Lundh7a830892006-05-27 10:39:48 +00004420static PyObject *
Guido van Rossumac7be682001-01-17 15:42:30 +00004421fast_function(PyObject *func, PyObject ***pp_stack, int n, int na, int nk)
Jeremy Hylton52820442001-01-03 23:52:36 +00004422{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004423 PyCodeObject *co = (PyCodeObject *)PyFunction_GET_CODE(func);
4424 PyObject *globals = PyFunction_GET_GLOBALS(func);
4425 PyObject *argdefs = PyFunction_GET_DEFAULTS(func);
4426 PyObject **d = NULL;
4427 int nd = 0;
Jeremy Hylton52820442001-01-03 23:52:36 +00004428
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004429 PCALL(PCALL_FUNCTION);
4430 PCALL(PCALL_FAST_FUNCTION);
4431 if (argdefs == NULL && co->co_argcount == n && nk==0 &&
4432 co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) {
4433 PyFrameObject *f;
4434 PyObject *retval = NULL;
4435 PyThreadState *tstate = PyThreadState_GET();
4436 PyObject **fastlocals, **stack;
4437 int i;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004438
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004439 PCALL(PCALL_FASTER_FUNCTION);
4440 assert(globals != NULL);
4441 /* XXX Perhaps we should create a specialized
4442 PyFrame_New() that doesn't take locals, but does
4443 take builtins without sanity checking them.
4444 */
4445 assert(tstate != NULL);
4446 f = PyFrame_New(tstate, co, globals, NULL);
4447 if (f == NULL)
4448 return NULL;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004449
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004450 fastlocals = f->f_localsplus;
4451 stack = (*pp_stack) - n;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004452
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004453 for (i = 0; i < n; i++) {
4454 Py_INCREF(*stack);
4455 fastlocals[i] = *stack++;
4456 }
4457 retval = PyEval_EvalFrameEx(f,0);
4458 ++tstate->recursion_depth;
4459 Py_DECREF(f);
4460 --tstate->recursion_depth;
4461 return retval;
4462 }
4463 if (argdefs != NULL) {
4464 d = &PyTuple_GET_ITEM(argdefs, 0);
4465 nd = Py_SIZE(argdefs);
4466 }
4467 return PyEval_EvalCodeEx(co, globals,
4468 (PyObject *)NULL, (*pp_stack)-n, na,
4469 (*pp_stack)-2*nk, nk, d, nd,
4470 PyFunction_GET_CLOSURE(func));
Jeremy Hylton52820442001-01-03 23:52:36 +00004471}
4472
Fredrik Lundh7a830892006-05-27 10:39:48 +00004473static PyObject *
Ka-Ping Yee20579702001-01-15 22:14:16 +00004474update_keyword_args(PyObject *orig_kwdict, int nk, PyObject ***pp_stack,
4475 PyObject *func)
Jeremy Hylton52820442001-01-03 23:52:36 +00004476{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004477 PyObject *kwdict = NULL;
4478 if (orig_kwdict == NULL)
4479 kwdict = PyDict_New();
4480 else {
4481 kwdict = PyDict_Copy(orig_kwdict);
4482 Py_DECREF(orig_kwdict);
4483 }
4484 if (kwdict == NULL)
4485 return NULL;
4486 while (--nk >= 0) {
4487 int err;
4488 PyObject *value = EXT_POP(*pp_stack);
4489 PyObject *key = EXT_POP(*pp_stack);
4490 if (PyDict_GetItem(kwdict, key) != NULL) {
4491 PyErr_Format(PyExc_TypeError,
4492 "%.200s%s got multiple values "
4493 "for keyword argument '%.200s'",
4494 PyEval_GetFuncName(func),
4495 PyEval_GetFuncDesc(func),
4496 PyString_AsString(key));
4497 Py_DECREF(key);
4498 Py_DECREF(value);
4499 Py_DECREF(kwdict);
4500 return NULL;
4501 }
4502 err = PyDict_SetItem(kwdict, key, value);
4503 Py_DECREF(key);
4504 Py_DECREF(value);
4505 if (err) {
4506 Py_DECREF(kwdict);
4507 return NULL;
4508 }
4509 }
4510 return kwdict;
Jeremy Hylton52820442001-01-03 23:52:36 +00004511}
4512
Fredrik Lundh7a830892006-05-27 10:39:48 +00004513static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004514update_star_args(int nstack, int nstar, PyObject *stararg,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004515 PyObject ***pp_stack)
Jeremy Hylton52820442001-01-03 23:52:36 +00004516{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004517 PyObject *callargs, *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004518
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004519 callargs = PyTuple_New(nstack + nstar);
4520 if (callargs == NULL) {
4521 return NULL;
4522 }
4523 if (nstar) {
4524 int i;
4525 for (i = 0; i < nstar; i++) {
4526 PyObject *a = PyTuple_GET_ITEM(stararg, i);
4527 Py_INCREF(a);
4528 PyTuple_SET_ITEM(callargs, nstack + i, a);
4529 }
4530 }
4531 while (--nstack >= 0) {
4532 w = EXT_POP(*pp_stack);
4533 PyTuple_SET_ITEM(callargs, nstack, w);
4534 }
4535 return callargs;
Jeremy Hylton52820442001-01-03 23:52:36 +00004536}
4537
Fredrik Lundh7a830892006-05-27 10:39:48 +00004538static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004539load_args(PyObject ***pp_stack, int na)
4540{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004541 PyObject *args = PyTuple_New(na);
4542 PyObject *w;
Jeremy Hylton52820442001-01-03 23:52:36 +00004543
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004544 if (args == NULL)
4545 return NULL;
4546 while (--na >= 0) {
4547 w = EXT_POP(*pp_stack);
4548 PyTuple_SET_ITEM(args, na, w);
4549 }
4550 return args;
Jeremy Hylton52820442001-01-03 23:52:36 +00004551}
4552
Fredrik Lundh7a830892006-05-27 10:39:48 +00004553static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004554do_call(PyObject *func, PyObject ***pp_stack, int na, int nk)
4555{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004556 PyObject *callargs = NULL;
4557 PyObject *kwdict = NULL;
4558 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004559
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004560 if (nk > 0) {
4561 kwdict = update_keyword_args(NULL, nk, pp_stack, func);
4562 if (kwdict == NULL)
4563 goto call_fail;
4564 }
4565 callargs = load_args(pp_stack, na);
4566 if (callargs == NULL)
4567 goto call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004568#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004569 /* At this point, we have to look at the type of func to
4570 update the call stats properly. Do it here so as to avoid
4571 exposing the call stats machinery outside ceval.c
4572 */
4573 if (PyFunction_Check(func))
4574 PCALL(PCALL_FUNCTION);
4575 else if (PyMethod_Check(func))
4576 PCALL(PCALL_METHOD);
4577 else if (PyType_Check(func))
4578 PCALL(PCALL_TYPE);
4579 else if (PyCFunction_Check(func))
4580 PCALL(PCALL_CFUNCTION);
4581 else
4582 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004583#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004584 if (PyCFunction_Check(func)) {
4585 PyThreadState *tstate = PyThreadState_GET();
4586 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4587 }
4588 else
4589 result = PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004590 call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004591 Py_XDECREF(callargs);
4592 Py_XDECREF(kwdict);
4593 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004594}
4595
Fredrik Lundh7a830892006-05-27 10:39:48 +00004596static PyObject *
Jeremy Hylton52820442001-01-03 23:52:36 +00004597ext_do_call(PyObject *func, PyObject ***pp_stack, int flags, int na, int nk)
4598{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004599 int nstar = 0;
4600 PyObject *callargs = NULL;
4601 PyObject *stararg = NULL;
4602 PyObject *kwdict = NULL;
4603 PyObject *result = NULL;
Jeremy Hylton52820442001-01-03 23:52:36 +00004604
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004605 if (flags & CALL_FLAG_KW) {
4606 kwdict = EXT_POP(*pp_stack);
4607 if (!PyDict_Check(kwdict)) {
4608 PyObject *d;
4609 d = PyDict_New();
4610 if (d == NULL)
4611 goto ext_call_fail;
4612 if (PyDict_Update(d, kwdict) != 0) {
4613 Py_DECREF(d);
4614 /* PyDict_Update raises attribute
4615 * error (percolated from an attempt
4616 * to get 'keys' attribute) instead of
4617 * a type error if its second argument
4618 * is not a mapping.
4619 */
4620 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
4621 PyErr_Format(PyExc_TypeError,
4622 "%.200s%.200s argument after ** "
4623 "must be a mapping, not %.200s",
4624 PyEval_GetFuncName(func),
4625 PyEval_GetFuncDesc(func),
4626 kwdict->ob_type->tp_name);
4627 }
4628 goto ext_call_fail;
4629 }
4630 Py_DECREF(kwdict);
4631 kwdict = d;
4632 }
4633 }
4634 if (flags & CALL_FLAG_VAR) {
4635 stararg = EXT_POP(*pp_stack);
4636 if (!PyTuple_Check(stararg)) {
4637 PyObject *t = NULL;
4638 t = PySequence_Tuple(stararg);
4639 if (t == NULL) {
Martin Panter0bb165e2016-01-31 06:30:56 +00004640 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4641 /* Don't mask TypeError raised from a generator */
4642 !PyGen_Check(stararg)) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004643 PyErr_Format(PyExc_TypeError,
4644 "%.200s%.200s argument after * "
Martin Panter0bb165e2016-01-31 06:30:56 +00004645 "must be an iterable, not %200s",
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004646 PyEval_GetFuncName(func),
4647 PyEval_GetFuncDesc(func),
4648 stararg->ob_type->tp_name);
4649 }
4650 goto ext_call_fail;
4651 }
4652 Py_DECREF(stararg);
4653 stararg = t;
4654 }
4655 nstar = PyTuple_GET_SIZE(stararg);
4656 }
4657 if (nk > 0) {
4658 kwdict = update_keyword_args(kwdict, nk, pp_stack, func);
4659 if (kwdict == NULL)
4660 goto ext_call_fail;
4661 }
4662 callargs = update_star_args(na, nstar, stararg, pp_stack);
4663 if (callargs == NULL)
4664 goto ext_call_fail;
Jeremy Hylton985eba52003-02-05 23:13:00 +00004665#ifdef CALL_PROFILE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004666 /* At this point, we have to look at the type of func to
4667 update the call stats properly. Do it here so as to avoid
4668 exposing the call stats machinery outside ceval.c
4669 */
4670 if (PyFunction_Check(func))
4671 PCALL(PCALL_FUNCTION);
4672 else if (PyMethod_Check(func))
4673 PCALL(PCALL_METHOD);
4674 else if (PyType_Check(func))
4675 PCALL(PCALL_TYPE);
4676 else if (PyCFunction_Check(func))
4677 PCALL(PCALL_CFUNCTION);
4678 else
4679 PCALL(PCALL_OTHER);
Jeremy Hylton985eba52003-02-05 23:13:00 +00004680#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004681 if (PyCFunction_Check(func)) {
4682 PyThreadState *tstate = PyThreadState_GET();
4683 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
4684 }
4685 else
4686 result = PyObject_Call(func, callargs, kwdict);
Thomas Woutersae406c62007-09-19 17:27:43 +00004687ext_call_fail:
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004688 Py_XDECREF(callargs);
4689 Py_XDECREF(kwdict);
4690 Py_XDECREF(stararg);
4691 return result;
Jeremy Hylton52820442001-01-03 23:52:36 +00004692}
4693
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004694/* Extract a slice index from a PyInt or PyLong or an object with the
4695 nb_index slot defined, and store in *pi.
4696 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang05469fa2017-05-10 19:20:28 +08004697 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004698 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004699*/
Tim Petersb5196382001-12-16 19:44:20 +00004700/* Note: If v is NULL, return success without storing into *pi. This
4701 is because_PyEval_SliceIndex() is called by apply_slice(), which can be
4702 called by the SLICE opcode with v and/or w equal to NULL.
Tim Peterscb479e72001-12-16 19:11:44 +00004703*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004704int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004705_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004706{
Serhiy Storchaka079f21f2017-03-30 20:32:18 +03004707 if (v != NULL && v != Py_None) {
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004708 Py_ssize_t x;
4709 if (PyInt_Check(v)) {
4710 /* XXX(nnorwitz): I think PyInt_AS_LONG is correct,
4711 however, it looks like it should be AsSsize_t.
4712 There should be a comment here explaining why.
4713 */
4714 x = PyInt_AS_LONG(v);
4715 }
4716 else if (PyIndex_Check(v)) {
4717 x = PyNumber_AsSsize_t(v, NULL);
4718 if (x == -1 && PyErr_Occurred())
4719 return 0;
4720 }
4721 else {
4722 PyErr_SetString(PyExc_TypeError,
4723 "slice indices must be integers or "
4724 "None or have an __index__ method");
4725 return 0;
4726 }
4727 *pi = x;
4728 }
4729 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004730}
4731
Serhiy Storchaka079f21f2017-03-30 20:32:18 +03004732int
4733_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
4734{
4735 Py_ssize_t x;
4736 if (PyIndex_Check(v)) {
4737 x = PyNumber_AsSsize_t(v, NULL);
4738 if (x == -1 && PyErr_Occurred())
4739 return 0;
4740 }
4741 else {
4742 PyErr_SetString(PyExc_TypeError,
4743 "slice indices must be integers or "
4744 "have an __index__ method");
4745 return 0;
4746 }
4747 *pi = x;
4748 return 1;
4749}
4750
4751
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004752#undef ISINDEX
Serhiy Storchaka48c8bf22018-07-31 09:09:36 +03004753#define ISINDEX(x) ((x) == NULL || _PyAnyInt_Check(x) || PyIndex_Check(x))
Guido van Rossum50d756e2001-08-18 17:43:36 +00004754
Fredrik Lundh7a830892006-05-27 10:39:48 +00004755static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004756apply_slice(PyObject *u, PyObject *v, PyObject *w) /* return u[v:w] */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004757{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004758 PyTypeObject *tp = u->ob_type;
4759 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004760
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004761 if (sq && sq->sq_slice && ISINDEX(v) && ISINDEX(w)) {
4762 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4763 if (!_PyEval_SliceIndex(v, &ilow))
4764 return NULL;
4765 if (!_PyEval_SliceIndex(w, &ihigh))
4766 return NULL;
4767 return PySequence_GetSlice(u, ilow, ihigh);
4768 }
4769 else {
4770 PyObject *slice = PySlice_New(v, w, NULL);
4771 if (slice != NULL) {
4772 PyObject *res = PyObject_GetItem(u, slice);
4773 Py_DECREF(slice);
4774 return res;
4775 }
4776 else
4777 return NULL;
4778 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004779}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004780
Fredrik Lundh7a830892006-05-27 10:39:48 +00004781static int
Guido van Rossumac7be682001-01-17 15:42:30 +00004782assign_slice(PyObject *u, PyObject *v, PyObject *w, PyObject *x)
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004783 /* u[v:w] = x */
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004784{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004785 PyTypeObject *tp = u->ob_type;
4786 PySequenceMethods *sq = tp->tp_as_sequence;
Guido van Rossum50d756e2001-08-18 17:43:36 +00004787
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004788 if (sq && sq->sq_ass_slice && ISINDEX(v) && ISINDEX(w)) {
4789 Py_ssize_t ilow = 0, ihigh = PY_SSIZE_T_MAX;
4790 if (!_PyEval_SliceIndex(v, &ilow))
4791 return -1;
4792 if (!_PyEval_SliceIndex(w, &ihigh))
4793 return -1;
4794 if (x == NULL)
4795 return PySequence_DelSlice(u, ilow, ihigh);
4796 else
4797 return PySequence_SetSlice(u, ilow, ihigh, x);
4798 }
4799 else {
4800 PyObject *slice = PySlice_New(v, w, NULL);
4801 if (slice != NULL) {
4802 int res;
4803 if (x != NULL)
4804 res = PyObject_SetItem(u, slice, x);
4805 else
4806 res = PyObject_DelItem(u, slice);
4807 Py_DECREF(slice);
4808 return res;
4809 }
4810 else
4811 return -1;
4812 }
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004813}
4814
Guido van Rossum04edb522008-03-18 02:49:46 +00004815#define Py3kExceptionClass_Check(x) \
4816 (PyType_Check((x)) && \
4817 PyType_FastSubclass((PyTypeObject*)(x), Py_TPFLAGS_BASE_EXC_SUBCLASS))
4818
4819#define CANNOT_CATCH_MSG "catching classes that don't inherit from " \
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004820 "BaseException is not allowed in 3.x"
Guido van Rossum04edb522008-03-18 02:49:46 +00004821
Fredrik Lundh7a830892006-05-27 10:39:48 +00004822static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004823cmp_outcome(int op, register PyObject *v, register PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004824{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004825 int res = 0;
4826 switch (op) {
4827 case PyCmp_IS:
4828 res = (v == w);
4829 break;
4830 case PyCmp_IS_NOT:
4831 res = (v != w);
4832 break;
4833 case PyCmp_IN:
4834 res = PySequence_Contains(w, v);
4835 if (res < 0)
4836 return NULL;
4837 break;
4838 case PyCmp_NOT_IN:
4839 res = PySequence_Contains(w, v);
4840 if (res < 0)
4841 return NULL;
4842 res = !res;
4843 break;
4844 case PyCmp_EXC_MATCH:
4845 if (PyTuple_Check(w)) {
4846 Py_ssize_t i, length;
4847 length = PyTuple_Size(w);
4848 for (i = 0; i < length; i += 1) {
4849 PyObject *exc = PyTuple_GET_ITEM(w, i);
4850 if (PyString_Check(exc)) {
4851 int ret_val;
4852 ret_val = PyErr_WarnEx(
4853 PyExc_DeprecationWarning,
4854 "catching of string "
4855 "exceptions is deprecated", 1);
4856 if (ret_val < 0)
4857 return NULL;
4858 }
4859 else if (Py_Py3kWarningFlag &&
4860 !PyTuple_Check(exc) &&
4861 !Py3kExceptionClass_Check(exc))
4862 {
4863 int ret_val;
4864 ret_val = PyErr_WarnEx(
4865 PyExc_DeprecationWarning,
4866 CANNOT_CATCH_MSG, 1);
4867 if (ret_val < 0)
4868 return NULL;
4869 }
4870 }
4871 }
4872 else {
4873 if (PyString_Check(w)) {
4874 int ret_val;
4875 ret_val = PyErr_WarnEx(
4876 PyExc_DeprecationWarning,
4877 "catching of string "
4878 "exceptions is deprecated", 1);
4879 if (ret_val < 0)
4880 return NULL;
4881 }
4882 else if (Py_Py3kWarningFlag &&
4883 !PyTuple_Check(w) &&
4884 !Py3kExceptionClass_Check(w))
4885 {
4886 int ret_val;
4887 ret_val = PyErr_WarnEx(
4888 PyExc_DeprecationWarning,
4889 CANNOT_CATCH_MSG, 1);
4890 if (ret_val < 0)
4891 return NULL;
4892 }
4893 }
4894 res = PyErr_GivenExceptionMatches(v, w);
4895 break;
4896 default:
4897 return PyObject_RichCompare(v, w, op);
4898 }
4899 v = res ? Py_True : Py_False;
4900 Py_INCREF(v);
4901 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004902}
4903
Fredrik Lundh7a830892006-05-27 10:39:48 +00004904static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004905import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004906{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004907 PyObject *x;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004908
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004909 x = PyObject_GetAttr(v, name);
4910 if (x == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
4911 PyErr_Format(PyExc_ImportError,
4912 "cannot import name %.230s",
4913 PyString_AsString(name));
4914 }
4915 return x;
Thomas Wouters52152252000-08-17 22:55:00 +00004916}
Guido van Rossumac7be682001-01-17 15:42:30 +00004917
Fredrik Lundh7a830892006-05-27 10:39:48 +00004918static int
Thomas Wouters52152252000-08-17 22:55:00 +00004919import_all_from(PyObject *locals, PyObject *v)
4920{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004921 PyObject *all = PyObject_GetAttrString(v, "__all__");
4922 PyObject *dict, *name, *value;
4923 int skip_leading_underscores = 0;
4924 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004925
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004926 if (all == NULL) {
4927 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4928 return -1; /* Unexpected error */
4929 PyErr_Clear();
4930 dict = PyObject_GetAttrString(v, "__dict__");
4931 if (dict == NULL) {
4932 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4933 return -1;
4934 PyErr_SetString(PyExc_ImportError,
4935 "from-import-* object has no __dict__ and no __all__");
4936 return -1;
4937 }
4938 all = PyMapping_Keys(dict);
4939 Py_DECREF(dict);
4940 if (all == NULL)
4941 return -1;
4942 skip_leading_underscores = 1;
4943 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004944
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004945 for (pos = 0, err = 0; ; pos++) {
4946 name = PySequence_GetItem(all, pos);
4947 if (name == NULL) {
4948 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4949 err = -1;
4950 else
4951 PyErr_Clear();
4952 break;
4953 }
4954 if (skip_leading_underscores &&
4955 PyString_Check(name) &&
4956 PyString_AS_STRING(name)[0] == '_')
4957 {
4958 Py_DECREF(name);
4959 continue;
4960 }
4961 value = PyObject_GetAttr(v, name);
4962 if (value == NULL)
4963 err = -1;
4964 else if (PyDict_CheckExact(locals))
4965 err = PyDict_SetItem(locals, name, value);
4966 else
4967 err = PyObject_SetItem(locals, name, value);
4968 Py_DECREF(name);
4969 Py_XDECREF(value);
4970 if (err != 0)
4971 break;
4972 }
4973 Py_DECREF(all);
4974 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004975}
4976
Fredrik Lundh7a830892006-05-27 10:39:48 +00004977static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004978build_class(PyObject *methods, PyObject *bases, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004979{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004980 PyObject *metaclass = NULL, *result, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004981
Antoine Pitrouc83ea132010-05-09 14:46:46 +00004982 if (PyDict_Check(methods))
4983 metaclass = PyDict_GetItemString(methods, "__metaclass__");
4984 if (metaclass != NULL)
4985 Py_INCREF(metaclass);
4986 else if (PyTuple_Check(bases) && PyTuple_GET_SIZE(bases) > 0) {
4987 base = PyTuple_GET_ITEM(bases, 0);
4988 metaclass = PyObject_GetAttrString(base, "__class__");
4989 if (metaclass == NULL) {
4990 PyErr_Clear();
4991 metaclass = (PyObject *)base->ob_type;
4992 Py_INCREF(metaclass);
4993 }
4994 }
4995 else {
4996 PyObject *g = PyEval_GetGlobals();
4997 if (g != NULL && PyDict_Check(g))
4998 metaclass = PyDict_GetItemString(g, "__metaclass__");
4999 if (metaclass == NULL)
5000 metaclass = (PyObject *) &PyClass_Type;
5001 Py_INCREF(metaclass);
5002 }
5003 result = PyObject_CallFunctionObjArgs(metaclass, name, bases, methods,
5004 NULL);
5005 Py_DECREF(metaclass);
5006 if (result == NULL && PyErr_ExceptionMatches(PyExc_TypeError)) {
5007 /* A type error here likely means that the user passed
5008 in a base that was not a class (such the random module
5009 instead of the random.random type). Help them out with
5010 by augmenting the error message with more information.*/
Raymond Hettingercfc31922004-09-16 16:41:57 +00005011
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005012 PyObject *ptype, *pvalue, *ptraceback;
Raymond Hettingercfc31922004-09-16 16:41:57 +00005013
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005014 PyErr_Fetch(&ptype, &pvalue, &ptraceback);
5015 if (PyString_Check(pvalue)) {
5016 PyObject *newmsg;
5017 newmsg = PyString_FromFormat(
5018 "Error when calling the metaclass bases\n"
5019 " %s",
5020 PyString_AS_STRING(pvalue));
5021 if (newmsg != NULL) {
5022 Py_DECREF(pvalue);
5023 pvalue = newmsg;
5024 }
5025 }
5026 PyErr_Restore(ptype, pvalue, ptraceback);
5027 }
5028 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00005029}
5030
Fredrik Lundh7a830892006-05-27 10:39:48 +00005031static int
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005032exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005033 PyObject *locals)
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005034{
Benjamin Petersond2903bd2014-08-09 19:39:36 -07005035 int n;
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005036 PyObject *v;
5037 int plain = 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005038
Benjamin Petersond2903bd2014-08-09 19:39:36 -07005039 if (PyTuple_Check(prog) && globals == Py_None && locals == Py_None &&
5040 ((n = PyTuple_Size(prog)) == 2 || n == 3)) {
5041 /* Backward compatibility hack */
5042 globals = PyTuple_GetItem(prog, 1);
5043 if (n == 3)
5044 locals = PyTuple_GetItem(prog, 2);
5045 prog = PyTuple_GetItem(prog, 0);
5046 }
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005047 if (globals == Py_None) {
5048 globals = PyEval_GetGlobals();
5049 if (locals == Py_None) {
5050 locals = PyEval_GetLocals();
5051 plain = 1;
5052 }
5053 if (!globals || !locals) {
5054 PyErr_SetString(PyExc_SystemError,
5055 "globals and locals cannot be NULL");
5056 return -1;
5057 }
5058 }
5059 else if (locals == Py_None)
5060 locals = globals;
5061 if (!PyString_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005062#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005063 !PyUnicode_Check(prog) &&
Benjamin Peterson78821dd2009-01-25 17:15:10 +00005064#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005065 !PyCode_Check(prog) &&
5066 !PyFile_Check(prog)) {
5067 PyErr_SetString(PyExc_TypeError,
5068 "exec: arg 1 must be a string, file, or code object");
5069 return -1;
5070 }
5071 if (!PyDict_Check(globals)) {
5072 PyErr_SetString(PyExc_TypeError,
5073 "exec: arg 2 must be a dictionary or None");
5074 return -1;
5075 }
5076 if (!PyMapping_Check(locals)) {
5077 PyErr_SetString(PyExc_TypeError,
5078 "exec: arg 3 must be a mapping or None");
5079 return -1;
5080 }
5081 if (PyDict_GetItemString(globals, "__builtins__") == NULL)
5082 PyDict_SetItemString(globals, "__builtins__", f->f_builtins);
5083 if (PyCode_Check(prog)) {
5084 if (PyCode_GetNumFree((PyCodeObject *)prog) > 0) {
5085 PyErr_SetString(PyExc_TypeError,
5086 "code object passed to exec may not contain free variables");
5087 return -1;
5088 }
5089 v = PyEval_EvalCode((PyCodeObject *) prog, globals, locals);
5090 }
5091 else if (PyFile_Check(prog)) {
5092 FILE *fp = PyFile_AsFile(prog);
5093 char *name = PyString_AsString(PyFile_Name(prog));
5094 PyCompilerFlags cf;
5095 if (name == NULL)
5096 return -1;
5097 cf.cf_flags = 0;
5098 if (PyEval_MergeCompilerFlags(&cf))
5099 v = PyRun_FileFlags(fp, name, Py_file_input, globals,
5100 locals, &cf);
5101 else
5102 v = PyRun_File(fp, name, Py_file_input, globals,
5103 locals);
5104 }
5105 else {
5106 PyObject *tmp = NULL;
5107 char *str;
5108 PyCompilerFlags cf;
5109 cf.cf_flags = 0;
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005110#ifdef Py_USING_UNICODE
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005111 if (PyUnicode_Check(prog)) {
5112 tmp = PyUnicode_AsUTF8String(prog);
5113 if (tmp == NULL)
5114 return -1;
5115 prog = tmp;
5116 cf.cf_flags |= PyCF_SOURCE_IS_UTF8;
5117 }
Just van Rossum3aaf42c2003-02-10 08:21:10 +00005118#endif
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005119 if (PyString_AsStringAndSize(prog, &str, NULL))
5120 return -1;
5121 if (PyEval_MergeCompilerFlags(&cf))
5122 v = PyRun_StringFlags(str, Py_file_input, globals,
5123 locals, &cf);
5124 else
5125 v = PyRun_String(str, Py_file_input, globals, locals);
5126 Py_XDECREF(tmp);
5127 }
5128 if (plain)
5129 PyFrame_LocalsToFast(f, 0);
5130 if (v == NULL)
5131 return -1;
5132 Py_DECREF(v);
5133 return 0;
Guido van Rossumdb3165e1993-10-18 17:06:59 +00005134}
Guido van Rossum24c13741995-02-14 09:42:43 +00005135
Fredrik Lundh7a830892006-05-27 10:39:48 +00005136static void
Paul Prescode68140d2000-08-30 20:25:01 +00005137format_exc_check_arg(PyObject *exc, char *format_str, PyObject *obj)
5138{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005139 char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005140
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005141 if (!obj)
5142 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005143
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005144 obj_str = PyString_AsString(obj);
5145 if (!obj_str)
5146 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005147
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005148 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005149}
Guido van Rossum950361c1997-01-24 13:49:28 +00005150
Fredrik Lundh7a830892006-05-27 10:39:48 +00005151static PyObject *
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005152string_concatenate(PyObject *v, PyObject *w,
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005153 PyFrameObject *f, unsigned char *next_instr)
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005154{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005155 /* This function implements 'variable += expr' when both arguments
5156 are strings. */
5157 Py_ssize_t v_len = PyString_GET_SIZE(v);
5158 Py_ssize_t w_len = PyString_GET_SIZE(w);
5159 Py_ssize_t new_len = v_len + w_len;
5160 if (new_len < 0) {
5161 PyErr_SetString(PyExc_OverflowError,
5162 "strings are too large to concat");
5163 return NULL;
5164 }
Tim Peters7df5e7f2006-05-26 23:14:37 +00005165
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005166 if (v->ob_refcnt == 2) {
5167 /* In the common case, there are 2 references to the value
5168 * stored in 'variable' when the += is performed: one on the
5169 * value stack (in 'v') and one still stored in the
5170 * 'variable'. We try to delete the variable now to reduce
5171 * the refcnt to 1.
5172 */
5173 switch (*next_instr) {
5174 case STORE_FAST:
5175 {
5176 int oparg = PEEKARG();
5177 PyObject **fastlocals = f->f_localsplus;
5178 if (GETLOCAL(oparg) == v)
5179 SETLOCAL(oparg, NULL);
5180 break;
5181 }
5182 case STORE_DEREF:
5183 {
5184 PyObject **freevars = (f->f_localsplus +
5185 f->f_code->co_nlocals);
5186 PyObject *c = freevars[PEEKARG()];
5187 if (PyCell_GET(c) == v)
5188 PyCell_Set(c, NULL);
5189 break;
5190 }
5191 case STORE_NAME:
5192 {
5193 PyObject *names = f->f_code->co_names;
5194 PyObject *name = GETITEM(names, PEEKARG());
5195 PyObject *locals = f->f_locals;
5196 if (PyDict_CheckExact(locals) &&
5197 PyDict_GetItem(locals, name) == v) {
5198 if (PyDict_DelItem(locals, name) != 0) {
5199 PyErr_Clear();
5200 }
5201 }
5202 break;
5203 }
5204 }
5205 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005206
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005207 if (v->ob_refcnt == 1 && !PyString_CHECK_INTERNED(v)) {
5208 /* Now we own the last reference to 'v', so we can resize it
5209 * in-place.
5210 */
5211 if (_PyString_Resize(&v, new_len) != 0) {
5212 /* XXX if _PyString_Resize() fails, 'v' has been
5213 * deallocated so it cannot be put back into
5214 * 'variable'. The MemoryError is raised when there
5215 * is no value in 'variable', which might (very
5216 * remotely) be a cause of incompatibilities.
5217 */
5218 return NULL;
5219 }
5220 /* copy 'w' into the newly allocated area of 'v' */
5221 memcpy(PyString_AS_STRING(v) + v_len,
5222 PyString_AS_STRING(w), w_len);
5223 return v;
5224 }
5225 else {
5226 /* When in-place resizing is not an option. */
5227 PyString_Concat(&v, w);
5228 return v;
5229 }
Raymond Hettinger52a21b82004-08-06 18:43:09 +00005230}
5231
Guido van Rossum950361c1997-01-24 13:49:28 +00005232#ifdef DYNAMIC_EXECUTION_PROFILE
5233
Fredrik Lundh7a830892006-05-27 10:39:48 +00005234static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005235getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005236{
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005237 int i;
5238 PyObject *l = PyList_New(256);
5239 if (l == NULL) return NULL;
5240 for (i = 0; i < 256; i++) {
5241 PyObject *x = PyInt_FromLong(a[i]);
5242 if (x == NULL) {
5243 Py_DECREF(l);
5244 return NULL;
5245 }
5246 PyList_SetItem(l, i, x);
5247 }
5248 for (i = 0; i < 256; i++)
5249 a[i] = 0;
5250 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005251}
5252
5253PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005254_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005255{
5256#ifndef DXPAIRS
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005257 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005258#else
Antoine Pitrouc83ea132010-05-09 14:46:46 +00005259 int i;
5260 PyObject *l = PyList_New(257);
5261 if (l == NULL) return NULL;
5262 for (i = 0; i < 257; i++) {
5263 PyObject *x = getarray(dxpairs[i]);
5264 if (x == NULL) {
5265 Py_DECREF(l);
5266 return NULL;
5267 }
5268 PyList_SetItem(l, i, x);
5269 }
5270 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005271#endif
5272}
5273
5274#endif