blob: 86ffec42b31e116e7207a3aa80920f4f305a9ae4 [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
Thomas Wouters477c8d52006-05-27 19:21:47 +00009/* enable more aggressive intra-module optimizations, where available */
10#define PY_LOCAL_AGGRESSIVE
11
Guido van Rossumb209a111997-04-29 18:18:01 +000012#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -060013#include "internal/pystate.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000014
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040016#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000018#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070019#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040020#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000021#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000022
Guido van Rossumc6004111993-11-05 10:22:19 +000023#include <ctype.h>
24
Guido van Rossum04691fc1992-08-12 15:35:34 +000025/* Turn this on if your compiler chokes on the big switch: */
Guido van Rossum1ae940a1995-01-02 19:04:15 +000026/* #define CASE_TOO_BIG 1 */
Guido van Rossum04691fc1992-08-12 15:35:34 +000027
Guido van Rossum408027e1996-12-30 16:17:54 +000028#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000029/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030#define LLTRACE 1 /* Low-level trace feature */
31#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032#endif
33
Yury Selivanovf2392132016-12-13 19:03:51 -050034/* Private API for the LOAD_METHOD opcode. */
35extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
36
Jeremy Hylton52820442001-01-03 23:52:36 +000037typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000038
Guido van Rossum374a9221991-04-04 10:40:29 +000039/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060040Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
41 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070042static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000043
Guido van Rossum0a066c01992-03-27 17:29:15 +000044#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000045static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020046static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000047#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010048static int call_trace(Py_tracefunc, PyObject *,
49 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000051static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010052 PyThreadState *, PyFrameObject *,
53 int, PyObject *);
54static void call_exc_trace(Py_tracefunc, PyObject *,
55 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000056static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060057 PyThreadState *, PyFrameObject *,
58 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070059static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
60static void dtrace_function_entry(PyFrameObject *);
61static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000062
Thomas Wouters477c8d52006-05-27 19:21:47 +000063static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060064static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
65 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000066static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000067static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000068static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000069static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020070static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030071 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050072static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030073static int check_args_iterable(PyObject *func, PyObject *vararg);
74static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Guido van Rossum374a9221991-04-04 10:40:29 +000075
Paul Prescode68140d2000-08-30 20:25:01 +000076#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000078#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000080#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 "free variable '%.200s' referenced before assignment" \
82 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000083
Guido van Rossum950361c1997-01-24 13:49:28 +000084/* Dynamic execution profile */
85#ifdef DYNAMIC_EXECUTION_PROFILE
86#ifdef DXPAIRS
87static long dxpairs[257][256];
88#define dxp dxpairs[256]
89#else
90static long dxp[256];
91#endif
92#endif
93
Eric Snow2ebc5ce2017-09-07 23:51:28 -060094#define GIL_REQUEST _Py_atomic_load_relaxed(&_PyRuntime.ceval.gil_drop_request)
Benjamin Petersond2be5b42010-09-10 22:47:02 +000095
Jeffrey Yasskin39370832010-05-03 19:29:34 +000096/* This can set eval_breaker to 0 even though gil_drop_request became
97 1. We believe this is all right because the eval loop will release
98 the GIL eventually anyway. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +000099#define COMPUTE_EVAL_BREAKER() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 _Py_atomic_store_relaxed( \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600101 &_PyRuntime.ceval.eval_breaker, \
Benjamin Petersond2be5b42010-09-10 22:47:02 +0000102 GIL_REQUEST | \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600103 _Py_atomic_load_relaxed(&_PyRuntime.ceval.pending.calls_to_do) | \
104 _PyRuntime.ceval.pending.async_exc)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000105
106#define SET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600108 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 1); \
109 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000111
112#define RESET_GIL_DROP_REQUEST() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600114 _Py_atomic_store_relaxed(&_PyRuntime.ceval.gil_drop_request, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 COMPUTE_EVAL_BREAKER(); \
116 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000117
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000118/* Pending calls are only modified under pending_lock */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000119#define SIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600121 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 1); \
122 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000124
125#define UNSIGNAL_PENDING_CALLS() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600127 _Py_atomic_store_relaxed(&_PyRuntime.ceval.pending.calls_to_do, 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 COMPUTE_EVAL_BREAKER(); \
129 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000130
131#define SIGNAL_ASYNC_EXC() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 do { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600133 _PyRuntime.ceval.pending.async_exc = 1; \
134 _Py_atomic_store_relaxed(&_PyRuntime.ceval.eval_breaker, 1); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000136
137#define UNSIGNAL_ASYNC_EXC() \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600138 do { \
139 _PyRuntime.ceval.pending.async_exc = 0; \
140 COMPUTE_EVAL_BREAKER(); \
141 } while (0)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000142
143
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000144#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000145#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000146#endif
Guido van Rossum49b56061998-10-01 20:42:43 +0000147#include "pythread.h"
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000148#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000149
Tim Peters7f468f22004-10-11 02:40:51 +0000150int
151PyEval_ThreadsInitialized(void)
152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000153 return gil_created();
Tim Peters7f468f22004-10-11 02:40:51 +0000154}
155
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000156void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000157PyEval_InitThreads(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000158{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 if (gil_created())
160 return;
161 create_gil();
162 take_gil(PyThreadState_GET());
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600163 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
164 if (!_PyRuntime.ceval.pending.lock)
165 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000166}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000167
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000168void
Antoine Pitrou1df15362010-09-13 14:16:46 +0000169_PyEval_FiniThreads(void)
170{
171 if (!gil_created())
172 return;
173 destroy_gil();
174 assert(!gil_created());
175}
176
177void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000178PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000179{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000180 PyThreadState *tstate = PyThreadState_GET();
181 if (tstate == NULL)
182 Py_FatalError("PyEval_AcquireLock: current thread state is NULL");
183 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000184}
185
186void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000187PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000188{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 /* This function must succeed when the current thread state is NULL.
190 We therefore avoid PyThreadState_GET() which dumps a fatal error
191 in debug mode.
192 */
193 drop_gil((PyThreadState*)_Py_atomic_load_relaxed(
194 &_PyThreadState_Current));
Guido van Rossum25ce5661997-08-02 03:10:38 +0000195}
196
197void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000198PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000199{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 if (tstate == NULL)
201 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
202 /* Check someone has called PyEval_InitThreads() to create the lock */
203 assert(gil_created());
204 take_gil(tstate);
205 if (PyThreadState_Swap(tstate) != NULL)
206 Py_FatalError(
207 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000208}
209
210void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000211PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000212{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 if (tstate == NULL)
214 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
215 if (PyThreadState_Swap(NULL) != tstate)
216 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
217 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000218}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000219
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200220/* This function is called from PyOS_AfterFork_Child to destroy all threads
221 * which are not running in the child process, and clear internal locks
222 * which might be held by those threads.
223 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000224
225void
226PyEval_ReInitThreads(void)
227{
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200228 PyThreadState *current_tstate = PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 if (!gil_created())
231 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600233 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200234 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600235 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000236
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200237 /* Destroy all threads except the current one */
238 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000239}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000240
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000241/* This function is used to signal that async exceptions are waiting to be
242 raised, therefore it is also useful in non-threaded builds. */
243
244void
245_PyEval_SignalAsyncExc(void)
246{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000249
Guido van Rossumff4949e1992-08-05 19:58:53 +0000250/* Functions save_thread and restore_thread are always defined so
251 dynamically loaded modules needn't be compiled separately for use
252 with and without threads: */
253
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000254PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000255PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 PyThreadState *tstate = PyThreadState_Swap(NULL);
258 if (tstate == NULL)
259 Py_FatalError("PyEval_SaveThread: NULL tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 if (gil_created())
261 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000263}
264
265void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000266PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000267{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 if (tstate == NULL)
269 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (gil_created()) {
271 int err = errno;
272 take_gil(tstate);
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200273 /* _Py_Finalizing is protected by the GIL */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600274 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200275 drop_gil(tstate);
276 PyThread_exit_thread();
Barry Warsawb2e57942017-09-14 18:13:16 -0700277 Py_UNREACHABLE();
Antoine Pitrou0d5e52d2011-05-04 20:02:30 +0200278 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 errno = err;
280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000282}
283
284
Guido van Rossuma9672091994-09-14 13:31:22 +0000285/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
286 signal handlers or Mac I/O completion routines) can schedule calls
287 to a function to be called synchronously.
288 The synchronous function is called with one void* argument.
289 It should return 0 for success or -1 for failure -- failure should
290 be accompanied by an exception.
291
292 If registry succeeds, the registry function returns 0; if it fails
293 (e.g. due to too many pending calls) it returns -1 (without setting
294 an exception condition).
295
296 Note that because registry may occur from within signal handlers,
297 or other asynchronous events, calling malloc() is unsafe!
298
Guido van Rossuma9672091994-09-14 13:31:22 +0000299 Any thread can schedule pending calls, but only the main thread
300 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000301 There is no facility to schedule calls to a particular thread, but
302 that should be easy to change, should that ever be required. In
303 that case, the static variables here should go into the python
304 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000305*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000306
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200307void
308_PyEval_SignalReceived(void)
309{
310 /* bpo-30703: Function called when the C signal handler of Python gets a
311 signal. We cannot queue a callback using Py_AddPendingCall() since
312 that function is not async-signal-safe. */
313 SIGNAL_PENDING_CALLS();
314}
315
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200316/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000317 scheduling to be made from any thread, and even from an executing
318 callback.
319 */
320
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000321int
322Py_AddPendingCall(int (*func)(void *), void *arg)
323{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600325 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000326
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 /* try a few times for the lock. Since this mechanism is used
328 * for signal handling (on the main thread), there is a (slim)
329 * chance that a signal is delivered on the same thread while we
330 * hold the lock during the Py_MakePendingCalls() function.
331 * This avoids a deadlock in that case.
332 * Note that signals can be delivered on any thread. In particular,
333 * on Windows, a SIGINT is delivered on a system-created worker
334 * thread.
335 * We also check for lock being NULL, in the unlikely case that
336 * this function is called before any bytecode evaluation takes place.
337 */
338 if (lock != NULL) {
339 for (i = 0; i<100; i++) {
340 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
341 break;
342 }
343 if (i == 100)
344 return -1;
345 }
346
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600347 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600349 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 result = -1; /* Queue full */
351 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600352 _PyRuntime.ceval.pending.calls[i].func = func;
353 _PyRuntime.ceval.pending.calls[i].arg = arg;
354 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 }
356 /* signal main loop */
357 SIGNAL_PENDING_CALLS();
358 if (lock != NULL)
359 PyThread_release_lock(lock);
360 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000361}
362
363int
364Py_MakePendingCalls(void)
365{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200366 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 int i;
368 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000369
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200370 assert(PyGILState_Check());
371
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600372 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600374 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
375 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 return -1;
377 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600380 if (_PyRuntime.ceval.pending.main_thread &&
381 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
382 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600384 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000385 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200386 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200388 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200389 /* unsignal before starting to call callbacks, so that any callback
390 added in-between re-signals */
391 UNSIGNAL_PENDING_CALLS();
392
393 /* Python signal handler doesn't really queue a callback: it only signals
394 that a signal was received, see _PyEval_SignalReceived(). */
395 if (PyErr_CheckSignals() < 0) {
396 goto error;
397 }
398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 /* perform a bounded number of calls, in case of recursion */
400 for (i=0; i<NPENDINGCALLS; i++) {
401 int j;
402 int (*func)(void *);
403 void *arg = NULL;
404
405 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600406 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
407 j = _PyRuntime.ceval.pending.first;
408 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 func = NULL; /* Queue empty */
410 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600411 func = _PyRuntime.ceval.pending.calls[j].func;
412 arg = _PyRuntime.ceval.pending.calls[j].arg;
413 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000414 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600415 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000416 /* having released the lock, perform the callback */
417 if (func == NULL)
418 break;
419 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200420 if (r) {
421 goto error;
422 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200424
Charles-François Natalif23339a2011-07-23 18:15:43 +0200425 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200427
428error:
429 busy = 0;
430 SIGNAL_PENDING_CALLS(); /* We're not done yet */
431 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000432}
433
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000434/* The interpreter's recursion limit */
435
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000436#ifndef Py_DEFAULT_RECURSION_LIMIT
437#define Py_DEFAULT_RECURSION_LIMIT 1000
438#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600439
Eric Snow05351c12017-09-05 21:43:08 -0700440int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000441
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600442void
443_PyEval_Initialize(struct _ceval_runtime_state *state)
444{
445 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
446 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
447 _gil_initialize(&state->gil);
448}
449
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000450int
451Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000452{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600453 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000454}
455
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000456void
457Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000458{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600459 _PyRuntime.ceval.recursion_limit = new_limit;
460 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000461}
462
Armin Rigo2b3eb402003-10-28 12:05:48 +0000463/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
464 if the recursion_depth reaches _Py_CheckRecursionLimit.
465 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
466 to guarantee that _Py_CheckRecursiveCall() is regularly called.
467 Without USE_STACKCHECK, there is no need for this. */
468int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300469_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000470{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 PyThreadState *tstate = PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600472 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000473
474#ifdef USE_STACKCHECK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 if (PyOS_CheckStack()) {
476 --tstate->recursion_depth;
477 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
478 return -1;
479 }
Armin Rigo2b3eb402003-10-28 12:05:48 +0000480#endif
Eric Snow05351c12017-09-05 21:43:08 -0700481 _Py_CheckRecursionLimit = recursion_limit;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000482 if (tstate->recursion_critical)
483 /* Somebody asked that we don't check for recursion. */
484 return 0;
485 if (tstate->overflowed) {
486 if (tstate->recursion_depth > recursion_limit + 50) {
487 /* Overflowing while handling an overflow. Give up. */
488 Py_FatalError("Cannot recover from stack overflow.");
489 }
490 return 0;
491 }
492 if (tstate->recursion_depth > recursion_limit) {
493 --tstate->recursion_depth;
494 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400495 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 "maximum recursion depth exceeded%s",
497 where);
498 return -1;
499 }
500 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000501}
502
Guido van Rossum374a9221991-04-04 10:40:29 +0000503/* Status code for main loop (reason for stack unwind) */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000504enum why_code {
Stefan Krahb7e10102010-06-23 18:42:39 +0000505 WHY_NOT = 0x0001, /* No error */
506 WHY_EXCEPTION = 0x0002, /* Exception occurred */
Stefan Krahb7e10102010-06-23 18:42:39 +0000507 WHY_RETURN = 0x0008, /* 'return' statement */
508 WHY_BREAK = 0x0010, /* 'break' statement */
509 WHY_CONTINUE = 0x0020, /* 'continue' statement */
510 WHY_YIELD = 0x0040, /* 'yield' operator */
511 WHY_SILENCED = 0x0080 /* Exception silenced by 'with' */
Raymond Hettinger7c958652004-04-06 10:11:10 +0000512};
Guido van Rossum374a9221991-04-04 10:40:29 +0000513
Benjamin Peterson87880242011-07-03 16:48:31 -0500514static void save_exc_state(PyThreadState *, PyFrameObject *);
515static void swap_exc_state(PyThreadState *, PyFrameObject *);
516static void restore_and_clear_exc_state(PyThreadState *, PyFrameObject *);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400517static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000518static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000519
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600520#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000521
Guido van Rossum374a9221991-04-04 10:40:29 +0000522
Guido van Rossumb209a111997-04-29 18:18:01 +0000523PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000524PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000525{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 return PyEval_EvalCodeEx(co,
527 globals, locals,
528 (PyObject **)NULL, 0,
529 (PyObject **)NULL, 0,
530 (PyObject **)NULL, 0,
531 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000532}
533
534
535/* Interpreter main loop */
536
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000537PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000538PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000539 /* This is for backward compatibility with extension modules that
540 used this API; core interpreter code should call
541 PyEval_EvalFrameEx() */
542 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000543}
544
545PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000547{
Brett Cannon3cebf932016-09-05 15:33:46 -0700548 PyThreadState *tstate = PyThreadState_GET();
549 return tstate->interp->eval_frame(f, throwflag);
550}
551
Victor Stinnerc6944e72016-11-11 02:13:35 +0100552PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700553_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
554{
Guido van Rossum950361c1997-01-24 13:49:28 +0000555#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000556 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000557#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200558 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300559 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200560 int opcode; /* Current opcode */
561 int oparg; /* Current opcode argument, if any */
562 enum why_code why; /* Reason for block stack unwind */
563 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 PyObject *retval = NULL; /* Return value */
565 PyThreadState *tstate = PyThreadState_GET();
566 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000567
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 is true when the line being executed has changed. The
573 initial values are such as to make this false the first
574 time it is tested. */
575 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000576
Serhiy Storchakaab874002016-09-11 13:48:15 +0300577 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyObject *names;
579 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000580
Brett Cannon368b4b72012-04-02 12:17:59 -0400581#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200582 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400583#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200584
Antoine Pitroub52ec782009-01-25 16:34:23 +0000585/* Computed GOTOs, or
586 the-optimization-commonly-but-improperly-known-as-"threaded code"
587 using gcc's labels-as-values extension
588 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
589
590 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000592 combined with a lookup table of jump addresses. However, since the
593 indirect jump instruction is shared by all opcodes, the CPU will have a
594 hard time making the right prediction for where to jump next (actually,
595 it will be always wrong except in the uncommon case of a sequence of
596 several identical opcodes).
597
598 "Threaded code" in contrast, uses an explicit jump table and an explicit
599 indirect jump instruction at the end of each opcode. Since the jump
600 instruction is at a different address for each opcode, the CPU will make a
601 separate prediction for each of these instructions, which is equivalent to
602 predicting the second opcode of each opcode pair. These predictions have
603 a much better chance to turn out valid, especially in small bytecode loops.
604
605 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000607 and potentially many more instructions (depending on the pipeline width).
608 A correctly predicted branch, however, is nearly free.
609
610 At the time of this writing, the "threaded code" version is up to 15-20%
611 faster than the normal "switch" version, depending on the compiler and the
612 CPU architecture.
613
614 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
615 because it would render the measurements invalid.
616
617
618 NOTE: care must be taken that the compiler doesn't try to "optimize" the
619 indirect jumps by sharing them between all opcodes. Such optimizations
620 can be disabled on gcc by using the -fno-gcse flag (or possibly
621 -fno-crossjumping).
622*/
623
Antoine Pitrou042b1282010-08-13 21:15:58 +0000624#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000625#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000626#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000627#endif
628
Antoine Pitrou042b1282010-08-13 21:15:58 +0000629#ifdef HAVE_COMPUTED_GOTOS
630 #ifndef USE_COMPUTED_GOTOS
631 #define USE_COMPUTED_GOTOS 1
632 #endif
633#else
634 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
635 #error "Computed gotos are not supported on this compiler."
636 #endif
637 #undef USE_COMPUTED_GOTOS
638 #define USE_COMPUTED_GOTOS 0
639#endif
640
641#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000642/* Import the static jump table */
643#include "opcode_targets.h"
644
Antoine Pitroub52ec782009-01-25 16:34:23 +0000645#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 TARGET_##op: \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 case op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000648
Antoine Pitroub52ec782009-01-25 16:34:23 +0000649#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600651 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 FAST_DISPATCH(); \
653 } \
654 continue; \
655 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000656
657#ifdef LLTRACE
658#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700660 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300662 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300663 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 } \
665 goto fast_next_opcode; \
666 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000667#else
668#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700670 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300672 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300673 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 } \
675 goto fast_next_opcode; \
676 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000677#endif
678
679#else
680#define TARGET(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 case op:
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300682
Antoine Pitroub52ec782009-01-25 16:34:23 +0000683#define DISPATCH() continue
684#define FAST_DISPATCH() goto fast_next_opcode
685#endif
686
687
Neal Norwitza81d2202002-07-14 00:27:26 +0000688/* Tuple access macros */
689
690#ifndef Py_DEBUG
691#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
692#else
693#define GETITEM(v, i) PyTuple_GetItem((v), (i))
694#endif
695
Guido van Rossum374a9221991-04-04 10:40:29 +0000696/* Code access macros */
697
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300698/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600699#define INSTR_OFFSET() \
700 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300701#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300702 _Py_CODEUNIT word = *next_instr; \
703 opcode = _Py_OPCODE(word); \
704 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300705 next_instr++; \
706 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300707#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
708#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000709
Raymond Hettingerf606f872003-03-16 03:11:04 +0000710/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 Some opcodes tend to come in pairs thus making it possible to
712 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300713 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 Verifying the prediction costs a single high-speed test of a register
716 variable against a constant. If the pairing was good, then the
717 processor's own internal branch predication has a high likelihood of
718 success, resulting in a nearly zero-overhead transition to the
719 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300720 including its unpredictable switch-case branch. Combined with the
721 processor's internal branch prediction, a successful PREDICT has the
722 effect of making the two opcodes run as if they were a single new opcode
723 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000724
Georg Brandl86b2fb92008-07-16 03:43:04 +0000725 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 predictions turned-on and interpret the results as if some opcodes
727 had been combined or turn-off predictions so that the opcode frequency
728 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000729
730 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 the CPU to record separate branch prediction information for each
732 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000733
Raymond Hettingerf606f872003-03-16 03:11:04 +0000734*/
735
Antoine Pitrou042b1282010-08-13 21:15:58 +0000736#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000738#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300739#define PREDICT(op) \
740 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300741 _Py_CODEUNIT word = *next_instr; \
742 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300743 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300744 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300745 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300746 goto PRED_##op; \
747 } \
748 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000749#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300750#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000751
Raymond Hettingerf606f872003-03-16 03:11:04 +0000752
Guido van Rossum374a9221991-04-04 10:40:29 +0000753/* Stack manipulation macros */
754
Martin v. Löwis18e16552006-02-15 17:27:45 +0000755/* The stack can grow at most MAXINT deep, as co_nlocals and
756 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000757#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
758#define EMPTY() (STACK_LEVEL() == 0)
759#define TOP() (stack_pointer[-1])
760#define SECOND() (stack_pointer[-2])
761#define THIRD() (stack_pointer[-3])
762#define FOURTH() (stack_pointer[-4])
763#define PEEK(n) (stack_pointer[-(n)])
764#define SET_TOP(v) (stack_pointer[-1] = (v))
765#define SET_SECOND(v) (stack_pointer[-2] = (v))
766#define SET_THIRD(v) (stack_pointer[-3] = (v))
767#define SET_FOURTH(v) (stack_pointer[-4] = (v))
768#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
769#define BASIC_STACKADJ(n) (stack_pointer += n)
770#define BASIC_PUSH(v) (*stack_pointer++ = (v))
771#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000772
Guido van Rossum96a42c81992-01-12 02:29:51 +0000773#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000775 lltrace && prtrace(TOP(), "push")); \
776 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000777#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000778 BASIC_POP())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000779#define STACKADJ(n) { (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000780 lltrace && prtrace(TOP(), "stackadj")); \
781 assert(STACK_LEVEL() <= co->co_stacksize); }
Christian Heimes0449f632007-12-15 01:27:15 +0000782#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000783 prtrace((STACK_POINTER)[-1], "ext_pop")), \
784 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000785#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000786#define PUSH(v) BASIC_PUSH(v)
787#define POP() BASIC_POP()
788#define STACKADJ(n) BASIC_STACKADJ(n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000789#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000790#endif
791
Guido van Rossum681d79a1995-07-18 14:51:37 +0000792/* Local variable macros */
793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000795
796/* The SETLOCAL() macro must not DECREF the local variable in-place and
797 then store the new value; it must copy the old value to a temporary
798 value, then store the new value, and then DECREF the temporary value.
799 This is because it is possible that during the DECREF the frame is
800 accessed by other code (e.g. a __del__ method or gc.collect()) and the
801 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000802#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000803 GETLOCAL(i) = value; \
804 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000805
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000806
807#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 while (STACK_LEVEL() > (b)->b_level) { \
809 PyObject *v = POP(); \
810 Py_XDECREF(v); \
811 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000812
813#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300814 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 PyObject *type, *value, *traceback; \
816 assert(STACK_LEVEL() >= (b)->b_level + 3); \
817 while (STACK_LEVEL() > (b)->b_level + 3) { \
818 value = POP(); \
819 Py_XDECREF(value); \
820 } \
821 type = tstate->exc_type; \
822 value = tstate->exc_value; \
823 traceback = tstate->exc_traceback; \
824 tstate->exc_type = POP(); \
825 tstate->exc_value = POP(); \
826 tstate->exc_traceback = POP(); \
827 Py_XDECREF(type); \
828 Py_XDECREF(value); \
829 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300830 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000831
Guido van Rossuma027efa1997-05-05 20:56:21 +0000832/* Start of code */
833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 /* push frame */
835 if (Py_EnterRecursiveCall(""))
836 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 if (tstate->use_tracing) {
841 if (tstate->c_tracefunc != NULL) {
842 /* tstate->c_tracefunc, if defined, is a
843 function that will be called on *every* entry
844 to a code block. Its return value, if not
845 None, is a function that will be called at
846 the start of each executed line of code.
847 (Actually, the function must return itself
848 in order to continue tracing.) The trace
849 functions are called with three arguments:
850 a pointer to the current frame, a string
851 indicating why the function is called, and
852 an argument which depends on the situation.
853 The global trace function is also called
854 whenever an exception is detected. */
855 if (call_trace_protected(tstate->c_tracefunc,
856 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100857 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* Trace function raised an error */
859 goto exit_eval_frame;
860 }
861 }
862 if (tstate->c_profilefunc != NULL) {
863 /* Similar for c_profilefunc, except it needn't
864 return itself and isn't called for "line" events */
865 if (call_trace_protected(tstate->c_profilefunc,
866 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100867 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 /* Profile function raised an error */
869 goto exit_eval_frame;
870 }
871 }
872 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000873
Łukasz Langaa785c872016-09-09 17:37:37 -0700874 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
875 dtrace_function_entry(f);
876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 co = f->f_code;
878 names = co->co_names;
879 consts = co->co_consts;
880 fastlocals = f->f_localsplus;
881 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300882 assert(PyBytes_Check(co->co_code));
883 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300884 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
885 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
886 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300887 /*
888 f->f_lasti refers to the index of the last instruction,
889 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000890
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300891 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500892 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 When the PREDICT() macros are enabled, some opcode pairs follow in
895 direct succession without updating f->f_lasti. A successful
896 prediction effectively links the two codes together as if they
897 were a single new opcode; accordingly,f->f_lasti will point to
898 the first code in the pair (for instance, GET_ITER followed by
899 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300900 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300902 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300903 next_instr = first_instr;
904 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300905 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
906 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 stack_pointer = f->f_stacktop;
909 assert(stack_pointer != NULL);
910 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200911 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000912
Yury Selivanoveb636452016-09-08 22:01:51 -0700913 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +0100914 if (!throwflag && f->f_exc_type != NULL && f->f_exc_type != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 /* We were in an except handler when we left,
916 restore the exception state which was put aside
917 (see YIELD_VALUE). */
Benjamin Peterson87880242011-07-03 16:48:31 -0500918 swap_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 }
Benjamin Peterson87880242011-07-03 16:48:31 -0500920 else
921 save_exc_state(tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000923
Tim Peters5ca576e2001-06-18 22:08:13 +0000924#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200925 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000926#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 why = WHY_NOT;
Guido van Rossumac7be682001-01-17 15:42:30 +0000929
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400930 if (throwflag) /* support for generator.throw() */
931 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000932
Victor Stinnerace47d72013-07-18 01:41:08 +0200933#ifdef Py_DEBUG
934 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100935 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000936 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200937 assert(!PyErr_Occurred());
938#endif
939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000940 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 assert(stack_pointer >= f->f_valuestack); /* else underflow */
942 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200943 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 /* Do periodic things. Doing this every time through
946 the loop would add too much overhead, so we do it
947 only every Nth instruction. We also do it if
948 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
949 event needs attention (e.g. a signal handler or
950 async I/O handler); see Py_AddPendingCall() and
951 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000952
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600953 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700954 if (_Py_OPCODE(*next_instr) == SETUP_FINALLY ||
955 _Py_OPCODE(*next_instr) == YIELD_FROM) {
956 /* Two cases where we skip running signal handlers and other
957 pending calls:
958 - If we're about to enter the try: of a try/finally (not
959 *very* useful, but might help in some cases and it's
960 traditional)
961 - If we're resuming a chain of nested 'yield from' or
962 'await' calls, then each frame is parked with YIELD_FROM
963 as its next opcode. If the user hit control-C we want to
964 wait until we've reached the innermost frame before
965 running the signal handler and raising KeyboardInterrupt
966 (see bpo-30039).
967 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 goto fast_next_opcode;
969 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600970 if (_Py_atomic_load_relaxed(
971 &_PyRuntime.ceval.pending.calls_to_do))
972 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400973 if (Py_MakePendingCalls() < 0)
974 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600976 if (_Py_atomic_load_relaxed(
977 &_PyRuntime.ceval.gil_drop_request))
978 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 /* Give another thread a chance */
980 if (PyThreadState_Swap(NULL) != tstate)
981 Py_FatalError("ceval: tstate mix-up");
982 drop_gil(tstate);
983
984 /* Other threads may run now */
985
986 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700987
988 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600989 if (_Py_IsFinalizing() &&
990 !_Py_CURRENTLY_FINALIZING(tstate))
991 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700992 drop_gil(tstate);
993 PyThread_exit_thread();
994 }
995
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 if (PyThreadState_Swap(tstate) != NULL)
997 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000998 }
999 /* Check for asynchronous exceptions. */
1000 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001001 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 tstate->async_exc = NULL;
1003 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001004 PyErr_SetNone(exc);
1005 Py_DECREF(exc);
1006 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 }
1008 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 fast_next_opcode:
1011 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001012
Łukasz Langaa785c872016-09-09 17:37:37 -07001013 if (PyDTrace_LINE_ENABLED())
1014 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001017
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001019 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001020 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 /* see maybe_call_line_trace
1022 for expository comments */
1023 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001024
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 err = maybe_call_line_trace(tstate->c_tracefunc,
1026 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001027 tstate, f,
1028 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 /* Reload possibly changed frame fields */
1030 JUMPTO(f->f_lasti);
1031 if (f->f_stacktop != NULL) {
1032 stack_pointer = f->f_stacktop;
1033 f->f_stacktop = NULL;
1034 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001035 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001037 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001038 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001041
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001042 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001043 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001044#ifdef DYNAMIC_EXECUTION_PROFILE
1045#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 dxpairs[lastopcode][opcode]++;
1047 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001048#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001050#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001051
Guido van Rossum96a42c81992-01-12 02:29:51 +00001052#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 if (lltrace) {
1056 if (HAS_ARG(opcode)) {
1057 printf("%d: %d, %d\n",
1058 f->f_lasti, opcode, oparg);
1059 }
1060 else {
1061 printf("%d: %d\n",
1062 f->f_lasti, opcode);
1063 }
1064 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001065#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 /* BEWARE!
1070 It is essential that any operation that fails sets either
1071 x to NULL, err to nonzero, or why to anything but WHY_NOT,
1072 and that no operation that succeeds does this! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 TARGET(NOP)
1075 FAST_DISPATCH();
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001076
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001077 TARGET(LOAD_FAST) {
1078 PyObject *value = GETLOCAL(oparg);
1079 if (value == NULL) {
1080 format_exc_check_arg(PyExc_UnboundLocalError,
1081 UNBOUNDLOCAL_ERROR_MSG,
1082 PyTuple_GetItem(co->co_varnames, oparg));
1083 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001085 Py_INCREF(value);
1086 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001088 }
1089
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001090 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001091 TARGET(LOAD_CONST) {
1092 PyObject *value = GETITEM(consts, oparg);
1093 Py_INCREF(value);
1094 PUSH(value);
1095 FAST_DISPATCH();
1096 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001097
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001098 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001099 TARGET(STORE_FAST) {
1100 PyObject *value = POP();
1101 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001102 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001103 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001104
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001105 TARGET(POP_TOP) {
1106 PyObject *value = POP();
1107 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001109 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001110
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001111 TARGET(ROT_TWO) {
1112 PyObject *top = TOP();
1113 PyObject *second = SECOND();
1114 SET_TOP(second);
1115 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001117 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001118
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001119 TARGET(ROT_THREE) {
1120 PyObject *top = TOP();
1121 PyObject *second = SECOND();
1122 PyObject *third = THIRD();
1123 SET_TOP(second);
1124 SET_SECOND(third);
1125 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001127 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001128
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001129 TARGET(DUP_TOP) {
1130 PyObject *top = TOP();
1131 Py_INCREF(top);
1132 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001134 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001135
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 TARGET(DUP_TOP_TWO) {
1137 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001138 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001139 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001140 Py_INCREF(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001141 STACKADJ(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001142 SET_TOP(top);
1143 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001144 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001145 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001146
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001147 TARGET(UNARY_POSITIVE) {
1148 PyObject *value = TOP();
1149 PyObject *res = PyNumber_Positive(value);
1150 Py_DECREF(value);
1151 SET_TOP(res);
1152 if (res == NULL)
1153 goto error;
1154 DISPATCH();
1155 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001156
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001157 TARGET(UNARY_NEGATIVE) {
1158 PyObject *value = TOP();
1159 PyObject *res = PyNumber_Negative(value);
1160 Py_DECREF(value);
1161 SET_TOP(res);
1162 if (res == NULL)
1163 goto error;
1164 DISPATCH();
1165 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001166
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001167 TARGET(UNARY_NOT) {
1168 PyObject *value = TOP();
1169 int err = PyObject_IsTrue(value);
1170 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (err == 0) {
1172 Py_INCREF(Py_True);
1173 SET_TOP(Py_True);
1174 DISPATCH();
1175 }
1176 else if (err > 0) {
1177 Py_INCREF(Py_False);
1178 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001179 DISPATCH();
1180 }
1181 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001182 goto error;
1183 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001184
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001185 TARGET(UNARY_INVERT) {
1186 PyObject *value = TOP();
1187 PyObject *res = PyNumber_Invert(value);
1188 Py_DECREF(value);
1189 SET_TOP(res);
1190 if (res == NULL)
1191 goto error;
1192 DISPATCH();
1193 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001194
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001195 TARGET(BINARY_POWER) {
1196 PyObject *exp = POP();
1197 PyObject *base = TOP();
1198 PyObject *res = PyNumber_Power(base, exp, Py_None);
1199 Py_DECREF(base);
1200 Py_DECREF(exp);
1201 SET_TOP(res);
1202 if (res == NULL)
1203 goto error;
1204 DISPATCH();
1205 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001206
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001207 TARGET(BINARY_MULTIPLY) {
1208 PyObject *right = POP();
1209 PyObject *left = TOP();
1210 PyObject *res = PyNumber_Multiply(left, right);
1211 Py_DECREF(left);
1212 Py_DECREF(right);
1213 SET_TOP(res);
1214 if (res == NULL)
1215 goto error;
1216 DISPATCH();
1217 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001218
Benjamin Petersond51374e2014-04-09 23:55:56 -04001219 TARGET(BINARY_MATRIX_MULTIPLY) {
1220 PyObject *right = POP();
1221 PyObject *left = TOP();
1222 PyObject *res = PyNumber_MatrixMultiply(left, right);
1223 Py_DECREF(left);
1224 Py_DECREF(right);
1225 SET_TOP(res);
1226 if (res == NULL)
1227 goto error;
1228 DISPATCH();
1229 }
1230
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001231 TARGET(BINARY_TRUE_DIVIDE) {
1232 PyObject *divisor = POP();
1233 PyObject *dividend = TOP();
1234 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1235 Py_DECREF(dividend);
1236 Py_DECREF(divisor);
1237 SET_TOP(quotient);
1238 if (quotient == NULL)
1239 goto error;
1240 DISPATCH();
1241 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001242
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001243 TARGET(BINARY_FLOOR_DIVIDE) {
1244 PyObject *divisor = POP();
1245 PyObject *dividend = TOP();
1246 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1247 Py_DECREF(dividend);
1248 Py_DECREF(divisor);
1249 SET_TOP(quotient);
1250 if (quotient == NULL)
1251 goto error;
1252 DISPATCH();
1253 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001254
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001255 TARGET(BINARY_MODULO) {
1256 PyObject *divisor = POP();
1257 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001258 PyObject *res;
1259 if (PyUnicode_CheckExact(dividend) && (
1260 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1261 // fast path; string formatting, but not if the RHS is a str subclass
1262 // (see issue28598)
1263 res = PyUnicode_Format(dividend, divisor);
1264 } else {
1265 res = PyNumber_Remainder(dividend, divisor);
1266 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001267 Py_DECREF(divisor);
1268 Py_DECREF(dividend);
1269 SET_TOP(res);
1270 if (res == NULL)
1271 goto error;
1272 DISPATCH();
1273 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001274
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001275 TARGET(BINARY_ADD) {
1276 PyObject *right = POP();
1277 PyObject *left = TOP();
1278 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001279 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1280 CPython using bytecode, it is simply worthless.
1281 See http://bugs.python.org/issue21955 and
1282 http://bugs.python.org/issue10044 for the discussion. In short,
1283 no patch shown any impact on a realistic benchmark, only a minor
1284 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001285 if (PyUnicode_CheckExact(left) &&
1286 PyUnicode_CheckExact(right)) {
1287 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001288 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001289 }
1290 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001291 sum = PyNumber_Add(left, right);
1292 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001294 Py_DECREF(right);
1295 SET_TOP(sum);
1296 if (sum == NULL)
1297 goto error;
1298 DISPATCH();
1299 }
1300
1301 TARGET(BINARY_SUBTRACT) {
1302 PyObject *right = POP();
1303 PyObject *left = TOP();
1304 PyObject *diff = PyNumber_Subtract(left, right);
1305 Py_DECREF(right);
1306 Py_DECREF(left);
1307 SET_TOP(diff);
1308 if (diff == NULL)
1309 goto error;
1310 DISPATCH();
1311 }
1312
1313 TARGET(BINARY_SUBSCR) {
1314 PyObject *sub = POP();
1315 PyObject *container = TOP();
1316 PyObject *res = PyObject_GetItem(container, sub);
1317 Py_DECREF(container);
1318 Py_DECREF(sub);
1319 SET_TOP(res);
1320 if (res == NULL)
1321 goto error;
1322 DISPATCH();
1323 }
1324
1325 TARGET(BINARY_LSHIFT) {
1326 PyObject *right = POP();
1327 PyObject *left = TOP();
1328 PyObject *res = PyNumber_Lshift(left, right);
1329 Py_DECREF(left);
1330 Py_DECREF(right);
1331 SET_TOP(res);
1332 if (res == NULL)
1333 goto error;
1334 DISPATCH();
1335 }
1336
1337 TARGET(BINARY_RSHIFT) {
1338 PyObject *right = POP();
1339 PyObject *left = TOP();
1340 PyObject *res = PyNumber_Rshift(left, right);
1341 Py_DECREF(left);
1342 Py_DECREF(right);
1343 SET_TOP(res);
1344 if (res == NULL)
1345 goto error;
1346 DISPATCH();
1347 }
1348
1349 TARGET(BINARY_AND) {
1350 PyObject *right = POP();
1351 PyObject *left = TOP();
1352 PyObject *res = PyNumber_And(left, right);
1353 Py_DECREF(left);
1354 Py_DECREF(right);
1355 SET_TOP(res);
1356 if (res == NULL)
1357 goto error;
1358 DISPATCH();
1359 }
1360
1361 TARGET(BINARY_XOR) {
1362 PyObject *right = POP();
1363 PyObject *left = TOP();
1364 PyObject *res = PyNumber_Xor(left, right);
1365 Py_DECREF(left);
1366 Py_DECREF(right);
1367 SET_TOP(res);
1368 if (res == NULL)
1369 goto error;
1370 DISPATCH();
1371 }
1372
1373 TARGET(BINARY_OR) {
1374 PyObject *right = POP();
1375 PyObject *left = TOP();
1376 PyObject *res = PyNumber_Or(left, right);
1377 Py_DECREF(left);
1378 Py_DECREF(right);
1379 SET_TOP(res);
1380 if (res == NULL)
1381 goto error;
1382 DISPATCH();
1383 }
1384
1385 TARGET(LIST_APPEND) {
1386 PyObject *v = POP();
1387 PyObject *list = PEEK(oparg);
1388 int err;
1389 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001391 if (err != 0)
1392 goto error;
1393 PREDICT(JUMP_ABSOLUTE);
1394 DISPATCH();
1395 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001397 TARGET(SET_ADD) {
1398 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001399 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 int err;
1401 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001403 if (err != 0)
1404 goto error;
1405 PREDICT(JUMP_ABSOLUTE);
1406 DISPATCH();
1407 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001408
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001409 TARGET(INPLACE_POWER) {
1410 PyObject *exp = POP();
1411 PyObject *base = TOP();
1412 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1413 Py_DECREF(base);
1414 Py_DECREF(exp);
1415 SET_TOP(res);
1416 if (res == NULL)
1417 goto error;
1418 DISPATCH();
1419 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001421 TARGET(INPLACE_MULTIPLY) {
1422 PyObject *right = POP();
1423 PyObject *left = TOP();
1424 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1425 Py_DECREF(left);
1426 Py_DECREF(right);
1427 SET_TOP(res);
1428 if (res == NULL)
1429 goto error;
1430 DISPATCH();
1431 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001432
Benjamin Petersond51374e2014-04-09 23:55:56 -04001433 TARGET(INPLACE_MATRIX_MULTIPLY) {
1434 PyObject *right = POP();
1435 PyObject *left = TOP();
1436 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1437 Py_DECREF(left);
1438 Py_DECREF(right);
1439 SET_TOP(res);
1440 if (res == NULL)
1441 goto error;
1442 DISPATCH();
1443 }
1444
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001445 TARGET(INPLACE_TRUE_DIVIDE) {
1446 PyObject *divisor = POP();
1447 PyObject *dividend = TOP();
1448 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1449 Py_DECREF(dividend);
1450 Py_DECREF(divisor);
1451 SET_TOP(quotient);
1452 if (quotient == NULL)
1453 goto error;
1454 DISPATCH();
1455 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001456
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001457 TARGET(INPLACE_FLOOR_DIVIDE) {
1458 PyObject *divisor = POP();
1459 PyObject *dividend = TOP();
1460 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1461 Py_DECREF(dividend);
1462 Py_DECREF(divisor);
1463 SET_TOP(quotient);
1464 if (quotient == NULL)
1465 goto error;
1466 DISPATCH();
1467 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001468
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001469 TARGET(INPLACE_MODULO) {
1470 PyObject *right = POP();
1471 PyObject *left = TOP();
1472 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1473 Py_DECREF(left);
1474 Py_DECREF(right);
1475 SET_TOP(mod);
1476 if (mod == NULL)
1477 goto error;
1478 DISPATCH();
1479 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001480
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001481 TARGET(INPLACE_ADD) {
1482 PyObject *right = POP();
1483 PyObject *left = TOP();
1484 PyObject *sum;
1485 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1486 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001487 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001488 }
1489 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001490 sum = PyNumber_InPlaceAdd(left, right);
1491 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001492 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001493 Py_DECREF(right);
1494 SET_TOP(sum);
1495 if (sum == NULL)
1496 goto error;
1497 DISPATCH();
1498 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001499
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001500 TARGET(INPLACE_SUBTRACT) {
1501 PyObject *right = POP();
1502 PyObject *left = TOP();
1503 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1504 Py_DECREF(left);
1505 Py_DECREF(right);
1506 SET_TOP(diff);
1507 if (diff == NULL)
1508 goto error;
1509 DISPATCH();
1510 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001511
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001512 TARGET(INPLACE_LSHIFT) {
1513 PyObject *right = POP();
1514 PyObject *left = TOP();
1515 PyObject *res = PyNumber_InPlaceLshift(left, right);
1516 Py_DECREF(left);
1517 Py_DECREF(right);
1518 SET_TOP(res);
1519 if (res == NULL)
1520 goto error;
1521 DISPATCH();
1522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001523
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001524 TARGET(INPLACE_RSHIFT) {
1525 PyObject *right = POP();
1526 PyObject *left = TOP();
1527 PyObject *res = PyNumber_InPlaceRshift(left, right);
1528 Py_DECREF(left);
1529 Py_DECREF(right);
1530 SET_TOP(res);
1531 if (res == NULL)
1532 goto error;
1533 DISPATCH();
1534 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001535
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001536 TARGET(INPLACE_AND) {
1537 PyObject *right = POP();
1538 PyObject *left = TOP();
1539 PyObject *res = PyNumber_InPlaceAnd(left, right);
1540 Py_DECREF(left);
1541 Py_DECREF(right);
1542 SET_TOP(res);
1543 if (res == NULL)
1544 goto error;
1545 DISPATCH();
1546 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001547
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001548 TARGET(INPLACE_XOR) {
1549 PyObject *right = POP();
1550 PyObject *left = TOP();
1551 PyObject *res = PyNumber_InPlaceXor(left, right);
1552 Py_DECREF(left);
1553 Py_DECREF(right);
1554 SET_TOP(res);
1555 if (res == NULL)
1556 goto error;
1557 DISPATCH();
1558 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001559
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001560 TARGET(INPLACE_OR) {
1561 PyObject *right = POP();
1562 PyObject *left = TOP();
1563 PyObject *res = PyNumber_InPlaceOr(left, right);
1564 Py_DECREF(left);
1565 Py_DECREF(right);
1566 SET_TOP(res);
1567 if (res == NULL)
1568 goto error;
1569 DISPATCH();
1570 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001571
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001572 TARGET(STORE_SUBSCR) {
1573 PyObject *sub = TOP();
1574 PyObject *container = SECOND();
1575 PyObject *v = THIRD();
1576 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 STACKADJ(-3);
Martin Panter95f53c12016-07-18 08:23:26 +00001578 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001579 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 Py_DECREF(container);
1582 Py_DECREF(sub);
1583 if (err != 0)
1584 goto error;
1585 DISPATCH();
1586 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001587
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001588 TARGET(STORE_ANNOTATION) {
1589 _Py_IDENTIFIER(__annotations__);
1590 PyObject *ann_dict;
1591 PyObject *ann = POP();
1592 PyObject *name = GETITEM(names, oparg);
1593 int err;
1594 if (f->f_locals == NULL) {
1595 PyErr_Format(PyExc_SystemError,
1596 "no locals found when storing annotation");
1597 Py_DECREF(ann);
1598 goto error;
1599 }
1600 /* first try to get __annotations__ from locals... */
1601 if (PyDict_CheckExact(f->f_locals)) {
1602 ann_dict = _PyDict_GetItemId(f->f_locals,
1603 &PyId___annotations__);
1604 if (ann_dict == NULL) {
1605 PyErr_SetString(PyExc_NameError,
1606 "__annotations__ not found");
1607 Py_DECREF(ann);
1608 goto error;
1609 }
1610 Py_INCREF(ann_dict);
1611 }
1612 else {
1613 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
1614 if (ann_str == NULL) {
1615 Py_DECREF(ann);
1616 goto error;
1617 }
1618 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
1619 if (ann_dict == NULL) {
1620 if (PyErr_ExceptionMatches(PyExc_KeyError)) {
1621 PyErr_SetString(PyExc_NameError,
1622 "__annotations__ not found");
1623 }
1624 Py_DECREF(ann);
1625 goto error;
1626 }
1627 }
1628 /* ...if succeeded, __annotations__[name] = ann */
1629 if (PyDict_CheckExact(ann_dict)) {
1630 err = PyDict_SetItem(ann_dict, name, ann);
1631 }
1632 else {
1633 err = PyObject_SetItem(ann_dict, name, ann);
1634 }
1635 Py_DECREF(ann_dict);
Yury Selivanov50c584f2016-09-08 23:38:21 -07001636 Py_DECREF(ann);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001637 if (err != 0) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001638 goto error;
1639 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001640 DISPATCH();
1641 }
1642
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 TARGET(DELETE_SUBSCR) {
1644 PyObject *sub = TOP();
1645 PyObject *container = SECOND();
1646 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 STACKADJ(-2);
Martin Panter95f53c12016-07-18 08:23:26 +00001648 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 err = PyObject_DelItem(container, sub);
1650 Py_DECREF(container);
1651 Py_DECREF(sub);
1652 if (err != 0)
1653 goto error;
1654 DISPATCH();
1655 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001656
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001657 TARGET(PRINT_EXPR) {
Victor Stinnercab75e32013-11-06 22:38:37 +01001658 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001659 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001660 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001661 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001662 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 PyErr_SetString(PyExc_RuntimeError,
1664 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001665 Py_DECREF(value);
1666 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001668 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001669 Py_DECREF(value);
1670 if (res == NULL)
1671 goto error;
1672 Py_DECREF(res);
1673 DISPATCH();
1674 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001675
Thomas Wouters434d0822000-08-24 20:11:32 +00001676#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 default: switch (opcode) {
Thomas Wouters434d0822000-08-24 20:11:32 +00001678#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001679 TARGET(RAISE_VARARGS) {
1680 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681 switch (oparg) {
1682 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001684 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001687 /* fall through */
1688 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001689 if (do_raise(exc, cause)) {
1690 why = WHY_EXCEPTION;
1691 goto fast_block_end;
1692 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 break;
1694 default:
1695 PyErr_SetString(PyExc_SystemError,
1696 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 break;
1698 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001699 goto error;
1700 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001701
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001702 TARGET(RETURN_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 retval = POP();
1704 why = WHY_RETURN;
1705 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001706 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001707
Yury Selivanov75445082015-05-11 22:57:16 -04001708 TARGET(GET_AITER) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001709 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001710 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001711 PyObject *obj = TOP();
1712 PyTypeObject *type = Py_TYPE(obj);
1713
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001714 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001715 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001716 }
Yury Selivanov75445082015-05-11 22:57:16 -04001717
1718 if (getter != NULL) {
1719 iter = (*getter)(obj);
1720 Py_DECREF(obj);
1721 if (iter == NULL) {
1722 SET_TOP(NULL);
1723 goto error;
1724 }
1725 }
1726 else {
1727 SET_TOP(NULL);
1728 PyErr_Format(
1729 PyExc_TypeError,
1730 "'async for' requires an object with "
1731 "__aiter__ method, got %.100s",
1732 type->tp_name);
1733 Py_DECREF(obj);
1734 goto error;
1735 }
1736
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001737 if (Py_TYPE(iter)->tp_as_async == NULL ||
1738 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001739
Yury Selivanov398ff912017-03-02 22:20:00 -05001740 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001741 PyErr_Format(
1742 PyExc_TypeError,
1743 "'async for' received an object from __aiter__ "
1744 "that does not implement __anext__: %.100s",
1745 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001746 Py_DECREF(iter);
1747 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001748 }
1749
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001750 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001751 DISPATCH();
1752 }
1753
1754 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001755 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001756 PyObject *next_iter = NULL;
1757 PyObject *awaitable = NULL;
1758 PyObject *aiter = TOP();
1759 PyTypeObject *type = Py_TYPE(aiter);
1760
Yury Selivanoveb636452016-09-08 22:01:51 -07001761 if (PyAsyncGen_CheckExact(aiter)) {
1762 awaitable = type->tp_as_async->am_anext(aiter);
1763 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001764 goto error;
1765 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001766 } else {
1767 if (type->tp_as_async != NULL){
1768 getter = type->tp_as_async->am_anext;
1769 }
Yury Selivanov75445082015-05-11 22:57:16 -04001770
Yury Selivanoveb636452016-09-08 22:01:51 -07001771 if (getter != NULL) {
1772 next_iter = (*getter)(aiter);
1773 if (next_iter == NULL) {
1774 goto error;
1775 }
1776 }
1777 else {
1778 PyErr_Format(
1779 PyExc_TypeError,
1780 "'async for' requires an iterator with "
1781 "__anext__ method, got %.100s",
1782 type->tp_name);
1783 goto error;
1784 }
Yury Selivanov75445082015-05-11 22:57:16 -04001785
Yury Selivanoveb636452016-09-08 22:01:51 -07001786 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1787 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001788 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001789 PyExc_TypeError,
1790 "'async for' received an invalid object "
1791 "from __anext__: %.100s",
1792 Py_TYPE(next_iter)->tp_name);
1793
1794 Py_DECREF(next_iter);
1795 goto error;
1796 } else {
1797 Py_DECREF(next_iter);
1798 }
1799 }
Yury Selivanov75445082015-05-11 22:57:16 -04001800
1801 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001802 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001803 DISPATCH();
1804 }
1805
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001806 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001807 TARGET(GET_AWAITABLE) {
1808 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001809 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001810
1811 Py_DECREF(iterable);
1812
Yury Selivanovc724bae2016-03-02 11:30:46 -05001813 if (iter != NULL && PyCoro_CheckExact(iter)) {
1814 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1815 if (yf != NULL) {
1816 /* `iter` is a coroutine object that is being
1817 awaited, `yf` is a pointer to the current awaitable
1818 being awaited on. */
1819 Py_DECREF(yf);
1820 Py_CLEAR(iter);
1821 PyErr_SetString(
1822 PyExc_RuntimeError,
1823 "coroutine is being awaited already");
1824 /* The code below jumps to `error` if `iter` is NULL. */
1825 }
1826 }
1827
Yury Selivanov75445082015-05-11 22:57:16 -04001828 SET_TOP(iter); /* Even if it's NULL */
1829
1830 if (iter == NULL) {
1831 goto error;
1832 }
1833
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001834 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001835 DISPATCH();
1836 }
1837
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001838 TARGET(YIELD_FROM) {
1839 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001840 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001841 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001842 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1843 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001844 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001845 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001846 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001847 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001848 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001849 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001850 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001851 Py_DECREF(v);
1852 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001853 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001854 if (tstate->c_tracefunc != NULL
1855 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001856 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001857 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001858 if (err < 0)
1859 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001860 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 SET_TOP(val);
1862 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001863 }
Martin Panter95f53c12016-07-18 08:23:26 +00001864 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001865 f->f_stacktop = stack_pointer;
1866 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001867 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001868 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001869 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001870 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001872
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001873 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001875
1876 if (co->co_flags & CO_ASYNC_GENERATOR) {
1877 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1878 Py_DECREF(retval);
1879 if (w == NULL) {
1880 retval = NULL;
1881 goto error;
1882 }
1883 retval = w;
1884 }
1885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 f->f_stacktop = stack_pointer;
1887 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001890
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001891 TARGET(POP_EXCEPT) {
1892 PyTryBlock *b = PyFrame_BlockPop(f);
1893 if (b->b_type != EXCEPT_HANDLER) {
1894 PyErr_SetString(PyExc_SystemError,
1895 "popped block is not an except handler");
1896 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001897 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001898 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001901
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001902 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 TARGET(POP_BLOCK) {
1904 PyTryBlock *b = PyFrame_BlockPop(f);
1905 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001907 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001909 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 TARGET(END_FINALLY) {
1911 PyObject *status = POP();
1912 if (PyLong_Check(status)) {
1913 why = (enum why_code) PyLong_AS_LONG(status);
1914 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 if (why == WHY_RETURN ||
1916 why == WHY_CONTINUE)
1917 retval = POP();
1918 if (why == WHY_SILENCED) {
1919 /* An exception was silenced by 'with', we must
1920 manually unwind the EXCEPT_HANDLER block which was
1921 created when the exception was caught, otherwise
1922 the stack will be in an inconsistent state. */
1923 PyTryBlock *b = PyFrame_BlockPop(f);
1924 assert(b->b_type == EXCEPT_HANDLER);
1925 UNWIND_EXCEPT_HANDLER(b);
1926 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 Py_DECREF(status);
1928 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001929 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001930 Py_DECREF(status);
1931 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 else if (PyExceptionClass_Check(status)) {
1934 PyObject *exc = POP();
1935 PyObject *tb = POP();
1936 PyErr_Restore(status, exc, tb);
1937 why = WHY_EXCEPTION;
1938 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 PyErr_SetString(PyExc_SystemError,
1942 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 Py_DECREF(status);
1944 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 Py_DECREF(status);
1947 DISPATCH();
1948 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001949
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001950 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001951 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001952
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001954 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001955 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1956 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001957 PyErr_SetString(PyExc_NameError,
1958 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001959 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001960 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001962 }
1963 else {
1964 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
1965 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02001966 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001967 bc = PyObject_GetItem(f->f_builtins, build_class_str);
1968 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001969 if (PyErr_ExceptionMatches(PyExc_KeyError))
1970 PyErr_SetString(PyExc_NameError,
1971 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001972 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001973 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001975 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04001976 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02001977 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001978
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001979 TARGET(STORE_NAME) {
1980 PyObject *name = GETITEM(names, oparg);
1981 PyObject *v = POP();
1982 PyObject *ns = f->f_locals;
1983 int err;
1984 if (ns == NULL) {
1985 PyErr_Format(PyExc_SystemError,
1986 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001987 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001989 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 if (PyDict_CheckExact(ns))
1991 err = PyDict_SetItem(ns, name, v);
1992 else
1993 err = PyObject_SetItem(ns, name, v);
1994 Py_DECREF(v);
1995 if (err != 0)
1996 goto error;
1997 DISPATCH();
1998 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001999
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002000 TARGET(DELETE_NAME) {
2001 PyObject *name = GETITEM(names, oparg);
2002 PyObject *ns = f->f_locals;
2003 int err;
2004 if (ns == NULL) {
2005 PyErr_Format(PyExc_SystemError,
2006 "no locals when deleting %R", name);
2007 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002008 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 err = PyObject_DelItem(ns, name);
2010 if (err != 0) {
2011 format_exc_check_arg(PyExc_NameError,
2012 NAME_ERROR_MSG,
2013 name);
2014 goto error;
2015 }
2016 DISPATCH();
2017 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002018
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002019 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002020 TARGET(UNPACK_SEQUENCE) {
2021 PyObject *seq = POP(), *item, **items;
2022 if (PyTuple_CheckExact(seq) &&
2023 PyTuple_GET_SIZE(seq) == oparg) {
2024 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 item = items[oparg];
2027 Py_INCREF(item);
2028 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002030 } else if (PyList_CheckExact(seq) &&
2031 PyList_GET_SIZE(seq) == oparg) {
2032 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002033 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002034 item = items[oparg];
2035 Py_INCREF(item);
2036 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002037 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002038 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002039 stack_pointer + oparg)) {
2040 STACKADJ(oparg);
2041 } else {
2042 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002043 Py_DECREF(seq);
2044 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002047 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002048 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002049
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002050 TARGET(UNPACK_EX) {
2051 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2052 PyObject *seq = POP();
2053
2054 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2055 stack_pointer + totalargs)) {
2056 stack_pointer += totalargs;
2057 } else {
2058 Py_DECREF(seq);
2059 goto error;
2060 }
2061 Py_DECREF(seq);
2062 DISPATCH();
2063 }
2064
2065 TARGET(STORE_ATTR) {
2066 PyObject *name = GETITEM(names, oparg);
2067 PyObject *owner = TOP();
2068 PyObject *v = SECOND();
2069 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002072 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002073 Py_DECREF(owner);
2074 if (err != 0)
2075 goto error;
2076 DISPATCH();
2077 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002078
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002079 TARGET(DELETE_ATTR) {
2080 PyObject *name = GETITEM(names, oparg);
2081 PyObject *owner = POP();
2082 int err;
2083 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2084 Py_DECREF(owner);
2085 if (err != 0)
2086 goto error;
2087 DISPATCH();
2088 }
2089
2090 TARGET(STORE_GLOBAL) {
2091 PyObject *name = GETITEM(names, oparg);
2092 PyObject *v = POP();
2093 int err;
2094 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002096 if (err != 0)
2097 goto error;
2098 DISPATCH();
2099 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002100
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002101 TARGET(DELETE_GLOBAL) {
2102 PyObject *name = GETITEM(names, oparg);
2103 int err;
2104 err = PyDict_DelItem(f->f_globals, name);
2105 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002107 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002109 }
2110 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002112
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002113 TARGET(LOAD_NAME) {
2114 PyObject *name = GETITEM(names, oparg);
2115 PyObject *locals = f->f_locals;
2116 PyObject *v;
2117 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002118 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 "no locals when loading %R", name);
2120 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002121 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002122 if (PyDict_CheckExact(locals)) {
2123 v = PyDict_GetItem(locals, name);
2124 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002125 }
2126 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002128 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002129 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2130 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002131 PyErr_Clear();
2132 }
2133 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 if (v == NULL) {
2135 v = PyDict_GetItem(f->f_globals, name);
2136 Py_XINCREF(v);
2137 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002138 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 v = PyDict_GetItem(f->f_builtins, name);
2140 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002141 format_exc_check_arg(
2142 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002143 NAME_ERROR_MSG, name);
2144 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002145 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002147 }
2148 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 v = PyObject_GetItem(f->f_builtins, name);
2150 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002151 if (PyErr_ExceptionMatches(PyExc_KeyError))
2152 format_exc_check_arg(
2153 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002154 NAME_ERROR_MSG, name);
2155 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002156 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002157 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002161 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002162 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002163
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 TARGET(LOAD_GLOBAL) {
2165 PyObject *name = GETITEM(names, oparg);
2166 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002167 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002168 && PyDict_CheckExact(f->f_builtins))
2169 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002170 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002171 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 name);
2173 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002174 if (!_PyErr_OCCURRED()) {
2175 /* _PyDict_LoadGlobal() returns NULL without raising
2176 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002177 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002178 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002179 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002181 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002182 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002183 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002184 else {
2185 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002186
2187 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002188 v = PyObject_GetItem(f->f_globals, name);
2189 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002190 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2191 goto error;
2192 PyErr_Clear();
2193
Victor Stinnerb4efc962015-11-20 09:24:02 +01002194 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 v = PyObject_GetItem(f->f_builtins, name);
2196 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002197 if (PyErr_ExceptionMatches(PyExc_KeyError))
2198 format_exc_check_arg(
2199 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002200 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002201 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002202 }
2203 }
2204 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002208
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 TARGET(DELETE_FAST) {
2210 PyObject *v = GETLOCAL(oparg);
2211 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002212 SETLOCAL(oparg, NULL);
2213 DISPATCH();
2214 }
2215 format_exc_check_arg(
2216 PyExc_UnboundLocalError,
2217 UNBOUNDLOCAL_ERROR_MSG,
2218 PyTuple_GetItem(co->co_varnames, oparg)
2219 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 goto error;
2221 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002222
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002223 TARGET(DELETE_DEREF) {
2224 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002225 PyObject *oldobj = PyCell_GET(cell);
2226 if (oldobj != NULL) {
2227 PyCell_SET(cell, NULL);
2228 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002229 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002230 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002231 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 goto error;
2233 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002234
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002235 TARGET(LOAD_CLOSURE) {
2236 PyObject *cell = freevars[oparg];
2237 Py_INCREF(cell);
2238 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002239 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002240 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002241
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002242 TARGET(LOAD_CLASSDEREF) {
2243 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002244 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002245 assert(locals);
2246 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2247 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2248 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2249 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2250 if (PyDict_CheckExact(locals)) {
2251 value = PyDict_GetItem(locals, name);
2252 Py_XINCREF(value);
2253 }
2254 else {
2255 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002256 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002257 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2258 goto error;
2259 PyErr_Clear();
2260 }
2261 }
2262 if (!value) {
2263 PyObject *cell = freevars[oparg];
2264 value = PyCell_GET(cell);
2265 if (value == NULL) {
2266 format_exc_unbound(co, oparg);
2267 goto error;
2268 }
2269 Py_INCREF(value);
2270 }
2271 PUSH(value);
2272 DISPATCH();
2273 }
2274
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002275 TARGET(LOAD_DEREF) {
2276 PyObject *cell = freevars[oparg];
2277 PyObject *value = PyCell_GET(cell);
2278 if (value == NULL) {
2279 format_exc_unbound(co, oparg);
2280 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002282 Py_INCREF(value);
2283 PUSH(value);
2284 DISPATCH();
2285 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002286
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002287 TARGET(STORE_DEREF) {
2288 PyObject *v = POP();
2289 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002290 PyObject *oldobj = PyCell_GET(cell);
2291 PyCell_SET(cell, v);
2292 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002293 DISPATCH();
2294 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002295
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002296 TARGET(BUILD_STRING) {
2297 PyObject *str;
2298 PyObject *empty = PyUnicode_New(0, 0);
2299 if (empty == NULL) {
2300 goto error;
2301 }
2302 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2303 Py_DECREF(empty);
2304 if (str == NULL)
2305 goto error;
2306 while (--oparg >= 0) {
2307 PyObject *item = POP();
2308 Py_DECREF(item);
2309 }
2310 PUSH(str);
2311 DISPATCH();
2312 }
2313
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002314 TARGET(BUILD_TUPLE) {
2315 PyObject *tup = PyTuple_New(oparg);
2316 if (tup == NULL)
2317 goto error;
2318 while (--oparg >= 0) {
2319 PyObject *item = POP();
2320 PyTuple_SET_ITEM(tup, oparg, item);
2321 }
2322 PUSH(tup);
2323 DISPATCH();
2324 }
2325
2326 TARGET(BUILD_LIST) {
2327 PyObject *list = PyList_New(oparg);
2328 if (list == NULL)
2329 goto error;
2330 while (--oparg >= 0) {
2331 PyObject *item = POP();
2332 PyList_SET_ITEM(list, oparg, item);
2333 }
2334 PUSH(list);
2335 DISPATCH();
2336 }
2337
Serhiy Storchaka73442852016-10-02 10:33:46 +03002338 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002339 TARGET(BUILD_TUPLE_UNPACK)
2340 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002341 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002342 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002343 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002344 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002345
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002346 if (sum == NULL)
2347 goto error;
2348
2349 for (i = oparg; i > 0; i--) {
2350 PyObject *none_val;
2351
2352 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2353 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002354 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002355 PyErr_ExceptionMatches(PyExc_TypeError))
2356 {
2357 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002358 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002359 Py_DECREF(sum);
2360 goto error;
2361 }
2362 Py_DECREF(none_val);
2363 }
2364
2365 if (convert_to_tuple) {
2366 return_value = PyList_AsTuple(sum);
2367 Py_DECREF(sum);
2368 if (return_value == NULL)
2369 goto error;
2370 }
2371 else {
2372 return_value = sum;
2373 }
2374
2375 while (oparg--)
2376 Py_DECREF(POP());
2377 PUSH(return_value);
2378 DISPATCH();
2379 }
2380
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002381 TARGET(BUILD_SET) {
2382 PyObject *set = PySet_New(NULL);
2383 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002384 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002385 if (set == NULL)
2386 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002387 for (i = oparg; i > 0; i--) {
2388 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 if (err == 0)
2390 err = PySet_Add(set, item);
2391 Py_DECREF(item);
2392 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002393 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002394 if (err != 0) {
2395 Py_DECREF(set);
2396 goto error;
2397 }
2398 PUSH(set);
2399 DISPATCH();
2400 }
2401
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002402 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002403 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002404 PyObject *sum = PySet_New(NULL);
2405 if (sum == NULL)
2406 goto error;
2407
2408 for (i = oparg; i > 0; i--) {
2409 if (_PySet_Update(sum, PEEK(i)) < 0) {
2410 Py_DECREF(sum);
2411 goto error;
2412 }
2413 }
2414
2415 while (oparg--)
2416 Py_DECREF(POP());
2417 PUSH(sum);
2418 DISPATCH();
2419 }
2420
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002421 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002422 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2424 if (map == NULL)
2425 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002426 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002427 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002428 PyObject *key = PEEK(2*i);
2429 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002430 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002431 if (err != 0) {
2432 Py_DECREF(map);
2433 goto error;
2434 }
2435 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002436
2437 while (oparg--) {
2438 Py_DECREF(POP());
2439 Py_DECREF(POP());
2440 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002441 PUSH(map);
2442 DISPATCH();
2443 }
2444
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002445 TARGET(SETUP_ANNOTATIONS) {
2446 _Py_IDENTIFIER(__annotations__);
2447 int err;
2448 PyObject *ann_dict;
2449 if (f->f_locals == NULL) {
2450 PyErr_Format(PyExc_SystemError,
2451 "no locals found when setting up annotations");
2452 goto error;
2453 }
2454 /* check if __annotations__ in locals()... */
2455 if (PyDict_CheckExact(f->f_locals)) {
2456 ann_dict = _PyDict_GetItemId(f->f_locals,
2457 &PyId___annotations__);
2458 if (ann_dict == NULL) {
2459 /* ...if not, create a new one */
2460 ann_dict = PyDict_New();
2461 if (ann_dict == NULL) {
2462 goto error;
2463 }
2464 err = _PyDict_SetItemId(f->f_locals,
2465 &PyId___annotations__, ann_dict);
2466 Py_DECREF(ann_dict);
2467 if (err != 0) {
2468 goto error;
2469 }
2470 }
2471 }
2472 else {
2473 /* do the same if locals() is not a dict */
2474 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2475 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002476 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002477 }
2478 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2479 if (ann_dict == NULL) {
2480 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2481 goto error;
2482 }
2483 PyErr_Clear();
2484 ann_dict = PyDict_New();
2485 if (ann_dict == NULL) {
2486 goto error;
2487 }
2488 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2489 Py_DECREF(ann_dict);
2490 if (err != 0) {
2491 goto error;
2492 }
2493 }
2494 else {
2495 Py_DECREF(ann_dict);
2496 }
2497 }
2498 DISPATCH();
2499 }
2500
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002501 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002502 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002503 PyObject *map;
2504 PyObject *keys = TOP();
2505 if (!PyTuple_CheckExact(keys) ||
2506 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2507 PyErr_SetString(PyExc_SystemError,
2508 "bad BUILD_CONST_KEY_MAP keys argument");
2509 goto error;
2510 }
2511 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2512 if (map == NULL) {
2513 goto error;
2514 }
2515 for (i = oparg; i > 0; i--) {
2516 int err;
2517 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2518 PyObject *value = PEEK(i + 1);
2519 err = PyDict_SetItem(map, key, value);
2520 if (err != 0) {
2521 Py_DECREF(map);
2522 goto error;
2523 }
2524 }
2525
2526 Py_DECREF(POP());
2527 while (oparg--) {
2528 Py_DECREF(POP());
2529 }
2530 PUSH(map);
2531 DISPATCH();
2532 }
2533
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002534 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002535 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002536 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002537 if (sum == NULL)
2538 goto error;
2539
2540 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002541 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002542 if (PyDict_Update(sum, arg) < 0) {
2543 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2544 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002545 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002546 arg->ob_type->tp_name);
2547 }
2548 Py_DECREF(sum);
2549 goto error;
2550 }
2551 }
2552
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002553 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002554 Py_DECREF(POP());
2555 PUSH(sum);
2556 DISPATCH();
2557 }
2558
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002559 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2560 Py_ssize_t i;
2561 PyObject *sum = PyDict_New();
2562 if (sum == NULL)
2563 goto error;
2564
2565 for (i = oparg; i > 0; i--) {
2566 PyObject *arg = PEEK(i);
2567 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2568 PyObject *func = PEEK(2 + oparg);
2569 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002570 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002571 }
2572 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2573 PyObject *exc, *val, *tb;
2574 PyErr_Fetch(&exc, &val, &tb);
2575 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2576 PyObject *key = PyTuple_GET_ITEM(val, 0);
2577 if (!PyUnicode_Check(key)) {
2578 PyErr_Format(PyExc_TypeError,
2579 "%.200s%.200s keywords must be strings",
2580 PyEval_GetFuncName(func),
2581 PyEval_GetFuncDesc(func));
2582 } else {
2583 PyErr_Format(PyExc_TypeError,
2584 "%.200s%.200s got multiple "
2585 "values for keyword argument '%U'",
2586 PyEval_GetFuncName(func),
2587 PyEval_GetFuncDesc(func),
2588 key);
2589 }
2590 Py_XDECREF(exc);
2591 Py_XDECREF(val);
2592 Py_XDECREF(tb);
2593 }
2594 else {
2595 PyErr_Restore(exc, val, tb);
2596 }
2597 }
2598 Py_DECREF(sum);
2599 goto error;
2600 }
2601 }
2602
2603 while (oparg--)
2604 Py_DECREF(POP());
2605 PUSH(sum);
2606 DISPATCH();
2607 }
2608
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002609 TARGET(MAP_ADD) {
2610 PyObject *key = TOP();
2611 PyObject *value = SECOND();
2612 PyObject *map;
2613 int err;
2614 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002615 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002616 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002617 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002618 Py_DECREF(value);
2619 Py_DECREF(key);
2620 if (err != 0)
2621 goto error;
2622 PREDICT(JUMP_ABSOLUTE);
2623 DISPATCH();
2624 }
2625
2626 TARGET(LOAD_ATTR) {
2627 PyObject *name = GETITEM(names, oparg);
2628 PyObject *owner = TOP();
2629 PyObject *res = PyObject_GetAttr(owner, name);
2630 Py_DECREF(owner);
2631 SET_TOP(res);
2632 if (res == NULL)
2633 goto error;
2634 DISPATCH();
2635 }
2636
2637 TARGET(COMPARE_OP) {
2638 PyObject *right = POP();
2639 PyObject *left = TOP();
2640 PyObject *res = cmp_outcome(oparg, left, right);
2641 Py_DECREF(left);
2642 Py_DECREF(right);
2643 SET_TOP(res);
2644 if (res == NULL)
2645 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002646 PREDICT(POP_JUMP_IF_FALSE);
2647 PREDICT(POP_JUMP_IF_TRUE);
2648 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002649 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002650
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002651 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002652 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002653 PyObject *fromlist = POP();
2654 PyObject *level = TOP();
2655 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002656 res = import_name(f, name, fromlist, level);
2657 Py_DECREF(level);
2658 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 SET_TOP(res);
2660 if (res == NULL)
2661 goto error;
2662 DISPATCH();
2663 }
2664
2665 TARGET(IMPORT_STAR) {
2666 PyObject *from = POP(), *locals;
2667 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002668 if (PyFrame_FastToLocalsWithError(f) < 0) {
2669 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002670 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002671 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002672
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002673 locals = f->f_locals;
2674 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 PyErr_SetString(PyExc_SystemError,
2676 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002677 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002678 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002680 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002681 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 Py_DECREF(from);
2683 if (err != 0)
2684 goto error;
2685 DISPATCH();
2686 }
Guido van Rossum25831651993-05-19 14:50:45 +00002687
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002688 TARGET(IMPORT_FROM) {
2689 PyObject *name = GETITEM(names, oparg);
2690 PyObject *from = TOP();
2691 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002693 PUSH(res);
2694 if (res == NULL)
2695 goto error;
2696 DISPATCH();
2697 }
Thomas Wouters52152252000-08-17 22:55:00 +00002698
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 JUMPBY(oparg);
2701 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002702 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002703
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002704 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 TARGET(POP_JUMP_IF_FALSE) {
2706 PyObject *cond = POP();
2707 int err;
2708 if (cond == Py_True) {
2709 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 FAST_DISPATCH();
2711 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002712 if (cond == Py_False) {
2713 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002714 JUMPTO(oparg);
2715 FAST_DISPATCH();
2716 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 err = PyObject_IsTrue(cond);
2718 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002720 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002721 else if (err == 0)
2722 JUMPTO(oparg);
2723 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002727
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002728 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 TARGET(POP_JUMP_IF_TRUE) {
2730 PyObject *cond = POP();
2731 int err;
2732 if (cond == Py_False) {
2733 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 FAST_DISPATCH();
2735 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002736 if (cond == Py_True) {
2737 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 JUMPTO(oparg);
2739 FAST_DISPATCH();
2740 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002741 err = PyObject_IsTrue(cond);
2742 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002743 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002744 JUMPTO(oparg);
2745 }
2746 else if (err == 0)
2747 ;
2748 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002751 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002752
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 TARGET(JUMP_IF_FALSE_OR_POP) {
2754 PyObject *cond = TOP();
2755 int err;
2756 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002758 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 FAST_DISPATCH();
2760 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 JUMPTO(oparg);
2763 FAST_DISPATCH();
2764 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002765 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 if (err > 0) {
2767 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002768 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002769 }
2770 else if (err == 0)
2771 JUMPTO(oparg);
2772 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002774 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002775 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002776
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002777 TARGET(JUMP_IF_TRUE_OR_POP) {
2778 PyObject *cond = TOP();
2779 int err;
2780 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002783 FAST_DISPATCH();
2784 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 JUMPTO(oparg);
2787 FAST_DISPATCH();
2788 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002790 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002791 JUMPTO(oparg);
2792 }
2793 else if (err == 0) {
2794 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 }
2797 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002801
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002802 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002805#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 /* Enabling this path speeds-up all while and for-loops by bypassing
2807 the per-loop checks for signals. By default, this should be turned-off
2808 because it prevents detection of a control-break in tight loops like
2809 "while 1: pass". Compile with this option turned-on when you need
2810 the speed-up and do not need break checking inside tight loops (ones
2811 that contain only instructions ending with FAST_DISPATCH).
2812 */
2813 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002814#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002815 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002816#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002817 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002818
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002821 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002822 PyObject *iter = PyObject_GetIter(iterable);
2823 Py_DECREF(iterable);
2824 SET_TOP(iter);
2825 if (iter == NULL)
2826 goto error;
2827 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002828 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002829 DISPATCH();
2830 }
2831
2832 TARGET(GET_YIELD_FROM_ITER) {
2833 /* before: [obj]; after [getiter(obj)] */
2834 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002835 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002836 if (PyCoro_CheckExact(iterable)) {
2837 /* `iterable` is a coroutine */
2838 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2839 /* and it is used in a 'yield from' expression of a
2840 regular generator. */
2841 Py_DECREF(iterable);
2842 SET_TOP(NULL);
2843 PyErr_SetString(PyExc_TypeError,
2844 "cannot 'yield from' a coroutine object "
2845 "in a non-coroutine generator");
2846 goto error;
2847 }
2848 }
2849 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002850 /* `iterable` is not a generator. */
2851 iter = PyObject_GetIter(iterable);
2852 Py_DECREF(iterable);
2853 SET_TOP(iter);
2854 if (iter == NULL)
2855 goto error;
2856 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002857 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 DISPATCH();
2859 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002860
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002861 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002863 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 PyObject *iter = TOP();
2865 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2866 if (next != NULL) {
2867 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 PREDICT(STORE_FAST);
2869 PREDICT(UNPACK_SEQUENCE);
2870 DISPATCH();
2871 }
2872 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002873 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2874 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002875 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002876 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002877 PyErr_Clear();
2878 }
2879 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002880 STACKADJ(-1);
2881 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002882 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002883 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002885 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002886
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002887 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 why = WHY_BREAK;
2889 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002890 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002891
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002892 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002893 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002894 if (retval == NULL)
2895 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 why = WHY_CONTINUE;
2897 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002898 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002899
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002900 TARGET(SETUP_LOOP)
2901 TARGET(SETUP_EXCEPT)
2902 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 /* NOTE: If you add any new block-setup opcodes that
2904 are not try/except/finally handlers, you may need
2905 to update the PyGen_NeedsFinalizing() function.
2906 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002908 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2909 STACK_LEVEL());
2910 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002912
Yury Selivanov75445082015-05-11 22:57:16 -04002913 TARGET(BEFORE_ASYNC_WITH) {
2914 _Py_IDENTIFIER(__aexit__);
2915 _Py_IDENTIFIER(__aenter__);
2916
2917 PyObject *mgr = TOP();
2918 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2919 *enter;
2920 PyObject *res;
2921 if (exit == NULL)
2922 goto error;
2923 SET_TOP(exit);
2924 enter = special_lookup(mgr, &PyId___aenter__);
2925 Py_DECREF(mgr);
2926 if (enter == NULL)
2927 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002928 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002929 Py_DECREF(enter);
2930 if (res == NULL)
2931 goto error;
2932 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002933 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002934 DISPATCH();
2935 }
2936
2937 TARGET(SETUP_ASYNC_WITH) {
2938 PyObject *res = POP();
2939 /* Setup the finally block before pushing the result
2940 of __aenter__ on the stack. */
2941 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2942 STACK_LEVEL());
2943 PUSH(res);
2944 DISPATCH();
2945 }
2946
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002947 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002948 _Py_IDENTIFIER(__exit__);
2949 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002951 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002952 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002953 if (enter == NULL)
2954 goto error;
2955 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002956 if (exit == NULL) {
2957 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002958 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002959 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002960 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002961 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002962 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002963 Py_DECREF(enter);
2964 if (res == NULL)
2965 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002966 /* Setup the finally block before pushing the result
2967 of __enter__ on the stack. */
2968 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2969 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002970
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 DISPATCH();
2973 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002974
Yury Selivanov75445082015-05-11 22:57:16 -04002975 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04002976 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002977 how/why we entered the finally clause:
2978 - TOP = None
2979 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
2980 - TOP = WHY_*; no retval below it
2981 - (TOP, SECOND, THIRD) = exc_info()
2982 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
2983 Below them is EXIT, the context.__exit__ bound method.
2984 In the last case, we must call
2985 EXIT(TOP, SECOND, THIRD)
2986 otherwise we must call
2987 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00002988
Benjamin Peterson8f169482013-10-29 22:25:06 -04002989 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04002991 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002992 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002993
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 In addition, if the stack represents an exception,
2995 *and* the function call returns a 'true' value, we
2996 push WHY_SILENCED onto the stack. END_FINALLY will
2997 then not re-raise the exception. (But non-local
2998 gotos should still be resumed.)
2999 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003000
Victor Stinner842cfff2016-12-01 14:45:31 +01003001 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003002 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003003 PyObject *exc, *val, *tb, *res;
3004
3005 val = tb = Py_None;
3006 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003007 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003008 (void)POP();
3009 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003010 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003012 else if (PyLong_Check(exc)) {
3013 STACKADJ(-1);
3014 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 case WHY_RETURN:
3016 case WHY_CONTINUE:
3017 /* Retval in TOP. */
3018 exit_func = SECOND();
3019 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003020 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003021 break;
3022 default:
3023 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003024 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003025 break;
3026 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 }
3029 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003031 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 val = SECOND();
3033 tb = THIRD();
3034 tp2 = FOURTH();
3035 exc2 = PEEK(5);
3036 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003038 SET_VALUE(7, tb2);
3039 SET_VALUE(6, exc2);
3040 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3042 SET_FOURTH(NULL);
3043 /* We just shifted the stack down, so we have
3044 to tell the except handler block that the
3045 values are lower than it expects. */
3046 block = &f->f_blockstack[f->f_iblock - 1];
3047 assert(block->b_type == EXCEPT_HANDLER);
3048 block->b_level--;
3049 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003050
3051 stack[0] = exc;
3052 stack[1] = val;
3053 stack[2] = tb;
3054 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003056 if (res == NULL)
3057 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003058
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003059 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003060 PUSH(exc);
3061 PUSH(res);
3062 PREDICT(WITH_CLEANUP_FINISH);
3063 DISPATCH();
3064 }
3065
3066 PREDICTED(WITH_CLEANUP_FINISH);
3067 TARGET(WITH_CLEANUP_FINISH) {
3068 PyObject *res = POP();
3069 PyObject *exc = POP();
3070 int err;
3071
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003072 if (exc != Py_None)
3073 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 else
3075 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003076
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003077 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003078 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003079
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003082 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 /* There was an exception and a True return */
3084 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3085 }
3086 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003088 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003089
Yury Selivanovf2392132016-12-13 19:03:51 -05003090 TARGET(LOAD_METHOD) {
3091 /* Designed to work in tamdem with CALL_METHOD. */
3092 PyObject *name = GETITEM(names, oparg);
3093 PyObject *obj = TOP();
3094 PyObject *meth = NULL;
3095
3096 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3097
Yury Selivanovf2392132016-12-13 19:03:51 -05003098 if (meth == NULL) {
3099 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003100 goto error;
3101 }
3102
3103 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003104 /* We can bypass temporary bound method object.
3105 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003106
INADA Naoki015bce62017-01-16 17:23:30 +09003107 meth | self | arg1 | ... | argN
3108 */
3109 SET_TOP(meth);
3110 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003111 }
3112 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003113 /* meth is not an unbound method (but a regular attr, or
3114 something was returned by a descriptor protocol). Set
3115 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003116 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003117
3118 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003119 */
INADA Naoki015bce62017-01-16 17:23:30 +09003120 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003121 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003122 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003123 }
3124 DISPATCH();
3125 }
3126
3127 TARGET(CALL_METHOD) {
3128 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003129 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003130
3131 sp = stack_pointer;
3132
INADA Naoki015bce62017-01-16 17:23:30 +09003133 meth = PEEK(oparg + 2);
3134 if (meth == NULL) {
3135 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3136 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003137
3138 Stack layout:
3139
INADA Naoki015bce62017-01-16 17:23:30 +09003140 ... | NULL | callable | arg1 | ... | argN
3141 ^- TOP()
3142 ^- (-oparg)
3143 ^- (-oparg-1)
3144 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003145
Ville Skyttä49b27342017-08-03 09:00:59 +03003146 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003147 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003148 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003149 res = call_function(&sp, oparg, NULL);
3150 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003151 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003152 }
3153 else {
3154 /* This is a method call. Stack layout:
3155
INADA Naoki015bce62017-01-16 17:23:30 +09003156 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003157 ^- TOP()
3158 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003159 ^- (-oparg-1)
3160 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003161
INADA Naoki015bce62017-01-16 17:23:30 +09003162 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003163 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003164 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003165 */
3166 res = call_function(&sp, oparg + 1, NULL);
3167 stack_pointer = sp;
3168 }
3169
3170 PUSH(res);
3171 if (res == NULL)
3172 goto error;
3173 DISPATCH();
3174 }
3175
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003176 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003177 TARGET(CALL_FUNCTION) {
3178 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003180 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003182 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003183 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003184 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003186 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003187 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003188
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003189 TARGET(CALL_FUNCTION_KW) {
3190 PyObject **sp, *res, *names;
3191
3192 names = POP();
3193 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003194 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003195 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003197 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003198 Py_DECREF(names);
3199
3200 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003202 }
3203 DISPATCH();
3204 }
3205
3206 TARGET(CALL_FUNCTION_EX) {
3207 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003208 if (oparg & 0x01) {
3209 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003210 if (!PyDict_CheckExact(kwargs)) {
3211 PyObject *d = PyDict_New();
3212 if (d == NULL)
3213 goto error;
3214 if (PyDict_Update(d, kwargs) != 0) {
3215 Py_DECREF(d);
3216 /* PyDict_Update raises attribute
3217 * error (percolated from an attempt
3218 * to get 'keys' attribute) instead of
3219 * a type error if its second argument
3220 * is not a mapping.
3221 */
3222 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003223 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003224 }
Victor Stinnereece2222016-09-12 11:16:37 +02003225 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003226 goto error;
3227 }
3228 Py_DECREF(kwargs);
3229 kwargs = d;
3230 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003231 assert(PyDict_CheckExact(kwargs));
3232 }
3233 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003234 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003235 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003236 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003237 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003238 goto error;
3239 }
3240 Py_SETREF(callargs, PySequence_Tuple(callargs));
3241 if (callargs == NULL) {
3242 goto error;
3243 }
3244 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003245 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003246
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003247 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003248 Py_DECREF(func);
3249 Py_DECREF(callargs);
3250 Py_XDECREF(kwargs);
3251
3252 SET_TOP(result);
3253 if (result == NULL) {
3254 goto error;
3255 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003256 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003257 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003258
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003259 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003260 PyObject *qualname = POP();
3261 PyObject *codeobj = POP();
3262 PyFunctionObject *func = (PyFunctionObject *)
3263 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003264
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003265 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003266 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003267 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003268 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003269 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003270
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003271 if (oparg & 0x08) {
3272 assert(PyTuple_CheckExact(TOP()));
3273 func ->func_closure = POP();
3274 }
3275 if (oparg & 0x04) {
3276 assert(PyDict_CheckExact(TOP()));
3277 func->func_annotations = POP();
3278 }
3279 if (oparg & 0x02) {
3280 assert(PyDict_CheckExact(TOP()));
3281 func->func_kwdefaults = POP();
3282 }
3283 if (oparg & 0x01) {
3284 assert(PyTuple_CheckExact(TOP()));
3285 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003287
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003288 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003289 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003291
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003292 TARGET(BUILD_SLICE) {
3293 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003294 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003295 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003296 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003297 step = NULL;
3298 stop = POP();
3299 start = TOP();
3300 slice = PySlice_New(start, stop, step);
3301 Py_DECREF(start);
3302 Py_DECREF(stop);
3303 Py_XDECREF(step);
3304 SET_TOP(slice);
3305 if (slice == NULL)
3306 goto error;
3307 DISPATCH();
3308 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003309
Eric V. Smitha78c7952015-11-03 12:45:05 -05003310 TARGET(FORMAT_VALUE) {
3311 /* Handles f-string value formatting. */
3312 PyObject *result;
3313 PyObject *fmt_spec;
3314 PyObject *value;
3315 PyObject *(*conv_fn)(PyObject *);
3316 int which_conversion = oparg & FVC_MASK;
3317 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3318
3319 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003320 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003321
3322 /* See if any conversion is specified. */
3323 switch (which_conversion) {
3324 case FVC_STR: conv_fn = PyObject_Str; break;
3325 case FVC_REPR: conv_fn = PyObject_Repr; break;
3326 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3327
3328 /* Must be 0 (meaning no conversion), since only four
3329 values are allowed by (oparg & FVC_MASK). */
3330 default: conv_fn = NULL; break;
3331 }
3332
3333 /* If there's a conversion function, call it and replace
3334 value with that result. Otherwise, just use value,
3335 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003336 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003337 result = conv_fn(value);
3338 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003339 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003340 Py_XDECREF(fmt_spec);
3341 goto error;
3342 }
3343 value = result;
3344 }
3345
3346 /* If value is a unicode object, and there's no fmt_spec,
3347 then we know the result of format(value) is value
3348 itself. In that case, skip calling format(). I plan to
3349 move this optimization in to PyObject_Format()
3350 itself. */
3351 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3352 /* Do nothing, just transfer ownership to result. */
3353 result = value;
3354 } else {
3355 /* Actually call format(). */
3356 result = PyObject_Format(value, fmt_spec);
3357 Py_DECREF(value);
3358 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003359 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003360 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003361 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003362 }
3363
Eric V. Smith135d5f42016-02-05 18:23:08 -05003364 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003365 DISPATCH();
3366 }
3367
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003368 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003369 int oldoparg = oparg;
3370 NEXTOPARG();
3371 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003372 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003373 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003374
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003375
Antoine Pitrou042b1282010-08-13 21:15:58 +00003376#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003377 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003378#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003379 default:
3380 fprintf(stderr,
3381 "XXX lineno: %d, opcode: %d\n",
3382 PyFrame_GetLineNumber(f),
3383 opcode);
3384 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003385 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003386
3387#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003389#endif
3390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003391 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003392
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003393 /* This should never be reached. Every opcode should end with DISPATCH()
3394 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003395 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003396
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003397error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003398
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003399 assert(why == WHY_NOT);
3400 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003401
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003403#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 if (!PyErr_Occurred())
3405 PyErr_SetString(PyExc_SystemError,
3406 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003407#else
3408 assert(PyErr_Occurred());
3409#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003410
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 /* Log traceback info. */
3412 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003413
Benjamin Peterson51f46162013-01-23 08:38:47 -05003414 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003415 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3416 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003417
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003418fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003419 assert(why != WHY_NOT);
3420
3421 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 while (why != WHY_NOT && f->f_iblock > 0) {
3423 /* Peek at the current block. */
3424 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 assert(why != WHY_YIELD);
3427 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3428 why = WHY_NOT;
3429 JUMPTO(PyLong_AS_LONG(retval));
3430 Py_DECREF(retval);
3431 break;
3432 }
3433 /* Now we have to pop the block. */
3434 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003435
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 if (b->b_type == EXCEPT_HANDLER) {
3437 UNWIND_EXCEPT_HANDLER(b);
3438 continue;
3439 }
3440 UNWIND_BLOCK(b);
3441 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3442 why = WHY_NOT;
3443 JUMPTO(b->b_handler);
3444 break;
3445 }
3446 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3447 || b->b_type == SETUP_FINALLY)) {
3448 PyObject *exc, *val, *tb;
3449 int handler = b->b_handler;
3450 /* Beware, this invalidates all b->b_* fields */
3451 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3452 PUSH(tstate->exc_traceback);
3453 PUSH(tstate->exc_value);
3454 if (tstate->exc_type != NULL) {
3455 PUSH(tstate->exc_type);
3456 }
3457 else {
3458 Py_INCREF(Py_None);
3459 PUSH(Py_None);
3460 }
3461 PyErr_Fetch(&exc, &val, &tb);
3462 /* Make the raw exception data
3463 available to the handler,
3464 so a program can emulate the
3465 Python main loop. */
3466 PyErr_NormalizeException(
3467 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003468 if (tb != NULL)
3469 PyException_SetTraceback(val, tb);
3470 else
3471 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 Py_INCREF(exc);
3473 tstate->exc_type = exc;
3474 Py_INCREF(val);
3475 tstate->exc_value = val;
3476 tstate->exc_traceback = tb;
3477 if (tb == NULL)
3478 tb = Py_None;
3479 Py_INCREF(tb);
3480 PUSH(tb);
3481 PUSH(val);
3482 PUSH(exc);
3483 why = WHY_NOT;
3484 JUMPTO(handler);
3485 break;
3486 }
3487 if (b->b_type == SETUP_FINALLY) {
3488 if (why & (WHY_RETURN | WHY_CONTINUE))
3489 PUSH(retval);
3490 PUSH(PyLong_FromLong((long)why));
3491 why = WHY_NOT;
3492 JUMPTO(b->b_handler);
3493 break;
3494 }
3495 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003497 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003499 if (why != WHY_NOT)
3500 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003501
Victor Stinnerace47d72013-07-18 01:41:08 +02003502 assert(!PyErr_Occurred());
3503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003504 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003505
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 assert(why != WHY_YIELD);
3507 /* Pop remaining stack entries. */
3508 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003509 PyObject *o = POP();
3510 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 if (why != WHY_RETURN)
3514 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003515
Victor Stinner4a7cc882015-03-06 23:35:27 +01003516 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003517
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003518fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003519 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003520
Benjamin Petersonac913412011-07-03 16:25:11 -05003521 /* The purpose of this block is to put aside the generator's exception
3522 state and restore that of the calling frame. If the current
3523 exception state is from the caller, we clear the exception values
3524 on the generator frame, so they are not swapped back in latter. The
3525 origin of the current exception state is determined by checking for
3526 except handler blocks, which we must be in iff a new exception
3527 state came into existence in this frame. (An uncaught exception
3528 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3529 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003530 for (i = 0; i < f->f_iblock; i++) {
3531 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003532 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003533 }
3534 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003535 if (i == f->f_iblock)
3536 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003537 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003538 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003539 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003540 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003543 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003545 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3546 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003547 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003548 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 why = WHY_EXCEPTION;
3550 }
3551 }
3552 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003553 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3554 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003555 PyTrace_RETURN, NULL);
3556 }
3557 }
3558 if (tstate->c_profilefunc) {
3559 if (why == WHY_EXCEPTION)
3560 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003561 tstate->c_profileobj,
3562 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003563 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003564 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3565 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003567 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003568 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003569 }
3570 }
3571 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003574exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003575 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3576 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003577 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003578 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003579 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003580
Victor Stinnerefde1462015-03-21 15:04:43 +01003581 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003582}
3583
Benjamin Petersonb204a422011-06-05 22:04:07 -05003584static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003585format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3586{
3587 int err;
3588 Py_ssize_t len = PyList_GET_SIZE(names);
3589 PyObject *name_str, *comma, *tail, *tmp;
3590
3591 assert(PyList_CheckExact(names));
3592 assert(len >= 1);
3593 /* Deal with the joys of natural language. */
3594 switch (len) {
3595 case 1:
3596 name_str = PyList_GET_ITEM(names, 0);
3597 Py_INCREF(name_str);
3598 break;
3599 case 2:
3600 name_str = PyUnicode_FromFormat("%U and %U",
3601 PyList_GET_ITEM(names, len - 2),
3602 PyList_GET_ITEM(names, len - 1));
3603 break;
3604 default:
3605 tail = PyUnicode_FromFormat(", %U, and %U",
3606 PyList_GET_ITEM(names, len - 2),
3607 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003608 if (tail == NULL)
3609 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003610 /* Chop off the last two objects in the list. This shouldn't actually
3611 fail, but we can't be too careful. */
3612 err = PyList_SetSlice(names, len - 2, len, NULL);
3613 if (err == -1) {
3614 Py_DECREF(tail);
3615 return;
3616 }
3617 /* Stitch everything up into a nice comma-separated list. */
3618 comma = PyUnicode_FromString(", ");
3619 if (comma == NULL) {
3620 Py_DECREF(tail);
3621 return;
3622 }
3623 tmp = PyUnicode_Join(comma, names);
3624 Py_DECREF(comma);
3625 if (tmp == NULL) {
3626 Py_DECREF(tail);
3627 return;
3628 }
3629 name_str = PyUnicode_Concat(tmp, tail);
3630 Py_DECREF(tmp);
3631 Py_DECREF(tail);
3632 break;
3633 }
3634 if (name_str == NULL)
3635 return;
3636 PyErr_Format(PyExc_TypeError,
3637 "%U() missing %i required %s argument%s: %U",
3638 co->co_name,
3639 len,
3640 kind,
3641 len == 1 ? "" : "s",
3642 name_str);
3643 Py_DECREF(name_str);
3644}
3645
3646static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003647missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003648 PyObject **fastlocals)
3649{
Victor Stinner74319ae2016-08-25 00:04:09 +02003650 Py_ssize_t i, j = 0;
3651 Py_ssize_t start, end;
3652 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003653 const char *kind = positional ? "positional" : "keyword-only";
3654 PyObject *missing_names;
3655
3656 /* Compute the names of the arguments that are missing. */
3657 missing_names = PyList_New(missing);
3658 if (missing_names == NULL)
3659 return;
3660 if (positional) {
3661 start = 0;
3662 end = co->co_argcount - defcount;
3663 }
3664 else {
3665 start = co->co_argcount;
3666 end = start + co->co_kwonlyargcount;
3667 }
3668 for (i = start; i < end; i++) {
3669 if (GETLOCAL(i) == NULL) {
3670 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3671 PyObject *name = PyObject_Repr(raw);
3672 if (name == NULL) {
3673 Py_DECREF(missing_names);
3674 return;
3675 }
3676 PyList_SET_ITEM(missing_names, j++, name);
3677 }
3678 }
3679 assert(j == missing);
3680 format_missing(kind, co, missing_names);
3681 Py_DECREF(missing_names);
3682}
3683
3684static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003685too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3686 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003687{
3688 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003689 Py_ssize_t kwonly_given = 0;
3690 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003691 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003692 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003693
Benjamin Petersone109c702011-06-24 09:37:26 -05003694 assert((co->co_flags & CO_VARARGS) == 0);
3695 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003696 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3697 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003698 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003699 }
3700 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003701 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003702 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003703 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003704 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003705 }
3706 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003707 plural = (co_argcount != 1);
3708 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003709 }
3710 if (sig == NULL)
3711 return;
3712 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003713 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3714 kwonly_sig = PyUnicode_FromFormat(format,
3715 given != 1 ? "s" : "",
3716 kwonly_given,
3717 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003718 if (kwonly_sig == NULL) {
3719 Py_DECREF(sig);
3720 return;
3721 }
3722 }
3723 else {
3724 /* This will not fail. */
3725 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003726 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003727 }
3728 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003729 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003730 co->co_name,
3731 sig,
3732 plural ? "s" : "",
3733 given,
3734 kwonly_sig,
3735 given == 1 && !kwonly_given ? "was" : "were");
3736 Py_DECREF(sig);
3737 Py_DECREF(kwonly_sig);
3738}
3739
Guido van Rossumc2e20742006-02-27 22:32:47 +00003740/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003741 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003742 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003743
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003744PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003745_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003746 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003747 PyObject **kwnames, PyObject **kwargs,
3748 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003749 PyObject **defs, Py_ssize_t defcount,
3750 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003751 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003752{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003753 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003754 PyFrameObject *f;
3755 PyObject *retval = NULL;
3756 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003757 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003759 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3760 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003761 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003763 if (globals == NULL) {
3764 PyErr_SetString(PyExc_SystemError,
3765 "PyEval_EvalCodeEx: NULL globals");
3766 return NULL;
3767 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003768
Victor Stinnerc7020012016-08-16 23:40:29 +02003769 /* Create the frame */
3770 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003772 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003773 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003774 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003775 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 fastlocals = f->f_localsplus;
3777 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003778
Victor Stinnerc7020012016-08-16 23:40:29 +02003779 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003780 if (co->co_flags & CO_VARKEYWORDS) {
3781 kwdict = PyDict_New();
3782 if (kwdict == NULL)
3783 goto fail;
3784 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003785 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003786 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003787 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 SETLOCAL(i, kwdict);
3789 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003790 else {
3791 kwdict = NULL;
3792 }
3793
3794 /* Copy positional arguments into local variables */
3795 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003796 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003797 }
3798 else {
3799 n = argcount;
3800 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003801 for (i = 0; i < n; i++) {
3802 x = args[i];
3803 Py_INCREF(x);
3804 SETLOCAL(i, x);
3805 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003806
3807 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003808 if (co->co_flags & CO_VARARGS) {
3809 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003810 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003811 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003812 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813 SETLOCAL(total_args, u);
3814 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003815 x = args[i];
3816 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003817 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003818 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003819 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003820
Serhiy Storchakab7281052016-09-12 00:52:40 +03003821 /* Handle keyword arguments passed as two strided arrays */
3822 kwcount *= kwstep;
3823 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003824 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003825 PyObject *keyword = kwnames[i];
3826 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003827 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003828
Benjamin Petersonb204a422011-06-05 22:04:07 -05003829 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3830 PyErr_Format(PyExc_TypeError,
3831 "%U() keywords must be strings",
3832 co->co_name);
3833 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003834 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003835
Benjamin Petersonb204a422011-06-05 22:04:07 -05003836 /* Speed hack: do raw pointer compares. As names are
3837 normally interned this should almost always hit. */
3838 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3839 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003840 PyObject *name = co_varnames[j];
3841 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003842 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003843 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003844 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003845
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 /* Slow fallback, just in case */
3847 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003848 PyObject *name = co_varnames[j];
3849 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3850 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003852 }
3853 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003854 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003855 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003856 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003857
Victor Stinner231d1f32017-01-11 02:12:06 +01003858 assert(j >= total_args);
3859 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003860 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003861 "%U() got an unexpected keyword argument '%S'",
3862 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003863 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003864 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003865
Christian Heimes0bd447f2013-07-20 14:48:10 +02003866 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3867 goto fail;
3868 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003869 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003870
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 kw_found:
3872 if (GETLOCAL(j) != NULL) {
3873 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003874 "%U() got multiple values for argument '%S'",
3875 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003876 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003877 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003878 Py_INCREF(value);
3879 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003880 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003881
3882 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003883 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003884 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003885 goto fail;
3886 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003887
3888 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003890 Py_ssize_t m = co->co_argcount - defcount;
3891 Py_ssize_t missing = 0;
3892 for (i = argcount; i < m; i++) {
3893 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003894 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003895 }
3896 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003897 if (missing) {
3898 missing_arguments(co, missing, defcount, fastlocals);
3899 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003900 }
3901 if (n > m)
3902 i = n - m;
3903 else
3904 i = 0;
3905 for (; i < defcount; i++) {
3906 if (GETLOCAL(m+i) == NULL) {
3907 PyObject *def = defs[i];
3908 Py_INCREF(def);
3909 SETLOCAL(m+i, def);
3910 }
3911 }
3912 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003913
3914 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003915 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003916 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003917 for (i = co->co_argcount; i < total_args; i++) {
3918 PyObject *name;
3919 if (GETLOCAL(i) != NULL)
3920 continue;
3921 name = PyTuple_GET_ITEM(co->co_varnames, i);
3922 if (kwdefs != NULL) {
3923 PyObject *def = PyDict_GetItem(kwdefs, name);
3924 if (def) {
3925 Py_INCREF(def);
3926 SETLOCAL(i, def);
3927 continue;
3928 }
3929 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003930 missing++;
3931 }
3932 if (missing) {
3933 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003934 goto fail;
3935 }
3936 }
3937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003938 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003939 vars into frame. */
3940 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003941 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003942 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003943 /* Possibly account for the cell variable being an argument. */
3944 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003945 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003946 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003947 /* Clear the local copy. */
3948 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003949 }
3950 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003951 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003952 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003953 if (c == NULL)
3954 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003955 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003956 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003957
3958 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003959 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3960 PyObject *o = PyTuple_GET_ITEM(closure, i);
3961 Py_INCREF(o);
3962 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003963 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003964
Yury Selivanoveb636452016-09-08 22:01:51 -07003965 /* Handle generator/coroutine/asynchronous generator */
3966 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003967 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003968 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003969 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003970
3971 if (is_coro && tstate->in_coroutine_wrapper) {
3972 assert(coro_wrapper != NULL);
3973 PyErr_Format(PyExc_RuntimeError,
3974 "coroutine wrapper %.200R attempted "
3975 "to recursively wrap %.200R",
3976 coro_wrapper,
3977 co);
3978 goto fail;
3979 }
Yury Selivanov75445082015-05-11 22:57:16 -04003980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003981 /* Don't need to keep the reference to f_back, it will be set
3982 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003983 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003985 /* Create a new generator that owns the ready to run frame
3986 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003987 if (is_coro) {
3988 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003989 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3990 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003991 } else {
3992 gen = PyGen_NewWithQualName(f, name, qualname);
3993 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003994 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003995 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003996 }
INADA Naoki9c157762016-12-26 18:52:46 +09003997
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003998 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003999
Yury Selivanov94c22632015-06-04 10:16:51 -04004000 if (is_coro && coro_wrapper != NULL) {
4001 PyObject *wrapped;
4002 tstate->in_coroutine_wrapper = 1;
4003 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4004 tstate->in_coroutine_wrapper = 0;
4005 return wrapped;
4006 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004007
Yury Selivanov75445082015-05-11 22:57:16 -04004008 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004009 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004010
Victor Stinner59a73272016-12-09 18:51:13 +01004011 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004012
Thomas Woutersce272b62007-09-19 21:19:28 +00004013fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 /* decref'ing the frame can cause __del__ methods to get invoked,
4016 which can call back into Python. While we're done with the
4017 current Python frame (f), the associated C stack is still in use,
4018 so recursion_depth must be boosted for the duration.
4019 */
4020 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004021 if (Py_REFCNT(f) > 1) {
4022 Py_DECREF(f);
4023 _PyObject_GC_TRACK(f);
4024 }
4025 else {
4026 ++tstate->recursion_depth;
4027 Py_DECREF(f);
4028 --tstate->recursion_depth;
4029 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004030 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004031}
4032
Victor Stinner40ee3012014-06-16 15:59:28 +02004033PyObject *
4034PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4035 PyObject **args, int argcount, PyObject **kws, int kwcount,
4036 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4037{
4038 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004039 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004040 kws, kws != NULL ? kws + 1 : NULL,
4041 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004042 defs, defcount,
4043 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004044 NULL, NULL);
4045}
Tim Peters5ca576e2001-06-18 22:08:13 +00004046
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004047static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004048special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004050 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004051 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004052 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004053 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004054 return NULL;
4055 }
4056 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004057}
4058
4059
Benjamin Peterson87880242011-07-03 16:48:31 -05004060/* These 3 functions deal with the exception state of generators. */
4061
4062static void
4063save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4064{
4065 PyObject *type, *value, *traceback;
4066 Py_XINCREF(tstate->exc_type);
4067 Py_XINCREF(tstate->exc_value);
4068 Py_XINCREF(tstate->exc_traceback);
4069 type = f->f_exc_type;
4070 value = f->f_exc_value;
4071 traceback = f->f_exc_traceback;
4072 f->f_exc_type = tstate->exc_type;
4073 f->f_exc_value = tstate->exc_value;
4074 f->f_exc_traceback = tstate->exc_traceback;
4075 Py_XDECREF(type);
4076 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004077 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004078}
4079
4080static void
4081swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4082{
4083 PyObject *tmp;
4084 tmp = tstate->exc_type;
4085 tstate->exc_type = f->f_exc_type;
4086 f->f_exc_type = tmp;
4087 tmp = tstate->exc_value;
4088 tstate->exc_value = f->f_exc_value;
4089 f->f_exc_value = tmp;
4090 tmp = tstate->exc_traceback;
4091 tstate->exc_traceback = f->f_exc_traceback;
4092 f->f_exc_traceback = tmp;
4093}
4094
4095static void
4096restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4097{
4098 PyObject *type, *value, *tb;
4099 type = tstate->exc_type;
4100 value = tstate->exc_value;
4101 tb = tstate->exc_traceback;
4102 tstate->exc_type = f->f_exc_type;
4103 tstate->exc_value = f->f_exc_value;
4104 tstate->exc_traceback = f->f_exc_traceback;
4105 f->f_exc_type = NULL;
4106 f->f_exc_value = NULL;
4107 f->f_exc_traceback = NULL;
4108 Py_XDECREF(type);
4109 Py_XDECREF(value);
4110 Py_XDECREF(tb);
4111}
4112
4113
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004114/* Logic for the raise statement (too complicated for inlining).
4115 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004116static int
Collin Winter828f04a2007-08-31 00:04:24 +00004117do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004118{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004119 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004120
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004121 if (exc == NULL) {
4122 /* Reraise */
4123 PyThreadState *tstate = PyThreadState_GET();
4124 PyObject *tb;
4125 type = tstate->exc_type;
4126 value = tstate->exc_value;
4127 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004128 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004129 PyErr_SetString(PyExc_RuntimeError,
4130 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004131 return 0;
4132 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004133 Py_XINCREF(type);
4134 Py_XINCREF(value);
4135 Py_XINCREF(tb);
4136 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004137 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004138 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004139
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004140 /* We support the following forms of raise:
4141 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004142 raise <instance>
4143 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004144
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004145 if (PyExceptionClass_Check(exc)) {
4146 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004147 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 if (value == NULL)
4149 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004150 if (!PyExceptionInstance_Check(value)) {
4151 PyErr_Format(PyExc_TypeError,
4152 "calling %R should have returned an instance of "
4153 "BaseException, not %R",
4154 type, Py_TYPE(value));
4155 goto raise_error;
4156 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 }
4158 else if (PyExceptionInstance_Check(exc)) {
4159 value = exc;
4160 type = PyExceptionInstance_Class(exc);
4161 Py_INCREF(type);
4162 }
4163 else {
4164 /* Not something you can raise. You get an exception
4165 anyway, just not what you specified :-) */
4166 Py_DECREF(exc);
4167 PyErr_SetString(PyExc_TypeError,
4168 "exceptions must derive from BaseException");
4169 goto raise_error;
4170 }
Collin Winter828f04a2007-08-31 00:04:24 +00004171
Serhiy Storchakac0191582016-09-27 11:37:10 +03004172 assert(type != NULL);
4173 assert(value != NULL);
4174
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004175 if (cause) {
4176 PyObject *fixed_cause;
4177 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004178 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004179 if (fixed_cause == NULL)
4180 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004181 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004182 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004183 else if (PyExceptionInstance_Check(cause)) {
4184 fixed_cause = cause;
4185 }
4186 else if (cause == Py_None) {
4187 Py_DECREF(cause);
4188 fixed_cause = NULL;
4189 }
4190 else {
4191 PyErr_SetString(PyExc_TypeError,
4192 "exception causes must derive from "
4193 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004194 goto raise_error;
4195 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004196 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004197 }
Collin Winter828f04a2007-08-31 00:04:24 +00004198
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004199 PyErr_SetObject(type, value);
4200 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004201 Py_DECREF(value);
4202 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004203 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004204
4205raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004206 Py_XDECREF(value);
4207 Py_XDECREF(type);
4208 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004209 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004210}
4211
Tim Petersd6d010b2001-06-21 02:49:55 +00004212/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004213 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004214
Guido van Rossum0368b722007-05-11 16:50:42 +00004215 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4216 with a variable target.
4217*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004218
Barry Warsawe42b18f1997-08-25 22:13:04 +00004219static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004220unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004221{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 int i = 0, j = 0;
4223 Py_ssize_t ll = 0;
4224 PyObject *it; /* iter(v) */
4225 PyObject *w;
4226 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004230 it = PyObject_GetIter(v);
4231 if (it == NULL)
4232 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004234 for (; i < argcnt; i++) {
4235 w = PyIter_Next(it);
4236 if (w == NULL) {
4237 /* Iterator done, via error or exhaustion. */
4238 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004239 if (argcntafter == -1) {
4240 PyErr_Format(PyExc_ValueError,
4241 "not enough values to unpack (expected %d, got %d)",
4242 argcnt, i);
4243 }
4244 else {
4245 PyErr_Format(PyExc_ValueError,
4246 "not enough values to unpack "
4247 "(expected at least %d, got %d)",
4248 argcnt + argcntafter, i);
4249 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004250 }
4251 goto Error;
4252 }
4253 *--sp = w;
4254 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004256 if (argcntafter == -1) {
4257 /* We better have exhausted the iterator now. */
4258 w = PyIter_Next(it);
4259 if (w == NULL) {
4260 if (PyErr_Occurred())
4261 goto Error;
4262 Py_DECREF(it);
4263 return 1;
4264 }
4265 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004266 PyErr_Format(PyExc_ValueError,
4267 "too many values to unpack (expected %d)",
4268 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004269 goto Error;
4270 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 l = PySequence_List(it);
4273 if (l == NULL)
4274 goto Error;
4275 *--sp = l;
4276 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004278 ll = PyList_GET_SIZE(l);
4279 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004280 PyErr_Format(PyExc_ValueError,
4281 "not enough values to unpack (expected at least %d, got %zd)",
4282 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004283 goto Error;
4284 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004286 /* Pop the "after-variable" args off the list. */
4287 for (j = argcntafter; j > 0; j--, i++) {
4288 *--sp = PyList_GET_ITEM(l, ll - j);
4289 }
4290 /* Resize the list. */
4291 Py_SIZE(l) = ll - argcntafter;
4292 Py_DECREF(it);
4293 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004294
Tim Petersd6d010b2001-06-21 02:49:55 +00004295Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004296 for (; i > 0; i--, sp++)
4297 Py_DECREF(*sp);
4298 Py_XDECREF(it);
4299 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004300}
4301
4302
Guido van Rossum96a42c81992-01-12 02:29:51 +00004303#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004304static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004305prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004306{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 printf("%s ", str);
4308 if (PyObject_Print(v, stdout, 0) != 0)
4309 PyErr_Clear(); /* Don't know what else to do */
4310 printf("\n");
4311 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004312}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004313#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004314
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004315static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004316call_exc_trace(Py_tracefunc func, PyObject *self,
4317 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004318{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004319 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004320 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004321 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 if (value == NULL) {
4323 value = Py_None;
4324 Py_INCREF(value);
4325 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004326 PyErr_NormalizeException(&type, &value, &orig_traceback);
4327 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004328 arg = PyTuple_Pack(3, type, value, traceback);
4329 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004330 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004331 return;
4332 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004333 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 Py_DECREF(arg);
4335 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004336 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004337 else {
4338 Py_XDECREF(type);
4339 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004340 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004341 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004342}
4343
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004344static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004345call_trace_protected(Py_tracefunc func, PyObject *obj,
4346 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004347 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004349 PyObject *type, *value, *traceback;
4350 int err;
4351 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004352 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 if (err == 0)
4354 {
4355 PyErr_Restore(type, value, traceback);
4356 return 0;
4357 }
4358 else {
4359 Py_XDECREF(type);
4360 Py_XDECREF(value);
4361 Py_XDECREF(traceback);
4362 return -1;
4363 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004364}
4365
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004366static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004367call_trace(Py_tracefunc func, PyObject *obj,
4368 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004371 int result;
4372 if (tstate->tracing)
4373 return 0;
4374 tstate->tracing++;
4375 tstate->use_tracing = 0;
4376 result = func(obj, frame, what, arg);
4377 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4378 || (tstate->c_profilefunc != NULL));
4379 tstate->tracing--;
4380 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004381}
4382
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004383PyObject *
4384_PyEval_CallTracing(PyObject *func, PyObject *args)
4385{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004386 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 int save_tracing = tstate->tracing;
4388 int save_use_tracing = tstate->use_tracing;
4389 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004390
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 tstate->tracing = 0;
4392 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4393 || (tstate->c_profilefunc != NULL));
4394 result = PyObject_Call(func, args, NULL);
4395 tstate->tracing = save_tracing;
4396 tstate->use_tracing = save_use_tracing;
4397 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004398}
4399
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004400/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004401static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004402maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004403 PyThreadState *tstate, PyFrameObject *frame,
4404 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004405{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004406 int result = 0;
4407 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 /* If the last instruction executed isn't in the current
4410 instruction window, reset the window.
4411 */
4412 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4413 PyAddrPair bounds;
4414 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4415 &bounds);
4416 *instr_lb = bounds.ap_lower;
4417 *instr_ub = bounds.ap_upper;
4418 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004419 /* Always emit an opcode event if we're tracing all opcodes. */
4420 if (frame->f_trace_opcodes) {
4421 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4422 }
4423 /* If the last instruction falls at the start of a line or if it
4424 represents a jump backwards, update the frame's line number and
4425 then call the trace function if we're tracing source lines.
4426 */
4427 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004428 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004429 if (frame->f_trace_lines) {
4430 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4431 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004432 }
4433 *instr_prev = frame->f_lasti;
4434 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004435}
4436
Fred Drake5755ce62001-06-27 19:19:46 +00004437void
4438PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004439{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004440 PyThreadState *tstate = PyThreadState_GET();
4441 PyObject *temp = tstate->c_profileobj;
4442 Py_XINCREF(arg);
4443 tstate->c_profilefunc = NULL;
4444 tstate->c_profileobj = NULL;
4445 /* Must make sure that tracing is not ignored if 'temp' is freed */
4446 tstate->use_tracing = tstate->c_tracefunc != NULL;
4447 Py_XDECREF(temp);
4448 tstate->c_profilefunc = func;
4449 tstate->c_profileobj = arg;
4450 /* Flag that tracing or profiling is turned on */
4451 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004452}
4453
4454void
4455PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyThreadState *tstate = PyThreadState_GET();
4458 PyObject *temp = tstate->c_traceobj;
4459 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4460 Py_XINCREF(arg);
4461 tstate->c_tracefunc = NULL;
4462 tstate->c_traceobj = NULL;
4463 /* Must make sure that profiling is not ignored if 'temp' is freed */
4464 tstate->use_tracing = tstate->c_profilefunc != NULL;
4465 Py_XDECREF(temp);
4466 tstate->c_tracefunc = func;
4467 tstate->c_traceobj = arg;
4468 /* Flag that tracing or profiling is turned on */
4469 tstate->use_tracing = ((func != NULL)
4470 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004471}
4472
Yury Selivanov75445082015-05-11 22:57:16 -04004473void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004474_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004475{
4476 PyThreadState *tstate = PyThreadState_GET();
4477
Yury Selivanov75445082015-05-11 22:57:16 -04004478 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004479 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004480}
4481
4482PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004483_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004484{
4485 PyThreadState *tstate = PyThreadState_GET();
4486 return tstate->coroutine_wrapper;
4487}
4488
Yury Selivanoveb636452016-09-08 22:01:51 -07004489void
4490_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4491{
4492 PyThreadState *tstate = PyThreadState_GET();
4493
4494 Py_XINCREF(firstiter);
4495 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4496}
4497
4498PyObject *
4499_PyEval_GetAsyncGenFirstiter(void)
4500{
4501 PyThreadState *tstate = PyThreadState_GET();
4502 return tstate->async_gen_firstiter;
4503}
4504
4505void
4506_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4507{
4508 PyThreadState *tstate = PyThreadState_GET();
4509
4510 Py_XINCREF(finalizer);
4511 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4512}
4513
4514PyObject *
4515_PyEval_GetAsyncGenFinalizer(void)
4516{
4517 PyThreadState *tstate = PyThreadState_GET();
4518 return tstate->async_gen_finalizer;
4519}
4520
Guido van Rossumb209a111997-04-29 18:18:01 +00004521PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004522PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004523{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004524 PyFrameObject *current_frame = PyEval_GetFrame();
4525 if (current_frame == NULL)
4526 return PyThreadState_GET()->interp->builtins;
4527 else
4528 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004529}
4530
Guido van Rossumb209a111997-04-29 18:18:01 +00004531PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004532PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004533{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004535 if (current_frame == NULL) {
4536 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004537 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004538 }
4539
4540 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4541 return NULL;
4542
4543 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004544 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004545}
4546
Guido van Rossumb209a111997-04-29 18:18:01 +00004547PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004548PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004549{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004550 PyFrameObject *current_frame = PyEval_GetFrame();
4551 if (current_frame == NULL)
4552 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004553
4554 assert(current_frame->f_globals != NULL);
4555 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004556}
4557
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004558PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004559PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004560{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 PyThreadState *tstate = PyThreadState_GET();
4562 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004563}
4564
Guido van Rossum6135a871995-01-09 17:53:26 +00004565int
Tim Peters5ba58662001-07-16 02:29:45 +00004566PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004568 PyFrameObject *current_frame = PyEval_GetFrame();
4569 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004571 if (current_frame != NULL) {
4572 const int codeflags = current_frame->f_code->co_flags;
4573 const int compilerflags = codeflags & PyCF_MASK;
4574 if (compilerflags) {
4575 result = 1;
4576 cf->cf_flags |= compilerflags;
4577 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004578#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004579 if (codeflags & CO_GENERATOR_ALLOWED) {
4580 result = 1;
4581 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4582 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004583#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 }
4585 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004586}
4587
Guido van Rossum3f5da241990-12-20 15:06:42 +00004588
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004589const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004590PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004591{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004592 if (PyMethod_Check(func))
4593 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4594 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004595 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004596 else if (PyCFunction_Check(func))
4597 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4598 else
4599 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004600}
4601
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004602const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004603PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 if (PyMethod_Check(func))
4606 return "()";
4607 else if (PyFunction_Check(func))
4608 return "()";
4609 else if (PyCFunction_Check(func))
4610 return "()";
4611 else
4612 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004613}
4614
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004615#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004616if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004617 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4618 tstate, tstate->frame, \
4619 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 x = NULL; \
4621 } \
4622 else { \
4623 x = call; \
4624 if (tstate->c_profilefunc != NULL) { \
4625 if (x == NULL) { \
4626 call_trace_protected(tstate->c_profilefunc, \
4627 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004628 tstate, tstate->frame, \
4629 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 /* XXX should pass (type, value, tb) */ \
4631 } else { \
4632 if (call_trace(tstate->c_profilefunc, \
4633 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004634 tstate, tstate->frame, \
4635 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004636 Py_DECREF(x); \
4637 x = NULL; \
4638 } \
4639 } \
4640 } \
4641 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004642} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 x = call; \
4644 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004645
Victor Stinner415c5102017-01-11 00:54:57 +01004646/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4647 to reduce the stack consumption. */
4648Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004649call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004650{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004651 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004652 PyObject *func = *pfunc;
4653 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004654 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4655 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004656 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 /* Always dispatch PyCFunction first, because these are
4659 presumed to be the most frequent callable object.
4660 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004661 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004663 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004664 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004665 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4666 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004667 if (tstate->use_tracing && tstate->c_profilefunc) {
4668 // We need to create PyCFunctionObject for tracing.
4669 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4670 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4671 if (func == NULL) {
4672 return NULL;
4673 }
4674 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4675 kwnames));
4676 Py_DECREF(func);
4677 }
4678 else {
4679 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4680 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004681 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004682 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004683 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004684 /* Optimize access to bound methods. Reuse the Python stack
4685 to pass 'self' as the first argument, replace 'func'
4686 with 'self'. It avoids the creation of a new temporary tuple
4687 for arguments (to replace func with self) when the method uses
4688 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004689 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004690 Py_INCREF(self);
4691 func = PyMethod_GET_FUNCTION(func);
4692 Py_INCREF(func);
4693 Py_SETREF(*pfunc, self);
4694 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004695 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004696 }
4697 else {
4698 Py_INCREF(func);
4699 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004700
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004701 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004702 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004703 }
4704 else {
4705 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4706 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004707 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004708 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004709
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004710 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4711
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004712 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004713 while ((*pp_stack) > pfunc) {
4714 w = EXT_POP(*pp_stack);
4715 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004719}
4720
Jeremy Hylton52820442001-01-03 23:52:36 +00004721static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004722do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004723{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004725 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004726 PyThreadState *tstate = PyThreadState_GET();
4727 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004728 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004729 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004730 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004731 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004732 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004733}
4734
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004735/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004736 nb_index slot defined, and store in *pi.
4737 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004738 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004739 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004740*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004741int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004742_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004743{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004744 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 Py_ssize_t x;
4746 if (PyIndex_Check(v)) {
4747 x = PyNumber_AsSsize_t(v, NULL);
4748 if (x == -1 && PyErr_Occurred())
4749 return 0;
4750 }
4751 else {
4752 PyErr_SetString(PyExc_TypeError,
4753 "slice indices must be integers or "
4754 "None or have an __index__ method");
4755 return 0;
4756 }
4757 *pi = x;
4758 }
4759 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004760}
4761
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004762int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004763_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004764{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004765 Py_ssize_t x;
4766 if (PyIndex_Check(v)) {
4767 x = PyNumber_AsSsize_t(v, NULL);
4768 if (x == -1 && PyErr_Occurred())
4769 return 0;
4770 }
4771 else {
4772 PyErr_SetString(PyExc_TypeError,
4773 "slice indices must be integers or "
4774 "have an __index__ method");
4775 return 0;
4776 }
4777 *pi = x;
4778 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004779}
4780
4781
Guido van Rossum486364b2007-06-30 05:01:58 +00004782#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004784
Guido van Rossumb209a111997-04-29 18:18:01 +00004785static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004786cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004787{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004788 int res = 0;
4789 switch (op) {
4790 case PyCmp_IS:
4791 res = (v == w);
4792 break;
4793 case PyCmp_IS_NOT:
4794 res = (v != w);
4795 break;
4796 case PyCmp_IN:
4797 res = PySequence_Contains(w, v);
4798 if (res < 0)
4799 return NULL;
4800 break;
4801 case PyCmp_NOT_IN:
4802 res = PySequence_Contains(w, v);
4803 if (res < 0)
4804 return NULL;
4805 res = !res;
4806 break;
4807 case PyCmp_EXC_MATCH:
4808 if (PyTuple_Check(w)) {
4809 Py_ssize_t i, length;
4810 length = PyTuple_Size(w);
4811 for (i = 0; i < length; i += 1) {
4812 PyObject *exc = PyTuple_GET_ITEM(w, i);
4813 if (!PyExceptionClass_Check(exc)) {
4814 PyErr_SetString(PyExc_TypeError,
4815 CANNOT_CATCH_MSG);
4816 return NULL;
4817 }
4818 }
4819 }
4820 else {
4821 if (!PyExceptionClass_Check(w)) {
4822 PyErr_SetString(PyExc_TypeError,
4823 CANNOT_CATCH_MSG);
4824 return NULL;
4825 }
4826 }
4827 res = PyErr_GivenExceptionMatches(v, w);
4828 break;
4829 default:
4830 return PyObject_RichCompare(v, w, op);
4831 }
4832 v = res ? Py_True : Py_False;
4833 Py_INCREF(v);
4834 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004835}
4836
Thomas Wouters52152252000-08-17 22:55:00 +00004837static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004838import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4839{
4840 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004841 PyObject *import_func, *res;
4842 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004843
4844 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4845 if (import_func == NULL) {
4846 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4847 return NULL;
4848 }
4849
4850 /* Fast path for not overloaded __import__. */
4851 if (import_func == PyThreadState_GET()->interp->import_func) {
4852 int ilevel = _PyLong_AsInt(level);
4853 if (ilevel == -1 && PyErr_Occurred()) {
4854 return NULL;
4855 }
4856 res = PyImport_ImportModuleLevelObject(
4857 name,
4858 f->f_globals,
4859 f->f_locals == NULL ? Py_None : f->f_locals,
4860 fromlist,
4861 ilevel);
4862 return res;
4863 }
4864
4865 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004866
4867 stack[0] = name;
4868 stack[1] = f->f_globals;
4869 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4870 stack[3] = fromlist;
4871 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004872 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004873 Py_DECREF(import_func);
4874 return res;
4875}
4876
4877static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004878import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004880 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004881 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004882 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004884 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004885 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
4886 return x;
4887 /* Issue #17636: in case this failed because of a circular relative
4888 import, try to fallback on reading the module directly from
4889 sys.modules. */
4890 PyErr_Clear();
4891 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004892 if (pkgname == NULL) {
4893 goto error;
4894 }
Oren Milman6db70332017-09-19 14:23:01 +03004895 if (!PyUnicode_Check(pkgname)) {
4896 Py_CLEAR(pkgname);
4897 goto error;
4898 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004899 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004900 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004901 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004902 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004903 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004904 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004905 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004906 if (x == NULL) {
4907 goto error;
4908 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004909 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004911 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004912 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004913 if (pkgname == NULL) {
4914 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4915 if (pkgname_or_unknown == NULL) {
4916 Py_XDECREF(pkgpath);
4917 return NULL;
4918 }
4919 } else {
4920 pkgname_or_unknown = pkgname;
4921 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004922
4923 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4924 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004925 errmsg = PyUnicode_FromFormat(
4926 "cannot import name %R from %R (unknown location)",
4927 name, pkgname_or_unknown
4928 );
4929 /* NULL check for errmsg done by PyErr_SetImportError. */
4930 PyErr_SetImportError(errmsg, pkgname, NULL);
4931 }
4932 else {
4933 errmsg = PyUnicode_FromFormat(
4934 "cannot import name %R from %R (%S)",
4935 name, pkgname_or_unknown, pkgpath
4936 );
4937 /* NULL check for errmsg done by PyErr_SetImportError. */
4938 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004939 }
4940
Xiang Zhang4830f582017-03-21 11:13:42 +08004941 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004942 Py_XDECREF(pkgname_or_unknown);
4943 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004944 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004945}
Guido van Rossumac7be682001-01-17 15:42:30 +00004946
Thomas Wouters52152252000-08-17 22:55:00 +00004947static int
4948import_all_from(PyObject *locals, PyObject *v)
4949{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004950 _Py_IDENTIFIER(__all__);
4951 _Py_IDENTIFIER(__dict__);
4952 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004953 PyObject *dict, *name, *value;
4954 int skip_leading_underscores = 0;
4955 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004956
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004957 if (all == NULL) {
4958 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4959 return -1; /* Unexpected error */
4960 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004961 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004962 if (dict == NULL) {
4963 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4964 return -1;
4965 PyErr_SetString(PyExc_ImportError,
4966 "from-import-* object has no __dict__ and no __all__");
4967 return -1;
4968 }
4969 all = PyMapping_Keys(dict);
4970 Py_DECREF(dict);
4971 if (all == NULL)
4972 return -1;
4973 skip_leading_underscores = 1;
4974 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 for (pos = 0, err = 0; ; pos++) {
4977 name = PySequence_GetItem(all, pos);
4978 if (name == NULL) {
4979 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4980 err = -1;
4981 else
4982 PyErr_Clear();
4983 break;
4984 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004985 if (skip_leading_underscores && PyUnicode_Check(name)) {
4986 if (PyUnicode_READY(name) == -1) {
4987 Py_DECREF(name);
4988 err = -1;
4989 break;
4990 }
4991 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4992 Py_DECREF(name);
4993 continue;
4994 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 }
4996 value = PyObject_GetAttr(v, name);
4997 if (value == NULL)
4998 err = -1;
4999 else if (PyDict_CheckExact(locals))
5000 err = PyDict_SetItem(locals, name, value);
5001 else
5002 err = PyObject_SetItem(locals, name, value);
5003 Py_DECREF(name);
5004 Py_XDECREF(value);
5005 if (err != 0)
5006 break;
5007 }
5008 Py_DECREF(all);
5009 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005010}
5011
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005012static int
5013check_args_iterable(PyObject *func, PyObject *args)
5014{
5015 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5016 PyErr_Format(PyExc_TypeError,
5017 "%.200s%.200s argument after * "
5018 "must be an iterable, not %.200s",
5019 PyEval_GetFuncName(func),
5020 PyEval_GetFuncDesc(func),
5021 args->ob_type->tp_name);
5022 return -1;
5023 }
5024 return 0;
5025}
5026
5027static void
5028format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5029{
5030 PyErr_Format(PyExc_TypeError,
5031 "%.200s%.200s argument after ** "
5032 "must be a mapping, not %.200s",
5033 PyEval_GetFuncName(func),
5034 PyEval_GetFuncDesc(func),
5035 kwargs->ob_type->tp_name);
5036}
5037
Guido van Rossumac7be682001-01-17 15:42:30 +00005038static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005039format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005042
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005043 if (!obj)
5044 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005045
Serhiy Storchaka06515832016-11-20 09:13:07 +02005046 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005047 if (!obj_str)
5048 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005049
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005050 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005051}
Guido van Rossum950361c1997-01-24 13:49:28 +00005052
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005053static void
5054format_exc_unbound(PyCodeObject *co, int oparg)
5055{
5056 PyObject *name;
5057 /* Don't stomp existing exception */
5058 if (PyErr_Occurred())
5059 return;
5060 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5061 name = PyTuple_GET_ITEM(co->co_cellvars,
5062 oparg);
5063 format_exc_check_arg(
5064 PyExc_UnboundLocalError,
5065 UNBOUNDLOCAL_ERROR_MSG,
5066 name);
5067 } else {
5068 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5069 PyTuple_GET_SIZE(co->co_cellvars));
5070 format_exc_check_arg(PyExc_NameError,
5071 UNBOUNDFREE_ERROR_MSG, name);
5072 }
5073}
5074
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005075static PyObject *
5076unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005077 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005078{
5079 PyObject *res;
5080 if (Py_REFCNT(v) == 2) {
5081 /* In the common case, there are 2 references to the value
5082 * stored in 'variable' when the += is performed: one on the
5083 * value stack (in 'v') and one still stored in the
5084 * 'variable'. We try to delete the variable now to reduce
5085 * the refcnt to 1.
5086 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005087 int opcode, oparg;
5088 NEXTOPARG();
5089 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005090 case STORE_FAST:
5091 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005092 PyObject **fastlocals = f->f_localsplus;
5093 if (GETLOCAL(oparg) == v)
5094 SETLOCAL(oparg, NULL);
5095 break;
5096 }
5097 case STORE_DEREF:
5098 {
5099 PyObject **freevars = (f->f_localsplus +
5100 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005101 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005102 if (PyCell_GET(c) == v) {
5103 PyCell_SET(c, NULL);
5104 Py_DECREF(v);
5105 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005106 break;
5107 }
5108 case STORE_NAME:
5109 {
5110 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005111 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005112 PyObject *locals = f->f_locals;
5113 if (PyDict_CheckExact(locals) &&
5114 PyDict_GetItem(locals, name) == v) {
5115 if (PyDict_DelItem(locals, name) != 0) {
5116 PyErr_Clear();
5117 }
5118 }
5119 break;
5120 }
5121 }
5122 }
5123 res = v;
5124 PyUnicode_Append(&res, w);
5125 return res;
5126}
5127
Guido van Rossum950361c1997-01-24 13:49:28 +00005128#ifdef DYNAMIC_EXECUTION_PROFILE
5129
Skip Montanarof118cb12001-10-15 20:51:38 +00005130static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005131getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005132{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005133 int i;
5134 PyObject *l = PyList_New(256);
5135 if (l == NULL) return NULL;
5136 for (i = 0; i < 256; i++) {
5137 PyObject *x = PyLong_FromLong(a[i]);
5138 if (x == NULL) {
5139 Py_DECREF(l);
5140 return NULL;
5141 }
5142 PyList_SetItem(l, i, x);
5143 }
5144 for (i = 0; i < 256; i++)
5145 a[i] = 0;
5146 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005147}
5148
5149PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005150_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005151{
5152#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005153 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005154#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005155 int i;
5156 PyObject *l = PyList_New(257);
5157 if (l == NULL) return NULL;
5158 for (i = 0; i < 257; i++) {
5159 PyObject *x = getarray(dxpairs[i]);
5160 if (x == NULL) {
5161 Py_DECREF(l);
5162 return NULL;
5163 }
5164 PyList_SetItem(l, i, x);
5165 }
5166 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005167#endif
5168}
5169
5170#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005171
5172Py_ssize_t
5173_PyEval_RequestCodeExtraIndex(freefunc free)
5174{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005175 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005176 Py_ssize_t new_index;
5177
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005178 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005179 return -1;
5180 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005181 new_index = interp->co_extra_user_count++;
5182 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005183 return new_index;
5184}
Łukasz Langaa785c872016-09-09 17:37:37 -07005185
5186static void
5187dtrace_function_entry(PyFrameObject *f)
5188{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005189 const char *filename;
5190 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005191 int lineno;
5192
5193 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5194 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5195 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5196
5197 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5198}
5199
5200static void
5201dtrace_function_return(PyFrameObject *f)
5202{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005203 const char *filename;
5204 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005205 int lineno;
5206
5207 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5208 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5209 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5210
5211 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5212}
5213
5214/* DTrace equivalent of maybe_call_line_trace. */
5215static void
5216maybe_dtrace_line(PyFrameObject *frame,
5217 int *instr_lb, int *instr_ub, int *instr_prev)
5218{
5219 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005220 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005221
5222 /* If the last instruction executed isn't in the current
5223 instruction window, reset the window.
5224 */
5225 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5226 PyAddrPair bounds;
5227 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5228 &bounds);
5229 *instr_lb = bounds.ap_lower;
5230 *instr_ub = bounds.ap_upper;
5231 }
5232 /* If the last instruction falls at the start of a line or if
5233 it represents a jump backwards, update the frame's line
5234 number and call the trace function. */
5235 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5236 frame->f_lineno = line;
5237 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5238 if (!co_filename)
5239 co_filename = "?";
5240 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5241 if (!co_name)
5242 co_name = "?";
5243 PyDTrace_LINE(co_filename, co_name, line);
5244 }
5245 *instr_prev = frame->f_lasti;
5246}