blob: ebe1c50e7bad235d9b514a3100ab2dd2629bfc38 [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"
Victor Stinnerbcda8f12018-11-21 22:27:47 +010013#include "pycore_object.h"
Victor Stinner621cebe2018-11-12 16:53:38 +010014#include "pycore_pystate.h"
Victor Stinnerec13b932018-11-25 23:56:17 +010015#include "pycore_tupleobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000016
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000017#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040018#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000019#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000020#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070021#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040022#include "setobject.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +000023#include "structmember.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000024
Guido van Rossumc6004111993-11-05 10:22:19 +000025#include <ctype.h>
26
Guido van Rossum408027e1996-12-30 16:17:54 +000027#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000028/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029#define LLTRACE 1 /* Low-level trace feature */
30#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#endif
32
Yury Selivanovf2392132016-12-13 19:03:51 -050033/* Private API for the LOAD_METHOD opcode. */
34extern int _PyObject_GetMethod(PyObject *, PyObject *, PyObject **);
35
Jeremy Hylton52820442001-01-03 23:52:36 +000036typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *);
Guido van Rossum5b722181993-03-30 17:46:03 +000037
Guido van Rossum374a9221991-04-04 10:40:29 +000038/* Forward declarations */
Eric Snow2ebc5ce2017-09-07 23:51:28 -060039Py_LOCAL_INLINE(PyObject *) call_function(PyObject ***, Py_ssize_t,
40 PyObject *);
Victor Stinnerf9b760f2016-09-09 10:17:08 -070041static PyObject * do_call_core(PyObject *, PyObject *, PyObject *);
Jeremy Hylton52820442001-01-03 23:52:36 +000042
Guido van Rossum0a066c01992-03-27 17:29:15 +000043#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000044static int lltrace;
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020045static int prtrace(PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000046#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010047static int call_trace(Py_tracefunc, PyObject *,
48 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000050static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010051 PyThreadState *, PyFrameObject *,
52 int, PyObject *);
53static void call_exc_trace(Py_tracefunc, PyObject *,
54 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000055static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060056 PyThreadState *, PyFrameObject *,
57 int *, int *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070058static void maybe_dtrace_line(PyFrameObject *, int *, int *, int *);
59static void dtrace_function_entry(PyFrameObject *);
60static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000061
Thomas Wouters477c8d52006-05-27 19:21:47 +000062static PyObject * cmp_outcome(int, PyObject *, PyObject *);
Eric Snow2ebc5ce2017-09-07 23:51:28 -060063static PyObject * import_name(PyFrameObject *, PyObject *, PyObject *,
64 PyObject *);
Thomas Wouters477c8d52006-05-27 19:21:47 +000065static PyObject * import_from(PyObject *, PyObject *);
Thomas Wouters52152252000-08-17 22:55:00 +000066static int import_all_from(PyObject *, PyObject *);
Neal Norwitzda059e32007-08-26 05:33:45 +000067static void format_exc_check_arg(PyObject *, const char *, PyObject *);
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +000068static void format_exc_unbound(PyCodeObject *co, int oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +020069static PyObject * unicode_concatenate(PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030070 PyFrameObject *, const _Py_CODEUNIT *);
Benjamin Petersonce798522012-01-22 11:24:29 -050071static PyObject * special_lookup(PyObject *, _Py_Identifier *);
Serhiy Storchaka25e4f772017-08-03 11:37:15 +030072static int check_args_iterable(PyObject *func, PyObject *vararg);
73static void format_kwargs_mapping_error(PyObject *func, PyObject *kwargs);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +030074static void format_awaitable_error(PyTypeObject *, int);
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();
Victor Stinner50b48572018-11-01 01:51:40 +0100162 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{
Victor Stinner50b48572018-11-01 01:51:40 +0100180 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000181 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.
Victor Stinner50b48572018-11-01 01:51:40 +0100190 We therefore avoid PyThreadState_Get() which dumps a fatal error
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191 in debug mode.
192 */
Victor Stinner50b48572018-11-01 01:51:40 +0100193 drop_gil(_PyThreadState_GET());
Guido van Rossum25ce5661997-08-02 03:10:38 +0000194}
195
196void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000197PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000198{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 if (tstate == NULL)
200 Py_FatalError("PyEval_AcquireThread: NULL new thread state");
201 /* Check someone has called PyEval_InitThreads() to create the lock */
202 assert(gil_created());
203 take_gil(tstate);
204 if (PyThreadState_Swap(tstate) != NULL)
205 Py_FatalError(
206 "PyEval_AcquireThread: non-NULL old thread state");
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000207}
208
209void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000210PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000211{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000212 if (tstate == NULL)
213 Py_FatalError("PyEval_ReleaseThread: NULL thread state");
214 if (PyThreadState_Swap(NULL) != tstate)
215 Py_FatalError("PyEval_ReleaseThread: wrong thread state");
216 drop_gil(tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000217}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000218
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200219/* This function is called from PyOS_AfterFork_Child to destroy all threads
220 * which are not running in the child process, and clear internal locks
221 * which might be held by those threads.
222 */
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000223
224void
225PyEval_ReInitThreads(void)
226{
Victor Stinner50b48572018-11-01 01:51:40 +0100227 PyThreadState *current_tstate = _PyThreadState_GET();
Jesse Nollera8513972008-07-17 16:49:17 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 if (!gil_created())
230 return;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 recreate_gil();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600232 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200233 take_gil(current_tstate);
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600234 _PyRuntime.ceval.pending.main_thread = PyThread_get_thread_ident();
Jesse Nollera8513972008-07-17 16:49:17 +0000235
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200236 /* Destroy all threads except the current one */
237 _PyThreadState_DeleteExcept(current_tstate);
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000238}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000239
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000240/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600241 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000242
243void
244_PyEval_SignalAsyncExc(void)
245{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 SIGNAL_ASYNC_EXC();
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000247}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000248
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000249PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000250PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000251{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 PyThreadState *tstate = PyThreadState_Swap(NULL);
253 if (tstate == NULL)
254 Py_FatalError("PyEval_SaveThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100255 assert(gil_created());
256 drop_gil(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000258}
259
260void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000261PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000262{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 if (tstate == NULL)
264 Py_FatalError("PyEval_RestoreThread: NULL tstate");
Victor Stinner2914bb32018-01-29 11:57:45 +0100265 assert(gil_created());
266
267 int err = errno;
268 take_gil(tstate);
269 /* _Py_Finalizing is protected by the GIL */
270 if (_Py_IsFinalizing() && !_Py_CURRENTLY_FINALIZING(tstate)) {
271 drop_gil(tstate);
272 PyThread_exit_thread();
273 Py_UNREACHABLE();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 }
Victor Stinner2914bb32018-01-29 11:57:45 +0100275 errno = err;
276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 PyThreadState_Swap(tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000278}
279
280
Guido van Rossuma9672091994-09-14 13:31:22 +0000281/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
282 signal handlers or Mac I/O completion routines) can schedule calls
283 to a function to be called synchronously.
284 The synchronous function is called with one void* argument.
285 It should return 0 for success or -1 for failure -- failure should
286 be accompanied by an exception.
287
288 If registry succeeds, the registry function returns 0; if it fails
289 (e.g. due to too many pending calls) it returns -1 (without setting
290 an exception condition).
291
292 Note that because registry may occur from within signal handlers,
293 or other asynchronous events, calling malloc() is unsafe!
294
Guido van Rossuma9672091994-09-14 13:31:22 +0000295 Any thread can schedule pending calls, but only the main thread
296 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000297 There is no facility to schedule calls to a particular thread, but
298 that should be easy to change, should that ever be required. In
299 that case, the static variables here should go into the python
300 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000301*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000302
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200303void
304_PyEval_SignalReceived(void)
305{
306 /* bpo-30703: Function called when the C signal handler of Python gets a
307 signal. We cannot queue a callback using Py_AddPendingCall() since
308 that function is not async-signal-safe. */
309 SIGNAL_PENDING_CALLS();
310}
311
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200312/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000313 scheduling to be made from any thread, and even from an executing
314 callback.
315 */
316
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000317int
318Py_AddPendingCall(int (*func)(void *), void *arg)
319{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 int i, j, result=0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600321 PyThread_type_lock lock = _PyRuntime.ceval.pending.lock;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000322
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000323 /* try a few times for the lock. Since this mechanism is used
324 * for signal handling (on the main thread), there is a (slim)
325 * chance that a signal is delivered on the same thread while we
326 * hold the lock during the Py_MakePendingCalls() function.
327 * This avoids a deadlock in that case.
328 * Note that signals can be delivered on any thread. In particular,
329 * on Windows, a SIGINT is delivered on a system-created worker
330 * thread.
331 * We also check for lock being NULL, in the unlikely case that
332 * this function is called before any bytecode evaluation takes place.
333 */
334 if (lock != NULL) {
335 for (i = 0; i<100; i++) {
336 if (PyThread_acquire_lock(lock, NOWAIT_LOCK))
337 break;
338 }
339 if (i == 100)
340 return -1;
341 }
342
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600343 i = _PyRuntime.ceval.pending.last;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 j = (i + 1) % NPENDINGCALLS;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600345 if (j == _PyRuntime.ceval.pending.first) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 result = -1; /* Queue full */
347 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600348 _PyRuntime.ceval.pending.calls[i].func = func;
349 _PyRuntime.ceval.pending.calls[i].arg = arg;
350 _PyRuntime.ceval.pending.last = j;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 }
352 /* signal main loop */
353 SIGNAL_PENDING_CALLS();
354 if (lock != NULL)
355 PyThread_release_lock(lock);
356 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000357}
358
359int
360Py_MakePendingCalls(void)
361{
Charles-François Natalif23339a2011-07-23 18:15:43 +0200362 static int busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 int i;
364 int r = 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000365
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200366 assert(PyGILState_Check());
367
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600368 if (!_PyRuntime.ceval.pending.lock) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 /* initial allocation of the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600370 _PyRuntime.ceval.pending.lock = PyThread_allocate_lock();
371 if (_PyRuntime.ceval.pending.lock == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 return -1;
373 }
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 /* only service pending calls on main thread */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600376 if (_PyRuntime.ceval.pending.main_thread &&
377 PyThread_get_thread_ident() != _PyRuntime.ceval.pending.main_thread)
378 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 return 0;
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600380 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 /* don't perform recursive pending calls */
Charles-François Natalif23339a2011-07-23 18:15:43 +0200382 if (busy)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 return 0;
Charles-François Natalif23339a2011-07-23 18:15:43 +0200384 busy = 1;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200385 /* unsignal before starting to call callbacks, so that any callback
386 added in-between re-signals */
387 UNSIGNAL_PENDING_CALLS();
388
389 /* Python signal handler doesn't really queue a callback: it only signals
390 that a signal was received, see _PyEval_SignalReceived(). */
391 if (PyErr_CheckSignals() < 0) {
392 goto error;
393 }
394
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 /* perform a bounded number of calls, in case of recursion */
396 for (i=0; i<NPENDINGCALLS; i++) {
397 int j;
398 int (*func)(void *);
399 void *arg = NULL;
400
401 /* pop one item off the queue while holding the lock */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600402 PyThread_acquire_lock(_PyRuntime.ceval.pending.lock, WAIT_LOCK);
403 j = _PyRuntime.ceval.pending.first;
404 if (j == _PyRuntime.ceval.pending.last) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 func = NULL; /* Queue empty */
406 } else {
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600407 func = _PyRuntime.ceval.pending.calls[j].func;
408 arg = _PyRuntime.ceval.pending.calls[j].arg;
409 _PyRuntime.ceval.pending.first = (j + 1) % NPENDINGCALLS;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000410 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600411 PyThread_release_lock(_PyRuntime.ceval.pending.lock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000412 /* having released the lock, perform the callback */
413 if (func == NULL)
414 break;
415 r = func(arg);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200416 if (r) {
417 goto error;
418 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200420
Charles-François Natalif23339a2011-07-23 18:15:43 +0200421 busy = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000422 return r;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200423
424error:
425 busy = 0;
426 SIGNAL_PENDING_CALLS(); /* We're not done yet */
427 return -1;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000428}
429
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000430/* The interpreter's recursion limit */
431
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000432#ifndef Py_DEFAULT_RECURSION_LIMIT
433#define Py_DEFAULT_RECURSION_LIMIT 1000
434#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600435
Eric Snow05351c12017-09-05 21:43:08 -0700436int _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000437
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600438void
439_PyEval_Initialize(struct _ceval_runtime_state *state)
440{
441 state->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
442 _Py_CheckRecursionLimit = Py_DEFAULT_RECURSION_LIMIT;
443 _gil_initialize(&state->gil);
444}
445
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000446int
447Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000448{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600449 return _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000450}
451
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000452void
453Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000454{
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600455 _PyRuntime.ceval.recursion_limit = new_limit;
456 _Py_CheckRecursionLimit = _PyRuntime.ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000457}
458
Armin Rigo2b3eb402003-10-28 12:05:48 +0000459/* the macro Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
460 if the recursion_depth reaches _Py_CheckRecursionLimit.
461 If USE_STACKCHECK, the macro decrements _Py_CheckRecursionLimit
462 to guarantee that _Py_CheckRecursiveCall() is regularly called.
463 Without USE_STACKCHECK, there is no need for this. */
464int
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +0300465_Py_CheckRecursiveCall(const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000466{
Victor Stinner50b48572018-11-01 01:51:40 +0100467 PyThreadState *tstate = _PyThreadState_GET();
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600468 int recursion_limit = _PyRuntime.ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000469
470#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700471 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (PyOS_CheckStack()) {
473 --tstate->recursion_depth;
474 PyErr_SetString(PyExc_MemoryError, "Stack overflow");
475 return -1;
476 }
pdox18967932017-10-25 23:03:01 -0700477 /* Needed for ABI backwards-compatibility (see bpo-31857) */
Eric Snow05351c12017-09-05 21:43:08 -0700478 _Py_CheckRecursionLimit = recursion_limit;
pdox18967932017-10-25 23:03:01 -0700479#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 if (tstate->recursion_critical)
481 /* Somebody asked that we don't check for recursion. */
482 return 0;
483 if (tstate->overflowed) {
484 if (tstate->recursion_depth > recursion_limit + 50) {
485 /* Overflowing while handling an overflow. Give up. */
486 Py_FatalError("Cannot recover from stack overflow.");
487 }
488 return 0;
489 }
490 if (tstate->recursion_depth > recursion_limit) {
491 --tstate->recursion_depth;
492 tstate->overflowed = 1;
Yury Selivanovf488fb42015-07-03 01:04:23 -0400493 PyErr_Format(PyExc_RecursionError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 "maximum recursion depth exceeded%s",
495 where);
496 return -1;
497 }
498 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000499}
500
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400501static int do_raise(PyObject *, PyObject *);
Guido van Rossum0368b722007-05-11 16:50:42 +0000502static int unpack_iterable(PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000503
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600504#define _Py_TracingPossible _PyRuntime.ceval.tracing_possible
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000505
Guido van Rossum374a9221991-04-04 10:40:29 +0000506
Guido van Rossumb209a111997-04-29 18:18:01 +0000507PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000508PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000509{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 return PyEval_EvalCodeEx(co,
511 globals, locals,
512 (PyObject **)NULL, 0,
513 (PyObject **)NULL, 0,
514 (PyObject **)NULL, 0,
515 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000516}
517
518
519/* Interpreter main loop */
520
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000521PyObject *
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000522PyEval_EvalFrame(PyFrameObject *f) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 /* This is for backward compatibility with extension modules that
524 used this API; core interpreter code should call
525 PyEval_EvalFrameEx() */
526 return PyEval_EvalFrameEx(f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000527}
528
529PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000531{
Victor Stinnercaba55b2018-08-03 15:33:52 +0200532 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
533 return interp->eval_frame(f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700534}
535
Victor Stinnerc6944e72016-11-11 02:13:35 +0100536PyObject* _Py_HOT_FUNCTION
Brett Cannon3cebf932016-09-05 15:33:46 -0700537_PyEval_EvalFrameDefault(PyFrameObject *f, int throwflag)
538{
Guido van Rossum950361c1997-01-24 13:49:28 +0000539#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000541#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200542 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300543 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200544 int opcode; /* Current opcode */
545 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200546 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PyObject *retval = NULL; /* Return value */
Victor Stinner50b48572018-11-01 01:51:40 +0100548 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000552
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000554
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 is true when the line being executed has changed. The
556 initial values are such as to make this false the first
557 time it is tested. */
558 int instr_ub = -1, instr_lb = 0, instr_prev = -1;
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000559
Serhiy Storchakaab874002016-09-11 13:48:15 +0300560 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561 PyObject *names;
562 PyObject *consts;
Guido van Rossum374a9221991-04-04 10:40:29 +0000563
Brett Cannon368b4b72012-04-02 12:17:59 -0400564#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200565 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -0400566#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +0200567
Antoine Pitroub52ec782009-01-25 16:34:23 +0000568/* Computed GOTOs, or
569 the-optimization-commonly-but-improperly-known-as-"threaded code"
570 using gcc's labels-as-values extension
571 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
572
573 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +0000575 combined with a lookup table of jump addresses. However, since the
576 indirect jump instruction is shared by all opcodes, the CPU will have a
577 hard time making the right prediction for where to jump next (actually,
578 it will be always wrong except in the uncommon case of a sequence of
579 several identical opcodes).
580
581 "Threaded code" in contrast, uses an explicit jump table and an explicit
582 indirect jump instruction at the end of each opcode. Since the jump
583 instruction is at a different address for each opcode, the CPU will make a
584 separate prediction for each of these instructions, which is equivalent to
585 predicting the second opcode of each opcode pair. These predictions have
586 a much better chance to turn out valid, especially in small bytecode loops.
587
588 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +0000590 and potentially many more instructions (depending on the pipeline width).
591 A correctly predicted branch, however, is nearly free.
592
593 At the time of this writing, the "threaded code" version is up to 15-20%
594 faster than the normal "switch" version, depending on the compiler and the
595 CPU architecture.
596
597 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
598 because it would render the measurements invalid.
599
600
601 NOTE: care must be taken that the compiler doesn't try to "optimize" the
602 indirect jumps by sharing them between all opcodes. Such optimizations
603 can be disabled on gcc by using the -fno-gcse flag (or possibly
604 -fno-crossjumping).
605*/
606
Antoine Pitrou042b1282010-08-13 21:15:58 +0000607#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +0000608#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +0000609#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +0000610#endif
611
Antoine Pitrou042b1282010-08-13 21:15:58 +0000612#ifdef HAVE_COMPUTED_GOTOS
613 #ifndef USE_COMPUTED_GOTOS
614 #define USE_COMPUTED_GOTOS 1
615 #endif
616#else
617 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
618 #error "Computed gotos are not supported on this compiler."
619 #endif
620 #undef USE_COMPUTED_GOTOS
621 #define USE_COMPUTED_GOTOS 0
622#endif
623
624#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +0000625/* Import the static jump table */
626#include "opcode_targets.h"
627
Antoine Pitroub52ec782009-01-25 16:34:23 +0000628#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -0700629 op: \
630 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +0000631
Antoine Pitroub52ec782009-01-25 16:34:23 +0000632#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000633 { \
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600634 if (!_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000635 FAST_DISPATCH(); \
636 } \
637 continue; \
638 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000639
640#ifdef LLTRACE
641#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700643 if (!lltrace && !_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000644 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300645 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300646 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 } \
648 goto fast_next_opcode; \
649 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000650#else
651#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 { \
Łukasz Langaa785c872016-09-09 17:37:37 -0700653 if (!_Py_TracingPossible && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300655 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300656 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000657 } \
658 goto fast_next_opcode; \
659 }
Antoine Pitroub52ec782009-01-25 16:34:23 +0000660#endif
661
662#else
Benjamin Petersonddd19492018-09-16 22:38:02 -0700663#define TARGET(op) op
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300664
Antoine Pitroub52ec782009-01-25 16:34:23 +0000665#define DISPATCH() continue
666#define FAST_DISPATCH() goto fast_next_opcode
667#endif
668
669
Neal Norwitza81d2202002-07-14 00:27:26 +0000670/* Tuple access macros */
671
672#ifndef Py_DEBUG
673#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
674#else
675#define GETITEM(v, i) PyTuple_GetItem((v), (i))
676#endif
677
Guido van Rossum374a9221991-04-04 10:40:29 +0000678/* Code access macros */
679
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300680/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600681#define INSTR_OFFSET() \
682 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300683#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300684 _Py_CODEUNIT word = *next_instr; \
685 opcode = _Py_OPCODE(word); \
686 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300687 next_instr++; \
688 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +0300689#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
690#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +0000691
Raymond Hettingerf606f872003-03-16 03:11:04 +0000692/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 Some opcodes tend to come in pairs thus making it possible to
694 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +0300695 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 Verifying the prediction costs a single high-speed test of a register
698 variable against a constant. If the pairing was good, then the
699 processor's own internal branch predication has a high likelihood of
700 success, resulting in a nearly zero-overhead transition to the
701 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300702 including its unpredictable switch-case branch. Combined with the
703 processor's internal branch prediction, a successful PREDICT has the
704 effect of making the two opcodes run as if they were a single new opcode
705 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +0000706
Georg Brandl86b2fb92008-07-16 03:43:04 +0000707 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 predictions turned-on and interpret the results as if some opcodes
709 had been combined or turn-off predictions so that the opcode frequency
710 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000711
712 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000713 the CPU to record separate branch prediction information for each
714 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +0000715
Raymond Hettingerf606f872003-03-16 03:11:04 +0000716*/
717
Antoine Pitrou042b1282010-08-13 21:15:58 +0000718#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719#define PREDICT(op) if (0) goto PRED_##op
Raymond Hettingera7216982004-02-08 19:59:27 +0000720#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300721#define PREDICT(op) \
722 do{ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300723 _Py_CODEUNIT word = *next_instr; \
724 opcode = _Py_OPCODE(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300725 if (opcode == op){ \
Serhiy Storchakaab874002016-09-11 13:48:15 +0300726 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300727 next_instr++; \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300728 goto PRED_##op; \
729 } \
730 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +0000731#endif
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300732#define PREDICTED(op) PRED_##op:
Antoine Pitroub52ec782009-01-25 16:34:23 +0000733
Raymond Hettingerf606f872003-03-16 03:11:04 +0000734
Guido van Rossum374a9221991-04-04 10:40:29 +0000735/* Stack manipulation macros */
736
Martin v. Löwis18e16552006-02-15 17:27:45 +0000737/* The stack can grow at most MAXINT deep, as co_nlocals and
738 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +0000739#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
740#define EMPTY() (STACK_LEVEL() == 0)
741#define TOP() (stack_pointer[-1])
742#define SECOND() (stack_pointer[-2])
743#define THIRD() (stack_pointer[-3])
744#define FOURTH() (stack_pointer[-4])
745#define PEEK(n) (stack_pointer[-(n)])
746#define SET_TOP(v) (stack_pointer[-1] = (v))
747#define SET_SECOND(v) (stack_pointer[-2] = (v))
748#define SET_THIRD(v) (stack_pointer[-3] = (v))
749#define SET_FOURTH(v) (stack_pointer[-4] = (v))
750#define SET_VALUE(n, v) (stack_pointer[-(n)] = (v))
751#define BASIC_STACKADJ(n) (stack_pointer += n)
752#define BASIC_PUSH(v) (*stack_pointer++ = (v))
753#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +0000754
Guido van Rossum96a42c81992-01-12 02:29:51 +0000755#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756#define PUSH(v) { (void)(BASIC_PUSH(v), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000757 lltrace && prtrace(TOP(), "push")); \
758 assert(STACK_LEVEL() <= co->co_stacksize); }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759#define POP() ((void)(lltrace && prtrace(TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000760 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +0000761#define STACK_GROW(n) do { \
762 assert(n >= 0); \
763 (void)(BASIC_STACKADJ(n), \
Stefan Krahb7e10102010-06-23 18:42:39 +0000764 lltrace && prtrace(TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +0000765 assert(STACK_LEVEL() <= co->co_stacksize); \
766 } while (0)
767#define STACK_SHRINK(n) do { \
768 assert(n >= 0); \
769 (void)(lltrace && prtrace(TOP(), "stackadj")); \
770 (void)(BASIC_STACKADJ(-n)); \
771 assert(STACK_LEVEL() <= co->co_stacksize); \
772 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +0000773#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Stefan Krahb7e10102010-06-23 18:42:39 +0000774 prtrace((STACK_POINTER)[-1], "ext_pop")), \
775 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000776#else
Stefan Krahb7e10102010-06-23 18:42:39 +0000777#define PUSH(v) BASIC_PUSH(v)
778#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +0000779#define STACK_GROW(n) BASIC_STACKADJ(n)
780#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +0000781#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +0000782#endif
783
Guido van Rossum681d79a1995-07-18 14:51:37 +0000784/* Local variable macros */
785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +0000787
788/* The SETLOCAL() macro must not DECREF the local variable in-place and
789 then store the new value; it must copy the old value to a temporary
790 value, then store the new value, and then DECREF the temporary value.
791 This is because it is possible that during the DECREF the frame is
792 accessed by other code (e.g. a __del__ method or gc.collect()) and the
793 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +0000795 GETLOCAL(i) = value; \
796 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000797
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000798
799#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000800 while (STACK_LEVEL() > (b)->b_level) { \
801 PyObject *v = POP(); \
802 Py_XDECREF(v); \
803 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000804
805#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300806 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +0100808 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 assert(STACK_LEVEL() >= (b)->b_level + 3); \
810 while (STACK_LEVEL() > (b)->b_level + 3) { \
811 value = POP(); \
812 Py_XDECREF(value); \
813 } \
Mark Shannonae3087c2017-10-22 22:41:51 +0100814 exc_info = tstate->exc_info; \
815 type = exc_info->exc_type; \
816 value = exc_info->exc_value; \
817 traceback = exc_info->exc_traceback; \
818 exc_info->exc_type = POP(); \
819 exc_info->exc_value = POP(); \
820 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 Py_XDECREF(type); \
822 Py_XDECREF(value); \
823 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300824 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000825
Guido van Rossuma027efa1997-05-05 20:56:21 +0000826/* Start of code */
827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 /* push frame */
829 if (Py_EnterRecursiveCall(""))
830 return NULL;
Guido van Rossum8861b741996-07-30 16:49:37 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 if (tstate->use_tracing) {
835 if (tstate->c_tracefunc != NULL) {
836 /* tstate->c_tracefunc, if defined, is a
837 function that will be called on *every* entry
838 to a code block. Its return value, if not
839 None, is a function that will be called at
840 the start of each executed line of code.
841 (Actually, the function must return itself
842 in order to continue tracing.) The trace
843 functions are called with three arguments:
844 a pointer to the current frame, a string
845 indicating why the function is called, and
846 an argument which depends on the situation.
847 The global trace function is also called
848 whenever an exception is detected. */
849 if (call_trace_protected(tstate->c_tracefunc,
850 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100851 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 /* Trace function raised an error */
853 goto exit_eval_frame;
854 }
855 }
856 if (tstate->c_profilefunc != NULL) {
857 /* Similar for c_profilefunc, except it needn't
858 return itself and isn't called for "line" events */
859 if (call_trace_protected(tstate->c_profilefunc,
860 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +0100861 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 /* Profile function raised an error */
863 goto exit_eval_frame;
864 }
865 }
866 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +0000867
Łukasz Langaa785c872016-09-09 17:37:37 -0700868 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
869 dtrace_function_entry(f);
870
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000871 co = f->f_code;
872 names = co->co_names;
873 consts = co->co_consts;
874 fastlocals = f->f_localsplus;
875 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +0300876 assert(PyBytes_Check(co->co_code));
877 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +0300878 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
879 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
880 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300881 /*
882 f->f_lasti refers to the index of the last instruction,
883 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000884
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300885 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500886 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 When the PREDICT() macros are enabled, some opcode pairs follow in
889 direct succession without updating f->f_lasti. A successful
890 prediction effectively links the two codes together as if they
891 were a single new opcode; accordingly,f->f_lasti will point to
892 the first code in the pair (for instance, GET_ITER followed by
893 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300894 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300896 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300897 next_instr = first_instr;
898 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +0300899 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
900 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +0300901 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000902 stack_pointer = f->f_stacktop;
903 assert(stack_pointer != NULL);
904 f->f_stacktop = NULL; /* remains NULL unless yield suspends frame */
Antoine Pitrou58720d62013-08-05 23:26:40 +0200905 f->f_executing = 1;
Michael W. Hudson019a78e2002-11-08 12:53:11 +0000906
Benjamin Petersoneec3d712008-06-11 15:59:43 +0000907
Tim Peters5ca576e2001-06-18 22:08:13 +0000908#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +0200909 lltrace = _PyDict_GetItemId(f->f_globals, &PyId___ltrace__) != NULL;
Tim Peters5ca576e2001-06-18 22:08:13 +0000910#endif
Guido van Rossumac7be682001-01-17 15:42:30 +0000911
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400912 if (throwflag) /* support for generator.throw() */
913 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000914
Victor Stinnerace47d72013-07-18 01:41:08 +0200915#ifdef Py_DEBUG
916 /* PyEval_EvalFrameEx() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +0100917 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +0000918 caller loses its exception */
Victor Stinnerace47d72013-07-18 01:41:08 +0200919 assert(!PyErr_Occurred());
920#endif
921
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200922main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 assert(stack_pointer >= f->f_valuestack); /* else underflow */
925 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinnerace47d72013-07-18 01:41:08 +0200926 assert(!PyErr_Occurred());
Michael W. Hudsondd32a912002-08-15 14:59:02 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 /* Do periodic things. Doing this every time through
929 the loop would add too much overhead, so we do it
930 only every Nth instruction. We also do it if
931 ``pendingcalls_to_do'' is set, i.e. when an asynchronous
932 event needs attention (e.g. a signal handler or
933 async I/O handler); see Py_AddPendingCall() and
934 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +0000935
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600936 if (_Py_atomic_load_relaxed(&_PyRuntime.ceval.eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300937 opcode = _Py_OPCODE(*next_instr);
938 if (opcode == SETUP_FINALLY ||
939 opcode == SETUP_WITH ||
940 opcode == BEFORE_ASYNC_WITH ||
941 opcode == YIELD_FROM) {
942 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700943 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +0300944 - If we're about to enter the 'with:'. It will prevent
945 emitting a resource warning in the common idiom
946 'with open(path) as file:'.
947 - If we're about to enter the 'async with:'.
948 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -0700949 *very* useful, but might help in some cases and it's
950 traditional)
951 - If we're resuming a chain of nested 'yield from' or
952 'await' calls, then each frame is parked with YIELD_FROM
953 as its next opcode. If the user hit control-C we want to
954 wait until we've reached the innermost frame before
955 running the signal handler and raising KeyboardInterrupt
956 (see bpo-30039).
957 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 goto fast_next_opcode;
959 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600960 if (_Py_atomic_load_relaxed(
961 &_PyRuntime.ceval.pending.calls_to_do))
962 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400963 if (Py_MakePendingCalls() < 0)
964 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600966 if (_Py_atomic_load_relaxed(
967 &_PyRuntime.ceval.gil_drop_request))
968 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000969 /* Give another thread a chance */
970 if (PyThreadState_Swap(NULL) != tstate)
971 Py_FatalError("ceval: tstate mix-up");
972 drop_gil(tstate);
973
974 /* Other threads may run now */
975
976 take_gil(tstate);
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700977
978 /* Check if we should make a quick exit. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600979 if (_Py_IsFinalizing() &&
980 !_Py_CURRENTLY_FINALIZING(tstate))
981 {
Benjamin Peterson17548dd2014-06-16 22:59:07 -0700982 drop_gil(tstate);
983 PyThread_exit_thread();
984 }
985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 if (PyThreadState_Swap(tstate) != NULL)
987 Py_FatalError("ceval: orphan tstate");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000988 }
989 /* Check for asynchronous exceptions. */
990 if (tstate->async_exc != NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400991 PyObject *exc = tstate->async_exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 tstate->async_exc = NULL;
993 UNSIGNAL_ASYNC_EXC();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -0400994 PyErr_SetNone(exc);
995 Py_DECREF(exc);
996 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 }
998 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000999
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 fast_next_opcode:
1001 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001002
Łukasz Langaa785c872016-09-09 17:37:37 -07001003 if (PyDTrace_LINE_ENABLED())
1004 maybe_dtrace_line(f, &instr_lb, &instr_ub, &instr_prev);
1005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001007
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 if (_Py_TracingPossible &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001009 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001010 int err;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 /* see maybe_call_line_trace
1012 for expository comments */
1013 f->f_stacktop = stack_pointer;
Tim Peters8a5c3c72004-04-05 19:36:21 +00001014
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 err = maybe_call_line_trace(tstate->c_tracefunc,
1016 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001017 tstate, f,
1018 &instr_lb, &instr_ub, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 /* Reload possibly changed frame fields */
1020 JUMPTO(f->f_lasti);
1021 if (f->f_stacktop != NULL) {
1022 stack_pointer = f->f_stacktop;
1023 f->f_stacktop = NULL;
1024 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001025 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001027 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001031
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001032 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001033 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001034#ifdef DYNAMIC_EXECUTION_PROFILE
1035#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 dxpairs[lastopcode][opcode]++;
1037 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001038#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001040#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001041
Guido van Rossum96a42c81992-01-12 02:29:51 +00001042#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001045 if (lltrace) {
1046 if (HAS_ARG(opcode)) {
1047 printf("%d: %d, %d\n",
1048 f->f_lasti, opcode, oparg);
1049 }
1050 else {
1051 printf("%d: %d\n",
1052 f->f_lasti, opcode);
1053 }
1054 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001055#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001056
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001060 It is essential that any operation that fails must goto error
1061 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001062
Benjamin Petersonddd19492018-09-16 22:38:02 -07001063 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001065 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001066
Benjamin Petersonddd19492018-09-16 22:38:02 -07001067 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001068 PyObject *value = GETLOCAL(oparg);
1069 if (value == NULL) {
1070 format_exc_check_arg(PyExc_UnboundLocalError,
1071 UNBOUNDLOCAL_ERROR_MSG,
1072 PyTuple_GetItem(co->co_varnames, oparg));
1073 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001075 Py_INCREF(value);
1076 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001078 }
1079
Benjamin Petersonddd19492018-09-16 22:38:02 -07001080 case TARGET(LOAD_CONST): {
1081 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001082 PyObject *value = GETITEM(consts, oparg);
1083 Py_INCREF(value);
1084 PUSH(value);
1085 FAST_DISPATCH();
1086 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001087
Benjamin Petersonddd19492018-09-16 22:38:02 -07001088 case TARGET(STORE_FAST): {
1089 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001090 PyObject *value = POP();
1091 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001092 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001093 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001094
Benjamin Petersonddd19492018-09-16 22:38:02 -07001095 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001096 PyObject *value = POP();
1097 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001098 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001099 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001100
Benjamin Petersonddd19492018-09-16 22:38:02 -07001101 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001102 PyObject *top = TOP();
1103 PyObject *second = SECOND();
1104 SET_TOP(second);
1105 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001107 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001108
Benjamin Petersonddd19492018-09-16 22:38:02 -07001109 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001110 PyObject *top = TOP();
1111 PyObject *second = SECOND();
1112 PyObject *third = THIRD();
1113 SET_TOP(second);
1114 SET_SECOND(third);
1115 SET_THIRD(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 Petersonddd19492018-09-16 22:38:02 -07001119 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001120 PyObject *top = TOP();
1121 PyObject *second = SECOND();
1122 PyObject *third = THIRD();
1123 PyObject *fourth = FOURTH();
1124 SET_TOP(second);
1125 SET_SECOND(third);
1126 SET_THIRD(fourth);
1127 SET_FOURTH(top);
1128 FAST_DISPATCH();
1129 }
1130
Benjamin Petersonddd19492018-09-16 22:38:02 -07001131 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001132 PyObject *top = TOP();
1133 Py_INCREF(top);
1134 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001136 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001137
Benjamin Petersonddd19492018-09-16 22:38:02 -07001138 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001139 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001140 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001141 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001142 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001143 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001144 SET_TOP(top);
1145 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001146 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001147 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001148
Benjamin Petersonddd19492018-09-16 22:38:02 -07001149 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001150 PyObject *value = TOP();
1151 PyObject *res = PyNumber_Positive(value);
1152 Py_DECREF(value);
1153 SET_TOP(res);
1154 if (res == NULL)
1155 goto error;
1156 DISPATCH();
1157 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001158
Benjamin Petersonddd19492018-09-16 22:38:02 -07001159 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001160 PyObject *value = TOP();
1161 PyObject *res = PyNumber_Negative(value);
1162 Py_DECREF(value);
1163 SET_TOP(res);
1164 if (res == NULL)
1165 goto error;
1166 DISPATCH();
1167 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001168
Benjamin Petersonddd19492018-09-16 22:38:02 -07001169 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001170 PyObject *value = TOP();
1171 int err = PyObject_IsTrue(value);
1172 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 if (err == 0) {
1174 Py_INCREF(Py_True);
1175 SET_TOP(Py_True);
1176 DISPATCH();
1177 }
1178 else if (err > 0) {
1179 Py_INCREF(Py_False);
1180 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 DISPATCH();
1182 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001183 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001184 goto error;
1185 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001186
Benjamin Petersonddd19492018-09-16 22:38:02 -07001187 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001188 PyObject *value = TOP();
1189 PyObject *res = PyNumber_Invert(value);
1190 Py_DECREF(value);
1191 SET_TOP(res);
1192 if (res == NULL)
1193 goto error;
1194 DISPATCH();
1195 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001196
Benjamin Petersonddd19492018-09-16 22:38:02 -07001197 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001198 PyObject *exp = POP();
1199 PyObject *base = TOP();
1200 PyObject *res = PyNumber_Power(base, exp, Py_None);
1201 Py_DECREF(base);
1202 Py_DECREF(exp);
1203 SET_TOP(res);
1204 if (res == NULL)
1205 goto error;
1206 DISPATCH();
1207 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001208
Benjamin Petersonddd19492018-09-16 22:38:02 -07001209 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001210 PyObject *right = POP();
1211 PyObject *left = TOP();
1212 PyObject *res = PyNumber_Multiply(left, right);
1213 Py_DECREF(left);
1214 Py_DECREF(right);
1215 SET_TOP(res);
1216 if (res == NULL)
1217 goto error;
1218 DISPATCH();
1219 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001220
Benjamin Petersonddd19492018-09-16 22:38:02 -07001221 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001222 PyObject *right = POP();
1223 PyObject *left = TOP();
1224 PyObject *res = PyNumber_MatrixMultiply(left, right);
1225 Py_DECREF(left);
1226 Py_DECREF(right);
1227 SET_TOP(res);
1228 if (res == NULL)
1229 goto error;
1230 DISPATCH();
1231 }
1232
Benjamin Petersonddd19492018-09-16 22:38:02 -07001233 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001234 PyObject *divisor = POP();
1235 PyObject *dividend = TOP();
1236 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1237 Py_DECREF(dividend);
1238 Py_DECREF(divisor);
1239 SET_TOP(quotient);
1240 if (quotient == NULL)
1241 goto error;
1242 DISPATCH();
1243 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001244
Benjamin Petersonddd19492018-09-16 22:38:02 -07001245 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001246 PyObject *divisor = POP();
1247 PyObject *dividend = TOP();
1248 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1249 Py_DECREF(dividend);
1250 Py_DECREF(divisor);
1251 SET_TOP(quotient);
1252 if (quotient == NULL)
1253 goto error;
1254 DISPATCH();
1255 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001256
Benjamin Petersonddd19492018-09-16 22:38:02 -07001257 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001258 PyObject *divisor = POP();
1259 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001260 PyObject *res;
1261 if (PyUnicode_CheckExact(dividend) && (
1262 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1263 // fast path; string formatting, but not if the RHS is a str subclass
1264 // (see issue28598)
1265 res = PyUnicode_Format(dividend, divisor);
1266 } else {
1267 res = PyNumber_Remainder(dividend, divisor);
1268 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001269 Py_DECREF(divisor);
1270 Py_DECREF(dividend);
1271 SET_TOP(res);
1272 if (res == NULL)
1273 goto error;
1274 DISPATCH();
1275 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001276
Benjamin Petersonddd19492018-09-16 22:38:02 -07001277 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001278 PyObject *right = POP();
1279 PyObject *left = TOP();
1280 PyObject *sum;
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001281 /* NOTE(haypo): Please don't try to micro-optimize int+int on
1282 CPython using bytecode, it is simply worthless.
1283 See http://bugs.python.org/issue21955 and
1284 http://bugs.python.org/issue10044 for the discussion. In short,
1285 no patch shown any impact on a realistic benchmark, only a minor
1286 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001287 if (PyUnicode_CheckExact(left) &&
1288 PyUnicode_CheckExact(right)) {
1289 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001290 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001291 }
1292 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001293 sum = PyNumber_Add(left, right);
1294 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001295 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001296 Py_DECREF(right);
1297 SET_TOP(sum);
1298 if (sum == NULL)
1299 goto error;
1300 DISPATCH();
1301 }
1302
Benjamin Petersonddd19492018-09-16 22:38:02 -07001303 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001304 PyObject *right = POP();
1305 PyObject *left = TOP();
1306 PyObject *diff = PyNumber_Subtract(left, right);
1307 Py_DECREF(right);
1308 Py_DECREF(left);
1309 SET_TOP(diff);
1310 if (diff == NULL)
1311 goto error;
1312 DISPATCH();
1313 }
1314
Benjamin Petersonddd19492018-09-16 22:38:02 -07001315 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001316 PyObject *sub = POP();
1317 PyObject *container = TOP();
1318 PyObject *res = PyObject_GetItem(container, sub);
1319 Py_DECREF(container);
1320 Py_DECREF(sub);
1321 SET_TOP(res);
1322 if (res == NULL)
1323 goto error;
1324 DISPATCH();
1325 }
1326
Benjamin Petersonddd19492018-09-16 22:38:02 -07001327 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001328 PyObject *right = POP();
1329 PyObject *left = TOP();
1330 PyObject *res = PyNumber_Lshift(left, right);
1331 Py_DECREF(left);
1332 Py_DECREF(right);
1333 SET_TOP(res);
1334 if (res == NULL)
1335 goto error;
1336 DISPATCH();
1337 }
1338
Benjamin Petersonddd19492018-09-16 22:38:02 -07001339 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001340 PyObject *right = POP();
1341 PyObject *left = TOP();
1342 PyObject *res = PyNumber_Rshift(left, right);
1343 Py_DECREF(left);
1344 Py_DECREF(right);
1345 SET_TOP(res);
1346 if (res == NULL)
1347 goto error;
1348 DISPATCH();
1349 }
1350
Benjamin Petersonddd19492018-09-16 22:38:02 -07001351 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001352 PyObject *right = POP();
1353 PyObject *left = TOP();
1354 PyObject *res = PyNumber_And(left, right);
1355 Py_DECREF(left);
1356 Py_DECREF(right);
1357 SET_TOP(res);
1358 if (res == NULL)
1359 goto error;
1360 DISPATCH();
1361 }
1362
Benjamin Petersonddd19492018-09-16 22:38:02 -07001363 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001364 PyObject *right = POP();
1365 PyObject *left = TOP();
1366 PyObject *res = PyNumber_Xor(left, right);
1367 Py_DECREF(left);
1368 Py_DECREF(right);
1369 SET_TOP(res);
1370 if (res == NULL)
1371 goto error;
1372 DISPATCH();
1373 }
1374
Benjamin Petersonddd19492018-09-16 22:38:02 -07001375 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001376 PyObject *right = POP();
1377 PyObject *left = TOP();
1378 PyObject *res = PyNumber_Or(left, right);
1379 Py_DECREF(left);
1380 Py_DECREF(right);
1381 SET_TOP(res);
1382 if (res == NULL)
1383 goto error;
1384 DISPATCH();
1385 }
1386
Benjamin Petersonddd19492018-09-16 22:38:02 -07001387 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001388 PyObject *v = POP();
1389 PyObject *list = PEEK(oparg);
1390 int err;
1391 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001393 if (err != 0)
1394 goto error;
1395 PREDICT(JUMP_ABSOLUTE);
1396 DISPATCH();
1397 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001398
Benjamin Petersonddd19492018-09-16 22:38:02 -07001399 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001400 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001401 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001402 int err;
1403 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001405 if (err != 0)
1406 goto error;
1407 PREDICT(JUMP_ABSOLUTE);
1408 DISPATCH();
1409 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001410
Benjamin Petersonddd19492018-09-16 22:38:02 -07001411 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001412 PyObject *exp = POP();
1413 PyObject *base = TOP();
1414 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1415 Py_DECREF(base);
1416 Py_DECREF(exp);
1417 SET_TOP(res);
1418 if (res == NULL)
1419 goto error;
1420 DISPATCH();
1421 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001422
Benjamin Petersonddd19492018-09-16 22:38:02 -07001423 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001424 PyObject *right = POP();
1425 PyObject *left = TOP();
1426 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1427 Py_DECREF(left);
1428 Py_DECREF(right);
1429 SET_TOP(res);
1430 if (res == NULL)
1431 goto error;
1432 DISPATCH();
1433 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001434
Benjamin Petersonddd19492018-09-16 22:38:02 -07001435 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001436 PyObject *right = POP();
1437 PyObject *left = TOP();
1438 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1439 Py_DECREF(left);
1440 Py_DECREF(right);
1441 SET_TOP(res);
1442 if (res == NULL)
1443 goto error;
1444 DISPATCH();
1445 }
1446
Benjamin Petersonddd19492018-09-16 22:38:02 -07001447 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001448 PyObject *divisor = POP();
1449 PyObject *dividend = TOP();
1450 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1451 Py_DECREF(dividend);
1452 Py_DECREF(divisor);
1453 SET_TOP(quotient);
1454 if (quotient == NULL)
1455 goto error;
1456 DISPATCH();
1457 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001458
Benjamin Petersonddd19492018-09-16 22:38:02 -07001459 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001460 PyObject *divisor = POP();
1461 PyObject *dividend = TOP();
1462 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
1463 Py_DECREF(dividend);
1464 Py_DECREF(divisor);
1465 SET_TOP(quotient);
1466 if (quotient == NULL)
1467 goto error;
1468 DISPATCH();
1469 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001470
Benjamin Petersonddd19492018-09-16 22:38:02 -07001471 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001472 PyObject *right = POP();
1473 PyObject *left = TOP();
1474 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
1475 Py_DECREF(left);
1476 Py_DECREF(right);
1477 SET_TOP(mod);
1478 if (mod == NULL)
1479 goto error;
1480 DISPATCH();
1481 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001482
Benjamin Petersonddd19492018-09-16 22:38:02 -07001483 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001484 PyObject *right = POP();
1485 PyObject *left = TOP();
1486 PyObject *sum;
1487 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
1488 sum = unicode_concatenate(left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001489 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001490 }
1491 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001492 sum = PyNumber_InPlaceAdd(left, right);
1493 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001494 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001495 Py_DECREF(right);
1496 SET_TOP(sum);
1497 if (sum == NULL)
1498 goto error;
1499 DISPATCH();
1500 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001501
Benjamin Petersonddd19492018-09-16 22:38:02 -07001502 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001503 PyObject *right = POP();
1504 PyObject *left = TOP();
1505 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
1506 Py_DECREF(left);
1507 Py_DECREF(right);
1508 SET_TOP(diff);
1509 if (diff == NULL)
1510 goto error;
1511 DISPATCH();
1512 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001513
Benjamin Petersonddd19492018-09-16 22:38:02 -07001514 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001515 PyObject *right = POP();
1516 PyObject *left = TOP();
1517 PyObject *res = PyNumber_InPlaceLshift(left, right);
1518 Py_DECREF(left);
1519 Py_DECREF(right);
1520 SET_TOP(res);
1521 if (res == NULL)
1522 goto error;
1523 DISPATCH();
1524 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001525
Benjamin Petersonddd19492018-09-16 22:38:02 -07001526 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001527 PyObject *right = POP();
1528 PyObject *left = TOP();
1529 PyObject *res = PyNumber_InPlaceRshift(left, right);
1530 Py_DECREF(left);
1531 Py_DECREF(right);
1532 SET_TOP(res);
1533 if (res == NULL)
1534 goto error;
1535 DISPATCH();
1536 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001537
Benjamin Petersonddd19492018-09-16 22:38:02 -07001538 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001539 PyObject *right = POP();
1540 PyObject *left = TOP();
1541 PyObject *res = PyNumber_InPlaceAnd(left, right);
1542 Py_DECREF(left);
1543 Py_DECREF(right);
1544 SET_TOP(res);
1545 if (res == NULL)
1546 goto error;
1547 DISPATCH();
1548 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001549
Benjamin Petersonddd19492018-09-16 22:38:02 -07001550 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001551 PyObject *right = POP();
1552 PyObject *left = TOP();
1553 PyObject *res = PyNumber_InPlaceXor(left, right);
1554 Py_DECREF(left);
1555 Py_DECREF(right);
1556 SET_TOP(res);
1557 if (res == NULL)
1558 goto error;
1559 DISPATCH();
1560 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001561
Benjamin Petersonddd19492018-09-16 22:38:02 -07001562 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001563 PyObject *right = POP();
1564 PyObject *left = TOP();
1565 PyObject *res = PyNumber_InPlaceOr(left, right);
1566 Py_DECREF(left);
1567 Py_DECREF(right);
1568 SET_TOP(res);
1569 if (res == NULL)
1570 goto error;
1571 DISPATCH();
1572 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001573
Benjamin Petersonddd19492018-09-16 22:38:02 -07001574 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001575 PyObject *sub = TOP();
1576 PyObject *container = SECOND();
1577 PyObject *v = THIRD();
1578 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001579 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00001580 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001581 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001582 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001583 Py_DECREF(container);
1584 Py_DECREF(sub);
1585 if (err != 0)
1586 goto error;
1587 DISPATCH();
1588 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001589
Benjamin Petersonddd19492018-09-16 22:38:02 -07001590 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001591 PyObject *sub = TOP();
1592 PyObject *container = SECOND();
1593 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00001594 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00001595 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001596 err = PyObject_DelItem(container, sub);
1597 Py_DECREF(container);
1598 Py_DECREF(sub);
1599 if (err != 0)
1600 goto error;
1601 DISPATCH();
1602 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00001603
Benjamin Petersonddd19492018-09-16 22:38:02 -07001604 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01001605 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001606 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01001607 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04001608 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001609 if (hook == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001610 PyErr_SetString(PyExc_RuntimeError,
1611 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001612 Py_DECREF(value);
1613 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 }
Victor Stinnerde4ae3d2016-12-04 22:59:09 +01001615 res = PyObject_CallFunctionObjArgs(hook, value, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001616 Py_DECREF(value);
1617 if (res == NULL)
1618 goto error;
1619 Py_DECREF(res);
1620 DISPATCH();
1621 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00001622
Benjamin Petersonddd19492018-09-16 22:38:02 -07001623 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001624 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 switch (oparg) {
1626 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001627 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02001628 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001630 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02001631 /* fall through */
1632 case 0:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001633 if (do_raise(exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001634 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001635 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 break;
1637 default:
1638 PyErr_SetString(PyExc_SystemError,
1639 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 break;
1641 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001642 goto error;
1643 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001644
Benjamin Petersonddd19492018-09-16 22:38:02 -07001645 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001647 assert(f->f_iblock == 0);
1648 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001649 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00001650
Benjamin Petersonddd19492018-09-16 22:38:02 -07001651 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001652 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001653 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001654 PyObject *obj = TOP();
1655 PyTypeObject *type = Py_TYPE(obj);
1656
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001657 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001658 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001659 }
Yury Selivanov75445082015-05-11 22:57:16 -04001660
1661 if (getter != NULL) {
1662 iter = (*getter)(obj);
1663 Py_DECREF(obj);
1664 if (iter == NULL) {
1665 SET_TOP(NULL);
1666 goto error;
1667 }
1668 }
1669 else {
1670 SET_TOP(NULL);
1671 PyErr_Format(
1672 PyExc_TypeError,
1673 "'async for' requires an object with "
1674 "__aiter__ method, got %.100s",
1675 type->tp_name);
1676 Py_DECREF(obj);
1677 goto error;
1678 }
1679
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001680 if (Py_TYPE(iter)->tp_as_async == NULL ||
1681 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001682
Yury Selivanov398ff912017-03-02 22:20:00 -05001683 SET_TOP(NULL);
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001684 PyErr_Format(
1685 PyExc_TypeError,
1686 "'async for' received an object from __aiter__ "
1687 "that does not implement __anext__: %.100s",
1688 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04001689 Py_DECREF(iter);
1690 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04001691 }
1692
Yury Selivanovfaa135a2017-10-06 02:08:57 -04001693 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04001694 DISPATCH();
1695 }
1696
Benjamin Petersonddd19492018-09-16 22:38:02 -07001697 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04001698 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04001699 PyObject *next_iter = NULL;
1700 PyObject *awaitable = NULL;
1701 PyObject *aiter = TOP();
1702 PyTypeObject *type = Py_TYPE(aiter);
1703
Yury Selivanoveb636452016-09-08 22:01:51 -07001704 if (PyAsyncGen_CheckExact(aiter)) {
1705 awaitable = type->tp_as_async->am_anext(aiter);
1706 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04001707 goto error;
1708 }
Yury Selivanoveb636452016-09-08 22:01:51 -07001709 } else {
1710 if (type->tp_as_async != NULL){
1711 getter = type->tp_as_async->am_anext;
1712 }
Yury Selivanov75445082015-05-11 22:57:16 -04001713
Yury Selivanoveb636452016-09-08 22:01:51 -07001714 if (getter != NULL) {
1715 next_iter = (*getter)(aiter);
1716 if (next_iter == NULL) {
1717 goto error;
1718 }
1719 }
1720 else {
1721 PyErr_Format(
1722 PyExc_TypeError,
1723 "'async for' requires an iterator with "
1724 "__anext__ method, got %.100s",
1725 type->tp_name);
1726 goto error;
1727 }
Yury Selivanov75445082015-05-11 22:57:16 -04001728
Yury Selivanoveb636452016-09-08 22:01:51 -07001729 awaitable = _PyCoro_GetAwaitableIter(next_iter);
1730 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05001731 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07001732 PyExc_TypeError,
1733 "'async for' received an invalid object "
1734 "from __anext__: %.100s",
1735 Py_TYPE(next_iter)->tp_name);
1736
1737 Py_DECREF(next_iter);
1738 goto error;
1739 } else {
1740 Py_DECREF(next_iter);
1741 }
1742 }
Yury Selivanov75445082015-05-11 22:57:16 -04001743
1744 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001745 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001746 DISPATCH();
1747 }
1748
Benjamin Petersonddd19492018-09-16 22:38:02 -07001749 case TARGET(GET_AWAITABLE): {
1750 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04001751 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04001752 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04001753
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03001754 if (iter == NULL) {
1755 format_awaitable_error(Py_TYPE(iterable),
1756 _Py_OPCODE(next_instr[-2]));
1757 }
1758
Yury Selivanov75445082015-05-11 22:57:16 -04001759 Py_DECREF(iterable);
1760
Yury Selivanovc724bae2016-03-02 11:30:46 -05001761 if (iter != NULL && PyCoro_CheckExact(iter)) {
1762 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
1763 if (yf != NULL) {
1764 /* `iter` is a coroutine object that is being
1765 awaited, `yf` is a pointer to the current awaitable
1766 being awaited on. */
1767 Py_DECREF(yf);
1768 Py_CLEAR(iter);
1769 PyErr_SetString(
1770 PyExc_RuntimeError,
1771 "coroutine is being awaited already");
1772 /* The code below jumps to `error` if `iter` is NULL. */
1773 }
1774 }
1775
Yury Selivanov75445082015-05-11 22:57:16 -04001776 SET_TOP(iter); /* Even if it's NULL */
1777
1778 if (iter == NULL) {
1779 goto error;
1780 }
1781
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001782 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04001783 DISPATCH();
1784 }
1785
Benjamin Petersonddd19492018-09-16 22:38:02 -07001786 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001787 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001788 PyObject *receiver = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001789 int err;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001790 if (PyGen_CheckExact(receiver) || PyCoro_CheckExact(receiver)) {
1791 retval = _PyGen_Send((PyGenObject *)receiver, v);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001792 } else {
Benjamin Peterson302e7902012-03-20 23:17:04 -04001793 _Py_IDENTIFIER(send);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001794 if (v == Py_None)
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001795 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001796 else
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001797 retval = _PyObject_CallMethodIdObjArgs(receiver, &PyId_send, v, NULL);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001798 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001799 Py_DECREF(v);
1800 if (retval == NULL) {
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001801 PyObject *val;
Guido van Rossum8820c232013-11-21 11:30:06 -08001802 if (tstate->c_tracefunc != NULL
1803 && PyErr_ExceptionMatches(PyExc_StopIteration))
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001804 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Nick Coghlanc40bc092012-06-17 15:15:49 +10001805 err = _PyGen_FetchStopIterationValue(&val);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001806 if (err < 0)
1807 goto error;
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07001808 Py_DECREF(receiver);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001809 SET_TOP(val);
1810 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001811 }
Martin Panter95f53c12016-07-18 08:23:26 +00001812 /* receiver remains on stack, retval is value to be yielded */
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001813 f->f_stacktop = stack_pointer;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001814 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01001815 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03001816 f->f_lasti -= sizeof(_Py_CODEUNIT);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001817 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10001819
Benjamin Petersonddd19492018-09-16 22:38:02 -07001820 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07001822
1823 if (co->co_flags & CO_ASYNC_GENERATOR) {
1824 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
1825 Py_DECREF(retval);
1826 if (w == NULL) {
1827 retval = NULL;
1828 goto error;
1829 }
1830 retval = w;
1831 }
1832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 f->f_stacktop = stack_pointer;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001834 goto return_or_yield;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001835 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001836
Benjamin Petersonddd19492018-09-16 22:38:02 -07001837 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001838 PyObject *type, *value, *traceback;
1839 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 PyTryBlock *b = PyFrame_BlockPop(f);
1841 if (b->b_type != EXCEPT_HANDLER) {
1842 PyErr_SetString(PyExc_SystemError,
1843 "popped block is not an except handler");
1844 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001846 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
1847 STACK_LEVEL() <= (b)->b_level + 4);
1848 exc_info = tstate->exc_info;
1849 type = exc_info->exc_type;
1850 value = exc_info->exc_value;
1851 traceback = exc_info->exc_traceback;
1852 exc_info->exc_type = POP();
1853 exc_info->exc_value = POP();
1854 exc_info->exc_traceback = POP();
1855 Py_XDECREF(type);
1856 Py_XDECREF(value);
1857 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001859 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001860
Benjamin Petersonddd19492018-09-16 22:38:02 -07001861 case TARGET(POP_BLOCK): {
1862 PREDICTED(POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001863 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001865 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001866
Benjamin Petersonddd19492018-09-16 22:38:02 -07001867 case TARGET(POP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001868 /* If oparg is 0 at the top of the stack are 1 or 6 values:
1869 Either:
1870 - TOP = NULL or an integer
1871 or:
1872 - (TOP, SECOND, THIRD) = exc_info()
1873 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1874
1875 If oparg is 1 the value for 'return' was additionally pushed
1876 at the top of the stack.
1877 */
1878 PyObject *res = NULL;
1879 if (oparg) {
1880 res = POP();
1881 }
1882 PyObject *exc = POP();
1883 if (exc == NULL || PyLong_CheckExact(exc)) {
1884 Py_XDECREF(exc);
1885 }
1886 else {
1887 Py_DECREF(exc);
1888 Py_DECREF(POP());
1889 Py_DECREF(POP());
1890
1891 PyObject *type, *value, *traceback;
1892 _PyErr_StackItem *exc_info;
1893 PyTryBlock *b = PyFrame_BlockPop(f);
1894 if (b->b_type != EXCEPT_HANDLER) {
1895 PyErr_SetString(PyExc_SystemError,
1896 "popped block is not an except handler");
1897 Py_XDECREF(res);
1898 goto error;
1899 }
1900 assert(STACK_LEVEL() == (b)->b_level + 3);
1901 exc_info = tstate->exc_info;
1902 type = exc_info->exc_type;
1903 value = exc_info->exc_value;
1904 traceback = exc_info->exc_traceback;
1905 exc_info->exc_type = POP();
1906 exc_info->exc_value = POP();
1907 exc_info->exc_traceback = POP();
1908 Py_XDECREF(type);
1909 Py_XDECREF(value);
1910 Py_XDECREF(traceback);
1911 }
1912 if (oparg) {
1913 PUSH(res);
1914 }
1915 DISPATCH();
1916 }
1917
Benjamin Petersonddd19492018-09-16 22:38:02 -07001918 case TARGET(CALL_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001919 PyObject *ret = PyLong_FromLong(INSTR_OFFSET());
1920 if (ret == NULL) {
1921 goto error;
1922 }
1923 PUSH(ret);
1924 JUMPBY(oparg);
1925 FAST_DISPATCH();
1926 }
1927
Benjamin Petersonddd19492018-09-16 22:38:02 -07001928 case TARGET(BEGIN_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001929 /* Push NULL onto the stack for using it in END_FINALLY,
1930 POP_FINALLY, WITH_CLEANUP_START and WITH_CLEANUP_FINISH.
1931 */
1932 PUSH(NULL);
1933 FAST_DISPATCH();
1934 }
1935
Benjamin Petersonddd19492018-09-16 22:38:02 -07001936 case TARGET(END_FINALLY): {
1937 PREDICTED(END_FINALLY);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001938 /* At the top of the stack are 1 or 6 values:
1939 Either:
1940 - TOP = NULL or an integer
1941 or:
1942 - (TOP, SECOND, THIRD) = exc_info()
1943 - (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
1944 */
1945 PyObject *exc = POP();
1946 if (exc == NULL) {
1947 FAST_DISPATCH();
1948 }
1949 else if (PyLong_CheckExact(exc)) {
1950 int ret = _PyLong_AsInt(exc);
1951 Py_DECREF(exc);
1952 if (ret == -1 && PyErr_Occurred()) {
1953 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001955 JUMPTO(ret);
1956 FAST_DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001958 else {
1959 assert(PyExceptionClass_Check(exc));
1960 PyObject *val = POP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001961 PyObject *tb = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001962 PyErr_Restore(exc, val, tb);
1963 goto exception_unwind;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001964 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001965 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001966
Benjamin Petersonddd19492018-09-16 22:38:02 -07001967 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001968 PyObject *exc = POP();
1969 assert(PyExceptionClass_Check(exc));
1970 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
1971 PyTryBlock *b = PyFrame_BlockPop(f);
1972 assert(b->b_type == EXCEPT_HANDLER);
1973 Py_DECREF(exc);
1974 UNWIND_EXCEPT_HANDLER(b);
1975 Py_DECREF(POP());
1976 JUMPBY(oparg);
1977 FAST_DISPATCH();
1978 }
1979 else {
1980 PyObject *val = POP();
1981 PyObject *tb = POP();
1982 PyErr_Restore(exc, val, tb);
1983 goto exception_unwind;
1984 }
1985 }
1986
Benjamin Petersonddd19492018-09-16 22:38:02 -07001987 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02001988 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001989
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001990 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001991 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001992 bc = _PyDict_GetItemId(f->f_builtins, &PyId___build_class__);
1993 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02001994 PyErr_SetString(PyExc_NameError,
1995 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001996 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02001997 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001998 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02001999 }
2000 else {
2001 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2002 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002003 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2005 if (bc == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002006 if (PyErr_ExceptionMatches(PyExc_KeyError))
2007 PyErr_SetString(PyExc_NameError,
2008 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002009 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002010 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002012 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002013 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002014 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002015
Benjamin Petersonddd19492018-09-16 22:38:02 -07002016 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002017 PyObject *name = GETITEM(names, oparg);
2018 PyObject *v = POP();
2019 PyObject *ns = f->f_locals;
2020 int err;
2021 if (ns == NULL) {
2022 PyErr_Format(PyExc_SystemError,
2023 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002024 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002025 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002026 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 if (PyDict_CheckExact(ns))
2028 err = PyDict_SetItem(ns, name, v);
2029 else
2030 err = PyObject_SetItem(ns, name, v);
2031 Py_DECREF(v);
2032 if (err != 0)
2033 goto error;
2034 DISPATCH();
2035 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002036
Benjamin Petersonddd19492018-09-16 22:38:02 -07002037 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002038 PyObject *name = GETITEM(names, oparg);
2039 PyObject *ns = f->f_locals;
2040 int err;
2041 if (ns == NULL) {
2042 PyErr_Format(PyExc_SystemError,
2043 "no locals when deleting %R", name);
2044 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002046 err = PyObject_DelItem(ns, name);
2047 if (err != 0) {
2048 format_exc_check_arg(PyExc_NameError,
2049 NAME_ERROR_MSG,
2050 name);
2051 goto error;
2052 }
2053 DISPATCH();
2054 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002055
Benjamin Petersonddd19492018-09-16 22:38:02 -07002056 case TARGET(UNPACK_SEQUENCE): {
2057 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002058 PyObject *seq = POP(), *item, **items;
2059 if (PyTuple_CheckExact(seq) &&
2060 PyTuple_GET_SIZE(seq) == oparg) {
2061 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002063 item = items[oparg];
2064 Py_INCREF(item);
2065 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002066 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002067 } else if (PyList_CheckExact(seq) &&
2068 PyList_GET_SIZE(seq) == oparg) {
2069 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002070 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 item = items[oparg];
2072 Py_INCREF(item);
2073 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002075 } else if (unpack_iterable(seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002076 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002077 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 } else {
2079 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 Py_DECREF(seq);
2081 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002084 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002085 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002086
Benjamin Petersonddd19492018-09-16 22:38:02 -07002087 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002088 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2089 PyObject *seq = POP();
2090
2091 if (unpack_iterable(seq, oparg & 0xFF, oparg >> 8,
2092 stack_pointer + totalargs)) {
2093 stack_pointer += totalargs;
2094 } else {
2095 Py_DECREF(seq);
2096 goto error;
2097 }
2098 Py_DECREF(seq);
2099 DISPATCH();
2100 }
2101
Benjamin Petersonddd19492018-09-16 22:38:02 -07002102 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002103 PyObject *name = GETITEM(names, oparg);
2104 PyObject *owner = TOP();
2105 PyObject *v = SECOND();
2106 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002107 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002108 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002109 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002110 Py_DECREF(owner);
2111 if (err != 0)
2112 goto error;
2113 DISPATCH();
2114 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002115
Benjamin Petersonddd19492018-09-16 22:38:02 -07002116 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002117 PyObject *name = GETITEM(names, oparg);
2118 PyObject *owner = POP();
2119 int err;
2120 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2121 Py_DECREF(owner);
2122 if (err != 0)
2123 goto error;
2124 DISPATCH();
2125 }
2126
Benjamin Petersonddd19492018-09-16 22:38:02 -07002127 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002128 PyObject *name = GETITEM(names, oparg);
2129 PyObject *v = POP();
2130 int err;
2131 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002132 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 if (err != 0)
2134 goto error;
2135 DISPATCH();
2136 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002137
Benjamin Petersonddd19492018-09-16 22:38:02 -07002138 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002139 PyObject *name = GETITEM(names, oparg);
2140 int err;
2141 err = PyDict_DelItem(f->f_globals, name);
2142 if (err != 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002143 format_exc_check_arg(
Ezio Melotti04a29552013-03-03 15:12:44 +02002144 PyExc_NameError, NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002146 }
2147 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002148 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002149
Benjamin Petersonddd19492018-09-16 22:38:02 -07002150 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002151 PyObject *name = GETITEM(names, oparg);
2152 PyObject *locals = f->f_locals;
2153 PyObject *v;
2154 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002155 PyErr_Format(PyExc_SystemError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 "no locals when loading %R", name);
2157 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002159 if (PyDict_CheckExact(locals)) {
2160 v = PyDict_GetItem(locals, name);
2161 Py_XINCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002162 }
2163 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002164 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002165 if (v == NULL) {
Benjamin Peterson92722792012-12-15 12:51:05 -05002166 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2167 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002168 PyErr_Clear();
2169 }
2170 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 if (v == NULL) {
2172 v = PyDict_GetItem(f->f_globals, name);
2173 Py_XINCREF(v);
2174 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002175 if (PyDict_CheckExact(f->f_builtins)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002176 v = PyDict_GetItem(f->f_builtins, name);
2177 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002178 format_exc_check_arg(
2179 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002180 NAME_ERROR_MSG, name);
2181 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002182 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002183 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002184 }
2185 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 v = PyObject_GetItem(f->f_builtins, name);
2187 if (v == NULL) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002188 if (PyErr_ExceptionMatches(PyExc_KeyError))
2189 format_exc_check_arg(
2190 PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002191 NAME_ERROR_MSG, name);
2192 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002193 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002194 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002195 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002196 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002197 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002198 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002199 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002200
Benjamin Petersonddd19492018-09-16 22:38:02 -07002201 case TARGET(LOAD_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002202 PyObject *name = GETITEM(names, oparg);
2203 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002204 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002205 && PyDict_CheckExact(f->f_builtins))
2206 {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002207 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002208 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002209 name);
2210 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002211 if (!_PyErr_OCCURRED()) {
2212 /* _PyDict_LoadGlobal() returns NULL without raising
2213 * an exception if the key doesn't exist */
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002214 format_exc_check_arg(PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002215 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002216 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002218 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002219 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002220 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002221 else {
2222 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002223
2224 /* namespace 1: globals */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002225 v = PyObject_GetItem(f->f_globals, name);
2226 if (v == NULL) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002227 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2228 goto error;
2229 PyErr_Clear();
2230
Victor Stinnerb4efc962015-11-20 09:24:02 +01002231 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002232 v = PyObject_GetItem(f->f_builtins, name);
2233 if (v == NULL) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002234 if (PyErr_ExceptionMatches(PyExc_KeyError))
2235 format_exc_check_arg(
2236 PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002237 NAME_ERROR_MSG, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002238 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002239 }
2240 }
2241 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002242 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002244 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002245
Benjamin Petersonddd19492018-09-16 22:38:02 -07002246 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002247 PyObject *v = GETLOCAL(oparg);
2248 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002249 SETLOCAL(oparg, NULL);
2250 DISPATCH();
2251 }
2252 format_exc_check_arg(
2253 PyExc_UnboundLocalError,
2254 UNBOUNDLOCAL_ERROR_MSG,
2255 PyTuple_GetItem(co->co_varnames, oparg)
2256 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002257 goto error;
2258 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002259
Benjamin Petersonddd19492018-09-16 22:38:02 -07002260 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002261 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002262 PyObject *oldobj = PyCell_GET(cell);
2263 if (oldobj != NULL) {
2264 PyCell_SET(cell, NULL);
2265 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002266 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002267 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002268 format_exc_unbound(co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002269 goto error;
2270 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002271
Benjamin Petersonddd19492018-09-16 22:38:02 -07002272 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002273 PyObject *cell = freevars[oparg];
2274 Py_INCREF(cell);
2275 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002278
Benjamin Petersonddd19492018-09-16 22:38:02 -07002279 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002280 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002281 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002282 assert(locals);
2283 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2284 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2285 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2286 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2287 if (PyDict_CheckExact(locals)) {
2288 value = PyDict_GetItem(locals, name);
2289 Py_XINCREF(value);
2290 }
2291 else {
2292 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002293 if (value == NULL) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002294 if (!PyErr_ExceptionMatches(PyExc_KeyError))
2295 goto error;
2296 PyErr_Clear();
2297 }
2298 }
2299 if (!value) {
2300 PyObject *cell = freevars[oparg];
2301 value = PyCell_GET(cell);
2302 if (value == NULL) {
2303 format_exc_unbound(co, oparg);
2304 goto error;
2305 }
2306 Py_INCREF(value);
2307 }
2308 PUSH(value);
2309 DISPATCH();
2310 }
2311
Benjamin Petersonddd19492018-09-16 22:38:02 -07002312 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002313 PyObject *cell = freevars[oparg];
2314 PyObject *value = PyCell_GET(cell);
2315 if (value == NULL) {
2316 format_exc_unbound(co, oparg);
2317 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002319 Py_INCREF(value);
2320 PUSH(value);
2321 DISPATCH();
2322 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002323
Benjamin Petersonddd19492018-09-16 22:38:02 -07002324 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002325 PyObject *v = POP();
2326 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002327 PyObject *oldobj = PyCell_GET(cell);
2328 PyCell_SET(cell, v);
2329 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002330 DISPATCH();
2331 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002332
Benjamin Petersonddd19492018-09-16 22:38:02 -07002333 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002334 PyObject *str;
2335 PyObject *empty = PyUnicode_New(0, 0);
2336 if (empty == NULL) {
2337 goto error;
2338 }
2339 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2340 Py_DECREF(empty);
2341 if (str == NULL)
2342 goto error;
2343 while (--oparg >= 0) {
2344 PyObject *item = POP();
2345 Py_DECREF(item);
2346 }
2347 PUSH(str);
2348 DISPATCH();
2349 }
2350
Benjamin Petersonddd19492018-09-16 22:38:02 -07002351 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002352 PyObject *tup = PyTuple_New(oparg);
2353 if (tup == NULL)
2354 goto error;
2355 while (--oparg >= 0) {
2356 PyObject *item = POP();
2357 PyTuple_SET_ITEM(tup, oparg, item);
2358 }
2359 PUSH(tup);
2360 DISPATCH();
2361 }
2362
Benjamin Petersonddd19492018-09-16 22:38:02 -07002363 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002364 PyObject *list = PyList_New(oparg);
2365 if (list == NULL)
2366 goto error;
2367 while (--oparg >= 0) {
2368 PyObject *item = POP();
2369 PyList_SET_ITEM(list, oparg, item);
2370 }
2371 PUSH(list);
2372 DISPATCH();
2373 }
2374
Benjamin Petersonddd19492018-09-16 22:38:02 -07002375 case TARGET(BUILD_TUPLE_UNPACK_WITH_CALL):
2376 case TARGET(BUILD_TUPLE_UNPACK):
2377 case TARGET(BUILD_LIST_UNPACK): {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002378 int convert_to_tuple = opcode != BUILD_LIST_UNPACK;
Victor Stinner74319ae2016-08-25 00:04:09 +02002379 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002380 PyObject *sum = PyList_New(0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002381 PyObject *return_value;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002382
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002383 if (sum == NULL)
2384 goto error;
2385
2386 for (i = oparg; i > 0; i--) {
2387 PyObject *none_val;
2388
2389 none_val = _PyList_Extend((PyListObject *)sum, PEEK(i));
2390 if (none_val == NULL) {
Serhiy Storchaka73442852016-10-02 10:33:46 +03002391 if (opcode == BUILD_TUPLE_UNPACK_WITH_CALL &&
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002392 PyErr_ExceptionMatches(PyExc_TypeError))
2393 {
2394 check_args_iterable(PEEK(1 + oparg), PEEK(i));
Serhiy Storchaka73442852016-10-02 10:33:46 +03002395 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002396 Py_DECREF(sum);
2397 goto error;
2398 }
2399 Py_DECREF(none_val);
2400 }
2401
2402 if (convert_to_tuple) {
2403 return_value = PyList_AsTuple(sum);
2404 Py_DECREF(sum);
2405 if (return_value == NULL)
2406 goto error;
2407 }
2408 else {
2409 return_value = sum;
2410 }
2411
2412 while (oparg--)
2413 Py_DECREF(POP());
2414 PUSH(return_value);
2415 DISPATCH();
2416 }
2417
Benjamin Petersonddd19492018-09-16 22:38:02 -07002418 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002419 PyObject *set = PySet_New(NULL);
2420 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002421 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002422 if (set == NULL)
2423 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002424 for (i = oparg; i > 0; i--) {
2425 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002426 if (err == 0)
2427 err = PySet_Add(set, item);
2428 Py_DECREF(item);
2429 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002430 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002431 if (err != 0) {
2432 Py_DECREF(set);
2433 goto error;
2434 }
2435 PUSH(set);
2436 DISPATCH();
2437 }
2438
Benjamin Petersonddd19492018-09-16 22:38:02 -07002439 case TARGET(BUILD_SET_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002440 Py_ssize_t i;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002441 PyObject *sum = PySet_New(NULL);
2442 if (sum == NULL)
2443 goto error;
2444
2445 for (i = oparg; i > 0; i--) {
2446 if (_PySet_Update(sum, PEEK(i)) < 0) {
2447 Py_DECREF(sum);
2448 goto error;
2449 }
2450 }
2451
2452 while (oparg--)
2453 Py_DECREF(POP());
2454 PUSH(sum);
2455 DISPATCH();
2456 }
2457
Benjamin Petersonddd19492018-09-16 22:38:02 -07002458 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002459 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002460 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2461 if (map == NULL)
2462 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002463 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002464 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002465 PyObject *key = PEEK(2*i);
2466 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002467 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002468 if (err != 0) {
2469 Py_DECREF(map);
2470 goto error;
2471 }
2472 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002473
2474 while (oparg--) {
2475 Py_DECREF(POP());
2476 Py_DECREF(POP());
2477 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 PUSH(map);
2479 DISPATCH();
2480 }
2481
Benjamin Petersonddd19492018-09-16 22:38:02 -07002482 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002483 _Py_IDENTIFIER(__annotations__);
2484 int err;
2485 PyObject *ann_dict;
2486 if (f->f_locals == NULL) {
2487 PyErr_Format(PyExc_SystemError,
2488 "no locals found when setting up annotations");
2489 goto error;
2490 }
2491 /* check if __annotations__ in locals()... */
2492 if (PyDict_CheckExact(f->f_locals)) {
2493 ann_dict = _PyDict_GetItemId(f->f_locals,
2494 &PyId___annotations__);
2495 if (ann_dict == NULL) {
2496 /* ...if not, create a new one */
2497 ann_dict = PyDict_New();
2498 if (ann_dict == NULL) {
2499 goto error;
2500 }
2501 err = _PyDict_SetItemId(f->f_locals,
2502 &PyId___annotations__, ann_dict);
2503 Py_DECREF(ann_dict);
2504 if (err != 0) {
2505 goto error;
2506 }
2507 }
2508 }
2509 else {
2510 /* do the same if locals() is not a dict */
2511 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
2512 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02002513 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07002514 }
2515 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
2516 if (ann_dict == NULL) {
2517 if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
2518 goto error;
2519 }
2520 PyErr_Clear();
2521 ann_dict = PyDict_New();
2522 if (ann_dict == NULL) {
2523 goto error;
2524 }
2525 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
2526 Py_DECREF(ann_dict);
2527 if (err != 0) {
2528 goto error;
2529 }
2530 }
2531 else {
2532 Py_DECREF(ann_dict);
2533 }
2534 }
2535 DISPATCH();
2536 }
2537
Benjamin Petersonddd19492018-09-16 22:38:02 -07002538 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002539 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03002540 PyObject *map;
2541 PyObject *keys = TOP();
2542 if (!PyTuple_CheckExact(keys) ||
2543 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
2544 PyErr_SetString(PyExc_SystemError,
2545 "bad BUILD_CONST_KEY_MAP keys argument");
2546 goto error;
2547 }
2548 map = _PyDict_NewPresized((Py_ssize_t)oparg);
2549 if (map == NULL) {
2550 goto error;
2551 }
2552 for (i = oparg; i > 0; i--) {
2553 int err;
2554 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
2555 PyObject *value = PEEK(i + 1);
2556 err = PyDict_SetItem(map, key, value);
2557 if (err != 0) {
2558 Py_DECREF(map);
2559 goto error;
2560 }
2561 }
2562
2563 Py_DECREF(POP());
2564 while (oparg--) {
2565 Py_DECREF(POP());
2566 }
2567 PUSH(map);
2568 DISPATCH();
2569 }
2570
Benjamin Petersonddd19492018-09-16 22:38:02 -07002571 case TARGET(BUILD_MAP_UNPACK): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002572 Py_ssize_t i;
Serhiy Storchakab7281052016-09-12 00:52:40 +03002573 PyObject *sum = PyDict_New();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002574 if (sum == NULL)
2575 goto error;
2576
2577 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002578 PyObject *arg = PEEK(i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002579 if (PyDict_Update(sum, arg) < 0) {
2580 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
2581 PyErr_Format(PyExc_TypeError,
Berker Peksag8e9045d2016-10-02 13:08:25 +03002582 "'%.200s' object is not a mapping",
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002583 arg->ob_type->tp_name);
2584 }
2585 Py_DECREF(sum);
2586 goto error;
2587 }
2588 }
2589
Victor Stinnerf9b760f2016-09-09 10:17:08 -07002590 while (oparg--)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002591 Py_DECREF(POP());
2592 PUSH(sum);
2593 DISPATCH();
2594 }
2595
Benjamin Petersonddd19492018-09-16 22:38:02 -07002596 case TARGET(BUILD_MAP_UNPACK_WITH_CALL): {
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002597 Py_ssize_t i;
2598 PyObject *sum = PyDict_New();
2599 if (sum == NULL)
2600 goto error;
2601
2602 for (i = oparg; i > 0; i--) {
2603 PyObject *arg = PEEK(i);
2604 if (_PyDict_MergeEx(sum, arg, 2) < 0) {
2605 PyObject *func = PEEK(2 + oparg);
2606 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03002607 format_kwargs_mapping_error(func, arg);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03002608 }
2609 else if (PyErr_ExceptionMatches(PyExc_KeyError)) {
2610 PyObject *exc, *val, *tb;
2611 PyErr_Fetch(&exc, &val, &tb);
2612 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
2613 PyObject *key = PyTuple_GET_ITEM(val, 0);
2614 if (!PyUnicode_Check(key)) {
2615 PyErr_Format(PyExc_TypeError,
2616 "%.200s%.200s keywords must be strings",
2617 PyEval_GetFuncName(func),
2618 PyEval_GetFuncDesc(func));
2619 } else {
2620 PyErr_Format(PyExc_TypeError,
2621 "%.200s%.200s got multiple "
2622 "values for keyword argument '%U'",
2623 PyEval_GetFuncName(func),
2624 PyEval_GetFuncDesc(func),
2625 key);
2626 }
2627 Py_XDECREF(exc);
2628 Py_XDECREF(val);
2629 Py_XDECREF(tb);
2630 }
2631 else {
2632 PyErr_Restore(exc, val, tb);
2633 }
2634 }
2635 Py_DECREF(sum);
2636 goto error;
2637 }
2638 }
2639
2640 while (oparg--)
2641 Py_DECREF(POP());
2642 PUSH(sum);
2643 DISPATCH();
2644 }
2645
Benjamin Petersonddd19492018-09-16 22:38:02 -07002646 case TARGET(MAP_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002647 PyObject *key = TOP();
2648 PyObject *value = SECOND();
2649 PyObject *map;
2650 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002651 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07002652 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002653 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00002654 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 Py_DECREF(value);
2656 Py_DECREF(key);
2657 if (err != 0)
2658 goto error;
2659 PREDICT(JUMP_ABSOLUTE);
2660 DISPATCH();
2661 }
2662
Benjamin Petersonddd19492018-09-16 22:38:02 -07002663 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002664 PyObject *name = GETITEM(names, oparg);
2665 PyObject *owner = TOP();
2666 PyObject *res = PyObject_GetAttr(owner, name);
2667 Py_DECREF(owner);
2668 SET_TOP(res);
2669 if (res == NULL)
2670 goto error;
2671 DISPATCH();
2672 }
2673
Benjamin Petersonddd19492018-09-16 22:38:02 -07002674 case TARGET(COMPARE_OP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002675 PyObject *right = POP();
2676 PyObject *left = TOP();
2677 PyObject *res = cmp_outcome(oparg, left, right);
2678 Py_DECREF(left);
2679 Py_DECREF(right);
2680 SET_TOP(res);
2681 if (res == NULL)
2682 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 PREDICT(POP_JUMP_IF_FALSE);
2684 PREDICT(POP_JUMP_IF_TRUE);
2685 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002686 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002687
Benjamin Petersonddd19492018-09-16 22:38:02 -07002688 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002689 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002690 PyObject *fromlist = POP();
2691 PyObject *level = TOP();
2692 PyObject *res;
Serhiy Storchaka133138a2016-08-02 22:51:21 +03002693 res = import_name(f, name, fromlist, level);
2694 Py_DECREF(level);
2695 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002696 SET_TOP(res);
2697 if (res == NULL)
2698 goto error;
2699 DISPATCH();
2700 }
2701
Benjamin Petersonddd19492018-09-16 22:38:02 -07002702 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002703 PyObject *from = POP(), *locals;
2704 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002705 if (PyFrame_FastToLocalsWithError(f) < 0) {
2706 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01002707 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002708 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01002709
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002710 locals = f->f_locals;
2711 if (locals == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002712 PyErr_SetString(PyExc_SystemError,
2713 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08002714 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002715 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002716 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002717 err = import_all_from(locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002718 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002719 Py_DECREF(from);
2720 if (err != 0)
2721 goto error;
2722 DISPATCH();
2723 }
Guido van Rossum25831651993-05-19 14:50:45 +00002724
Benjamin Petersonddd19492018-09-16 22:38:02 -07002725 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002726 PyObject *name = GETITEM(names, oparg);
2727 PyObject *from = TOP();
2728 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002729 res = import_from(from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 PUSH(res);
2731 if (res == NULL)
2732 goto error;
2733 DISPATCH();
2734 }
Thomas Wouters52152252000-08-17 22:55:00 +00002735
Benjamin Petersonddd19492018-09-16 22:38:02 -07002736 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 JUMPBY(oparg);
2738 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002739 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002740
Benjamin Petersonddd19492018-09-16 22:38:02 -07002741 case TARGET(POP_JUMP_IF_FALSE): {
2742 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002743 PyObject *cond = POP();
2744 int err;
2745 if (cond == Py_True) {
2746 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 FAST_DISPATCH();
2748 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002749 if (cond == Py_False) {
2750 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 JUMPTO(oparg);
2752 FAST_DISPATCH();
2753 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002754 err = PyObject_IsTrue(cond);
2755 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002756 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07002757 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002758 else if (err == 0)
2759 JUMPTO(oparg);
2760 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002764
Benjamin Petersonddd19492018-09-16 22:38:02 -07002765 case TARGET(POP_JUMP_IF_TRUE): {
2766 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002767 PyObject *cond = POP();
2768 int err;
2769 if (cond == Py_False) {
2770 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002771 FAST_DISPATCH();
2772 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002773 if (cond == Py_True) {
2774 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002775 JUMPTO(oparg);
2776 FAST_DISPATCH();
2777 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002778 err = PyObject_IsTrue(cond);
2779 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002780 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 JUMPTO(oparg);
2782 }
2783 else if (err == 0)
2784 ;
2785 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002786 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002787 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002788 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002789
Benjamin Petersonddd19492018-09-16 22:38:02 -07002790 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002791 PyObject *cond = TOP();
2792 int err;
2793 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002794 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002795 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002796 FAST_DISPATCH();
2797 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002798 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002799 JUMPTO(oparg);
2800 FAST_DISPATCH();
2801 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002802 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002803 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002804 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002805 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 }
2807 else if (err == 0)
2808 JUMPTO(oparg);
2809 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002810 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002811 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00002813
Benjamin Petersonddd19492018-09-16 22:38:02 -07002814 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002815 PyObject *cond = TOP();
2816 int err;
2817 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002818 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002820 FAST_DISPATCH();
2821 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002822 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002823 JUMPTO(oparg);
2824 FAST_DISPATCH();
2825 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002826 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002827 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002828 JUMPTO(oparg);
2829 }
2830 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002831 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002832 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002833 }
2834 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002835 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002836 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002837 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002838
Benjamin Petersonddd19492018-09-16 22:38:02 -07002839 case TARGET(JUMP_ABSOLUTE): {
2840 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002841 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00002842#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002843 /* Enabling this path speeds-up all while and for-loops by bypassing
2844 the per-loop checks for signals. By default, this should be turned-off
2845 because it prevents detection of a control-break in tight loops like
2846 "while 1: pass". Compile with this option turned-on when you need
2847 the speed-up and do not need break checking inside tight loops (ones
2848 that contain only instructions ending with FAST_DISPATCH).
2849 */
2850 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002851#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002852 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00002853#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002854 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002855
Benjamin Petersonddd19492018-09-16 22:38:02 -07002856 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002857 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002858 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002859 PyObject *iter = PyObject_GetIter(iterable);
2860 Py_DECREF(iterable);
2861 SET_TOP(iter);
2862 if (iter == NULL)
2863 goto error;
2864 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002865 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04002866 DISPATCH();
2867 }
2868
Benjamin Petersonddd19492018-09-16 22:38:02 -07002869 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04002870 /* before: [obj]; after [getiter(obj)] */
2871 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04002872 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04002873 if (PyCoro_CheckExact(iterable)) {
2874 /* `iterable` is a coroutine */
2875 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
2876 /* and it is used in a 'yield from' expression of a
2877 regular generator. */
2878 Py_DECREF(iterable);
2879 SET_TOP(NULL);
2880 PyErr_SetString(PyExc_TypeError,
2881 "cannot 'yield from' a coroutine object "
2882 "in a non-coroutine generator");
2883 goto error;
2884 }
2885 }
2886 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04002887 /* `iterable` is not a generator. */
2888 iter = PyObject_GetIter(iterable);
2889 Py_DECREF(iterable);
2890 SET_TOP(iter);
2891 if (iter == NULL)
2892 goto error;
2893 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002894 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 DISPATCH();
2896 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002897
Benjamin Petersonddd19492018-09-16 22:38:02 -07002898 case TARGET(FOR_ITER): {
2899 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002900 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 PyObject *iter = TOP();
2902 PyObject *next = (*iter->ob_type->tp_iternext)(iter);
2903 if (next != NULL) {
2904 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 PREDICT(STORE_FAST);
2906 PREDICT(UNPACK_SEQUENCE);
2907 DISPATCH();
2908 }
2909 if (PyErr_Occurred()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002910 if (!PyErr_ExceptionMatches(PyExc_StopIteration))
2911 goto error;
Guido van Rossum8820c232013-11-21 11:30:06 -08002912 else if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01002913 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002914 PyErr_Clear();
2915 }
2916 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00002917 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002918 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002919 JUMPBY(oparg);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002920 PREDICT(POP_BLOCK);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002921 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002922 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002923
Benjamin Petersonddd19492018-09-16 22:38:02 -07002924 case TARGET(SETUP_FINALLY): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002925 /* NOTE: If you add any new block-setup opcodes that
2926 are not try/except/finally handlers, you may need
2927 to update the PyGen_NeedsFinalizing() function.
2928 */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002929
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002930 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002931 STACK_LEVEL());
2932 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002934
Benjamin Petersonddd19492018-09-16 22:38:02 -07002935 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002936 _Py_IDENTIFIER(__aexit__);
2937 _Py_IDENTIFIER(__aenter__);
2938
2939 PyObject *mgr = TOP();
2940 PyObject *exit = special_lookup(mgr, &PyId___aexit__),
2941 *enter;
2942 PyObject *res;
2943 if (exit == NULL)
2944 goto error;
2945 SET_TOP(exit);
2946 enter = special_lookup(mgr, &PyId___aenter__);
2947 Py_DECREF(mgr);
2948 if (enter == NULL)
2949 goto error;
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002950 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04002951 Py_DECREF(enter);
2952 if (res == NULL)
2953 goto error;
2954 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002955 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002956 DISPATCH();
2957 }
2958
Benjamin Petersonddd19492018-09-16 22:38:02 -07002959 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04002960 PyObject *res = POP();
2961 /* Setup the finally block before pushing the result
2962 of __aenter__ on the stack. */
2963 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2964 STACK_LEVEL());
2965 PUSH(res);
2966 DISPATCH();
2967 }
2968
Benjamin Petersonddd19492018-09-16 22:38:02 -07002969 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05002970 _Py_IDENTIFIER(__exit__);
2971 _Py_IDENTIFIER(__enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002972 PyObject *mgr = TOP();
Raymond Hettingera3fec152016-11-21 17:24:23 -08002973 PyObject *enter = special_lookup(mgr, &PyId___enter__), *exit;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002974 PyObject *res;
Raymond Hettingera3fec152016-11-21 17:24:23 -08002975 if (enter == NULL)
2976 goto error;
2977 exit = special_lookup(mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002978 if (exit == NULL) {
2979 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08002981 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002982 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002983 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01002984 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002985 Py_DECREF(enter);
2986 if (res == NULL)
2987 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 /* Setup the finally block before pushing the result
2989 of __enter__ on the stack. */
2990 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
2991 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002992
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002993 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 DISPATCH();
2995 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00002996
Benjamin Petersonddd19492018-09-16 22:38:02 -07002997 case TARGET(WITH_CLEANUP_START): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002998 /* At the top of the stack are 1 or 6 values indicating
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 how/why we entered the finally clause:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003000 - TOP = NULL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003001 - (TOP, SECOND, THIRD) = exc_info()
3002 (FOURTH, FITH, SIXTH) = previous exception for EXCEPT_HANDLER
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003003 Below them is EXIT, the context.__exit__ or context.__aexit__
3004 bound method.
3005 In the first case, we must call
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003006 EXIT(None, None, None)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003007 otherwise we must call
3008 EXIT(TOP, SECOND, THIRD)
Christian Heimesdd15f6c2008-03-16 00:07:10 +00003009
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003010 In the first case, we remove EXIT from the
3011 stack, leaving TOP, and push TOP on the stack.
3012 Otherwise we shift the bottom 3 values of the
3013 stack down, replace the empty spot with NULL, and push
3014 None on the stack.
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00003015
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003016 Finally we push the result of the call.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003017 */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003018 PyObject *stack[3];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003020 PyObject *exc, *val, *tb, *res;
3021
3022 val = tb = Py_None;
3023 exc = TOP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003024 if (exc == NULL) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003025 STACK_SHRINK(1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003026 exit_func = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003027 SET_TOP(exc);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003028 exc = Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003029 }
3030 else {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003031 assert(PyExceptionClass_Check(exc));
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003032 PyObject *tp2, *exc2, *tb2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003033 PyTryBlock *block;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 val = SECOND();
3035 tb = THIRD();
3036 tp2 = FOURTH();
3037 exc2 = PEEK(5);
3038 tb2 = PEEK(6);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003039 exit_func = PEEK(7);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003040 SET_VALUE(7, tb2);
3041 SET_VALUE(6, exc2);
3042 SET_VALUE(5, tp2);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003043 /* UNWIND_EXCEPT_HANDLER will pop this off. */
3044 SET_FOURTH(NULL);
3045 /* We just shifted the stack down, so we have
3046 to tell the except handler block that the
3047 values are lower than it expects. */
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003048 assert(f->f_iblock > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003049 block = &f->f_blockstack[f->f_iblock - 1];
3050 assert(block->b_type == EXCEPT_HANDLER);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003051 assert(block->b_level > 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 block->b_level--;
3053 }
Victor Stinner842cfff2016-12-01 14:45:31 +01003054
3055 stack[0] = exc;
3056 stack[1] = val;
3057 stack[2] = tb;
3058 res = _PyObject_FastCall(exit_func, stack, 3);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003059 Py_DECREF(exit_func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003060 if (res == NULL)
3061 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003062
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003063 Py_INCREF(exc); /* Duplicating the exception on the stack */
Yury Selivanov75445082015-05-11 22:57:16 -04003064 PUSH(exc);
3065 PUSH(res);
3066 PREDICT(WITH_CLEANUP_FINISH);
3067 DISPATCH();
3068 }
3069
Benjamin Petersonddd19492018-09-16 22:38:02 -07003070 case TARGET(WITH_CLEANUP_FINISH): {
3071 PREDICTED(WITH_CLEANUP_FINISH);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003072 /* TOP = the result of calling the context.__exit__ bound method
3073 SECOND = either None or exception type
3074
3075 If SECOND is None below is NULL or the return address,
3076 otherwise below are 7 values representing an exception.
3077 */
Yury Selivanov75445082015-05-11 22:57:16 -04003078 PyObject *res = POP();
3079 PyObject *exc = POP();
3080 int err;
3081
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003082 if (exc != Py_None)
3083 err = PyObject_IsTrue(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 else
3085 err = 0;
Yury Selivanov75445082015-05-11 22:57:16 -04003086
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003087 Py_DECREF(res);
Nick Coghlanbaaadbf2015-05-13 15:54:02 +10003088 Py_DECREF(exc);
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 if (err < 0)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003091 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003092 else if (err > 0) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003093 /* There was an exception and a True return.
3094 * We must manually unwind the EXCEPT_HANDLER block
3095 * which was created when the exception was caught,
Quan Tian3bd0d622018-10-20 05:30:03 +08003096 * otherwise the stack will be in an inconsistent state.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003097 */
3098 PyTryBlock *b = PyFrame_BlockPop(f);
3099 assert(b->b_type == EXCEPT_HANDLER);
3100 UNWIND_EXCEPT_HANDLER(b);
3101 PUSH(NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003102 }
3103 PREDICT(END_FINALLY);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003104 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003105 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003106
Benjamin Petersonddd19492018-09-16 22:38:02 -07003107 case TARGET(LOAD_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003108 /* Designed to work in tamdem with CALL_METHOD. */
3109 PyObject *name = GETITEM(names, oparg);
3110 PyObject *obj = TOP();
3111 PyObject *meth = NULL;
3112
3113 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3114
Yury Selivanovf2392132016-12-13 19:03:51 -05003115 if (meth == NULL) {
3116 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003117 goto error;
3118 }
3119
3120 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003121 /* We can bypass temporary bound method object.
3122 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003123
INADA Naoki015bce62017-01-16 17:23:30 +09003124 meth | self | arg1 | ... | argN
3125 */
3126 SET_TOP(meth);
3127 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003128 }
3129 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003130 /* meth is not an unbound method (but a regular attr, or
3131 something was returned by a descriptor protocol). Set
3132 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003133 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003134
3135 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003136 */
INADA Naoki015bce62017-01-16 17:23:30 +09003137 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003138 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003139 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003140 }
3141 DISPATCH();
3142 }
3143
Benjamin Petersonddd19492018-09-16 22:38:02 -07003144 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003145 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003146 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003147
3148 sp = stack_pointer;
3149
INADA Naoki015bce62017-01-16 17:23:30 +09003150 meth = PEEK(oparg + 2);
3151 if (meth == NULL) {
3152 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3153 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003154
3155 Stack layout:
3156
INADA Naoki015bce62017-01-16 17:23:30 +09003157 ... | NULL | callable | arg1 | ... | argN
3158 ^- TOP()
3159 ^- (-oparg)
3160 ^- (-oparg-1)
3161 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003162
Ville Skyttä49b27342017-08-03 09:00:59 +03003163 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003164 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003165 */
Yury Selivanovf2392132016-12-13 19:03:51 -05003166 res = call_function(&sp, oparg, NULL);
3167 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003168 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003169 }
3170 else {
3171 /* This is a method call. Stack layout:
3172
INADA Naoki015bce62017-01-16 17:23:30 +09003173 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003174 ^- TOP()
3175 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003176 ^- (-oparg-1)
3177 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003178
INADA Naoki015bce62017-01-16 17:23:30 +09003179 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003180 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003181 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003182 */
3183 res = call_function(&sp, oparg + 1, NULL);
3184 stack_pointer = sp;
3185 }
3186
3187 PUSH(res);
3188 if (res == NULL)
3189 goto error;
3190 DISPATCH();
3191 }
3192
Benjamin Petersonddd19492018-09-16 22:38:02 -07003193 case TARGET(CALL_FUNCTION): {
3194 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003195 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003196 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003197 res = call_function(&sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003198 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003199 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003200 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003201 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003202 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003203 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003204 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003205
Benjamin Petersonddd19492018-09-16 22:38:02 -07003206 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003207 PyObject **sp, *res, *names;
3208
3209 names = POP();
3210 assert(PyTuple_CheckExact(names) && PyTuple_GET_SIZE(names) <= oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003211 sp = stack_pointer;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003212 res = call_function(&sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003213 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003214 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003215 Py_DECREF(names);
3216
3217 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003218 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003219 }
3220 DISPATCH();
3221 }
3222
Benjamin Petersonddd19492018-09-16 22:38:02 -07003223 case TARGET(CALL_FUNCTION_EX): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003224 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003225 if (oparg & 0x01) {
3226 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003227 if (!PyDict_CheckExact(kwargs)) {
3228 PyObject *d = PyDict_New();
3229 if (d == NULL)
3230 goto error;
3231 if (PyDict_Update(d, kwargs) != 0) {
3232 Py_DECREF(d);
3233 /* PyDict_Update raises attribute
3234 * error (percolated from an attempt
3235 * to get 'keys' attribute) instead of
3236 * a type error if its second argument
3237 * is not a mapping.
3238 */
3239 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003240 format_kwargs_mapping_error(SECOND(), kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003241 }
Victor Stinnereece2222016-09-12 11:16:37 +02003242 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003243 goto error;
3244 }
3245 Py_DECREF(kwargs);
3246 kwargs = d;
3247 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003248 assert(PyDict_CheckExact(kwargs));
3249 }
3250 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003251 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003252 if (!PyTuple_CheckExact(callargs)) {
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03003253 if (check_args_iterable(func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003254 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003255 goto error;
3256 }
3257 Py_SETREF(callargs, PySequence_Tuple(callargs));
3258 if (callargs == NULL) {
3259 goto error;
3260 }
3261 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003262 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003263
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003264 result = do_call_core(func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003265 Py_DECREF(func);
3266 Py_DECREF(callargs);
3267 Py_XDECREF(kwargs);
3268
3269 SET_TOP(result);
3270 if (result == NULL) {
3271 goto error;
3272 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003273 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003275
Benjamin Petersonddd19492018-09-16 22:38:02 -07003276 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003277 PyObject *qualname = POP();
3278 PyObject *codeobj = POP();
3279 PyFunctionObject *func = (PyFunctionObject *)
3280 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003281
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003282 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003283 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003284 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003286 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003287
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003288 if (oparg & 0x08) {
3289 assert(PyTuple_CheckExact(TOP()));
3290 func ->func_closure = POP();
3291 }
3292 if (oparg & 0x04) {
3293 assert(PyDict_CheckExact(TOP()));
3294 func->func_annotations = POP();
3295 }
3296 if (oparg & 0x02) {
3297 assert(PyDict_CheckExact(TOP()));
3298 func->func_kwdefaults = POP();
3299 }
3300 if (oparg & 0x01) {
3301 assert(PyTuple_CheckExact(TOP()));
3302 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003303 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003304
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003305 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003306 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003307 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003308
Benjamin Petersonddd19492018-09-16 22:38:02 -07003309 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003310 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003311 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003312 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003313 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003314 step = NULL;
3315 stop = POP();
3316 start = TOP();
3317 slice = PySlice_New(start, stop, step);
3318 Py_DECREF(start);
3319 Py_DECREF(stop);
3320 Py_XDECREF(step);
3321 SET_TOP(slice);
3322 if (slice == NULL)
3323 goto error;
3324 DISPATCH();
3325 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003326
Benjamin Petersonddd19492018-09-16 22:38:02 -07003327 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003328 /* Handles f-string value formatting. */
3329 PyObject *result;
3330 PyObject *fmt_spec;
3331 PyObject *value;
3332 PyObject *(*conv_fn)(PyObject *);
3333 int which_conversion = oparg & FVC_MASK;
3334 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3335
3336 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003337 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003338
3339 /* See if any conversion is specified. */
3340 switch (which_conversion) {
3341 case FVC_STR: conv_fn = PyObject_Str; break;
3342 case FVC_REPR: conv_fn = PyObject_Repr; break;
3343 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
3344
3345 /* Must be 0 (meaning no conversion), since only four
3346 values are allowed by (oparg & FVC_MASK). */
3347 default: conv_fn = NULL; break;
3348 }
3349
3350 /* If there's a conversion function, call it and replace
3351 value with that result. Otherwise, just use value,
3352 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003353 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003354 result = conv_fn(value);
3355 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003356 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003357 Py_XDECREF(fmt_spec);
3358 goto error;
3359 }
3360 value = result;
3361 }
3362
3363 /* If value is a unicode object, and there's no fmt_spec,
3364 then we know the result of format(value) is value
3365 itself. In that case, skip calling format(). I plan to
3366 move this optimization in to PyObject_Format()
3367 itself. */
3368 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3369 /* Do nothing, just transfer ownership to result. */
3370 result = value;
3371 } else {
3372 /* Actually call format(). */
3373 result = PyObject_Format(value, fmt_spec);
3374 Py_DECREF(value);
3375 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003376 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003377 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003378 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003379 }
3380
Eric V. Smith135d5f42016-02-05 18:23:08 -05003381 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003382 DISPATCH();
3383 }
3384
Benjamin Petersonddd19492018-09-16 22:38:02 -07003385 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003386 int oldoparg = oparg;
3387 NEXTOPARG();
3388 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003389 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003390 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003391
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003392
Antoine Pitrou042b1282010-08-13 21:15:58 +00003393#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003394 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003395#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003396 default:
3397 fprintf(stderr,
3398 "XXX lineno: %d, opcode: %d\n",
3399 PyFrame_GetLineNumber(f),
3400 opcode);
3401 PyErr_SetString(PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003402 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00003403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003404 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00003405
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003406 /* This should never be reached. Every opcode should end with DISPATCH()
3407 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07003408 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00003409
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003410error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02003412#ifdef NDEBUG
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003413 if (!PyErr_Occurred())
3414 PyErr_SetString(PyExc_SystemError,
3415 "error return without exception set");
Victor Stinner365b6932013-07-12 00:11:58 +02003416#else
3417 assert(PyErr_Occurred());
3418#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00003419
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003420 /* Log traceback info. */
3421 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003422
Benjamin Peterson51f46162013-01-23 08:38:47 -05003423 if (tstate->c_tracefunc != NULL)
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003424 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
3425 tstate, f);
Guido van Rossumac7be682001-01-17 15:42:30 +00003426
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003427exception_unwind:
3428 /* Unwind stacks if an exception occurred */
3429 while (f->f_iblock > 0) {
3430 /* Pop the current block. */
3431 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00003432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 if (b->b_type == EXCEPT_HANDLER) {
3434 UNWIND_EXCEPT_HANDLER(b);
3435 continue;
3436 }
3437 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003438 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003439 PyObject *exc, *val, *tb;
3440 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01003441 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003442 /* Beware, this invalidates all b->b_* fields */
3443 PyFrame_BlockSetup(f, EXCEPT_HANDLER, -1, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01003444 PUSH(exc_info->exc_traceback);
3445 PUSH(exc_info->exc_value);
3446 if (exc_info->exc_type != NULL) {
3447 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003448 }
3449 else {
3450 Py_INCREF(Py_None);
3451 PUSH(Py_None);
3452 }
3453 PyErr_Fetch(&exc, &val, &tb);
3454 /* Make the raw exception data
3455 available to the handler,
3456 so a program can emulate the
3457 Python main loop. */
3458 PyErr_NormalizeException(
3459 &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02003460 if (tb != NULL)
3461 PyException_SetTraceback(val, tb);
3462 else
3463 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003464 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01003465 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01003467 exc_info->exc_value = val;
3468 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003469 if (tb == NULL)
3470 tb = Py_None;
3471 Py_INCREF(tb);
3472 PUSH(tb);
3473 PUSH(val);
3474 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003475 JUMPTO(handler);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003476 /* Resume normal execution */
3477 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003478 }
3479 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00003480
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003481 /* End the loop as we still have an error */
3482 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00003484
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003485 /* Pop remaining stack entries. */
3486 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003487 PyObject *o = POP();
3488 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003489 }
Guido van Rossum35974fb2001-12-06 21:28:18 +00003490
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003491 assert(retval == NULL);
3492 assert(PyErr_Occurred());
Guido van Rossumac7be682001-01-17 15:42:30 +00003493
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003494return_or_yield:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003495 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05003496 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003497 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
3498 tstate, f, PyTrace_RETURN, retval)) {
3499 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003500 }
3501 }
3502 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003503 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
3504 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003505 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003506 }
3507 }
3508 }
Guido van Rossuma4240131997-01-21 21:18:36 +00003509
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003510 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00003511exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07003512 if (PyDTrace_FUNCTION_RETURN_ENABLED())
3513 dtrace_function_return(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003514 Py_LeaveRecursiveCall();
Antoine Pitrou58720d62013-08-05 23:26:40 +02003515 f->f_executing = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003516 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00003517
Victor Stinnerefde1462015-03-21 15:04:43 +01003518 return _Py_CheckFunctionResult(NULL, retval, "PyEval_EvalFrameEx");
Guido van Rossum374a9221991-04-04 10:40:29 +00003519}
3520
Benjamin Petersonb204a422011-06-05 22:04:07 -05003521static void
Benjamin Petersone109c702011-06-24 09:37:26 -05003522format_missing(const char *kind, PyCodeObject *co, PyObject *names)
3523{
3524 int err;
3525 Py_ssize_t len = PyList_GET_SIZE(names);
3526 PyObject *name_str, *comma, *tail, *tmp;
3527
3528 assert(PyList_CheckExact(names));
3529 assert(len >= 1);
3530 /* Deal with the joys of natural language. */
3531 switch (len) {
3532 case 1:
3533 name_str = PyList_GET_ITEM(names, 0);
3534 Py_INCREF(name_str);
3535 break;
3536 case 2:
3537 name_str = PyUnicode_FromFormat("%U and %U",
3538 PyList_GET_ITEM(names, len - 2),
3539 PyList_GET_ITEM(names, len - 1));
3540 break;
3541 default:
3542 tail = PyUnicode_FromFormat(", %U, and %U",
3543 PyList_GET_ITEM(names, len - 2),
3544 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07003545 if (tail == NULL)
3546 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05003547 /* Chop off the last two objects in the list. This shouldn't actually
3548 fail, but we can't be too careful. */
3549 err = PyList_SetSlice(names, len - 2, len, NULL);
3550 if (err == -1) {
3551 Py_DECREF(tail);
3552 return;
3553 }
3554 /* Stitch everything up into a nice comma-separated list. */
3555 comma = PyUnicode_FromString(", ");
3556 if (comma == NULL) {
3557 Py_DECREF(tail);
3558 return;
3559 }
3560 tmp = PyUnicode_Join(comma, names);
3561 Py_DECREF(comma);
3562 if (tmp == NULL) {
3563 Py_DECREF(tail);
3564 return;
3565 }
3566 name_str = PyUnicode_Concat(tmp, tail);
3567 Py_DECREF(tmp);
3568 Py_DECREF(tail);
3569 break;
3570 }
3571 if (name_str == NULL)
3572 return;
3573 PyErr_Format(PyExc_TypeError,
3574 "%U() missing %i required %s argument%s: %U",
3575 co->co_name,
3576 len,
3577 kind,
3578 len == 1 ? "" : "s",
3579 name_str);
3580 Py_DECREF(name_str);
3581}
3582
3583static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003584missing_arguments(PyCodeObject *co, Py_ssize_t missing, Py_ssize_t defcount,
Benjamin Petersone109c702011-06-24 09:37:26 -05003585 PyObject **fastlocals)
3586{
Victor Stinner74319ae2016-08-25 00:04:09 +02003587 Py_ssize_t i, j = 0;
3588 Py_ssize_t start, end;
3589 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05003590 const char *kind = positional ? "positional" : "keyword-only";
3591 PyObject *missing_names;
3592
3593 /* Compute the names of the arguments that are missing. */
3594 missing_names = PyList_New(missing);
3595 if (missing_names == NULL)
3596 return;
3597 if (positional) {
3598 start = 0;
3599 end = co->co_argcount - defcount;
3600 }
3601 else {
3602 start = co->co_argcount;
3603 end = start + co->co_kwonlyargcount;
3604 }
3605 for (i = start; i < end; i++) {
3606 if (GETLOCAL(i) == NULL) {
3607 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
3608 PyObject *name = PyObject_Repr(raw);
3609 if (name == NULL) {
3610 Py_DECREF(missing_names);
3611 return;
3612 }
3613 PyList_SET_ITEM(missing_names, j++, name);
3614 }
3615 }
3616 assert(j == missing);
3617 format_missing(kind, co, missing_names);
3618 Py_DECREF(missing_names);
3619}
3620
3621static void
Victor Stinner74319ae2016-08-25 00:04:09 +02003622too_many_positional(PyCodeObject *co, Py_ssize_t given, Py_ssize_t defcount,
3623 PyObject **fastlocals)
Benjamin Petersonb204a422011-06-05 22:04:07 -05003624{
3625 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02003626 Py_ssize_t kwonly_given = 0;
3627 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003628 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02003629 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003630
Benjamin Petersone109c702011-06-24 09:37:26 -05003631 assert((co->co_flags & CO_VARARGS) == 0);
3632 /* Count missing keyword-only args. */
Victor Stinner74319ae2016-08-25 00:04:09 +02003633 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
3634 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003635 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02003636 }
3637 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003638 if (defcount) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003639 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003640 plural = 1;
Victor Stinner74319ae2016-08-25 00:04:09 +02003641 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003642 }
3643 else {
Victor Stinner74319ae2016-08-25 00:04:09 +02003644 plural = (co_argcount != 1);
3645 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003646 }
3647 if (sig == NULL)
3648 return;
3649 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02003650 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
3651 kwonly_sig = PyUnicode_FromFormat(format,
3652 given != 1 ? "s" : "",
3653 kwonly_given,
3654 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05003655 if (kwonly_sig == NULL) {
3656 Py_DECREF(sig);
3657 return;
3658 }
3659 }
3660 else {
3661 /* This will not fail. */
3662 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05003663 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003664 }
3665 PyErr_Format(PyExc_TypeError,
Victor Stinner74319ae2016-08-25 00:04:09 +02003666 "%U() takes %U positional argument%s but %zd%U %s given",
Benjamin Petersonb204a422011-06-05 22:04:07 -05003667 co->co_name,
3668 sig,
3669 plural ? "s" : "",
3670 given,
3671 kwonly_sig,
3672 given == 1 && !kwonly_given ? "was" : "were");
3673 Py_DECREF(sig);
3674 Py_DECREF(kwonly_sig);
3675}
3676
Guido van Rossumc2e20742006-02-27 22:32:47 +00003677/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02003678 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00003679 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00003680
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01003681PyObject *
Victor Stinner40ee3012014-06-16 15:59:28 +02003682_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003683 PyObject *const *args, Py_ssize_t argcount,
3684 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03003685 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003686 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02003687 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003688 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00003689{
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00003690 PyCodeObject* co = (PyCodeObject*)_co;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02003691 PyFrameObject *f;
3692 PyObject *retval = NULL;
3693 PyObject **fastlocals, **freevars;
Victor Stinnerc7020012016-08-16 23:40:29 +02003694 PyThreadState *tstate;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003695 PyObject *x, *u;
Victor Stinner17061a92016-08-16 23:39:42 +02003696 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
3697 Py_ssize_t i, n;
Victor Stinnerc7020012016-08-16 23:40:29 +02003698 PyObject *kwdict;
Tim Peters5ca576e2001-06-18 22:08:13 +00003699
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003700 if (globals == NULL) {
3701 PyErr_SetString(PyExc_SystemError,
3702 "PyEval_EvalCodeEx: NULL globals");
3703 return NULL;
3704 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003705
Victor Stinnerc7020012016-08-16 23:40:29 +02003706 /* Create the frame */
Victor Stinner50b48572018-11-01 01:51:40 +01003707 tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003708 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003709 f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02003710 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003711 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02003712 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003713 fastlocals = f->f_localsplus;
3714 freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00003715
Victor Stinnerc7020012016-08-16 23:40:29 +02003716 /* Create a dictionary for keyword parameters (**kwags) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003717 if (co->co_flags & CO_VARKEYWORDS) {
3718 kwdict = PyDict_New();
3719 if (kwdict == NULL)
3720 goto fail;
3721 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02003722 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003723 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02003724 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003725 SETLOCAL(i, kwdict);
3726 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003727 else {
3728 kwdict = NULL;
3729 }
3730
3731 /* Copy positional arguments into local variables */
3732 if (argcount > co->co_argcount) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003733 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02003734 }
3735 else {
3736 n = argcount;
3737 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003738 for (i = 0; i < n; i++) {
3739 x = args[i];
3740 Py_INCREF(x);
3741 SETLOCAL(i, x);
3742 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003743
3744 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003745 if (co->co_flags & CO_VARARGS) {
3746 u = PyTuple_New(argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02003747 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003748 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02003749 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003750 SETLOCAL(total_args, u);
3751 for (i = n; i < argcount; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 x = args[i];
3753 Py_INCREF(x);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003754 PyTuple_SET_ITEM(u, i-n, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003755 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003756 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003757
Serhiy Storchakab7281052016-09-12 00:52:40 +03003758 /* Handle keyword arguments passed as two strided arrays */
3759 kwcount *= kwstep;
3760 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003761 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03003762 PyObject *keyword = kwnames[i];
3763 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02003764 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02003765
Benjamin Petersonb204a422011-06-05 22:04:07 -05003766 if (keyword == NULL || !PyUnicode_Check(keyword)) {
3767 PyErr_Format(PyExc_TypeError,
3768 "%U() keywords must be strings",
3769 co->co_name);
3770 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003772
Benjamin Petersonb204a422011-06-05 22:04:07 -05003773 /* Speed hack: do raw pointer compares. As names are
3774 normally interned this should almost always hit. */
3775 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
3776 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003777 PyObject *name = co_varnames[j];
3778 if (name == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003779 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003780 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003781 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003782
Benjamin Petersonb204a422011-06-05 22:04:07 -05003783 /* Slow fallback, just in case */
3784 for (j = 0; j < total_args; j++) {
Victor Stinner6fea7f72016-08-22 23:17:30 +02003785 PyObject *name = co_varnames[j];
3786 int cmp = PyObject_RichCompareBool( keyword, name, Py_EQ);
3787 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003788 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003789 }
3790 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02003792 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003793 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003794
Victor Stinner231d1f32017-01-11 02:12:06 +01003795 assert(j >= total_args);
3796 if (kwdict == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05003797 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003798 "%U() got an unexpected keyword argument '%S'",
3799 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003800 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003801 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003802
Christian Heimes0bd447f2013-07-20 14:48:10 +02003803 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
3804 goto fail;
3805 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003806 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02003807
Benjamin Petersonb204a422011-06-05 22:04:07 -05003808 kw_found:
3809 if (GETLOCAL(j) != NULL) {
3810 PyErr_Format(PyExc_TypeError,
Victor Stinner6fea7f72016-08-22 23:17:30 +02003811 "%U() got multiple values for argument '%S'",
3812 co->co_name, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003813 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05003815 Py_INCREF(value);
3816 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003818
3819 /* Check the number of positional arguments */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003820 if (argcount > co->co_argcount && !(co->co_flags & CO_VARARGS)) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003821 too_many_positional(co, argcount, defcount, fastlocals);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003822 goto fail;
3823 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003824
3825 /* Add missing positional arguments (copy default values from defs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003826 if (argcount < co->co_argcount) {
Victor Stinner17061a92016-08-16 23:39:42 +02003827 Py_ssize_t m = co->co_argcount - defcount;
3828 Py_ssize_t missing = 0;
3829 for (i = argcount; i < m; i++) {
3830 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05003831 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02003832 }
3833 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003834 if (missing) {
3835 missing_arguments(co, missing, defcount, fastlocals);
3836 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003837 }
3838 if (n > m)
3839 i = n - m;
3840 else
3841 i = 0;
3842 for (; i < defcount; i++) {
3843 if (GETLOCAL(m+i) == NULL) {
3844 PyObject *def = defs[i];
3845 Py_INCREF(def);
3846 SETLOCAL(m+i, def);
3847 }
3848 }
3849 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003850
3851 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05003852 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02003853 Py_ssize_t missing = 0;
Benjamin Petersonb204a422011-06-05 22:04:07 -05003854 for (i = co->co_argcount; i < total_args; i++) {
3855 PyObject *name;
3856 if (GETLOCAL(i) != NULL)
3857 continue;
3858 name = PyTuple_GET_ITEM(co->co_varnames, i);
3859 if (kwdefs != NULL) {
3860 PyObject *def = PyDict_GetItem(kwdefs, name);
3861 if (def) {
3862 Py_INCREF(def);
3863 SETLOCAL(i, def);
3864 continue;
3865 }
3866 }
Benjamin Petersone109c702011-06-24 09:37:26 -05003867 missing++;
3868 }
3869 if (missing) {
3870 missing_arguments(co, missing, -1, fastlocals);
Benjamin Petersonb204a422011-06-05 22:04:07 -05003871 goto fail;
3872 }
3873 }
3874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003875 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05003876 vars into frame. */
3877 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003878 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02003879 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05003880 /* Possibly account for the cell variable being an argument. */
3881 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07003882 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05003883 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05003884 /* Clear the local copy. */
3885 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003886 }
3887 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05003888 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07003889 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05003890 if (c == NULL)
3891 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05003892 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003893 }
Victor Stinnerc7020012016-08-16 23:40:29 +02003894
3895 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05003896 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
3897 PyObject *o = PyTuple_GET_ITEM(closure, i);
3898 Py_INCREF(o);
3899 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003900 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003901
Yury Selivanoveb636452016-09-08 22:01:51 -07003902 /* Handle generator/coroutine/asynchronous generator */
3903 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003904 PyObject *gen;
Yury Selivanov94c22632015-06-04 10:16:51 -04003905 PyObject *coro_wrapper = tstate->coroutine_wrapper;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003906 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04003907
3908 if (is_coro && tstate->in_coroutine_wrapper) {
3909 assert(coro_wrapper != NULL);
3910 PyErr_Format(PyExc_RuntimeError,
3911 "coroutine wrapper %.200R attempted "
3912 "to recursively wrap %.200R",
3913 coro_wrapper,
3914 co);
3915 goto fail;
3916 }
Yury Selivanov75445082015-05-11 22:57:16 -04003917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003918 /* Don't need to keep the reference to f_back, it will be set
3919 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02003920 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00003921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003922 /* Create a new generator that owns the ready to run frame
3923 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04003924 if (is_coro) {
3925 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07003926 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
3927 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003928 } else {
3929 gen = PyGen_NewWithQualName(f, name, qualname);
3930 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003931 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003932 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003933 }
INADA Naoki9c157762016-12-26 18:52:46 +09003934
INADA Naoki6a3cedf2016-12-26 18:01:46 +09003935 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04003936
Yury Selivanov94c22632015-06-04 10:16:51 -04003937 if (is_coro && coro_wrapper != NULL) {
3938 PyObject *wrapped;
3939 tstate->in_coroutine_wrapper = 1;
3940 wrapped = PyObject_CallFunction(coro_wrapper, "N", gen);
3941 tstate->in_coroutine_wrapper = 0;
3942 return wrapped;
3943 }
Yury Selivanovaab3c4a2015-06-02 18:43:51 -04003944
Yury Selivanov75445082015-05-11 22:57:16 -04003945 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003946 }
Tim Peters5ca576e2001-06-18 22:08:13 +00003947
Victor Stinner59a73272016-12-09 18:51:13 +01003948 retval = PyEval_EvalFrameEx(f,0);
Tim Peters5ca576e2001-06-18 22:08:13 +00003949
Thomas Woutersce272b62007-09-19 21:19:28 +00003950fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00003951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003952 /* decref'ing the frame can cause __del__ methods to get invoked,
3953 which can call back into Python. While we're done with the
3954 current Python frame (f), the associated C stack is still in use,
3955 so recursion_depth must be boosted for the duration.
3956 */
3957 assert(tstate != NULL);
INADA Naoki5a625d02016-12-24 20:19:08 +09003958 if (Py_REFCNT(f) > 1) {
3959 Py_DECREF(f);
3960 _PyObject_GC_TRACK(f);
3961 }
3962 else {
3963 ++tstate->recursion_depth;
3964 Py_DECREF(f);
3965 --tstate->recursion_depth;
3966 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003967 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00003968}
3969
Victor Stinner40ee3012014-06-16 15:59:28 +02003970PyObject *
3971PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02003972 PyObject *const *args, int argcount,
3973 PyObject *const *kws, int kwcount,
3974 PyObject *const *defs, int defcount,
3975 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02003976{
3977 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003978 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06003979 kws, kws != NULL ? kws + 1 : NULL,
3980 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02003981 defs, defcount,
3982 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02003983 NULL, NULL);
3984}
Tim Peters5ca576e2001-06-18 22:08:13 +00003985
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003986static PyObject *
Benjamin Petersonce798522012-01-22 11:24:29 -05003987special_lookup(PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003988{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003989 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05003990 res = _PyObject_LookupSpecial(o, id);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003991 if (res == NULL && !PyErr_Occurred()) {
Benjamin Petersonce798522012-01-22 11:24:29 -05003992 PyErr_SetObject(PyExc_AttributeError, id->object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003993 return NULL;
3994 }
3995 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003996}
3997
3998
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00003999/* Logic for the raise statement (too complicated for inlining).
4000 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004001static int
Collin Winter828f04a2007-08-31 00:04:24 +00004002do_raise(PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004004 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004006 if (exc == NULL) {
4007 /* Reraise */
Victor Stinner50b48572018-11-01 01:51:40 +01004008 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannonae3087c2017-10-22 22:41:51 +01004009 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004010 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004011 type = exc_info->exc_type;
4012 value = exc_info->exc_value;
4013 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004014 if (type == Py_None || type == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004015 PyErr_SetString(PyExc_RuntimeError,
4016 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004017 return 0;
4018 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004019 Py_XINCREF(type);
4020 Py_XINCREF(value);
4021 Py_XINCREF(tb);
4022 PyErr_Restore(type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004023 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004024 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004026 /* We support the following forms of raise:
4027 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004028 raise <instance>
4029 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004031 if (PyExceptionClass_Check(exc)) {
4032 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004033 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004034 if (value == NULL)
4035 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004036 if (!PyExceptionInstance_Check(value)) {
4037 PyErr_Format(PyExc_TypeError,
4038 "calling %R should have returned an instance of "
4039 "BaseException, not %R",
4040 type, Py_TYPE(value));
4041 goto raise_error;
4042 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004043 }
4044 else if (PyExceptionInstance_Check(exc)) {
4045 value = exc;
4046 type = PyExceptionInstance_Class(exc);
4047 Py_INCREF(type);
4048 }
4049 else {
4050 /* Not something you can raise. You get an exception
4051 anyway, just not what you specified :-) */
4052 Py_DECREF(exc);
4053 PyErr_SetString(PyExc_TypeError,
4054 "exceptions must derive from BaseException");
4055 goto raise_error;
4056 }
Collin Winter828f04a2007-08-31 00:04:24 +00004057
Serhiy Storchakac0191582016-09-27 11:37:10 +03004058 assert(type != NULL);
4059 assert(value != NULL);
4060
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 if (cause) {
4062 PyObject *fixed_cause;
4063 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004064 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 if (fixed_cause == NULL)
4066 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004067 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004069 else if (PyExceptionInstance_Check(cause)) {
4070 fixed_cause = cause;
4071 }
4072 else if (cause == Py_None) {
4073 Py_DECREF(cause);
4074 fixed_cause = NULL;
4075 }
4076 else {
4077 PyErr_SetString(PyExc_TypeError,
4078 "exception causes must derive from "
4079 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004080 goto raise_error;
4081 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004082 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004083 }
Collin Winter828f04a2007-08-31 00:04:24 +00004084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004085 PyErr_SetObject(type, value);
4086 /* PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004087 Py_DECREF(value);
4088 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004089 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004090
4091raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004092 Py_XDECREF(value);
4093 Py_XDECREF(type);
4094 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004095 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004096}
4097
Tim Petersd6d010b2001-06-21 02:49:55 +00004098/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004099 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004100
Guido van Rossum0368b722007-05-11 16:50:42 +00004101 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4102 with a variable target.
4103*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004104
Barry Warsawe42b18f1997-08-25 22:13:04 +00004105static int
Guido van Rossum0368b722007-05-11 16:50:42 +00004106unpack_iterable(PyObject *v, int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004107{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004108 int i = 0, j = 0;
4109 Py_ssize_t ll = 0;
4110 PyObject *it; /* iter(v) */
4111 PyObject *w;
4112 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004114 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004115
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004116 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004117 if (it == NULL) {
4118 if (PyErr_ExceptionMatches(PyExc_TypeError) &&
4119 v->ob_type->tp_iter == NULL && !PySequence_Check(v))
4120 {
4121 PyErr_Format(PyExc_TypeError,
4122 "cannot unpack non-iterable %.200s object",
4123 v->ob_type->tp_name);
4124 }
4125 return 0;
4126 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004128 for (; i < argcnt; i++) {
4129 w = PyIter_Next(it);
4130 if (w == NULL) {
4131 /* Iterator done, via error or exhaustion. */
4132 if (!PyErr_Occurred()) {
R David Murray4171bbe2015-04-15 17:08:45 -04004133 if (argcntafter == -1) {
4134 PyErr_Format(PyExc_ValueError,
4135 "not enough values to unpack (expected %d, got %d)",
4136 argcnt, i);
4137 }
4138 else {
4139 PyErr_Format(PyExc_ValueError,
4140 "not enough values to unpack "
4141 "(expected at least %d, got %d)",
4142 argcnt + argcntafter, i);
4143 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004144 }
4145 goto Error;
4146 }
4147 *--sp = w;
4148 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 if (argcntafter == -1) {
4151 /* We better have exhausted the iterator now. */
4152 w = PyIter_Next(it);
4153 if (w == NULL) {
4154 if (PyErr_Occurred())
4155 goto Error;
4156 Py_DECREF(it);
4157 return 1;
4158 }
4159 Py_DECREF(w);
R David Murray4171bbe2015-04-15 17:08:45 -04004160 PyErr_Format(PyExc_ValueError,
4161 "too many values to unpack (expected %d)",
4162 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004163 goto Error;
4164 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004165
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004166 l = PySequence_List(it);
4167 if (l == NULL)
4168 goto Error;
4169 *--sp = l;
4170 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004171
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004172 ll = PyList_GET_SIZE(l);
4173 if (ll < argcntafter) {
R David Murray4171bbe2015-04-15 17:08:45 -04004174 PyErr_Format(PyExc_ValueError,
4175 "not enough values to unpack (expected at least %d, got %zd)",
4176 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004177 goto Error;
4178 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004179
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004180 /* Pop the "after-variable" args off the list. */
4181 for (j = argcntafter; j > 0; j--, i++) {
4182 *--sp = PyList_GET_ITEM(l, ll - j);
4183 }
4184 /* Resize the list. */
4185 Py_SIZE(l) = ll - argcntafter;
4186 Py_DECREF(it);
4187 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004188
Tim Petersd6d010b2001-06-21 02:49:55 +00004189Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004190 for (; i > 0; i--, sp++)
4191 Py_DECREF(*sp);
4192 Py_XDECREF(it);
4193 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004194}
4195
4196
Guido van Rossum96a42c81992-01-12 02:29:51 +00004197#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004198static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004199prtrace(PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004201 printf("%s ", str);
4202 if (PyObject_Print(v, stdout, 0) != 0)
4203 PyErr_Clear(); /* Don't know what else to do */
4204 printf("\n");
4205 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004206}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004207#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004208
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004209static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004210call_exc_trace(Py_tracefunc func, PyObject *self,
4211 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004212{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004213 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004214 int err;
Antoine Pitrou89335212013-11-23 14:05:23 +01004215 PyErr_Fetch(&type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004216 if (value == NULL) {
4217 value = Py_None;
4218 Py_INCREF(value);
4219 }
Antoine Pitrou89335212013-11-23 14:05:23 +01004220 PyErr_NormalizeException(&type, &value, &orig_traceback);
4221 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004222 arg = PyTuple_Pack(3, type, value, traceback);
4223 if (arg == NULL) {
Antoine Pitrou89335212013-11-23 14:05:23 +01004224 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004225 return;
4226 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004227 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004228 Py_DECREF(arg);
4229 if (err == 0)
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004230 PyErr_Restore(type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004231 else {
4232 Py_XDECREF(type);
4233 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004234 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004235 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004236}
4237
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004238static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004239call_trace_protected(Py_tracefunc func, PyObject *obj,
4240 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004241 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004242{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004243 PyObject *type, *value, *traceback;
4244 int err;
4245 PyErr_Fetch(&type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004246 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004247 if (err == 0)
4248 {
4249 PyErr_Restore(type, value, traceback);
4250 return 0;
4251 }
4252 else {
4253 Py_XDECREF(type);
4254 Py_XDECREF(value);
4255 Py_XDECREF(traceback);
4256 return -1;
4257 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004258}
4259
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004260static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004261call_trace(Py_tracefunc func, PyObject *obj,
4262 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004263 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004264{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004265 int result;
4266 if (tstate->tracing)
4267 return 0;
4268 tstate->tracing++;
4269 tstate->use_tracing = 0;
4270 result = func(obj, frame, what, arg);
4271 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4272 || (tstate->c_profilefunc != NULL));
4273 tstate->tracing--;
4274 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004275}
4276
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004277PyObject *
4278_PyEval_CallTracing(PyObject *func, PyObject *args)
4279{
Victor Stinner50b48572018-11-01 01:51:40 +01004280 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004281 int save_tracing = tstate->tracing;
4282 int save_use_tracing = tstate->use_tracing;
4283 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004284
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004285 tstate->tracing = 0;
4286 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4287 || (tstate->c_profilefunc != NULL));
4288 result = PyObject_Call(func, args, NULL);
4289 tstate->tracing = save_tracing;
4290 tstate->use_tracing = save_use_tracing;
4291 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004292}
4293
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004294/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004295static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004296maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004297 PyThreadState *tstate, PyFrameObject *frame,
4298 int *instr_lb, int *instr_ub, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004299{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004300 int result = 0;
4301 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004303 /* If the last instruction executed isn't in the current
4304 instruction window, reset the window.
4305 */
4306 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
4307 PyAddrPair bounds;
4308 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
4309 &bounds);
4310 *instr_lb = bounds.ap_lower;
4311 *instr_ub = bounds.ap_upper;
4312 }
Nick Coghlan5a851672017-09-08 10:14:16 +10004313 /* If the last instruction falls at the start of a line or if it
4314 represents a jump backwards, update the frame's line number and
4315 then call the trace function if we're tracing source lines.
4316 */
4317 if ((frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004318 frame->f_lineno = line;
Nick Coghlan5a851672017-09-08 10:14:16 +10004319 if (frame->f_trace_lines) {
4320 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
4321 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004322 }
George King20faa682017-10-18 17:44:22 -07004323 /* Always emit an opcode event if we're tracing all opcodes. */
4324 if (frame->f_trace_opcodes) {
4325 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
4326 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004327 *instr_prev = frame->f_lasti;
4328 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004329}
4330
Fred Drake5755ce62001-06-27 19:19:46 +00004331void
4332PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00004333{
Victor Stinner50b48572018-11-01 01:51:40 +01004334 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004335 PyObject *temp = tstate->c_profileobj;
4336 Py_XINCREF(arg);
4337 tstate->c_profilefunc = NULL;
4338 tstate->c_profileobj = NULL;
4339 /* Must make sure that tracing is not ignored if 'temp' is freed */
4340 tstate->use_tracing = tstate->c_tracefunc != NULL;
4341 Py_XDECREF(temp);
4342 tstate->c_profilefunc = func;
4343 tstate->c_profileobj = arg;
4344 /* Flag that tracing or profiling is turned on */
4345 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
Fred Drake5755ce62001-06-27 19:19:46 +00004346}
4347
4348void
4349PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
4350{
Victor Stinner50b48572018-11-01 01:51:40 +01004351 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004352 PyObject *temp = tstate->c_traceobj;
4353 _Py_TracingPossible += (func != NULL) - (tstate->c_tracefunc != NULL);
4354 Py_XINCREF(arg);
4355 tstate->c_tracefunc = NULL;
4356 tstate->c_traceobj = NULL;
4357 /* Must make sure that profiling is not ignored if 'temp' is freed */
4358 tstate->use_tracing = tstate->c_profilefunc != NULL;
4359 Py_XDECREF(temp);
4360 tstate->c_tracefunc = func;
4361 tstate->c_traceobj = arg;
4362 /* Flag that tracing or profiling is turned on */
4363 tstate->use_tracing = ((func != NULL)
4364 || (tstate->c_profilefunc != NULL));
Fred Draked0838392001-06-16 21:02:31 +00004365}
4366
Yury Selivanov75445082015-05-11 22:57:16 -04004367void
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004368_PyEval_SetCoroutineOriginTrackingDepth(int new_depth)
4369{
4370 assert(new_depth >= 0);
Victor Stinner50b48572018-11-01 01:51:40 +01004371 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004372 tstate->coroutine_origin_tracking_depth = new_depth;
4373}
4374
4375int
4376_PyEval_GetCoroutineOriginTrackingDepth(void)
4377{
Victor Stinner50b48572018-11-01 01:51:40 +01004378 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08004379 return tstate->coroutine_origin_tracking_depth;
4380}
4381
4382void
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004383_PyEval_SetCoroutineWrapper(PyObject *wrapper)
Yury Selivanov75445082015-05-11 22:57:16 -04004384{
Victor Stinner50b48572018-11-01 01:51:40 +01004385 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004386
Yury Selivanov75445082015-05-11 22:57:16 -04004387 Py_XINCREF(wrapper);
Serhiy Storchaka48842712016-04-06 09:45:48 +03004388 Py_XSETREF(tstate->coroutine_wrapper, wrapper);
Yury Selivanov75445082015-05-11 22:57:16 -04004389}
4390
4391PyObject *
Yury Selivanovd8cf3822015-06-01 12:15:23 -04004392_PyEval_GetCoroutineWrapper(void)
Yury Selivanov75445082015-05-11 22:57:16 -04004393{
Victor Stinner50b48572018-11-01 01:51:40 +01004394 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanov75445082015-05-11 22:57:16 -04004395 return tstate->coroutine_wrapper;
4396}
4397
Yury Selivanoveb636452016-09-08 22:01:51 -07004398void
4399_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
4400{
Victor Stinner50b48572018-11-01 01:51:40 +01004401 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004402
4403 Py_XINCREF(firstiter);
4404 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
4405}
4406
4407PyObject *
4408_PyEval_GetAsyncGenFirstiter(void)
4409{
Victor Stinner50b48572018-11-01 01:51:40 +01004410 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004411 return tstate->async_gen_firstiter;
4412}
4413
4414void
4415_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
4416{
Victor Stinner50b48572018-11-01 01:51:40 +01004417 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004418
4419 Py_XINCREF(finalizer);
4420 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
4421}
4422
4423PyObject *
4424_PyEval_GetAsyncGenFinalizer(void)
4425{
Victor Stinner50b48572018-11-01 01:51:40 +01004426 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07004427 return tstate->async_gen_finalizer;
4428}
4429
Guido van Rossumb209a111997-04-29 18:18:01 +00004430PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004431PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00004432{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004433 PyFrameObject *current_frame = PyEval_GetFrame();
4434 if (current_frame == NULL)
Victor Stinnercaba55b2018-08-03 15:33:52 +02004435 return _PyInterpreterState_GET_UNSAFE()->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004436 else
4437 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00004438}
4439
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02004440/* Convenience function to get a builtin from its name */
4441PyObject *
4442_PyEval_GetBuiltinId(_Py_Identifier *name)
4443{
4444 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
4445 if (attr) {
4446 Py_INCREF(attr);
4447 }
4448 else if (!PyErr_Occurred()) {
4449 PyErr_SetObject(PyExc_AttributeError, _PyUnicode_FromId(name));
4450 }
4451 return attr;
4452}
4453
Guido van Rossumb209a111997-04-29 18:18:01 +00004454PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004455PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00004456{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 PyFrameObject *current_frame = PyEval_GetFrame();
Victor Stinner41bb43a2013-10-29 01:19:37 +01004458 if (current_frame == NULL) {
4459 PyErr_SetString(PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004460 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004461 }
4462
4463 if (PyFrame_FastToLocalsWithError(current_frame) < 0)
4464 return NULL;
4465
4466 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00004468}
4469
Guido van Rossumb209a111997-04-29 18:18:01 +00004470PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004471PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00004472{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004473 PyFrameObject *current_frame = PyEval_GetFrame();
4474 if (current_frame == NULL)
4475 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01004476
4477 assert(current_frame->f_globals != NULL);
4478 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00004479}
4480
Guido van Rossum6297a7a2003-02-19 15:53:17 +00004481PyFrameObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00004482PyEval_GetFrame(void)
Guido van Rossume59214e1994-08-30 08:01:59 +00004483{
Victor Stinner50b48572018-11-01 01:51:40 +01004484 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004485 return _PyThreadState_GetFrame(tstate);
Guido van Rossume59214e1994-08-30 08:01:59 +00004486}
4487
Guido van Rossum6135a871995-01-09 17:53:26 +00004488int
Tim Peters5ba58662001-07-16 02:29:45 +00004489PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00004490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 PyFrameObject *current_frame = PyEval_GetFrame();
4492 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00004493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004494 if (current_frame != NULL) {
4495 const int codeflags = current_frame->f_code->co_flags;
4496 const int compilerflags = codeflags & PyCF_MASK;
4497 if (compilerflags) {
4498 result = 1;
4499 cf->cf_flags |= compilerflags;
4500 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004501#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004502 if (codeflags & CO_GENERATOR_ALLOWED) {
4503 result = 1;
4504 cf->cf_flags |= CO_GENERATOR_ALLOWED;
4505 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00004506#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507 }
4508 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00004509}
4510
Guido van Rossum3f5da241990-12-20 15:06:42 +00004511
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004512const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004513PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004514{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004515 if (PyMethod_Check(func))
4516 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
4517 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02004518 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004519 else if (PyCFunction_Check(func))
4520 return ((PyCFunctionObject*)func)->m_ml->ml_name;
4521 else
4522 return func->ob_type->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00004523}
4524
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00004525const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00004526PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00004527{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004528 if (PyMethod_Check(func))
4529 return "()";
4530 else if (PyFunction_Check(func))
4531 return "()";
4532 else if (PyCFunction_Check(func))
4533 return "()";
4534 else
4535 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00004536}
4537
Armin Rigo1c2d7e52005-09-20 18:34:01 +00004538#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00004539if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004540 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
4541 tstate, tstate->frame, \
4542 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004543 x = NULL; \
4544 } \
4545 else { \
4546 x = call; \
4547 if (tstate->c_profilefunc != NULL) { \
4548 if (x == NULL) { \
4549 call_trace_protected(tstate->c_profilefunc, \
4550 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004551 tstate, tstate->frame, \
4552 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004553 /* XXX should pass (type, value, tb) */ \
4554 } else { \
4555 if (call_trace(tstate->c_profilefunc, \
4556 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004557 tstate, tstate->frame, \
4558 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004559 Py_DECREF(x); \
4560 x = NULL; \
4561 } \
4562 } \
4563 } \
4564 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00004565} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 x = call; \
4567 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00004568
Victor Stinner415c5102017-01-11 00:54:57 +01004569/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
4570 to reduce the stack consumption. */
4571Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Benjamin Peterson4fd64b92016-09-09 14:57:58 -07004572call_function(PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004573{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004574 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004575 PyObject *func = *pfunc;
4576 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07004577 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
4578 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004579 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004580
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004581 /* Always dispatch PyCFunction first, because these are
4582 presumed to be the most frequent callable object.
4583 */
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004584 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004585 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004586 C_TRACE(x, _PyCFunction_FastCallKeywords(func, stack, nargs, kwnames));
Victor Stinner4a7cc882015-03-06 23:35:27 +01004587 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004588 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004589 PyThreadState *tstate = _PyThreadState_GET();
jdemeyer56868f92018-07-21 10:30:59 +02004590 if (nargs > 0 && tstate->use_tracing) {
4591 /* We need to create a temporary bound method as argument
4592 for profiling.
4593
4594 If nargs == 0, then this cannot work because we have no
4595 "self". In any case, the call itself would raise
4596 TypeError (foo needs an argument), so we just skip
4597 profiling. */
4598 PyObject *self = stack[0];
4599 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
jdemeyer147d9552018-07-23 18:41:20 +02004600 if (func != NULL) {
4601 C_TRACE(x, _PyCFunction_FastCallKeywords(func,
4602 stack+1, nargs-1,
4603 kwnames));
4604 Py_DECREF(func);
INADA Naoki93fac8d2017-03-07 14:24:37 +09004605 }
jdemeyer147d9552018-07-23 18:41:20 +02004606 else {
4607 x = NULL;
4608 }
INADA Naoki93fac8d2017-03-07 14:24:37 +09004609 }
4610 else {
4611 x = _PyMethodDescr_FastCallKeywords(func, stack, nargs, kwnames);
4612 }
INADA Naoki5566bbb2017-02-03 07:43:03 +09004613 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01004614 else {
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004615 if (PyMethod_Check(func) && PyMethod_GET_SELF(func) != NULL) {
Victor Stinnerb69ee8c2016-11-28 18:32:31 +01004616 /* Optimize access to bound methods. Reuse the Python stack
4617 to pass 'self' as the first argument, replace 'func'
4618 with 'self'. It avoids the creation of a new temporary tuple
4619 for arguments (to replace func with self) when the method uses
4620 FASTCALL. */
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004621 PyObject *self = PyMethod_GET_SELF(func);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004622 Py_INCREF(self);
4623 func = PyMethod_GET_FUNCTION(func);
4624 Py_INCREF(func);
4625 Py_SETREF(*pfunc, self);
4626 nargs++;
INADA Naoki5566bbb2017-02-03 07:43:03 +09004627 stack--;
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004628 }
4629 else {
4630 Py_INCREF(func);
4631 }
Victor Stinnerd8735722016-09-09 12:36:44 -07004632
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004633 if (PyFunction_Check(func)) {
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004634 x = _PyFunction_FastCallKeywords(func, stack, nargs, kwnames);
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004635 }
4636 else {
4637 x = _PyObject_FastCallKeywords(func, stack, nargs, kwnames);
4638 }
Victor Stinnerae8b69c2016-09-09 14:07:44 -07004639 Py_DECREF(func);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004640 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00004641
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004642 assert((x != NULL) ^ (PyErr_Occurred() != NULL));
4643
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004644 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004645 while ((*pp_stack) > pfunc) {
4646 w = EXT_POP(*pp_stack);
4647 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004648 }
Victor Stinnerace47d72013-07-18 01:41:08 +02004649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004650 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00004651}
4652
Jeremy Hylton52820442001-01-03 23:52:36 +00004653static PyObject *
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004654do_call_core(PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00004655{
jdemeyere89de732018-09-19 12:06:20 +02004656 PyObject *result;
4657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004658 if (PyCFunction_Check(func)) {
Victor Stinner50b48572018-11-01 01:51:40 +01004659 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004660 C_TRACE(result, PyCFunction_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004661 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004662 }
jdemeyere89de732018-09-19 12:06:20 +02004663 else if (Py_TYPE(func) == &PyMethodDescr_Type) {
Victor Stinner50b48572018-11-01 01:51:40 +01004664 PyThreadState *tstate = _PyThreadState_GET();
jdemeyere89de732018-09-19 12:06:20 +02004665 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
4666 if (nargs > 0 && tstate->use_tracing) {
4667 /* We need to create a temporary bound method as argument
4668 for profiling.
4669
4670 If nargs == 0, then this cannot work because we have no
4671 "self". In any case, the call itself would raise
4672 TypeError (foo needs an argument), so we just skip
4673 profiling. */
4674 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
4675 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
4676 if (func == NULL) {
4677 return NULL;
4678 }
4679
4680 C_TRACE(result, _PyCFunction_FastCallDict(func,
Victor Stinnerd17a6932018-11-09 16:56:48 +01004681 &_PyTuple_ITEMS(callargs)[1],
jdemeyere89de732018-09-19 12:06:20 +02004682 nargs - 1,
4683 kwdict));
4684 Py_DECREF(func);
4685 return result;
4686 }
Victor Stinner74319ae2016-08-25 00:04:09 +02004687 }
jdemeyere89de732018-09-19 12:06:20 +02004688 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00004689}
4690
Serhiy Storchaka483405b2015-02-17 10:14:30 +02004691/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00004692 nb_index slot defined, and store in *pi.
4693 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08004694 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00004695 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00004696*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00004697int
Martin v. Löwis18e16552006-02-15 17:27:45 +00004698_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004699{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004700 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 Py_ssize_t x;
4702 if (PyIndex_Check(v)) {
4703 x = PyNumber_AsSsize_t(v, NULL);
4704 if (x == -1 && PyErr_Occurred())
4705 return 0;
4706 }
4707 else {
4708 PyErr_SetString(PyExc_TypeError,
4709 "slice indices must be integers or "
4710 "None or have an __index__ method");
4711 return 0;
4712 }
4713 *pi = x;
4714 }
4715 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004716}
4717
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004718int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004719_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004720{
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03004721 Py_ssize_t x;
4722 if (PyIndex_Check(v)) {
4723 x = PyNumber_AsSsize_t(v, NULL);
4724 if (x == -1 && PyErr_Occurred())
4725 return 0;
4726 }
4727 else {
4728 PyErr_SetString(PyExc_TypeError,
4729 "slice indices must be integers or "
4730 "have an __index__ method");
4731 return 0;
4732 }
4733 *pi = x;
4734 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02004735}
4736
4737
Guido van Rossum486364b2007-06-30 05:01:58 +00004738#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004739 "BaseException is not allowed"
Brett Cannonf74225d2007-02-26 21:10:16 +00004740
Guido van Rossumb209a111997-04-29 18:18:01 +00004741static PyObject *
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004742cmp_outcome(int op, PyObject *v, PyObject *w)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004743{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004744 int res = 0;
4745 switch (op) {
4746 case PyCmp_IS:
4747 res = (v == w);
4748 break;
4749 case PyCmp_IS_NOT:
4750 res = (v != w);
4751 break;
4752 case PyCmp_IN:
4753 res = PySequence_Contains(w, v);
4754 if (res < 0)
4755 return NULL;
4756 break;
4757 case PyCmp_NOT_IN:
4758 res = PySequence_Contains(w, v);
4759 if (res < 0)
4760 return NULL;
4761 res = !res;
4762 break;
4763 case PyCmp_EXC_MATCH:
4764 if (PyTuple_Check(w)) {
4765 Py_ssize_t i, length;
4766 length = PyTuple_Size(w);
4767 for (i = 0; i < length; i += 1) {
4768 PyObject *exc = PyTuple_GET_ITEM(w, i);
4769 if (!PyExceptionClass_Check(exc)) {
4770 PyErr_SetString(PyExc_TypeError,
4771 CANNOT_CATCH_MSG);
4772 return NULL;
4773 }
4774 }
4775 }
4776 else {
4777 if (!PyExceptionClass_Check(w)) {
4778 PyErr_SetString(PyExc_TypeError,
4779 CANNOT_CATCH_MSG);
4780 return NULL;
4781 }
4782 }
4783 res = PyErr_GivenExceptionMatches(v, w);
4784 break;
4785 default:
4786 return PyObject_RichCompare(v, w, op);
4787 }
4788 v = res ? Py_True : Py_False;
4789 Py_INCREF(v);
4790 return v;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004791}
4792
Thomas Wouters52152252000-08-17 22:55:00 +00004793static PyObject *
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004794import_name(PyFrameObject *f, PyObject *name, PyObject *fromlist, PyObject *level)
4795{
4796 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004797 PyObject *import_func, *res;
4798 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004799
4800 import_func = _PyDict_GetItemId(f->f_builtins, &PyId___import__);
4801 if (import_func == NULL) {
4802 PyErr_SetString(PyExc_ImportError, "__import__ not found");
4803 return NULL;
4804 }
4805
4806 /* Fast path for not overloaded __import__. */
Victor Stinnercaba55b2018-08-03 15:33:52 +02004807 if (import_func == _PyInterpreterState_GET_UNSAFE()->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004808 int ilevel = _PyLong_AsInt(level);
4809 if (ilevel == -1 && PyErr_Occurred()) {
4810 return NULL;
4811 }
4812 res = PyImport_ImportModuleLevelObject(
4813 name,
4814 f->f_globals,
4815 f->f_locals == NULL ? Py_None : f->f_locals,
4816 fromlist,
4817 ilevel);
4818 return res;
4819 }
4820
4821 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02004822
4823 stack[0] = name;
4824 stack[1] = f->f_globals;
4825 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
4826 stack[3] = fromlist;
4827 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02004828 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03004829 Py_DECREF(import_func);
4830 return res;
4831}
4832
4833static PyObject *
Thomas Wouters52152252000-08-17 22:55:00 +00004834import_from(PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00004835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 PyObject *x;
Antoine Pitrou0373a102014-10-13 20:19:45 +02004837 _Py_IDENTIFIER(__name__);
Xiang Zhang4830f582017-03-21 11:13:42 +08004838 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004839
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004840 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02004841 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004842 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004843 /* Issue #17636: in case this failed because of a circular relative
4844 import, try to fallback on reading the module directly from
4845 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02004846 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07004847 if (pkgname == NULL) {
4848 goto error;
4849 }
Oren Milman6db70332017-09-19 14:23:01 +03004850 if (!PyUnicode_Check(pkgname)) {
4851 Py_CLEAR(pkgname);
4852 goto error;
4853 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02004854 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07004855 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08004856 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004857 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07004858 }
Eric Snow3f9eee62017-09-15 16:35:20 -06004859 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02004860 Py_DECREF(fullmodname);
Brett Cannon3008bc02015-08-11 18:01:31 -07004861 if (x == NULL) {
4862 goto error;
4863 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004864 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004865 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07004866 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004867 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004868 if (pkgname == NULL) {
4869 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
4870 if (pkgname_or_unknown == NULL) {
4871 Py_XDECREF(pkgpath);
4872 return NULL;
4873 }
4874 } else {
4875 pkgname_or_unknown = pkgname;
4876 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004877
4878 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
4879 PyErr_Clear();
Xiang Zhang4830f582017-03-21 11:13:42 +08004880 errmsg = PyUnicode_FromFormat(
4881 "cannot import name %R from %R (unknown location)",
4882 name, pkgname_or_unknown
4883 );
4884 /* NULL check for errmsg done by PyErr_SetImportError. */
4885 PyErr_SetImportError(errmsg, pkgname, NULL);
4886 }
4887 else {
4888 errmsg = PyUnicode_FromFormat(
4889 "cannot import name %R from %R (%S)",
4890 name, pkgname_or_unknown, pkgpath
4891 );
4892 /* NULL check for errmsg done by PyErr_SetImportError. */
4893 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08004894 }
4895
Xiang Zhang4830f582017-03-21 11:13:42 +08004896 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08004897 Py_XDECREF(pkgname_or_unknown);
4898 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07004899 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00004900}
Guido van Rossumac7be682001-01-17 15:42:30 +00004901
Thomas Wouters52152252000-08-17 22:55:00 +00004902static int
4903import_all_from(PyObject *locals, PyObject *v)
4904{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02004905 _Py_IDENTIFIER(__all__);
4906 _Py_IDENTIFIER(__dict__);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004907 _Py_IDENTIFIER(__name__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004908 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004909 int skip_leading_underscores = 0;
4910 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00004911
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004912 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
4913 return -1; /* Unexpected error */
4914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004915 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004916 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
4917 return -1;
4918 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 if (dict == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004920 PyErr_SetString(PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02004921 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 return -1;
4923 }
4924 all = PyMapping_Keys(dict);
4925 Py_DECREF(dict);
4926 if (all == NULL)
4927 return -1;
4928 skip_leading_underscores = 1;
4929 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00004930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004931 for (pos = 0, err = 0; ; pos++) {
4932 name = PySequence_GetItem(all, pos);
4933 if (name == NULL) {
4934 if (!PyErr_ExceptionMatches(PyExc_IndexError))
4935 err = -1;
4936 else
4937 PyErr_Clear();
4938 break;
4939 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08004940 if (!PyUnicode_Check(name)) {
4941 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
4942 if (modname == NULL) {
4943 Py_DECREF(name);
4944 err = -1;
4945 break;
4946 }
4947 if (!PyUnicode_Check(modname)) {
4948 PyErr_Format(PyExc_TypeError,
4949 "module __name__ must be a string, not %.100s",
4950 Py_TYPE(modname)->tp_name);
4951 }
4952 else {
4953 PyErr_Format(PyExc_TypeError,
4954 "%s in %U.%s must be str, not %.100s",
4955 skip_leading_underscores ? "Key" : "Item",
4956 modname,
4957 skip_leading_underscores ? "__dict__" : "__all__",
4958 Py_TYPE(name)->tp_name);
4959 }
4960 Py_DECREF(modname);
4961 Py_DECREF(name);
4962 err = -1;
4963 break;
4964 }
4965 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03004966 if (PyUnicode_READY(name) == -1) {
4967 Py_DECREF(name);
4968 err = -1;
4969 break;
4970 }
4971 if (PyUnicode_READ_CHAR(name, 0) == '_') {
4972 Py_DECREF(name);
4973 continue;
4974 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004975 }
4976 value = PyObject_GetAttr(v, name);
4977 if (value == NULL)
4978 err = -1;
4979 else if (PyDict_CheckExact(locals))
4980 err = PyDict_SetItem(locals, name, value);
4981 else
4982 err = PyObject_SetItem(locals, name, value);
4983 Py_DECREF(name);
4984 Py_XDECREF(value);
4985 if (err != 0)
4986 break;
4987 }
4988 Py_DECREF(all);
4989 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00004990}
4991
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03004992static int
4993check_args_iterable(PyObject *func, PyObject *args)
4994{
4995 if (args->ob_type->tp_iter == NULL && !PySequence_Check(args)) {
4996 PyErr_Format(PyExc_TypeError,
4997 "%.200s%.200s argument after * "
4998 "must be an iterable, not %.200s",
4999 PyEval_GetFuncName(func),
5000 PyEval_GetFuncDesc(func),
5001 args->ob_type->tp_name);
5002 return -1;
5003 }
5004 return 0;
5005}
5006
5007static void
5008format_kwargs_mapping_error(PyObject *func, PyObject *kwargs)
5009{
5010 PyErr_Format(PyExc_TypeError,
5011 "%.200s%.200s argument after ** "
5012 "must be a mapping, not %.200s",
5013 PyEval_GetFuncName(func),
5014 PyEval_GetFuncDesc(func),
5015 kwargs->ob_type->tp_name);
5016}
5017
Guido van Rossumac7be682001-01-17 15:42:30 +00005018static void
Neal Norwitzda059e32007-08-26 05:33:45 +00005019format_exc_check_arg(PyObject *exc, const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005020{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005021 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005023 if (!obj)
5024 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005025
Serhiy Storchaka06515832016-11-20 09:13:07 +02005026 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005027 if (!obj_str)
5028 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005030 PyErr_Format(exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005031}
Guido van Rossum950361c1997-01-24 13:49:28 +00005032
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005033static void
5034format_exc_unbound(PyCodeObject *co, int oparg)
5035{
5036 PyObject *name;
5037 /* Don't stomp existing exception */
5038 if (PyErr_Occurred())
5039 return;
5040 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5041 name = PyTuple_GET_ITEM(co->co_cellvars,
5042 oparg);
5043 format_exc_check_arg(
5044 PyExc_UnboundLocalError,
5045 UNBOUNDLOCAL_ERROR_MSG,
5046 name);
5047 } else {
5048 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5049 PyTuple_GET_SIZE(co->co_cellvars));
5050 format_exc_check_arg(PyExc_NameError,
5051 UNBOUNDFREE_ERROR_MSG, name);
5052 }
5053}
5054
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005055static void
5056format_awaitable_error(PyTypeObject *type, int prevopcode)
5057{
5058 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5059 if (prevopcode == BEFORE_ASYNC_WITH) {
5060 PyErr_Format(PyExc_TypeError,
5061 "'async with' received an object from __aenter__ "
5062 "that does not implement __await__: %.100s",
5063 type->tp_name);
5064 }
5065 else if (prevopcode == WITH_CLEANUP_START) {
5066 PyErr_Format(PyExc_TypeError,
5067 "'async with' received an object from __aexit__ "
5068 "that does not implement __await__: %.100s",
5069 type->tp_name);
5070 }
5071 }
5072}
5073
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005074static PyObject *
5075unicode_concatenate(PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005076 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005077{
5078 PyObject *res;
5079 if (Py_REFCNT(v) == 2) {
5080 /* In the common case, there are 2 references to the value
5081 * stored in 'variable' when the += is performed: one on the
5082 * value stack (in 'v') and one still stored in the
5083 * 'variable'. We try to delete the variable now to reduce
5084 * the refcnt to 1.
5085 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005086 int opcode, oparg;
5087 NEXTOPARG();
5088 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005089 case STORE_FAST:
5090 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005091 PyObject **fastlocals = f->f_localsplus;
5092 if (GETLOCAL(oparg) == v)
5093 SETLOCAL(oparg, NULL);
5094 break;
5095 }
5096 case STORE_DEREF:
5097 {
5098 PyObject **freevars = (f->f_localsplus +
5099 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005100 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005101 if (PyCell_GET(c) == v) {
5102 PyCell_SET(c, NULL);
5103 Py_DECREF(v);
5104 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005105 break;
5106 }
5107 case STORE_NAME:
5108 {
5109 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005110 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005111 PyObject *locals = f->f_locals;
5112 if (PyDict_CheckExact(locals) &&
5113 PyDict_GetItem(locals, name) == v) {
5114 if (PyDict_DelItem(locals, name) != 0) {
5115 PyErr_Clear();
5116 }
5117 }
5118 break;
5119 }
5120 }
5121 }
5122 res = v;
5123 PyUnicode_Append(&res, w);
5124 return res;
5125}
5126
Guido van Rossum950361c1997-01-24 13:49:28 +00005127#ifdef DYNAMIC_EXECUTION_PROFILE
5128
Skip Montanarof118cb12001-10-15 20:51:38 +00005129static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005130getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005131{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005132 int i;
5133 PyObject *l = PyList_New(256);
5134 if (l == NULL) return NULL;
5135 for (i = 0; i < 256; i++) {
5136 PyObject *x = PyLong_FromLong(a[i]);
5137 if (x == NULL) {
5138 Py_DECREF(l);
5139 return NULL;
5140 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005141 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005142 }
5143 for (i = 0; i < 256; i++)
5144 a[i] = 0;
5145 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005146}
5147
5148PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005149_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005150{
5151#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005153#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 int i;
5155 PyObject *l = PyList_New(257);
5156 if (l == NULL) return NULL;
5157 for (i = 0; i < 257; i++) {
5158 PyObject *x = getarray(dxpairs[i]);
5159 if (x == NULL) {
5160 Py_DECREF(l);
5161 return NULL;
5162 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005163 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005164 }
5165 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005166#endif
5167}
5168
5169#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005170
5171Py_ssize_t
5172_PyEval_RequestCodeExtraIndex(freefunc free)
5173{
Victor Stinnercaba55b2018-08-03 15:33:52 +02005174 PyInterpreterState *interp = _PyInterpreterState_GET_UNSAFE();
Brett Cannon5c4de282016-09-07 11:16:41 -07005175 Py_ssize_t new_index;
5176
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005177 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005178 return -1;
5179 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005180 new_index = interp->co_extra_user_count++;
5181 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005182 return new_index;
5183}
Łukasz Langaa785c872016-09-09 17:37:37 -07005184
5185static void
5186dtrace_function_entry(PyFrameObject *f)
5187{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005188 const char *filename;
5189 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005190 int lineno;
5191
5192 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5193 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5194 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5195
5196 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
5197}
5198
5199static void
5200dtrace_function_return(PyFrameObject *f)
5201{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005202 const char *filename;
5203 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005204 int lineno;
5205
5206 filename = PyUnicode_AsUTF8(f->f_code->co_filename);
5207 funcname = PyUnicode_AsUTF8(f->f_code->co_name);
5208 lineno = PyCode_Addr2Line(f->f_code, f->f_lasti);
5209
5210 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
5211}
5212
5213/* DTrace equivalent of maybe_call_line_trace. */
5214static void
5215maybe_dtrace_line(PyFrameObject *frame,
5216 int *instr_lb, int *instr_ub, int *instr_prev)
5217{
5218 int line = frame->f_lineno;
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005219 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005220
5221 /* If the last instruction executed isn't in the current
5222 instruction window, reset the window.
5223 */
5224 if (frame->f_lasti < *instr_lb || frame->f_lasti >= *instr_ub) {
5225 PyAddrPair bounds;
5226 line = _PyCode_CheckLineNumber(frame->f_code, frame->f_lasti,
5227 &bounds);
5228 *instr_lb = bounds.ap_lower;
5229 *instr_ub = bounds.ap_upper;
5230 }
5231 /* If the last instruction falls at the start of a line or if
5232 it represents a jump backwards, update the frame's line
5233 number and call the trace function. */
5234 if (frame->f_lasti == *instr_lb || frame->f_lasti < *instr_prev) {
5235 frame->f_lineno = line;
5236 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5237 if (!co_filename)
5238 co_filename = "?";
5239 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5240 if (!co_name)
5241 co_name = "?";
5242 PyDTrace_LINE(co_filename, co_name, line);
5243 }
5244 *instr_prev = frame->f_lasti;
5245}