blob: f0f39539c97bb682107f70bf15edba45ed848376 [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 Stinnere560f902020-04-14 18:30:41 +020013#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner384621c2020-06-22 17:27:35 +020014#include "pycore_call.h" // _PyObject_FastCallDictTstate()
15#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
16#include "pycore_code.h" // _PyCode_InitOpcache()
17#include "pycore_initconfig.h" // _PyStatus_OK()
18#include "pycore_object.h" // _PyObject_GC_TRACK()
19#include "pycore_pyerrors.h" // _PyErr_Fetch()
20#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere560f902020-04-14 18:30:41 +020021#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
22#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020023#include "pycore_sysmodule.h" // _PySys_Audit()
24#include "pycore_tuple.h" // _PyTuple_ITEMS()
Guido van Rossum10dc2e81990-11-18 17:27:39 +000025
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000026#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040027#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000028#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000029#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070030#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040031#include "setobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000032
Guido van Rossumc6004111993-11-05 10:22:19 +000033#include <ctype.h>
34
Guido van Rossum408027e1996-12-30 16:17:54 +000035#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000036/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037#define LLTRACE 1 /* Low-level trace feature */
38#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000039#endif
40
Victor Stinner5c75f372019-04-17 23:02:26 +020041#if !defined(Py_BUILD_CORE)
42# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
43#endif
44
Hai Shi46874c22020-01-30 17:20:25 -060045_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000046
Guido van Rossum374a9221991-04-04 10:40:29 +000047/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020048Py_LOCAL_INLINE(PyObject *) call_function(
49 PyThreadState *tstate, PyObject ***pp_stack,
50 Py_ssize_t oparg, PyObject *kwnames);
51static PyObject * do_call_core(
52 PyThreadState *tstate, PyObject *func,
53 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000054
Guido van Rossum0a066c01992-03-27 17:29:15 +000055#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000056static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020057static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000058#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010059static int call_trace(Py_tracefunc, PyObject *,
60 PyThreadState *, PyFrameObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000062static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010063 PyThreadState *, PyFrameObject *,
64 int, PyObject *);
65static void call_exc_trace(Py_tracefunc, PyObject *,
66 PyThreadState *, PyFrameObject *);
Tim Peters8a5c3c72004-04-05 19:36:21 +000067static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060068 PyThreadState *, PyFrameObject *,
Mark Shannon877df852020-11-12 09:43:29 +000069 PyCodeAddressRange *, int *);
70static void maybe_dtrace_line(PyFrameObject *, PyCodeAddressRange *, int *);
Łukasz Langaa785c872016-09-09 17:37:37 -070071static void dtrace_function_entry(PyFrameObject *);
72static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000073
Victor Stinner438a12d2019-05-24 17:01:38 +020074static PyObject * import_name(PyThreadState *, PyFrameObject *,
75 PyObject *, PyObject *, PyObject *);
76static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
77static int import_all_from(PyThreadState *, PyObject *, PyObject *);
78static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
79static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
80static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030081 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020082static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
83static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
84static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000085static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000086
Paul Prescode68140d2000-08-30 20:25:01 +000087#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +000089#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +000091#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 "free variable '%.200s' referenced before assignment" \
93 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +000094
Guido van Rossum950361c1997-01-24 13:49:28 +000095/* Dynamic execution profile */
96#ifdef DYNAMIC_EXECUTION_PROFILE
97#ifdef DXPAIRS
98static long dxpairs[257][256];
99#define dxp dxpairs[256]
100#else
101static long dxp[256];
102#endif
103#endif
104
Inada Naoki91234a12019-06-03 21:30:58 +0900105/* per opcode cache */
Inada Naokieddef862019-06-04 07:38:10 +0900106#ifdef Py_DEBUG
107// --with-pydebug is used to find memory leak. opcache makes it harder.
108// So we disable opcache when Py_DEBUG is defined.
109// See bpo-37146
110#define OPCACHE_MIN_RUNS 0 /* disable opcache */
111#else
Inada Naoki91234a12019-06-03 21:30:58 +0900112#define OPCACHE_MIN_RUNS 1024 /* create opcache when code executed this time */
Inada Naokieddef862019-06-04 07:38:10 +0900113#endif
Pablo Galindo109826c2020-10-20 06:22:44 +0100114#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900115#define OPCACHE_STATS 0 /* Enable stats */
116
117#if OPCACHE_STATS
118static size_t opcache_code_objects = 0;
119static size_t opcache_code_objects_extra_mem = 0;
120
121static size_t opcache_global_opts = 0;
122static size_t opcache_global_hits = 0;
123static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100124
125static size_t opcache_attr_opts = 0;
126static size_t opcache_attr_hits = 0;
127static size_t opcache_attr_misses = 0;
128static size_t opcache_attr_deopts = 0;
129static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900130#endif
131
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100132
Victor Stinnerda2914d2020-03-20 09:29:08 +0100133#ifndef NDEBUG
134/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
135 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
136 when a thread continues to run after Python finalization, especially
137 daemon threads. */
138static int
139is_tstate_valid(PyThreadState *tstate)
140{
141 assert(!_PyMem_IsPtrFreed(tstate));
142 assert(!_PyMem_IsPtrFreed(tstate->interp));
143 return 1;
144}
145#endif
146
147
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000148/* This can set eval_breaker to 0 even though gil_drop_request became
149 1. We believe this is all right because the eval loop will release
150 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100151static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200152COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200153 struct _ceval_runtime_state *ceval,
154 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100155{
Victor Stinner299b8c62020-05-05 17:40:18 +0200156 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
157 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200158 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200159 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200160 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100161 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200162 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100163}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000164
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000165
Victor Stinnerda2914d2020-03-20 09:29:08 +0100166static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200167SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100168{
Victor Stinner299b8c62020-05-05 17:40:18 +0200169 struct _ceval_state *ceval2 = &interp->ceval;
170 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
171 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100172}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000173
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000174
Victor Stinnerda2914d2020-03-20 09:29:08 +0100175static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200176RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100177{
Victor Stinner299b8c62020-05-05 17:40:18 +0200178 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
179 struct _ceval_state *ceval2 = &interp->ceval;
180 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
181 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100182}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000183
Eric Snowfdf282d2019-01-11 14:26:55 -0700184
Victor Stinnerda2914d2020-03-20 09:29:08 +0100185static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200186SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100187{
Victor Stinner299b8c62020-05-05 17:40:18 +0200188 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
189 struct _ceval_state *ceval2 = &interp->ceval;
190 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
191 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100192}
Eric Snowfdf282d2019-01-11 14:26:55 -0700193
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000194
Victor Stinnerda2914d2020-03-20 09:29:08 +0100195static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200196UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100197{
Victor Stinner299b8c62020-05-05 17:40:18 +0200198 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
199 struct _ceval_state *ceval2 = &interp->ceval;
200 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
201 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100202}
203
204
205static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100206SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100207{
Victor Stinner299b8c62020-05-05 17:40:18 +0200208 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
209 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200210 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100211 if (force) {
212 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
213 }
214 else {
215 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
216 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
217 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100218}
219
220
221static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200222UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100223{
Victor Stinner299b8c62020-05-05 17:40:18 +0200224 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
225 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200226 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200227 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100228}
229
230
231static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200232SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100233{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200234 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100235 ceval2->pending.async_exc = 1;
236 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
237}
238
239
240static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200241UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100242{
Victor Stinner299b8c62020-05-05 17:40:18 +0200243 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
244 struct _ceval_state *ceval2 = &interp->ceval;
245 ceval2->pending.async_exc = 0;
246 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100247}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000248
249
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000250#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000251#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000252#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000253#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000254
Victor Stinner3026cad2020-06-01 16:02:40 +0200255void _Py_NO_RETURN
256_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100257{
Victor Stinner3026cad2020-06-01 16:02:40 +0200258 _Py_FatalErrorFunc(func,
259 "the function must be called with the GIL held, "
260 "but the GIL is released "
261 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100262}
263
Victor Stinner7be4e352020-05-05 20:27:47 +0200264#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
265int
266_PyEval_ThreadsInitialized(PyInterpreterState *interp)
267{
268 return gil_created(&interp->ceval.gil);
269}
270
271int
272PyEval_ThreadsInitialized(void)
273{
274 // Fatal error if there is no current interpreter
275 PyInterpreterState *interp = PyInterpreterState_Get();
276 return _PyEval_ThreadsInitialized(interp);
277}
278#else
Tim Peters7f468f22004-10-11 02:40:51 +0000279int
Victor Stinner175a7042020-03-10 00:37:48 +0100280_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
281{
282 return gil_created(&runtime->ceval.gil);
283}
284
285int
Tim Peters7f468f22004-10-11 02:40:51 +0000286PyEval_ThreadsInitialized(void)
287{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100288 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100289 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000290}
Victor Stinner7be4e352020-05-05 20:27:47 +0200291#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000292
Victor Stinner111e4ee2020-03-09 21:24:14 +0100293PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200294_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000295{
Victor Stinner7be4e352020-05-05 20:27:47 +0200296#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200297 if (!_Py_IsMainInterpreter(tstate)) {
298 /* Currently, the GIL is shared by all interpreters,
299 and only the main interpreter is responsible to create
300 and destroy it. */
301 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100302 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200303#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100304
Victor Stinner7be4e352020-05-05 20:27:47 +0200305#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
306 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
307#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200308 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200309#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200310 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100311
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200312 PyThread_init_thread();
313 create_gil(gil);
314
315 take_gil(tstate);
316
317 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100318 return _PyStatus_OK();
319}
320
321void
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200322_PyEval_FiniGIL(PyThreadState *tstate)
323{
Victor Stinner7be4e352020-05-05 20:27:47 +0200324#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200325 if (!_Py_IsMainInterpreter(tstate)) {
326 /* Currently, the GIL is shared by all interpreters,
327 and only the main interpreter is responsible to create
328 and destroy it. */
329 return;
330 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200331#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200332
Victor Stinner7be4e352020-05-05 20:27:47 +0200333#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
334 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
335#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200336 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200337#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200338 if (!gil_created(gil)) {
339 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
340 yet: do nothing. */
341 return;
342 }
343
344 destroy_gil(gil);
345 assert(!gil_created(gil));
346}
347
348void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100349PyEval_InitThreads(void)
350{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100351 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000352}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000353
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000354void
Inada Naoki91234a12019-06-03 21:30:58 +0900355_PyEval_Fini(void)
356{
357#if OPCACHE_STATS
358 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
359 opcache_code_objects);
360
361 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
362 opcache_code_objects_extra_mem);
363
364 fprintf(stderr, "\n");
365
366 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
367 opcache_global_hits,
368 (int) (100.0 * opcache_global_hits /
369 (opcache_global_hits + opcache_global_misses)));
370
371 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
372 opcache_global_misses,
373 (int) (100.0 * opcache_global_misses /
374 (opcache_global_hits + opcache_global_misses)));
375
376 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
377 opcache_global_opts);
378
379 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100380
381 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
382 opcache_attr_hits,
383 (int) (100.0 * opcache_attr_hits /
384 opcache_attr_total));
385
386 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
387 opcache_attr_misses,
388 (int) (100.0 * opcache_attr_misses /
389 opcache_attr_total));
390
391 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
392 opcache_attr_opts);
393
394 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
395 opcache_attr_deopts);
396
397 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
398 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900399#endif
400}
401
402void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000403PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000404{
Victor Stinner09532fe2019-05-10 23:39:09 +0200405 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200406 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200407 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100408
Victor Stinner85f5a692020-03-09 22:12:04 +0100409 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000410}
411
412void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000413PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000414{
Victor Stinner09532fe2019-05-10 23:39:09 +0200415 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200416 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100418 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100419 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200420 struct _ceval_runtime_state *ceval = &runtime->ceval;
421 struct _ceval_state *ceval2 = &tstate->interp->ceval;
422 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000423}
424
425void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100426_PyEval_ReleaseLock(PyThreadState *tstate)
427{
428 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200429 struct _ceval_state *ceval2 = &tstate->interp->ceval;
430 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100431}
432
433void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000434PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000435{
Victor Stinner3026cad2020-06-01 16:02:40 +0200436 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100437
Victor Stinner85f5a692020-03-09 22:12:04 +0100438 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200439
Victor Stinner85f5a692020-03-09 22:12:04 +0100440 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200441#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
442 (void)_PyThreadState_Swap(gilstate, tstate);
443#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100444 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100445 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200446 }
Victor Stinnere838a932020-05-05 19:56:48 +0200447#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000448}
449
450void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000451PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000452{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100453 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200454
Victor Stinner01b1cc12019-11-20 02:27:56 +0100455 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200456 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
457 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100458 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200459 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200460 struct _ceval_runtime_state *ceval = &runtime->ceval;
461 struct _ceval_state *ceval2 = &tstate->interp->ceval;
462 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000463}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000464
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900465#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200466/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200467 which are not running in the child process, and clear internal locks
468 which might be held by those threads. */
469PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200470_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000471{
Victor Stinner317bab02020-06-02 18:44:54 +0200472 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200473
474#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
475 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
476#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100477 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200478#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100479 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200480 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200481 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100482 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100483
484 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700485
Victor Stinner50e6e992020-03-19 02:41:21 +0100486 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900487 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200488 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700489 }
Jesse Nollera8513972008-07-17 16:49:17 +0000490
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200491 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100492 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200493 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000494}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900495#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000496
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000497/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600498 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000499
500void
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100501_PyEval_SignalAsyncExc(PyThreadState *tstate)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000502{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200503 assert(is_tstate_valid(tstate));
504 SIGNAL_ASYNC_EXC(tstate->interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000505}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000506
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000507PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000508PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000509{
Victor Stinner09532fe2019-05-10 23:39:09 +0200510 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200511#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
512 PyThreadState *old_tstate = _PyThreadState_GET();
513 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
514#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200515 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200516#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200517 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100518
Victor Stinner0b1e3302020-05-05 16:14:31 +0200519 struct _ceval_runtime_state *ceval = &runtime->ceval;
520 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200521#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
522 assert(gil_created(&ceval2->gil));
523#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200524 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200525#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200526 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000528}
529
530void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000531PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000532{
Victor Stinner3026cad2020-06-01 16:02:40 +0200533 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100534
Victor Stinner85f5a692020-03-09 22:12:04 +0100535 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100536
Victor Stinner85f5a692020-03-09 22:12:04 +0100537 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
538 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000539}
540
541
Guido van Rossuma9672091994-09-14 13:31:22 +0000542/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
543 signal handlers or Mac I/O completion routines) can schedule calls
544 to a function to be called synchronously.
545 The synchronous function is called with one void* argument.
546 It should return 0 for success or -1 for failure -- failure should
547 be accompanied by an exception.
548
549 If registry succeeds, the registry function returns 0; if it fails
550 (e.g. due to too many pending calls) it returns -1 (without setting
551 an exception condition).
552
553 Note that because registry may occur from within signal handlers,
554 or other asynchronous events, calling malloc() is unsafe!
555
Guido van Rossuma9672091994-09-14 13:31:22 +0000556 Any thread can schedule pending calls, but only the main thread
557 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000558 There is no facility to schedule calls to a particular thread, but
559 that should be easy to change, should that ever be required. In
560 that case, the static variables here should go into the python
561 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000562*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000563
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200564void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200565_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200566{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100567#ifdef MS_WINDOWS
568 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
569 // handler which can run in a thread different than the Python thread, in
570 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
571 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
572 //
573 // The next eval_frame_handle_pending() call will call
574 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
575 int force = 1;
576#else
577 int force = 0;
578#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200579 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100580 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200581 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100582 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200583}
584
Eric Snow5be45a62019-03-08 22:47:07 -0700585/* Push one item onto the queue while holding the lock. */
586static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200587_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600588 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700589{
Eric Snow842a2f02019-03-15 15:47:51 -0600590 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700591 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600592 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700593 return -1; /* Queue full */
594 }
Eric Snow842a2f02019-03-15 15:47:51 -0600595 pending->calls[i].func = func;
596 pending->calls[i].arg = arg;
597 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700598 return 0;
599}
600
601/* Pop one item off the queue while holding the lock. */
602static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200603_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600604 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700605{
Eric Snow842a2f02019-03-15 15:47:51 -0600606 int i = pending->first;
607 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700608 return; /* Queue empty */
609 }
610
Eric Snow842a2f02019-03-15 15:47:51 -0600611 *func = pending->calls[i].func;
612 *arg = pending->calls[i].arg;
613 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700614}
615
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200616/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000617 scheduling to be made from any thread, and even from an executing
618 callback.
619 */
620
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000621int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200622_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200623 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000624{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200625 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600626
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200627 /* Ensure that _PyEval_InitPendingCalls() was called
628 and that _PyEval_FiniPendingCalls() is not called yet. */
629 assert(pending->lock != NULL);
630
Eric Snow842a2f02019-03-15 15:47:51 -0600631 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200632 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600633 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700634
Victor Stinnere225beb2019-06-03 18:14:24 +0200635 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200636 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000637 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000638}
639
Victor Stinner09532fe2019-05-10 23:39:09 +0200640int
641Py_AddPendingCall(int (*func)(void *), void *arg)
642{
Victor Stinner50e6e992020-03-19 02:41:21 +0100643 /* Best-effort to support subinterpreters and calls with the GIL released.
644
645 First attempt _PyThreadState_GET() since it supports subinterpreters.
646
647 If the GIL is released, _PyThreadState_GET() returns NULL . In this
648 case, use PyGILState_GetThisThreadState() which works even if the GIL
649 is released.
650
651 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
652 see bpo-10915 and bpo-15751.
653
Victor Stinner8849e592020-03-18 19:28:53 +0100654 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100655 PyThreadState *tstate = _PyThreadState_GET();
656 if (tstate == NULL) {
657 tstate = PyGILState_GetThisThreadState();
658 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200659
660 PyInterpreterState *interp;
661 if (tstate != NULL) {
662 interp = tstate->interp;
663 }
664 else {
665 /* Last resort: use the main interpreter */
666 interp = _PyRuntime.interpreters.main;
667 }
668 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200669}
670
Eric Snowfdf282d2019-01-11 14:26:55 -0700671static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100672handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700673{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200674 assert(is_tstate_valid(tstate));
675 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700676 return 0;
677 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700678
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200679 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100680 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
681 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100682 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700683 return -1;
684 }
685 return 0;
686}
687
688static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100689make_pending_calls(PyThreadState *tstate)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000690{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200691 assert(is_tstate_valid(tstate));
692
Victor Stinnerd8316882020-03-20 14:50:35 +0100693 /* only execute pending calls on main thread */
694 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200695 return 0;
696 }
697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100699 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700700 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700702 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200703 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100704
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200705 /* unsignal before starting to call callbacks, so that any callback
706 added in-between re-signals */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200707 UNSIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700708 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100711 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700712 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700713 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 void *arg = NULL;
715
716 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600717 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200718 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600719 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700720
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100721 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700722 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100723 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700724 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700725 res = func(arg);
726 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200727 goto error;
728 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200730
Charles-François Natalif23339a2011-07-23 18:15:43 +0200731 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700732 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200733
734error:
735 busy = 0;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200736 SIGNAL_PENDING_CALLS(tstate->interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700737 return res;
738}
739
Eric Snow842a2f02019-03-15 15:47:51 -0600740void
Victor Stinner2b1df452020-01-13 18:46:59 +0100741_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600742{
Eric Snow842a2f02019-03-15 15:47:51 -0600743 assert(PyGILState_Check());
744
Victor Stinner50e6e992020-03-19 02:41:21 +0100745 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200746
Eric Snow842a2f02019-03-15 15:47:51 -0600747 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
748 return;
749 }
750
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100751 if (make_pending_calls(tstate) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200752 PyObject *exc, *val, *tb;
753 _PyErr_Fetch(tstate, &exc, &val, &tb);
754 PyErr_BadInternalCall();
755 _PyErr_ChainExceptions(exc, val, tb);
756 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600757 }
758}
759
Eric Snowfdf282d2019-01-11 14:26:55 -0700760/* Py_MakePendingCalls() is a simple wrapper for the sake
761 of backward-compatibility. */
762int
763Py_MakePendingCalls(void)
764{
765 assert(PyGILState_Check());
766
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100767 PyThreadState *tstate = _PyThreadState_GET();
768
Eric Snowfdf282d2019-01-11 14:26:55 -0700769 /* Python signal handler doesn't really queue a callback: it only signals
770 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100771 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700772 if (res != 0) {
773 return res;
774 }
775
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100776 res = make_pending_calls(tstate);
Eric Snowb75b1a352019-04-12 10:20:10 -0600777 if (res != 0) {
778 return res;
779 }
780
781 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000782}
783
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000784/* The interpreter's recursion limit */
785
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000786#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200787# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000788#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600789
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600790void
Victor Stinnerdab84232020-03-17 18:56:44 +0100791_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600792{
Victor Stinner7be4e352020-05-05 20:27:47 +0200793#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100794 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200795#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100796}
797
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200798int
Victor Stinnerdab84232020-03-17 18:56:44 +0100799_PyEval_InitState(struct _ceval_state *ceval)
800{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200801 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
802
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200803 struct _pending_calls *pending = &ceval->pending;
804 assert(pending->lock == NULL);
805
806 pending->lock = PyThread_allocate_lock();
807 if (pending->lock == NULL) {
808 return -1;
809 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200810
811#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
812 _gil_initialize(&ceval->gil);
813#endif
814
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200815 return 0;
816}
817
818void
819_PyEval_FiniState(struct _ceval_state *ceval)
820{
821 struct _pending_calls *pending = &ceval->pending;
822 if (pending->lock != NULL) {
823 PyThread_free_lock(pending->lock);
824 pending->lock = NULL;
825 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600826}
827
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000828int
829Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000830{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200831 PyInterpreterState *interp = _PyInterpreterState_GET();
832 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000833}
834
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000835void
836Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000837{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200838 PyThreadState *tstate = _PyThreadState_GET();
839 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000840}
841
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100842/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200843 if the recursion_depth reaches recursion_limit.
844 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000845 to guarantee that _Py_CheckRecursiveCall() is regularly called.
846 Without USE_STACKCHECK, there is no need for this. */
847int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100848_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000849{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200850 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000851
852#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700853 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 if (PyOS_CheckStack()) {
855 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200856 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 return -1;
858 }
pdox18967932017-10-25 23:03:01 -0700859#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000860 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 if (tstate->recursion_depth > recursion_limit + 50) {
862 /* Overflowing while handling an overflow. Give up. */
863 Py_FatalError("Cannot recover from stack overflow.");
864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000866 else {
867 if (tstate->recursion_depth > recursion_limit) {
868 tstate->recursion_headroom++;
869 _PyErr_Format(tstate, PyExc_RecursionError,
870 "maximum recursion depth exceeded%s",
871 where);
872 tstate->recursion_headroom--;
873 --tstate->recursion_depth;
874 return -1;
875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 }
877 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000878}
879
Victor Stinner09532fe2019-05-10 23:39:09 +0200880static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +0200881static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +0000882
Victor Stinnere225beb2019-06-03 18:14:24 +0200883#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000884
Guido van Rossum374a9221991-04-04 10:40:29 +0000885
Guido van Rossumb209a111997-04-29 18:18:01 +0000886PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000887PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +0000888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 return PyEval_EvalCodeEx(co,
890 globals, locals,
891 (PyObject **)NULL, 0,
892 (PyObject **)NULL, 0,
893 (PyObject **)NULL, 0,
894 NULL, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +0000895}
896
897
898/* Interpreter main loop */
899
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000900PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +0100901PyEval_EvalFrame(PyFrameObject *f)
902{
Victor Stinner0b72b232020-03-12 23:18:39 +0100903 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +0100904 PyThreadState *tstate = _PyThreadState_GET();
905 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000906}
907
908PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000909PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +0000910{
Victor Stinnerb9e68122019-11-14 12:20:46 +0100911 PyThreadState *tstate = _PyThreadState_GET();
912 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -0700913}
914
Victor Stinnerda2914d2020-03-20 09:29:08 +0100915
916/* Handle signals, pending calls, GIL drop request
917 and asynchronous exception */
918static int
919eval_frame_handle_pending(PyThreadState *tstate)
920{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100921 _PyRuntimeState * const runtime = &_PyRuntime;
922 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200923
924 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +0200925 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100926 if (handle_signals(tstate) != 0) {
927 return -1;
928 }
929 }
930
931 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +0200932 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100933 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
934 if (make_pending_calls(tstate) != 0) {
935 return -1;
936 }
937 }
938
939 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +0200940 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +0100941 /* Give another thread a chance */
942 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
943 Py_FatalError("tstate mix-up");
944 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200945 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100946
947 /* Other threads may run now */
948
949 take_gil(tstate);
950
Victor Stinnere838a932020-05-05 19:56:48 +0200951#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
952 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
953#else
Victor Stinnerda2914d2020-03-20 09:29:08 +0100954 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
955 Py_FatalError("orphan tstate");
956 }
Victor Stinnere838a932020-05-05 19:56:48 +0200957#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +0100958 }
959
960 /* Check for asynchronous exception. */
961 if (tstate->async_exc != NULL) {
962 PyObject *exc = tstate->async_exc;
963 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200964 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100965 _PyErr_SetNone(tstate, exc);
966 Py_DECREF(exc);
967 return -1;
968 }
969
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100970#ifdef MS_WINDOWS
971 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
972 // different thread than the Python thread, in which case
973 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
974 // current Python thread with the correct _Py_ThreadCanHandleSignals()
975 // value. It prevents to interrupt the eval loop at every instruction if
976 // the current Python thread cannot handle signals (if
977 // _Py_ThreadCanHandleSignals() is false).
978 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
979#endif
980
Victor Stinnerda2914d2020-03-20 09:29:08 +0100981 return 0;
982}
983
Victor Stinnerc6944e72016-11-11 02:13:35 +0100984PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +0100985_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -0700986{
Victor Stinner3026cad2020-06-01 16:02:40 +0200987 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +0100988
Guido van Rossum950361c1997-01-24 13:49:28 +0000989#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +0000991#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200992 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +0300993 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200994 int opcode; /* Current opcode */
995 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200996 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +0100998 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +0100999 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001003
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001005
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 is true when the line being executed has changed. The
1007 initial values are such as to make this false the first
1008 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001009
Serhiy Storchakaab874002016-09-11 13:48:15 +03001010 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyObject *names;
1012 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001013 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001014
Brett Cannon368b4b72012-04-02 12:17:59 -04001015#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001016 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001017#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001018
Antoine Pitroub52ec782009-01-25 16:34:23 +00001019/* Computed GOTOs, or
1020 the-optimization-commonly-but-improperly-known-as-"threaded code"
1021 using gcc's labels-as-values extension
1022 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1023
1024 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001026 combined with a lookup table of jump addresses. However, since the
1027 indirect jump instruction is shared by all opcodes, the CPU will have a
1028 hard time making the right prediction for where to jump next (actually,
1029 it will be always wrong except in the uncommon case of a sequence of
1030 several identical opcodes).
1031
1032 "Threaded code" in contrast, uses an explicit jump table and an explicit
1033 indirect jump instruction at the end of each opcode. Since the jump
1034 instruction is at a different address for each opcode, the CPU will make a
1035 separate prediction for each of these instructions, which is equivalent to
1036 predicting the second opcode of each opcode pair. These predictions have
1037 a much better chance to turn out valid, especially in small bytecode loops.
1038
1039 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001041 and potentially many more instructions (depending on the pipeline width).
1042 A correctly predicted branch, however, is nearly free.
1043
1044 At the time of this writing, the "threaded code" version is up to 15-20%
1045 faster than the normal "switch" version, depending on the compiler and the
1046 CPU architecture.
1047
1048 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1049 because it would render the measurements invalid.
1050
1051
1052 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1053 indirect jumps by sharing them between all opcodes. Such optimizations
1054 can be disabled on gcc by using the -fno-gcse flag (or possibly
1055 -fno-crossjumping).
1056*/
1057
Antoine Pitrou042b1282010-08-13 21:15:58 +00001058#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001059#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001060#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001061#endif
1062
Antoine Pitrou042b1282010-08-13 21:15:58 +00001063#ifdef HAVE_COMPUTED_GOTOS
1064 #ifndef USE_COMPUTED_GOTOS
1065 #define USE_COMPUTED_GOTOS 1
1066 #endif
1067#else
1068 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1069 #error "Computed gotos are not supported on this compiler."
1070 #endif
1071 #undef USE_COMPUTED_GOTOS
1072 #define USE_COMPUTED_GOTOS 0
1073#endif
1074
1075#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001076/* Import the static jump table */
1077#include "opcode_targets.h"
1078
Antoine Pitroub52ec782009-01-25 16:34:23 +00001079#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001080 op: \
1081 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001082
Antoine Pitroub52ec782009-01-25 16:34:23 +00001083#ifdef LLTRACE
1084#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001086 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001088 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001089 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001090 } \
1091 goto fast_next_opcode; \
1092 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001093#else
1094#define FAST_DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001096 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001097 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001098 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001099 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001100 } \
1101 goto fast_next_opcode; \
1102 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001103#endif
1104
Victor Stinner09532fe2019-05-10 23:39:09 +02001105#define DISPATCH() \
1106 { \
1107 if (!_Py_atomic_load_relaxed(eval_breaker)) { \
1108 FAST_DISPATCH(); \
1109 } \
1110 continue; \
1111 }
1112
Antoine Pitroub52ec782009-01-25 16:34:23 +00001113#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001114#define TARGET(op) op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001115#define FAST_DISPATCH() goto fast_next_opcode
Victor Stinner09532fe2019-05-10 23:39:09 +02001116#define DISPATCH() continue
Antoine Pitroub52ec782009-01-25 16:34:23 +00001117#endif
1118
1119
Neal Norwitza81d2202002-07-14 00:27:26 +00001120/* Tuple access macros */
1121
1122#ifndef Py_DEBUG
1123#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1124#else
1125#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1126#endif
1127
Guido van Rossum374a9221991-04-04 10:40:29 +00001128/* Code access macros */
1129
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001130/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001131#define INSTR_OFFSET() \
1132 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001133#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001134 _Py_CODEUNIT word = *next_instr; \
1135 opcode = _Py_OPCODE(word); \
1136 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001137 next_instr++; \
1138 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001139#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1140#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001141
Raymond Hettingerf606f872003-03-16 03:11:04 +00001142/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 Some opcodes tend to come in pairs thus making it possible to
1144 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001145 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001146
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 Verifying the prediction costs a single high-speed test of a register
1148 variable against a constant. If the pairing was good, then the
1149 processor's own internal branch predication has a high likelihood of
1150 success, resulting in a nearly zero-overhead transition to the
1151 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001152 including its unpredictable switch-case branch. Combined with the
1153 processor's internal branch prediction, a successful PREDICT has the
1154 effect of making the two opcodes run as if they were a single new opcode
1155 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001156
Georg Brandl86b2fb92008-07-16 03:43:04 +00001157 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 predictions turned-on and interpret the results as if some opcodes
1159 had been combined or turn-off predictions so that the opcode frequency
1160 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001161
1162 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 the CPU to record separate branch prediction information for each
1164 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001165
Raymond Hettingerf606f872003-03-16 03:11:04 +00001166*/
1167
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001168#define PREDICT_ID(op) PRED_##op
1169
Antoine Pitrou042b1282010-08-13 21:15:58 +00001170#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001171#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001172#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001173#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001174 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001175 _Py_CODEUNIT word = *next_instr; \
1176 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001177 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001178 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001179 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001180 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001181 } \
1182 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001183#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001184#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001185
Raymond Hettingerf606f872003-03-16 03:11:04 +00001186
Guido van Rossum374a9221991-04-04 10:40:29 +00001187/* Stack manipulation macros */
1188
Martin v. Löwis18e16552006-02-15 17:27:45 +00001189/* The stack can grow at most MAXINT deep, as co_nlocals and
1190 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001191#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1192#define EMPTY() (STACK_LEVEL() == 0)
1193#define TOP() (stack_pointer[-1])
1194#define SECOND() (stack_pointer[-2])
1195#define THIRD() (stack_pointer[-3])
1196#define FOURTH() (stack_pointer[-4])
1197#define PEEK(n) (stack_pointer[-(n)])
1198#define SET_TOP(v) (stack_pointer[-1] = (v))
1199#define SET_SECOND(v) (stack_pointer[-2] = (v))
1200#define SET_THIRD(v) (stack_pointer[-3] = (v))
1201#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001202#define BASIC_STACKADJ(n) (stack_pointer += n)
1203#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1204#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001205
Guido van Rossum96a42c81992-01-12 02:29:51 +00001206#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001208 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001209 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001210#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001211 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001212#define STACK_GROW(n) do { \
1213 assert(n >= 0); \
1214 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001215 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001216 assert(STACK_LEVEL() <= co->co_stacksize); \
1217 } while (0)
1218#define STACK_SHRINK(n) do { \
1219 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001220 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001221 (void)(BASIC_STACKADJ(-n)); \
1222 assert(STACK_LEVEL() <= co->co_stacksize); \
1223 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001224#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001225 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001226 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001227#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001228#define PUSH(v) BASIC_PUSH(v)
1229#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001230#define STACK_GROW(n) BASIC_STACKADJ(n)
1231#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001232#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001233#endif
1234
Guido van Rossum681d79a1995-07-18 14:51:37 +00001235/* Local variable macros */
1236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001238
1239/* The SETLOCAL() macro must not DECREF the local variable in-place and
1240 then store the new value; it must copy the old value to a temporary
1241 value, then store the new value, and then DECREF the temporary value.
1242 This is because it is possible that during the DECREF the frame is
1243 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1244 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001246 GETLOCAL(i) = value; \
1247 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001248
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001249
1250#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 while (STACK_LEVEL() > (b)->b_level) { \
1252 PyObject *v = POP(); \
1253 Py_XDECREF(v); \
1254 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001255
1256#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001257 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001258 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001259 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1261 while (STACK_LEVEL() > (b)->b_level + 3) { \
1262 value = POP(); \
1263 Py_XDECREF(value); \
1264 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001265 exc_info = tstate->exc_info; \
1266 type = exc_info->exc_type; \
1267 value = exc_info->exc_value; \
1268 traceback = exc_info->exc_traceback; \
1269 exc_info->exc_type = POP(); \
1270 exc_info->exc_value = POP(); \
1271 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001272 Py_XDECREF(type); \
1273 Py_XDECREF(value); \
1274 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001275 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001276
Inada Naoki91234a12019-06-03 21:30:58 +09001277 /* macros for opcode cache */
1278#define OPCACHE_CHECK() \
1279 do { \
1280 co_opcache = NULL; \
1281 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001282 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001283 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001284 if (co_opcache_offset > 0) { \
1285 assert(co_opcache_offset <= co->co_opcache_size); \
1286 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001287 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001288 } \
1289 } \
1290 } while (0)
1291
Pablo Galindo109826c2020-10-20 06:22:44 +01001292#define OPCACHE_DEOPT() \
1293 do { \
1294 if (co_opcache != NULL) { \
1295 co_opcache->optimized = -1; \
1296 unsigned char co_opcache_offset = \
1297 co->co_opcache_map[next_instr - first_instr]; \
1298 assert(co_opcache_offset <= co->co_opcache_size); \
1299 co->co_opcache_map[co_opcache_offset] = 0; \
1300 co_opcache = NULL; \
1301 } \
1302 } while (0)
1303
1304#define OPCACHE_DEOPT_LOAD_ATTR() \
1305 do { \
1306 if (co_opcache != NULL) { \
1307 OPCACHE_STAT_ATTR_DEOPT(); \
1308 OPCACHE_DEOPT(); \
1309 } \
1310 } while (0)
1311
1312#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1313 do { \
1314 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1315 OPCACHE_DEOPT_LOAD_ATTR(); \
1316 } \
1317 } while (0)
1318
Inada Naoki91234a12019-06-03 21:30:58 +09001319#if OPCACHE_STATS
1320
1321#define OPCACHE_STAT_GLOBAL_HIT() \
1322 do { \
1323 if (co->co_opcache != NULL) opcache_global_hits++; \
1324 } while (0)
1325
1326#define OPCACHE_STAT_GLOBAL_MISS() \
1327 do { \
1328 if (co->co_opcache != NULL) opcache_global_misses++; \
1329 } while (0)
1330
1331#define OPCACHE_STAT_GLOBAL_OPT() \
1332 do { \
1333 if (co->co_opcache != NULL) opcache_global_opts++; \
1334 } while (0)
1335
Pablo Galindo109826c2020-10-20 06:22:44 +01001336#define OPCACHE_STAT_ATTR_HIT() \
1337 do { \
1338 if (co->co_opcache != NULL) opcache_attr_hits++; \
1339 } while (0)
1340
1341#define OPCACHE_STAT_ATTR_MISS() \
1342 do { \
1343 if (co->co_opcache != NULL) opcache_attr_misses++; \
1344 } while (0)
1345
1346#define OPCACHE_STAT_ATTR_OPT() \
1347 do { \
1348 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1349 } while (0)
1350
1351#define OPCACHE_STAT_ATTR_DEOPT() \
1352 do { \
1353 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1354 } while (0)
1355
1356#define OPCACHE_STAT_ATTR_TOTAL() \
1357 do { \
1358 if (co->co_opcache != NULL) opcache_attr_total++; \
1359 } while (0)
1360
Inada Naoki91234a12019-06-03 21:30:58 +09001361#else /* OPCACHE_STATS */
1362
1363#define OPCACHE_STAT_GLOBAL_HIT()
1364#define OPCACHE_STAT_GLOBAL_MISS()
1365#define OPCACHE_STAT_GLOBAL_OPT()
1366
Pablo Galindo109826c2020-10-20 06:22:44 +01001367#define OPCACHE_STAT_ATTR_HIT()
1368#define OPCACHE_STAT_ATTR_MISS()
1369#define OPCACHE_STAT_ATTR_OPT()
1370#define OPCACHE_STAT_ATTR_DEOPT()
1371#define OPCACHE_STAT_ATTR_TOTAL()
1372
Inada Naoki91234a12019-06-03 21:30:58 +09001373#endif
1374
Guido van Rossuma027efa1997-05-05 20:56:21 +00001375/* Start of code */
1376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 /* push frame */
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001378 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001380 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001382 tstate->frame = f;
Tim Peters5ca576e2001-06-18 22:08:13 +00001383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 if (tstate->use_tracing) {
1385 if (tstate->c_tracefunc != NULL) {
1386 /* tstate->c_tracefunc, if defined, is a
1387 function that will be called on *every* entry
1388 to a code block. Its return value, if not
1389 None, is a function that will be called at
1390 the start of each executed line of code.
1391 (Actually, the function must return itself
1392 in order to continue tracing.) The trace
1393 functions are called with three arguments:
1394 a pointer to the current frame, a string
1395 indicating why the function is called, and
1396 an argument which depends on the situation.
1397 The global trace function is also called
1398 whenever an exception is detected. */
1399 if (call_trace_protected(tstate->c_tracefunc,
1400 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001401 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 /* Trace function raised an error */
1403 goto exit_eval_frame;
1404 }
1405 }
1406 if (tstate->c_profilefunc != NULL) {
1407 /* Similar for c_profilefunc, except it needn't
1408 return itself and isn't called for "line" events */
1409 if (call_trace_protected(tstate->c_profilefunc,
1410 tstate->c_profileobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001411 tstate, f, PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 /* Profile function raised an error */
1413 goto exit_eval_frame;
1414 }
1415 }
1416 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001417
Łukasz Langaa785c872016-09-09 17:37:37 -07001418 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1419 dtrace_function_entry(f);
1420
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 co = f->f_code;
Mark Shannon877df852020-11-12 09:43:29 +00001422 PyCodeAddressRange bounds;
1423 _PyCode_InitAddressRange(co, &bounds);
1424 int instr_prev = -1;
1425
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 names = co->co_names;
1427 consts = co->co_consts;
1428 fastlocals = f->f_localsplus;
1429 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001430 assert(PyBytes_Check(co->co_code));
1431 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001432 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1433 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1434 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001435 /*
1436 f->f_lasti refers to the index of the last instruction,
1437 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001438
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001439 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001440 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 When the PREDICT() macros are enabled, some opcode pairs follow in
1443 direct succession without updating f->f_lasti. A successful
1444 prediction effectively links the two codes together as if they
1445 were a single new opcode; accordingly,f->f_lasti will point to
1446 the first code in the pair (for instance, GET_ITER followed by
1447 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001448 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001450 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001451 next_instr = first_instr;
1452 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001453 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1454 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001455 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001456 stack_pointer = f->f_valuestack + f->f_stackdepth;
1457 /* Set f->f_stackdepth to -1.
1458 * Update when returning or calling trace function.
1459 Having f_stackdepth <= 0 ensures that invalid
1460 values are not visible to the cycle GC.
1461 We choose -1 rather than 0 to assist debugging.
1462 */
1463 f->f_stackdepth = -1;
1464 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001465
Inada Naoki91234a12019-06-03 21:30:58 +09001466 if (co->co_opcache_flag < OPCACHE_MIN_RUNS) {
1467 co->co_opcache_flag++;
1468 if (co->co_opcache_flag == OPCACHE_MIN_RUNS) {
1469 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001470 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001471 }
1472#if OPCACHE_STATS
1473 opcache_code_objects_extra_mem +=
1474 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1475 sizeof(_PyOpcache) * co->co_opcache_size;
1476 opcache_code_objects++;
1477#endif
1478 }
1479 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001480
Tim Peters5ca576e2001-06-18 22:08:13 +00001481#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001482 {
1483 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1484 if (r < 0) {
1485 goto exit_eval_frame;
1486 }
1487 lltrace = r;
1488 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001489#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001490
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001491 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001492 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001493 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001494
Victor Stinnerace47d72013-07-18 01:41:08 +02001495#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001496 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001497 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001498 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001499 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001500#endif
1501
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001502main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001503 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1505 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001506 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 /* Do periodic things. Doing this every time through
1509 the loop would add too much overhead, so we do it
1510 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001511 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 event needs attention (e.g. a signal handler or
1513 async I/O handler); see Py_AddPendingCall() and
1514 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001515
Eric Snow7bda9de2019-03-08 17:25:54 -07001516 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001517 opcode = _Py_OPCODE(*next_instr);
1518 if (opcode == SETUP_FINALLY ||
1519 opcode == SETUP_WITH ||
1520 opcode == BEFORE_ASYNC_WITH ||
1521 opcode == YIELD_FROM) {
1522 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001523 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001524 - If we're about to enter the 'with:'. It will prevent
1525 emitting a resource warning in the common idiom
1526 'with open(path) as file:'.
1527 - If we're about to enter the 'async with:'.
1528 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001529 *very* useful, but might help in some cases and it's
1530 traditional)
1531 - If we're resuming a chain of nested 'yield from' or
1532 'await' calls, then each frame is parked with YIELD_FROM
1533 as its next opcode. If the user hit control-C we want to
1534 wait until we've reached the innermost frame before
1535 running the signal handler and raising KeyboardInterrupt
1536 (see bpo-30039).
1537 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 goto fast_next_opcode;
1539 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001540
Victor Stinnerda2914d2020-03-20 09:29:08 +01001541 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001542 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 }
1544 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001545
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 fast_next_opcode:
1547 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001548
Łukasz Langaa785c872016-09-09 17:37:37 -07001549 if (PyDTrace_LINE_ENABLED())
Mark Shannon877df852020-11-12 09:43:29 +00001550 maybe_dtrace_line(f, &bounds, &instr_prev);
Łukasz Langaa785c872016-09-09 17:37:37 -07001551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001553
Victor Stinnerdab84232020-03-17 18:56:44 +01001554 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001555 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001556 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001557 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001559 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 err = maybe_call_line_trace(tstate->c_tracefunc,
1562 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001563 tstate, f,
Mark Shannon877df852020-11-12 09:43:29 +00001564 &bounds, &instr_prev);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 /* Reload possibly changed frame fields */
1566 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001567 stack_pointer = f->f_valuestack+f->f_stackdepth;
1568 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001569 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001571 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001573
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001575
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001576 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001577 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001578#ifdef DYNAMIC_EXECUTION_PROFILE
1579#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 dxpairs[lastopcode][opcode]++;
1581 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001582#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001584#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001585
Guido van Rossum96a42c81992-01-12 02:29:51 +00001586#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 if (lltrace) {
1590 if (HAS_ARG(opcode)) {
1591 printf("%d: %d, %d\n",
1592 f->f_lasti, opcode, oparg);
1593 }
1594 else {
1595 printf("%d: %d\n",
1596 f->f_lasti, opcode);
1597 }
1598 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001599#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001604 It is essential that any operation that fails must goto error
1605 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001606
Benjamin Petersonddd19492018-09-16 22:38:02 -07001607 case TARGET(NOP): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 FAST_DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001609 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001610
Benjamin Petersonddd19492018-09-16 22:38:02 -07001611 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001612 PyObject *value = GETLOCAL(oparg);
1613 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001614 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001615 UNBOUNDLOCAL_ERROR_MSG,
1616 PyTuple_GetItem(co->co_varnames, oparg));
1617 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001619 Py_INCREF(value);
1620 PUSH(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001622 }
1623
Benjamin Petersonddd19492018-09-16 22:38:02 -07001624 case TARGET(LOAD_CONST): {
1625 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001626 PyObject *value = GETITEM(consts, oparg);
1627 Py_INCREF(value);
1628 PUSH(value);
1629 FAST_DISPATCH();
1630 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001631
Benjamin Petersonddd19492018-09-16 22:38:02 -07001632 case TARGET(STORE_FAST): {
1633 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001634 PyObject *value = POP();
1635 SETLOCAL(oparg, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001637 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001638
Benjamin Petersonddd19492018-09-16 22:38:02 -07001639 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001640 PyObject *value = POP();
1641 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001642 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001643 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001644
Benjamin Petersonddd19492018-09-16 22:38:02 -07001645 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001646 PyObject *top = TOP();
1647 PyObject *second = SECOND();
1648 SET_TOP(second);
1649 SET_SECOND(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001651 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001652
Benjamin Petersonddd19492018-09-16 22:38:02 -07001653 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001654 PyObject *top = TOP();
1655 PyObject *second = SECOND();
1656 PyObject *third = THIRD();
1657 SET_TOP(second);
1658 SET_SECOND(third);
1659 SET_THIRD(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001661 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001662
Benjamin Petersonddd19492018-09-16 22:38:02 -07001663 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001664 PyObject *top = TOP();
1665 PyObject *second = SECOND();
1666 PyObject *third = THIRD();
1667 PyObject *fourth = FOURTH();
1668 SET_TOP(second);
1669 SET_SECOND(third);
1670 SET_THIRD(fourth);
1671 SET_FOURTH(top);
1672 FAST_DISPATCH();
1673 }
1674
Benjamin Petersonddd19492018-09-16 22:38:02 -07001675 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001676 PyObject *top = TOP();
1677 Py_INCREF(top);
1678 PUSH(top);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001680 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001681
Benjamin Petersonddd19492018-09-16 22:38:02 -07001682 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001683 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001684 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001685 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001686 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001687 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001688 SET_TOP(top);
1689 SET_SECOND(second);
Antoine Pitrou74a69fa2010-09-04 18:43:52 +00001690 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001691 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001692
Benjamin Petersonddd19492018-09-16 22:38:02 -07001693 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001694 PyObject *value = TOP();
1695 PyObject *res = PyNumber_Positive(value);
1696 Py_DECREF(value);
1697 SET_TOP(res);
1698 if (res == NULL)
1699 goto error;
1700 DISPATCH();
1701 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001702
Benjamin Petersonddd19492018-09-16 22:38:02 -07001703 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001704 PyObject *value = TOP();
1705 PyObject *res = PyNumber_Negative(value);
1706 Py_DECREF(value);
1707 SET_TOP(res);
1708 if (res == NULL)
1709 goto error;
1710 DISPATCH();
1711 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001712
Benjamin Petersonddd19492018-09-16 22:38:02 -07001713 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001714 PyObject *value = TOP();
1715 int err = PyObject_IsTrue(value);
1716 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 if (err == 0) {
1718 Py_INCREF(Py_True);
1719 SET_TOP(Py_True);
1720 DISPATCH();
1721 }
1722 else if (err > 0) {
1723 Py_INCREF(Py_False);
1724 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 DISPATCH();
1726 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001727 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001728 goto error;
1729 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001730
Benjamin Petersonddd19492018-09-16 22:38:02 -07001731 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001732 PyObject *value = TOP();
1733 PyObject *res = PyNumber_Invert(value);
1734 Py_DECREF(value);
1735 SET_TOP(res);
1736 if (res == NULL)
1737 goto error;
1738 DISPATCH();
1739 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001740
Benjamin Petersonddd19492018-09-16 22:38:02 -07001741 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001742 PyObject *exp = POP();
1743 PyObject *base = TOP();
1744 PyObject *res = PyNumber_Power(base, exp, Py_None);
1745 Py_DECREF(base);
1746 Py_DECREF(exp);
1747 SET_TOP(res);
1748 if (res == NULL)
1749 goto error;
1750 DISPATCH();
1751 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001752
Benjamin Petersonddd19492018-09-16 22:38:02 -07001753 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001754 PyObject *right = POP();
1755 PyObject *left = TOP();
1756 PyObject *res = PyNumber_Multiply(left, right);
1757 Py_DECREF(left);
1758 Py_DECREF(right);
1759 SET_TOP(res);
1760 if (res == NULL)
1761 goto error;
1762 DISPATCH();
1763 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Benjamin Petersonddd19492018-09-16 22:38:02 -07001765 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001766 PyObject *right = POP();
1767 PyObject *left = TOP();
1768 PyObject *res = PyNumber_MatrixMultiply(left, right);
1769 Py_DECREF(left);
1770 Py_DECREF(right);
1771 SET_TOP(res);
1772 if (res == NULL)
1773 goto error;
1774 DISPATCH();
1775 }
1776
Benjamin Petersonddd19492018-09-16 22:38:02 -07001777 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001778 PyObject *divisor = POP();
1779 PyObject *dividend = TOP();
1780 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
1781 Py_DECREF(dividend);
1782 Py_DECREF(divisor);
1783 SET_TOP(quotient);
1784 if (quotient == NULL)
1785 goto error;
1786 DISPATCH();
1787 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001788
Benjamin Petersonddd19492018-09-16 22:38:02 -07001789 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001790 PyObject *divisor = POP();
1791 PyObject *dividend = TOP();
1792 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
1793 Py_DECREF(dividend);
1794 Py_DECREF(divisor);
1795 SET_TOP(quotient);
1796 if (quotient == NULL)
1797 goto error;
1798 DISPATCH();
1799 }
Guido van Rossum4668b002001-08-08 05:00:18 +00001800
Benjamin Petersonddd19492018-09-16 22:38:02 -07001801 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001802 PyObject *divisor = POP();
1803 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00001804 PyObject *res;
1805 if (PyUnicode_CheckExact(dividend) && (
1806 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
1807 // fast path; string formatting, but not if the RHS is a str subclass
1808 // (see issue28598)
1809 res = PyUnicode_Format(dividend, divisor);
1810 } else {
1811 res = PyNumber_Remainder(dividend, divisor);
1812 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001813 Py_DECREF(divisor);
1814 Py_DECREF(dividend);
1815 SET_TOP(res);
1816 if (res == NULL)
1817 goto error;
1818 DISPATCH();
1819 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001820
Benjamin Petersonddd19492018-09-16 22:38:02 -07001821 case TARGET(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001822 PyObject *right = POP();
1823 PyObject *left = TOP();
1824 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02001825 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02001826 CPython using bytecode, it is simply worthless.
1827 See http://bugs.python.org/issue21955 and
1828 http://bugs.python.org/issue10044 for the discussion. In short,
1829 no patch shown any impact on a realistic benchmark, only a minor
1830 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001831 if (PyUnicode_CheckExact(left) &&
1832 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001833 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00001834 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001835 }
1836 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001837 sum = PyNumber_Add(left, right);
1838 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02001839 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001840 Py_DECREF(right);
1841 SET_TOP(sum);
1842 if (sum == NULL)
1843 goto error;
1844 DISPATCH();
1845 }
1846
Benjamin Petersonddd19492018-09-16 22:38:02 -07001847 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001848 PyObject *right = POP();
1849 PyObject *left = TOP();
1850 PyObject *diff = PyNumber_Subtract(left, right);
1851 Py_DECREF(right);
1852 Py_DECREF(left);
1853 SET_TOP(diff);
1854 if (diff == NULL)
1855 goto error;
1856 DISPATCH();
1857 }
1858
Benjamin Petersonddd19492018-09-16 22:38:02 -07001859 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001860 PyObject *sub = POP();
1861 PyObject *container = TOP();
1862 PyObject *res = PyObject_GetItem(container, sub);
1863 Py_DECREF(container);
1864 Py_DECREF(sub);
1865 SET_TOP(res);
1866 if (res == NULL)
1867 goto error;
1868 DISPATCH();
1869 }
1870
Benjamin Petersonddd19492018-09-16 22:38:02 -07001871 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001872 PyObject *right = POP();
1873 PyObject *left = TOP();
1874 PyObject *res = PyNumber_Lshift(left, right);
1875 Py_DECREF(left);
1876 Py_DECREF(right);
1877 SET_TOP(res);
1878 if (res == NULL)
1879 goto error;
1880 DISPATCH();
1881 }
1882
Benjamin Petersonddd19492018-09-16 22:38:02 -07001883 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001884 PyObject *right = POP();
1885 PyObject *left = TOP();
1886 PyObject *res = PyNumber_Rshift(left, right);
1887 Py_DECREF(left);
1888 Py_DECREF(right);
1889 SET_TOP(res);
1890 if (res == NULL)
1891 goto error;
1892 DISPATCH();
1893 }
1894
Benjamin Petersonddd19492018-09-16 22:38:02 -07001895 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001896 PyObject *right = POP();
1897 PyObject *left = TOP();
1898 PyObject *res = PyNumber_And(left, right);
1899 Py_DECREF(left);
1900 Py_DECREF(right);
1901 SET_TOP(res);
1902 if (res == NULL)
1903 goto error;
1904 DISPATCH();
1905 }
1906
Benjamin Petersonddd19492018-09-16 22:38:02 -07001907 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001908 PyObject *right = POP();
1909 PyObject *left = TOP();
1910 PyObject *res = PyNumber_Xor(left, right);
1911 Py_DECREF(left);
1912 Py_DECREF(right);
1913 SET_TOP(res);
1914 if (res == NULL)
1915 goto error;
1916 DISPATCH();
1917 }
1918
Benjamin Petersonddd19492018-09-16 22:38:02 -07001919 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001920 PyObject *right = POP();
1921 PyObject *left = TOP();
1922 PyObject *res = PyNumber_Or(left, right);
1923 Py_DECREF(left);
1924 Py_DECREF(right);
1925 SET_TOP(res);
1926 if (res == NULL)
1927 goto error;
1928 DISPATCH();
1929 }
1930
Benjamin Petersonddd19492018-09-16 22:38:02 -07001931 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 PyObject *v = POP();
1933 PyObject *list = PEEK(oparg);
1934 int err;
1935 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 if (err != 0)
1938 goto error;
1939 PREDICT(JUMP_ABSOLUTE);
1940 DISPATCH();
1941 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001942
Benjamin Petersonddd19492018-09-16 22:38:02 -07001943 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001944 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07001945 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001946 int err;
1947 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001948 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001949 if (err != 0)
1950 goto error;
1951 PREDICT(JUMP_ABSOLUTE);
1952 DISPATCH();
1953 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001954
Benjamin Petersonddd19492018-09-16 22:38:02 -07001955 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001956 PyObject *exp = POP();
1957 PyObject *base = TOP();
1958 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
1959 Py_DECREF(base);
1960 Py_DECREF(exp);
1961 SET_TOP(res);
1962 if (res == NULL)
1963 goto error;
1964 DISPATCH();
1965 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001966
Benjamin Petersonddd19492018-09-16 22:38:02 -07001967 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001968 PyObject *right = POP();
1969 PyObject *left = TOP();
1970 PyObject *res = PyNumber_InPlaceMultiply(left, right);
1971 Py_DECREF(left);
1972 Py_DECREF(right);
1973 SET_TOP(res);
1974 if (res == NULL)
1975 goto error;
1976 DISPATCH();
1977 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001978
Benjamin Petersonddd19492018-09-16 22:38:02 -07001979 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04001980 PyObject *right = POP();
1981 PyObject *left = TOP();
1982 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
1983 Py_DECREF(left);
1984 Py_DECREF(right);
1985 SET_TOP(res);
1986 if (res == NULL)
1987 goto error;
1988 DISPATCH();
1989 }
1990
Benjamin Petersonddd19492018-09-16 22:38:02 -07001991 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001992 PyObject *divisor = POP();
1993 PyObject *dividend = TOP();
1994 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
1995 Py_DECREF(dividend);
1996 Py_DECREF(divisor);
1997 SET_TOP(quotient);
1998 if (quotient == NULL)
1999 goto error;
2000 DISPATCH();
2001 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002002
Benjamin Petersonddd19492018-09-16 22:38:02 -07002003 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002004 PyObject *divisor = POP();
2005 PyObject *dividend = TOP();
2006 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2007 Py_DECREF(dividend);
2008 Py_DECREF(divisor);
2009 SET_TOP(quotient);
2010 if (quotient == NULL)
2011 goto error;
2012 DISPATCH();
2013 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002014
Benjamin Petersonddd19492018-09-16 22:38:02 -07002015 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002016 PyObject *right = POP();
2017 PyObject *left = TOP();
2018 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2019 Py_DECREF(left);
2020 Py_DECREF(right);
2021 SET_TOP(mod);
2022 if (mod == NULL)
2023 goto error;
2024 DISPATCH();
2025 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002026
Benjamin Petersonddd19492018-09-16 22:38:02 -07002027 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002028 PyObject *right = POP();
2029 PyObject *left = TOP();
2030 PyObject *sum;
2031 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002032 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002033 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002034 }
2035 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002036 sum = PyNumber_InPlaceAdd(left, right);
2037 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002038 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 Py_DECREF(right);
2040 SET_TOP(sum);
2041 if (sum == NULL)
2042 goto error;
2043 DISPATCH();
2044 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002045
Benjamin Petersonddd19492018-09-16 22:38:02 -07002046 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002047 PyObject *right = POP();
2048 PyObject *left = TOP();
2049 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2050 Py_DECREF(left);
2051 Py_DECREF(right);
2052 SET_TOP(diff);
2053 if (diff == NULL)
2054 goto error;
2055 DISPATCH();
2056 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002057
Benjamin Petersonddd19492018-09-16 22:38:02 -07002058 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002059 PyObject *right = POP();
2060 PyObject *left = TOP();
2061 PyObject *res = PyNumber_InPlaceLshift(left, right);
2062 Py_DECREF(left);
2063 Py_DECREF(right);
2064 SET_TOP(res);
2065 if (res == NULL)
2066 goto error;
2067 DISPATCH();
2068 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002069
Benjamin Petersonddd19492018-09-16 22:38:02 -07002070 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 PyObject *right = POP();
2072 PyObject *left = TOP();
2073 PyObject *res = PyNumber_InPlaceRshift(left, right);
2074 Py_DECREF(left);
2075 Py_DECREF(right);
2076 SET_TOP(res);
2077 if (res == NULL)
2078 goto error;
2079 DISPATCH();
2080 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002081
Benjamin Petersonddd19492018-09-16 22:38:02 -07002082 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002083 PyObject *right = POP();
2084 PyObject *left = TOP();
2085 PyObject *res = PyNumber_InPlaceAnd(left, right);
2086 Py_DECREF(left);
2087 Py_DECREF(right);
2088 SET_TOP(res);
2089 if (res == NULL)
2090 goto error;
2091 DISPATCH();
2092 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002093
Benjamin Petersonddd19492018-09-16 22:38:02 -07002094 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002095 PyObject *right = POP();
2096 PyObject *left = TOP();
2097 PyObject *res = PyNumber_InPlaceXor(left, right);
2098 Py_DECREF(left);
2099 Py_DECREF(right);
2100 SET_TOP(res);
2101 if (res == NULL)
2102 goto error;
2103 DISPATCH();
2104 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002105
Benjamin Petersonddd19492018-09-16 22:38:02 -07002106 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002107 PyObject *right = POP();
2108 PyObject *left = TOP();
2109 PyObject *res = PyNumber_InPlaceOr(left, right);
2110 Py_DECREF(left);
2111 Py_DECREF(right);
2112 SET_TOP(res);
2113 if (res == NULL)
2114 goto error;
2115 DISPATCH();
2116 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002117
Benjamin Petersonddd19492018-09-16 22:38:02 -07002118 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002119 PyObject *sub = TOP();
2120 PyObject *container = SECOND();
2121 PyObject *v = THIRD();
2122 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002123 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002124 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002125 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002126 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002127 Py_DECREF(container);
2128 Py_DECREF(sub);
2129 if (err != 0)
2130 goto error;
2131 DISPATCH();
2132 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002133
Benjamin Petersonddd19492018-09-16 22:38:02 -07002134 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002135 PyObject *sub = TOP();
2136 PyObject *container = SECOND();
2137 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002138 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002139 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002140 err = PyObject_DelItem(container, sub);
2141 Py_DECREF(container);
2142 Py_DECREF(sub);
2143 if (err != 0)
2144 goto error;
2145 DISPATCH();
2146 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002147
Benjamin Petersonddd19492018-09-16 22:38:02 -07002148 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002149 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002150 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002151 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002152 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002153 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002154 _PyErr_SetString(tstate, PyExc_RuntimeError,
2155 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002156 Py_DECREF(value);
2157 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002158 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002159 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002160 Py_DECREF(value);
2161 if (res == NULL)
2162 goto error;
2163 Py_DECREF(res);
2164 DISPATCH();
2165 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002166
Benjamin Petersonddd19492018-09-16 22:38:02 -07002167 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002168 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002169 switch (oparg) {
2170 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002171 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002172 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002173 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002174 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002175 /* fall through */
2176 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002177 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002178 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002179 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002180 break;
2181 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002182 _PyErr_SetString(tstate, PyExc_SystemError,
2183 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002184 break;
2185 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 goto error;
2187 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002188
Benjamin Petersonddd19492018-09-16 22:38:02 -07002189 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002190 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002191 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002192 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002193 f->f_state = FRAME_RETURNED;
2194 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002195 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002196 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002197
Benjamin Petersonddd19492018-09-16 22:38:02 -07002198 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002199 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002200 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002201 PyObject *obj = TOP();
2202 PyTypeObject *type = Py_TYPE(obj);
2203
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002204 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002205 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002206 }
Yury Selivanov75445082015-05-11 22:57:16 -04002207
2208 if (getter != NULL) {
2209 iter = (*getter)(obj);
2210 Py_DECREF(obj);
2211 if (iter == NULL) {
2212 SET_TOP(NULL);
2213 goto error;
2214 }
2215 }
2216 else {
2217 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002218 _PyErr_Format(tstate, PyExc_TypeError,
2219 "'async for' requires an object with "
2220 "__aiter__ method, got %.100s",
2221 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002222 Py_DECREF(obj);
2223 goto error;
2224 }
2225
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002226 if (Py_TYPE(iter)->tp_as_async == NULL ||
2227 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002228
Yury Selivanov398ff912017-03-02 22:20:00 -05002229 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002230 _PyErr_Format(tstate, PyExc_TypeError,
2231 "'async for' received an object from __aiter__ "
2232 "that does not implement __anext__: %.100s",
2233 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002234 Py_DECREF(iter);
2235 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002236 }
2237
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002238 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002239 DISPATCH();
2240 }
2241
Benjamin Petersonddd19492018-09-16 22:38:02 -07002242 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002243 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002244 PyObject *next_iter = NULL;
2245 PyObject *awaitable = NULL;
2246 PyObject *aiter = TOP();
2247 PyTypeObject *type = Py_TYPE(aiter);
2248
Yury Selivanoveb636452016-09-08 22:01:51 -07002249 if (PyAsyncGen_CheckExact(aiter)) {
2250 awaitable = type->tp_as_async->am_anext(aiter);
2251 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002252 goto error;
2253 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002254 } else {
2255 if (type->tp_as_async != NULL){
2256 getter = type->tp_as_async->am_anext;
2257 }
Yury Selivanov75445082015-05-11 22:57:16 -04002258
Yury Selivanoveb636452016-09-08 22:01:51 -07002259 if (getter != NULL) {
2260 next_iter = (*getter)(aiter);
2261 if (next_iter == NULL) {
2262 goto error;
2263 }
2264 }
2265 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002266 _PyErr_Format(tstate, PyExc_TypeError,
2267 "'async for' requires an iterator with "
2268 "__anext__ method, got %.100s",
2269 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002270 goto error;
2271 }
Yury Selivanov75445082015-05-11 22:57:16 -04002272
Yury Selivanoveb636452016-09-08 22:01:51 -07002273 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2274 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002275 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002276 PyExc_TypeError,
2277 "'async for' received an invalid object "
2278 "from __anext__: %.100s",
2279 Py_TYPE(next_iter)->tp_name);
2280
2281 Py_DECREF(next_iter);
2282 goto error;
2283 } else {
2284 Py_DECREF(next_iter);
2285 }
2286 }
Yury Selivanov75445082015-05-11 22:57:16 -04002287
2288 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002289 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002290 DISPATCH();
2291 }
2292
Benjamin Petersonddd19492018-09-16 22:38:02 -07002293 case TARGET(GET_AWAITABLE): {
2294 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002295 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002296 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002297
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002298 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002299 int opcode_at_minus_3 = 0;
2300 if ((next_instr - first_instr) > 2) {
2301 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2302 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002303 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002304 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002305 _Py_OPCODE(next_instr[-2]));
2306 }
2307
Yury Selivanov75445082015-05-11 22:57:16 -04002308 Py_DECREF(iterable);
2309
Yury Selivanovc724bae2016-03-02 11:30:46 -05002310 if (iter != NULL && PyCoro_CheckExact(iter)) {
2311 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2312 if (yf != NULL) {
2313 /* `iter` is a coroutine object that is being
2314 awaited, `yf` is a pointer to the current awaitable
2315 being awaited on. */
2316 Py_DECREF(yf);
2317 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002318 _PyErr_SetString(tstate, PyExc_RuntimeError,
2319 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002320 /* The code below jumps to `error` if `iter` is NULL. */
2321 }
2322 }
2323
Yury Selivanov75445082015-05-11 22:57:16 -04002324 SET_TOP(iter); /* Even if it's NULL */
2325
2326 if (iter == NULL) {
2327 goto error;
2328 }
2329
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002330 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002331 DISPATCH();
2332 }
2333
Benjamin Petersonddd19492018-09-16 22:38:02 -07002334 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002335 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002336 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002337 PySendResult gen_status;
2338 if (tstate->c_tracefunc == NULL) {
2339 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002340 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002341 _Py_IDENTIFIER(send);
2342 if (v == Py_None && PyIter_Check(receiver)) {
2343 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002344 }
2345 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002346 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002347 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002348 if (retval == NULL) {
2349 if (tstate->c_tracefunc != NULL
2350 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
2351 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
2352 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2353 gen_status = PYGEN_RETURN;
2354 }
2355 else {
2356 gen_status = PYGEN_ERROR;
2357 }
2358 }
2359 else {
2360 gen_status = PYGEN_NEXT;
2361 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002362 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002363 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002364 if (gen_status == PYGEN_ERROR) {
2365 assert (retval == NULL);
2366 goto error;
2367 }
2368 if (gen_status == PYGEN_RETURN) {
2369 assert (retval != NULL);
2370
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002371 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002372 SET_TOP(retval);
2373 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002375 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002376 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002377 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002378 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002379 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002380 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002381 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002382 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002383 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002385
Benjamin Petersonddd19492018-09-16 22:38:02 -07002386 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002387 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002388
2389 if (co->co_flags & CO_ASYNC_GENERATOR) {
2390 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2391 Py_DECREF(retval);
2392 if (w == NULL) {
2393 retval = NULL;
2394 goto error;
2395 }
2396 retval = w;
2397 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002398 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002399 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002400 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002401 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002402
Benjamin Petersonddd19492018-09-16 22:38:02 -07002403 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002404 PyObject *type, *value, *traceback;
2405 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002406 PyTryBlock *b = PyFrame_BlockPop(f);
2407 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002408 _PyErr_SetString(tstate, PyExc_SystemError,
2409 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002410 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002411 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002412 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2413 STACK_LEVEL() <= (b)->b_level + 4);
2414 exc_info = tstate->exc_info;
2415 type = exc_info->exc_type;
2416 value = exc_info->exc_value;
2417 traceback = exc_info->exc_traceback;
2418 exc_info->exc_type = POP();
2419 exc_info->exc_value = POP();
2420 exc_info->exc_traceback = POP();
2421 Py_XDECREF(type);
2422 Py_XDECREF(value);
2423 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002425 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002426
Benjamin Petersonddd19492018-09-16 22:38:02 -07002427 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002428 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002430 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002431
Mark Shannonfee55262019-11-21 09:11:43 +00002432 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002433 assert(f->f_iblock > 0);
2434 if (oparg) {
2435 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2436 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002437 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002438 PyObject *val = POP();
2439 PyObject *tb = POP();
2440 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002441 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002442 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002443 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002444
Benjamin Petersonddd19492018-09-16 22:38:02 -07002445 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002446 PyObject *exc = POP();
2447 assert(PyExceptionClass_Check(exc));
2448 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2449 PyTryBlock *b = PyFrame_BlockPop(f);
2450 assert(b->b_type == EXCEPT_HANDLER);
2451 Py_DECREF(exc);
2452 UNWIND_EXCEPT_HANDLER(b);
2453 Py_DECREF(POP());
2454 JUMPBY(oparg);
2455 FAST_DISPATCH();
2456 }
2457 else {
2458 PyObject *val = POP();
2459 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002460 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002461 goto exception_unwind;
2462 }
2463 }
2464
Zackery Spytzce6a0702019-08-25 03:44:09 -06002465 case TARGET(LOAD_ASSERTION_ERROR): {
2466 PyObject *value = PyExc_AssertionError;
2467 Py_INCREF(value);
2468 PUSH(value);
2469 FAST_DISPATCH();
2470 }
2471
Benjamin Petersonddd19492018-09-16 22:38:02 -07002472 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002473 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002474
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002475 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002476 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002477 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002478 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002479 if (!_PyErr_Occurred(tstate)) {
2480 _PyErr_SetString(tstate, PyExc_NameError,
2481 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002482 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002483 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002484 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002485 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002486 }
2487 else {
2488 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2489 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002490 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002491 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2492 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002493 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2494 _PyErr_SetString(tstate, PyExc_NameError,
2495 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002496 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002497 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002498 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002499 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002500 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002501 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002502
Benjamin Petersonddd19492018-09-16 22:38:02 -07002503 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002504 PyObject *name = GETITEM(names, oparg);
2505 PyObject *v = POP();
2506 PyObject *ns = f->f_locals;
2507 int err;
2508 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002509 _PyErr_Format(tstate, PyExc_SystemError,
2510 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002511 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002512 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002513 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002514 if (PyDict_CheckExact(ns))
2515 err = PyDict_SetItem(ns, name, v);
2516 else
2517 err = PyObject_SetItem(ns, name, v);
2518 Py_DECREF(v);
2519 if (err != 0)
2520 goto error;
2521 DISPATCH();
2522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002523
Benjamin Petersonddd19492018-09-16 22:38:02 -07002524 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002525 PyObject *name = GETITEM(names, oparg);
2526 PyObject *ns = f->f_locals;
2527 int err;
2528 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002529 _PyErr_Format(tstate, PyExc_SystemError,
2530 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002531 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002532 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002533 err = PyObject_DelItem(ns, name);
2534 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002535 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002536 NAME_ERROR_MSG,
2537 name);
2538 goto error;
2539 }
2540 DISPATCH();
2541 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002542
Benjamin Petersonddd19492018-09-16 22:38:02 -07002543 case TARGET(UNPACK_SEQUENCE): {
2544 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002545 PyObject *seq = POP(), *item, **items;
2546 if (PyTuple_CheckExact(seq) &&
2547 PyTuple_GET_SIZE(seq) == oparg) {
2548 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002549 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002550 item = items[oparg];
2551 Py_INCREF(item);
2552 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002553 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002554 } else if (PyList_CheckExact(seq) &&
2555 PyList_GET_SIZE(seq) == oparg) {
2556 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002557 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002558 item = items[oparg];
2559 Py_INCREF(item);
2560 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002561 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002562 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002563 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002564 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002565 } else {
2566 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002567 Py_DECREF(seq);
2568 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002569 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002570 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002571 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002572 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002573
Benjamin Petersonddd19492018-09-16 22:38:02 -07002574 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002575 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2576 PyObject *seq = POP();
2577
Victor Stinner438a12d2019-05-24 17:01:38 +02002578 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002579 stack_pointer + totalargs)) {
2580 stack_pointer += totalargs;
2581 } else {
2582 Py_DECREF(seq);
2583 goto error;
2584 }
2585 Py_DECREF(seq);
2586 DISPATCH();
2587 }
2588
Benjamin Petersonddd19492018-09-16 22:38:02 -07002589 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002590 PyObject *name = GETITEM(names, oparg);
2591 PyObject *owner = TOP();
2592 PyObject *v = SECOND();
2593 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002594 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002595 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002596 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002597 Py_DECREF(owner);
2598 if (err != 0)
2599 goto error;
2600 DISPATCH();
2601 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002602
Benjamin Petersonddd19492018-09-16 22:38:02 -07002603 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002604 PyObject *name = GETITEM(names, oparg);
2605 PyObject *owner = POP();
2606 int err;
2607 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2608 Py_DECREF(owner);
2609 if (err != 0)
2610 goto error;
2611 DISPATCH();
2612 }
2613
Benjamin Petersonddd19492018-09-16 22:38:02 -07002614 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002615 PyObject *name = GETITEM(names, oparg);
2616 PyObject *v = POP();
2617 int err;
2618 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002619 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002620 if (err != 0)
2621 goto error;
2622 DISPATCH();
2623 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002624
Benjamin Petersonddd19492018-09-16 22:38:02 -07002625 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002626 PyObject *name = GETITEM(names, oparg);
2627 int err;
2628 err = PyDict_DelItem(f->f_globals, name);
2629 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002630 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2631 format_exc_check_arg(tstate, PyExc_NameError,
2632 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002633 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002634 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002635 }
2636 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002637 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002638
Benjamin Petersonddd19492018-09-16 22:38:02 -07002639 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002640 PyObject *name = GETITEM(names, oparg);
2641 PyObject *locals = f->f_locals;
2642 PyObject *v;
2643 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002644 _PyErr_Format(tstate, PyExc_SystemError,
2645 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002646 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002648 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002649 v = PyDict_GetItemWithError(locals, name);
2650 if (v != NULL) {
2651 Py_INCREF(v);
2652 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002653 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002654 goto error;
2655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002656 }
2657 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002658 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002659 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002660 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002661 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002662 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 }
2664 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002665 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002666 v = PyDict_GetItemWithError(f->f_globals, name);
2667 if (v != NULL) {
2668 Py_INCREF(v);
2669 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002670 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002671 goto error;
2672 }
2673 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002674 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002675 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002676 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002677 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002678 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002679 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002680 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002681 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002682 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002683 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002684 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002685 }
2686 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002687 v = PyObject_GetItem(f->f_builtins, name);
2688 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002689 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002690 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002691 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002693 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002694 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002695 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002696 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002698 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002699 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002701 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002702
Benjamin Petersonddd19492018-09-16 22:38:02 -07002703 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002704 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002705 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002706 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002707 && PyDict_CheckExact(f->f_builtins))
2708 {
Inada Naoki91234a12019-06-03 21:30:58 +09002709 OPCACHE_CHECK();
2710 if (co_opcache != NULL && co_opcache->optimized > 0) {
2711 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2712
2713 if (lg->globals_ver ==
2714 ((PyDictObject *)f->f_globals)->ma_version_tag
2715 && lg->builtins_ver ==
2716 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2717 {
2718 PyObject *ptr = lg->ptr;
2719 OPCACHE_STAT_GLOBAL_HIT();
2720 assert(ptr != NULL);
2721 Py_INCREF(ptr);
2722 PUSH(ptr);
2723 DISPATCH();
2724 }
2725 }
2726
2727 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002728 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002729 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002730 name);
2731 if (v == NULL) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002732 if (!_PyErr_OCCURRED()) {
2733 /* _PyDict_LoadGlobal() returns NULL without raising
2734 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002735 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002736 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002737 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002738 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002739 }
Inada Naoki91234a12019-06-03 21:30:58 +09002740
2741 if (co_opcache != NULL) {
2742 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2743
2744 if (co_opcache->optimized == 0) {
2745 /* Wasn't optimized before. */
2746 OPCACHE_STAT_GLOBAL_OPT();
2747 } else {
2748 OPCACHE_STAT_GLOBAL_MISS();
2749 }
2750
2751 co_opcache->optimized = 1;
2752 lg->globals_ver =
2753 ((PyDictObject *)f->f_globals)->ma_version_tag;
2754 lg->builtins_ver =
2755 ((PyDictObject *)f->f_builtins)->ma_version_tag;
2756 lg->ptr = v; /* borrowed */
2757 }
2758
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002759 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002761 else {
2762 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01002763
2764 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09002765 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002766 v = PyObject_GetItem(f->f_globals, name);
2767 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002768 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002769 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002770 }
2771 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01002772
Victor Stinnerb4efc962015-11-20 09:24:02 +01002773 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 v = PyObject_GetItem(f->f_builtins, name);
2775 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002776 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002777 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002778 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002779 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002780 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002781 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002782 }
2783 }
2784 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002786 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002787 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00002788
Benjamin Petersonddd19492018-09-16 22:38:02 -07002789 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002790 PyObject *v = GETLOCAL(oparg);
2791 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002792 SETLOCAL(oparg, NULL);
2793 DISPATCH();
2794 }
2795 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002796 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002797 UNBOUNDLOCAL_ERROR_MSG,
2798 PyTuple_GetItem(co->co_varnames, oparg)
2799 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002800 goto error;
2801 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002802
Benjamin Petersonddd19492018-09-16 22:38:02 -07002803 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002804 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05002805 PyObject *oldobj = PyCell_GET(cell);
2806 if (oldobj != NULL) {
2807 PyCell_SET(cell, NULL);
2808 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00002809 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002810 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002811 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002812 goto error;
2813 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00002814
Benjamin Petersonddd19492018-09-16 22:38:02 -07002815 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 PyObject *cell = freevars[oparg];
2817 Py_INCREF(cell);
2818 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002819 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002820 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00002821
Benjamin Petersonddd19492018-09-16 22:38:02 -07002822 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002823 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02002824 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002825 assert(locals);
2826 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
2827 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
2828 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
2829 name = PyTuple_GET_ITEM(co->co_freevars, idx);
2830 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002831 value = PyDict_GetItemWithError(locals, name);
2832 if (value != NULL) {
2833 Py_INCREF(value);
2834 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002835 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002836 goto error;
2837 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002838 }
2839 else {
2840 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002841 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002842 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002843 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002844 }
2845 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002846 }
2847 }
2848 if (!value) {
2849 PyObject *cell = freevars[oparg];
2850 value = PyCell_GET(cell);
2851 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002852 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04002853 goto error;
2854 }
2855 Py_INCREF(value);
2856 }
2857 PUSH(value);
2858 DISPATCH();
2859 }
2860
Benjamin Petersonddd19492018-09-16 22:38:02 -07002861 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002862 PyObject *cell = freevars[oparg];
2863 PyObject *value = PyCell_GET(cell);
2864 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002865 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002866 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002867 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002868 Py_INCREF(value);
2869 PUSH(value);
2870 DISPATCH();
2871 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002872
Benjamin Petersonddd19492018-09-16 22:38:02 -07002873 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002874 PyObject *v = POP();
2875 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08002876 PyObject *oldobj = PyCell_GET(cell);
2877 PyCell_SET(cell, v);
2878 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002879 DISPATCH();
2880 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002881
Benjamin Petersonddd19492018-09-16 22:38:02 -07002882 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03002883 PyObject *str;
2884 PyObject *empty = PyUnicode_New(0, 0);
2885 if (empty == NULL) {
2886 goto error;
2887 }
2888 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
2889 Py_DECREF(empty);
2890 if (str == NULL)
2891 goto error;
2892 while (--oparg >= 0) {
2893 PyObject *item = POP();
2894 Py_DECREF(item);
2895 }
2896 PUSH(str);
2897 DISPATCH();
2898 }
2899
Benjamin Petersonddd19492018-09-16 22:38:02 -07002900 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002901 PyObject *tup = PyTuple_New(oparg);
2902 if (tup == NULL)
2903 goto error;
2904 while (--oparg >= 0) {
2905 PyObject *item = POP();
2906 PyTuple_SET_ITEM(tup, oparg, item);
2907 }
2908 PUSH(tup);
2909 DISPATCH();
2910 }
2911
Benjamin Petersonddd19492018-09-16 22:38:02 -07002912 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002913 PyObject *list = PyList_New(oparg);
2914 if (list == NULL)
2915 goto error;
2916 while (--oparg >= 0) {
2917 PyObject *item = POP();
2918 PyList_SET_ITEM(list, oparg, item);
2919 }
2920 PUSH(list);
2921 DISPATCH();
2922 }
2923
Mark Shannon13bc1392020-01-23 09:25:17 +00002924 case TARGET(LIST_TO_TUPLE): {
2925 PyObject *list = POP();
2926 PyObject *tuple = PyList_AsTuple(list);
2927 Py_DECREF(list);
2928 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002929 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00002930 }
2931 PUSH(tuple);
2932 DISPATCH();
2933 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002934
Mark Shannon13bc1392020-01-23 09:25:17 +00002935 case TARGET(LIST_EXTEND): {
2936 PyObject *iterable = POP();
2937 PyObject *list = PEEK(oparg);
2938 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
2939 if (none_val == NULL) {
2940 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01002941 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00002942 {
Victor Stinner61f4db82020-01-28 03:37:45 +01002943 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00002944 _PyErr_Format(tstate, PyExc_TypeError,
2945 "Value after * must be an iterable, not %.200s",
2946 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002947 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002948 Py_DECREF(iterable);
2949 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002950 }
Mark Shannon13bc1392020-01-23 09:25:17 +00002951 Py_DECREF(none_val);
2952 Py_DECREF(iterable);
2953 DISPATCH();
2954 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002955
Mark Shannon13bc1392020-01-23 09:25:17 +00002956 case TARGET(SET_UPDATE): {
2957 PyObject *iterable = POP();
2958 PyObject *set = PEEK(oparg);
2959 int err = _PySet_Update(set, iterable);
2960 Py_DECREF(iterable);
2961 if (err < 0) {
2962 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002963 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002964 DISPATCH();
2965 }
2966
Benjamin Petersonddd19492018-09-16 22:38:02 -07002967 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002968 PyObject *set = PySet_New(NULL);
2969 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002970 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002971 if (set == NULL)
2972 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07002973 for (i = oparg; i > 0; i--) {
2974 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002975 if (err == 0)
2976 err = PySet_Add(set, item);
2977 Py_DECREF(item);
2978 }
costypetrisor8ed317f2018-07-31 20:55:14 +00002979 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002980 if (err != 0) {
2981 Py_DECREF(set);
2982 goto error;
2983 }
2984 PUSH(set);
2985 DISPATCH();
2986 }
2987
Benjamin Petersonddd19492018-09-16 22:38:02 -07002988 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02002989 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002990 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
2991 if (map == NULL)
2992 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002993 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002994 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05002995 PyObject *key = PEEK(2*i);
2996 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002997 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002998 if (err != 0) {
2999 Py_DECREF(map);
3000 goto error;
3001 }
3002 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003003
3004 while (oparg--) {
3005 Py_DECREF(POP());
3006 Py_DECREF(POP());
3007 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 PUSH(map);
3009 DISPATCH();
3010 }
3011
Benjamin Petersonddd19492018-09-16 22:38:02 -07003012 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003013 _Py_IDENTIFIER(__annotations__);
3014 int err;
3015 PyObject *ann_dict;
3016 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003017 _PyErr_Format(tstate, PyExc_SystemError,
3018 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003019 goto error;
3020 }
3021 /* check if __annotations__ in locals()... */
3022 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003023 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003024 &PyId___annotations__);
3025 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003026 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003027 goto error;
3028 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003029 /* ...if not, create a new one */
3030 ann_dict = PyDict_New();
3031 if (ann_dict == NULL) {
3032 goto error;
3033 }
3034 err = _PyDict_SetItemId(f->f_locals,
3035 &PyId___annotations__, ann_dict);
3036 Py_DECREF(ann_dict);
3037 if (err != 0) {
3038 goto error;
3039 }
3040 }
3041 }
3042 else {
3043 /* do the same if locals() is not a dict */
3044 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3045 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003046 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003047 }
3048 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3049 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003050 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003051 goto error;
3052 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003053 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003054 ann_dict = PyDict_New();
3055 if (ann_dict == NULL) {
3056 goto error;
3057 }
3058 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3059 Py_DECREF(ann_dict);
3060 if (err != 0) {
3061 goto error;
3062 }
3063 }
3064 else {
3065 Py_DECREF(ann_dict);
3066 }
3067 }
3068 DISPATCH();
3069 }
3070
Benjamin Petersonddd19492018-09-16 22:38:02 -07003071 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003072 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003073 PyObject *map;
3074 PyObject *keys = TOP();
3075 if (!PyTuple_CheckExact(keys) ||
3076 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003077 _PyErr_SetString(tstate, PyExc_SystemError,
3078 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003079 goto error;
3080 }
3081 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3082 if (map == NULL) {
3083 goto error;
3084 }
3085 for (i = oparg; i > 0; i--) {
3086 int err;
3087 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3088 PyObject *value = PEEK(i + 1);
3089 err = PyDict_SetItem(map, key, value);
3090 if (err != 0) {
3091 Py_DECREF(map);
3092 goto error;
3093 }
3094 }
3095
3096 Py_DECREF(POP());
3097 while (oparg--) {
3098 Py_DECREF(POP());
3099 }
3100 PUSH(map);
3101 DISPATCH();
3102 }
3103
Mark Shannon8a4cd702020-01-27 09:57:45 +00003104 case TARGET(DICT_UPDATE): {
3105 PyObject *update = POP();
3106 PyObject *dict = PEEK(oparg);
3107 if (PyDict_Update(dict, update) < 0) {
3108 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3109 _PyErr_Format(tstate, PyExc_TypeError,
3110 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003111 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003112 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003113 Py_DECREF(update);
3114 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003115 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003116 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003117 DISPATCH();
3118 }
3119
Mark Shannon8a4cd702020-01-27 09:57:45 +00003120 case TARGET(DICT_MERGE): {
3121 PyObject *update = POP();
3122 PyObject *dict = PEEK(oparg);
3123
3124 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3125 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3126 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003127 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003128 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003129 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003130 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003131 DISPATCH();
3132 }
3133
Benjamin Petersonddd19492018-09-16 22:38:02 -07003134 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003135 PyObject *value = TOP();
3136 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003137 PyObject *map;
3138 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003139 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003140 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003141 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003142 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003143 Py_DECREF(value);
3144 Py_DECREF(key);
3145 if (err != 0)
3146 goto error;
3147 PREDICT(JUMP_ABSOLUTE);
3148 DISPATCH();
3149 }
3150
Benjamin Petersonddd19492018-09-16 22:38:02 -07003151 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003152 PyObject *name = GETITEM(names, oparg);
3153 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003154
3155 PyTypeObject *type = Py_TYPE(owner);
3156 PyObject *res;
3157 PyObject **dictptr;
3158 PyObject *dict;
3159 _PyOpCodeOpt_LoadAttr *la;
3160
3161 OPCACHE_STAT_ATTR_TOTAL();
3162
3163 OPCACHE_CHECK();
3164 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3165 {
3166 if (co_opcache->optimized > 0) {
3167 /* Fast path -- cache hit makes LOAD_ATTR ~30% faster */
3168 la = &co_opcache->u.la;
3169 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3170 {
3171 assert(type->tp_dict != NULL);
3172 assert(type->tp_dictoffset > 0);
3173
3174 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3175 dict = *dictptr;
3176 if (dict != NULL && PyDict_CheckExact(dict)) {
3177 Py_ssize_t hint = la->hint;
3178 Py_INCREF(dict);
3179 res = NULL;
3180 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
3181
3182 if (res != NULL) {
3183 if (la->hint == hint && hint >= 0) {
3184 /* Our hint has helped -- cache hit. */
3185 OPCACHE_STAT_ATTR_HIT();
3186 } else {
3187 /* The hint we provided didn't work.
3188 Maybe next time? */
3189 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3190 }
3191
3192 Py_INCREF(res);
3193 SET_TOP(res);
3194 Py_DECREF(owner);
3195 Py_DECREF(dict);
3196 DISPATCH();
3197 } else {
3198 // This attribute can be missing sometimes -- we
3199 // don't want to optimize this lookup.
3200 OPCACHE_DEOPT_LOAD_ATTR();
3201 Py_DECREF(dict);
3202 }
3203 } else {
3204 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3205 OPCACHE_DEOPT_LOAD_ATTR();
3206 }
3207 } else {
3208 // The type of the object has either been updated,
3209 // or is different. Maybe it will stabilize?
3210 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3211 }
3212
3213 OPCACHE_STAT_ATTR_MISS();
3214 }
3215
3216 if (co_opcache != NULL && /* co_opcache can be NULL after a DEOPT() call. */
3217 type->tp_getattro == PyObject_GenericGetAttr)
3218 {
Pablo Galindo109826c2020-10-20 06:22:44 +01003219 Py_ssize_t ret;
3220
3221 if (type->tp_dictoffset > 0) {
3222 if (type->tp_dict == NULL) {
3223 if (PyType_Ready(type) < 0) {
3224 Py_DECREF(owner);
3225 SET_TOP(NULL);
3226 goto error;
3227 }
3228 }
Pablo Galindo80449f22020-11-05 09:23:15 +00003229 if (_PyType_Lookup(type, name) == NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003230 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3231 dict = *dictptr;
3232
3233 if (dict != NULL && PyDict_CheckExact(dict)) {
3234 Py_INCREF(dict);
3235 res = NULL;
3236 ret = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3237 if (res != NULL) {
3238 Py_INCREF(res);
3239 Py_DECREF(dict);
3240 Py_DECREF(owner);
3241 SET_TOP(res);
3242
3243 if (co_opcache->optimized == 0) {
3244 // First time we optimize this opcode. */
3245 OPCACHE_STAT_ATTR_OPT();
3246 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3247 }
3248
3249 la = &co_opcache->u.la;
3250 la->type = type;
3251 la->tp_version_tag = type->tp_version_tag;
3252 la->hint = ret;
3253
3254 DISPATCH();
3255 }
3256 Py_DECREF(dict);
3257 } else {
3258 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact
3259 OPCACHE_DEOPT_LOAD_ATTR();
3260 }
3261 } else {
3262 // We failed to find an attribute without a data-like descriptor
3263 OPCACHE_DEOPT_LOAD_ATTR();
3264 }
3265 } else {
3266 // The object's class does not have a tp_dictoffset we can use
3267 OPCACHE_DEOPT_LOAD_ATTR();
3268 }
3269 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3270 OPCACHE_DEOPT_LOAD_ATTR();
3271 }
3272 }
3273
3274 /* slow path */
3275 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003276 Py_DECREF(owner);
3277 SET_TOP(res);
3278 if (res == NULL)
3279 goto error;
3280 DISPATCH();
3281 }
3282
Benjamin Petersonddd19492018-09-16 22:38:02 -07003283 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003284 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003285 PyObject *right = POP();
3286 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003287 PyObject *res = PyObject_RichCompare(left, right, oparg);
3288 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003289 Py_DECREF(left);
3290 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003291 if (res == NULL)
3292 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003293 PREDICT(POP_JUMP_IF_FALSE);
3294 PREDICT(POP_JUMP_IF_TRUE);
3295 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003296 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003297
Mark Shannon9af0e472020-01-14 10:12:45 +00003298 case TARGET(IS_OP): {
3299 PyObject *right = POP();
3300 PyObject *left = TOP();
3301 int res = (left == right)^oparg;
3302 PyObject *b = res ? Py_True : Py_False;
3303 Py_INCREF(b);
3304 SET_TOP(b);
3305 Py_DECREF(left);
3306 Py_DECREF(right);
3307 PREDICT(POP_JUMP_IF_FALSE);
3308 PREDICT(POP_JUMP_IF_TRUE);
3309 FAST_DISPATCH();
3310 }
3311
3312 case TARGET(CONTAINS_OP): {
3313 PyObject *right = POP();
3314 PyObject *left = POP();
3315 int res = PySequence_Contains(right, left);
3316 Py_DECREF(left);
3317 Py_DECREF(right);
3318 if (res < 0) {
3319 goto error;
3320 }
3321 PyObject *b = (res^oparg) ? Py_True : Py_False;
3322 Py_INCREF(b);
3323 PUSH(b);
3324 PREDICT(POP_JUMP_IF_FALSE);
3325 PREDICT(POP_JUMP_IF_TRUE);
3326 FAST_DISPATCH();
3327 }
3328
3329#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3330 "BaseException is not allowed"
3331
3332 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3333 PyObject *right = POP();
3334 PyObject *left = POP();
3335 if (PyTuple_Check(right)) {
3336 Py_ssize_t i, length;
3337 length = PyTuple_GET_SIZE(right);
3338 for (i = 0; i < length; i++) {
3339 PyObject *exc = PyTuple_GET_ITEM(right, i);
3340 if (!PyExceptionClass_Check(exc)) {
3341 _PyErr_SetString(tstate, PyExc_TypeError,
3342 CANNOT_CATCH_MSG);
3343 Py_DECREF(left);
3344 Py_DECREF(right);
3345 goto error;
3346 }
3347 }
3348 }
3349 else {
3350 if (!PyExceptionClass_Check(right)) {
3351 _PyErr_SetString(tstate, PyExc_TypeError,
3352 CANNOT_CATCH_MSG);
3353 Py_DECREF(left);
3354 Py_DECREF(right);
3355 goto error;
3356 }
3357 }
3358 int res = PyErr_GivenExceptionMatches(left, right);
3359 Py_DECREF(left);
3360 Py_DECREF(right);
3361 if (res > 0) {
3362 /* Exception matches -- Do nothing */;
3363 }
3364 else if (res == 0) {
3365 JUMPTO(oparg);
3366 }
3367 else {
3368 goto error;
3369 }
3370 DISPATCH();
3371 }
3372
Benjamin Petersonddd19492018-09-16 22:38:02 -07003373 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003374 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003375 PyObject *fromlist = POP();
3376 PyObject *level = TOP();
3377 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003378 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003379 Py_DECREF(level);
3380 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003381 SET_TOP(res);
3382 if (res == NULL)
3383 goto error;
3384 DISPATCH();
3385 }
3386
Benjamin Petersonddd19492018-09-16 22:38:02 -07003387 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003388 PyObject *from = POP(), *locals;
3389 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003390 if (PyFrame_FastToLocalsWithError(f) < 0) {
3391 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003392 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003393 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003394
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003395 locals = f->f_locals;
3396 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003397 _PyErr_SetString(tstate, PyExc_SystemError,
3398 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003399 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003400 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003401 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003402 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003403 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003404 Py_DECREF(from);
3405 if (err != 0)
3406 goto error;
3407 DISPATCH();
3408 }
Guido van Rossum25831651993-05-19 14:50:45 +00003409
Benjamin Petersonddd19492018-09-16 22:38:02 -07003410 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003411 PyObject *name = GETITEM(names, oparg);
3412 PyObject *from = TOP();
3413 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003414 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003415 PUSH(res);
3416 if (res == NULL)
3417 goto error;
3418 DISPATCH();
3419 }
Thomas Wouters52152252000-08-17 22:55:00 +00003420
Benjamin Petersonddd19492018-09-16 22:38:02 -07003421 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 JUMPBY(oparg);
3423 FAST_DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003424 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003425
Benjamin Petersonddd19492018-09-16 22:38:02 -07003426 case TARGET(POP_JUMP_IF_FALSE): {
3427 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003428 PyObject *cond = POP();
3429 int err;
3430 if (cond == Py_True) {
3431 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003432 FAST_DISPATCH();
3433 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003434 if (cond == Py_False) {
3435 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003436 JUMPTO(oparg);
3437 FAST_DISPATCH();
3438 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003439 err = PyObject_IsTrue(cond);
3440 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003441 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003442 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003443 else if (err == 0)
3444 JUMPTO(oparg);
3445 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003446 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003448 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003449
Benjamin Petersonddd19492018-09-16 22:38:02 -07003450 case TARGET(POP_JUMP_IF_TRUE): {
3451 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003452 PyObject *cond = POP();
3453 int err;
3454 if (cond == Py_False) {
3455 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 FAST_DISPATCH();
3457 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003458 if (cond == Py_True) {
3459 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003460 JUMPTO(oparg);
3461 FAST_DISPATCH();
3462 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003463 err = PyObject_IsTrue(cond);
3464 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003465 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003466 JUMPTO(oparg);
3467 }
3468 else if (err == 0)
3469 ;
3470 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003471 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003472 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003473 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003474
Benjamin Petersonddd19492018-09-16 22:38:02 -07003475 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003476 PyObject *cond = TOP();
3477 int err;
3478 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003479 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003480 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 FAST_DISPATCH();
3482 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003483 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003484 JUMPTO(oparg);
3485 FAST_DISPATCH();
3486 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003487 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003488 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003489 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003490 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003491 }
3492 else if (err == 0)
3493 JUMPTO(oparg);
3494 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003495 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003496 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003497 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003498
Benjamin Petersonddd19492018-09-16 22:38:02 -07003499 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003500 PyObject *cond = TOP();
3501 int err;
3502 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003503 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003504 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003505 FAST_DISPATCH();
3506 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003507 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003508 JUMPTO(oparg);
3509 FAST_DISPATCH();
3510 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003511 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003512 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003513 JUMPTO(oparg);
3514 }
3515 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003516 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003517 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 }
3519 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003520 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003521 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003522 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003523
Benjamin Petersonddd19492018-09-16 22:38:02 -07003524 case TARGET(JUMP_ABSOLUTE): {
3525 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003526 JUMPTO(oparg);
Guido van Rossum58da9312007-11-10 23:39:45 +00003527#if FAST_LOOPS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003528 /* Enabling this path speeds-up all while and for-loops by bypassing
3529 the per-loop checks for signals. By default, this should be turned-off
3530 because it prevents detection of a control-break in tight loops like
3531 "while 1: pass". Compile with this option turned-on when you need
3532 the speed-up and do not need break checking inside tight loops (ones
3533 that contain only instructions ending with FAST_DISPATCH).
3534 */
3535 FAST_DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003536#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003537 DISPATCH();
Guido van Rossum58da9312007-11-10 23:39:45 +00003538#endif
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003539 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003540
Benjamin Petersonddd19492018-09-16 22:38:02 -07003541 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003542 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003543 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04003544 PyObject *iter = PyObject_GetIter(iterable);
3545 Py_DECREF(iterable);
3546 SET_TOP(iter);
3547 if (iter == NULL)
3548 goto error;
3549 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003550 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04003551 DISPATCH();
3552 }
3553
Benjamin Petersonddd19492018-09-16 22:38:02 -07003554 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04003555 /* before: [obj]; after [getiter(obj)] */
3556 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04003557 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04003558 if (PyCoro_CheckExact(iterable)) {
3559 /* `iterable` is a coroutine */
3560 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
3561 /* and it is used in a 'yield from' expression of a
3562 regular generator. */
3563 Py_DECREF(iterable);
3564 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02003565 _PyErr_SetString(tstate, PyExc_TypeError,
3566 "cannot 'yield from' a coroutine object "
3567 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04003568 goto error;
3569 }
3570 }
3571 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04003572 /* `iterable` is not a generator. */
3573 iter = PyObject_GetIter(iterable);
3574 Py_DECREF(iterable);
3575 SET_TOP(iter);
3576 if (iter == NULL)
3577 goto error;
3578 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003579 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003580 DISPATCH();
3581 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003582
Benjamin Petersonddd19492018-09-16 22:38:02 -07003583 case TARGET(FOR_ITER): {
3584 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003585 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003586 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01003587 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003588 if (next != NULL) {
3589 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003590 PREDICT(STORE_FAST);
3591 PREDICT(UNPACK_SEQUENCE);
3592 DISPATCH();
3593 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003594 if (_PyErr_Occurred(tstate)) {
3595 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003596 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003597 }
3598 else if (tstate->c_tracefunc != NULL) {
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01003599 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f);
Victor Stinner438a12d2019-05-24 17:01:38 +02003600 }
3601 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003602 }
3603 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00003604 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003605 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003606 JUMPBY(oparg);
3607 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003608 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00003609
Benjamin Petersonddd19492018-09-16 22:38:02 -07003610 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003611 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 STACK_LEVEL());
3613 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003614 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003615
Benjamin Petersonddd19492018-09-16 22:38:02 -07003616 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003617 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003618 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04003619 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003620 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04003621 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003622 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04003623 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003624 }
3625 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
3626 if (exit == NULL) {
3627 Py_DECREF(enter);
3628 goto error;
3629 }
Yury Selivanov75445082015-05-11 22:57:16 -04003630 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04003631 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003632 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04003633 Py_DECREF(enter);
3634 if (res == NULL)
3635 goto error;
3636 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03003637 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04003638 DISPATCH();
3639 }
3640
Benjamin Petersonddd19492018-09-16 22:38:02 -07003641 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04003642 PyObject *res = POP();
3643 /* Setup the finally block before pushing the result
3644 of __aenter__ on the stack. */
3645 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3646 STACK_LEVEL());
3647 PUSH(res);
3648 DISPATCH();
3649 }
3650
Benjamin Petersonddd19492018-09-16 22:38:02 -07003651 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05003652 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01003653 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003654 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02003655 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003656 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003657 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08003658 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003659 }
3660 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003661 if (exit == NULL) {
3662 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003663 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08003664 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003665 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003666 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01003667 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003668 Py_DECREF(enter);
3669 if (res == NULL)
3670 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003671 /* Setup the finally block before pushing the result
3672 of __enter__ on the stack. */
3673 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
3674 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003675
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003676 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003677 DISPATCH();
3678 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00003679
Mark Shannonfee55262019-11-21 09:11:43 +00003680 case TARGET(WITH_EXCEPT_START): {
3681 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003682 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00003683 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
3684 - SEVENTH: the context.__exit__ bound method
3685 We call SEVENTH(TOP, SECOND, THIRD).
3686 Then we push again the TOP exception and the __exit__
3687 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003688 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003689 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01003690 PyObject *exc, *val, *tb, *res;
3691
Victor Stinner842cfff2016-12-01 14:45:31 +01003692 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00003693 val = SECOND();
3694 tb = THIRD();
3695 assert(exc != Py_None);
3696 assert(!PyLong_Check(exc));
3697 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003698 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01003699 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02003700 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003701 if (res == NULL)
3702 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00003703
Yury Selivanov75445082015-05-11 22:57:16 -04003704 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003705 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003706 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00003707
Benjamin Petersonddd19492018-09-16 22:38:02 -07003708 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10003709 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003710 PyObject *name = GETITEM(names, oparg);
3711 PyObject *obj = TOP();
3712 PyObject *meth = NULL;
3713
3714 int meth_found = _PyObject_GetMethod(obj, name, &meth);
3715
Yury Selivanovf2392132016-12-13 19:03:51 -05003716 if (meth == NULL) {
3717 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003718 goto error;
3719 }
3720
3721 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09003722 /* We can bypass temporary bound method object.
3723 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01003724
INADA Naoki015bce62017-01-16 17:23:30 +09003725 meth | self | arg1 | ... | argN
3726 */
3727 SET_TOP(meth);
3728 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05003729 }
3730 else {
INADA Naoki015bce62017-01-16 17:23:30 +09003731 /* meth is not an unbound method (but a regular attr, or
3732 something was returned by a descriptor protocol). Set
3733 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05003734 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09003735
3736 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003737 */
INADA Naoki015bce62017-01-16 17:23:30 +09003738 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003739 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09003740 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05003741 }
3742 DISPATCH();
3743 }
3744
Benjamin Petersonddd19492018-09-16 22:38:02 -07003745 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05003746 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09003747 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05003748
3749 sp = stack_pointer;
3750
INADA Naoki015bce62017-01-16 17:23:30 +09003751 meth = PEEK(oparg + 2);
3752 if (meth == NULL) {
3753 /* `meth` is NULL when LOAD_METHOD thinks that it's not
3754 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05003755
3756 Stack layout:
3757
INADA Naoki015bce62017-01-16 17:23:30 +09003758 ... | NULL | callable | arg1 | ... | argN
3759 ^- TOP()
3760 ^- (-oparg)
3761 ^- (-oparg-1)
3762 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003763
Ville Skyttä49b27342017-08-03 09:00:59 +03003764 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09003765 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05003766 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003767 res = call_function(tstate, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003768 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09003769 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05003770 }
3771 else {
3772 /* This is a method call. Stack layout:
3773
INADA Naoki015bce62017-01-16 17:23:30 +09003774 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05003775 ^- TOP()
3776 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09003777 ^- (-oparg-1)
3778 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05003779
INADA Naoki015bce62017-01-16 17:23:30 +09003780 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05003781 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09003782 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05003783 */
Victor Stinner09532fe2019-05-10 23:39:09 +02003784 res = call_function(tstate, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05003785 stack_pointer = sp;
3786 }
3787
3788 PUSH(res);
3789 if (res == NULL)
3790 goto error;
3791 DISPATCH();
3792 }
3793
Benjamin Petersonddd19492018-09-16 22:38:02 -07003794 case TARGET(CALL_FUNCTION): {
3795 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003796 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003798 res = call_function(tstate, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003800 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003801 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003802 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003803 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003804 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003805 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003806
Benjamin Petersonddd19492018-09-16 22:38:02 -07003807 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003808 PyObject **sp, *res, *names;
3809
3810 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02003811 assert(PyTuple_Check(names));
3812 assert(PyTuple_GET_SIZE(names) <= oparg);
3813 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003814 sp = stack_pointer;
Victor Stinner09532fe2019-05-10 23:39:09 +02003815 res = call_function(tstate, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003816 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003817 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003818 Py_DECREF(names);
3819
3820 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003821 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003822 }
3823 DISPATCH();
3824 }
3825
Benjamin Petersonddd19492018-09-16 22:38:02 -07003826 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07003827 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003828 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003829 if (oparg & 0x01) {
3830 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03003831 if (!PyDict_CheckExact(kwargs)) {
3832 PyObject *d = PyDict_New();
3833 if (d == NULL)
3834 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02003835 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03003836 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02003837 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02003838 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003839 goto error;
3840 }
3841 Py_DECREF(kwargs);
3842 kwargs = d;
3843 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003844 assert(PyDict_CheckExact(kwargs));
3845 }
3846 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003847 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003848 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003849 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02003850 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03003851 goto error;
3852 }
3853 Py_SETREF(callargs, PySequence_Tuple(callargs));
3854 if (callargs == NULL) {
3855 goto error;
3856 }
3857 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03003858 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003859
Victor Stinner09532fe2019-05-10 23:39:09 +02003860 result = do_call_core(tstate, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07003861 Py_DECREF(func);
3862 Py_DECREF(callargs);
3863 Py_XDECREF(kwargs);
3864
3865 SET_TOP(result);
3866 if (result == NULL) {
3867 goto error;
3868 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003869 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003870 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003871
Benjamin Petersonddd19492018-09-16 22:38:02 -07003872 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003873 PyObject *qualname = POP();
3874 PyObject *codeobj = POP();
3875 PyFunctionObject *func = (PyFunctionObject *)
3876 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00003877
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003878 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003879 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003880 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003881 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003882 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003883
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003884 if (oparg & 0x08) {
3885 assert(PyTuple_CheckExact(TOP()));
3886 func ->func_closure = POP();
3887 }
3888 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02003889 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003890 func->func_annotations = POP();
3891 }
3892 if (oparg & 0x02) {
3893 assert(PyDict_CheckExact(TOP()));
3894 func->func_kwdefaults = POP();
3895 }
3896 if (oparg & 0x01) {
3897 assert(PyTuple_CheckExact(TOP()));
3898 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003899 }
Neal Norwitzc1505362006-12-28 06:47:50 +00003900
Serhiy Storchaka64204de2016-06-12 17:36:24 +03003901 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003902 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003903 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003904
Benjamin Petersonddd19492018-09-16 22:38:02 -07003905 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003906 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003907 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003908 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003909 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003910 step = NULL;
3911 stop = POP();
3912 start = TOP();
3913 slice = PySlice_New(start, stop, step);
3914 Py_DECREF(start);
3915 Py_DECREF(stop);
3916 Py_XDECREF(step);
3917 SET_TOP(slice);
3918 if (slice == NULL)
3919 goto error;
3920 DISPATCH();
3921 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003922
Benjamin Petersonddd19492018-09-16 22:38:02 -07003923 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003924 /* Handles f-string value formatting. */
3925 PyObject *result;
3926 PyObject *fmt_spec;
3927 PyObject *value;
3928 PyObject *(*conv_fn)(PyObject *);
3929 int which_conversion = oparg & FVC_MASK;
3930 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
3931
3932 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05003933 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05003934
3935 /* See if any conversion is specified. */
3936 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003937 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003938 case FVC_STR: conv_fn = PyObject_Str; break;
3939 case FVC_REPR: conv_fn = PyObject_Repr; break;
3940 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003941 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02003942 _PyErr_Format(tstate, PyExc_SystemError,
3943 "unexpected conversion flag %d",
3944 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04003945 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05003946 }
3947
3948 /* If there's a conversion function, call it and replace
3949 value with that result. Otherwise, just use value,
3950 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05003951 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003952 result = conv_fn(value);
3953 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003954 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003955 Py_XDECREF(fmt_spec);
3956 goto error;
3957 }
3958 value = result;
3959 }
3960
3961 /* If value is a unicode object, and there's no fmt_spec,
3962 then we know the result of format(value) is value
3963 itself. In that case, skip calling format(). I plan to
3964 move this optimization in to PyObject_Format()
3965 itself. */
3966 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
3967 /* Do nothing, just transfer ownership to result. */
3968 result = value;
3969 } else {
3970 /* Actually call format(). */
3971 result = PyObject_Format(value, fmt_spec);
3972 Py_DECREF(value);
3973 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05003974 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05003975 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05003976 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05003977 }
3978
Eric V. Smith135d5f42016-02-05 18:23:08 -05003979 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05003980 DISPATCH();
3981 }
3982
Benjamin Petersonddd19492018-09-16 22:38:02 -07003983 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03003984 int oldoparg = oparg;
3985 NEXTOPARG();
3986 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003987 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003988 }
Guido van Rossum8861b741996-07-30 16:49:37 +00003989
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003990
Antoine Pitrou042b1282010-08-13 21:15:58 +00003991#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003992 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00003993#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003994 default:
3995 fprintf(stderr,
3996 "XXX lineno: %d, opcode: %d\n",
3997 PyFrame_GetLineNumber(f),
3998 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02003999 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004000 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004001
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004002 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004003
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004004 /* This should never be reached. Every opcode should end with DISPATCH()
4005 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004006 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004007
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004008error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004009 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004010#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004011 if (!_PyErr_Occurred(tstate)) {
4012 _PyErr_SetString(tstate, PyExc_SystemError,
4013 "error return without exception set");
4014 }
Victor Stinner365b6932013-07-12 00:11:58 +02004015#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004016 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004017#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004018
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004019 /* Log traceback info. */
4020 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004021
Mark Shannoncb9879b2020-07-17 11:44:23 +01004022 if (tstate->c_tracefunc != NULL) {
4023 /* Make sure state is set to FRAME_EXECUTING for tracing */
4024 assert(f->f_state == FRAME_EXECUTING);
4025 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004026 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
4027 tstate, f);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004028 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004029exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004030 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004031 /* Unwind stacks if an exception occurred */
4032 while (f->f_iblock > 0) {
4033 /* Pop the current block. */
4034 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004036 if (b->b_type == EXCEPT_HANDLER) {
4037 UNWIND_EXCEPT_HANDLER(b);
4038 continue;
4039 }
4040 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004041 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004042 PyObject *exc, *val, *tb;
4043 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004044 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004045 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004046 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004047 PUSH(exc_info->exc_traceback);
4048 PUSH(exc_info->exc_value);
4049 if (exc_info->exc_type != NULL) {
4050 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004051 }
4052 else {
4053 Py_INCREF(Py_None);
4054 PUSH(Py_None);
4055 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004056 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004057 /* Make the raw exception data
4058 available to the handler,
4059 so a program can emulate the
4060 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004061 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004062 if (tb != NULL)
4063 PyException_SetTraceback(val, tb);
4064 else
4065 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004066 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004067 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004068 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004069 exc_info->exc_value = val;
4070 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 if (tb == NULL)
4072 tb = Py_None;
4073 Py_INCREF(tb);
4074 PUSH(tb);
4075 PUSH(val);
4076 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004077 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004078 if (_Py_TracingPossible(ceval2)) {
Mark Shannon877df852020-11-12 09:43:29 +00004079 instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004080 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004081 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004082 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004083 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004084 }
4085 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004086
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004087 /* End the loop as we still have an error */
4088 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004089 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004090
Pablo Galindof00828a2019-05-09 16:52:02 +01004091 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004092 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004093
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004094 /* Pop remaining stack entries. */
4095 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004096 PyObject *o = POP();
4097 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004098 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004099 f->f_stackdepth = 0;
4100 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004101exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004102 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004103 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004104 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
4105 tstate, f, PyTrace_RETURN, retval)) {
4106 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004107 }
4108 }
4109 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004110 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
4111 tstate, f, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004112 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004113 }
4114 }
4115 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004116
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004117 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004118exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004119 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4120 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004121 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004122 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004123
Victor Stinner0b72b232020-03-12 23:18:39 +01004124 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004125}
4126
Benjamin Petersonb204a422011-06-05 22:04:07 -05004127static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004128format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004129 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004130{
4131 int err;
4132 Py_ssize_t len = PyList_GET_SIZE(names);
4133 PyObject *name_str, *comma, *tail, *tmp;
4134
4135 assert(PyList_CheckExact(names));
4136 assert(len >= 1);
4137 /* Deal with the joys of natural language. */
4138 switch (len) {
4139 case 1:
4140 name_str = PyList_GET_ITEM(names, 0);
4141 Py_INCREF(name_str);
4142 break;
4143 case 2:
4144 name_str = PyUnicode_FromFormat("%U and %U",
4145 PyList_GET_ITEM(names, len - 2),
4146 PyList_GET_ITEM(names, len - 1));
4147 break;
4148 default:
4149 tail = PyUnicode_FromFormat(", %U, and %U",
4150 PyList_GET_ITEM(names, len - 2),
4151 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004152 if (tail == NULL)
4153 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004154 /* Chop off the last two objects in the list. This shouldn't actually
4155 fail, but we can't be too careful. */
4156 err = PyList_SetSlice(names, len - 2, len, NULL);
4157 if (err == -1) {
4158 Py_DECREF(tail);
4159 return;
4160 }
4161 /* Stitch everything up into a nice comma-separated list. */
4162 comma = PyUnicode_FromString(", ");
4163 if (comma == NULL) {
4164 Py_DECREF(tail);
4165 return;
4166 }
4167 tmp = PyUnicode_Join(comma, names);
4168 Py_DECREF(comma);
4169 if (tmp == NULL) {
4170 Py_DECREF(tail);
4171 return;
4172 }
4173 name_str = PyUnicode_Concat(tmp, tail);
4174 Py_DECREF(tmp);
4175 Py_DECREF(tail);
4176 break;
4177 }
4178 if (name_str == NULL)
4179 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004180 _PyErr_Format(tstate, PyExc_TypeError,
4181 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004182 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004183 len,
4184 kind,
4185 len == 1 ? "" : "s",
4186 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004187 Py_DECREF(name_str);
4188}
4189
4190static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004191missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4192 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004193 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004194{
Victor Stinner74319ae2016-08-25 00:04:09 +02004195 Py_ssize_t i, j = 0;
4196 Py_ssize_t start, end;
4197 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004198 const char *kind = positional ? "positional" : "keyword-only";
4199 PyObject *missing_names;
4200
4201 /* Compute the names of the arguments that are missing. */
4202 missing_names = PyList_New(missing);
4203 if (missing_names == NULL)
4204 return;
4205 if (positional) {
4206 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004207 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004208 }
4209 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004210 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004211 end = start + co->co_kwonlyargcount;
4212 }
4213 for (i = start; i < end; i++) {
4214 if (GETLOCAL(i) == NULL) {
4215 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4216 PyObject *name = PyObject_Repr(raw);
4217 if (name == NULL) {
4218 Py_DECREF(missing_names);
4219 return;
4220 }
4221 PyList_SET_ITEM(missing_names, j++, name);
4222 }
4223 }
4224 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004225 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004226 Py_DECREF(missing_names);
4227}
4228
4229static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004230too_many_positional(PyThreadState *tstate, PyCodeObject *co,
4231 Py_ssize_t given, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004232 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004233{
4234 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004235 Py_ssize_t kwonly_given = 0;
4236 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004237 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004238 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004239
Benjamin Petersone109c702011-06-24 09:37:26 -05004240 assert((co->co_flags & CO_VARARGS) == 0);
4241 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004242 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004243 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004244 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004245 }
4246 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004247 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004248 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004249 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004250 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004251 }
4252 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004253 plural = (co_argcount != 1);
4254 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004255 }
4256 if (sig == NULL)
4257 return;
4258 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004259 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4260 kwonly_sig = PyUnicode_FromFormat(format,
4261 given != 1 ? "s" : "",
4262 kwonly_given,
4263 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004264 if (kwonly_sig == NULL) {
4265 Py_DECREF(sig);
4266 return;
4267 }
4268 }
4269 else {
4270 /* This will not fail. */
4271 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004272 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004273 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004274 _PyErr_Format(tstate, PyExc_TypeError,
4275 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004276 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004277 sig,
4278 plural ? "s" : "",
4279 given,
4280 kwonly_sig,
4281 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004282 Py_DECREF(sig);
4283 Py_DECREF(kwonly_sig);
4284}
4285
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004286static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004287positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004288 Py_ssize_t kwcount, PyObject* const* kwnames,
4289 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004290{
4291 int posonly_conflicts = 0;
4292 PyObject* posonly_names = PyList_New(0);
4293
4294 for(int k=0; k < co->co_posonlyargcount; k++){
4295 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4296
4297 for (int k2=0; k2<kwcount; k2++){
4298 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
4299 PyObject* kwname = kwnames[k2];
4300 if (kwname == posonly_name){
4301 if(PyList_Append(posonly_names, kwname) != 0) {
4302 goto fail;
4303 }
4304 posonly_conflicts++;
4305 continue;
4306 }
4307
4308 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4309
4310 if ( cmp > 0) {
4311 if(PyList_Append(posonly_names, kwname) != 0) {
4312 goto fail;
4313 }
4314 posonly_conflicts++;
4315 } else if (cmp < 0) {
4316 goto fail;
4317 }
4318
4319 }
4320 }
4321 if (posonly_conflicts) {
4322 PyObject* comma = PyUnicode_FromString(", ");
4323 if (comma == NULL) {
4324 goto fail;
4325 }
4326 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4327 Py_DECREF(comma);
4328 if (error_names == NULL) {
4329 goto fail;
4330 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004331 _PyErr_Format(tstate, PyExc_TypeError,
4332 "%U() got some positional-only arguments passed"
4333 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004334 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004335 Py_DECREF(error_names);
4336 goto fail;
4337 }
4338
4339 Py_DECREF(posonly_names);
4340 return 0;
4341
4342fail:
4343 Py_XDECREF(posonly_names);
4344 return 1;
4345
4346}
4347
Guido van Rossumc2e20742006-02-27 22:32:47 +00004348/* This is gonna seem *real weird*, but if you put some other code between
Marcel Plch3a9ccee2018-04-06 23:22:04 +02004349 PyEval_EvalFrame() and _PyEval_EvalFrameDefault() you will need to adjust
Guido van Rossumc2e20742006-02-27 22:32:47 +00004350 the test in the if statements in Misc/gdbinit (pystack and pystackv). */
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004351
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01004352PyObject *
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004353_PyEval_EvalCode(PyThreadState *tstate,
4354 PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004355 PyObject *const *args, Py_ssize_t argcount,
4356 PyObject *const *kwnames, PyObject *const *kwargs,
Serhiy Storchakab7281052016-09-12 00:52:40 +03004357 Py_ssize_t kwcount, int kwstep,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004358 PyObject *const *defs, Py_ssize_t defcount,
Victor Stinner74319ae2016-08-25 00:04:09 +02004359 PyObject *kwdefs, PyObject *closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004360 PyObject *name, PyObject *qualname)
Tim Peters5ca576e2001-06-18 22:08:13 +00004361{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004362 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004363
Victor Stinner232dda62020-06-04 15:19:02 +02004364 PyCodeObject *co = (PyCodeObject*)_co;
4365
4366 if (!name) {
4367 name = co->co_name;
4368 }
4369 assert(name != NULL);
4370 assert(PyUnicode_Check(name));
4371
4372 if (!qualname) {
4373 qualname = name;
4374 }
4375 assert(qualname != NULL);
4376 assert(PyUnicode_Check(qualname));
4377
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02004378 PyObject *retval = NULL;
Pablo Galindocd74e662019-06-01 18:08:04 +01004379 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004381 if (globals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004382 _PyErr_SetString(tstate, PyExc_SystemError,
4383 "PyEval_EvalCodeEx: NULL globals");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004384 return NULL;
4385 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004386
Victor Stinnerc7020012016-08-16 23:40:29 +02004387 /* Create the frame */
Victor Stinner232dda62020-06-04 15:19:02 +02004388 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, co, globals, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004389 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004390 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004391 }
Victor Stinner232dda62020-06-04 15:19:02 +02004392 PyObject **fastlocals = f->f_localsplus;
4393 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004394
Victor Stinnerc7020012016-08-16 23:40:29 +02004395 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004396 PyObject *kwdict;
4397 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004398 if (co->co_flags & CO_VARKEYWORDS) {
4399 kwdict = PyDict_New();
4400 if (kwdict == NULL)
4401 goto fail;
4402 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004403 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004404 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004405 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004406 SETLOCAL(i, kwdict);
4407 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004408 else {
4409 kwdict = NULL;
4410 }
4411
Pablo Galindocd74e662019-06-01 18:08:04 +01004412 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004413 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004414 if (argcount > co->co_argcount) {
4415 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004416 }
4417 else {
4418 n = argcount;
4419 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004420 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004421 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004422 Py_INCREF(x);
4423 SETLOCAL(j, x);
4424 }
4425
Victor Stinnerc7020012016-08-16 23:40:29 +02004426 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004427 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004428 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004429 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004430 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004431 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004432 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004433 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004434
Serhiy Storchakab7281052016-09-12 00:52:40 +03004435 /* Handle keyword arguments passed as two strided arrays */
4436 kwcount *= kwstep;
4437 for (i = 0; i < kwcount; i += kwstep) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004438 PyObject **co_varnames;
Serhiy Storchakab7281052016-09-12 00:52:40 +03004439 PyObject *keyword = kwnames[i];
4440 PyObject *value = kwargs[i];
Victor Stinner17061a92016-08-16 23:39:42 +02004441 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004442
Benjamin Petersonb204a422011-06-05 22:04:07 -05004443 if (keyword == NULL || !PyUnicode_Check(keyword)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004444 _PyErr_Format(tstate, PyExc_TypeError,
4445 "%U() keywords must be strings",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004446 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004447 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004449
Benjamin Petersonb204a422011-06-05 22:04:07 -05004450 /* Speed hack: do raw pointer compares. As names are
4451 normally interned this should almost always hit. */
4452 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004453 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004454 PyObject *varname = co_varnames[j];
4455 if (varname == keyword) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004456 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004457 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004458 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004459
Benjamin Petersonb204a422011-06-05 22:04:07 -05004460 /* Slow fallback, just in case */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004461 for (j = co->co_posonlyargcount; j < total_args; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004462 PyObject *varname = co_varnames[j];
4463 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
Victor Stinner6fea7f72016-08-22 23:17:30 +02004464 if (cmp > 0) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004465 goto kw_found;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004466 }
4467 else if (cmp < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004468 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004469 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004470 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004471
Victor Stinner231d1f32017-01-11 02:12:06 +01004472 assert(j >= total_args);
4473 if (kwdict == NULL) {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004474
Victor Stinner438a12d2019-05-24 17:01:38 +02004475 if (co->co_posonlyargcount
4476 && positional_only_passed_as_keyword(tstate, co,
Victor Stinner232dda62020-06-04 15:19:02 +02004477 kwcount, kwnames,
4478 qualname))
Victor Stinner438a12d2019-05-24 17:01:38 +02004479 {
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004480 goto fail;
4481 }
4482
Victor Stinner438a12d2019-05-24 17:01:38 +02004483 _PyErr_Format(tstate, PyExc_TypeError,
4484 "%U() got an unexpected keyword argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004485 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004486 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004487 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004488
Christian Heimes0bd447f2013-07-20 14:48:10 +02004489 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4490 goto fail;
4491 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004492 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004493
Benjamin Petersonb204a422011-06-05 22:04:07 -05004494 kw_found:
4495 if (GETLOCAL(j) != NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004496 _PyErr_Format(tstate, PyExc_TypeError,
4497 "%U() got multiple values for argument '%S'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004498 qualname, keyword);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004499 goto fail;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004501 Py_INCREF(value);
4502 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004503 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004504
4505 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004506 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Victor Stinner232dda62020-06-04 15:19:02 +02004507 too_many_positional(tstate, co, argcount, defcount, fastlocals,
4508 qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004509 goto fail;
4510 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004511
4512 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004513 if (argcount < co->co_argcount) {
4514 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004515 Py_ssize_t missing = 0;
4516 for (i = argcount; i < m; i++) {
4517 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004518 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004519 }
4520 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004521 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004522 missing_arguments(tstate, co, missing, defcount, fastlocals,
4523 qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004524 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004525 }
4526 if (n > m)
4527 i = n - m;
4528 else
4529 i = 0;
4530 for (; i < defcount; i++) {
4531 if (GETLOCAL(m+i) == NULL) {
4532 PyObject *def = defs[i];
4533 Py_INCREF(def);
4534 SETLOCAL(m+i, def);
4535 }
4536 }
4537 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004538
4539 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004540 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004541 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004542 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004543 if (GETLOCAL(i) != NULL)
4544 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004545 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004546 if (kwdefs != NULL) {
Victor Stinner232dda62020-06-04 15:19:02 +02004547 PyObject *def = PyDict_GetItemWithError(kwdefs, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004548 if (def) {
4549 Py_INCREF(def);
4550 SETLOCAL(i, def);
4551 continue;
4552 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004553 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004554 goto fail;
4555 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004556 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004557 missing++;
4558 }
4559 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004560 missing_arguments(tstate, co, missing, -1, fastlocals,
4561 qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004562 goto fail;
4563 }
4564 }
4565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004566 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05004567 vars into frame. */
4568 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004569 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02004570 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05004571 /* Possibly account for the cell variable being an argument. */
4572 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07004573 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05004574 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05004575 /* Clear the local copy. */
4576 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004577 }
4578 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05004579 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07004580 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05004581 if (c == NULL)
4582 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05004583 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004584 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004585
4586 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05004587 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
4588 PyObject *o = PyTuple_GET_ITEM(closure, i);
4589 Py_INCREF(o);
4590 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004591 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004592
Yury Selivanoveb636452016-09-08 22:01:51 -07004593 /* Handle generator/coroutine/asynchronous generator */
4594 if (co->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004595 PyObject *gen;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004596 int is_coro = co->co_flags & CO_COROUTINE;
Yury Selivanov94c22632015-06-04 10:16:51 -04004597
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004598 /* Don't need to keep the reference to f_back, it will be set
4599 * when the generator is resumed. */
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004600 Py_CLEAR(f->f_back);
Neil Schemenauer2b13ce82001-06-21 02:41:10 +00004601
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004602 /* Create a new generator that owns the ready to run frame
4603 * and return that as the value. */
Yury Selivanov5376ba92015-06-22 12:19:30 -04004604 if (is_coro) {
4605 gen = PyCoro_New(f, name, qualname);
Yury Selivanoveb636452016-09-08 22:01:51 -07004606 } else if (co->co_flags & CO_ASYNC_GENERATOR) {
4607 gen = PyAsyncGen_New(f, name, qualname);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004608 } else {
4609 gen = PyGen_NewWithQualName(f, name, qualname);
4610 }
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004611 if (gen == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004612 return NULL;
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004613 }
INADA Naoki9c157762016-12-26 18:52:46 +09004614
INADA Naoki6a3cedf2016-12-26 18:01:46 +09004615 _PyObject_GC_TRACK(f);
Yury Selivanov75445082015-05-11 22:57:16 -04004616
Yury Selivanov75445082015-05-11 22:57:16 -04004617 return gen;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 }
Tim Peters5ca576e2001-06-18 22:08:13 +00004619
Victor Stinnerb9e68122019-11-14 12:20:46 +01004620 retval = _PyEval_EvalFrame(tstate, f, 0);
Tim Peters5ca576e2001-06-18 22:08:13 +00004621
Thomas Woutersce272b62007-09-19 21:19:28 +00004622fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00004623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004624 /* decref'ing the frame can cause __del__ methods to get invoked,
4625 which can call back into Python. While we're done with the
4626 current Python frame (f), the associated C stack is still in use,
4627 so recursion_depth must be boosted for the duration.
4628 */
INADA Naoki5a625d02016-12-24 20:19:08 +09004629 if (Py_REFCNT(f) > 1) {
4630 Py_DECREF(f);
4631 _PyObject_GC_TRACK(f);
4632 }
4633 else {
4634 ++tstate->recursion_depth;
4635 Py_DECREF(f);
4636 --tstate->recursion_depth;
4637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004638 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00004639}
4640
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004641
4642PyObject *
4643_PyEval_EvalCodeWithName(PyObject *_co, PyObject *globals, PyObject *locals,
4644 PyObject *const *args, Py_ssize_t argcount,
4645 PyObject *const *kwnames, PyObject *const *kwargs,
4646 Py_ssize_t kwcount, int kwstep,
4647 PyObject *const *defs, Py_ssize_t defcount,
4648 PyObject *kwdefs, PyObject *closure,
4649 PyObject *name, PyObject *qualname)
4650{
4651 PyThreadState *tstate = _PyThreadState_GET();
4652 return _PyEval_EvalCode(tstate, _co, globals, locals,
4653 args, argcount,
4654 kwnames, kwargs,
4655 kwcount, kwstep,
4656 defs, defcount,
4657 kwdefs, closure,
4658 name, qualname);
4659}
4660
Victor Stinner40ee3012014-06-16 15:59:28 +02004661PyObject *
4662PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004663 PyObject *const *args, int argcount,
4664 PyObject *const *kws, int kwcount,
4665 PyObject *const *defs, int defcount,
4666 PyObject *kwdefs, PyObject *closure)
Victor Stinner40ee3012014-06-16 15:59:28 +02004667{
4668 return _PyEval_EvalCodeWithName(_co, globals, locals,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004669 args, argcount,
Zackery Spytzc6ea8972017-07-31 08:24:37 -06004670 kws, kws != NULL ? kws + 1 : NULL,
4671 kwcount, 2,
Victor Stinner9be7e7b2016-08-19 16:11:43 +02004672 defs, defcount,
4673 kwdefs, closure,
Victor Stinner40ee3012014-06-16 15:59:28 +02004674 NULL, NULL);
4675}
Tim Peters5ca576e2001-06-18 22:08:13 +00004676
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004677static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02004678special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004680 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05004681 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02004682 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02004683 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004684 return NULL;
4685 }
4686 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004687}
4688
4689
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004690/* Logic for the raise statement (too complicated for inlining).
4691 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004692static int
Victor Stinner09532fe2019-05-10 23:39:09 +02004693do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004694{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004695 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00004696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004697 if (exc == NULL) {
4698 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01004699 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004700 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01004701 type = exc_info->exc_type;
4702 value = exc_info->exc_value;
4703 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02004704 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004705 _PyErr_SetString(tstate, PyExc_RuntimeError,
4706 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004707 return 0;
4708 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004709 Py_XINCREF(type);
4710 Py_XINCREF(value);
4711 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02004712 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004713 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004714 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 /* We support the following forms of raise:
4717 raise
Collin Winter828f04a2007-08-31 00:04:24 +00004718 raise <instance>
4719 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 if (PyExceptionClass_Check(exc)) {
4722 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004723 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004724 if (value == NULL)
4725 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004726 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004727 _PyErr_Format(tstate, PyExc_TypeError,
4728 "calling %R should have returned an instance of "
4729 "BaseException, not %R",
4730 type, Py_TYPE(value));
4731 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05004732 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 }
4734 else if (PyExceptionInstance_Check(exc)) {
4735 value = exc;
4736 type = PyExceptionInstance_Class(exc);
4737 Py_INCREF(type);
4738 }
4739 else {
4740 /* Not something you can raise. You get an exception
4741 anyway, just not what you specified :-) */
4742 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02004743 _PyErr_SetString(tstate, PyExc_TypeError,
4744 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004745 goto raise_error;
4746 }
Collin Winter828f04a2007-08-31 00:04:24 +00004747
Serhiy Storchakac0191582016-09-27 11:37:10 +03004748 assert(type != NULL);
4749 assert(value != NULL);
4750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004751 if (cause) {
4752 PyObject *fixed_cause;
4753 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01004754 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004755 if (fixed_cause == NULL)
4756 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004757 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004758 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004759 else if (PyExceptionInstance_Check(cause)) {
4760 fixed_cause = cause;
4761 }
4762 else if (cause == Py_None) {
4763 Py_DECREF(cause);
4764 fixed_cause = NULL;
4765 }
4766 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004767 _PyErr_SetString(tstate, PyExc_TypeError,
4768 "exception causes must derive from "
4769 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004770 goto raise_error;
4771 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07004772 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004773 }
Collin Winter828f04a2007-08-31 00:04:24 +00004774
Victor Stinner438a12d2019-05-24 17:01:38 +02004775 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01004776 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03004777 Py_DECREF(value);
4778 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004779 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00004780
4781raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004782 Py_XDECREF(value);
4783 Py_XDECREF(type);
4784 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004785 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00004786}
4787
Tim Petersd6d010b2001-06-21 02:49:55 +00004788/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00004789 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00004790
Guido van Rossum0368b722007-05-11 16:50:42 +00004791 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
4792 with a variable target.
4793*/
Tim Petersd6d010b2001-06-21 02:49:55 +00004794
Barry Warsawe42b18f1997-08-25 22:13:04 +00004795static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004796unpack_iterable(PyThreadState *tstate, PyObject *v,
4797 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00004798{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004799 int i = 0, j = 0;
4800 Py_ssize_t ll = 0;
4801 PyObject *it; /* iter(v) */
4802 PyObject *w;
4803 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00004804
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004805 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00004806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004807 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004808 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004809 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01004810 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004811 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004812 _PyErr_Format(tstate, PyExc_TypeError,
4813 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01004814 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02004815 }
4816 return 0;
4817 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004819 for (; i < argcnt; i++) {
4820 w = PyIter_Next(it);
4821 if (w == NULL) {
4822 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004823 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04004824 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004825 _PyErr_Format(tstate, PyExc_ValueError,
4826 "not enough values to unpack "
4827 "(expected %d, got %d)",
4828 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004829 }
4830 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02004831 _PyErr_Format(tstate, PyExc_ValueError,
4832 "not enough values to unpack "
4833 "(expected at least %d, got %d)",
4834 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04004835 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004836 }
4837 goto Error;
4838 }
4839 *--sp = w;
4840 }
Tim Petersd6d010b2001-06-21 02:49:55 +00004841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004842 if (argcntafter == -1) {
4843 /* We better have exhausted the iterator now. */
4844 w = PyIter_Next(it);
4845 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004846 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004847 goto Error;
4848 Py_DECREF(it);
4849 return 1;
4850 }
4851 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02004852 _PyErr_Format(tstate, PyExc_ValueError,
4853 "too many values to unpack (expected %d)",
4854 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004855 goto Error;
4856 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004858 l = PySequence_List(it);
4859 if (l == NULL)
4860 goto Error;
4861 *--sp = l;
4862 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00004863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004864 ll = PyList_GET_SIZE(l);
4865 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004866 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04004867 "not enough values to unpack (expected at least %d, got %zd)",
4868 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004869 goto Error;
4870 }
Guido van Rossum0368b722007-05-11 16:50:42 +00004871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004872 /* Pop the "after-variable" args off the list. */
4873 for (j = argcntafter; j > 0; j--, i++) {
4874 *--sp = PyList_GET_ITEM(l, ll - j);
4875 }
4876 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01004877 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004878 Py_DECREF(it);
4879 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00004880
Tim Petersd6d010b2001-06-21 02:49:55 +00004881Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004882 for (; i > 0; i--, sp++)
4883 Py_DECREF(*sp);
4884 Py_XDECREF(it);
4885 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00004886}
4887
4888
Guido van Rossum96a42c81992-01-12 02:29:51 +00004889#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00004890static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004891prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004893 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02004894 if (PyObject_Print(v, stdout, 0) != 0) {
4895 /* Don't know what else to do */
4896 _PyErr_Clear(tstate);
4897 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004898 printf("\n");
4899 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004900}
Guido van Rossum3f5da241990-12-20 15:06:42 +00004901#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00004902
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004903static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004904call_exc_trace(Py_tracefunc func, PyObject *self,
4905 PyThreadState *tstate, PyFrameObject *f)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004906{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004907 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004908 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004909 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004910 if (value == NULL) {
4911 value = Py_None;
4912 Py_INCREF(value);
4913 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004914 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01004915 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004916 arg = PyTuple_Pack(3, type, value, traceback);
4917 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004918 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004919 return;
4920 }
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004921 err = call_trace(func, self, tstate, f, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004922 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02004923 if (err == 0) {
4924 _PyErr_Restore(tstate, type, value, orig_traceback);
4925 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004926 else {
4927 Py_XDECREF(type);
4928 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02004929 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004930 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004931}
4932
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00004933static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004934call_trace_protected(Py_tracefunc func, PyObject *obj,
4935 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004936 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00004937{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004938 PyObject *type, *value, *traceback;
4939 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02004940 _PyErr_Fetch(tstate, &type, &value, &traceback);
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004941 err = call_trace(func, obj, tstate, frame, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 if (err == 0)
4943 {
Victor Stinner438a12d2019-05-24 17:01:38 +02004944 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004945 return 0;
4946 }
4947 else {
4948 Py_XDECREF(type);
4949 Py_XDECREF(value);
4950 Py_XDECREF(traceback);
4951 return -1;
4952 }
Fred Drake4ec5d562001-10-04 19:26:43 +00004953}
4954
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00004955static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004956call_trace(Py_tracefunc func, PyObject *obj,
4957 PyThreadState *tstate, PyFrameObject *frame,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004958 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00004959{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004960 int result;
4961 if (tstate->tracing)
4962 return 0;
4963 tstate->tracing++;
4964 tstate->use_tracing = 0;
4965 result = func(obj, frame, what, arg);
4966 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4967 || (tstate->c_profilefunc != NULL));
4968 tstate->tracing--;
4969 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00004970}
4971
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004972PyObject *
4973_PyEval_CallTracing(PyObject *func, PyObject *args)
4974{
Victor Stinner50b48572018-11-01 01:51:40 +01004975 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004976 int save_tracing = tstate->tracing;
4977 int save_use_tracing = tstate->use_tracing;
4978 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004979
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004980 tstate->tracing = 0;
4981 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
4982 || (tstate->c_profilefunc != NULL));
4983 result = PyObject_Call(func, args, NULL);
4984 tstate->tracing = save_tracing;
4985 tstate->use_tracing = save_use_tracing;
4986 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00004987}
4988
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00004989/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00004990static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00004991maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004992 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00004993 PyCodeAddressRange *bounds, int *instr_prev)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00004994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004995 int result = 0;
4996 int line = frame->f_lineno;
Michael W. Hudson006c7522002-11-08 13:08:46 +00004997
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004998 /* If the last instruction executed isn't in the current
4999 instruction window, reset the window.
5000 */
Mark Shannon877df852020-11-12 09:43:29 +00005001 line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Nick Coghlan5a851672017-09-08 10:14:16 +10005002 /* If the last instruction falls at the start of a line or if it
5003 represents a jump backwards, update the frame's line number and
5004 then call the trace function if we're tracing source lines.
5005 */
Mark Shannon877df852020-11-12 09:43:29 +00005006 if ((line != frame->f_lineno || frame->f_lasti < *instr_prev)) {
5007 if (line != -1) {
5008 frame->f_lineno = line;
5009 if (frame->f_trace_lines) {
5010 result = call_trace(func, obj, tstate, frame, PyTrace_LINE, Py_None);
5011 }
Nick Coghlan5a851672017-09-08 10:14:16 +10005012 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 }
George King20faa682017-10-18 17:44:22 -07005014 /* Always emit an opcode event if we're tracing all opcodes. */
5015 if (frame->f_trace_opcodes) {
5016 result = call_trace(func, obj, tstate, frame, PyTrace_OPCODE, Py_None);
5017 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 *instr_prev = frame->f_lasti;
5019 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005020}
5021
Victor Stinner309d7cc2020-03-13 16:39:12 +01005022int
5023_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5024{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005025 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005026 /* The caller must hold the GIL */
5027 assert(PyGILState_Check());
5028
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005029 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005030 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005031 PyThreadState *current_tstate = _PyThreadState_GET();
5032 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005033 return -1;
5034 }
5035
5036 PyObject *profileobj = tstate->c_profileobj;
5037
5038 tstate->c_profilefunc = NULL;
5039 tstate->c_profileobj = NULL;
5040 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5041 tstate->use_tracing = tstate->c_tracefunc != NULL;
5042 Py_XDECREF(profileobj);
5043
5044 Py_XINCREF(arg);
5045 tstate->c_profileobj = arg;
5046 tstate->c_profilefunc = func;
5047
5048 /* Flag that tracing or profiling is turned on */
5049 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5050 return 0;
5051}
5052
Fred Drake5755ce62001-06-27 19:19:46 +00005053void
5054PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005055{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005056 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005057 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005058 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005059 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5060 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005061}
5062
5063int
5064_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5065{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005066 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005067 /* The caller must hold the GIL */
5068 assert(PyGILState_Check());
5069
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005070 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005071 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005072 PyThreadState *current_tstate = _PyThreadState_GET();
5073 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005074 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005075 }
5076
Victor Stinnerda2914d2020-03-20 09:29:08 +01005077 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005078 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005079 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005080
5081 tstate->c_tracefunc = NULL;
5082 tstate->c_traceobj = NULL;
5083 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5084 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5085 Py_XDECREF(traceobj);
5086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005088 tstate->c_traceobj = arg;
5089 tstate->c_tracefunc = func;
5090
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005091 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005092 tstate->use_tracing = ((func != NULL)
5093 || (tstate->c_profilefunc != NULL));
5094
5095 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005096}
5097
5098void
5099PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5100{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005101 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005102 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005103 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005104 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5105 }
Fred Draked0838392001-06-16 21:02:31 +00005106}
5107
Victor Stinner309d7cc2020-03-13 16:39:12 +01005108
Yury Selivanov75445082015-05-11 22:57:16 -04005109void
Victor Stinner838f2642019-06-13 22:41:23 +02005110_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005111{
5112 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005113 tstate->coroutine_origin_tracking_depth = new_depth;
5114}
5115
5116int
5117_PyEval_GetCoroutineOriginTrackingDepth(void)
5118{
Victor Stinner50b48572018-11-01 01:51:40 +01005119 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005120 return tstate->coroutine_origin_tracking_depth;
5121}
5122
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005123int
Yury Selivanoveb636452016-09-08 22:01:51 -07005124_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5125{
Victor Stinner50b48572018-11-01 01:51:40 +01005126 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005127
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005128 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005129 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005130 }
5131
Yury Selivanoveb636452016-09-08 22:01:51 -07005132 Py_XINCREF(firstiter);
5133 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005134 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005135}
5136
5137PyObject *
5138_PyEval_GetAsyncGenFirstiter(void)
5139{
Victor Stinner50b48572018-11-01 01:51:40 +01005140 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005141 return tstate->async_gen_firstiter;
5142}
5143
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005144int
Yury Selivanoveb636452016-09-08 22:01:51 -07005145_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5146{
Victor Stinner50b48572018-11-01 01:51:40 +01005147 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005148
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005149 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005150 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005151 }
5152
Yury Selivanoveb636452016-09-08 22:01:51 -07005153 Py_XINCREF(finalizer);
5154 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005155 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005156}
5157
5158PyObject *
5159_PyEval_GetAsyncGenFinalizer(void)
5160{
Victor Stinner50b48572018-11-01 01:51:40 +01005161 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005162 return tstate->async_gen_finalizer;
5163}
5164
Victor Stinner438a12d2019-05-24 17:01:38 +02005165PyFrameObject *
5166PyEval_GetFrame(void)
5167{
5168 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005169 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005170}
5171
Guido van Rossumb209a111997-04-29 18:18:01 +00005172PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005173PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005174{
Victor Stinner438a12d2019-05-24 17:01:38 +02005175 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005176 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005177 if (current_frame == NULL)
Victor Stinner438a12d2019-05-24 17:01:38 +02005178 return tstate->interp->builtins;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005179 else
5180 return current_frame->f_builtins;
Guido van Rossum6135a871995-01-09 17:53:26 +00005181}
5182
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005183/* Convenience function to get a builtin from its name */
5184PyObject *
5185_PyEval_GetBuiltinId(_Py_Identifier *name)
5186{
Victor Stinner438a12d2019-05-24 17:01:38 +02005187 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005188 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5189 if (attr) {
5190 Py_INCREF(attr);
5191 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005192 else if (!_PyErr_Occurred(tstate)) {
5193 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005194 }
5195 return attr;
5196}
5197
Guido van Rossumb209a111997-04-29 18:18:01 +00005198PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005199PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005200{
Victor Stinner438a12d2019-05-24 17:01:38 +02005201 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005202 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005203 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005204 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005205 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005206 }
5207
Victor Stinner438a12d2019-05-24 17:01:38 +02005208 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005209 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005210 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005211
5212 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005213 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005214}
5215
Guido van Rossumb209a111997-04-29 18:18:01 +00005216PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005217PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005218{
Victor Stinner438a12d2019-05-24 17:01:38 +02005219 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005220 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005221 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005222 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005223 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005224
5225 assert(current_frame->f_globals != NULL);
5226 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005227}
5228
Guido van Rossum6135a871995-01-09 17:53:26 +00005229int
Tim Peters5ba58662001-07-16 02:29:45 +00005230PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005231{
Victor Stinner438a12d2019-05-24 17:01:38 +02005232 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005233 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005234 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005236 if (current_frame != NULL) {
5237 const int codeflags = current_frame->f_code->co_flags;
5238 const int compilerflags = codeflags & PyCF_MASK;
5239 if (compilerflags) {
5240 result = 1;
5241 cf->cf_flags |= compilerflags;
5242 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005243#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 if (codeflags & CO_GENERATOR_ALLOWED) {
5245 result = 1;
5246 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5247 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005248#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 }
5250 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005251}
5252
Guido van Rossum3f5da241990-12-20 15:06:42 +00005253
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005254const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005255PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005257 if (PyMethod_Check(func))
5258 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5259 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005260 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 else if (PyCFunction_Check(func))
5262 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5263 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005264 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005265}
5266
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005267const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005268PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005269{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005270 if (PyMethod_Check(func))
5271 return "()";
5272 else if (PyFunction_Check(func))
5273 return "()";
5274 else if (PyCFunction_Check(func))
5275 return "()";
5276 else
5277 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005278}
5279
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005280#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005281if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005282 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
5283 tstate, tstate->frame, \
5284 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005285 x = NULL; \
5286 } \
5287 else { \
5288 x = call; \
5289 if (tstate->c_profilefunc != NULL) { \
5290 if (x == NULL) { \
5291 call_trace_protected(tstate->c_profilefunc, \
5292 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005293 tstate, tstate->frame, \
5294 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005295 /* XXX should pass (type, value, tb) */ \
5296 } else { \
5297 if (call_trace(tstate->c_profilefunc, \
5298 tstate->c_profileobj, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005299 tstate, tstate->frame, \
5300 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 Py_DECREF(x); \
5302 x = NULL; \
5303 } \
5304 } \
5305 } \
5306 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005307} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005308 x = call; \
5309 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005310
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005311
5312static PyObject *
5313trace_call_function(PyThreadState *tstate,
5314 PyObject *func,
5315 PyObject **args, Py_ssize_t nargs,
5316 PyObject *kwnames)
5317{
5318 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005319 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005320 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005321 return x;
5322 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005323 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005324 /* We need to create a temporary bound method as argument
5325 for profiling.
5326
5327 If nargs == 0, then this cannot work because we have no
5328 "self". In any case, the call itself would raise
5329 TypeError (foo needs an argument), so we just skip
5330 profiling. */
5331 PyObject *self = args[0];
5332 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5333 if (func == NULL) {
5334 return NULL;
5335 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005336 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005337 args+1, nargs-1,
5338 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005339 Py_DECREF(func);
5340 return x;
5341 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005342 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005343}
5344
Victor Stinner415c5102017-01-11 00:54:57 +01005345/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5346 to reduce the stack consumption. */
5347Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Victor Stinner09532fe2019-05-10 23:39:09 +02005348call_function(PyThreadState *tstate, PyObject ***pp_stack, Py_ssize_t oparg, PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005349{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005350 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005351 PyObject *func = *pfunc;
5352 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005353 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5354 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005355 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005356
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005357 if (tstate->use_tracing) {
5358 x = trace_call_function(tstate, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005359 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005360 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005361 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005362 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005363
Victor Stinner438a12d2019-05-24 17:01:38 +02005364 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005365
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005366 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005367 while ((*pp_stack) > pfunc) {
5368 w = EXT_POP(*pp_stack);
5369 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005373}
5374
Jeremy Hylton52820442001-01-03 23:52:36 +00005375static PyObject *
Victor Stinner09532fe2019-05-10 23:39:09 +02005376do_call_core(PyThreadState *tstate, PyObject *func, PyObject *callargs, PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005377{
jdemeyere89de732018-09-19 12:06:20 +02005378 PyObject *result;
5379
scoder4c9ea092020-05-12 16:12:41 +02005380 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005381 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005382 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005383 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005384 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005385 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5386 if (nargs > 0 && tstate->use_tracing) {
5387 /* We need to create a temporary bound method as argument
5388 for profiling.
5389
5390 If nargs == 0, then this cannot work because we have no
5391 "self". In any case, the call itself would raise
5392 TypeError (foo needs an argument), so we just skip
5393 profiling. */
5394 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5395 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5396 if (func == NULL) {
5397 return NULL;
5398 }
5399
Victor Stinner4d231bc2019-11-14 13:36:21 +01005400 C_TRACE(result, _PyObject_FastCallDictTstate(
5401 tstate, func,
5402 &_PyTuple_ITEMS(callargs)[1],
5403 nargs - 1,
5404 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005405 Py_DECREF(func);
5406 return result;
5407 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005408 }
jdemeyere89de732018-09-19 12:06:20 +02005409 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005410}
5411
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005412/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005413 nb_index slot defined, and store in *pi.
5414 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005415 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005416 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005417*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005418int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005419_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005420{
Victor Stinner438a12d2019-05-24 17:01:38 +02005421 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005422 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005423 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005424 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005425 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005426 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005427 return 0;
5428 }
5429 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005430 _PyErr_SetString(tstate, PyExc_TypeError,
5431 "slice indices must be integers or "
5432 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 return 0;
5434 }
5435 *pi = x;
5436 }
5437 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005438}
5439
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005440int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005441_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005442{
Victor Stinner438a12d2019-05-24 17:01:38 +02005443 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005444 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005445 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005446 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005447 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005448 return 0;
5449 }
5450 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005451 _PyErr_SetString(tstate, PyExc_TypeError,
5452 "slice indices must be integers or "
5453 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005454 return 0;
5455 }
5456 *pi = x;
5457 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02005458}
5459
Thomas Wouters52152252000-08-17 22:55:00 +00005460static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005461import_name(PyThreadState *tstate, PyFrameObject *f,
5462 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005463{
5464 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005465 PyObject *import_func, *res;
5466 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005467
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005468 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005469 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005470 if (!_PyErr_Occurred(tstate)) {
5471 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005472 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005473 return NULL;
5474 }
5475
5476 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005477 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005478 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02005479 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005480 return NULL;
5481 }
5482 res = PyImport_ImportModuleLevelObject(
5483 name,
5484 f->f_globals,
5485 f->f_locals == NULL ? Py_None : f->f_locals,
5486 fromlist,
5487 ilevel);
5488 return res;
5489 }
5490
5491 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02005492
5493 stack[0] = name;
5494 stack[1] = f->f_globals;
5495 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
5496 stack[3] = fromlist;
5497 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02005498 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03005499 Py_DECREF(import_func);
5500 return res;
5501}
5502
5503static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005504import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00005505{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005506 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08005507 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005508
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005509 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02005510 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005511 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005512 /* Issue #17636: in case this failed because of a circular relative
5513 import, try to fallback on reading the module directly from
5514 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02005515 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07005516 if (pkgname == NULL) {
5517 goto error;
5518 }
Oren Milman6db70332017-09-19 14:23:01 +03005519 if (!PyUnicode_Check(pkgname)) {
5520 Py_CLEAR(pkgname);
5521 goto error;
5522 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02005523 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07005524 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08005525 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005526 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07005527 }
Eric Snow3f9eee62017-09-15 16:35:20 -06005528 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02005529 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02005530 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07005531 goto error;
5532 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005533 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005534 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07005535 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005536 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005537 if (pkgname == NULL) {
5538 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
5539 if (pkgname_or_unknown == NULL) {
5540 Py_XDECREF(pkgpath);
5541 return NULL;
5542 }
5543 } else {
5544 pkgname_or_unknown = pkgname;
5545 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005546
5547 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005548 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08005549 errmsg = PyUnicode_FromFormat(
5550 "cannot import name %R from %R (unknown location)",
5551 name, pkgname_or_unknown
5552 );
Stefan Krah027b09c2019-03-25 21:50:58 +01005553 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005554 PyErr_SetImportError(errmsg, pkgname, NULL);
5555 }
5556 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07005557 _Py_IDENTIFIER(__spec__);
5558 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07005559 const char *fmt =
5560 _PyModuleSpec_IsInitializing(spec) ?
5561 "cannot import name %R from partially initialized module %R "
5562 "(most likely due to a circular import) (%S)" :
5563 "cannot import name %R from %R (%S)";
5564 Py_XDECREF(spec);
5565
5566 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01005567 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08005568 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08005569 }
5570
Xiang Zhang4830f582017-03-21 11:13:42 +08005571 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08005572 Py_XDECREF(pkgname_or_unknown);
5573 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07005574 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00005575}
Guido van Rossumac7be682001-01-17 15:42:30 +00005576
Thomas Wouters52152252000-08-17 22:55:00 +00005577static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005578import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00005579{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02005580 _Py_IDENTIFIER(__all__);
5581 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005582 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005583 int skip_leading_underscores = 0;
5584 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00005585
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005586 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
5587 return -1; /* Unexpected error */
5588 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005589 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005590 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
5591 return -1;
5592 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005593 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005594 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02005595 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005596 return -1;
5597 }
5598 all = PyMapping_Keys(dict);
5599 Py_DECREF(dict);
5600 if (all == NULL)
5601 return -1;
5602 skip_leading_underscores = 1;
5603 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00005604
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005605 for (pos = 0, err = 0; ; pos++) {
5606 name = PySequence_GetItem(all, pos);
5607 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005608 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005609 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02005610 }
5611 else {
5612 _PyErr_Clear(tstate);
5613 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005614 break;
5615 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005616 if (!PyUnicode_Check(name)) {
5617 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
5618 if (modname == NULL) {
5619 Py_DECREF(name);
5620 err = -1;
5621 break;
5622 }
5623 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005624 _PyErr_Format(tstate, PyExc_TypeError,
5625 "module __name__ must be a string, not %.100s",
5626 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005627 }
5628 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005629 _PyErr_Format(tstate, PyExc_TypeError,
5630 "%s in %U.%s must be str, not %.100s",
5631 skip_leading_underscores ? "Key" : "Item",
5632 modname,
5633 skip_leading_underscores ? "__dict__" : "__all__",
5634 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08005635 }
5636 Py_DECREF(modname);
5637 Py_DECREF(name);
5638 err = -1;
5639 break;
5640 }
5641 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03005642 if (PyUnicode_READY(name) == -1) {
5643 Py_DECREF(name);
5644 err = -1;
5645 break;
5646 }
5647 if (PyUnicode_READ_CHAR(name, 0) == '_') {
5648 Py_DECREF(name);
5649 continue;
5650 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005651 }
5652 value = PyObject_GetAttr(v, name);
5653 if (value == NULL)
5654 err = -1;
5655 else if (PyDict_CheckExact(locals))
5656 err = PyDict_SetItem(locals, name, value);
5657 else
5658 err = PyObject_SetItem(locals, name, value);
5659 Py_DECREF(name);
5660 Py_XDECREF(value);
5661 if (err != 0)
5662 break;
5663 }
5664 Py_DECREF(all);
5665 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00005666}
5667
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005668static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005669check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005670{
Victor Stinnera102ed72020-02-07 02:24:48 +01005671 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005672 /* check_args_iterable() may be called with a live exception:
5673 * clear it to prevent calling _PyObject_FunctionStr() with an
5674 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01005675 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005676 PyObject *funcstr = _PyObject_FunctionStr(func);
5677 if (funcstr != NULL) {
5678 _PyErr_Format(tstate, PyExc_TypeError,
5679 "%U argument after * must be an iterable, not %.200s",
5680 funcstr, Py_TYPE(args)->tp_name);
5681 Py_DECREF(funcstr);
5682 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005683 return -1;
5684 }
5685 return 0;
5686}
5687
5688static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005689format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005690{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005691 /* _PyDict_MergeEx raises attribute
5692 * error (percolated from an attempt
5693 * to get 'keys' attribute) instead of
5694 * a type error if its second argument
5695 * is not a mapping.
5696 */
Victor Stinner438a12d2019-05-24 17:01:38 +02005697 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005698 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005699 PyObject *funcstr = _PyObject_FunctionStr(func);
5700 if (funcstr != NULL) {
5701 _PyErr_Format(
5702 tstate, PyExc_TypeError,
5703 "%U argument after ** must be a mapping, not %.200s",
5704 funcstr, Py_TYPE(kwargs)->tp_name);
5705 Py_DECREF(funcstr);
5706 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005707 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005708 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005709 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02005710 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005711 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01005712 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01005713 PyObject *funcstr = _PyObject_FunctionStr(func);
5714 if (funcstr != NULL) {
5715 PyObject *key = PyTuple_GET_ITEM(val, 0);
5716 _PyErr_Format(
5717 tstate, PyExc_TypeError,
5718 "%U got multiple values for keyword argument '%S'",
5719 funcstr, key);
5720 Py_DECREF(funcstr);
5721 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005722 Py_XDECREF(exc);
5723 Py_XDECREF(val);
5724 Py_XDECREF(tb);
5725 }
5726 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005727 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02005728 }
5729 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03005730}
5731
Guido van Rossumac7be682001-01-17 15:42:30 +00005732static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005733format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
5734 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00005735{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005736 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00005737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005738 if (!obj)
5739 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005740
Serhiy Storchaka06515832016-11-20 09:13:07 +02005741 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005742 if (!obj_str)
5743 return;
Paul Prescode68140d2000-08-30 20:25:01 +00005744
Victor Stinner438a12d2019-05-24 17:01:38 +02005745 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00005746}
Guido van Rossum950361c1997-01-24 13:49:28 +00005747
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005748static void
Victor Stinner438a12d2019-05-24 17:01:38 +02005749format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005750{
5751 PyObject *name;
5752 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02005753 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005754 return;
5755 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
5756 name = PyTuple_GET_ITEM(co->co_cellvars,
5757 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005758 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005759 PyExc_UnboundLocalError,
5760 UNBOUNDLOCAL_ERROR_MSG,
5761 name);
5762 } else {
5763 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
5764 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02005765 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00005766 UNBOUNDFREE_ERROR_MSG, name);
5767 }
5768}
5769
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005770static void
Mark Shannonfee55262019-11-21 09:11:43 +00005771format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005772{
5773 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
5774 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005775 _PyErr_Format(tstate, PyExc_TypeError,
5776 "'async with' received an object from __aenter__ "
5777 "that does not implement __await__: %.100s",
5778 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005779 }
Mark Shannonfee55262019-11-21 09:11:43 +00005780 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005781 _PyErr_Format(tstate, PyExc_TypeError,
5782 "'async with' received an object from __aexit__ "
5783 "that does not implement __await__: %.100s",
5784 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03005785 }
5786 }
5787}
5788
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005789static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005790unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03005791 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005792{
5793 PyObject *res;
5794 if (Py_REFCNT(v) == 2) {
5795 /* In the common case, there are 2 references to the value
5796 * stored in 'variable' when the += is performed: one on the
5797 * value stack (in 'v') and one still stored in the
5798 * 'variable'. We try to delete the variable now to reduce
5799 * the refcnt to 1.
5800 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005801 int opcode, oparg;
5802 NEXTOPARG();
5803 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005804 case STORE_FAST:
5805 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005806 PyObject **fastlocals = f->f_localsplus;
5807 if (GETLOCAL(oparg) == v)
5808 SETLOCAL(oparg, NULL);
5809 break;
5810 }
5811 case STORE_DEREF:
5812 {
5813 PyObject **freevars = (f->f_localsplus +
5814 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005815 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05005816 if (PyCell_GET(c) == v) {
5817 PyCell_SET(c, NULL);
5818 Py_DECREF(v);
5819 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005820 break;
5821 }
5822 case STORE_NAME:
5823 {
5824 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03005825 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005826 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005827 if (locals && PyDict_CheckExact(locals)) {
5828 PyObject *w = PyDict_GetItemWithError(locals, name);
5829 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02005830 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02005831 {
5832 Py_DECREF(v);
5833 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02005834 }
5835 }
5836 break;
5837 }
5838 }
5839 }
5840 res = v;
5841 PyUnicode_Append(&res, w);
5842 return res;
5843}
5844
Guido van Rossum950361c1997-01-24 13:49:28 +00005845#ifdef DYNAMIC_EXECUTION_PROFILE
5846
Skip Montanarof118cb12001-10-15 20:51:38 +00005847static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005848getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00005849{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005850 int i;
5851 PyObject *l = PyList_New(256);
5852 if (l == NULL) return NULL;
5853 for (i = 0; i < 256; i++) {
5854 PyObject *x = PyLong_FromLong(a[i]);
5855 if (x == NULL) {
5856 Py_DECREF(l);
5857 return NULL;
5858 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005859 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005860 }
5861 for (i = 0; i < 256; i++)
5862 a[i] = 0;
5863 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005864}
5865
5866PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005867_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00005868{
5869#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005870 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00005871#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005872 int i;
5873 PyObject *l = PyList_New(257);
5874 if (l == NULL) return NULL;
5875 for (i = 0; i < 257; i++) {
5876 PyObject *x = getarray(dxpairs[i]);
5877 if (x == NULL) {
5878 Py_DECREF(l);
5879 return NULL;
5880 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07005881 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005882 }
5883 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00005884#endif
5885}
5886
5887#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07005888
5889Py_ssize_t
5890_PyEval_RequestCodeExtraIndex(freefunc free)
5891{
Victor Stinner81a7be32020-04-14 15:14:01 +02005892 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07005893 Py_ssize_t new_index;
5894
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005895 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07005896 return -1;
5897 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07005898 new_index = interp->co_extra_user_count++;
5899 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07005900 return new_index;
5901}
Łukasz Langaa785c872016-09-09 17:37:37 -07005902
5903static void
5904dtrace_function_entry(PyFrameObject *f)
5905{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005906 const char *filename;
5907 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005908 int lineno;
5909
Victor Stinner6d86a232020-04-29 00:56:58 +02005910 PyCodeObject *code = f->f_code;
5911 filename = PyUnicode_AsUTF8(code->co_filename);
5912 funcname = PyUnicode_AsUTF8(code->co_name);
5913 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005914
Andy Lestere6be9b52020-02-11 20:28:35 -06005915 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005916}
5917
5918static void
5919dtrace_function_return(PyFrameObject *f)
5920{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005921 const char *filename;
5922 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07005923 int lineno;
5924
Victor Stinner6d86a232020-04-29 00:56:58 +02005925 PyCodeObject *code = f->f_code;
5926 filename = PyUnicode_AsUTF8(code->co_filename);
5927 funcname = PyUnicode_AsUTF8(code->co_name);
5928 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07005929
Andy Lestere6be9b52020-02-11 20:28:35 -06005930 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07005931}
5932
5933/* DTrace equivalent of maybe_call_line_trace. */
5934static void
5935maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon877df852020-11-12 09:43:29 +00005936 PyCodeAddressRange *bounds, int *instr_prev)
Łukasz Langaa785c872016-09-09 17:37:37 -07005937{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02005938 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07005939
5940 /* If the last instruction executed isn't in the current
5941 instruction window, reset the window.
5942 */
Mark Shannon877df852020-11-12 09:43:29 +00005943 int line = _PyCode_CheckLineNumber(frame->f_lasti, bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07005944 /* If the last instruction falls at the start of a line or if
5945 it represents a jump backwards, update the frame's line
5946 number and call the trace function. */
Mark Shannon877df852020-11-12 09:43:29 +00005947 if (line != frame->f_lineno || frame->f_lasti < *instr_prev) {
5948 if (line != -1) {
5949 frame->f_lineno = line;
5950 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
5951 if (!co_filename)
5952 co_filename = "?";
5953 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
5954 if (!co_name)
5955 co_name = "?";
5956 PyDTrace_LINE(co_filename, co_name, line);
5957 }
Łukasz Langaa785c872016-09-09 17:37:37 -07005958 }
5959 *instr_prev = frame->f_lasti;
5960}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005961
5962
5963/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
5964 for the limited API. */
5965
5966#undef Py_EnterRecursiveCall
5967
5968int Py_EnterRecursiveCall(const char *where)
5969{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005970 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005971}
5972
5973#undef Py_LeaveRecursiveCall
5974
5975void Py_LeaveRecursiveCall(void)
5976{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01005977 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01005978}