blob: aef79e1a9db314199246aced190cc97297bedf48 [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 */
db3l131d5512021-03-03 22:09:48 -050010/* affects both release and debug builds - see bpo-43271 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000011#define PY_LOCAL_AGGRESSIVE
12
Guido van Rossumb209a111997-04-29 18:18:01 +000013#include "Python.h"
Victor Stinnere560f902020-04-14 18:30:41 +020014#include "pycore_abstract.h" // _PyIndex_Check()
Victor Stinner384621c2020-06-22 17:27:35 +020015#include "pycore_call.h" // _PyObject_FastCallDictTstate()
16#include "pycore_ceval.h" // _PyEval_SignalAsyncExc()
17#include "pycore_code.h" // _PyCode_InitOpcache()
18#include "pycore_initconfig.h" // _PyStatus_OK()
19#include "pycore_object.h" // _PyObject_GC_TRACK()
20#include "pycore_pyerrors.h" // _PyErr_Fetch()
21#include "pycore_pylifecycle.h" // _PyErr_Print()
Victor Stinnere560f902020-04-14 18:30:41 +020022#include "pycore_pymem.h" // _PyMem_IsPtrFreed()
23#include "pycore_pystate.h" // _PyInterpreterState_GET()
Victor Stinner384621c2020-06-22 17:27:35 +020024#include "pycore_sysmodule.h" // _PySys_Audit()
25#include "pycore_tuple.h" // _PyTuple_ITEMS()
Guido van Rossum10dc2e81990-11-18 17:27:39 +000026
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027#include "code.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040028#include "dictobject.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000029#include "frameobject.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#include "opcode.h"
Łukasz Langaa785c872016-09-09 17:37:37 -070031#include "pydtrace.h"
Benjamin Peterson025e9eb2015-05-05 20:16:41 -040032#include "setobject.h"
Guido van Rossum5c5a9382021-01-29 18:02:29 -080033#include "structmember.h" // struct PyMemberDef, T_OFFSET_EX
Guido van Rossum10dc2e81990-11-18 17:27:39 +000034
Guido van Rossumc6004111993-11-05 10:22:19 +000035#include <ctype.h>
36
Mark Shannon8e1b4062021-03-05 14:45:50 +000037typedef struct {
38 PyCodeObject *code; // The code object for the bounds. May be NULL.
39 int instr_prev; // Only valid if code != NULL.
40 PyCodeAddressRange bounds; // Only valid if code != NULL.
41} PyTraceInfo;
42
43
Guido van Rossum408027e1996-12-30 16:17:54 +000044#ifdef Py_DEBUG
Guido van Rossum96a42c81992-01-12 02:29:51 +000045/* For debugging the interpreter: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046#define LLTRACE 1 /* Low-level trace feature */
47#define CHECKEXC 1 /* Double-check exception checking */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000048#endif
49
Victor Stinner5c75f372019-04-17 23:02:26 +020050#if !defined(Py_BUILD_CORE)
51# error "ceval.c must be build with Py_BUILD_CORE define for best performance"
52#endif
53
Hai Shi46874c22020-01-30 17:20:25 -060054_Py_IDENTIFIER(__name__);
Guido van Rossum5b722181993-03-30 17:46:03 +000055
Guido van Rossum374a9221991-04-04 10:40:29 +000056/* Forward declarations */
Victor Stinner09532fe2019-05-10 23:39:09 +020057Py_LOCAL_INLINE(PyObject *) call_function(
Mark Shannon8e1b4062021-03-05 14:45:50 +000058 PyThreadState *tstate, PyTraceInfo *, PyObject ***pp_stack,
Victor Stinner09532fe2019-05-10 23:39:09 +020059 Py_ssize_t oparg, PyObject *kwnames);
60static PyObject * do_call_core(
Mark Shannon8e1b4062021-03-05 14:45:50 +000061 PyThreadState *tstate, PyTraceInfo *, PyObject *func,
Victor Stinner09532fe2019-05-10 23:39:09 +020062 PyObject *callargs, PyObject *kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +000063
Guido van Rossum0a066c01992-03-27 17:29:15 +000064#ifdef LLTRACE
Guido van Rossumc2e20742006-02-27 22:32:47 +000065static int lltrace;
Victor Stinner438a12d2019-05-24 17:01:38 +020066static int prtrace(PyThreadState *, PyObject *, const char *);
Guido van Rossum0a066c01992-03-27 17:29:15 +000067#endif
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010068static int call_trace(Py_tracefunc, PyObject *,
69 PyThreadState *, PyFrameObject *,
Mark Shannon8e1b4062021-03-05 14:45:50 +000070 PyTraceInfo *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 int, PyObject *);
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +000072static int call_trace_protected(Py_tracefunc, PyObject *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010073 PyThreadState *, PyFrameObject *,
Mark Shannon8e1b4062021-03-05 14:45:50 +000074 PyTraceInfo *,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +010075 int, PyObject *);
76static void call_exc_trace(Py_tracefunc, PyObject *,
Mark Shannon86433452021-01-07 16:49:02 +000077 PyThreadState *, PyFrameObject *,
Mark Shannon8e1b4062021-03-05 14:45:50 +000078 PyTraceInfo *trace_info);
Tim Peters8a5c3c72004-04-05 19:36:21 +000079static int maybe_call_line_trace(Py_tracefunc, PyObject *,
Eric Snow2ebc5ce2017-09-07 23:51:28 -060080 PyThreadState *, PyFrameObject *,
Mark Shannon8e1b4062021-03-05 14:45:50 +000081 PyTraceInfo *);
82static void maybe_dtrace_line(PyFrameObject *, PyTraceInfo *);
Łukasz Langaa785c872016-09-09 17:37:37 -070083static void dtrace_function_entry(PyFrameObject *);
84static void dtrace_function_return(PyFrameObject *);
Michael W. Hudsondd32a912002-08-15 14:59:02 +000085
Victor Stinner438a12d2019-05-24 17:01:38 +020086static PyObject * import_name(PyThreadState *, PyFrameObject *,
87 PyObject *, PyObject *, PyObject *);
88static PyObject * import_from(PyThreadState *, PyObject *, PyObject *);
89static int import_all_from(PyThreadState *, PyObject *, PyObject *);
90static void format_exc_check_arg(PyThreadState *, PyObject *, const char *, PyObject *);
91static void format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg);
92static PyObject * unicode_concatenate(PyThreadState *, PyObject *, PyObject *,
Serhiy Storchakaab874002016-09-11 13:48:15 +030093 PyFrameObject *, const _Py_CODEUNIT *);
Victor Stinner438a12d2019-05-24 17:01:38 +020094static PyObject * special_lookup(PyThreadState *, PyObject *, _Py_Identifier *);
95static int check_args_iterable(PyThreadState *, PyObject *func, PyObject *vararg);
96static void format_kwargs_error(PyThreadState *, PyObject *func, PyObject *kwargs);
Mark Shannonfee55262019-11-21 09:11:43 +000097static void format_awaitable_error(PyThreadState *, PyTypeObject *, int, int);
Guido van Rossum374a9221991-04-04 10:40:29 +000098
Paul Prescode68140d2000-08-30 20:25:01 +000099#define NAME_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 "name '%.200s' is not defined"
Paul Prescode68140d2000-08-30 20:25:01 +0000101#define UNBOUNDLOCAL_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000102 "local variable '%.200s' referenced before assignment"
Jeremy Hyltonc76770c2001-04-13 16:51:46 +0000103#define UNBOUNDFREE_ERROR_MSG \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 "free variable '%.200s' referenced before assignment" \
105 " in enclosing scope"
Guido van Rossum374a9221991-04-04 10:40:29 +0000106
Guido van Rossum950361c1997-01-24 13:49:28 +0000107/* Dynamic execution profile */
108#ifdef DYNAMIC_EXECUTION_PROFILE
109#ifdef DXPAIRS
110static long dxpairs[257][256];
111#define dxp dxpairs[256]
112#else
113static long dxp[256];
114#endif
115#endif
116
Inada Naoki91234a12019-06-03 21:30:58 +0900117/* per opcode cache */
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000118static int opcache_min_runs = 1024; /* create opcache when code executed this many times */
Pablo Galindo109826c2020-10-20 06:22:44 +0100119#define OPCODE_CACHE_MAX_TRIES 20
Inada Naoki91234a12019-06-03 21:30:58 +0900120#define OPCACHE_STATS 0 /* Enable stats */
121
Pablo Galindoaf5fa132021-02-28 22:41:09 +0000122// This function allows to deactivate the opcode cache. As different cache mechanisms may hold
123// references, this can mess with the reference leak detector functionality so the cache needs
124// to be deactivated in such scenarios to avoid false positives. See bpo-3714 for more information.
125void
126_PyEval_DeactivateOpCache(void)
127{
128 opcache_min_runs = 0;
129}
130
Inada Naoki91234a12019-06-03 21:30:58 +0900131#if OPCACHE_STATS
132static size_t opcache_code_objects = 0;
133static size_t opcache_code_objects_extra_mem = 0;
134
135static size_t opcache_global_opts = 0;
136static size_t opcache_global_hits = 0;
137static size_t opcache_global_misses = 0;
Pablo Galindo109826c2020-10-20 06:22:44 +0100138
139static size_t opcache_attr_opts = 0;
140static size_t opcache_attr_hits = 0;
141static size_t opcache_attr_misses = 0;
142static size_t opcache_attr_deopts = 0;
143static size_t opcache_attr_total = 0;
Inada Naoki91234a12019-06-03 21:30:58 +0900144#endif
145
Victor Stinner5a3a71d2020-03-19 17:40:12 +0100146
Victor Stinnerda2914d2020-03-20 09:29:08 +0100147#ifndef NDEBUG
148/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
149 PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
150 when a thread continues to run after Python finalization, especially
151 daemon threads. */
152static int
153is_tstate_valid(PyThreadState *tstate)
154{
155 assert(!_PyMem_IsPtrFreed(tstate));
156 assert(!_PyMem_IsPtrFreed(tstate->interp));
157 return 1;
158}
159#endif
160
161
Jeffrey Yasskin39370832010-05-03 19:29:34 +0000162/* This can set eval_breaker to 0 even though gil_drop_request became
163 1. We believe this is all right because the eval loop will release
164 the GIL eventually anyway. */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100165static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200166COMPUTE_EVAL_BREAKER(PyInterpreterState *interp,
Victor Stinner299b8c62020-05-05 17:40:18 +0200167 struct _ceval_runtime_state *ceval,
168 struct _ceval_state *ceval2)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100169{
Victor Stinner299b8c62020-05-05 17:40:18 +0200170 _Py_atomic_store_relaxed(&ceval2->eval_breaker,
171 _Py_atomic_load_relaxed(&ceval2->gil_drop_request)
Victor Stinner0b1e3302020-05-05 16:14:31 +0200172 | (_Py_atomic_load_relaxed(&ceval->signals_pending)
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200173 && _Py_ThreadCanHandleSignals(interp))
Victor Stinner299b8c62020-05-05 17:40:18 +0200174 | (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)
Victor Stinnerd8316882020-03-20 14:50:35 +0100175 && _Py_ThreadCanHandlePendingCalls())
Victor Stinner299b8c62020-05-05 17:40:18 +0200176 | ceval2->pending.async_exc);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100177}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000178
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000179
Victor Stinnerda2914d2020-03-20 09:29:08 +0100180static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200181SET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100182{
Victor Stinner299b8c62020-05-05 17:40:18 +0200183 struct _ceval_state *ceval2 = &interp->ceval;
184 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 1);
185 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100186}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000187
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000188
Victor Stinnerda2914d2020-03-20 09:29:08 +0100189static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200190RESET_GIL_DROP_REQUEST(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100191{
Victor Stinner299b8c62020-05-05 17:40:18 +0200192 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
193 struct _ceval_state *ceval2 = &interp->ceval;
194 _Py_atomic_store_relaxed(&ceval2->gil_drop_request, 0);
195 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100196}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000197
Eric Snowfdf282d2019-01-11 14:26:55 -0700198
Victor Stinnerda2914d2020-03-20 09:29:08 +0100199static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200200SIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100201{
Victor Stinner299b8c62020-05-05 17:40:18 +0200202 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
203 struct _ceval_state *ceval2 = &interp->ceval;
204 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 1);
205 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100206}
Eric Snowfdf282d2019-01-11 14:26:55 -0700207
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000208
Victor Stinnerda2914d2020-03-20 09:29:08 +0100209static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200210UNSIGNAL_PENDING_CALLS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100211{
Victor Stinner299b8c62020-05-05 17:40:18 +0200212 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
213 struct _ceval_state *ceval2 = &interp->ceval;
214 _Py_atomic_store_relaxed(&ceval2->pending.calls_to_do, 0);
215 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100216}
217
218
219static inline void
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100220SIGNAL_PENDING_SIGNALS(PyInterpreterState *interp, int force)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100221{
Victor Stinner299b8c62020-05-05 17:40:18 +0200222 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
223 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200224 _Py_atomic_store_relaxed(&ceval->signals_pending, 1);
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100225 if (force) {
226 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
227 }
228 else {
229 /* eval_breaker is not set to 1 if thread_can_handle_signals() is false */
230 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
231 }
Victor Stinnerda2914d2020-03-20 09:29:08 +0100232}
233
234
235static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200236UNSIGNAL_PENDING_SIGNALS(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100237{
Victor Stinner299b8c62020-05-05 17:40:18 +0200238 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
239 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200240 _Py_atomic_store_relaxed(&ceval->signals_pending, 0);
Victor Stinner299b8c62020-05-05 17:40:18 +0200241 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100242}
243
244
245static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200246SIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100247{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200248 struct _ceval_state *ceval2 = &interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100249 ceval2->pending.async_exc = 1;
250 _Py_atomic_store_relaxed(&ceval2->eval_breaker, 1);
251}
252
253
254static inline void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200255UNSIGNAL_ASYNC_EXC(PyInterpreterState *interp)
Victor Stinnerda2914d2020-03-20 09:29:08 +0100256{
Victor Stinner299b8c62020-05-05 17:40:18 +0200257 struct _ceval_runtime_state *ceval = &interp->runtime->ceval;
258 struct _ceval_state *ceval2 = &interp->ceval;
259 ceval2->pending.async_exc = 0;
260 COMPUTE_EVAL_BREAKER(interp, ceval, ceval2);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100261}
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000262
263
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000264#ifdef HAVE_ERRNO_H
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000265#include <errno.h>
Guido van Rossum2571cc81999-04-07 16:07:23 +0000266#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000267#include "ceval_gil.h"
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000268
Victor Stinner3026cad2020-06-01 16:02:40 +0200269void _Py_NO_RETURN
270_Py_FatalError_TstateNULL(const char *func)
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100271{
Victor Stinner3026cad2020-06-01 16:02:40 +0200272 _Py_FatalErrorFunc(func,
273 "the function must be called with the GIL held, "
274 "but the GIL is released "
275 "(the current Python thread state is NULL)");
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100276}
277
Victor Stinner7be4e352020-05-05 20:27:47 +0200278#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
279int
280_PyEval_ThreadsInitialized(PyInterpreterState *interp)
281{
282 return gil_created(&interp->ceval.gil);
283}
284
285int
286PyEval_ThreadsInitialized(void)
287{
288 // Fatal error if there is no current interpreter
289 PyInterpreterState *interp = PyInterpreterState_Get();
290 return _PyEval_ThreadsInitialized(interp);
291}
292#else
Tim Peters7f468f22004-10-11 02:40:51 +0000293int
Victor Stinner175a7042020-03-10 00:37:48 +0100294_PyEval_ThreadsInitialized(_PyRuntimeState *runtime)
295{
296 return gil_created(&runtime->ceval.gil);
297}
298
299int
Tim Peters7f468f22004-10-11 02:40:51 +0000300PyEval_ThreadsInitialized(void)
301{
Victor Stinner01b1cc12019-11-20 02:27:56 +0100302 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner175a7042020-03-10 00:37:48 +0100303 return _PyEval_ThreadsInitialized(runtime);
Tim Peters7f468f22004-10-11 02:40:51 +0000304}
Victor Stinner7be4e352020-05-05 20:27:47 +0200305#endif
Tim Peters7f468f22004-10-11 02:40:51 +0000306
Victor Stinner111e4ee2020-03-09 21:24:14 +0100307PyStatus
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200308_PyEval_InitGIL(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000309{
Victor Stinner7be4e352020-05-05 20:27:47 +0200310#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinner101bf692021-02-19 13:33:31 +0100311 if (!_Py_IsMainInterpreter(tstate->interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200312 /* Currently, the GIL is shared by all interpreters,
313 and only the main interpreter is responsible to create
314 and destroy it. */
315 return _PyStatus_OK();
Victor Stinner111e4ee2020-03-09 21:24:14 +0100316 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200317#endif
Victor Stinner111e4ee2020-03-09 21:24:14 +0100318
Victor Stinner7be4e352020-05-05 20:27:47 +0200319#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
320 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
321#else
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200322 struct _gil_runtime_state *gil = &tstate->interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200323#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200324 assert(!gil_created(gil));
Victor Stinner85f5a692020-03-09 22:12:04 +0100325
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200326 PyThread_init_thread();
327 create_gil(gil);
328
329 take_gil(tstate);
330
331 assert(gil_created(gil));
Victor Stinner111e4ee2020-03-09 21:24:14 +0100332 return _PyStatus_OK();
333}
334
335void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100336_PyEval_FiniGIL(PyInterpreterState *interp)
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200337{
Victor Stinner7be4e352020-05-05 20:27:47 +0200338#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100339 if (!_Py_IsMainInterpreter(interp)) {
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200340 /* Currently, the GIL is shared by all interpreters,
341 and only the main interpreter is responsible to create
342 and destroy it. */
343 return;
344 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200345#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200346
Victor Stinner7be4e352020-05-05 20:27:47 +0200347#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100348 struct _gil_runtime_state *gil = &interp->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200349#else
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100350 struct _gil_runtime_state *gil = &interp->runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200351#endif
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200352 if (!gil_created(gil)) {
353 /* First Py_InitializeFromConfig() call: the GIL doesn't exist
354 yet: do nothing. */
355 return;
356 }
357
358 destroy_gil(gil);
359 assert(!gil_created(gil));
360}
361
362void
Victor Stinner111e4ee2020-03-09 21:24:14 +0100363PyEval_InitThreads(void)
364{
Victor Stinnerb4698ec2020-03-10 01:28:54 +0100365 /* Do nothing: kept for backward compatibility */
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000366}
Guido van Rossumff4949e1992-08-05 19:58:53 +0000367
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000368void
Inada Naoki91234a12019-06-03 21:30:58 +0900369_PyEval_Fini(void)
370{
371#if OPCACHE_STATS
372 fprintf(stderr, "-- Opcode cache number of objects = %zd\n",
373 opcache_code_objects);
374
375 fprintf(stderr, "-- Opcode cache total extra mem = %zd\n",
376 opcache_code_objects_extra_mem);
377
378 fprintf(stderr, "\n");
379
380 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL hits = %zd (%d%%)\n",
381 opcache_global_hits,
382 (int) (100.0 * opcache_global_hits /
383 (opcache_global_hits + opcache_global_misses)));
384
385 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL misses = %zd (%d%%)\n",
386 opcache_global_misses,
387 (int) (100.0 * opcache_global_misses /
388 (opcache_global_hits + opcache_global_misses)));
389
390 fprintf(stderr, "-- Opcode cache LOAD_GLOBAL opts = %zd\n",
391 opcache_global_opts);
392
393 fprintf(stderr, "\n");
Pablo Galindo109826c2020-10-20 06:22:44 +0100394
395 fprintf(stderr, "-- Opcode cache LOAD_ATTR hits = %zd (%d%%)\n",
396 opcache_attr_hits,
397 (int) (100.0 * opcache_attr_hits /
398 opcache_attr_total));
399
400 fprintf(stderr, "-- Opcode cache LOAD_ATTR misses = %zd (%d%%)\n",
401 opcache_attr_misses,
402 (int) (100.0 * opcache_attr_misses /
403 opcache_attr_total));
404
405 fprintf(stderr, "-- Opcode cache LOAD_ATTR opts = %zd\n",
406 opcache_attr_opts);
407
408 fprintf(stderr, "-- Opcode cache LOAD_ATTR deopts = %zd\n",
409 opcache_attr_deopts);
410
411 fprintf(stderr, "-- Opcode cache LOAD_ATTR total = %zd\n",
412 opcache_attr_total);
Inada Naoki91234a12019-06-03 21:30:58 +0900413#endif
414}
415
416void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000417PyEval_AcquireLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000418{
Victor Stinner09532fe2019-05-10 23:39:09 +0200419 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200420 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Victor Stinner3026cad2020-06-01 16:02:40 +0200421 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100422
Victor Stinner85f5a692020-03-09 22:12:04 +0100423 take_gil(tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000424}
425
426void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000427PyEval_ReleaseLock(void)
Guido van Rossum25ce5661997-08-02 03:10:38 +0000428{
Victor Stinner09532fe2019-05-10 23:39:09 +0200429 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere225beb2019-06-03 18:14:24 +0200430 PyThreadState *tstate = _PyRuntimeState_GetThreadState(runtime);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 /* This function must succeed when the current thread state is NULL.
Victor Stinner50b48572018-11-01 01:51:40 +0100432 We therefore avoid PyThreadState_Get() which dumps a fatal error
Victor Stinnerda2914d2020-03-20 09:29:08 +0100433 in debug mode. */
Victor Stinner299b8c62020-05-05 17:40:18 +0200434 struct _ceval_runtime_state *ceval = &runtime->ceval;
435 struct _ceval_state *ceval2 = &tstate->interp->ceval;
436 drop_gil(ceval, ceval2, tstate);
Guido van Rossum25ce5661997-08-02 03:10:38 +0000437}
438
439void
Victor Stinner23ef89d2020-03-18 02:26:04 +0100440_PyEval_ReleaseLock(PyThreadState *tstate)
441{
442 struct _ceval_runtime_state *ceval = &tstate->interp->runtime->ceval;
Victor Stinner0b1e3302020-05-05 16:14:31 +0200443 struct _ceval_state *ceval2 = &tstate->interp->ceval;
444 drop_gil(ceval, ceval2, tstate);
Victor Stinner23ef89d2020-03-18 02:26:04 +0100445}
446
447void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000448PyEval_AcquireThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000449{
Victor Stinner3026cad2020-06-01 16:02:40 +0200450 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100451
Victor Stinner85f5a692020-03-09 22:12:04 +0100452 take_gil(tstate);
Victor Stinnere225beb2019-06-03 18:14:24 +0200453
Victor Stinner85f5a692020-03-09 22:12:04 +0100454 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
Victor Stinnere838a932020-05-05 19:56:48 +0200455#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
456 (void)_PyThreadState_Swap(gilstate, tstate);
457#else
Victor Stinner85f5a692020-03-09 22:12:04 +0100458 if (_PyThreadState_Swap(gilstate, tstate) != NULL) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100459 Py_FatalError("non-NULL old thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200460 }
Victor Stinnere838a932020-05-05 19:56:48 +0200461#endif
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000462}
463
464void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000465PyEval_ReleaseThread(PyThreadState *tstate)
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000466{
Victor Stinnerda2914d2020-03-20 09:29:08 +0100467 assert(is_tstate_valid(tstate));
Victor Stinner09532fe2019-05-10 23:39:09 +0200468
Victor Stinner01b1cc12019-11-20 02:27:56 +0100469 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner09532fe2019-05-10 23:39:09 +0200470 PyThreadState *new_tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
471 if (new_tstate != tstate) {
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100472 Py_FatalError("wrong thread state");
Victor Stinner09532fe2019-05-10 23:39:09 +0200473 }
Victor Stinner0b1e3302020-05-05 16:14:31 +0200474 struct _ceval_runtime_state *ceval = &runtime->ceval;
475 struct _ceval_state *ceval2 = &tstate->interp->ceval;
476 drop_gil(ceval, ceval2, tstate);
Guido van Rossum9cc8a201997-07-19 19:55:50 +0000477}
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000478
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900479#ifdef HAVE_FORK
Antoine Pitrouf7ecfac2017-05-28 11:35:14 +0200480/* This function is called from PyOS_AfterFork_Child to destroy all threads
Victor Stinner26881c82020-06-02 15:51:37 +0200481 which are not running in the child process, and clear internal locks
482 which might be held by those threads. */
483PyStatus
Victor Stinner317bab02020-06-02 18:44:54 +0200484_PyEval_ReInitThreads(PyThreadState *tstate)
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000485{
Victor Stinner317bab02020-06-02 18:44:54 +0200486 _PyRuntimeState *runtime = tstate->interp->runtime;
Victor Stinner7be4e352020-05-05 20:27:47 +0200487
488#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
489 struct _gil_runtime_state *gil = &tstate->interp->ceval.gil;
490#else
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100491 struct _gil_runtime_state *gil = &runtime->ceval.gil;
Victor Stinner7be4e352020-05-05 20:27:47 +0200492#endif
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100493 if (!gil_created(gil)) {
Victor Stinner26881c82020-06-02 15:51:37 +0200494 return _PyStatus_OK();
Victor Stinner09532fe2019-05-10 23:39:09 +0200495 }
Victor Stinner56bfdeb2020-03-18 09:26:25 +0100496 recreate_gil(gil);
Victor Stinner85f5a692020-03-09 22:12:04 +0100497
498 take_gil(tstate);
Eric Snow8479a342019-03-08 23:44:33 -0700499
Victor Stinner50e6e992020-03-19 02:41:21 +0100500 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900501 if (_PyThread_at_fork_reinit(&pending->lock) < 0) {
Victor Stinner26881c82020-06-02 15:51:37 +0200502 return _PyStatus_ERR("Can't reinitialize pending calls lock");
Eric Snow8479a342019-03-08 23:44:33 -0700503 }
Jesse Nollera8513972008-07-17 16:49:17 +0000504
Antoine Pitrou8408cea2013-05-05 23:47:09 +0200505 /* Destroy all threads except the current one */
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100506 _PyThreadState_DeleteExcept(runtime, tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200507 return _PyStatus_OK();
Guido van Rossumfee3a2d2000-08-27 17:34:07 +0000508}
Dong-hee Na62f75fe2020-04-15 01:16:24 +0900509#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000510
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000511/* This function is used to signal that async exceptions are waiting to be
Zackery Spytzeef05962018-09-29 10:07:11 -0600512 raised. */
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000513
514void
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100515_PyEval_SignalAsyncExc(PyInterpreterState *interp)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000516{
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100517 SIGNAL_ASYNC_EXC(interp);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000518}
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000519
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000520PyThreadState *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000521PyEval_SaveThread(void)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000522{
Victor Stinner09532fe2019-05-10 23:39:09 +0200523 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinnere838a932020-05-05 19:56:48 +0200524#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
525 PyThreadState *old_tstate = _PyThreadState_GET();
526 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, old_tstate);
527#else
Victor Stinner09532fe2019-05-10 23:39:09 +0200528 PyThreadState *tstate = _PyThreadState_Swap(&runtime->gilstate, NULL);
Victor Stinnere838a932020-05-05 19:56:48 +0200529#endif
Victor Stinner3026cad2020-06-01 16:02:40 +0200530 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +0100531
Victor Stinner0b1e3302020-05-05 16:14:31 +0200532 struct _ceval_runtime_state *ceval = &runtime->ceval;
533 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner7be4e352020-05-05 20:27:47 +0200534#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
535 assert(gil_created(&ceval2->gil));
536#else
Victor Stinnere225beb2019-06-03 18:14:24 +0200537 assert(gil_created(&ceval->gil));
Victor Stinner7be4e352020-05-05 20:27:47 +0200538#endif
Victor Stinner0b1e3302020-05-05 16:14:31 +0200539 drop_gil(ceval, ceval2, tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000540 return tstate;
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000541}
542
543void
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000544PyEval_RestoreThread(PyThreadState *tstate)
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000545{
Victor Stinner3026cad2020-06-01 16:02:40 +0200546 _Py_EnsureTstateNotNULL(tstate);
Victor Stinnereb4e2ae2020-03-08 11:57:45 +0100547
Victor Stinner85f5a692020-03-09 22:12:04 +0100548 take_gil(tstate);
Victor Stinner17c68b82020-01-30 12:20:48 +0100549
Victor Stinner85f5a692020-03-09 22:12:04 +0100550 struct _gilstate_runtime_state *gilstate = &tstate->interp->runtime->gilstate;
551 _PyThreadState_Swap(gilstate, tstate);
Guido van Rossum1984f1e1992-08-04 12:41:02 +0000552}
553
554
Guido van Rossuma9672091994-09-14 13:31:22 +0000555/* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
556 signal handlers or Mac I/O completion routines) can schedule calls
557 to a function to be called synchronously.
558 The synchronous function is called with one void* argument.
559 It should return 0 for success or -1 for failure -- failure should
560 be accompanied by an exception.
561
562 If registry succeeds, the registry function returns 0; if it fails
563 (e.g. due to too many pending calls) it returns -1 (without setting
564 an exception condition).
565
566 Note that because registry may occur from within signal handlers,
567 or other asynchronous events, calling malloc() is unsafe!
568
Guido van Rossuma9672091994-09-14 13:31:22 +0000569 Any thread can schedule pending calls, but only the main thread
570 will execute them.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000571 There is no facility to schedule calls to a particular thread, but
572 that should be easy to change, should that ever be required. In
573 that case, the static variables here should go into the python
574 threadstate.
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000575*/
Guido van Rossuma9672091994-09-14 13:31:22 +0000576
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200577void
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200578_PyEval_SignalReceived(PyInterpreterState *interp)
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200579{
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100580#ifdef MS_WINDOWS
581 // bpo-42296: On Windows, _PyEval_SignalReceived() is called from a signal
582 // handler which can run in a thread different than the Python thread, in
583 // which case _Py_ThreadCanHandleSignals() is wrong. Ignore
584 // _Py_ThreadCanHandleSignals() and always set eval_breaker to 1.
585 //
586 // The next eval_frame_handle_pending() call will call
587 // _Py_ThreadCanHandleSignals() to recompute eval_breaker.
588 int force = 1;
589#else
590 int force = 0;
591#endif
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200592 /* bpo-30703: Function called when the C signal handler of Python gets a
Victor Stinner50e6e992020-03-19 02:41:21 +0100593 signal. We cannot queue a callback using _PyEval_AddPendingCall() since
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200594 that function is not async-signal-safe. */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100595 SIGNAL_PENDING_SIGNALS(interp, force);
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200596}
597
Eric Snow5be45a62019-03-08 22:47:07 -0700598/* Push one item onto the queue while holding the lock. */
599static int
Victor Stinnere225beb2019-06-03 18:14:24 +0200600_push_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600601 int (*func)(void *), void *arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700602{
Eric Snow842a2f02019-03-15 15:47:51 -0600603 int i = pending->last;
Eric Snow5be45a62019-03-08 22:47:07 -0700604 int j = (i + 1) % NPENDINGCALLS;
Eric Snow842a2f02019-03-15 15:47:51 -0600605 if (j == pending->first) {
Eric Snow5be45a62019-03-08 22:47:07 -0700606 return -1; /* Queue full */
607 }
Eric Snow842a2f02019-03-15 15:47:51 -0600608 pending->calls[i].func = func;
609 pending->calls[i].arg = arg;
610 pending->last = j;
Eric Snow5be45a62019-03-08 22:47:07 -0700611 return 0;
612}
613
614/* Pop one item off the queue while holding the lock. */
615static void
Victor Stinnere225beb2019-06-03 18:14:24 +0200616_pop_pending_call(struct _pending_calls *pending,
Eric Snow842a2f02019-03-15 15:47:51 -0600617 int (**func)(void *), void **arg)
Eric Snow5be45a62019-03-08 22:47:07 -0700618{
Eric Snow842a2f02019-03-15 15:47:51 -0600619 int i = pending->first;
620 if (i == pending->last) {
Eric Snow5be45a62019-03-08 22:47:07 -0700621 return; /* Queue empty */
622 }
623
Eric Snow842a2f02019-03-15 15:47:51 -0600624 *func = pending->calls[i].func;
625 *arg = pending->calls[i].arg;
626 pending->first = (i + 1) % NPENDINGCALLS;
Eric Snow5be45a62019-03-08 22:47:07 -0700627}
628
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200629/* This implementation is thread-safe. It allows
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000630 scheduling to be made from any thread, and even from an executing
631 callback.
632 */
633
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000634int
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200635_PyEval_AddPendingCall(PyInterpreterState *interp,
Victor Stinner09532fe2019-05-10 23:39:09 +0200636 int (*func)(void *), void *arg)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000637{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200638 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snow842a2f02019-03-15 15:47:51 -0600639
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200640 /* Ensure that _PyEval_InitPendingCalls() was called
641 and that _PyEval_FiniPendingCalls() is not called yet. */
642 assert(pending->lock != NULL);
643
Eric Snow842a2f02019-03-15 15:47:51 -0600644 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200645 int result = _push_pending_call(pending, func, arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600646 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700647
Victor Stinnere225beb2019-06-03 18:14:24 +0200648 /* signal main loop */
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200649 SIGNAL_PENDING_CALLS(interp);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return result;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000651}
652
Victor Stinner09532fe2019-05-10 23:39:09 +0200653int
654Py_AddPendingCall(int (*func)(void *), void *arg)
655{
Victor Stinner50e6e992020-03-19 02:41:21 +0100656 /* Best-effort to support subinterpreters and calls with the GIL released.
657
658 First attempt _PyThreadState_GET() since it supports subinterpreters.
659
660 If the GIL is released, _PyThreadState_GET() returns NULL . In this
661 case, use PyGILState_GetThisThreadState() which works even if the GIL
662 is released.
663
664 Sadly, PyGILState_GetThisThreadState() doesn't support subinterpreters:
665 see bpo-10915 and bpo-15751.
666
Victor Stinner8849e592020-03-18 19:28:53 +0100667 Py_AddPendingCall() doesn't require the caller to hold the GIL. */
Victor Stinner50e6e992020-03-19 02:41:21 +0100668 PyThreadState *tstate = _PyThreadState_GET();
669 if (tstate == NULL) {
670 tstate = PyGILState_GetThisThreadState();
671 }
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200672
673 PyInterpreterState *interp;
674 if (tstate != NULL) {
675 interp = tstate->interp;
676 }
677 else {
678 /* Last resort: use the main interpreter */
679 interp = _PyRuntime.interpreters.main;
680 }
681 return _PyEval_AddPendingCall(interp, func, arg);
Victor Stinner09532fe2019-05-10 23:39:09 +0200682}
683
Eric Snowfdf282d2019-01-11 14:26:55 -0700684static int
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100685handle_signals(PyThreadState *tstate)
Eric Snowfdf282d2019-01-11 14:26:55 -0700686{
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200687 assert(is_tstate_valid(tstate));
688 if (!_Py_ThreadCanHandleSignals(tstate->interp)) {
Eric Snow64d6cc82019-02-23 15:40:43 -0700689 return 0;
690 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700691
Victor Stinnerb54a99d2020-04-08 23:35:05 +0200692 UNSIGNAL_PENDING_SIGNALS(tstate->interp);
Victor Stinner72818982020-03-26 22:28:11 +0100693 if (_PyErr_CheckSignalsTstate(tstate) < 0) {
694 /* On failure, re-schedule a call to handle_signals(). */
Victor Stinnerd96a7a82020-11-13 14:44:42 +0100695 SIGNAL_PENDING_SIGNALS(tstate->interp, 0);
Eric Snowfdf282d2019-01-11 14:26:55 -0700696 return -1;
697 }
698 return 0;
699}
700
701static int
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100702make_pending_calls(PyInterpreterState *interp)
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000703{
Victor Stinnerd8316882020-03-20 14:50:35 +0100704 /* only execute pending calls on main thread */
705 if (!_Py_ThreadCanHandlePendingCalls()) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200706 return 0;
707 }
708
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000709 /* don't perform recursive pending calls */
Victor Stinnerda2914d2020-03-20 09:29:08 +0100710 static int busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700711 if (busy) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 return 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700713 }
Charles-François Natalif23339a2011-07-23 18:15:43 +0200714 busy = 1;
Victor Stinnerda2914d2020-03-20 09:29:08 +0100715
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200716 /* unsignal before starting to call callbacks, so that any callback
717 added in-between re-signals */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100718 UNSIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700719 int res = 0;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 /* perform a bounded number of calls, in case of recursion */
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100722 struct _pending_calls *pending = &interp->ceval.pending;
Eric Snowfdf282d2019-01-11 14:26:55 -0700723 for (int i=0; i<NPENDINGCALLS; i++) {
Eric Snow5be45a62019-03-08 22:47:07 -0700724 int (*func)(void *) = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 void *arg = NULL;
726
727 /* pop one item off the queue while holding the lock */
Eric Snow842a2f02019-03-15 15:47:51 -0600728 PyThread_acquire_lock(pending->lock, WAIT_LOCK);
Victor Stinnere225beb2019-06-03 18:14:24 +0200729 _pop_pending_call(pending, &func, &arg);
Eric Snow842a2f02019-03-15 15:47:51 -0600730 PyThread_release_lock(pending->lock);
Eric Snow5be45a62019-03-08 22:47:07 -0700731
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100732 /* having released the lock, perform the callback */
Eric Snow5be45a62019-03-08 22:47:07 -0700733 if (func == NULL) {
Victor Stinner4d61e6e2019-03-04 14:21:28 +0100734 break;
Eric Snow5be45a62019-03-08 22:47:07 -0700735 }
Eric Snowfdf282d2019-01-11 14:26:55 -0700736 res = func(arg);
737 if (res) {
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200738 goto error;
739 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 }
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200741
Charles-François Natalif23339a2011-07-23 18:15:43 +0200742 busy = 0;
Eric Snowfdf282d2019-01-11 14:26:55 -0700743 return res;
Antoine Pitrouc08177a2017-06-28 23:29:29 +0200744
745error:
746 busy = 0;
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100747 SIGNAL_PENDING_CALLS(interp);
Eric Snowfdf282d2019-01-11 14:26:55 -0700748 return res;
749}
750
Eric Snow842a2f02019-03-15 15:47:51 -0600751void
Victor Stinner2b1df452020-01-13 18:46:59 +0100752_Py_FinishPendingCalls(PyThreadState *tstate)
Eric Snow842a2f02019-03-15 15:47:51 -0600753{
Eric Snow842a2f02019-03-15 15:47:51 -0600754 assert(PyGILState_Check());
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100755 assert(is_tstate_valid(tstate));
Eric Snow842a2f02019-03-15 15:47:51 -0600756
Victor Stinner50e6e992020-03-19 02:41:21 +0100757 struct _pending_calls *pending = &tstate->interp->ceval.pending;
Victor Stinner09532fe2019-05-10 23:39:09 +0200758
Eric Snow842a2f02019-03-15 15:47:51 -0600759 if (!_Py_atomic_load_relaxed(&(pending->calls_to_do))) {
760 return;
761 }
762
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100763 if (make_pending_calls(tstate->interp) < 0) {
Victor Stinnere225beb2019-06-03 18:14:24 +0200764 PyObject *exc, *val, *tb;
765 _PyErr_Fetch(tstate, &exc, &val, &tb);
766 PyErr_BadInternalCall();
767 _PyErr_ChainExceptions(exc, val, tb);
768 _PyErr_Print(tstate);
Eric Snow842a2f02019-03-15 15:47:51 -0600769 }
770}
771
Eric Snowfdf282d2019-01-11 14:26:55 -0700772/* Py_MakePendingCalls() is a simple wrapper for the sake
773 of backward-compatibility. */
774int
775Py_MakePendingCalls(void)
776{
777 assert(PyGILState_Check());
778
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100779 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100780 assert(is_tstate_valid(tstate));
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100781
Eric Snowfdf282d2019-01-11 14:26:55 -0700782 /* Python signal handler doesn't really queue a callback: it only signals
783 that a signal was received, see _PyEval_SignalReceived(). */
Victor Stinnerd7fabc12020-03-18 01:56:21 +0100784 int res = handle_signals(tstate);
Eric Snowfdf282d2019-01-11 14:26:55 -0700785 if (res != 0) {
786 return res;
787 }
788
Victor Stinnerbcb094b2021-02-19 15:10:45 +0100789 res = make_pending_calls(tstate->interp);
Eric Snowb75b1a352019-04-12 10:20:10 -0600790 if (res != 0) {
791 return res;
792 }
793
794 return 0;
Benjamin Petersone5bf3832009-01-17 23:43:58 +0000795}
796
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000797/* The interpreter's recursion limit */
798
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000799#ifndef Py_DEFAULT_RECURSION_LIMIT
Victor Stinner19c3ac92020-09-23 14:04:57 +0200800# define Py_DEFAULT_RECURSION_LIMIT 1000
Hye-Shik Changb6fa2812005-04-04 15:49:02 +0000801#endif
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600802
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600803void
Victor Stinnerdab84232020-03-17 18:56:44 +0100804_PyEval_InitRuntimeState(struct _ceval_runtime_state *ceval)
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600805{
Victor Stinner7be4e352020-05-05 20:27:47 +0200806#ifndef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
Victor Stinnerdab84232020-03-17 18:56:44 +0100807 _gil_initialize(&ceval->gil);
Victor Stinner7be4e352020-05-05 20:27:47 +0200808#endif
Victor Stinnerdab84232020-03-17 18:56:44 +0100809}
810
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200811int
Victor Stinnerdab84232020-03-17 18:56:44 +0100812_PyEval_InitState(struct _ceval_state *ceval)
813{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200814 ceval->recursion_limit = Py_DEFAULT_RECURSION_LIMIT;
815
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200816 struct _pending_calls *pending = &ceval->pending;
817 assert(pending->lock == NULL);
818
819 pending->lock = PyThread_allocate_lock();
820 if (pending->lock == NULL) {
821 return -1;
822 }
Victor Stinner7be4e352020-05-05 20:27:47 +0200823
824#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
825 _gil_initialize(&ceval->gil);
826#endif
827
Victor Stinnerdda5d6e2020-04-08 17:54:59 +0200828 return 0;
829}
830
831void
832_PyEval_FiniState(struct _ceval_state *ceval)
833{
834 struct _pending_calls *pending = &ceval->pending;
835 if (pending->lock != NULL) {
836 PyThread_free_lock(pending->lock);
837 pending->lock = NULL;
838 }
Eric Snow2ebc5ce2017-09-07 23:51:28 -0600839}
840
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000841int
842Py_GetRecursionLimit(void)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000843{
Victor Stinner1bcc32f2020-06-10 20:08:26 +0200844 PyInterpreterState *interp = _PyInterpreterState_GET();
845 return interp->ceval.recursion_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000846}
847
Vladimir Marangozov7bd25be2000-09-01 11:07:19 +0000848void
849Py_SetRecursionLimit(int new_limit)
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000850{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200851 PyThreadState *tstate = _PyThreadState_GET();
852 tstate->interp->ceval.recursion_limit = new_limit;
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000853}
854
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100855/* The function _Py_EnterRecursiveCall() only calls _Py_CheckRecursiveCall()
Victor Stinner19c3ac92020-09-23 14:04:57 +0200856 if the recursion_depth reaches recursion_limit.
857 If USE_STACKCHECK, the macro decrements recursion_limit
Armin Rigo2b3eb402003-10-28 12:05:48 +0000858 to guarantee that _Py_CheckRecursiveCall() is regularly called.
859 Without USE_STACKCHECK, there is no need for this. */
860int
Victor Stinnerbe434dc2019-11-05 00:51:22 +0100861_Py_CheckRecursiveCall(PyThreadState *tstate, const char *where)
Armin Rigo2b3eb402003-10-28 12:05:48 +0000862{
Victor Stinner4e30ed32020-05-05 16:52:52 +0200863 int recursion_limit = tstate->interp->ceval.recursion_limit;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000864
865#ifdef USE_STACKCHECK
pdox18967932017-10-25 23:03:01 -0700866 tstate->stackcheck_counter = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000867 if (PyOS_CheckStack()) {
868 --tstate->recursion_depth;
Victor Stinner438a12d2019-05-24 17:01:38 +0200869 _PyErr_SetString(tstate, PyExc_MemoryError, "Stack overflow");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 return -1;
871 }
pdox18967932017-10-25 23:03:01 -0700872#endif
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000873 if (tstate->recursion_headroom) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 if (tstate->recursion_depth > recursion_limit + 50) {
875 /* Overflowing while handling an overflow. Give up. */
876 Py_FatalError("Cannot recover from stack overflow.");
877 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878 }
Mark Shannon4e7a69b2020-12-02 13:30:55 +0000879 else {
880 if (tstate->recursion_depth > recursion_limit) {
881 tstate->recursion_headroom++;
882 _PyErr_Format(tstate, PyExc_RecursionError,
883 "maximum recursion depth exceeded%s",
884 where);
885 tstate->recursion_headroom--;
886 --tstate->recursion_depth;
887 return -1;
888 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 }
890 return 0;
Armin Rigo2b3eb402003-10-28 12:05:48 +0000891}
892
Brandt Bucher145bf262021-02-26 14:51:55 -0800893
894// PEP 634: Structural Pattern Matching
895
896
897// Return a tuple of values corresponding to keys, with error checks for
898// duplicate/missing keys.
899static PyObject*
900match_keys(PyThreadState *tstate, PyObject *map, PyObject *keys)
901{
902 assert(PyTuple_CheckExact(keys));
903 Py_ssize_t nkeys = PyTuple_GET_SIZE(keys);
904 if (!nkeys) {
905 // No keys means no items.
906 return PyTuple_New(0);
907 }
908 PyObject *seen = NULL;
909 PyObject *dummy = NULL;
910 PyObject *values = NULL;
911 // We use the two argument form of map.get(key, default) for two reasons:
912 // - Atomically check for a key and get its value without error handling.
913 // - Don't cause key creation or resizing in dict subclasses like
914 // collections.defaultdict that define __missing__ (or similar).
915 _Py_IDENTIFIER(get);
916 PyObject *get = _PyObject_GetAttrId(map, &PyId_get);
917 if (get == NULL) {
918 goto fail;
919 }
920 seen = PySet_New(NULL);
921 if (seen == NULL) {
922 goto fail;
923 }
924 // dummy = object()
925 dummy = _PyObject_CallNoArg((PyObject *)&PyBaseObject_Type);
926 if (dummy == NULL) {
927 goto fail;
928 }
929 values = PyList_New(0);
930 if (values == NULL) {
931 goto fail;
932 }
933 for (Py_ssize_t i = 0; i < nkeys; i++) {
934 PyObject *key = PyTuple_GET_ITEM(keys, i);
935 if (PySet_Contains(seen, key) || PySet_Add(seen, key)) {
936 if (!_PyErr_Occurred(tstate)) {
937 // Seen it before!
938 _PyErr_Format(tstate, PyExc_ValueError,
939 "mapping pattern checks duplicate key (%R)", key);
940 }
941 goto fail;
942 }
943 PyObject *value = PyObject_CallFunctionObjArgs(get, key, dummy, NULL);
944 if (value == NULL) {
945 goto fail;
946 }
947 if (value == dummy) {
948 // key not in map!
949 Py_DECREF(value);
950 Py_DECREF(values);
951 // Return None:
952 Py_INCREF(Py_None);
953 values = Py_None;
954 goto done;
955 }
956 PyList_Append(values, value);
957 Py_DECREF(value);
958 }
959 Py_SETREF(values, PyList_AsTuple(values));
960 // Success:
961done:
962 Py_DECREF(get);
963 Py_DECREF(seen);
964 Py_DECREF(dummy);
965 return values;
966fail:
967 Py_XDECREF(get);
968 Py_XDECREF(seen);
969 Py_XDECREF(dummy);
970 Py_XDECREF(values);
971 return NULL;
972}
973
974// Extract a named attribute from the subject, with additional bookkeeping to
975// raise TypeErrors for repeated lookups. On failure, return NULL (with no
976// error set). Use _PyErr_Occurred(tstate) to disambiguate.
977static PyObject*
978match_class_attr(PyThreadState *tstate, PyObject *subject, PyObject *type,
979 PyObject *name, PyObject *seen)
980{
981 assert(PyUnicode_CheckExact(name));
982 assert(PySet_CheckExact(seen));
983 if (PySet_Contains(seen, name) || PySet_Add(seen, name)) {
984 if (!_PyErr_Occurred(tstate)) {
985 // Seen it before!
986 _PyErr_Format(tstate, PyExc_TypeError,
987 "%s() got multiple sub-patterns for attribute %R",
988 ((PyTypeObject*)type)->tp_name, name);
989 }
990 return NULL;
991 }
992 PyObject *attr = PyObject_GetAttr(subject, name);
993 if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
994 _PyErr_Clear(tstate);
995 }
996 return attr;
997}
998
999// On success (match), return a tuple of extracted attributes. On failure (no
1000// match), return NULL. Use _PyErr_Occurred(tstate) to disambiguate.
1001static PyObject*
1002match_class(PyThreadState *tstate, PyObject *subject, PyObject *type,
1003 Py_ssize_t nargs, PyObject *kwargs)
1004{
1005 if (!PyType_Check(type)) {
1006 const char *e = "called match pattern must be a type";
1007 _PyErr_Format(tstate, PyExc_TypeError, e);
1008 return NULL;
1009 }
1010 assert(PyTuple_CheckExact(kwargs));
1011 // First, an isinstance check:
1012 if (PyObject_IsInstance(subject, type) <= 0) {
1013 return NULL;
1014 }
1015 // So far so good:
1016 PyObject *seen = PySet_New(NULL);
1017 if (seen == NULL) {
1018 return NULL;
1019 }
1020 PyObject *attrs = PyList_New(0);
1021 if (attrs == NULL) {
1022 Py_DECREF(seen);
1023 return NULL;
1024 }
1025 // NOTE: From this point on, goto fail on failure:
1026 PyObject *match_args = NULL;
1027 // First, the positional subpatterns:
1028 if (nargs) {
1029 int match_self = 0;
1030 match_args = PyObject_GetAttrString(type, "__match_args__");
1031 if (match_args) {
1032 if (PyList_CheckExact(match_args)) {
1033 Py_SETREF(match_args, PyList_AsTuple(match_args));
1034 }
1035 if (match_args == NULL) {
1036 goto fail;
1037 }
1038 if (!PyTuple_CheckExact(match_args)) {
1039 const char *e = "%s.__match_args__ must be a list or tuple "
1040 "(got %s)";
1041 _PyErr_Format(tstate, PyExc_TypeError, e,
1042 ((PyTypeObject *)type)->tp_name,
1043 Py_TYPE(match_args)->tp_name);
1044 goto fail;
1045 }
1046 }
1047 else if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
1048 _PyErr_Clear(tstate);
1049 // _Py_TPFLAGS_MATCH_SELF is only acknowledged if the type does not
1050 // define __match_args__. This is natural behavior for subclasses:
1051 // it's as if __match_args__ is some "magic" value that is lost as
1052 // soon as they redefine it.
1053 match_args = PyTuple_New(0);
1054 match_self = PyType_HasFeature((PyTypeObject*)type,
1055 _Py_TPFLAGS_MATCH_SELF);
1056 }
1057 else {
1058 goto fail;
1059 }
1060 assert(PyTuple_CheckExact(match_args));
1061 Py_ssize_t allowed = match_self ? 1 : PyTuple_GET_SIZE(match_args);
1062 if (allowed < nargs) {
1063 const char *plural = (allowed == 1) ? "" : "s";
1064 _PyErr_Format(tstate, PyExc_TypeError,
1065 "%s() accepts %d positional sub-pattern%s (%d given)",
1066 ((PyTypeObject*)type)->tp_name,
1067 allowed, plural, nargs);
1068 goto fail;
1069 }
1070 if (match_self) {
1071 // Easy. Copy the subject itself, and move on to kwargs.
1072 PyList_Append(attrs, subject);
1073 }
1074 else {
1075 for (Py_ssize_t i = 0; i < nargs; i++) {
1076 PyObject *name = PyTuple_GET_ITEM(match_args, i);
1077 if (!PyUnicode_CheckExact(name)) {
1078 _PyErr_Format(tstate, PyExc_TypeError,
1079 "__match_args__ elements must be strings "
1080 "(got %s)", Py_TYPE(name)->tp_name);
1081 goto fail;
1082 }
1083 PyObject *attr = match_class_attr(tstate, subject, type, name,
1084 seen);
1085 if (attr == NULL) {
1086 goto fail;
1087 }
1088 PyList_Append(attrs, attr);
1089 Py_DECREF(attr);
1090 }
1091 }
1092 Py_CLEAR(match_args);
1093 }
1094 // Finally, the keyword subpatterns:
1095 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(kwargs); i++) {
1096 PyObject *name = PyTuple_GET_ITEM(kwargs, i);
1097 PyObject *attr = match_class_attr(tstate, subject, type, name, seen);
1098 if (attr == NULL) {
1099 goto fail;
1100 }
1101 PyList_Append(attrs, attr);
1102 Py_DECREF(attr);
1103 }
1104 Py_SETREF(attrs, PyList_AsTuple(attrs));
1105 Py_DECREF(seen);
1106 return attrs;
1107fail:
1108 // We really don't care whether an error was raised or not... that's our
1109 // caller's problem. All we know is that the match failed.
1110 Py_XDECREF(match_args);
1111 Py_DECREF(seen);
1112 Py_DECREF(attrs);
1113 return NULL;
1114}
1115
1116
Victor Stinner09532fe2019-05-10 23:39:09 +02001117static int do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause);
Victor Stinner438a12d2019-05-24 17:01:38 +02001118static int unpack_iterable(PyThreadState *, PyObject *, int, int, PyObject **);
Guido van Rossum1aa14831997-01-21 05:34:20 +00001119
Victor Stinnere225beb2019-06-03 18:14:24 +02001120#define _Py_TracingPossible(ceval) ((ceval)->tracing_possible)
Antoine Pitrou074e5ed2009-11-10 19:50:40 +00001121
Guido van Rossum374a9221991-04-04 10:40:29 +00001122
Guido van Rossumb209a111997-04-29 18:18:01 +00001123PyObject *
Martin v. Löwis4d0d4712010-12-03 20:14:31 +00001124PyEval_EvalCode(PyObject *co, PyObject *globals, PyObject *locals)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001125{
Victor Stinner46496f92021-02-20 15:17:18 +01001126 PyThreadState *tstate = PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00001127 if (locals == NULL) {
1128 locals = globals;
1129 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01001130 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannon0332e562021-02-01 10:42:03 +00001131 if (builtins == NULL) {
1132 return NULL;
1133 }
1134 PyFrameConstructor desc = {
1135 .fc_globals = globals,
1136 .fc_builtins = builtins,
1137 .fc_name = ((PyCodeObject *)co)->co_name,
1138 .fc_qualname = ((PyCodeObject *)co)->co_name,
1139 .fc_code = co,
1140 .fc_defaults = NULL,
1141 .fc_kwdefaults = NULL,
1142 .fc_closure = NULL
1143 };
Victor Stinner46496f92021-02-20 15:17:18 +01001144 return _PyEval_Vector(tstate, &desc, locals, NULL, 0, NULL);
Guido van Rossum681d79a1995-07-18 14:51:37 +00001145}
1146
1147
1148/* Interpreter main loop */
1149
Martin v. Löwis8d97e332004-06-27 15:43:12 +00001150PyObject *
Victor Stinnerb9e68122019-11-14 12:20:46 +01001151PyEval_EvalFrame(PyFrameObject *f)
1152{
Victor Stinner0b72b232020-03-12 23:18:39 +01001153 /* Function kept for backward compatibility */
Victor Stinnerb9e68122019-11-14 12:20:46 +01001154 PyThreadState *tstate = _PyThreadState_GET();
1155 return _PyEval_EvalFrame(tstate, f, 0);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +00001156}
1157
1158PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001159PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
Guido van Rossum374a9221991-04-04 10:40:29 +00001160{
Victor Stinnerb9e68122019-11-14 12:20:46 +01001161 PyThreadState *tstate = _PyThreadState_GET();
1162 return _PyEval_EvalFrame(tstate, f, throwflag);
Brett Cannon3cebf932016-09-05 15:33:46 -07001163}
1164
Victor Stinnerda2914d2020-03-20 09:29:08 +01001165
1166/* Handle signals, pending calls, GIL drop request
1167 and asynchronous exception */
1168static int
1169eval_frame_handle_pending(PyThreadState *tstate)
1170{
Victor Stinnerda2914d2020-03-20 09:29:08 +01001171 _PyRuntimeState * const runtime = &_PyRuntime;
1172 struct _ceval_runtime_state *ceval = &runtime->ceval;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001173
1174 /* Pending signals */
Victor Stinner299b8c62020-05-05 17:40:18 +02001175 if (_Py_atomic_load_relaxed(&ceval->signals_pending)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001176 if (handle_signals(tstate) != 0) {
1177 return -1;
1178 }
1179 }
1180
1181 /* Pending calls */
Victor Stinner299b8c62020-05-05 17:40:18 +02001182 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinnerda2914d2020-03-20 09:29:08 +01001183 if (_Py_atomic_load_relaxed(&ceval2->pending.calls_to_do)) {
Victor Stinnerbcb094b2021-02-19 15:10:45 +01001184 if (make_pending_calls(tstate->interp) != 0) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001185 return -1;
1186 }
1187 }
1188
1189 /* GIL drop request */
Victor Stinner0b1e3302020-05-05 16:14:31 +02001190 if (_Py_atomic_load_relaxed(&ceval2->gil_drop_request)) {
Victor Stinnerda2914d2020-03-20 09:29:08 +01001191 /* Give another thread a chance */
1192 if (_PyThreadState_Swap(&runtime->gilstate, NULL) != tstate) {
1193 Py_FatalError("tstate mix-up");
1194 }
Victor Stinner0b1e3302020-05-05 16:14:31 +02001195 drop_gil(ceval, ceval2, tstate);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001196
1197 /* Other threads may run now */
1198
1199 take_gil(tstate);
1200
Victor Stinnere838a932020-05-05 19:56:48 +02001201#ifdef EXPERIMENTAL_ISOLATED_SUBINTERPRETERS
1202 (void)_PyThreadState_Swap(&runtime->gilstate, tstate);
1203#else
Victor Stinnerda2914d2020-03-20 09:29:08 +01001204 if (_PyThreadState_Swap(&runtime->gilstate, tstate) != NULL) {
1205 Py_FatalError("orphan tstate");
1206 }
Victor Stinnere838a932020-05-05 19:56:48 +02001207#endif
Victor Stinnerda2914d2020-03-20 09:29:08 +01001208 }
1209
1210 /* Check for asynchronous exception. */
1211 if (tstate->async_exc != NULL) {
1212 PyObject *exc = tstate->async_exc;
1213 tstate->async_exc = NULL;
Victor Stinnerb54a99d2020-04-08 23:35:05 +02001214 UNSIGNAL_ASYNC_EXC(tstate->interp);
Victor Stinnerda2914d2020-03-20 09:29:08 +01001215 _PyErr_SetNone(tstate, exc);
1216 Py_DECREF(exc);
1217 return -1;
1218 }
1219
Victor Stinnerd96a7a82020-11-13 14:44:42 +01001220#ifdef MS_WINDOWS
1221 // bpo-42296: On Windows, _PyEval_SignalReceived() can be called in a
1222 // different thread than the Python thread, in which case
1223 // _Py_ThreadCanHandleSignals() is wrong. Recompute eval_breaker in the
1224 // current Python thread with the correct _Py_ThreadCanHandleSignals()
1225 // value. It prevents to interrupt the eval loop at every instruction if
1226 // the current Python thread cannot handle signals (if
1227 // _Py_ThreadCanHandleSignals() is false).
1228 COMPUTE_EVAL_BREAKER(tstate->interp, ceval, ceval2);
1229#endif
1230
Victor Stinnerda2914d2020-03-20 09:29:08 +01001231 return 0;
1232}
1233
Victor Stinnerc6944e72016-11-11 02:13:35 +01001234PyObject* _Py_HOT_FUNCTION
Victor Stinner0b72b232020-03-12 23:18:39 +01001235_PyEval_EvalFrameDefault(PyThreadState *tstate, PyFrameObject *f, int throwflag)
Brett Cannon3cebf932016-09-05 15:33:46 -07001236{
Victor Stinner3026cad2020-06-01 16:02:40 +02001237 _Py_EnsureTstateNotNULL(tstate);
Victor Stinner0b72b232020-03-12 23:18:39 +01001238
Guido van Rossum950361c1997-01-24 13:49:28 +00001239#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001240 int lastopcode = 0;
Guido van Rossum950361c1997-01-24 13:49:28 +00001241#endif
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001242 PyObject **stack_pointer; /* Next free slot in value stack */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001243 const _Py_CODEUNIT *next_instr;
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001244 int opcode; /* Current opcode */
1245 int oparg; /* Current opcode argument, if any */
Antoine Pitrou9ed5f272013-08-13 20:18:52 +02001246 PyObject **fastlocals, **freevars;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001247 PyObject *retval = NULL; /* Return value */
Victor Stinnerdab84232020-03-17 18:56:44 +01001248 struct _ceval_state * const ceval2 = &tstate->interp->ceval;
Victor Stinner50e6e992020-03-19 02:41:21 +01001249 _Py_atomic_int * const eval_breaker = &ceval2->eval_breaker;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 PyCodeObject *co;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 /* when tracing we set things up so that
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001253
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 not (instr_lb <= current_bytecode_offset < instr_ub)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001255
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 is true when the line being executed has changed. The
1257 initial values are such as to make this false the first
1258 time it is tested. */
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001259
Serhiy Storchakaab874002016-09-11 13:48:15 +03001260 const _Py_CODEUNIT *first_instr;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 PyObject *names;
1262 PyObject *consts;
Inada Naoki91234a12019-06-03 21:30:58 +09001263 _PyOpcache *co_opcache;
Guido van Rossum374a9221991-04-04 10:40:29 +00001264
Brett Cannon368b4b72012-04-02 12:17:59 -04001265#ifdef LLTRACE
Victor Stinner3c1e4812012-03-26 22:10:51 +02001266 _Py_IDENTIFIER(__ltrace__);
Brett Cannon368b4b72012-04-02 12:17:59 -04001267#endif
Victor Stinner3c1e4812012-03-26 22:10:51 +02001268
Antoine Pitroub52ec782009-01-25 16:34:23 +00001269/* Computed GOTOs, or
1270 the-optimization-commonly-but-improperly-known-as-"threaded code"
1271 using gcc's labels-as-values extension
1272 (http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html).
1273
1274 The traditional bytecode evaluation loop uses a "switch" statement, which
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 decent compilers will optimize as a single indirect branch instruction
Antoine Pitroub52ec782009-01-25 16:34:23 +00001276 combined with a lookup table of jump addresses. However, since the
1277 indirect jump instruction is shared by all opcodes, the CPU will have a
1278 hard time making the right prediction for where to jump next (actually,
1279 it will be always wrong except in the uncommon case of a sequence of
1280 several identical opcodes).
1281
1282 "Threaded code" in contrast, uses an explicit jump table and an explicit
1283 indirect jump instruction at the end of each opcode. Since the jump
1284 instruction is at a different address for each opcode, the CPU will make a
1285 separate prediction for each of these instructions, which is equivalent to
1286 predicting the second opcode of each opcode pair. These predictions have
1287 a much better chance to turn out valid, especially in small bytecode loops.
1288
1289 A mispredicted branch on a modern CPU flushes the whole pipeline and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001290 can cost several CPU cycles (depending on the pipeline depth),
Antoine Pitroub52ec782009-01-25 16:34:23 +00001291 and potentially many more instructions (depending on the pipeline width).
1292 A correctly predicted branch, however, is nearly free.
1293
1294 At the time of this writing, the "threaded code" version is up to 15-20%
1295 faster than the normal "switch" version, depending on the compiler and the
1296 CPU architecture.
1297
1298 We disable the optimization if DYNAMIC_EXECUTION_PROFILE is defined,
1299 because it would render the measurements invalid.
1300
1301
1302 NOTE: care must be taken that the compiler doesn't try to "optimize" the
1303 indirect jumps by sharing them between all opcodes. Such optimizations
1304 can be disabled on gcc by using the -fno-gcse flag (or possibly
1305 -fno-crossjumping).
1306*/
1307
Antoine Pitrou042b1282010-08-13 21:15:58 +00001308#ifdef DYNAMIC_EXECUTION_PROFILE
Antoine Pitroub52ec782009-01-25 16:34:23 +00001309#undef USE_COMPUTED_GOTOS
Antoine Pitrou042b1282010-08-13 21:15:58 +00001310#define USE_COMPUTED_GOTOS 0
Antoine Pitroub52ec782009-01-25 16:34:23 +00001311#endif
1312
Antoine Pitrou042b1282010-08-13 21:15:58 +00001313#ifdef HAVE_COMPUTED_GOTOS
1314 #ifndef USE_COMPUTED_GOTOS
1315 #define USE_COMPUTED_GOTOS 1
1316 #endif
1317#else
1318 #if defined(USE_COMPUTED_GOTOS) && USE_COMPUTED_GOTOS
1319 #error "Computed gotos are not supported on this compiler."
1320 #endif
1321 #undef USE_COMPUTED_GOTOS
1322 #define USE_COMPUTED_GOTOS 0
1323#endif
1324
1325#if USE_COMPUTED_GOTOS
Antoine Pitroub52ec782009-01-25 16:34:23 +00001326/* Import the static jump table */
1327#include "opcode_targets.h"
1328
Antoine Pitroub52ec782009-01-25 16:34:23 +00001329#define TARGET(op) \
Benjamin Petersonddd19492018-09-16 22:38:02 -07001330 op: \
1331 TARGET_##op
Antoine Pitroub52ec782009-01-25 16:34:23 +00001332
Antoine Pitroub52ec782009-01-25 16:34:23 +00001333#ifdef LLTRACE
Mark Shannon4958f5d2021-03-24 17:56:12 +00001334#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001335 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001336 if (!lltrace && !_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001338 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001339 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 } \
1341 goto fast_next_opcode; \
1342 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001343#else
Mark Shannon4958f5d2021-03-24 17:56:12 +00001344#define DISPATCH() \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001345 { \
Victor Stinnerdab84232020-03-17 18:56:44 +01001346 if (!_Py_TracingPossible(ceval2) && !PyDTrace_LINE_ENABLED()) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 f->f_lasti = INSTR_OFFSET(); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001348 NEXTOPARG(); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001349 goto *opcode_targets[opcode]; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 } \
1351 goto fast_next_opcode; \
1352 }
Antoine Pitroub52ec782009-01-25 16:34:23 +00001353#endif
1354
1355#else
Benjamin Petersonddd19492018-09-16 22:38:02 -07001356#define TARGET(op) op
Mark Shannon4958f5d2021-03-24 17:56:12 +00001357#define DISPATCH() goto fast_next_opcode
1358
Antoine Pitroub52ec782009-01-25 16:34:23 +00001359#endif
1360
Mark Shannon4958f5d2021-03-24 17:56:12 +00001361#define CHECK_EVAL_BREAKER() \
1362 if (_Py_atomic_load_relaxed(eval_breaker)) { \
1363 continue; \
1364 }
1365
Antoine Pitroub52ec782009-01-25 16:34:23 +00001366
Neal Norwitza81d2202002-07-14 00:27:26 +00001367/* Tuple access macros */
1368
1369#ifndef Py_DEBUG
1370#define GETITEM(v, i) PyTuple_GET_ITEM((PyTupleObject *)(v), (i))
1371#else
1372#define GETITEM(v, i) PyTuple_GetItem((v), (i))
1373#endif
1374
Guido van Rossum374a9221991-04-04 10:40:29 +00001375/* Code access macros */
1376
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001377/* The integer overflow is checked by an assertion below. */
Eric Snow2ebc5ce2017-09-07 23:51:28 -06001378#define INSTR_OFFSET() \
1379 (sizeof(_Py_CODEUNIT) * (int)(next_instr - first_instr))
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001380#define NEXTOPARG() do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001381 _Py_CODEUNIT word = *next_instr; \
1382 opcode = _Py_OPCODE(word); \
1383 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001384 next_instr++; \
1385 } while (0)
Serhiy Storchakaab874002016-09-11 13:48:15 +03001386#define JUMPTO(x) (next_instr = first_instr + (x) / sizeof(_Py_CODEUNIT))
1387#define JUMPBY(x) (next_instr += (x) / sizeof(_Py_CODEUNIT))
Guido van Rossum374a9221991-04-04 10:40:29 +00001388
Raymond Hettingerf606f872003-03-16 03:11:04 +00001389/* OpCode prediction macros
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 Some opcodes tend to come in pairs thus making it possible to
1391 predict the second code when the first is run. For example,
Serhiy Storchakada9c5132016-06-27 18:58:57 +03001392 COMPARE_OP is often followed by POP_JUMP_IF_FALSE or POP_JUMP_IF_TRUE.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 Verifying the prediction costs a single high-speed test of a register
1395 variable against a constant. If the pairing was good, then the
1396 processor's own internal branch predication has a high likelihood of
1397 success, resulting in a nearly zero-overhead transition to the
1398 next opcode. A successful prediction saves a trip through the eval-loop
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001399 including its unpredictable switch-case branch. Combined with the
1400 processor's internal branch prediction, a successful PREDICT has the
1401 effect of making the two opcodes run as if they were a single new opcode
1402 with the bodies combined.
Raymond Hettingerf606f872003-03-16 03:11:04 +00001403
Georg Brandl86b2fb92008-07-16 03:43:04 +00001404 If collecting opcode statistics, your choices are to either keep the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 predictions turned-on and interpret the results as if some opcodes
1406 had been combined or turn-off predictions so that the opcode frequency
1407 counter updates for both opcodes.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001408
1409 Opcode prediction is disabled with threaded code, since the latter allows
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 the CPU to record separate branch prediction information for each
1411 opcode.
Antoine Pitroub52ec782009-01-25 16:34:23 +00001412
Raymond Hettingerf606f872003-03-16 03:11:04 +00001413*/
1414
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001415#define PREDICT_ID(op) PRED_##op
1416
Antoine Pitrou042b1282010-08-13 21:15:58 +00001417#if defined(DYNAMIC_EXECUTION_PROFILE) || USE_COMPUTED_GOTOS
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001418#define PREDICT(op) if (0) goto PREDICT_ID(op)
Raymond Hettingera7216982004-02-08 19:59:27 +00001419#else
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001420#define PREDICT(op) \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001421 do { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001422 _Py_CODEUNIT word = *next_instr; \
1423 opcode = _Py_OPCODE(word); \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001424 if (opcode == op) { \
Serhiy Storchakaab874002016-09-11 13:48:15 +03001425 oparg = _Py_OPARG(word); \
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001426 next_instr++; \
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001427 goto PREDICT_ID(op); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001428 } \
1429 } while(0)
Antoine Pitroub52ec782009-01-25 16:34:23 +00001430#endif
Denis Chernikovbaf29b22020-02-21 12:17:50 +03001431#define PREDICTED(op) PREDICT_ID(op):
Antoine Pitroub52ec782009-01-25 16:34:23 +00001432
Raymond Hettingerf606f872003-03-16 03:11:04 +00001433
Guido van Rossum374a9221991-04-04 10:40:29 +00001434/* Stack manipulation macros */
1435
Martin v. Löwis18e16552006-02-15 17:27:45 +00001436/* The stack can grow at most MAXINT deep, as co_nlocals and
1437 co_stacksize are ints. */
Stefan Krahb7e10102010-06-23 18:42:39 +00001438#define STACK_LEVEL() ((int)(stack_pointer - f->f_valuestack))
1439#define EMPTY() (STACK_LEVEL() == 0)
1440#define TOP() (stack_pointer[-1])
1441#define SECOND() (stack_pointer[-2])
1442#define THIRD() (stack_pointer[-3])
1443#define FOURTH() (stack_pointer[-4])
1444#define PEEK(n) (stack_pointer[-(n)])
1445#define SET_TOP(v) (stack_pointer[-1] = (v))
1446#define SET_SECOND(v) (stack_pointer[-2] = (v))
1447#define SET_THIRD(v) (stack_pointer[-3] = (v))
1448#define SET_FOURTH(v) (stack_pointer[-4] = (v))
Stefan Krahb7e10102010-06-23 18:42:39 +00001449#define BASIC_STACKADJ(n) (stack_pointer += n)
1450#define BASIC_PUSH(v) (*stack_pointer++ = (v))
1451#define BASIC_POP() (*--stack_pointer)
Guido van Rossum374a9221991-04-04 10:40:29 +00001452
Guido van Rossum96a42c81992-01-12 02:29:51 +00001453#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454#define PUSH(v) { (void)(BASIC_PUSH(v), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001455 lltrace && prtrace(tstate, TOP(), "push")); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001456 assert(STACK_LEVEL() <= co->co_stacksize); }
Victor Stinner438a12d2019-05-24 17:01:38 +02001457#define POP() ((void)(lltrace && prtrace(tstate, TOP(), "pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001458 BASIC_POP())
costypetrisor8ed317f2018-07-31 20:55:14 +00001459#define STACK_GROW(n) do { \
1460 assert(n >= 0); \
1461 (void)(BASIC_STACKADJ(n), \
Victor Stinner438a12d2019-05-24 17:01:38 +02001462 lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001463 assert(STACK_LEVEL() <= co->co_stacksize); \
1464 } while (0)
1465#define STACK_SHRINK(n) do { \
1466 assert(n >= 0); \
Victor Stinner438a12d2019-05-24 17:01:38 +02001467 (void)(lltrace && prtrace(tstate, TOP(), "stackadj")); \
costypetrisor8ed317f2018-07-31 20:55:14 +00001468 (void)(BASIC_STACKADJ(-n)); \
1469 assert(STACK_LEVEL() <= co->co_stacksize); \
1470 } while (0)
Christian Heimes0449f632007-12-15 01:27:15 +00001471#define EXT_POP(STACK_POINTER) ((void)(lltrace && \
Victor Stinner438a12d2019-05-24 17:01:38 +02001472 prtrace(tstate, (STACK_POINTER)[-1], "ext_pop")), \
Stefan Krahb7e10102010-06-23 18:42:39 +00001473 *--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001474#else
Stefan Krahb7e10102010-06-23 18:42:39 +00001475#define PUSH(v) BASIC_PUSH(v)
1476#define POP() BASIC_POP()
costypetrisor8ed317f2018-07-31 20:55:14 +00001477#define STACK_GROW(n) BASIC_STACKADJ(n)
1478#define STACK_SHRINK(n) BASIC_STACKADJ(-n)
Guido van Rossumc2e20742006-02-27 22:32:47 +00001479#define EXT_POP(STACK_POINTER) (*--(STACK_POINTER))
Guido van Rossum374a9221991-04-04 10:40:29 +00001480#endif
1481
Guido van Rossum681d79a1995-07-18 14:51:37 +00001482/* Local variable macros */
1483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484#define GETLOCAL(i) (fastlocals[i])
Guido van Rossumcfbf1a32002-03-28 20:17:52 +00001485
1486/* The SETLOCAL() macro must not DECREF the local variable in-place and
1487 then store the new value; it must copy the old value to a temporary
1488 value, then store the new value, and then DECREF the temporary value.
1489 This is because it is possible that during the DECREF the frame is
1490 accessed by other code (e.g. a __del__ method or gc.collect()) and the
1491 variable would be pointing to already-freed memory. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492#define SETLOCAL(i, value) do { PyObject *tmp = GETLOCAL(i); \
Stefan Krahb7e10102010-06-23 18:42:39 +00001493 GETLOCAL(i) = value; \
1494 Py_XDECREF(tmp); } while (0)
Guido van Rossum681d79a1995-07-18 14:51:37 +00001495
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001496
1497#define UNWIND_BLOCK(b) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001498 while (STACK_LEVEL() > (b)->b_level) { \
1499 PyObject *v = POP(); \
1500 Py_XDECREF(v); \
1501 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001502
1503#define UNWIND_EXCEPT_HANDLER(b) \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001504 do { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 PyObject *type, *value, *traceback; \
Mark Shannonae3087c2017-10-22 22:41:51 +01001506 _PyErr_StackItem *exc_info; \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 assert(STACK_LEVEL() >= (b)->b_level + 3); \
1508 while (STACK_LEVEL() > (b)->b_level + 3) { \
1509 value = POP(); \
1510 Py_XDECREF(value); \
1511 } \
Mark Shannonae3087c2017-10-22 22:41:51 +01001512 exc_info = tstate->exc_info; \
1513 type = exc_info->exc_type; \
1514 value = exc_info->exc_value; \
1515 traceback = exc_info->exc_traceback; \
1516 exc_info->exc_type = POP(); \
1517 exc_info->exc_value = POP(); \
1518 exc_info->exc_traceback = POP(); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 Py_XDECREF(type); \
1520 Py_XDECREF(value); \
1521 Py_XDECREF(traceback); \
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001522 } while(0)
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001523
Inada Naoki91234a12019-06-03 21:30:58 +09001524 /* macros for opcode cache */
1525#define OPCACHE_CHECK() \
1526 do { \
1527 co_opcache = NULL; \
1528 if (co->co_opcache != NULL) { \
Pablo Galindo109826c2020-10-20 06:22:44 +01001529 unsigned char co_opcache_offset = \
Inada Naoki91234a12019-06-03 21:30:58 +09001530 co->co_opcache_map[next_instr - first_instr]; \
Pablo Galindo109826c2020-10-20 06:22:44 +01001531 if (co_opcache_offset > 0) { \
1532 assert(co_opcache_offset <= co->co_opcache_size); \
1533 co_opcache = &co->co_opcache[co_opcache_offset - 1]; \
Inada Naoki91234a12019-06-03 21:30:58 +09001534 assert(co_opcache != NULL); \
Inada Naoki91234a12019-06-03 21:30:58 +09001535 } \
1536 } \
1537 } while (0)
1538
Pablo Galindo109826c2020-10-20 06:22:44 +01001539#define OPCACHE_DEOPT() \
1540 do { \
1541 if (co_opcache != NULL) { \
1542 co_opcache->optimized = -1; \
1543 unsigned char co_opcache_offset = \
1544 co->co_opcache_map[next_instr - first_instr]; \
1545 assert(co_opcache_offset <= co->co_opcache_size); \
1546 co->co_opcache_map[co_opcache_offset] = 0; \
1547 co_opcache = NULL; \
1548 } \
1549 } while (0)
1550
1551#define OPCACHE_DEOPT_LOAD_ATTR() \
1552 do { \
1553 if (co_opcache != NULL) { \
1554 OPCACHE_STAT_ATTR_DEOPT(); \
1555 OPCACHE_DEOPT(); \
1556 } \
1557 } while (0)
1558
1559#define OPCACHE_MAYBE_DEOPT_LOAD_ATTR() \
1560 do { \
1561 if (co_opcache != NULL && --co_opcache->optimized <= 0) { \
1562 OPCACHE_DEOPT_LOAD_ATTR(); \
1563 } \
1564 } while (0)
1565
Inada Naoki91234a12019-06-03 21:30:58 +09001566#if OPCACHE_STATS
1567
1568#define OPCACHE_STAT_GLOBAL_HIT() \
1569 do { \
1570 if (co->co_opcache != NULL) opcache_global_hits++; \
1571 } while (0)
1572
1573#define OPCACHE_STAT_GLOBAL_MISS() \
1574 do { \
1575 if (co->co_opcache != NULL) opcache_global_misses++; \
1576 } while (0)
1577
1578#define OPCACHE_STAT_GLOBAL_OPT() \
1579 do { \
1580 if (co->co_opcache != NULL) opcache_global_opts++; \
1581 } while (0)
1582
Pablo Galindo109826c2020-10-20 06:22:44 +01001583#define OPCACHE_STAT_ATTR_HIT() \
1584 do { \
1585 if (co->co_opcache != NULL) opcache_attr_hits++; \
1586 } while (0)
1587
1588#define OPCACHE_STAT_ATTR_MISS() \
1589 do { \
1590 if (co->co_opcache != NULL) opcache_attr_misses++; \
1591 } while (0)
1592
1593#define OPCACHE_STAT_ATTR_OPT() \
1594 do { \
1595 if (co->co_opcache!= NULL) opcache_attr_opts++; \
1596 } while (0)
1597
1598#define OPCACHE_STAT_ATTR_DEOPT() \
1599 do { \
1600 if (co->co_opcache != NULL) opcache_attr_deopts++; \
1601 } while (0)
1602
1603#define OPCACHE_STAT_ATTR_TOTAL() \
1604 do { \
1605 if (co->co_opcache != NULL) opcache_attr_total++; \
1606 } while (0)
1607
Inada Naoki91234a12019-06-03 21:30:58 +09001608#else /* OPCACHE_STATS */
1609
1610#define OPCACHE_STAT_GLOBAL_HIT()
1611#define OPCACHE_STAT_GLOBAL_MISS()
1612#define OPCACHE_STAT_GLOBAL_OPT()
1613
Pablo Galindo109826c2020-10-20 06:22:44 +01001614#define OPCACHE_STAT_ATTR_HIT()
1615#define OPCACHE_STAT_ATTR_MISS()
1616#define OPCACHE_STAT_ATTR_OPT()
1617#define OPCACHE_STAT_ATTR_DEOPT()
1618#define OPCACHE_STAT_ATTR_TOTAL()
1619
Inada Naoki91234a12019-06-03 21:30:58 +09001620#endif
1621
Guido van Rossuma027efa1997-05-05 20:56:21 +00001622/* Start of code */
1623
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001624 if (_Py_EnterRecursiveCall(tstate, "")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 return NULL;
Victor Stinnerbe434dc2019-11-05 00:51:22 +01001626 }
Guido van Rossum8861b741996-07-30 16:49:37 +00001627
Mark Shannon8e1b4062021-03-05 14:45:50 +00001628 PyTraceInfo trace_info;
1629 /* Mark trace_info as initialized */
1630 trace_info.code = NULL;
1631
1632 /* push frame */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 tstate->frame = f;
Mark Shannon86433452021-01-07 16:49:02 +00001634 co = f->f_code;
Tim Peters5ca576e2001-06-18 22:08:13 +00001635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (tstate->use_tracing) {
1637 if (tstate->c_tracefunc != NULL) {
1638 /* tstate->c_tracefunc, if defined, is a
1639 function that will be called on *every* entry
1640 to a code block. Its return value, if not
1641 None, is a function that will be called at
1642 the start of each executed line of code.
1643 (Actually, the function must return itself
1644 in order to continue tracing.) The trace
1645 functions are called with three arguments:
1646 a pointer to the current frame, a string
1647 indicating why the function is called, and
1648 an argument which depends on the situation.
1649 The global trace function is also called
1650 whenever an exception is detected. */
1651 if (call_trace_protected(tstate->c_tracefunc,
1652 tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001653 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001654 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 /* Trace function raised an error */
1656 goto exit_eval_frame;
1657 }
1658 }
1659 if (tstate->c_profilefunc != NULL) {
1660 /* Similar for c_profilefunc, except it needn't
1661 return itself and isn't called for "line" events */
1662 if (call_trace_protected(tstate->c_profilefunc,
1663 tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001664 tstate, f, &trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00001665 PyTrace_CALL, Py_None)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 /* Profile function raised an error */
1667 goto exit_eval_frame;
1668 }
1669 }
1670 }
Neil Schemenauer6c0f2002001-09-04 19:03:35 +00001671
Łukasz Langaa785c872016-09-09 17:37:37 -07001672 if (PyDTrace_FUNCTION_ENTRY_ENABLED())
1673 dtrace_function_entry(f);
1674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675 names = co->co_names;
1676 consts = co->co_consts;
1677 fastlocals = f->f_localsplus;
1678 freevars = f->f_localsplus + co->co_nlocals;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001679 assert(PyBytes_Check(co->co_code));
1680 assert(PyBytes_GET_SIZE(co->co_code) <= INT_MAX);
Serhiy Storchakaab874002016-09-11 13:48:15 +03001681 assert(PyBytes_GET_SIZE(co->co_code) % sizeof(_Py_CODEUNIT) == 0);
1682 assert(_Py_IS_ALIGNED(PyBytes_AS_STRING(co->co_code), sizeof(_Py_CODEUNIT)));
1683 first_instr = (_Py_CODEUNIT *) PyBytes_AS_STRING(co->co_code);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001684 /*
1685 f->f_lasti refers to the index of the last instruction,
1686 unless it's -1 in which case next_instr should be first_instr.
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001687
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001688 YIELD_FROM sets f_lasti to itself, in order to repeatedly yield
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05001689 multiple values.
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 When the PREDICT() macros are enabled, some opcode pairs follow in
1692 direct succession without updating f->f_lasti. A successful
1693 prediction effectively links the two codes together as if they
1694 were a single new opcode; accordingly,f->f_lasti will point to
1695 the first code in the pair (for instance, GET_ITER followed by
1696 FOR_ITER is effectively a single opcode and f->f_lasti will point
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001697 to the beginning of the combined pair.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698 */
Serhiy Storchakaab874002016-09-11 13:48:15 +03001699 assert(f->f_lasti >= -1);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001700 next_instr = first_instr;
1701 if (f->f_lasti >= 0) {
Serhiy Storchakaab874002016-09-11 13:48:15 +03001702 assert(f->f_lasti % sizeof(_Py_CODEUNIT) == 0);
1703 next_instr += f->f_lasti / sizeof(_Py_CODEUNIT) + 1;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001704 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01001705 stack_pointer = f->f_valuestack + f->f_stackdepth;
1706 /* Set f->f_stackdepth to -1.
1707 * Update when returning or calling trace function.
1708 Having f_stackdepth <= 0 ensures that invalid
1709 values are not visible to the cycle GC.
1710 We choose -1 rather than 0 to assist debugging.
1711 */
1712 f->f_stackdepth = -1;
1713 f->f_state = FRAME_EXECUTING;
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001714
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001715 if (co->co_opcache_flag < opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001716 co->co_opcache_flag++;
Pablo Galindoaf5fa132021-02-28 22:41:09 +00001717 if (co->co_opcache_flag == opcache_min_runs) {
Inada Naoki91234a12019-06-03 21:30:58 +09001718 if (_PyCode_InitOpcache(co) < 0) {
Victor Stinner25104942020-04-24 02:43:18 +02001719 goto exit_eval_frame;
Inada Naoki91234a12019-06-03 21:30:58 +09001720 }
1721#if OPCACHE_STATS
1722 opcache_code_objects_extra_mem +=
1723 PyBytes_Size(co->co_code) / sizeof(_Py_CODEUNIT) +
1724 sizeof(_PyOpcache) * co->co_opcache_size;
1725 opcache_code_objects++;
1726#endif
1727 }
1728 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00001729
Tim Peters5ca576e2001-06-18 22:08:13 +00001730#ifdef LLTRACE
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001731 {
1732 int r = _PyDict_ContainsId(f->f_globals, &PyId___ltrace__);
1733 if (r < 0) {
1734 goto exit_eval_frame;
1735 }
1736 lltrace = r;
1737 }
Tim Peters5ca576e2001-06-18 22:08:13 +00001738#endif
Guido van Rossumac7be682001-01-17 15:42:30 +00001739
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001740 if (throwflag) { /* support for generator.throw() */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001741 goto error;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001742 }
Thomas Wouters477c8d52006-05-27 19:21:47 +00001743
Victor Stinnerace47d72013-07-18 01:41:08 +02001744#ifdef Py_DEBUG
Victor Stinner0b72b232020-03-12 23:18:39 +01001745 /* _PyEval_EvalFrameDefault() must not be called with an exception set,
Victor Stinnera8cb5152017-01-18 14:12:51 +01001746 because it can clear it (directly or indirectly) and so the
Martin Panter9955a372015-10-07 10:26:23 +00001747 caller loses its exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02001748 assert(!_PyErr_Occurred(tstate));
Victor Stinnerace47d72013-07-18 01:41:08 +02001749#endif
1750
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001751main_loop:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 for (;;) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 assert(stack_pointer >= f->f_valuestack); /* else underflow */
1754 assert(STACK_LEVEL() <= co->co_stacksize); /* else overflow */
Victor Stinner438a12d2019-05-24 17:01:38 +02001755 assert(!_PyErr_Occurred(tstate));
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001756
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 /* Do periodic things. Doing this every time through
1758 the loop would add too much overhead, so we do it
1759 only every Nth instruction. We also do it if
Chris Jerdonek4a12d122020-05-14 19:25:45 -07001760 ``pending.calls_to_do'' is set, i.e. when an asynchronous
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 event needs attention (e.g. a signal handler or
1762 async I/O handler); see Py_AddPendingCall() and
1763 Py_MakePendingCalls() above. */
Guido van Rossumac7be682001-01-17 15:42:30 +00001764
Eric Snow7bda9de2019-03-08 17:25:54 -07001765 if (_Py_atomic_load_relaxed(eval_breaker)) {
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001766 opcode = _Py_OPCODE(*next_instr);
1767 if (opcode == SETUP_FINALLY ||
1768 opcode == SETUP_WITH ||
1769 opcode == BEFORE_ASYNC_WITH ||
1770 opcode == YIELD_FROM) {
1771 /* Few cases where we skip running signal handlers and other
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001772 pending calls:
Serhiy Storchaka3f4d90d2018-07-09 15:40:14 +03001773 - If we're about to enter the 'with:'. It will prevent
1774 emitting a resource warning in the common idiom
1775 'with open(path) as file:'.
1776 - If we're about to enter the 'async with:'.
1777 - If we're about to enter the 'try:' of a try/finally (not
Nathaniel J. Smithab4413a2017-05-17 13:33:23 -07001778 *very* useful, but might help in some cases and it's
1779 traditional)
1780 - If we're resuming a chain of nested 'yield from' or
1781 'await' calls, then each frame is parked with YIELD_FROM
1782 as its next opcode. If the user hit control-C we want to
1783 wait until we've reached the innermost frame before
1784 running the signal handler and raising KeyboardInterrupt
1785 (see bpo-30039).
1786 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 goto fast_next_opcode;
1788 }
Eric Snowfdf282d2019-01-11 14:26:55 -07001789
Victor Stinnerda2914d2020-03-20 09:29:08 +01001790 if (eval_frame_handle_pending(tstate) != 0) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001791 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 }
1793 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 fast_next_opcode:
1796 f->f_lasti = INSTR_OFFSET();
Guido van Rossumac7be682001-01-17 15:42:30 +00001797
Łukasz Langaa785c872016-09-09 17:37:37 -07001798 if (PyDTrace_LINE_ENABLED())
Mark Shannon8e1b4062021-03-05 14:45:50 +00001799 maybe_dtrace_line(f, &trace_info);
Łukasz Langaa785c872016-09-09 17:37:37 -07001800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001801 /* line-by-line tracing support */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001802
Victor Stinnerdab84232020-03-17 18:56:44 +01001803 if (_Py_TracingPossible(ceval2) &&
Benjamin Peterson51f46162013-01-23 08:38:47 -05001804 tstate->c_tracefunc != NULL && !tstate->tracing) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001805 int err;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001806 /* see maybe_call_line_trace()
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001807 for expository comments */
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02001808 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Tim Peters8a5c3c72004-04-05 19:36:21 +00001809
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 err = maybe_call_line_trace(tstate->c_tracefunc,
1811 tstate->c_traceobj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01001812 tstate, f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00001813 &trace_info);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 /* Reload possibly changed frame fields */
1815 JUMPTO(f->f_lasti);
Mark Shannoncb9879b2020-07-17 11:44:23 +01001816 stack_pointer = f->f_valuestack+f->f_stackdepth;
1817 f->f_stackdepth = -1;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001818 if (err)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 /* trace function raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001820 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 }
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 /* Extract opcode and argument */
Michael W. Hudson019a78e2002-11-08 12:53:11 +00001824
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03001825 NEXTOPARG();
Stefan Krahb7e10102010-06-23 18:42:39 +00001826 dispatch_opcode:
Guido van Rossum950361c1997-01-24 13:49:28 +00001827#ifdef DYNAMIC_EXECUTION_PROFILE
1828#ifdef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829 dxpairs[lastopcode][opcode]++;
1830 lastopcode = opcode;
Guido van Rossum950361c1997-01-24 13:49:28 +00001831#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 dxp[opcode]++;
Guido van Rossum950361c1997-01-24 13:49:28 +00001833#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00001834
Guido van Rossum96a42c81992-01-12 02:29:51 +00001835#ifdef LLTRACE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 /* Instruction tracing */
Guido van Rossumac7be682001-01-17 15:42:30 +00001837
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 if (lltrace) {
1839 if (HAS_ARG(opcode)) {
1840 printf("%d: %d, %d\n",
1841 f->f_lasti, opcode, oparg);
1842 }
1843 else {
1844 printf("%d: %d\n",
1845 f->f_lasti, opcode);
1846 }
1847 }
Guido van Rossum374a9221991-04-04 10:40:29 +00001848#endif
Michael W. Hudsondd32a912002-08-15 14:59:02 +00001849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 switch (opcode) {
Guido van Rossumac7be682001-01-17 15:42:30 +00001851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852 /* BEWARE!
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001853 It is essential that any operation that fails must goto error
1854 and that all operation that succeed call [FAST_]DISPATCH() ! */
Guido van Rossumac7be682001-01-17 15:42:30 +00001855
Benjamin Petersonddd19492018-09-16 22:38:02 -07001856 case TARGET(NOP): {
Mark Shannon4958f5d2021-03-24 17:56:12 +00001857 DISPATCH();
Benjamin Petersonddd19492018-09-16 22:38:02 -07001858 }
Raymond Hettinger9c18e812004-06-21 16:31:15 +00001859
Benjamin Petersonddd19492018-09-16 22:38:02 -07001860 case TARGET(LOAD_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001861 PyObject *value = GETLOCAL(oparg);
1862 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02001863 format_exc_check_arg(tstate, PyExc_UnboundLocalError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001864 UNBOUNDLOCAL_ERROR_MSG,
1865 PyTuple_GetItem(co->co_varnames, oparg));
1866 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001868 Py_INCREF(value);
1869 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001870 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001871 }
1872
Benjamin Petersonddd19492018-09-16 22:38:02 -07001873 case TARGET(LOAD_CONST): {
1874 PREDICTED(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001875 PyObject *value = GETITEM(consts, oparg);
1876 Py_INCREF(value);
1877 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001878 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001879 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001880
Benjamin Petersonddd19492018-09-16 22:38:02 -07001881 case TARGET(STORE_FAST): {
1882 PREDICTED(STORE_FAST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001883 PyObject *value = POP();
1884 SETLOCAL(oparg, value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001885 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001886 }
Neil Schemenauer63543862002-02-17 19:10:14 +00001887
Benjamin Petersonddd19492018-09-16 22:38:02 -07001888 case TARGET(POP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001889 PyObject *value = POP();
1890 Py_DECREF(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001891 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001892 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001893
Benjamin Petersonddd19492018-09-16 22:38:02 -07001894 case TARGET(ROT_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001895 PyObject *top = TOP();
1896 PyObject *second = SECOND();
1897 SET_TOP(second);
1898 SET_SECOND(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001899 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001900 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001901
Benjamin Petersonddd19492018-09-16 22:38:02 -07001902 case TARGET(ROT_THREE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001903 PyObject *top = TOP();
1904 PyObject *second = SECOND();
1905 PyObject *third = THIRD();
1906 SET_TOP(second);
1907 SET_SECOND(third);
1908 SET_THIRD(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001909 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001910 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001911
Benjamin Petersonddd19492018-09-16 22:38:02 -07001912 case TARGET(ROT_FOUR): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001913 PyObject *top = TOP();
1914 PyObject *second = SECOND();
1915 PyObject *third = THIRD();
1916 PyObject *fourth = FOURTH();
1917 SET_TOP(second);
1918 SET_SECOND(third);
1919 SET_THIRD(fourth);
1920 SET_FOURTH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001921 DISPATCH();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001922 }
1923
Benjamin Petersonddd19492018-09-16 22:38:02 -07001924 case TARGET(DUP_TOP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001925 PyObject *top = TOP();
1926 Py_INCREF(top);
1927 PUSH(top);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001928 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001929 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001930
Benjamin Petersonddd19492018-09-16 22:38:02 -07001931 case TARGET(DUP_TOP_TWO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001932 PyObject *top = TOP();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001933 PyObject *second = SECOND();
Benjamin Petersonf208df32012-10-12 11:37:56 -04001934 Py_INCREF(top);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001935 Py_INCREF(second);
costypetrisor8ed317f2018-07-31 20:55:14 +00001936 STACK_GROW(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001937 SET_TOP(top);
1938 SET_SECOND(second);
Mark Shannon4958f5d2021-03-24 17:56:12 +00001939 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001940 }
Thomas Wouters434d0822000-08-24 20:11:32 +00001941
Benjamin Petersonddd19492018-09-16 22:38:02 -07001942 case TARGET(UNARY_POSITIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001943 PyObject *value = TOP();
1944 PyObject *res = PyNumber_Positive(value);
1945 Py_DECREF(value);
1946 SET_TOP(res);
1947 if (res == NULL)
1948 goto error;
1949 DISPATCH();
1950 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001951
Benjamin Petersonddd19492018-09-16 22:38:02 -07001952 case TARGET(UNARY_NEGATIVE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001953 PyObject *value = TOP();
1954 PyObject *res = PyNumber_Negative(value);
1955 Py_DECREF(value);
1956 SET_TOP(res);
1957 if (res == NULL)
1958 goto error;
1959 DISPATCH();
1960 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001961
Benjamin Petersonddd19492018-09-16 22:38:02 -07001962 case TARGET(UNARY_NOT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001963 PyObject *value = TOP();
1964 int err = PyObject_IsTrue(value);
1965 Py_DECREF(value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001966 if (err == 0) {
1967 Py_INCREF(Py_True);
1968 SET_TOP(Py_True);
1969 DISPATCH();
1970 }
1971 else if (err > 0) {
1972 Py_INCREF(Py_False);
1973 SET_TOP(Py_False);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001974 DISPATCH();
1975 }
costypetrisor8ed317f2018-07-31 20:55:14 +00001976 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001977 goto error;
1978 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001979
Benjamin Petersonddd19492018-09-16 22:38:02 -07001980 case TARGET(UNARY_INVERT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001981 PyObject *value = TOP();
1982 PyObject *res = PyNumber_Invert(value);
1983 Py_DECREF(value);
1984 SET_TOP(res);
1985 if (res == NULL)
1986 goto error;
1987 DISPATCH();
1988 }
Guido van Rossumac7be682001-01-17 15:42:30 +00001989
Benjamin Petersonddd19492018-09-16 22:38:02 -07001990 case TARGET(BINARY_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04001991 PyObject *exp = POP();
1992 PyObject *base = TOP();
1993 PyObject *res = PyNumber_Power(base, exp, Py_None);
1994 Py_DECREF(base);
1995 Py_DECREF(exp);
1996 SET_TOP(res);
1997 if (res == NULL)
1998 goto error;
1999 DISPATCH();
2000 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002001
Benjamin Petersonddd19492018-09-16 22:38:02 -07002002 case TARGET(BINARY_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002003 PyObject *right = POP();
2004 PyObject *left = TOP();
2005 PyObject *res = PyNumber_Multiply(left, right);
2006 Py_DECREF(left);
2007 Py_DECREF(right);
2008 SET_TOP(res);
2009 if (res == NULL)
2010 goto error;
2011 DISPATCH();
2012 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002013
Benjamin Petersonddd19492018-09-16 22:38:02 -07002014 case TARGET(BINARY_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002015 PyObject *right = POP();
2016 PyObject *left = TOP();
2017 PyObject *res = PyNumber_MatrixMultiply(left, right);
2018 Py_DECREF(left);
2019 Py_DECREF(right);
2020 SET_TOP(res);
2021 if (res == NULL)
2022 goto error;
2023 DISPATCH();
2024 }
2025
Benjamin Petersonddd19492018-09-16 22:38:02 -07002026 case TARGET(BINARY_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002027 PyObject *divisor = POP();
2028 PyObject *dividend = TOP();
2029 PyObject *quotient = PyNumber_TrueDivide(dividend, divisor);
2030 Py_DECREF(dividend);
2031 Py_DECREF(divisor);
2032 SET_TOP(quotient);
2033 if (quotient == NULL)
2034 goto error;
2035 DISPATCH();
2036 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002037
Benjamin Petersonddd19492018-09-16 22:38:02 -07002038 case TARGET(BINARY_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002039 PyObject *divisor = POP();
2040 PyObject *dividend = TOP();
2041 PyObject *quotient = PyNumber_FloorDivide(dividend, divisor);
2042 Py_DECREF(dividend);
2043 Py_DECREF(divisor);
2044 SET_TOP(quotient);
2045 if (quotient == NULL)
2046 goto error;
2047 DISPATCH();
2048 }
Guido van Rossum4668b002001-08-08 05:00:18 +00002049
Benjamin Petersonddd19492018-09-16 22:38:02 -07002050 case TARGET(BINARY_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002051 PyObject *divisor = POP();
2052 PyObject *dividend = TOP();
Martijn Pietersd7e64332017-02-23 13:38:04 +00002053 PyObject *res;
2054 if (PyUnicode_CheckExact(dividend) && (
2055 !PyUnicode_Check(divisor) || PyUnicode_CheckExact(divisor))) {
2056 // fast path; string formatting, but not if the RHS is a str subclass
2057 // (see issue28598)
2058 res = PyUnicode_Format(dividend, divisor);
2059 } else {
2060 res = PyNumber_Remainder(dividend, divisor);
2061 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002062 Py_DECREF(divisor);
2063 Py_DECREF(dividend);
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(BINARY_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002071 PyObject *right = POP();
2072 PyObject *left = TOP();
2073 PyObject *sum;
Victor Stinnerbd0a08e2020-10-01 18:57:37 +02002074 /* NOTE(vstinner): Please don't try to micro-optimize int+int on
Victor Stinnerd65f42a2016-10-20 12:18:10 +02002075 CPython using bytecode, it is simply worthless.
2076 See http://bugs.python.org/issue21955 and
2077 http://bugs.python.org/issue10044 for the discussion. In short,
2078 no patch shown any impact on a realistic benchmark, only a minor
2079 speedup on microbenchmarks. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002080 if (PyUnicode_CheckExact(left) &&
2081 PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002082 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002083 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002084 }
2085 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002086 sum = PyNumber_Add(left, right);
2087 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002088 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002089 Py_DECREF(right);
2090 SET_TOP(sum);
2091 if (sum == NULL)
2092 goto error;
2093 DISPATCH();
2094 }
2095
Benjamin Petersonddd19492018-09-16 22:38:02 -07002096 case TARGET(BINARY_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002097 PyObject *right = POP();
2098 PyObject *left = TOP();
2099 PyObject *diff = PyNumber_Subtract(left, right);
2100 Py_DECREF(right);
2101 Py_DECREF(left);
2102 SET_TOP(diff);
2103 if (diff == NULL)
2104 goto error;
2105 DISPATCH();
2106 }
2107
Benjamin Petersonddd19492018-09-16 22:38:02 -07002108 case TARGET(BINARY_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002109 PyObject *sub = POP();
2110 PyObject *container = TOP();
2111 PyObject *res = PyObject_GetItem(container, sub);
2112 Py_DECREF(container);
2113 Py_DECREF(sub);
2114 SET_TOP(res);
2115 if (res == NULL)
2116 goto error;
2117 DISPATCH();
2118 }
2119
Benjamin Petersonddd19492018-09-16 22:38:02 -07002120 case TARGET(BINARY_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002121 PyObject *right = POP();
2122 PyObject *left = TOP();
2123 PyObject *res = PyNumber_Lshift(left, right);
2124 Py_DECREF(left);
2125 Py_DECREF(right);
2126 SET_TOP(res);
2127 if (res == NULL)
2128 goto error;
2129 DISPATCH();
2130 }
2131
Benjamin Petersonddd19492018-09-16 22:38:02 -07002132 case TARGET(BINARY_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002133 PyObject *right = POP();
2134 PyObject *left = TOP();
2135 PyObject *res = PyNumber_Rshift(left, right);
2136 Py_DECREF(left);
2137 Py_DECREF(right);
2138 SET_TOP(res);
2139 if (res == NULL)
2140 goto error;
2141 DISPATCH();
2142 }
2143
Benjamin Petersonddd19492018-09-16 22:38:02 -07002144 case TARGET(BINARY_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002145 PyObject *right = POP();
2146 PyObject *left = TOP();
2147 PyObject *res = PyNumber_And(left, right);
2148 Py_DECREF(left);
2149 Py_DECREF(right);
2150 SET_TOP(res);
2151 if (res == NULL)
2152 goto error;
2153 DISPATCH();
2154 }
2155
Benjamin Petersonddd19492018-09-16 22:38:02 -07002156 case TARGET(BINARY_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002157 PyObject *right = POP();
2158 PyObject *left = TOP();
2159 PyObject *res = PyNumber_Xor(left, right);
2160 Py_DECREF(left);
2161 Py_DECREF(right);
2162 SET_TOP(res);
2163 if (res == NULL)
2164 goto error;
2165 DISPATCH();
2166 }
2167
Benjamin Petersonddd19492018-09-16 22:38:02 -07002168 case TARGET(BINARY_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002169 PyObject *right = POP();
2170 PyObject *left = TOP();
2171 PyObject *res = PyNumber_Or(left, right);
2172 Py_DECREF(left);
2173 Py_DECREF(right);
2174 SET_TOP(res);
2175 if (res == NULL)
2176 goto error;
2177 DISPATCH();
2178 }
2179
Benjamin Petersonddd19492018-09-16 22:38:02 -07002180 case TARGET(LIST_APPEND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002181 PyObject *v = POP();
2182 PyObject *list = PEEK(oparg);
2183 int err;
2184 err = PyList_Append(list, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002185 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002186 if (err != 0)
2187 goto error;
2188 PREDICT(JUMP_ABSOLUTE);
2189 DISPATCH();
2190 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002191
Benjamin Petersonddd19492018-09-16 22:38:02 -07002192 case TARGET(SET_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002193 PyObject *v = POP();
Raymond Hettinger41862222016-10-15 19:03:06 -07002194 PyObject *set = PEEK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002195 int err;
2196 err = PySet_Add(set, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002197 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002198 if (err != 0)
2199 goto error;
2200 PREDICT(JUMP_ABSOLUTE);
2201 DISPATCH();
2202 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002203
Benjamin Petersonddd19492018-09-16 22:38:02 -07002204 case TARGET(INPLACE_POWER): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002205 PyObject *exp = POP();
2206 PyObject *base = TOP();
2207 PyObject *res = PyNumber_InPlacePower(base, exp, Py_None);
2208 Py_DECREF(base);
2209 Py_DECREF(exp);
2210 SET_TOP(res);
2211 if (res == NULL)
2212 goto error;
2213 DISPATCH();
2214 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002215
Benjamin Petersonddd19492018-09-16 22:38:02 -07002216 case TARGET(INPLACE_MULTIPLY): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002217 PyObject *right = POP();
2218 PyObject *left = TOP();
2219 PyObject *res = PyNumber_InPlaceMultiply(left, right);
2220 Py_DECREF(left);
2221 Py_DECREF(right);
2222 SET_TOP(res);
2223 if (res == NULL)
2224 goto error;
2225 DISPATCH();
2226 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002227
Benjamin Petersonddd19492018-09-16 22:38:02 -07002228 case TARGET(INPLACE_MATRIX_MULTIPLY): {
Benjamin Petersond51374e2014-04-09 23:55:56 -04002229 PyObject *right = POP();
2230 PyObject *left = TOP();
2231 PyObject *res = PyNumber_InPlaceMatrixMultiply(left, right);
2232 Py_DECREF(left);
2233 Py_DECREF(right);
2234 SET_TOP(res);
2235 if (res == NULL)
2236 goto error;
2237 DISPATCH();
2238 }
2239
Benjamin Petersonddd19492018-09-16 22:38:02 -07002240 case TARGET(INPLACE_TRUE_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002241 PyObject *divisor = POP();
2242 PyObject *dividend = TOP();
2243 PyObject *quotient = PyNumber_InPlaceTrueDivide(dividend, divisor);
2244 Py_DECREF(dividend);
2245 Py_DECREF(divisor);
2246 SET_TOP(quotient);
2247 if (quotient == NULL)
2248 goto error;
2249 DISPATCH();
2250 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002251
Benjamin Petersonddd19492018-09-16 22:38:02 -07002252 case TARGET(INPLACE_FLOOR_DIVIDE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002253 PyObject *divisor = POP();
2254 PyObject *dividend = TOP();
2255 PyObject *quotient = PyNumber_InPlaceFloorDivide(dividend, divisor);
2256 Py_DECREF(dividend);
2257 Py_DECREF(divisor);
2258 SET_TOP(quotient);
2259 if (quotient == NULL)
2260 goto error;
2261 DISPATCH();
2262 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002263
Benjamin Petersonddd19492018-09-16 22:38:02 -07002264 case TARGET(INPLACE_MODULO): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002265 PyObject *right = POP();
2266 PyObject *left = TOP();
2267 PyObject *mod = PyNumber_InPlaceRemainder(left, right);
2268 Py_DECREF(left);
2269 Py_DECREF(right);
2270 SET_TOP(mod);
2271 if (mod == NULL)
2272 goto error;
2273 DISPATCH();
2274 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002275
Benjamin Petersonddd19492018-09-16 22:38:02 -07002276 case TARGET(INPLACE_ADD): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002277 PyObject *right = POP();
2278 PyObject *left = TOP();
2279 PyObject *sum;
2280 if (PyUnicode_CheckExact(left) && PyUnicode_CheckExact(right)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002281 sum = unicode_concatenate(tstate, left, right, f, next_instr);
Martin Panter95f53c12016-07-18 08:23:26 +00002282 /* unicode_concatenate consumed the ref to left */
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002283 }
2284 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002285 sum = PyNumber_InPlaceAdd(left, right);
2286 Py_DECREF(left);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02002287 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002288 Py_DECREF(right);
2289 SET_TOP(sum);
2290 if (sum == NULL)
2291 goto error;
2292 DISPATCH();
2293 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002294
Benjamin Petersonddd19492018-09-16 22:38:02 -07002295 case TARGET(INPLACE_SUBTRACT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002296 PyObject *right = POP();
2297 PyObject *left = TOP();
2298 PyObject *diff = PyNumber_InPlaceSubtract(left, right);
2299 Py_DECREF(left);
2300 Py_DECREF(right);
2301 SET_TOP(diff);
2302 if (diff == NULL)
2303 goto error;
2304 DISPATCH();
2305 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002306
Benjamin Petersonddd19492018-09-16 22:38:02 -07002307 case TARGET(INPLACE_LSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002308 PyObject *right = POP();
2309 PyObject *left = TOP();
2310 PyObject *res = PyNumber_InPlaceLshift(left, right);
2311 Py_DECREF(left);
2312 Py_DECREF(right);
2313 SET_TOP(res);
2314 if (res == NULL)
2315 goto error;
2316 DISPATCH();
2317 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002318
Benjamin Petersonddd19492018-09-16 22:38:02 -07002319 case TARGET(INPLACE_RSHIFT): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002320 PyObject *right = POP();
2321 PyObject *left = TOP();
2322 PyObject *res = PyNumber_InPlaceRshift(left, right);
2323 Py_DECREF(left);
2324 Py_DECREF(right);
2325 SET_TOP(res);
2326 if (res == NULL)
2327 goto error;
2328 DISPATCH();
2329 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002330
Benjamin Petersonddd19492018-09-16 22:38:02 -07002331 case TARGET(INPLACE_AND): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002332 PyObject *right = POP();
2333 PyObject *left = TOP();
2334 PyObject *res = PyNumber_InPlaceAnd(left, right);
2335 Py_DECREF(left);
2336 Py_DECREF(right);
2337 SET_TOP(res);
2338 if (res == NULL)
2339 goto error;
2340 DISPATCH();
2341 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002342
Benjamin Petersonddd19492018-09-16 22:38:02 -07002343 case TARGET(INPLACE_XOR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002344 PyObject *right = POP();
2345 PyObject *left = TOP();
2346 PyObject *res = PyNumber_InPlaceXor(left, right);
2347 Py_DECREF(left);
2348 Py_DECREF(right);
2349 SET_TOP(res);
2350 if (res == NULL)
2351 goto error;
2352 DISPATCH();
2353 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002354
Benjamin Petersonddd19492018-09-16 22:38:02 -07002355 case TARGET(INPLACE_OR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002356 PyObject *right = POP();
2357 PyObject *left = TOP();
2358 PyObject *res = PyNumber_InPlaceOr(left, right);
2359 Py_DECREF(left);
2360 Py_DECREF(right);
2361 SET_TOP(res);
2362 if (res == NULL)
2363 goto error;
2364 DISPATCH();
2365 }
Thomas Wouters434d0822000-08-24 20:11:32 +00002366
Benjamin Petersonddd19492018-09-16 22:38:02 -07002367 case TARGET(STORE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002368 PyObject *sub = TOP();
2369 PyObject *container = SECOND();
2370 PyObject *v = THIRD();
2371 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002372 STACK_SHRINK(3);
Martin Panter95f53c12016-07-18 08:23:26 +00002373 /* container[sub] = v */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002374 err = PyObject_SetItem(container, sub, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002375 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002376 Py_DECREF(container);
2377 Py_DECREF(sub);
2378 if (err != 0)
2379 goto error;
2380 DISPATCH();
2381 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002382
Benjamin Petersonddd19492018-09-16 22:38:02 -07002383 case TARGET(DELETE_SUBSCR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002384 PyObject *sub = TOP();
2385 PyObject *container = SECOND();
2386 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002387 STACK_SHRINK(2);
Martin Panter95f53c12016-07-18 08:23:26 +00002388 /* del container[sub] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002389 err = PyObject_DelItem(container, sub);
2390 Py_DECREF(container);
2391 Py_DECREF(sub);
2392 if (err != 0)
2393 goto error;
2394 DISPATCH();
2395 }
Barry Warsaw23c9ec82000-08-21 15:44:01 +00002396
Benjamin Petersonddd19492018-09-16 22:38:02 -07002397 case TARGET(PRINT_EXPR): {
Victor Stinnercab75e32013-11-06 22:38:37 +01002398 _Py_IDENTIFIER(displayhook);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002399 PyObject *value = POP();
Victor Stinnercab75e32013-11-06 22:38:37 +01002400 PyObject *hook = _PySys_GetObjectId(&PyId_displayhook);
Benjamin Petersonfe1bcb62012-10-12 11:40:01 -04002401 PyObject *res;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002402 if (hook == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002403 _PyErr_SetString(tstate, PyExc_RuntimeError,
2404 "lost sys.displayhook");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002405 Py_DECREF(value);
2406 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002407 }
Petr Viktorinffd97532020-02-11 17:46:57 +01002408 res = PyObject_CallOneArg(hook, value);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002409 Py_DECREF(value);
2410 if (res == NULL)
2411 goto error;
2412 Py_DECREF(res);
2413 DISPATCH();
2414 }
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +00002415
Benjamin Petersonddd19492018-09-16 22:38:02 -07002416 case TARGET(RAISE_VARARGS): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002417 PyObject *cause = NULL, *exc = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418 switch (oparg) {
2419 case 2:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002420 cause = POP(); /* cause */
Stefan Krahf432a322017-08-21 13:09:59 +02002421 /* fall through */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002422 case 1:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002423 exc = POP(); /* exc */
Stefan Krahf432a322017-08-21 13:09:59 +02002424 /* fall through */
2425 case 0:
Victor Stinner09532fe2019-05-10 23:39:09 +02002426 if (do_raise(tstate, exc, cause)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002427 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002428 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002429 break;
2430 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02002431 _PyErr_SetString(tstate, PyExc_SystemError,
2432 "bad RAISE_VARARGS oparg");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002433 break;
2434 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002435 goto error;
2436 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002437
Benjamin Petersonddd19492018-09-16 22:38:02 -07002438 case TARGET(RETURN_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002439 retval = POP();
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002440 assert(f->f_iblock == 0);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002441 assert(EMPTY());
Mark Shannoncb9879b2020-07-17 11:44:23 +01002442 f->f_state = FRAME_RETURNED;
2443 f->f_stackdepth = 0;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002444 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002445 }
Guido van Rossumdb3165e1993-10-18 17:06:59 +00002446
Benjamin Petersonddd19492018-09-16 22:38:02 -07002447 case TARGET(GET_AITER): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002448 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002449 PyObject *iter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002450 PyObject *obj = TOP();
2451 PyTypeObject *type = Py_TYPE(obj);
2452
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002453 if (type->tp_as_async != NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002454 getter = type->tp_as_async->am_aiter;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002455 }
Yury Selivanov75445082015-05-11 22:57:16 -04002456
2457 if (getter != NULL) {
2458 iter = (*getter)(obj);
2459 Py_DECREF(obj);
2460 if (iter == NULL) {
2461 SET_TOP(NULL);
2462 goto error;
2463 }
2464 }
2465 else {
2466 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002467 _PyErr_Format(tstate, PyExc_TypeError,
2468 "'async for' requires an object with "
2469 "__aiter__ method, got %.100s",
2470 type->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002471 Py_DECREF(obj);
2472 goto error;
2473 }
2474
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002475 if (Py_TYPE(iter)->tp_as_async == NULL ||
2476 Py_TYPE(iter)->tp_as_async->am_anext == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002477
Yury Selivanov398ff912017-03-02 22:20:00 -05002478 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02002479 _PyErr_Format(tstate, PyExc_TypeError,
2480 "'async for' received an object from __aiter__ "
2481 "that does not implement __anext__: %.100s",
2482 Py_TYPE(iter)->tp_name);
Yury Selivanov75445082015-05-11 22:57:16 -04002483 Py_DECREF(iter);
2484 goto error;
Yury Selivanova6f6edb2016-06-09 15:08:31 -04002485 }
2486
Yury Selivanovfaa135a2017-10-06 02:08:57 -04002487 SET_TOP(iter);
Yury Selivanov75445082015-05-11 22:57:16 -04002488 DISPATCH();
2489 }
2490
Benjamin Petersonddd19492018-09-16 22:38:02 -07002491 case TARGET(GET_ANEXT): {
Yury Selivanov6ef05902015-05-28 11:21:31 -04002492 unaryfunc getter = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002493 PyObject *next_iter = NULL;
2494 PyObject *awaitable = NULL;
2495 PyObject *aiter = TOP();
2496 PyTypeObject *type = Py_TYPE(aiter);
2497
Yury Selivanoveb636452016-09-08 22:01:51 -07002498 if (PyAsyncGen_CheckExact(aiter)) {
2499 awaitable = type->tp_as_async->am_anext(aiter);
2500 if (awaitable == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002501 goto error;
2502 }
Yury Selivanoveb636452016-09-08 22:01:51 -07002503 } else {
2504 if (type->tp_as_async != NULL){
2505 getter = type->tp_as_async->am_anext;
2506 }
Yury Selivanov75445082015-05-11 22:57:16 -04002507
Yury Selivanoveb636452016-09-08 22:01:51 -07002508 if (getter != NULL) {
2509 next_iter = (*getter)(aiter);
2510 if (next_iter == NULL) {
2511 goto error;
2512 }
2513 }
2514 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02002515 _PyErr_Format(tstate, PyExc_TypeError,
2516 "'async for' requires an iterator with "
2517 "__anext__ method, got %.100s",
2518 type->tp_name);
Yury Selivanoveb636452016-09-08 22:01:51 -07002519 goto error;
2520 }
Yury Selivanov75445082015-05-11 22:57:16 -04002521
Yury Selivanoveb636452016-09-08 22:01:51 -07002522 awaitable = _PyCoro_GetAwaitableIter(next_iter);
2523 if (awaitable == NULL) {
Yury Selivanov398ff912017-03-02 22:20:00 -05002524 _PyErr_FormatFromCause(
Yury Selivanoveb636452016-09-08 22:01:51 -07002525 PyExc_TypeError,
2526 "'async for' received an invalid object "
2527 "from __anext__: %.100s",
2528 Py_TYPE(next_iter)->tp_name);
2529
2530 Py_DECREF(next_iter);
2531 goto error;
2532 } else {
2533 Py_DECREF(next_iter);
2534 }
2535 }
Yury Selivanov75445082015-05-11 22:57:16 -04002536
2537 PUSH(awaitable);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002538 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002539 DISPATCH();
2540 }
2541
Benjamin Petersonddd19492018-09-16 22:38:02 -07002542 case TARGET(GET_AWAITABLE): {
2543 PREDICTED(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04002544 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04002545 PyObject *iter = _PyCoro_GetAwaitableIter(iterable);
Yury Selivanov75445082015-05-11 22:57:16 -04002546
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002547 if (iter == NULL) {
Mark Shannonfee55262019-11-21 09:11:43 +00002548 int opcode_at_minus_3 = 0;
2549 if ((next_instr - first_instr) > 2) {
2550 opcode_at_minus_3 = _Py_OPCODE(next_instr[-3]);
2551 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002552 format_awaitable_error(tstate, Py_TYPE(iterable),
Mark Shannonfee55262019-11-21 09:11:43 +00002553 opcode_at_minus_3,
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03002554 _Py_OPCODE(next_instr[-2]));
2555 }
2556
Yury Selivanov75445082015-05-11 22:57:16 -04002557 Py_DECREF(iterable);
2558
Yury Selivanovc724bae2016-03-02 11:30:46 -05002559 if (iter != NULL && PyCoro_CheckExact(iter)) {
2560 PyObject *yf = _PyGen_yf((PyGenObject*)iter);
2561 if (yf != NULL) {
2562 /* `iter` is a coroutine object that is being
2563 awaited, `yf` is a pointer to the current awaitable
2564 being awaited on. */
2565 Py_DECREF(yf);
2566 Py_CLEAR(iter);
Victor Stinner438a12d2019-05-24 17:01:38 +02002567 _PyErr_SetString(tstate, PyExc_RuntimeError,
2568 "coroutine is being awaited already");
Yury Selivanovc724bae2016-03-02 11:30:46 -05002569 /* The code below jumps to `error` if `iter` is NULL. */
2570 }
2571 }
2572
Yury Selivanov75445082015-05-11 22:57:16 -04002573 SET_TOP(iter); /* Even if it's NULL */
2574
2575 if (iter == NULL) {
2576 goto error;
2577 }
2578
Serhiy Storchakada9c5132016-06-27 18:58:57 +03002579 PREDICT(LOAD_CONST);
Yury Selivanov75445082015-05-11 22:57:16 -04002580 DISPATCH();
2581 }
2582
Benjamin Petersonddd19492018-09-16 22:38:02 -07002583 case TARGET(YIELD_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002584 PyObject *v = POP();
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002585 PyObject *receiver = TOP();
Vladimir Matveev037245c2020-10-09 17:15:15 -07002586 PySendResult gen_status;
2587 if (tstate->c_tracefunc == NULL) {
2588 gen_status = PyIter_Send(receiver, v, &retval);
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002589 } else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002590 _Py_IDENTIFIER(send);
2591 if (v == Py_None && PyIter_Check(receiver)) {
2592 retval = Py_TYPE(receiver)->tp_iternext(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002593 }
2594 else {
Vladimir Matveev037245c2020-10-09 17:15:15 -07002595 retval = _PyObject_CallMethodIdOneArg(receiver, &PyId_send, v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002596 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002597 if (retval == NULL) {
2598 if (tstate->c_tracefunc != NULL
2599 && _PyErr_ExceptionMatches(tstate, PyExc_StopIteration))
Mark Shannon8e1b4062021-03-05 14:45:50 +00002600 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002601 if (_PyGen_FetchStopIterationValue(&retval) == 0) {
2602 gen_status = PYGEN_RETURN;
2603 }
2604 else {
2605 gen_status = PYGEN_ERROR;
2606 }
2607 }
2608 else {
2609 gen_status = PYGEN_NEXT;
2610 }
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002611 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002612 Py_DECREF(v);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002613 if (gen_status == PYGEN_ERROR) {
2614 assert (retval == NULL);
2615 goto error;
2616 }
2617 if (gen_status == PYGEN_RETURN) {
2618 assert (retval != NULL);
2619
Raymond Hettinger15f44ab2016-08-30 10:47:49 -07002620 Py_DECREF(receiver);
Vladimir Matveev2b053612020-09-18 18:38:38 -07002621 SET_TOP(retval);
2622 retval = NULL;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002623 DISPATCH();
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002624 }
Vladimir Matveev2b053612020-09-18 18:38:38 -07002625 assert (gen_status == PYGEN_NEXT);
Martin Panter95f53c12016-07-18 08:23:26 +00002626 /* receiver remains on stack, retval is value to be yielded */
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -05002627 /* and repeat... */
Victor Stinnerf7d199f2016-11-24 22:33:01 +01002628 assert(f->f_lasti >= (int)sizeof(_Py_CODEUNIT));
Serhiy Storchakaab874002016-09-11 13:48:15 +03002629 f->f_lasti -= sizeof(_Py_CODEUNIT);
Mark Shannoncb9879b2020-07-17 11:44:23 +01002630 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002631 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002632 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002633 }
Nick Coghlan1f7ce622012-01-13 21:43:40 +10002634
Benjamin Petersonddd19492018-09-16 22:38:02 -07002635 case TARGET(YIELD_VALUE): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002636 retval = POP();
Yury Selivanoveb636452016-09-08 22:01:51 -07002637
2638 if (co->co_flags & CO_ASYNC_GENERATOR) {
2639 PyObject *w = _PyAsyncGenValueWrapperNew(retval);
2640 Py_DECREF(retval);
2641 if (w == NULL) {
2642 retval = NULL;
2643 goto error;
2644 }
2645 retval = w;
2646 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01002647 f->f_state = FRAME_SUSPENDED;
Victor Stinnerb7d8d8d2020-09-23 14:07:16 +02002648 f->f_stackdepth = (int)(stack_pointer - f->f_valuestack);
Mark Shannone7c9f4a2020-01-13 12:51:26 +00002649 goto exiting;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002650 }
Tim Peters5ca576e2001-06-18 22:08:13 +00002651
Benjamin Petersonddd19492018-09-16 22:38:02 -07002652 case TARGET(POP_EXCEPT): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002653 PyObject *type, *value, *traceback;
2654 _PyErr_StackItem *exc_info;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002655 PyTryBlock *b = PyFrame_BlockPop(f);
2656 if (b->b_type != EXCEPT_HANDLER) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002657 _PyErr_SetString(tstate, PyExc_SystemError,
2658 "popped block is not an except handler");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002659 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002660 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002661 assert(STACK_LEVEL() >= (b)->b_level + 3 &&
2662 STACK_LEVEL() <= (b)->b_level + 4);
2663 exc_info = tstate->exc_info;
2664 type = exc_info->exc_type;
2665 value = exc_info->exc_value;
2666 traceback = exc_info->exc_traceback;
2667 exc_info->exc_type = POP();
2668 exc_info->exc_value = POP();
2669 exc_info->exc_traceback = POP();
2670 Py_XDECREF(type);
2671 Py_XDECREF(value);
2672 Py_XDECREF(traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002674 }
Benjamin Petersoneec3d712008-06-11 15:59:43 +00002675
Benjamin Petersonddd19492018-09-16 22:38:02 -07002676 case TARGET(POP_BLOCK): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002677 PyFrame_BlockPop(f);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002678 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002679 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002680
Mark Shannonfee55262019-11-21 09:11:43 +00002681 case TARGET(RERAISE): {
Mark Shannonbf353f32020-12-17 13:55:28 +00002682 assert(f->f_iblock > 0);
2683 if (oparg) {
2684 f->f_lasti = f->f_blockstack[f->f_iblock-1].b_handler;
2685 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002686 PyObject *exc = POP();
Mark Shannonfee55262019-11-21 09:11:43 +00002687 PyObject *val = POP();
2688 PyObject *tb = POP();
2689 assert(PyExceptionClass_Check(exc));
Victor Stinner61f4db82020-01-28 03:37:45 +01002690 _PyErr_Restore(tstate, exc, val, tb);
Mark Shannonfee55262019-11-21 09:11:43 +00002691 goto exception_unwind;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002692 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002693
Benjamin Petersonddd19492018-09-16 22:38:02 -07002694 case TARGET(END_ASYNC_FOR): {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002695 PyObject *exc = POP();
2696 assert(PyExceptionClass_Check(exc));
2697 if (PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration)) {
2698 PyTryBlock *b = PyFrame_BlockPop(f);
2699 assert(b->b_type == EXCEPT_HANDLER);
2700 Py_DECREF(exc);
2701 UNWIND_EXCEPT_HANDLER(b);
2702 Py_DECREF(POP());
2703 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002704 DISPATCH();
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002705 }
2706 else {
2707 PyObject *val = POP();
2708 PyObject *tb = POP();
Victor Stinner438a12d2019-05-24 17:01:38 +02002709 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002710 goto exception_unwind;
2711 }
2712 }
2713
Zackery Spytzce6a0702019-08-25 03:44:09 -06002714 case TARGET(LOAD_ASSERTION_ERROR): {
2715 PyObject *value = PyExc_AssertionError;
2716 Py_INCREF(value);
2717 PUSH(value);
Mark Shannon4958f5d2021-03-24 17:56:12 +00002718 DISPATCH();
Zackery Spytzce6a0702019-08-25 03:44:09 -06002719 }
2720
Benjamin Petersonddd19492018-09-16 22:38:02 -07002721 case TARGET(LOAD_BUILD_CLASS): {
Victor Stinner3c1e4812012-03-26 22:10:51 +02002722 _Py_IDENTIFIER(__build_class__);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002723
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002724 PyObject *bc;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002725 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002726 bc = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___build_class__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002727 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002728 if (!_PyErr_Occurred(tstate)) {
2729 _PyErr_SetString(tstate, PyExc_NameError,
2730 "__build_class__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002731 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002732 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002733 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002734 Py_INCREF(bc);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002735 }
2736 else {
2737 PyObject *build_class_str = _PyUnicode_FromId(&PyId___build_class__);
2738 if (build_class_str == NULL)
Serhiy Storchaka70b72f02016-11-08 23:12:46 +02002739 goto error;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002740 bc = PyObject_GetItem(f->f_builtins, build_class_str);
2741 if (bc == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002742 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
2743 _PyErr_SetString(tstate, PyExc_NameError,
2744 "__build_class__ not found");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002745 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002746 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002747 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002748 PUSH(bc);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002749 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02002750 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002751
Benjamin Petersonddd19492018-09-16 22:38:02 -07002752 case TARGET(STORE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002753 PyObject *name = GETITEM(names, oparg);
2754 PyObject *v = POP();
2755 PyObject *ns = f->f_locals;
2756 int err;
2757 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002758 _PyErr_Format(tstate, PyExc_SystemError,
2759 "no locals found when storing %R", name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002761 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002763 if (PyDict_CheckExact(ns))
2764 err = PyDict_SetItem(ns, name, v);
2765 else
2766 err = PyObject_SetItem(ns, name, v);
2767 Py_DECREF(v);
2768 if (err != 0)
2769 goto error;
2770 DISPATCH();
2771 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002772
Benjamin Petersonddd19492018-09-16 22:38:02 -07002773 case TARGET(DELETE_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002774 PyObject *name = GETITEM(names, oparg);
2775 PyObject *ns = f->f_locals;
2776 int err;
2777 if (ns == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002778 _PyErr_Format(tstate, PyExc_SystemError,
2779 "no locals when deleting %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002780 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002781 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002782 err = PyObject_DelItem(ns, name);
2783 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002784 format_exc_check_arg(tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002785 NAME_ERROR_MSG,
2786 name);
2787 goto error;
2788 }
2789 DISPATCH();
2790 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00002791
Benjamin Petersonddd19492018-09-16 22:38:02 -07002792 case TARGET(UNPACK_SEQUENCE): {
2793 PREDICTED(UNPACK_SEQUENCE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002794 PyObject *seq = POP(), *item, **items;
2795 if (PyTuple_CheckExact(seq) &&
2796 PyTuple_GET_SIZE(seq) == oparg) {
2797 items = ((PyTupleObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002798 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002799 item = items[oparg];
2800 Py_INCREF(item);
2801 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002802 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002803 } else if (PyList_CheckExact(seq) &&
2804 PyList_GET_SIZE(seq) == oparg) {
2805 items = ((PyListObject *)seq)->ob_item;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002806 while (oparg--) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002807 item = items[oparg];
2808 Py_INCREF(item);
2809 PUSH(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002810 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002811 } else if (unpack_iterable(tstate, seq, oparg, -1,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002812 stack_pointer + oparg)) {
costypetrisor8ed317f2018-07-31 20:55:14 +00002813 STACK_GROW(oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002814 } else {
2815 /* unpack_iterable() raised an exception */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002816 Py_DECREF(seq);
2817 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002818 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002819 Py_DECREF(seq);
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002820 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002821 }
Guido van Rossum0368b722007-05-11 16:50:42 +00002822
Benjamin Petersonddd19492018-09-16 22:38:02 -07002823 case TARGET(UNPACK_EX): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002824 int totalargs = 1 + (oparg & 0xFF) + (oparg >> 8);
2825 PyObject *seq = POP();
2826
Victor Stinner438a12d2019-05-24 17:01:38 +02002827 if (unpack_iterable(tstate, seq, oparg & 0xFF, oparg >> 8,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002828 stack_pointer + totalargs)) {
2829 stack_pointer += totalargs;
2830 } else {
2831 Py_DECREF(seq);
2832 goto error;
2833 }
2834 Py_DECREF(seq);
2835 DISPATCH();
2836 }
2837
Benjamin Petersonddd19492018-09-16 22:38:02 -07002838 case TARGET(STORE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002839 PyObject *name = GETITEM(names, oparg);
2840 PyObject *owner = TOP();
2841 PyObject *v = SECOND();
2842 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00002843 STACK_SHRINK(2);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002844 err = PyObject_SetAttr(owner, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002846 Py_DECREF(owner);
2847 if (err != 0)
2848 goto error;
2849 DISPATCH();
2850 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002851
Benjamin Petersonddd19492018-09-16 22:38:02 -07002852 case TARGET(DELETE_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002853 PyObject *name = GETITEM(names, oparg);
2854 PyObject *owner = POP();
2855 int err;
2856 err = PyObject_SetAttr(owner, name, (PyObject *)NULL);
2857 Py_DECREF(owner);
2858 if (err != 0)
2859 goto error;
2860 DISPATCH();
2861 }
2862
Benjamin Petersonddd19492018-09-16 22:38:02 -07002863 case TARGET(STORE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002864 PyObject *name = GETITEM(names, oparg);
2865 PyObject *v = POP();
2866 int err;
2867 err = PyDict_SetItem(f->f_globals, name, v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 Py_DECREF(v);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002869 if (err != 0)
2870 goto error;
2871 DISPATCH();
2872 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002873
Benjamin Petersonddd19492018-09-16 22:38:02 -07002874 case TARGET(DELETE_GLOBAL): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002875 PyObject *name = GETITEM(names, oparg);
2876 int err;
2877 err = PyDict_DelItem(f->f_globals, name);
2878 if (err != 0) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002879 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
2880 format_exc_check_arg(tstate, PyExc_NameError,
2881 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002882 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002883 goto error;
Benjamin Peterson00f86f22012-10-10 14:10:33 -04002884 }
2885 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002886 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002887
Benjamin Petersonddd19492018-09-16 22:38:02 -07002888 case TARGET(LOAD_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002889 PyObject *name = GETITEM(names, oparg);
2890 PyObject *locals = f->f_locals;
2891 PyObject *v;
2892 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002893 _PyErr_Format(tstate, PyExc_SystemError,
2894 "no locals when loading %R", name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002895 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002896 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002897 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002898 v = PyDict_GetItemWithError(locals, name);
2899 if (v != NULL) {
2900 Py_INCREF(v);
2901 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002902 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002903 goto error;
2904 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002905 }
2906 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002907 v = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01002908 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002909 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError))
Benjamin Peterson92722792012-12-15 12:51:05 -05002910 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02002911 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002912 }
2913 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002914 if (v == NULL) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002915 v = PyDict_GetItemWithError(f->f_globals, name);
2916 if (v != NULL) {
2917 Py_INCREF(v);
2918 }
Victor Stinner438a12d2019-05-24 17:01:38 +02002919 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002920 goto error;
2921 }
2922 else {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002923 if (PyDict_CheckExact(f->f_builtins)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002924 v = PyDict_GetItemWithError(f->f_builtins, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002925 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002926 if (!_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002927 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002928 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002929 NAME_ERROR_MSG, name);
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02002930 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002931 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002932 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002933 Py_INCREF(v);
Victor Stinnerb0b22422012-04-19 00:57:45 +02002934 }
2935 else {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002936 v = PyObject_GetItem(f->f_builtins, name);
2937 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02002938 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinnerb0b22422012-04-19 00:57:45 +02002939 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02002940 tstate, PyExc_NameError,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002941 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02002942 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002943 goto error;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002944 }
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00002945 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002946 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002947 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002948 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002949 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002950 }
Guido van Rossumac7be682001-01-17 15:42:30 +00002951
Benjamin Petersonddd19492018-09-16 22:38:02 -07002952 case TARGET(LOAD_GLOBAL): {
Inada Naoki91234a12019-06-03 21:30:58 +09002953 PyObject *name;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002954 PyObject *v;
Victor Stinnerb0b22422012-04-19 00:57:45 +02002955 if (PyDict_CheckExact(f->f_globals)
Victor Stinnerb4efc962015-11-20 09:24:02 +01002956 && PyDict_CheckExact(f->f_builtins))
2957 {
Inada Naoki91234a12019-06-03 21:30:58 +09002958 OPCACHE_CHECK();
2959 if (co_opcache != NULL && co_opcache->optimized > 0) {
2960 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2961
2962 if (lg->globals_ver ==
2963 ((PyDictObject *)f->f_globals)->ma_version_tag
2964 && lg->builtins_ver ==
2965 ((PyDictObject *)f->f_builtins)->ma_version_tag)
2966 {
2967 PyObject *ptr = lg->ptr;
2968 OPCACHE_STAT_GLOBAL_HIT();
2969 assert(ptr != NULL);
2970 Py_INCREF(ptr);
2971 PUSH(ptr);
2972 DISPATCH();
2973 }
2974 }
2975
2976 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002977 v = _PyDict_LoadGlobal((PyDictObject *)f->f_globals,
Benjamin Peterson7d95e402012-04-23 11:24:50 -04002978 (PyDictObject *)f->f_builtins,
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002979 name);
2980 if (v == NULL) {
Victor Stinnera4860542021-02-19 15:08:54 +01002981 if (!_PyErr_Occurred(tstate)) {
Victor Stinnerb4efc962015-11-20 09:24:02 +01002982 /* _PyDict_LoadGlobal() returns NULL without raising
2983 * an exception if the key doesn't exist */
Victor Stinner438a12d2019-05-24 17:01:38 +02002984 format_exc_check_arg(tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02002985 NAME_ERROR_MSG, name);
Victor Stinnerb4efc962015-11-20 09:24:02 +01002986 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04002987 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002988 }
Inada Naoki91234a12019-06-03 21:30:58 +09002989
2990 if (co_opcache != NULL) {
2991 _PyOpcache_LoadGlobal *lg = &co_opcache->u.lg;
2992
2993 if (co_opcache->optimized == 0) {
2994 /* Wasn't optimized before. */
2995 OPCACHE_STAT_GLOBAL_OPT();
2996 } else {
2997 OPCACHE_STAT_GLOBAL_MISS();
2998 }
2999
3000 co_opcache->optimized = 1;
3001 lg->globals_ver =
3002 ((PyDictObject *)f->f_globals)->ma_version_tag;
3003 lg->builtins_ver =
3004 ((PyDictObject *)f->f_builtins)->ma_version_tag;
3005 lg->ptr = v; /* borrowed */
3006 }
3007
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003008 Py_INCREF(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003009 }
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003010 else {
3011 /* Slow-path if globals or builtins is not a dict */
Victor Stinnerb4efc962015-11-20 09:24:02 +01003012
3013 /* namespace 1: globals */
Inada Naoki91234a12019-06-03 21:30:58 +09003014 name = GETITEM(names, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003015 v = PyObject_GetItem(f->f_globals, name);
3016 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003017 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003018 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003019 }
3020 _PyErr_Clear(tstate);
Victor Stinner60a1d3c2015-11-05 13:55:20 +01003021
Victor Stinnerb4efc962015-11-20 09:24:02 +01003022 /* namespace 2: builtins */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003023 v = PyObject_GetItem(f->f_builtins, name);
3024 if (v == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003025 if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003026 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003027 tstate, PyExc_NameError,
Ezio Melotti04a29552013-03-03 15:12:44 +02003028 NAME_ERROR_MSG, name);
Victor Stinner438a12d2019-05-24 17:01:38 +02003029 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003030 goto error;
Benjamin Peterson7d95e402012-04-23 11:24:50 -04003031 }
3032 }
3033 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003034 PUSH(v);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003035 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003036 }
Guido van Rossum681d79a1995-07-18 14:51:37 +00003037
Benjamin Petersonddd19492018-09-16 22:38:02 -07003038 case TARGET(DELETE_FAST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003039 PyObject *v = GETLOCAL(oparg);
3040 if (v != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003041 SETLOCAL(oparg, NULL);
3042 DISPATCH();
3043 }
3044 format_exc_check_arg(
Victor Stinner438a12d2019-05-24 17:01:38 +02003045 tstate, PyExc_UnboundLocalError,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003046 UNBOUNDLOCAL_ERROR_MSG,
3047 PyTuple_GetItem(co->co_varnames, oparg)
3048 );
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003049 goto error;
3050 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003051
Benjamin Petersonddd19492018-09-16 22:38:02 -07003052 case TARGET(DELETE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003053 PyObject *cell = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05003054 PyObject *oldobj = PyCell_GET(cell);
3055 if (oldobj != NULL) {
3056 PyCell_SET(cell, NULL);
3057 Py_DECREF(oldobj);
Benjamin Peterson00ebe2c2010-09-10 22:02:31 +00003058 DISPATCH();
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003059 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003060 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003061 goto error;
3062 }
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003063
Benjamin Petersonddd19492018-09-16 22:38:02 -07003064 case TARGET(LOAD_CLOSURE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003065 PyObject *cell = freevars[oparg];
3066 Py_INCREF(cell);
3067 PUSH(cell);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003068 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003069 }
Jeremy Hylton64949cb2001-01-25 20:06:59 +00003070
Benjamin Petersonddd19492018-09-16 22:38:02 -07003071 case TARGET(LOAD_CLASSDEREF): {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003072 PyObject *name, *value, *locals = f->f_locals;
Victor Stinnerd3dfd0e2013-05-16 23:48:01 +02003073 Py_ssize_t idx;
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003074 assert(locals);
3075 assert(oparg >= PyTuple_GET_SIZE(co->co_cellvars));
3076 idx = oparg - PyTuple_GET_SIZE(co->co_cellvars);
3077 assert(idx >= 0 && idx < PyTuple_GET_SIZE(co->co_freevars));
3078 name = PyTuple_GET_ITEM(co->co_freevars, idx);
3079 if (PyDict_CheckExact(locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003080 value = PyDict_GetItemWithError(locals, name);
3081 if (value != NULL) {
3082 Py_INCREF(value);
3083 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003084 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003085 goto error;
3086 }
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003087 }
3088 else {
3089 value = PyObject_GetItem(locals, name);
Victor Stinnere20310f2015-11-05 13:56:58 +01003090 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003091 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003092 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02003093 }
3094 _PyErr_Clear(tstate);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003095 }
3096 }
3097 if (!value) {
3098 PyObject *cell = freevars[oparg];
3099 value = PyCell_GET(cell);
3100 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003101 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003102 goto error;
3103 }
3104 Py_INCREF(value);
3105 }
3106 PUSH(value);
3107 DISPATCH();
3108 }
3109
Benjamin Petersonddd19492018-09-16 22:38:02 -07003110 case TARGET(LOAD_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003111 PyObject *cell = freevars[oparg];
3112 PyObject *value = PyCell_GET(cell);
3113 if (value == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003114 format_exc_unbound(tstate, co, oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003115 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003116 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003117 Py_INCREF(value);
3118 PUSH(value);
3119 DISPATCH();
3120 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003121
Benjamin Petersonddd19492018-09-16 22:38:02 -07003122 case TARGET(STORE_DEREF): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003123 PyObject *v = POP();
3124 PyObject *cell = freevars[oparg];
Raymond Hettingerb2b15432016-11-11 04:32:11 -08003125 PyObject *oldobj = PyCell_GET(cell);
3126 PyCell_SET(cell, v);
3127 Py_XDECREF(oldobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003128 DISPATCH();
3129 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003130
Benjamin Petersonddd19492018-09-16 22:38:02 -07003131 case TARGET(BUILD_STRING): {
Serhiy Storchakaea525a22016-09-06 22:07:53 +03003132 PyObject *str;
3133 PyObject *empty = PyUnicode_New(0, 0);
3134 if (empty == NULL) {
3135 goto error;
3136 }
3137 str = _PyUnicode_JoinArray(empty, stack_pointer - oparg, oparg);
3138 Py_DECREF(empty);
3139 if (str == NULL)
3140 goto error;
3141 while (--oparg >= 0) {
3142 PyObject *item = POP();
3143 Py_DECREF(item);
3144 }
3145 PUSH(str);
3146 DISPATCH();
3147 }
3148
Benjamin Petersonddd19492018-09-16 22:38:02 -07003149 case TARGET(BUILD_TUPLE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003150 PyObject *tup = PyTuple_New(oparg);
3151 if (tup == NULL)
3152 goto error;
3153 while (--oparg >= 0) {
3154 PyObject *item = POP();
3155 PyTuple_SET_ITEM(tup, oparg, item);
3156 }
3157 PUSH(tup);
3158 DISPATCH();
3159 }
3160
Benjamin Petersonddd19492018-09-16 22:38:02 -07003161 case TARGET(BUILD_LIST): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003162 PyObject *list = PyList_New(oparg);
3163 if (list == NULL)
3164 goto error;
3165 while (--oparg >= 0) {
3166 PyObject *item = POP();
3167 PyList_SET_ITEM(list, oparg, item);
3168 }
3169 PUSH(list);
3170 DISPATCH();
3171 }
3172
Mark Shannon13bc1392020-01-23 09:25:17 +00003173 case TARGET(LIST_TO_TUPLE): {
3174 PyObject *list = POP();
3175 PyObject *tuple = PyList_AsTuple(list);
3176 Py_DECREF(list);
3177 if (tuple == NULL) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003178 goto error;
Mark Shannon13bc1392020-01-23 09:25:17 +00003179 }
3180 PUSH(tuple);
3181 DISPATCH();
3182 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003183
Mark Shannon13bc1392020-01-23 09:25:17 +00003184 case TARGET(LIST_EXTEND): {
3185 PyObject *iterable = POP();
3186 PyObject *list = PEEK(oparg);
3187 PyObject *none_val = _PyList_Extend((PyListObject *)list, iterable);
3188 if (none_val == NULL) {
3189 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01003190 (Py_TYPE(iterable)->tp_iter == NULL && !PySequence_Check(iterable)))
Mark Shannon13bc1392020-01-23 09:25:17 +00003191 {
Victor Stinner61f4db82020-01-28 03:37:45 +01003192 _PyErr_Clear(tstate);
Mark Shannon13bc1392020-01-23 09:25:17 +00003193 _PyErr_Format(tstate, PyExc_TypeError,
3194 "Value after * must be an iterable, not %.200s",
3195 Py_TYPE(iterable)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003196 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003197 Py_DECREF(iterable);
3198 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003199 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003200 Py_DECREF(none_val);
3201 Py_DECREF(iterable);
3202 DISPATCH();
3203 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003204
Mark Shannon13bc1392020-01-23 09:25:17 +00003205 case TARGET(SET_UPDATE): {
3206 PyObject *iterable = POP();
3207 PyObject *set = PEEK(oparg);
3208 int err = _PySet_Update(set, iterable);
3209 Py_DECREF(iterable);
3210 if (err < 0) {
3211 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003212 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003213 DISPATCH();
3214 }
3215
Benjamin Petersonddd19492018-09-16 22:38:02 -07003216 case TARGET(BUILD_SET): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003217 PyObject *set = PySet_New(NULL);
3218 int err = 0;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003219 int i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003220 if (set == NULL)
3221 goto error;
Raymond Hettinger4c483ad2016-09-08 14:45:40 -07003222 for (i = oparg; i > 0; i--) {
3223 PyObject *item = PEEK(i);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003224 if (err == 0)
3225 err = PySet_Add(set, item);
3226 Py_DECREF(item);
3227 }
costypetrisor8ed317f2018-07-31 20:55:14 +00003228 STACK_SHRINK(oparg);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003229 if (err != 0) {
3230 Py_DECREF(set);
3231 goto error;
3232 }
3233 PUSH(set);
3234 DISPATCH();
3235 }
3236
Benjamin Petersonddd19492018-09-16 22:38:02 -07003237 case TARGET(BUILD_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003238 Py_ssize_t i;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003239 PyObject *map = _PyDict_NewPresized((Py_ssize_t)oparg);
3240 if (map == NULL)
3241 goto error;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003242 for (i = oparg; i > 0; i--) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003243 int err;
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003244 PyObject *key = PEEK(2*i);
3245 PyObject *value = PEEK(2*i - 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003246 err = PyDict_SetItem(map, key, value);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003247 if (err != 0) {
3248 Py_DECREF(map);
3249 goto error;
3250 }
3251 }
Benjamin Petersond5d77aa2015-07-05 10:37:25 -05003252
3253 while (oparg--) {
3254 Py_DECREF(POP());
3255 Py_DECREF(POP());
3256 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003257 PUSH(map);
3258 DISPATCH();
3259 }
3260
Benjamin Petersonddd19492018-09-16 22:38:02 -07003261 case TARGET(SETUP_ANNOTATIONS): {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003262 _Py_IDENTIFIER(__annotations__);
3263 int err;
3264 PyObject *ann_dict;
3265 if (f->f_locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003266 _PyErr_Format(tstate, PyExc_SystemError,
3267 "no locals found when setting up annotations");
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003268 goto error;
3269 }
3270 /* check if __annotations__ in locals()... */
3271 if (PyDict_CheckExact(f->f_locals)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003272 ann_dict = _PyDict_GetItemIdWithError(f->f_locals,
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003273 &PyId___annotations__);
3274 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003275 if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02003276 goto error;
3277 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003278 /* ...if not, create a new one */
3279 ann_dict = PyDict_New();
3280 if (ann_dict == NULL) {
3281 goto error;
3282 }
3283 err = _PyDict_SetItemId(f->f_locals,
3284 &PyId___annotations__, ann_dict);
3285 Py_DECREF(ann_dict);
3286 if (err != 0) {
3287 goto error;
3288 }
3289 }
3290 }
3291 else {
3292 /* do the same if locals() is not a dict */
3293 PyObject *ann_str = _PyUnicode_FromId(&PyId___annotations__);
3294 if (ann_str == NULL) {
Serhiy Storchaka4678b2f2016-11-08 23:13:36 +02003295 goto error;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003296 }
3297 ann_dict = PyObject_GetItem(f->f_locals, ann_str);
3298 if (ann_dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003299 if (!_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003300 goto error;
3301 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003302 _PyErr_Clear(tstate);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003303 ann_dict = PyDict_New();
3304 if (ann_dict == NULL) {
3305 goto error;
3306 }
3307 err = PyObject_SetItem(f->f_locals, ann_str, ann_dict);
3308 Py_DECREF(ann_dict);
3309 if (err != 0) {
3310 goto error;
3311 }
3312 }
3313 else {
3314 Py_DECREF(ann_dict);
3315 }
3316 }
3317 DISPATCH();
3318 }
3319
Benjamin Petersonddd19492018-09-16 22:38:02 -07003320 case TARGET(BUILD_CONST_KEY_MAP): {
Victor Stinner74319ae2016-08-25 00:04:09 +02003321 Py_ssize_t i;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003322 PyObject *map;
3323 PyObject *keys = TOP();
3324 if (!PyTuple_CheckExact(keys) ||
3325 PyTuple_GET_SIZE(keys) != (Py_ssize_t)oparg) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003326 _PyErr_SetString(tstate, PyExc_SystemError,
3327 "bad BUILD_CONST_KEY_MAP keys argument");
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003328 goto error;
3329 }
3330 map = _PyDict_NewPresized((Py_ssize_t)oparg);
3331 if (map == NULL) {
3332 goto error;
3333 }
3334 for (i = oparg; i > 0; i--) {
3335 int err;
3336 PyObject *key = PyTuple_GET_ITEM(keys, oparg - i);
3337 PyObject *value = PEEK(i + 1);
3338 err = PyDict_SetItem(map, key, value);
3339 if (err != 0) {
3340 Py_DECREF(map);
3341 goto error;
3342 }
3343 }
3344
3345 Py_DECREF(POP());
3346 while (oparg--) {
3347 Py_DECREF(POP());
3348 }
3349 PUSH(map);
3350 DISPATCH();
3351 }
3352
Mark Shannon8a4cd702020-01-27 09:57:45 +00003353 case TARGET(DICT_UPDATE): {
3354 PyObject *update = POP();
3355 PyObject *dict = PEEK(oparg);
3356 if (PyDict_Update(dict, update) < 0) {
3357 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
3358 _PyErr_Format(tstate, PyExc_TypeError,
3359 "'%.200s' object is not a mapping",
Victor Stinnera102ed72020-02-07 02:24:48 +01003360 Py_TYPE(update)->tp_name);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003361 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003362 Py_DECREF(update);
3363 goto error;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003364 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003365 Py_DECREF(update);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003366 DISPATCH();
3367 }
3368
Mark Shannon8a4cd702020-01-27 09:57:45 +00003369 case TARGET(DICT_MERGE): {
3370 PyObject *update = POP();
3371 PyObject *dict = PEEK(oparg);
3372
3373 if (_PyDict_MergeEx(dict, update, 2) < 0) {
3374 format_kwargs_error(tstate, PEEK(2 + oparg), update);
3375 Py_DECREF(update);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003376 goto error;
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003377 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003378 Py_DECREF(update);
Brandt Bucherf185a732019-09-28 17:12:49 -07003379 PREDICT(CALL_FUNCTION_EX);
Serhiy Storchakae036ef82016-10-02 11:06:43 +03003380 DISPATCH();
3381 }
3382
Benjamin Petersonddd19492018-09-16 22:38:02 -07003383 case TARGET(MAP_ADD): {
Jörn Heisslerc8a35412019-06-22 16:40:55 +02003384 PyObject *value = TOP();
3385 PyObject *key = SECOND();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003386 PyObject *map;
3387 int err;
costypetrisor8ed317f2018-07-31 20:55:14 +00003388 STACK_SHRINK(2);
Raymond Hettinger41862222016-10-15 19:03:06 -07003389 map = PEEK(oparg); /* dict */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003390 assert(PyDict_CheckExact(map));
Martin Panter95f53c12016-07-18 08:23:26 +00003391 err = PyDict_SetItem(map, key, value); /* map[key] = value */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003392 Py_DECREF(value);
3393 Py_DECREF(key);
3394 if (err != 0)
3395 goto error;
3396 PREDICT(JUMP_ABSOLUTE);
3397 DISPATCH();
3398 }
3399
Benjamin Petersonddd19492018-09-16 22:38:02 -07003400 case TARGET(LOAD_ATTR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003401 PyObject *name = GETITEM(names, oparg);
3402 PyObject *owner = TOP();
Pablo Galindo109826c2020-10-20 06:22:44 +01003403
3404 PyTypeObject *type = Py_TYPE(owner);
3405 PyObject *res;
3406 PyObject **dictptr;
3407 PyObject *dict;
3408 _PyOpCodeOpt_LoadAttr *la;
3409
3410 OPCACHE_STAT_ATTR_TOTAL();
3411
3412 OPCACHE_CHECK();
3413 if (co_opcache != NULL && PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
3414 {
3415 if (co_opcache->optimized > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003416 // Fast path -- cache hit makes LOAD_ATTR ~30% faster.
Pablo Galindo109826c2020-10-20 06:22:44 +01003417 la = &co_opcache->u.la;
3418 if (la->type == type && la->tp_version_tag == type->tp_version_tag)
3419 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003420 // Hint >= 0 is a dict index; hint == -1 is a dict miss.
3421 // Hint < -1 is an inverted slot offset: offset is strictly > 0,
3422 // so ~offset is strictly < -1 (assuming 2's complement).
3423 if (la->hint < -1) {
3424 // Even faster path -- slot hint.
3425 Py_ssize_t offset = ~la->hint;
3426 // fprintf(stderr, "Using hint for offset %zd\n", offset);
3427 char *addr = (char *)owner + offset;
3428 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003429 if (res != NULL) {
Pablo Galindo109826c2020-10-20 06:22:44 +01003430 Py_INCREF(res);
3431 SET_TOP(res);
3432 Py_DECREF(owner);
Pablo Galindo109826c2020-10-20 06:22:44 +01003433 DISPATCH();
Pablo Galindo109826c2020-10-20 06:22:44 +01003434 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003435 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
3436 // Don't DEOPT, since the slot is still there.
Pablo Galindo109826c2020-10-20 06:22:44 +01003437 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003438 // Fast path for dict.
3439 assert(type->tp_dict != NULL);
3440 assert(type->tp_dictoffset > 0);
3441
3442 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3443 dict = *dictptr;
3444 if (dict != NULL && PyDict_CheckExact(dict)) {
3445 Py_ssize_t hint = la->hint;
3446 Py_INCREF(dict);
3447 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003448 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003449 la->hint = _PyDict_GetItemHint((PyDictObject*)dict, name, hint, &res);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003450 if (res != NULL) {
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003451 assert(la->hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003452 if (la->hint == hint && hint >= 0) {
3453 // Our hint has helped -- cache hit.
3454 OPCACHE_STAT_ATTR_HIT();
3455 } else {
3456 // The hint we provided didn't work.
3457 // Maybe next time?
3458 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3459 }
3460
3461 Py_INCREF(res);
3462 SET_TOP(res);
3463 Py_DECREF(owner);
3464 Py_DECREF(dict);
3465 DISPATCH();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003466 }
3467 else {
3468 _PyErr_Clear(tstate);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003469 // This attribute can be missing sometimes;
3470 // we don't want to optimize this lookup.
3471 OPCACHE_DEOPT_LOAD_ATTR();
3472 Py_DECREF(dict);
3473 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003474 }
3475 else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003476 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
3477 OPCACHE_DEOPT_LOAD_ATTR();
3478 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003479 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003480 }
3481 else {
Pablo Galindo109826c2020-10-20 06:22:44 +01003482 // The type of the object has either been updated,
3483 // or is different. Maybe it will stabilize?
3484 OPCACHE_MAYBE_DEOPT_LOAD_ATTR();
3485 }
Pablo Galindo109826c2020-10-20 06:22:44 +01003486 OPCACHE_STAT_ATTR_MISS();
3487 }
3488
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003489 if (co_opcache != NULL && // co_opcache can be NULL after a DEOPT() call.
Pablo Galindo109826c2020-10-20 06:22:44 +01003490 type->tp_getattro == PyObject_GenericGetAttr)
3491 {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003492 if (type->tp_dict == NULL) {
3493 if (PyType_Ready(type) < 0) {
3494 Py_DECREF(owner);
3495 SET_TOP(NULL);
3496 goto error;
Pablo Galindo109826c2020-10-20 06:22:44 +01003497 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003498 }
3499 PyObject *descr = _PyType_Lookup(type, name);
3500 if (descr != NULL) {
3501 // We found an attribute with a data-like descriptor.
3502 PyTypeObject *dtype = Py_TYPE(descr);
3503 if (dtype == &PyMemberDescr_Type) { // It's a slot
3504 PyMemberDescrObject *member = (PyMemberDescrObject *)descr;
3505 struct PyMemberDef *dmem = member->d_member;
3506 if (dmem->type == T_OBJECT_EX) {
3507 Py_ssize_t offset = dmem->offset;
3508 assert(offset > 0); // 0 would be confused with dict hint == -1 (miss).
Pablo Galindo109826c2020-10-20 06:22:44 +01003509
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003510 if (co_opcache->optimized == 0) {
3511 // First time we optimize this opcode.
3512 OPCACHE_STAT_ATTR_OPT();
3513 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3514 // fprintf(stderr, "Setting hint for %s, offset %zd\n", dmem->name, offset);
3515 }
3516
3517 la = &co_opcache->u.la;
3518 la->type = type;
3519 la->tp_version_tag = type->tp_version_tag;
3520 la->hint = ~offset;
3521
3522 char *addr = (char *)owner + offset;
3523 res = *(PyObject **)addr;
Pablo Galindo109826c2020-10-20 06:22:44 +01003524 if (res != NULL) {
3525 Py_INCREF(res);
Pablo Galindo109826c2020-10-20 06:22:44 +01003526 Py_DECREF(owner);
3527 SET_TOP(res);
3528
Pablo Galindo109826c2020-10-20 06:22:44 +01003529 DISPATCH();
3530 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003531 // Else slot is NULL. Fall through to slow path to raise AttributeError(name).
Pablo Galindo109826c2020-10-20 06:22:44 +01003532 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003533 // Else it's a slot of a different type. We don't handle those.
3534 }
3535 // Else it's some other kind of descriptor that we don't handle.
3536 OPCACHE_DEOPT_LOAD_ATTR();
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003537 }
3538 else if (type->tp_dictoffset > 0) {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003539 // We found an instance with a __dict__.
3540 dictptr = (PyObject **) ((char *)owner + type->tp_dictoffset);
3541 dict = *dictptr;
3542
3543 if (dict != NULL && PyDict_CheckExact(dict)) {
3544 Py_INCREF(dict);
3545 res = NULL;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003546 assert(!_PyErr_Occurred(tstate));
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003547 Py_ssize_t hint = _PyDict_GetItemHint((PyDictObject*)dict, name, -1, &res);
3548 if (res != NULL) {
3549 Py_INCREF(res);
3550 Py_DECREF(dict);
3551 Py_DECREF(owner);
3552 SET_TOP(res);
3553
3554 if (co_opcache->optimized == 0) {
3555 // First time we optimize this opcode.
3556 OPCACHE_STAT_ATTR_OPT();
3557 co_opcache->optimized = OPCODE_CACHE_MAX_TRIES;
3558 }
3559
3560 la = &co_opcache->u.la;
3561 la->type = type;
3562 la->tp_version_tag = type->tp_version_tag;
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003563 assert(hint >= 0);
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003564 la->hint = hint;
3565
3566 DISPATCH();
3567 }
Victor Stinnerd5fc9982021-02-21 12:02:04 +01003568 else {
3569 _PyErr_Clear(tstate);
3570 }
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003571 Py_DECREF(dict);
Pablo Galindo109826c2020-10-20 06:22:44 +01003572 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003573 // There is no dict, or __dict__ doesn't satisfy PyDict_CheckExact.
Pablo Galindo109826c2020-10-20 06:22:44 +01003574 OPCACHE_DEOPT_LOAD_ATTR();
3575 }
3576 } else {
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003577 // The object's class does not have a tp_dictoffset we can use.
Pablo Galindo109826c2020-10-20 06:22:44 +01003578 OPCACHE_DEOPT_LOAD_ATTR();
3579 }
3580 } else if (type->tp_getattro != PyObject_GenericGetAttr) {
3581 OPCACHE_DEOPT_LOAD_ATTR();
3582 }
3583 }
3584
Guido van Rossum5c5a9382021-01-29 18:02:29 -08003585 // Slow path.
Pablo Galindo109826c2020-10-20 06:22:44 +01003586 res = PyObject_GetAttr(owner, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003587 Py_DECREF(owner);
3588 SET_TOP(res);
3589 if (res == NULL)
3590 goto error;
3591 DISPATCH();
3592 }
3593
Benjamin Petersonddd19492018-09-16 22:38:02 -07003594 case TARGET(COMPARE_OP): {
Mark Shannon9af0e472020-01-14 10:12:45 +00003595 assert(oparg <= Py_GE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003596 PyObject *right = POP();
3597 PyObject *left = TOP();
Mark Shannon9af0e472020-01-14 10:12:45 +00003598 PyObject *res = PyObject_RichCompare(left, right, oparg);
3599 SET_TOP(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003600 Py_DECREF(left);
3601 Py_DECREF(right);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003602 if (res == NULL)
3603 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 PREDICT(POP_JUMP_IF_FALSE);
3605 PREDICT(POP_JUMP_IF_TRUE);
3606 DISPATCH();
Victor Stinner3c1e4812012-03-26 22:10:51 +02003607 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003608
Mark Shannon9af0e472020-01-14 10:12:45 +00003609 case TARGET(IS_OP): {
3610 PyObject *right = POP();
3611 PyObject *left = TOP();
3612 int res = (left == right)^oparg;
3613 PyObject *b = res ? Py_True : Py_False;
3614 Py_INCREF(b);
3615 SET_TOP(b);
3616 Py_DECREF(left);
3617 Py_DECREF(right);
3618 PREDICT(POP_JUMP_IF_FALSE);
3619 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003620 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003621 }
3622
3623 case TARGET(CONTAINS_OP): {
3624 PyObject *right = POP();
3625 PyObject *left = POP();
3626 int res = PySequence_Contains(right, left);
3627 Py_DECREF(left);
3628 Py_DECREF(right);
3629 if (res < 0) {
3630 goto error;
3631 }
3632 PyObject *b = (res^oparg) ? Py_True : Py_False;
3633 Py_INCREF(b);
3634 PUSH(b);
3635 PREDICT(POP_JUMP_IF_FALSE);
3636 PREDICT(POP_JUMP_IF_TRUE);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003637 DISPATCH();
Mark Shannon9af0e472020-01-14 10:12:45 +00003638 }
3639
3640#define CANNOT_CATCH_MSG "catching classes that do not inherit from "\
3641 "BaseException is not allowed"
3642
3643 case TARGET(JUMP_IF_NOT_EXC_MATCH): {
3644 PyObject *right = POP();
3645 PyObject *left = POP();
3646 if (PyTuple_Check(right)) {
3647 Py_ssize_t i, length;
3648 length = PyTuple_GET_SIZE(right);
3649 for (i = 0; i < length; i++) {
3650 PyObject *exc = PyTuple_GET_ITEM(right, i);
3651 if (!PyExceptionClass_Check(exc)) {
3652 _PyErr_SetString(tstate, PyExc_TypeError,
3653 CANNOT_CATCH_MSG);
3654 Py_DECREF(left);
3655 Py_DECREF(right);
3656 goto error;
3657 }
3658 }
3659 }
3660 else {
3661 if (!PyExceptionClass_Check(right)) {
3662 _PyErr_SetString(tstate, PyExc_TypeError,
3663 CANNOT_CATCH_MSG);
3664 Py_DECREF(left);
3665 Py_DECREF(right);
3666 goto error;
3667 }
3668 }
3669 int res = PyErr_GivenExceptionMatches(left, right);
3670 Py_DECREF(left);
3671 Py_DECREF(right);
3672 if (res > 0) {
3673 /* Exception matches -- Do nothing */;
3674 }
3675 else if (res == 0) {
3676 JUMPTO(oparg);
3677 }
3678 else {
3679 goto error;
3680 }
3681 DISPATCH();
3682 }
3683
Benjamin Petersonddd19492018-09-16 22:38:02 -07003684 case TARGET(IMPORT_NAME): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003685 PyObject *name = GETITEM(names, oparg);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003686 PyObject *fromlist = POP();
3687 PyObject *level = TOP();
3688 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003689 res = import_name(tstate, f, name, fromlist, level);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03003690 Py_DECREF(level);
3691 Py_DECREF(fromlist);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003692 SET_TOP(res);
3693 if (res == NULL)
3694 goto error;
3695 DISPATCH();
3696 }
3697
Benjamin Petersonddd19492018-09-16 22:38:02 -07003698 case TARGET(IMPORT_STAR): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003699 PyObject *from = POP(), *locals;
3700 int err;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003701 if (PyFrame_FastToLocalsWithError(f) < 0) {
3702 Py_DECREF(from);
Victor Stinner41bb43a2013-10-29 01:19:37 +01003703 goto error;
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003704 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01003705
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003706 locals = f->f_locals;
3707 if (locals == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02003708 _PyErr_SetString(tstate, PyExc_SystemError,
3709 "no locals found during 'import *'");
Matthias Bussonnier160edb42017-02-25 21:58:05 -08003710 Py_DECREF(from);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003711 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003712 }
Victor Stinner438a12d2019-05-24 17:01:38 +02003713 err = import_all_from(tstate, locals, from);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003714 PyFrame_LocalsToFast(f, 0);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003715 Py_DECREF(from);
3716 if (err != 0)
3717 goto error;
3718 DISPATCH();
3719 }
Guido van Rossum25831651993-05-19 14:50:45 +00003720
Benjamin Petersonddd19492018-09-16 22:38:02 -07003721 case TARGET(IMPORT_FROM): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003722 PyObject *name = GETITEM(names, oparg);
3723 PyObject *from = TOP();
3724 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02003725 res = import_from(tstate, from, name);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003726 PUSH(res);
3727 if (res == NULL)
3728 goto error;
3729 DISPATCH();
3730 }
Thomas Wouters52152252000-08-17 22:55:00 +00003731
Benjamin Petersonddd19492018-09-16 22:38:02 -07003732 case TARGET(JUMP_FORWARD): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003733 JUMPBY(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003734 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003735 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003736
Benjamin Petersonddd19492018-09-16 22:38:02 -07003737 case TARGET(POP_JUMP_IF_FALSE): {
3738 PREDICTED(POP_JUMP_IF_FALSE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003739 PyObject *cond = POP();
3740 int err;
3741 if (cond == Py_True) {
3742 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003743 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003744 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003745 if (cond == Py_False) {
3746 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003747 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003748 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003749 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003750 err = PyObject_IsTrue(cond);
3751 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003752 if (err > 0)
Adrian Wielgosik50c28502017-06-23 13:35:41 -07003753 ;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003754 else if (err == 0)
3755 JUMPTO(oparg);
3756 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003757 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003758 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003759 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003760
Benjamin Petersonddd19492018-09-16 22:38:02 -07003761 case TARGET(POP_JUMP_IF_TRUE): {
3762 PREDICTED(POP_JUMP_IF_TRUE);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003763 PyObject *cond = POP();
3764 int err;
3765 if (cond == Py_False) {
3766 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003767 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003768 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003769 if (cond == Py_True) {
3770 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003771 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003772 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003773 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003774 err = PyObject_IsTrue(cond);
3775 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003776 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003777 JUMPTO(oparg);
3778 }
3779 else if (err == 0)
3780 ;
3781 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003782 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003783 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003784 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003785
Benjamin Petersonddd19492018-09-16 22:38:02 -07003786 case TARGET(JUMP_IF_FALSE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003787 PyObject *cond = TOP();
3788 int err;
3789 if (cond == Py_True) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003790 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003791 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003792 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003794 if (cond == Py_False) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003795 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003796 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003797 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003798 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003799 if (err > 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003800 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003801 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003802 }
3803 else if (err == 0)
3804 JUMPTO(oparg);
3805 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003806 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003807 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003808 }
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003809
Benjamin Petersonddd19492018-09-16 22:38:02 -07003810 case TARGET(JUMP_IF_TRUE_OR_POP): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003811 PyObject *cond = TOP();
3812 int err;
3813 if (cond == Py_False) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003814 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003815 Py_DECREF(cond);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003816 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003817 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003818 if (cond == Py_True) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003819 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003820 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003821 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003822 err = PyObject_IsTrue(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003823 if (err > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003824 JUMPTO(oparg);
3825 }
3826 else if (err == 0) {
costypetrisor8ed317f2018-07-31 20:55:14 +00003827 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003828 Py_DECREF(cond);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003829 }
3830 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003831 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003832 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003833 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003834
Benjamin Petersonddd19492018-09-16 22:38:02 -07003835 case TARGET(JUMP_ABSOLUTE): {
3836 PREDICTED(JUMP_ABSOLUTE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003837 JUMPTO(oparg);
Mark Shannon4958f5d2021-03-24 17:56:12 +00003838 CHECK_EVAL_BREAKER();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003839 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04003840 }
Guido van Rossumac7be682001-01-17 15:42:30 +00003841
Brandt Bucher145bf262021-02-26 14:51:55 -08003842 case TARGET(GET_LEN): {
3843 // PUSH(len(TOS))
3844 Py_ssize_t len_i = PyObject_Length(TOP());
3845 if (len_i < 0) {
3846 goto error;
3847 }
3848 PyObject *len_o = PyLong_FromSsize_t(len_i);
3849 if (len_o == NULL) {
3850 goto error;
3851 }
3852 PUSH(len_o);
3853 DISPATCH();
3854 }
3855
3856 case TARGET(MATCH_CLASS): {
3857 // Pop TOS. On success, set TOS to True and TOS1 to a tuple of
3858 // attributes. On failure, set TOS to False.
3859 PyObject *names = POP();
3860 PyObject *type = TOP();
3861 PyObject *subject = SECOND();
3862 assert(PyTuple_CheckExact(names));
3863 PyObject *attrs = match_class(tstate, subject, type, oparg, names);
3864 Py_DECREF(names);
3865 if (attrs) {
3866 // Success!
3867 assert(PyTuple_CheckExact(attrs));
3868 Py_DECREF(subject);
3869 SET_SECOND(attrs);
3870 }
3871 else if (_PyErr_Occurred(tstate)) {
3872 goto error;
3873 }
3874 Py_DECREF(type);
3875 SET_TOP(PyBool_FromLong(!!attrs));
3876 DISPATCH();
3877 }
3878
3879 case TARGET(MATCH_MAPPING): {
3880 // PUSH(isinstance(TOS, _collections_abc.Mapping))
3881 PyObject *subject = TOP();
3882 // Fast path for dicts:
3883 if (PyDict_Check(subject)) {
3884 Py_INCREF(Py_True);
3885 PUSH(Py_True);
3886 DISPATCH();
3887 }
3888 // Lazily import _collections_abc.Mapping, and keep it handy on the
3889 // PyInterpreterState struct (it gets cleaned up at exit):
3890 PyInterpreterState *interp = PyInterpreterState_Get();
3891 if (interp->map_abc == NULL) {
3892 PyObject *abc = PyImport_ImportModule("_collections_abc");
3893 if (abc == NULL) {
3894 goto error;
3895 }
3896 interp->map_abc = PyObject_GetAttrString(abc, "Mapping");
3897 if (interp->map_abc == NULL) {
3898 goto error;
3899 }
3900 }
3901 int match = PyObject_IsInstance(subject, interp->map_abc);
3902 if (match < 0) {
3903 goto error;
3904 }
3905 PUSH(PyBool_FromLong(match));
3906 DISPATCH();
3907 }
3908
3909 case TARGET(MATCH_SEQUENCE): {
3910 // PUSH(not isinstance(TOS, (bytearray, bytes, str))
3911 // and isinstance(TOS, _collections_abc.Sequence))
3912 PyObject *subject = TOP();
3913 // Fast path for lists and tuples:
3914 if (PyType_FastSubclass(Py_TYPE(subject),
3915 Py_TPFLAGS_LIST_SUBCLASS |
3916 Py_TPFLAGS_TUPLE_SUBCLASS))
3917 {
3918 Py_INCREF(Py_True);
3919 PUSH(Py_True);
3920 DISPATCH();
3921 }
3922 // Bail on some possible Sequences that we intentionally exclude:
3923 if (PyType_FastSubclass(Py_TYPE(subject),
3924 Py_TPFLAGS_BYTES_SUBCLASS |
3925 Py_TPFLAGS_UNICODE_SUBCLASS) ||
3926 PyByteArray_Check(subject))
3927 {
3928 Py_INCREF(Py_False);
3929 PUSH(Py_False);
3930 DISPATCH();
3931 }
3932 // Lazily import _collections_abc.Sequence, and keep it handy on the
3933 // PyInterpreterState struct (it gets cleaned up at exit):
3934 PyInterpreterState *interp = PyInterpreterState_Get();
3935 if (interp->seq_abc == NULL) {
3936 PyObject *abc = PyImport_ImportModule("_collections_abc");
3937 if (abc == NULL) {
3938 goto error;
3939 }
3940 interp->seq_abc = PyObject_GetAttrString(abc, "Sequence");
3941 if (interp->seq_abc == NULL) {
3942 goto error;
3943 }
3944 }
3945 int match = PyObject_IsInstance(subject, interp->seq_abc);
3946 if (match < 0) {
3947 goto error;
3948 }
3949 PUSH(PyBool_FromLong(match));
3950 DISPATCH();
3951 }
3952
3953 case TARGET(MATCH_KEYS): {
3954 // On successful match for all keys, PUSH(values) and PUSH(True).
3955 // Otherwise, PUSH(None) and PUSH(False).
3956 PyObject *keys = TOP();
3957 PyObject *subject = SECOND();
3958 PyObject *values_or_none = match_keys(tstate, subject, keys);
3959 if (values_or_none == NULL) {
3960 goto error;
3961 }
3962 PUSH(values_or_none);
3963 if (values_or_none == Py_None) {
3964 Py_INCREF(Py_False);
3965 PUSH(Py_False);
3966 DISPATCH();
3967 }
3968 assert(PyTuple_CheckExact(values_or_none));
3969 Py_INCREF(Py_True);
3970 PUSH(Py_True);
3971 DISPATCH();
3972 }
3973
3974 case TARGET(COPY_DICT_WITHOUT_KEYS): {
3975 // rest = dict(TOS1)
3976 // for key in TOS:
3977 // del rest[key]
3978 // SET_TOP(rest)
3979 PyObject *keys = TOP();
3980 PyObject *subject = SECOND();
3981 PyObject *rest = PyDict_New();
3982 if (rest == NULL || PyDict_Update(rest, subject)) {
3983 Py_XDECREF(rest);
3984 goto error;
3985 }
3986 // This may seem a bit inefficient, but keys is rarely big enough to
3987 // actually impact runtime.
3988 assert(PyTuple_CheckExact(keys));
3989 for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(keys); i++) {
3990 if (PyDict_DelItem(rest, PyTuple_GET_ITEM(keys, i))) {
3991 Py_DECREF(rest);
3992 goto error;
3993 }
3994 }
3995 Py_DECREF(keys);
3996 SET_TOP(rest);
3997 DISPATCH();
3998 }
3999
Benjamin Petersonddd19492018-09-16 22:38:02 -07004000 case TARGET(GET_ITER): {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004001 /* before: [obj]; after [getiter(obj)] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004002 PyObject *iterable = TOP();
Yury Selivanov5376ba92015-06-22 12:19:30 -04004003 PyObject *iter = PyObject_GetIter(iterable);
4004 Py_DECREF(iterable);
4005 SET_TOP(iter);
4006 if (iter == NULL)
4007 goto error;
4008 PREDICT(FOR_ITER);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004009 PREDICT(CALL_FUNCTION);
Yury Selivanov5376ba92015-06-22 12:19:30 -04004010 DISPATCH();
4011 }
4012
Benjamin Petersonddd19492018-09-16 22:38:02 -07004013 case TARGET(GET_YIELD_FROM_ITER): {
Yury Selivanov5376ba92015-06-22 12:19:30 -04004014 /* before: [obj]; after [getiter(obj)] */
4015 PyObject *iterable = TOP();
Yury Selivanov75445082015-05-11 22:57:16 -04004016 PyObject *iter;
Yury Selivanov5376ba92015-06-22 12:19:30 -04004017 if (PyCoro_CheckExact(iterable)) {
4018 /* `iterable` is a coroutine */
4019 if (!(co->co_flags & (CO_COROUTINE | CO_ITERABLE_COROUTINE))) {
4020 /* and it is used in a 'yield from' expression of a
4021 regular generator. */
4022 Py_DECREF(iterable);
4023 SET_TOP(NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004024 _PyErr_SetString(tstate, PyExc_TypeError,
4025 "cannot 'yield from' a coroutine object "
4026 "in a non-coroutine generator");
Yury Selivanov5376ba92015-06-22 12:19:30 -04004027 goto error;
4028 }
4029 }
4030 else if (!PyGen_CheckExact(iterable)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004031 /* `iterable` is not a generator. */
4032 iter = PyObject_GetIter(iterable);
4033 Py_DECREF(iterable);
4034 SET_TOP(iter);
4035 if (iter == NULL)
4036 goto error;
4037 }
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004038 PREDICT(LOAD_CONST);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004039 DISPATCH();
4040 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004041
Benjamin Petersonddd19492018-09-16 22:38:02 -07004042 case TARGET(FOR_ITER): {
4043 PREDICTED(FOR_ITER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004044 /* before: [iter]; after: [iter, iter()] *or* [] */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004045 PyObject *iter = TOP();
Victor Stinnera102ed72020-02-07 02:24:48 +01004046 PyObject *next = (*Py_TYPE(iter)->tp_iternext)(iter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004047 if (next != NULL) {
4048 PUSH(next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004049 PREDICT(STORE_FAST);
4050 PREDICT(UNPACK_SEQUENCE);
4051 DISPATCH();
4052 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004053 if (_PyErr_Occurred(tstate)) {
4054 if (!_PyErr_ExceptionMatches(tstate, PyExc_StopIteration)) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004055 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004056 }
4057 else if (tstate->c_tracefunc != NULL) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004058 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj, tstate, f, &trace_info);
Victor Stinner438a12d2019-05-24 17:01:38 +02004059 }
4060 _PyErr_Clear(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004061 }
4062 /* iterator ended normally */
costypetrisor8ed317f2018-07-31 20:55:14 +00004063 STACK_SHRINK(1);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004064 Py_DECREF(iter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004065 JUMPBY(oparg);
4066 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004067 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00004068
Benjamin Petersonddd19492018-09-16 22:38:02 -07004069 case TARGET(SETUP_FINALLY): {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004070 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004071 STACK_LEVEL());
4072 DISPATCH();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004073 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004074
Benjamin Petersonddd19492018-09-16 22:38:02 -07004075 case TARGET(BEFORE_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004076 _Py_IDENTIFIER(__aenter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004077 _Py_IDENTIFIER(__aexit__);
Yury Selivanov75445082015-05-11 22:57:16 -04004078 PyObject *mgr = TOP();
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004079 PyObject *enter = special_lookup(tstate, mgr, &PyId___aenter__);
Yury Selivanov75445082015-05-11 22:57:16 -04004080 PyObject *res;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004081 if (enter == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04004082 goto error;
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004083 }
4084 PyObject *exit = special_lookup(tstate, mgr, &PyId___aexit__);
4085 if (exit == NULL) {
4086 Py_DECREF(enter);
4087 goto error;
4088 }
Yury Selivanov75445082015-05-11 22:57:16 -04004089 SET_TOP(exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004090 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004091 res = _PyObject_CallNoArg(enter);
Yury Selivanov75445082015-05-11 22:57:16 -04004092 Py_DECREF(enter);
4093 if (res == NULL)
4094 goto error;
4095 PUSH(res);
Serhiy Storchakada9c5132016-06-27 18:58:57 +03004096 PREDICT(GET_AWAITABLE);
Yury Selivanov75445082015-05-11 22:57:16 -04004097 DISPATCH();
4098 }
4099
Benjamin Petersonddd19492018-09-16 22:38:02 -07004100 case TARGET(SETUP_ASYNC_WITH): {
Yury Selivanov75445082015-05-11 22:57:16 -04004101 PyObject *res = POP();
4102 /* Setup the finally block before pushing the result
4103 of __aenter__ on the stack. */
4104 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4105 STACK_LEVEL());
4106 PUSH(res);
4107 DISPATCH();
4108 }
4109
Benjamin Petersonddd19492018-09-16 22:38:02 -07004110 case TARGET(SETUP_WITH): {
Benjamin Petersonce798522012-01-22 11:24:29 -05004111 _Py_IDENTIFIER(__enter__);
Géry Ogam1d1b97a2020-01-14 12:58:29 +01004112 _Py_IDENTIFIER(__exit__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004113 PyObject *mgr = TOP();
Victor Stinner438a12d2019-05-24 17:01:38 +02004114 PyObject *enter = special_lookup(tstate, mgr, &PyId___enter__);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004115 PyObject *res;
Victor Stinner438a12d2019-05-24 17:01:38 +02004116 if (enter == NULL) {
Raymond Hettingera3fec152016-11-21 17:24:23 -08004117 goto error;
Victor Stinner438a12d2019-05-24 17:01:38 +02004118 }
4119 PyObject *exit = special_lookup(tstate, mgr, &PyId___exit__);
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004120 if (exit == NULL) {
4121 Py_DECREF(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004122 goto error;
Raymond Hettinger64e2f9a2016-11-22 11:50:40 -08004123 }
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004124 SET_TOP(exit);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004125 Py_DECREF(mgr);
Victor Stinnerf17c3de2016-12-06 18:46:19 +01004126 res = _PyObject_CallNoArg(enter);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004127 Py_DECREF(enter);
4128 if (res == NULL)
4129 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004130 /* Setup the finally block before pushing the result
4131 of __enter__ on the stack. */
4132 PyFrame_BlockSetup(f, SETUP_FINALLY, INSTR_OFFSET() + oparg,
4133 STACK_LEVEL());
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004134
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004135 PUSH(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004136 DISPATCH();
4137 }
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004138
Mark Shannonfee55262019-11-21 09:11:43 +00004139 case TARGET(WITH_EXCEPT_START): {
4140 /* At the top of the stack are 7 values:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004141 - (TOP, SECOND, THIRD) = exc_info()
Mark Shannonfee55262019-11-21 09:11:43 +00004142 - (FOURTH, FIFTH, SIXTH) = previous exception for EXCEPT_HANDLER
4143 - SEVENTH: the context.__exit__ bound method
4144 We call SEVENTH(TOP, SECOND, THIRD).
4145 Then we push again the TOP exception and the __exit__
4146 return value.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004147 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 PyObject *exit_func;
Victor Stinner842cfff2016-12-01 14:45:31 +01004149 PyObject *exc, *val, *tb, *res;
4150
Victor Stinner842cfff2016-12-01 14:45:31 +01004151 exc = TOP();
Mark Shannonfee55262019-11-21 09:11:43 +00004152 val = SECOND();
4153 tb = THIRD();
4154 assert(exc != Py_None);
4155 assert(!PyLong_Check(exc));
4156 exit_func = PEEK(7);
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004157 PyObject *stack[4] = {NULL, exc, val, tb};
Petr Viktorinffd97532020-02-11 17:46:57 +01004158 res = PyObject_Vectorcall(exit_func, stack + 1,
Jeroen Demeyer469d1a72019-07-03 12:52:21 +02004159 3 | PY_VECTORCALL_ARGUMENTS_OFFSET, NULL);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004160 if (res == NULL)
4161 goto error;
Amaury Forgeot d'Arc10b24e82008-12-10 23:49:33 +00004162
Yury Selivanov75445082015-05-11 22:57:16 -04004163 PUSH(res);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004164 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004165 }
Guido van Rossumc2e20742006-02-27 22:32:47 +00004166
Benjamin Petersonddd19492018-09-16 22:38:02 -07004167 case TARGET(LOAD_METHOD): {
Andreyb021ba52019-04-29 14:33:26 +10004168 /* Designed to work in tandem with CALL_METHOD. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004169 PyObject *name = GETITEM(names, oparg);
4170 PyObject *obj = TOP();
4171 PyObject *meth = NULL;
4172
4173 int meth_found = _PyObject_GetMethod(obj, name, &meth);
4174
Yury Selivanovf2392132016-12-13 19:03:51 -05004175 if (meth == NULL) {
4176 /* Most likely attribute wasn't found. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004177 goto error;
4178 }
4179
4180 if (meth_found) {
INADA Naoki015bce62017-01-16 17:23:30 +09004181 /* We can bypass temporary bound method object.
4182 meth is unbound method and obj is self.
Victor Stinnera8cb5152017-01-18 14:12:51 +01004183
INADA Naoki015bce62017-01-16 17:23:30 +09004184 meth | self | arg1 | ... | argN
4185 */
4186 SET_TOP(meth);
4187 PUSH(obj); // self
Yury Selivanovf2392132016-12-13 19:03:51 -05004188 }
4189 else {
INADA Naoki015bce62017-01-16 17:23:30 +09004190 /* meth is not an unbound method (but a regular attr, or
4191 something was returned by a descriptor protocol). Set
4192 the second element of the stack to NULL, to signal
Yury Selivanovf2392132016-12-13 19:03:51 -05004193 CALL_METHOD that it's not a method call.
INADA Naoki015bce62017-01-16 17:23:30 +09004194
4195 NULL | meth | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004196 */
INADA Naoki015bce62017-01-16 17:23:30 +09004197 SET_TOP(NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004198 Py_DECREF(obj);
INADA Naoki015bce62017-01-16 17:23:30 +09004199 PUSH(meth);
Yury Selivanovf2392132016-12-13 19:03:51 -05004200 }
4201 DISPATCH();
4202 }
4203
Benjamin Petersonddd19492018-09-16 22:38:02 -07004204 case TARGET(CALL_METHOD): {
Yury Selivanovf2392132016-12-13 19:03:51 -05004205 /* Designed to work in tamdem with LOAD_METHOD. */
INADA Naoki015bce62017-01-16 17:23:30 +09004206 PyObject **sp, *res, *meth;
Yury Selivanovf2392132016-12-13 19:03:51 -05004207
4208 sp = stack_pointer;
4209
INADA Naoki015bce62017-01-16 17:23:30 +09004210 meth = PEEK(oparg + 2);
4211 if (meth == NULL) {
4212 /* `meth` is NULL when LOAD_METHOD thinks that it's not
4213 a method call.
Yury Selivanovf2392132016-12-13 19:03:51 -05004214
4215 Stack layout:
4216
INADA Naoki015bce62017-01-16 17:23:30 +09004217 ... | NULL | callable | arg1 | ... | argN
4218 ^- TOP()
4219 ^- (-oparg)
4220 ^- (-oparg-1)
4221 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004222
Ville Skyttä49b27342017-08-03 09:00:59 +03004223 `callable` will be POPed by call_function.
INADA Naoki015bce62017-01-16 17:23:30 +09004224 NULL will will be POPed manually later.
Yury Selivanovf2392132016-12-13 19:03:51 -05004225 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004226 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004227 stack_pointer = sp;
INADA Naoki015bce62017-01-16 17:23:30 +09004228 (void)POP(); /* POP the NULL. */
Yury Selivanovf2392132016-12-13 19:03:51 -05004229 }
4230 else {
4231 /* This is a method call. Stack layout:
4232
INADA Naoki015bce62017-01-16 17:23:30 +09004233 ... | method | self | arg1 | ... | argN
Yury Selivanovf2392132016-12-13 19:03:51 -05004234 ^- TOP()
4235 ^- (-oparg)
INADA Naoki015bce62017-01-16 17:23:30 +09004236 ^- (-oparg-1)
4237 ^- (-oparg-2)
Yury Selivanovf2392132016-12-13 19:03:51 -05004238
INADA Naoki015bce62017-01-16 17:23:30 +09004239 `self` and `method` will be POPed by call_function.
Yury Selivanovf2392132016-12-13 19:03:51 -05004240 We'll be passing `oparg + 1` to call_function, to
INADA Naoki015bce62017-01-16 17:23:30 +09004241 make it accept the `self` as a first argument.
Yury Selivanovf2392132016-12-13 19:03:51 -05004242 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00004243 res = call_function(tstate, &trace_info, &sp, oparg + 1, NULL);
Yury Selivanovf2392132016-12-13 19:03:51 -05004244 stack_pointer = sp;
4245 }
4246
4247 PUSH(res);
4248 if (res == NULL)
4249 goto error;
Mark Shannon4958f5d2021-03-24 17:56:12 +00004250 CHECK_EVAL_BREAKER();
Yury Selivanovf2392132016-12-13 19:03:51 -05004251 DISPATCH();
4252 }
4253
Benjamin Petersonddd19492018-09-16 22:38:02 -07004254 case TARGET(CALL_FUNCTION): {
4255 PREDICTED(CALL_FUNCTION);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004256 PyObject **sp, *res;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004257 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004258 res = call_function(tstate, &trace_info, &sp, oparg, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004259 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004260 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004261 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004262 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004263 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004264 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004265 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004266 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004267
Benjamin Petersonddd19492018-09-16 22:38:02 -07004268 case TARGET(CALL_FUNCTION_KW): {
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004269 PyObject **sp, *res, *names;
4270
4271 names = POP();
Jeroen Demeyer05677862019-08-16 12:41:27 +02004272 assert(PyTuple_Check(names));
4273 assert(PyTuple_GET_SIZE(names) <= oparg);
4274 /* We assume without checking that names contains only strings */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004275 sp = stack_pointer;
Mark Shannon8e1b4062021-03-05 14:45:50 +00004276 res = call_function(tstate, &trace_info, &sp, oparg, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004277 stack_pointer = sp;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004278 PUSH(res);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004279 Py_DECREF(names);
4280
4281 if (res == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004282 goto error;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004283 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004284 CHECK_EVAL_BREAKER();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004285 DISPATCH();
4286 }
4287
Benjamin Petersonddd19492018-09-16 22:38:02 -07004288 case TARGET(CALL_FUNCTION_EX): {
Brandt Bucherf185a732019-09-28 17:12:49 -07004289 PREDICTED(CALL_FUNCTION_EX);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004290 PyObject *func, *callargs, *kwargs = NULL, *result;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004291 if (oparg & 0x01) {
4292 kwargs = POP();
Serhiy Storchakab7281052016-09-12 00:52:40 +03004293 if (!PyDict_CheckExact(kwargs)) {
4294 PyObject *d = PyDict_New();
4295 if (d == NULL)
4296 goto error;
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02004297 if (_PyDict_MergeEx(d, kwargs, 2) < 0) {
Serhiy Storchakab7281052016-09-12 00:52:40 +03004298 Py_DECREF(d);
Victor Stinner438a12d2019-05-24 17:01:38 +02004299 format_kwargs_error(tstate, SECOND(), kwargs);
Victor Stinnereece2222016-09-12 11:16:37 +02004300 Py_DECREF(kwargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004301 goto error;
4302 }
4303 Py_DECREF(kwargs);
4304 kwargs = d;
4305 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004306 assert(PyDict_CheckExact(kwargs));
4307 }
4308 callargs = POP();
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004309 func = TOP();
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004310 if (!PyTuple_CheckExact(callargs)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02004311 if (check_args_iterable(tstate, func, callargs) < 0) {
Victor Stinnereece2222016-09-12 11:16:37 +02004312 Py_DECREF(callargs);
Serhiy Storchakab7281052016-09-12 00:52:40 +03004313 goto error;
4314 }
4315 Py_SETREF(callargs, PySequence_Tuple(callargs));
4316 if (callargs == NULL) {
4317 goto error;
4318 }
4319 }
Serhiy Storchaka63dc5482016-09-22 19:41:20 +03004320 assert(PyTuple_CheckExact(callargs));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004321
Mark Shannon8e1b4062021-03-05 14:45:50 +00004322 result = do_call_core(tstate, &trace_info, func, callargs, kwargs);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004323 Py_DECREF(func);
4324 Py_DECREF(callargs);
4325 Py_XDECREF(kwargs);
4326
4327 SET_TOP(result);
4328 if (result == NULL) {
4329 goto error;
4330 }
Mark Shannon4958f5d2021-03-24 17:56:12 +00004331 CHECK_EVAL_BREAKER();
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004332 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004333 }
Guido van Rossumac7be682001-01-17 15:42:30 +00004334
Benjamin Petersonddd19492018-09-16 22:38:02 -07004335 case TARGET(MAKE_FUNCTION): {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004336 PyObject *qualname = POP();
4337 PyObject *codeobj = POP();
4338 PyFunctionObject *func = (PyFunctionObject *)
4339 PyFunction_NewWithQualName(codeobj, f->f_globals, qualname);
Guido van Rossum4f72a782006-10-27 23:31:49 +00004340
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004341 Py_DECREF(codeobj);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004342 Py_DECREF(qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004343 if (func == NULL) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004344 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004345 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004346
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004347 if (oparg & 0x08) {
4348 assert(PyTuple_CheckExact(TOP()));
Mark Shannond6c33fb2021-01-29 13:24:55 +00004349 func->func_closure = POP();
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004350 }
4351 if (oparg & 0x04) {
Yurii Karabas73019792020-11-25 12:43:18 +02004352 assert(PyTuple_CheckExact(TOP()));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004353 func->func_annotations = POP();
4354 }
4355 if (oparg & 0x02) {
4356 assert(PyDict_CheckExact(TOP()));
4357 func->func_kwdefaults = POP();
4358 }
4359 if (oparg & 0x01) {
4360 assert(PyTuple_CheckExact(TOP()));
4361 func->func_defaults = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004362 }
Neal Norwitzc1505362006-12-28 06:47:50 +00004363
Serhiy Storchaka64204de2016-06-12 17:36:24 +03004364 PUSH((PyObject *)func);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004365 DISPATCH();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004366 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004367
Benjamin Petersonddd19492018-09-16 22:38:02 -07004368 case TARGET(BUILD_SLICE): {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004369 PyObject *start, *stop, *step, *slice;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004370 if (oparg == 3)
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004371 step = POP();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004372 else
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004373 step = NULL;
4374 stop = POP();
4375 start = TOP();
4376 slice = PySlice_New(start, stop, step);
4377 Py_DECREF(start);
4378 Py_DECREF(stop);
4379 Py_XDECREF(step);
4380 SET_TOP(slice);
4381 if (slice == NULL)
4382 goto error;
4383 DISPATCH();
4384 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004385
Benjamin Petersonddd19492018-09-16 22:38:02 -07004386 case TARGET(FORMAT_VALUE): {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004387 /* Handles f-string value formatting. */
4388 PyObject *result;
4389 PyObject *fmt_spec;
4390 PyObject *value;
4391 PyObject *(*conv_fn)(PyObject *);
4392 int which_conversion = oparg & FVC_MASK;
4393 int have_fmt_spec = (oparg & FVS_MASK) == FVS_HAVE_SPEC;
4394
4395 fmt_spec = have_fmt_spec ? POP() : NULL;
Eric V. Smith135d5f42016-02-05 18:23:08 -05004396 value = POP();
Eric V. Smitha78c7952015-11-03 12:45:05 -05004397
4398 /* See if any conversion is specified. */
4399 switch (which_conversion) {
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004400 case FVC_NONE: conv_fn = NULL; break;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004401 case FVC_STR: conv_fn = PyObject_Str; break;
4402 case FVC_REPR: conv_fn = PyObject_Repr; break;
4403 case FVC_ASCII: conv_fn = PyObject_ASCII; break;
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004404 default:
Victor Stinner438a12d2019-05-24 17:01:38 +02004405 _PyErr_Format(tstate, PyExc_SystemError,
4406 "unexpected conversion flag %d",
4407 which_conversion);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004408 goto error;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004409 }
4410
4411 /* If there's a conversion function, call it and replace
4412 value with that result. Otherwise, just use value,
4413 without conversion. */
Eric V. Smitheb588a12016-02-05 18:26:20 -05004414 if (conv_fn != NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004415 result = conv_fn(value);
4416 Py_DECREF(value);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004417 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004418 Py_XDECREF(fmt_spec);
4419 goto error;
4420 }
4421 value = result;
4422 }
4423
4424 /* If value is a unicode object, and there's no fmt_spec,
4425 then we know the result of format(value) is value
4426 itself. In that case, skip calling format(). I plan to
4427 move this optimization in to PyObject_Format()
4428 itself. */
4429 if (PyUnicode_CheckExact(value) && fmt_spec == NULL) {
4430 /* Do nothing, just transfer ownership to result. */
4431 result = value;
4432 } else {
4433 /* Actually call format(). */
4434 result = PyObject_Format(value, fmt_spec);
4435 Py_DECREF(value);
4436 Py_XDECREF(fmt_spec);
Eric V. Smitheb588a12016-02-05 18:26:20 -05004437 if (result == NULL) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004438 goto error;
Eric V. Smitheb588a12016-02-05 18:26:20 -05004439 }
Eric V. Smitha78c7952015-11-03 12:45:05 -05004440 }
4441
Eric V. Smith135d5f42016-02-05 18:23:08 -05004442 PUSH(result);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004443 DISPATCH();
4444 }
4445
Benjamin Petersonddd19492018-09-16 22:38:02 -07004446 case TARGET(EXTENDED_ARG): {
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03004447 int oldoparg = oparg;
4448 NEXTOPARG();
4449 oparg |= oldoparg << 8;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004450 goto dispatch_opcode;
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004451 }
Guido van Rossum8861b741996-07-30 16:49:37 +00004452
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004453
Antoine Pitrou042b1282010-08-13 21:15:58 +00004454#if USE_COMPUTED_GOTOS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 _unknown_opcode:
Antoine Pitroub52ec782009-01-25 16:34:23 +00004456#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 default:
4458 fprintf(stderr,
4459 "XXX lineno: %d, opcode: %d\n",
4460 PyFrame_GetLineNumber(f),
4461 opcode);
Victor Stinner438a12d2019-05-24 17:01:38 +02004462 _PyErr_SetString(tstate, PyExc_SystemError, "unknown opcode");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004463 goto error;
Guido van Rossum04691fc1992-08-12 15:35:34 +00004464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004465 } /* switch */
Guido van Rossum374a9221991-04-04 10:40:29 +00004466
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004467 /* This should never be reached. Every opcode should end with DISPATCH()
4468 or goto error. */
Barry Warsawb2e57942017-09-14 18:13:16 -07004469 Py_UNREACHABLE();
Guido van Rossumac7be682001-01-17 15:42:30 +00004470
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004471error:
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004472 /* Double-check exception status. */
Victor Stinner365b6932013-07-12 00:11:58 +02004473#ifdef NDEBUG
Victor Stinner438a12d2019-05-24 17:01:38 +02004474 if (!_PyErr_Occurred(tstate)) {
4475 _PyErr_SetString(tstate, PyExc_SystemError,
4476 "error return without exception set");
4477 }
Victor Stinner365b6932013-07-12 00:11:58 +02004478#else
Victor Stinner438a12d2019-05-24 17:01:38 +02004479 assert(_PyErr_Occurred(tstate));
Victor Stinner365b6932013-07-12 00:11:58 +02004480#endif
Guido van Rossum374a9221991-04-04 10:40:29 +00004481
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004482 /* Log traceback info. */
4483 PyTraceBack_Here(f);
Guido van Rossumac7be682001-01-17 15:42:30 +00004484
Mark Shannoncb9879b2020-07-17 11:44:23 +01004485 if (tstate->c_tracefunc != NULL) {
4486 /* Make sure state is set to FRAME_EXECUTING for tracing */
4487 assert(f->f_state == FRAME_EXECUTING);
4488 f->f_state = FRAME_UNWINDING;
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01004489 call_exc_trace(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004490 tstate, f, &trace_info);
Mark Shannoncb9879b2020-07-17 11:44:23 +01004491 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004492exception_unwind:
Mark Shannoncb9879b2020-07-17 11:44:23 +01004493 f->f_state = FRAME_UNWINDING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004494 /* Unwind stacks if an exception occurred */
4495 while (f->f_iblock > 0) {
4496 /* Pop the current block. */
4497 PyTryBlock *b = &f->f_blockstack[--f->f_iblock];
Jeremy Hylton3faa52e2001-02-01 22:48:12 +00004498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004499 if (b->b_type == EXCEPT_HANDLER) {
4500 UNWIND_EXCEPT_HANDLER(b);
4501 continue;
4502 }
4503 UNWIND_BLOCK(b);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004504 if (b->b_type == SETUP_FINALLY) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505 PyObject *exc, *val, *tb;
4506 int handler = b->b_handler;
Mark Shannonae3087c2017-10-22 22:41:51 +01004507 _PyErr_StackItem *exc_info = tstate->exc_info;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004508 /* Beware, this invalidates all b->b_* fields */
Mark Shannonbf353f32020-12-17 13:55:28 +00004509 PyFrame_BlockSetup(f, EXCEPT_HANDLER, f->f_lasti, STACK_LEVEL());
Mark Shannonae3087c2017-10-22 22:41:51 +01004510 PUSH(exc_info->exc_traceback);
4511 PUSH(exc_info->exc_value);
4512 if (exc_info->exc_type != NULL) {
4513 PUSH(exc_info->exc_type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004514 }
4515 else {
4516 Py_INCREF(Py_None);
4517 PUSH(Py_None);
4518 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004519 _PyErr_Fetch(tstate, &exc, &val, &tb);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004520 /* Make the raw exception data
4521 available to the handler,
4522 so a program can emulate the
4523 Python main loop. */
Victor Stinner438a12d2019-05-24 17:01:38 +02004524 _PyErr_NormalizeException(tstate, &exc, &val, &tb);
Victor Stinner7eab0d02013-07-15 21:16:27 +02004525 if (tb != NULL)
4526 PyException_SetTraceback(val, tb);
4527 else
4528 PyException_SetTraceback(val, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004529 Py_INCREF(exc);
Mark Shannonae3087c2017-10-22 22:41:51 +01004530 exc_info->exc_type = exc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004531 Py_INCREF(val);
Mark Shannonae3087c2017-10-22 22:41:51 +01004532 exc_info->exc_value = val;
4533 exc_info->exc_traceback = tb;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004534 if (tb == NULL)
4535 tb = Py_None;
4536 Py_INCREF(tb);
4537 PUSH(tb);
4538 PUSH(val);
4539 PUSH(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004540 JUMPTO(handler);
Victor Stinnerdab84232020-03-17 18:56:44 +01004541 if (_Py_TracingPossible(ceval2)) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00004542 trace_info.instr_prev = INT_MAX;
Mark Shannonfee55262019-11-21 09:11:43 +00004543 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004544 /* Resume normal execution */
Mark Shannoncb9879b2020-07-17 11:44:23 +01004545 f->f_state = FRAME_EXECUTING;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004546 goto main_loop;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004547 }
4548 } /* unwind stack */
Guido van Rossum374a9221991-04-04 10:40:29 +00004549
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004550 /* End the loop as we still have an error */
4551 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004552 } /* main loop */
Guido van Rossumac7be682001-01-17 15:42:30 +00004553
Pablo Galindof00828a2019-05-09 16:52:02 +01004554 assert(retval == NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02004555 assert(_PyErr_Occurred(tstate));
Pablo Galindof00828a2019-05-09 16:52:02 +01004556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004557 /* Pop remaining stack entries. */
4558 while (!EMPTY()) {
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04004559 PyObject *o = POP();
4560 Py_XDECREF(o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004561 }
Mark Shannoncb9879b2020-07-17 11:44:23 +01004562 f->f_stackdepth = 0;
4563 f->f_state = FRAME_RAISED;
Mark Shannone7c9f4a2020-01-13 12:51:26 +00004564exiting:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004565 if (tstate->use_tracing) {
Benjamin Peterson51f46162013-01-23 08:38:47 -05004566 if (tstate->c_tracefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004567 if (call_trace_protected(tstate->c_tracefunc, tstate->c_traceobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004568 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004569 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004570 }
4571 }
4572 if (tstate->c_profilefunc) {
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004573 if (call_trace_protected(tstate->c_profilefunc, tstate->c_profileobj,
Mark Shannon8e1b4062021-03-05 14:45:50 +00004574 tstate, f, &trace_info, PyTrace_RETURN, retval)) {
Serhiy Storchaka505ff752014-02-09 13:33:53 +02004575 Py_CLEAR(retval);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004576 }
4577 }
4578 }
Guido van Rossuma4240131997-01-21 21:18:36 +00004579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004580 /* pop frame */
Thomas Woutersce272b62007-09-19 21:19:28 +00004581exit_eval_frame:
Łukasz Langaa785c872016-09-09 17:37:37 -07004582 if (PyDTrace_FUNCTION_RETURN_ENABLED())
4583 dtrace_function_return(f);
Victor Stinnerbe434dc2019-11-05 00:51:22 +01004584 _Py_LeaveRecursiveCall(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004585 tstate->frame = f->f_back;
Guido van Rossumac7be682001-01-17 15:42:30 +00004586
Victor Stinner0b72b232020-03-12 23:18:39 +01004587 return _Py_CheckFunctionResult(tstate, NULL, retval, __func__);
Guido van Rossum374a9221991-04-04 10:40:29 +00004588}
4589
Benjamin Petersonb204a422011-06-05 22:04:07 -05004590static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004591format_missing(PyThreadState *tstate, const char *kind,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004592 PyCodeObject *co, PyObject *names, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004593{
4594 int err;
4595 Py_ssize_t len = PyList_GET_SIZE(names);
4596 PyObject *name_str, *comma, *tail, *tmp;
4597
4598 assert(PyList_CheckExact(names));
4599 assert(len >= 1);
4600 /* Deal with the joys of natural language. */
4601 switch (len) {
4602 case 1:
4603 name_str = PyList_GET_ITEM(names, 0);
4604 Py_INCREF(name_str);
4605 break;
4606 case 2:
4607 name_str = PyUnicode_FromFormat("%U and %U",
4608 PyList_GET_ITEM(names, len - 2),
4609 PyList_GET_ITEM(names, len - 1));
4610 break;
4611 default:
4612 tail = PyUnicode_FromFormat(", %U, and %U",
4613 PyList_GET_ITEM(names, len - 2),
4614 PyList_GET_ITEM(names, len - 1));
Benjamin Petersond1ab6082012-06-01 11:18:22 -07004615 if (tail == NULL)
4616 return;
Benjamin Petersone109c702011-06-24 09:37:26 -05004617 /* Chop off the last two objects in the list. This shouldn't actually
4618 fail, but we can't be too careful. */
4619 err = PyList_SetSlice(names, len - 2, len, NULL);
4620 if (err == -1) {
4621 Py_DECREF(tail);
4622 return;
4623 }
4624 /* Stitch everything up into a nice comma-separated list. */
4625 comma = PyUnicode_FromString(", ");
4626 if (comma == NULL) {
4627 Py_DECREF(tail);
4628 return;
4629 }
4630 tmp = PyUnicode_Join(comma, names);
4631 Py_DECREF(comma);
4632 if (tmp == NULL) {
4633 Py_DECREF(tail);
4634 return;
4635 }
4636 name_str = PyUnicode_Concat(tmp, tail);
4637 Py_DECREF(tmp);
4638 Py_DECREF(tail);
4639 break;
4640 }
4641 if (name_str == NULL)
4642 return;
Victor Stinner438a12d2019-05-24 17:01:38 +02004643 _PyErr_Format(tstate, PyExc_TypeError,
4644 "%U() missing %i required %s argument%s: %U",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004645 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004646 len,
4647 kind,
4648 len == 1 ? "" : "s",
4649 name_str);
Benjamin Petersone109c702011-06-24 09:37:26 -05004650 Py_DECREF(name_str);
4651}
4652
4653static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004654missing_arguments(PyThreadState *tstate, PyCodeObject *co,
4655 Py_ssize_t missing, Py_ssize_t defcount,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004656 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersone109c702011-06-24 09:37:26 -05004657{
Victor Stinner74319ae2016-08-25 00:04:09 +02004658 Py_ssize_t i, j = 0;
4659 Py_ssize_t start, end;
4660 int positional = (defcount != -1);
Benjamin Petersone109c702011-06-24 09:37:26 -05004661 const char *kind = positional ? "positional" : "keyword-only";
4662 PyObject *missing_names;
4663
4664 /* Compute the names of the arguments that are missing. */
4665 missing_names = PyList_New(missing);
4666 if (missing_names == NULL)
4667 return;
4668 if (positional) {
4669 start = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004670 end = co->co_argcount - defcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004671 }
4672 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004673 start = co->co_argcount;
Benjamin Petersone109c702011-06-24 09:37:26 -05004674 end = start + co->co_kwonlyargcount;
4675 }
4676 for (i = start; i < end; i++) {
4677 if (GETLOCAL(i) == NULL) {
4678 PyObject *raw = PyTuple_GET_ITEM(co->co_varnames, i);
4679 PyObject *name = PyObject_Repr(raw);
4680 if (name == NULL) {
4681 Py_DECREF(missing_names);
4682 return;
4683 }
4684 PyList_SET_ITEM(missing_names, j++, name);
4685 }
4686 }
4687 assert(j == missing);
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004688 format_missing(tstate, kind, co, missing_names, qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004689 Py_DECREF(missing_names);
4690}
4691
4692static void
Victor Stinner438a12d2019-05-24 17:01:38 +02004693too_many_positional(PyThreadState *tstate, PyCodeObject *co,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004694 Py_ssize_t given, PyObject *defaults,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004695 PyObject **fastlocals, PyObject *qualname)
Benjamin Petersonb204a422011-06-05 22:04:07 -05004696{
4697 int plural;
Victor Stinner74319ae2016-08-25 00:04:09 +02004698 Py_ssize_t kwonly_given = 0;
4699 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004700 PyObject *sig, *kwonly_sig;
Victor Stinner74319ae2016-08-25 00:04:09 +02004701 Py_ssize_t co_argcount = co->co_argcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004702
Benjamin Petersone109c702011-06-24 09:37:26 -05004703 assert((co->co_flags & CO_VARARGS) == 0);
4704 /* Count missing keyword-only args. */
Pablo Galindocd74e662019-06-01 18:08:04 +01004705 for (i = co_argcount; i < co_argcount + co->co_kwonlyargcount; i++) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004706 if (GETLOCAL(i) != NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004707 kwonly_given++;
Victor Stinner74319ae2016-08-25 00:04:09 +02004708 }
4709 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00004710 Py_ssize_t defcount = defaults == NULL ? 0 : PyTuple_GET_SIZE(defaults);
Benjamin Petersone109c702011-06-24 09:37:26 -05004711 if (defcount) {
Pablo Galindocd74e662019-06-01 18:08:04 +01004712 Py_ssize_t atleast = co_argcount - defcount;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004713 plural = 1;
Pablo Galindocd74e662019-06-01 18:08:04 +01004714 sig = PyUnicode_FromFormat("from %zd to %zd", atleast, co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004715 }
4716 else {
Pablo Galindocd74e662019-06-01 18:08:04 +01004717 plural = (co_argcount != 1);
4718 sig = PyUnicode_FromFormat("%zd", co_argcount);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004719 }
4720 if (sig == NULL)
4721 return;
4722 if (kwonly_given) {
Victor Stinner74319ae2016-08-25 00:04:09 +02004723 const char *format = " positional argument%s (and %zd keyword-only argument%s)";
4724 kwonly_sig = PyUnicode_FromFormat(format,
4725 given != 1 ? "s" : "",
4726 kwonly_given,
4727 kwonly_given != 1 ? "s" : "");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004728 if (kwonly_sig == NULL) {
4729 Py_DECREF(sig);
4730 return;
4731 }
4732 }
4733 else {
4734 /* This will not fail. */
4735 kwonly_sig = PyUnicode_FromString("");
Benjamin Petersone109c702011-06-24 09:37:26 -05004736 assert(kwonly_sig != NULL);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004737 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004738 _PyErr_Format(tstate, PyExc_TypeError,
4739 "%U() takes %U positional argument%s but %zd%U %s given",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004740 qualname,
Victor Stinner438a12d2019-05-24 17:01:38 +02004741 sig,
4742 plural ? "s" : "",
4743 given,
4744 kwonly_sig,
4745 given == 1 && !kwonly_given ? "was" : "were");
Benjamin Petersonb204a422011-06-05 22:04:07 -05004746 Py_DECREF(sig);
4747 Py_DECREF(kwonly_sig);
4748}
4749
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004750static int
Victor Stinner438a12d2019-05-24 17:01:38 +02004751positional_only_passed_as_keyword(PyThreadState *tstate, PyCodeObject *co,
Mark Shannon0332e562021-02-01 10:42:03 +00004752 Py_ssize_t kwcount, PyObject* kwnames,
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004753 PyObject *qualname)
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004754{
4755 int posonly_conflicts = 0;
4756 PyObject* posonly_names = PyList_New(0);
4757
4758 for(int k=0; k < co->co_posonlyargcount; k++){
4759 PyObject* posonly_name = PyTuple_GET_ITEM(co->co_varnames, k);
4760
4761 for (int k2=0; k2<kwcount; k2++){
4762 /* Compare the pointers first and fallback to PyObject_RichCompareBool*/
Mark Shannon0332e562021-02-01 10:42:03 +00004763 PyObject* kwname = PyTuple_GET_ITEM(kwnames, k2);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004764 if (kwname == posonly_name){
4765 if(PyList_Append(posonly_names, kwname) != 0) {
4766 goto fail;
4767 }
4768 posonly_conflicts++;
4769 continue;
4770 }
4771
4772 int cmp = PyObject_RichCompareBool(posonly_name, kwname, Py_EQ);
4773
4774 if ( cmp > 0) {
4775 if(PyList_Append(posonly_names, kwname) != 0) {
4776 goto fail;
4777 }
4778 posonly_conflicts++;
4779 } else if (cmp < 0) {
4780 goto fail;
4781 }
4782
4783 }
4784 }
4785 if (posonly_conflicts) {
4786 PyObject* comma = PyUnicode_FromString(", ");
4787 if (comma == NULL) {
4788 goto fail;
4789 }
4790 PyObject* error_names = PyUnicode_Join(comma, posonly_names);
4791 Py_DECREF(comma);
4792 if (error_names == NULL) {
4793 goto fail;
4794 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004795 _PyErr_Format(tstate, PyExc_TypeError,
4796 "%U() got some positional-only arguments passed"
4797 " as keyword arguments: '%U'",
Dennis Sweeneyb5cc2082020-05-22 16:40:17 -04004798 qualname, error_names);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004799 Py_DECREF(error_names);
4800 goto fail;
4801 }
4802
4803 Py_DECREF(posonly_names);
4804 return 0;
4805
4806fail:
4807 Py_XDECREF(posonly_names);
4808 return 1;
4809
4810}
4811
Skip Montanaro786ea6b2004-03-01 15:44:05 +00004812
Mark Shannon0332e562021-02-01 10:42:03 +00004813PyFrameObject *
4814_PyEval_MakeFrameVector(PyThreadState *tstate,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004815 PyFrameConstructor *con, PyObject *locals,
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02004816 PyObject *const *args, Py_ssize_t argcount,
Mark Shannon0332e562021-02-01 10:42:03 +00004817 PyObject *kwnames)
Tim Peters5ca576e2001-06-18 22:08:13 +00004818{
Victor Stinnerda2914d2020-03-20 09:29:08 +01004819 assert(is_tstate_valid(tstate));
Victor Stinnerb5e170f2019-11-16 01:03:22 +01004820
Mark Shannond6c33fb2021-01-29 13:24:55 +00004821 PyCodeObject *co = (PyCodeObject*)con->fc_code;
4822 assert(con->fc_defaults == NULL || PyTuple_CheckExact(con->fc_defaults));
Pablo Galindocd74e662019-06-01 18:08:04 +01004823 const Py_ssize_t total_args = co->co_argcount + co->co_kwonlyargcount;
Tim Peters5ca576e2001-06-18 22:08:13 +00004824
Victor Stinnerc7020012016-08-16 23:40:29 +02004825 /* Create the frame */
Mark Shannon0332e562021-02-01 10:42:03 +00004826 PyFrameObject *f = _PyFrame_New_NoTrack(tstate, con, locals);
Victor Stinnerc7020012016-08-16 23:40:29 +02004827 if (f == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004828 return NULL;
Victor Stinnerc7020012016-08-16 23:40:29 +02004829 }
Victor Stinner232dda62020-06-04 15:19:02 +02004830 PyObject **fastlocals = f->f_localsplus;
4831 PyObject **freevars = f->f_localsplus + co->co_nlocals;
Tim Peters5ca576e2001-06-18 22:08:13 +00004832
Victor Stinnerc7020012016-08-16 23:40:29 +02004833 /* Create a dictionary for keyword parameters (**kwags) */
Victor Stinner232dda62020-06-04 15:19:02 +02004834 PyObject *kwdict;
4835 Py_ssize_t i;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004836 if (co->co_flags & CO_VARKEYWORDS) {
4837 kwdict = PyDict_New();
4838 if (kwdict == NULL)
4839 goto fail;
4840 i = total_args;
Victor Stinnerc7020012016-08-16 23:40:29 +02004841 if (co->co_flags & CO_VARARGS) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004842 i++;
Victor Stinnerc7020012016-08-16 23:40:29 +02004843 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004844 SETLOCAL(i, kwdict);
4845 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004846 else {
4847 kwdict = NULL;
4848 }
4849
Pablo Galindocd74e662019-06-01 18:08:04 +01004850 /* Copy all positional arguments into local variables */
Victor Stinner232dda62020-06-04 15:19:02 +02004851 Py_ssize_t j, n;
Pablo Galindocd74e662019-06-01 18:08:04 +01004852 if (argcount > co->co_argcount) {
4853 n = co->co_argcount;
Victor Stinnerc7020012016-08-16 23:40:29 +02004854 }
4855 else {
4856 n = argcount;
4857 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004858 for (j = 0; j < n; j++) {
Victor Stinner232dda62020-06-04 15:19:02 +02004859 PyObject *x = args[j];
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004860 Py_INCREF(x);
4861 SETLOCAL(j, x);
4862 }
4863
Victor Stinnerc7020012016-08-16 23:40:29 +02004864 /* Pack other positional arguments into the *args argument */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004865 if (co->co_flags & CO_VARARGS) {
Victor Stinner232dda62020-06-04 15:19:02 +02004866 PyObject *u = _PyTuple_FromArray(args + n, argcount - n);
Victor Stinnerc7020012016-08-16 23:40:29 +02004867 if (u == NULL) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004868 goto fail;
Victor Stinnerc7020012016-08-16 23:40:29 +02004869 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004870 SETLOCAL(total_args, u);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004871 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004872
Mark Shannon0332e562021-02-01 10:42:03 +00004873 /* Handle keyword arguments */
4874 if (kwnames != NULL) {
4875 Py_ssize_t kwcount = PyTuple_GET_SIZE(kwnames);
4876 for (i = 0; i < kwcount; i++) {
4877 PyObject **co_varnames;
4878 PyObject *keyword = PyTuple_GET_ITEM(kwnames, i);
4879 PyObject *value = args[i+argcount];
4880 Py_ssize_t j;
Victor Stinnerc7020012016-08-16 23:40:29 +02004881
Mark Shannon0332e562021-02-01 10:42:03 +00004882 if (keyword == NULL || !PyUnicode_Check(keyword)) {
4883 _PyErr_Format(tstate, PyExc_TypeError,
4884 "%U() keywords must be strings",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004885 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004886 goto fail;
Victor Stinner6fea7f72016-08-22 23:17:30 +02004887 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004888
Mark Shannon0332e562021-02-01 10:42:03 +00004889 /* Speed hack: do raw pointer compares. As names are
4890 normally interned this should almost always hit. */
4891 co_varnames = ((PyTupleObject *)(co->co_varnames))->ob_item;
4892 for (j = co->co_posonlyargcount; j < total_args; j++) {
4893 PyObject *varname = co_varnames[j];
4894 if (varname == keyword) {
4895 goto kw_found;
4896 }
4897 }
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004898
Mark Shannon0332e562021-02-01 10:42:03 +00004899 /* Slow fallback, just in case */
4900 for (j = co->co_posonlyargcount; j < total_args; j++) {
4901 PyObject *varname = co_varnames[j];
4902 int cmp = PyObject_RichCompareBool( keyword, varname, Py_EQ);
4903 if (cmp > 0) {
4904 goto kw_found;
4905 }
4906 else if (cmp < 0) {
4907 goto fail;
4908 }
4909 }
4910
4911 assert(j >= total_args);
4912 if (kwdict == NULL) {
4913
4914 if (co->co_posonlyargcount
4915 && positional_only_passed_as_keyword(tstate, co,
4916 kwcount, kwnames,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004917 con->fc_qualname))
Mark Shannon0332e562021-02-01 10:42:03 +00004918 {
4919 goto fail;
4920 }
4921
4922 _PyErr_Format(tstate, PyExc_TypeError,
4923 "%U() got an unexpected keyword argument '%S'",
4924 con->fc_qualname, keyword);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01004925 goto fail;
4926 }
4927
Mark Shannon0332e562021-02-01 10:42:03 +00004928 if (PyDict_SetItem(kwdict, keyword, value) == -1) {
4929 goto fail;
4930 }
4931 continue;
Victor Stinnerc7020012016-08-16 23:40:29 +02004932
Mark Shannon0332e562021-02-01 10:42:03 +00004933 kw_found:
4934 if (GETLOCAL(j) != NULL) {
4935 _PyErr_Format(tstate, PyExc_TypeError,
4936 "%U() got multiple values for argument '%S'",
Mark Shannond6c33fb2021-01-29 13:24:55 +00004937 con->fc_qualname, keyword);
Mark Shannon0332e562021-02-01 10:42:03 +00004938 goto fail;
4939 }
4940 Py_INCREF(value);
4941 SETLOCAL(j, value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004942 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004943 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004944
4945 /* Check the number of positional arguments */
Pablo Galindocd74e662019-06-01 18:08:04 +01004946 if ((argcount > co->co_argcount) && !(co->co_flags & CO_VARARGS)) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004947 too_many_positional(tstate, co, argcount, con->fc_defaults, fastlocals,
4948 con->fc_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004949 goto fail;
4950 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004951
4952 /* Add missing positional arguments (copy default values from defs) */
Pablo Galindocd74e662019-06-01 18:08:04 +01004953 if (argcount < co->co_argcount) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00004954 Py_ssize_t defcount = con->fc_defaults == NULL ? 0 : PyTuple_GET_SIZE(con->fc_defaults);
Pablo Galindocd74e662019-06-01 18:08:04 +01004955 Py_ssize_t m = co->co_argcount - defcount;
Victor Stinner17061a92016-08-16 23:39:42 +02004956 Py_ssize_t missing = 0;
4957 for (i = argcount; i < m; i++) {
4958 if (GETLOCAL(i) == NULL) {
Benjamin Petersone109c702011-06-24 09:37:26 -05004959 missing++;
Victor Stinner17061a92016-08-16 23:39:42 +02004960 }
4961 }
Benjamin Petersone109c702011-06-24 09:37:26 -05004962 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02004963 missing_arguments(tstate, co, missing, defcount, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00004964 con->fc_qualname);
Benjamin Petersone109c702011-06-24 09:37:26 -05004965 goto fail;
Benjamin Petersonb204a422011-06-05 22:04:07 -05004966 }
4967 if (n > m)
4968 i = n - m;
4969 else
4970 i = 0;
Mark Shannond6c33fb2021-01-29 13:24:55 +00004971 if (defcount) {
4972 PyObject **defs = &PyTuple_GET_ITEM(con->fc_defaults, 0);
4973 for (; i < defcount; i++) {
4974 if (GETLOCAL(m+i) == NULL) {
4975 PyObject *def = defs[i];
4976 Py_INCREF(def);
4977 SETLOCAL(m+i, def);
4978 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05004979 }
4980 }
4981 }
Victor Stinnerc7020012016-08-16 23:40:29 +02004982
4983 /* Add missing keyword arguments (copy default values from kwdefs) */
Benjamin Petersonb204a422011-06-05 22:04:07 -05004984 if (co->co_kwonlyargcount > 0) {
Victor Stinner17061a92016-08-16 23:39:42 +02004985 Py_ssize_t missing = 0;
Pablo Galindocd74e662019-06-01 18:08:04 +01004986 for (i = co->co_argcount; i < total_args; i++) {
Benjamin Petersonb204a422011-06-05 22:04:07 -05004987 if (GETLOCAL(i) != NULL)
4988 continue;
Victor Stinner232dda62020-06-04 15:19:02 +02004989 PyObject *varname = PyTuple_GET_ITEM(co->co_varnames, i);
Mark Shannond6c33fb2021-01-29 13:24:55 +00004990 if (con->fc_kwdefaults != NULL) {
4991 PyObject *def = PyDict_GetItemWithError(con->fc_kwdefaults, varname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05004992 if (def) {
4993 Py_INCREF(def);
4994 SETLOCAL(i, def);
4995 continue;
4996 }
Victor Stinner438a12d2019-05-24 17:01:38 +02004997 else if (_PyErr_Occurred(tstate)) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02004998 goto fail;
4999 }
Benjamin Petersonb204a422011-06-05 22:04:07 -05005000 }
Benjamin Petersone109c702011-06-24 09:37:26 -05005001 missing++;
5002 }
5003 if (missing) {
Victor Stinner232dda62020-06-04 15:19:02 +02005004 missing_arguments(tstate, co, missing, -1, fastlocals,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005005 con->fc_qualname);
Benjamin Petersonb204a422011-06-05 22:04:07 -05005006 goto fail;
5007 }
5008 }
5009
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005010 /* Allocate and initialize storage for cell vars, and copy free
Benjamin Peterson90037602011-06-25 22:54:45 -05005011 vars into frame. */
5012 for (i = 0; i < PyTuple_GET_SIZE(co->co_cellvars); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005013 PyObject *c;
Serhiy Storchaka5bb8b912016-12-16 19:19:02 +02005014 Py_ssize_t arg;
Benjamin Peterson90037602011-06-25 22:54:45 -05005015 /* Possibly account for the cell variable being an argument. */
5016 if (co->co_cell2arg != NULL &&
Guido van Rossum6832c812013-05-10 08:47:42 -07005017 (arg = co->co_cell2arg[i]) != CO_CELL_NOT_AN_ARG) {
Benjamin Peterson90037602011-06-25 22:54:45 -05005018 c = PyCell_New(GETLOCAL(arg));
Benjamin Peterson159ae412013-05-12 18:16:06 -05005019 /* Clear the local copy. */
5020 SETLOCAL(arg, NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005021 }
5022 else {
Benjamin Peterson90037602011-06-25 22:54:45 -05005023 c = PyCell_New(NULL);
Guido van Rossum6832c812013-05-10 08:47:42 -07005024 }
Benjamin Peterson159ae412013-05-12 18:16:06 -05005025 if (c == NULL)
5026 goto fail;
Benjamin Peterson90037602011-06-25 22:54:45 -05005027 SETLOCAL(co->co_nlocals + i, c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005028 }
Victor Stinnerc7020012016-08-16 23:40:29 +02005029
5030 /* Copy closure variables to free variables */
Benjamin Peterson90037602011-06-25 22:54:45 -05005031 for (i = 0; i < PyTuple_GET_SIZE(co->co_freevars); ++i) {
Mark Shannond6c33fb2021-01-29 13:24:55 +00005032 PyObject *o = PyTuple_GET_ITEM(con->fc_closure, i);
Benjamin Peterson90037602011-06-25 22:54:45 -05005033 Py_INCREF(o);
5034 freevars[PyTuple_GET_SIZE(co->co_cellvars) + i] = o;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005035 }
Tim Peters5ca576e2001-06-18 22:08:13 +00005036
Mark Shannon0332e562021-02-01 10:42:03 +00005037 return f;
Tim Peters5ca576e2001-06-18 22:08:13 +00005038
Thomas Woutersce272b62007-09-19 21:19:28 +00005039fail: /* Jump here from prelude on failure */
Tim Peters5ca576e2001-06-18 22:08:13 +00005040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005041 /* decref'ing the frame can cause __del__ methods to get invoked,
5042 which can call back into Python. While we're done with the
5043 current Python frame (f), the associated C stack is still in use,
5044 so recursion_depth must be boosted for the duration.
5045 */
INADA Naoki5a625d02016-12-24 20:19:08 +09005046 if (Py_REFCNT(f) > 1) {
5047 Py_DECREF(f);
5048 _PyObject_GC_TRACK(f);
5049 }
5050 else {
5051 ++tstate->recursion_depth;
5052 Py_DECREF(f);
5053 --tstate->recursion_depth;
5054 }
Mark Shannon0332e562021-02-01 10:42:03 +00005055 return NULL;
5056}
5057
5058static PyObject *
5059make_coro(PyFrameConstructor *con, PyFrameObject *f)
5060{
5061 assert (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR));
5062 PyObject *gen;
5063 int is_coro = ((PyCodeObject *)con->fc_code)->co_flags & CO_COROUTINE;
5064
5065 /* Don't need to keep the reference to f_back, it will be set
5066 * when the generator is resumed. */
5067 Py_CLEAR(f->f_back);
5068
5069 /* Create a new generator that owns the ready to run frame
5070 * and return that as the value. */
5071 if (is_coro) {
5072 gen = PyCoro_New(f, con->fc_name, con->fc_qualname);
5073 } else if (((PyCodeObject *)con->fc_code)->co_flags & CO_ASYNC_GENERATOR) {
5074 gen = PyAsyncGen_New(f, con->fc_name, con->fc_qualname);
5075 } else {
5076 gen = PyGen_NewWithQualName(f, con->fc_name, con->fc_qualname);
5077 }
5078 if (gen == NULL) {
5079 return NULL;
5080 }
5081
5082 _PyObject_GC_TRACK(f);
5083
5084 return gen;
5085}
5086
5087PyObject *
5088_PyEval_Vector(PyThreadState *tstate, PyFrameConstructor *con,
5089 PyObject *locals,
5090 PyObject* const* args, size_t argcount,
5091 PyObject *kwnames)
5092{
5093 PyFrameObject *f = _PyEval_MakeFrameVector(
5094 tstate, con, locals, args, argcount, kwnames);
5095 if (f == NULL) {
5096 return NULL;
5097 }
5098 if (((PyCodeObject *)con->fc_code)->co_flags & (CO_GENERATOR | CO_COROUTINE | CO_ASYNC_GENERATOR)) {
5099 return make_coro(con, f);
5100 }
5101 PyObject *retval = _PyEval_EvalFrame(tstate, f, 0);
5102
5103 /* decref'ing the frame can cause __del__ methods to get invoked,
5104 which can call back into Python. While we're done with the
5105 current Python frame (f), the associated C stack is still in use,
5106 so recursion_depth must be boosted for the duration.
5107 */
5108 if (Py_REFCNT(f) > 1) {
5109 Py_DECREF(f);
5110 _PyObject_GC_TRACK(f);
5111 }
5112 else {
5113 ++tstate->recursion_depth;
5114 Py_DECREF(f);
5115 --tstate->recursion_depth;
5116 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005117 return retval;
Tim Peters5ca576e2001-06-18 22:08:13 +00005118}
5119
Mark Shannond6c33fb2021-01-29 13:24:55 +00005120/* Legacy API */
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005121PyObject *
Mark Shannon0332e562021-02-01 10:42:03 +00005122PyEval_EvalCodeEx(PyObject *_co, PyObject *globals, PyObject *locals,
5123 PyObject *const *args, int argcount,
5124 PyObject *const *kws, int kwcount,
5125 PyObject *const *defs, int defcount,
5126 PyObject *kwdefs, PyObject *closure)
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005127{
Victor Stinner46496f92021-02-20 15:17:18 +01005128 PyThreadState *tstate = _PyThreadState_GET();
Mark Shannon0332e562021-02-01 10:42:03 +00005129 PyObject *res;
Mark Shannond6c33fb2021-01-29 13:24:55 +00005130 PyObject *defaults = _PyTuple_FromArray(defs, defcount);
5131 if (defaults == NULL) {
5132 return NULL;
5133 }
Victor Stinnerfc980e02021-03-18 14:51:24 +01005134 PyObject *builtins = _PyEval_BuiltinsFromGlobals(tstate, globals); // borrowed ref
Mark Shannond6c33fb2021-01-29 13:24:55 +00005135 if (builtins == NULL) {
5136 Py_DECREF(defaults);
5137 return NULL;
5138 }
Mark Shannon0332e562021-02-01 10:42:03 +00005139 if (locals == NULL) {
5140 locals = globals;
5141 }
5142 PyObject *kwnames;
5143 PyObject *const *allargs;
5144 PyObject **newargs;
5145 if (kwcount == 0) {
5146 allargs = args;
5147 kwnames = NULL;
5148 }
5149 else {
5150 kwnames = PyTuple_New(kwcount);
5151 if (kwnames == NULL) {
5152 res = NULL;
5153 goto fail;
5154 }
5155 newargs = PyMem_Malloc(sizeof(PyObject *)*(kwcount+argcount));
5156 if (newargs == NULL) {
5157 res = NULL;
5158 Py_DECREF(kwnames);
5159 goto fail;
5160 }
5161 for (int i = 0; i < argcount; i++) {
5162 newargs[i] = args[i];
5163 }
5164 for (int i = 0; i < kwcount; i++) {
5165 Py_INCREF(kws[2*i]);
5166 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5167 newargs[argcount+i] = kws[2*i+1];
5168 }
5169 allargs = newargs;
5170 }
5171 PyObject **kwargs = PyMem_Malloc(sizeof(PyObject *)*kwcount);
5172 if (kwargs == NULL) {
5173 res = NULL;
5174 Py_DECREF(kwnames);
5175 goto fail;
5176 }
5177 for (int i = 0; i < kwcount; i++) {
5178 Py_INCREF(kws[2*i]);
5179 PyTuple_SET_ITEM(kwnames, i, kws[2*i]);
5180 kwargs[i] = kws[2*i+1];
5181 }
Mark Shannond6c33fb2021-01-29 13:24:55 +00005182 PyFrameConstructor constr = {
5183 .fc_globals = globals,
5184 .fc_builtins = builtins,
Mark Shannon0332e562021-02-01 10:42:03 +00005185 .fc_name = ((PyCodeObject *)_co)->co_name,
5186 .fc_qualname = ((PyCodeObject *)_co)->co_name,
Mark Shannond6c33fb2021-01-29 13:24:55 +00005187 .fc_code = _co,
5188 .fc_defaults = defaults,
5189 .fc_kwdefaults = kwdefs,
5190 .fc_closure = closure
5191 };
Mark Shannon0332e562021-02-01 10:42:03 +00005192 res = _PyEval_Vector(tstate, &constr, locals,
Victor Stinner44085a32021-02-18 19:20:16 +01005193 allargs, argcount,
5194 kwnames);
Mark Shannon0332e562021-02-01 10:42:03 +00005195 if (kwcount) {
5196 Py_DECREF(kwnames);
5197 PyMem_Free(newargs);
5198 }
5199fail:
Mark Shannond6c33fb2021-01-29 13:24:55 +00005200 Py_DECREF(defaults);
Mark Shannond6c33fb2021-01-29 13:24:55 +00005201 return res;
Victor Stinnerb5e170f2019-11-16 01:03:22 +01005202}
5203
Tim Peters5ca576e2001-06-18 22:08:13 +00005204
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005205static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02005206special_lookup(PyThreadState *tstate, PyObject *o, _Py_Identifier *id)
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005207{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005208 PyObject *res;
Benjamin Petersonce798522012-01-22 11:24:29 -05005209 res = _PyObject_LookupSpecial(o, id);
Victor Stinner438a12d2019-05-24 17:01:38 +02005210 if (res == NULL && !_PyErr_Occurred(tstate)) {
Victor Stinner4804b5b2020-05-12 01:43:38 +02005211 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(id));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005212 return NULL;
5213 }
5214 return res;
Benjamin Peterson876b2f22009-06-28 03:18:59 +00005215}
5216
5217
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005218/* Logic for the raise statement (too complicated for inlining).
5219 This *consumes* a reference count to each of its arguments. */
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005220static int
Victor Stinner09532fe2019-05-10 23:39:09 +02005221do_raise(PyThreadState *tstate, PyObject *exc, PyObject *cause)
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005222{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005223 PyObject *type = NULL, *value = NULL;
Collin Winter828f04a2007-08-31 00:04:24 +00005224
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005225 if (exc == NULL) {
5226 /* Reraise */
Mark Shannonae3087c2017-10-22 22:41:51 +01005227 _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005228 PyObject *tb;
Mark Shannonae3087c2017-10-22 22:41:51 +01005229 type = exc_info->exc_type;
5230 value = exc_info->exc_value;
5231 tb = exc_info->exc_traceback;
Victor Stinnereec93312016-08-18 18:13:10 +02005232 if (type == Py_None || type == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005233 _PyErr_SetString(tstate, PyExc_RuntimeError,
5234 "No active exception to reraise");
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005235 return 0;
5236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005237 Py_XINCREF(type);
5238 Py_XINCREF(value);
5239 Py_XINCREF(tb);
Victor Stinner438a12d2019-05-24 17:01:38 +02005240 _PyErr_Restore(tstate, type, value, tb);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005241 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005242 }
Guido van Rossumac7be682001-01-17 15:42:30 +00005243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005244 /* We support the following forms of raise:
5245 raise
Collin Winter828f04a2007-08-31 00:04:24 +00005246 raise <instance>
5247 raise <type> */
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005248
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005249 if (PyExceptionClass_Check(exc)) {
5250 type = exc;
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005251 value = _PyObject_CallNoArg(exc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005252 if (value == NULL)
5253 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005254 if (!PyExceptionInstance_Check(value)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005255 _PyErr_Format(tstate, PyExc_TypeError,
5256 "calling %R should have returned an instance of "
5257 "BaseException, not %R",
5258 type, Py_TYPE(value));
5259 goto raise_error;
Benjamin Peterson5afa03a2011-07-15 14:09:26 -05005260 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005261 }
5262 else if (PyExceptionInstance_Check(exc)) {
5263 value = exc;
5264 type = PyExceptionInstance_Class(exc);
5265 Py_INCREF(type);
5266 }
5267 else {
5268 /* Not something you can raise. You get an exception
5269 anyway, just not what you specified :-) */
5270 Py_DECREF(exc);
Victor Stinner438a12d2019-05-24 17:01:38 +02005271 _PyErr_SetString(tstate, PyExc_TypeError,
5272 "exceptions must derive from BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005273 goto raise_error;
5274 }
Collin Winter828f04a2007-08-31 00:04:24 +00005275
Serhiy Storchakac0191582016-09-27 11:37:10 +03005276 assert(type != NULL);
5277 assert(value != NULL);
5278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005279 if (cause) {
5280 PyObject *fixed_cause;
5281 if (PyExceptionClass_Check(cause)) {
Victor Stinnera5ed5f02016-12-06 18:45:50 +01005282 fixed_cause = _PyObject_CallNoArg(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005283 if (fixed_cause == NULL)
5284 goto raise_error;
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005285 Py_DECREF(cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005286 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005287 else if (PyExceptionInstance_Check(cause)) {
5288 fixed_cause = cause;
5289 }
5290 else if (cause == Py_None) {
5291 Py_DECREF(cause);
5292 fixed_cause = NULL;
5293 }
5294 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005295 _PyErr_SetString(tstate, PyExc_TypeError,
5296 "exception causes must derive from "
5297 "BaseException");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005298 goto raise_error;
5299 }
Benjamin Petersond5a1c442012-05-14 22:09:31 -07005300 PyException_SetCause(value, fixed_cause);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005301 }
Collin Winter828f04a2007-08-31 00:04:24 +00005302
Victor Stinner438a12d2019-05-24 17:01:38 +02005303 _PyErr_SetObject(tstate, type, value);
Victor Stinner61f4db82020-01-28 03:37:45 +01005304 /* _PyErr_SetObject incref's its arguments */
Serhiy Storchakac0191582016-09-27 11:37:10 +03005305 Py_DECREF(value);
5306 Py_DECREF(type);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005307 return 0;
Collin Winter828f04a2007-08-31 00:04:24 +00005308
5309raise_error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005310 Py_XDECREF(value);
5311 Py_XDECREF(type);
5312 Py_XDECREF(cause);
Benjamin Peterson31a58ff2012-10-12 11:34:51 -04005313 return 0;
Guido van Rossum0aa9ee61996-12-10 18:07:35 +00005314}
5315
Tim Petersd6d010b2001-06-21 02:49:55 +00005316/* Iterate v argcnt times and store the results on the stack (via decreasing
Guido van Rossum0368b722007-05-11 16:50:42 +00005317 sp). Return 1 for success, 0 if error.
Antoine Pitrou9a2310d2008-07-25 22:39:39 +00005318
Guido van Rossum0368b722007-05-11 16:50:42 +00005319 If argcntafter == -1, do a simple unpack. If it is >= 0, do an unpack
5320 with a variable target.
5321*/
Tim Petersd6d010b2001-06-21 02:49:55 +00005322
Barry Warsawe42b18f1997-08-25 22:13:04 +00005323static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005324unpack_iterable(PyThreadState *tstate, PyObject *v,
5325 int argcnt, int argcntafter, PyObject **sp)
Barry Warsawe42b18f1997-08-25 22:13:04 +00005326{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 int i = 0, j = 0;
5328 Py_ssize_t ll = 0;
5329 PyObject *it; /* iter(v) */
5330 PyObject *w;
5331 PyObject *l = NULL; /* variable list */
Guido van Rossumac7be682001-01-17 15:42:30 +00005332
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005333 assert(v != NULL);
Tim Petersd6d010b2001-06-21 02:49:55 +00005334
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005335 it = PyObject_GetIter(v);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005336 if (it == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005337 if (_PyErr_ExceptionMatches(tstate, PyExc_TypeError) &&
Victor Stinnera102ed72020-02-07 02:24:48 +01005338 Py_TYPE(v)->tp_iter == NULL && !PySequence_Check(v))
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005339 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005340 _PyErr_Format(tstate, PyExc_TypeError,
5341 "cannot unpack non-iterable %.200s object",
Victor Stinnera102ed72020-02-07 02:24:48 +01005342 Py_TYPE(v)->tp_name);
Serhiy Storchaka13a6c092017-12-26 12:30:41 +02005343 }
5344 return 0;
5345 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005346
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005347 for (; i < argcnt; i++) {
5348 w = PyIter_Next(it);
5349 if (w == NULL) {
5350 /* Iterator done, via error or exhaustion. */
Victor Stinner438a12d2019-05-24 17:01:38 +02005351 if (!_PyErr_Occurred(tstate)) {
R David Murray4171bbe2015-04-15 17:08:45 -04005352 if (argcntafter == -1) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005353 _PyErr_Format(tstate, PyExc_ValueError,
5354 "not enough values to unpack "
5355 "(expected %d, got %d)",
5356 argcnt, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005357 }
5358 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005359 _PyErr_Format(tstate, PyExc_ValueError,
5360 "not enough values to unpack "
5361 "(expected at least %d, got %d)",
5362 argcnt + argcntafter, i);
R David Murray4171bbe2015-04-15 17:08:45 -04005363 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005364 }
5365 goto Error;
5366 }
5367 *--sp = w;
5368 }
Tim Petersd6d010b2001-06-21 02:49:55 +00005369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 if (argcntafter == -1) {
5371 /* We better have exhausted the iterator now. */
5372 w = PyIter_Next(it);
5373 if (w == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005374 if (_PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005375 goto Error;
5376 Py_DECREF(it);
5377 return 1;
5378 }
5379 Py_DECREF(w);
Victor Stinner438a12d2019-05-24 17:01:38 +02005380 _PyErr_Format(tstate, PyExc_ValueError,
5381 "too many values to unpack (expected %d)",
5382 argcnt);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005383 goto Error;
5384 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005386 l = PySequence_List(it);
5387 if (l == NULL)
5388 goto Error;
5389 *--sp = l;
5390 i++;
Guido van Rossum0368b722007-05-11 16:50:42 +00005391
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005392 ll = PyList_GET_SIZE(l);
5393 if (ll < argcntafter) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005394 _PyErr_Format(tstate, PyExc_ValueError,
R David Murray4171bbe2015-04-15 17:08:45 -04005395 "not enough values to unpack (expected at least %d, got %zd)",
5396 argcnt + argcntafter, argcnt + ll);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005397 goto Error;
5398 }
Guido van Rossum0368b722007-05-11 16:50:42 +00005399
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005400 /* Pop the "after-variable" args off the list. */
5401 for (j = argcntafter; j > 0; j--, i++) {
5402 *--sp = PyList_GET_ITEM(l, ll - j);
5403 }
5404 /* Resize the list. */
Victor Stinner60ac6ed2020-02-07 23:18:08 +01005405 Py_SET_SIZE(l, ll - argcntafter);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005406 Py_DECREF(it);
5407 return 1;
Guido van Rossum0368b722007-05-11 16:50:42 +00005408
Tim Petersd6d010b2001-06-21 02:49:55 +00005409Error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005410 for (; i > 0; i--, sp++)
5411 Py_DECREF(*sp);
5412 Py_XDECREF(it);
5413 return 0;
Barry Warsawe42b18f1997-08-25 22:13:04 +00005414}
5415
5416
Guido van Rossum96a42c81992-01-12 02:29:51 +00005417#ifdef LLTRACE
Guido van Rossum3f5da241990-12-20 15:06:42 +00005418static int
Victor Stinner438a12d2019-05-24 17:01:38 +02005419prtrace(PyThreadState *tstate, PyObject *v, const char *str)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005420{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005421 printf("%s ", str);
Victor Stinner438a12d2019-05-24 17:01:38 +02005422 if (PyObject_Print(v, stdout, 0) != 0) {
5423 /* Don't know what else to do */
5424 _PyErr_Clear(tstate);
5425 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005426 printf("\n");
5427 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005428}
Guido van Rossum3f5da241990-12-20 15:06:42 +00005429#endif
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005430
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005431static void
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005432call_exc_trace(Py_tracefunc func, PyObject *self,
Mark Shannon86433452021-01-07 16:49:02 +00005433 PyThreadState *tstate,
5434 PyFrameObject *f,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005435 PyTraceInfo *trace_info)
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005436{
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005437 PyObject *type, *value, *traceback, *orig_traceback, *arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005438 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005439 _PyErr_Fetch(tstate, &type, &value, &orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005440 if (value == NULL) {
5441 value = Py_None;
5442 Py_INCREF(value);
5443 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005444 _PyErr_NormalizeException(tstate, &type, &value, &orig_traceback);
Antoine Pitrou89335212013-11-23 14:05:23 +01005445 traceback = (orig_traceback != NULL) ? orig_traceback : Py_None;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005446 arg = PyTuple_Pack(3, type, value, traceback);
5447 if (arg == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005448 _PyErr_Restore(tstate, type, value, orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005449 return;
5450 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005451 err = call_trace(func, self, tstate, f, trace_info, PyTrace_EXCEPTION, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005452 Py_DECREF(arg);
Victor Stinner438a12d2019-05-24 17:01:38 +02005453 if (err == 0) {
5454 _PyErr_Restore(tstate, type, value, orig_traceback);
5455 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005456 else {
5457 Py_XDECREF(type);
5458 Py_XDECREF(value);
Victor Stinneraaa8ed82013-07-10 13:57:55 +02005459 Py_XDECREF(orig_traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005460 }
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005461}
5462
Amaury Forgeot d'Arcf05149a2007-11-13 01:05:30 +00005463static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005464call_trace_protected(Py_tracefunc func, PyObject *obj,
5465 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005466 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005467 int what, PyObject *arg)
Fred Drake4ec5d562001-10-04 19:26:43 +00005468{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005469 PyObject *type, *value, *traceback;
5470 int err;
Victor Stinner438a12d2019-05-24 17:01:38 +02005471 _PyErr_Fetch(tstate, &type, &value, &traceback);
Mark Shannon8e1b4062021-03-05 14:45:50 +00005472 err = call_trace(func, obj, tstate, frame, trace_info, what, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005473 if (err == 0)
5474 {
Victor Stinner438a12d2019-05-24 17:01:38 +02005475 _PyErr_Restore(tstate, type, value, traceback);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 return 0;
5477 }
5478 else {
5479 Py_XDECREF(type);
5480 Py_XDECREF(value);
5481 Py_XDECREF(traceback);
5482 return -1;
5483 }
Fred Drake4ec5d562001-10-04 19:26:43 +00005484}
5485
Mark Shannon8e1b4062021-03-05 14:45:50 +00005486static void
5487initialize_trace_info(PyTraceInfo *trace_info, PyFrameObject *frame)
5488{
5489 if (trace_info->code != frame->f_code) {
5490 trace_info->code = frame->f_code;
5491 trace_info->instr_prev = -1;
5492 _PyCode_InitAddressRange(frame->f_code, &trace_info->bounds);
5493 }
5494}
5495
Guido van Rossum9c8d70d1992-03-23 18:19:28 +00005496static int
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005497call_trace(Py_tracefunc func, PyObject *obj,
5498 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005499 PyTraceInfo *trace_info,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005500 int what, PyObject *arg)
Guido van Rossum96a42c81992-01-12 02:29:51 +00005501{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005502 int result;
5503 if (tstate->tracing)
5504 return 0;
5505 tstate->tracing++;
5506 tstate->use_tracing = 0;
Mark Shannon86433452021-01-07 16:49:02 +00005507 if (frame->f_lasti < 0) {
5508 frame->f_lineno = frame->f_code->co_firstlineno;
5509 }
5510 else {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005511 initialize_trace_info(trace_info, frame);
5512 frame->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti, &trace_info->bounds);
Mark Shannon86433452021-01-07 16:49:02 +00005513 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005514 result = func(obj, frame, what, arg);
Mark Shannon86433452021-01-07 16:49:02 +00005515 frame->f_lineno = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005516 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5517 || (tstate->c_profilefunc != NULL));
5518 tstate->tracing--;
5519 return result;
Guido van Rossum96a42c81992-01-12 02:29:51 +00005520}
5521
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005522PyObject *
5523_PyEval_CallTracing(PyObject *func, PyObject *args)
5524{
Victor Stinner50b48572018-11-01 01:51:40 +01005525 PyThreadState *tstate = _PyThreadState_GET();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005526 int save_tracing = tstate->tracing;
5527 int save_use_tracing = tstate->use_tracing;
5528 PyObject *result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005530 tstate->tracing = 0;
5531 tstate->use_tracing = ((tstate->c_tracefunc != NULL)
5532 || (tstate->c_profilefunc != NULL));
5533 result = PyObject_Call(func, args, NULL);
5534 tstate->tracing = save_tracing;
5535 tstate->use_tracing = save_use_tracing;
5536 return result;
Guido van Rossuma12fe4e2003-04-09 19:06:21 +00005537}
5538
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005539/* See Objects/lnotab_notes.txt for a description of how tracing works. */
Michael W. Hudson006c7522002-11-08 13:08:46 +00005540static int
Tim Peters8a5c3c72004-04-05 19:36:21 +00005541maybe_call_line_trace(Py_tracefunc func, PyObject *obj,
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005542 PyThreadState *tstate, PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005543 PyTraceInfo *trace_info)
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005545 int result = 0;
Michael W. Hudson006c7522002-11-08 13:08:46 +00005546
Nick Coghlan5a851672017-09-08 10:14:16 +10005547 /* If the last instruction falls at the start of a line or if it
5548 represents a jump backwards, update the frame's line number and
5549 then call the trace function if we're tracing source lines.
5550 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005551 initialize_trace_info(trace_info, frame);
5552 int lastline = trace_info->bounds.ar_line;
5553 int line = _PyCode_CheckLineNumber(frame->f_lasti, &trace_info->bounds);
Mark Shannonee9f98d2021-01-05 12:04:10 +00005554 if (line != -1 && frame->f_trace_lines) {
5555 /* Trace backward edges or first instruction of a new line */
Mark Shannon8e1b4062021-03-05 14:45:50 +00005556 if (frame->f_lasti < trace_info->instr_prev ||
5557 (line != lastline && frame->f_lasti == trace_info->bounds.ar_start))
Mark Shannonee9f98d2021-01-05 12:04:10 +00005558 {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005559 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_LINE, Py_None);
Nick Coghlan5a851672017-09-08 10:14:16 +10005560 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005561 }
George King20faa682017-10-18 17:44:22 -07005562 /* Always emit an opcode event if we're tracing all opcodes. */
5563 if (frame->f_trace_opcodes) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005564 result = call_trace(func, obj, tstate, frame, trace_info, PyTrace_OPCODE, Py_None);
George King20faa682017-10-18 17:44:22 -07005565 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00005566 trace_info->instr_prev = frame->f_lasti;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005567 return result;
Michael W. Hudsondd32a912002-08-15 14:59:02 +00005568}
5569
Victor Stinner309d7cc2020-03-13 16:39:12 +01005570int
5571_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5572{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005573 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005574 /* The caller must hold the GIL */
5575 assert(PyGILState_Check());
5576
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005577 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005578 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005579 PyThreadState *current_tstate = _PyThreadState_GET();
5580 if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005581 return -1;
5582 }
5583
5584 PyObject *profileobj = tstate->c_profileobj;
5585
5586 tstate->c_profilefunc = NULL;
5587 tstate->c_profileobj = NULL;
5588 /* Must make sure that tracing is not ignored if 'profileobj' is freed */
5589 tstate->use_tracing = tstate->c_tracefunc != NULL;
5590 Py_XDECREF(profileobj);
5591
5592 Py_XINCREF(arg);
5593 tstate->c_profileobj = arg;
5594 tstate->c_profilefunc = func;
5595
5596 /* Flag that tracing or profiling is turned on */
5597 tstate->use_tracing = (func != NULL) || (tstate->c_tracefunc != NULL);
5598 return 0;
5599}
5600
Fred Drake5755ce62001-06-27 19:19:46 +00005601void
5602PyEval_SetProfile(Py_tracefunc func, PyObject *arg)
Fred Draked0838392001-06-16 21:02:31 +00005603{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005604 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005605 if (_PyEval_SetProfile(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005606 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005607 _PyErr_WriteUnraisableMsg("in PyEval_SetProfile", NULL);
5608 }
Victor Stinner309d7cc2020-03-13 16:39:12 +01005609}
5610
5611int
5612_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
5613{
Victor Stinnerda2914d2020-03-20 09:29:08 +01005614 assert(is_tstate_valid(tstate));
Victor Stinner309d7cc2020-03-13 16:39:12 +01005615 /* The caller must hold the GIL */
5616 assert(PyGILState_Check());
5617
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005618 /* Call _PySys_Audit() in the context of the current thread state,
Victor Stinner309d7cc2020-03-13 16:39:12 +01005619 even if tstate is not the current thread state. */
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005620 PyThreadState *current_tstate = _PyThreadState_GET();
5621 if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
Victor Stinner309d7cc2020-03-13 16:39:12 +01005622 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005623 }
5624
Victor Stinnerda2914d2020-03-20 09:29:08 +01005625 struct _ceval_state *ceval2 = &tstate->interp->ceval;
Victor Stinner309d7cc2020-03-13 16:39:12 +01005626 PyObject *traceobj = tstate->c_traceobj;
Victor Stinnerda2914d2020-03-20 09:29:08 +01005627 ceval2->tracing_possible += (func != NULL) - (tstate->c_tracefunc != NULL);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005628
5629 tstate->c_tracefunc = NULL;
5630 tstate->c_traceobj = NULL;
5631 /* Must make sure that profiling is not ignored if 'traceobj' is freed */
5632 tstate->use_tracing = (tstate->c_profilefunc != NULL);
5633 Py_XDECREF(traceobj);
5634
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005635 Py_XINCREF(arg);
Victor Stinner309d7cc2020-03-13 16:39:12 +01005636 tstate->c_traceobj = arg;
5637 tstate->c_tracefunc = func;
5638
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005639 /* Flag that tracing or profiling is turned on */
Victor Stinner309d7cc2020-03-13 16:39:12 +01005640 tstate->use_tracing = ((func != NULL)
5641 || (tstate->c_profilefunc != NULL));
5642
5643 return 0;
Fred Drake5755ce62001-06-27 19:19:46 +00005644}
5645
5646void
5647PyEval_SetTrace(Py_tracefunc func, PyObject *arg)
5648{
Victor Stinner309d7cc2020-03-13 16:39:12 +01005649 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinnerf6a58502020-03-16 17:41:44 +01005650 if (_PyEval_SetTrace(tstate, func, arg) < 0) {
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005651 /* Log _PySys_Audit() error */
Victor Stinnerf6a58502020-03-16 17:41:44 +01005652 _PyErr_WriteUnraisableMsg("in PyEval_SetTrace", NULL);
5653 }
Fred Draked0838392001-06-16 21:02:31 +00005654}
5655
Victor Stinner309d7cc2020-03-13 16:39:12 +01005656
Yury Selivanov75445082015-05-11 22:57:16 -04005657void
Victor Stinner838f2642019-06-13 22:41:23 +02005658_PyEval_SetCoroutineOriginTrackingDepth(PyThreadState *tstate, int new_depth)
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005659{
5660 assert(new_depth >= 0);
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005661 tstate->coroutine_origin_tracking_depth = new_depth;
5662}
5663
5664int
5665_PyEval_GetCoroutineOriginTrackingDepth(void)
5666{
Victor Stinner50b48572018-11-01 01:51:40 +01005667 PyThreadState *tstate = _PyThreadState_GET();
Nathaniel J. Smithfc2f4072018-01-21 06:44:07 -08005668 return tstate->coroutine_origin_tracking_depth;
5669}
5670
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005671int
Yury Selivanoveb636452016-09-08 22:01:51 -07005672_PyEval_SetAsyncGenFirstiter(PyObject *firstiter)
5673{
Victor Stinner50b48572018-11-01 01:51:40 +01005674 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005675
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005676 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_firstiter", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005677 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005678 }
5679
Yury Selivanoveb636452016-09-08 22:01:51 -07005680 Py_XINCREF(firstiter);
5681 Py_XSETREF(tstate->async_gen_firstiter, firstiter);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005682 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005683}
5684
5685PyObject *
5686_PyEval_GetAsyncGenFirstiter(void)
5687{
Victor Stinner50b48572018-11-01 01:51:40 +01005688 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005689 return tstate->async_gen_firstiter;
5690}
5691
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005692int
Yury Selivanoveb636452016-09-08 22:01:51 -07005693_PyEval_SetAsyncGenFinalizer(PyObject *finalizer)
5694{
Victor Stinner50b48572018-11-01 01:51:40 +01005695 PyThreadState *tstate = _PyThreadState_GET();
Steve Dowerb82e17e2019-05-23 08:45:22 -07005696
Victor Stinner1c1e68c2020-03-27 15:11:45 +01005697 if (_PySys_Audit(tstate, "sys.set_asyncgen_hook_finalizer", NULL) < 0) {
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005698 return -1;
Steve Dowerb82e17e2019-05-23 08:45:22 -07005699 }
5700
Yury Selivanoveb636452016-09-08 22:01:51 -07005701 Py_XINCREF(finalizer);
5702 Py_XSETREF(tstate->async_gen_finalizer, finalizer);
Zackery Spytz79ceccd2020-03-26 06:11:13 -06005703 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -07005704}
5705
5706PyObject *
5707_PyEval_GetAsyncGenFinalizer(void)
5708{
Victor Stinner50b48572018-11-01 01:51:40 +01005709 PyThreadState *tstate = _PyThreadState_GET();
Yury Selivanoveb636452016-09-08 22:01:51 -07005710 return tstate->async_gen_finalizer;
5711}
5712
Victor Stinner438a12d2019-05-24 17:01:38 +02005713PyFrameObject *
5714PyEval_GetFrame(void)
5715{
5716 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005717 return tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005718}
5719
Guido van Rossumb209a111997-04-29 18:18:01 +00005720PyObject *
Victor Stinner46496f92021-02-20 15:17:18 +01005721_PyEval_GetBuiltins(PyThreadState *tstate)
5722{
5723 PyFrameObject *frame = tstate->frame;
5724 if (frame != NULL) {
5725 return frame->f_builtins;
5726 }
5727 return tstate->interp->builtins;
5728}
5729
5730PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005731PyEval_GetBuiltins(void)
Guido van Rossum6135a871995-01-09 17:53:26 +00005732{
Victor Stinner438a12d2019-05-24 17:01:38 +02005733 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner46496f92021-02-20 15:17:18 +01005734 return _PyEval_GetBuiltins(tstate);
Guido van Rossum6135a871995-01-09 17:53:26 +00005735}
5736
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005737/* Convenience function to get a builtin from its name */
5738PyObject *
5739_PyEval_GetBuiltinId(_Py_Identifier *name)
5740{
Victor Stinner438a12d2019-05-24 17:01:38 +02005741 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005742 PyObject *attr = _PyDict_GetItemIdWithError(PyEval_GetBuiltins(), name);
5743 if (attr) {
5744 Py_INCREF(attr);
5745 }
Victor Stinner438a12d2019-05-24 17:01:38 +02005746 else if (!_PyErr_Occurred(tstate)) {
5747 _PyErr_SetObject(tstate, PyExc_AttributeError, _PyUnicode_FromId(name));
Serhiy Storchakabb86bf42018-12-11 08:28:18 +02005748 }
5749 return attr;
5750}
5751
Guido van Rossumb209a111997-04-29 18:18:01 +00005752PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005753PyEval_GetLocals(void)
Guido van Rossum5b722181993-03-30 17:46:03 +00005754{
Victor Stinner438a12d2019-05-24 17:01:38 +02005755 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005756 PyFrameObject *current_frame = tstate->frame;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005757 if (current_frame == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02005758 _PyErr_SetString(tstate, PyExc_SystemError, "frame does not exist");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005759 return NULL;
Victor Stinner41bb43a2013-10-29 01:19:37 +01005760 }
5761
Victor Stinner438a12d2019-05-24 17:01:38 +02005762 if (PyFrame_FastToLocalsWithError(current_frame) < 0) {
Victor Stinner41bb43a2013-10-29 01:19:37 +01005763 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005764 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005765
5766 assert(current_frame->f_locals != NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005767 return current_frame->f_locals;
Guido van Rossum5b722181993-03-30 17:46:03 +00005768}
5769
Guido van Rossumb209a111997-04-29 18:18:01 +00005770PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00005771PyEval_GetGlobals(void)
Guido van Rossum3f5da241990-12-20 15:06:42 +00005772{
Victor Stinner438a12d2019-05-24 17:01:38 +02005773 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005774 PyFrameObject *current_frame = tstate->frame;
Victor Stinner438a12d2019-05-24 17:01:38 +02005775 if (current_frame == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005776 return NULL;
Victor Stinner438a12d2019-05-24 17:01:38 +02005777 }
Victor Stinner41bb43a2013-10-29 01:19:37 +01005778
5779 assert(current_frame->f_globals != NULL);
5780 return current_frame->f_globals;
Guido van Rossum3f5da241990-12-20 15:06:42 +00005781}
5782
Guido van Rossum6135a871995-01-09 17:53:26 +00005783int
Tim Peters5ba58662001-07-16 02:29:45 +00005784PyEval_MergeCompilerFlags(PyCompilerFlags *cf)
Jeremy Hylton061d1062001-03-22 02:32:48 +00005785{
Victor Stinner438a12d2019-05-24 17:01:38 +02005786 PyThreadState *tstate = _PyThreadState_GET();
Victor Stinner6723e932020-03-20 17:46:56 +01005787 PyFrameObject *current_frame = tstate->frame;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005788 int result = cf->cf_flags != 0;
Tim Peters5ba58662001-07-16 02:29:45 +00005789
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005790 if (current_frame != NULL) {
5791 const int codeflags = current_frame->f_code->co_flags;
5792 const int compilerflags = codeflags & PyCF_MASK;
5793 if (compilerflags) {
5794 result = 1;
5795 cf->cf_flags |= compilerflags;
5796 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005797#if 0 /* future keyword */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005798 if (codeflags & CO_GENERATOR_ALLOWED) {
5799 result = 1;
5800 cf->cf_flags |= CO_GENERATOR_ALLOWED;
5801 }
Neil Schemenauerc24ea082002-03-22 23:53:36 +00005802#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005803 }
5804 return result;
Jeremy Hylton061d1062001-03-22 02:32:48 +00005805}
5806
Guido van Rossum3f5da241990-12-20 15:06:42 +00005807
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005808const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005809PyEval_GetFuncName(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005810{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005811 if (PyMethod_Check(func))
5812 return PyEval_GetFuncName(PyMethod_GET_FUNCTION(func));
5813 else if (PyFunction_Check(func))
Serhiy Storchaka06515832016-11-20 09:13:07 +02005814 return PyUnicode_AsUTF8(((PyFunctionObject*)func)->func_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005815 else if (PyCFunction_Check(func))
5816 return ((PyCFunctionObject*)func)->m_ml->ml_name;
5817 else
Victor Stinnera102ed72020-02-07 02:24:48 +01005818 return Py_TYPE(func)->tp_name;
Jeremy Hylton512a2372001-04-11 13:52:29 +00005819}
5820
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00005821const char *
Tim Peters6d6c1a32001-08-02 04:15:00 +00005822PyEval_GetFuncDesc(PyObject *func)
Jeremy Hylton512a2372001-04-11 13:52:29 +00005823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005824 if (PyMethod_Check(func))
5825 return "()";
5826 else if (PyFunction_Check(func))
5827 return "()";
5828 else if (PyCFunction_Check(func))
5829 return "()";
5830 else
5831 return " object";
Jeremy Hylton512a2372001-04-11 13:52:29 +00005832}
5833
Armin Rigo1c2d7e52005-09-20 18:34:01 +00005834#define C_TRACE(x, call) \
Nicholas Bastind858a772004-06-25 23:31:06 +00005835if (tstate->use_tracing && tstate->c_profilefunc) { \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005836 if (call_trace(tstate->c_profilefunc, tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005837 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005838 PyTrace_C_CALL, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005839 x = NULL; \
5840 } \
5841 else { \
5842 x = call; \
5843 if (tstate->c_profilefunc != NULL) { \
5844 if (x == NULL) { \
5845 call_trace_protected(tstate->c_profilefunc, \
5846 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005847 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005848 PyTrace_C_EXCEPTION, func); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005849 /* XXX should pass (type, value, tb) */ \
5850 } else { \
5851 if (call_trace(tstate->c_profilefunc, \
5852 tstate->c_profileobj, \
Mark Shannon8e1b4062021-03-05 14:45:50 +00005853 tstate, tstate->frame, trace_info, \
Victor Stinnerfdeb6ec2013-12-13 02:01:38 +01005854 PyTrace_C_RETURN, func)) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005855 Py_DECREF(x); \
5856 x = NULL; \
5857 } \
5858 } \
5859 } \
5860 } \
Nicholas Bastind858a772004-06-25 23:31:06 +00005861} else { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005862 x = call; \
5863 }
Nicholas Bastinc69ebe82004-03-24 21:57:10 +00005864
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005865
5866static PyObject *
5867trace_call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005868 PyTraceInfo *trace_info,
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005869 PyObject *func,
5870 PyObject **args, Py_ssize_t nargs,
5871 PyObject *kwnames)
5872{
5873 PyObject *x;
scoder4c9ea092020-05-12 16:12:41 +02005874 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Petr Viktorinffd97532020-02-11 17:46:57 +01005875 C_TRACE(x, PyObject_Vectorcall(func, args, nargs, kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005876 return x;
5877 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005878 else if (Py_IS_TYPE(func, &PyMethodDescr_Type) && nargs > 0) {
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005879 /* We need to create a temporary bound method as argument
5880 for profiling.
5881
5882 If nargs == 0, then this cannot work because we have no
5883 "self". In any case, the call itself would raise
5884 TypeError (foo needs an argument), so we just skip
5885 profiling. */
5886 PyObject *self = args[0];
5887 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5888 if (func == NULL) {
5889 return NULL;
5890 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005891 C_TRACE(x, PyObject_Vectorcall(func,
Jeroen Demeyer0d722f32019-07-05 14:48:24 +02005892 args+1, nargs-1,
5893 kwnames));
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005894 Py_DECREF(func);
5895 return x;
5896 }
Petr Viktorinffd97532020-02-11 17:46:57 +01005897 return PyObject_Vectorcall(func, args, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005898}
5899
Victor Stinner415c5102017-01-11 00:54:57 +01005900/* Issue #29227: Inline call_function() into _PyEval_EvalFrameDefault()
5901 to reduce the stack consumption. */
5902Py_LOCAL_INLINE(PyObject *) _Py_HOT_FUNCTION
Mark Shannon86433452021-01-07 16:49:02 +00005903call_function(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005904 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005905 PyObject ***pp_stack,
5906 Py_ssize_t oparg,
5907 PyObject *kwnames)
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005908{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005909 PyObject **pfunc = (*pp_stack) - oparg - 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005910 PyObject *func = *pfunc;
5911 PyObject *x, *w;
Victor Stinnerd8735722016-09-09 12:36:44 -07005912 Py_ssize_t nkwargs = (kwnames == NULL) ? 0 : PyTuple_GET_SIZE(kwnames);
5913 Py_ssize_t nargs = oparg - nkwargs;
INADA Naoki5566bbb2017-02-03 07:43:03 +09005914 PyObject **stack = (*pp_stack) - nargs - nkwargs;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005915
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +02005916 if (tstate->use_tracing) {
Mark Shannon8e1b4062021-03-05 14:45:50 +00005917 x = trace_call_function(tstate, trace_info, func, stack, nargs, kwnames);
INADA Naoki5566bbb2017-02-03 07:43:03 +09005918 }
Victor Stinner4a7cc882015-03-06 23:35:27 +01005919 else {
Petr Viktorinffd97532020-02-11 17:46:57 +01005920 x = PyObject_Vectorcall(func, stack, nargs | PY_VECTORCALL_ARGUMENTS_OFFSET, kwnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005921 }
Tim Peters8a5c3c72004-04-05 19:36:21 +00005922
Victor Stinner438a12d2019-05-24 17:01:38 +02005923 assert((x != NULL) ^ (_PyErr_Occurred(tstate) != NULL));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005924
Victor Stinnerc22bfaa2017-02-12 19:27:05 +01005925 /* Clear the stack of the function object. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005926 while ((*pp_stack) > pfunc) {
5927 w = EXT_POP(*pp_stack);
5928 Py_DECREF(w);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005929 }
Victor Stinnerace47d72013-07-18 01:41:08 +02005930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005931 return x;
Jeremy Hyltone8c04322002-08-16 17:47:26 +00005932}
5933
Jeremy Hylton52820442001-01-03 23:52:36 +00005934static PyObject *
Mark Shannon86433452021-01-07 16:49:02 +00005935do_call_core(PyThreadState *tstate,
Mark Shannon8e1b4062021-03-05 14:45:50 +00005936 PyTraceInfo *trace_info,
Mark Shannon86433452021-01-07 16:49:02 +00005937 PyObject *func,
5938 PyObject *callargs,
5939 PyObject *kwdict)
Jeremy Hylton52820442001-01-03 23:52:36 +00005940{
jdemeyere89de732018-09-19 12:06:20 +02005941 PyObject *result;
5942
scoder4c9ea092020-05-12 16:12:41 +02005943 if (PyCFunction_CheckExact(func) || PyCMethod_CheckExact(func)) {
Jeroen Demeyer7a6873c2019-09-11 13:01:01 +02005944 C_TRACE(result, PyObject_Call(func, callargs, kwdict));
Victor Stinnerf9b760f2016-09-09 10:17:08 -07005945 return result;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005946 }
Andy Lesterdffe4c02020-03-04 07:15:20 -06005947 else if (Py_IS_TYPE(func, &PyMethodDescr_Type)) {
jdemeyere89de732018-09-19 12:06:20 +02005948 Py_ssize_t nargs = PyTuple_GET_SIZE(callargs);
5949 if (nargs > 0 && tstate->use_tracing) {
5950 /* We need to create a temporary bound method as argument
5951 for profiling.
5952
5953 If nargs == 0, then this cannot work because we have no
5954 "self". In any case, the call itself would raise
5955 TypeError (foo needs an argument), so we just skip
5956 profiling. */
5957 PyObject *self = PyTuple_GET_ITEM(callargs, 0);
5958 func = Py_TYPE(func)->tp_descr_get(func, self, (PyObject*)Py_TYPE(self));
5959 if (func == NULL) {
5960 return NULL;
5961 }
5962
Victor Stinner4d231bc2019-11-14 13:36:21 +01005963 C_TRACE(result, _PyObject_FastCallDictTstate(
5964 tstate, func,
5965 &_PyTuple_ITEMS(callargs)[1],
5966 nargs - 1,
5967 kwdict));
jdemeyere89de732018-09-19 12:06:20 +02005968 Py_DECREF(func);
5969 return result;
5970 }
Victor Stinner74319ae2016-08-25 00:04:09 +02005971 }
jdemeyere89de732018-09-19 12:06:20 +02005972 return PyObject_Call(func, callargs, kwdict);
Jeremy Hylton52820442001-01-03 23:52:36 +00005973}
5974
Serhiy Storchaka483405b2015-02-17 10:14:30 +02005975/* Extract a slice index from a PyLong or an object with the
Guido van Rossum38fff8c2006-03-07 18:50:55 +00005976 nb_index slot defined, and store in *pi.
5977 Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
Xiang Zhang2ddf5a12017-05-10 18:19:41 +08005978 and silently boost values less than PY_SSIZE_T_MIN to PY_SSIZE_T_MIN.
Martin v. Löwisdde99d22006-02-17 15:57:41 +00005979 Return 0 on error, 1 on success.
Tim Peterscb479e72001-12-16 19:11:44 +00005980*/
Guido van Rossum20c6add2000-05-08 14:06:50 +00005981int
Martin v. Löwis18e16552006-02-15 17:27:45 +00005982_PyEval_SliceIndex(PyObject *v, Py_ssize_t *pi)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005983{
Victor Stinner438a12d2019-05-24 17:01:38 +02005984 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03005985 if (v != Py_None) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005986 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02005987 if (_PyIndex_Check(v)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005988 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02005989 if (x == -1 && _PyErr_Occurred(tstate))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005990 return 0;
5991 }
5992 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02005993 _PyErr_SetString(tstate, PyExc_TypeError,
5994 "slice indices must be integers or "
5995 "None or have an __index__ method");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005996 return 0;
5997 }
5998 *pi = x;
5999 }
6000 return 1;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00006001}
6002
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006003int
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006004_PyEval_SliceIndexNotNone(PyObject *v, Py_ssize_t *pi)
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006005{
Victor Stinner438a12d2019-05-24 17:01:38 +02006006 PyThreadState *tstate = _PyThreadState_GET();
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006007 Py_ssize_t x;
Victor Stinnera15e2602020-04-08 02:01:56 +02006008 if (_PyIndex_Check(v)) {
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006009 x = PyNumber_AsSsize_t(v, NULL);
Victor Stinner438a12d2019-05-24 17:01:38 +02006010 if (x == -1 && _PyErr_Occurred(tstate))
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006011 return 0;
6012 }
6013 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006014 _PyErr_SetString(tstate, PyExc_TypeError,
6015 "slice indices must be integers or "
6016 "have an __index__ method");
Serhiy Storchakad4edfc92017-03-30 18:29:23 +03006017 return 0;
6018 }
6019 *pi = x;
6020 return 1;
Serhiy Storchaka80ec8362017-03-19 19:37:40 +02006021}
6022
Thomas Wouters52152252000-08-17 22:55:00 +00006023static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006024import_name(PyThreadState *tstate, PyFrameObject *f,
6025 PyObject *name, PyObject *fromlist, PyObject *level)
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006026{
6027 _Py_IDENTIFIER(__import__);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006028 PyObject *import_func, *res;
6029 PyObject* stack[5];
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006030
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006031 import_func = _PyDict_GetItemIdWithError(f->f_builtins, &PyId___import__);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006032 if (import_func == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006033 if (!_PyErr_Occurred(tstate)) {
6034 _PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006035 }
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006036 return NULL;
6037 }
6038
6039 /* Fast path for not overloaded __import__. */
Victor Stinner438a12d2019-05-24 17:01:38 +02006040 if (import_func == tstate->interp->import_func) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006041 int ilevel = _PyLong_AsInt(level);
Victor Stinner438a12d2019-05-24 17:01:38 +02006042 if (ilevel == -1 && _PyErr_Occurred(tstate)) {
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006043 return NULL;
6044 }
6045 res = PyImport_ImportModuleLevelObject(
6046 name,
6047 f->f_globals,
6048 f->f_locals == NULL ? Py_None : f->f_locals,
6049 fromlist,
6050 ilevel);
6051 return res;
6052 }
6053
6054 Py_INCREF(import_func);
Victor Stinnerdf142fd2016-08-20 00:44:42 +02006055
6056 stack[0] = name;
6057 stack[1] = f->f_globals;
6058 stack[2] = f->f_locals == NULL ? Py_None : f->f_locals;
6059 stack[3] = fromlist;
6060 stack[4] = level;
Victor Stinner559bb6a2016-08-22 22:48:54 +02006061 res = _PyObject_FastCall(import_func, stack, 5);
Serhiy Storchaka133138a2016-08-02 22:51:21 +03006062 Py_DECREF(import_func);
6063 return res;
6064}
6065
6066static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006067import_from(PyThreadState *tstate, PyObject *v, PyObject *name)
Guido van Rossume9736fc1990-11-18 17:33:06 +00006068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006069 PyObject *x;
Xiang Zhang4830f582017-03-21 11:13:42 +08006070 PyObject *fullmodname, *pkgname, *pkgpath, *pkgname_or_unknown, *errmsg;
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006071
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006072 if (_PyObject_LookupAttr(v, name, &x) != 0) {
Antoine Pitrou0373a102014-10-13 20:19:45 +02006073 return x;
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006074 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006075 /* Issue #17636: in case this failed because of a circular relative
6076 import, try to fallback on reading the module directly from
6077 sys.modules. */
Antoine Pitrou0373a102014-10-13 20:19:45 +02006078 pkgname = _PyObject_GetAttrId(v, &PyId___name__);
Brett Cannon3008bc02015-08-11 18:01:31 -07006079 if (pkgname == NULL) {
6080 goto error;
6081 }
Oren Milman6db70332017-09-19 14:23:01 +03006082 if (!PyUnicode_Check(pkgname)) {
6083 Py_CLEAR(pkgname);
6084 goto error;
6085 }
Antoine Pitrou0373a102014-10-13 20:19:45 +02006086 fullmodname = PyUnicode_FromFormat("%U.%U", pkgname, name);
Brett Cannon3008bc02015-08-11 18:01:31 -07006087 if (fullmodname == NULL) {
Xiang Zhang4830f582017-03-21 11:13:42 +08006088 Py_DECREF(pkgname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006089 return NULL;
Brett Cannon3008bc02015-08-11 18:01:31 -07006090 }
Eric Snow3f9eee62017-09-15 16:35:20 -06006091 x = PyImport_GetModule(fullmodname);
Antoine Pitrou0373a102014-10-13 20:19:45 +02006092 Py_DECREF(fullmodname);
Victor Stinner438a12d2019-05-24 17:01:38 +02006093 if (x == NULL && !_PyErr_Occurred(tstate)) {
Brett Cannon3008bc02015-08-11 18:01:31 -07006094 goto error;
6095 }
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006096 Py_DECREF(pkgname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006097 return x;
Brett Cannon3008bc02015-08-11 18:01:31 -07006098 error:
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006099 pkgpath = PyModule_GetFilenameObject(v);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006100 if (pkgname == NULL) {
6101 pkgname_or_unknown = PyUnicode_FromString("<unknown module name>");
6102 if (pkgname_or_unknown == NULL) {
6103 Py_XDECREF(pkgpath);
6104 return NULL;
6105 }
6106 } else {
6107 pkgname_or_unknown = pkgname;
6108 }
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006109
6110 if (pkgpath == NULL || !PyUnicode_Check(pkgpath)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006111 _PyErr_Clear(tstate);
Xiang Zhang4830f582017-03-21 11:13:42 +08006112 errmsg = PyUnicode_FromFormat(
6113 "cannot import name %R from %R (unknown location)",
6114 name, pkgname_or_unknown
6115 );
Stefan Krah027b09c2019-03-25 21:50:58 +01006116 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006117 PyErr_SetImportError(errmsg, pkgname, NULL);
6118 }
6119 else {
Anthony Sottile65366bc2019-09-09 08:17:50 -07006120 _Py_IDENTIFIER(__spec__);
6121 PyObject *spec = _PyObject_GetAttrId(v, &PyId___spec__);
Anthony Sottile65366bc2019-09-09 08:17:50 -07006122 const char *fmt =
6123 _PyModuleSpec_IsInitializing(spec) ?
6124 "cannot import name %R from partially initialized module %R "
6125 "(most likely due to a circular import) (%S)" :
6126 "cannot import name %R from %R (%S)";
6127 Py_XDECREF(spec);
6128
6129 errmsg = PyUnicode_FromFormat(fmt, name, pkgname_or_unknown, pkgpath);
Stefan Krah027b09c2019-03-25 21:50:58 +01006130 /* NULL checks for errmsg and pkgname done by PyErr_SetImportError. */
Xiang Zhang4830f582017-03-21 11:13:42 +08006131 PyErr_SetImportError(errmsg, pkgname, pkgpath);
Matthias Bussonnierbc4bed42017-02-14 16:05:25 -08006132 }
6133
Xiang Zhang4830f582017-03-21 11:13:42 +08006134 Py_XDECREF(errmsg);
Matthias Bussonnier1bc15642017-02-22 07:06:50 -08006135 Py_XDECREF(pkgname_or_unknown);
6136 Py_XDECREF(pkgpath);
Brett Cannon3008bc02015-08-11 18:01:31 -07006137 return NULL;
Thomas Wouters52152252000-08-17 22:55:00 +00006138}
Guido van Rossumac7be682001-01-17 15:42:30 +00006139
Thomas Wouters52152252000-08-17 22:55:00 +00006140static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006141import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
Thomas Wouters52152252000-08-17 22:55:00 +00006142{
Martin v. Löwis1c67dd92011-10-14 15:16:45 +02006143 _Py_IDENTIFIER(__all__);
6144 _Py_IDENTIFIER(__dict__);
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006145 PyObject *all, *dict, *name, *value;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006146 int skip_leading_underscores = 0;
6147 int pos, err;
Thomas Wouters52152252000-08-17 22:55:00 +00006148
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006149 if (_PyObject_LookupAttrId(v, &PyId___all__, &all) < 0) {
6150 return -1; /* Unexpected error */
6151 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006152 if (all == NULL) {
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006153 if (_PyObject_LookupAttrId(v, &PyId___dict__, &dict) < 0) {
6154 return -1;
6155 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006156 if (dict == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006157 _PyErr_SetString(tstate, PyExc_ImportError,
Serhiy Storchakaf320be72018-01-25 10:49:40 +02006158 "from-import-* object has no __dict__ and no __all__");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006159 return -1;
6160 }
6161 all = PyMapping_Keys(dict);
6162 Py_DECREF(dict);
6163 if (all == NULL)
6164 return -1;
6165 skip_leading_underscores = 1;
6166 }
Guido van Rossum18d4d8f2001-01-12 16:24:03 +00006167
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006168 for (pos = 0, err = 0; ; pos++) {
6169 name = PySequence_GetItem(all, pos);
6170 if (name == NULL) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006171 if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006172 err = -1;
Victor Stinner438a12d2019-05-24 17:01:38 +02006173 }
6174 else {
6175 _PyErr_Clear(tstate);
6176 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006177 break;
6178 }
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006179 if (!PyUnicode_Check(name)) {
6180 PyObject *modname = _PyObject_GetAttrId(v, &PyId___name__);
6181 if (modname == NULL) {
6182 Py_DECREF(name);
6183 err = -1;
6184 break;
6185 }
6186 if (!PyUnicode_Check(modname)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006187 _PyErr_Format(tstate, PyExc_TypeError,
6188 "module __name__ must be a string, not %.100s",
6189 Py_TYPE(modname)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006190 }
6191 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006192 _PyErr_Format(tstate, PyExc_TypeError,
6193 "%s in %U.%s must be str, not %.100s",
6194 skip_leading_underscores ? "Key" : "Item",
6195 modname,
6196 skip_leading_underscores ? "__dict__" : "__all__",
6197 Py_TYPE(name)->tp_name);
Xiang Zhangd8b291a2018-03-24 18:39:36 +08006198 }
6199 Py_DECREF(modname);
6200 Py_DECREF(name);
6201 err = -1;
6202 break;
6203 }
6204 if (skip_leading_underscores) {
Serhiy Storchakae3b2b4b2017-09-08 09:58:51 +03006205 if (PyUnicode_READY(name) == -1) {
6206 Py_DECREF(name);
6207 err = -1;
6208 break;
6209 }
6210 if (PyUnicode_READ_CHAR(name, 0) == '_') {
6211 Py_DECREF(name);
6212 continue;
6213 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006214 }
6215 value = PyObject_GetAttr(v, name);
6216 if (value == NULL)
6217 err = -1;
6218 else if (PyDict_CheckExact(locals))
6219 err = PyDict_SetItem(locals, name, value);
6220 else
6221 err = PyObject_SetItem(locals, name, value);
6222 Py_DECREF(name);
6223 Py_XDECREF(value);
6224 if (err != 0)
6225 break;
6226 }
6227 Py_DECREF(all);
6228 return err;
Guido van Rossume9736fc1990-11-18 17:33:06 +00006229}
6230
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006231static int
Victor Stinner438a12d2019-05-24 17:01:38 +02006232check_args_iterable(PyThreadState *tstate, PyObject *func, PyObject *args)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006233{
Victor Stinnera102ed72020-02-07 02:24:48 +01006234 if (Py_TYPE(args)->tp_iter == NULL && !PySequence_Check(args)) {
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006235 /* check_args_iterable() may be called with a live exception:
6236 * clear it to prevent calling _PyObject_FunctionStr() with an
6237 * exception set. */
Victor Stinner61f4db82020-01-28 03:37:45 +01006238 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006239 PyObject *funcstr = _PyObject_FunctionStr(func);
6240 if (funcstr != NULL) {
6241 _PyErr_Format(tstate, PyExc_TypeError,
6242 "%U argument after * must be an iterable, not %.200s",
6243 funcstr, Py_TYPE(args)->tp_name);
6244 Py_DECREF(funcstr);
6245 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006246 return -1;
6247 }
6248 return 0;
6249}
6250
6251static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006252format_kwargs_error(PyThreadState *tstate, PyObject *func, PyObject *kwargs)
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006253{
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006254 /* _PyDict_MergeEx raises attribute
6255 * error (percolated from an attempt
6256 * to get 'keys' attribute) instead of
6257 * a type error if its second argument
6258 * is not a mapping.
6259 */
Victor Stinner438a12d2019-05-24 17:01:38 +02006260 if (_PyErr_ExceptionMatches(tstate, PyExc_AttributeError)) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006261 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006262 PyObject *funcstr = _PyObject_FunctionStr(func);
6263 if (funcstr != NULL) {
6264 _PyErr_Format(
6265 tstate, PyExc_TypeError,
6266 "%U argument after ** must be a mapping, not %.200s",
6267 funcstr, Py_TYPE(kwargs)->tp_name);
6268 Py_DECREF(funcstr);
6269 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006270 }
Victor Stinner438a12d2019-05-24 17:01:38 +02006271 else if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006272 PyObject *exc, *val, *tb;
Victor Stinner438a12d2019-05-24 17:01:38 +02006273 _PyErr_Fetch(tstate, &exc, &val, &tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006274 if (val && PyTuple_Check(val) && PyTuple_GET_SIZE(val) == 1) {
Victor Stinner61f4db82020-01-28 03:37:45 +01006275 _PyErr_Clear(tstate);
Jeroen Demeyerbf17d412019-11-05 16:48:04 +01006276 PyObject *funcstr = _PyObject_FunctionStr(func);
6277 if (funcstr != NULL) {
6278 PyObject *key = PyTuple_GET_ITEM(val, 0);
6279 _PyErr_Format(
6280 tstate, PyExc_TypeError,
6281 "%U got multiple values for keyword argument '%S'",
6282 funcstr, key);
6283 Py_DECREF(funcstr);
6284 }
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006285 Py_XDECREF(exc);
6286 Py_XDECREF(val);
6287 Py_XDECREF(tb);
6288 }
6289 else {
Victor Stinner438a12d2019-05-24 17:01:38 +02006290 _PyErr_Restore(tstate, exc, val, tb);
Serhiy Storchakaf1ec3ce2019-01-12 10:12:24 +02006291 }
6292 }
Serhiy Storchaka25e4f772017-08-03 11:37:15 +03006293}
6294
Guido van Rossumac7be682001-01-17 15:42:30 +00006295static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006296format_exc_check_arg(PyThreadState *tstate, PyObject *exc,
6297 const char *format_str, PyObject *obj)
Paul Prescode68140d2000-08-30 20:25:01 +00006298{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006299 const char *obj_str;
Paul Prescode68140d2000-08-30 20:25:01 +00006300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006301 if (!obj)
6302 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006303
Serhiy Storchaka06515832016-11-20 09:13:07 +02006304 obj_str = PyUnicode_AsUTF8(obj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006305 if (!obj_str)
6306 return;
Paul Prescode68140d2000-08-30 20:25:01 +00006307
Victor Stinner438a12d2019-05-24 17:01:38 +02006308 _PyErr_Format(tstate, exc, format_str, obj_str);
Paul Prescode68140d2000-08-30 20:25:01 +00006309}
Guido van Rossum950361c1997-01-24 13:49:28 +00006310
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006311static void
Victor Stinner438a12d2019-05-24 17:01:38 +02006312format_exc_unbound(PyThreadState *tstate, PyCodeObject *co, int oparg)
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006313{
6314 PyObject *name;
6315 /* Don't stomp existing exception */
Victor Stinner438a12d2019-05-24 17:01:38 +02006316 if (_PyErr_Occurred(tstate))
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006317 return;
6318 if (oparg < PyTuple_GET_SIZE(co->co_cellvars)) {
6319 name = PyTuple_GET_ITEM(co->co_cellvars,
6320 oparg);
Victor Stinner438a12d2019-05-24 17:01:38 +02006321 format_exc_check_arg(tstate,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006322 PyExc_UnboundLocalError,
6323 UNBOUNDLOCAL_ERROR_MSG,
6324 name);
6325 } else {
6326 name = PyTuple_GET_ITEM(co->co_freevars, oparg -
6327 PyTuple_GET_SIZE(co->co_cellvars));
Victor Stinner438a12d2019-05-24 17:01:38 +02006328 format_exc_check_arg(tstate, PyExc_NameError,
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00006329 UNBOUNDFREE_ERROR_MSG, name);
6330 }
6331}
6332
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006333static void
Mark Shannonfee55262019-11-21 09:11:43 +00006334format_awaitable_error(PyThreadState *tstate, PyTypeObject *type, int prevprevopcode, int prevopcode)
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006335{
6336 if (type->tp_as_async == NULL || type->tp_as_async->am_await == NULL) {
6337 if (prevopcode == BEFORE_ASYNC_WITH) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006338 _PyErr_Format(tstate, PyExc_TypeError,
6339 "'async with' received an object from __aenter__ "
6340 "that does not implement __await__: %.100s",
6341 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006342 }
Mark Shannonfee55262019-11-21 09:11:43 +00006343 else if (prevopcode == WITH_EXCEPT_START || (prevopcode == CALL_FUNCTION && prevprevopcode == DUP_TOP)) {
Victor Stinner438a12d2019-05-24 17:01:38 +02006344 _PyErr_Format(tstate, PyExc_TypeError,
6345 "'async with' received an object from __aexit__ "
6346 "that does not implement __await__: %.100s",
6347 type->tp_name);
Serhiy Storchakaa68f2f02018-04-03 01:41:38 +03006348 }
6349 }
6350}
6351
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006352static PyObject *
Victor Stinner438a12d2019-05-24 17:01:38 +02006353unicode_concatenate(PyThreadState *tstate, PyObject *v, PyObject *w,
Serhiy Storchakaab874002016-09-11 13:48:15 +03006354 PyFrameObject *f, const _Py_CODEUNIT *next_instr)
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006355{
6356 PyObject *res;
6357 if (Py_REFCNT(v) == 2) {
6358 /* In the common case, there are 2 references to the value
6359 * stored in 'variable' when the += is performed: one on the
6360 * value stack (in 'v') and one still stored in the
6361 * 'variable'. We try to delete the variable now to reduce
6362 * the refcnt to 1.
6363 */
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006364 int opcode, oparg;
6365 NEXTOPARG();
6366 switch (opcode) {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006367 case STORE_FAST:
6368 {
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006369 PyObject **fastlocals = f->f_localsplus;
6370 if (GETLOCAL(oparg) == v)
6371 SETLOCAL(oparg, NULL);
6372 break;
6373 }
6374 case STORE_DEREF:
6375 {
6376 PyObject **freevars = (f->f_localsplus +
6377 f->f_code->co_nlocals);
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006378 PyObject *c = freevars[oparg];
Raymond Hettingerc32f9db2016-11-12 04:10:35 -05006379 if (PyCell_GET(c) == v) {
6380 PyCell_SET(c, NULL);
6381 Py_DECREF(v);
6382 }
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006383 break;
6384 }
6385 case STORE_NAME:
6386 {
6387 PyObject *names = f->f_code->co_names;
Serhiy Storchakaf60bf5f2016-05-25 20:02:01 +03006388 PyObject *name = GETITEM(names, oparg);
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006389 PyObject *locals = f->f_locals;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006390 if (locals && PyDict_CheckExact(locals)) {
6391 PyObject *w = PyDict_GetItemWithError(locals, name);
6392 if ((w == v && PyDict_DelItem(locals, name) != 0) ||
Victor Stinner438a12d2019-05-24 17:01:38 +02006393 (w == NULL && _PyErr_Occurred(tstate)))
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02006394 {
6395 Py_DECREF(v);
6396 return NULL;
Victor Stinnerd2a915d2011-10-02 20:34:20 +02006397 }
6398 }
6399 break;
6400 }
6401 }
6402 }
6403 res = v;
6404 PyUnicode_Append(&res, w);
6405 return res;
6406}
6407
Guido van Rossum950361c1997-01-24 13:49:28 +00006408#ifdef DYNAMIC_EXECUTION_PROFILE
6409
Skip Montanarof118cb12001-10-15 20:51:38 +00006410static PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006411getarray(long a[256])
Guido van Rossum950361c1997-01-24 13:49:28 +00006412{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006413 int i;
6414 PyObject *l = PyList_New(256);
6415 if (l == NULL) return NULL;
6416 for (i = 0; i < 256; i++) {
6417 PyObject *x = PyLong_FromLong(a[i]);
6418 if (x == NULL) {
6419 Py_DECREF(l);
6420 return NULL;
6421 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006422 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006423 }
6424 for (i = 0; i < 256; i++)
6425 a[i] = 0;
6426 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006427}
6428
6429PyObject *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +00006430_Py_GetDXProfile(PyObject *self, PyObject *args)
Guido van Rossum950361c1997-01-24 13:49:28 +00006431{
6432#ifndef DXPAIRS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006433 return getarray(dxp);
Guido van Rossum950361c1997-01-24 13:49:28 +00006434#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006435 int i;
6436 PyObject *l = PyList_New(257);
6437 if (l == NULL) return NULL;
6438 for (i = 0; i < 257; i++) {
6439 PyObject *x = getarray(dxpairs[i]);
6440 if (x == NULL) {
6441 Py_DECREF(l);
6442 return NULL;
6443 }
Zackery Spytz99d56b52018-12-08 07:16:55 -07006444 PyList_SET_ITEM(l, i, x);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006445 }
6446 return l;
Guido van Rossum950361c1997-01-24 13:49:28 +00006447#endif
6448}
6449
6450#endif
Brett Cannon5c4de282016-09-07 11:16:41 -07006451
6452Py_ssize_t
6453_PyEval_RequestCodeExtraIndex(freefunc free)
6454{
Victor Stinner81a7be32020-04-14 15:14:01 +02006455 PyInterpreterState *interp = _PyInterpreterState_GET();
Brett Cannon5c4de282016-09-07 11:16:41 -07006456 Py_ssize_t new_index;
6457
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006458 if (interp->co_extra_user_count == MAX_CO_EXTRA_USERS - 1) {
Brett Cannon5c4de282016-09-07 11:16:41 -07006459 return -1;
6460 }
Dino Viehlandf3cffd22017-06-21 14:44:36 -07006461 new_index = interp->co_extra_user_count++;
6462 interp->co_extra_freefuncs[new_index] = free;
Brett Cannon5c4de282016-09-07 11:16:41 -07006463 return new_index;
6464}
Łukasz Langaa785c872016-09-09 17:37:37 -07006465
6466static void
6467dtrace_function_entry(PyFrameObject *f)
6468{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006469 const char *filename;
6470 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006471 int lineno;
6472
Victor Stinner6d86a232020-04-29 00:56:58 +02006473 PyCodeObject *code = f->f_code;
6474 filename = PyUnicode_AsUTF8(code->co_filename);
6475 funcname = PyUnicode_AsUTF8(code->co_name);
6476 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006477
Andy Lestere6be9b52020-02-11 20:28:35 -06006478 PyDTrace_FUNCTION_ENTRY(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006479}
6480
6481static void
6482dtrace_function_return(PyFrameObject *f)
6483{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006484 const char *filename;
6485 const char *funcname;
Łukasz Langaa785c872016-09-09 17:37:37 -07006486 int lineno;
6487
Victor Stinner6d86a232020-04-29 00:56:58 +02006488 PyCodeObject *code = f->f_code;
6489 filename = PyUnicode_AsUTF8(code->co_filename);
6490 funcname = PyUnicode_AsUTF8(code->co_name);
6491 lineno = PyCode_Addr2Line(code, f->f_lasti);
Łukasz Langaa785c872016-09-09 17:37:37 -07006492
Andy Lestere6be9b52020-02-11 20:28:35 -06006493 PyDTrace_FUNCTION_RETURN(filename, funcname, lineno);
Łukasz Langaa785c872016-09-09 17:37:37 -07006494}
6495
6496/* DTrace equivalent of maybe_call_line_trace. */
6497static void
6498maybe_dtrace_line(PyFrameObject *frame,
Mark Shannon8e1b4062021-03-05 14:45:50 +00006499 PyTraceInfo *trace_info)
Łukasz Langaa785c872016-09-09 17:37:37 -07006500{
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02006501 const char *co_filename, *co_name;
Łukasz Langaa785c872016-09-09 17:37:37 -07006502
6503 /* If the last instruction executed isn't in the current
6504 instruction window, reset the window.
6505 */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006506 initialize_trace_info(trace_info, frame);
6507 int line = _PyCode_CheckLineNumber(frame->f_lasti, &trace_info->bounds);
Łukasz Langaa785c872016-09-09 17:37:37 -07006508 /* If the last instruction falls at the start of a line or if
6509 it represents a jump backwards, update the frame's line
6510 number and call the trace function. */
Mark Shannon8e1b4062021-03-05 14:45:50 +00006511 if (line != frame->f_lineno || frame->f_lasti < trace_info->instr_prev) {
Mark Shannon877df852020-11-12 09:43:29 +00006512 if (line != -1) {
6513 frame->f_lineno = line;
6514 co_filename = PyUnicode_AsUTF8(frame->f_code->co_filename);
6515 if (!co_filename)
6516 co_filename = "?";
6517 co_name = PyUnicode_AsUTF8(frame->f_code->co_name);
6518 if (!co_name)
6519 co_name = "?";
6520 PyDTrace_LINE(co_filename, co_name, line);
6521 }
Łukasz Langaa785c872016-09-09 17:37:37 -07006522 }
Mark Shannon8e1b4062021-03-05 14:45:50 +00006523 trace_info->instr_prev = frame->f_lasti;
Łukasz Langaa785c872016-09-09 17:37:37 -07006524}
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006525
6526
6527/* Implement Py_EnterRecursiveCall() and Py_LeaveRecursiveCall() as functions
6528 for the limited API. */
6529
6530#undef Py_EnterRecursiveCall
6531
6532int Py_EnterRecursiveCall(const char *where)
6533{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006534 return _Py_EnterRecursiveCall_inline(where);
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006535}
6536
6537#undef Py_LeaveRecursiveCall
6538
6539void Py_LeaveRecursiveCall(void)
6540{
Victor Stinnerbe434dc2019-11-05 00:51:22 +01006541 _Py_LeaveRecursiveCall_inline();
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +01006542}