blob: 8cc5094a3f46ef66768a8dca9ed7fd76bf5529f8 [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;
1711 PyObject *awaitable = NULL;
1712 PyObject *obj = TOP();
1713 PyTypeObject *type = Py_TYPE(obj);
1714
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001715 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001716 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001717 }
Yury Selivanov75445082015-05-11 22:57:16 -04001718
1719 if (getter != NULL) {
1720 iter = (*getter)(obj);
1721 Py_DECREF(obj);
1722 if (iter == NULL) {
1723 SET_TOP(NULL);
1724 goto error;
1725 }
1726 }
1727 else {
1728 SET_TOP(NULL);
1729 PyErr_Format(
1730 PyExc_TypeError,
1731 "'async for' requires an object with "
1732 "__aiter__ method, got %.100s",
1733 type->tp_name);
1734 Py_DECREF(obj);
1735 goto error;
1736 }
1737
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001738 if (Py_TYPE(iter)->tp_as_async != NULL &&
1739 Py_TYPE(iter)->tp_as_async->am_anext != NULL) {
1740
1741 /* Starting with CPython 3.5.2 __aiter__ should return
1742 asynchronous iterators directly (not awaitables that
1743 resolve to asynchronous iterators.)
1744
1745 Therefore, we check if the object that was returned
1746 from __aiter__ has an __anext__ method. If it does,
1747 we wrap it in an awaitable that resolves to `iter`.
1748
1749 See http://bugs.python.org/issue27243 for more
1750 details.
1751 */
1752
1753 PyObject *wrapper = _PyAIterWrapper_New(iter);
1754 Py_DECREF(iter);
1755 SET_TOP(wrapper);
1756 DISPATCH();
1757 }
1758
Yury Selivanov5376ba92015-06-22 12:19:30 -04001759 awaitable = _PyCoro_GetAwaitableIter(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001760 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001761 _PyErr_FormatFromCause(
Yury Selivanov75445082015-05-11 22:57:16 -04001762 PyExc_TypeError,
1763 "'async for' received an invalid object "
1764 "from __aiter__: %.100s",
1765 Py_TYPE(iter)->tp_name);
1766
Yury Selivanov398ff912017-03-02 22:20:00 -05001767 SET_TOP(NULL);
Yury Selivanov75445082015-05-11 22:57:16 -04001768 Py_DECREF(iter);
1769 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001770 } else {
Yury Selivanov75445082015-05-11 22:57:16 -04001771 Py_DECREF(iter);
1772
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001773 if (PyErr_WarnFormat(
Yury Selivanov2edd8a12016-11-08 15:13:07 -05001774 PyExc_DeprecationWarning, 1,
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001775 "'%.100s' implements legacy __aiter__ protocol; "
1776 "__aiter__ should return an asynchronous "
1777 "iterator, not awaitable",
1778 type->tp_name))
1779 {
1780 /* Warning was converted to an error. */
1781 Py_DECREF(awaitable);
1782 SET_TOP(NULL);
1783 goto error;
1784 }
1785 }
1786
Yury Selivanov75445082015-05-11 22:57:16 -04001787 SET_TOP(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001788 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001789 DISPATCH();
1790 }
1791
1792 TARGET(GET_ANEXT) {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001793 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001794 PyObject *next_iter = NULL;
1795 PyObject *awaitable = NULL;
1796 PyObject *aiter = TOP();
1797 PyTypeObject *type = Py_TYPE(aiter);
1798
Yury Selivanoveb636452016-09-08 22:01:51 -07001799 if (PyAsyncGen_CheckExact(aiter)) {
1800 awaitable = type->tp_as_async->am_anext(aiter);
1801 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001802 goto error;
1803 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001804 } else {
1805 if (type->tp_as_async != NULL){
1806 getter = type->tp_as_async->am_anext;
1807 }
Yury Selivanov75445082015-05-11 22:57:16 -04001808
Yury Selivanoveb636452016-09-08 22:01:51 -07001809 if (getter != NULL) {
1810 next_iter = (*getter)(aiter);
1811 if (next_iter == NULL) {
1812 goto error;
1813 }
1814 }
1815 else {
1816 PyErr_Format(
1817 PyExc_TypeError,
1818 "'async for' requires an iterator with "
1819 "__anext__ method, got %.100s",
1820 type->tp_name);
1821 goto error;
1822 }
Yury Selivanov75445082015-05-11 22:57:16 -04001823
Yury Selivanoveb636452016-09-08 22:01:51 -07001824 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1825 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001826 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001827 PyExc_TypeError,
1828 "'async for' received an invalid object "
1829 "from __anext__: %.100s",
1830 Py_TYPE(next_iter)->tp_name);
1831
1832 Py_DECREF(next_iter);
1833 goto error;
1834 } else {
1835 Py_DECREF(next_iter);
1836 }
1837 }
Yury Selivanov75445082015-05-11 22:57:16 -04001838
1839 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001840 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001841 DISPATCH();
1842 }
1843
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001844 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001845 TARGET(GET_AWAITABLE) {
1846 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001847 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001848
1849 Py_DECREF(iterable);
1850
Yury Selivanovc724bae2016-03-02 11:30:46 -05001851 if (iter != NULL && PyCoro_CheckExact(iter)) {
1852 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1853 if (yf != NULL) {
1854 /* `iter` is a coroutine object that is being
1855 awaited, `yf` is a pointer to the current awaitable
1856 being awaited on. */
1857 Py_DECREF(yf);
1858 Py_CLEAR(iter);
1859 PyErr_SetString(
1860 PyExc_RuntimeError,
1861 "coroutine is being awaited already");
1862 /* The code below jumps to `error` if `iter` is NULL. */
1863 }
1864 }
1865
Yury Selivanov75445082015-05-11 22:57:16 -04001866 SET_TOP(iter); /* Even if it's NULL */
1867
1868 if (iter == NULL) {
1869 goto error;
1870 }
1871
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001872 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001873 DISPATCH();
1874 }
1875
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001876 TARGET(YIELD_FROM) {
1877 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001878 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001880 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1881 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001882 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001883 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001885 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001886 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001887 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001888 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 Py_DECREF(v);
1890 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001891 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001892 if (tstate->c_tracefunc != NULL
1893 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001894 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001895 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 if (err < 0)
1897 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001898 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001899 SET_TOP(val);
1900 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001901 }
Martin Panter95f53c12016-07-18 08:23:26 +00001902 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001903 f->f_stacktop = stack_pointer;
1904 why = WHY_YIELD;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001905 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001906 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001907 f->f_lasti -= sizeof(_Py_CODEUNIT);
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001908 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001909 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001910
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001911 TARGET(YIELD_VALUE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001913
1914 if (co->co_flags & CO_ASYNC_GENERATOR) {
1915 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1916 Py_DECREF(retval);
1917 if (w == NULL) {
1918 retval = NULL;
1919 goto error;
1920 }
1921 retval = w;
1922 }
1923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001924 f->f_stacktop = stack_pointer;
1925 why = WHY_YIELD;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 goto fast_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001927 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001928
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 TARGET(POP_EXCEPT) {
1930 PyTryBlock *b = PyFrame_BlockPop(f);
1931 if (b->b_type != EXCEPT_HANDLER) {
1932 PyErr_SetString(PyExc_SystemError,
1933 "popped block is not an except handler");
1934 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001936 UNWIND_EXCEPT_HANDLER(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001938 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001939
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001940 PREDICTED(POP_BLOCK);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001941 TARGET(POP_BLOCK) {
1942 PyTryBlock *b = PyFrame_BlockPop(f);
1943 UNWIND_BLOCK(b);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001945 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001946
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001947 PREDICTED(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001948 TARGET(END_FINALLY) {
1949 PyObject *status = POP();
1950 if (PyLong_Check(status)) {
1951 why = (enum why_code) PyLong_AS_LONG(status);
1952 assert(why != WHY_YIELD && why != WHY_EXCEPTION);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 if (why == WHY_RETURN ||
1954 why == WHY_CONTINUE)
1955 retval = POP();
1956 if (why == WHY_SILENCED) {
1957 /* An exception was silenced by 'with', we must
1958 manually unwind the EXCEPT_HANDLER block which was
1959 created when the exception was caught, otherwise
1960 the stack will be in an inconsistent state. */
1961 PyTryBlock *b = PyFrame_BlockPop(f);
1962 assert(b->b_type == EXCEPT_HANDLER);
1963 UNWIND_EXCEPT_HANDLER(b);
1964 why = WHY_NOT;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 Py_DECREF(status);
1966 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001967 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 Py_DECREF(status);
1969 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001971 else if (PyExceptionClass_Check(status)) {
1972 PyObject *exc = POP();
1973 PyObject *tb = POP();
1974 PyErr_Restore(status, exc, tb);
1975 why = WHY_EXCEPTION;
1976 goto fast_block_end;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001977 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001978 else if (status != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 PyErr_SetString(PyExc_SystemError,
1980 "'finally' pops bad exception");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 Py_DECREF(status);
1982 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001983 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001984 Py_DECREF(status);
1985 DISPATCH();
1986 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001987
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001988 TARGET(LOAD_BUILD_CLASS) {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001989 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001990
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001992 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001993 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1994 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001995 PyErr_SetString(PyExc_NameError,
1996 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001997 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001998 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001999 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002000 }
2001 else {
2002 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2003 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002004 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002005 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2006 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002007 if (PyErr_ExceptionMatches(PyExc_KeyError))
2008 PyErr_SetString(PyExc_NameError,
2009 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002010 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002011 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002012 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002013 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002014 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002015 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002016
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 TARGET(STORE_NAME) {
2018 PyObject *name = GETITEM(names, oparg);
2019 PyObject *v = POP();
2020 PyObject *ns = f->f_locals;
2021 int err;
2022 if (ns == NULL) {
2023 PyErr_Format(PyExc_SystemError,
2024 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002025 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002026 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002027 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 if (PyDict_CheckExact(ns))
2029 err = PyDict_SetItem(ns, name, v);
2030 else
2031 err = PyObject_SetItem(ns, name, v);
2032 Py_DECREF(v);
2033 if (err != 0)
2034 goto error;
2035 DISPATCH();
2036 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002037
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002038 TARGET(DELETE_NAME) {
2039 PyObject *name = GETITEM(names, oparg);
2040 PyObject *ns = f->f_locals;
2041 int err;
2042 if (ns == NULL) {
2043 PyErr_Format(PyExc_SystemError,
2044 "no locals when deleting %R", name);
2045 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002046 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 err = PyObject_DelItem(ns, name);
2048 if (err != 0) {
2049 format_exc_check_arg(PyExc_NameError,
2050 NAME_ERROR_MSG,
2051 name);
2052 goto error;
2053 }
2054 DISPATCH();
2055 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002056
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002057 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 TARGET(UNPACK_SEQUENCE) {
2059 PyObject *seq = POP(), *item, **items;
2060 if (PyTuple_CheckExact(seq) &&
2061 PyTuple_GET_SIZE(seq) == oparg) {
2062 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002063 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002064 item = items[oparg];
2065 Py_INCREF(item);
2066 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002067 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002068 } else if (PyList_CheckExact(seq) &&
2069 PyList_GET_SIZE(seq) == oparg) {
2070 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002071 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002072 item = items[oparg];
2073 Py_INCREF(item);
2074 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002075 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002076 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 stack_pointer + oparg)) {
2078 STACKADJ(oparg);
2079 } else {
2080 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002081 Py_DECREF(seq);
2082 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002084 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002085 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002086 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002087
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002088 TARGET(UNPACK_EX) {
2089 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2090 PyObject *seq = POP();
2091
2092 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2093 stack_pointer + totalargs)) {
2094 stack_pointer += totalargs;
2095 } else {
2096 Py_DECREF(seq);
2097 goto error;
2098 }
2099 Py_DECREF(seq);
2100 DISPATCH();
2101 }
2102
2103 TARGET(STORE_ATTR) {
2104 PyObject *name = GETITEM(names, oparg);
2105 PyObject *owner = TOP();
2106 PyObject *v = SECOND();
2107 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002108 STACKADJ(-2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002110 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002111 Py_DECREF(owner);
2112 if (err != 0)
2113 goto error;
2114 DISPATCH();
2115 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002116
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 TARGET(DELETE_ATTR) {
2118 PyObject *name = GETITEM(names, oparg);
2119 PyObject *owner = POP();
2120 int err;
2121 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2122 Py_DECREF(owner);
2123 if (err != 0)
2124 goto error;
2125 DISPATCH();
2126 }
2127
2128 TARGET(STORE_GLOBAL) {
2129 PyObject *name = GETITEM(names, oparg);
2130 PyObject *v = POP();
2131 int err;
2132 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002133 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002134 if (err != 0)
2135 goto error;
2136 DISPATCH();
2137 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002138
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 TARGET(DELETE_GLOBAL) {
2140 PyObject *name = GETITEM(names, oparg);
2141 int err;
2142 err = PyDict_DelItem(f->f_globals, name);
2143 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002144 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002145 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002146 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002147 }
2148 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002149 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002150
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 TARGET(LOAD_NAME) {
2152 PyObject *name = GETITEM(names, oparg);
2153 PyObject *locals = f->f_locals;
2154 PyObject *v;
2155 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002156 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 "no locals when loading %R", name);
2158 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002159 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 if (PyDict_CheckExact(locals)) {
2161 v = PyDict_GetItem(locals, name);
2162 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002163 }
2164 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002165 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002166 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002167 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2168 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 PyErr_Clear();
2170 }
2171 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002172 if (v == NULL) {
2173 v = PyDict_GetItem(f->f_globals, name);
2174 Py_XINCREF(v);
2175 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002176 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002177 v = PyDict_GetItem(f->f_builtins, name);
2178 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002179 format_exc_check_arg(
2180 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 NAME_ERROR_MSG, name);
2182 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002183 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002184 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002185 }
2186 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002187 v = PyObject_GetItem(f->f_builtins, name);
2188 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002189 if (PyErr_ExceptionMatches(PyExc_KeyError))
2190 format_exc_check_arg(
2191 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002192 NAME_ERROR_MSG, name);
2193 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002194 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002199 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002200 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002201
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 TARGET(LOAD_GLOBAL) {
2203 PyObject *name = GETITEM(names, oparg);
2204 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002205 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002206 && PyDict_CheckExact(f->f_builtins))
2207 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002208 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002209 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002210 name);
2211 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002212 if (!_PyErr_OCCURRED()) {
2213 /* _PyDict_LoadGlobal() returns NULL without raising
2214 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002215 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002216 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002217 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002218 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002219 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002220 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002221 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002222 else {
2223 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002224
2225 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002226 v = PyObject_GetItem(f->f_globals, name);
2227 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002228 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2229 goto error;
2230 PyErr_Clear();
2231
Victor Stinnerb4efc962015-11-20 09:24:02 +01002232 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002233 v = PyObject_GetItem(f->f_builtins, name);
2234 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002235 if (PyErr_ExceptionMatches(PyExc_KeyError))
2236 format_exc_check_arg(
2237 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002238 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002239 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002240 }
2241 }
2242 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002243 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002244 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002245 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002246
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 TARGET(DELETE_FAST) {
2248 PyObject *v = GETLOCAL(oparg);
2249 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002250 SETLOCAL(oparg, NULL);
2251 DISPATCH();
2252 }
2253 format_exc_check_arg(
2254 PyExc_UnboundLocalError,
2255 UNBOUNDLOCAL_ERROR_MSG,
2256 PyTuple_GetItem(co->co_varnames, oparg)
2257 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002258 goto error;
2259 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002260
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 TARGET(DELETE_DEREF) {
2262 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002263 PyObject *oldobj = PyCell_GET(cell);
2264 if (oldobj != NULL) {
2265 PyCell_SET(cell, NULL);
2266 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002267 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002268 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002269 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002270 goto error;
2271 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002272
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 TARGET(LOAD_CLOSURE) {
2274 PyObject *cell = freevars[oparg];
2275 Py_INCREF(cell);
2276 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002278 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002279
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002280 TARGET(LOAD_CLASSDEREF) {
2281 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002282 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002283 assert(locals);
2284 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2285 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2286 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2287 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2288 if (PyDict_CheckExact(locals)) {
2289 value = PyDict_GetItem(locals, name);
2290 Py_XINCREF(value);
2291 }
2292 else {
2293 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002294 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002295 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2296 goto error;
2297 PyErr_Clear();
2298 }
2299 }
2300 if (!value) {
2301 PyObject *cell = freevars[oparg];
2302 value = PyCell_GET(cell);
2303 if (value == NULL) {
2304 format_exc_unbound(co, oparg);
2305 goto error;
2306 }
2307 Py_INCREF(value);
2308 }
2309 PUSH(value);
2310 DISPATCH();
2311 }
2312
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 TARGET(LOAD_DEREF) {
2314 PyObject *cell = freevars[oparg];
2315 PyObject *value = PyCell_GET(cell);
2316 if (value == NULL) {
2317 format_exc_unbound(co, oparg);
2318 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 Py_INCREF(value);
2321 PUSH(value);
2322 DISPATCH();
2323 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002324
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 TARGET(STORE_DEREF) {
2326 PyObject *v = POP();
2327 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002328 PyObject *oldobj = PyCell_GET(cell);
2329 PyCell_SET(cell, v);
2330 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002331 DISPATCH();
2332 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002333
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002334 TARGET(BUILD_STRING) {
2335 PyObject *str;
2336 PyObject *empty = PyUnicode_New(0, 0);
2337 if (empty == NULL) {
2338 goto error;
2339 }
2340 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2341 Py_DECREF(empty);
2342 if (str == NULL)
2343 goto error;
2344 while (--oparg >= 0) {
2345 PyObject *item = POP();
2346 Py_DECREF(item);
2347 }
2348 PUSH(str);
2349 DISPATCH();
2350 }
2351
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 TARGET(BUILD_TUPLE) {
2353 PyObject *tup = PyTuple_New(oparg);
2354 if (tup == NULL)
2355 goto error;
2356 while (--oparg >= 0) {
2357 PyObject *item = POP();
2358 PyTuple_SET_ITEM(tup, oparg, item);
2359 }
2360 PUSH(tup);
2361 DISPATCH();
2362 }
2363
2364 TARGET(BUILD_LIST) {
2365 PyObject *list = PyList_New(oparg);
2366 if (list == NULL)
2367 goto error;
2368 while (--oparg >= 0) {
2369 PyObject *item = POP();
2370 PyList_SET_ITEM(list, oparg, item);
2371 }
2372 PUSH(list);
2373 DISPATCH();
2374 }
2375
Serhiy Storchaka73442852016-10-02 10:33:46 +03002376 TARGET(BUILD_TUPLE_UNPACK_WITH_CALL)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002377 TARGET(BUILD_TUPLE_UNPACK)
2378 TARGET(BUILD_LIST_UNPACK) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002379 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002380 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002381 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002382 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002383
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002384 if (sum == NULL)
2385 goto error;
2386
2387 for (i = oparg; i > 0; i--) {
2388 PyObject *none_val;
2389
2390 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2391 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002392 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002393 PyErr_ExceptionMatches(PyExc_TypeError))
2394 {
2395 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002396 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002397 Py_DECREF(sum);
2398 goto error;
2399 }
2400 Py_DECREF(none_val);
2401 }
2402
2403 if (convert_to_tuple) {
2404 return_value = PyList_AsTuple(sum);
2405 Py_DECREF(sum);
2406 if (return_value == NULL)
2407 goto error;
2408 }
2409 else {
2410 return_value = sum;
2411 }
2412
2413 while (oparg--)
2414 Py_DECREF(POP());
2415 PUSH(return_value);
2416 DISPATCH();
2417 }
2418
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 TARGET(BUILD_SET) {
2420 PyObject *set = PySet_New(NULL);
2421 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002422 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 if (set == NULL)
2424 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002425 for (i = oparg; i > 0; i--) {
2426 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002427 if (err == 0)
2428 err = PySet_Add(set, item);
2429 Py_DECREF(item);
2430 }
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002431 STACKADJ(-oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002432 if (err != 0) {
2433 Py_DECREF(set);
2434 goto error;
2435 }
2436 PUSH(set);
2437 DISPATCH();
2438 }
2439
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002440 TARGET(BUILD_SET_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002441 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002442 PyObject *sum = PySet_New(NULL);
2443 if (sum == NULL)
2444 goto error;
2445
2446 for (i = oparg; i > 0; i--) {
2447 if (_PySet_Update(sum, PEEK(i)) < 0) {
2448 Py_DECREF(sum);
2449 goto error;
2450 }
2451 }
2452
2453 while (oparg--)
2454 Py_DECREF(POP());
2455 PUSH(sum);
2456 DISPATCH();
2457 }
2458
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002459 TARGET(BUILD_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002460 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002461 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2462 if (map == NULL)
2463 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002464 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002465 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002466 PyObject *key = PEEK(2*i);
2467 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002468 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002469 if (err != 0) {
2470 Py_DECREF(map);
2471 goto error;
2472 }
2473 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002474
2475 while (oparg--) {
2476 Py_DECREF(POP());
2477 Py_DECREF(POP());
2478 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002479 PUSH(map);
2480 DISPATCH();
2481 }
2482
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002483 TARGET(SETUP_ANNOTATIONS) {
2484 _Py_IDENTIFIER(__annotations__);
2485 int err;
2486 PyObject *ann_dict;
2487 if (f->f_locals == NULL) {
2488 PyErr_Format(PyExc_SystemError,
2489 "no locals found when setting up annotations");
2490 goto error;
2491 }
2492 /* check if __annotations__ in locals()... */
2493 if (PyDict_CheckExact(f->f_locals)) {
2494 ann_dict = _PyDict_GetItemId(f->f_locals,
2495 &PyId___annotations__);
2496 if (ann_dict == NULL) {
2497 /* ...if not, create a new one */
2498 ann_dict = PyDict_New();
2499 if (ann_dict == NULL) {
2500 goto error;
2501 }
2502 err = _PyDict_SetItemId(f->f_locals,
2503 &PyId___annotations__, ann_dict);
2504 Py_DECREF(ann_dict);
2505 if (err != 0) {
2506 goto error;
2507 }
2508 }
2509 }
2510 else {
2511 /* do the same if locals() is not a dict */
2512 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2513 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002514 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002515 }
2516 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2517 if (ann_dict == NULL) {
2518 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2519 goto error;
2520 }
2521 PyErr_Clear();
2522 ann_dict = PyDict_New();
2523 if (ann_dict == NULL) {
2524 goto error;
2525 }
2526 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2527 Py_DECREF(ann_dict);
2528 if (err != 0) {
2529 goto error;
2530 }
2531 }
2532 else {
2533 Py_DECREF(ann_dict);
2534 }
2535 }
2536 DISPATCH();
2537 }
2538
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002539 TARGET(BUILD_CONST_KEY_MAP) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002540 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002541 PyObject *map;
2542 PyObject *keys = TOP();
2543 if (!PyTuple_CheckExact(keys) ||
2544 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2545 PyErr_SetString(PyExc_SystemError,
2546 "bad BUILD_CONST_KEY_MAP keys argument");
2547 goto error;
2548 }
2549 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2550 if (map == NULL) {
2551 goto error;
2552 }
2553 for (i = oparg; i > 0; i--) {
2554 int err;
2555 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2556 PyObject *value = PEEK(i + 1);
2557 err = PyDict_SetItem(map, key, value);
2558 if (err != 0) {
2559 Py_DECREF(map);
2560 goto error;
2561 }
2562 }
2563
2564 Py_DECREF(POP());
2565 while (oparg--) {
2566 Py_DECREF(POP());
2567 }
2568 PUSH(map);
2569 DISPATCH();
2570 }
2571
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002572 TARGET(BUILD_MAP_UNPACK) {
Victor Stinner74319ae2016-08-25 00:04:09 +02002573 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002574 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002575 if (sum == NULL)
2576 goto error;
2577
2578 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002579 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002580 if (PyDict_Update(sum, arg) < 0) {
2581 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2582 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002583 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002584 arg->ob_type->tp_name);
2585 }
2586 Py_DECREF(sum);
2587 goto error;
2588 }
2589 }
2590
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002591 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002592 Py_DECREF(POP());
2593 PUSH(sum);
2594 DISPATCH();
2595 }
2596
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002597 TARGET(BUILD_MAP_UNPACK_WITH_CALL) {
2598 Py_ssize_t i;
2599 PyObject *sum = PyDict_New();
2600 if (sum == NULL)
2601 goto error;
2602
2603 for (i = oparg; i > 0; i--) {
2604 PyObject *arg = PEEK(i);
2605 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2606 PyObject *func = PEEK(2 + oparg);
2607 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002608 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002609 }
2610 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2611 PyObject *exc, *val, *tb;
2612 PyErr_Fetch(&exc, &val, &tb);
2613 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2614 PyObject *key = PyTuple_GET_ITEM(val, 0);
2615 if (!PyUnicode_Check(key)) {
2616 PyErr_Format(PyExc_TypeError,
2617 "%.200s%.200s keywords must be strings",
2618 PyEval_GetFuncName(func),
2619 PyEval_GetFuncDesc(func));
2620 } else {
2621 PyErr_Format(PyExc_TypeError,
2622 "%.200s%.200s got multiple "
2623 "values for keyword argument '%U'",
2624 PyEval_GetFuncName(func),
2625 PyEval_GetFuncDesc(func),
2626 key);
2627 }
2628 Py_XDECREF(exc);
2629 Py_XDECREF(val);
2630 Py_XDECREF(tb);
2631 }
2632 else {
2633 PyErr_Restore(exc, val, tb);
2634 }
2635 }
2636 Py_DECREF(sum);
2637 goto error;
2638 }
2639 }
2640
2641 while (oparg--)
2642 Py_DECREF(POP());
2643 PUSH(sum);
2644 DISPATCH();
2645 }
2646
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002647 TARGET(MAP_ADD) {
2648 PyObject *key = TOP();
2649 PyObject *value = SECOND();
2650 PyObject *map;
2651 int err;
2652 STACKADJ(-2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002653 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002654 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002655 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002656 Py_DECREF(value);
2657 Py_DECREF(key);
2658 if (err != 0)
2659 goto error;
2660 PREDICT(JUMP_ABSOLUTE);
2661 DISPATCH();
2662 }
2663
2664 TARGET(LOAD_ATTR) {
2665 PyObject *name = GETITEM(names, oparg);
2666 PyObject *owner = TOP();
2667 PyObject *res = PyObject_GetAttr(owner, name);
2668 Py_DECREF(owner);
2669 SET_TOP(res);
2670 if (res == NULL)
2671 goto error;
2672 DISPATCH();
2673 }
2674
2675 TARGET(COMPARE_OP) {
2676 PyObject *right = POP();
2677 PyObject *left = TOP();
2678 PyObject *res = cmp_outcome(oparg, left, right);
2679 Py_DECREF(left);
2680 Py_DECREF(right);
2681 SET_TOP(res);
2682 if (res == NULL)
2683 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002684 PREDICT(POP_JUMP_IF_FALSE);
2685 PREDICT(POP_JUMP_IF_TRUE);
2686 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002687 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002688
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 TARGET(IMPORT_NAME) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002690 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002691 PyObject *fromlist = POP();
2692 PyObject *level = TOP();
2693 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002694 res = import_name(f, name, fromlist, level);
2695 Py_DECREF(level);
2696 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002697 SET_TOP(res);
2698 if (res == NULL)
2699 goto error;
2700 DISPATCH();
2701 }
2702
2703 TARGET(IMPORT_STAR) {
2704 PyObject *from = POP(), *locals;
2705 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002706 if (PyFrame_FastToLocalsWithError(f) < 0) {
2707 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002708 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002709 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002710
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002711 locals = f->f_locals;
2712 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 PyErr_SetString(PyExc_SystemError,
2714 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002715 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002716 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002717 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002718 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002719 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002720 Py_DECREF(from);
2721 if (err != 0)
2722 goto error;
2723 DISPATCH();
2724 }
Guido van Rossum25831651993-05-19 14:50:45 +00002725
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 TARGET(IMPORT_FROM) {
2727 PyObject *name = GETITEM(names, oparg);
2728 PyObject *from = TOP();
2729 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002731 PUSH(res);
2732 if (res == NULL)
2733 goto error;
2734 DISPATCH();
2735 }
Thomas Wouters52152252000-08-17 22:55:00 +00002736
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002737 TARGET(JUMP_FORWARD) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002738 JUMPBY(oparg);
2739 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002741
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002742 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 TARGET(POP_JUMP_IF_FALSE) {
2744 PyObject *cond = POP();
2745 int err;
2746 if (cond == Py_True) {
2747 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 FAST_DISPATCH();
2749 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002750 if (cond == Py_False) {
2751 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002752 JUMPTO(oparg);
2753 FAST_DISPATCH();
2754 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002755 err = PyObject_IsTrue(cond);
2756 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002758 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 else if (err == 0)
2760 JUMPTO(oparg);
2761 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002762 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002764 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002765
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002766 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 TARGET(POP_JUMP_IF_TRUE) {
2768 PyObject *cond = POP();
2769 int err;
2770 if (cond == Py_False) {
2771 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002772 FAST_DISPATCH();
2773 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 if (cond == Py_True) {
2775 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002776 JUMPTO(oparg);
2777 FAST_DISPATCH();
2778 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002779 err = PyObject_IsTrue(cond);
2780 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002782 JUMPTO(oparg);
2783 }
2784 else if (err == 0)
2785 ;
2786 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002788 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002789 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002790
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 TARGET(JUMP_IF_FALSE_OR_POP) {
2792 PyObject *cond = TOP();
2793 int err;
2794 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002795 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002796 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 FAST_DISPATCH();
2798 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002800 JUMPTO(oparg);
2801 FAST_DISPATCH();
2802 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002804 if (err > 0) {
2805 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002806 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002807 }
2808 else if (err == 0)
2809 JUMPTO(oparg);
2810 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002811 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002813 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002814
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 TARGET(JUMP_IF_TRUE_OR_POP) {
2816 PyObject *cond = TOP();
2817 int err;
2818 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 FAST_DISPATCH();
2822 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002823 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002824 JUMPTO(oparg);
2825 FAST_DISPATCH();
2826 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002827 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 JUMPTO(oparg);
2830 }
2831 else if (err == 0) {
2832 STACKADJ(-1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002833 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002834 }
2835 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002836 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002837 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002838 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002839
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002840 PREDICTED(JUMP_ABSOLUTE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002841 TARGET(JUMP_ABSOLUTE) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002842 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002843#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002844 /* Enabling this path speeds-up all while and for-loops by bypassing
2845 the per-loop checks for signals. By default, this should be turned-off
2846 because it prevents detection of a control-break in tight loops like
2847 "while 1: pass". Compile with this option turned-on when you need
2848 the speed-up and do not need break checking inside tight loops (ones
2849 that contain only instructions ending with FAST_DISPATCH).
2850 */
2851 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002852#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002853 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002854#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002855 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002856
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002857 TARGET(GET_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002858 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002859 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002860 PyObject *iter = PyObject_GetIter(iterable);
2861 Py_DECREF(iterable);
2862 SET_TOP(iter);
2863 if (iter == NULL)
2864 goto error;
2865 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002866 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002867 DISPATCH();
2868 }
2869
2870 TARGET(GET_YIELD_FROM_ITER) {
2871 /* before: [obj]; after [getiter(obj)] */
2872 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002873 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002874 if (PyCoro_CheckExact(iterable)) {
2875 /* `iterable` is a coroutine */
2876 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2877 /* and it is used in a 'yield from' expression of a
2878 regular generator. */
2879 Py_DECREF(iterable);
2880 SET_TOP(NULL);
2881 PyErr_SetString(PyExc_TypeError,
2882 "cannot 'yield from' a coroutine object "
2883 "in a non-coroutine generator");
2884 goto error;
2885 }
2886 }
2887 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002888 /* `iterable` is not a generator. */
2889 iter = PyObject_GetIter(iterable);
2890 Py_DECREF(iterable);
2891 SET_TOP(iter);
2892 if (iter == NULL)
2893 goto error;
2894 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002895 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002896 DISPATCH();
2897 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002898
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002899 PREDICTED(FOR_ITER);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002900 TARGET(FOR_ITER) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002901 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002902 PyObject *iter = TOP();
2903 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2904 if (next != NULL) {
2905 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 PREDICT(STORE_FAST);
2907 PREDICT(UNPACK_SEQUENCE);
2908 DISPATCH();
2909 }
2910 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002911 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2912 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002913 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002914 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002915 PyErr_Clear();
2916 }
2917 /* iterator ended normally */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 STACKADJ(-1);
2919 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002921 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002922 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002923 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002924
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 TARGET(BREAK_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002926 why = WHY_BREAK;
2927 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002928 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002929
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002930 TARGET(CONTINUE_LOOP) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 retval = PyLong_FromLong(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002932 if (retval == NULL)
2933 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002934 why = WHY_CONTINUE;
2935 goto fast_block_end;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 }
Raymond Hettinger2d783e92004-03-12 09:12:22 +00002937
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03002938 TARGET(SETUP_LOOP)
2939 TARGET(SETUP_EXCEPT)
2940 TARGET(SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002941 /* NOTE: If you add any new block-setup opcodes that
2942 are not try/except/finally handlers, you may need
2943 to update the PyGen_NeedsFinalizing() function.
2944 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 PyFrame_BlockSetup(f, opcode, INSTR_OFFSET() + oparg,
2947 STACK_LEVEL());
2948 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002949 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002950
Yury Selivanov75445082015-05-11 22:57:16 -04002951 TARGET(BEFORE_ASYNC_WITH) {
2952 _Py_IDENTIFIER(__aexit__);
2953 _Py_IDENTIFIER(__aenter__);
2954
2955 PyObject *mgr = TOP();
2956 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2957 *enter;
2958 PyObject *res;
2959 if (exit == NULL)
2960 goto error;
2961 SET_TOP(exit);
2962 enter = special_lookup(mgr, &PyId___aenter__);
2963 Py_DECREF(mgr);
2964 if (enter == NULL)
2965 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002966 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002967 Py_DECREF(enter);
2968 if (res == NULL)
2969 goto error;
2970 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002971 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002972 DISPATCH();
2973 }
2974
2975 TARGET(SETUP_ASYNC_WITH) {
2976 PyObject *res = POP();
2977 /* Setup the finally block before pushing the result
2978 of __aenter__ on the stack. */
2979 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2980 STACK_LEVEL());
2981 PUSH(res);
2982 DISPATCH();
2983 }
2984
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 TARGET(SETUP_WITH) {
Benjamin Petersonce798522012-01-22 11:24:29 -05002986 _Py_IDENTIFIER(__exit__);
2987 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002988 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002989 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002991 if (enter == NULL)
2992 goto error;
2993 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002994 if (exit == NULL) {
2995 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002996 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002997 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002998 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002999 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003000 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003001 Py_DECREF(enter);
3002 if (res == NULL)
3003 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 /* Setup the finally block before pushing the result
3005 of __enter__ on the stack. */
3006 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3007 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003008
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003009 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003010 DISPATCH();
3011 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003012
Yury Selivanov75445082015-05-11 22:57:16 -04003013 TARGET(WITH_CLEANUP_START) {
Benjamin Peterson8f169482013-10-29 22:25:06 -04003014 /* At the top of the stack are 1-6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003015 how/why we entered the finally clause:
3016 - TOP = None
3017 - (TOP, SECOND) = (WHY_{RETURN,CONTINUE}), retval
3018 - TOP = WHY_*; no retval below it
3019 - (TOP, SECOND, THIRD) = exc_info()
3020 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
3021 Below them is EXIT, the context.__exit__ bound method.
3022 In the last case, we must call
3023 EXIT(TOP, SECOND, THIRD)
3024 otherwise we must call
3025 EXIT(None, None, None)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003026
Benjamin Peterson8f169482013-10-29 22:25:06 -04003027 In the first three cases, we remove EXIT from the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003028 stack, leaving the rest in the same order. In the
Benjamin Peterson8f169482013-10-29 22:25:06 -04003029 fourth case, we shift the bottom 3 values of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 stack down, and replace the empty spot with NULL.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003032 In addition, if the stack represents an exception,
3033 *and* the function call returns a 'true' value, we
3034 push WHY_SILENCED onto the stack. END_FINALLY will
3035 then not re-raise the exception. (But non-local
3036 gotos should still be resumed.)
3037 */
Thomas Wouters477c8d52006-05-27 19:21:47 +00003038
Victor Stinner842cfff2016-12-01 14:45:31 +01003039 PyObject* stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003040 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003041 PyObject *exc, *val, *tb, *res;
3042
3043 val = tb = Py_None;
3044 exc = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003045 if (exc == Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 (void)POP();
3047 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003048 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003050 else if (PyLong_Check(exc)) {
3051 STACKADJ(-1);
3052 switch (PyLong_AsLong(exc)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003053 case WHY_RETURN:
3054 case WHY_CONTINUE:
3055 /* Retval in TOP. */
3056 exit_func = SECOND();
3057 SET_SECOND(TOP());
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003058 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 break;
3060 default:
3061 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003062 SET_TOP(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003063 break;
3064 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 }
3067 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003068 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003070 val = SECOND();
3071 tb = THIRD();
3072 tp2 = FOURTH();
3073 exc2 = PEEK(5);
3074 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003075 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003076 SET_VALUE(7, tb2);
3077 SET_VALUE(6, exc2);
3078 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003079 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3080 SET_FOURTH(NULL);
3081 /* We just shifted the stack down, so we have
3082 to tell the except handler block that the
3083 values are lower than it expects. */
3084 block = &f->f_blockstack[f->f_iblock - 1];
3085 assert(block->b_type == EXCEPT_HANDLER);
3086 block->b_level--;
3087 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003088
3089 stack[0] = exc;
3090 stack[1] = val;
3091 stack[2] = tb;
3092 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003093 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003094 if (res == NULL)
3095 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003096
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003097 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003098 PUSH(exc);
3099 PUSH(res);
3100 PREDICT(WITH_CLEANUP_FINISH);
3101 DISPATCH();
3102 }
3103
3104 PREDICTED(WITH_CLEANUP_FINISH);
3105 TARGET(WITH_CLEANUP_FINISH) {
3106 PyObject *res = POP();
3107 PyObject *exc = POP();
3108 int err;
3109
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003110 if (exc != Py_None)
3111 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003112 else
3113 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003114
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003116 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003118 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003119 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120 else if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003121 /* There was an exception and a True return */
3122 PUSH(PyLong_FromLong((long) WHY_SILENCED));
3123 }
3124 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003125 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003126 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003127
Yury Selivanovf2392132016-12-13 19:03:51 -05003128 TARGET(LOAD_METHOD) {
3129 /* Designed to work in tamdem with CALL_METHOD. */
3130 PyObject *name = GETITEM(names, oparg);
3131 PyObject *obj = TOP();
3132 PyObject *meth = NULL;
3133
3134 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3135
Yury Selivanovf2392132016-12-13 19:03:51 -05003136 if (meth == NULL) {
3137 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003138 goto error;
3139 }
3140
3141 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003142 /* We can bypass temporary bound method object.
3143 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003144
INADA Naoki015bce62017-01-16 17:23:30 +09003145 meth | self | arg1 | ... | argN
3146 */
3147 SET_TOP(meth);
3148 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003149 }
3150 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003151 /* meth is not an unbound method (but a regular attr, or
3152 something was returned by a descriptor protocol). Set
3153 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003154 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003155
3156 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003157 */
INADA Naoki015bce62017-01-16 17:23:30 +09003158 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003159 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003160 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003161 }
3162 DISPATCH();
3163 }
3164
3165 TARGET(CALL_METHOD) {
3166 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003167 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003168
3169 sp = stack_pointer;
3170
INADA Naoki015bce62017-01-16 17:23:30 +09003171 meth = PEEK(oparg + 2);
3172 if (meth == NULL) {
3173 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3174 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003175
3176 Stack layout:
3177
INADA Naoki015bce62017-01-16 17:23:30 +09003178 ... | NULL | callable | arg1 | ... | argN
3179 ^- TOP()
3180 ^- (-oparg)
3181 ^- (-oparg-1)
3182 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003183
Ville Skyttä49b27342017-08-03 09:00:59 +03003184 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003185 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003186 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003187 res = call_function(&sp, oparg, NULL);
3188 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003189 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003190 }
3191 else {
3192 /* This is a method call. Stack layout:
3193
INADA Naoki015bce62017-01-16 17:23:30 +09003194 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003195 ^- TOP()
3196 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003197 ^- (-oparg-1)
3198 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003199
INADA Naoki015bce62017-01-16 17:23:30 +09003200 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003201 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003202 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003203 */
3204 res = call_function(&sp, oparg + 1, NULL);
3205 stack_pointer = sp;
3206 }
3207
3208 PUSH(res);
3209 if (res == NULL)
3210 goto error;
3211 DISPATCH();
3212 }
3213
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003214 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003215 TARGET(CALL_FUNCTION) {
3216 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003217 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003218 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003219 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003221 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003222 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003223 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003225 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003226
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003227 TARGET(CALL_FUNCTION_KW) {
3228 PyObject **sp, *res, *names;
3229
3230 names = POP();
3231 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003232 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003233 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003234 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003235 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003236 Py_DECREF(names);
3237
3238 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003240 }
3241 DISPATCH();
3242 }
3243
3244 TARGET(CALL_FUNCTION_EX) {
3245 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003246 if (oparg & 0x01) {
3247 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003248 if (!PyDict_CheckExact(kwargs)) {
3249 PyObject *d = PyDict_New();
3250 if (d == NULL)
3251 goto error;
3252 if (PyDict_Update(d, kwargs) != 0) {
3253 Py_DECREF(d);
3254 /* PyDict_Update raises attribute
3255 * error (percolated from an attempt
3256 * to get 'keys' attribute) instead of
3257 * a type error if its second argument
3258 * is not a mapping.
3259 */
3260 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003261 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003262 }
Victor Stinnereece2222016-09-12 11:16:37 +02003263 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003264 goto error;
3265 }
3266 Py_DECREF(kwargs);
3267 kwargs = d;
3268 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003269 assert(PyDict_CheckExact(kwargs));
3270 }
3271 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003272 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003273 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003274 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003275 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003276 goto error;
3277 }
3278 Py_SETREF(callargs, PySequence_Tuple(callargs));
3279 if (callargs == NULL) {
3280 goto error;
3281 }
3282 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003283 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003284
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003285 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003286 Py_DECREF(func);
3287 Py_DECREF(callargs);
3288 Py_XDECREF(kwargs);
3289
3290 SET_TOP(result);
3291 if (result == NULL) {
3292 goto error;
3293 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003294 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003295 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003296
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03003297 TARGET(MAKE_FUNCTION) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003298 PyObject *qualname = POP();
3299 PyObject *codeobj = POP();
3300 PyFunctionObject *func = (PyFunctionObject *)
3301 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003302
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003303 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003304 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003305 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003306 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003308
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003309 if (oparg & 0x08) {
3310 assert(PyTuple_CheckExact(TOP()));
3311 func ->func_closure = POP();
3312 }
3313 if (oparg & 0x04) {
3314 assert(PyDict_CheckExact(TOP()));
3315 func->func_annotations = POP();
3316 }
3317 if (oparg & 0x02) {
3318 assert(PyDict_CheckExact(TOP()));
3319 func->func_kwdefaults = POP();
3320 }
3321 if (oparg & 0x01) {
3322 assert(PyTuple_CheckExact(TOP()));
3323 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003324 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003325
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003326 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003327 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003328 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003329
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003330 TARGET(BUILD_SLICE) {
3331 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003332 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003333 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003335 step = NULL;
3336 stop = POP();
3337 start = TOP();
3338 slice = PySlice_New(start, stop, step);
3339 Py_DECREF(start);
3340 Py_DECREF(stop);
3341 Py_XDECREF(step);
3342 SET_TOP(slice);
3343 if (slice == NULL)
3344 goto error;
3345 DISPATCH();
3346 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003347
Eric V. Smitha78c7952015-11-03 12:45:05 -05003348 TARGET(FORMAT_VALUE) {
3349 /* Handles f-string value formatting. */
3350 PyObject *result;
3351 PyObject *fmt_spec;
3352 PyObject *value;
3353 PyObject *(*conv_fn)(PyObject *);
3354 int which_conversion = oparg & FVC_MASK;
3355 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3356
3357 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003358 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003359
3360 /* See if any conversion is specified. */
3361 switch (which_conversion) {
3362 case FVC_STR: conv_fn = PyObject_Str; break;
3363 case FVC_REPR: conv_fn = PyObject_Repr; break;
3364 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3365
3366 /* Must be 0 (meaning no conversion), since only four
3367 values are allowed by (oparg & FVC_MASK). */
3368 default: conv_fn = NULL; break;
3369 }
3370
3371 /* If there's a conversion function, call it and replace
3372 value with that result. Otherwise, just use value,
3373 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003374 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003375 result = conv_fn(value);
3376 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003377 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003378 Py_XDECREF(fmt_spec);
3379 goto error;
3380 }
3381 value = result;
3382 }
3383
3384 /* If value is a unicode object, and there's no fmt_spec,
3385 then we know the result of format(value) is value
3386 itself. In that case, skip calling format(). I plan to
3387 move this optimization in to PyObject_Format()
3388 itself. */
3389 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3390 /* Do nothing, just transfer ownership to result. */
3391 result = value;
3392 } else {
3393 /* Actually call format(). */
3394 result = PyObject_Format(value, fmt_spec);
3395 Py_DECREF(value);
3396 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003397 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003398 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003399 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003400 }
3401
Eric V. Smith135d5f42016-02-05 18:23:08 -05003402 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003403 DISPATCH();
3404 }
3405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 TARGET(EXTENDED_ARG) {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003407 int oldoparg = oparg;
3408 NEXTOPARG();
3409 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003410 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003412
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003413
Antoine Pitrou042b1282010-08-13 21:15:58 +00003414#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003415 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003416#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 default:
3418 fprintf(stderr,
3419 "XXX lineno: %d, opcode: %d\n",
3420 PyFrame_GetLineNumber(f),
3421 opcode);
3422 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003423 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003424
3425#ifdef CASE_TOO_BIG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00003427#endif
3428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003430
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003431 /* This should never be reached. Every opcode should end with DISPATCH()
3432 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003433 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003434
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003435error:
Martin v. Löwisf30d60e2004-06-08 08:17:44 +00003436
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003437 assert(why == WHY_NOT);
3438 why = WHY_EXCEPTION;
Guido van Rossumac7be682001-01-17 15:42:30 +00003439
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003440 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003441#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003442 if (!PyErr_Occurred())
3443 PyErr_SetString(PyExc_SystemError,
3444 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003445#else
3446 assert(PyErr_Occurred());
3447#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003448
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003449 /* Log traceback info. */
3450 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003451
Benjamin Peterson51f46162013-01-23 08:38:47 -05003452 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003453 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3454 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003455
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003456fast_block_end:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003457 assert(why != WHY_NOT);
3458
3459 /* Unwind stacks if a (pseudo) exception occurred */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 while (why != WHY_NOT && f->f_iblock > 0) {
3461 /* Peek at the current block. */
3462 PyTryBlock *b = &f->f_blockstack[f->f_iblock - 1];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 assert(why != WHY_YIELD);
3465 if (b->b_type == SETUP_LOOP && why == WHY_CONTINUE) {
3466 why = WHY_NOT;
3467 JUMPTO(PyLong_AS_LONG(retval));
3468 Py_DECREF(retval);
3469 break;
3470 }
3471 /* Now we have to pop the block. */
3472 f->f_iblock--;
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003473
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 if (b->b_type == EXCEPT_HANDLER) {
3475 UNWIND_EXCEPT_HANDLER(b);
3476 continue;
3477 }
3478 UNWIND_BLOCK(b);
3479 if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
3480 why = WHY_NOT;
3481 JUMPTO(b->b_handler);
3482 break;
3483 }
3484 if (why == WHY_EXCEPTION && (b->b_type == SETUP_EXCEPT
3485 || b->b_type == SETUP_FINALLY)) {
3486 PyObject *exc, *val, *tb;
3487 int handler = b->b_handler;
3488 /* Beware, this invalidates all b->b_* fields */
3489 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
3490 PUSH(tstate->exc_traceback);
3491 PUSH(tstate->exc_value);
3492 if (tstate->exc_type != NULL) {
3493 PUSH(tstate->exc_type);
3494 }
3495 else {
3496 Py_INCREF(Py_None);
3497 PUSH(Py_None);
3498 }
3499 PyErr_Fetch(&exc, &val, &tb);
3500 /* Make the raw exception data
3501 available to the handler,
3502 so a program can emulate the
3503 Python main loop. */
3504 PyErr_NormalizeException(
3505 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003506 if (tb != NULL)
3507 PyException_SetTraceback(val, tb);
3508 else
3509 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 Py_INCREF(exc);
3511 tstate->exc_type = exc;
3512 Py_INCREF(val);
3513 tstate->exc_value = val;
3514 tstate->exc_traceback = tb;
3515 if (tb == NULL)
3516 tb = Py_None;
3517 Py_INCREF(tb);
3518 PUSH(tb);
3519 PUSH(val);
3520 PUSH(exc);
3521 why = WHY_NOT;
3522 JUMPTO(handler);
3523 break;
3524 }
3525 if (b->b_type == SETUP_FINALLY) {
3526 if (why & (WHY_RETURN | WHY_CONTINUE))
3527 PUSH(retval);
3528 PUSH(PyLong_FromLong((long)why));
3529 why = WHY_NOT;
3530 JUMPTO(b->b_handler);
3531 break;
3532 }
3533 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003535 /* End the loop if we still have an error (or return) */
Guido van Rossumac7be682001-01-17 15:42:30 +00003536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 if (why != WHY_NOT)
3538 break;
Guido van Rossumac7be682001-01-17 15:42:30 +00003539
Victor Stinnerace47d72013-07-18 01:41:08 +02003540 assert(!PyErr_Occurred());
3541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003544 assert(why != WHY_YIELD);
3545 /* Pop remaining stack entries. */
3546 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003547 PyObject *o = POP();
3548 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003549 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003551 if (why != WHY_RETURN)
3552 retval = NULL;
Guido van Rossumac7be682001-01-17 15:42:30 +00003553
Victor Stinner4a7cc882015-03-06 23:35:27 +01003554 assert((retval != NULL) ^ (PyErr_Occurred() != NULL));
Victor Stinnerace47d72013-07-18 01:41:08 +02003555
Raymond Hettinger1dd83092004-02-06 18:32:33 +00003556fast_yield:
Yury Selivanoveb636452016-09-08 22:01:51 -07003557 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Victor Stinner26f7b8a2015-01-31 10:29:47 +01003558
Benjamin Petersonac913412011-07-03 16:25:11 -05003559 /* The purpose of this block is to put aside the generator's exception
3560 state and restore that of the calling frame. If the current
3561 exception state is from the caller, we clear the exception values
3562 on the generator frame, so they are not swapped back in latter. The
3563 origin of the current exception state is determined by checking for
3564 except handler blocks, which we must be in iff a new exception
3565 state came into existence in this frame. (An uncaught exception
3566 would have why == WHY_EXCEPTION, and we wouldn't be here). */
3567 int i;
Victor Stinner74319ae2016-08-25 00:04:09 +02003568 for (i = 0; i < f->f_iblock; i++) {
3569 if (f->f_blockstack[i].b_type == EXCEPT_HANDLER) {
Benjamin Petersonac913412011-07-03 16:25:11 -05003570 break;
Victor Stinner74319ae2016-08-25 00:04:09 +02003571 }
3572 }
Benjamin Petersonac913412011-07-03 16:25:11 -05003573 if (i == f->f_iblock)
3574 /* We did not create this exception. */
Benjamin Peterson87880242011-07-03 16:48:31 -05003575 restore_and_clear_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003576 else
Benjamin Peterson87880242011-07-03 16:48:31 -05003577 swap_exc_state(tstate, f);
Benjamin Petersonac913412011-07-03 16:25:11 -05003578 }
Benjamin Peterson83195c32011-07-03 13:44:00 -05003579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003580 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003581 if (tstate->c_tracefunc) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003582 if (why == WHY_RETURN || why == WHY_YIELD) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003583 if (call_trace(tstate->c_tracefunc, tstate->c_traceobj,
3584 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003586 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003587 why = WHY_EXCEPTION;
3588 }
3589 }
3590 else if (why == WHY_EXCEPTION) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003591 call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3592 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003593 PyTrace_RETURN, NULL);
3594 }
3595 }
3596 if (tstate->c_profilefunc) {
3597 if (why == WHY_EXCEPTION)
3598 call_trace_protected(tstate->c_profilefunc,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003599 tstate->c_profileobj,
3600 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 PyTrace_RETURN, NULL);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003602 else if (call_trace(tstate->c_profilefunc, tstate->c_profileobj,
3603 tstate, f,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003605 Py_CLEAR(retval);
Brett Cannonb94767f2011-02-22 20:15:44 +00003606 /* why = WHY_EXCEPTION; */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003607 }
3608 }
3609 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003612exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003613 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3614 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003615 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003616 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003617 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003618
Victor Stinnerefde1462015-03-21 15:04:43 +01003619 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003620}
3621
Benjamin Petersonb204a422011-06-05 22:04:07 -05003622static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003623format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3624{
3625 int err;
3626 Py_ssize_t len = PyList_GET_SIZE(names);
3627 PyObject *name_str, *comma, *tail, *tmp;
3628
3629 assert(PyList_CheckExact(names));
3630 assert(len >= 1);
3631 /* Deal with the joys of natural language. */
3632 switch (len) {
3633 case 1:
3634 name_str = PyList_GET_ITEM(names, 0);
3635 Py_INCREF(name_str);
3636 break;
3637 case 2:
3638 name_str = PyUnicode_FromFormat("%U and %U",
3639 PyList_GET_ITEM(names, len - 2),
3640 PyList_GET_ITEM(names, len - 1));
3641 break;
3642 default:
3643 tail = PyUnicode_FromFormat(", %U, and %U",
3644 PyList_GET_ITEM(names, len - 2),
3645 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003646 if (tail == NULL)
3647 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003648 /* Chop off the last two objects in the list. This shouldn't actually
3649 fail, but we can't be too careful. */
3650 err = PyList_SetSlice(names, len - 2, len, NULL);
3651 if (err == -1) {
3652 Py_DECREF(tail);
3653 return;
3654 }
3655 /* Stitch everything up into a nice comma-separated list. */
3656 comma = PyUnicode_FromString(", ");
3657 if (comma == NULL) {
3658 Py_DECREF(tail);
3659 return;
3660 }
3661 tmp = PyUnicode_Join(comma, names);
3662 Py_DECREF(comma);
3663 if (tmp == NULL) {
3664 Py_DECREF(tail);
3665 return;
3666 }
3667 name_str = PyUnicode_Concat(tmp, tail);
3668 Py_DECREF(tmp);
3669 Py_DECREF(tail);
3670 break;
3671 }
3672 if (name_str == NULL)
3673 return;
3674 PyErr_Format(PyExc_TypeError,
3675 "%U() missing %i required %s argument%s: %U",
3676 co->co_name,
3677 len,
3678 kind,
3679 len == 1 ? "" : "s",
3680 name_str);
3681 Py_DECREF(name_str);
3682}
3683
3684static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003685missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003686 PyObject **fastlocals)
3687{
Victor Stinner74319ae2016-08-25 00:04:09 +02003688 Py_ssize_t i, j = 0;
3689 Py_ssize_t start, end;
3690 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003691 const char *kind = positional ? "positional" : "keyword-only";
3692 PyObject *missing_names;
3693
3694 /* Compute the names of the arguments that are missing. */
3695 missing_names = PyList_New(missing);
3696 if (missing_names == NULL)
3697 return;
3698 if (positional) {
3699 start = 0;
3700 end = co->co_argcount - defcount;
3701 }
3702 else {
3703 start = co->co_argcount;
3704 end = start + co->co_kwonlyargcount;
3705 }
3706 for (i = start; i < end; i++) {
3707 if (GETLOCAL(i) == NULL) {
3708 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3709 PyObject *name = PyObject_Repr(raw);
3710 if (name == NULL) {
3711 Py_DECREF(missing_names);
3712 return;
3713 }
3714 PyList_SET_ITEM(missing_names, j++, name);
3715 }
3716 }
3717 assert(j == missing);
3718 format_missing(kind, co, missing_names);
3719 Py_DECREF(missing_names);
3720}
3721
3722static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003723too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3724 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003725{
3726 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003727 Py_ssize_t kwonly_given = 0;
3728 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003729 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003730 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003731
Benjamin Petersone109c702011-06-24 09:37:26 -05003732 assert((co->co_flags & CO_VARARGS) == 0);
3733 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003734 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3735 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003736 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003737 }
3738 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003739 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003740 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003741 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003742 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003743 }
3744 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003745 plural = (co_argcount != 1);
3746 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003747 }
3748 if (sig == NULL)
3749 return;
3750 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003751 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3752 kwonly_sig = PyUnicode_FromFormat(format,
3753 given != 1 ? "s" : "",
3754 kwonly_given,
3755 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003756 if (kwonly_sig == NULL) {
3757 Py_DECREF(sig);
3758 return;
3759 }
3760 }
3761 else {
3762 /* This will not fail. */
3763 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003764 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003765 }
3766 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003767 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003768 co->co_name,
3769 sig,
3770 plural ? "s" : "",
3771 given,
3772 kwonly_sig,
3773 given == 1 && !kwonly_given ? "was" : "were");
3774 Py_DECREF(sig);
3775 Py_DECREF(kwonly_sig);
3776}
3777
Guido van Rossumc2e20742006-02-27 22:32:47 +00003778/* This is gonna seem *real weird*, but if you put some other code between
Martin v. Löwis8d97e332004-06-27 15:43:12 +00003779 PyEval_EvalFrame() and PyEval_EvalCodeEx() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003780 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003781
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003782PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003783_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Victor Stinner74319ae2016-08-25 00:04:09 +02003784 PyObject **args, Py_ssize_t argcount,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003785 PyObject **kwnames, PyObject **kwargs,
3786 Py_ssize_t kwcount, int kwstep,
Victor Stinner74319ae2016-08-25 00:04:09 +02003787 PyObject **defs, Py_ssize_t defcount,
3788 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003789 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003790{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003791 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003792 PyFrameObject *f;
3793 PyObject *retval = NULL;
3794 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003795 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003797 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3798 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003799 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 if (globals == NULL) {
3802 PyErr_SetString(PyExc_SystemError,
3803 "PyEval_EvalCodeEx: NULL globals");
3804 return NULL;
3805 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003806
Victor Stinnerc7020012016-08-16 23:40:29 +02003807 /* Create the frame */
3808 tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003809 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003810 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003811 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003812 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003813 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 fastlocals = f->f_localsplus;
3815 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003816
Victor Stinnerc7020012016-08-16 23:40:29 +02003817 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003818 if (co->co_flags & CO_VARKEYWORDS) {
3819 kwdict = PyDict_New();
3820 if (kwdict == NULL)
3821 goto fail;
3822 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003823 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003824 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003825 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 SETLOCAL(i, kwdict);
3827 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003828 else {
3829 kwdict = NULL;
3830 }
3831
3832 /* Copy positional arguments into local variables */
3833 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003834 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003835 }
3836 else {
3837 n = argcount;
3838 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003839 for (i = 0; i < n; i++) {
3840 x = args[i];
3841 Py_INCREF(x);
3842 SETLOCAL(i, x);
3843 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003844
3845 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003846 if (co->co_flags & CO_VARARGS) {
3847 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003848 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003849 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003850 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003851 SETLOCAL(total_args, u);
3852 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003853 x = args[i];
3854 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003855 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003856 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003857 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003858
Serhiy Storchakab7281052016-09-12 00:52:40 +03003859 /* Handle keyword arguments passed as two strided arrays */
3860 kwcount *= kwstep;
3861 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003862 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003863 PyObject *keyword = kwnames[i];
3864 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003865 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003866
Benjamin Petersonb204a422011-06-05 22:04:07 -05003867 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3868 PyErr_Format(PyExc_TypeError,
3869 "%U() keywords must be strings",
3870 co->co_name);
3871 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003872 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003873
Benjamin Petersonb204a422011-06-05 22:04:07 -05003874 /* Speed hack: do raw pointer compares. As names are
3875 normally interned this should almost always hit. */
3876 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3877 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003878 PyObject *name = co_varnames[j];
3879 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003880 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003881 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003882 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003883
Benjamin Petersonb204a422011-06-05 22:04:07 -05003884 /* Slow fallback, just in case */
3885 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003886 PyObject *name = co_varnames[j];
3887 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3888 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003889 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003890 }
3891 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003892 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003893 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003894 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003895
Victor Stinner231d1f32017-01-11 02:12:06 +01003896 assert(j >= total_args);
3897 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003898 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003899 "%U() got an unexpected keyword argument '%S'",
3900 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003901 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003902 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003903
Christian Heimes0bd447f2013-07-20 14:48:10 +02003904 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3905 goto fail;
3906 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003907 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003908
Benjamin Petersonb204a422011-06-05 22:04:07 -05003909 kw_found:
3910 if (GETLOCAL(j) != NULL) {
3911 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003912 "%U() got multiple values for argument '%S'",
3913 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003914 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003915 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003916 Py_INCREF(value);
3917 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003919
3920 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003921 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003922 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003923 goto fail;
3924 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003925
3926 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003927 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003928 Py_ssize_t m = co->co_argcount - defcount;
3929 Py_ssize_t missing = 0;
3930 for (i = argcount; i < m; i++) {
3931 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003932 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003933 }
3934 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003935 if (missing) {
3936 missing_arguments(co, missing, defcount, fastlocals);
3937 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003938 }
3939 if (n > m)
3940 i = n - m;
3941 else
3942 i = 0;
3943 for (; i < defcount; i++) {
3944 if (GETLOCAL(m+i) == NULL) {
3945 PyObject *def = defs[i];
3946 Py_INCREF(def);
3947 SETLOCAL(m+i, def);
3948 }
3949 }
3950 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003951
3952 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003953 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003954 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003955 for (i = co->co_argcount; i < total_args; i++) {
3956 PyObject *name;
3957 if (GETLOCAL(i) != NULL)
3958 continue;
3959 name = PyTuple_GET_ITEM(co->co_varnames, i);
3960 if (kwdefs != NULL) {
3961 PyObject *def = PyDict_GetItem(kwdefs, name);
3962 if (def) {
3963 Py_INCREF(def);
3964 SETLOCAL(i, def);
3965 continue;
3966 }
3967 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003968 missing++;
3969 }
3970 if (missing) {
3971 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003972 goto fail;
3973 }
3974 }
3975
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003976 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003977 vars into frame. */
3978 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003979 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003980 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003981 /* Possibly account for the cell variable being an argument. */
3982 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003983 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003984 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003985 /* Clear the local copy. */
3986 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003987 }
3988 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003989 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003990 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003991 if (c == NULL)
3992 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003993 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003995
3996 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003997 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3998 PyObject *o = PyTuple_GET_ITEM(closure, i);
3999 Py_INCREF(o);
4000 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004002
Yury Selivanoveb636452016-09-08 22:01:51 -07004003 /* Handle generator/coroutine/asynchronous generator */
4004 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004005 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04004006 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004007 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004008
4009 if (is_coro && tstate->in_coroutine_wrapper) {
4010 assert(coro_wrapper != NULL);
4011 PyErr_Format(PyExc_RuntimeError,
4012 "coroutine wrapper %.200R attempted "
4013 "to recursively wrap %.200R",
4014 coro_wrapper,
4015 co);
4016 goto fail;
4017 }
Yury Selivanov75445082015-05-11 22:57:16 -04004018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 /* Don't need to keep the reference to f_back, it will be set
4020 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004021 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004023 /* Create a new generator that owns the ready to run frame
4024 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004025 if (is_coro) {
4026 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004027 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4028 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004029 } else {
4030 gen = PyGen_NewWithQualName(f, name, qualname);
4031 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004032 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004033 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004034 }
INADA Naoki9c157762016-12-26 18:52:46 +09004035
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004036 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004037
Yury Selivanov94c22632015-06-04 10:16:51 -04004038 if (is_coro && coro_wrapper != NULL) {
4039 PyObject *wrapped;
4040 tstate->in_coroutine_wrapper = 1;
4041 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
4042 tstate->in_coroutine_wrapper = 0;
4043 return wrapped;
4044 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04004045
Yury Selivanov75445082015-05-11 22:57:16 -04004046 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004047 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004048
Victor Stinner59a73272016-12-09 18:51:13 +01004049 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004050
Thomas Woutersce272b62007-09-19 21:19:28 +00004051fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004053 /* decref'ing the frame can cause __del__ methods to get invoked,
4054 which can call back into Python. While we're done with the
4055 current Python frame (f), the associated C stack is still in use,
4056 so recursion_depth must be boosted for the duration.
4057 */
4058 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09004059 if (Py_REFCNT(f) > 1) {
4060 Py_DECREF(f);
4061 _PyObject_GC_TRACK(f);
4062 }
4063 else {
4064 ++tstate->recursion_depth;
4065 Py_DECREF(f);
4066 --tstate->recursion_depth;
4067 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004069}
4070
Victor Stinner40ee3012014-06-16 15:59:28 +02004071PyObject *
4072PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
4073 PyObject **args, int argcount, PyObject **kws, int kwcount,
4074 PyObject **defs, int defcount, PyObject *kwdefs, PyObject *closure)
4075{
4076 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004077 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004078 kws, kws != NULL ? kws + 1 : NULL,
4079 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004080 defs, defcount,
4081 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004082 NULL, NULL);
4083}
Tim Peters5ca576e2001-06-18 22:08:13 +00004084
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004085static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05004086special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004087{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004088 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004089 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004090 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05004091 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 return NULL;
4093 }
4094 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004095}
4096
4097
Benjamin Peterson87880242011-07-03 16:48:31 -05004098/* These 3 functions deal with the exception state of generators. */
4099
4100static void
4101save_exc_state(PyThreadState *tstate, PyFrameObject *f)
4102{
4103 PyObject *type, *value, *traceback;
4104 Py_XINCREF(tstate->exc_type);
4105 Py_XINCREF(tstate->exc_value);
4106 Py_XINCREF(tstate->exc_traceback);
4107 type = f->f_exc_type;
4108 value = f->f_exc_value;
4109 traceback = f->f_exc_traceback;
4110 f->f_exc_type = tstate->exc_type;
4111 f->f_exc_value = tstate->exc_value;
4112 f->f_exc_traceback = tstate->exc_traceback;
4113 Py_XDECREF(type);
4114 Py_XDECREF(value);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02004115 Py_XDECREF(traceback);
Benjamin Peterson87880242011-07-03 16:48:31 -05004116}
4117
4118static void
4119swap_exc_state(PyThreadState *tstate, PyFrameObject *f)
4120{
4121 PyObject *tmp;
4122 tmp = tstate->exc_type;
4123 tstate->exc_type = f->f_exc_type;
4124 f->f_exc_type = tmp;
4125 tmp = tstate->exc_value;
4126 tstate->exc_value = f->f_exc_value;
4127 f->f_exc_value = tmp;
4128 tmp = tstate->exc_traceback;
4129 tstate->exc_traceback = f->f_exc_traceback;
4130 f->f_exc_traceback = tmp;
4131}
4132
4133static void
4134restore_and_clear_exc_state(PyThreadState *tstate, PyFrameObject *f)
4135{
4136 PyObject *type, *value, *tb;
4137 type = tstate->exc_type;
4138 value = tstate->exc_value;
4139 tb = tstate->exc_traceback;
4140 tstate->exc_type = f->f_exc_type;
4141 tstate->exc_value = f->f_exc_value;
4142 tstate->exc_traceback = f->f_exc_traceback;
4143 f->f_exc_type = NULL;
4144 f->f_exc_value = NULL;
4145 f->f_exc_traceback = NULL;
4146 Py_XDECREF(type);
4147 Py_XDECREF(value);
4148 Py_XDECREF(tb);
4149}
4150
4151
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004152/* Logic for the raise statement (too complicated for inlining).
4153 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004154static int
Collin Winter828f04a2007-08-31 00:04:24 +00004155do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004156{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004157 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004158
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004159 if (exc == NULL) {
4160 /* Reraise */
4161 PyThreadState *tstate = PyThreadState_GET();
4162 PyObject *tb;
4163 type = tstate->exc_type;
4164 value = tstate->exc_value;
4165 tb = tstate->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004166 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004167 PyErr_SetString(PyExc_RuntimeError,
4168 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004169 return 0;
4170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004171 Py_XINCREF(type);
4172 Py_XINCREF(value);
4173 Py_XINCREF(tb);
4174 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004175 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004176 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004177
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004178 /* We support the following forms of raise:
4179 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004180 raise <instance>
4181 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004182
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004183 if (PyExceptionClass_Check(exc)) {
4184 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004185 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004186 if (value == NULL)
4187 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004188 if (!PyExceptionInstance_Check(value)) {
4189 PyErr_Format(PyExc_TypeError,
4190 "calling %R should have returned an instance of "
4191 "BaseException, not %R",
4192 type, Py_TYPE(value));
4193 goto raise_error;
4194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004195 }
4196 else if (PyExceptionInstance_Check(exc)) {
4197 value = exc;
4198 type = PyExceptionInstance_Class(exc);
4199 Py_INCREF(type);
4200 }
4201 else {
4202 /* Not something you can raise. You get an exception
4203 anyway, just not what you specified :-) */
4204 Py_DECREF(exc);
4205 PyErr_SetString(PyExc_TypeError,
4206 "exceptions must derive from BaseException");
4207 goto raise_error;
4208 }
Collin Winter828f04a2007-08-31 00:04:24 +00004209
Serhiy Storchakac0191582016-09-27 11:37:10 +03004210 assert(type != NULL);
4211 assert(value != NULL);
4212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004213 if (cause) {
4214 PyObject *fixed_cause;
4215 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004216 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004217 if (fixed_cause == NULL)
4218 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004219 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004220 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004221 else if (PyExceptionInstance_Check(cause)) {
4222 fixed_cause = cause;
4223 }
4224 else if (cause == Py_None) {
4225 Py_DECREF(cause);
4226 fixed_cause = NULL;
4227 }
4228 else {
4229 PyErr_SetString(PyExc_TypeError,
4230 "exception causes must derive from "
4231 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004232 goto raise_error;
4233 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004234 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 }
Collin Winter828f04a2007-08-31 00:04:24 +00004236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004237 PyErr_SetObject(type, value);
4238 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004239 Py_DECREF(value);
4240 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004241 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004242
4243raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004244 Py_XDECREF(value);
4245 Py_XDECREF(type);
4246 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004247 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004248}
4249
Tim Petersd6d010b2001-06-21 02:49:55 +00004250/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004251 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004252
Guido van Rossum0368b722007-05-11 16:50:42 +00004253 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4254 with a variable target.
4255*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004256
Barry Warsawe42b18f1997-08-25 22:13:04 +00004257static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004258unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004259{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004260 int i = 0, j = 0;
4261 Py_ssize_t ll = 0;
4262 PyObject *it; /* iter(v) */
4263 PyObject *w;
4264 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004265
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004267
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004268 it = PyObject_GetIter(v);
4269 if (it == NULL)
4270 goto Error;
Tim Petersd6d010b2001-06-21 02:49:55 +00004271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 for (; i < argcnt; i++) {
4273 w = PyIter_Next(it);
4274 if (w == NULL) {
4275 /* Iterator done, via error or exhaustion. */
4276 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004277 if (argcntafter == -1) {
4278 PyErr_Format(PyExc_ValueError,
4279 "not enough values to unpack (expected %d, got %d)",
4280 argcnt, i);
4281 }
4282 else {
4283 PyErr_Format(PyExc_ValueError,
4284 "not enough values to unpack "
4285 "(expected at least %d, got %d)",
4286 argcnt + argcntafter, i);
4287 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004288 }
4289 goto Error;
4290 }
4291 *--sp = w;
4292 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004294 if (argcntafter == -1) {
4295 /* We better have exhausted the iterator now. */
4296 w = PyIter_Next(it);
4297 if (w == NULL) {
4298 if (PyErr_Occurred())
4299 goto Error;
4300 Py_DECREF(it);
4301 return 1;
4302 }
4303 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004304 PyErr_Format(PyExc_ValueError,
4305 "too many values to unpack (expected %d)",
4306 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004307 goto Error;
4308 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004310 l = PySequence_List(it);
4311 if (l == NULL)
4312 goto Error;
4313 *--sp = l;
4314 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004315
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004316 ll = PyList_GET_SIZE(l);
4317 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004318 PyErr_Format(PyExc_ValueError,
4319 "not enough values to unpack (expected at least %d, got %zd)",
4320 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004321 goto Error;
4322 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004324 /* Pop the "after-variable" args off the list. */
4325 for (j = argcntafter; j > 0; j--, i++) {
4326 *--sp = PyList_GET_ITEM(l, ll - j);
4327 }
4328 /* Resize the list. */
4329 Py_SIZE(l) = ll - argcntafter;
4330 Py_DECREF(it);
4331 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004332
Tim Petersd6d010b2001-06-21 02:49:55 +00004333Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004334 for (; i > 0; i--, sp++)
4335 Py_DECREF(*sp);
4336 Py_XDECREF(it);
4337 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004338}
4339
4340
Guido van Rossum96a42c81992-01-12 02:29:51 +00004341#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004342static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004343prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 printf("%s ", str);
4346 if (PyObject_Print(v, stdout, 0) != 0)
4347 PyErr_Clear(); /* Don't know what else to do */
4348 printf("\n");
4349 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004350}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004351#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004352
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004353static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004354call_exc_trace(Py_tracefunc func, PyObject *self,
4355 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004356{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004357 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004358 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004359 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004360 if (value == NULL) {
4361 value = Py_None;
4362 Py_INCREF(value);
4363 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004364 PyErr_NormalizeException(&type, &value, &orig_traceback);
4365 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 arg = PyTuple_Pack(3, type, value, traceback);
4367 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004368 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004369 return;
4370 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004371 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 Py_DECREF(arg);
4373 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004374 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004375 else {
4376 Py_XDECREF(type);
4377 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004378 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004380}
4381
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004382static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004383call_trace_protected(Py_tracefunc func, PyObject *obj,
4384 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004385 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004386{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004387 PyObject *type, *value, *traceback;
4388 int err;
4389 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004390 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004391 if (err == 0)
4392 {
4393 PyErr_Restore(type, value, traceback);
4394 return 0;
4395 }
4396 else {
4397 Py_XDECREF(type);
4398 Py_XDECREF(value);
4399 Py_XDECREF(traceback);
4400 return -1;
4401 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004402}
4403
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004404static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004405call_trace(Py_tracefunc func, PyObject *obj,
4406 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004407 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004408{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004409 int result;
4410 if (tstate->tracing)
4411 return 0;
4412 tstate->tracing++;
4413 tstate->use_tracing = 0;
4414 result = func(obj, frame, what, arg);
4415 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4416 || (tstate->c_profilefunc != NULL));
4417 tstate->tracing--;
4418 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004419}
4420
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004421PyObject *
4422_PyEval_CallTracing(PyObject *func, PyObject *args)
4423{
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004424 PyThreadState *tstate = PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004425 int save_tracing = tstate->tracing;
4426 int save_use_tracing = tstate->use_tracing;
4427 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004429 tstate->tracing = 0;
4430 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4431 || (tstate->c_profilefunc != NULL));
4432 result = PyObject_Call(func, args, NULL);
4433 tstate->tracing = save_tracing;
4434 tstate->use_tracing = save_use_tracing;
4435 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004436}
4437
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004438/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004439static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004440maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004441 PyThreadState *tstate, PyFrameObject *frame,
4442 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004443{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004444 int result = 0;
4445 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004446
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004447 /* If the last instruction executed isn't in the current
4448 instruction window, reset the window.
4449 */
4450 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4451 PyAddrPair bounds;
4452 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4453 &bounds);
4454 *instr_lb = bounds.ap_lower;
4455 *instr_ub = bounds.ap_upper;
4456 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004457 /* Always emit an opcode event if we're tracing all opcodes. */
4458 if (frame->f_trace_opcodes) {
4459 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4460 }
4461 /* If the last instruction falls at the start of a line or if it
4462 represents a jump backwards, update the frame's line number and
4463 then call the trace function if we're tracing source lines.
4464 */
4465 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004467 if (frame->f_trace_lines) {
4468 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4469 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 }
4471 *instr_prev = frame->f_lasti;
4472 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004473}
4474
Fred Drake5755ce62001-06-27 19:19:46 +00004475void
4476PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004478 PyThreadState *tstate = PyThreadState_GET();
4479 PyObject *temp = tstate->c_profileobj;
4480 Py_XINCREF(arg);
4481 tstate->c_profilefunc = NULL;
4482 tstate->c_profileobj = NULL;
4483 /* Must make sure that tracing is not ignored if 'temp' is freed */
4484 tstate->use_tracing = tstate->c_tracefunc != NULL;
4485 Py_XDECREF(temp);
4486 tstate->c_profilefunc = func;
4487 tstate->c_profileobj = arg;
4488 /* Flag that tracing or profiling is turned on */
4489 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004490}
4491
4492void
4493PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4494{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 PyThreadState *tstate = PyThreadState_GET();
4496 PyObject *temp = tstate->c_traceobj;
4497 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4498 Py_XINCREF(arg);
4499 tstate->c_tracefunc = NULL;
4500 tstate->c_traceobj = NULL;
4501 /* Must make sure that profiling is not ignored if 'temp' is freed */
4502 tstate->use_tracing = tstate->c_profilefunc != NULL;
4503 Py_XDECREF(temp);
4504 tstate->c_tracefunc = func;
4505 tstate->c_traceobj = arg;
4506 /* Flag that tracing or profiling is turned on */
4507 tstate->use_tracing = ((func != NULL)
4508 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004509}
4510
Yury Selivanov75445082015-05-11 22:57:16 -04004511void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004512_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004513{
4514 PyThreadState *tstate = PyThreadState_GET();
4515
Yury Selivanov75445082015-05-11 22:57:16 -04004516 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004517 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004518}
4519
4520PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004521_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004522{
4523 PyThreadState *tstate = PyThreadState_GET();
4524 return tstate->coroutine_wrapper;
4525}
4526
Yury Selivanoveb636452016-09-08 22:01:51 -07004527void
4528_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4529{
4530 PyThreadState *tstate = PyThreadState_GET();
4531
4532 Py_XINCREF(firstiter);
4533 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4534}
4535
4536PyObject *
4537_PyEval_GetAsyncGenFirstiter(void)
4538{
4539 PyThreadState *tstate = PyThreadState_GET();
4540 return tstate->async_gen_firstiter;
4541}
4542
4543void
4544_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4545{
4546 PyThreadState *tstate = PyThreadState_GET();
4547
4548 Py_XINCREF(finalizer);
4549 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4550}
4551
4552PyObject *
4553_PyEval_GetAsyncGenFinalizer(void)
4554{
4555 PyThreadState *tstate = PyThreadState_GET();
4556 return tstate->async_gen_finalizer;
4557}
4558
Guido van Rossumb209a111997-04-29 18:18:01 +00004559PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004560PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004561{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004562 PyFrameObject *current_frame = PyEval_GetFrame();
4563 if (current_frame == NULL)
4564 return PyThreadState_GET()->interp->builtins;
4565 else
4566 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004567}
4568
Guido van Rossumb209a111997-04-29 18:18:01 +00004569PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004570PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004572 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004573 if (current_frame == NULL) {
4574 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004576 }
4577
4578 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4579 return NULL;
4580
4581 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004582 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004583}
4584
Guido van Rossumb209a111997-04-29 18:18:01 +00004585PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004586PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004588 PyFrameObject *current_frame = PyEval_GetFrame();
4589 if (current_frame == NULL)
4590 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004591
4592 assert(current_frame->f_globals != NULL);
4593 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004594}
4595
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004596PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004597PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004599 PyThreadState *tstate = PyThreadState_GET();
4600 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004601}
4602
Guido van Rossum6135a871995-01-09 17:53:26 +00004603int
Tim Peters5ba58662001-07-16 02:29:45 +00004604PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004605{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004606 PyFrameObject *current_frame = PyEval_GetFrame();
4607 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004609 if (current_frame != NULL) {
4610 const int codeflags = current_frame->f_code->co_flags;
4611 const int compilerflags = codeflags & PyCF_MASK;
4612 if (compilerflags) {
4613 result = 1;
4614 cf->cf_flags |= compilerflags;
4615 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004616#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004617 if (codeflags & CO_GENERATOR_ALLOWED) {
4618 result = 1;
4619 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4620 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004621#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004622 }
4623 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004624}
4625
Guido van Rossum3f5da241990-12-20 15:06:42 +00004626
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004627const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004628PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004630 if (PyMethod_Check(func))
4631 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4632 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004633 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004634 else if (PyCFunction_Check(func))
4635 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4636 else
4637 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004638}
4639
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004640const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004641PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004643 if (PyMethod_Check(func))
4644 return "()";
4645 else if (PyFunction_Check(func))
4646 return "()";
4647 else if (PyCFunction_Check(func))
4648 return "()";
4649 else
4650 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004651}
4652
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004653#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004654if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004655 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4656 tstate, tstate->frame, \
4657 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 x = NULL; \
4659 } \
4660 else { \
4661 x = call; \
4662 if (tstate->c_profilefunc != NULL) { \
4663 if (x == NULL) { \
4664 call_trace_protected(tstate->c_profilefunc, \
4665 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004666 tstate, tstate->frame, \
4667 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 /* XXX should pass (type, value, tb) */ \
4669 } else { \
4670 if (call_trace(tstate->c_profilefunc, \
4671 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004672 tstate, tstate->frame, \
4673 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004674 Py_DECREF(x); \
4675 x = NULL; \
4676 } \
4677 } \
4678 } \
4679 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004680} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 x = call; \
4682 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004683
Victor Stinner415c5102017-01-11 00:54:57 +01004684/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4685 to reduce the stack consumption. */
4686Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004687call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004688{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004689 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004690 PyObject *func = *pfunc;
4691 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004692 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4693 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004694 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 /* Always dispatch PyCFunction first, because these are
4697 presumed to be the most frequent callable object.
4698 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004699 if (PyCFunction_Check(func)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 PyThreadState *tstate = PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004701 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004702 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004703 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
4704 PyThreadState *tstate = PyThreadState_GET();
INADA Naoki93fac8d2017-03-07 14:24:37 +09004705 if (tstate->use_tracing && tstate->c_profilefunc) {
4706 // We need to create PyCFunctionObject for tracing.
4707 PyMethodDescrObject *descr = (PyMethodDescrObject*)func;
4708 func = PyCFunction_NewEx(descr->d_method, stack[0], NULL);
4709 if (func == NULL) {
4710 return NULL;
4711 }
4712 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack+1, nargs-1,
4713 kwnames));
4714 Py_DECREF(func);
4715 }
4716 else {
4717 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4718 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004719 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004720 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004721 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004722 /* Optimize access to bound methods. Reuse the Python stack
4723 to pass 'self' as the first argument, replace 'func'
4724 with 'self'. It avoids the creation of a new temporary tuple
4725 for arguments (to replace func with self) when the method uses
4726 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004727 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004728 Py_INCREF(self);
4729 func = PyMethod_GET_FUNCTION(func);
4730 Py_INCREF(func);
4731 Py_SETREF(*pfunc, self);
4732 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004733 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004734 }
4735 else {
4736 Py_INCREF(func);
4737 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004738
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004739 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004740 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004741 }
4742 else {
4743 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4744 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004745 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004746 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004747
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004748 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4749
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004750 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 while ((*pp_stack) > pfunc) {
4752 w = EXT_POP(*pp_stack);
4753 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004754 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004756 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004757}
4758
Jeremy Hylton52820442001-01-03 23:52:36 +00004759static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004760do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004762 if (PyCFunction_Check(func)) {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004763 PyObject *result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004764 PyThreadState *tstate = PyThreadState_GET();
4765 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004766 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004768 else {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004769 return PyObject_Call(func, callargs, kwdict);
Victor Stinner74319ae2016-08-25 00:04:09 +02004770 }
Jeremy Hylton52820442001-01-03 23:52:36 +00004771}
4772
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004773/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004774 nb_index slot defined, and store in *pi.
4775 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004776 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004777 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004778*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004779int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004780_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004781{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004782 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004783 Py_ssize_t x;
4784 if (PyIndex_Check(v)) {
4785 x = PyNumber_AsSsize_t(v, NULL);
4786 if (x == -1 && PyErr_Occurred())
4787 return 0;
4788 }
4789 else {
4790 PyErr_SetString(PyExc_TypeError,
4791 "slice indices must be integers or "
4792 "None or have an __index__ method");
4793 return 0;
4794 }
4795 *pi = x;
4796 }
4797 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004798}
4799
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004800int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004801_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004802{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004803 Py_ssize_t x;
4804 if (PyIndex_Check(v)) {
4805 x = PyNumber_AsSsize_t(v, NULL);
4806 if (x == -1 && PyErr_Occurred())
4807 return 0;
4808 }
4809 else {
4810 PyErr_SetString(PyExc_TypeError,
4811 "slice indices must be integers or "
4812 "have an __index__ method");
4813 return 0;
4814 }
4815 *pi = x;
4816 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004817}
4818
4819
Guido van Rossum486364b2007-06-30 05:01:58 +00004820#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004821 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004822
Guido van Rossumb209a111997-04-29 18:18:01 +00004823static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004824cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004826 int res = 0;
4827 switch (op) {
4828 case PyCmp_IS:
4829 res = (v == w);
4830 break;
4831 case PyCmp_IS_NOT:
4832 res = (v != w);
4833 break;
4834 case PyCmp_IN:
4835 res = PySequence_Contains(w, v);
4836 if (res < 0)
4837 return NULL;
4838 break;
4839 case PyCmp_NOT_IN:
4840 res = PySequence_Contains(w, v);
4841 if (res < 0)
4842 return NULL;
4843 res = !res;
4844 break;
4845 case PyCmp_EXC_MATCH:
4846 if (PyTuple_Check(w)) {
4847 Py_ssize_t i, length;
4848 length = PyTuple_Size(w);
4849 for (i = 0; i < length; i += 1) {
4850 PyObject *exc = PyTuple_GET_ITEM(w, i);
4851 if (!PyExceptionClass_Check(exc)) {
4852 PyErr_SetString(PyExc_TypeError,
4853 CANNOT_CATCH_MSG);
4854 return NULL;
4855 }
4856 }
4857 }
4858 else {
4859 if (!PyExceptionClass_Check(w)) {
4860 PyErr_SetString(PyExc_TypeError,
4861 CANNOT_CATCH_MSG);
4862 return NULL;
4863 }
4864 }
4865 res = PyErr_GivenExceptionMatches(v, w);
4866 break;
4867 default:
4868 return PyObject_RichCompare(v, w, op);
4869 }
4870 v = res ? Py_True : Py_False;
4871 Py_INCREF(v);
4872 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004873}
4874
Thomas Wouters52152252000-08-17 22:55:00 +00004875static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004876import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4877{
4878 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004879 PyObject *import_func, *res;
4880 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004881
4882 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4883 if (import_func == NULL) {
4884 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4885 return NULL;
4886 }
4887
4888 /* Fast path for not overloaded __import__. */
4889 if (import_func == PyThreadState_GET()->interp->import_func) {
4890 int ilevel = _PyLong_AsInt(level);
4891 if (ilevel == -1 && PyErr_Occurred()) {
4892 return NULL;
4893 }
4894 res = PyImport_ImportModuleLevelObject(
4895 name,
4896 f->f_globals,
4897 f->f_locals == NULL ? Py_None : f->f_locals,
4898 fromlist,
4899 ilevel);
4900 return res;
4901 }
4902
4903 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004904
4905 stack[0] = name;
4906 stack[1] = f->f_globals;
4907 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4908 stack[3] = fromlist;
4909 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004910 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004911 Py_DECREF(import_func);
4912 return res;
4913}
4914
4915static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004916import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004918 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004919 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004920 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 x = PyObject_GetAttr(v, name);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004923 if (x != NULL || !PyErr_ExceptionMatches(PyExc_AttributeError))
4924 return x;
4925 /* Issue #17636: in case this failed because of a circular relative
4926 import, try to fallback on reading the module directly from
4927 sys.modules. */
4928 PyErr_Clear();
4929 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004930 if (pkgname == NULL) {
4931 goto error;
4932 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004933 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004934 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004935 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004936 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004937 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004938 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004939 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004940 if (x == NULL) {
4941 goto error;
4942 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004943 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004944 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004945 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004946 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004947 if (pkgname == NULL) {
4948 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4949 if (pkgname_or_unknown == NULL) {
4950 Py_XDECREF(pkgpath);
4951 return NULL;
4952 }
4953 } else {
4954 pkgname_or_unknown = pkgname;
4955 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004956
4957 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4958 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004959 errmsg = PyUnicode_FromFormat(
4960 "cannot import name %R from %R (unknown location)",
4961 name, pkgname_or_unknown
4962 );
4963 /* NULL check for errmsg done by PyErr_SetImportError. */
4964 PyErr_SetImportError(errmsg, pkgname, NULL);
4965 }
4966 else {
4967 errmsg = PyUnicode_FromFormat(
4968 "cannot import name %R from %R (%S)",
4969 name, pkgname_or_unknown, pkgpath
4970 );
4971 /* NULL check for errmsg done by PyErr_SetImportError. */
4972 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004973 }
4974
Xiang Zhang4830f582017-03-21 11:13:42 +08004975 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004976 Py_XDECREF(pkgname_or_unknown);
4977 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004978 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004979}
Guido van Rossumac7be682001-01-17 15:42:30 +00004980
Thomas Wouters52152252000-08-17 22:55:00 +00004981static int
4982import_all_from(PyObject *locals, PyObject *v)
4983{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004984 _Py_IDENTIFIER(__all__);
4985 _Py_IDENTIFIER(__dict__);
4986 PyObject *all = _PyObject_GetAttrId(v, &PyId___all__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004987 PyObject *dict, *name, *value;
4988 int skip_leading_underscores = 0;
4989 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004991 if (all == NULL) {
4992 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4993 return -1; /* Unexpected error */
4994 PyErr_Clear();
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004995 dict = _PyObject_GetAttrId(v, &PyId___dict__);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004996 if (dict == NULL) {
4997 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
4998 return -1;
4999 PyErr_SetString(PyExc_ImportError,
5000 "from-import-* object has no __dict__ and no __all__");
5001 return -1;
5002 }
5003 all = PyMapping_Keys(dict);
5004 Py_DECREF(dict);
5005 if (all == NULL)
5006 return -1;
5007 skip_leading_underscores = 1;
5008 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 for (pos = 0, err = 0; ; pos++) {
5011 name = PySequence_GetItem(all, pos);
5012 if (name == NULL) {
5013 if (!PyErr_ExceptionMatches(PyExc_IndexError))
5014 err = -1;
5015 else
5016 PyErr_Clear();
5017 break;
5018 }
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005019 if (skip_leading_underscores && PyUnicode_Check(name)) {
5020 if (PyUnicode_READY(name) == -1) {
5021 Py_DECREF(name);
5022 err = -1;
5023 break;
5024 }
5025 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5026 Py_DECREF(name);
5027 continue;
5028 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 }
5030 value = PyObject_GetAttr(v, name);
5031 if (value == NULL)
5032 err = -1;
5033 else if (PyDict_CheckExact(locals))
5034 err = PyDict_SetItem(locals, name, value);
5035 else
5036 err = PyObject_SetItem(locals, name, value);
5037 Py_DECREF(name);
5038 Py_XDECREF(value);
5039 if (err != 0)
5040 break;
5041 }
5042 Py_DECREF(all);
5043 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005044}
5045
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005046static int
5047check_args_iterable(PyObject *func, PyObject *args)
5048{
5049 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
5050 PyErr_Format(PyExc_TypeError,
5051 "%.200s%.200s argument after * "
5052 "must be an iterable, not %.200s",
5053 PyEval_GetFuncName(func),
5054 PyEval_GetFuncDesc(func),
5055 args->ob_type->tp_name);
5056 return -1;
5057 }
5058 return 0;
5059}
5060
5061static void
5062format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5063{
5064 PyErr_Format(PyExc_TypeError,
5065 "%.200s%.200s argument after ** "
5066 "must be a mapping, not %.200s",
5067 PyEval_GetFuncName(func),
5068 PyEval_GetFuncDesc(func),
5069 kwargs->ob_type->tp_name);
5070}
5071
Guido van Rossumac7be682001-01-17 15:42:30 +00005072static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005073format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005074{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005075 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 if (!obj)
5078 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005079
Serhiy Storchaka06515832016-11-20 09:13:07 +02005080 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 if (!obj_str)
5082 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005085}
Guido van Rossum950361c1997-01-24 13:49:28 +00005086
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005087static void
5088format_exc_unbound(PyCodeObject *co, int oparg)
5089{
5090 PyObject *name;
5091 /* Don't stomp existing exception */
5092 if (PyErr_Occurred())
5093 return;
5094 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5095 name = PyTuple_GET_ITEM(co->co_cellvars,
5096 oparg);
5097 format_exc_check_arg(
5098 PyExc_UnboundLocalError,
5099 UNBOUNDLOCAL_ERROR_MSG,
5100 name);
5101 } else {
5102 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5103 PyTuple_GET_SIZE(co->co_cellvars));
5104 format_exc_check_arg(PyExc_NameError,
5105 UNBOUNDFREE_ERROR_MSG, name);
5106 }
5107}
5108
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005109static PyObject *
5110unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005111 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005112{
5113 PyObject *res;
5114 if (Py_REFCNT(v) == 2) {
5115 /* In the common case, there are 2 references to the value
5116 * stored in 'variable' when the += is performed: one on the
5117 * value stack (in 'v') and one still stored in the
5118 * 'variable'. We try to delete the variable now to reduce
5119 * the refcnt to 1.
5120 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005121 int opcode, oparg;
5122 NEXTOPARG();
5123 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005124 case STORE_FAST:
5125 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005126 PyObject **fastlocals = f->f_localsplus;
5127 if (GETLOCAL(oparg) == v)
5128 SETLOCAL(oparg, NULL);
5129 break;
5130 }
5131 case STORE_DEREF:
5132 {
5133 PyObject **freevars = (f->f_localsplus +
5134 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005135 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005136 if (PyCell_GET(c) == v) {
5137 PyCell_SET(c, NULL);
5138 Py_DECREF(v);
5139 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005140 break;
5141 }
5142 case STORE_NAME:
5143 {
5144 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005145 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005146 PyObject *locals = f->f_locals;
5147 if (PyDict_CheckExact(locals) &&
5148 PyDict_GetItem(locals, name) == v) {
5149 if (PyDict_DelItem(locals, name) != 0) {
5150 PyErr_Clear();
5151 }
5152 }
5153 break;
5154 }
5155 }
5156 }
5157 res = v;
5158 PyUnicode_Append(&res, w);
5159 return res;
5160}
5161
Guido van Rossum950361c1997-01-24 13:49:28 +00005162#ifdef DYNAMIC_EXECUTION_PROFILE
5163
Skip Montanarof118cb12001-10-15 20:51:38 +00005164static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005165getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005166{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005167 int i;
5168 PyObject *l = PyList_New(256);
5169 if (l == NULL) return NULL;
5170 for (i = 0; i < 256; i++) {
5171 PyObject *x = PyLong_FromLong(a[i]);
5172 if (x == NULL) {
5173 Py_DECREF(l);
5174 return NULL;
5175 }
5176 PyList_SetItem(l, i, x);
5177 }
5178 for (i = 0; i < 256; i++)
5179 a[i] = 0;
5180 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005181}
5182
5183PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005184_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005185{
5186#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005187 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005188#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005189 int i;
5190 PyObject *l = PyList_New(257);
5191 if (l == NULL) return NULL;
5192 for (i = 0; i < 257; i++) {
5193 PyObject *x = getarray(dxpairs[i]);
5194 if (x == NULL) {
5195 Py_DECREF(l);
5196 return NULL;
5197 }
5198 PyList_SetItem(l, i, x);
5199 }
5200 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005201#endif
5202}
5203
5204#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005205
5206Py_ssize_t
5207_PyEval_RequestCodeExtraIndex(freefunc free)
5208{
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005209 PyInterpreterState *interp = PyThreadState_Get()->interp;
Brett Cannon5c4de282016-09-07 11:16:41 -07005210 Py_ssize_t new_index;
5211
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005212 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005213 return -1;
5214 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005215 new_index = interp->co_extra_user_count++;
5216 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005217 return new_index;
5218}
Łukasz Langaa785c872016-09-09 17:37:37 -07005219
5220static void
5221dtrace_function_entry(PyFrameObject *f)
5222{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005223 const char *filename;
5224 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005225 int lineno;
5226
5227 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5228 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5229 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5230
5231 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5232}
5233
5234static void
5235dtrace_function_return(PyFrameObject *f)
5236{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005237 const char *filename;
5238 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005239 int lineno;
5240
5241 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5242 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5243 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5244
5245 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5246}
5247
5248/* DTrace equivalent of maybe_call_line_trace. */
5249static void
5250maybe_dtrace_line(PyFrameObject *frame,
5251 int *instr_lb, int *instr_ub, int *instr_prev)
5252{
5253 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005254 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005255
5256 /* If the last instruction executed isn't in the current
5257 instruction window, reset the window.
5258 */
5259 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5260 PyAddrPair bounds;
5261 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5262 &bounds);
5263 *instr_lb = bounds.ap_lower;
5264 *instr_ub = bounds.ap_upper;
5265 }
5266 /* If the last instruction falls at the start of a line or if
5267 it represents a jump backwards, update the frame's line
5268 number and call the trace function. */
5269 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5270 frame->f_lineno = line;
5271 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5272 if (!co_filename)
5273 co_filename = "?";
5274 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5275 if (!co_name)
5276 co_name = "?";
5277 PyDTrace_LINE(co_filename, co_name, line);
5278 }
5279 *instr_prev = frame->f_lasti;
5280}